blob: b0eb58a273da5d6e2eaf4a16963f1d1a12e70a36 [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
697bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
Evan Chengf3caa522010-03-17 23:58:35 +0000698 X86ISelListener &DeadNodes,
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000699 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000700 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000701 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000702 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +0000703 dbgs() << "MatchAddress: ";
Bill Wendling12321672009-08-07 21:33:25 +0000704 AM.dump();
705 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000706 // Limit recursion.
707 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000708 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000709
710 CodeModel::Model M = TM.getCodeModel();
711
Chris Lattner18c59872009-06-27 04:16:01 +0000712 // If this is already a %rip relative address, we can only merge immediates
713 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000714 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000715 if (AM.isRIPRelative()) {
716 // FIXME: JumpTable and ExternalSymbol address currently don't like
717 // displacements. It isn't very important, but this should be fixed for
718 // consistency.
719 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000720
Chris Lattner18c59872009-06-27 04:16:01 +0000721 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
722 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000723 if (X86::isOffsetSuitableForCodeModel(Val, M,
724 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000725 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000726 return false;
727 }
728 }
729 return true;
730 }
731
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000732 switch (N.getOpcode()) {
733 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000734 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000735 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000736 if (!is64Bit ||
737 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
738 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000739 AM.Disp += Val;
740 return false;
741 }
742 break;
743 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000744
Rafael Espindola094fad32009-04-08 21:14:34 +0000745 case X86ISD::SegmentBaseAddress:
746 if (!MatchSegmentBaseAddress(N, AM))
747 return false;
748 break;
749
Rafael Espindola49a168d2009-04-12 21:55:03 +0000750 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000751 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000752 if (!MatchWrapper(N, AM))
753 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000754 break;
755
Rafael Espindola094fad32009-04-08 21:14:34 +0000756 case ISD::LOAD:
757 if (!MatchLoad(N, AM))
758 return false;
759 break;
760
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000761 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000762 if (AM.BaseType == X86ISelAddressMode::RegBase
763 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000764 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
765 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
766 return false;
767 }
768 break;
Evan Chengec693f72005-12-08 02:01:35 +0000769
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000770 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000771 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000772 break;
773
Gabor Greif93c53e52008-08-31 15:37:04 +0000774 if (ConstantSDNode
775 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000776 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000777 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
778 // that the base operand remains free for further matching. If
779 // the base doesn't end up getting used, a post-processing step
780 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000781 if (Val == 1 || Val == 2 || Val == 3) {
782 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000783 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000784
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000785 // Okay, we know that we have a scale by now. However, if the scaled
786 // value is an add of something and a constant, we can fold the
787 // constant into the disp field here.
Dan Gohmana10756e2010-01-21 02:09:26 +0000788 if (ShVal.getNode()->getOpcode() == ISD::ADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000789 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
790 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000791 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000792 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000793 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000794 if (!is64Bit ||
795 X86::isOffsetSuitableForCodeModel(Disp, M,
796 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000797 AM.Disp = Disp;
798 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000799 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000800 } else {
801 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000802 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000803 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000804 }
805 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000806 }
Evan Chengec693f72005-12-08 02:01:35 +0000807
Dan Gohman83688052007-10-22 20:22:24 +0000808 case ISD::SMUL_LOHI:
809 case ISD::UMUL_LOHI:
810 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000811 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000812 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000813 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000814 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000815 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000816 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000817 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000818 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000819 if (ConstantSDNode
820 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000821 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
822 CN->getZExtValue() == 9) {
823 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000824
Gabor Greifba36cb52008-08-28 21:40:38 +0000825 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000826 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000827
828 // Okay, we know that we have a scale by now. However, if the scaled
829 // value is an add of something and a constant, we can fold the
830 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000831 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
832 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
833 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000834 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000835 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000836 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000837 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000838 if (!is64Bit ||
839 X86::isOffsetSuitableForCodeModel(Disp, M,
840 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000841 AM.Disp = Disp;
842 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000843 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000844 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000845 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000846 }
847
848 AM.IndexReg = AM.Base.Reg = Reg;
849 return false;
850 }
Chris Lattner62412262007-02-04 20:18:17 +0000851 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000852 break;
853
Dan Gohman3cd90a12009-05-11 18:02:53 +0000854 case ISD::SUB: {
855 // Given A-B, if A can be completely folded into the address and
856 // the index field with the index field unused, use -B as the index.
857 // This is a win if a has multiple parts that can be folded into
858 // the address. Also, this saves a mov if the base register has
859 // other uses, since it avoids a two-address sub instruction, however
860 // it costs an additional mov if the index register has other uses.
861
862 // Test if the LHS of the sub can be folded.
863 X86ISelAddressMode Backup = AM;
Evan Chengf3caa522010-03-17 23:58:35 +0000864 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM,
865 DeadNodes, Depth+1) ||
866 // If it is successful but the recursive update causes N to be deleted,
867 // then it's not safe to continue.
868 DeadNodes.IsDeleted(N.getNode())) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000869 AM = Backup;
870 break;
871 }
872 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000873 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000874 AM = Backup;
875 break;
876 }
Evan Chengf3caa522010-03-17 23:58:35 +0000877
Dan Gohman3cd90a12009-05-11 18:02:53 +0000878 int Cost = 0;
879 SDValue RHS = N.getNode()->getOperand(1);
880 // If the RHS involves a register with multiple uses, this
881 // transformation incurs an extra mov, due to the neg instruction
882 // clobbering its operand.
883 if (!RHS.getNode()->hasOneUse() ||
884 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
885 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
886 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
887 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +0000889 ++Cost;
890 // If the base is a register with multiple uses, this
891 // transformation may save a mov.
892 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
893 AM.Base.Reg.getNode() &&
894 !AM.Base.Reg.getNode()->hasOneUse()) ||
895 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
896 --Cost;
897 // If the folded LHS was interesting, this transformation saves
898 // address arithmetic.
899 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
900 ((AM.Disp != 0) && (Backup.Disp == 0)) +
901 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
902 --Cost;
903 // If it doesn't look like it may be an overall win, don't do it.
904 if (Cost >= 0) {
905 AM = Backup;
906 break;
907 }
908
909 // Ok, the transformation is legal and appears profitable. Go for it.
910 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
911 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
912 AM.IndexReg = Neg;
913 AM.Scale = 1;
914
915 // Insert the new nodes into the topological ordering.
916 if (Zero.getNode()->getNodeId() == -1 ||
917 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
918 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
919 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
920 }
921 if (Neg.getNode()->getNodeId() == -1 ||
922 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
923 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
924 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
925 }
926 return false;
927 }
928
Evan Cheng8e278262009-01-17 07:09:27 +0000929 case ISD::ADD: {
930 X86ISelAddressMode Backup = AM;
Evan Chengf3caa522010-03-17 23:58:35 +0000931 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM,
932 DeadNodes, Depth+1)) {
933 if (DeadNodes.IsDeleted(N.getNode()))
934 // If it is successful but the recursive update causes N to be deleted,
935 // then it's not safe to continue.
936 return true;
937 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM,
938 DeadNodes, Depth+1))
939 // If it is successful but the recursive update causes N to be deleted,
940 // then it's not safe to continue.
941 return DeadNodes.IsDeleted(N.getNode());
942 }
943
944 // Try again after commuting the operands.
Evan Cheng8e278262009-01-17 07:09:27 +0000945 AM = Backup;
Evan Chengf3caa522010-03-17 23:58:35 +0000946 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM,
947 DeadNodes, Depth+1)) {
948 if (DeadNodes.IsDeleted(N.getNode()))
949 // If it is successful but the recursive update causes N to be deleted,
950 // then it's not safe to continue.
951 return true;
952 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM,
953 DeadNodes, Depth+1))
954 // If it is successful but the recursive update causes N to be deleted,
955 // then it's not safe to continue.
956 return DeadNodes.IsDeleted(N.getNode());
957 }
Evan Cheng8e278262009-01-17 07:09:27 +0000958 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000959
960 // If we couldn't fold both operands into the address at the same time,
961 // see if we can just put each operand into a register and fold at least
962 // the add.
963 if (AM.BaseType == X86ISelAddressMode::RegBase &&
964 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +0000965 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +0000966 AM.Base.Reg = N.getNode()->getOperand(0);
967 AM.IndexReg = N.getNode()->getOperand(1);
968 AM.Scale = 1;
969 return false;
970 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000971 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000972 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000973
Chris Lattner62412262007-02-04 20:18:17 +0000974 case ISD::OR:
975 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000976 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
977 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000978 uint64_t Offset = CN->getSExtValue();
Evan Chengf3caa522010-03-17 23:58:35 +0000979
980 // Check to see if the LHS & C is zero.
981 if (!CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue()))
982 break;
983
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000984 // Start with the LHS as an addr mode.
Evan Chengf3caa522010-03-17 23:58:35 +0000985 if (!MatchAddressRecursively(N.getOperand(0), AM, DeadNodes, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000986 // Address could not have picked a GV address for the displacement.
987 AM.GV == NULL &&
988 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000989 (!is64Bit ||
990 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
Evan Chengf3caa522010-03-17 23:58:35 +0000991 AM.hasSymbolicDisplacement()))) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000992 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000993 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000994 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000995 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000996 }
997 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000998
999 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001000 // Perform some heroic transforms on an and of a constant-count shift
1001 // with a constant to enable use of the scaled offset field.
1002
Dan Gohman475871a2008-07-27 21:46:04 +00001003 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001004 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001005
Evan Cheng1314b002007-12-13 00:43:27 +00001006 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001007 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001008
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001009 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001010 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1011 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1012 if (!C1 || !C2) break;
1013
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001014 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1015 // allows us to convert the shift and and into an h-register extract and
1016 // a scaled index.
1017 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1018 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001019 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001020 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001022 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1023 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1024 X, Eight);
1025 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1026 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001027 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001028 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1029 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001030
1031 // Insert the new nodes into the topological ordering.
1032 if (Eight.getNode()->getNodeId() == -1 ||
1033 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1034 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1035 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1036 }
1037 if (Mask.getNode()->getNodeId() == -1 ||
1038 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1039 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1040 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1041 }
1042 if (Srl.getNode()->getNodeId() == -1 ||
1043 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1044 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1045 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1046 }
1047 if (And.getNode()->getNodeId() == -1 ||
1048 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1049 CurDAG->RepositionNode(N.getNode(), And.getNode());
1050 And.getNode()->setNodeId(N.getNode()->getNodeId());
1051 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001052 if (ShlCount.getNode()->getNodeId() == -1 ||
1053 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1054 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1055 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1056 }
1057 if (Shl.getNode()->getNodeId() == -1 ||
1058 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1059 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1060 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1061 }
Evan Chengf3caa522010-03-17 23:58:35 +00001062 CurDAG->ReplaceAllUsesWith(N, Shl, &DeadNodes);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001063 AM.IndexReg = And;
1064 AM.Scale = (1 << ScaleLog);
1065 return false;
1066 }
1067 }
1068
1069 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1070 // allows us to fold the shift into this addressing mode.
1071 if (Shift.getOpcode() != ISD::SHL) break;
1072
Evan Cheng1314b002007-12-13 00:43:27 +00001073 // Not likely to be profitable if either the AND or SHIFT node has more
1074 // than one use (unless all uses are for address computation). Besides,
1075 // isel mechanism requires their node ids to be reused.
1076 if (!N.hasOneUse() || !Shift.hasOneUse())
1077 break;
1078
1079 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001080 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001081 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1082 break;
1083
1084 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001085 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001086 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001087 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1088 NewANDMask);
1089 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001090 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001091
1092 // Insert the new nodes into the topological ordering.
1093 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1094 CurDAG->RepositionNode(X.getNode(), C1);
1095 C1->setNodeId(X.getNode()->getNodeId());
1096 }
1097 if (NewANDMask.getNode()->getNodeId() == -1 ||
1098 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1099 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1100 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1101 }
1102 if (NewAND.getNode()->getNodeId() == -1 ||
1103 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1104 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1105 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1106 }
1107 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1108 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1109 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1110 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1111 }
1112
Evan Chengf3caa522010-03-17 23:58:35 +00001113 CurDAG->ReplaceAllUsesWith(N, NewSHIFT, &DeadNodes);
Evan Cheng1314b002007-12-13 00:43:27 +00001114
1115 AM.Scale = 1 << ShiftCst;
1116 AM.IndexReg = NewAND;
1117 return false;
1118 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001119 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001120
Rafael Espindola523249f2009-03-31 16:16:57 +00001121 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001122}
1123
1124/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1125/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001126bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001127 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001128 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001129 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001130 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001131 AM.IndexReg = N;
1132 AM.Scale = 1;
1133 return false;
1134 }
1135
1136 // Otherwise, we cannot select it.
1137 return true;
1138 }
1139
1140 // Default, generate it as a register.
1141 AM.BaseType = X86ISelAddressMode::RegBase;
1142 AM.Base.Reg = N;
1143 return false;
1144}
1145
Evan Chengec693f72005-12-08 02:01:35 +00001146/// SelectAddr - returns true if it is able pattern match an addressing mode.
1147/// It returns the operands which make up the maximal addressing mode it can
1148/// match by reference.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001149bool X86DAGToDAGISel::SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +00001150 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001151 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001152 X86ISelAddressMode AM;
Evan Chengc7928f82009-12-18 01:59:21 +00001153 if (MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001154 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001155
Owen Andersone50ed302009-08-10 22:56:29 +00001156 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001157 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001158 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001159 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001160 }
Evan Cheng8700e142006-01-11 06:09:51 +00001161
Gabor Greifba36cb52008-08-28 21:40:38 +00001162 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001163 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001164
Rafael Espindola094fad32009-04-08 21:14:34 +00001165 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001166 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001167}
1168
Chris Lattner3a7cd952006-10-07 21:55:32 +00001169/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1170/// match a load whose top elements are either undef or zeros. The load flavor
1171/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner64b49862010-02-17 06:07:47 +00001172///
1173/// We also return:
Chris Lattnera170b5e2010-02-21 03:17:59 +00001174/// PatternChainNode: this is the matched node that has a chain input and
1175/// output.
Chris Lattnere60f7b42010-03-01 22:51:11 +00001176bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
Dan Gohman475871a2008-07-27 21:46:04 +00001177 SDValue N, SDValue &Base,
1178 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001179 SDValue &Disp, SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +00001180 SDValue &PatternNodeWithChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001181 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001182 PatternNodeWithChain = N.getOperand(0);
1183 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1184 PatternNodeWithChain.hasOneUse() &&
Chris Lattnerf1c64282010-02-21 04:53:34 +00001185 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohmand858e902010-04-17 15:26:15 +00001186 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001187 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattner92d3ada2010-02-16 22:35:06 +00001188 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp,Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001189 return false;
1190 return true;
1191 }
1192 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001193
1194 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001195 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001196 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001197 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001198 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001199 N.getOperand(0).getNode()->hasOneUse() &&
1200 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattner92d3ada2010-02-16 22:35:06 +00001201 N.getOperand(0).getOperand(0).hasOneUse() &&
1202 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohmand858e902010-04-17 15:26:15 +00001203 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00001204 // Okay, this is a zero extending load. Fold it.
1205 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattner92d3ada2010-02-16 22:35:06 +00001206 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001207 return false;
Chris Lattnera170b5e2010-02-21 03:17:59 +00001208 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001209 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001210 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001211 return false;
1212}
1213
1214
Evan Cheng51a9ed92006-02-25 10:09:08 +00001215/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1216/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001217bool X86DAGToDAGISel::SelectLEAAddr(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001218 SDValue &Base, SDValue &Scale,
1219 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001220 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001221
1222 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1223 // segments.
1224 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001226 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001227 if (MatchAddress(N, AM))
1228 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001229 assert (T == AM.Segment);
1230 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001231
Owen Andersone50ed302009-08-10 22:56:29 +00001232 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001233 unsigned Complexity = 0;
1234 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001235 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001236 Complexity = 1;
1237 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001238 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001239 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1240 Complexity = 4;
1241
Gabor Greifba36cb52008-08-28 21:40:38 +00001242 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001243 Complexity++;
1244 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001245 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001246
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001247 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1248 // a simple shift.
1249 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001250 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001251
1252 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1253 // to a LEA. This is determined with some expermentation but is by no means
1254 // optimal (especially for code size consideration). LEA is nice because of
1255 // its three-address nature. Tweak the cost function again when we can run
1256 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001257 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001258 // For X86-64, we should always use lea to materialize RIP relative
1259 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001260 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001261 Complexity = 4;
1262 else
1263 Complexity += 2;
1264 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001265
Gabor Greifba36cb52008-08-28 21:40:38 +00001266 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001267 Complexity++;
1268
Chris Lattner25142782009-07-11 22:50:33 +00001269 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001270 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001271 return false;
1272
1273 SDValue Segment;
1274 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1275 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001276}
1277
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001278/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001279bool X86DAGToDAGISel::SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001280 SDValue &Scale, SDValue &Index,
1281 SDValue &Disp) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001282 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1283 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1284
1285 X86ISelAddressMode AM;
1286 AM.GV = GA->getGlobal();
1287 AM.Disp += GA->getOffset();
1288 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001289 AM.SymbolFlags = GA->getTargetFlags();
1290
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001292 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001294 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001295 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001296 }
1297
1298 SDValue Segment;
1299 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1300 return true;
1301}
1302
1303
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001304bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001306 SDValue &Index, SDValue &Disp,
1307 SDValue &Segment) {
Chris Lattnerd1b73822010-03-02 22:20:06 +00001308 if (!ISD::isNON_EXTLoad(N.getNode()) ||
1309 !IsProfitableToFold(N, P, P) ||
Dan Gohmand858e902010-04-17 15:26:15 +00001310 !IsLegalToFold(N, P, P, OptLevel))
Chris Lattnerd1b73822010-03-02 22:20:06 +00001311 return false;
1312
1313 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001314}
1315
Dan Gohman8b746962008-09-23 18:22:58 +00001316/// getGlobalBaseReg - Return an SDNode that returns the value of
1317/// the global base register. Output instructions required to
1318/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001319///
Evan Cheng9ade2182006-08-26 05:34:46 +00001320SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001321 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001322 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001323}
1324
Evan Chengb245d922006-05-20 01:36:52 +00001325static SDNode *FindCallStartFromCall(SDNode *Node) {
1326 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001327 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001328 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001329 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001330}
1331
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001332SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1333 SDValue Chain = Node->getOperand(0);
1334 SDValue In1 = Node->getOperand(1);
1335 SDValue In2L = Node->getOperand(2);
1336 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001337 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001338 if (!SelectAddr(In1.getNode(), In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001339 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001340 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1341 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1342 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1343 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1344 MVT::i32, MVT::i32, MVT::Other, Ops,
1345 array_lengthof(Ops));
1346 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1347 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001348}
Christopher Lambc59e5212007-08-10 21:48:46 +00001349
Owen Andersone50ed302009-08-10 22:56:29 +00001350SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001351 if (Node->hasAnyUseOfValue(0))
1352 return 0;
1353
1354 // Optimize common patterns for __sync_add_and_fetch and
1355 // __sync_sub_and_fetch where the result is not used. This allows us
1356 // to use "lock" version of add, sub, inc, dec instructions.
1357 // FIXME: Do not use special instructions but instead add the "lock"
1358 // prefix to the target node somehow. The extra information will then be
1359 // transferred to machine instruction and it denotes the prefix.
1360 SDValue Chain = Node->getOperand(0);
1361 SDValue Ptr = Node->getOperand(1);
1362 SDValue Val = Node->getOperand(2);
1363 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001364 if (!SelectAddr(Ptr.getNode(), Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Evan Cheng37b73872009-07-30 08:33:02 +00001365 return 0;
1366
1367 bool isInc = false, isDec = false, isSub = false, isCN = false;
1368 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1369 if (CN) {
1370 isCN = true;
1371 int64_t CNVal = CN->getSExtValue();
1372 if (CNVal == 1)
1373 isInc = true;
1374 else if (CNVal == -1)
1375 isDec = true;
1376 else if (CNVal >= 0)
1377 Val = CurDAG->getTargetConstant(CNVal, NVT);
1378 else {
1379 isSub = true;
1380 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1381 }
1382 } else if (Val.hasOneUse() &&
1383 Val.getOpcode() == ISD::SUB &&
1384 X86::isZeroNode(Val.getOperand(0))) {
1385 isSub = true;
1386 Val = Val.getOperand(1);
1387 }
1388
1389 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001390 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001391 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001393 if (isInc)
1394 Opc = X86::LOCK_INC8m;
1395 else if (isDec)
1396 Opc = X86::LOCK_DEC8m;
1397 else if (isSub) {
1398 if (isCN)
1399 Opc = X86::LOCK_SUB8mi;
1400 else
1401 Opc = X86::LOCK_SUB8mr;
1402 } else {
1403 if (isCN)
1404 Opc = X86::LOCK_ADD8mi;
1405 else
1406 Opc = X86::LOCK_ADD8mr;
1407 }
1408 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001410 if (isInc)
1411 Opc = X86::LOCK_INC16m;
1412 else if (isDec)
1413 Opc = X86::LOCK_DEC16m;
1414 else if (isSub) {
1415 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001416 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001417 Opc = X86::LOCK_SUB16mi8;
1418 else
1419 Opc = X86::LOCK_SUB16mi;
1420 } else
1421 Opc = X86::LOCK_SUB16mr;
1422 } else {
1423 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001424 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001425 Opc = X86::LOCK_ADD16mi8;
1426 else
1427 Opc = X86::LOCK_ADD16mi;
1428 } else
1429 Opc = X86::LOCK_ADD16mr;
1430 }
1431 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001432 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001433 if (isInc)
1434 Opc = X86::LOCK_INC32m;
1435 else if (isDec)
1436 Opc = X86::LOCK_DEC32m;
1437 else if (isSub) {
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_SUB32mi8;
1441 else
1442 Opc = X86::LOCK_SUB32mi;
1443 } else
1444 Opc = X86::LOCK_SUB32mr;
1445 } else {
1446 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001447 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001448 Opc = X86::LOCK_ADD32mi8;
1449 else
1450 Opc = X86::LOCK_ADD32mi;
1451 } else
1452 Opc = X86::LOCK_ADD32mr;
1453 }
1454 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001455 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001456 if (isInc)
1457 Opc = X86::LOCK_INC64m;
1458 else if (isDec)
1459 Opc = X86::LOCK_DEC64m;
1460 else if (isSub) {
1461 Opc = X86::LOCK_SUB64mr;
1462 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001463 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001464 Opc = X86::LOCK_SUB64mi8;
1465 else if (Predicate_i64immSExt32(Val.getNode()))
1466 Opc = X86::LOCK_SUB64mi32;
1467 }
1468 } else {
1469 Opc = X86::LOCK_ADD64mr;
1470 if (isCN) {
Chris Lattner18409912010-03-03 01:45:01 +00001471 if (Predicate_immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001472 Opc = X86::LOCK_ADD64mi8;
1473 else if (Predicate_i64immSExt32(Val.getNode()))
1474 Opc = X86::LOCK_ADD64mi32;
1475 }
1476 }
1477 break;
1478 }
1479
1480 DebugLoc dl = Node->getDebugLoc();
Chris Lattner518bb532010-02-09 19:54:29 +00001481 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +00001482 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001483 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1484 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001485 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001486 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1487 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1488 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001489 SDValue RetVals[] = { Undef, Ret };
1490 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1491 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001492 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1493 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1494 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001495 SDValue RetVals[] = { Undef, Ret };
1496 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1497 }
1498}
1499
Dan Gohman11596ed2009-10-09 20:35:19 +00001500/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1501/// any uses which require the SF or OF bits to be accurate.
1502static bool HasNoSignedComparisonUses(SDNode *N) {
1503 // Examine each user of the node.
1504 for (SDNode::use_iterator UI = N->use_begin(),
1505 UE = N->use_end(); UI != UE; ++UI) {
1506 // Only examine CopyToReg uses.
1507 if (UI->getOpcode() != ISD::CopyToReg)
1508 return false;
1509 // Only examine CopyToReg uses that copy to EFLAGS.
1510 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1511 X86::EFLAGS)
1512 return false;
1513 // Examine each user of the CopyToReg use.
1514 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1515 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1516 // Only examine the Flag result.
1517 if (FlagUI.getUse().getResNo() != 1) continue;
1518 // Anything unusual: assume conservatively.
1519 if (!FlagUI->isMachineOpcode()) return false;
1520 // Examine the opcode of the user.
1521 switch (FlagUI->getMachineOpcode()) {
1522 // These comparisons don't treat the most significant bit specially.
1523 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1524 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1525 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1526 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001527 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1528 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
Dan Gohman11596ed2009-10-09 20:35:19 +00001529 case X86::CMOVA16rr: case X86::CMOVA16rm:
1530 case X86::CMOVA32rr: case X86::CMOVA32rm:
1531 case X86::CMOVA64rr: case X86::CMOVA64rm:
1532 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1533 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1534 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1535 case X86::CMOVB16rr: case X86::CMOVB16rm:
1536 case X86::CMOVB32rr: case X86::CMOVB32rm:
1537 case X86::CMOVB64rr: case X86::CMOVB64rm:
1538 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1539 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1540 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1541 case X86::CMOVE16rr: case X86::CMOVE16rm:
1542 case X86::CMOVE32rr: case X86::CMOVE32rm:
1543 case X86::CMOVE64rr: case X86::CMOVE64rm:
1544 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1545 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1546 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1547 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1548 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1549 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1550 case X86::CMOVP16rr: case X86::CMOVP16rm:
1551 case X86::CMOVP32rr: case X86::CMOVP32rm:
1552 case X86::CMOVP64rr: case X86::CMOVP64rm:
1553 continue;
1554 // Anything else: assume conservatively.
1555 default: return false;
1556 }
1557 }
1558 }
1559 return true;
1560}
1561
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001562SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Owen Andersone50ed302009-08-10 22:56:29 +00001563 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001564 unsigned Opc, MOpc;
1565 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001566 DebugLoc dl = Node->getDebugLoc();
1567
Chris Lattner7c306da2010-03-02 06:34:30 +00001568 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
Evan Chengf597dc72006-02-10 22:24:32 +00001569
Dan Gohmane8be6c62008-07-17 19:10:17 +00001570 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +00001571 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001572 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001573 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001574
Evan Cheng0114e942006-01-06 20:36:21 +00001575 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001576 default: break;
1577 case X86ISD::GlobalBaseReg:
1578 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001579
Dan Gohman72677342009-08-02 16:10:52 +00001580 case X86ISD::ATOMOR64_DAG:
1581 return SelectAtomic64(Node, X86::ATOMOR6432);
1582 case X86ISD::ATOMXOR64_DAG:
1583 return SelectAtomic64(Node, X86::ATOMXOR6432);
1584 case X86ISD::ATOMADD64_DAG:
1585 return SelectAtomic64(Node, X86::ATOMADD6432);
1586 case X86ISD::ATOMSUB64_DAG:
1587 return SelectAtomic64(Node, X86::ATOMSUB6432);
1588 case X86ISD::ATOMNAND64_DAG:
1589 return SelectAtomic64(Node, X86::ATOMNAND6432);
1590 case X86ISD::ATOMAND64_DAG:
1591 return SelectAtomic64(Node, X86::ATOMAND6432);
1592 case X86ISD::ATOMSWAP64_DAG:
1593 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001594
Dan Gohman72677342009-08-02 16:10:52 +00001595 case ISD::ATOMIC_LOAD_ADD: {
1596 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1597 if (RetVal)
1598 return RetVal;
1599 break;
1600 }
1601
1602 case ISD::SMUL_LOHI:
1603 case ISD::UMUL_LOHI: {
1604 SDValue N0 = Node->getOperand(0);
1605 SDValue N1 = Node->getOperand(1);
1606
1607 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001608 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001610 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001611 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1612 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1613 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1614 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001615 }
Bill Wendling12321672009-08-07 21:33:25 +00001616 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001617 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001618 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1620 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1621 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1622 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001623 }
Bill Wendling12321672009-08-07 21:33:25 +00001624 }
Dan Gohman72677342009-08-02 16:10:52 +00001625
1626 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001627 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001628 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001629 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1630 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1631 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1632 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001633 }
1634
1635 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001636 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001637 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001638 if (!foldedLoad) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001639 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001640 if (foldedLoad)
1641 std::swap(N0, N1);
1642 }
1643
1644 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1645 N0, SDValue()).getValue(1);
1646
1647 if (foldedLoad) {
1648 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1649 InFlag };
1650 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001651 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1652 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001653 InFlag = SDValue(CNode, 1);
1654 // Update the chain.
1655 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1656 } else {
1657 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001658 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001659 }
1660
1661 // Copy the low half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001662 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001663 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1664 LoReg, NVT, InFlag);
1665 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001666 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001667 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001668 }
1669 // Copy the high half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001670 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001671 SDValue Result;
1672 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1673 // Prevent use of AH in a REX instruction by referencing AX instead.
1674 // Shift it down 8 bits.
1675 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001676 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001677 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001678 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1679 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001680 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001681 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001682 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1683 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001684 } else {
1685 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1686 HiReg, NVT, InFlag);
1687 InFlag = Result.getValue(2);
1688 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001689 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001690 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001691 }
1692
Dan Gohman72677342009-08-02 16:10:52 +00001693 return NULL;
1694 }
1695
1696 case ISD::SDIVREM:
1697 case ISD::UDIVREM: {
1698 SDValue N0 = Node->getOperand(0);
1699 SDValue N1 = Node->getOperand(1);
1700
1701 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001702 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001704 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1706 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1707 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1708 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001709 }
Bill Wendling12321672009-08-07 21:33:25 +00001710 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001712 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1714 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1715 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1716 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001717 }
Bill Wendling12321672009-08-07 21:33:25 +00001718 }
Dan Gohman72677342009-08-02 16:10:52 +00001719
Chris Lattner9e323832009-12-23 01:45:04 +00001720 unsigned LoReg, HiReg, ClrReg;
Dan Gohman72677342009-08-02 16:10:52 +00001721 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001723 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001724 case MVT::i8:
Chris Lattner9e323832009-12-23 01:45:04 +00001725 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman72677342009-08-02 16:10:52 +00001726 ClrOpcode = 0;
1727 SExtOpcode = X86::CBW;
1728 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001729 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001730 LoReg = X86::AX; HiReg = X86::DX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001731 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
Dan Gohman72677342009-08-02 16:10:52 +00001732 SExtOpcode = X86::CWD;
1733 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001734 case MVT::i32:
Chris Lattner9e323832009-12-23 01:45:04 +00001735 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman72677342009-08-02 16:10:52 +00001736 ClrOpcode = X86::MOV32r0;
1737 SExtOpcode = X86::CDQ;
1738 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001739 case MVT::i64:
Chris Lattner9e323832009-12-23 01:45:04 +00001740 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001741 ClrOpcode = X86::MOV64r0;
Dan Gohman72677342009-08-02 16:10:52 +00001742 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001743 break;
1744 }
1745
Dan Gohman72677342009-08-02 16:10:52 +00001746 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001747 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001748 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001749
Dan Gohman72677342009-08-02 16:10:52 +00001750 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001751 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001752 // Special case for div8, just use a move with zero extension to AX to
1753 // clear the upper 8 bits (AH).
1754 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001755 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman72677342009-08-02 16:10:52 +00001756 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1757 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001758 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1759 MVT::Other, Ops,
1760 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001761 Chain = Move.getValue(1);
1762 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001763 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001764 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001765 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001766 Chain = CurDAG->getEntryNode();
1767 }
1768 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1769 InFlag = Chain.getValue(1);
1770 } else {
1771 InFlag =
1772 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1773 LoReg, N0, SDValue()).getValue(1);
1774 if (isSigned && !signBitIsZero) {
1775 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001776 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001777 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001778 } else {
1779 // Zero out the high part, effectively zero extending the input.
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001780 SDValue ClrNode =
1781 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Chris Lattner9e323832009-12-23 01:45:04 +00001782 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman72677342009-08-02 16:10:52 +00001783 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001784 }
Evan Cheng948f3432006-01-06 23:19:29 +00001785 }
Dan Gohman525178c2007-10-08 18:33:35 +00001786
Dan Gohman72677342009-08-02 16:10:52 +00001787 if (foldedLoad) {
1788 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1789 InFlag };
1790 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001791 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1792 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001793 InFlag = SDValue(CNode, 1);
1794 // Update the chain.
1795 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1796 } else {
1797 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001798 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001799 }
Evan Cheng948f3432006-01-06 23:19:29 +00001800
Dan Gohman72677342009-08-02 16:10:52 +00001801 // Copy the division (low) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001802 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001803 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1804 LoReg, NVT, InFlag);
1805 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001806 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001807 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001808 }
1809 // Copy the remainder (high) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001810 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001811 SDValue Result;
1812 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1813 // Prevent use of AH in a REX instruction by referencing AX instead.
1814 // Shift it down 8 bits.
1815 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001816 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001817 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001818 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001819 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001820 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001821 0);
1822 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001823 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1824 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001825 } else {
1826 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1827 HiReg, NVT, InFlag);
1828 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001829 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001830 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001831 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001832 }
Dan Gohman72677342009-08-02 16:10:52 +00001833 return NULL;
1834 }
1835
Dan Gohman6a402dc2009-08-19 18:16:17 +00001836 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001837 SDValue N0 = Node->getOperand(0);
1838 SDValue N1 = Node->getOperand(1);
1839
1840 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
1841 // use a smaller encoding.
1842 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
1843 N0.getValueType() != MVT::i8 &&
1844 X86::isZeroNode(N1)) {
1845 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
1846 if (!C) break;
1847
1848 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00001849 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
1850 (!(C->getZExtValue() & 0x80) ||
1851 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001852 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
1853 SDValue Reg = N0.getNode()->getOperand(0);
1854
1855 // On x86-32, only the ABCD registers have 8-bit subregisters.
1856 if (!Subtarget->is64Bit()) {
1857 TargetRegisterClass *TRC = 0;
1858 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1859 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1860 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1861 default: llvm_unreachable("Unsupported TEST operand type!");
1862 }
1863 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001864 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1865 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001866 }
1867
1868 // Extract the l-register.
1869 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1870 MVT::i8, Reg);
1871
1872 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00001873 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001874 }
1875
1876 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00001877 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
1878 (!(C->getZExtValue() & 0x8000) ||
1879 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001880 // Shift the immediate right by 8 bits.
1881 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
1882 MVT::i8);
1883 SDValue Reg = N0.getNode()->getOperand(0);
1884
1885 // Put the value in an ABCD register.
1886 TargetRegisterClass *TRC = 0;
1887 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1888 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
1889 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1890 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1891 default: llvm_unreachable("Unsupported TEST operand type!");
1892 }
1893 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001894 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1895 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001896
1897 // Extract the h-register.
1898 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
1899 MVT::i8, Reg);
1900
1901 // Emit a testb. No special NOREX tricks are needed since there's
1902 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00001903 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
1904 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001905 }
1906
1907 // For example, "testl %eax, $32776" to "testw %ax, $32776".
1908 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001909 N0.getValueType() != MVT::i16 &&
1910 (!(C->getZExtValue() & 0x8000) ||
1911 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001912 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
1913 SDValue Reg = N0.getNode()->getOperand(0);
1914
1915 // Extract the 16-bit subregister.
1916 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
1917 MVT::i16, Reg);
1918
1919 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00001920 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001921 }
1922
1923 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
1924 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001925 N0.getValueType() == MVT::i64 &&
1926 (!(C->getZExtValue() & 0x80000000) ||
1927 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001928 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
1929 SDValue Reg = N0.getNode()->getOperand(0);
1930
1931 // Extract the 32-bit subregister.
1932 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
1933 MVT::i32, Reg);
1934
1935 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00001936 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001937 }
1938 }
1939 break;
1940 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001941 }
1942
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001943 SDNode *ResNode = SelectCode(Node);
Evan Cheng64a752f2006-08-11 09:08:15 +00001944
Chris Lattner7c306da2010-03-02 06:34:30 +00001945 DEBUG(dbgs() << "=> ";
1946 if (ResNode == NULL || ResNode == Node)
1947 Node->dump(CurDAG);
1948 else
1949 ResNode->dump(CurDAG);
1950 dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001951
1952 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001953}
1954
Chris Lattnerc0bad572006-06-08 18:03:49 +00001955bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001956SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001957 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001958 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001959 switch (ConstraintCode) {
1960 case 'o': // offsetable ??
1961 case 'v': // not offsetable ??
1962 default: return true;
1963 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001964 if (!SelectAddr(Op.getNode(), Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001965 return true;
1966 break;
1967 }
1968
Evan Cheng04699902006-08-26 01:05:16 +00001969 OutOps.push_back(Op0);
1970 OutOps.push_back(Op1);
1971 OutOps.push_back(Op2);
1972 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001973 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001974 return false;
1975}
1976
Chris Lattnerc961eea2005-11-16 01:54:32 +00001977/// createX86ISelDag - This pass converts a legalized DAG into a
1978/// X86-specific DAG, ready for instruction scheduling.
1979///
Bill Wendling98a366d2009-04-29 23:29:43 +00001980FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1981 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001982 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001983}