blob: 1c4263922b323b8165d0f683ffccc7c69cc7735f [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
58 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000059 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000060 int FrameIndex;
61 } Base;
62
63 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000065 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000066 SDValue Segment;
Dan Gohman46510a72010-04-15 01:51:59 +000067 const GlobalValue *GV;
68 const Constant *CP;
69 const BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000070 const char *ES;
71 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000072 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000073 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074
75 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000076 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000077 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000078 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000079 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000080
81 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000082 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000083 }
Chris Lattner18c59872009-06-27 04:16:01 +000084
85 bool hasBaseOrIndexReg() const {
86 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
87 }
88
89 /// isRIPRelative - Return true if this addressing mode is already RIP
90 /// relative.
91 bool isRIPRelative() const {
92 if (BaseType != RegBase) return false;
93 if (RegisterSDNode *RegNode =
94 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
95 return RegNode->getReg() == X86::RIP;
96 return false;
97 }
98
99 void setBaseReg(SDValue Reg) {
100 BaseType = RegBase;
101 Base.Reg = Reg;
102 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000103
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000104 void dump() {
David Greened7f4f242010-01-05 01:29:08 +0000105 dbgs() << "X86ISelAddressMode " << this << '\n';
106 dbgs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000107 if (Base.Reg.getNode() != 0)
108 Base.Reg.getNode()->dump();
109 else
David Greened7f4f242010-01-05 01:29:08 +0000110 dbgs() << "nul";
111 dbgs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000112 << " Scale" << Scale << '\n'
113 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000114 if (IndexReg.getNode() != 0)
115 IndexReg.getNode()->dump();
116 else
David Greened7f4f242010-01-05 01:29:08 +0000117 dbgs() << "nul";
118 dbgs() << " Disp " << Disp << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000119 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000120 if (GV)
121 GV->dump();
122 else
David Greened7f4f242010-01-05 01:29:08 +0000123 dbgs() << "nul";
124 dbgs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000125 if (CP)
126 CP->dump();
127 else
David Greened7f4f242010-01-05 01:29:08 +0000128 dbgs() << "nul";
129 dbgs() << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000130 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000131 if (ES)
David Greened7f4f242010-01-05 01:29:08 +0000132 dbgs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000133 else
David Greened7f4f242010-01-05 01:29:08 +0000134 dbgs() << "nul";
135 dbgs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000136 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000137 };
138}
139
140namespace {
Evan Chengf3caa522010-03-17 23:58:35 +0000141 class X86ISelListener : public SelectionDAG::DAGUpdateListener {
142 SmallSet<SDNode*, 4> Deletes;
143 public:
144 explicit X86ISelListener() {}
145 virtual void NodeDeleted(SDNode *N, SDNode *E) {
146 Deletes.insert(N);
147 }
148 virtual void NodeUpdated(SDNode *N) {
149 // Ignore updates.
150 }
151 bool IsDeleted(SDNode *N) {
152 return Deletes.count(N);
153 }
154 };
155
Chris Lattnerc961eea2005-11-16 01:54:32 +0000156 //===--------------------------------------------------------------------===//
157 /// ISel - X86 specific code to select X86 machine instructions for
158 /// SelectionDAG operations.
159 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000160 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000161 /// X86Lowering - This object fully describes how to lower LLVM code to an
162 /// X86-specific SelectionDAG.
Dan Gohmand858e902010-04-17 15:26:15 +0000163 const X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000164
165 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
166 /// make the right decision when generating code for different targets.
167 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000168
Evan Chengb7a75a52008-09-26 23:41:32 +0000169 /// OptForSize - If true, selector should try to optimize for code size
170 /// instead of performance.
171 bool OptForSize;
172
Chris Lattnerc961eea2005-11-16 01:54:32 +0000173 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000174 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000175 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000176 X86Lowering(*tm.getTargetLowering()),
177 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000178 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000179
180 virtual const char *getPassName() const {
181 return "X86 DAG->DAG Instruction Selection";
182 }
183
Dan Gohman64652652010-04-14 20:17:22 +0000184 virtual void EmitFunctionEntryCode();
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000185
Evan Cheng014bf212010-02-15 19:41:07 +0000186 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
187
Chris Lattner7c306da2010-03-02 06:34:30 +0000188 virtual void PreprocessISelDAG();
189
Chris Lattnerc961eea2005-11-16 01:54:32 +0000190// Include the pieces autogenerated from the target description.
191#include "X86GenDAGISel.inc"
192
193 private:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000194 SDNode *Select(SDNode *N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000195 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000196 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000197
Rafael Espindola094fad32009-04-08 21:14:34 +0000198 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
199 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000200 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000201 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
202 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
Evan Chengf3caa522010-03-17 23:58:35 +0000203 X86ISelListener &DeadNodes,
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000204 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000205 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000206 bool SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000207 SDValue &Scale, SDValue &Index, SDValue &Disp,
208 SDValue &Segment);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000209 bool SelectLEAAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000210 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000211 bool SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000212 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattnere60f7b42010-03-01 22:51:11 +0000213 bool SelectScalarSSELoad(SDNode *Root, SDValue N,
Chris Lattner92d3ada2010-02-16 22:35:06 +0000214 SDValue &Base, SDValue &Scale,
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000216 SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +0000217 SDValue &NodeWithChain);
218
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000219 bool TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000220 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000221 SDValue &Index, SDValue &Disp,
222 SDValue &Segment);
Chris Lattner7c306da2010-03-02 06:34:30 +0000223
Chris Lattnerc0bad572006-06-08 18:03:49 +0000224 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
225 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000226 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000227 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000228 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000229
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000230 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
231
Dan Gohman475871a2008-07-27 21:46:04 +0000232 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
233 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000234 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000235 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000236 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
237 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000238 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000239 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000240 // These are 32-bit even in 64-bit mode since RIP relative offset
241 // is 32-bit.
242 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000244 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000245 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000247 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000248 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000250 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000252 else if (AM.BlockAddr)
Dan Gohman29cbade2009-11-20 23:18:13 +0000253 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
254 true, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000255 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000257
258 if (AM.Segment.getNode())
259 Segment = AM.Segment;
260 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000262 }
263
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000264 /// getI8Imm - Return a target constant with the specified value, of type
265 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000266 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000268 }
269
Chris Lattnerc961eea2005-11-16 01:54:32 +0000270 /// getI16Imm - Return a target constant with the specified value, of type
271 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000272 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000274 }
275
276 /// getI32Imm - Return a target constant with the specified value, of type
277 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000278 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000279 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000280 }
Evan Chengf597dc72006-02-10 22:24:32 +0000281
Dan Gohman8b746962008-09-23 18:22:58 +0000282 /// getGlobalBaseReg - Return an SDNode that returns the value of
283 /// the global base register. Output instructions required to
284 /// initialize the global base register, if necessary.
285 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000286 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000287
Dan Gohmanc5534622009-06-03 20:20:00 +0000288 /// getTargetMachine - Return a reference to the TargetMachine, casted
289 /// to the target-specific type.
290 const X86TargetMachine &getTargetMachine() {
291 return static_cast<const X86TargetMachine &>(TM);
292 }
293
294 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
295 /// to the target-specific type.
296 const X86InstrInfo *getInstrInfo() {
297 return getTargetMachine().getInstrInfo();
298 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000299 };
300}
301
Evan Chengf4b4c412006-08-08 00:31:00 +0000302
Evan Cheng014bf212010-02-15 19:41:07 +0000303bool
304X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000305 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000306
Evan Cheng014bf212010-02-15 19:41:07 +0000307 if (!N.hasOneUse())
308 return false;
309
310 if (N.getOpcode() != ISD::LOAD)
311 return true;
312
313 // If N is a load, do additional profitability checks.
314 if (U == Root) {
Evan Cheng884c70c2008-11-27 00:49:46 +0000315 switch (U->getOpcode()) {
316 default: break;
Dan Gohman9ef51c82010-01-04 20:51:50 +0000317 case X86ISD::ADD:
318 case X86ISD::SUB:
319 case X86ISD::AND:
320 case X86ISD::XOR:
321 case X86ISD::OR:
Evan Cheng884c70c2008-11-27 00:49:46 +0000322 case ISD::ADD:
323 case ISD::ADDC:
324 case ISD::ADDE:
325 case ISD::AND:
326 case ISD::OR:
327 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000328 SDValue Op1 = U->getOperand(1);
329
Evan Cheng884c70c2008-11-27 00:49:46 +0000330 // If the other operand is a 8-bit immediate we should fold the immediate
331 // instead. This reduces code size.
332 // e.g.
333 // movl 4(%esp), %eax
334 // addl $4, %eax
335 // vs.
336 // movl $4, %eax
337 // addl 4(%esp), %eax
338 // The former is 2 bytes shorter. In case where the increment is 1, then
339 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000340 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000341 if (Imm->getAPIntValue().isSignedIntN(8))
342 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000343
344 // If the other operand is a TLS address, we should fold it instead.
345 // This produces
346 // movl %gs:0, %eax
347 // leal i@NTPOFF(%eax), %eax
348 // instead of
349 // movl $i@NTPOFF, %eax
350 // addl %gs:0, %eax
351 // if the block also has an access to a second TLS address this will save
352 // a load.
353 // FIXME: This is probably also true for non TLS addresses.
354 if (Op1.getOpcode() == X86ISD::Wrapper) {
355 SDValue Val = Op1.getOperand(0);
356 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
357 return false;
358 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000359 }
360 }
Evan Cheng014bf212010-02-15 19:41:07 +0000361 }
362
363 return true;
364}
365
Evan Chengf48ef032010-03-14 03:48:46 +0000366/// MoveBelowCallOrigChain - Replace the original chain operand of the call with
367/// load's chain operand and move load below the call's chain operand.
368static void MoveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load,
369 SDValue Call, SDValue OrigChain) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000370 SmallVector<SDValue, 8> Ops;
Evan Chengf48ef032010-03-14 03:48:46 +0000371 SDValue Chain = OrigChain.getOperand(0);
Evan Cheng5b2e5892009-01-26 18:43:34 +0000372 if (Chain.getNode() == Load.getNode())
373 Ops.push_back(Load.getOperand(0));
374 else {
375 assert(Chain.getOpcode() == ISD::TokenFactor &&
Evan Chengf48ef032010-03-14 03:48:46 +0000376 "Unexpected chain operand");
Evan Cheng5b2e5892009-01-26 18:43:34 +0000377 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
378 if (Chain.getOperand(i).getNode() == Load.getNode())
379 Ops.push_back(Load.getOperand(0));
380 else
381 Ops.push_back(Chain.getOperand(i));
382 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000383 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000385 Ops.clear();
386 Ops.push_back(NewChain);
387 }
Evan Chengf48ef032010-03-14 03:48:46 +0000388 for (unsigned i = 1, e = OrigChain.getNumOperands(); i != e; ++i)
389 Ops.push_back(OrigChain.getOperand(i));
390 CurDAG->UpdateNodeOperands(OrigChain, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000391 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
392 Load.getOperand(1), Load.getOperand(2));
393 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000394 Ops.push_back(SDValue(Load.getNode(), 1));
395 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000396 Ops.push_back(Call.getOperand(i));
397 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
398}
399
400/// isCalleeLoad - Return true if call address is a load and it can be
401/// moved below CALLSEQ_START and the chains leading up to the call.
402/// Return the CALLSEQ_START by reference as a second output.
Evan Chengf48ef032010-03-14 03:48:46 +0000403/// In the case of a tail call, there isn't a callseq node between the call
404/// chain and the load.
405static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000406 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000407 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000408 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000409 if (!LD ||
410 LD->isVolatile() ||
411 LD->getAddressingMode() != ISD::UNINDEXED ||
412 LD->getExtensionType() != ISD::NON_EXTLOAD)
413 return false;
414
415 // Now let's find the callseq_start.
Evan Chengf48ef032010-03-14 03:48:46 +0000416 while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000417 if (!Chain.hasOneUse())
418 return false;
419 Chain = Chain.getOperand(0);
420 }
Evan Chengf48ef032010-03-14 03:48:46 +0000421
422 if (!Chain.getNumOperands())
423 return false;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000424 if (Chain.getOperand(0).getNode() == Callee.getNode())
425 return true;
426 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000427 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
428 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000429 return true;
430 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000431}
432
Chris Lattnerfb444af2010-03-02 23:12:51 +0000433void X86DAGToDAGISel::PreprocessISelDAG() {
Chris Lattner97d85342010-03-04 01:43:43 +0000434 // OptForSize is used in pattern predicates that isel is matching.
Chris Lattnerfb444af2010-03-02 23:12:51 +0000435 OptForSize = MF->getFunction()->hasFnAttr(Attribute::OptimizeForSize);
436
Dan Gohmanf350b272008-08-23 02:25:05 +0000437 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
438 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000439 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
Chris Lattnerfb444af2010-03-02 23:12:51 +0000440
Evan Chengf48ef032010-03-14 03:48:46 +0000441 if (OptLevel != CodeGenOpt::None &&
442 (N->getOpcode() == X86ISD::CALL ||
443 N->getOpcode() == X86ISD::TC_RETURN)) {
Chris Lattnerfb444af2010-03-02 23:12:51 +0000444 /// Also try moving call address load from outside callseq_start to just
445 /// before the call to allow it to be folded.
446 ///
447 /// [Load chain]
448 /// ^
449 /// |
450 /// [Load]
451 /// ^ ^
452 /// | |
453 /// / \--
454 /// / |
455 ///[CALLSEQ_START] |
456 /// ^ |
457 /// | |
458 /// [LOAD/C2Reg] |
459 /// | |
460 /// \ /
461 /// \ /
462 /// [CALL]
Evan Chengf48ef032010-03-14 03:48:46 +0000463 bool HasCallSeq = N->getOpcode() == X86ISD::CALL;
Chris Lattnerfb444af2010-03-02 23:12:51 +0000464 SDValue Chain = N->getOperand(0);
465 SDValue Load = N->getOperand(1);
Evan Chengf48ef032010-03-14 03:48:46 +0000466 if (!isCalleeLoad(Load, Chain, HasCallSeq))
Chris Lattnerfb444af2010-03-02 23:12:51 +0000467 continue;
Evan Chengf48ef032010-03-14 03:48:46 +0000468 MoveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain);
Chris Lattnerfb444af2010-03-02 23:12:51 +0000469 ++NumLoadMoved;
470 continue;
471 }
472
473 // Lower fpround and fpextend nodes that target the FP stack to be store and
474 // load to the stack. This is a gross hack. We would like to simply mark
475 // these as being illegal, but when we do that, legalize produces these when
476 // it expands calls, then expands these in the same legalize pass. We would
477 // like dag combine to be able to hack on these between the call expansion
478 // and the node legalization. As such this pass basically does "really
479 // late" legalization of these inline with the X86 isel pass.
480 // FIXME: This should only happen when not compiled with -O0.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000481 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
482 continue;
483
484 // If the source and destination are SSE registers, then this is a legal
485 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000486 EVT SrcVT = N->getOperand(0).getValueType();
487 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000488 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
489 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
490 if (SrcIsSSE && DstIsSSE)
491 continue;
492
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000493 if (!SrcIsSSE && !DstIsSSE) {
494 // If this is an FPStack extension, it is a noop.
495 if (N->getOpcode() == ISD::FP_EXTEND)
496 continue;
497 // If this is a value-preserving FPStack truncation, it is a noop.
498 if (N->getConstantOperandVal(1))
499 continue;
500 }
501
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000502 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
503 // FPStack has extload and truncstore. SSE can fold direct loads into other
504 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000505 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000506 if (N->getOpcode() == ISD::FP_ROUND)
507 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
508 else
509 MemVT = SrcIsSSE ? SrcVT : DstVT;
510
Dan Gohmanf350b272008-08-23 02:25:05 +0000511 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000512 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000513
514 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000515 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000516 N->getOperand(0),
David Greenedb8d9892010-02-15 16:57:43 +0000517 MemTmp, NULL, 0, MemVT,
518 false, false, 0);
Dale Johannesend8392542009-02-03 21:48:12 +0000519 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
David Greenedb8d9892010-02-15 16:57:43 +0000520 NULL, 0, MemVT, false, false, 0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000521
522 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
523 // extload we created. This will cause general havok on the dag because
524 // anything below the conversion could be folded into other existing nodes.
525 // To avoid invalidating 'I', back it up to the convert node.
526 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000527 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000528
529 // Now that we did that, the node is dead. Increment the iterator to the
530 // next node to process, then delete N.
531 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000532 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000533 }
534}
535
Chris Lattnerc961eea2005-11-16 01:54:32 +0000536
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000537/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
538/// the main function.
539void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
540 MachineFrameInfo *MFI) {
541 const TargetInstrInfo *TII = TM.getInstrInfo();
542 if (Subtarget->isTargetCygMing())
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000543 BuildMI(BB, DebugLoc(),
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000544 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000545}
546
Dan Gohman64652652010-04-14 20:17:22 +0000547void X86DAGToDAGISel::EmitFunctionEntryCode() {
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000548 // If this is main, emit special code for main.
Dan Gohman64652652010-04-14 20:17:22 +0000549 if (const Function *Fn = MF->getFunction())
550 if (Fn->hasExternalLinkage() && Fn->getName() == "main")
551 EmitSpecialCodeForMain(MF->begin(), MF->getFrameInfo());
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000552}
553
Rafael Espindola094fad32009-04-08 21:14:34 +0000554
555bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
556 X86ISelAddressMode &AM) {
557 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
558 SDValue Segment = N.getOperand(0);
559
560 if (AM.Segment.getNode() == 0) {
561 AM.Segment = Segment;
562 return false;
563 }
564
565 return true;
566}
567
568bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
569 // This optimization is valid because the GNU TLS model defines that
570 // gs:0 (or fs:0 on X86-64) contains its own address.
571 // For more information see http://people.redhat.com/drepper/tls.pdf
572
573 SDValue Address = N.getOperand(1);
574 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
575 !MatchSegmentBaseAddress (Address, AM))
576 return false;
577
578 return true;
579}
580
Chris Lattner18c59872009-06-27 04:16:01 +0000581/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
582/// into an addressing mode. These wrap things that will resolve down into a
583/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000584/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000585bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000586 // If the addressing mode already has a symbol as the displacement, we can
587 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000588 if (AM.hasSymbolicDisplacement())
589 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000590
591 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000592 CodeModel::Model M = TM.getCodeModel();
593
Chris Lattner18c59872009-06-27 04:16:01 +0000594 // Handle X86-64 rip-relative addresses. We check this before checking direct
595 // folding because RIP is preferable to non-RIP accesses.
596 if (Subtarget->is64Bit() &&
597 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
598 // they cannot be folded into immediate fields.
599 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000600 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000601 // Base and index reg must be 0 in order to use %rip as base and lowering
602 // must allow RIP.
603 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000604 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
605 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000606 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000607 AM.GV = G->getGlobal();
608 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000609 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000610 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
611 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000612 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000613 AM.CP = CP->getConstVal();
614 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000615 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000616 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000617 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
618 AM.ES = S->getSymbol();
619 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000620 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000621 AM.JT = J->getIndex();
622 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000623 } else {
624 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000625 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000626 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000627
Chris Lattner18c59872009-06-27 04:16:01 +0000628 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000630 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000631 }
632
633 // Handle the case when globals fit in our immediate field: This is true for
634 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
635 // mode, this results in a non-RIP-relative computation.
636 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000637 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000638 TM.getRelocationModel() == Reloc::Static)) {
639 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
640 AM.GV = G->getGlobal();
641 AM.Disp += G->getOffset();
642 AM.SymbolFlags = G->getTargetFlags();
643 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
644 AM.CP = CP->getConstVal();
645 AM.Align = CP->getAlignment();
646 AM.Disp += CP->getOffset();
647 AM.SymbolFlags = CP->getTargetFlags();
648 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
649 AM.ES = S->getSymbol();
650 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000651 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000652 AM.JT = J->getIndex();
653 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000654 } else {
655 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000656 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000657 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000658 return false;
659 }
660
661 return true;
662}
663
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000664/// MatchAddress - Add the specified node to the specified addressing mode,
665/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000666/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000667bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
Evan Chengf3caa522010-03-17 23:58:35 +0000668 X86ISelListener DeadNodes;
669 if (MatchAddressRecursively(N, AM, DeadNodes, 0))
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000670 return true;
671
672 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
673 // a smaller encoding and avoids a scaled-index.
674 if (AM.Scale == 2 &&
675 AM.BaseType == X86ISelAddressMode::RegBase &&
676 AM.Base.Reg.getNode() == 0) {
677 AM.Base.Reg = AM.IndexReg;
678 AM.Scale = 1;
679 }
680
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000681 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
682 // because it has a smaller encoding.
683 // TODO: Which other code models can use this?
684 if (TM.getCodeModel() == CodeModel::Small &&
685 Subtarget->is64Bit() &&
686 AM.Scale == 1 &&
687 AM.BaseType == X86ISelAddressMode::RegBase &&
688 AM.Base.Reg.getNode() == 0 &&
689 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000690 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000691 AM.hasSymbolicDisplacement())
692 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
693
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000694 return false;
695}
696
Chris Lattnerd6139422010-04-20 23:18:40 +0000697/// isLogicallyAddWithConstant - Return true if this node is semantically an
698/// add of a value with a constantint.
699static bool isLogicallyAddWithConstant(SDValue V, SelectionDAG *CurDAG) {
700 // Check for (add x, Cst)
701 if (V->getOpcode() == ISD::ADD)
702 return isa<ConstantSDNode>(V->getOperand(1));
703
704 // Check for (or x, Cst), where Cst & x == 0.
705 if (V->getOpcode() != ISD::OR ||
706 !isa<ConstantSDNode>(V->getOperand(1)))
707 return false;
708
709 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
710 ConstantSDNode *CN = cast<ConstantSDNode>(V->getOperand(1));
711
712 // Check to see if the LHS & C is zero.
713 return CurDAG->MaskedValueIsZero(V->getOperand(0), CN->getAPIntValue());
714}
715
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000716bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
Evan Chengf3caa522010-03-17 23:58:35 +0000717 X86ISelListener &DeadNodes,
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000718 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000719 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000720 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000721 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +0000722 dbgs() << "MatchAddress: ";
Bill Wendling12321672009-08-07 21:33:25 +0000723 AM.dump();
724 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000725 // Limit recursion.
726 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000727 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000728
729 CodeModel::Model M = TM.getCodeModel();
730
Chris Lattner18c59872009-06-27 04:16:01 +0000731 // If this is already a %rip relative address, we can only merge immediates
732 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000733 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000734 if (AM.isRIPRelative()) {
735 // FIXME: JumpTable and ExternalSymbol address currently don't like
736 // displacements. It isn't very important, but this should be fixed for
737 // consistency.
738 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000739
Chris Lattner18c59872009-06-27 04:16:01 +0000740 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
741 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000742 if (X86::isOffsetSuitableForCodeModel(Val, M,
743 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000744 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000745 return false;
746 }
747 }
748 return true;
749 }
750
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000751 switch (N.getOpcode()) {
752 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000753 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000754 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000755 if (!is64Bit ||
756 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
757 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000758 AM.Disp += Val;
759 return false;
760 }
761 break;
762 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000763
Rafael Espindola094fad32009-04-08 21:14:34 +0000764 case X86ISD::SegmentBaseAddress:
765 if (!MatchSegmentBaseAddress(N, AM))
766 return false;
767 break;
768
Rafael Espindola49a168d2009-04-12 21:55:03 +0000769 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000770 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000771 if (!MatchWrapper(N, AM))
772 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000773 break;
774
Rafael Espindola094fad32009-04-08 21:14:34 +0000775 case ISD::LOAD:
776 if (!MatchLoad(N, AM))
777 return false;
778 break;
779
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000780 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000781 if (AM.BaseType == X86ISelAddressMode::RegBase
782 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000783 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
784 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
785 return false;
786 }
787 break;
Evan Chengec693f72005-12-08 02:01:35 +0000788
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000789 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000790 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000791 break;
792
Gabor Greif93c53e52008-08-31 15:37:04 +0000793 if (ConstantSDNode
794 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000795 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000796 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
797 // that the base operand remains free for further matching. If
798 // the base doesn't end up getting used, a post-processing step
799 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000800 if (Val == 1 || Val == 2 || Val == 3) {
801 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000802 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000803
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000804 // Okay, we know that we have a scale by now. However, if the scaled
805 // value is an add of something and a constant, we can fold the
806 // constant into the disp field here.
Chris Lattnerd6139422010-04-20 23:18:40 +0000807 if (isLogicallyAddWithConstant(ShVal, CurDAG)) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000808 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000809 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000810 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000811 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000812 if (!is64Bit ||
813 X86::isOffsetSuitableForCodeModel(Disp, M,
814 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000815 AM.Disp = Disp;
816 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000817 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000818 } else {
819 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000820 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000821 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000822 }
823 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000824 }
Evan Chengec693f72005-12-08 02:01:35 +0000825
Dan Gohman83688052007-10-22 20:22:24 +0000826 case ISD::SMUL_LOHI:
827 case ISD::UMUL_LOHI:
828 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000829 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000830 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000831 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000832 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000833 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000834 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000835 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000836 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000837 if (ConstantSDNode
838 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000839 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
840 CN->getZExtValue() == 9) {
841 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000842
Gabor Greifba36cb52008-08-28 21:40:38 +0000843 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000844 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000845
846 // Okay, we know that we have a scale by now. However, if the scaled
847 // value is an add of something and a constant, we can fold the
848 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000849 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
850 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
851 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000852 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000853 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000854 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000855 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000856 if (!is64Bit ||
857 X86::isOffsetSuitableForCodeModel(Disp, M,
858 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000859 AM.Disp = Disp;
860 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000861 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000862 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000863 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000864 }
865
866 AM.IndexReg = AM.Base.Reg = Reg;
867 return false;
868 }
Chris Lattner62412262007-02-04 20:18:17 +0000869 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000870 break;
871
Dan Gohman3cd90a12009-05-11 18:02:53 +0000872 case ISD::SUB: {
873 // Given A-B, if A can be completely folded into the address and
874 // the index field with the index field unused, use -B as the index.
875 // This is a win if a has multiple parts that can be folded into
876 // the address. Also, this saves a mov if the base register has
877 // other uses, since it avoids a two-address sub instruction, however
878 // it costs an additional mov if the index register has other uses.
879
880 // Test if the LHS of the sub can be folded.
881 X86ISelAddressMode Backup = AM;
Evan Chengf3caa522010-03-17 23:58:35 +0000882 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM,
883 DeadNodes, Depth+1) ||
884 // If it is successful but the recursive update causes N to be deleted,
885 // then it's not safe to continue.
886 DeadNodes.IsDeleted(N.getNode())) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000887 AM = Backup;
888 break;
889 }
890 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000891 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000892 AM = Backup;
893 break;
894 }
Evan Chengf3caa522010-03-17 23:58:35 +0000895
Dan Gohman3cd90a12009-05-11 18:02:53 +0000896 int Cost = 0;
897 SDValue RHS = N.getNode()->getOperand(1);
898 // If the RHS involves a register with multiple uses, this
899 // transformation incurs an extra mov, due to the neg instruction
900 // clobbering its operand.
901 if (!RHS.getNode()->hasOneUse() ||
902 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
903 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
904 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
905 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000906 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +0000907 ++Cost;
908 // If the base is a register with multiple uses, this
909 // transformation may save a mov.
910 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
911 AM.Base.Reg.getNode() &&
912 !AM.Base.Reg.getNode()->hasOneUse()) ||
913 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
914 --Cost;
915 // If the folded LHS was interesting, this transformation saves
916 // address arithmetic.
917 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
918 ((AM.Disp != 0) && (Backup.Disp == 0)) +
919 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
920 --Cost;
921 // If it doesn't look like it may be an overall win, don't do it.
922 if (Cost >= 0) {
923 AM = Backup;
924 break;
925 }
926
927 // Ok, the transformation is legal and appears profitable. Go for it.
928 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
929 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
930 AM.IndexReg = Neg;
931 AM.Scale = 1;
932
933 // Insert the new nodes into the topological ordering.
934 if (Zero.getNode()->getNodeId() == -1 ||
935 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
936 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
937 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
938 }
939 if (Neg.getNode()->getNodeId() == -1 ||
940 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
941 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
942 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
943 }
944 return false;
945 }
946
Evan Cheng8e278262009-01-17 07:09:27 +0000947 case ISD::ADD: {
948 X86ISelAddressMode Backup = AM;
Evan Chengf3caa522010-03-17 23:58:35 +0000949 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM,
950 DeadNodes, Depth+1)) {
951 if (DeadNodes.IsDeleted(N.getNode()))
952 // If it is successful but the recursive update causes N to be deleted,
953 // then it's not safe to continue.
954 return true;
955 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM,
956 DeadNodes, Depth+1))
957 // If it is successful but the recursive update causes N to be deleted,
958 // then it's not safe to continue.
959 return DeadNodes.IsDeleted(N.getNode());
960 }
961
962 // Try again after commuting the operands.
Evan Cheng8e278262009-01-17 07:09:27 +0000963 AM = Backup;
Evan Chengf3caa522010-03-17 23:58:35 +0000964 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM,
965 DeadNodes, Depth+1)) {
966 if (DeadNodes.IsDeleted(N.getNode()))
967 // If it is successful but the recursive update causes N to be deleted,
968 // then it's not safe to continue.
969 return true;
970 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM,
971 DeadNodes, Depth+1))
972 // If it is successful but the recursive update causes N to be deleted,
973 // then it's not safe to continue.
974 return DeadNodes.IsDeleted(N.getNode());
975 }
Evan Cheng8e278262009-01-17 07:09:27 +0000976 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000977
978 // If we couldn't fold both operands into the address at the same time,
979 // see if we can just put each operand into a register and fold at least
980 // the add.
981 if (AM.BaseType == X86ISelAddressMode::RegBase &&
982 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +0000983 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +0000984 AM.Base.Reg = N.getNode()->getOperand(0);
985 AM.IndexReg = N.getNode()->getOperand(1);
986 AM.Scale = 1;
987 return false;
988 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000989 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000990 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000991
Chris Lattner62412262007-02-04 20:18:17 +0000992 case ISD::OR:
993 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattnerd6139422010-04-20 23:18:40 +0000994 if (isLogicallyAddWithConstant(N, CurDAG)) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000995 X86ISelAddressMode Backup = AM;
Chris Lattnerd6139422010-04-20 23:18:40 +0000996 ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1));
Dan Gohman27cae7b2008-11-11 15:52:29 +0000997 uint64_t Offset = CN->getSExtValue();
Evan Chengf3caa522010-03-17 23:58:35 +0000998
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000999 // Start with the LHS as an addr mode.
Evan Chengf3caa522010-03-17 23:58:35 +00001000 if (!MatchAddressRecursively(N.getOperand(0), AM, DeadNodes, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001001 // Address could not have picked a GV address for the displacement.
1002 AM.GV == NULL &&
1003 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001004 (!is64Bit ||
1005 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
Evan Chengf3caa522010-03-17 23:58:35 +00001006 AM.hasSymbolicDisplacement()))) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001007 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001008 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001009 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001010 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001011 }
1012 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001013
1014 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001015 // Perform some heroic transforms on an and of a constant-count shift
1016 // with a constant to enable use of the scaled offset field.
1017
Dan Gohman475871a2008-07-27 21:46:04 +00001018 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001019 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001020
Evan Cheng1314b002007-12-13 00:43:27 +00001021 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001022 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001023
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001024 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001025 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1026 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1027 if (!C1 || !C2) break;
1028
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001029 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1030 // allows us to convert the shift and and into an h-register extract and
1031 // a scaled index.
1032 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1033 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001034 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001035 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001036 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001037 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1038 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1039 X, Eight);
1040 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1041 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001042 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001043 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1044 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001045
1046 // Insert the new nodes into the topological ordering.
1047 if (Eight.getNode()->getNodeId() == -1 ||
1048 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1049 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1050 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1051 }
1052 if (Mask.getNode()->getNodeId() == -1 ||
1053 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1054 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1055 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1056 }
1057 if (Srl.getNode()->getNodeId() == -1 ||
1058 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1059 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1060 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1061 }
1062 if (And.getNode()->getNodeId() == -1 ||
1063 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1064 CurDAG->RepositionNode(N.getNode(), And.getNode());
1065 And.getNode()->setNodeId(N.getNode()->getNodeId());
1066 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001067 if (ShlCount.getNode()->getNodeId() == -1 ||
1068 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1069 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1070 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1071 }
1072 if (Shl.getNode()->getNodeId() == -1 ||
1073 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1074 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1075 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1076 }
Evan Chengf3caa522010-03-17 23:58:35 +00001077 CurDAG->ReplaceAllUsesWith(N, Shl, &DeadNodes);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001078 AM.IndexReg = And;
1079 AM.Scale = (1 << ScaleLog);
1080 return false;
1081 }
1082 }
1083
1084 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1085 // allows us to fold the shift into this addressing mode.
1086 if (Shift.getOpcode() != ISD::SHL) break;
1087
Evan Cheng1314b002007-12-13 00:43:27 +00001088 // Not likely to be profitable if either the AND or SHIFT node has more
1089 // than one use (unless all uses are for address computation). Besides,
1090 // isel mechanism requires their node ids to be reused.
1091 if (!N.hasOneUse() || !Shift.hasOneUse())
1092 break;
1093
1094 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001095 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001096 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1097 break;
1098
1099 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001100 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001101 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001102 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1103 NewANDMask);
1104 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001105 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001106
1107 // Insert the new nodes into the topological ordering.
1108 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1109 CurDAG->RepositionNode(X.getNode(), C1);
1110 C1->setNodeId(X.getNode()->getNodeId());
1111 }
1112 if (NewANDMask.getNode()->getNodeId() == -1 ||
1113 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1114 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1115 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1116 }
1117 if (NewAND.getNode()->getNodeId() == -1 ||
1118 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1119 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1120 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1121 }
1122 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1123 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1124 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1125 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1126 }
1127
Evan Chengf3caa522010-03-17 23:58:35 +00001128 CurDAG->ReplaceAllUsesWith(N, NewSHIFT, &DeadNodes);
Evan Cheng1314b002007-12-13 00:43:27 +00001129
1130 AM.Scale = 1 << ShiftCst;
1131 AM.IndexReg = NewAND;
1132 return false;
1133 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001134 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001135
Rafael Espindola523249f2009-03-31 16:16:57 +00001136 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001137}
1138
1139/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1140/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001141bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001142 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001143 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001144 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001145 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001146 AM.IndexReg = N;
1147 AM.Scale = 1;
1148 return false;
1149 }
1150
1151 // Otherwise, we cannot select it.
1152 return true;
1153 }
1154
1155 // Default, generate it as a register.
1156 AM.BaseType = X86ISelAddressMode::RegBase;
1157 AM.Base.Reg = N;
1158 return false;
1159}
1160
Evan Chengec693f72005-12-08 02:01:35 +00001161/// SelectAddr - returns true if it is able pattern match an addressing mode.
1162/// It returns the operands which make up the maximal addressing mode it can
1163/// match by reference.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001164bool X86DAGToDAGISel::SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +00001165 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001166 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001167 X86ISelAddressMode AM;
Evan Chengc7928f82009-12-18 01:59:21 +00001168 if (MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001169 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001170
Owen Andersone50ed302009-08-10 22:56:29 +00001171 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001172 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001173 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001174 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001175 }
Evan Cheng8700e142006-01-11 06:09:51 +00001176
Gabor Greifba36cb52008-08-28 21:40:38 +00001177 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001178 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001179
Rafael Espindola094fad32009-04-08 21:14:34 +00001180 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001181 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001182}
1183
Chris Lattner3a7cd952006-10-07 21:55:32 +00001184/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1185/// match a load whose top elements are either undef or zeros. The load flavor
1186/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner64b49862010-02-17 06:07:47 +00001187///
1188/// We also return:
Chris Lattnera170b5e2010-02-21 03:17:59 +00001189/// PatternChainNode: this is the matched node that has a chain input and
1190/// output.
Chris Lattnere60f7b42010-03-01 22:51:11 +00001191bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
Dan Gohman475871a2008-07-27 21:46:04 +00001192 SDValue N, SDValue &Base,
1193 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001194 SDValue &Disp, SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +00001195 SDValue &PatternNodeWithChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001196 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001197 PatternNodeWithChain = N.getOperand(0);
1198 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1199 PatternNodeWithChain.hasOneUse() &&
Chris Lattnerf1c64282010-02-21 04:53:34 +00001200 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohmand858e902010-04-17 15:26:15 +00001201 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001202 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattner92d3ada2010-02-16 22:35:06 +00001203 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp,Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001204 return false;
1205 return true;
1206 }
1207 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001208
1209 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001210 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001211 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001212 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001213 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001214 N.getOperand(0).getNode()->hasOneUse() &&
1215 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattner92d3ada2010-02-16 22:35:06 +00001216 N.getOperand(0).getOperand(0).hasOneUse() &&
1217 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohmand858e902010-04-17 15:26:15 +00001218 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00001219 // Okay, this is a zero extending load. Fold it.
1220 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattner92d3ada2010-02-16 22:35:06 +00001221 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001222 return false;
Chris Lattnera170b5e2010-02-21 03:17:59 +00001223 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001224 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001225 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001226 return false;
1227}
1228
1229
Evan Cheng51a9ed92006-02-25 10:09:08 +00001230/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1231/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001232bool X86DAGToDAGISel::SelectLEAAddr(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001233 SDValue &Base, SDValue &Scale,
1234 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001235 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001236
1237 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1238 // segments.
1239 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001241 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001242 if (MatchAddress(N, AM))
1243 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001244 assert (T == AM.Segment);
1245 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001246
Owen Andersone50ed302009-08-10 22:56:29 +00001247 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001248 unsigned Complexity = 0;
1249 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001250 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001251 Complexity = 1;
1252 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001253 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001254 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1255 Complexity = 4;
1256
Gabor Greifba36cb52008-08-28 21:40:38 +00001257 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001258 Complexity++;
1259 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001260 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001261
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001262 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1263 // a simple shift.
1264 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001265 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001266
1267 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1268 // to a LEA. This is determined with some expermentation but is by no means
1269 // optimal (especially for code size consideration). LEA is nice because of
1270 // its three-address nature. Tweak the cost function again when we can run
1271 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001272 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001273 // For X86-64, we should always use lea to materialize RIP relative
1274 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001275 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001276 Complexity = 4;
1277 else
1278 Complexity += 2;
1279 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001280
Gabor Greifba36cb52008-08-28 21:40:38 +00001281 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001282 Complexity++;
1283
Chris Lattner25142782009-07-11 22:50:33 +00001284 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001285 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001286 return false;
1287
1288 SDValue Segment;
1289 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1290 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001291}
1292
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001293/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001294bool X86DAGToDAGISel::SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001295 SDValue &Scale, SDValue &Index,
1296 SDValue &Disp) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001297 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1298 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1299
1300 X86ISelAddressMode AM;
1301 AM.GV = GA->getGlobal();
1302 AM.Disp += GA->getOffset();
1303 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001304 AM.SymbolFlags = GA->getTargetFlags();
1305
Owen Anderson825b72b2009-08-11 20:47:22 +00001306 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001307 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001308 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001309 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001310 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001311 }
1312
1313 SDValue Segment;
1314 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1315 return true;
1316}
1317
1318
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001319bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001321 SDValue &Index, SDValue &Disp,
1322 SDValue &Segment) {
Chris Lattnerd1b73822010-03-02 22:20:06 +00001323 if (!ISD::isNON_EXTLoad(N.getNode()) ||
1324 !IsProfitableToFold(N, P, P) ||
Dan Gohmand858e902010-04-17 15:26:15 +00001325 !IsLegalToFold(N, P, P, OptLevel))
Chris Lattnerd1b73822010-03-02 22:20:06 +00001326 return false;
1327
1328 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001329}
1330
Dan Gohman8b746962008-09-23 18:22:58 +00001331/// getGlobalBaseReg - Return an SDNode that returns the value of
1332/// the global base register. Output instructions required to
1333/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001334///
Evan Cheng9ade2182006-08-26 05:34:46 +00001335SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001336 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001337 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001338}
1339
Evan Chengb245d922006-05-20 01:36:52 +00001340static SDNode *FindCallStartFromCall(SDNode *Node) {
1341 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001343 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001344 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001345}
1346
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001347SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1348 SDValue Chain = Node->getOperand(0);
1349 SDValue In1 = Node->getOperand(1);
1350 SDValue In2L = Node->getOperand(2);
1351 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001352 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001353 if (!SelectAddr(In1.getNode(), In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001354 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001355 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1356 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1357 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1358 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1359 MVT::i32, MVT::i32, MVT::Other, Ops,
1360 array_lengthof(Ops));
1361 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1362 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001363}
Christopher Lambc59e5212007-08-10 21:48:46 +00001364
Owen Andersone50ed302009-08-10 22:56:29 +00001365SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001366 if (Node->hasAnyUseOfValue(0))
1367 return 0;
1368
1369 // Optimize common patterns for __sync_add_and_fetch and
1370 // __sync_sub_and_fetch where the result is not used. This allows us
1371 // to use "lock" version of add, sub, inc, dec instructions.
1372 // FIXME: Do not use special instructions but instead add the "lock"
1373 // prefix to the target node somehow. The extra information will then be
1374 // transferred to machine instruction and it denotes the prefix.
1375 SDValue Chain = Node->getOperand(0);
1376 SDValue Ptr = Node->getOperand(1);
1377 SDValue Val = Node->getOperand(2);
1378 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001379 if (!SelectAddr(Ptr.getNode(), Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Evan Cheng37b73872009-07-30 08:33:02 +00001380 return 0;
1381
1382 bool isInc = false, isDec = false, isSub = false, isCN = false;
1383 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1384 if (CN) {
1385 isCN = true;
1386 int64_t CNVal = CN->getSExtValue();
1387 if (CNVal == 1)
1388 isInc = true;
1389 else if (CNVal == -1)
1390 isDec = true;
1391 else if (CNVal >= 0)
1392 Val = CurDAG->getTargetConstant(CNVal, NVT);
1393 else {
1394 isSub = true;
1395 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1396 }
1397 } else if (Val.hasOneUse() &&
1398 Val.getOpcode() == ISD::SUB &&
1399 X86::isZeroNode(Val.getOperand(0))) {
1400 isSub = true;
1401 Val = Val.getOperand(1);
1402 }
1403
1404 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001405 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001406 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001407 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001408 if (isInc)
1409 Opc = X86::LOCK_INC8m;
1410 else if (isDec)
1411 Opc = X86::LOCK_DEC8m;
1412 else if (isSub) {
1413 if (isCN)
1414 Opc = X86::LOCK_SUB8mi;
1415 else
1416 Opc = X86::LOCK_SUB8mr;
1417 } else {
1418 if (isCN)
1419 Opc = X86::LOCK_ADD8mi;
1420 else
1421 Opc = X86::LOCK_ADD8mr;
1422 }
1423 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001424 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001425 if (isInc)
1426 Opc = X86::LOCK_INC16m;
1427 else if (isDec)
1428 Opc = X86::LOCK_DEC16m;
1429 else if (isSub) {
1430 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001431 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001432 Opc = X86::LOCK_SUB16mi8;
1433 else
1434 Opc = X86::LOCK_SUB16mi;
1435 } else
1436 Opc = X86::LOCK_SUB16mr;
1437 } else {
1438 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001439 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001440 Opc = X86::LOCK_ADD16mi8;
1441 else
1442 Opc = X86::LOCK_ADD16mi;
1443 } else
1444 Opc = X86::LOCK_ADD16mr;
1445 }
1446 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001447 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001448 if (isInc)
1449 Opc = X86::LOCK_INC32m;
1450 else if (isDec)
1451 Opc = X86::LOCK_DEC32m;
1452 else if (isSub) {
1453 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001454 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001455 Opc = X86::LOCK_SUB32mi8;
1456 else
1457 Opc = X86::LOCK_SUB32mi;
1458 } else
1459 Opc = X86::LOCK_SUB32mr;
1460 } else {
1461 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001462 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001463 Opc = X86::LOCK_ADD32mi8;
1464 else
1465 Opc = X86::LOCK_ADD32mi;
1466 } else
1467 Opc = X86::LOCK_ADD32mr;
1468 }
1469 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001470 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001471 if (isInc)
1472 Opc = X86::LOCK_INC64m;
1473 else if (isDec)
1474 Opc = X86::LOCK_DEC64m;
1475 else if (isSub) {
1476 Opc = X86::LOCK_SUB64mr;
1477 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001478 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001479 Opc = X86::LOCK_SUB64mi8;
1480 else if (Predicate_i64immSExt32(Val.getNode()))
1481 Opc = X86::LOCK_SUB64mi32;
1482 }
1483 } else {
1484 Opc = X86::LOCK_ADD64mr;
1485 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001486 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001487 Opc = X86::LOCK_ADD64mi8;
1488 else if (Predicate_i64immSExt32(Val.getNode()))
1489 Opc = X86::LOCK_ADD64mi32;
1490 }
1491 }
1492 break;
1493 }
1494
1495 DebugLoc dl = Node->getDebugLoc();
Chris Lattner518bb532010-02-09 19:54:29 +00001496 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +00001497 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001498 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1499 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001500 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001501 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1502 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1503 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001504 SDValue RetVals[] = { Undef, Ret };
1505 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1506 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001507 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1508 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1509 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001510 SDValue RetVals[] = { Undef, Ret };
1511 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1512 }
1513}
1514
Dan Gohman11596ed2009-10-09 20:35:19 +00001515/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1516/// any uses which require the SF or OF bits to be accurate.
1517static bool HasNoSignedComparisonUses(SDNode *N) {
1518 // Examine each user of the node.
1519 for (SDNode::use_iterator UI = N->use_begin(),
1520 UE = N->use_end(); UI != UE; ++UI) {
1521 // Only examine CopyToReg uses.
1522 if (UI->getOpcode() != ISD::CopyToReg)
1523 return false;
1524 // Only examine CopyToReg uses that copy to EFLAGS.
1525 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1526 X86::EFLAGS)
1527 return false;
1528 // Examine each user of the CopyToReg use.
1529 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1530 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1531 // Only examine the Flag result.
1532 if (FlagUI.getUse().getResNo() != 1) continue;
1533 // Anything unusual: assume conservatively.
1534 if (!FlagUI->isMachineOpcode()) return false;
1535 // Examine the opcode of the user.
1536 switch (FlagUI->getMachineOpcode()) {
1537 // These comparisons don't treat the most significant bit specially.
1538 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1539 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1540 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1541 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001542 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1543 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
Dan Gohman11596ed2009-10-09 20:35:19 +00001544 case X86::CMOVA16rr: case X86::CMOVA16rm:
1545 case X86::CMOVA32rr: case X86::CMOVA32rm:
1546 case X86::CMOVA64rr: case X86::CMOVA64rm:
1547 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1548 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1549 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1550 case X86::CMOVB16rr: case X86::CMOVB16rm:
1551 case X86::CMOVB32rr: case X86::CMOVB32rm:
1552 case X86::CMOVB64rr: case X86::CMOVB64rm:
1553 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1554 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1555 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1556 case X86::CMOVE16rr: case X86::CMOVE16rm:
1557 case X86::CMOVE32rr: case X86::CMOVE32rm:
1558 case X86::CMOVE64rr: case X86::CMOVE64rm:
1559 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1560 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1561 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1562 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1563 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1564 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1565 case X86::CMOVP16rr: case X86::CMOVP16rm:
1566 case X86::CMOVP32rr: case X86::CMOVP32rm:
1567 case X86::CMOVP64rr: case X86::CMOVP64rm:
1568 continue;
1569 // Anything else: assume conservatively.
1570 default: return false;
1571 }
1572 }
1573 }
1574 return true;
1575}
1576
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001577SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Owen Andersone50ed302009-08-10 22:56:29 +00001578 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001579 unsigned Opc, MOpc;
1580 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001581 DebugLoc dl = Node->getDebugLoc();
1582
Chris Lattner7c306da2010-03-02 06:34:30 +00001583 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
Evan Chengf597dc72006-02-10 22:24:32 +00001584
Dan Gohmane8be6c62008-07-17 19:10:17 +00001585 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +00001586 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001587 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001588 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001589
Evan Cheng0114e942006-01-06 20:36:21 +00001590 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001591 default: break;
1592 case X86ISD::GlobalBaseReg:
1593 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001594
Dan Gohman72677342009-08-02 16:10:52 +00001595 case X86ISD::ATOMOR64_DAG:
1596 return SelectAtomic64(Node, X86::ATOMOR6432);
1597 case X86ISD::ATOMXOR64_DAG:
1598 return SelectAtomic64(Node, X86::ATOMXOR6432);
1599 case X86ISD::ATOMADD64_DAG:
1600 return SelectAtomic64(Node, X86::ATOMADD6432);
1601 case X86ISD::ATOMSUB64_DAG:
1602 return SelectAtomic64(Node, X86::ATOMSUB6432);
1603 case X86ISD::ATOMNAND64_DAG:
1604 return SelectAtomic64(Node, X86::ATOMNAND6432);
1605 case X86ISD::ATOMAND64_DAG:
1606 return SelectAtomic64(Node, X86::ATOMAND6432);
1607 case X86ISD::ATOMSWAP64_DAG:
1608 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001609
Dan Gohman72677342009-08-02 16:10:52 +00001610 case ISD::ATOMIC_LOAD_ADD: {
1611 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1612 if (RetVal)
1613 return RetVal;
1614 break;
1615 }
1616
1617 case ISD::SMUL_LOHI:
1618 case ISD::UMUL_LOHI: {
1619 SDValue N0 = Node->getOperand(0);
1620 SDValue N1 = Node->getOperand(1);
1621
1622 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001623 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001624 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001625 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1627 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1628 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1629 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001630 }
Bill Wendling12321672009-08-07 21:33:25 +00001631 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001632 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001633 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001634 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1635 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1636 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1637 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001638 }
Bill Wendling12321672009-08-07 21:33:25 +00001639 }
Dan Gohman72677342009-08-02 16:10:52 +00001640
1641 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001642 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001643 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001644 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1645 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1646 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1647 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001648 }
1649
1650 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001651 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001652 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001653 if (!foldedLoad) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001654 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001655 if (foldedLoad)
1656 std::swap(N0, N1);
1657 }
1658
1659 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1660 N0, SDValue()).getValue(1);
1661
1662 if (foldedLoad) {
1663 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1664 InFlag };
1665 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001666 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1667 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001668 InFlag = SDValue(CNode, 1);
1669 // Update the chain.
1670 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1671 } else {
1672 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001673 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001674 }
1675
1676 // Copy the low half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001677 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001678 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1679 LoReg, NVT, InFlag);
1680 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001681 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001682 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001683 }
1684 // Copy the high half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001685 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001686 SDValue Result;
1687 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1688 // Prevent use of AH in a REX instruction by referencing AX instead.
1689 // Shift it down 8 bits.
1690 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001691 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001692 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001693 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1694 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001696 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001697 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1698 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001699 } else {
1700 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1701 HiReg, NVT, InFlag);
1702 InFlag = Result.getValue(2);
1703 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001704 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001705 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001706 }
1707
Dan Gohman72677342009-08-02 16:10:52 +00001708 return NULL;
1709 }
1710
1711 case ISD::SDIVREM:
1712 case ISD::UDIVREM: {
1713 SDValue N0 = Node->getOperand(0);
1714 SDValue N1 = Node->getOperand(1);
1715
1716 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001717 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001718 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001719 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001720 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1721 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1722 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1723 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001724 }
Bill Wendling12321672009-08-07 21:33:25 +00001725 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001726 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001727 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1729 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1730 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1731 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001732 }
Bill Wendling12321672009-08-07 21:33:25 +00001733 }
Dan Gohman72677342009-08-02 16:10:52 +00001734
Chris Lattner9e323832009-12-23 01:45:04 +00001735 unsigned LoReg, HiReg, ClrReg;
Dan Gohman72677342009-08-02 16:10:52 +00001736 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001737 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001738 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001739 case MVT::i8:
Chris Lattner9e323832009-12-23 01:45:04 +00001740 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman72677342009-08-02 16:10:52 +00001741 ClrOpcode = 0;
1742 SExtOpcode = X86::CBW;
1743 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001744 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001745 LoReg = X86::AX; HiReg = X86::DX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001746 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
Dan Gohman72677342009-08-02 16:10:52 +00001747 SExtOpcode = X86::CWD;
1748 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001749 case MVT::i32:
Chris Lattner9e323832009-12-23 01:45:04 +00001750 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman72677342009-08-02 16:10:52 +00001751 ClrOpcode = X86::MOV32r0;
1752 SExtOpcode = X86::CDQ;
1753 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 case MVT::i64:
Chris Lattner9e323832009-12-23 01:45:04 +00001755 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001756 ClrOpcode = X86::MOV64r0;
Dan Gohman72677342009-08-02 16:10:52 +00001757 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001758 break;
1759 }
1760
Dan Gohman72677342009-08-02 16:10:52 +00001761 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001762 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001763 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001764
Dan Gohman72677342009-08-02 16:10:52 +00001765 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001767 // Special case for div8, just use a move with zero extension to AX to
1768 // clear the upper 8 bits (AH).
1769 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001770 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman72677342009-08-02 16:10:52 +00001771 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1772 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001773 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1774 MVT::Other, Ops,
1775 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001776 Chain = Move.getValue(1);
1777 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001778 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001779 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001780 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001781 Chain = CurDAG->getEntryNode();
1782 }
1783 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1784 InFlag = Chain.getValue(1);
1785 } else {
1786 InFlag =
1787 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1788 LoReg, N0, SDValue()).getValue(1);
1789 if (isSigned && !signBitIsZero) {
1790 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001791 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001792 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001793 } else {
1794 // Zero out the high part, effectively zero extending the input.
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001795 SDValue ClrNode =
1796 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Chris Lattner9e323832009-12-23 01:45:04 +00001797 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman72677342009-08-02 16:10:52 +00001798 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001799 }
Evan Cheng948f3432006-01-06 23:19:29 +00001800 }
Dan Gohman525178c2007-10-08 18:33:35 +00001801
Dan Gohman72677342009-08-02 16:10:52 +00001802 if (foldedLoad) {
1803 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1804 InFlag };
1805 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001806 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1807 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001808 InFlag = SDValue(CNode, 1);
1809 // Update the chain.
1810 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1811 } else {
1812 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001813 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001814 }
Evan Cheng948f3432006-01-06 23:19:29 +00001815
Dan Gohman72677342009-08-02 16:10:52 +00001816 // Copy the division (low) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001817 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001818 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1819 LoReg, NVT, InFlag);
1820 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001821 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001822 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001823 }
1824 // Copy the remainder (high) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001825 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001826 SDValue Result;
1827 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1828 // Prevent use of AH in a REX instruction by referencing AX instead.
1829 // Shift it down 8 bits.
1830 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001831 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001832 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001833 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001834 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001835 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001836 0);
1837 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001838 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1839 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001840 } else {
1841 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1842 HiReg, NVT, InFlag);
1843 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001844 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001845 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001846 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001847 }
Dan Gohman72677342009-08-02 16:10:52 +00001848 return NULL;
1849 }
1850
Dan Gohman6a402dc2009-08-19 18:16:17 +00001851 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001852 SDValue N0 = Node->getOperand(0);
1853 SDValue N1 = Node->getOperand(1);
1854
1855 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
1856 // use a smaller encoding.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00001857 if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse())
1858 // Look past the truncate if CMP is the only use of it.
1859 N0 = N0.getOperand(0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001860 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
1861 N0.getValueType() != MVT::i8 &&
1862 X86::isZeroNode(N1)) {
1863 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
1864 if (!C) break;
1865
1866 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00001867 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
1868 (!(C->getZExtValue() & 0x80) ||
1869 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001870 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
1871 SDValue Reg = N0.getNode()->getOperand(0);
1872
1873 // On x86-32, only the ABCD registers have 8-bit subregisters.
1874 if (!Subtarget->is64Bit()) {
1875 TargetRegisterClass *TRC = 0;
1876 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1877 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1878 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1879 default: llvm_unreachable("Unsupported TEST operand type!");
1880 }
1881 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001882 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1883 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001884 }
1885
1886 // Extract the l-register.
1887 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1888 MVT::i8, Reg);
1889
1890 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00001891 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001892 }
1893
1894 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00001895 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
1896 (!(C->getZExtValue() & 0x8000) ||
1897 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001898 // Shift the immediate right by 8 bits.
1899 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
1900 MVT::i8);
1901 SDValue Reg = N0.getNode()->getOperand(0);
1902
1903 // Put the value in an ABCD register.
1904 TargetRegisterClass *TRC = 0;
1905 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1906 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
1907 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1908 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1909 default: llvm_unreachable("Unsupported TEST operand type!");
1910 }
1911 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001912 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1913 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001914
1915 // Extract the h-register.
1916 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
1917 MVT::i8, Reg);
1918
1919 // Emit a testb. No special NOREX tricks are needed since there's
1920 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00001921 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
1922 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001923 }
1924
1925 // For example, "testl %eax, $32776" to "testw %ax, $32776".
1926 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001927 N0.getValueType() != MVT::i16 &&
1928 (!(C->getZExtValue() & 0x8000) ||
1929 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001930 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
1931 SDValue Reg = N0.getNode()->getOperand(0);
1932
1933 // Extract the 16-bit subregister.
1934 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
1935 MVT::i16, Reg);
1936
1937 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00001938 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001939 }
1940
1941 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
1942 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001943 N0.getValueType() == MVT::i64 &&
1944 (!(C->getZExtValue() & 0x80000000) ||
1945 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001946 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
1947 SDValue Reg = N0.getNode()->getOperand(0);
1948
1949 // Extract the 32-bit subregister.
1950 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
1951 MVT::i32, Reg);
1952
1953 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00001954 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001955 }
1956 }
1957 break;
1958 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001959 }
1960
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001961 SDNode *ResNode = SelectCode(Node);
Evan Cheng64a752f2006-08-11 09:08:15 +00001962
Chris Lattner7c306da2010-03-02 06:34:30 +00001963 DEBUG(dbgs() << "=> ";
1964 if (ResNode == NULL || ResNode == Node)
1965 Node->dump(CurDAG);
1966 else
1967 ResNode->dump(CurDAG);
1968 dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001969
1970 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001971}
1972
Chris Lattnerc0bad572006-06-08 18:03:49 +00001973bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001974SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001975 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001976 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001977 switch (ConstraintCode) {
1978 case 'o': // offsetable ??
1979 case 'v': // not offsetable ??
1980 default: return true;
1981 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001982 if (!SelectAddr(Op.getNode(), Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001983 return true;
1984 break;
1985 }
1986
Evan Cheng04699902006-08-26 01:05:16 +00001987 OutOps.push_back(Op0);
1988 OutOps.push_back(Op1);
1989 OutOps.push_back(Op2);
1990 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001991 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001992 return false;
1993}
1994
Chris Lattnerc961eea2005-11-16 01:54:32 +00001995/// createX86ISelDag - This pass converts a legalized DAG into a
1996/// X86-specific DAG, ready for instruction scheduling.
1997///
Bill Wendling98a366d2009-04-29 23:29:43 +00001998FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1999 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002000 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002001}