blob: 026cea7415551d4e83fb40c1648c547f720a1473 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000036#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000038#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000039#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Chris Lattner95b2c7d2006-12-19 22:59:26 +000044STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
45
Chris Lattnerc961eea2005-11-16 01:54:32 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000052 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000053 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000057 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000061 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000062 int FrameIndex;
63 } Base;
64
65 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000066 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000067 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000068 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000070 Constant *CP;
Chris Lattner43f44aa2009-11-01 03:25:03 +000071 BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000072 const char *ES;
73 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000074 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000075 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000076
77 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000078 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000079 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000080 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000081 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000082
83 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000084 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000085 }
Chris Lattner18c59872009-06-27 04:16:01 +000086
87 bool hasBaseOrIndexReg() const {
88 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
89 }
90
91 /// isRIPRelative - Return true if this addressing mode is already RIP
92 /// relative.
93 bool isRIPRelative() const {
94 if (BaseType != RegBase) return false;
95 if (RegisterSDNode *RegNode =
96 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
97 return RegNode->getReg() == X86::RIP;
98 return false;
99 }
100
101 void setBaseReg(SDValue Reg) {
102 BaseType = RegBase;
103 Base.Reg = Reg;
104 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000105
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000106 void dump() {
David Greened7f4f242010-01-05 01:29:08 +0000107 dbgs() << "X86ISelAddressMode " << this << '\n';
108 dbgs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000109 if (Base.Reg.getNode() != 0)
110 Base.Reg.getNode()->dump();
111 else
David Greened7f4f242010-01-05 01:29:08 +0000112 dbgs() << "nul";
113 dbgs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000114 << " Scale" << Scale << '\n'
115 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000116 if (IndexReg.getNode() != 0)
117 IndexReg.getNode()->dump();
118 else
David Greened7f4f242010-01-05 01:29:08 +0000119 dbgs() << "nul";
120 dbgs() << " Disp " << Disp << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000121 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000122 if (GV)
123 GV->dump();
124 else
David Greened7f4f242010-01-05 01:29:08 +0000125 dbgs() << "nul";
126 dbgs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000127 if (CP)
128 CP->dump();
129 else
David Greened7f4f242010-01-05 01:29:08 +0000130 dbgs() << "nul";
131 dbgs() << '\n'
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000132 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000133 if (ES)
David Greened7f4f242010-01-05 01:29:08 +0000134 dbgs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000135 else
David Greened7f4f242010-01-05 01:29:08 +0000136 dbgs() << "nul";
137 dbgs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000138 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000139 };
140}
141
142namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000143 //===--------------------------------------------------------------------===//
144 /// ISel - X86 specific code to select X86 machine instructions for
145 /// SelectionDAG operations.
146 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000147 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000148 /// X86Lowering - This object fully describes how to lower LLVM code to an
149 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000150 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000151
152 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
153 /// make the right decision when generating code for different targets.
154 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000155
Evan Chengb7a75a52008-09-26 23:41:32 +0000156 /// OptForSize - If true, selector should try to optimize for code size
157 /// instead of performance.
158 bool OptForSize;
159
Chris Lattnerc961eea2005-11-16 01:54:32 +0000160 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000161 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000162 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000163 X86Lowering(*tm.getTargetLowering()),
164 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000165 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000166
167 virtual const char *getPassName() const {
168 return "X86 DAG->DAG Instruction Selection";
169 }
170
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000171 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
172
Evan Cheng014bf212010-02-15 19:41:07 +0000173 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
174
Chris Lattner7c306da2010-03-02 06:34:30 +0000175 virtual void PreprocessISelDAG();
176
Chris Lattnerc961eea2005-11-16 01:54:32 +0000177// Include the pieces autogenerated from the target description.
178#include "X86GenDAGISel.inc"
179
180 private:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000181 SDNode *Select(SDNode *N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000182 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000183 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000184
Rafael Espindola094fad32009-04-08 21:14:34 +0000185 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
186 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000187 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000188 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
189 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
190 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000191 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000192 bool SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000193 SDValue &Scale, SDValue &Index, SDValue &Disp,
194 SDValue &Segment);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000195 bool SelectLEAAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000196 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000197 bool SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000198 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattnere60f7b42010-03-01 22:51:11 +0000199 bool SelectScalarSSELoad(SDNode *Root, SDValue N,
Chris Lattner92d3ada2010-02-16 22:35:06 +0000200 SDValue &Base, SDValue &Scale,
Dan Gohman475871a2008-07-27 21:46:04 +0000201 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000202 SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +0000203 SDValue &NodeWithChain);
204
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000205 bool TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000206 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000207 SDValue &Index, SDValue &Disp,
208 SDValue &Segment);
Chris Lattner7c306da2010-03-02 06:34:30 +0000209
Chris Lattner98d45792010-03-02 22:33:56 +0000210 void PreprocessForCallLoads();
Dan Gohmanf350b272008-08-23 02:25:05 +0000211 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000212
Chris Lattnerc0bad572006-06-08 18:03:49 +0000213 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
214 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000215 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000216 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000217 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000218
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000219 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
220
Dan Gohman475871a2008-07-27 21:46:04 +0000221 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
222 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000223 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000224 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000225 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
226 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000227 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000228 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 // These are 32-bit even in 64-bit mode since RIP relative offset
230 // is 32-bit.
231 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000233 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000236 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000237 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000239 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000241 else if (AM.BlockAddr)
Dan Gohman29cbade2009-11-20 23:18:13 +0000242 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
243 true, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000244 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000246
247 if (AM.Segment.getNode())
248 Segment = AM.Segment;
249 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000251 }
252
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000253 /// getI8Imm - Return a target constant with the specified value, of type
254 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000255 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000257 }
258
Chris Lattnerc961eea2005-11-16 01:54:32 +0000259 /// getI16Imm - Return a target constant with the specified value, of type
260 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000261 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000263 }
264
265 /// getI32Imm - Return a target constant with the specified value, of type
266 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000269 }
Evan Chengf597dc72006-02-10 22:24:32 +0000270
Dan Gohman8b746962008-09-23 18:22:58 +0000271 /// getGlobalBaseReg - Return an SDNode that returns the value of
272 /// the global base register. Output instructions required to
273 /// initialize the global base register, if necessary.
274 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000275 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000276
Dan Gohmanc5534622009-06-03 20:20:00 +0000277 /// getTargetMachine - Return a reference to the TargetMachine, casted
278 /// to the target-specific type.
279 const X86TargetMachine &getTargetMachine() {
280 return static_cast<const X86TargetMachine &>(TM);
281 }
282
283 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
284 /// to the target-specific type.
285 const X86InstrInfo *getInstrInfo() {
286 return getTargetMachine().getInstrInfo();
287 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000288 };
289}
290
Evan Chengf4b4c412006-08-08 00:31:00 +0000291
Evan Cheng014bf212010-02-15 19:41:07 +0000292bool
293X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000294 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000295
Evan Cheng014bf212010-02-15 19:41:07 +0000296 if (!N.hasOneUse())
297 return false;
298
299 if (N.getOpcode() != ISD::LOAD)
300 return true;
301
302 // If N is a load, do additional profitability checks.
303 if (U == Root) {
Evan Cheng884c70c2008-11-27 00:49:46 +0000304 switch (U->getOpcode()) {
305 default: break;
Dan Gohman9ef51c82010-01-04 20:51:50 +0000306 case X86ISD::ADD:
307 case X86ISD::SUB:
308 case X86ISD::AND:
309 case X86ISD::XOR:
310 case X86ISD::OR:
Evan Cheng884c70c2008-11-27 00:49:46 +0000311 case ISD::ADD:
312 case ISD::ADDC:
313 case ISD::ADDE:
314 case ISD::AND:
315 case ISD::OR:
316 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000317 SDValue Op1 = U->getOperand(1);
318
Evan Cheng884c70c2008-11-27 00:49:46 +0000319 // If the other operand is a 8-bit immediate we should fold the immediate
320 // instead. This reduces code size.
321 // e.g.
322 // movl 4(%esp), %eax
323 // addl $4, %eax
324 // vs.
325 // movl $4, %eax
326 // addl 4(%esp), %eax
327 // The former is 2 bytes shorter. In case where the increment is 1, then
328 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000329 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000330 if (Imm->getAPIntValue().isSignedIntN(8))
331 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000332
333 // If the other operand is a TLS address, we should fold it instead.
334 // This produces
335 // movl %gs:0, %eax
336 // leal i@NTPOFF(%eax), %eax
337 // instead of
338 // movl $i@NTPOFF, %eax
339 // addl %gs:0, %eax
340 // if the block also has an access to a second TLS address this will save
341 // a load.
342 // FIXME: This is probably also true for non TLS addresses.
343 if (Op1.getOpcode() == X86ISD::Wrapper) {
344 SDValue Val = Op1.getOperand(0);
345 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
346 return false;
347 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000348 }
349 }
Evan Cheng014bf212010-02-15 19:41:07 +0000350 }
351
352 return true;
353}
354
Evan Chengab6c3bb2008-08-25 21:27:18 +0000355/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
356/// operand and move load below the call's chain operand.
357static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000358 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000359 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000360 SDValue Chain = CallSeqStart.getOperand(0);
361 if (Chain.getNode() == Load.getNode())
362 Ops.push_back(Load.getOperand(0));
363 else {
364 assert(Chain.getOpcode() == ISD::TokenFactor &&
365 "Unexpected CallSeqStart chain operand");
366 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
367 if (Chain.getOperand(i).getNode() == Load.getNode())
368 Ops.push_back(Load.getOperand(0));
369 else
370 Ops.push_back(Chain.getOperand(i));
371 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000372 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000374 Ops.clear();
375 Ops.push_back(NewChain);
376 }
377 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
378 Ops.push_back(CallSeqStart.getOperand(i));
379 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000380 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
381 Load.getOperand(1), Load.getOperand(2));
382 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000383 Ops.push_back(SDValue(Load.getNode(), 1));
384 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000385 Ops.push_back(Call.getOperand(i));
386 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
387}
388
389/// isCalleeLoad - Return true if call address is a load and it can be
390/// moved below CALLSEQ_START and the chains leading up to the call.
391/// Return the CALLSEQ_START by reference as a second output.
392static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000393 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000394 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000395 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000396 if (!LD ||
397 LD->isVolatile() ||
398 LD->getAddressingMode() != ISD::UNINDEXED ||
399 LD->getExtensionType() != ISD::NON_EXTLOAD)
400 return false;
401
402 // Now let's find the callseq_start.
403 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
404 if (!Chain.hasOneUse())
405 return false;
406 Chain = Chain.getOperand(0);
407 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000408
409 if (Chain.getOperand(0).getNode() == Callee.getNode())
410 return true;
411 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000412 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
413 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000414 return true;
415 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000416}
417
418
Chris Lattner98d45792010-03-02 22:33:56 +0000419void X86DAGToDAGISel::PreprocessForCallLoads() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000420 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
421 E = CurDAG->allnodes_end(); I != E; ++I) {
Chris Lattner98d45792010-03-02 22:33:56 +0000422 if (I->getOpcode() != X86ISD::CALL)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000423 continue;
Chris Lattnerd1b73822010-03-02 22:20:06 +0000424
Chris Lattner98d45792010-03-02 22:33:56 +0000425 /// Also try moving call address load from outside callseq_start to just
426 /// before the call to allow it to be folded.
427 ///
428 /// [Load chain]
429 /// ^
430 /// |
431 /// [Load]
432 /// ^ ^
433 /// | |
434 /// / \--
435 /// / |
436 ///[CALLSEQ_START] |
437 /// ^ |
438 /// | |
439 /// [LOAD/C2Reg] |
440 /// | |
441 /// \ /
442 /// \ /
443 /// [CALL]
Dan Gohman475871a2008-07-27 21:46:04 +0000444 SDValue Chain = I->getOperand(0);
Chris Lattner98d45792010-03-02 22:33:56 +0000445 SDValue Load = I->getOperand(1);
446 if (!isCalleeLoad(Load, Chain))
Evan Cheng70e674e2006-08-28 20:10:17 +0000447 continue;
Chris Lattner98d45792010-03-02 22:33:56 +0000448 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
449 ++NumLoadMoved;
Evan Cheng70e674e2006-08-28 20:10:17 +0000450 }
451}
452
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000453
454/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
455/// nodes that target the FP stack to be store and load to the stack. This is a
456/// gross hack. We would like to simply mark these as being illegal, but when
457/// we do that, legalize produces these when it expands calls, then expands
458/// these in the same legalize pass. We would like dag combine to be able to
459/// hack on these between the call expansion and the node legalization. As such
460/// this pass basically does "really late" legalization of these inline with the
461/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000462void X86DAGToDAGISel::PreprocessForFPConvert() {
463 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
464 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000465 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
466 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
467 continue;
468
469 // If the source and destination are SSE registers, then this is a legal
470 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000471 EVT SrcVT = N->getOperand(0).getValueType();
472 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000473 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
474 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
475 if (SrcIsSSE && DstIsSSE)
476 continue;
477
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000478 if (!SrcIsSSE && !DstIsSSE) {
479 // If this is an FPStack extension, it is a noop.
480 if (N->getOpcode() == ISD::FP_EXTEND)
481 continue;
482 // If this is a value-preserving FPStack truncation, it is a noop.
483 if (N->getConstantOperandVal(1))
484 continue;
485 }
486
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000487 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
488 // FPStack has extload and truncstore. SSE can fold direct loads into other
489 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000490 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000491 if (N->getOpcode() == ISD::FP_ROUND)
492 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
493 else
494 MemVT = SrcIsSSE ? SrcVT : DstVT;
495
Dan Gohmanf350b272008-08-23 02:25:05 +0000496 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000497 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000498
499 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000500 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000501 N->getOperand(0),
David Greenedb8d9892010-02-15 16:57:43 +0000502 MemTmp, NULL, 0, MemVT,
503 false, false, 0);
Dale Johannesend8392542009-02-03 21:48:12 +0000504 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
David Greenedb8d9892010-02-15 16:57:43 +0000505 NULL, 0, MemVT, false, false, 0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000506
507 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
508 // extload we created. This will cause general havok on the dag because
509 // anything below the conversion could be folded into other existing nodes.
510 // To avoid invalidating 'I', back it up to the convert node.
511 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000512 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000513
514 // Now that we did that, the node is dead. Increment the iterator to the
515 // next node to process, then delete N.
516 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000517 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000518 }
519}
520
Chris Lattner7c306da2010-03-02 06:34:30 +0000521void X86DAGToDAGISel::PreprocessISelDAG() {
522 OptForSize = MF->getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000523
Bill Wendling98a366d2009-04-29 23:29:43 +0000524 if (OptLevel != CodeGenOpt::None)
Chris Lattner98d45792010-03-02 22:33:56 +0000525 PreprocessForCallLoads();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000526
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000527 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000528 PreprocessForFPConvert();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000529}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000530
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000531/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
532/// the main function.
533void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
534 MachineFrameInfo *MFI) {
535 const TargetInstrInfo *TII = TM.getInstrInfo();
536 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000537 BuildMI(BB, DebugLoc::getUnknownLoc(),
538 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000539}
540
541void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
542 // If this is main, emit special code for main.
543 MachineBasicBlock *BB = MF.begin();
544 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
545 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
546}
547
Rafael Espindola094fad32009-04-08 21:14:34 +0000548
549bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
550 X86ISelAddressMode &AM) {
551 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
552 SDValue Segment = N.getOperand(0);
553
554 if (AM.Segment.getNode() == 0) {
555 AM.Segment = Segment;
556 return false;
557 }
558
559 return true;
560}
561
562bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
563 // This optimization is valid because the GNU TLS model defines that
564 // gs:0 (or fs:0 on X86-64) contains its own address.
565 // For more information see http://people.redhat.com/drepper/tls.pdf
566
567 SDValue Address = N.getOperand(1);
568 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
569 !MatchSegmentBaseAddress (Address, AM))
570 return false;
571
572 return true;
573}
574
Chris Lattner18c59872009-06-27 04:16:01 +0000575/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
576/// into an addressing mode. These wrap things that will resolve down into a
577/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000578/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000579bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000580 // If the addressing mode already has a symbol as the displacement, we can
581 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000582 if (AM.hasSymbolicDisplacement())
583 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000584
585 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000586 CodeModel::Model M = TM.getCodeModel();
587
Chris Lattner18c59872009-06-27 04:16:01 +0000588 // Handle X86-64 rip-relative addresses. We check this before checking direct
589 // folding because RIP is preferable to non-RIP accesses.
590 if (Subtarget->is64Bit() &&
591 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
592 // they cannot be folded into immediate fields.
593 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000594 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000595 // Base and index reg must be 0 in order to use %rip as base and lowering
596 // must allow RIP.
597 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000598 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
599 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000600 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000601 AM.GV = G->getGlobal();
602 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000603 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000604 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
605 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000606 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000607 AM.CP = CP->getConstVal();
608 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000609 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000610 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000611 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
612 AM.ES = S->getSymbol();
613 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000614 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000615 AM.JT = J->getIndex();
616 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000617 } else {
618 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000619 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000620 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000621
Chris Lattner18c59872009-06-27 04:16:01 +0000622 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000623 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000624 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000625 }
626
627 // Handle the case when globals fit in our immediate field: This is true for
628 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
629 // mode, this results in a non-RIP-relative computation.
630 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000631 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000632 TM.getRelocationModel() == Reloc::Static)) {
633 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
634 AM.GV = G->getGlobal();
635 AM.Disp += G->getOffset();
636 AM.SymbolFlags = G->getTargetFlags();
637 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
638 AM.CP = CP->getConstVal();
639 AM.Align = CP->getAlignment();
640 AM.Disp += CP->getOffset();
641 AM.SymbolFlags = CP->getTargetFlags();
642 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
643 AM.ES = S->getSymbol();
644 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000645 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000646 AM.JT = J->getIndex();
647 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000648 } else {
649 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000650 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000651 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000652 return false;
653 }
654
655 return true;
656}
657
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000658/// MatchAddress - Add the specified node to the specified addressing mode,
659/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000660/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000661bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
662 if (MatchAddressRecursively(N, AM, 0))
663 return true;
664
665 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
666 // a smaller encoding and avoids a scaled-index.
667 if (AM.Scale == 2 &&
668 AM.BaseType == X86ISelAddressMode::RegBase &&
669 AM.Base.Reg.getNode() == 0) {
670 AM.Base.Reg = AM.IndexReg;
671 AM.Scale = 1;
672 }
673
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000674 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
675 // because it has a smaller encoding.
676 // TODO: Which other code models can use this?
677 if (TM.getCodeModel() == CodeModel::Small &&
678 Subtarget->is64Bit() &&
679 AM.Scale == 1 &&
680 AM.BaseType == X86ISelAddressMode::RegBase &&
681 AM.Base.Reg.getNode() == 0 &&
682 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000683 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000684 AM.hasSymbolicDisplacement())
685 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
686
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000687 return false;
688}
689
690bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
691 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000692 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000693 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000694 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +0000695 dbgs() << "MatchAddress: ";
Bill Wendling12321672009-08-07 21:33:25 +0000696 AM.dump();
697 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000698 // Limit recursion.
699 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000700 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000701
702 CodeModel::Model M = TM.getCodeModel();
703
Chris Lattner18c59872009-06-27 04:16:01 +0000704 // If this is already a %rip relative address, we can only merge immediates
705 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000706 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000707 if (AM.isRIPRelative()) {
708 // FIXME: JumpTable and ExternalSymbol address currently don't like
709 // displacements. It isn't very important, but this should be fixed for
710 // consistency.
711 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000712
Chris Lattner18c59872009-06-27 04:16:01 +0000713 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
714 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000715 if (X86::isOffsetSuitableForCodeModel(Val, M,
716 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000717 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000718 return false;
719 }
720 }
721 return true;
722 }
723
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000724 switch (N.getOpcode()) {
725 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000726 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000727 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000728 if (!is64Bit ||
729 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
730 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000731 AM.Disp += Val;
732 return false;
733 }
734 break;
735 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000736
Rafael Espindola094fad32009-04-08 21:14:34 +0000737 case X86ISD::SegmentBaseAddress:
738 if (!MatchSegmentBaseAddress(N, AM))
739 return false;
740 break;
741
Rafael Espindola49a168d2009-04-12 21:55:03 +0000742 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000743 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000744 if (!MatchWrapper(N, AM))
745 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000746 break;
747
Rafael Espindola094fad32009-04-08 21:14:34 +0000748 case ISD::LOAD:
749 if (!MatchLoad(N, AM))
750 return false;
751 break;
752
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000753 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000754 if (AM.BaseType == X86ISelAddressMode::RegBase
755 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000756 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
757 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
758 return false;
759 }
760 break;
Evan Chengec693f72005-12-08 02:01:35 +0000761
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000762 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000763 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000764 break;
765
Gabor Greif93c53e52008-08-31 15:37:04 +0000766 if (ConstantSDNode
767 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000768 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000769 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
770 // that the base operand remains free for further matching. If
771 // the base doesn't end up getting used, a post-processing step
772 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000773 if (Val == 1 || Val == 2 || Val == 3) {
774 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000775 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000776
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000777 // Okay, we know that we have a scale by now. However, if the scaled
778 // value is an add of something and a constant, we can fold the
779 // constant into the disp field here.
Dan Gohmana10756e2010-01-21 02:09:26 +0000780 if (ShVal.getNode()->getOpcode() == ISD::ADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000781 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
782 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000783 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000784 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000785 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000786 if (!is64Bit ||
787 X86::isOffsetSuitableForCodeModel(Disp, M,
788 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000789 AM.Disp = Disp;
790 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000791 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000792 } else {
793 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000794 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000795 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000796 }
797 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000798 }
Evan Chengec693f72005-12-08 02:01:35 +0000799
Dan Gohman83688052007-10-22 20:22:24 +0000800 case ISD::SMUL_LOHI:
801 case ISD::UMUL_LOHI:
802 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000803 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000804 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000805 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000806 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000807 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000808 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000809 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000810 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000811 if (ConstantSDNode
812 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000813 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
814 CN->getZExtValue() == 9) {
815 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000816
Gabor Greifba36cb52008-08-28 21:40:38 +0000817 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000818 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000819
820 // Okay, we know that we have a scale by now. However, if the scaled
821 // value is an add of something and a constant, we can fold the
822 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000823 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
824 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
825 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000826 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000827 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000828 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000829 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000830 if (!is64Bit ||
831 X86::isOffsetSuitableForCodeModel(Disp, M,
832 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000833 AM.Disp = Disp;
834 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000835 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000836 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000837 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000838 }
839
840 AM.IndexReg = AM.Base.Reg = Reg;
841 return false;
842 }
Chris Lattner62412262007-02-04 20:18:17 +0000843 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000844 break;
845
Dan Gohman3cd90a12009-05-11 18:02:53 +0000846 case ISD::SUB: {
847 // Given A-B, if A can be completely folded into the address and
848 // the index field with the index field unused, use -B as the index.
849 // This is a win if a has multiple parts that can be folded into
850 // the address. Also, this saves a mov if the base register has
851 // other uses, since it avoids a two-address sub instruction, however
852 // it costs an additional mov if the index register has other uses.
853
854 // Test if the LHS of the sub can be folded.
855 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000856 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000857 AM = Backup;
858 break;
859 }
860 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000861 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000862 AM = Backup;
863 break;
864 }
865 int Cost = 0;
866 SDValue RHS = N.getNode()->getOperand(1);
867 // If the RHS involves a register with multiple uses, this
868 // transformation incurs an extra mov, due to the neg instruction
869 // clobbering its operand.
870 if (!RHS.getNode()->hasOneUse() ||
871 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
872 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
873 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
874 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +0000876 ++Cost;
877 // If the base is a register with multiple uses, this
878 // transformation may save a mov.
879 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
880 AM.Base.Reg.getNode() &&
881 !AM.Base.Reg.getNode()->hasOneUse()) ||
882 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
883 --Cost;
884 // If the folded LHS was interesting, this transformation saves
885 // address arithmetic.
886 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
887 ((AM.Disp != 0) && (Backup.Disp == 0)) +
888 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
889 --Cost;
890 // If it doesn't look like it may be an overall win, don't do it.
891 if (Cost >= 0) {
892 AM = Backup;
893 break;
894 }
895
896 // Ok, the transformation is legal and appears profitable. Go for it.
897 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
898 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
899 AM.IndexReg = Neg;
900 AM.Scale = 1;
901
902 // Insert the new nodes into the topological ordering.
903 if (Zero.getNode()->getNodeId() == -1 ||
904 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
905 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
906 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
907 }
908 if (Neg.getNode()->getNodeId() == -1 ||
909 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
910 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
911 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
912 }
913 return false;
914 }
915
Evan Cheng8e278262009-01-17 07:09:27 +0000916 case ISD::ADD: {
917 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000918 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
919 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000920 return false;
921 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000922 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
923 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000924 return false;
925 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000926
927 // If we couldn't fold both operands into the address at the same time,
928 // see if we can just put each operand into a register and fold at least
929 // the add.
930 if (AM.BaseType == X86ISelAddressMode::RegBase &&
931 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +0000932 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +0000933 AM.Base.Reg = N.getNode()->getOperand(0);
934 AM.IndexReg = N.getNode()->getOperand(1);
935 AM.Scale = 1;
936 return false;
937 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000938 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000939 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000940
Chris Lattner62412262007-02-04 20:18:17 +0000941 case ISD::OR:
942 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000943 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
944 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000945 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000946 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000947 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000948 // Address could not have picked a GV address for the displacement.
949 AM.GV == NULL &&
950 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000951 (!is64Bit ||
952 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
953 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000954 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000955 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000956 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000957 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000958 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000959 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000960 }
961 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000962
963 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000964 // Perform some heroic transforms on an and of a constant-count shift
965 // with a constant to enable use of the scaled offset field.
966
Dan Gohman475871a2008-07-27 21:46:04 +0000967 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000968 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000969
Evan Cheng1314b002007-12-13 00:43:27 +0000970 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +0000971 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000972
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000973 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +0000974 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
975 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
976 if (!C1 || !C2) break;
977
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000978 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
979 // allows us to convert the shift and and into an h-register extract and
980 // a scaled index.
981 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
982 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +0000983 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000984 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000985 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000986 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
987 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
988 X, Eight);
989 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
990 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +0000992 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
993 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000994
995 // Insert the new nodes into the topological ordering.
996 if (Eight.getNode()->getNodeId() == -1 ||
997 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
998 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
999 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1000 }
1001 if (Mask.getNode()->getNodeId() == -1 ||
1002 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1003 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1004 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1005 }
1006 if (Srl.getNode()->getNodeId() == -1 ||
1007 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1008 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1009 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1010 }
1011 if (And.getNode()->getNodeId() == -1 ||
1012 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1013 CurDAG->RepositionNode(N.getNode(), And.getNode());
1014 And.getNode()->setNodeId(N.getNode()->getNodeId());
1015 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001016 if (ShlCount.getNode()->getNodeId() == -1 ||
1017 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1018 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1019 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1020 }
1021 if (Shl.getNode()->getNodeId() == -1 ||
1022 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1023 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1024 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1025 }
1026 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001027 AM.IndexReg = And;
1028 AM.Scale = (1 << ScaleLog);
1029 return false;
1030 }
1031 }
1032
1033 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1034 // allows us to fold the shift into this addressing mode.
1035 if (Shift.getOpcode() != ISD::SHL) break;
1036
Evan Cheng1314b002007-12-13 00:43:27 +00001037 // Not likely to be profitable if either the AND or SHIFT node has more
1038 // than one use (unless all uses are for address computation). Besides,
1039 // isel mechanism requires their node ids to be reused.
1040 if (!N.hasOneUse() || !Shift.hasOneUse())
1041 break;
1042
1043 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001044 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001045 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1046 break;
1047
1048 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001049 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001050 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001051 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1052 NewANDMask);
1053 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001054 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001055
1056 // Insert the new nodes into the topological ordering.
1057 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1058 CurDAG->RepositionNode(X.getNode(), C1);
1059 C1->setNodeId(X.getNode()->getNodeId());
1060 }
1061 if (NewANDMask.getNode()->getNodeId() == -1 ||
1062 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1063 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1064 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1065 }
1066 if (NewAND.getNode()->getNodeId() == -1 ||
1067 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1068 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1069 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1070 }
1071 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1072 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1073 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1074 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1075 }
1076
Dan Gohman7b8e9642008-10-13 20:52:04 +00001077 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001078
1079 AM.Scale = 1 << ShiftCst;
1080 AM.IndexReg = NewAND;
1081 return false;
1082 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001083 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001084
Rafael Espindola523249f2009-03-31 16:16:57 +00001085 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001086}
1087
1088/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1089/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001090bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001091 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001092 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001093 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001094 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001095 AM.IndexReg = N;
1096 AM.Scale = 1;
1097 return false;
1098 }
1099
1100 // Otherwise, we cannot select it.
1101 return true;
1102 }
1103
1104 // Default, generate it as a register.
1105 AM.BaseType = X86ISelAddressMode::RegBase;
1106 AM.Base.Reg = N;
1107 return false;
1108}
1109
Evan Chengec693f72005-12-08 02:01:35 +00001110/// SelectAddr - returns true if it is able pattern match an addressing mode.
1111/// It returns the operands which make up the maximal addressing mode it can
1112/// match by reference.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001113bool X86DAGToDAGISel::SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +00001114 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001115 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001116 X86ISelAddressMode AM;
Evan Chengc7928f82009-12-18 01:59:21 +00001117 if (MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001118 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001119
Owen Andersone50ed302009-08-10 22:56:29 +00001120 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001121 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001122 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001123 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001124 }
Evan Cheng8700e142006-01-11 06:09:51 +00001125
Gabor Greifba36cb52008-08-28 21:40:38 +00001126 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001127 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001128
Rafael Espindola094fad32009-04-08 21:14:34 +00001129 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001130 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001131}
1132
Chris Lattner3a7cd952006-10-07 21:55:32 +00001133/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1134/// match a load whose top elements are either undef or zeros. The load flavor
1135/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner64b49862010-02-17 06:07:47 +00001136///
1137/// We also return:
Chris Lattnera170b5e2010-02-21 03:17:59 +00001138/// PatternChainNode: this is the matched node that has a chain input and
1139/// output.
Chris Lattnere60f7b42010-03-01 22:51:11 +00001140bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
Dan Gohman475871a2008-07-27 21:46:04 +00001141 SDValue N, SDValue &Base,
1142 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001143 SDValue &Disp, SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +00001144 SDValue &PatternNodeWithChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001145 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001146 PatternNodeWithChain = N.getOperand(0);
1147 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1148 PatternNodeWithChain.hasOneUse() &&
Chris Lattnerf1c64282010-02-21 04:53:34 +00001149 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1150 IsLegalToFold(N.getOperand(0), N.getNode(), Root)) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001151 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattner92d3ada2010-02-16 22:35:06 +00001152 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp,Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001153 return false;
1154 return true;
1155 }
1156 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001157
1158 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001159 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001160 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001161 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001162 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001163 N.getOperand(0).getNode()->hasOneUse() &&
1164 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattner92d3ada2010-02-16 22:35:06 +00001165 N.getOperand(0).getOperand(0).hasOneUse() &&
1166 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1167 IsLegalToFold(N.getOperand(0), N.getNode(), Root)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00001168 // Okay, this is a zero extending load. Fold it.
1169 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattner92d3ada2010-02-16 22:35:06 +00001170 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001171 return false;
Chris Lattnera170b5e2010-02-21 03:17:59 +00001172 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001173 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001174 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001175 return false;
1176}
1177
1178
Evan Cheng51a9ed92006-02-25 10:09:08 +00001179/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1180/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001181bool X86DAGToDAGISel::SelectLEAAddr(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001182 SDValue &Base, SDValue &Scale,
1183 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001184 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001185
1186 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1187 // segments.
1188 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001189 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001190 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001191 if (MatchAddress(N, AM))
1192 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001193 assert (T == AM.Segment);
1194 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001195
Owen Andersone50ed302009-08-10 22:56:29 +00001196 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001197 unsigned Complexity = 0;
1198 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001199 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001200 Complexity = 1;
1201 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001202 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001203 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1204 Complexity = 4;
1205
Gabor Greifba36cb52008-08-28 21:40:38 +00001206 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001207 Complexity++;
1208 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001209 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001210
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001211 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1212 // a simple shift.
1213 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001214 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001215
1216 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1217 // to a LEA. This is determined with some expermentation but is by no means
1218 // optimal (especially for code size consideration). LEA is nice because of
1219 // its three-address nature. Tweak the cost function again when we can run
1220 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001221 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001222 // For X86-64, we should always use lea to materialize RIP relative
1223 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001224 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001225 Complexity = 4;
1226 else
1227 Complexity += 2;
1228 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001229
Gabor Greifba36cb52008-08-28 21:40:38 +00001230 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001231 Complexity++;
1232
Chris Lattner25142782009-07-11 22:50:33 +00001233 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001234 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001235 return false;
1236
1237 SDValue Segment;
1238 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1239 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001240}
1241
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001242/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001243bool X86DAGToDAGISel::SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001244 SDValue &Scale, SDValue &Index,
1245 SDValue &Disp) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001246 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1247 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1248
1249 X86ISelAddressMode AM;
1250 AM.GV = GA->getGlobal();
1251 AM.Disp += GA->getOffset();
1252 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001253 AM.SymbolFlags = GA->getTargetFlags();
1254
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001256 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001258 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001259 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001260 }
1261
1262 SDValue Segment;
1263 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1264 return true;
1265}
1266
1267
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001268bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001269 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001270 SDValue &Index, SDValue &Disp,
1271 SDValue &Segment) {
Chris Lattnerd1b73822010-03-02 22:20:06 +00001272 if (!ISD::isNON_EXTLoad(N.getNode()) ||
1273 !IsProfitableToFold(N, P, P) ||
1274 !IsLegalToFold(N, P, P))
1275 return false;
1276
1277 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001278}
1279
Dan Gohman8b746962008-09-23 18:22:58 +00001280/// getGlobalBaseReg - Return an SDNode that returns the value of
1281/// the global base register. Output instructions required to
1282/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001283///
Evan Cheng9ade2182006-08-26 05:34:46 +00001284SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001285 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001287}
1288
Evan Chengb245d922006-05-20 01:36:52 +00001289static SDNode *FindCallStartFromCall(SDNode *Node) {
1290 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001292 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001293 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001294}
1295
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001296SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1297 SDValue Chain = Node->getOperand(0);
1298 SDValue In1 = Node->getOperand(1);
1299 SDValue In2L = Node->getOperand(2);
1300 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001301 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001302 if (!SelectAddr(In1.getNode(), In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001303 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001304 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1305 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1306 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1307 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1308 MVT::i32, MVT::i32, MVT::Other, Ops,
1309 array_lengthof(Ops));
1310 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1311 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001312}
Christopher Lambc59e5212007-08-10 21:48:46 +00001313
Owen Andersone50ed302009-08-10 22:56:29 +00001314SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001315 if (Node->hasAnyUseOfValue(0))
1316 return 0;
1317
1318 // Optimize common patterns for __sync_add_and_fetch and
1319 // __sync_sub_and_fetch where the result is not used. This allows us
1320 // to use "lock" version of add, sub, inc, dec instructions.
1321 // FIXME: Do not use special instructions but instead add the "lock"
1322 // prefix to the target node somehow. The extra information will then be
1323 // transferred to machine instruction and it denotes the prefix.
1324 SDValue Chain = Node->getOperand(0);
1325 SDValue Ptr = Node->getOperand(1);
1326 SDValue Val = Node->getOperand(2);
1327 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001328 if (!SelectAddr(Ptr.getNode(), Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Evan Cheng37b73872009-07-30 08:33:02 +00001329 return 0;
1330
1331 bool isInc = false, isDec = false, isSub = false, isCN = false;
1332 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1333 if (CN) {
1334 isCN = true;
1335 int64_t CNVal = CN->getSExtValue();
1336 if (CNVal == 1)
1337 isInc = true;
1338 else if (CNVal == -1)
1339 isDec = true;
1340 else if (CNVal >= 0)
1341 Val = CurDAG->getTargetConstant(CNVal, NVT);
1342 else {
1343 isSub = true;
1344 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1345 }
1346 } else if (Val.hasOneUse() &&
1347 Val.getOpcode() == ISD::SUB &&
1348 X86::isZeroNode(Val.getOperand(0))) {
1349 isSub = true;
1350 Val = Val.getOperand(1);
1351 }
1352
1353 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001354 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001355 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001356 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001357 if (isInc)
1358 Opc = X86::LOCK_INC8m;
1359 else if (isDec)
1360 Opc = X86::LOCK_DEC8m;
1361 else if (isSub) {
1362 if (isCN)
1363 Opc = X86::LOCK_SUB8mi;
1364 else
1365 Opc = X86::LOCK_SUB8mr;
1366 } else {
1367 if (isCN)
1368 Opc = X86::LOCK_ADD8mi;
1369 else
1370 Opc = X86::LOCK_ADD8mr;
1371 }
1372 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001373 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001374 if (isInc)
1375 Opc = X86::LOCK_INC16m;
1376 else if (isDec)
1377 Opc = X86::LOCK_DEC16m;
1378 else if (isSub) {
1379 if (isCN) {
1380 if (Predicate_i16immSExt8(Val.getNode()))
1381 Opc = X86::LOCK_SUB16mi8;
1382 else
1383 Opc = X86::LOCK_SUB16mi;
1384 } else
1385 Opc = X86::LOCK_SUB16mr;
1386 } else {
1387 if (isCN) {
1388 if (Predicate_i16immSExt8(Val.getNode()))
1389 Opc = X86::LOCK_ADD16mi8;
1390 else
1391 Opc = X86::LOCK_ADD16mi;
1392 } else
1393 Opc = X86::LOCK_ADD16mr;
1394 }
1395 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001396 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001397 if (isInc)
1398 Opc = X86::LOCK_INC32m;
1399 else if (isDec)
1400 Opc = X86::LOCK_DEC32m;
1401 else if (isSub) {
1402 if (isCN) {
1403 if (Predicate_i32immSExt8(Val.getNode()))
1404 Opc = X86::LOCK_SUB32mi8;
1405 else
1406 Opc = X86::LOCK_SUB32mi;
1407 } else
1408 Opc = X86::LOCK_SUB32mr;
1409 } else {
1410 if (isCN) {
1411 if (Predicate_i32immSExt8(Val.getNode()))
1412 Opc = X86::LOCK_ADD32mi8;
1413 else
1414 Opc = X86::LOCK_ADD32mi;
1415 } else
1416 Opc = X86::LOCK_ADD32mr;
1417 }
1418 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001420 if (isInc)
1421 Opc = X86::LOCK_INC64m;
1422 else if (isDec)
1423 Opc = X86::LOCK_DEC64m;
1424 else if (isSub) {
1425 Opc = X86::LOCK_SUB64mr;
1426 if (isCN) {
1427 if (Predicate_i64immSExt8(Val.getNode()))
1428 Opc = X86::LOCK_SUB64mi8;
1429 else if (Predicate_i64immSExt32(Val.getNode()))
1430 Opc = X86::LOCK_SUB64mi32;
1431 }
1432 } else {
1433 Opc = X86::LOCK_ADD64mr;
1434 if (isCN) {
1435 if (Predicate_i64immSExt8(Val.getNode()))
1436 Opc = X86::LOCK_ADD64mi8;
1437 else if (Predicate_i64immSExt32(Val.getNode()))
1438 Opc = X86::LOCK_ADD64mi32;
1439 }
1440 }
1441 break;
1442 }
1443
1444 DebugLoc dl = Node->getDebugLoc();
Chris Lattner518bb532010-02-09 19:54:29 +00001445 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +00001446 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001447 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1448 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001449 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001450 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1451 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1452 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001453 SDValue RetVals[] = { Undef, Ret };
1454 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1455 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001456 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1457 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1458 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001459 SDValue RetVals[] = { Undef, Ret };
1460 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1461 }
1462}
1463
Dan Gohman11596ed2009-10-09 20:35:19 +00001464/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1465/// any uses which require the SF or OF bits to be accurate.
1466static bool HasNoSignedComparisonUses(SDNode *N) {
1467 // Examine each user of the node.
1468 for (SDNode::use_iterator UI = N->use_begin(),
1469 UE = N->use_end(); UI != UE; ++UI) {
1470 // Only examine CopyToReg uses.
1471 if (UI->getOpcode() != ISD::CopyToReg)
1472 return false;
1473 // Only examine CopyToReg uses that copy to EFLAGS.
1474 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1475 X86::EFLAGS)
1476 return false;
1477 // Examine each user of the CopyToReg use.
1478 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1479 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1480 // Only examine the Flag result.
1481 if (FlagUI.getUse().getResNo() != 1) continue;
1482 // Anything unusual: assume conservatively.
1483 if (!FlagUI->isMachineOpcode()) return false;
1484 // Examine the opcode of the user.
1485 switch (FlagUI->getMachineOpcode()) {
1486 // These comparisons don't treat the most significant bit specially.
1487 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1488 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1489 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1490 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001491 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1492 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
Dan Gohman11596ed2009-10-09 20:35:19 +00001493 case X86::CMOVA16rr: case X86::CMOVA16rm:
1494 case X86::CMOVA32rr: case X86::CMOVA32rm:
1495 case X86::CMOVA64rr: case X86::CMOVA64rm:
1496 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1497 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1498 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1499 case X86::CMOVB16rr: case X86::CMOVB16rm:
1500 case X86::CMOVB32rr: case X86::CMOVB32rm:
1501 case X86::CMOVB64rr: case X86::CMOVB64rm:
1502 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1503 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1504 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1505 case X86::CMOVE16rr: case X86::CMOVE16rm:
1506 case X86::CMOVE32rr: case X86::CMOVE32rm:
1507 case X86::CMOVE64rr: case X86::CMOVE64rm:
1508 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1509 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1510 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1511 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1512 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1513 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1514 case X86::CMOVP16rr: case X86::CMOVP16rm:
1515 case X86::CMOVP32rr: case X86::CMOVP32rm:
1516 case X86::CMOVP64rr: case X86::CMOVP64rm:
1517 continue;
1518 // Anything else: assume conservatively.
1519 default: return false;
1520 }
1521 }
1522 }
1523 return true;
1524}
1525
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001526SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Owen Andersone50ed302009-08-10 22:56:29 +00001527 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001528 unsigned Opc, MOpc;
1529 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001530 DebugLoc dl = Node->getDebugLoc();
1531
Chris Lattner7c306da2010-03-02 06:34:30 +00001532 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
Evan Chengf597dc72006-02-10 22:24:32 +00001533
Dan Gohmane8be6c62008-07-17 19:10:17 +00001534 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +00001535 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001536 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001537 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001538
Evan Cheng0114e942006-01-06 20:36:21 +00001539 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001540 default: break;
1541 case X86ISD::GlobalBaseReg:
1542 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001543
Dan Gohman72677342009-08-02 16:10:52 +00001544 case X86ISD::ATOMOR64_DAG:
1545 return SelectAtomic64(Node, X86::ATOMOR6432);
1546 case X86ISD::ATOMXOR64_DAG:
1547 return SelectAtomic64(Node, X86::ATOMXOR6432);
1548 case X86ISD::ATOMADD64_DAG:
1549 return SelectAtomic64(Node, X86::ATOMADD6432);
1550 case X86ISD::ATOMSUB64_DAG:
1551 return SelectAtomic64(Node, X86::ATOMSUB6432);
1552 case X86ISD::ATOMNAND64_DAG:
1553 return SelectAtomic64(Node, X86::ATOMNAND6432);
1554 case X86ISD::ATOMAND64_DAG:
1555 return SelectAtomic64(Node, X86::ATOMAND6432);
1556 case X86ISD::ATOMSWAP64_DAG:
1557 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001558
Dan Gohman72677342009-08-02 16:10:52 +00001559 case ISD::ATOMIC_LOAD_ADD: {
1560 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1561 if (RetVal)
1562 return RetVal;
1563 break;
1564 }
1565
1566 case ISD::SMUL_LOHI:
1567 case ISD::UMUL_LOHI: {
1568 SDValue N0 = Node->getOperand(0);
1569 SDValue N1 = Node->getOperand(1);
1570
1571 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001572 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001573 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001574 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1576 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1577 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1578 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001579 }
Bill Wendling12321672009-08-07 21:33:25 +00001580 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001581 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001582 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1584 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1585 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1586 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001587 }
Bill Wendling12321672009-08-07 21:33:25 +00001588 }
Dan Gohman72677342009-08-02 16:10:52 +00001589
1590 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001591 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001592 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001593 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1594 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1595 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1596 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001597 }
1598
1599 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001600 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001601 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001602 if (!foldedLoad) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001603 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001604 if (foldedLoad)
1605 std::swap(N0, N1);
1606 }
1607
1608 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1609 N0, SDValue()).getValue(1);
1610
1611 if (foldedLoad) {
1612 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1613 InFlag };
1614 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001615 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1616 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001617 InFlag = SDValue(CNode, 1);
1618 // Update the chain.
1619 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1620 } else {
1621 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001622 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001623 }
1624
1625 // Copy the low half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001626 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001627 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1628 LoReg, NVT, InFlag);
1629 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001630 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001631 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001632 }
1633 // Copy the high half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001634 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001635 SDValue Result;
1636 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1637 // Prevent use of AH in a REX instruction by referencing AX instead.
1638 // Shift it down 8 bits.
1639 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001641 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001642 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1643 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001644 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001645 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001646 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1647 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001648 } else {
1649 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1650 HiReg, NVT, InFlag);
1651 InFlag = Result.getValue(2);
1652 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001653 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001654 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001655 }
1656
Dan Gohman72677342009-08-02 16:10:52 +00001657 return NULL;
1658 }
1659
1660 case ISD::SDIVREM:
1661 case ISD::UDIVREM: {
1662 SDValue N0 = Node->getOperand(0);
1663 SDValue N1 = Node->getOperand(1);
1664
1665 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001666 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001667 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001668 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001669 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1670 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1671 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1672 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001673 }
Bill Wendling12321672009-08-07 21:33:25 +00001674 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001675 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001676 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001677 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1678 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1679 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1680 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001681 }
Bill Wendling12321672009-08-07 21:33:25 +00001682 }
Dan Gohman72677342009-08-02 16:10:52 +00001683
Chris Lattner9e323832009-12-23 01:45:04 +00001684 unsigned LoReg, HiReg, ClrReg;
Dan Gohman72677342009-08-02 16:10:52 +00001685 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001686 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001687 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001688 case MVT::i8:
Chris Lattner9e323832009-12-23 01:45:04 +00001689 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman72677342009-08-02 16:10:52 +00001690 ClrOpcode = 0;
1691 SExtOpcode = X86::CBW;
1692 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001694 LoReg = X86::AX; HiReg = X86::DX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001695 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
Dan Gohman72677342009-08-02 16:10:52 +00001696 SExtOpcode = X86::CWD;
1697 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 case MVT::i32:
Chris Lattner9e323832009-12-23 01:45:04 +00001699 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman72677342009-08-02 16:10:52 +00001700 ClrOpcode = X86::MOV32r0;
1701 SExtOpcode = X86::CDQ;
1702 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 case MVT::i64:
Chris Lattner9e323832009-12-23 01:45:04 +00001704 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001705 ClrOpcode = X86::MOV64r0;
Dan Gohman72677342009-08-02 16:10:52 +00001706 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001707 break;
1708 }
1709
Dan Gohman72677342009-08-02 16:10:52 +00001710 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001711 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001712 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001713
Dan Gohman72677342009-08-02 16:10:52 +00001714 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001715 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001716 // Special case for div8, just use a move with zero extension to AX to
1717 // clear the upper 8 bits (AH).
1718 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001719 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman72677342009-08-02 16:10:52 +00001720 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1721 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001722 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1723 MVT::Other, Ops,
1724 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001725 Chain = Move.getValue(1);
1726 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001727 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001728 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001729 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001730 Chain = CurDAG->getEntryNode();
1731 }
1732 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1733 InFlag = Chain.getValue(1);
1734 } else {
1735 InFlag =
1736 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1737 LoReg, N0, SDValue()).getValue(1);
1738 if (isSigned && !signBitIsZero) {
1739 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001740 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001741 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001742 } else {
1743 // Zero out the high part, effectively zero extending the input.
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001744 SDValue ClrNode =
1745 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Chris Lattner9e323832009-12-23 01:45:04 +00001746 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman72677342009-08-02 16:10:52 +00001747 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001748 }
Evan Cheng948f3432006-01-06 23:19:29 +00001749 }
Dan Gohman525178c2007-10-08 18:33:35 +00001750
Dan Gohman72677342009-08-02 16:10:52 +00001751 if (foldedLoad) {
1752 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1753 InFlag };
1754 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001755 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1756 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001757 InFlag = SDValue(CNode, 1);
1758 // Update the chain.
1759 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1760 } else {
1761 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001762 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001763 }
Evan Cheng948f3432006-01-06 23:19:29 +00001764
Dan Gohman72677342009-08-02 16:10:52 +00001765 // Copy the division (low) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001766 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001767 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1768 LoReg, NVT, InFlag);
1769 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001770 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001771 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001772 }
1773 // Copy the remainder (high) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001774 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001775 SDValue Result;
1776 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1777 // Prevent use of AH in a REX instruction by referencing AX instead.
1778 // Shift it down 8 bits.
1779 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001781 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001782 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001783 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001785 0);
1786 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001787 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1788 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001789 } else {
1790 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1791 HiReg, NVT, InFlag);
1792 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001793 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001794 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattner7c306da2010-03-02 06:34:30 +00001795 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman72677342009-08-02 16:10:52 +00001796 }
Dan Gohman72677342009-08-02 16:10:52 +00001797 return NULL;
1798 }
1799
Dan Gohman6a402dc2009-08-19 18:16:17 +00001800 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001801 SDValue N0 = Node->getOperand(0);
1802 SDValue N1 = Node->getOperand(1);
1803
1804 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
1805 // use a smaller encoding.
1806 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
1807 N0.getValueType() != MVT::i8 &&
1808 X86::isZeroNode(N1)) {
1809 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
1810 if (!C) break;
1811
1812 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00001813 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
1814 (!(C->getZExtValue() & 0x80) ||
1815 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001816 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
1817 SDValue Reg = N0.getNode()->getOperand(0);
1818
1819 // On x86-32, only the ABCD registers have 8-bit subregisters.
1820 if (!Subtarget->is64Bit()) {
1821 TargetRegisterClass *TRC = 0;
1822 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1823 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1824 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1825 default: llvm_unreachable("Unsupported TEST operand type!");
1826 }
1827 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001828 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1829 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001830 }
1831
1832 // Extract the l-register.
1833 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1834 MVT::i8, Reg);
1835
1836 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00001837 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001838 }
1839
1840 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00001841 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
1842 (!(C->getZExtValue() & 0x8000) ||
1843 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001844 // Shift the immediate right by 8 bits.
1845 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
1846 MVT::i8);
1847 SDValue Reg = N0.getNode()->getOperand(0);
1848
1849 // Put the value in an ABCD register.
1850 TargetRegisterClass *TRC = 0;
1851 switch (N0.getValueType().getSimpleVT().SimpleTy) {
1852 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
1853 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
1854 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
1855 default: llvm_unreachable("Unsupported TEST operand type!");
1856 }
1857 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00001858 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
1859 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001860
1861 // Extract the h-register.
1862 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
1863 MVT::i8, Reg);
1864
1865 // Emit a testb. No special NOREX tricks are needed since there's
1866 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00001867 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
1868 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001869 }
1870
1871 // For example, "testl %eax, $32776" to "testw %ax, $32776".
1872 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001873 N0.getValueType() != MVT::i16 &&
1874 (!(C->getZExtValue() & 0x8000) ||
1875 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001876 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
1877 SDValue Reg = N0.getNode()->getOperand(0);
1878
1879 // Extract the 16-bit subregister.
1880 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
1881 MVT::i16, Reg);
1882
1883 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00001884 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001885 }
1886
1887 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
1888 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00001889 N0.getValueType() == MVT::i64 &&
1890 (!(C->getZExtValue() & 0x80000000) ||
1891 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00001892 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
1893 SDValue Reg = N0.getNode()->getOperand(0);
1894
1895 // Extract the 32-bit subregister.
1896 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
1897 MVT::i32, Reg);
1898
1899 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00001900 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00001901 }
1902 }
1903 break;
1904 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001905 }
1906
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001907 SDNode *ResNode = SelectCode(Node);
Evan Cheng64a752f2006-08-11 09:08:15 +00001908
Chris Lattner7c306da2010-03-02 06:34:30 +00001909 DEBUG(dbgs() << "=> ";
1910 if (ResNode == NULL || ResNode == Node)
1911 Node->dump(CurDAG);
1912 else
1913 ResNode->dump(CurDAG);
1914 dbgs() << '\n');
Evan Cheng64a752f2006-08-11 09:08:15 +00001915
1916 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001917}
1918
Chris Lattnerc0bad572006-06-08 18:03:49 +00001919bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001920SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001921 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001922 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001923 switch (ConstraintCode) {
1924 case 'o': // offsetable ??
1925 case 'v': // not offsetable ??
1926 default: return true;
1927 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001928 if (!SelectAddr(Op.getNode(), Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001929 return true;
1930 break;
1931 }
1932
Evan Cheng04699902006-08-26 01:05:16 +00001933 OutOps.push_back(Op0);
1934 OutOps.push_back(Op1);
1935 OutOps.push_back(Op2);
1936 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001937 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001938 return false;
1939}
1940
Chris Lattnerc961eea2005-11-16 01:54:32 +00001941/// createX86ISelDag - This pass converts a legalized DAG into a
1942/// X86-specific DAG, ready for instruction scheduling.
1943///
Bill Wendling98a366d2009-04-29 23:29:43 +00001944FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1945 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001946 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001947}