blob: 5fdc61ebd5052d36754b3092a409ae4ea99663b9 [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"
Reid Spencer7aa8a452007-01-12 23:22:14 +000024#include "llvm/Type.h"
Eric Christophere3997d42011-07-01 23:04:38 +000025#include "llvm/CodeGen/FunctionLoweringInfo.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"
Craig Topper79aa3412012-03-17 18:46:09 +000034#include "llvm/Support/CFG.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000035#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000036#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000038#include "llvm/Support/raw_ostream.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000039#include "llvm/ADT/Statistic.h"
40using namespace llvm;
41
Chris Lattner95b2c7d2006-12-19 22:59:26 +000042STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
43
Chris Lattnerc961eea2005-11-16 01:54:32 +000044//===----------------------------------------------------------------------===//
45// Pattern Matcher Implementation
46//===----------------------------------------------------------------------===//
47
48namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000049 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000050 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000051 /// tree.
52 struct X86ISelAddressMode {
53 enum {
54 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000055 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 } BaseType;
57
Dan Gohmanffce6f12010-04-29 23:30:41 +000058 // This is really a union, discriminated by BaseType!
59 SDValue Base_Reg;
60 int Base_FrameIndex;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061
62 unsigned Scale;
Chad Rosiera20e1e72012-08-01 18:39:17 +000063 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000064 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000065 SDValue Segment;
Dan Gohman46510a72010-04-15 01:51:59 +000066 const GlobalValue *GV;
67 const Constant *CP;
68 const BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000069 const char *ES;
70 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000071 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000072 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000073
74 X86ISelAddressMode()
Dan Gohmanffce6f12010-04-29 23:30:41 +000075 : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000076 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000077 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000079
80 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000081 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000082 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000083
Chris Lattner18c59872009-06-27 04:16:01 +000084 bool hasBaseOrIndexReg() const {
Dan Gohmanffce6f12010-04-29 23:30:41 +000085 return IndexReg.getNode() != 0 || Base_Reg.getNode() != 0;
Chris Lattner18c59872009-06-27 04:16:01 +000086 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000087
Chris Lattner18c59872009-06-27 04:16:01 +000088 /// isRIPRelative - Return true if this addressing mode is already RIP
89 /// relative.
90 bool isRIPRelative() const {
91 if (BaseType != RegBase) return false;
92 if (RegisterSDNode *RegNode =
Dan Gohmanffce6f12010-04-29 23:30:41 +000093 dyn_cast_or_null<RegisterSDNode>(Base_Reg.getNode()))
Chris Lattner18c59872009-06-27 04:16:01 +000094 return RegNode->getReg() == X86::RIP;
95 return false;
96 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000097
Chris Lattner18c59872009-06-27 04:16:01 +000098 void setBaseReg(SDValue Reg) {
99 BaseType = RegBase;
Dan Gohmanffce6f12010-04-29 23:30:41 +0000100 Base_Reg = Reg;
Chris Lattner18c59872009-06-27 04:16:01 +0000101 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000102
Manman Ren77e300e2012-09-06 19:06:06 +0000103#ifndef NDEBUG
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000104 void dump() {
David Greened7f4f242010-01-05 01:29:08 +0000105 dbgs() << "X86ISelAddressMode " << this << '\n';
Dan Gohmanffce6f12010-04-29 23:30:41 +0000106 dbgs() << "Base_Reg ";
107 if (Base_Reg.getNode() != 0)
Chad Rosiera20e1e72012-08-01 18:39:17 +0000108 Base_Reg.getNode()->dump();
Bill Wendling12321672009-08-07 21:33:25 +0000109 else
David Greened7f4f242010-01-05 01:29:08 +0000110 dbgs() << "nul";
Dan Gohmanffce6f12010-04-29 23:30:41 +0000111 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
Chad Rosiera20e1e72012-08-01 18:39:17 +0000117 dbgs() << "nul";
David Greened7f4f242010-01-05 01:29:08 +0000118 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 }
Manman Ren77e300e2012-09-06 19:06:06 +0000137#endif
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000138 };
139}
140
141namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000142 //===--------------------------------------------------------------------===//
143 /// ISel - X86 specific code to select X86 machine instructions for
144 /// SelectionDAG operations.
145 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000146 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000147 /// X86Lowering - This object fully describes how to lower LLVM code to an
148 /// X86-specific SelectionDAG.
Dan Gohmand858e902010-04-17 15:26:15 +0000149 const X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000150
151 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
152 /// make the right decision when generating code for different targets.
153 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000154
Evan Chengb7a75a52008-09-26 23:41:32 +0000155 /// OptForSize - If true, selector should try to optimize for code size
156 /// instead of performance.
157 bool OptForSize;
158
Chris Lattnerc961eea2005-11-16 01:54:32 +0000159 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000160 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000161 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000162 X86Lowering(*tm.getTargetLowering()),
163 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000164 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000165
166 virtual const char *getPassName() const {
167 return "X86 DAG->DAG Instruction Selection";
168 }
169
Dan Gohman64652652010-04-14 20:17:22 +0000170 virtual void EmitFunctionEntryCode();
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000171
Evan Cheng014bf212010-02-15 19:41:07 +0000172 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
173
Chris Lattner7c306da2010-03-02 06:34:30 +0000174 virtual void PreprocessISelDAG();
175
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +0000176 inline bool immSext8(SDNode *N) const {
177 return isInt<8>(cast<ConstantSDNode>(N)->getSExtValue());
178 }
179
180 // i64immSExt32 predicate - True if the 64-bit immediate fits in a 32-bit
181 // sign extended field.
182 inline bool i64immSExt32(SDNode *N) const {
183 uint64_t v = cast<ConstantSDNode>(N)->getZExtValue();
184 return (int64_t)v == (int32_t)v;
185 }
186
Chris Lattnerc961eea2005-11-16 01:54:32 +0000187// Include the pieces autogenerated from the target description.
188#include "X86GenDAGISel.inc"
189
190 private:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000191 SDNode *Select(SDNode *N);
Manman Ren1f7a1b62012-06-26 19:47:59 +0000192 SDNode *SelectGather(SDNode *N, unsigned Opc);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000193 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000194 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Eric Christopherc324f722011-05-17 08:10:18 +0000195 SDNode *SelectAtomicLoadArith(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000196
Eli Friedman4977eb52011-07-13 20:44:23 +0000197 bool FoldOffsetIntoAddress(uint64_t Offset, X86ISelAddressMode &AM);
Chris Lattnerf93b90c2010-09-22 04:39:11 +0000198 bool MatchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000199 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000200 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
201 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
202 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000203 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Chris Lattnerb86faa12010-09-21 22:07:31 +0000204 bool SelectAddr(SDNode *Parent, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000205 SDValue &Scale, SDValue &Index, SDValue &Disp,
206 SDValue &Segment);
Chris Lattner52a261b2010-09-21 20:31:19 +0000207 bool SelectLEAAddr(SDValue N, SDValue &Base,
Chris Lattner599b5312010-07-08 23:46:44 +0000208 SDValue &Scale, SDValue &Index, SDValue &Disp,
209 SDValue &Segment);
Chris Lattner52a261b2010-09-21 20:31:19 +0000210 bool SelectTLSADDRAddr(SDValue N, SDValue &Base,
Chris Lattner599b5312010-07-08 23:46:44 +0000211 SDValue &Scale, SDValue &Index, SDValue &Disp,
212 SDValue &Segment);
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);
Chad Rosiera20e1e72012-08-01 18:39:17 +0000218
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);
Chad Rosiera20e1e72012-08-01 18:39:17 +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);
Chad Rosiera20e1e72012-08-01 18:39:17 +0000229
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000230 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
231
Chad Rosiera20e1e72012-08-01 18:39:17 +0000232 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000233 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) ?
Dan Gohmanffce6f12010-04-29 23:30:41 +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)
Devang Patel0d881da2010-07-06 22:08:15 +0000243 Disp = CurDAG->getTargetGlobalAddress(AM.GV, DebugLoc(),
244 MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000245 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000246 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000248 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000251 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000253 else if (AM.BlockAddr)
Dan Gohman29cbade2009-11-20 23:18:13 +0000254 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
255 true, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000258
259 if (AM.Segment.getNode())
260 Segment = AM.Segment;
261 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000263 }
264
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000265 /// getI8Imm - Return a target constant with the specified value, of type
266 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000269 }
270
Chris Lattnerc961eea2005-11-16 01:54:32 +0000271 /// getI32Imm - Return a target constant with the specified value, of type
272 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000273 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000275 }
Evan Chengf597dc72006-02-10 22:24:32 +0000276
Dan Gohman8b746962008-09-23 18:22:58 +0000277 /// getGlobalBaseReg - Return an SDNode that returns the value of
278 /// the global base register. Output instructions required to
279 /// initialize the global base register, if necessary.
280 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000281 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000282
Dan Gohmanc5534622009-06-03 20:20:00 +0000283 /// getTargetMachine - Return a reference to the TargetMachine, casted
284 /// to the target-specific type.
285 const X86TargetMachine &getTargetMachine() {
286 return static_cast<const X86TargetMachine &>(TM);
287 }
288
289 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
290 /// to the target-specific type.
291 const X86InstrInfo *getInstrInfo() {
292 return getTargetMachine().getInstrInfo();
293 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000294 };
295}
296
Evan Chengf4b4c412006-08-08 00:31:00 +0000297
Evan Cheng014bf212010-02-15 19:41:07 +0000298bool
299X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000300 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000301
Evan Cheng014bf212010-02-15 19:41:07 +0000302 if (!N.hasOneUse())
303 return false;
304
305 if (N.getOpcode() != ISD::LOAD)
306 return true;
307
308 // If N is a load, do additional profitability checks.
309 if (U == Root) {
Evan Cheng884c70c2008-11-27 00:49:46 +0000310 switch (U->getOpcode()) {
311 default: break;
Dan Gohman9ef51c82010-01-04 20:51:50 +0000312 case X86ISD::ADD:
313 case X86ISD::SUB:
314 case X86ISD::AND:
315 case X86ISD::XOR:
316 case X86ISD::OR:
Evan Cheng884c70c2008-11-27 00:49:46 +0000317 case ISD::ADD:
318 case ISD::ADDC:
319 case ISD::ADDE:
320 case ISD::AND:
321 case ISD::OR:
322 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000323 SDValue Op1 = U->getOperand(1);
324
Evan Cheng884c70c2008-11-27 00:49:46 +0000325 // If the other operand is a 8-bit immediate we should fold the immediate
326 // instead. This reduces code size.
327 // e.g.
328 // movl 4(%esp), %eax
329 // addl $4, %eax
330 // vs.
331 // movl $4, %eax
332 // addl 4(%esp), %eax
333 // The former is 2 bytes shorter. In case where the increment is 1, then
334 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000335 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000336 if (Imm->getAPIntValue().isSignedIntN(8))
337 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000338
339 // If the other operand is a TLS address, we should fold it instead.
340 // This produces
341 // movl %gs:0, %eax
342 // leal i@NTPOFF(%eax), %eax
343 // instead of
344 // movl $i@NTPOFF, %eax
345 // addl %gs:0, %eax
346 // if the block also has an access to a second TLS address this will save
347 // a load.
348 // FIXME: This is probably also true for non TLS addresses.
349 if (Op1.getOpcode() == X86ISD::Wrapper) {
350 SDValue Val = Op1.getOperand(0);
351 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
352 return false;
353 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000354 }
355 }
Evan Cheng014bf212010-02-15 19:41:07 +0000356 }
357
358 return true;
359}
360
Evan Chengf48ef032010-03-14 03:48:46 +0000361/// MoveBelowCallOrigChain - Replace the original chain operand of the call with
362/// load's chain operand and move load below the call's chain operand.
363static void MoveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load,
364 SDValue Call, SDValue OrigChain) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000365 SmallVector<SDValue, 8> Ops;
Evan Chengf48ef032010-03-14 03:48:46 +0000366 SDValue Chain = OrigChain.getOperand(0);
Evan Cheng5b2e5892009-01-26 18:43:34 +0000367 if (Chain.getNode() == Load.getNode())
368 Ops.push_back(Load.getOperand(0));
369 else {
370 assert(Chain.getOpcode() == ISD::TokenFactor &&
Evan Chengf48ef032010-03-14 03:48:46 +0000371 "Unexpected chain operand");
Evan Cheng5b2e5892009-01-26 18:43:34 +0000372 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
373 if (Chain.getOperand(i).getNode() == Load.getNode())
374 Ops.push_back(Load.getOperand(0));
375 else
376 Ops.push_back(Chain.getOperand(i));
377 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000378 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000380 Ops.clear();
381 Ops.push_back(NewChain);
382 }
Evan Chengf48ef032010-03-14 03:48:46 +0000383 for (unsigned i = 1, e = OrigChain.getNumOperands(); i != e; ++i)
384 Ops.push_back(OrigChain.getOperand(i));
Dan Gohman027657d2010-06-18 15:30:29 +0000385 CurDAG->UpdateNodeOperands(OrigChain.getNode(), &Ops[0], Ops.size());
386 CurDAG->UpdateNodeOperands(Load.getNode(), Call.getOperand(0),
Evan Chengab6c3bb2008-08-25 21:27:18 +0000387 Load.getOperand(1), Load.getOperand(2));
388 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000389 Ops.push_back(SDValue(Load.getNode(), 1));
390 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000391 Ops.push_back(Call.getOperand(i));
Dan Gohman027657d2010-06-18 15:30:29 +0000392 CurDAG->UpdateNodeOperands(Call.getNode(), &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000393}
394
395/// isCalleeLoad - Return true if call address is a load and it can be
396/// moved below CALLSEQ_START and the chains leading up to the call.
397/// Return the CALLSEQ_START by reference as a second output.
Evan Chengf48ef032010-03-14 03:48:46 +0000398/// In the case of a tail call, there isn't a callseq node between the call
399/// chain and the load.
400static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000401 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000402 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000403 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000404 if (!LD ||
405 LD->isVolatile() ||
406 LD->getAddressingMode() != ISD::UNINDEXED ||
407 LD->getExtensionType() != ISD::NON_EXTLOAD)
408 return false;
409
410 // Now let's find the callseq_start.
Evan Chengf48ef032010-03-14 03:48:46 +0000411 while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000412 if (!Chain.hasOneUse())
413 return false;
414 Chain = Chain.getOperand(0);
415 }
Evan Chengf48ef032010-03-14 03:48:46 +0000416
417 if (!Chain.getNumOperands())
418 return false;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000419 if (Chain.getOperand(0).getNode() == Callee.getNode())
420 return true;
421 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000422 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
423 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000424 return true;
425 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000426}
427
Chris Lattnerfb444af2010-03-02 23:12:51 +0000428void X86DAGToDAGISel::PreprocessISelDAG() {
Chris Lattner97d85342010-03-04 01:43:43 +0000429 // OptForSize is used in pattern predicates that isel is matching.
Chris Lattnerfb444af2010-03-02 23:12:51 +0000430 OptForSize = MF->getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Chad Rosiera20e1e72012-08-01 18:39:17 +0000431
Dan Gohmanf350b272008-08-23 02:25:05 +0000432 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
433 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000434 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
Chris Lattnerfb444af2010-03-02 23:12:51 +0000435
Evan Chengf48ef032010-03-14 03:48:46 +0000436 if (OptLevel != CodeGenOpt::None &&
437 (N->getOpcode() == X86ISD::CALL ||
438 N->getOpcode() == X86ISD::TC_RETURN)) {
Chris Lattnerfb444af2010-03-02 23:12:51 +0000439 /// Also try moving call address load from outside callseq_start to just
440 /// before the call to allow it to be folded.
441 ///
442 /// [Load chain]
443 /// ^
444 /// |
445 /// [Load]
446 /// ^ ^
447 /// | |
448 /// / \--
449 /// / |
450 ///[CALLSEQ_START] |
451 /// ^ |
452 /// | |
453 /// [LOAD/C2Reg] |
454 /// | |
455 /// \ /
456 /// \ /
457 /// [CALL]
Evan Chengf48ef032010-03-14 03:48:46 +0000458 bool HasCallSeq = N->getOpcode() == X86ISD::CALL;
Chris Lattnerfb444af2010-03-02 23:12:51 +0000459 SDValue Chain = N->getOperand(0);
460 SDValue Load = N->getOperand(1);
Evan Chengf48ef032010-03-14 03:48:46 +0000461 if (!isCalleeLoad(Load, Chain, HasCallSeq))
Chris Lattnerfb444af2010-03-02 23:12:51 +0000462 continue;
Evan Chengf48ef032010-03-14 03:48:46 +0000463 MoveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain);
Chris Lattnerfb444af2010-03-02 23:12:51 +0000464 ++NumLoadMoved;
465 continue;
466 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000467
Chris Lattnerfb444af2010-03-02 23:12:51 +0000468 // Lower fpround and fpextend nodes that target the FP stack to be store and
469 // load to the stack. This is a gross hack. We would like to simply mark
470 // these as being illegal, but when we do that, legalize produces these when
471 // it expands calls, then expands these in the same legalize pass. We would
472 // like dag combine to be able to hack on these between the call expansion
473 // and the node legalization. As such this pass basically does "really
474 // late" legalization of these inline with the X86 isel pass.
475 // FIXME: This should only happen when not compiled with -O0.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000476 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
477 continue;
Chad Rosiera20e1e72012-08-01 18:39:17 +0000478
Owen Andersone50ed302009-08-10 22:56:29 +0000479 EVT SrcVT = N->getOperand(0).getValueType();
480 EVT DstVT = N->getValueType(0);
Bruno Cardoso Lopesaed890b2011-08-01 21:54:05 +0000481
482 // If any of the sources are vectors, no fp stack involved.
483 if (SrcVT.isVector() || DstVT.isVector())
484 continue;
485
486 // If the source and destination are SSE registers, then this is a legal
487 // conversion that should not be lowered.
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 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000501
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;
Chad Rosiera20e1e72012-08-01 18:39:17 +0000510
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();
Chad Rosiera20e1e72012-08-01 18:39:17 +0000513
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000514 // 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),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000517 MemTmp, MachinePointerInfo(), MemVT,
David Greenedb8d9892010-02-15 16:57:43 +0000518 false, false, 0);
Stuart Hastingsa9011292011-02-16 16:23:55 +0000519 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000520 MachinePointerInfo(),
521 MemVT, false, false, 0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000522
523 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
524 // extload we created. This will cause general havok on the dag because
525 // anything below the conversion could be folded into other existing nodes.
526 // To avoid invalidating 'I', back it up to the convert node.
527 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000528 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chad Rosiera20e1e72012-08-01 18:39:17 +0000529
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000530 // Now that we did that, the node is dead. Increment the iterator to the
531 // next node to process, then delete N.
532 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000533 CurDAG->DeleteNode(N);
Chad Rosiera20e1e72012-08-01 18:39:17 +0000534 }
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000535}
536
Chris Lattnerc961eea2005-11-16 01:54:32 +0000537
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000538/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
539/// the main function.
540void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
541 MachineFrameInfo *MFI) {
542 const TargetInstrInfo *TII = TM.getInstrInfo();
Bill Wendling78d15762011-01-06 00:47:10 +0000543 if (Subtarget->isTargetCygMing()) {
544 unsigned CallOp =
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +0000545 Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000546 BuildMI(BB, DebugLoc(),
Bill Wendling78d15762011-01-06 00:47:10 +0000547 TII->get(CallOp)).addExternalSymbol("__main");
548 }
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000549}
550
Dan Gohman64652652010-04-14 20:17:22 +0000551void X86DAGToDAGISel::EmitFunctionEntryCode() {
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000552 // If this is main, emit special code for main.
Dan Gohman64652652010-04-14 20:17:22 +0000553 if (const Function *Fn = MF->getFunction())
554 if (Fn->hasExternalLinkage() && Fn->getName() == "main")
555 EmitSpecialCodeForMain(MF->begin(), MF->getFrameInfo());
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000556}
557
Eli Friedman2a019462011-07-13 21:29:53 +0000558static bool isDispSafeForFrameIndex(int64_t Val) {
559 // On 64-bit platforms, we can run into an issue where a frame index
560 // includes a displacement that, when added to the explicit displacement,
561 // will overflow the displacement field. Assuming that the frame index
562 // displacement fits into a 31-bit integer (which is only slightly more
563 // aggressive than the current fundamental assumption that it fits into
564 // a 32-bit integer), a 31-bit disp should always be safe.
565 return isInt<31>(Val);
566}
567
Eli Friedman4977eb52011-07-13 20:44:23 +0000568bool X86DAGToDAGISel::FoldOffsetIntoAddress(uint64_t Offset,
569 X86ISelAddressMode &AM) {
570 int64_t Val = AM.Disp + Offset;
571 CodeModel::Model M = TM.getCodeModel();
Eli Friedman2a019462011-07-13 21:29:53 +0000572 if (Subtarget->is64Bit()) {
573 if (!X86::isOffsetSuitableForCodeModel(Val, M,
574 AM.hasSymbolicDisplacement()))
575 return true;
576 // In addition to the checks required for a register base, check that
577 // we do not try to use an unsafe Disp with a frame index.
578 if (AM.BaseType == X86ISelAddressMode::FrameIndexBase &&
579 !isDispSafeForFrameIndex(Val))
580 return true;
Eli Friedman4977eb52011-07-13 20:44:23 +0000581 }
Eli Friedman2a019462011-07-13 21:29:53 +0000582 AM.Disp = Val;
583 return false;
584
Eli Friedman4977eb52011-07-13 20:44:23 +0000585}
Rafael Espindola094fad32009-04-08 21:14:34 +0000586
Chris Lattnerf93b90c2010-09-22 04:39:11 +0000587bool X86DAGToDAGISel::MatchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){
588 SDValue Address = N->getOperand(1);
Chad Rosiera20e1e72012-08-01 18:39:17 +0000589
Chris Lattnerf93b90c2010-09-22 04:39:11 +0000590 // load gs:0 -> GS segment register.
591 // load fs:0 -> FS segment register.
592 //
Rafael Espindola094fad32009-04-08 21:14:34 +0000593 // This optimization is valid because the GNU TLS model defines that
594 // gs:0 (or fs:0 on X86-64) contains its own address.
595 // For more information see http://people.redhat.com/drepper/tls.pdf
Chris Lattnerf93b90c2010-09-22 04:39:11 +0000596 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address))
597 if (C->getSExtValue() == 0 && AM.Segment.getNode() == 0 &&
David Chisnall23a62cb2012-07-24 20:04:16 +0000598 Subtarget->isTargetLinux())
Chris Lattnerf93b90c2010-09-22 04:39:11 +0000599 switch (N->getPointerInfo().getAddrSpace()) {
600 case 256:
601 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
602 return false;
603 case 257:
604 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
605 return false;
606 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000607
Rafael Espindola094fad32009-04-08 21:14:34 +0000608 return true;
609}
610
Chris Lattner18c59872009-06-27 04:16:01 +0000611/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
612/// into an addressing mode. These wrap things that will resolve down into a
613/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000614/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000615bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000616 // If the addressing mode already has a symbol as the displacement, we can
617 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000618 if (AM.hasSymbolicDisplacement())
619 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000620
621 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000622 CodeModel::Model M = TM.getCodeModel();
623
Chris Lattner18c59872009-06-27 04:16:01 +0000624 // Handle X86-64 rip-relative addresses. We check this before checking direct
625 // folding because RIP is preferable to non-RIP accesses.
Chandler Carruthab5a55e2012-04-09 02:13:06 +0000626 if (Subtarget->is64Bit() && N.getOpcode() == X86ISD::WrapperRIP &&
Chris Lattner18c59872009-06-27 04:16:01 +0000627 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
628 // they cannot be folded into immediate fields.
629 // FIXME: This can be improved for kernel and other models?
Chandler Carruthab5a55e2012-04-09 02:13:06 +0000630 (M == CodeModel::Small || M == CodeModel::Kernel)) {
631 // Base and index reg must be 0 in order to use %rip as base.
632 if (AM.hasBaseOrIndexReg())
633 return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000634 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Eli Friedman4977eb52011-07-13 20:44:23 +0000635 X86ISelAddressMode Backup = AM;
Chris Lattner18c59872009-06-27 04:16:01 +0000636 AM.GV = G->getGlobal();
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000637 AM.SymbolFlags = G->getTargetFlags();
Eli Friedman4977eb52011-07-13 20:44:23 +0000638 if (FoldOffsetIntoAddress(G->getOffset(), AM)) {
639 AM = Backup;
640 return true;
641 }
Chris Lattner18c59872009-06-27 04:16:01 +0000642 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Eli Friedman4977eb52011-07-13 20:44:23 +0000643 X86ISelAddressMode Backup = AM;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000644 AM.CP = CP->getConstVal();
645 AM.Align = CP->getAlignment();
Chris Lattner0b0deab2009-06-26 05:56:49 +0000646 AM.SymbolFlags = CP->getTargetFlags();
Eli Friedman4977eb52011-07-13 20:44:23 +0000647 if (FoldOffsetIntoAddress(CP->getOffset(), AM)) {
648 AM = Backup;
649 return true;
650 }
Chris Lattner18c59872009-06-27 04:16:01 +0000651 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
652 AM.ES = S->getSymbol();
653 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000654 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000655 AM.JT = J->getIndex();
656 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000657 } else {
658 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000659 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000660 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000661
Chris Lattner18c59872009-06-27 04:16:01 +0000662 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000664 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000665 }
666
667 // Handle the case when globals fit in our immediate field: This is true for
Chandler Carruthab5a55e2012-04-09 02:13:06 +0000668 // X86-32 always and X86-64 when in -mcmodel=small mode. In 64-bit
669 // mode, this only applies to a non-RIP-relative computation.
Chris Lattner18c59872009-06-27 04:16:01 +0000670 if (!Subtarget->is64Bit() ||
Chandler Carruthab5a55e2012-04-09 02:13:06 +0000671 M == CodeModel::Small || M == CodeModel::Kernel) {
672 assert(N.getOpcode() != X86ISD::WrapperRIP &&
673 "RIP-relative addressing already handled");
Chris Lattner18c59872009-06-27 04:16:01 +0000674 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
675 AM.GV = G->getGlobal();
676 AM.Disp += G->getOffset();
677 AM.SymbolFlags = G->getTargetFlags();
678 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
679 AM.CP = CP->getConstVal();
680 AM.Align = CP->getAlignment();
681 AM.Disp += CP->getOffset();
682 AM.SymbolFlags = CP->getTargetFlags();
683 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
684 AM.ES = S->getSymbol();
685 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000686 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000687 AM.JT = J->getIndex();
688 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000689 } else {
690 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000691 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000692 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000693 return false;
694 }
695
696 return true;
697}
698
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000699/// MatchAddress - Add the specified node to the specified addressing mode,
700/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000701/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000702bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
Dan Gohmane5408102010-06-18 01:24:29 +0000703 if (MatchAddressRecursively(N, AM, 0))
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000704 return true;
705
706 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
707 // a smaller encoding and avoids a scaled-index.
708 if (AM.Scale == 2 &&
709 AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +0000710 AM.Base_Reg.getNode() == 0) {
711 AM.Base_Reg = AM.IndexReg;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000712 AM.Scale = 1;
713 }
714
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000715 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
716 // because it has a smaller encoding.
717 // TODO: Which other code models can use this?
718 if (TM.getCodeModel() == CodeModel::Small &&
719 Subtarget->is64Bit() &&
720 AM.Scale == 1 &&
721 AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +0000722 AM.Base_Reg.getNode() == 0 &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000723 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000724 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000725 AM.hasSymbolicDisplacement())
Dan Gohmanffce6f12010-04-29 23:30:41 +0000726 AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000727
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000728 return false;
729}
730
Chandler Carruthd65a9102012-01-11 11:04:36 +0000731// Insert a node into the DAG at least before the Pos node's position. This
732// will reposition the node as needed, and will assign it a node ID that is <=
733// the Pos node's ID. Note that this does *not* preserve the uniqueness of node
734// IDs! The selection DAG must no longer depend on their uniqueness when this
735// is used.
736static void InsertDAGNode(SelectionDAG &DAG, SDValue Pos, SDValue N) {
737 if (N.getNode()->getNodeId() == -1 ||
738 N.getNode()->getNodeId() > Pos.getNode()->getNodeId()) {
739 DAG.RepositionNode(Pos.getNode(), N.getNode());
740 N.getNode()->setNodeId(Pos.getNode()->getNodeId());
741 }
742}
743
Chandler Carruth6ae18e52012-01-11 08:48:20 +0000744// Transform "(X >> (8-C1)) & C2" to "(X >> 8) & 0xff)" if safe. This
745// allows us to convert the shift and and into an h-register extract and
746// a scaled index. Returns false if the simplification is performed.
747static bool FoldMaskAndShiftToExtract(SelectionDAG &DAG, SDValue N,
748 uint64_t Mask,
749 SDValue Shift, SDValue X,
750 X86ISelAddressMode &AM) {
751 if (Shift.getOpcode() != ISD::SRL ||
752 !isa<ConstantSDNode>(Shift.getOperand(1)) ||
753 !Shift.hasOneUse())
754 return true;
755
756 int ScaleLog = 8 - Shift.getConstantOperandVal(1);
757 if (ScaleLog <= 0 || ScaleLog >= 4 ||
758 Mask != (0xffu << ScaleLog))
759 return true;
760
761 EVT VT = N.getValueType();
762 DebugLoc DL = N.getDebugLoc();
763 SDValue Eight = DAG.getConstant(8, MVT::i8);
764 SDValue NewMask = DAG.getConstant(0xff, VT);
765 SDValue Srl = DAG.getNode(ISD::SRL, DL, VT, X, Eight);
766 SDValue And = DAG.getNode(ISD::AND, DL, VT, Srl, NewMask);
767 SDValue ShlCount = DAG.getConstant(ScaleLog, MVT::i8);
768 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, And, ShlCount);
769
Chandler Carruth0fe9a922012-01-12 01:34:44 +0000770 // Insert the new nodes into the topological ordering. We must do this in
771 // a valid topological ordering as nothing is going to go back and re-sort
772 // these nodes. We continually insert before 'N' in sequence as this is
773 // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
774 // hierarchy left to express.
775 InsertDAGNode(DAG, N, Eight);
776 InsertDAGNode(DAG, N, Srl);
777 InsertDAGNode(DAG, N, NewMask);
Chandler Carruthd65a9102012-01-11 11:04:36 +0000778 InsertDAGNode(DAG, N, And);
Chandler Carruth0fe9a922012-01-12 01:34:44 +0000779 InsertDAGNode(DAG, N, ShlCount);
Chandler Carruthd65a9102012-01-11 11:04:36 +0000780 InsertDAGNode(DAG, N, Shl);
Chandler Carruth6ae18e52012-01-11 08:48:20 +0000781 DAG.ReplaceAllUsesWith(N, Shl);
782 AM.IndexReg = And;
783 AM.Scale = (1 << ScaleLog);
784 return false;
785}
786
Chandler Carruthfde2c1a2012-01-11 09:35:00 +0000787// Transforms "(X << C1) & C2" to "(X & (C2>>C1)) << C1" if safe and if this
788// allows us to fold the shift into this addressing mode. Returns false if the
789// transform succeeded.
790static bool FoldMaskedShiftToScaledMask(SelectionDAG &DAG, SDValue N,
791 uint64_t Mask,
792 SDValue Shift, SDValue X,
793 X86ISelAddressMode &AM) {
794 if (Shift.getOpcode() != ISD::SHL ||
795 !isa<ConstantSDNode>(Shift.getOperand(1)))
796 return true;
797
798 // Not likely to be profitable if either the AND or SHIFT node has more
799 // than one use (unless all uses are for address computation). Besides,
800 // isel mechanism requires their node ids to be reused.
801 if (!N.hasOneUse() || !Shift.hasOneUse())
802 return true;
803
804 // Verify that the shift amount is something we can fold.
805 unsigned ShiftAmt = Shift.getConstantOperandVal(1);
806 if (ShiftAmt != 1 && ShiftAmt != 2 && ShiftAmt != 3)
807 return true;
808
809 EVT VT = N.getValueType();
810 DebugLoc DL = N.getDebugLoc();
811 SDValue NewMask = DAG.getConstant(Mask >> ShiftAmt, VT);
812 SDValue NewAnd = DAG.getNode(ISD::AND, DL, VT, X, NewMask);
813 SDValue NewShift = DAG.getNode(ISD::SHL, DL, VT, NewAnd, Shift.getOperand(1));
814
Chandler Carruth0fe9a922012-01-12 01:34:44 +0000815 // Insert the new nodes into the topological ordering. We must do this in
816 // a valid topological ordering as nothing is going to go back and re-sort
817 // these nodes. We continually insert before 'N' in sequence as this is
818 // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
819 // hierarchy left to express.
820 InsertDAGNode(DAG, N, NewMask);
821 InsertDAGNode(DAG, N, NewAnd);
Chandler Carruthd65a9102012-01-11 11:04:36 +0000822 InsertDAGNode(DAG, N, NewShift);
Chandler Carruthfde2c1a2012-01-11 09:35:00 +0000823 DAG.ReplaceAllUsesWith(N, NewShift);
824
825 AM.Scale = 1 << ShiftAmt;
826 AM.IndexReg = NewAnd;
827 return false;
828}
829
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000830// Implement some heroics to detect shifts of masked values where the mask can
831// be replaced by extending the shift and undoing that in the addressing mode
832// scale. Patterns such as (shl (srl x, c1), c2) are canonicalized into (and
833// (srl x, SHIFT), MASK) by DAGCombines that don't know the shl can be done in
834// the addressing mode. This results in code such as:
835//
836// int f(short *y, int *lookup_table) {
837// ...
838// return *y + lookup_table[*y >> 11];
839// }
840//
841// Turning into:
842// movzwl (%rdi), %eax
843// movl %eax, %ecx
844// shrl $11, %ecx
845// addl (%rsi,%rcx,4), %eax
846//
847// Instead of:
848// movzwl (%rdi), %eax
849// movl %eax, %ecx
850// shrl $9, %ecx
851// andl $124, %rcx
852// addl (%rsi,%rcx), %eax
853//
Chandler Carruthdddcd782012-01-11 09:35:02 +0000854// Note that this function assumes the mask is provided as a mask *after* the
855// value is shifted. The input chain may or may not match that, but computing
856// such a mask is trivial.
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000857static bool FoldMaskAndShiftToScale(SelectionDAG &DAG, SDValue N,
Chandler Carruthdddcd782012-01-11 09:35:02 +0000858 uint64_t Mask,
859 SDValue Shift, SDValue X,
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000860 X86ISelAddressMode &AM) {
Chandler Carruthdddcd782012-01-11 09:35:02 +0000861 if (Shift.getOpcode() != ISD::SRL || !Shift.hasOneUse() ||
862 !isa<ConstantSDNode>(Shift.getOperand(1)))
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000863 return true;
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000864
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000865 unsigned ShiftAmt = Shift.getConstantOperandVal(1);
866 unsigned MaskLZ = CountLeadingZeros_64(Mask);
867 unsigned MaskTZ = CountTrailingZeros_64(Mask);
868
869 // The amount of shift we're trying to fit into the addressing mode is taken
Chandler Carruthdddcd782012-01-11 09:35:02 +0000870 // from the trailing zeros of the mask.
871 unsigned AMShiftAmt = MaskTZ;
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000872
873 // There is nothing we can do here unless the mask is removing some bits.
874 // Also, the addressing mode can only represent shifts of 1, 2, or 3 bits.
875 if (AMShiftAmt <= 0 || AMShiftAmt > 3) return true;
876
877 // We also need to ensure that mask is a continuous run of bits.
878 if (CountTrailingOnes_64(Mask >> MaskTZ) + MaskTZ + MaskLZ != 64) return true;
879
880 // Scale the leading zero count down based on the actual size of the value.
Chandler Carruthdddcd782012-01-11 09:35:02 +0000881 // Also scale it down based on the size of the shift.
882 MaskLZ -= (64 - X.getValueSizeInBits()) + ShiftAmt;
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000883
884 // The final check is to ensure that any masked out high bits of X are
885 // already known to be zero. Otherwise, the mask has a semantic impact
886 // other than masking out a couple of low bits. Unfortunately, because of
887 // the mask, zero extensions will be removed from operands in some cases.
888 // This code works extra hard to look through extensions because we can
889 // replace them with zero extensions cheaply if necessary.
890 bool ReplacingAnyExtend = false;
891 if (X.getOpcode() == ISD::ANY_EXTEND) {
892 unsigned ExtendBits =
893 X.getValueSizeInBits() - X.getOperand(0).getValueSizeInBits();
894 // Assume that we'll replace the any-extend with a zero-extend, and
895 // narrow the search to the extended value.
896 X = X.getOperand(0);
897 MaskLZ = ExtendBits > MaskLZ ? 0 : MaskLZ - ExtendBits;
898 ReplacingAnyExtend = true;
899 }
900 APInt MaskedHighBits = APInt::getHighBitsSet(X.getValueSizeInBits(),
901 MaskLZ);
902 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000903 DAG.ComputeMaskedBits(X, KnownZero, KnownOne);
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000904 if (MaskedHighBits != KnownZero) return true;
905
906 // We've identified a pattern that can be transformed into a single shift
907 // and an addressing mode. Make it so.
908 EVT VT = N.getValueType();
909 if (ReplacingAnyExtend) {
910 assert(X.getValueType() != VT);
911 // We looked through an ANY_EXTEND node, insert a ZERO_EXTEND.
912 SDValue NewX = DAG.getNode(ISD::ZERO_EXTEND, X.getDebugLoc(), VT, X);
Chandler Carruthd65a9102012-01-11 11:04:36 +0000913 InsertDAGNode(DAG, N, NewX);
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000914 X = NewX;
915 }
916 DebugLoc DL = N.getDebugLoc();
917 SDValue NewSRLAmt = DAG.getConstant(ShiftAmt + AMShiftAmt, MVT::i8);
918 SDValue NewSRL = DAG.getNode(ISD::SRL, DL, VT, X, NewSRLAmt);
919 SDValue NewSHLAmt = DAG.getConstant(AMShiftAmt, MVT::i8);
920 SDValue NewSHL = DAG.getNode(ISD::SHL, DL, VT, NewSRL, NewSHLAmt);
Chandler Carruth0fe9a922012-01-12 01:34:44 +0000921
922 // Insert the new nodes into the topological ordering. We must do this in
923 // a valid topological ordering as nothing is going to go back and re-sort
924 // these nodes. We continually insert before 'N' in sequence as this is
925 // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
926 // hierarchy left to express.
Chandler Carruthd65a9102012-01-11 11:04:36 +0000927 InsertDAGNode(DAG, N, NewSRLAmt);
928 InsertDAGNode(DAG, N, NewSRL);
929 InsertDAGNode(DAG, N, NewSHLAmt);
930 InsertDAGNode(DAG, N, NewSHL);
Chandler Carruthf103b3d2012-01-11 08:41:08 +0000931 DAG.ReplaceAllUsesWith(N, NewSHL);
932
933 AM.Scale = 1 << AMShiftAmt;
934 AM.IndexReg = NewSRL;
935 return false;
936}
937
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000938bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
939 unsigned Depth) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000940 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000941 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +0000942 dbgs() << "MatchAddress: ";
Bill Wendling12321672009-08-07 21:33:25 +0000943 AM.dump();
944 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000945 // Limit recursion.
946 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000947 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000948
Chris Lattner18c59872009-06-27 04:16:01 +0000949 // If this is already a %rip relative address, we can only merge immediates
950 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000951 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000952 if (AM.isRIPRelative()) {
953 // FIXME: JumpTable and ExternalSymbol address currently don't like
954 // displacements. It isn't very important, but this should be fixed for
955 // consistency.
956 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000957
Eli Friedman4977eb52011-07-13 20:44:23 +0000958 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N))
959 if (!FoldOffsetIntoAddress(Cst->getSExtValue(), AM))
Evan Cheng25ab6902006-09-08 06:48:29 +0000960 return false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000961 return true;
962 }
963
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000964 switch (N.getOpcode()) {
965 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000966 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000967 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Eli Friedman4977eb52011-07-13 20:44:23 +0000968 if (!FoldOffsetIntoAddress(Val, AM))
Evan Cheng25ab6902006-09-08 06:48:29 +0000969 return false;
Evan Cheng25ab6902006-09-08 06:48:29 +0000970 break;
971 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000972
Rafael Espindola49a168d2009-04-12 21:55:03 +0000973 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000974 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000975 if (!MatchWrapper(N, AM))
976 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000977 break;
978
Rafael Espindola094fad32009-04-08 21:14:34 +0000979 case ISD::LOAD:
Chris Lattnerf93b90c2010-09-22 04:39:11 +0000980 if (!MatchLoadInAddress(cast<LoadSDNode>(N), AM))
Rafael Espindola094fad32009-04-08 21:14:34 +0000981 return false;
982 break;
983
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000984 case ISD::FrameIndex:
Eli Friedman2a019462011-07-13 21:29:53 +0000985 if (AM.BaseType == X86ISelAddressMode::RegBase &&
986 AM.Base_Reg.getNode() == 0 &&
987 (!Subtarget->is64Bit() || isDispSafeForFrameIndex(AM.Disp))) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000988 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Dan Gohmanffce6f12010-04-29 23:30:41 +0000989 AM.Base_FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000990 return false;
991 }
992 break;
Evan Chengec693f72005-12-08 02:01:35 +0000993
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000994 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000995 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000996 break;
Chad Rosiera20e1e72012-08-01 18:39:17 +0000997
Gabor Greif93c53e52008-08-31 15:37:04 +0000998 if (ConstantSDNode
999 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001000 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001001 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
1002 // that the base operand remains free for further matching. If
1003 // the base doesn't end up getting used, a post-processing step
1004 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001005 if (Val == 1 || Val == 2 || Val == 3) {
1006 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +00001007 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001008
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001009 // Okay, we know that we have a scale by now. However, if the scaled
1010 // value is an add of something and a constant, we can fold the
1011 // constant into the disp field here.
Chris Lattner0a9481f2011-02-13 22:25:43 +00001012 if (CurDAG->isBaseWithConstantOffset(ShVal)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001013 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001014 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +00001015 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Richard Smith1144af32012-08-24 23:29:28 +00001016 uint64_t Disp = (uint64_t)AddVal->getSExtValue() << Val;
Eli Friedman4977eb52011-07-13 20:44:23 +00001017 if (!FoldOffsetIntoAddress(Disp, AM))
1018 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001019 }
Eli Friedman4977eb52011-07-13 20:44:23 +00001020
1021 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001022 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001023 }
1024 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001025 }
Evan Chengec693f72005-12-08 02:01:35 +00001026
Chandler Carruthdddcd782012-01-11 09:35:02 +00001027 case ISD::SRL: {
1028 // Scale must not be used already.
1029 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
1030
1031 SDValue And = N.getOperand(0);
1032 if (And.getOpcode() != ISD::AND) break;
1033 SDValue X = And.getOperand(0);
1034
1035 // We only handle up to 64-bit values here as those are what matter for
1036 // addressing mode optimizations.
1037 if (X.getValueSizeInBits() > 64) break;
1038
1039 // The mask used for the transform is expected to be post-shift, but we
1040 // found the shift first so just apply the shift to the mask before passing
1041 // it down.
1042 if (!isa<ConstantSDNode>(N.getOperand(1)) ||
1043 !isa<ConstantSDNode>(And.getOperand(1)))
1044 break;
1045 uint64_t Mask = And.getConstantOperandVal(1) >> N.getConstantOperandVal(1);
1046
Chandler Carruthf103b3d2012-01-11 08:41:08 +00001047 // Try to fold the mask and shift into the scale, and return false if we
1048 // succeed.
Chandler Carruthdddcd782012-01-11 09:35:02 +00001049 if (!FoldMaskAndShiftToScale(*CurDAG, N, Mask, N, X, AM))
Chandler Carruthf103b3d2012-01-11 08:41:08 +00001050 return false;
1051 break;
Chandler Carruthdddcd782012-01-11 09:35:02 +00001052 }
Chandler Carruthf103b3d2012-01-11 08:41:08 +00001053
Dan Gohman83688052007-10-22 20:22:24 +00001054 case ISD::SMUL_LOHI:
1055 case ISD::UMUL_LOHI:
1056 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001057 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +00001058 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001059 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +00001060 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001061 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001062 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +00001063 AM.Base_Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +00001064 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +00001065 if (ConstantSDNode
1066 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001067 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
1068 CN->getZExtValue() == 9) {
1069 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001070
Gabor Greifba36cb52008-08-28 21:40:38 +00001071 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001072 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001073
1074 // Okay, we know that we have a scale by now. However, if the scaled
1075 // value is an add of something and a constant, we can fold the
1076 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +00001077 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
1078 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
1079 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001080 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +00001081 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Eli Friedman4977eb52011-07-13 20:44:23 +00001082 uint64_t Disp = AddVal->getSExtValue() * CN->getZExtValue();
1083 if (FoldOffsetIntoAddress(Disp, AM))
Gabor Greifba36cb52008-08-28 21:40:38 +00001084 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001085 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001086 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001087 }
1088
Dan Gohmanffce6f12010-04-29 23:30:41 +00001089 AM.IndexReg = AM.Base_Reg = Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001090 return false;
1091 }
Chris Lattner62412262007-02-04 20:18:17 +00001092 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001093 break;
1094
Dan Gohman3cd90a12009-05-11 18:02:53 +00001095 case ISD::SUB: {
1096 // Given A-B, if A can be completely folded into the address and
1097 // the index field with the index field unused, use -B as the index.
1098 // This is a win if a has multiple parts that can be folded into
1099 // the address. Also, this saves a mov if the base register has
1100 // other uses, since it avoids a two-address sub instruction, however
1101 // it costs an additional mov if the index register has other uses.
1102
Dan Gohmane5408102010-06-18 01:24:29 +00001103 // Add an artificial use to this node so that we can keep track of
1104 // it if it gets CSE'd with a different node.
1105 HandleSDNode Handle(N);
1106
Dan Gohman3cd90a12009-05-11 18:02:53 +00001107 // Test if the LHS of the sub can be folded.
1108 X86ISelAddressMode Backup = AM;
Dan Gohmane5408102010-06-18 01:24:29 +00001109 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001110 AM = Backup;
1111 break;
1112 }
1113 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001114 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001115 AM = Backup;
1116 break;
1117 }
Evan Chengf3caa522010-03-17 23:58:35 +00001118
Dan Gohman3cd90a12009-05-11 18:02:53 +00001119 int Cost = 0;
Dan Gohmane5408102010-06-18 01:24:29 +00001120 SDValue RHS = Handle.getValue().getNode()->getOperand(1);
Dan Gohman3cd90a12009-05-11 18:02:53 +00001121 // If the RHS involves a register with multiple uses, this
1122 // transformation incurs an extra mov, due to the neg instruction
1123 // clobbering its operand.
1124 if (!RHS.getNode()->hasOneUse() ||
1125 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1126 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1127 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1128 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001129 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001130 ++Cost;
1131 // If the base is a register with multiple uses, this
1132 // transformation may save a mov.
1133 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +00001134 AM.Base_Reg.getNode() &&
1135 !AM.Base_Reg.getNode()->hasOneUse()) ||
Dan Gohman3cd90a12009-05-11 18:02:53 +00001136 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1137 --Cost;
1138 // If the folded LHS was interesting, this transformation saves
1139 // address arithmetic.
1140 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1141 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1142 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1143 --Cost;
1144 // If it doesn't look like it may be an overall win, don't do it.
1145 if (Cost >= 0) {
1146 AM = Backup;
1147 break;
1148 }
1149
1150 // Ok, the transformation is legal and appears profitable. Go for it.
1151 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1152 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1153 AM.IndexReg = Neg;
1154 AM.Scale = 1;
1155
1156 // Insert the new nodes into the topological ordering.
Chandler Carruthd65a9102012-01-11 11:04:36 +00001157 InsertDAGNode(*CurDAG, N, Zero);
1158 InsertDAGNode(*CurDAG, N, Neg);
Dan Gohman3cd90a12009-05-11 18:02:53 +00001159 return false;
1160 }
1161
Evan Cheng8e278262009-01-17 07:09:27 +00001162 case ISD::ADD: {
Dan Gohmane5408102010-06-18 01:24:29 +00001163 // Add an artificial use to this node so that we can keep track of
1164 // it if it gets CSE'd with a different node.
1165 HandleSDNode Handle(N);
Dan Gohmane5408102010-06-18 01:24:29 +00001166
Evan Cheng8e278262009-01-17 07:09:27 +00001167 X86ISelAddressMode Backup = AM;
Chris Lattnerdec28ce2011-01-16 08:48:11 +00001168 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
1169 !MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1))
Dan Gohmane5408102010-06-18 01:24:29 +00001170 return false;
1171 AM = Backup;
Chad Rosiera20e1e72012-08-01 18:39:17 +00001172
Evan Chengf3caa522010-03-17 23:58:35 +00001173 // Try again after commuting the operands.
Chris Lattnerdec28ce2011-01-16 08:48:11 +00001174 if (!MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)&&
1175 !MatchAddressRecursively(Handle.getValue().getOperand(0), AM, Depth+1))
Dan Gohmane5408102010-06-18 01:24:29 +00001176 return false;
Evan Cheng8e278262009-01-17 07:09:27 +00001177 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001178
1179 // If we couldn't fold both operands into the address at the same time,
1180 // see if we can just put each operand into a register and fold at least
1181 // the add.
1182 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohmanffce6f12010-04-29 23:30:41 +00001183 !AM.Base_Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001184 !AM.IndexReg.getNode()) {
Chris Lattnerdec28ce2011-01-16 08:48:11 +00001185 N = Handle.getValue();
1186 AM.Base_Reg = N.getOperand(0);
1187 AM.IndexReg = N.getOperand(1);
Dan Gohman77502c92009-03-13 02:25:09 +00001188 AM.Scale = 1;
1189 return false;
1190 }
Chris Lattnerdec28ce2011-01-16 08:48:11 +00001191 N = Handle.getValue();
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001192 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001193 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001194
Chris Lattner62412262007-02-04 20:18:17 +00001195 case ISD::OR:
1196 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner0a9481f2011-02-13 22:25:43 +00001197 if (CurDAG->isBaseWithConstantOffset(N)) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001198 X86ISelAddressMode Backup = AM;
Chris Lattnerd6139422010-04-20 23:18:40 +00001199 ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1));
Evan Chengf3caa522010-03-17 23:58:35 +00001200
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001201 // Start with the LHS as an addr mode.
Dan Gohmane5408102010-06-18 01:24:29 +00001202 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Eli Friedman4977eb52011-07-13 20:44:23 +00001203 !FoldOffsetIntoAddress(CN->getSExtValue(), AM))
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001204 return false;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001205 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001206 }
1207 break;
Chad Rosiera20e1e72012-08-01 18:39:17 +00001208
Evan Cheng1314b002007-12-13 00:43:27 +00001209 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001210 // Perform some heroic transforms on an and of a constant-count shift
1211 // with a constant to enable use of the scaled offset field.
1212
Evan Cheng1314b002007-12-13 00:43:27 +00001213 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001214 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001215
Chandler Carruthfde2c1a2012-01-11 09:35:00 +00001216 SDValue Shift = N.getOperand(0);
1217 if (Shift.getOpcode() != ISD::SRL && Shift.getOpcode() != ISD::SHL) break;
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001218 SDValue X = Shift.getOperand(0);
Chandler Carruthfde2c1a2012-01-11 09:35:00 +00001219
1220 // We only handle up to 64-bit values here as those are what matter for
1221 // addressing mode optimizations.
1222 if (X.getValueSizeInBits() > 64) break;
1223
Chandler Carruth93b73582012-01-11 09:35:04 +00001224 if (!isa<ConstantSDNode>(N.getOperand(1)))
1225 break;
1226 uint64_t Mask = N.getConstantOperandVal(1);
Evan Cheng1314b002007-12-13 00:43:27 +00001227
Chandler Carruth6ae18e52012-01-11 08:48:20 +00001228 // Try to fold the mask and shift into an extract and scale.
Chandler Carruth93b73582012-01-11 09:35:04 +00001229 if (!FoldMaskAndShiftToExtract(*CurDAG, N, Mask, Shift, X, AM))
Chandler Carruth6ae18e52012-01-11 08:48:20 +00001230 return false;
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001231
Chandler Carruth6ae18e52012-01-11 08:48:20 +00001232 // Try to fold the mask and shift directly into the scale.
Chandler Carruth93b73582012-01-11 09:35:04 +00001233 if (!FoldMaskAndShiftToScale(*CurDAG, N, Mask, Shift, X, AM))
Chandler Carruthf103b3d2012-01-11 08:41:08 +00001234 return false;
1235
Chandler Carruthfde2c1a2012-01-11 09:35:00 +00001236 // Try to swap the mask and shift to place shifts which can be done as
1237 // a scale on the outside of the mask.
Chandler Carruth93b73582012-01-11 09:35:04 +00001238 if (!FoldMaskedShiftToScaledMask(*CurDAG, N, Mask, Shift, X, AM))
Chandler Carruthfde2c1a2012-01-11 09:35:00 +00001239 return false;
1240 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001241 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001242 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001243
Rafael Espindola523249f2009-03-31 16:16:57 +00001244 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001245}
1246
1247/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1248/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001249bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001250 // Is the base register already occupied?
Dan Gohmanffce6f12010-04-29 23:30:41 +00001251 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001252 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001253 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001254 AM.IndexReg = N;
1255 AM.Scale = 1;
1256 return false;
1257 }
1258
1259 // Otherwise, we cannot select it.
1260 return true;
1261 }
1262
1263 // Default, generate it as a register.
1264 AM.BaseType = X86ISelAddressMode::RegBase;
Dan Gohmanffce6f12010-04-29 23:30:41 +00001265 AM.Base_Reg = N;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001266 return false;
1267}
1268
Evan Chengec693f72005-12-08 02:01:35 +00001269/// SelectAddr - returns true if it is able pattern match an addressing mode.
1270/// It returns the operands which make up the maximal addressing mode it can
1271/// match by reference.
Chris Lattnerb86faa12010-09-21 22:07:31 +00001272///
1273/// Parent is the parent node of the addr operand that is being matched. It
1274/// is always a load, store, atomic node, or null. It is only null when
1275/// checking memory operands for inline asm nodes.
1276bool X86DAGToDAGISel::SelectAddr(SDNode *Parent, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +00001277 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001278 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001279 X86ISelAddressMode AM;
Chad Rosiera20e1e72012-08-01 18:39:17 +00001280
Chris Lattnerf93b90c2010-09-22 04:39:11 +00001281 if (Parent &&
1282 // This list of opcodes are all the nodes that have an "addr:$ptr" operand
1283 // that are not a MemSDNode, and thus don't have proper addrspace info.
Chris Lattnerf93b90c2010-09-22 04:39:11 +00001284 Parent->getOpcode() != ISD::INTRINSIC_W_CHAIN && // unaligned loads, fixme
Eric Christopher56a8b812010-09-22 20:42:08 +00001285 Parent->getOpcode() != ISD::INTRINSIC_VOID && // nontemporal stores
1286 Parent->getOpcode() != X86ISD::TLSCALL) { // Fixme
Chris Lattnerf93b90c2010-09-22 04:39:11 +00001287 unsigned AddrSpace =
1288 cast<MemSDNode>(Parent)->getPointerInfo().getAddrSpace();
1289 // AddrSpace 256 -> GS, 257 -> FS.
1290 if (AddrSpace == 256)
1291 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
1292 if (AddrSpace == 257)
1293 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
1294 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00001295
Evan Chengc7928f82009-12-18 01:59:21 +00001296 if (MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001297 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001298
Owen Andersone50ed302009-08-10 22:56:29 +00001299 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001300 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Dan Gohmanffce6f12010-04-29 23:30:41 +00001301 if (!AM.Base_Reg.getNode())
1302 AM.Base_Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001303 }
Evan Cheng8700e142006-01-11 06:09:51 +00001304
Gabor Greifba36cb52008-08-28 21:40:38 +00001305 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001306 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001307
Rafael Espindola094fad32009-04-08 21:14:34 +00001308 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001309 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001310}
1311
Chris Lattner3a7cd952006-10-07 21:55:32 +00001312/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1313/// match a load whose top elements are either undef or zeros. The load flavor
1314/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner64b49862010-02-17 06:07:47 +00001315///
1316/// We also return:
Chris Lattnera170b5e2010-02-21 03:17:59 +00001317/// PatternChainNode: this is the matched node that has a chain input and
1318/// output.
Chris Lattnere60f7b42010-03-01 22:51:11 +00001319bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SDValue N, SDValue &Base,
1321 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001322 SDValue &Disp, SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +00001323 SDValue &PatternNodeWithChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001324 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001325 PatternNodeWithChain = N.getOperand(0);
1326 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1327 PatternNodeWithChain.hasOneUse() &&
Chris Lattnerf1c64282010-02-21 04:53:34 +00001328 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohmand858e902010-04-17 15:26:15 +00001329 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001330 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattnerb86faa12010-09-21 22:07:31 +00001331 if (!SelectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001332 return false;
1333 return true;
1334 }
1335 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001336
1337 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001338 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001339 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001340 // Check to see if the top elements are all zeros (or bitcast of zeros).
Chad Rosiera20e1e72012-08-01 18:39:17 +00001341 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001342 N.getOperand(0).getNode()->hasOneUse() &&
1343 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattner92d3ada2010-02-16 22:35:06 +00001344 N.getOperand(0).getOperand(0).hasOneUse() &&
1345 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohmand858e902010-04-17 15:26:15 +00001346 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00001347 // Okay, this is a zero extending load. Fold it.
1348 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattnerb86faa12010-09-21 22:07:31 +00001349 if (!SelectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001350 return false;
Chris Lattnera170b5e2010-02-21 03:17:59 +00001351 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001352 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001353 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001354 return false;
1355}
1356
1357
Evan Cheng51a9ed92006-02-25 10:09:08 +00001358/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1359/// mode it matches can be cost effectively emitted as an LEA instruction.
Chris Lattner52a261b2010-09-21 20:31:19 +00001360bool X86DAGToDAGISel::SelectLEAAddr(SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001361 SDValue &Base, SDValue &Scale,
Chris Lattner599b5312010-07-08 23:46:44 +00001362 SDValue &Index, SDValue &Disp,
1363 SDValue &Segment) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001364 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001365
1366 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1367 // segments.
1368 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001369 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001370 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001371 if (MatchAddress(N, AM))
1372 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001373 assert (T == AM.Segment);
1374 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001375
Owen Andersone50ed302009-08-10 22:56:29 +00001376 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001377 unsigned Complexity = 0;
1378 if (AM.BaseType == X86ISelAddressMode::RegBase)
Dan Gohmanffce6f12010-04-29 23:30:41 +00001379 if (AM.Base_Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001380 Complexity = 1;
1381 else
Dan Gohmanffce6f12010-04-29 23:30:41 +00001382 AM.Base_Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001383 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1384 Complexity = 4;
1385
Gabor Greifba36cb52008-08-28 21:40:38 +00001386 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001387 Complexity++;
1388 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001389 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001390
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001391 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1392 // a simple shift.
1393 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001394 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001395
1396 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1397 // to a LEA. This is determined with some expermentation but is by no means
1398 // optimal (especially for code size consideration). LEA is nice because of
1399 // its three-address nature. Tweak the cost function again when we can run
1400 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001401 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001402 // For X86-64, we should always use lea to materialize RIP relative
1403 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001404 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001405 Complexity = 4;
1406 else
1407 Complexity += 2;
1408 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001409
Dan Gohmanffce6f12010-04-29 23:30:41 +00001410 if (AM.Disp && (AM.Base_Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001411 Complexity++;
1412
Chris Lattner25142782009-07-11 22:50:33 +00001413 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001414 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001415 return false;
Chad Rosiera20e1e72012-08-01 18:39:17 +00001416
Chris Lattner25142782009-07-11 22:50:33 +00001417 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1418 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001419}
1420
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001421/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Chris Lattner52a261b2010-09-21 20:31:19 +00001422bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001423 SDValue &Scale, SDValue &Index,
Chris Lattner599b5312010-07-08 23:46:44 +00001424 SDValue &Disp, SDValue &Segment) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001425 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1426 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Chad Rosiera20e1e72012-08-01 18:39:17 +00001427
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001428 X86ISelAddressMode AM;
1429 AM.GV = GA->getGlobal();
1430 AM.Disp += GA->getOffset();
Dan Gohmanffce6f12010-04-29 23:30:41 +00001431 AM.Base_Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001432 AM.SymbolFlags = GA->getTargetFlags();
1433
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001435 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001436 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001437 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001438 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001439 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00001440
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001441 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1442 return true;
1443}
1444
1445
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001446bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001447 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001448 SDValue &Index, SDValue &Disp,
1449 SDValue &Segment) {
Chris Lattnerd1b73822010-03-02 22:20:06 +00001450 if (!ISD::isNON_EXTLoad(N.getNode()) ||
1451 !IsProfitableToFold(N, P, P) ||
Dan Gohmand858e902010-04-17 15:26:15 +00001452 !IsLegalToFold(N, P, P, OptLevel))
Chris Lattnerd1b73822010-03-02 22:20:06 +00001453 return false;
Chad Rosiera20e1e72012-08-01 18:39:17 +00001454
Chris Lattnerb86faa12010-09-21 22:07:31 +00001455 return SelectAddr(N.getNode(),
1456 N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001457}
1458
Dan Gohman8b746962008-09-23 18:22:58 +00001459/// getGlobalBaseReg - Return an SDNode that returns the value of
1460/// the global base register. Output instructions required to
1461/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001462///
Evan Cheng9ade2182006-08-26 05:34:46 +00001463SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001464 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001465 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001466}
1467
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001468SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1469 SDValue Chain = Node->getOperand(0);
1470 SDValue In1 = Node->getOperand(1);
1471 SDValue In2L = Node->getOperand(2);
1472 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001473 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerb86faa12010-09-21 22:07:31 +00001474 if (!SelectAddr(Node, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001475 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001476 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1477 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1478 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1479 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1480 MVT::i32, MVT::i32, MVT::Other, Ops,
1481 array_lengthof(Ops));
1482 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1483 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001484}
Christopher Lambc59e5212007-08-10 21:48:46 +00001485
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001486// FIXME: Figure out some way to unify this with the 'or' and other code
1487// below.
Owen Andersone50ed302009-08-10 22:56:29 +00001488SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001489 if (Node->hasAnyUseOfValue(0))
1490 return 0;
1491
1492 // Optimize common patterns for __sync_add_and_fetch and
1493 // __sync_sub_and_fetch where the result is not used. This allows us
1494 // to use "lock" version of add, sub, inc, dec instructions.
1495 // FIXME: Do not use special instructions but instead add the "lock"
1496 // prefix to the target node somehow. The extra information will then be
1497 // transferred to machine instruction and it denotes the prefix.
1498 SDValue Chain = Node->getOperand(0);
1499 SDValue Ptr = Node->getOperand(1);
1500 SDValue Val = Node->getOperand(2);
1501 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerb86faa12010-09-21 22:07:31 +00001502 if (!SelectAddr(Node, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Evan Cheng37b73872009-07-30 08:33:02 +00001503 return 0;
1504
1505 bool isInc = false, isDec = false, isSub = false, isCN = false;
1506 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
Eric Christophere3997d42011-07-01 23:04:38 +00001507 if (CN && CN->getSExtValue() == (int32_t)CN->getSExtValue()) {
Evan Cheng37b73872009-07-30 08:33:02 +00001508 isCN = true;
1509 int64_t CNVal = CN->getSExtValue();
1510 if (CNVal == 1)
1511 isInc = true;
1512 else if (CNVal == -1)
1513 isDec = true;
1514 else if (CNVal >= 0)
1515 Val = CurDAG->getTargetConstant(CNVal, NVT);
1516 else {
1517 isSub = true;
1518 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1519 }
1520 } else if (Val.hasOneUse() &&
1521 Val.getOpcode() == ISD::SUB &&
1522 X86::isZeroNode(Val.getOperand(0))) {
1523 isSub = true;
1524 Val = Val.getOperand(1);
1525 }
1526
Eric Christophere3997d42011-07-01 23:04:38 +00001527 DebugLoc dl = Node->getDebugLoc();
Evan Cheng37b73872009-07-30 08:33:02 +00001528 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001530 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001532 if (isInc)
1533 Opc = X86::LOCK_INC8m;
1534 else if (isDec)
1535 Opc = X86::LOCK_DEC8m;
1536 else if (isSub) {
1537 if (isCN)
1538 Opc = X86::LOCK_SUB8mi;
1539 else
1540 Opc = X86::LOCK_SUB8mr;
1541 } else {
1542 if (isCN)
1543 Opc = X86::LOCK_ADD8mi;
1544 else
1545 Opc = X86::LOCK_ADD8mr;
1546 }
1547 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001548 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001549 if (isInc)
1550 Opc = X86::LOCK_INC16m;
1551 else if (isDec)
1552 Opc = X86::LOCK_DEC16m;
1553 else if (isSub) {
1554 if (isCN) {
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +00001555 if (immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001556 Opc = X86::LOCK_SUB16mi8;
1557 else
1558 Opc = X86::LOCK_SUB16mi;
1559 } else
1560 Opc = X86::LOCK_SUB16mr;
1561 } else {
1562 if (isCN) {
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +00001563 if (immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001564 Opc = X86::LOCK_ADD16mi8;
1565 else
1566 Opc = X86::LOCK_ADD16mi;
1567 } else
1568 Opc = X86::LOCK_ADD16mr;
1569 }
1570 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001572 if (isInc)
1573 Opc = X86::LOCK_INC32m;
1574 else if (isDec)
1575 Opc = X86::LOCK_DEC32m;
1576 else if (isSub) {
1577 if (isCN) {
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +00001578 if (immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001579 Opc = X86::LOCK_SUB32mi8;
1580 else
1581 Opc = X86::LOCK_SUB32mi;
1582 } else
1583 Opc = X86::LOCK_SUB32mr;
1584 } else {
1585 if (isCN) {
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +00001586 if (immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001587 Opc = X86::LOCK_ADD32mi8;
1588 else
1589 Opc = X86::LOCK_ADD32mi;
1590 } else
1591 Opc = X86::LOCK_ADD32mr;
1592 }
1593 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001594 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001595 if (isInc)
1596 Opc = X86::LOCK_INC64m;
1597 else if (isDec)
1598 Opc = X86::LOCK_DEC64m;
1599 else if (isSub) {
1600 Opc = X86::LOCK_SUB64mr;
1601 if (isCN) {
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +00001602 if (immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001603 Opc = X86::LOCK_SUB64mi8;
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +00001604 else if (i64immSExt32(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001605 Opc = X86::LOCK_SUB64mi32;
1606 }
1607 } else {
1608 Opc = X86::LOCK_ADD64mr;
1609 if (isCN) {
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +00001610 if (immSext8(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001611 Opc = X86::LOCK_ADD64mi8;
Jakob Stoklund Olesen3061c442010-09-03 00:35:18 +00001612 else if (i64immSExt32(Val.getNode()))
Evan Cheng37b73872009-07-30 08:33:02 +00001613 Opc = X86::LOCK_ADD64mi32;
1614 }
1615 }
1616 break;
1617 }
1618
Chris Lattner518bb532010-02-09 19:54:29 +00001619 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +00001620 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001621 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1622 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001623 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001624 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1625 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1626 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001627 SDValue RetVals[] = { Undef, Ret };
1628 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1629 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001630 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1631 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1632 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001633 SDValue RetVals[] = { Undef, Ret };
1634 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1635 }
1636}
1637
Eric Christopher8102bf02011-05-17 07:47:55 +00001638enum AtomicOpc {
Eric Christopher811c2b72011-05-17 07:50:41 +00001639 OR,
Eric Christopherc324f722011-05-17 08:10:18 +00001640 AND,
1641 XOR,
Eric Christopher811c2b72011-05-17 07:50:41 +00001642 AtomicOpcEnd
Eric Christopher8102bf02011-05-17 07:47:55 +00001643};
1644
1645enum AtomicSz {
1646 ConstantI8,
1647 I8,
1648 SextConstantI16,
1649 ConstantI16,
1650 I16,
1651 SextConstantI32,
1652 ConstantI32,
1653 I32,
1654 SextConstantI64,
1655 ConstantI64,
Eric Christopher811c2b72011-05-17 07:50:41 +00001656 I64,
1657 AtomicSzEnd
Eric Christopher8102bf02011-05-17 07:47:55 +00001658};
1659
Craig Topper72051bf2012-03-09 07:45:21 +00001660static const uint16_t AtomicOpcTbl[AtomicOpcEnd][AtomicSzEnd] = {
Eric Christopherc493a1f2011-05-11 21:44:58 +00001661 {
1662 X86::LOCK_OR8mi,
1663 X86::LOCK_OR8mr,
1664 X86::LOCK_OR16mi8,
1665 X86::LOCK_OR16mi,
1666 X86::LOCK_OR16mr,
1667 X86::LOCK_OR32mi8,
1668 X86::LOCK_OR32mi,
1669 X86::LOCK_OR32mr,
1670 X86::LOCK_OR64mi8,
1671 X86::LOCK_OR64mi32,
1672 X86::LOCK_OR64mr
Eric Christopherc324f722011-05-17 08:10:18 +00001673 },
1674 {
1675 X86::LOCK_AND8mi,
1676 X86::LOCK_AND8mr,
1677 X86::LOCK_AND16mi8,
1678 X86::LOCK_AND16mi,
1679 X86::LOCK_AND16mr,
1680 X86::LOCK_AND32mi8,
1681 X86::LOCK_AND32mi,
1682 X86::LOCK_AND32mr,
1683 X86::LOCK_AND64mi8,
1684 X86::LOCK_AND64mi32,
1685 X86::LOCK_AND64mr
1686 },
1687 {
1688 X86::LOCK_XOR8mi,
1689 X86::LOCK_XOR8mr,
1690 X86::LOCK_XOR16mi8,
1691 X86::LOCK_XOR16mi,
1692 X86::LOCK_XOR16mr,
1693 X86::LOCK_XOR32mi8,
1694 X86::LOCK_XOR32mi,
1695 X86::LOCK_XOR32mr,
1696 X86::LOCK_XOR64mi8,
1697 X86::LOCK_XOR64mi32,
1698 X86::LOCK_XOR64mr
Eric Christopherc493a1f2011-05-11 21:44:58 +00001699 }
1700};
1701
Eric Christopherc324f722011-05-17 08:10:18 +00001702SDNode *X86DAGToDAGISel::SelectAtomicLoadArith(SDNode *Node, EVT NVT) {
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001703 if (Node->hasAnyUseOfValue(0))
1704 return 0;
Chad Rosiera20e1e72012-08-01 18:39:17 +00001705
Eric Christopher6abb7ba2011-05-17 08:16:14 +00001706 // Optimize common patterns for __sync_or_and_fetch and similar arith
1707 // operations where the result is not used. This allows us to use the "lock"
1708 // version of the arithmetic instruction.
1709 // FIXME: Same as for 'add' and 'sub', try to merge those down here.
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001710 SDValue Chain = Node->getOperand(0);
1711 SDValue Ptr = Node->getOperand(1);
1712 SDValue Val = Node->getOperand(2);
1713 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1714 if (!SelectAddr(Node, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1715 return 0;
1716
Eric Christopherc324f722011-05-17 08:10:18 +00001717 // Which index into the table.
1718 enum AtomicOpc Op;
1719 switch (Node->getOpcode()) {
1720 case ISD::ATOMIC_LOAD_OR:
1721 Op = OR;
1722 break;
1723 case ISD::ATOMIC_LOAD_AND:
1724 Op = AND;
1725 break;
1726 case ISD::ATOMIC_LOAD_XOR:
1727 Op = XOR;
1728 break;
1729 default:
1730 return 0;
1731 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00001732
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001733 bool isCN = false;
1734 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
Eric Christophere3997d42011-07-01 23:04:38 +00001735 if (CN && (int32_t)CN->getSExtValue() == CN->getSExtValue()) {
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001736 isCN = true;
1737 Val = CurDAG->getTargetConstant(CN->getSExtValue(), NVT);
1738 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00001739
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001740 unsigned Opc = 0;
1741 switch (NVT.getSimpleVT().SimpleTy) {
1742 default: return 0;
1743 case MVT::i8:
1744 if (isCN)
Eric Christopher8102bf02011-05-17 07:47:55 +00001745 Opc = AtomicOpcTbl[Op][ConstantI8];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001746 else
Eric Christopher8102bf02011-05-17 07:47:55 +00001747 Opc = AtomicOpcTbl[Op][I8];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001748 break;
1749 case MVT::i16:
1750 if (isCN) {
1751 if (immSext8(Val.getNode()))
Eric Christopher8102bf02011-05-17 07:47:55 +00001752 Opc = AtomicOpcTbl[Op][SextConstantI16];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001753 else
Eric Christopher8102bf02011-05-17 07:47:55 +00001754 Opc = AtomicOpcTbl[Op][ConstantI16];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001755 } else
Eric Christopher8102bf02011-05-17 07:47:55 +00001756 Opc = AtomicOpcTbl[Op][I16];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001757 break;
1758 case MVT::i32:
1759 if (isCN) {
1760 if (immSext8(Val.getNode()))
Eric Christopher8102bf02011-05-17 07:47:55 +00001761 Opc = AtomicOpcTbl[Op][SextConstantI32];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001762 else
Eric Christopher8102bf02011-05-17 07:47:55 +00001763 Opc = AtomicOpcTbl[Op][ConstantI32];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001764 } else
Eric Christopher8102bf02011-05-17 07:47:55 +00001765 Opc = AtomicOpcTbl[Op][I32];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001766 break;
1767 case MVT::i64:
Eric Christopher5d8aa342011-06-30 00:48:30 +00001768 Opc = AtomicOpcTbl[Op][I64];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001769 if (isCN) {
1770 if (immSext8(Val.getNode()))
Eric Christopher8102bf02011-05-17 07:47:55 +00001771 Opc = AtomicOpcTbl[Op][SextConstantI64];
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001772 else if (i64immSExt32(Val.getNode()))
Eric Christopher8102bf02011-05-17 07:47:55 +00001773 Opc = AtomicOpcTbl[Op][ConstantI64];
Eric Christopher5d8aa342011-06-30 00:48:30 +00001774 }
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001775 break;
1776 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00001777
Eric Christopher5d8aa342011-06-30 00:48:30 +00001778 assert(Opc != 0 && "Invalid arith lock transform!");
1779
Eric Christopherb38fe4b2011-05-10 23:57:45 +00001780 DebugLoc dl = Node->getDebugLoc();
1781 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
1782 dl, NVT), 0);
1783 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1784 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1785 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1786 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1787 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
1788 SDValue RetVals[] = { Undef, Ret };
1789 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1790}
1791
Dan Gohman11596ed2009-10-09 20:35:19 +00001792/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1793/// any uses which require the SF or OF bits to be accurate.
1794static bool HasNoSignedComparisonUses(SDNode *N) {
1795 // Examine each user of the node.
1796 for (SDNode::use_iterator UI = N->use_begin(),
1797 UE = N->use_end(); UI != UE; ++UI) {
1798 // Only examine CopyToReg uses.
1799 if (UI->getOpcode() != ISD::CopyToReg)
1800 return false;
1801 // Only examine CopyToReg uses that copy to EFLAGS.
1802 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1803 X86::EFLAGS)
1804 return false;
1805 // Examine each user of the CopyToReg use.
1806 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1807 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1808 // Only examine the Flag result.
1809 if (FlagUI.getUse().getResNo() != 1) continue;
1810 // Anything unusual: assume conservatively.
1811 if (!FlagUI->isMachineOpcode()) return false;
1812 // Examine the opcode of the user.
1813 switch (FlagUI->getMachineOpcode()) {
1814 // These comparisons don't treat the most significant bit specially.
1815 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1816 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1817 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1818 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001819 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1820 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
Dan Gohman11596ed2009-10-09 20:35:19 +00001821 case X86::CMOVA16rr: case X86::CMOVA16rm:
1822 case X86::CMOVA32rr: case X86::CMOVA32rm:
1823 case X86::CMOVA64rr: case X86::CMOVA64rm:
1824 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1825 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1826 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1827 case X86::CMOVB16rr: case X86::CMOVB16rm:
1828 case X86::CMOVB32rr: case X86::CMOVB32rm:
1829 case X86::CMOVB64rr: case X86::CMOVB64rm:
Chris Lattner25cbf502010-10-05 23:00:14 +00001830 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1831 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1832 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
Dan Gohman11596ed2009-10-09 20:35:19 +00001833 case X86::CMOVE16rr: case X86::CMOVE16rm:
1834 case X86::CMOVE32rr: case X86::CMOVE32rm:
1835 case X86::CMOVE64rr: case X86::CMOVE64rm:
1836 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1837 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1838 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1839 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1840 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1841 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1842 case X86::CMOVP16rr: case X86::CMOVP16rm:
1843 case X86::CMOVP32rr: case X86::CMOVP32rm:
1844 case X86::CMOVP64rr: case X86::CMOVP64rm:
1845 continue;
1846 // Anything else: assume conservatively.
1847 default: return false;
1848 }
1849 }
1850 }
1851 return true;
1852}
1853
Joel Jones76d03102012-03-29 05:45:48 +00001854/// isLoadIncOrDecStore - Check whether or not the chain ending in StoreNode
1855/// is suitable for doing the {load; increment or decrement; store} to modify
1856/// transformation.
Chad Rosiera20e1e72012-08-01 18:39:17 +00001857static bool isLoadIncOrDecStore(StoreSDNode *StoreNode, unsigned Opc,
Evan Chengf0bcecc2012-04-12 19:14:21 +00001858 SDValue StoredVal, SelectionDAG *CurDAG,
1859 LoadSDNode* &LoadNode, SDValue &InputChain) {
Joel Jones76d03102012-03-29 05:45:48 +00001860
1861 // is the value stored the result of a DEC or INC?
1862 if (!(Opc == X86ISD::DEC || Opc == X86ISD::INC)) return false;
1863
Joel Jones76d03102012-03-29 05:45:48 +00001864 // is the stored value result 0 of the load?
1865 if (StoredVal.getResNo() != 0) return false;
1866
1867 // are there other uses of the loaded value than the inc or dec?
1868 if (!StoredVal.getNode()->hasNUsesOfValue(1, 0)) return false;
1869
Joel Jones76d03102012-03-29 05:45:48 +00001870 // is the store non-extending and non-indexed?
Evan Chengf0bcecc2012-04-12 19:14:21 +00001871 if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal())
Joel Jones76d03102012-03-29 05:45:48 +00001872 return false;
1873
Evan Chengf0bcecc2012-04-12 19:14:21 +00001874 SDValue Load = StoredVal->getOperand(0);
1875 // Is the stored value a non-extending and non-indexed load?
1876 if (!ISD::isNormalLoad(Load.getNode())) return false;
1877
1878 // Return LoadNode by reference.
1879 LoadNode = cast<LoadSDNode>(Load);
1880 // is the size of the value one that we can handle? (i.e. 64, 32, 16, or 8)
Chad Rosiera20e1e72012-08-01 18:39:17 +00001881 EVT LdVT = LoadNode->getMemoryVT();
1882 if (LdVT != MVT::i64 && LdVT != MVT::i32 && LdVT != MVT::i16 &&
Evan Chengf0bcecc2012-04-12 19:14:21 +00001883 LdVT != MVT::i8)
1884 return false;
1885
1886 // Is store the only read of the loaded value?
1887 if (!Load.hasOneUse())
1888 return false;
Chad Rosiera20e1e72012-08-01 18:39:17 +00001889
Evan Chengf0bcecc2012-04-12 19:14:21 +00001890 // Is the address of the store the same as the load?
1891 if (LoadNode->getBasePtr() != StoreNode->getBasePtr() ||
1892 LoadNode->getOffset() != StoreNode->getOffset())
1893 return false;
1894
1895 // Check if the chain is produced by the load or is a TokenFactor with
1896 // the load output chain as an operand. Return InputChain by reference.
1897 SDValue Chain = StoreNode->getChain();
1898
1899 bool ChainCheck = false;
1900 if (Chain == Load.getValue(1)) {
1901 ChainCheck = true;
1902 InputChain = LoadNode->getChain();
1903 } else if (Chain.getOpcode() == ISD::TokenFactor) {
1904 SmallVector<SDValue, 4> ChainOps;
1905 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) {
1906 SDValue Op = Chain.getOperand(i);
1907 if (Op == Load.getValue(1)) {
1908 ChainCheck = true;
1909 continue;
1910 }
Evan Cheng61003662012-05-16 01:54:27 +00001911
1912 // Make sure using Op as part of the chain would not cause a cycle here.
1913 // In theory, we could check whether the chain node is a predecessor of
1914 // the load. But that can be very expensive. Instead visit the uses and
1915 // make sure they all have smaller node id than the load.
1916 int LoadId = LoadNode->getNodeId();
1917 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
1918 UE = UI->use_end(); UI != UE; ++UI) {
1919 if (UI.getUse().getResNo() != 0)
1920 continue;
1921 if (UI->getNodeId() > LoadId)
1922 return false;
1923 }
1924
Evan Chengf0bcecc2012-04-12 19:14:21 +00001925 ChainOps.push_back(Op);
1926 }
1927
1928 if (ChainCheck)
1929 // Make a new TokenFactor with all the other input chains except
1930 // for the load.
1931 InputChain = CurDAG->getNode(ISD::TokenFactor, Chain.getDebugLoc(),
1932 MVT::Other, &ChainOps[0], ChainOps.size());
1933 }
1934 if (!ChainCheck)
Joel Jones76d03102012-03-29 05:45:48 +00001935 return false;
1936
1937 return true;
1938}
1939
Benjamin Kramer73478402012-03-29 12:37:26 +00001940/// getFusedLdStOpcode - Get the appropriate X86 opcode for an in memory
1941/// increment or decrement. Opc should be X86ISD::DEC or X86ISD::INC.
Joel Jones76d03102012-03-29 05:45:48 +00001942static unsigned getFusedLdStOpcode(EVT &LdVT, unsigned Opc) {
1943 if (Opc == X86ISD::DEC) {
1944 if (LdVT == MVT::i64) return X86::DEC64m;
1945 if (LdVT == MVT::i32) return X86::DEC32m;
1946 if (LdVT == MVT::i16) return X86::DEC16m;
1947 if (LdVT == MVT::i8) return X86::DEC8m;
Benjamin Kramer73478402012-03-29 12:37:26 +00001948 } else {
1949 assert(Opc == X86ISD::INC && "unrecognized opcode");
Joel Jones76d03102012-03-29 05:45:48 +00001950 if (LdVT == MVT::i64) return X86::INC64m;
1951 if (LdVT == MVT::i32) return X86::INC32m;
1952 if (LdVT == MVT::i16) return X86::INC16m;
1953 if (LdVT == MVT::i8) return X86::INC8m;
Joel Jones76d03102012-03-29 05:45:48 +00001954 }
Benjamin Kramer73478402012-03-29 12:37:26 +00001955 llvm_unreachable("unrecognized size for LdVT");
Joel Jones76d03102012-03-29 05:45:48 +00001956}
1957
Manman Ren1f7a1b62012-06-26 19:47:59 +00001958/// SelectGather - Customized ISel for GATHER operations.
1959///
1960SDNode *X86DAGToDAGISel::SelectGather(SDNode *Node, unsigned Opc) {
1961 // Operands of Gather: VSrc, Base, VIdx, VMask, Scale
1962 SDValue Chain = Node->getOperand(0);
1963 SDValue VSrc = Node->getOperand(2);
1964 SDValue Base = Node->getOperand(3);
1965 SDValue VIdx = Node->getOperand(4);
1966 SDValue VMask = Node->getOperand(5);
1967 ConstantSDNode *Scale = dyn_cast<ConstantSDNode>(Node->getOperand(6));
Craig Topper15d39ad2012-07-01 02:17:08 +00001968 if (!Scale)
1969 return 0;
Manman Ren1f7a1b62012-06-26 19:47:59 +00001970
Craig Topper5aba78b2012-07-12 06:52:41 +00001971 SDVTList VTs = CurDAG->getVTList(VSrc.getValueType(), VSrc.getValueType(),
1972 MVT::Other);
1973
Manman Ren1f7a1b62012-06-26 19:47:59 +00001974 // Memory Operands: Base, Scale, Index, Disp, Segment
1975 SDValue Disp = CurDAG->getTargetConstant(0, MVT::i32);
1976 SDValue Segment = CurDAG->getRegister(0, MVT::i32);
1977 const SDValue Ops[] = { VSrc, Base, getI8Imm(Scale->getSExtValue()), VIdx,
1978 Disp, Segment, VMask, Chain};
1979 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
Craig Topper5aba78b2012-07-12 06:52:41 +00001980 VTs, Ops, array_lengthof(Ops));
1981 // Node has 2 outputs: VDst and MVT::Other.
1982 // ResNode has 3 outputs: VDst, VMask_wb, and MVT::Other.
1983 // We replace VDst of Node with VDst of ResNode, and Other of Node with Other
1984 // of ResNode.
1985 ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
1986 ReplaceUses(SDValue(Node, 1), SDValue(ResNode, 2));
Manman Ren1f7a1b62012-06-26 19:47:59 +00001987 return ResNode;
1988}
1989
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001990SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Owen Andersone50ed302009-08-10 22:56:29 +00001991 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001992 unsigned Opc, MOpc;
1993 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001994 DebugLoc dl = Node->getDebugLoc();
Chad Rosiera20e1e72012-08-01 18:39:17 +00001995
Chris Lattner7c306da2010-03-02 06:34:30 +00001996 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
Evan Chengf597dc72006-02-10 22:24:32 +00001997
Dan Gohmane8be6c62008-07-17 19:10:17 +00001998 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +00001999 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00002000 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00002001 }
Evan Cheng38262ca2006-01-11 22:15:18 +00002002
Evan Cheng0114e942006-01-06 20:36:21 +00002003 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00002004 default: break;
Manman Ren1f7a1b62012-06-26 19:47:59 +00002005 case ISD::INTRINSIC_W_CHAIN: {
2006 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2007 switch (IntNo) {
2008 default: break;
2009 case Intrinsic::x86_avx2_gather_d_pd:
Manman Ren1f7a1b62012-06-26 19:47:59 +00002010 case Intrinsic::x86_avx2_gather_d_pd_256:
Manman Ren1f7a1b62012-06-26 19:47:59 +00002011 case Intrinsic::x86_avx2_gather_q_pd:
Manman Ren1f7a1b62012-06-26 19:47:59 +00002012 case Intrinsic::x86_avx2_gather_q_pd_256:
Manman Ren1f7a1b62012-06-26 19:47:59 +00002013 case Intrinsic::x86_avx2_gather_d_ps:
Manman Ren1f7a1b62012-06-26 19:47:59 +00002014 case Intrinsic::x86_avx2_gather_d_ps_256:
Manman Ren1f7a1b62012-06-26 19:47:59 +00002015 case Intrinsic::x86_avx2_gather_q_ps:
Manman Ren1f7a1b62012-06-26 19:47:59 +00002016 case Intrinsic::x86_avx2_gather_q_ps_256:
Manman Ren40307c72012-06-29 00:54:20 +00002017 case Intrinsic::x86_avx2_gather_d_q:
Manman Ren40307c72012-06-29 00:54:20 +00002018 case Intrinsic::x86_avx2_gather_d_q_256:
Manman Ren40307c72012-06-29 00:54:20 +00002019 case Intrinsic::x86_avx2_gather_q_q:
Manman Ren40307c72012-06-29 00:54:20 +00002020 case Intrinsic::x86_avx2_gather_q_q_256:
Manman Ren40307c72012-06-29 00:54:20 +00002021 case Intrinsic::x86_avx2_gather_d_d:
Manman Ren40307c72012-06-29 00:54:20 +00002022 case Intrinsic::x86_avx2_gather_d_d_256:
Manman Ren40307c72012-06-29 00:54:20 +00002023 case Intrinsic::x86_avx2_gather_q_d:
Craig Topperde6e4842012-07-01 02:05:52 +00002024 case Intrinsic::x86_avx2_gather_q_d_256: {
2025 unsigned Opc;
2026 switch (IntNo) {
Craig Topper51e89c02012-07-01 02:55:34 +00002027 default: llvm_unreachable("Impossible intrinsic");
Craig Topperde6e4842012-07-01 02:05:52 +00002028 case Intrinsic::x86_avx2_gather_d_pd: Opc = X86::VGATHERDPDrm; break;
2029 case Intrinsic::x86_avx2_gather_d_pd_256: Opc = X86::VGATHERDPDYrm; break;
2030 case Intrinsic::x86_avx2_gather_q_pd: Opc = X86::VGATHERQPDrm; break;
2031 case Intrinsic::x86_avx2_gather_q_pd_256: Opc = X86::VGATHERQPDYrm; break;
2032 case Intrinsic::x86_avx2_gather_d_ps: Opc = X86::VGATHERDPSrm; break;
2033 case Intrinsic::x86_avx2_gather_d_ps_256: Opc = X86::VGATHERDPSYrm; break;
2034 case Intrinsic::x86_avx2_gather_q_ps: Opc = X86::VGATHERQPSrm; break;
2035 case Intrinsic::x86_avx2_gather_q_ps_256: Opc = X86::VGATHERQPSYrm; break;
2036 case Intrinsic::x86_avx2_gather_d_q: Opc = X86::VPGATHERDQrm; break;
2037 case Intrinsic::x86_avx2_gather_d_q_256: Opc = X86::VPGATHERDQYrm; break;
2038 case Intrinsic::x86_avx2_gather_q_q: Opc = X86::VPGATHERQQrm; break;
2039 case Intrinsic::x86_avx2_gather_q_q_256: Opc = X86::VPGATHERQQYrm; break;
2040 case Intrinsic::x86_avx2_gather_d_d: Opc = X86::VPGATHERDDrm; break;
2041 case Intrinsic::x86_avx2_gather_d_d_256: Opc = X86::VPGATHERDDYrm; break;
2042 case Intrinsic::x86_avx2_gather_q_d: Opc = X86::VPGATHERQDrm; break;
2043 case Intrinsic::x86_avx2_gather_q_d_256: Opc = X86::VPGATHERQDYrm; break;
2044 }
Craig Topper15d39ad2012-07-01 02:17:08 +00002045 SDNode *RetVal = SelectGather(Node, Opc);
2046 if (RetVal)
Craig Topper5aba78b2012-07-12 06:52:41 +00002047 // We already called ReplaceUses inside SelectGather.
2048 return NULL;
Craig Topper65b382c2012-07-01 02:18:18 +00002049 break;
Craig Topperde6e4842012-07-01 02:05:52 +00002050 }
Manman Ren1f7a1b62012-06-26 19:47:59 +00002051 }
2052 break;
2053 }
Dan Gohman72677342009-08-02 16:10:52 +00002054 case X86ISD::GlobalBaseReg:
2055 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00002056
Craig Topper51e89c02012-07-01 02:55:34 +00002057
Dan Gohman72677342009-08-02 16:10:52 +00002058 case X86ISD::ATOMOR64_DAG:
Dan Gohman72677342009-08-02 16:10:52 +00002059 case X86ISD::ATOMXOR64_DAG:
Dan Gohman72677342009-08-02 16:10:52 +00002060 case X86ISD::ATOMADD64_DAG:
Dan Gohman72677342009-08-02 16:10:52 +00002061 case X86ISD::ATOMSUB64_DAG:
Dan Gohman72677342009-08-02 16:10:52 +00002062 case X86ISD::ATOMNAND64_DAG:
Dan Gohman72677342009-08-02 16:10:52 +00002063 case X86ISD::ATOMAND64_DAG:
Craig Topper51e89c02012-07-01 02:55:34 +00002064 case X86ISD::ATOMSWAP64_DAG: {
2065 unsigned Opc;
2066 switch (Opcode) {
Craig Topper28654222012-08-11 17:44:14 +00002067 default: llvm_unreachable("Impossible opcode");
Craig Topper51e89c02012-07-01 02:55:34 +00002068 case X86ISD::ATOMOR64_DAG: Opc = X86::ATOMOR6432; break;
2069 case X86ISD::ATOMXOR64_DAG: Opc = X86::ATOMXOR6432; break;
2070 case X86ISD::ATOMADD64_DAG: Opc = X86::ATOMADD6432; break;
2071 case X86ISD::ATOMSUB64_DAG: Opc = X86::ATOMSUB6432; break;
2072 case X86ISD::ATOMNAND64_DAG: Opc = X86::ATOMNAND6432; break;
2073 case X86ISD::ATOMAND64_DAG: Opc = X86::ATOMAND6432; break;
2074 case X86ISD::ATOMSWAP64_DAG: Opc = X86::ATOMSWAP6432; break;
2075 }
2076 SDNode *RetVal = SelectAtomic64(Node, Opc);
2077 if (RetVal)
2078 return RetVal;
2079 break;
2080 }
Dale Johannesen48c1bc22008-10-02 18:53:47 +00002081
Dan Gohman72677342009-08-02 16:10:52 +00002082 case ISD::ATOMIC_LOAD_ADD: {
2083 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
2084 if (RetVal)
2085 return RetVal;
2086 break;
2087 }
Eric Christopherc324f722011-05-17 08:10:18 +00002088 case ISD::ATOMIC_LOAD_XOR:
2089 case ISD::ATOMIC_LOAD_AND:
Eric Christopherb38fe4b2011-05-10 23:57:45 +00002090 case ISD::ATOMIC_LOAD_OR: {
Eric Christopherc324f722011-05-17 08:10:18 +00002091 SDNode *RetVal = SelectAtomicLoadArith(Node, NVT);
Eric Christopherb38fe4b2011-05-10 23:57:45 +00002092 if (RetVal)
2093 return RetVal;
2094 break;
2095 }
Benjamin Kramerb20a8fc2011-04-22 15:30:40 +00002096 case ISD::AND:
2097 case ISD::OR:
2098 case ISD::XOR: {
2099 // For operations of the form (x << C1) op C2, check if we can use a smaller
2100 // encoding for C2 by transforming it into (x op (C2>>C1)) << C1.
2101 SDValue N0 = Node->getOperand(0);
2102 SDValue N1 = Node->getOperand(1);
2103
2104 if (N0->getOpcode() != ISD::SHL || !N0->hasOneUse())
2105 break;
2106
2107 // i8 is unshrinkable, i16 should be promoted to i32.
2108 if (NVT != MVT::i32 && NVT != MVT::i64)
2109 break;
2110
2111 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N1);
2112 ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(N0->getOperand(1));
2113 if (!Cst || !ShlCst)
2114 break;
2115
2116 int64_t Val = Cst->getSExtValue();
2117 uint64_t ShlVal = ShlCst->getZExtValue();
2118
2119 // Make sure that we don't change the operation by removing bits.
2120 // This only matters for OR and XOR, AND is unaffected.
Richard Smith1144af32012-08-24 23:29:28 +00002121 uint64_t RemovedBitsMask = (1ULL << ShlVal) - 1;
2122 if (Opcode != ISD::AND && (Val & RemovedBitsMask) != 0)
Benjamin Kramerb20a8fc2011-04-22 15:30:40 +00002123 break;
2124
Craig Topper28654222012-08-11 17:44:14 +00002125 unsigned ShlOp, Op;
Benjamin Kramerb20a8fc2011-04-22 15:30:40 +00002126 EVT CstVT = NVT;
2127
2128 // Check the minimum bitwidth for the new constant.
2129 // TODO: AND32ri is the same as AND64ri32 with zext imm.
2130 // TODO: MOV32ri+OR64r is cheaper than MOV64ri64+OR64rr
2131 // TODO: Using 16 and 8 bit operations is also possible for or32 & xor32.
2132 if (!isInt<8>(Val) && isInt<8>(Val >> ShlVal))
2133 CstVT = MVT::i8;
2134 else if (!isInt<32>(Val) && isInt<32>(Val >> ShlVal))
2135 CstVT = MVT::i32;
2136
2137 // Bail if there is no smaller encoding.
2138 if (NVT == CstVT)
2139 break;
2140
2141 switch (NVT.getSimpleVT().SimpleTy) {
2142 default: llvm_unreachable("Unsupported VT!");
2143 case MVT::i32:
2144 assert(CstVT == MVT::i8);
2145 ShlOp = X86::SHL32ri;
2146
2147 switch (Opcode) {
Craig Topper28654222012-08-11 17:44:14 +00002148 default: llvm_unreachable("Impossible opcode");
Benjamin Kramerb20a8fc2011-04-22 15:30:40 +00002149 case ISD::AND: Op = X86::AND32ri8; break;
2150 case ISD::OR: Op = X86::OR32ri8; break;
2151 case ISD::XOR: Op = X86::XOR32ri8; break;
2152 }
2153 break;
2154 case MVT::i64:
2155 assert(CstVT == MVT::i8 || CstVT == MVT::i32);
2156 ShlOp = X86::SHL64ri;
2157
2158 switch (Opcode) {
Craig Topper28654222012-08-11 17:44:14 +00002159 default: llvm_unreachable("Impossible opcode");
Benjamin Kramerb20a8fc2011-04-22 15:30:40 +00002160 case ISD::AND: Op = CstVT==MVT::i8? X86::AND64ri8 : X86::AND64ri32; break;
2161 case ISD::OR: Op = CstVT==MVT::i8? X86::OR64ri8 : X86::OR64ri32; break;
2162 case ISD::XOR: Op = CstVT==MVT::i8? X86::XOR64ri8 : X86::XOR64ri32; break;
2163 }
2164 break;
2165 }
2166
2167 // Emit the smaller op and the shift.
2168 SDValue NewCst = CurDAG->getTargetConstant(Val >> ShlVal, CstVT);
2169 SDNode *New = CurDAG->getMachineNode(Op, dl, NVT, N0->getOperand(0),NewCst);
2170 return CurDAG->SelectNodeTo(Node, ShlOp, NVT, SDValue(New, 0),
2171 getI8Imm(ShlVal));
Benjamin Kramerb20a8fc2011-04-22 15:30:40 +00002172 }
Chris Lattnerb20e0b12010-12-05 07:30:36 +00002173 case X86ISD::UMUL: {
2174 SDValue N0 = Node->getOperand(0);
2175 SDValue N1 = Node->getOperand(1);
Chad Rosiera20e1e72012-08-01 18:39:17 +00002176
Ted Kremenekd7f696e2011-01-14 22:34:13 +00002177 unsigned LoReg;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00002178 switch (NVT.getSimpleVT().SimpleTy) {
2179 default: llvm_unreachable("Unsupported VT!");
Ted Kremenekd7f696e2011-01-14 22:34:13 +00002180 case MVT::i8: LoReg = X86::AL; Opc = X86::MUL8r; break;
2181 case MVT::i16: LoReg = X86::AX; Opc = X86::MUL16r; break;
2182 case MVT::i32: LoReg = X86::EAX; Opc = X86::MUL32r; break;
2183 case MVT::i64: LoReg = X86::RAX; Opc = X86::MUL64r; break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00002184 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00002185
Chris Lattnerb20e0b12010-12-05 07:30:36 +00002186 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
2187 N0, SDValue()).getValue(1);
Chad Rosiera20e1e72012-08-01 18:39:17 +00002188
Chris Lattnerb20e0b12010-12-05 07:30:36 +00002189 SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::i32);
2190 SDValue Ops[] = {N1, InFlag};
2191 SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops, 2);
Chad Rosiera20e1e72012-08-01 18:39:17 +00002192
Chris Lattnerb20e0b12010-12-05 07:30:36 +00002193 ReplaceUses(SDValue(Node, 0), SDValue(CNode, 0));
2194 ReplaceUses(SDValue(Node, 1), SDValue(CNode, 1));
2195 ReplaceUses(SDValue(Node, 2), SDValue(CNode, 2));
2196 return NULL;
2197 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00002198
Dan Gohman72677342009-08-02 16:10:52 +00002199 case ISD::SMUL_LOHI:
2200 case ISD::UMUL_LOHI: {
2201 SDValue N0 = Node->getOperand(0);
2202 SDValue N1 = Node->getOperand(1);
2203
2204 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00002205 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002206 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00002207 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002208 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
2209 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
2210 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
2211 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00002212 }
Bill Wendling12321672009-08-07 21:33:25 +00002213 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002214 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00002215 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002216 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
2217 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
2218 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
2219 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00002220 }
Bill Wendling12321672009-08-07 21:33:25 +00002221 }
Dan Gohman72677342009-08-02 16:10:52 +00002222
2223 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00002224 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00002225 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002226 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
2227 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
2228 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
2229 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00002230 }
2231
2232 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002233 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00002234 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00002235 if (!foldedLoad) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002236 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00002237 if (foldedLoad)
2238 std::swap(N0, N1);
2239 }
2240
2241 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Craig Topper88097812012-05-23 05:44:51 +00002242 N0, SDValue()).getValue(1);
Dan Gohman72677342009-08-02 16:10:52 +00002243
2244 if (foldedLoad) {
2245 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
2246 InFlag };
2247 SDNode *CNode =
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002248 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Glue, Ops,
Dan Gohman602b0c82009-09-25 18:54:59 +00002249 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00002250 InFlag = SDValue(CNode, 1);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00002251
Dan Gohman72677342009-08-02 16:10:52 +00002252 // Update the chain.
2253 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
2254 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002255 SDNode *CNode = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N1, InFlag);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00002256 InFlag = SDValue(CNode, 0);
Dan Gohman72677342009-08-02 16:10:52 +00002257 }
2258
Jakob Stoklund Olesen4f5d84e2010-06-26 00:39:23 +00002259 // Prevent use of AH in a REX instruction by referencing AX instead.
2260 if (HiReg == X86::AH && Subtarget->is64Bit() &&
2261 !SDValue(Node, 1).use_empty()) {
2262 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2263 X86::AX, MVT::i16, InFlag);
2264 InFlag = Result.getValue(2);
2265 // Get the low part if needed. Don't use getCopyFromReg for aliasing
2266 // registers.
2267 if (!SDValue(Node, 0).use_empty())
2268 ReplaceUses(SDValue(Node, 1),
2269 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2270
2271 // Shift AX down 8 bits.
2272 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
2273 Result,
2274 CurDAG->getTargetConstant(8, MVT::i8)), 0);
2275 // Then truncate it down to i8.
2276 ReplaceUses(SDValue(Node, 1),
2277 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2278 }
Dan Gohman72677342009-08-02 16:10:52 +00002279 // Copy the low half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002280 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00002281 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Craig Topper88097812012-05-23 05:44:51 +00002282 LoReg, NVT, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00002283 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002284 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00002285 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00002286 }
2287 // Copy the high half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002288 if (!SDValue(Node, 1).use_empty()) {
Jakob Stoklund Olesen4f5d84e2010-06-26 00:39:23 +00002289 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2290 HiReg, NVT, InFlag);
2291 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002292 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00002293 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00002294 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00002295
Dan Gohman72677342009-08-02 16:10:52 +00002296 return NULL;
2297 }
2298
2299 case ISD::SDIVREM:
2300 case ISD::UDIVREM: {
2301 SDValue N0 = Node->getOperand(0);
2302 SDValue N1 = Node->getOperand(1);
2303
2304 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00002305 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002306 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00002307 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002308 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
2309 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
2310 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
2311 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00002312 }
Bill Wendling12321672009-08-07 21:33:25 +00002313 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00002314 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00002315 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
2317 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
2318 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
2319 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00002320 }
Bill Wendling12321672009-08-07 21:33:25 +00002321 }
Dan Gohman72677342009-08-02 16:10:52 +00002322
Chris Lattner9e323832009-12-23 01:45:04 +00002323 unsigned LoReg, HiReg, ClrReg;
Dan Gohman72677342009-08-02 16:10:52 +00002324 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00002325 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00002326 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002327 case MVT::i8:
Chris Lattner9e323832009-12-23 01:45:04 +00002328 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman72677342009-08-02 16:10:52 +00002329 ClrOpcode = 0;
2330 SExtOpcode = X86::CBW;
2331 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002332 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00002333 LoReg = X86::AX; HiReg = X86::DX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00002334 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
Dan Gohman72677342009-08-02 16:10:52 +00002335 SExtOpcode = X86::CWD;
2336 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002337 case MVT::i32:
Chris Lattner9e323832009-12-23 01:45:04 +00002338 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman72677342009-08-02 16:10:52 +00002339 ClrOpcode = X86::MOV32r0;
2340 SExtOpcode = X86::CDQ;
2341 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 case MVT::i64:
Chris Lattner9e323832009-12-23 01:45:04 +00002343 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00002344 ClrOpcode = X86::MOV64r0;
Dan Gohman72677342009-08-02 16:10:52 +00002345 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00002346 break;
2347 }
2348
Dan Gohman72677342009-08-02 16:10:52 +00002349 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002350 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00002351 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00002352
Dan Gohman72677342009-08-02 16:10:52 +00002353 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00002354 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00002355 // Special case for div8, just use a move with zero extension to AX to
2356 // clear the upper 8 bits (AH).
2357 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002358 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman72677342009-08-02 16:10:52 +00002359 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
2360 Move =
Stuart Hastings0e29ed02011-05-20 19:04:40 +00002361 SDValue(CurDAG->getMachineNode(X86::MOVZX32rm8, dl, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +00002362 MVT::Other, Ops,
2363 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00002364 Chain = Move.getValue(1);
2365 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00002366 } else {
Dan Gohman72677342009-08-02 16:10:52 +00002367 Move =
Stuart Hastings0e29ed02011-05-20 19:04:40 +00002368 SDValue(CurDAG->getMachineNode(X86::MOVZX32rr8, dl, MVT::i32, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00002369 Chain = CurDAG->getEntryNode();
2370 }
Stuart Hastings0e29ed02011-05-20 19:04:40 +00002371 Chain = CurDAG->getCopyToReg(Chain, dl, X86::EAX, Move, SDValue());
Dan Gohman72677342009-08-02 16:10:52 +00002372 InFlag = Chain.getValue(1);
2373 } else {
2374 InFlag =
2375 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
2376 LoReg, N0, SDValue()).getValue(1);
2377 if (isSigned && !signBitIsZero) {
2378 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00002379 InFlag =
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002380 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Glue, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00002381 } else {
2382 // Zero out the high part, effectively zero extending the input.
Dan Gohmanf1b4d262010-01-12 04:42:54 +00002383 SDValue ClrNode =
2384 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Chris Lattner9e323832009-12-23 01:45:04 +00002385 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman72677342009-08-02 16:10:52 +00002386 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00002387 }
Evan Cheng948f3432006-01-06 23:19:29 +00002388 }
Dan Gohman525178c2007-10-08 18:33:35 +00002389
Dan Gohman72677342009-08-02 16:10:52 +00002390 if (foldedLoad) {
2391 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
2392 InFlag };
2393 SDNode *CNode =
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002394 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Glue, Ops,
Dan Gohman602b0c82009-09-25 18:54:59 +00002395 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00002396 InFlag = SDValue(CNode, 1);
2397 // Update the chain.
2398 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
2399 } else {
2400 InFlag =
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002401 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00002402 }
Evan Cheng948f3432006-01-06 23:19:29 +00002403
Jakob Stoklund Olesen4f5d84e2010-06-26 00:39:23 +00002404 // Prevent use of AH in a REX instruction by referencing AX instead.
2405 // Shift it down 8 bits.
2406 if (HiReg == X86::AH && Subtarget->is64Bit() &&
2407 !SDValue(Node, 1).use_empty()) {
2408 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2409 X86::AX, MVT::i16, InFlag);
2410 InFlag = Result.getValue(2);
2411
2412 // If we also need AL (the quotient), get it by extracting a subreg from
2413 // Result. The fast register allocator does not like multiple CopyFromReg
2414 // nodes using aliasing registers.
2415 if (!SDValue(Node, 0).use_empty())
2416 ReplaceUses(SDValue(Node, 0),
2417 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2418
2419 // Shift AX right by 8 bits instead of using AH.
2420 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
2421 Result,
2422 CurDAG->getTargetConstant(8, MVT::i8)),
2423 0);
2424 ReplaceUses(SDValue(Node, 1),
2425 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2426 }
Dan Gohman72677342009-08-02 16:10:52 +00002427 // Copy the division (low) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002428 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00002429 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2430 LoReg, NVT, InFlag);
2431 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002432 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00002433 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00002434 }
2435 // Copy the remainder (high) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002436 if (!SDValue(Node, 1).use_empty()) {
Jakob Stoklund Olesen4f5d84e2010-06-26 00:39:23 +00002437 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2438 HiReg, NVT, InFlag);
2439 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002440 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00002441 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00002442 }
Dan Gohman72677342009-08-02 16:10:52 +00002443 return NULL;
2444 }
2445
Manman Ren39ad5682012-08-08 00:51:41 +00002446 case X86ISD::CMP:
2447 case X86ISD::SUB: {
2448 // Sometimes a SUB is used to perform comparison.
2449 if (Opcode == X86ISD::SUB && Node->hasAnyUseOfValue(0))
2450 // This node is not a CMP.
2451 break;
Dan Gohman6a402dc2009-08-19 18:16:17 +00002452 SDValue N0 = Node->getOperand(0);
2453 SDValue N1 = Node->getOperand(1);
2454
2455 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2456 // use a smaller encoding.
Eli Friedman77524422010-08-04 22:40:58 +00002457 if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() &&
2458 HasNoSignedComparisonUses(Node))
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00002459 // Look past the truncate if CMP is the only use of it.
2460 N0 = N0.getOperand(0);
Dan Gohman65fd6562011-11-03 21:49:52 +00002461 if ((N0.getNode()->getOpcode() == ISD::AND ||
2462 (N0.getResNo() == 0 && N0.getNode()->getOpcode() == X86ISD::AND)) &&
2463 N0.getNode()->hasOneUse() &&
Dan Gohman6a402dc2009-08-19 18:16:17 +00002464 N0.getValueType() != MVT::i8 &&
2465 X86::isZeroNode(N1)) {
2466 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2467 if (!C) break;
2468
2469 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00002470 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2471 (!(C->getZExtValue() & 0x80) ||
2472 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002473 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2474 SDValue Reg = N0.getNode()->getOperand(0);
2475
2476 // On x86-32, only the ABCD registers have 8-bit subregisters.
2477 if (!Subtarget->is64Bit()) {
Craig Topperc528e462012-02-22 07:28:11 +00002478 const TargetRegisterClass *TRC;
Dan Gohman6a402dc2009-08-19 18:16:17 +00002479 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2480 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2481 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2482 default: llvm_unreachable("Unsupported TEST operand type!");
2483 }
2484 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002485 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2486 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002487 }
2488
2489 // Extract the l-register.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00002490 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00002491 MVT::i8, Reg);
2492
2493 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00002494 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002495 }
2496
2497 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00002498 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2499 (!(C->getZExtValue() & 0x8000) ||
2500 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002501 // Shift the immediate right by 8 bits.
2502 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2503 MVT::i8);
2504 SDValue Reg = N0.getNode()->getOperand(0);
2505
2506 // Put the value in an ABCD register.
Craig Topperc528e462012-02-22 07:28:11 +00002507 const TargetRegisterClass *TRC;
Dan Gohman6a402dc2009-08-19 18:16:17 +00002508 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2509 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2510 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2511 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2512 default: llvm_unreachable("Unsupported TEST operand type!");
2513 }
2514 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002515 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2516 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002517
2518 // Extract the h-register.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00002519 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit_hi, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00002520 MVT::i8, Reg);
2521
Jakob Stoklund Olesened744822011-10-08 18:28:28 +00002522 // Emit a testb. The EXTRACT_SUBREG becomes a COPY that can only
2523 // target GR8_NOREX registers, so make sure the register class is
2524 // forced.
2525 return CurDAG->getMachineNode(X86::TEST8ri_NOREX, dl, MVT::i32,
Dan Gohman602b0c82009-09-25 18:54:59 +00002526 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002527 }
2528
2529 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2530 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002531 N0.getValueType() != MVT::i16 &&
2532 (!(C->getZExtValue() & 0x8000) ||
2533 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002534 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2535 SDValue Reg = N0.getNode()->getOperand(0);
2536
2537 // Extract the 16-bit subregister.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00002538 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00002539 MVT::i16, Reg);
2540
2541 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00002542 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002543 }
2544
2545 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2546 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002547 N0.getValueType() == MVT::i64 &&
2548 (!(C->getZExtValue() & 0x80000000) ||
2549 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002550 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2551 SDValue Reg = N0.getNode()->getOperand(0);
2552
2553 // Extract the 32-bit subregister.
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00002554 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_32bit, dl,
Dan Gohman6a402dc2009-08-19 18:16:17 +00002555 MVT::i32, Reg);
2556
2557 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00002558 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002559 }
2560 }
2561 break;
2562 }
Pete Cooper2d496892011-11-15 21:57:53 +00002563 case ISD::STORE: {
Joel Jones76d03102012-03-29 05:45:48 +00002564 // Change a chain of {load; incr or dec; store} of the same value into
2565 // a simple increment or decrement through memory of that value, if the
2566 // uses of the modified value and its address are suitable.
Pete Coopercd75e442011-11-16 19:03:23 +00002567 // The DEC64m tablegen pattern is currently not able to match the case where
Chad Rosiera20e1e72012-08-01 18:39:17 +00002568 // the EFLAGS on the original DEC are used. (This also applies to
Joel Jones76d03102012-03-29 05:45:48 +00002569 // {INC,DEC}X{64,32,16,8}.)
2570 // We'll need to improve tablegen to allow flags to be transferred from a
Pete Coopercd75e442011-11-16 19:03:23 +00002571 // node in the pattern to the result node. probably with a new keyword
2572 // for example, we have this
2573 // def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst",
2574 // [(store (add (loadi64 addr:$dst), -1), addr:$dst),
2575 // (implicit EFLAGS)]>;
2576 // but maybe need something like this
2577 // def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst",
2578 // [(store (add (loadi64 addr:$dst), -1), addr:$dst),
2579 // (transferrable EFLAGS)]>;
Joel Jones76d03102012-03-29 05:45:48 +00002580
Pete Cooper2d496892011-11-15 21:57:53 +00002581 StoreSDNode *StoreNode = cast<StoreSDNode>(Node);
Pete Cooper2d496892011-11-15 21:57:53 +00002582 SDValue StoredVal = StoreNode->getOperand(1);
Joel Jones76d03102012-03-29 05:45:48 +00002583 unsigned Opc = StoredVal->getOpcode();
Pete Cooper2d496892011-11-15 21:57:53 +00002584
Evan Chengf0bcecc2012-04-12 19:14:21 +00002585 LoadSDNode *LoadNode = 0;
2586 SDValue InputChain;
2587 if (!isLoadIncOrDecStore(StoreNode, Opc, StoredVal, CurDAG,
2588 LoadNode, InputChain))
2589 break;
Pete Cooper2d496892011-11-15 21:57:53 +00002590
2591 SDValue Base, Scale, Index, Disp, Segment;
2592 if (!SelectAddr(LoadNode, LoadNode->getBasePtr(),
2593 Base, Scale, Index, Disp, Segment))
2594 break;
2595
2596 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(2);
2597 MemOp[0] = StoreNode->getMemOperand();
2598 MemOp[1] = LoadNode->getMemOperand();
2599 const SDValue Ops[] = { Base, Scale, Index, Disp, Segment, InputChain };
Chad Rosiera20e1e72012-08-01 18:39:17 +00002600 EVT LdVT = LoadNode->getMemoryVT();
Joel Jones76d03102012-03-29 05:45:48 +00002601 unsigned newOpc = getFusedLdStOpcode(LdVT, Opc);
2602 MachineSDNode *Result = CurDAG->getMachineNode(newOpc,
Pete Cooper2d496892011-11-15 21:57:53 +00002603 Node->getDebugLoc(),
2604 MVT::i32, MVT::Other, Ops,
2605 array_lengthof(Ops));
2606 Result->setMemRefs(MemOp, MemOp + 2);
2607
2608 ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1));
2609 ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0));
2610
2611 return Result;
2612 }
Craig Topper4feb6472012-08-06 06:22:36 +00002613
2614 // FIXME: Custom handling because TableGen doesn't support multiple implicit
2615 // defs in an instruction pattern
2616 case X86ISD::PCMPESTRI: {
2617 SDValue N0 = Node->getOperand(0);
2618 SDValue N1 = Node->getOperand(1);
2619 SDValue N2 = Node->getOperand(2);
2620 SDValue N3 = Node->getOperand(3);
2621 SDValue N4 = Node->getOperand(4);
2622
2623 // Make sure last argument is a constant
2624 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N4);
2625 if (!Cst)
2626 break;
2627
2628 uint64_t Imm = Cst->getZExtValue();
2629
2630 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
2631 X86::EAX, N1, SDValue()).getValue(1);
2632 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, X86::EDX,
2633 N3, InFlag).getValue(1);
2634
2635 SDValue Ops[] = { N0, N2, getI8Imm(Imm), InFlag };
2636 unsigned Opc = Subtarget->hasAVX() ? X86::VPCMPESTRIrr :
2637 X86::PCMPESTRIrr;
2638 InFlag = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Ops,
2639 array_lengthof(Ops)), 0);
2640
2641 if (!SDValue(Node, 0).use_empty()) {
2642 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2643 X86::ECX, NVT, InFlag);
2644 InFlag = Result.getValue(2);
2645 ReplaceUses(SDValue(Node, 0), Result);
2646 }
2647 if (!SDValue(Node, 1).use_empty()) {
2648 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2649 X86::EFLAGS, NVT, InFlag);
2650 InFlag = Result.getValue(2);
2651 ReplaceUses(SDValue(Node, 1), Result);
2652 }
2653
2654 return NULL;
2655 }
2656
2657 // FIXME: Custom handling because TableGen doesn't support multiple implicit
2658 // defs in an instruction pattern
2659 case X86ISD::PCMPISTRI: {
2660 SDValue N0 = Node->getOperand(0);
2661 SDValue N1 = Node->getOperand(1);
2662 SDValue N2 = Node->getOperand(2);
2663
2664 // Make sure last argument is a constant
2665 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N2);
2666 if (!Cst)
2667 break;
2668
2669 uint64_t Imm = Cst->getZExtValue();
2670
2671 SDValue Ops[] = { N0, N1, getI8Imm(Imm) };
2672 unsigned Opc = Subtarget->hasAVX() ? X86::VPCMPISTRIrr :
2673 X86::PCMPISTRIrr;
2674 SDValue InFlag = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Ops,
2675 array_lengthof(Ops)), 0);
2676
2677 if (!SDValue(Node, 0).use_empty()) {
2678 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2679 X86::ECX, NVT, InFlag);
2680 InFlag = Result.getValue(2);
2681 ReplaceUses(SDValue(Node, 0), Result);
2682 }
2683 if (!SDValue(Node, 1).use_empty()) {
2684 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2685 X86::EFLAGS, NVT, InFlag);
2686 InFlag = Result.getValue(2);
2687 ReplaceUses(SDValue(Node, 1), Result);
2688 }
2689
2690 return NULL;
2691 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002692 }
2693
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002694 SDNode *ResNode = SelectCode(Node);
Evan Cheng64a752f2006-08-11 09:08:15 +00002695
Chris Lattner7c306da2010-03-02 06:34:30 +00002696 DEBUG(dbgs() << "=> ";
2697 if (ResNode == NULL || ResNode == Node)
2698 Node->dump(CurDAG);
2699 else
2700 ResNode->dump(CurDAG);
2701 dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00002702
2703 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002704}
2705
Chris Lattnerc0bad572006-06-08 18:03:49 +00002706bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002707SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002708 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002709 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002710 switch (ConstraintCode) {
2711 case 'o': // offsetable ??
2712 case 'v': // not offsetable ??
2713 default: return true;
2714 case 'm': // memory
Chris Lattnerb86faa12010-09-21 22:07:31 +00002715 if (!SelectAddr(0, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002716 return true;
2717 break;
2718 }
Chad Rosiera20e1e72012-08-01 18:39:17 +00002719
Evan Cheng04699902006-08-26 01:05:16 +00002720 OutOps.push_back(Op0);
2721 OutOps.push_back(Op1);
2722 OutOps.push_back(Op2);
2723 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002724 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002725 return false;
2726}
2727
Chad Rosiera20e1e72012-08-01 18:39:17 +00002728/// createX86ISelDag - This pass converts a legalized DAG into a
Chris Lattnerc961eea2005-11-16 01:54:32 +00002729/// X86-specific DAG, ready for instruction scheduling.
2730///
Bill Wendling98a366d2009-04-29 23:29:43 +00002731FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
Craig Topperc89c7442012-03-27 07:21:54 +00002732 CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002733 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002734}