blob: 296a4d3856a9fc05977877bdc50ec5455e79ff7b [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000038#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000039#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000040#include "llvm/Support/Streams.h"
Torok Edwindac237e2009-07-08 20:53:28 +000041#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000043#include "llvm/ADT/Statistic.h"
44using namespace llvm;
45
Evan Cheng4d952322009-03-31 01:13:53 +000046#include "llvm/Support/CommandLine.h"
47static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
48
Chris Lattner95b2c7d2006-12-19 22:59:26 +000049STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
50
Chris Lattnerc961eea2005-11-16 01:54:32 +000051//===----------------------------------------------------------------------===//
52// Pattern Matcher Implementation
53//===----------------------------------------------------------------------===//
54
55namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000057 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 /// tree.
59 struct X86ISelAddressMode {
60 enum {
61 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000062 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000063 } BaseType;
64
65 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000066 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000067 int FrameIndex;
68 } Base;
69
70 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000071 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000072 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000073 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000075 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000076 const char *ES;
77 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000078 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000079 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000080
81 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000082 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattnerb8afeb92009-06-26 05:51:45 +000083 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0), SymbolFlags(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000084 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000085
86 bool hasSymbolicDisplacement() const {
87 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
88 }
Chris Lattner18c59872009-06-27 04:16:01 +000089
90 bool hasBaseOrIndexReg() const {
91 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
92 }
93
94 /// isRIPRelative - Return true if this addressing mode is already RIP
95 /// relative.
96 bool isRIPRelative() const {
97 if (BaseType != RegBase) return false;
98 if (RegisterSDNode *RegNode =
99 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
100 return RegNode->getReg() == X86::RIP;
101 return false;
102 }
103
104 void setBaseReg(SDValue Reg) {
105 BaseType = RegBase;
106 Base.Reg = Reg;
107 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000108
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000109 void dump() {
110 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000111 cerr << "Base.Reg ";
112 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
113 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000114 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
Chris Lattner18c59872009-06-27 04:16:01 +0000115 cerr << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000116 cerr << "IndexReg ";
117 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
118 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000119 cerr << " Disp " << Disp << "\n";
120 cerr << "GV "; if (GV) GV->dump();
121 else cerr << "nul";
122 cerr << " CP "; if (CP) CP->dump();
123 else cerr << "nul";
124 cerr << "\n";
125 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
126 cerr << " JT" << JT << " Align" << Align << "\n";
127 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000128 };
129}
130
131namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000132 //===--------------------------------------------------------------------===//
133 /// ISel - X86 specific code to select X86 machine instructions for
134 /// SelectionDAG operations.
135 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000136 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000137 /// X86Lowering - This object fully describes how to lower LLVM code to an
138 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000139 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000140
141 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
142 /// make the right decision when generating code for different targets.
143 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000144
Evan Chengdb8d56b2008-06-30 20:45:06 +0000145 /// CurBB - Current BB being isel'd.
146 ///
147 MachineBasicBlock *CurBB;
148
Evan Chengb7a75a52008-09-26 23:41:32 +0000149 /// OptForSize - If true, selector should try to optimize for code size
150 /// instead of performance.
151 bool OptForSize;
152
Chris Lattnerc961eea2005-11-16 01:54:32 +0000153 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000154 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000155 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000156 X86Lowering(*tm.getTargetLowering()),
157 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000158 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000159
160 virtual const char *getPassName() const {
161 return "X86 DAG->DAG Instruction Selection";
162 }
163
Evan Chengdb8d56b2008-06-30 20:45:06 +0000164 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000165 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000166 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000167
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000168 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
169
Evan Cheng884c70c2008-11-27 00:49:46 +0000170 virtual
171 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000172
Chris Lattnerc961eea2005-11-16 01:54:32 +0000173// Include the pieces autogenerated from the target description.
174#include "X86GenDAGISel.inc"
175
176 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000177 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000178 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000179
Rafael Espindola094fad32009-04-08 21:14:34 +0000180 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
181 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000182 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000183 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000184 unsigned Depth = 0);
185 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000186 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000187 SDValue &Scale, SDValue &Index, SDValue &Disp,
188 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000189 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
190 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000191 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
192 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000193 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
194 SDValue N, SDValue &Base, SDValue &Scale,
195 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000196 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000197 SDValue &InChain, SDValue &OutChain);
198 bool TryFoldLoad(SDValue P, SDValue N,
199 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000200 SDValue &Index, SDValue &Disp,
201 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000202 void PreprocessForRMW();
203 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000204
Chris Lattnerc0bad572006-06-08 18:03:49 +0000205 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
206 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000207 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000208 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000209 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000210
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000211 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
212
Dan Gohman475871a2008-07-27 21:46:04 +0000213 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
214 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000215 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000216 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000217 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
218 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000219 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000220 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000221 // These are 32-bit even in 64-bit mode since RIP relative offset
222 // is 32-bit.
223 if (AM.GV)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000224 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
225 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000227 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000228 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 else if (AM.ES)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000230 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 else if (AM.JT != -1)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000232 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000233 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000234 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000235
236 if (AM.Segment.getNode())
237 Segment = AM.Segment;
238 else
239 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000240 }
241
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000242 /// getI8Imm - Return a target constant with the specified value, of type
243 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000244 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000245 return CurDAG->getTargetConstant(Imm, MVT::i8);
246 }
247
Chris Lattnerc961eea2005-11-16 01:54:32 +0000248 /// getI16Imm - Return a target constant with the specified value, of type
249 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000250 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000251 return CurDAG->getTargetConstant(Imm, MVT::i16);
252 }
253
254 /// getI32Imm - Return a target constant with the specified value, of type
255 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000256 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000257 return CurDAG->getTargetConstant(Imm, MVT::i32);
258 }
Evan Chengf597dc72006-02-10 22:24:32 +0000259
Dan Gohman8b746962008-09-23 18:22:58 +0000260 /// getGlobalBaseReg - Return an SDNode that returns the value of
261 /// the global base register. Output instructions required to
262 /// initialize the global base register, if necessary.
263 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000264 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000265
Dan Gohmanc5534622009-06-03 20:20:00 +0000266 /// getTargetMachine - Return a reference to the TargetMachine, casted
267 /// to the target-specific type.
268 const X86TargetMachine &getTargetMachine() {
269 return static_cast<const X86TargetMachine &>(TM);
270 }
271
272 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
273 /// to the target-specific type.
274 const X86InstrInfo *getInstrInfo() {
275 return getTargetMachine().getInstrInfo();
276 }
277
Evan Cheng23addc02006-02-10 22:46:26 +0000278#ifndef NDEBUG
279 unsigned Indent;
280#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000281 };
282}
283
Evan Chengf4b4c412006-08-08 00:31:00 +0000284
Evan Cheng884c70c2008-11-27 00:49:46 +0000285bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
286 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000287 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000288
Evan Cheng884c70c2008-11-27 00:49:46 +0000289 if (U == Root)
290 switch (U->getOpcode()) {
291 default: break;
292 case ISD::ADD:
293 case ISD::ADDC:
294 case ISD::ADDE:
295 case ISD::AND:
296 case ISD::OR:
297 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000298 SDValue Op1 = U->getOperand(1);
299
Evan Cheng884c70c2008-11-27 00:49:46 +0000300 // If the other operand is a 8-bit immediate we should fold the immediate
301 // instead. This reduces code size.
302 // e.g.
303 // movl 4(%esp), %eax
304 // addl $4, %eax
305 // vs.
306 // movl $4, %eax
307 // addl 4(%esp), %eax
308 // The former is 2 bytes shorter. In case where the increment is 1, then
309 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000310 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000311 if (Imm->getAPIntValue().isSignedIntN(8))
312 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000313
314 // If the other operand is a TLS address, we should fold it instead.
315 // This produces
316 // movl %gs:0, %eax
317 // leal i@NTPOFF(%eax), %eax
318 // instead of
319 // movl $i@NTPOFF, %eax
320 // addl %gs:0, %eax
321 // if the block also has an access to a second TLS address this will save
322 // a load.
323 // FIXME: This is probably also true for non TLS addresses.
324 if (Op1.getOpcode() == X86ISD::Wrapper) {
325 SDValue Val = Op1.getOperand(0);
326 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
327 return false;
328 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000329 }
330 }
331
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000332 // Proceed to 'generic' cycle finder code
333 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000334}
335
Evan Cheng70e674e2006-08-28 20:10:17 +0000336/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
337/// and move load below the TokenFactor. Replace store's chain operand with
338/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000339static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000340 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000341 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000342 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
343 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000344 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000345 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000346 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000347 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
348 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
349 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
350 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000351}
352
Evan Chengcd0baf22008-05-23 21:23:16 +0000353/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
354///
Dan Gohman475871a2008-07-27 21:46:04 +0000355static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
356 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000357 if (N.getOpcode() == ISD::BIT_CONVERT)
358 N = N.getOperand(0);
359
360 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
361 if (!LD || LD->isVolatile())
362 return false;
363 if (LD->getAddressingMode() != ISD::UNINDEXED)
364 return false;
365
366 ISD::LoadExtType ExtType = LD->getExtensionType();
367 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
368 return false;
369
370 if (N.hasOneUse() &&
371 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000372 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000373 Load = N;
374 return true;
375 }
376 return false;
377}
378
Evan Chengab6c3bb2008-08-25 21:27:18 +0000379/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
380/// operand and move load below the call's chain operand.
381static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000382 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000383 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000384 SDValue Chain = CallSeqStart.getOperand(0);
385 if (Chain.getNode() == Load.getNode())
386 Ops.push_back(Load.getOperand(0));
387 else {
388 assert(Chain.getOpcode() == ISD::TokenFactor &&
389 "Unexpected CallSeqStart chain operand");
390 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
391 if (Chain.getOperand(i).getNode() == Load.getNode())
392 Ops.push_back(Load.getOperand(0));
393 else
394 Ops.push_back(Chain.getOperand(i));
395 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000396 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
397 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000398 Ops.clear();
399 Ops.push_back(NewChain);
400 }
401 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
402 Ops.push_back(CallSeqStart.getOperand(i));
403 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000404 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
405 Load.getOperand(1), Load.getOperand(2));
406 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000407 Ops.push_back(SDValue(Load.getNode(), 1));
408 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000409 Ops.push_back(Call.getOperand(i));
410 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
411}
412
413/// isCalleeLoad - Return true if call address is a load and it can be
414/// moved below CALLSEQ_START and the chains leading up to the call.
415/// Return the CALLSEQ_START by reference as a second output.
416static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000417 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000418 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000419 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000420 if (!LD ||
421 LD->isVolatile() ||
422 LD->getAddressingMode() != ISD::UNINDEXED ||
423 LD->getExtensionType() != ISD::NON_EXTLOAD)
424 return false;
425
426 // Now let's find the callseq_start.
427 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
428 if (!Chain.hasOneUse())
429 return false;
430 Chain = Chain.getOperand(0);
431 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000432
433 if (Chain.getOperand(0).getNode() == Callee.getNode())
434 return true;
435 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
436 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
437 return true;
438 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000439}
440
441
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000442/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000443/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000444/// This allows the instruction selector to pick more read-modify-write
445/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000446///
447/// [Load chain]
448/// ^
449/// |
450/// [Load]
451/// ^ ^
452/// | |
453/// / \-
454/// / |
455/// [TokenFactor] [Op]
456/// ^ ^
457/// | |
458/// \ /
459/// \ /
460/// [Store]
461///
462/// The fact the store's chain operand != load's chain will prevent the
463/// (store (op (load))) instruction from being selected. We can transform it to:
464///
465/// [Load chain]
466/// ^
467/// |
468/// [TokenFactor]
469/// ^
470/// |
471/// [Load]
472/// ^ ^
473/// | |
474/// | \-
475/// | |
476/// | [Op]
477/// | ^
478/// | |
479/// \ /
480/// \ /
481/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000482void X86DAGToDAGISel::PreprocessForRMW() {
483 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
484 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000485 if (I->getOpcode() == X86ISD::CALL) {
486 /// Also try moving call address load from outside callseq_start to just
487 /// before the call to allow it to be folded.
488 ///
489 /// [Load chain]
490 /// ^
491 /// |
492 /// [Load]
493 /// ^ ^
494 /// | |
495 /// / \--
496 /// / |
497 ///[CALLSEQ_START] |
498 /// ^ |
499 /// | |
500 /// [LOAD/C2Reg] |
501 /// | |
502 /// \ /
503 /// \ /
504 /// [CALL]
505 SDValue Chain = I->getOperand(0);
506 SDValue Load = I->getOperand(1);
507 if (!isCalleeLoad(Load, Chain))
508 continue;
509 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
510 ++NumLoadMoved;
511 continue;
512 }
513
Evan Cheng8b2794a2006-10-13 21:14:26 +0000514 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000515 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000516 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000517
Gabor Greifba36cb52008-08-28 21:40:38 +0000518 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000519 continue;
520
Dan Gohman475871a2008-07-27 21:46:04 +0000521 SDValue N1 = I->getOperand(1);
522 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000523 if ((N1.getValueType().isFloatingPoint() &&
524 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000525 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000526 continue;
527
528 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000529 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000530 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000531 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000532 case ISD::ADD:
533 case ISD::MUL:
534 case ISD::AND:
535 case ISD::OR:
536 case ISD::XOR:
537 case ISD::ADDC:
538 case ISD::ADDE:
539 case ISD::VECTOR_SHUFFLE: {
540 SDValue N10 = N1.getOperand(0);
541 SDValue N11 = N1.getOperand(1);
542 RModW = isRMWLoad(N10, Chain, N2, Load);
543 if (!RModW)
544 RModW = isRMWLoad(N11, Chain, N2, Load);
545 break;
546 }
547 case ISD::SUB:
548 case ISD::SHL:
549 case ISD::SRA:
550 case ISD::SRL:
551 case ISD::ROTL:
552 case ISD::ROTR:
553 case ISD::SUBC:
554 case ISD::SUBE:
555 case X86ISD::SHLD:
556 case X86ISD::SHRD: {
557 SDValue N10 = N1.getOperand(0);
558 RModW = isRMWLoad(N10, Chain, N2, Load);
559 break;
560 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000561 }
562
Evan Cheng82a35b32006-08-29 06:44:17 +0000563 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000564 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000565 ++NumLoadMoved;
566 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000567 }
568}
569
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000570
571/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
572/// nodes that target the FP stack to be store and load to the stack. This is a
573/// gross hack. We would like to simply mark these as being illegal, but when
574/// we do that, legalize produces these when it expands calls, then expands
575/// these in the same legalize pass. We would like dag combine to be able to
576/// hack on these between the call expansion and the node legalization. As such
577/// this pass basically does "really late" legalization of these inline with the
578/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000579void X86DAGToDAGISel::PreprocessForFPConvert() {
580 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
581 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000582 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
583 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
584 continue;
585
586 // If the source and destination are SSE registers, then this is a legal
587 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000588 MVT SrcVT = N->getOperand(0).getValueType();
589 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000590 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
591 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
592 if (SrcIsSSE && DstIsSSE)
593 continue;
594
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000595 if (!SrcIsSSE && !DstIsSSE) {
596 // If this is an FPStack extension, it is a noop.
597 if (N->getOpcode() == ISD::FP_EXTEND)
598 continue;
599 // If this is a value-preserving FPStack truncation, it is a noop.
600 if (N->getConstantOperandVal(1))
601 continue;
602 }
603
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000604 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
605 // FPStack has extload and truncstore. SSE can fold direct loads into other
606 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000607 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000608 if (N->getOpcode() == ISD::FP_ROUND)
609 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
610 else
611 MemVT = SrcIsSSE ? SrcVT : DstVT;
612
Dan Gohmanf350b272008-08-23 02:25:05 +0000613 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000614 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000615
616 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000617 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000618 N->getOperand(0),
619 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000620 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000621 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000622
623 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
624 // extload we created. This will cause general havok on the dag because
625 // anything below the conversion could be folded into other existing nodes.
626 // To avoid invalidating 'I', back it up to the convert node.
627 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000628 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000629
630 // Now that we did that, the node is dead. Increment the iterator to the
631 // next node to process, then delete N.
632 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000633 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000634 }
635}
636
Chris Lattnerc961eea2005-11-16 01:54:32 +0000637/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
638/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000639void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000640 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000641 const Function *F = CurDAG->getMachineFunction().getFunction();
642 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000643
Evan Chengdb8d56b2008-06-30 20:45:06 +0000644 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000645 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000646 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000647
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000648 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000649 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000650
Chris Lattnerc961eea2005-11-16 01:54:32 +0000651 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000652#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000653 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000654 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000655#endif
David Greene8ad4c002008-10-27 21:56:29 +0000656 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000657#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000658 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000659#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000660
Dan Gohmanf350b272008-08-23 02:25:05 +0000661 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000662}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000663
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000664/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
665/// the main function.
666void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
667 MachineFrameInfo *MFI) {
668 const TargetInstrInfo *TII = TM.getInstrInfo();
669 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000670 BuildMI(BB, DebugLoc::getUnknownLoc(),
671 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000672}
673
674void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
675 // If this is main, emit special code for main.
676 MachineBasicBlock *BB = MF.begin();
677 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
678 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
679}
680
Rafael Espindola094fad32009-04-08 21:14:34 +0000681
682bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
683 X86ISelAddressMode &AM) {
684 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
685 SDValue Segment = N.getOperand(0);
686
687 if (AM.Segment.getNode() == 0) {
688 AM.Segment = Segment;
689 return false;
690 }
691
692 return true;
693}
694
695bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
696 // This optimization is valid because the GNU TLS model defines that
697 // gs:0 (or fs:0 on X86-64) contains its own address.
698 // For more information see http://people.redhat.com/drepper/tls.pdf
699
700 SDValue Address = N.getOperand(1);
701 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
702 !MatchSegmentBaseAddress (Address, AM))
703 return false;
704
705 return true;
706}
707
Chris Lattner18c59872009-06-27 04:16:01 +0000708/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
709/// into an addressing mode. These wrap things that will resolve down into a
710/// symbol reference. If no match is possible, this returns true, otherwise it
711/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000712bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000713 // If the addressing mode already has a symbol as the displacement, we can
714 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000715 if (AM.hasSymbolicDisplacement())
716 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000717
718 SDValue N0 = N.getOperand(0);
Chris Lattner18c59872009-06-27 04:16:01 +0000719
720 // Handle X86-64 rip-relative addresses. We check this before checking direct
721 // folding because RIP is preferable to non-RIP accesses.
722 if (Subtarget->is64Bit() &&
723 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
724 // they cannot be folded into immediate fields.
725 // FIXME: This can be improved for kernel and other models?
726 TM.getCodeModel() == CodeModel::Small &&
727
728 // Base and index reg must be 0 in order to use %rip as base and lowering
729 // must allow RIP.
730 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
731
732 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
733 int64_t Offset = AM.Disp + G->getOffset();
734 if (!isInt32(Offset)) return true;
735 AM.GV = G->getGlobal();
736 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000737 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000738 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
739 int64_t Offset = AM.Disp + CP->getOffset();
740 if (!isInt32(Offset)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000741 AM.CP = CP->getConstVal();
742 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000743 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000744 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000745 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
746 AM.ES = S->getSymbol();
747 AM.SymbolFlags = S->getTargetFlags();
748 } else {
749 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
750 AM.JT = J->getIndex();
751 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000752 }
Chris Lattner18c59872009-06-27 04:16:01 +0000753
754 if (N.getOpcode() == X86ISD::WrapperRIP)
755 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000756 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000757 }
758
759 // Handle the case when globals fit in our immediate field: This is true for
760 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
761 // mode, this results in a non-RIP-relative computation.
762 if (!Subtarget->is64Bit() ||
763 (TM.getCodeModel() == CodeModel::Small &&
764 TM.getRelocationModel() == Reloc::Static)) {
765 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
766 AM.GV = G->getGlobal();
767 AM.Disp += G->getOffset();
768 AM.SymbolFlags = G->getTargetFlags();
769 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
770 AM.CP = CP->getConstVal();
771 AM.Align = CP->getAlignment();
772 AM.Disp += CP->getOffset();
773 AM.SymbolFlags = CP->getTargetFlags();
774 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
775 AM.ES = S->getSymbol();
776 AM.SymbolFlags = S->getTargetFlags();
777 } else {
778 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
779 AM.JT = J->getIndex();
780 AM.SymbolFlags = J->getTargetFlags();
781 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000782 return false;
783 }
784
785 return true;
786}
787
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000788/// MatchAddress - Add the specified node to the specified addressing mode,
789/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000790/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000791bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000792 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000793 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000794 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000795 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000796 // Limit recursion.
797 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000798 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000799
Chris Lattner18c59872009-06-27 04:16:01 +0000800 // If this is already a %rip relative address, we can only merge immediates
801 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000802 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000803 if (AM.isRIPRelative()) {
804 // FIXME: JumpTable and ExternalSymbol address currently don't like
805 // displacements. It isn't very important, but this should be fixed for
806 // consistency.
807 if (!AM.ES && AM.JT != -1) return true;
808
809 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
810 int64_t Val = AM.Disp + Cst->getSExtValue();
811 if (isInt32(Val)) {
812 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000813 return false;
814 }
815 }
816 return true;
817 }
818
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000819 switch (N.getOpcode()) {
820 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000821 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000822 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000823 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000824 AM.Disp += Val;
825 return false;
826 }
827 break;
828 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000829
Rafael Espindola094fad32009-04-08 21:14:34 +0000830 case X86ISD::SegmentBaseAddress:
831 if (!MatchSegmentBaseAddress(N, AM))
832 return false;
833 break;
834
Rafael Espindola49a168d2009-04-12 21:55:03 +0000835 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000836 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000837 if (!MatchWrapper(N, AM))
838 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000839 break;
840
Rafael Espindola094fad32009-04-08 21:14:34 +0000841 case ISD::LOAD:
842 if (!MatchLoad(N, AM))
843 return false;
844 break;
845
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000846 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000847 if (AM.BaseType == X86ISelAddressMode::RegBase
848 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000849 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
850 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
851 return false;
852 }
853 break;
Evan Chengec693f72005-12-08 02:01:35 +0000854
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000855 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000856 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000857 break;
858
Gabor Greif93c53e52008-08-31 15:37:04 +0000859 if (ConstantSDNode
860 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000861 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000862 if (Val == 1 || Val == 2 || Val == 3) {
863 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000864 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000865
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000866 // Okay, we know that we have a scale by now. However, if the scaled
867 // value is an add of something and a constant, we can fold the
868 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000869 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
870 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
871 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000872 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000873 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000874 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000875 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000876 AM.Disp = Disp;
877 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000878 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000879 } else {
880 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000881 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000882 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000883 }
884 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000885 }
Evan Chengec693f72005-12-08 02:01:35 +0000886
Dan Gohman83688052007-10-22 20:22:24 +0000887 case ISD::SMUL_LOHI:
888 case ISD::UMUL_LOHI:
889 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000890 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000891 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000892 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000893 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000894 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000895 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000896 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000897 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000898 if (ConstantSDNode
899 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000900 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
901 CN->getZExtValue() == 9) {
902 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000903
Gabor Greifba36cb52008-08-28 21:40:38 +0000904 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000905 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000906
907 // Okay, we know that we have a scale by now. However, if the scaled
908 // value is an add of something and a constant, we can fold the
909 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000910 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
911 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
912 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000913 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000914 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000915 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000916 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000917 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000918 AM.Disp = Disp;
919 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000920 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000921 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000922 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000923 }
924
925 AM.IndexReg = AM.Base.Reg = Reg;
926 return false;
927 }
Chris Lattner62412262007-02-04 20:18:17 +0000928 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000929 break;
930
Dan Gohman3cd90a12009-05-11 18:02:53 +0000931 case ISD::SUB: {
932 // Given A-B, if A can be completely folded into the address and
933 // the index field with the index field unused, use -B as the index.
934 // This is a win if a has multiple parts that can be folded into
935 // the address. Also, this saves a mov if the base register has
936 // other uses, since it avoids a two-address sub instruction, however
937 // it costs an additional mov if the index register has other uses.
938
939 // Test if the LHS of the sub can be folded.
940 X86ISelAddressMode Backup = AM;
941 if (MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) {
942 AM = Backup;
943 break;
944 }
945 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000946 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000947 AM = Backup;
948 break;
949 }
950 int Cost = 0;
951 SDValue RHS = N.getNode()->getOperand(1);
952 // If the RHS involves a register with multiple uses, this
953 // transformation incurs an extra mov, due to the neg instruction
954 // clobbering its operand.
955 if (!RHS.getNode()->hasOneUse() ||
956 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
957 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
958 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
959 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
960 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
961 ++Cost;
962 // If the base is a register with multiple uses, this
963 // transformation may save a mov.
964 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
965 AM.Base.Reg.getNode() &&
966 !AM.Base.Reg.getNode()->hasOneUse()) ||
967 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
968 --Cost;
969 // If the folded LHS was interesting, this transformation saves
970 // address arithmetic.
971 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
972 ((AM.Disp != 0) && (Backup.Disp == 0)) +
973 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
974 --Cost;
975 // If it doesn't look like it may be an overall win, don't do it.
976 if (Cost >= 0) {
977 AM = Backup;
978 break;
979 }
980
981 // Ok, the transformation is legal and appears profitable. Go for it.
982 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
983 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
984 AM.IndexReg = Neg;
985 AM.Scale = 1;
986
987 // Insert the new nodes into the topological ordering.
988 if (Zero.getNode()->getNodeId() == -1 ||
989 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
990 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
991 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
992 }
993 if (Neg.getNode()->getNodeId() == -1 ||
994 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
995 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
996 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
997 }
998 return false;
999 }
1000
Evan Cheng8e278262009-01-17 07:09:27 +00001001 case ISD::ADD: {
1002 X86ISelAddressMode Backup = AM;
Rafael Espindola523249f2009-03-31 16:16:57 +00001003 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
1004 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001005 return false;
1006 AM = Backup;
Rafael Espindola523249f2009-03-31 16:16:57 +00001007 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
1008 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001009 return false;
1010 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001011
1012 // If we couldn't fold both operands into the address at the same time,
1013 // see if we can just put each operand into a register and fold at least
1014 // the add.
1015 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1016 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001017 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001018 AM.Base.Reg = N.getNode()->getOperand(0);
1019 AM.IndexReg = N.getNode()->getOperand(1);
1020 AM.Scale = 1;
1021 return false;
1022 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001023 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001024 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001025
Chris Lattner62412262007-02-04 20:18:17 +00001026 case ISD::OR:
1027 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001028 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1029 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001030 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001031 // Start with the LHS as an addr mode.
Rafael Espindola523249f2009-03-31 16:16:57 +00001032 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001033 // Address could not have picked a GV address for the displacement.
1034 AM.GV == NULL &&
1035 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +00001036 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001037 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001038 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001039 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001040 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001041 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001042 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001043 }
1044 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001045
1046 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001047 // Perform some heroic transforms on an and of a constant-count shift
1048 // with a constant to enable use of the scaled offset field.
1049
Dan Gohman475871a2008-07-27 21:46:04 +00001050 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001051 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001052
Evan Cheng1314b002007-12-13 00:43:27 +00001053 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001054 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001055
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001056 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001057 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1058 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1059 if (!C1 || !C2) break;
1060
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001061 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1062 // allows us to convert the shift and and into an h-register extract and
1063 // a scaled index.
1064 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1065 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001066 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001067 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1068 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1069 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1070 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1071 X, Eight);
1072 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1073 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +00001074 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1075 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1076 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001077
1078 // Insert the new nodes into the topological ordering.
1079 if (Eight.getNode()->getNodeId() == -1 ||
1080 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1081 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1082 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1083 }
1084 if (Mask.getNode()->getNodeId() == -1 ||
1085 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1086 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1087 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1088 }
1089 if (Srl.getNode()->getNodeId() == -1 ||
1090 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1091 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1092 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1093 }
1094 if (And.getNode()->getNodeId() == -1 ||
1095 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1096 CurDAG->RepositionNode(N.getNode(), And.getNode());
1097 And.getNode()->setNodeId(N.getNode()->getNodeId());
1098 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001099 if (ShlCount.getNode()->getNodeId() == -1 ||
1100 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1101 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1102 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1103 }
1104 if (Shl.getNode()->getNodeId() == -1 ||
1105 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1106 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1107 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1108 }
1109 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001110 AM.IndexReg = And;
1111 AM.Scale = (1 << ScaleLog);
1112 return false;
1113 }
1114 }
1115
1116 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1117 // allows us to fold the shift into this addressing mode.
1118 if (Shift.getOpcode() != ISD::SHL) break;
1119
Evan Cheng1314b002007-12-13 00:43:27 +00001120 // Not likely to be profitable if either the AND or SHIFT node has more
1121 // than one use (unless all uses are for address computation). Besides,
1122 // isel mechanism requires their node ids to be reused.
1123 if (!N.hasOneUse() || !Shift.hasOneUse())
1124 break;
1125
1126 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001127 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001128 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1129 break;
1130
1131 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001132 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001133 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001134 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1135 NewANDMask);
1136 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001137 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001138
1139 // Insert the new nodes into the topological ordering.
1140 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1141 CurDAG->RepositionNode(X.getNode(), C1);
1142 C1->setNodeId(X.getNode()->getNodeId());
1143 }
1144 if (NewANDMask.getNode()->getNodeId() == -1 ||
1145 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1146 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1147 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1148 }
1149 if (NewAND.getNode()->getNodeId() == -1 ||
1150 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1151 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1152 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1153 }
1154 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1155 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1156 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1157 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1158 }
1159
Dan Gohman7b8e9642008-10-13 20:52:04 +00001160 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001161
1162 AM.Scale = 1 << ShiftCst;
1163 AM.IndexReg = NewAND;
1164 return false;
1165 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001166 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001167
Rafael Espindola523249f2009-03-31 16:16:57 +00001168 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001169}
1170
1171/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1172/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001173bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001174 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001175 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001176 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001177 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001178 AM.IndexReg = N;
1179 AM.Scale = 1;
1180 return false;
1181 }
1182
1183 // Otherwise, we cannot select it.
1184 return true;
1185 }
1186
1187 // Default, generate it as a register.
1188 AM.BaseType = X86ISelAddressMode::RegBase;
1189 AM.Base.Reg = N;
1190 return false;
1191}
1192
Evan Chengec693f72005-12-08 02:01:35 +00001193/// SelectAddr - returns true if it is able pattern match an addressing mode.
1194/// It returns the operands which make up the maximal addressing mode it can
1195/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001196bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1197 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001198 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001199 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001200 bool Done = false;
1201 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1202 unsigned Opcode = N.getOpcode();
1203 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001204 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001205 // If we are able to fold N into addressing mode, then we'll allow it even
1206 // if N has multiple uses. In general, addressing computation is used as
1207 // addresses by all of its uses. But watch out for CopyToReg uses, that
1208 // means the address computation is liveout. It will be computed by a LEA
1209 // so we want to avoid computing the address twice.
1210 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1211 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1212 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001213 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001214 Done = true;
1215 break;
1216 }
1217 }
1218 }
1219 }
1220
1221 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001222 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001223
Duncan Sands83ec4b62008-06-06 12:08:01 +00001224 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001225 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001226 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001227 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001228 }
Evan Cheng8700e142006-01-11 06:09:51 +00001229
Gabor Greifba36cb52008-08-28 21:40:38 +00001230 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001231 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001232
Rafael Espindola094fad32009-04-08 21:14:34 +00001233 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001234 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001235}
1236
Chris Lattner3a7cd952006-10-07 21:55:32 +00001237/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1238/// match a load whose top elements are either undef or zeros. The load flavor
1239/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001240bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1241 SDValue N, SDValue &Base,
1242 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001243 SDValue &Disp, SDValue &Segment,
1244 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001245 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001246 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001247 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001248 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001249 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001250 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001251 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001252 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001253 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001254 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001255 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001256 return true;
1257 }
1258 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001259
1260 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001261 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001262 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001263 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001264 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001265 N.getOperand(0).getNode()->hasOneUse() &&
1266 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001267 N.getOperand(0).getOperand(0).hasOneUse()) {
1268 // Okay, this is a zero extending load. Fold it.
1269 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001270 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001271 return false;
1272 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001273 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001274 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001275 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001276 return false;
1277}
1278
1279
Evan Cheng51a9ed92006-02-25 10:09:08 +00001280/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1281/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001282bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1283 SDValue &Base, SDValue &Scale,
1284 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001285 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001286
1287 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1288 // segments.
1289 SDValue Copy = AM.Segment;
1290 SDValue T = CurDAG->getRegister(0, MVT::i32);
1291 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001292 if (MatchAddress(N, AM))
1293 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001294 assert (T == AM.Segment);
1295 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001296
Duncan Sands83ec4b62008-06-06 12:08:01 +00001297 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001298 unsigned Complexity = 0;
1299 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001300 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001301 Complexity = 1;
1302 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001303 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001304 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1305 Complexity = 4;
1306
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001308 Complexity++;
1309 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001310 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001311
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001312 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1313 // a simple shift.
1314 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001315 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001316
1317 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1318 // to a LEA. This is determined with some expermentation but is by no means
1319 // optimal (especially for code size consideration). LEA is nice because of
1320 // its three-address nature. Tweak the cost function again when we can run
1321 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001322 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001323 // For X86-64, we should always use lea to materialize RIP relative
1324 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001325 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001326 Complexity = 4;
1327 else
1328 Complexity += 2;
1329 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001330
Gabor Greifba36cb52008-08-28 21:40:38 +00001331 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001332 Complexity++;
1333
Chris Lattner25142782009-07-11 22:50:33 +00001334 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001335 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001336 return false;
1337
1338 SDValue Segment;
1339 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1340 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001341}
1342
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001343/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1344bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1345 SDValue &Scale, SDValue &Index,
1346 SDValue &Disp) {
1347 assert(Op.getOpcode() == X86ISD::TLSADDR);
1348 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1349 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1350
1351 X86ISelAddressMode AM;
1352 AM.GV = GA->getGlobal();
1353 AM.Disp += GA->getOffset();
1354 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001355 AM.SymbolFlags = GA->getTargetFlags();
1356
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001357 if (N.getValueType() == MVT::i32) {
1358 AM.Scale = 1;
1359 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1360 } else {
1361 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1362 }
1363
1364 SDValue Segment;
1365 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1366 return true;
1367}
1368
1369
Dan Gohman475871a2008-07-27 21:46:04 +00001370bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1371 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001372 SDValue &Index, SDValue &Disp,
1373 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001374 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001375 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001376 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001377 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001378 return false;
1379}
1380
Dan Gohman8b746962008-09-23 18:22:58 +00001381/// getGlobalBaseReg - Return an SDNode that returns the value of
1382/// the global base register. Output instructions required to
1383/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001384///
Evan Cheng9ade2182006-08-26 05:34:46 +00001385SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001386 MachineFunction *MF = CurBB->getParent();
Dan Gohmanc5534622009-06-03 20:20:00 +00001387 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001388 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001389}
1390
Evan Chengb245d922006-05-20 01:36:52 +00001391static SDNode *FindCallStartFromCall(SDNode *Node) {
1392 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1393 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1394 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001395 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001396}
1397
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001398SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1399 SDValue Chain = Node->getOperand(0);
1400 SDValue In1 = Node->getOperand(1);
1401 SDValue In2L = Node->getOperand(2);
1402 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001403 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1404 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001405 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001406 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001407 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001408 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1409 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001410 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001411}
Christopher Lambc59e5212007-08-10 21:48:46 +00001412
Dan Gohman475871a2008-07-27 21:46:04 +00001413SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001414 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001415 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001416 unsigned Opc, MOpc;
1417 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001418 DebugLoc dl = Node->getDebugLoc();
1419
Evan Chengf597dc72006-02-10 22:24:32 +00001420#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001421 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001422 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001423 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001424 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001425#endif
1426
Dan Gohmane8be6c62008-07-17 19:10:17 +00001427 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001428#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001429 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001430 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001431 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001432 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001433#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001434 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001435 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001436
Evan Cheng0114e942006-01-06 20:36:21 +00001437 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001438 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001439 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001440 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001441
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001442 case X86ISD::ATOMOR64_DAG:
1443 return SelectAtomic64(Node, X86::ATOMOR6432);
1444 case X86ISD::ATOMXOR64_DAG:
1445 return SelectAtomic64(Node, X86::ATOMXOR6432);
1446 case X86ISD::ATOMADD64_DAG:
1447 return SelectAtomic64(Node, X86::ATOMADD6432);
1448 case X86ISD::ATOMSUB64_DAG:
1449 return SelectAtomic64(Node, X86::ATOMSUB6432);
1450 case X86ISD::ATOMNAND64_DAG:
1451 return SelectAtomic64(Node, X86::ATOMNAND6432);
1452 case X86ISD::ATOMAND64_DAG:
1453 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001454 case X86ISD::ATOMSWAP64_DAG:
1455 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001456
Dan Gohman525178c2007-10-08 18:33:35 +00001457 case ISD::SMUL_LOHI:
1458 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001459 SDValue N0 = Node->getOperand(0);
1460 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001461
Dan Gohman525178c2007-10-08 18:33:35 +00001462 bool isSigned = Opcode == ISD::SMUL_LOHI;
1463 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001464 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001465 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001466 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1467 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1468 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001469 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001470 }
1471 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001472 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001473 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001474 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1475 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1476 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001477 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001478 }
1479
1480 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001481 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001482 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001483 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1484 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1485 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001486 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001487 }
1488
Rafael Espindola094fad32009-04-08 21:14:34 +00001489 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1490 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001491 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001492 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001493 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001494 if (foldedLoad)
1495 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001496 }
1497
Dale Johannesendd64c412009-02-04 00:33:20 +00001498 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001499 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001500
1501 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001502 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1503 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001504 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001505 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001506 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001507 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001508 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001509 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001510 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001511 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001512 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001513 }
1514
Dan Gohman525178c2007-10-08 18:33:35 +00001515 // Copy the low half of the result, if it is needed.
1516 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001517 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001518 LoReg, NVT, InFlag);
1519 InFlag = Result.getValue(2);
1520 ReplaceUses(N.getValue(0), Result);
1521#ifndef NDEBUG
1522 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001523 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001524 DOUT << "\n";
1525#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001526 }
Dan Gohman525178c2007-10-08 18:33:35 +00001527 // Copy the high half of the result, if it is needed.
1528 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001529 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001530 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1531 // Prevent use of AH in a REX instruction by referencing AX instead.
1532 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001533 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001534 X86::AX, MVT::i16, InFlag);
1535 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001536 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1537 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001538 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001539 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001540 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001541 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001542 MVT::i8, Result, SRIdx), 0);
1543 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001544 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001545 HiReg, NVT, InFlag);
1546 InFlag = Result.getValue(2);
1547 }
1548 ReplaceUses(N.getValue(1), Result);
1549#ifndef NDEBUG
1550 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001551 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001552 DOUT << "\n";
1553#endif
1554 }
Evan Cheng34167212006-02-09 00:37:58 +00001555
Evan Chengf597dc72006-02-10 22:24:32 +00001556#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001557 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001558#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001559
Evan Cheng64a752f2006-08-11 09:08:15 +00001560 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001561 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001562
Dan Gohman525178c2007-10-08 18:33:35 +00001563 case ISD::SDIVREM:
1564 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001565 SDValue N0 = Node->getOperand(0);
1566 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001567
1568 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001569 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001570 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001571 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001572 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1573 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1574 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001575 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001576 }
1577 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001578 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001579 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001580 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1581 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1582 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001583 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001584 }
1585
1586 unsigned LoReg, HiReg;
1587 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001588 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001589 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001590 case MVT::i8:
1591 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001592 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001593 SExtOpcode = X86::CBW;
1594 break;
1595 case MVT::i16:
1596 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001597 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001598 SExtOpcode = X86::CWD;
1599 break;
1600 case MVT::i32:
1601 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001602 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001603 SExtOpcode = X86::CDQ;
1604 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001605 case MVT::i64:
1606 LoReg = X86::RAX; HiReg = X86::RDX;
Bill Wendling69600ca2009-07-12 02:49:22 +00001607 ClrOpcode = X86::MOV64r0;
Evan Cheng25ab6902006-09-08 06:48:29 +00001608 SExtOpcode = X86::CQO;
1609 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001610 }
1611
Rafael Espindola094fad32009-04-08 21:14:34 +00001612 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1613 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001614 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001615
Dan Gohman475871a2008-07-27 21:46:04 +00001616 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001617 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001618 // Special case for div8, just use a move with zero extension to AX to
1619 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001620 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1621 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1622 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001623 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001624 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001625 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001626 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001627 Chain = Move.getValue(1);
1628 ReplaceUses(N0.getValue(1), Chain);
1629 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001630 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001631 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001632 Chain = CurDAG->getEntryNode();
1633 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001634 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001635 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001636 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001637 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001638 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001639 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001640 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001641 // Sign extend the low part into the high part.
1642 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001643 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001644 } else {
1645 // Zero out the high part, effectively zero extending the input.
Bill Wendling69600ca2009-07-12 02:49:22 +00001646 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1647 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00001648 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001649 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001650 }
Evan Cheng948f3432006-01-06 23:19:29 +00001651 }
1652
1653 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001654 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1655 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001656 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001657 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001658 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001659 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001660 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001661 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001662 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001663 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001664 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001665 }
1666
Dan Gohmana37c9f72007-09-25 18:23:27 +00001667 // Copy the division (low) result, if it is needed.
1668 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001669 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001670 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001671 InFlag = Result.getValue(2);
1672 ReplaceUses(N.getValue(0), Result);
1673#ifndef NDEBUG
1674 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001675 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001676 DOUT << "\n";
1677#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001678 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001679 // Copy the remainder (high) result, if it is needed.
1680 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001681 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001682 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1683 // Prevent use of AH in a REX instruction by referencing AX instead.
1684 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001685 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001686 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001687 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001688 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1689 Result,
1690 CurDAG->getTargetConstant(8, MVT::i8)),
1691 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001692 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001693 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001694 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001695 MVT::i8, Result, SRIdx), 0);
1696 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001697 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001698 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001699 InFlag = Result.getValue(2);
1700 }
1701 ReplaceUses(N.getValue(1), Result);
1702#ifndef NDEBUG
1703 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001704 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001705 DOUT << "\n";
1706#endif
1707 }
Evan Chengf597dc72006-02-10 22:24:32 +00001708
1709#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001710 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001711#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001712
1713 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001714 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001715
Evan Cheng851bc042008-06-17 02:01:22 +00001716 case ISD::DECLARE: {
1717 // Handle DECLARE nodes here because the second operand may have been
1718 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001719 SDValue Chain = Node->getOperand(0);
1720 SDValue N1 = Node->getOperand(1);
1721 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001722 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001723
1724 // FIXME: We need to handle this for VLAs.
1725 if (!FINode) {
1726 ReplaceUses(N.getValue(0), Chain);
1727 return NULL;
1728 }
1729
Evan Chengfab83872008-06-18 02:48:27 +00001730 if (N2.getOpcode() == ISD::ADD &&
1731 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1732 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001733
1734 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1735 // somehow, just ignore it.
Chris Lattner18c59872009-06-27 04:16:01 +00001736 if (N2.getOpcode() != X86ISD::Wrapper &&
1737 N2.getOpcode() != X86ISD::WrapperRIP) {
Chris Lattner1823c922009-02-12 17:33:11 +00001738 ReplaceUses(N.getValue(0), Chain);
1739 return NULL;
1740 }
Evan Chengf2accb52009-01-10 03:33:22 +00001741 GlobalAddressSDNode *GVNode =
1742 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001743 if (GVNode == 0) {
1744 ReplaceUses(N.getValue(0), Chain);
1745 return NULL;
1746 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001747 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1748 TLI.getPointerTy());
1749 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1750 TLI.getPointerTy());
1751 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001752 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001753 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001754 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001755 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001756 }
1757
Evan Cheng9ade2182006-08-26 05:34:46 +00001758 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001759
Evan Chengf597dc72006-02-10 22:24:32 +00001760#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001761 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001762 if (ResNode == NULL || ResNode == N.getNode())
1763 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001764 else
1765 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001766 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001767 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001768#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001769
1770 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001771}
1772
Chris Lattnerc0bad572006-06-08 18:03:49 +00001773bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001774SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001775 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001776 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001777 switch (ConstraintCode) {
1778 case 'o': // offsetable ??
1779 case 'v': // not offsetable ??
1780 default: return true;
1781 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001782 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001783 return true;
1784 break;
1785 }
1786
Evan Cheng04699902006-08-26 01:05:16 +00001787 OutOps.push_back(Op0);
1788 OutOps.push_back(Op1);
1789 OutOps.push_back(Op2);
1790 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001791 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001792 return false;
1793}
1794
Chris Lattnerc961eea2005-11-16 01:54:32 +00001795/// createX86ISelDag - This pass converts a legalized DAG into a
1796/// X86-specific DAG, ready for instruction scheduling.
1797///
Bill Wendling98a366d2009-04-29 23:29:43 +00001798FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1799 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001800 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001801}