blob: 586d6efaa67ea977b993483b8d53610ee8d74bb5 [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
Evan Chengdb8d56b2008-06-30 20:45:06 +0000171 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000172 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000173 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000174
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000175 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
176
Evan Cheng014bf212010-02-15 19:41:07 +0000177 virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const;
178
Chris Lattnerc961eea2005-11-16 01:54:32 +0000179// Include the pieces autogenerated from the target description.
180#include "X86GenDAGISel.inc"
181
182 private:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000183 SDNode *Select(SDNode *N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000184 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000185 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000186
Rafael Espindola094fad32009-04-08 21:14:34 +0000187 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
188 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000189 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000190 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
191 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
192 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000193 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000194 bool SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000195 SDValue &Scale, SDValue &Index, SDValue &Disp,
196 SDValue &Segment);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000197 bool SelectLEAAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000199 bool SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000200 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattnere60f7b42010-03-01 22:51:11 +0000201 bool SelectScalarSSELoad(SDNode *Root, SDValue N,
Chris Lattner92d3ada2010-02-16 22:35:06 +0000202 SDValue &Base, SDValue &Scale,
Dan Gohman475871a2008-07-27 21:46:04 +0000203 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000204 SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +0000205 SDValue &NodeWithChain);
206
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000207 bool TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +0000208 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000209 SDValue &Index, SDValue &Disp,
210 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000211 void PreprocessForRMW();
212 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000213
Chris Lattnerc0bad572006-06-08 18:03:49 +0000214 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
215 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000216 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000217 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000218 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000219
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000220 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
221
Dan Gohman475871a2008-07-27 21:46:04 +0000222 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
223 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000224 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000225 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
227 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000228 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000229 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 // These are 32-bit even in 64-bit mode since RIP relative offset
231 // is 32-bit.
232 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000234 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000235 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000237 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000238 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000240 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000242 else if (AM.BlockAddr)
Dan Gohman29cbade2009-11-20 23:18:13 +0000243 Disp = CurDAG->getBlockAddress(AM.BlockAddr, MVT::i32,
244 true, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000245 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000247
248 if (AM.Segment.getNode())
249 Segment = AM.Segment;
250 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000252 }
253
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000254 /// getI8Imm - Return a target constant with the specified value, of type
255 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000256 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000258 }
259
Chris Lattnerc961eea2005-11-16 01:54:32 +0000260 /// getI16Imm - Return a target constant with the specified value, of type
261 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000262 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000264 }
265
266 /// getI32Imm - Return a target constant with the specified value, of type
267 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000268 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000270 }
Evan Chengf597dc72006-02-10 22:24:32 +0000271
Dan Gohman8b746962008-09-23 18:22:58 +0000272 /// getGlobalBaseReg - Return an SDNode that returns the value of
273 /// the global base register. Output instructions required to
274 /// initialize the global base register, if necessary.
275 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000276 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000277
Dan Gohmanc5534622009-06-03 20:20:00 +0000278 /// getTargetMachine - Return a reference to the TargetMachine, casted
279 /// to the target-specific type.
280 const X86TargetMachine &getTargetMachine() {
281 return static_cast<const X86TargetMachine &>(TM);
282 }
283
284 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
285 /// to the target-specific type.
286 const X86InstrInfo *getInstrInfo() {
287 return getTargetMachine().getInstrInfo();
288 }
289
Evan Cheng23addc02006-02-10 22:46:26 +0000290#ifndef NDEBUG
291 unsigned Indent;
292#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000293 };
294}
295
Evan Chengf4b4c412006-08-08 00:31:00 +0000296
Evan Cheng014bf212010-02-15 19:41:07 +0000297bool
298X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000299 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000300
Evan Cheng014bf212010-02-15 19:41:07 +0000301 if (!N.hasOneUse())
302 return false;
303
304 if (N.getOpcode() != ISD::LOAD)
305 return true;
306
307 // If N is a load, do additional profitability checks.
308 if (U == Root) {
Evan Cheng884c70c2008-11-27 00:49:46 +0000309 switch (U->getOpcode()) {
310 default: break;
Dan Gohman9ef51c82010-01-04 20:51:50 +0000311 case X86ISD::ADD:
312 case X86ISD::SUB:
313 case X86ISD::AND:
314 case X86ISD::XOR:
315 case X86ISD::OR:
Evan Cheng884c70c2008-11-27 00:49:46 +0000316 case ISD::ADD:
317 case ISD::ADDC:
318 case ISD::ADDE:
319 case ISD::AND:
320 case ISD::OR:
321 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000322 SDValue Op1 = U->getOperand(1);
323
Evan Cheng884c70c2008-11-27 00:49:46 +0000324 // If the other operand is a 8-bit immediate we should fold the immediate
325 // instead. This reduces code size.
326 // e.g.
327 // movl 4(%esp), %eax
328 // addl $4, %eax
329 // vs.
330 // movl $4, %eax
331 // addl 4(%esp), %eax
332 // The former is 2 bytes shorter. In case where the increment is 1, then
333 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000334 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000335 if (Imm->getAPIntValue().isSignedIntN(8))
336 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000337
338 // If the other operand is a TLS address, we should fold it instead.
339 // This produces
340 // movl %gs:0, %eax
341 // leal i@NTPOFF(%eax), %eax
342 // instead of
343 // movl $i@NTPOFF, %eax
344 // addl %gs:0, %eax
345 // if the block also has an access to a second TLS address this will save
346 // a load.
347 // FIXME: This is probably also true for non TLS addresses.
348 if (Op1.getOpcode() == X86ISD::Wrapper) {
349 SDValue Val = Op1.getOperand(0);
350 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
351 return false;
352 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000353 }
354 }
Evan Cheng014bf212010-02-15 19:41:07 +0000355 }
356
357 return true;
358}
359
Evan Cheng70e674e2006-08-28 20:10:17 +0000360/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
361/// and move load below the TokenFactor. Replace store's chain operand with
362/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000363static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000364 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000365 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000366 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
367 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000368 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000369 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000370 Ops.push_back(TF.getOperand(i));
Dan Gohmanaae317a2009-08-06 09:22:57 +0000371 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
372 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
373 Load.getOperand(1),
374 Load.getOperand(2));
375 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000376 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000377}
378
Nate Begeman206a3572009-09-16 03:20:46 +0000379/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. The
380/// chain produced by the load must only be used by the store's chain operand,
381/// otherwise this may produce a cycle in the DAG.
Evan Chengcd0baf22008-05-23 21:23:16 +0000382///
Dan Gohman475871a2008-07-27 21:46:04 +0000383static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
384 SDValue &Load) {
David Greeneee9c5952010-01-15 23:23:41 +0000385 if (N.getOpcode() == ISD::BIT_CONVERT) {
386 if (!N.hasOneUse())
387 return false;
Evan Chengcd0baf22008-05-23 21:23:16 +0000388 N = N.getOperand(0);
David Greeneee9c5952010-01-15 23:23:41 +0000389 }
Evan Chengcd0baf22008-05-23 21:23:16 +0000390
391 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
392 if (!LD || LD->isVolatile())
393 return false;
394 if (LD->getAddressingMode() != ISD::UNINDEXED)
395 return false;
396
397 ISD::LoadExtType ExtType = LD->getExtensionType();
398 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
399 return false;
400
401 if (N.hasOneUse() &&
Nate Begeman206a3572009-09-16 03:20:46 +0000402 LD->hasNUsesOfValue(1, 1) &&
Evan Chengcd0baf22008-05-23 21:23:16 +0000403 N.getOperand(1) == Address &&
Nate Begeman206a3572009-09-16 03:20:46 +0000404 LD->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000405 Load = N;
406 return true;
407 }
408 return false;
409}
410
Evan Chengab6c3bb2008-08-25 21:27:18 +0000411/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
412/// operand and move load below the call's chain operand.
413static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000414 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000415 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000416 SDValue Chain = CallSeqStart.getOperand(0);
417 if (Chain.getNode() == Load.getNode())
418 Ops.push_back(Load.getOperand(0));
419 else {
420 assert(Chain.getOpcode() == ISD::TokenFactor &&
421 "Unexpected CallSeqStart chain operand");
422 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
423 if (Chain.getOperand(i).getNode() == Load.getNode())
424 Ops.push_back(Load.getOperand(0));
425 else
426 Ops.push_back(Chain.getOperand(i));
427 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000428 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000430 Ops.clear();
431 Ops.push_back(NewChain);
432 }
433 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
434 Ops.push_back(CallSeqStart.getOperand(i));
435 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000436 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
437 Load.getOperand(1), Load.getOperand(2));
438 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000439 Ops.push_back(SDValue(Load.getNode(), 1));
440 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000441 Ops.push_back(Call.getOperand(i));
442 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
443}
444
445/// isCalleeLoad - Return true if call address is a load and it can be
446/// moved below CALLSEQ_START and the chains leading up to the call.
447/// Return the CALLSEQ_START by reference as a second output.
448static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000449 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000450 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000451 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000452 if (!LD ||
453 LD->isVolatile() ||
454 LD->getAddressingMode() != ISD::UNINDEXED ||
455 LD->getExtensionType() != ISD::NON_EXTLOAD)
456 return false;
457
458 // Now let's find the callseq_start.
459 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
460 if (!Chain.hasOneUse())
461 return false;
462 Chain = Chain.getOperand(0);
463 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000464
465 if (Chain.getOperand(0).getNode() == Callee.getNode())
466 return true;
467 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000468 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
469 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000470 return true;
471 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000472}
473
474
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000475/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000476/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000477/// This allows the instruction selector to pick more read-modify-write
478/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000479///
480/// [Load chain]
481/// ^
482/// |
483/// [Load]
484/// ^ ^
485/// | |
486/// / \-
487/// / |
488/// [TokenFactor] [Op]
489/// ^ ^
490/// | |
491/// \ /
492/// \ /
493/// [Store]
494///
495/// The fact the store's chain operand != load's chain will prevent the
496/// (store (op (load))) instruction from being selected. We can transform it to:
497///
498/// [Load chain]
499/// ^
500/// |
501/// [TokenFactor]
502/// ^
503/// |
504/// [Load]
505/// ^ ^
506/// | |
507/// | \-
508/// | |
509/// | [Op]
510/// | ^
511/// | |
512/// \ /
513/// \ /
514/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000515void X86DAGToDAGISel::PreprocessForRMW() {
516 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
517 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000518 if (I->getOpcode() == X86ISD::CALL) {
519 /// Also try moving call address load from outside callseq_start to just
520 /// before the call to allow it to be folded.
521 ///
522 /// [Load chain]
523 /// ^
524 /// |
525 /// [Load]
526 /// ^ ^
527 /// | |
528 /// / \--
529 /// / |
530 ///[CALLSEQ_START] |
531 /// ^ |
532 /// | |
533 /// [LOAD/C2Reg] |
534 /// | |
535 /// \ /
536 /// \ /
537 /// [CALL]
538 SDValue Chain = I->getOperand(0);
539 SDValue Load = I->getOperand(1);
540 if (!isCalleeLoad(Load, Chain))
541 continue;
542 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
543 ++NumLoadMoved;
544 continue;
545 }
546
Evan Cheng8b2794a2006-10-13 21:14:26 +0000547 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000548 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000549 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000550
Gabor Greifba36cb52008-08-28 21:40:38 +0000551 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000552 continue;
553
Dan Gohman475871a2008-07-27 21:46:04 +0000554 SDValue N1 = I->getOperand(1);
555 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000556 if ((N1.getValueType().isFloatingPoint() &&
557 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000558 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000559 continue;
560
561 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000562 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000563 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000564 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000565 case ISD::ADD:
566 case ISD::MUL:
567 case ISD::AND:
568 case ISD::OR:
569 case ISD::XOR:
570 case ISD::ADDC:
571 case ISD::ADDE:
572 case ISD::VECTOR_SHUFFLE: {
573 SDValue N10 = N1.getOperand(0);
574 SDValue N11 = N1.getOperand(1);
575 RModW = isRMWLoad(N10, Chain, N2, Load);
576 if (!RModW)
577 RModW = isRMWLoad(N11, Chain, N2, Load);
578 break;
579 }
580 case ISD::SUB:
581 case ISD::SHL:
582 case ISD::SRA:
583 case ISD::SRL:
584 case ISD::ROTL:
585 case ISD::ROTR:
586 case ISD::SUBC:
587 case ISD::SUBE:
588 case X86ISD::SHLD:
589 case X86ISD::SHRD: {
590 SDValue N10 = N1.getOperand(0);
591 RModW = isRMWLoad(N10, Chain, N2, Load);
592 break;
593 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000594 }
595
Evan Cheng82a35b32006-08-29 06:44:17 +0000596 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000597 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000598 ++NumLoadMoved;
David Greenecf495bc2010-01-20 20:13:31 +0000599 checkForCycles(I);
Evan Cheng82a35b32006-08-29 06:44:17 +0000600 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000601 }
602}
603
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000604
605/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
606/// nodes that target the FP stack to be store and load to the stack. This is a
607/// gross hack. We would like to simply mark these as being illegal, but when
608/// we do that, legalize produces these when it expands calls, then expands
609/// these in the same legalize pass. We would like dag combine to be able to
610/// hack on these between the call expansion and the node legalization. As such
611/// this pass basically does "really late" legalization of these inline with the
612/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000613void X86DAGToDAGISel::PreprocessForFPConvert() {
614 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
615 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000616 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
617 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
618 continue;
619
620 // If the source and destination are SSE registers, then this is a legal
621 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000622 EVT SrcVT = N->getOperand(0).getValueType();
623 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000624 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
625 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
626 if (SrcIsSSE && DstIsSSE)
627 continue;
628
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000629 if (!SrcIsSSE && !DstIsSSE) {
630 // If this is an FPStack extension, it is a noop.
631 if (N->getOpcode() == ISD::FP_EXTEND)
632 continue;
633 // If this is a value-preserving FPStack truncation, it is a noop.
634 if (N->getConstantOperandVal(1))
635 continue;
636 }
637
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000638 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
639 // FPStack has extload and truncstore. SSE can fold direct loads into other
640 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000641 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000642 if (N->getOpcode() == ISD::FP_ROUND)
643 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
644 else
645 MemVT = SrcIsSSE ? SrcVT : DstVT;
646
Dan Gohmanf350b272008-08-23 02:25:05 +0000647 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000648 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000649
650 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000651 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000652 N->getOperand(0),
David Greenedb8d9892010-02-15 16:57:43 +0000653 MemTmp, NULL, 0, MemVT,
654 false, false, 0);
Dale Johannesend8392542009-02-03 21:48:12 +0000655 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
David Greenedb8d9892010-02-15 16:57:43 +0000656 NULL, 0, MemVT, false, false, 0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000657
658 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
659 // extload we created. This will cause general havok on the dag because
660 // anything below the conversion could be folded into other existing nodes.
661 // To avoid invalidating 'I', back it up to the convert node.
662 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000663 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000664
665 // Now that we did that, the node is dead. Increment the iterator to the
666 // next node to process, then delete N.
667 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000668 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000669 }
670}
671
Chris Lattnerc961eea2005-11-16 01:54:32 +0000672/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
673/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000674void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000675 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000676 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000677
Bill Wendling98a366d2009-04-29 23:29:43 +0000678 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000679 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000680
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000681 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000682 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000683
Chris Lattnerc961eea2005-11-16 01:54:32 +0000684 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000685#ifndef NDEBUG
David Greened7f4f242010-01-05 01:29:08 +0000686 DEBUG(dbgs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000687 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000688#endif
David Greene8ad4c002008-10-27 21:56:29 +0000689 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000690#ifndef NDEBUG
David Greened7f4f242010-01-05 01:29:08 +0000691 DEBUG(dbgs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000692#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000693
Dan Gohmanf350b272008-08-23 02:25:05 +0000694 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000695}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000696
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000697/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
698/// the main function.
699void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
700 MachineFrameInfo *MFI) {
701 const TargetInstrInfo *TII = TM.getInstrInfo();
702 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000703 BuildMI(BB, DebugLoc::getUnknownLoc(),
704 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000705}
706
707void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
708 // If this is main, emit special code for main.
709 MachineBasicBlock *BB = MF.begin();
710 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
711 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
712}
713
Rafael Espindola094fad32009-04-08 21:14:34 +0000714
715bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
716 X86ISelAddressMode &AM) {
717 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
718 SDValue Segment = N.getOperand(0);
719
720 if (AM.Segment.getNode() == 0) {
721 AM.Segment = Segment;
722 return false;
723 }
724
725 return true;
726}
727
728bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
729 // This optimization is valid because the GNU TLS model defines that
730 // gs:0 (or fs:0 on X86-64) contains its own address.
731 // For more information see http://people.redhat.com/drepper/tls.pdf
732
733 SDValue Address = N.getOperand(1);
734 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
735 !MatchSegmentBaseAddress (Address, AM))
736 return false;
737
738 return true;
739}
740
Chris Lattner18c59872009-06-27 04:16:01 +0000741/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
742/// into an addressing mode. These wrap things that will resolve down into a
743/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000744/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000745bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000746 // If the addressing mode already has a symbol as the displacement, we can
747 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000748 if (AM.hasSymbolicDisplacement())
749 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000750
751 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000752 CodeModel::Model M = TM.getCodeModel();
753
Chris Lattner18c59872009-06-27 04:16:01 +0000754 // Handle X86-64 rip-relative addresses. We check this before checking direct
755 // folding because RIP is preferable to non-RIP accesses.
756 if (Subtarget->is64Bit() &&
757 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
758 // they cannot be folded into immediate fields.
759 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000760 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000761 // Base and index reg must be 0 in order to use %rip as base and lowering
762 // must allow RIP.
763 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000764 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
765 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000766 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000767 AM.GV = G->getGlobal();
768 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000769 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000770 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
771 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000772 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000773 AM.CP = CP->getConstVal();
774 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000775 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000776 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000777 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
778 AM.ES = S->getSymbol();
779 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000780 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000781 AM.JT = J->getIndex();
782 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000783 } else {
784 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000785 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000786 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000787
Chris Lattner18c59872009-06-27 04:16:01 +0000788 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000790 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000791 }
792
793 // Handle the case when globals fit in our immediate field: This is true for
794 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
795 // mode, this results in a non-RIP-relative computation.
796 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000797 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000798 TM.getRelocationModel() == Reloc::Static)) {
799 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
800 AM.GV = G->getGlobal();
801 AM.Disp += G->getOffset();
802 AM.SymbolFlags = G->getTargetFlags();
803 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
804 AM.CP = CP->getConstVal();
805 AM.Align = CP->getAlignment();
806 AM.Disp += CP->getOffset();
807 AM.SymbolFlags = CP->getTargetFlags();
808 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
809 AM.ES = S->getSymbol();
810 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000811 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000812 AM.JT = J->getIndex();
813 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000814 } else {
815 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +0000816 AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000817 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000818 return false;
819 }
820
821 return true;
822}
823
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000824/// MatchAddress - Add the specified node to the specified addressing mode,
825/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000826/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000827bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
828 if (MatchAddressRecursively(N, AM, 0))
829 return true;
830
831 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
832 // a smaller encoding and avoids a scaled-index.
833 if (AM.Scale == 2 &&
834 AM.BaseType == X86ISelAddressMode::RegBase &&
835 AM.Base.Reg.getNode() == 0) {
836 AM.Base.Reg = AM.IndexReg;
837 AM.Scale = 1;
838 }
839
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000840 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
841 // because it has a smaller encoding.
842 // TODO: Which other code models can use this?
843 if (TM.getCodeModel() == CodeModel::Small &&
844 Subtarget->is64Bit() &&
845 AM.Scale == 1 &&
846 AM.BaseType == X86ISelAddressMode::RegBase &&
847 AM.Base.Reg.getNode() == 0 &&
848 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000849 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000850 AM.hasSymbolicDisplacement())
851 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
852
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000853 return false;
854}
855
856bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
857 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000858 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000859 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000860 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +0000861 dbgs() << "MatchAddress: ";
Bill Wendling12321672009-08-07 21:33:25 +0000862 AM.dump();
863 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000864 // Limit recursion.
865 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000866 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000867
868 CodeModel::Model M = TM.getCodeModel();
869
Chris Lattner18c59872009-06-27 04:16:01 +0000870 // If this is already a %rip relative address, we can only merge immediates
871 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000872 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000873 if (AM.isRIPRelative()) {
874 // FIXME: JumpTable and ExternalSymbol address currently don't like
875 // displacements. It isn't very important, but this should be fixed for
876 // consistency.
877 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000878
Chris Lattner18c59872009-06-27 04:16:01 +0000879 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
880 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000881 if (X86::isOffsetSuitableForCodeModel(Val, M,
882 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000883 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000884 return false;
885 }
886 }
887 return true;
888 }
889
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000890 switch (N.getOpcode()) {
891 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000892 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000893 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000894 if (!is64Bit ||
895 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
896 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000897 AM.Disp += Val;
898 return false;
899 }
900 break;
901 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000902
Rafael Espindola094fad32009-04-08 21:14:34 +0000903 case X86ISD::SegmentBaseAddress:
904 if (!MatchSegmentBaseAddress(N, AM))
905 return false;
906 break;
907
Rafael Espindola49a168d2009-04-12 21:55:03 +0000908 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000909 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000910 if (!MatchWrapper(N, AM))
911 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000912 break;
913
Rafael Espindola094fad32009-04-08 21:14:34 +0000914 case ISD::LOAD:
915 if (!MatchLoad(N, AM))
916 return false;
917 break;
918
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000919 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000920 if (AM.BaseType == X86ISelAddressMode::RegBase
921 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000922 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
923 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
924 return false;
925 }
926 break;
Evan Chengec693f72005-12-08 02:01:35 +0000927
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000928 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000929 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000930 break;
931
Gabor Greif93c53e52008-08-31 15:37:04 +0000932 if (ConstantSDNode
933 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000934 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000935 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
936 // that the base operand remains free for further matching. If
937 // the base doesn't end up getting used, a post-processing step
938 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000939 if (Val == 1 || Val == 2 || Val == 3) {
940 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000941 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000942
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000943 // Okay, we know that we have a scale by now. However, if the scaled
944 // value is an add of something and a constant, we can fold the
945 // constant into the disp field here.
Dan Gohmana10756e2010-01-21 02:09:26 +0000946 if (ShVal.getNode()->getOpcode() == ISD::ADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000947 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
948 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000949 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000950 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000951 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000952 if (!is64Bit ||
953 X86::isOffsetSuitableForCodeModel(Disp, M,
954 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000955 AM.Disp = Disp;
956 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000957 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000958 } else {
959 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000960 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000961 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000962 }
963 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000964 }
Evan Chengec693f72005-12-08 02:01:35 +0000965
Dan Gohman83688052007-10-22 20:22:24 +0000966 case ISD::SMUL_LOHI:
967 case ISD::UMUL_LOHI:
968 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000969 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000970 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000971 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000972 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000973 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000974 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000975 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000976 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000977 if (ConstantSDNode
978 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000979 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
980 CN->getZExtValue() == 9) {
981 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000982
Gabor Greifba36cb52008-08-28 21:40:38 +0000983 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000984 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000985
986 // Okay, we know that we have a scale by now. However, if the scaled
987 // value is an add of something and a constant, we can fold the
988 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000989 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
990 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
991 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000992 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000993 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000994 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000995 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000996 if (!is64Bit ||
997 X86::isOffsetSuitableForCodeModel(Disp, M,
998 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000999 AM.Disp = Disp;
1000 else
Gabor Greifba36cb52008-08-28 21:40:38 +00001001 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001002 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001003 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001004 }
1005
1006 AM.IndexReg = AM.Base.Reg = Reg;
1007 return false;
1008 }
Chris Lattner62412262007-02-04 20:18:17 +00001009 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001010 break;
1011
Dan Gohman3cd90a12009-05-11 18:02:53 +00001012 case ISD::SUB: {
1013 // Given A-B, if A can be completely folded into the address and
1014 // the index field with the index field unused, use -B as the index.
1015 // This is a win if a has multiple parts that can be folded into
1016 // the address. Also, this saves a mov if the base register has
1017 // other uses, since it avoids a two-address sub instruction, however
1018 // it costs an additional mov if the index register has other uses.
1019
1020 // Test if the LHS of the sub can be folded.
1021 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001022 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001023 AM = Backup;
1024 break;
1025 }
1026 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001027 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001028 AM = Backup;
1029 break;
1030 }
1031 int Cost = 0;
1032 SDValue RHS = N.getNode()->getOperand(1);
1033 // If the RHS involves a register with multiple uses, this
1034 // transformation incurs an extra mov, due to the neg instruction
1035 // clobbering its operand.
1036 if (!RHS.getNode()->hasOneUse() ||
1037 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1038 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1039 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1040 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001042 ++Cost;
1043 // If the base is a register with multiple uses, this
1044 // transformation may save a mov.
1045 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1046 AM.Base.Reg.getNode() &&
1047 !AM.Base.Reg.getNode()->hasOneUse()) ||
1048 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1049 --Cost;
1050 // If the folded LHS was interesting, this transformation saves
1051 // address arithmetic.
1052 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1053 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1054 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1055 --Cost;
1056 // If it doesn't look like it may be an overall win, don't do it.
1057 if (Cost >= 0) {
1058 AM = Backup;
1059 break;
1060 }
1061
1062 // Ok, the transformation is legal and appears profitable. Go for it.
1063 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1064 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1065 AM.IndexReg = Neg;
1066 AM.Scale = 1;
1067
1068 // Insert the new nodes into the topological ordering.
1069 if (Zero.getNode()->getNodeId() == -1 ||
1070 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1071 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1072 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1073 }
1074 if (Neg.getNode()->getNodeId() == -1 ||
1075 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1076 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1077 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1078 }
1079 return false;
1080 }
1081
Evan Cheng8e278262009-01-17 07:09:27 +00001082 case ISD::ADD: {
1083 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001084 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1085 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001086 return false;
1087 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001088 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1089 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001090 return false;
1091 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001092
1093 // If we couldn't fold both operands into the address at the same time,
1094 // see if we can just put each operand into a register and fold at least
1095 // the add.
1096 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1097 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001098 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001099 AM.Base.Reg = N.getNode()->getOperand(0);
1100 AM.IndexReg = N.getNode()->getOperand(1);
1101 AM.Scale = 1;
1102 return false;
1103 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001104 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001105 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001106
Chris Lattner62412262007-02-04 20:18:17 +00001107 case ISD::OR:
1108 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001109 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1110 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001111 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001112 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001113 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001114 // Address could not have picked a GV address for the displacement.
1115 AM.GV == NULL &&
1116 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001117 (!is64Bit ||
1118 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1119 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001120 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001121 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001122 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001123 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001124 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001125 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001126 }
1127 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001128
1129 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001130 // Perform some heroic transforms on an and of a constant-count shift
1131 // with a constant to enable use of the scaled offset field.
1132
Dan Gohman475871a2008-07-27 21:46:04 +00001133 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001134 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001135
Evan Cheng1314b002007-12-13 00:43:27 +00001136 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001137 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001138
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001139 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001140 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1141 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1142 if (!C1 || !C2) break;
1143
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001144 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1145 // allows us to convert the shift and and into an h-register extract and
1146 // a scaled index.
1147 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1148 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001149 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001150 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001151 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001152 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1153 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1154 X, Eight);
1155 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1156 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001157 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001158 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1159 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001160
1161 // Insert the new nodes into the topological ordering.
1162 if (Eight.getNode()->getNodeId() == -1 ||
1163 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1164 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1165 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1166 }
1167 if (Mask.getNode()->getNodeId() == -1 ||
1168 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1169 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1170 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1171 }
1172 if (Srl.getNode()->getNodeId() == -1 ||
1173 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1174 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1175 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1176 }
1177 if (And.getNode()->getNodeId() == -1 ||
1178 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1179 CurDAG->RepositionNode(N.getNode(), And.getNode());
1180 And.getNode()->setNodeId(N.getNode()->getNodeId());
1181 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001182 if (ShlCount.getNode()->getNodeId() == -1 ||
1183 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1184 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1185 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1186 }
1187 if (Shl.getNode()->getNodeId() == -1 ||
1188 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1189 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1190 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1191 }
1192 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001193 AM.IndexReg = And;
1194 AM.Scale = (1 << ScaleLog);
1195 return false;
1196 }
1197 }
1198
1199 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1200 // allows us to fold the shift into this addressing mode.
1201 if (Shift.getOpcode() != ISD::SHL) break;
1202
Evan Cheng1314b002007-12-13 00:43:27 +00001203 // Not likely to be profitable if either the AND or SHIFT node has more
1204 // than one use (unless all uses are for address computation). Besides,
1205 // isel mechanism requires their node ids to be reused.
1206 if (!N.hasOneUse() || !Shift.hasOneUse())
1207 break;
1208
1209 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001210 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001211 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1212 break;
1213
1214 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001215 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001216 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001217 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1218 NewANDMask);
1219 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001220 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001221
1222 // Insert the new nodes into the topological ordering.
1223 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1224 CurDAG->RepositionNode(X.getNode(), C1);
1225 C1->setNodeId(X.getNode()->getNodeId());
1226 }
1227 if (NewANDMask.getNode()->getNodeId() == -1 ||
1228 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1229 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1230 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1231 }
1232 if (NewAND.getNode()->getNodeId() == -1 ||
1233 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1234 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1235 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1236 }
1237 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1238 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1239 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1240 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1241 }
1242
Dan Gohman7b8e9642008-10-13 20:52:04 +00001243 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001244
1245 AM.Scale = 1 << ShiftCst;
1246 AM.IndexReg = NewAND;
1247 return false;
1248 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001249 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001250
Rafael Espindola523249f2009-03-31 16:16:57 +00001251 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001252}
1253
1254/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1255/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001256bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001257 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001258 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001259 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001260 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001261 AM.IndexReg = N;
1262 AM.Scale = 1;
1263 return false;
1264 }
1265
1266 // Otherwise, we cannot select it.
1267 return true;
1268 }
1269
1270 // Default, generate it as a register.
1271 AM.BaseType = X86ISelAddressMode::RegBase;
1272 AM.Base.Reg = N;
1273 return false;
1274}
1275
Evan Chengec693f72005-12-08 02:01:35 +00001276/// SelectAddr - returns true if it is able pattern match an addressing mode.
1277/// It returns the operands which make up the maximal addressing mode it can
1278/// match by reference.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001279bool X86DAGToDAGISel::SelectAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +00001280 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001281 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001282 X86ISelAddressMode AM;
Evan Chengc7928f82009-12-18 01:59:21 +00001283 if (MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001284 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001285
Owen Andersone50ed302009-08-10 22:56:29 +00001286 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001287 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001289 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001290 }
Evan Cheng8700e142006-01-11 06:09:51 +00001291
Gabor Greifba36cb52008-08-28 21:40:38 +00001292 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001293 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001294
Rafael Espindola094fad32009-04-08 21:14:34 +00001295 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001296 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001297}
1298
Chris Lattner3a7cd952006-10-07 21:55:32 +00001299/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1300/// match a load whose top elements are either undef or zeros. The load flavor
1301/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner64b49862010-02-17 06:07:47 +00001302///
1303/// We also return:
Chris Lattnera170b5e2010-02-21 03:17:59 +00001304/// PatternChainNode: this is the matched node that has a chain input and
1305/// output.
Chris Lattnere60f7b42010-03-01 22:51:11 +00001306bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue N, SDValue &Base,
1308 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001309 SDValue &Disp, SDValue &Segment,
Chris Lattnera170b5e2010-02-21 03:17:59 +00001310 SDValue &PatternNodeWithChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001311 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001312 PatternNodeWithChain = N.getOperand(0);
1313 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1314 PatternNodeWithChain.hasOneUse() &&
Chris Lattnerf1c64282010-02-21 04:53:34 +00001315 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1316 IsLegalToFold(N.getOperand(0), N.getNode(), Root)) {
Chris Lattnera170b5e2010-02-21 03:17:59 +00001317 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattner92d3ada2010-02-16 22:35:06 +00001318 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp,Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001319 return false;
1320 return true;
1321 }
1322 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001323
1324 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001325 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001326 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001327 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001328 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001329 N.getOperand(0).getNode()->hasOneUse() &&
1330 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattner92d3ada2010-02-16 22:35:06 +00001331 N.getOperand(0).getOperand(0).hasOneUse() &&
1332 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1333 IsLegalToFold(N.getOperand(0), N.getNode(), Root)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00001334 // Okay, this is a zero extending load. Fold it.
1335 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattner92d3ada2010-02-16 22:35:06 +00001336 if (!SelectAddr(Root, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001337 return false;
Chris Lattnera170b5e2010-02-21 03:17:59 +00001338 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001339 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001340 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001341 return false;
1342}
1343
1344
Evan Cheng51a9ed92006-02-25 10:09:08 +00001345/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1346/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001347bool X86DAGToDAGISel::SelectLEAAddr(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001348 SDValue &Base, SDValue &Scale,
1349 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001350 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001351
1352 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1353 // segments.
1354 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001355 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001356 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001357 if (MatchAddress(N, AM))
1358 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001359 assert (T == AM.Segment);
1360 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001361
Owen Andersone50ed302009-08-10 22:56:29 +00001362 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001363 unsigned Complexity = 0;
1364 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001365 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001366 Complexity = 1;
1367 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001368 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001369 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1370 Complexity = 4;
1371
Gabor Greifba36cb52008-08-28 21:40:38 +00001372 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001373 Complexity++;
1374 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001375 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001376
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001377 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1378 // a simple shift.
1379 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001380 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001381
1382 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1383 // to a LEA. This is determined with some expermentation but is by no means
1384 // optimal (especially for code size consideration). LEA is nice because of
1385 // its three-address nature. Tweak the cost function again when we can run
1386 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001387 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001388 // For X86-64, we should always use lea to materialize RIP relative
1389 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001390 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001391 Complexity = 4;
1392 else
1393 Complexity += 2;
1394 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001395
Gabor Greifba36cb52008-08-28 21:40:38 +00001396 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001397 Complexity++;
1398
Chris Lattner25142782009-07-11 22:50:33 +00001399 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001400 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001401 return false;
1402
1403 SDValue Segment;
1404 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1405 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001406}
1407
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001408/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001409bool X86DAGToDAGISel::SelectTLSADDRAddr(SDNode *Op, SDValue N, SDValue &Base,
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001410 SDValue &Scale, SDValue &Index,
1411 SDValue &Disp) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001412 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1413 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1414
1415 X86ISelAddressMode AM;
1416 AM.GV = GA->getGlobal();
1417 AM.Disp += GA->getOffset();
1418 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001419 AM.SymbolFlags = GA->getTargetFlags();
1420
Owen Anderson825b72b2009-08-11 20:47:22 +00001421 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001422 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001424 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001425 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001426 }
1427
1428 SDValue Segment;
1429 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1430 return true;
1431}
1432
1433
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001434bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +00001435 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001436 SDValue &Index, SDValue &Disp,
1437 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001438 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng014bf212010-02-15 19:41:07 +00001439 IsProfitableToFold(N, P, P) &&
1440 IsLegalToFold(N, P, P))
Rafael Espindola094fad32009-04-08 21:14:34 +00001441 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001442 return false;
1443}
1444
Dan Gohman8b746962008-09-23 18:22:58 +00001445/// getGlobalBaseReg - Return an SDNode that returns the value of
1446/// the global base register. Output instructions required to
1447/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001448///
Evan Cheng9ade2182006-08-26 05:34:46 +00001449SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001450 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001451 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001452}
1453
Evan Chengb245d922006-05-20 01:36:52 +00001454static SDNode *FindCallStartFromCall(SDNode *Node) {
1455 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001456 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001457 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001458 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001459}
1460
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001461SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1462 SDValue Chain = Node->getOperand(0);
1463 SDValue In1 = Node->getOperand(1);
1464 SDValue In2L = Node->getOperand(2);
1465 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001466 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001467 if (!SelectAddr(In1.getNode(), In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001468 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001469 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1470 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1471 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1472 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1473 MVT::i32, MVT::i32, MVT::Other, Ops,
1474 array_lengthof(Ops));
1475 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1476 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001477}
Christopher Lambc59e5212007-08-10 21:48:46 +00001478
Owen Andersone50ed302009-08-10 22:56:29 +00001479SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001480 if (Node->hasAnyUseOfValue(0))
1481 return 0;
1482
1483 // Optimize common patterns for __sync_add_and_fetch and
1484 // __sync_sub_and_fetch where the result is not used. This allows us
1485 // to use "lock" version of add, sub, inc, dec instructions.
1486 // FIXME: Do not use special instructions but instead add the "lock"
1487 // prefix to the target node somehow. The extra information will then be
1488 // transferred to machine instruction and it denotes the prefix.
1489 SDValue Chain = Node->getOperand(0);
1490 SDValue Ptr = Node->getOperand(1);
1491 SDValue Val = Node->getOperand(2);
1492 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001493 if (!SelectAddr(Ptr.getNode(), Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Evan Cheng37b73872009-07-30 08:33:02 +00001494 return 0;
1495
1496 bool isInc = false, isDec = false, isSub = false, isCN = false;
1497 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1498 if (CN) {
1499 isCN = true;
1500 int64_t CNVal = CN->getSExtValue();
1501 if (CNVal == 1)
1502 isInc = true;
1503 else if (CNVal == -1)
1504 isDec = true;
1505 else if (CNVal >= 0)
1506 Val = CurDAG->getTargetConstant(CNVal, NVT);
1507 else {
1508 isSub = true;
1509 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1510 }
1511 } else if (Val.hasOneUse() &&
1512 Val.getOpcode() == ISD::SUB &&
1513 X86::isZeroNode(Val.getOperand(0))) {
1514 isSub = true;
1515 Val = Val.getOperand(1);
1516 }
1517
1518 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001519 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001520 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001521 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001522 if (isInc)
1523 Opc = X86::LOCK_INC8m;
1524 else if (isDec)
1525 Opc = X86::LOCK_DEC8m;
1526 else if (isSub) {
1527 if (isCN)
1528 Opc = X86::LOCK_SUB8mi;
1529 else
1530 Opc = X86::LOCK_SUB8mr;
1531 } else {
1532 if (isCN)
1533 Opc = X86::LOCK_ADD8mi;
1534 else
1535 Opc = X86::LOCK_ADD8mr;
1536 }
1537 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001539 if (isInc)
1540 Opc = X86::LOCK_INC16m;
1541 else if (isDec)
1542 Opc = X86::LOCK_DEC16m;
1543 else if (isSub) {
1544 if (isCN) {
1545 if (Predicate_i16immSExt8(Val.getNode()))
1546 Opc = X86::LOCK_SUB16mi8;
1547 else
1548 Opc = X86::LOCK_SUB16mi;
1549 } else
1550 Opc = X86::LOCK_SUB16mr;
1551 } else {
1552 if (isCN) {
1553 if (Predicate_i16immSExt8(Val.getNode()))
1554 Opc = X86::LOCK_ADD16mi8;
1555 else
1556 Opc = X86::LOCK_ADD16mi;
1557 } else
1558 Opc = X86::LOCK_ADD16mr;
1559 }
1560 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001562 if (isInc)
1563 Opc = X86::LOCK_INC32m;
1564 else if (isDec)
1565 Opc = X86::LOCK_DEC32m;
1566 else if (isSub) {
1567 if (isCN) {
1568 if (Predicate_i32immSExt8(Val.getNode()))
1569 Opc = X86::LOCK_SUB32mi8;
1570 else
1571 Opc = X86::LOCK_SUB32mi;
1572 } else
1573 Opc = X86::LOCK_SUB32mr;
1574 } else {
1575 if (isCN) {
1576 if (Predicate_i32immSExt8(Val.getNode()))
1577 Opc = X86::LOCK_ADD32mi8;
1578 else
1579 Opc = X86::LOCK_ADD32mi;
1580 } else
1581 Opc = X86::LOCK_ADD32mr;
1582 }
1583 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001585 if (isInc)
1586 Opc = X86::LOCK_INC64m;
1587 else if (isDec)
1588 Opc = X86::LOCK_DEC64m;
1589 else if (isSub) {
1590 Opc = X86::LOCK_SUB64mr;
1591 if (isCN) {
1592 if (Predicate_i64immSExt8(Val.getNode()))
1593 Opc = X86::LOCK_SUB64mi8;
1594 else if (Predicate_i64immSExt32(Val.getNode()))
1595 Opc = X86::LOCK_SUB64mi32;
1596 }
1597 } else {
1598 Opc = X86::LOCK_ADD64mr;
1599 if (isCN) {
1600 if (Predicate_i64immSExt8(Val.getNode()))
1601 Opc = X86::LOCK_ADD64mi8;
1602 else if (Predicate_i64immSExt32(Val.getNode()))
1603 Opc = X86::LOCK_ADD64mi32;
1604 }
1605 }
1606 break;
1607 }
1608
1609 DebugLoc dl = Node->getDebugLoc();
Chris Lattner518bb532010-02-09 19:54:29 +00001610 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +00001611 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001612 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1613 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001614 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001615 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1616 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1617 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001618 SDValue RetVals[] = { Undef, Ret };
1619 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1620 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001621 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1622 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1623 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001624 SDValue RetVals[] = { Undef, Ret };
1625 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1626 }
1627}
1628
Dan Gohman11596ed2009-10-09 20:35:19 +00001629/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1630/// any uses which require the SF or OF bits to be accurate.
1631static bool HasNoSignedComparisonUses(SDNode *N) {
1632 // Examine each user of the node.
1633 for (SDNode::use_iterator UI = N->use_begin(),
1634 UE = N->use_end(); UI != UE; ++UI) {
1635 // Only examine CopyToReg uses.
1636 if (UI->getOpcode() != ISD::CopyToReg)
1637 return false;
1638 // Only examine CopyToReg uses that copy to EFLAGS.
1639 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1640 X86::EFLAGS)
1641 return false;
1642 // Examine each user of the CopyToReg use.
1643 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1644 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1645 // Only examine the Flag result.
1646 if (FlagUI.getUse().getResNo() != 1) continue;
1647 // Anything unusual: assume conservatively.
1648 if (!FlagUI->isMachineOpcode()) return false;
1649 // Examine the opcode of the user.
1650 switch (FlagUI->getMachineOpcode()) {
1651 // These comparisons don't treat the most significant bit specially.
1652 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1653 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1654 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1655 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001656 case X86::JA_4: case X86::JAE_4: case X86::JB_4: case X86::JBE_4:
1657 case X86::JE_4: case X86::JNE_4: case X86::JP_4: case X86::JNP_4:
Dan Gohman11596ed2009-10-09 20:35:19 +00001658 case X86::CMOVA16rr: case X86::CMOVA16rm:
1659 case X86::CMOVA32rr: case X86::CMOVA32rm:
1660 case X86::CMOVA64rr: case X86::CMOVA64rm:
1661 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1662 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1663 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1664 case X86::CMOVB16rr: case X86::CMOVB16rm:
1665 case X86::CMOVB32rr: case X86::CMOVB32rm:
1666 case X86::CMOVB64rr: case X86::CMOVB64rm:
1667 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1668 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1669 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1670 case X86::CMOVE16rr: case X86::CMOVE16rm:
1671 case X86::CMOVE32rr: case X86::CMOVE32rm:
1672 case X86::CMOVE64rr: case X86::CMOVE64rm:
1673 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1674 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1675 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1676 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1677 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1678 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1679 case X86::CMOVP16rr: case X86::CMOVP16rm:
1680 case X86::CMOVP32rr: case X86::CMOVP32rm:
1681 case X86::CMOVP64rr: case X86::CMOVP64rm:
1682 continue;
1683 // Anything else: assume conservatively.
1684 default: return false;
1685 }
1686 }
1687 }
1688 return true;
1689}
1690
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001691SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Owen Andersone50ed302009-08-10 22:56:29 +00001692 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001693 unsigned Opc, MOpc;
1694 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001695 DebugLoc dl = Node->getDebugLoc();
1696
Evan Chengf597dc72006-02-10 22:24:32 +00001697#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001698 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +00001699 dbgs() << std::string(Indent, ' ') << "Selecting: ";
Bill Wendling12321672009-08-07 21:33:25 +00001700 Node->dump(CurDAG);
David Greened7f4f242010-01-05 01:29:08 +00001701 dbgs() << '\n';
Bill Wendling12321672009-08-07 21:33:25 +00001702 });
Evan Cheng23addc02006-02-10 22:46:26 +00001703 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001704#endif
1705
Dan Gohmane8be6c62008-07-17 19:10:17 +00001706 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001707#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001708 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +00001709 dbgs() << std::string(Indent-2, ' ') << "== ";
Bill Wendling12321672009-08-07 21:33:25 +00001710 Node->dump(CurDAG);
David Greened7f4f242010-01-05 01:29:08 +00001711 dbgs() << '\n';
Bill Wendling12321672009-08-07 21:33:25 +00001712 });
Evan Cheng23addc02006-02-10 22:46:26 +00001713 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001714#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001715 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001716 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001717
Evan Cheng0114e942006-01-06 20:36:21 +00001718 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001719 default: break;
1720 case X86ISD::GlobalBaseReg:
1721 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001722
Dan Gohman72677342009-08-02 16:10:52 +00001723 case X86ISD::ATOMOR64_DAG:
1724 return SelectAtomic64(Node, X86::ATOMOR6432);
1725 case X86ISD::ATOMXOR64_DAG:
1726 return SelectAtomic64(Node, X86::ATOMXOR6432);
1727 case X86ISD::ATOMADD64_DAG:
1728 return SelectAtomic64(Node, X86::ATOMADD6432);
1729 case X86ISD::ATOMSUB64_DAG:
1730 return SelectAtomic64(Node, X86::ATOMSUB6432);
1731 case X86ISD::ATOMNAND64_DAG:
1732 return SelectAtomic64(Node, X86::ATOMNAND6432);
1733 case X86ISD::ATOMAND64_DAG:
1734 return SelectAtomic64(Node, X86::ATOMAND6432);
1735 case X86ISD::ATOMSWAP64_DAG:
1736 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001737
Dan Gohman72677342009-08-02 16:10:52 +00001738 case ISD::ATOMIC_LOAD_ADD: {
1739 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1740 if (RetVal)
1741 return RetVal;
1742 break;
1743 }
1744
1745 case ISD::SMUL_LOHI:
1746 case ISD::UMUL_LOHI: {
1747 SDValue N0 = Node->getOperand(0);
1748 SDValue N1 = Node->getOperand(1);
1749
1750 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001751 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001752 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001753 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001754 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1755 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1756 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1757 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001758 }
Bill Wendling12321672009-08-07 21:33:25 +00001759 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001760 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001761 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001762 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1763 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1764 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1765 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001766 }
Bill Wendling12321672009-08-07 21:33:25 +00001767 }
Dan Gohman72677342009-08-02 16:10:52 +00001768
1769 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001770 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001771 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001772 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1773 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1774 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1775 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001776 }
1777
1778 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001779 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001780 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001781 if (!foldedLoad) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001782 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001783 if (foldedLoad)
1784 std::swap(N0, N1);
1785 }
1786
1787 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1788 N0, SDValue()).getValue(1);
1789
1790 if (foldedLoad) {
1791 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1792 InFlag };
1793 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001794 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1795 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001796 InFlag = SDValue(CNode, 1);
1797 // Update the chain.
1798 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1799 } else {
1800 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001801 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001802 }
1803
1804 // Copy the low half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001805 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001806 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1807 LoReg, NVT, InFlag);
1808 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001809 ReplaceUses(SDValue(Node, 0), Result);
Dan Gohman72677342009-08-02 16:10:52 +00001810#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001811 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +00001812 dbgs() << std::string(Indent-2, ' ') << "=> ";
Bill Wendling12321672009-08-07 21:33:25 +00001813 Result.getNode()->dump(CurDAG);
David Greened7f4f242010-01-05 01:29:08 +00001814 dbgs() << '\n';
Bill Wendling12321672009-08-07 21:33:25 +00001815 });
Dan Gohman72677342009-08-02 16:10:52 +00001816#endif
1817 }
1818 // Copy the high half of the result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001819 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001820 SDValue Result;
1821 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1822 // Prevent use of AH in a REX instruction by referencing AX instead.
1823 // Shift it down 8 bits.
1824 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001825 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001826 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001827 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1828 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001829 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001830 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001831 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1832 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001833 } else {
1834 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1835 HiReg, NVT, InFlag);
1836 InFlag = Result.getValue(2);
1837 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001838 ReplaceUses(SDValue(Node, 1), Result);
Dan Gohman72677342009-08-02 16:10:52 +00001839#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001840 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +00001841 dbgs() << std::string(Indent-2, ' ') << "=> ";
Bill Wendling12321672009-08-07 21:33:25 +00001842 Result.getNode()->dump(CurDAG);
David Greened7f4f242010-01-05 01:29:08 +00001843 dbgs() << '\n';
Bill Wendling12321672009-08-07 21:33:25 +00001844 });
Dan Gohman72677342009-08-02 16:10:52 +00001845#endif
1846 }
1847
1848#ifndef NDEBUG
1849 Indent -= 2;
1850#endif
1851
1852 return NULL;
1853 }
1854
1855 case ISD::SDIVREM:
1856 case ISD::UDIVREM: {
1857 SDValue N0 = Node->getOperand(0);
1858 SDValue N1 = Node->getOperand(1);
1859
1860 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001861 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001862 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001863 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1865 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1866 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1867 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001868 }
Bill Wendling12321672009-08-07 21:33:25 +00001869 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001871 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001872 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1873 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1874 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1875 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001876 }
Bill Wendling12321672009-08-07 21:33:25 +00001877 }
Dan Gohman72677342009-08-02 16:10:52 +00001878
Chris Lattner9e323832009-12-23 01:45:04 +00001879 unsigned LoReg, HiReg, ClrReg;
Dan Gohman72677342009-08-02 16:10:52 +00001880 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001881 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001882 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 case MVT::i8:
Chris Lattner9e323832009-12-23 01:45:04 +00001884 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman72677342009-08-02 16:10:52 +00001885 ClrOpcode = 0;
1886 SExtOpcode = X86::CBW;
1887 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001889 LoReg = X86::AX; HiReg = X86::DX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001890 ClrOpcode = X86::MOV16r0; ClrReg = X86::DX;
Dan Gohman72677342009-08-02 16:10:52 +00001891 SExtOpcode = X86::CWD;
1892 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001893 case MVT::i32:
Chris Lattner9e323832009-12-23 01:45:04 +00001894 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman72677342009-08-02 16:10:52 +00001895 ClrOpcode = X86::MOV32r0;
1896 SExtOpcode = X86::CDQ;
1897 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 case MVT::i64:
Chris Lattner9e323832009-12-23 01:45:04 +00001899 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001900 ClrOpcode = X86::MOV64r0;
Dan Gohman72677342009-08-02 16:10:52 +00001901 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001902 break;
1903 }
1904
Dan Gohman72677342009-08-02 16:10:52 +00001905 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001906 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman72677342009-08-02 16:10:52 +00001907 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001908
Dan Gohman72677342009-08-02 16:10:52 +00001909 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001911 // Special case for div8, just use a move with zero extension to AX to
1912 // clear the upper 8 bits (AH).
1913 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001914 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman72677342009-08-02 16:10:52 +00001915 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1916 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001917 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1918 MVT::Other, Ops,
1919 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001920 Chain = Move.getValue(1);
1921 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001922 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001923 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001924 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001925 Chain = CurDAG->getEntryNode();
1926 }
1927 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1928 InFlag = Chain.getValue(1);
1929 } else {
1930 InFlag =
1931 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1932 LoReg, N0, SDValue()).getValue(1);
1933 if (isSigned && !signBitIsZero) {
1934 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001935 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001936 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001937 } else {
1938 // Zero out the high part, effectively zero extending the input.
Dan Gohmanf1b4d262010-01-12 04:42:54 +00001939 SDValue ClrNode =
1940 SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Chris Lattner9e323832009-12-23 01:45:04 +00001941 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman72677342009-08-02 16:10:52 +00001942 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001943 }
Evan Cheng948f3432006-01-06 23:19:29 +00001944 }
Dan Gohman525178c2007-10-08 18:33:35 +00001945
Dan Gohman72677342009-08-02 16:10:52 +00001946 if (foldedLoad) {
1947 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1948 InFlag };
1949 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001950 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1951 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001952 InFlag = SDValue(CNode, 1);
1953 // Update the chain.
1954 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1955 } else {
1956 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001957 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001958 }
Evan Cheng948f3432006-01-06 23:19:29 +00001959
Dan Gohman72677342009-08-02 16:10:52 +00001960 // Copy the division (low) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001961 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001962 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1963 LoReg, NVT, InFlag);
1964 InFlag = Result.getValue(2);
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001965 ReplaceUses(SDValue(Node, 0), Result);
Dan Gohman72677342009-08-02 16:10:52 +00001966#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001967 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +00001968 dbgs() << std::string(Indent-2, ' ') << "=> ";
Bill Wendling12321672009-08-07 21:33:25 +00001969 Result.getNode()->dump(CurDAG);
David Greened7f4f242010-01-05 01:29:08 +00001970 dbgs() << '\n';
Bill Wendling12321672009-08-07 21:33:25 +00001971 });
Dan Gohman72677342009-08-02 16:10:52 +00001972#endif
1973 }
1974 // Copy the remainder (high) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001975 if (!SDValue(Node, 1).use_empty()) {
Dan Gohman72677342009-08-02 16:10:52 +00001976 SDValue Result;
1977 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1978 // Prevent use of AH in a REX instruction by referencing AX instead.
1979 // Shift it down 8 bits.
1980 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001981 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001982 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001983 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00001984 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001985 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00001986 0);
1987 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001988 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1989 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001990 } else {
1991 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1992 HiReg, NVT, InFlag);
1993 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001994 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001995 ReplaceUses(SDValue(Node, 1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001996#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001997 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +00001998 dbgs() << std::string(Indent-2, ' ') << "=> ";
Bill Wendling12321672009-08-07 21:33:25 +00001999 Result.getNode()->dump(CurDAG);
David Greened7f4f242010-01-05 01:29:08 +00002000 dbgs() << '\n';
Bill Wendling12321672009-08-07 21:33:25 +00002001 });
Dan Gohmana37c9f72007-09-25 18:23:27 +00002002#endif
Dan Gohman72677342009-08-02 16:10:52 +00002003 }
Evan Chengf597dc72006-02-10 22:24:32 +00002004
2005#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00002006 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002007#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002008
Dan Gohman72677342009-08-02 16:10:52 +00002009 return NULL;
2010 }
2011
Dan Gohman6a402dc2009-08-19 18:16:17 +00002012 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002013 SDValue N0 = Node->getOperand(0);
2014 SDValue N1 = Node->getOperand(1);
2015
2016 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2017 // use a smaller encoding.
2018 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
2019 N0.getValueType() != MVT::i8 &&
2020 X86::isZeroNode(N1)) {
2021 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2022 if (!C) break;
2023
2024 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00002025 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2026 (!(C->getZExtValue() & 0x80) ||
2027 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002028 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2029 SDValue Reg = N0.getNode()->getOperand(0);
2030
2031 // On x86-32, only the ABCD registers have 8-bit subregisters.
2032 if (!Subtarget->is64Bit()) {
2033 TargetRegisterClass *TRC = 0;
2034 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2035 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2036 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2037 default: llvm_unreachable("Unsupported TEST operand type!");
2038 }
2039 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002040 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2041 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002042 }
2043
2044 // Extract the l-register.
2045 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2046 MVT::i8, Reg);
2047
2048 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00002049 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002050 }
2051
2052 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00002053 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2054 (!(C->getZExtValue() & 0x8000) ||
2055 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002056 // Shift the immediate right by 8 bits.
2057 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2058 MVT::i8);
2059 SDValue Reg = N0.getNode()->getOperand(0);
2060
2061 // Put the value in an ABCD register.
2062 TargetRegisterClass *TRC = 0;
2063 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2064 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2065 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2066 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2067 default: llvm_unreachable("Unsupported TEST operand type!");
2068 }
2069 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002070 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2071 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002072
2073 // Extract the h-register.
2074 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2075 MVT::i8, Reg);
2076
2077 // Emit a testb. No special NOREX tricks are needed since there's
2078 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00002079 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2080 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002081 }
2082
2083 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2084 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002085 N0.getValueType() != MVT::i16 &&
2086 (!(C->getZExtValue() & 0x8000) ||
2087 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002088 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2089 SDValue Reg = N0.getNode()->getOperand(0);
2090
2091 // Extract the 16-bit subregister.
2092 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2093 MVT::i16, Reg);
2094
2095 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00002096 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002097 }
2098
2099 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2100 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002101 N0.getValueType() == MVT::i64 &&
2102 (!(C->getZExtValue() & 0x80000000) ||
2103 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002104 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2105 SDValue Reg = N0.getNode()->getOperand(0);
2106
2107 // Extract the 32-bit subregister.
2108 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2109 MVT::i32, Reg);
2110
2111 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00002112 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002113 }
2114 }
2115 break;
2116 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002117 }
2118
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002119 SDNode *ResNode = SelectCode(Node);
Evan Cheng64a752f2006-08-11 09:08:15 +00002120
Evan Chengf597dc72006-02-10 22:24:32 +00002121#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002122 DEBUG({
David Greened7f4f242010-01-05 01:29:08 +00002123 dbgs() << std::string(Indent-2, ' ') << "=> ";
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002124 if (ResNode == NULL || ResNode == Node)
2125 Node->dump(CurDAG);
Bill Wendling12321672009-08-07 21:33:25 +00002126 else
2127 ResNode->dump(CurDAG);
David Greened7f4f242010-01-05 01:29:08 +00002128 dbgs() << '\n';
Bill Wendling12321672009-08-07 21:33:25 +00002129 });
Evan Cheng23addc02006-02-10 22:46:26 +00002130 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002131#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002132
2133 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002134}
2135
Chris Lattnerc0bad572006-06-08 18:03:49 +00002136bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002137SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002138 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002139 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002140 switch (ConstraintCode) {
2141 case 'o': // offsetable ??
2142 case 'v': // not offsetable ??
2143 default: return true;
2144 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002145 if (!SelectAddr(Op.getNode(), Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002146 return true;
2147 break;
2148 }
2149
Evan Cheng04699902006-08-26 01:05:16 +00002150 OutOps.push_back(Op0);
2151 OutOps.push_back(Op1);
2152 OutOps.push_back(Op2);
2153 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002154 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002155 return false;
2156}
2157
Chris Lattnerc961eea2005-11-16 01:54:32 +00002158/// createX86ISelDag - This pass converts a legalized DAG into a
2159/// X86-specific DAG, ready for instruction scheduling.
2160///
Bill Wendling98a366d2009-04-29 23:29:43 +00002161FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2162 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002163 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002164}