blob: 2608365e58ba8fea65856b8bf35cb6d30302a44c [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
Daniel Dunbar5da58852009-11-10 18:24:37 +000015// Force NDEBUG on in any optimized build on Darwin.
16//
17// FIXME: This is a huge hack, to work around ridiculously awful compile times
18// on this file with gcc-4.2 on Darwin, in Release mode.
19#if defined(__APPLE__) && defined(__OPTIMIZE__) && !defined(NDEBUG)
20#define NDEBUG
21#endif
22
Evan Cheng2ef88a02006-08-07 22:28:20 +000023#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000024#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000025#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000026#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000027#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000028#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000030#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000031#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000032#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000033#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000034#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000035#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000036#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000037#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000038#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000039#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000040#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/CodeGen/SelectionDAGISel.h"
42#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000043#include "llvm/Target/TargetOptions.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000044#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000045#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000046#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000047#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000048#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000049#include "llvm/ADT/Statistic.h"
50using namespace llvm;
51
Evan Cheng4d952322009-03-31 01:13:53 +000052#include "llvm/Support/CommandLine.h"
53static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
54
Chris Lattner95b2c7d2006-12-19 22:59:26 +000055STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
56
Chris Lattnerc961eea2005-11-16 01:54:32 +000057//===----------------------------------------------------------------------===//
58// Pattern Matcher Implementation
59//===----------------------------------------------------------------------===//
60
61namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000062 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000063 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000064 /// tree.
65 struct X86ISelAddressMode {
66 enum {
67 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000068 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 } BaseType;
70
71 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000072 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000073 int FrameIndex;
74 } Base;
75
76 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000077 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000078 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000079 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000080 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000081 Constant *CP;
Chris Lattner43f44aa2009-11-01 03:25:03 +000082 BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000083 const char *ES;
84 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000085 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000086 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000087
88 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000089 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000090 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000091 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000092 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000093
94 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000095 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000096 }
Chris Lattner18c59872009-06-27 04:16:01 +000097
98 bool hasBaseOrIndexReg() const {
99 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
100 }
101
102 /// isRIPRelative - Return true if this addressing mode is already RIP
103 /// relative.
104 bool isRIPRelative() const {
105 if (BaseType != RegBase) return false;
106 if (RegisterSDNode *RegNode =
107 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
108 return RegNode->getReg() == X86::RIP;
109 return false;
110 }
111
112 void setBaseReg(SDValue Reg) {
113 BaseType = RegBase;
114 Base.Reg = Reg;
115 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000116
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000117 void dump() {
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000118 errs() << "X86ISelAddressMode " << this << '\n';
119 errs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000120 if (Base.Reg.getNode() != 0)
121 Base.Reg.getNode()->dump();
122 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000123 errs() << "nul";
124 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
125 << " Scale" << Scale << '\n'
126 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000127 if (IndexReg.getNode() != 0)
128 IndexReg.getNode()->dump();
129 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000130 errs() << "nul";
131 errs() << " Disp " << Disp << '\n'
132 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000133 if (GV)
134 GV->dump();
135 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000136 errs() << "nul";
137 errs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000138 if (CP)
139 CP->dump();
140 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000141 errs() << "nul";
142 errs() << '\n'
143 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000144 if (ES)
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000145 errs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000146 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000147 errs() << "nul";
148 errs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000149 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000150 };
151}
152
153namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000154 //===--------------------------------------------------------------------===//
155 /// ISel - X86 specific code to select X86 machine instructions for
156 /// SelectionDAG operations.
157 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000158 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000159 /// X86Lowering - This object fully describes how to lower LLVM code to an
160 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000161 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000162
163 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
164 /// make the right decision when generating code for different targets.
165 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000166
Evan Chengb7a75a52008-09-26 23:41:32 +0000167 /// OptForSize - If true, selector should try to optimize for code size
168 /// instead of performance.
169 bool OptForSize;
170
Chris Lattnerc961eea2005-11-16 01:54:32 +0000171 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000172 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000173 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000174 X86Lowering(*tm.getTargetLowering()),
175 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000176 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000177
178 virtual const char *getPassName() const {
179 return "X86 DAG->DAG Instruction Selection";
180 }
181
Evan Chengdb8d56b2008-06-30 20:45:06 +0000182 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000183 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000184 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000185
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000186 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
187
Evan Cheng884c70c2008-11-27 00:49:46 +0000188 virtual
189 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000190
Chris Lattnerc961eea2005-11-16 01:54:32 +0000191// Include the pieces autogenerated from the target description.
192#include "X86GenDAGISel.inc"
193
194 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000195 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000196 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000197 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000198
Rafael Espindola094fad32009-04-08 21:14:34 +0000199 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
200 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000201 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000202 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
203 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
204 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000205 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000206 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000207 SDValue &Scale, SDValue &Index, SDValue &Disp,
208 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000209 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
210 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000211 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
212 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000213 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
214 SDValue N, SDValue &Base, SDValue &Scale,
215 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000216 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue &InChain, SDValue &OutChain);
218 bool TryFoldLoad(SDValue P, SDValue N,
219 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000220 SDValue &Index, SDValue &Disp,
221 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000222 void PreprocessForRMW();
223 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000224
Chris Lattnerc0bad572006-06-08 18:03:49 +0000225 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
226 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000227 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000228 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000229 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000230
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000231 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
232
Dan Gohman475871a2008-07-27 21:46:04 +0000233 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
234 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000235 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000236 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000237 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
238 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000239 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000240 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000241 // These are 32-bit even in 64-bit mode since RIP relative offset
242 // is 32-bit.
243 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000245 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000246 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000248 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000251 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000253 else if (AM.BlockAddr)
254 Disp = CurDAG->getBlockAddress(AM.BlockAddr, DebugLoc()/*MVT::i32*/,
255 true /*AM.SymbolFlags*/);
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000258
259 if (AM.Segment.getNode())
260 Segment = AM.Segment;
261 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000263 }
264
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000265 /// getI8Imm - Return a target constant with the specified value, of type
266 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000269 }
270
Chris Lattnerc961eea2005-11-16 01:54:32 +0000271 /// getI16Imm - Return a target constant with the specified value, of type
272 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000273 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000275 }
276
277 /// getI32Imm - Return a target constant with the specified value, of type
278 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000279 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000281 }
Evan Chengf597dc72006-02-10 22:24:32 +0000282
Dan Gohman8b746962008-09-23 18:22:58 +0000283 /// getGlobalBaseReg - Return an SDNode that returns the value of
284 /// the global base register. Output instructions required to
285 /// initialize the global base register, if necessary.
286 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000287 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000288
Dan Gohmanc5534622009-06-03 20:20:00 +0000289 /// getTargetMachine - Return a reference to the TargetMachine, casted
290 /// to the target-specific type.
291 const X86TargetMachine &getTargetMachine() {
292 return static_cast<const X86TargetMachine &>(TM);
293 }
294
295 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
296 /// to the target-specific type.
297 const X86InstrInfo *getInstrInfo() {
298 return getTargetMachine().getInstrInfo();
299 }
300
Evan Cheng23addc02006-02-10 22:46:26 +0000301#ifndef NDEBUG
302 unsigned Indent;
303#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000304 };
305}
306
Evan Chengf4b4c412006-08-08 00:31:00 +0000307
Evan Cheng884c70c2008-11-27 00:49:46 +0000308bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
309 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000310 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000311
Evan Cheng884c70c2008-11-27 00:49:46 +0000312 if (U == Root)
313 switch (U->getOpcode()) {
314 default: break;
315 case ISD::ADD:
316 case ISD::ADDC:
317 case ISD::ADDE:
318 case ISD::AND:
319 case ISD::OR:
320 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000321 SDValue Op1 = U->getOperand(1);
322
Evan Cheng884c70c2008-11-27 00:49:46 +0000323 // If the other operand is a 8-bit immediate we should fold the immediate
324 // instead. This reduces code size.
325 // e.g.
326 // movl 4(%esp), %eax
327 // addl $4, %eax
328 // vs.
329 // movl $4, %eax
330 // addl 4(%esp), %eax
331 // The former is 2 bytes shorter. In case where the increment is 1, then
332 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000333 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000334 if (Imm->getAPIntValue().isSignedIntN(8))
335 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000336
337 // If the other operand is a TLS address, we should fold it instead.
338 // This produces
339 // movl %gs:0, %eax
340 // leal i@NTPOFF(%eax), %eax
341 // instead of
342 // movl $i@NTPOFF, %eax
343 // addl %gs:0, %eax
344 // if the block also has an access to a second TLS address this will save
345 // a load.
346 // FIXME: This is probably also true for non TLS addresses.
347 if (Op1.getOpcode() == X86ISD::Wrapper) {
348 SDValue Val = Op1.getOperand(0);
349 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
350 return false;
351 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000352 }
353 }
354
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000355 // Proceed to 'generic' cycle finder code
356 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000357}
358
Evan Cheng70e674e2006-08-28 20:10:17 +0000359/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
360/// and move load below the TokenFactor. Replace store's chain operand with
361/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000362static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000363 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000364 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000365 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
366 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000367 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000368 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000369 Ops.push_back(TF.getOperand(i));
Dan Gohmanaae317a2009-08-06 09:22:57 +0000370 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
371 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
372 Load.getOperand(1),
373 Load.getOperand(2));
374 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000375 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000376}
377
Nate Begeman206a3572009-09-16 03:20:46 +0000378/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. The
379/// chain produced by the load must only be used by the store's chain operand,
380/// otherwise this may produce a cycle in the DAG.
Evan Chengcd0baf22008-05-23 21:23:16 +0000381///
Dan Gohman475871a2008-07-27 21:46:04 +0000382static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
383 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000384 if (N.getOpcode() == ISD::BIT_CONVERT)
385 N = N.getOperand(0);
386
387 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
388 if (!LD || LD->isVolatile())
389 return false;
390 if (LD->getAddressingMode() != ISD::UNINDEXED)
391 return false;
392
393 ISD::LoadExtType ExtType = LD->getExtensionType();
394 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
395 return false;
396
397 if (N.hasOneUse() &&
Nate Begeman206a3572009-09-16 03:20:46 +0000398 LD->hasNUsesOfValue(1, 1) &&
Evan Chengcd0baf22008-05-23 21:23:16 +0000399 N.getOperand(1) == Address &&
Nate Begeman206a3572009-09-16 03:20:46 +0000400 LD->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000401 Load = N;
402 return true;
403 }
404 return false;
405}
406
Evan Chengab6c3bb2008-08-25 21:27:18 +0000407/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
408/// operand and move load below the call's chain operand.
409static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000410 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000411 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000412 SDValue Chain = CallSeqStart.getOperand(0);
413 if (Chain.getNode() == Load.getNode())
414 Ops.push_back(Load.getOperand(0));
415 else {
416 assert(Chain.getOpcode() == ISD::TokenFactor &&
417 "Unexpected CallSeqStart chain operand");
418 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
419 if (Chain.getOperand(i).getNode() == Load.getNode())
420 Ops.push_back(Load.getOperand(0));
421 else
422 Ops.push_back(Chain.getOperand(i));
423 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000424 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000426 Ops.clear();
427 Ops.push_back(NewChain);
428 }
429 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
430 Ops.push_back(CallSeqStart.getOperand(i));
431 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000432 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
433 Load.getOperand(1), Load.getOperand(2));
434 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000435 Ops.push_back(SDValue(Load.getNode(), 1));
436 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000437 Ops.push_back(Call.getOperand(i));
438 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
439}
440
441/// isCalleeLoad - Return true if call address is a load and it can be
442/// moved below CALLSEQ_START and the chains leading up to the call.
443/// Return the CALLSEQ_START by reference as a second output.
444static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000445 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000446 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000447 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000448 if (!LD ||
449 LD->isVolatile() ||
450 LD->getAddressingMode() != ISD::UNINDEXED ||
451 LD->getExtensionType() != ISD::NON_EXTLOAD)
452 return false;
453
454 // Now let's find the callseq_start.
455 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
456 if (!Chain.hasOneUse())
457 return false;
458 Chain = Chain.getOperand(0);
459 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000460
461 if (Chain.getOperand(0).getNode() == Callee.getNode())
462 return true;
463 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000464 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
465 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000466 return true;
467 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000468}
469
470
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000471/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000472/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000473/// This allows the instruction selector to pick more read-modify-write
474/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000475///
476/// [Load chain]
477/// ^
478/// |
479/// [Load]
480/// ^ ^
481/// | |
482/// / \-
483/// / |
484/// [TokenFactor] [Op]
485/// ^ ^
486/// | |
487/// \ /
488/// \ /
489/// [Store]
490///
491/// The fact the store's chain operand != load's chain will prevent the
492/// (store (op (load))) instruction from being selected. We can transform it to:
493///
494/// [Load chain]
495/// ^
496/// |
497/// [TokenFactor]
498/// ^
499/// |
500/// [Load]
501/// ^ ^
502/// | |
503/// | \-
504/// | |
505/// | [Op]
506/// | ^
507/// | |
508/// \ /
509/// \ /
510/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000511void X86DAGToDAGISel::PreprocessForRMW() {
512 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
513 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000514 if (I->getOpcode() == X86ISD::CALL) {
515 /// Also try moving call address load from outside callseq_start to just
516 /// before the call to allow it to be folded.
517 ///
518 /// [Load chain]
519 /// ^
520 /// |
521 /// [Load]
522 /// ^ ^
523 /// | |
524 /// / \--
525 /// / |
526 ///[CALLSEQ_START] |
527 /// ^ |
528 /// | |
529 /// [LOAD/C2Reg] |
530 /// | |
531 /// \ /
532 /// \ /
533 /// [CALL]
534 SDValue Chain = I->getOperand(0);
535 SDValue Load = I->getOperand(1);
536 if (!isCalleeLoad(Load, Chain))
537 continue;
538 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
539 ++NumLoadMoved;
540 continue;
541 }
542
Evan Cheng8b2794a2006-10-13 21:14:26 +0000543 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000544 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000545 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000546
Gabor Greifba36cb52008-08-28 21:40:38 +0000547 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000548 continue;
549
Dan Gohman475871a2008-07-27 21:46:04 +0000550 SDValue N1 = I->getOperand(1);
551 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000552 if ((N1.getValueType().isFloatingPoint() &&
553 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000554 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000555 continue;
556
557 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000558 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000559 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000560 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000561 case ISD::ADD:
562 case ISD::MUL:
563 case ISD::AND:
564 case ISD::OR:
565 case ISD::XOR:
566 case ISD::ADDC:
567 case ISD::ADDE:
568 case ISD::VECTOR_SHUFFLE: {
569 SDValue N10 = N1.getOperand(0);
570 SDValue N11 = N1.getOperand(1);
571 RModW = isRMWLoad(N10, Chain, N2, Load);
572 if (!RModW)
573 RModW = isRMWLoad(N11, Chain, N2, Load);
574 break;
575 }
576 case ISD::SUB:
577 case ISD::SHL:
578 case ISD::SRA:
579 case ISD::SRL:
580 case ISD::ROTL:
581 case ISD::ROTR:
582 case ISD::SUBC:
583 case ISD::SUBE:
584 case X86ISD::SHLD:
585 case X86ISD::SHRD: {
586 SDValue N10 = N1.getOperand(0);
587 RModW = isRMWLoad(N10, Chain, N2, Load);
588 break;
589 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000590 }
591
Evan Cheng82a35b32006-08-29 06:44:17 +0000592 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000593 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000594 ++NumLoadMoved;
595 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000596 }
597}
598
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000599
600/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
601/// nodes that target the FP stack to be store and load to the stack. This is a
602/// gross hack. We would like to simply mark these as being illegal, but when
603/// we do that, legalize produces these when it expands calls, then expands
604/// these in the same legalize pass. We would like dag combine to be able to
605/// hack on these between the call expansion and the node legalization. As such
606/// this pass basically does "really late" legalization of these inline with the
607/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000608void X86DAGToDAGISel::PreprocessForFPConvert() {
609 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
610 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000611 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
612 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
613 continue;
614
615 // If the source and destination are SSE registers, then this is a legal
616 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000617 EVT SrcVT = N->getOperand(0).getValueType();
618 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000619 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
620 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
621 if (SrcIsSSE && DstIsSSE)
622 continue;
623
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000624 if (!SrcIsSSE && !DstIsSSE) {
625 // If this is an FPStack extension, it is a noop.
626 if (N->getOpcode() == ISD::FP_EXTEND)
627 continue;
628 // If this is a value-preserving FPStack truncation, it is a noop.
629 if (N->getConstantOperandVal(1))
630 continue;
631 }
632
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000633 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
634 // FPStack has extload and truncstore. SSE can fold direct loads into other
635 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000636 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000637 if (N->getOpcode() == ISD::FP_ROUND)
638 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
639 else
640 MemVT = SrcIsSSE ? SrcVT : DstVT;
641
Dan Gohmanf350b272008-08-23 02:25:05 +0000642 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000643 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000644
645 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000646 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000647 N->getOperand(0),
648 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000649 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000650 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000651
652 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
653 // extload we created. This will cause general havok on the dag because
654 // anything below the conversion could be folded into other existing nodes.
655 // To avoid invalidating 'I', back it up to the convert node.
656 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000657 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000658
659 // Now that we did that, the node is dead. Increment the iterator to the
660 // next node to process, then delete N.
661 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000662 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000663 }
664}
665
Chris Lattnerc961eea2005-11-16 01:54:32 +0000666/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
667/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000668void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000669 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000670 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000671
Bill Wendling98a366d2009-04-29 23:29:43 +0000672 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000673 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000674
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000675 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000676 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000677
Chris Lattnerc961eea2005-11-16 01:54:32 +0000678 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000679#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000680 DEBUG(errs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000681 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000682#endif
David Greene8ad4c002008-10-27 21:56:29 +0000683 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000684#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000685 DEBUG(errs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000686#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000687
Dan Gohmanf350b272008-08-23 02:25:05 +0000688 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000689}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000690
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000691/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
692/// the main function.
693void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
694 MachineFrameInfo *MFI) {
695 const TargetInstrInfo *TII = TM.getInstrInfo();
696 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000697 BuildMI(BB, DebugLoc::getUnknownLoc(),
698 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000699}
700
701void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
702 // If this is main, emit special code for main.
703 MachineBasicBlock *BB = MF.begin();
704 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
705 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
706}
707
Rafael Espindola094fad32009-04-08 21:14:34 +0000708
709bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
710 X86ISelAddressMode &AM) {
711 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
712 SDValue Segment = N.getOperand(0);
713
714 if (AM.Segment.getNode() == 0) {
715 AM.Segment = Segment;
716 return false;
717 }
718
719 return true;
720}
721
722bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
723 // This optimization is valid because the GNU TLS model defines that
724 // gs:0 (or fs:0 on X86-64) contains its own address.
725 // For more information see http://people.redhat.com/drepper/tls.pdf
726
727 SDValue Address = N.getOperand(1);
728 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
729 !MatchSegmentBaseAddress (Address, AM))
730 return false;
731
732 return true;
733}
734
Chris Lattner18c59872009-06-27 04:16:01 +0000735/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
736/// into an addressing mode. These wrap things that will resolve down into a
737/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000738/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000739bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000740 // If the addressing mode already has a symbol as the displacement, we can
741 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000742 if (AM.hasSymbolicDisplacement())
743 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000744
745 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000746 CodeModel::Model M = TM.getCodeModel();
747
Chris Lattner18c59872009-06-27 04:16:01 +0000748 // Handle X86-64 rip-relative addresses. We check this before checking direct
749 // folding because RIP is preferable to non-RIP accesses.
750 if (Subtarget->is64Bit() &&
751 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
752 // they cannot be folded into immediate fields.
753 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000754 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000755 // Base and index reg must be 0 in order to use %rip as base and lowering
756 // must allow RIP.
757 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000758 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
759 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000760 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000761 AM.GV = G->getGlobal();
762 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000763 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000764 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
765 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000766 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000767 AM.CP = CP->getConstVal();
768 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000769 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000770 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000771 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
772 AM.ES = S->getSymbol();
773 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000774 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000775 AM.JT = J->getIndex();
776 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000777 } else {
778 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
779 //AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000780 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000781
Chris Lattner18c59872009-06-27 04:16:01 +0000782 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000784 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000785 }
786
787 // Handle the case when globals fit in our immediate field: This is true for
788 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
789 // mode, this results in a non-RIP-relative computation.
790 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000791 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000792 TM.getRelocationModel() == Reloc::Static)) {
793 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
794 AM.GV = G->getGlobal();
795 AM.Disp += G->getOffset();
796 AM.SymbolFlags = G->getTargetFlags();
797 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
798 AM.CP = CP->getConstVal();
799 AM.Align = CP->getAlignment();
800 AM.Disp += CP->getOffset();
801 AM.SymbolFlags = CP->getTargetFlags();
802 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
803 AM.ES = S->getSymbol();
804 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000805 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000806 AM.JT = J->getIndex();
807 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000808 } else {
809 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
810 //AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000811 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000812 return false;
813 }
814
815 return true;
816}
817
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000818/// MatchAddress - Add the specified node to the specified addressing mode,
819/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000820/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000821bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
822 if (MatchAddressRecursively(N, AM, 0))
823 return true;
824
825 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
826 // a smaller encoding and avoids a scaled-index.
827 if (AM.Scale == 2 &&
828 AM.BaseType == X86ISelAddressMode::RegBase &&
829 AM.Base.Reg.getNode() == 0) {
830 AM.Base.Reg = AM.IndexReg;
831 AM.Scale = 1;
832 }
833
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000834 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
835 // because it has a smaller encoding.
836 // TODO: Which other code models can use this?
837 if (TM.getCodeModel() == CodeModel::Small &&
838 Subtarget->is64Bit() &&
839 AM.Scale == 1 &&
840 AM.BaseType == X86ISelAddressMode::RegBase &&
841 AM.Base.Reg.getNode() == 0 &&
842 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000843 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000844 AM.hasSymbolicDisplacement())
845 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
846
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000847 return false;
848}
849
850bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
851 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000852 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000853 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000854 DEBUG({
855 errs() << "MatchAddress: ";
856 AM.dump();
857 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000858 // Limit recursion.
859 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000860 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000861
862 CodeModel::Model M = TM.getCodeModel();
863
Chris Lattner18c59872009-06-27 04:16:01 +0000864 // If this is already a %rip relative address, we can only merge immediates
865 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000866 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000867 if (AM.isRIPRelative()) {
868 // FIXME: JumpTable and ExternalSymbol address currently don't like
869 // displacements. It isn't very important, but this should be fixed for
870 // consistency.
871 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000872
Chris Lattner18c59872009-06-27 04:16:01 +0000873 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
874 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000875 if (X86::isOffsetSuitableForCodeModel(Val, M,
876 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000877 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000878 return false;
879 }
880 }
881 return true;
882 }
883
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000884 switch (N.getOpcode()) {
885 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000886 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000887 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000888 if (!is64Bit ||
889 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
890 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000891 AM.Disp += Val;
892 return false;
893 }
894 break;
895 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000896
Rafael Espindola094fad32009-04-08 21:14:34 +0000897 case X86ISD::SegmentBaseAddress:
898 if (!MatchSegmentBaseAddress(N, AM))
899 return false;
900 break;
901
Rafael Espindola49a168d2009-04-12 21:55:03 +0000902 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000903 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000904 if (!MatchWrapper(N, AM))
905 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000906 break;
907
Rafael Espindola094fad32009-04-08 21:14:34 +0000908 case ISD::LOAD:
909 if (!MatchLoad(N, AM))
910 return false;
911 break;
912
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000913 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000914 if (AM.BaseType == X86ISelAddressMode::RegBase
915 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000916 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
917 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
918 return false;
919 }
920 break;
Evan Chengec693f72005-12-08 02:01:35 +0000921
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000922 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000923 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000924 break;
925
Gabor Greif93c53e52008-08-31 15:37:04 +0000926 if (ConstantSDNode
927 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000928 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000929 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
930 // that the base operand remains free for further matching. If
931 // the base doesn't end up getting used, a post-processing step
932 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000933 if (Val == 1 || Val == 2 || Val == 3) {
934 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000935 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000936
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000937 // Okay, we know that we have a scale by now. However, if the scaled
938 // value is an add of something and a constant, we can fold the
939 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000940 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
941 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
942 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000943 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000944 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000945 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000946 if (!is64Bit ||
947 X86::isOffsetSuitableForCodeModel(Disp, M,
948 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000949 AM.Disp = Disp;
950 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000951 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000952 } else {
953 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000954 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000955 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000956 }
957 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000958 }
Evan Chengec693f72005-12-08 02:01:35 +0000959
Dan Gohman83688052007-10-22 20:22:24 +0000960 case ISD::SMUL_LOHI:
961 case ISD::UMUL_LOHI:
962 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000963 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000964 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000965 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000966 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000967 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000968 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000969 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000970 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000971 if (ConstantSDNode
972 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000973 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
974 CN->getZExtValue() == 9) {
975 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000976
Gabor Greifba36cb52008-08-28 21:40:38 +0000977 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000978 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000979
980 // Okay, we know that we have a scale by now. However, if the scaled
981 // value is an add of something and a constant, we can fold the
982 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000983 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
984 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
985 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000986 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000987 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000988 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000989 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000990 if (!is64Bit ||
991 X86::isOffsetSuitableForCodeModel(Disp, M,
992 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000993 AM.Disp = Disp;
994 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000995 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000996 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000997 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000998 }
999
1000 AM.IndexReg = AM.Base.Reg = Reg;
1001 return false;
1002 }
Chris Lattner62412262007-02-04 20:18:17 +00001003 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001004 break;
1005
Dan Gohman3cd90a12009-05-11 18:02:53 +00001006 case ISD::SUB: {
1007 // Given A-B, if A can be completely folded into the address and
1008 // the index field with the index field unused, use -B as the index.
1009 // This is a win if a has multiple parts that can be folded into
1010 // the address. Also, this saves a mov if the base register has
1011 // other uses, since it avoids a two-address sub instruction, however
1012 // it costs an additional mov if the index register has other uses.
1013
1014 // Test if the LHS of the sub can be folded.
1015 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001016 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001017 AM = Backup;
1018 break;
1019 }
1020 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001021 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001022 AM = Backup;
1023 break;
1024 }
1025 int Cost = 0;
1026 SDValue RHS = N.getNode()->getOperand(1);
1027 // If the RHS involves a register with multiple uses, this
1028 // transformation incurs an extra mov, due to the neg instruction
1029 // clobbering its operand.
1030 if (!RHS.getNode()->hasOneUse() ||
1031 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1032 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1033 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1034 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001035 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001036 ++Cost;
1037 // If the base is a register with multiple uses, this
1038 // transformation may save a mov.
1039 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1040 AM.Base.Reg.getNode() &&
1041 !AM.Base.Reg.getNode()->hasOneUse()) ||
1042 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1043 --Cost;
1044 // If the folded LHS was interesting, this transformation saves
1045 // address arithmetic.
1046 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1047 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1048 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1049 --Cost;
1050 // If it doesn't look like it may be an overall win, don't do it.
1051 if (Cost >= 0) {
1052 AM = Backup;
1053 break;
1054 }
1055
1056 // Ok, the transformation is legal and appears profitable. Go for it.
1057 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1058 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1059 AM.IndexReg = Neg;
1060 AM.Scale = 1;
1061
1062 // Insert the new nodes into the topological ordering.
1063 if (Zero.getNode()->getNodeId() == -1 ||
1064 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1065 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1066 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1067 }
1068 if (Neg.getNode()->getNodeId() == -1 ||
1069 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1070 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1071 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1072 }
1073 return false;
1074 }
1075
Evan Cheng8e278262009-01-17 07:09:27 +00001076 case ISD::ADD: {
1077 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001078 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1079 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001080 return false;
1081 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001082 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1083 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001084 return false;
1085 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001086
1087 // If we couldn't fold both operands into the address at the same time,
1088 // see if we can just put each operand into a register and fold at least
1089 // the add.
1090 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1091 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001092 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001093 AM.Base.Reg = N.getNode()->getOperand(0);
1094 AM.IndexReg = N.getNode()->getOperand(1);
1095 AM.Scale = 1;
1096 return false;
1097 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001098 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001099 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001100
Chris Lattner62412262007-02-04 20:18:17 +00001101 case ISD::OR:
1102 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001103 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1104 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001105 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001106 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001107 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001108 // Address could not have picked a GV address for the displacement.
1109 AM.GV == NULL &&
1110 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001111 (!is64Bit ||
1112 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1113 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001114 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001115 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001116 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001117 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001118 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001119 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001120 }
1121 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001122
1123 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001124 // Perform some heroic transforms on an and of a constant-count shift
1125 // with a constant to enable use of the scaled offset field.
1126
Dan Gohman475871a2008-07-27 21:46:04 +00001127 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001128 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001129
Evan Cheng1314b002007-12-13 00:43:27 +00001130 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001131 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001132
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001133 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001134 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1135 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1136 if (!C1 || !C2) break;
1137
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001138 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1139 // allows us to convert the shift and and into an h-register extract and
1140 // a scaled index.
1141 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1142 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001143 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001144 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001145 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001146 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1147 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1148 X, Eight);
1149 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1150 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001151 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001152 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1153 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001154
1155 // Insert the new nodes into the topological ordering.
1156 if (Eight.getNode()->getNodeId() == -1 ||
1157 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1158 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1159 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1160 }
1161 if (Mask.getNode()->getNodeId() == -1 ||
1162 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1163 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1164 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1165 }
1166 if (Srl.getNode()->getNodeId() == -1 ||
1167 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1168 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1169 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1170 }
1171 if (And.getNode()->getNodeId() == -1 ||
1172 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1173 CurDAG->RepositionNode(N.getNode(), And.getNode());
1174 And.getNode()->setNodeId(N.getNode()->getNodeId());
1175 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001176 if (ShlCount.getNode()->getNodeId() == -1 ||
1177 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1178 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1179 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1180 }
1181 if (Shl.getNode()->getNodeId() == -1 ||
1182 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1183 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1184 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1185 }
1186 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001187 AM.IndexReg = And;
1188 AM.Scale = (1 << ScaleLog);
1189 return false;
1190 }
1191 }
1192
1193 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1194 // allows us to fold the shift into this addressing mode.
1195 if (Shift.getOpcode() != ISD::SHL) break;
1196
Evan Cheng1314b002007-12-13 00:43:27 +00001197 // Not likely to be profitable if either the AND or SHIFT node has more
1198 // than one use (unless all uses are for address computation). Besides,
1199 // isel mechanism requires their node ids to be reused.
1200 if (!N.hasOneUse() || !Shift.hasOneUse())
1201 break;
1202
1203 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001204 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001205 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1206 break;
1207
1208 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001209 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001210 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001211 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1212 NewANDMask);
1213 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001214 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001215
1216 // Insert the new nodes into the topological ordering.
1217 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1218 CurDAG->RepositionNode(X.getNode(), C1);
1219 C1->setNodeId(X.getNode()->getNodeId());
1220 }
1221 if (NewANDMask.getNode()->getNodeId() == -1 ||
1222 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1223 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1224 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1225 }
1226 if (NewAND.getNode()->getNodeId() == -1 ||
1227 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1228 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1229 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1230 }
1231 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1232 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1233 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1234 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1235 }
1236
Dan Gohman7b8e9642008-10-13 20:52:04 +00001237 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001238
1239 AM.Scale = 1 << ShiftCst;
1240 AM.IndexReg = NewAND;
1241 return false;
1242 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001243 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001244
Rafael Espindola523249f2009-03-31 16:16:57 +00001245 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001246}
1247
1248/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1249/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001250bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001251 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001253 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001254 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001255 AM.IndexReg = N;
1256 AM.Scale = 1;
1257 return false;
1258 }
1259
1260 // Otherwise, we cannot select it.
1261 return true;
1262 }
1263
1264 // Default, generate it as a register.
1265 AM.BaseType = X86ISelAddressMode::RegBase;
1266 AM.Base.Reg = N;
1267 return false;
1268}
1269
Evan Chengec693f72005-12-08 02:01:35 +00001270/// SelectAddr - returns true if it is able pattern match an addressing mode.
1271/// It returns the operands which make up the maximal addressing mode it can
1272/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001273bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1274 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001275 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001276 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001277 bool Done = false;
1278 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1279 unsigned Opcode = N.getOpcode();
1280 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001281 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001282 // If we are able to fold N into addressing mode, then we'll allow it even
1283 // if N has multiple uses. In general, addressing computation is used as
1284 // addresses by all of its uses. But watch out for CopyToReg uses, that
1285 // means the address computation is liveout. It will be computed by a LEA
1286 // so we want to avoid computing the address twice.
1287 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1288 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1289 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001290 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001291 Done = true;
1292 break;
1293 }
1294 }
1295 }
1296 }
1297
1298 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001299 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001300
Owen Andersone50ed302009-08-10 22:56:29 +00001301 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001302 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001303 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001304 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001305 }
Evan Cheng8700e142006-01-11 06:09:51 +00001306
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001308 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001309
Rafael Espindola094fad32009-04-08 21:14:34 +00001310 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001311 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001312}
1313
Chris Lattner3a7cd952006-10-07 21:55:32 +00001314/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1315/// match a load whose top elements are either undef or zeros. The load flavor
1316/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001317bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1318 SDValue N, SDValue &Base,
1319 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001320 SDValue &Disp, SDValue &Segment,
1321 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001322 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001323 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001324 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001325 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001326 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001327 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001328 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001329 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001330 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001331 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001332 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001333 return true;
1334 }
1335 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001336
1337 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001338 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001339 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001340 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001341 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001342 N.getOperand(0).getNode()->hasOneUse() &&
1343 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001344 N.getOperand(0).getOperand(0).hasOneUse()) {
1345 // Okay, this is a zero extending load. Fold it.
1346 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001347 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001348 return false;
1349 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001350 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001351 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001352 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001353 return false;
1354}
1355
1356
Evan Cheng51a9ed92006-02-25 10:09:08 +00001357/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1358/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001359bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1360 SDValue &Base, SDValue &Scale,
1361 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001362 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001363
1364 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1365 // segments.
1366 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001367 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001368 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001369 if (MatchAddress(N, AM))
1370 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001371 assert (T == AM.Segment);
1372 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001373
Owen Andersone50ed302009-08-10 22:56:29 +00001374 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001375 unsigned Complexity = 0;
1376 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001377 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001378 Complexity = 1;
1379 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001380 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001381 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1382 Complexity = 4;
1383
Gabor Greifba36cb52008-08-28 21:40:38 +00001384 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001385 Complexity++;
1386 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001387 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001388
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001389 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1390 // a simple shift.
1391 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001392 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001393
1394 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1395 // to a LEA. This is determined with some expermentation but is by no means
1396 // optimal (especially for code size consideration). LEA is nice because of
1397 // its three-address nature. Tweak the cost function again when we can run
1398 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001399 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001400 // For X86-64, we should always use lea to materialize RIP relative
1401 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001402 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001403 Complexity = 4;
1404 else
1405 Complexity += 2;
1406 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001407
Gabor Greifba36cb52008-08-28 21:40:38 +00001408 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001409 Complexity++;
1410
Chris Lattner25142782009-07-11 22:50:33 +00001411 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001412 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001413 return false;
1414
1415 SDValue Segment;
1416 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1417 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001418}
1419
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001420/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1421bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1422 SDValue &Scale, SDValue &Index,
1423 SDValue &Disp) {
1424 assert(Op.getOpcode() == X86ISD::TLSADDR);
1425 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1426 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1427
1428 X86ISelAddressMode AM;
1429 AM.GV = GA->getGlobal();
1430 AM.Disp += GA->getOffset();
1431 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001432 AM.SymbolFlags = GA->getTargetFlags();
1433
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001435 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001436 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001437 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001438 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001439 }
1440
1441 SDValue Segment;
1442 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1443 return true;
1444}
1445
1446
Dan Gohman475871a2008-07-27 21:46:04 +00001447bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1448 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001449 SDValue &Index, SDValue &Disp,
1450 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001451 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001452 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001453 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001454 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001455 return false;
1456}
1457
Dan Gohman8b746962008-09-23 18:22:58 +00001458/// getGlobalBaseReg - Return an SDNode that returns the value of
1459/// the global base register. Output instructions required to
1460/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001461///
Evan Cheng9ade2182006-08-26 05:34:46 +00001462SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001463 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001464 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001465}
1466
Evan Chengb245d922006-05-20 01:36:52 +00001467static SDNode *FindCallStartFromCall(SDNode *Node) {
1468 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001470 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001471 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001472}
1473
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001474SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1475 SDValue Chain = Node->getOperand(0);
1476 SDValue In1 = Node->getOperand(1);
1477 SDValue In2L = Node->getOperand(2);
1478 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001479 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1480 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001481 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001482 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1483 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1484 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1485 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1486 MVT::i32, MVT::i32, MVT::Other, Ops,
1487 array_lengthof(Ops));
1488 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1489 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001490}
Christopher Lambc59e5212007-08-10 21:48:46 +00001491
Owen Andersone50ed302009-08-10 22:56:29 +00001492SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001493 if (Node->hasAnyUseOfValue(0))
1494 return 0;
1495
1496 // Optimize common patterns for __sync_add_and_fetch and
1497 // __sync_sub_and_fetch where the result is not used. This allows us
1498 // to use "lock" version of add, sub, inc, dec instructions.
1499 // FIXME: Do not use special instructions but instead add the "lock"
1500 // prefix to the target node somehow. The extra information will then be
1501 // transferred to machine instruction and it denotes the prefix.
1502 SDValue Chain = Node->getOperand(0);
1503 SDValue Ptr = Node->getOperand(1);
1504 SDValue Val = Node->getOperand(2);
1505 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1506 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1507 return 0;
1508
1509 bool isInc = false, isDec = false, isSub = false, isCN = false;
1510 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1511 if (CN) {
1512 isCN = true;
1513 int64_t CNVal = CN->getSExtValue();
1514 if (CNVal == 1)
1515 isInc = true;
1516 else if (CNVal == -1)
1517 isDec = true;
1518 else if (CNVal >= 0)
1519 Val = CurDAG->getTargetConstant(CNVal, NVT);
1520 else {
1521 isSub = true;
1522 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1523 }
1524 } else if (Val.hasOneUse() &&
1525 Val.getOpcode() == ISD::SUB &&
1526 X86::isZeroNode(Val.getOperand(0))) {
1527 isSub = true;
1528 Val = Val.getOperand(1);
1529 }
1530
1531 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001532 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001533 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001535 if (isInc)
1536 Opc = X86::LOCK_INC8m;
1537 else if (isDec)
1538 Opc = X86::LOCK_DEC8m;
1539 else if (isSub) {
1540 if (isCN)
1541 Opc = X86::LOCK_SUB8mi;
1542 else
1543 Opc = X86::LOCK_SUB8mr;
1544 } else {
1545 if (isCN)
1546 Opc = X86::LOCK_ADD8mi;
1547 else
1548 Opc = X86::LOCK_ADD8mr;
1549 }
1550 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001552 if (isInc)
1553 Opc = X86::LOCK_INC16m;
1554 else if (isDec)
1555 Opc = X86::LOCK_DEC16m;
1556 else if (isSub) {
1557 if (isCN) {
1558 if (Predicate_i16immSExt8(Val.getNode()))
1559 Opc = X86::LOCK_SUB16mi8;
1560 else
1561 Opc = X86::LOCK_SUB16mi;
1562 } else
1563 Opc = X86::LOCK_SUB16mr;
1564 } else {
1565 if (isCN) {
1566 if (Predicate_i16immSExt8(Val.getNode()))
1567 Opc = X86::LOCK_ADD16mi8;
1568 else
1569 Opc = X86::LOCK_ADD16mi;
1570 } else
1571 Opc = X86::LOCK_ADD16mr;
1572 }
1573 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001574 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001575 if (isInc)
1576 Opc = X86::LOCK_INC32m;
1577 else if (isDec)
1578 Opc = X86::LOCK_DEC32m;
1579 else if (isSub) {
1580 if (isCN) {
1581 if (Predicate_i32immSExt8(Val.getNode()))
1582 Opc = X86::LOCK_SUB32mi8;
1583 else
1584 Opc = X86::LOCK_SUB32mi;
1585 } else
1586 Opc = X86::LOCK_SUB32mr;
1587 } else {
1588 if (isCN) {
1589 if (Predicate_i32immSExt8(Val.getNode()))
1590 Opc = X86::LOCK_ADD32mi8;
1591 else
1592 Opc = X86::LOCK_ADD32mi;
1593 } else
1594 Opc = X86::LOCK_ADD32mr;
1595 }
1596 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001597 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001598 if (isInc)
1599 Opc = X86::LOCK_INC64m;
1600 else if (isDec)
1601 Opc = X86::LOCK_DEC64m;
1602 else if (isSub) {
1603 Opc = X86::LOCK_SUB64mr;
1604 if (isCN) {
1605 if (Predicate_i64immSExt8(Val.getNode()))
1606 Opc = X86::LOCK_SUB64mi8;
1607 else if (Predicate_i64immSExt32(Val.getNode()))
1608 Opc = X86::LOCK_SUB64mi32;
1609 }
1610 } else {
1611 Opc = X86::LOCK_ADD64mr;
1612 if (isCN) {
1613 if (Predicate_i64immSExt8(Val.getNode()))
1614 Opc = X86::LOCK_ADD64mi8;
1615 else if (Predicate_i64immSExt32(Val.getNode()))
1616 Opc = X86::LOCK_ADD64mi32;
1617 }
1618 }
1619 break;
1620 }
1621
1622 DebugLoc dl = Node->getDebugLoc();
Dan Gohman602b0c82009-09-25 18:54:59 +00001623 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
1624 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001625 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1626 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001627 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001628 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1629 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1630 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001631 SDValue RetVals[] = { Undef, Ret };
1632 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1633 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001634 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1635 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1636 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001637 SDValue RetVals[] = { Undef, Ret };
1638 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1639 }
1640}
1641
Dan Gohman11596ed2009-10-09 20:35:19 +00001642/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1643/// any uses which require the SF or OF bits to be accurate.
1644static bool HasNoSignedComparisonUses(SDNode *N) {
1645 // Examine each user of the node.
1646 for (SDNode::use_iterator UI = N->use_begin(),
1647 UE = N->use_end(); UI != UE; ++UI) {
1648 // Only examine CopyToReg uses.
1649 if (UI->getOpcode() != ISD::CopyToReg)
1650 return false;
1651 // Only examine CopyToReg uses that copy to EFLAGS.
1652 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1653 X86::EFLAGS)
1654 return false;
1655 // Examine each user of the CopyToReg use.
1656 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1657 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1658 // Only examine the Flag result.
1659 if (FlagUI.getUse().getResNo() != 1) continue;
1660 // Anything unusual: assume conservatively.
1661 if (!FlagUI->isMachineOpcode()) return false;
1662 // Examine the opcode of the user.
1663 switch (FlagUI->getMachineOpcode()) {
1664 // These comparisons don't treat the most significant bit specially.
1665 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1666 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1667 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1668 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
1669 case X86::JA: case X86::JAE: case X86::JB: case X86::JBE:
1670 case X86::JE: case X86::JNE: case X86::JP: case X86::JNP:
1671 case X86::CMOVA16rr: case X86::CMOVA16rm:
1672 case X86::CMOVA32rr: case X86::CMOVA32rm:
1673 case X86::CMOVA64rr: case X86::CMOVA64rm:
1674 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1675 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1676 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1677 case X86::CMOVB16rr: case X86::CMOVB16rm:
1678 case X86::CMOVB32rr: case X86::CMOVB32rm:
1679 case X86::CMOVB64rr: case X86::CMOVB64rm:
1680 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1681 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1682 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1683 case X86::CMOVE16rr: case X86::CMOVE16rm:
1684 case X86::CMOVE32rr: case X86::CMOVE32rm:
1685 case X86::CMOVE64rr: case X86::CMOVE64rm:
1686 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1687 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1688 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1689 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1690 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1691 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1692 case X86::CMOVP16rr: case X86::CMOVP16rm:
1693 case X86::CMOVP32rr: case X86::CMOVP32rm:
1694 case X86::CMOVP64rr: case X86::CMOVP64rm:
1695 continue;
1696 // Anything else: assume conservatively.
1697 default: return false;
1698 }
1699 }
1700 }
1701 return true;
1702}
1703
Dan Gohman475871a2008-07-27 21:46:04 +00001704SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001705 SDNode *Node = N.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001706 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001707 unsigned Opc, MOpc;
1708 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001709 DebugLoc dl = Node->getDebugLoc();
1710
Evan Chengf597dc72006-02-10 22:24:32 +00001711#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001712 DEBUG({
1713 errs() << std::string(Indent, ' ') << "Selecting: ";
1714 Node->dump(CurDAG);
1715 errs() << '\n';
1716 });
Evan Cheng23addc02006-02-10 22:46:26 +00001717 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001718#endif
1719
Dan Gohmane8be6c62008-07-17 19:10:17 +00001720 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001721#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001722 DEBUG({
1723 errs() << std::string(Indent-2, ' ') << "== ";
1724 Node->dump(CurDAG);
1725 errs() << '\n';
1726 });
Evan Cheng23addc02006-02-10 22:46:26 +00001727 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001728#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001729 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001730 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001731
Evan Cheng0114e942006-01-06 20:36:21 +00001732 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001733 default: break;
1734 case X86ISD::GlobalBaseReg:
1735 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001736
Dan Gohman72677342009-08-02 16:10:52 +00001737 case X86ISD::ATOMOR64_DAG:
1738 return SelectAtomic64(Node, X86::ATOMOR6432);
1739 case X86ISD::ATOMXOR64_DAG:
1740 return SelectAtomic64(Node, X86::ATOMXOR6432);
1741 case X86ISD::ATOMADD64_DAG:
1742 return SelectAtomic64(Node, X86::ATOMADD6432);
1743 case X86ISD::ATOMSUB64_DAG:
1744 return SelectAtomic64(Node, X86::ATOMSUB6432);
1745 case X86ISD::ATOMNAND64_DAG:
1746 return SelectAtomic64(Node, X86::ATOMNAND6432);
1747 case X86ISD::ATOMAND64_DAG:
1748 return SelectAtomic64(Node, X86::ATOMAND6432);
1749 case X86ISD::ATOMSWAP64_DAG:
1750 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001751
Dan Gohman72677342009-08-02 16:10:52 +00001752 case ISD::ATOMIC_LOAD_ADD: {
1753 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1754 if (RetVal)
1755 return RetVal;
1756 break;
1757 }
1758
1759 case ISD::SMUL_LOHI:
1760 case ISD::UMUL_LOHI: {
1761 SDValue N0 = Node->getOperand(0);
1762 SDValue N1 = Node->getOperand(1);
1763
1764 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001765 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001767 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1769 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1770 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1771 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001772 }
Bill Wendling12321672009-08-07 21:33:25 +00001773 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001774 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001775 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001776 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1777 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1778 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1779 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001780 }
Bill Wendling12321672009-08-07 21:33:25 +00001781 }
Dan Gohman72677342009-08-02 16:10:52 +00001782
1783 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001784 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001785 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1787 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1788 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1789 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001790 }
1791
1792 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1793 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001794 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001795 if (!foldedLoad) {
1796 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1797 if (foldedLoad)
1798 std::swap(N0, N1);
1799 }
1800
1801 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1802 N0, SDValue()).getValue(1);
1803
1804 if (foldedLoad) {
1805 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1806 InFlag };
1807 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001808 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1809 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001810 InFlag = SDValue(CNode, 1);
1811 // Update the chain.
1812 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1813 } else {
1814 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001815 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001816 }
1817
1818 // Copy the low half of the result, if it is needed.
1819 if (!N.getValue(0).use_empty()) {
1820 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1821 LoReg, NVT, InFlag);
1822 InFlag = Result.getValue(2);
1823 ReplaceUses(N.getValue(0), Result);
1824#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001825 DEBUG({
1826 errs() << std::string(Indent-2, ' ') << "=> ";
1827 Result.getNode()->dump(CurDAG);
1828 errs() << '\n';
1829 });
Dan Gohman72677342009-08-02 16:10:52 +00001830#endif
1831 }
1832 // Copy the high half of the result, if it is needed.
1833 if (!N.getValue(1).use_empty()) {
1834 SDValue Result;
1835 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1836 // Prevent use of AH in a REX instruction by referencing AX instead.
1837 // Shift it down 8 bits.
1838 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001839 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001840 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001841 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1842 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001843 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001844 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001845 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1846 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001847 } else {
1848 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1849 HiReg, NVT, InFlag);
1850 InFlag = Result.getValue(2);
1851 }
1852 ReplaceUses(N.getValue(1), Result);
1853#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001854 DEBUG({
1855 errs() << std::string(Indent-2, ' ') << "=> ";
1856 Result.getNode()->dump(CurDAG);
1857 errs() << '\n';
1858 });
Dan Gohman72677342009-08-02 16:10:52 +00001859#endif
1860 }
1861
1862#ifndef NDEBUG
1863 Indent -= 2;
1864#endif
1865
1866 return NULL;
1867 }
1868
1869 case ISD::SDIVREM:
1870 case ISD::UDIVREM: {
1871 SDValue N0 = Node->getOperand(0);
1872 SDValue N1 = Node->getOperand(1);
1873
1874 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001875 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001876 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001877 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001878 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1879 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1880 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1881 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001882 }
Bill Wendling12321672009-08-07 21:33:25 +00001883 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001884 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001885 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001886 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1887 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1888 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1889 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001890 }
Bill Wendling12321672009-08-07 21:33:25 +00001891 }
Dan Gohman72677342009-08-02 16:10:52 +00001892
1893 unsigned LoReg, HiReg;
1894 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001896 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 case MVT::i8:
Dan Gohman72677342009-08-02 16:10:52 +00001898 LoReg = X86::AL; HiReg = X86::AH;
1899 ClrOpcode = 0;
1900 SExtOpcode = X86::CBW;
1901 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001902 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001903 LoReg = X86::AX; HiReg = X86::DX;
1904 ClrOpcode = X86::MOV16r0;
1905 SExtOpcode = X86::CWD;
1906 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 case MVT::i32:
Dan Gohman72677342009-08-02 16:10:52 +00001908 LoReg = X86::EAX; HiReg = X86::EDX;
1909 ClrOpcode = X86::MOV32r0;
1910 SExtOpcode = X86::CDQ;
1911 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001912 case MVT::i64:
Dan Gohman72677342009-08-02 16:10:52 +00001913 LoReg = X86::RAX; HiReg = X86::RDX;
1914 ClrOpcode = ~0U; // NOT USED.
1915 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001916 break;
1917 }
1918
Dan Gohman72677342009-08-02 16:10:52 +00001919 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1920 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1921 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001922
Dan Gohman72677342009-08-02 16:10:52 +00001923 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001924 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001925 // Special case for div8, just use a move with zero extension to AX to
1926 // clear the upper 8 bits (AH).
1927 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1928 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1929 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1930 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001931 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1932 MVT::Other, Ops,
1933 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001934 Chain = Move.getValue(1);
1935 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001936 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001937 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001938 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001939 Chain = CurDAG->getEntryNode();
1940 }
1941 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1942 InFlag = Chain.getValue(1);
1943 } else {
1944 InFlag =
1945 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1946 LoReg, N0, SDValue()).getValue(1);
1947 if (isSigned && !signBitIsZero) {
1948 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001949 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001950 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001951 } else {
1952 // Zero out the high part, effectively zero extending the input.
1953 SDValue ClrNode;
Evan Cheng0114e942006-01-06 20:36:21 +00001954
Owen Anderson825b72b2009-08-11 20:47:22 +00001955 if (NVT.getSimpleVT() == MVT::i64) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001956 ClrNode = SDValue(CurDAG->getMachineNode(X86::MOV32r0, dl, MVT::i32),
Dan Gohman72677342009-08-02 16:10:52 +00001957 0);
1958 // We just did a 32-bit clear, insert it into a 64-bit register to
1959 // clear the whole 64-bit reg.
Dan Gohman7289ed22009-11-05 23:53:08 +00001960 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i64);
Dan Gohman72677342009-08-02 16:10:52 +00001961 SDValue SubRegNo =
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
Dan Gohman72677342009-08-02 16:10:52 +00001963 ClrNode =
Dan Gohman7289ed22009-11-05 23:53:08 +00001964 SDValue(CurDAG->getMachineNode(TargetInstrInfo::SUBREG_TO_REG, dl,
1965 MVT::i64, Zero, ClrNode, SubRegNo),
Dan Gohman72677342009-08-02 16:10:52 +00001966 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001967 } else {
Dan Gohman602b0c82009-09-25 18:54:59 +00001968 ClrNode = SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001969 }
Dan Gohman72677342009-08-02 16:10:52 +00001970
1971 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
1972 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001973 }
Evan Cheng948f3432006-01-06 23:19:29 +00001974 }
Dan Gohman525178c2007-10-08 18:33:35 +00001975
Dan Gohman72677342009-08-02 16:10:52 +00001976 if (foldedLoad) {
1977 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1978 InFlag };
1979 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001980 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1981 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001982 InFlag = SDValue(CNode, 1);
1983 // Update the chain.
1984 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1985 } else {
1986 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001987 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001988 }
Evan Cheng948f3432006-01-06 23:19:29 +00001989
Dan Gohman72677342009-08-02 16:10:52 +00001990 // Copy the division (low) result, if it is needed.
1991 if (!N.getValue(0).use_empty()) {
1992 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1993 LoReg, NVT, InFlag);
1994 InFlag = Result.getValue(2);
1995 ReplaceUses(N.getValue(0), Result);
1996#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001997 DEBUG({
1998 errs() << std::string(Indent-2, ' ') << "=> ";
1999 Result.getNode()->dump(CurDAG);
2000 errs() << '\n';
2001 });
Dan Gohman72677342009-08-02 16:10:52 +00002002#endif
2003 }
2004 // Copy the remainder (high) result, if it is needed.
2005 if (!N.getValue(1).use_empty()) {
2006 SDValue Result;
2007 if (HiReg == X86::AH && Subtarget->is64Bit()) {
2008 // Prevent use of AH in a REX instruction by referencing AX instead.
2009 // Shift it down 8 bits.
2010 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002011 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00002012 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00002013 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00002014 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00002016 0);
2017 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00002018 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2019 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00002020 } else {
2021 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2022 HiReg, NVT, InFlag);
2023 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00002024 }
Dan Gohman72677342009-08-02 16:10:52 +00002025 ReplaceUses(N.getValue(1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00002026#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002027 DEBUG({
2028 errs() << std::string(Indent-2, ' ') << "=> ";
2029 Result.getNode()->dump(CurDAG);
2030 errs() << '\n';
2031 });
Dan Gohmana37c9f72007-09-25 18:23:27 +00002032#endif
Dan Gohman72677342009-08-02 16:10:52 +00002033 }
Evan Chengf597dc72006-02-10 22:24:32 +00002034
2035#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00002036 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002037#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002038
Dan Gohman72677342009-08-02 16:10:52 +00002039 return NULL;
2040 }
2041
Dan Gohman6a402dc2009-08-19 18:16:17 +00002042 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002043 SDValue N0 = Node->getOperand(0);
2044 SDValue N1 = Node->getOperand(1);
2045
2046 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2047 // use a smaller encoding.
2048 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
2049 N0.getValueType() != MVT::i8 &&
2050 X86::isZeroNode(N1)) {
2051 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2052 if (!C) break;
2053
2054 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00002055 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2056 (!(C->getZExtValue() & 0x80) ||
2057 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002058 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2059 SDValue Reg = N0.getNode()->getOperand(0);
2060
2061 // On x86-32, only the ABCD registers have 8-bit subregisters.
2062 if (!Subtarget->is64Bit()) {
2063 TargetRegisterClass *TRC = 0;
2064 switch (N0.getValueType().getSimpleVT().SimpleTy) {
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
2074 // Extract the l-register.
2075 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2076 MVT::i8, Reg);
2077
2078 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00002079 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002080 }
2081
2082 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00002083 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2084 (!(C->getZExtValue() & 0x8000) ||
2085 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002086 // Shift the immediate right by 8 bits.
2087 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2088 MVT::i8);
2089 SDValue Reg = N0.getNode()->getOperand(0);
2090
2091 // Put the value in an ABCD register.
2092 TargetRegisterClass *TRC = 0;
2093 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2094 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2095 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2096 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2097 default: llvm_unreachable("Unsupported TEST operand type!");
2098 }
2099 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002100 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2101 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002102
2103 // Extract the h-register.
2104 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2105 MVT::i8, Reg);
2106
2107 // Emit a testb. No special NOREX tricks are needed since there's
2108 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00002109 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2110 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002111 }
2112
2113 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2114 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002115 N0.getValueType() != MVT::i16 &&
2116 (!(C->getZExtValue() & 0x8000) ||
2117 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002118 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2119 SDValue Reg = N0.getNode()->getOperand(0);
2120
2121 // Extract the 16-bit subregister.
2122 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2123 MVT::i16, Reg);
2124
2125 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00002126 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002127 }
2128
2129 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2130 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002131 N0.getValueType() == MVT::i64 &&
2132 (!(C->getZExtValue() & 0x80000000) ||
2133 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002134 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2135 SDValue Reg = N0.getNode()->getOperand(0);
2136
2137 // Extract the 32-bit subregister.
2138 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2139 MVT::i32, Reg);
2140
2141 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00002142 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002143 }
2144 }
2145 break;
2146 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002147 }
2148
Evan Cheng9ade2182006-08-26 05:34:46 +00002149 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00002150
Evan Chengf597dc72006-02-10 22:24:32 +00002151#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002152 DEBUG({
2153 errs() << std::string(Indent-2, ' ') << "=> ";
2154 if (ResNode == NULL || ResNode == N.getNode())
2155 N.getNode()->dump(CurDAG);
2156 else
2157 ResNode->dump(CurDAG);
2158 errs() << '\n';
2159 });
Evan Cheng23addc02006-02-10 22:46:26 +00002160 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002161#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002162
2163 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002164}
2165
Chris Lattnerc0bad572006-06-08 18:03:49 +00002166bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002167SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002168 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002169 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002170 switch (ConstraintCode) {
2171 case 'o': // offsetable ??
2172 case 'v': // not offsetable ??
2173 default: return true;
2174 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00002175 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002176 return true;
2177 break;
2178 }
2179
Evan Cheng04699902006-08-26 01:05:16 +00002180 OutOps.push_back(Op0);
2181 OutOps.push_back(Op1);
2182 OutOps.push_back(Op2);
2183 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002184 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002185 return false;
2186}
2187
Chris Lattnerc961eea2005-11-16 01:54:32 +00002188/// createX86ISelDag - This pass converts a legalized DAG into a
2189/// X86-specific DAG, ready for instruction scheduling.
2190///
Bill Wendling98a366d2009-04-29 23:29:43 +00002191FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2192 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002193 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002194}