blob: 1336177de249f07fb3d3c8c8a778f81e54235f9c [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"
38#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Evan Cheng4d952322009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Chris Lattner95b2c7d2006-12-19 22:59:26 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
68 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000069 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000070 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000071 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000072 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000074 const char *ES;
75 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000076 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000077 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078
79 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000080 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattnerb8afeb92009-06-26 05:51:45 +000081 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0), SymbolFlags(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000082 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000083
84 bool hasSymbolicDisplacement() const {
85 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
86 }
Chris Lattner18c59872009-06-27 04:16:01 +000087
88 bool hasBaseOrIndexReg() const {
89 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
90 }
91
92 /// isRIPRelative - Return true if this addressing mode is already RIP
93 /// relative.
94 bool isRIPRelative() const {
95 if (BaseType != RegBase) return false;
96 if (RegisterSDNode *RegNode =
97 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
98 return RegNode->getReg() == X86::RIP;
99 return false;
100 }
101
102 void setBaseReg(SDValue Reg) {
103 BaseType = RegBase;
104 Base.Reg = Reg;
105 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000106
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000107 void dump() {
108 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000109 cerr << "Base.Reg ";
110 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
111 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000112 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
Chris Lattner18c59872009-06-27 04:16:01 +0000113 cerr << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000114 cerr << "IndexReg ";
115 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
116 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000117 cerr << " Disp " << Disp << "\n";
118 cerr << "GV "; if (GV) GV->dump();
119 else cerr << "nul";
120 cerr << " CP "; if (CP) CP->dump();
121 else cerr << "nul";
122 cerr << "\n";
123 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
124 cerr << " JT" << JT << " Align" << Align << "\n";
125 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000126 };
127}
128
129namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000130 //===--------------------------------------------------------------------===//
131 /// ISel - X86 specific code to select X86 machine instructions for
132 /// SelectionDAG operations.
133 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000134 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000135 /// X86Lowering - This object fully describes how to lower LLVM code to an
136 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000137 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000138
139 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
140 /// make the right decision when generating code for different targets.
141 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000142
Evan Chengdb8d56b2008-06-30 20:45:06 +0000143 /// CurBB - Current BB being isel'd.
144 ///
145 MachineBasicBlock *CurBB;
146
Evan Chengb7a75a52008-09-26 23:41:32 +0000147 /// OptForSize - If true, selector should try to optimize for code size
148 /// instead of performance.
149 bool OptForSize;
150
Chris Lattnerc961eea2005-11-16 01:54:32 +0000151 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000152 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000153 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000154 X86Lowering(*tm.getTargetLowering()),
155 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000156 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000157
158 virtual const char *getPassName() const {
159 return "X86 DAG->DAG Instruction Selection";
160 }
161
Evan Chengdb8d56b2008-06-30 20:45:06 +0000162 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000163 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000164 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000165
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000166 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
167
Evan Cheng884c70c2008-11-27 00:49:46 +0000168 virtual
169 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000170
Chris Lattnerc961eea2005-11-16 01:54:32 +0000171// Include the pieces autogenerated from the target description.
172#include "X86GenDAGISel.inc"
173
174 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000175 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000176 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000177
Rafael Espindola094fad32009-04-08 21:14:34 +0000178 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
179 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000180 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000181 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000182 unsigned Depth = 0);
183 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000184 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000185 SDValue &Scale, SDValue &Index, SDValue &Disp,
186 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000187 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
188 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000189 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
190 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000191 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
192 SDValue N, SDValue &Base, SDValue &Scale,
193 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000194 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000195 SDValue &InChain, SDValue &OutChain);
196 bool TryFoldLoad(SDValue P, SDValue N,
197 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000198 SDValue &Index, SDValue &Disp,
199 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000200 void PreprocessForRMW();
201 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000202
Chris Lattnerc0bad572006-06-08 18:03:49 +0000203 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
204 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000205 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000206 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000207 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000208
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000209 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
210
Dan Gohman475871a2008-07-27 21:46:04 +0000211 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
212 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000213 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000214 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
216 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000217 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000218 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 // These are 32-bit even in 64-bit mode since RIP relative offset
220 // is 32-bit.
221 if (AM.GV)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000222 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
223 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000225 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000226 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 else if (AM.ES)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000228 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 else if (AM.JT != -1)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000230 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000232 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000233
234 if (AM.Segment.getNode())
235 Segment = AM.Segment;
236 else
237 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000238 }
239
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000240 /// getI8Imm - Return a target constant with the specified value, of type
241 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000242 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000243 return CurDAG->getTargetConstant(Imm, MVT::i8);
244 }
245
Chris Lattnerc961eea2005-11-16 01:54:32 +0000246 /// getI16Imm - Return a target constant with the specified value, of type
247 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000248 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000249 return CurDAG->getTargetConstant(Imm, MVT::i16);
250 }
251
252 /// getI32Imm - Return a target constant with the specified value, of type
253 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000254 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000255 return CurDAG->getTargetConstant(Imm, MVT::i32);
256 }
Evan Chengf597dc72006-02-10 22:24:32 +0000257
Dan Gohman8b746962008-09-23 18:22:58 +0000258 /// getGlobalBaseReg - Return an SDNode that returns the value of
259 /// the global base register. Output instructions required to
260 /// initialize the global base register, if necessary.
261 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000262 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000263
Dan Gohmanc5534622009-06-03 20:20:00 +0000264 /// getTargetMachine - Return a reference to the TargetMachine, casted
265 /// to the target-specific type.
266 const X86TargetMachine &getTargetMachine() {
267 return static_cast<const X86TargetMachine &>(TM);
268 }
269
270 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
271 /// to the target-specific type.
272 const X86InstrInfo *getInstrInfo() {
273 return getTargetMachine().getInstrInfo();
274 }
275
Evan Cheng23addc02006-02-10 22:46:26 +0000276#ifndef NDEBUG
277 unsigned Indent;
278#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000279 };
280}
281
Evan Chengf4b4c412006-08-08 00:31:00 +0000282
Evan Cheng884c70c2008-11-27 00:49:46 +0000283bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
284 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000285 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000286
Evan Cheng884c70c2008-11-27 00:49:46 +0000287 if (U == Root)
288 switch (U->getOpcode()) {
289 default: break;
290 case ISD::ADD:
291 case ISD::ADDC:
292 case ISD::ADDE:
293 case ISD::AND:
294 case ISD::OR:
295 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000296 SDValue Op1 = U->getOperand(1);
297
Evan Cheng884c70c2008-11-27 00:49:46 +0000298 // If the other operand is a 8-bit immediate we should fold the immediate
299 // instead. This reduces code size.
300 // e.g.
301 // movl 4(%esp), %eax
302 // addl $4, %eax
303 // vs.
304 // movl $4, %eax
305 // addl 4(%esp), %eax
306 // The former is 2 bytes shorter. In case where the increment is 1, then
307 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000308 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000309 if (Imm->getAPIntValue().isSignedIntN(8))
310 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000311
312 // If the other operand is a TLS address, we should fold it instead.
313 // This produces
314 // movl %gs:0, %eax
315 // leal i@NTPOFF(%eax), %eax
316 // instead of
317 // movl $i@NTPOFF, %eax
318 // addl %gs:0, %eax
319 // if the block also has an access to a second TLS address this will save
320 // a load.
321 // FIXME: This is probably also true for non TLS addresses.
322 if (Op1.getOpcode() == X86ISD::Wrapper) {
323 SDValue Val = Op1.getOperand(0);
324 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
325 return false;
326 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000327 }
328 }
329
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000330 // Proceed to 'generic' cycle finder code
331 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000332}
333
Evan Cheng70e674e2006-08-28 20:10:17 +0000334/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
335/// and move load below the TokenFactor. Replace store's chain operand with
336/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000337static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000338 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000339 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000340 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
341 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000342 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000343 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000344 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000345 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
346 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
347 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
348 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000349}
350
Evan Chengcd0baf22008-05-23 21:23:16 +0000351/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
352///
Dan Gohman475871a2008-07-27 21:46:04 +0000353static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
354 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000355 if (N.getOpcode() == ISD::BIT_CONVERT)
356 N = N.getOperand(0);
357
358 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
359 if (!LD || LD->isVolatile())
360 return false;
361 if (LD->getAddressingMode() != ISD::UNINDEXED)
362 return false;
363
364 ISD::LoadExtType ExtType = LD->getExtensionType();
365 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
366 return false;
367
368 if (N.hasOneUse() &&
369 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000370 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000371 Load = N;
372 return true;
373 }
374 return false;
375}
376
Evan Chengab6c3bb2008-08-25 21:27:18 +0000377/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
378/// operand and move load below the call's chain operand.
379static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000380 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000381 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000382 SDValue Chain = CallSeqStart.getOperand(0);
383 if (Chain.getNode() == Load.getNode())
384 Ops.push_back(Load.getOperand(0));
385 else {
386 assert(Chain.getOpcode() == ISD::TokenFactor &&
387 "Unexpected CallSeqStart chain operand");
388 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
389 if (Chain.getOperand(i).getNode() == Load.getNode())
390 Ops.push_back(Load.getOperand(0));
391 else
392 Ops.push_back(Chain.getOperand(i));
393 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000394 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
395 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000396 Ops.clear();
397 Ops.push_back(NewChain);
398 }
399 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
400 Ops.push_back(CallSeqStart.getOperand(i));
401 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000402 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
403 Load.getOperand(1), Load.getOperand(2));
404 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000405 Ops.push_back(SDValue(Load.getNode(), 1));
406 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000407 Ops.push_back(Call.getOperand(i));
408 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
409}
410
411/// isCalleeLoad - Return true if call address is a load and it can be
412/// moved below CALLSEQ_START and the chains leading up to the call.
413/// Return the CALLSEQ_START by reference as a second output.
414static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000415 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000416 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000417 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000418 if (!LD ||
419 LD->isVolatile() ||
420 LD->getAddressingMode() != ISD::UNINDEXED ||
421 LD->getExtensionType() != ISD::NON_EXTLOAD)
422 return false;
423
424 // Now let's find the callseq_start.
425 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
426 if (!Chain.hasOneUse())
427 return false;
428 Chain = Chain.getOperand(0);
429 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000430
431 if (Chain.getOperand(0).getNode() == Callee.getNode())
432 return true;
433 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
434 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
435 return true;
436 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000437}
438
439
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000440/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000441/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000442/// This allows the instruction selector to pick more read-modify-write
443/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000444///
445/// [Load chain]
446/// ^
447/// |
448/// [Load]
449/// ^ ^
450/// | |
451/// / \-
452/// / |
453/// [TokenFactor] [Op]
454/// ^ ^
455/// | |
456/// \ /
457/// \ /
458/// [Store]
459///
460/// The fact the store's chain operand != load's chain will prevent the
461/// (store (op (load))) instruction from being selected. We can transform it to:
462///
463/// [Load chain]
464/// ^
465/// |
466/// [TokenFactor]
467/// ^
468/// |
469/// [Load]
470/// ^ ^
471/// | |
472/// | \-
473/// | |
474/// | [Op]
475/// | ^
476/// | |
477/// \ /
478/// \ /
479/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000480void X86DAGToDAGISel::PreprocessForRMW() {
481 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
482 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000483 if (I->getOpcode() == X86ISD::CALL) {
484 /// Also try moving call address load from outside callseq_start to just
485 /// before the call to allow it to be folded.
486 ///
487 /// [Load chain]
488 /// ^
489 /// |
490 /// [Load]
491 /// ^ ^
492 /// | |
493 /// / \--
494 /// / |
495 ///[CALLSEQ_START] |
496 /// ^ |
497 /// | |
498 /// [LOAD/C2Reg] |
499 /// | |
500 /// \ /
501 /// \ /
502 /// [CALL]
503 SDValue Chain = I->getOperand(0);
504 SDValue Load = I->getOperand(1);
505 if (!isCalleeLoad(Load, Chain))
506 continue;
507 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
508 ++NumLoadMoved;
509 continue;
510 }
511
Evan Cheng8b2794a2006-10-13 21:14:26 +0000512 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000513 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000514 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000515
Gabor Greifba36cb52008-08-28 21:40:38 +0000516 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000517 continue;
518
Dan Gohman475871a2008-07-27 21:46:04 +0000519 SDValue N1 = I->getOperand(1);
520 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000521 if ((N1.getValueType().isFloatingPoint() &&
522 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000523 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000524 continue;
525
526 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000527 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000528 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000529 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000530 case ISD::ADD:
531 case ISD::MUL:
532 case ISD::AND:
533 case ISD::OR:
534 case ISD::XOR:
535 case ISD::ADDC:
536 case ISD::ADDE:
537 case ISD::VECTOR_SHUFFLE: {
538 SDValue N10 = N1.getOperand(0);
539 SDValue N11 = N1.getOperand(1);
540 RModW = isRMWLoad(N10, Chain, N2, Load);
541 if (!RModW)
542 RModW = isRMWLoad(N11, Chain, N2, Load);
543 break;
544 }
545 case ISD::SUB:
546 case ISD::SHL:
547 case ISD::SRA:
548 case ISD::SRL:
549 case ISD::ROTL:
550 case ISD::ROTR:
551 case ISD::SUBC:
552 case ISD::SUBE:
553 case X86ISD::SHLD:
554 case X86ISD::SHRD: {
555 SDValue N10 = N1.getOperand(0);
556 RModW = isRMWLoad(N10, Chain, N2, Load);
557 break;
558 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000559 }
560
Evan Cheng82a35b32006-08-29 06:44:17 +0000561 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000562 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000563 ++NumLoadMoved;
564 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000565 }
566}
567
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000568
569/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
570/// nodes that target the FP stack to be store and load to the stack. This is a
571/// gross hack. We would like to simply mark these as being illegal, but when
572/// we do that, legalize produces these when it expands calls, then expands
573/// these in the same legalize pass. We would like dag combine to be able to
574/// hack on these between the call expansion and the node legalization. As such
575/// this pass basically does "really late" legalization of these inline with the
576/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000577void X86DAGToDAGISel::PreprocessForFPConvert() {
578 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
579 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000580 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
581 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
582 continue;
583
584 // If the source and destination are SSE registers, then this is a legal
585 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000586 MVT SrcVT = N->getOperand(0).getValueType();
587 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000588 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
589 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
590 if (SrcIsSSE && DstIsSSE)
591 continue;
592
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000593 if (!SrcIsSSE && !DstIsSSE) {
594 // If this is an FPStack extension, it is a noop.
595 if (N->getOpcode() == ISD::FP_EXTEND)
596 continue;
597 // If this is a value-preserving FPStack truncation, it is a noop.
598 if (N->getConstantOperandVal(1))
599 continue;
600 }
601
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000602 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
603 // FPStack has extload and truncstore. SSE can fold direct loads into other
604 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000605 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000606 if (N->getOpcode() == ISD::FP_ROUND)
607 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
608 else
609 MemVT = SrcIsSSE ? SrcVT : DstVT;
610
Dan Gohmanf350b272008-08-23 02:25:05 +0000611 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000612 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000613
614 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000615 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000616 N->getOperand(0),
617 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000618 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000619 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000620
621 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
622 // extload we created. This will cause general havok on the dag because
623 // anything below the conversion could be folded into other existing nodes.
624 // To avoid invalidating 'I', back it up to the convert node.
625 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000626 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000627
628 // Now that we did that, the node is dead. Increment the iterator to the
629 // next node to process, then delete N.
630 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000631 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000632 }
633}
634
Chris Lattnerc961eea2005-11-16 01:54:32 +0000635/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
636/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000637void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000638 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000639 const Function *F = CurDAG->getMachineFunction().getFunction();
640 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000641
Evan Chengdb8d56b2008-06-30 20:45:06 +0000642 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000643 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000644 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000645
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000646 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000647 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000648
Chris Lattnerc961eea2005-11-16 01:54:32 +0000649 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000650#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000651 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000652 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000653#endif
David Greene8ad4c002008-10-27 21:56:29 +0000654 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000655#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000656 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000657#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000658
Dan Gohmanf350b272008-08-23 02:25:05 +0000659 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000660}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000661
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000662/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
663/// the main function.
664void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
665 MachineFrameInfo *MFI) {
666 const TargetInstrInfo *TII = TM.getInstrInfo();
667 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000668 BuildMI(BB, DebugLoc::getUnknownLoc(),
669 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000670}
671
672void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
673 // If this is main, emit special code for main.
674 MachineBasicBlock *BB = MF.begin();
675 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
676 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
677}
678
Rafael Espindola094fad32009-04-08 21:14:34 +0000679
680bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
681 X86ISelAddressMode &AM) {
682 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
683 SDValue Segment = N.getOperand(0);
684
685 if (AM.Segment.getNode() == 0) {
686 AM.Segment = Segment;
687 return false;
688 }
689
690 return true;
691}
692
693bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
694 // This optimization is valid because the GNU TLS model defines that
695 // gs:0 (or fs:0 on X86-64) contains its own address.
696 // For more information see http://people.redhat.com/drepper/tls.pdf
697
698 SDValue Address = N.getOperand(1);
699 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
700 !MatchSegmentBaseAddress (Address, AM))
701 return false;
702
703 return true;
704}
705
Chris Lattner18c59872009-06-27 04:16:01 +0000706/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
707/// into an addressing mode. These wrap things that will resolve down into a
708/// symbol reference. If no match is possible, this returns true, otherwise it
709/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000710bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000711 // If the addressing mode already has a symbol as the displacement, we can
712 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000713 if (AM.hasSymbolicDisplacement())
714 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000715
716 SDValue N0 = N.getOperand(0);
Chris Lattner18c59872009-06-27 04:16:01 +0000717
718 // Handle X86-64 rip-relative addresses. We check this before checking direct
719 // folding because RIP is preferable to non-RIP accesses.
720 if (Subtarget->is64Bit() &&
721 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
722 // they cannot be folded into immediate fields.
723 // FIXME: This can be improved for kernel and other models?
724 TM.getCodeModel() == CodeModel::Small &&
725
726 // Base and index reg must be 0 in order to use %rip as base and lowering
727 // must allow RIP.
728 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
729
730 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
731 int64_t Offset = AM.Disp + G->getOffset();
732 if (!isInt32(Offset)) return true;
733 AM.GV = G->getGlobal();
734 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000735 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000736 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
737 int64_t Offset = AM.Disp + CP->getOffset();
738 if (!isInt32(Offset)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000739 AM.CP = CP->getConstVal();
740 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000741 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000742 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000743 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
744 AM.ES = S->getSymbol();
745 AM.SymbolFlags = S->getTargetFlags();
746 } else {
747 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
748 AM.JT = J->getIndex();
749 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000750 }
Chris Lattner18c59872009-06-27 04:16:01 +0000751
752 if (N.getOpcode() == X86ISD::WrapperRIP)
753 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000754 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000755 }
756
757 // Handle the case when globals fit in our immediate field: This is true for
758 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
759 // mode, this results in a non-RIP-relative computation.
760 if (!Subtarget->is64Bit() ||
761 (TM.getCodeModel() == CodeModel::Small &&
762 TM.getRelocationModel() == Reloc::Static)) {
763 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
764 AM.GV = G->getGlobal();
765 AM.Disp += G->getOffset();
766 AM.SymbolFlags = G->getTargetFlags();
767 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
768 AM.CP = CP->getConstVal();
769 AM.Align = CP->getAlignment();
770 AM.Disp += CP->getOffset();
771 AM.SymbolFlags = CP->getTargetFlags();
772 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
773 AM.ES = S->getSymbol();
774 AM.SymbolFlags = S->getTargetFlags();
775 } else {
776 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
777 AM.JT = J->getIndex();
778 AM.SymbolFlags = J->getTargetFlags();
779 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000780 return false;
781 }
782
783 return true;
784}
785
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000786/// MatchAddress - Add the specified node to the specified addressing mode,
787/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000788/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000789bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000790 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000791 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000792 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000793 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000794 // Limit recursion.
795 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000796 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000797
Chris Lattner18c59872009-06-27 04:16:01 +0000798 // If this is already a %rip relative address, we can only merge immediates
799 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000800 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000801 if (AM.isRIPRelative()) {
802 // FIXME: JumpTable and ExternalSymbol address currently don't like
803 // displacements. It isn't very important, but this should be fixed for
804 // consistency.
805 if (!AM.ES && AM.JT != -1) return true;
806
807 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
808 int64_t Val = AM.Disp + Cst->getSExtValue();
809 if (isInt32(Val)) {
810 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000811 return false;
812 }
813 }
814 return true;
815 }
816
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000817 switch (N.getOpcode()) {
818 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000819 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000820 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000821 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000822 AM.Disp += Val;
823 return false;
824 }
825 break;
826 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000827
Rafael Espindola094fad32009-04-08 21:14:34 +0000828 case X86ISD::SegmentBaseAddress:
829 if (!MatchSegmentBaseAddress(N, AM))
830 return false;
831 break;
832
Rafael Espindola49a168d2009-04-12 21:55:03 +0000833 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000834 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000835 if (!MatchWrapper(N, AM))
836 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000837 break;
838
Rafael Espindola094fad32009-04-08 21:14:34 +0000839 case ISD::LOAD:
840 if (!MatchLoad(N, AM))
841 return false;
842 break;
843
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000844 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000845 if (AM.BaseType == X86ISelAddressMode::RegBase
846 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000847 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
848 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
849 return false;
850 }
851 break;
Evan Chengec693f72005-12-08 02:01:35 +0000852
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000853 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000854 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000855 break;
856
Gabor Greif93c53e52008-08-31 15:37:04 +0000857 if (ConstantSDNode
858 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000859 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000860 if (Val == 1 || Val == 2 || Val == 3) {
861 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000862 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000863
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000864 // Okay, we know that we have a scale by now. However, if the scaled
865 // value is an add of something and a constant, we can fold the
866 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000867 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
868 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
869 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000870 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000871 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000872 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000873 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000874 AM.Disp = Disp;
875 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000876 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000877 } else {
878 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000879 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000880 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000881 }
882 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000883 }
Evan Chengec693f72005-12-08 02:01:35 +0000884
Dan Gohman83688052007-10-22 20:22:24 +0000885 case ISD::SMUL_LOHI:
886 case ISD::UMUL_LOHI:
887 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000888 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000889 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000890 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000891 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000892 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000893 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000894 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000895 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000896 if (ConstantSDNode
897 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000898 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
899 CN->getZExtValue() == 9) {
900 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000901
Gabor Greifba36cb52008-08-28 21:40:38 +0000902 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000903 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000904
905 // Okay, we know that we have a scale by now. However, if the scaled
906 // value is an add of something and a constant, we can fold the
907 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000908 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
909 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
910 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000911 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000912 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000913 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000914 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000915 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000916 AM.Disp = Disp;
917 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000918 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000919 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000920 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000921 }
922
923 AM.IndexReg = AM.Base.Reg = Reg;
924 return false;
925 }
Chris Lattner62412262007-02-04 20:18:17 +0000926 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000927 break;
928
Dan Gohman3cd90a12009-05-11 18:02:53 +0000929 case ISD::SUB: {
930 // Given A-B, if A can be completely folded into the address and
931 // the index field with the index field unused, use -B as the index.
932 // This is a win if a has multiple parts that can be folded into
933 // the address. Also, this saves a mov if the base register has
934 // other uses, since it avoids a two-address sub instruction, however
935 // it costs an additional mov if the index register has other uses.
936
937 // Test if the LHS of the sub can be folded.
938 X86ISelAddressMode Backup = AM;
939 if (MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) {
940 AM = Backup;
941 break;
942 }
943 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000944 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000945 AM = Backup;
946 break;
947 }
948 int Cost = 0;
949 SDValue RHS = N.getNode()->getOperand(1);
950 // If the RHS involves a register with multiple uses, this
951 // transformation incurs an extra mov, due to the neg instruction
952 // clobbering its operand.
953 if (!RHS.getNode()->hasOneUse() ||
954 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
955 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
956 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
957 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
958 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
959 ++Cost;
960 // If the base is a register with multiple uses, this
961 // transformation may save a mov.
962 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
963 AM.Base.Reg.getNode() &&
964 !AM.Base.Reg.getNode()->hasOneUse()) ||
965 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
966 --Cost;
967 // If the folded LHS was interesting, this transformation saves
968 // address arithmetic.
969 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
970 ((AM.Disp != 0) && (Backup.Disp == 0)) +
971 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
972 --Cost;
973 // If it doesn't look like it may be an overall win, don't do it.
974 if (Cost >= 0) {
975 AM = Backup;
976 break;
977 }
978
979 // Ok, the transformation is legal and appears profitable. Go for it.
980 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
981 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
982 AM.IndexReg = Neg;
983 AM.Scale = 1;
984
985 // Insert the new nodes into the topological ordering.
986 if (Zero.getNode()->getNodeId() == -1 ||
987 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
988 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
989 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
990 }
991 if (Neg.getNode()->getNodeId() == -1 ||
992 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
993 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
994 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
995 }
996 return false;
997 }
998
Evan Cheng8e278262009-01-17 07:09:27 +0000999 case ISD::ADD: {
1000 X86ISelAddressMode Backup = AM;
Rafael Espindola523249f2009-03-31 16:16:57 +00001001 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
1002 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001003 return false;
1004 AM = Backup;
Rafael Espindola523249f2009-03-31 16:16:57 +00001005 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
1006 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001007 return false;
1008 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001009
1010 // If we couldn't fold both operands into the address at the same time,
1011 // see if we can just put each operand into a register and fold at least
1012 // the add.
1013 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1014 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001015 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001016 AM.Base.Reg = N.getNode()->getOperand(0);
1017 AM.IndexReg = N.getNode()->getOperand(1);
1018 AM.Scale = 1;
1019 return false;
1020 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001021 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001022 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001023
Chris Lattner62412262007-02-04 20:18:17 +00001024 case ISD::OR:
1025 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001026 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1027 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001028 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001029 // Start with the LHS as an addr mode.
Rafael Espindola523249f2009-03-31 16:16:57 +00001030 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001031 // Address could not have picked a GV address for the displacement.
1032 AM.GV == NULL &&
1033 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +00001034 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001035 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001036 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001037 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001038 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001039 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001040 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001041 }
1042 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001043
1044 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001045 // Perform some heroic transforms on an and of a constant-count shift
1046 // with a constant to enable use of the scaled offset field.
1047
Dan Gohman475871a2008-07-27 21:46:04 +00001048 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001049 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001050
Evan Cheng1314b002007-12-13 00:43:27 +00001051 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001052 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001053
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001054 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001055 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1056 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1057 if (!C1 || !C2) break;
1058
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001059 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1060 // allows us to convert the shift and and into an h-register extract and
1061 // a scaled index.
1062 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1063 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001064 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001065 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1066 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1067 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1068 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1069 X, Eight);
1070 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1071 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +00001072 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1073 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1074 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001075
1076 // Insert the new nodes into the topological ordering.
1077 if (Eight.getNode()->getNodeId() == -1 ||
1078 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1079 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1080 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1081 }
1082 if (Mask.getNode()->getNodeId() == -1 ||
1083 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1084 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1085 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1086 }
1087 if (Srl.getNode()->getNodeId() == -1 ||
1088 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1089 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1090 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1091 }
1092 if (And.getNode()->getNodeId() == -1 ||
1093 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1094 CurDAG->RepositionNode(N.getNode(), And.getNode());
1095 And.getNode()->setNodeId(N.getNode()->getNodeId());
1096 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001097 if (ShlCount.getNode()->getNodeId() == -1 ||
1098 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1099 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1100 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1101 }
1102 if (Shl.getNode()->getNodeId() == -1 ||
1103 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1104 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1105 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1106 }
1107 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001108 AM.IndexReg = And;
1109 AM.Scale = (1 << ScaleLog);
1110 return false;
1111 }
1112 }
1113
1114 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1115 // allows us to fold the shift into this addressing mode.
1116 if (Shift.getOpcode() != ISD::SHL) break;
1117
Evan Cheng1314b002007-12-13 00:43:27 +00001118 // Not likely to be profitable if either the AND or SHIFT node has more
1119 // than one use (unless all uses are for address computation). Besides,
1120 // isel mechanism requires their node ids to be reused.
1121 if (!N.hasOneUse() || !Shift.hasOneUse())
1122 break;
1123
1124 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001125 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001126 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1127 break;
1128
1129 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001130 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001131 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001132 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1133 NewANDMask);
1134 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001135 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001136
1137 // Insert the new nodes into the topological ordering.
1138 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1139 CurDAG->RepositionNode(X.getNode(), C1);
1140 C1->setNodeId(X.getNode()->getNodeId());
1141 }
1142 if (NewANDMask.getNode()->getNodeId() == -1 ||
1143 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1144 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1145 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1146 }
1147 if (NewAND.getNode()->getNodeId() == -1 ||
1148 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1149 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1150 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1151 }
1152 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1153 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1154 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1155 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1156 }
1157
Dan Gohman7b8e9642008-10-13 20:52:04 +00001158 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001159
1160 AM.Scale = 1 << ShiftCst;
1161 AM.IndexReg = NewAND;
1162 return false;
1163 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001164 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001165
Rafael Espindola523249f2009-03-31 16:16:57 +00001166 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001167}
1168
1169/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1170/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001171bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001172 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001173 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001174 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001175 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001176 AM.IndexReg = N;
1177 AM.Scale = 1;
1178 return false;
1179 }
1180
1181 // Otherwise, we cannot select it.
1182 return true;
1183 }
1184
1185 // Default, generate it as a register.
1186 AM.BaseType = X86ISelAddressMode::RegBase;
1187 AM.Base.Reg = N;
1188 return false;
1189}
1190
Evan Chengec693f72005-12-08 02:01:35 +00001191/// SelectAddr - returns true if it is able pattern match an addressing mode.
1192/// It returns the operands which make up the maximal addressing mode it can
1193/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001194bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1195 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001196 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001197 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001198 bool Done = false;
1199 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1200 unsigned Opcode = N.getOpcode();
1201 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001202 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001203 // If we are able to fold N into addressing mode, then we'll allow it even
1204 // if N has multiple uses. In general, addressing computation is used as
1205 // addresses by all of its uses. But watch out for CopyToReg uses, that
1206 // means the address computation is liveout. It will be computed by a LEA
1207 // so we want to avoid computing the address twice.
1208 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1209 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1210 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001211 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001212 Done = true;
1213 break;
1214 }
1215 }
1216 }
1217 }
1218
1219 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001220 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001221
Duncan Sands83ec4b62008-06-06 12:08:01 +00001222 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001223 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001224 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001225 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001226 }
Evan Cheng8700e142006-01-11 06:09:51 +00001227
Gabor Greifba36cb52008-08-28 21:40:38 +00001228 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001229 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001230
Rafael Espindola094fad32009-04-08 21:14:34 +00001231 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001232 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001233}
1234
Chris Lattner3a7cd952006-10-07 21:55:32 +00001235/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1236/// match a load whose top elements are either undef or zeros. The load flavor
1237/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001238bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1239 SDValue N, SDValue &Base,
1240 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001241 SDValue &Disp, SDValue &Segment,
1242 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001243 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001244 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001245 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001246 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001247 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001248 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001249 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001250 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001251 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001252 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001253 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001254 return true;
1255 }
1256 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001257
1258 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001259 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001260 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001261 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001262 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001263 N.getOperand(0).getNode()->hasOneUse() &&
1264 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001265 N.getOperand(0).getOperand(0).hasOneUse()) {
1266 // Okay, this is a zero extending load. Fold it.
1267 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001268 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001269 return false;
1270 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001271 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001272 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001273 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001274 return false;
1275}
1276
1277
Evan Cheng51a9ed92006-02-25 10:09:08 +00001278/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1279/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001280bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1281 SDValue &Base, SDValue &Scale,
1282 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001283 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001284
1285 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1286 // segments.
1287 SDValue Copy = AM.Segment;
1288 SDValue T = CurDAG->getRegister(0, MVT::i32);
1289 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001290 if (MatchAddress(N, AM))
1291 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001292 assert (T == AM.Segment);
1293 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001294
Duncan Sands83ec4b62008-06-06 12:08:01 +00001295 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001296 unsigned Complexity = 0;
1297 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001298 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001299 Complexity = 1;
1300 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001301 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001302 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1303 Complexity = 4;
1304
Gabor Greifba36cb52008-08-28 21:40:38 +00001305 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001306 Complexity++;
1307 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001308 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001309
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001310 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1311 // a simple shift.
1312 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001313 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001314
1315 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1316 // to a LEA. This is determined with some expermentation but is by no means
1317 // optimal (especially for code size consideration). LEA is nice because of
1318 // its three-address nature. Tweak the cost function again when we can run
1319 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001320 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001321 // For X86-64, we should always use lea to materialize RIP relative
1322 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001323 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001324 Complexity = 4;
1325 else
1326 Complexity += 2;
1327 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001328
Gabor Greifba36cb52008-08-28 21:40:38 +00001329 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001330 Complexity++;
1331
1332 if (Complexity > 2) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001333 SDValue Segment;
1334 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001335 return true;
1336 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001337 return false;
1338}
1339
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001340/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1341bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1342 SDValue &Scale, SDValue &Index,
1343 SDValue &Disp) {
1344 assert(Op.getOpcode() == X86ISD::TLSADDR);
1345 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1346 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1347
1348 X86ISelAddressMode AM;
1349 AM.GV = GA->getGlobal();
1350 AM.Disp += GA->getOffset();
1351 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001352 AM.SymbolFlags = GA->getTargetFlags();
1353
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001354 if (N.getValueType() == MVT::i32) {
1355 AM.Scale = 1;
1356 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1357 } else {
1358 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1359 }
1360
1361 SDValue Segment;
1362 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1363 return true;
1364}
1365
1366
Dan Gohman475871a2008-07-27 21:46:04 +00001367bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1368 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001369 SDValue &Index, SDValue &Disp,
1370 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001371 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001372 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001373 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001374 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001375 return false;
1376}
1377
Dan Gohman8b746962008-09-23 18:22:58 +00001378/// getGlobalBaseReg - Return an SDNode that returns the value of
1379/// the global base register. Output instructions required to
1380/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001381///
Evan Cheng9ade2182006-08-26 05:34:46 +00001382SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001383 MachineFunction *MF = CurBB->getParent();
Dan Gohmanc5534622009-06-03 20:20:00 +00001384 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001385 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001386}
1387
Evan Chengb245d922006-05-20 01:36:52 +00001388static SDNode *FindCallStartFromCall(SDNode *Node) {
1389 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1390 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1391 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001392 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001393}
1394
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001395SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1396 SDValue Chain = Node->getOperand(0);
1397 SDValue In1 = Node->getOperand(1);
1398 SDValue In2L = Node->getOperand(2);
1399 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001400 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1401 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001402 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001403 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001404 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001405 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1406 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001407 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001408}
Christopher Lambc59e5212007-08-10 21:48:46 +00001409
Dan Gohman475871a2008-07-27 21:46:04 +00001410SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001411 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001412 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001413 unsigned Opc, MOpc;
1414 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001415 DebugLoc dl = Node->getDebugLoc();
1416
Evan Chengf597dc72006-02-10 22:24:32 +00001417#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001418 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001419 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001420 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001421 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001422#endif
1423
Dan Gohmane8be6c62008-07-17 19:10:17 +00001424 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001425#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001426 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001427 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001428 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001429 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001430#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001431 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001432 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001433
Evan Cheng0114e942006-01-06 20:36:21 +00001434 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001435 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001436 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001437 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001438
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001439 case X86ISD::ATOMOR64_DAG:
1440 return SelectAtomic64(Node, X86::ATOMOR6432);
1441 case X86ISD::ATOMXOR64_DAG:
1442 return SelectAtomic64(Node, X86::ATOMXOR6432);
1443 case X86ISD::ATOMADD64_DAG:
1444 return SelectAtomic64(Node, X86::ATOMADD6432);
1445 case X86ISD::ATOMSUB64_DAG:
1446 return SelectAtomic64(Node, X86::ATOMSUB6432);
1447 case X86ISD::ATOMNAND64_DAG:
1448 return SelectAtomic64(Node, X86::ATOMNAND6432);
1449 case X86ISD::ATOMAND64_DAG:
1450 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001451 case X86ISD::ATOMSWAP64_DAG:
1452 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001453
Dan Gohman525178c2007-10-08 18:33:35 +00001454 case ISD::SMUL_LOHI:
1455 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001456 SDValue N0 = Node->getOperand(0);
1457 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001458
Dan Gohman525178c2007-10-08 18:33:35 +00001459 bool isSigned = Opcode == ISD::SMUL_LOHI;
1460 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001461 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001462 default: assert(0 && "Unsupported VT!");
1463 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1464 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1465 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001466 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001467 }
1468 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001469 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001470 default: assert(0 && "Unsupported VT!");
1471 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1472 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1473 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001474 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001475 }
1476
1477 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001478 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001479 default: assert(0 && "Unsupported VT!");
1480 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1481 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1482 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001483 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001484 }
1485
Rafael Espindola094fad32009-04-08 21:14:34 +00001486 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1487 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001488 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001489 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001490 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001491 if (foldedLoad)
1492 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001493 }
1494
Dale Johannesendd64c412009-02-04 00:33:20 +00001495 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001496 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001497
1498 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001499 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1500 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001501 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001502 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001503 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001504 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001505 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001506 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001507 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001508 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001509 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001510 }
1511
Dan Gohman525178c2007-10-08 18:33:35 +00001512 // Copy the low half of the result, if it is needed.
1513 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001514 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001515 LoReg, NVT, InFlag);
1516 InFlag = Result.getValue(2);
1517 ReplaceUses(N.getValue(0), Result);
1518#ifndef NDEBUG
1519 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001520 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001521 DOUT << "\n";
1522#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001523 }
Dan Gohman525178c2007-10-08 18:33:35 +00001524 // Copy the high half of the result, if it is needed.
1525 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001526 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001527 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1528 // Prevent use of AH in a REX instruction by referencing AX instead.
1529 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001530 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001531 X86::AX, MVT::i16, InFlag);
1532 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001533 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1534 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001535 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001536 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001537 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001538 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001539 MVT::i8, Result, SRIdx), 0);
1540 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001541 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001542 HiReg, NVT, InFlag);
1543 InFlag = Result.getValue(2);
1544 }
1545 ReplaceUses(N.getValue(1), Result);
1546#ifndef NDEBUG
1547 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001548 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001549 DOUT << "\n";
1550#endif
1551 }
Evan Cheng34167212006-02-09 00:37:58 +00001552
Evan Chengf597dc72006-02-10 22:24:32 +00001553#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001554 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001555#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001556
Evan Cheng64a752f2006-08-11 09:08:15 +00001557 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001558 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001559
Dan Gohman525178c2007-10-08 18:33:35 +00001560 case ISD::SDIVREM:
1561 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001562 SDValue N0 = Node->getOperand(0);
1563 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001564
1565 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001566 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001567 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001568 default: assert(0 && "Unsupported VT!");
1569 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1570 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1571 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001572 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001573 }
1574 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001575 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001576 default: assert(0 && "Unsupported VT!");
1577 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1578 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1579 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001580 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001581 }
1582
1583 unsigned LoReg, HiReg;
1584 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001585 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001586 default: assert(0 && "Unsupported VT!");
1587 case MVT::i8:
1588 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001589 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001590 SExtOpcode = X86::CBW;
1591 break;
1592 case MVT::i16:
1593 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001594 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001595 SExtOpcode = X86::CWD;
1596 break;
1597 case MVT::i32:
1598 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001599 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001600 SExtOpcode = X86::CDQ;
1601 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001602 case MVT::i64:
1603 LoReg = X86::RAX; HiReg = X86::RDX;
1604 ClrOpcode = X86::MOV64r0;
1605 SExtOpcode = X86::CQO;
1606 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001607 }
1608
Rafael Espindola094fad32009-04-08 21:14:34 +00001609 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1610 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001611 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001612
Dan Gohman475871a2008-07-27 21:46:04 +00001613 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001614 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001615 // Special case for div8, just use a move with zero extension to AX to
1616 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001617 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1618 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1619 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001620 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001621 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001622 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001623 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001624 Chain = Move.getValue(1);
1625 ReplaceUses(N0.getValue(1), Chain);
1626 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001627 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001628 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001629 Chain = CurDAG->getEntryNode();
1630 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001631 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001632 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001633 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001634 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001635 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001636 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001637 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001638 // Sign extend the low part into the high part.
1639 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001640 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001641 } else {
1642 // Zero out the high part, effectively zero extending the input.
Dale Johannesend8392542009-02-03 21:48:12 +00001643 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1644 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00001645 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001646 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001647 }
Evan Cheng948f3432006-01-06 23:19:29 +00001648 }
1649
1650 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001651 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1652 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001653 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001654 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001655 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001656 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001657 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001658 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001659 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001660 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001661 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001662 }
1663
Dan Gohmana37c9f72007-09-25 18:23:27 +00001664 // Copy the division (low) result, if it is needed.
1665 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001666 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001667 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001668 InFlag = Result.getValue(2);
1669 ReplaceUses(N.getValue(0), Result);
1670#ifndef NDEBUG
1671 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001672 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001673 DOUT << "\n";
1674#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001675 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001676 // Copy the remainder (high) result, if it is needed.
1677 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001678 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001679 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1680 // Prevent use of AH in a REX instruction by referencing AX instead.
1681 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001682 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001683 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001684 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001685 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1686 Result,
1687 CurDAG->getTargetConstant(8, MVT::i8)),
1688 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001689 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001690 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001691 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001692 MVT::i8, Result, SRIdx), 0);
1693 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001694 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001695 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001696 InFlag = Result.getValue(2);
1697 }
1698 ReplaceUses(N.getValue(1), Result);
1699#ifndef NDEBUG
1700 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001701 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001702 DOUT << "\n";
1703#endif
1704 }
Evan Chengf597dc72006-02-10 22:24:32 +00001705
1706#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001707 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001708#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001709
1710 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001711 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001712
Evan Cheng851bc042008-06-17 02:01:22 +00001713 case ISD::DECLARE: {
1714 // Handle DECLARE nodes here because the second operand may have been
1715 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001716 SDValue Chain = Node->getOperand(0);
1717 SDValue N1 = Node->getOperand(1);
1718 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001719 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001720
1721 // FIXME: We need to handle this for VLAs.
1722 if (!FINode) {
1723 ReplaceUses(N.getValue(0), Chain);
1724 return NULL;
1725 }
1726
Evan Chengfab83872008-06-18 02:48:27 +00001727 if (N2.getOpcode() == ISD::ADD &&
1728 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1729 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001730
1731 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1732 // somehow, just ignore it.
Chris Lattner18c59872009-06-27 04:16:01 +00001733 if (N2.getOpcode() != X86ISD::Wrapper &&
1734 N2.getOpcode() != X86ISD::WrapperRIP) {
Chris Lattner1823c922009-02-12 17:33:11 +00001735 ReplaceUses(N.getValue(0), Chain);
1736 return NULL;
1737 }
Evan Chengf2accb52009-01-10 03:33:22 +00001738 GlobalAddressSDNode *GVNode =
1739 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001740 if (GVNode == 0) {
1741 ReplaceUses(N.getValue(0), Chain);
1742 return NULL;
1743 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001744 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1745 TLI.getPointerTy());
1746 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1747 TLI.getPointerTy());
1748 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001749 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001750 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001751 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001752 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001753 }
1754
Evan Cheng9ade2182006-08-26 05:34:46 +00001755 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001756
Evan Chengf597dc72006-02-10 22:24:32 +00001757#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001758 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001759 if (ResNode == NULL || ResNode == N.getNode())
1760 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001761 else
1762 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001763 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001764 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001765#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001766
1767 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001768}
1769
Chris Lattnerc0bad572006-06-08 18:03:49 +00001770bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001771SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001772 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001773 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001774 switch (ConstraintCode) {
1775 case 'o': // offsetable ??
1776 case 'v': // not offsetable ??
1777 default: return true;
1778 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001779 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001780 return true;
1781 break;
1782 }
1783
Evan Cheng04699902006-08-26 01:05:16 +00001784 OutOps.push_back(Op0);
1785 OutOps.push_back(Op1);
1786 OutOps.push_back(Op2);
1787 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001788 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001789 return false;
1790}
1791
Chris Lattnerc961eea2005-11-16 01:54:32 +00001792/// createX86ISelDag - This pass converts a legalized DAG into a
1793/// X86-specific DAG, ready for instruction scheduling.
1794///
Bill Wendling98a366d2009-04-29 23:29:43 +00001795FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1796 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001797 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001798}