blob: 7340d7fb64b547547c9148f87e867f11bc4d4d2f [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000038#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000039#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000040#include "llvm/Support/Streams.h"
Torok Edwindac237e2009-07-08 20:53:28 +000041#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000043#include "llvm/ADT/Statistic.h"
44using namespace llvm;
45
Evan Cheng4d952322009-03-31 01:13:53 +000046#include "llvm/Support/CommandLine.h"
47static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
48
Chris Lattner95b2c7d2006-12-19 22:59:26 +000049STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
50
Chris Lattnerc961eea2005-11-16 01:54:32 +000051//===----------------------------------------------------------------------===//
52// Pattern Matcher Implementation
53//===----------------------------------------------------------------------===//
54
55namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000057 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 /// tree.
59 struct X86ISelAddressMode {
60 enum {
61 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000062 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000063 } BaseType;
64
65 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000066 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000067 int FrameIndex;
68 } Base;
69
70 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000071 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000072 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000073 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000075 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000076 const char *ES;
77 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000078 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000079 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000080
81 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000082 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattnerb8afeb92009-06-26 05:51:45 +000083 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0), SymbolFlags(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000084 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000085
86 bool hasSymbolicDisplacement() const {
87 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
88 }
Chris Lattner18c59872009-06-27 04:16:01 +000089
90 bool hasBaseOrIndexReg() const {
91 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
92 }
93
94 /// isRIPRelative - Return true if this addressing mode is already RIP
95 /// relative.
96 bool isRIPRelative() const {
97 if (BaseType != RegBase) return false;
98 if (RegisterSDNode *RegNode =
99 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
100 return RegNode->getReg() == X86::RIP;
101 return false;
102 }
103
104 void setBaseReg(SDValue Reg) {
105 BaseType = RegBase;
106 Base.Reg = Reg;
107 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000108
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000109 void dump() {
110 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000111 cerr << "Base.Reg ";
112 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
113 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000114 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
Chris Lattner18c59872009-06-27 04:16:01 +0000115 cerr << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000116 cerr << "IndexReg ";
117 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
118 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000119 cerr << " Disp " << Disp << "\n";
120 cerr << "GV "; if (GV) GV->dump();
121 else cerr << "nul";
122 cerr << " CP "; if (CP) CP->dump();
123 else cerr << "nul";
124 cerr << "\n";
125 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
126 cerr << " JT" << JT << " Align" << Align << "\n";
127 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000128 };
129}
130
131namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000132 //===--------------------------------------------------------------------===//
133 /// ISel - X86 specific code to select X86 machine instructions for
134 /// SelectionDAG operations.
135 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000136 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000137 /// X86Lowering - This object fully describes how to lower LLVM code to an
138 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000139 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000140
141 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
142 /// make the right decision when generating code for different targets.
143 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000144
Evan Chengb7a75a52008-09-26 23:41:32 +0000145 /// OptForSize - If true, selector should try to optimize for code size
146 /// instead of performance.
147 bool OptForSize;
148
Chris Lattnerc961eea2005-11-16 01:54:32 +0000149 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000150 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000151 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000152 X86Lowering(*tm.getTargetLowering()),
153 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000154 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000155
156 virtual const char *getPassName() const {
157 return "X86 DAG->DAG Instruction Selection";
158 }
159
Evan Chengdb8d56b2008-06-30 20:45:06 +0000160 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000161 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000162 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000163
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000164 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
165
Evan Cheng884c70c2008-11-27 00:49:46 +0000166 virtual
167 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000168
Chris Lattnerc961eea2005-11-16 01:54:32 +0000169// Include the pieces autogenerated from the target description.
170#include "X86GenDAGISel.inc"
171
172 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000173 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000174 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Evan Cheng37b73872009-07-30 08:33:02 +0000175 SDNode *SelectAtomicLoadAdd(SDNode *Node, MVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000176
Rafael Espindola094fad32009-04-08 21:14:34 +0000177 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
178 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000179 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000180 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
181 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
182 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000183 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 Gohmanaae317a2009-08-06 09:22:57 +0000345 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
346 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
347 Load.getOperand(1),
348 Load.getOperand(2));
349 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000350 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000351}
352
Evan Chengcd0baf22008-05-23 21:23:16 +0000353/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
354///
Dan Gohman475871a2008-07-27 21:46:04 +0000355static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
356 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000357 if (N.getOpcode() == ISD::BIT_CONVERT)
358 N = N.getOperand(0);
359
360 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
361 if (!LD || LD->isVolatile())
362 return false;
363 if (LD->getAddressingMode() != ISD::UNINDEXED)
364 return false;
365
366 ISD::LoadExtType ExtType = LD->getExtensionType();
367 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
368 return false;
369
370 if (N.hasOneUse() &&
371 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000372 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000373 Load = N;
374 return true;
375 }
376 return false;
377}
378
Evan Chengab6c3bb2008-08-25 21:27:18 +0000379/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
380/// operand and move load below the call's chain operand.
381static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000382 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000383 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000384 SDValue Chain = CallSeqStart.getOperand(0);
385 if (Chain.getNode() == Load.getNode())
386 Ops.push_back(Load.getOperand(0));
387 else {
388 assert(Chain.getOpcode() == ISD::TokenFactor &&
389 "Unexpected CallSeqStart chain operand");
390 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
391 if (Chain.getOperand(i).getNode() == Load.getNode())
392 Ops.push_back(Load.getOperand(0));
393 else
394 Ops.push_back(Chain.getOperand(i));
395 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000396 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
397 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000398 Ops.clear();
399 Ops.push_back(NewChain);
400 }
401 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
402 Ops.push_back(CallSeqStart.getOperand(i));
403 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000404 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
405 Load.getOperand(1), Load.getOperand(2));
406 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000407 Ops.push_back(SDValue(Load.getNode(), 1));
408 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000409 Ops.push_back(Call.getOperand(i));
410 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
411}
412
413/// isCalleeLoad - Return true if call address is a load and it can be
414/// moved below CALLSEQ_START and the chains leading up to the call.
415/// Return the CALLSEQ_START by reference as a second output.
416static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000417 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000418 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000419 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000420 if (!LD ||
421 LD->isVolatile() ||
422 LD->getAddressingMode() != ISD::UNINDEXED ||
423 LD->getExtensionType() != ISD::NON_EXTLOAD)
424 return false;
425
426 // Now let's find the callseq_start.
427 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
428 if (!Chain.hasOneUse())
429 return false;
430 Chain = Chain.getOperand(0);
431 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000432
433 if (Chain.getOperand(0).getNode() == Callee.getNode())
434 return true;
435 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
436 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
437 return true;
438 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000439}
440
441
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000442/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000443/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000444/// This allows the instruction selector to pick more read-modify-write
445/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000446///
447/// [Load chain]
448/// ^
449/// |
450/// [Load]
451/// ^ ^
452/// | |
453/// / \-
454/// / |
455/// [TokenFactor] [Op]
456/// ^ ^
457/// | |
458/// \ /
459/// \ /
460/// [Store]
461///
462/// The fact the store's chain operand != load's chain will prevent the
463/// (store (op (load))) instruction from being selected. We can transform it to:
464///
465/// [Load chain]
466/// ^
467/// |
468/// [TokenFactor]
469/// ^
470/// |
471/// [Load]
472/// ^ ^
473/// | |
474/// | \-
475/// | |
476/// | [Op]
477/// | ^
478/// | |
479/// \ /
480/// \ /
481/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000482void X86DAGToDAGISel::PreprocessForRMW() {
483 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
484 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000485 if (I->getOpcode() == X86ISD::CALL) {
486 /// Also try moving call address load from outside callseq_start to just
487 /// before the call to allow it to be folded.
488 ///
489 /// [Load chain]
490 /// ^
491 /// |
492 /// [Load]
493 /// ^ ^
494 /// | |
495 /// / \--
496 /// / |
497 ///[CALLSEQ_START] |
498 /// ^ |
499 /// | |
500 /// [LOAD/C2Reg] |
501 /// | |
502 /// \ /
503 /// \ /
504 /// [CALL]
505 SDValue Chain = I->getOperand(0);
506 SDValue Load = I->getOperand(1);
507 if (!isCalleeLoad(Load, Chain))
508 continue;
509 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
510 ++NumLoadMoved;
511 continue;
512 }
513
Evan Cheng8b2794a2006-10-13 21:14:26 +0000514 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000515 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000516 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000517
Gabor Greifba36cb52008-08-28 21:40:38 +0000518 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000519 continue;
520
Dan Gohman475871a2008-07-27 21:46:04 +0000521 SDValue N1 = I->getOperand(1);
522 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000523 if ((N1.getValueType().isFloatingPoint() &&
524 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000525 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000526 continue;
527
528 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000529 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000530 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000531 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000532 case ISD::ADD:
533 case ISD::MUL:
534 case ISD::AND:
535 case ISD::OR:
536 case ISD::XOR:
537 case ISD::ADDC:
538 case ISD::ADDE:
539 case ISD::VECTOR_SHUFFLE: {
540 SDValue N10 = N1.getOperand(0);
541 SDValue N11 = N1.getOperand(1);
542 RModW = isRMWLoad(N10, Chain, N2, Load);
543 if (!RModW)
544 RModW = isRMWLoad(N11, Chain, N2, Load);
545 break;
546 }
547 case ISD::SUB:
548 case ISD::SHL:
549 case ISD::SRA:
550 case ISD::SRL:
551 case ISD::ROTL:
552 case ISD::ROTR:
553 case ISD::SUBC:
554 case ISD::SUBE:
555 case X86ISD::SHLD:
556 case X86ISD::SHRD: {
557 SDValue N10 = N1.getOperand(0);
558 RModW = isRMWLoad(N10, Chain, N2, Load);
559 break;
560 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000561 }
562
Evan Cheng82a35b32006-08-29 06:44:17 +0000563 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000564 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000565 ++NumLoadMoved;
566 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000567 }
568}
569
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000570
571/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
572/// nodes that target the FP stack to be store and load to the stack. This is a
573/// gross hack. We would like to simply mark these as being illegal, but when
574/// we do that, legalize produces these when it expands calls, then expands
575/// these in the same legalize pass. We would like dag combine to be able to
576/// hack on these between the call expansion and the node legalization. As such
577/// this pass basically does "really late" legalization of these inline with the
578/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000579void X86DAGToDAGISel::PreprocessForFPConvert() {
580 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
581 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000582 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
583 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
584 continue;
585
586 // If the source and destination are SSE registers, then this is a legal
587 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000588 MVT SrcVT = N->getOperand(0).getValueType();
589 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000590 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
591 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
592 if (SrcIsSSE && DstIsSSE)
593 continue;
594
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000595 if (!SrcIsSSE && !DstIsSSE) {
596 // If this is an FPStack extension, it is a noop.
597 if (N->getOpcode() == ISD::FP_EXTEND)
598 continue;
599 // If this is a value-preserving FPStack truncation, it is a noop.
600 if (N->getConstantOperandVal(1))
601 continue;
602 }
603
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000604 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
605 // FPStack has extload and truncstore. SSE can fold direct loads into other
606 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000607 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000608 if (N->getOpcode() == ISD::FP_ROUND)
609 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
610 else
611 MemVT = SrcIsSSE ? SrcVT : DstVT;
612
Dan Gohmanf350b272008-08-23 02:25:05 +0000613 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000614 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000615
616 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000617 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000618 N->getOperand(0),
619 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000620 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000621 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000622
623 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
624 // extload we created. This will cause general havok on the dag because
625 // anything below the conversion could be folded into other existing nodes.
626 // To avoid invalidating 'I', back it up to the convert node.
627 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000628 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000629
630 // Now that we did that, the node is dead. Increment the iterator to the
631 // next node to process, then delete N.
632 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000633 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000634 }
635}
636
Chris Lattnerc961eea2005-11-16 01:54:32 +0000637/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
638/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000639void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000640 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000641 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000642
Evan Chengdb8d56b2008-06-30 20:45:06 +0000643 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000644 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000645 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000646
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000647 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000648 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000649
Chris Lattnerc961eea2005-11-16 01:54:32 +0000650 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000651#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000652 DEBUG(errs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000653 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000654#endif
David Greene8ad4c002008-10-27 21:56:29 +0000655 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000656#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000657 DEBUG(errs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000658#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000659
Dan Gohmanf350b272008-08-23 02:25:05 +0000660 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000661}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000662
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000663/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
664/// the main function.
665void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
666 MachineFrameInfo *MFI) {
667 const TargetInstrInfo *TII = TM.getInstrInfo();
668 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000669 BuildMI(BB, DebugLoc::getUnknownLoc(),
670 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000671}
672
673void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
674 // If this is main, emit special code for main.
675 MachineBasicBlock *BB = MF.begin();
676 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
677 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
678}
679
Rafael Espindola094fad32009-04-08 21:14:34 +0000680
681bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
682 X86ISelAddressMode &AM) {
683 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
684 SDValue Segment = N.getOperand(0);
685
686 if (AM.Segment.getNode() == 0) {
687 AM.Segment = Segment;
688 return false;
689 }
690
691 return true;
692}
693
694bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
695 // This optimization is valid because the GNU TLS model defines that
696 // gs:0 (or fs:0 on X86-64) contains its own address.
697 // For more information see http://people.redhat.com/drepper/tls.pdf
698
699 SDValue Address = N.getOperand(1);
700 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
701 !MatchSegmentBaseAddress (Address, AM))
702 return false;
703
704 return true;
705}
706
Chris Lattner18c59872009-06-27 04:16:01 +0000707/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
708/// into an addressing mode. These wrap things that will resolve down into a
709/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000710/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000711bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000712 // If the addressing mode already has a symbol as the displacement, we can
713 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000714 if (AM.hasSymbolicDisplacement())
715 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000716
717 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000718 CodeModel::Model M = TM.getCodeModel();
719
Chris Lattner18c59872009-06-27 04:16:01 +0000720 // Handle X86-64 rip-relative addresses. We check this before checking direct
721 // folding because RIP is preferable to non-RIP accesses.
722 if (Subtarget->is64Bit() &&
723 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
724 // they cannot be folded into immediate fields.
725 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000726 (M == CodeModel::Small || CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000727 // Base and index reg must be 0 in order to use %rip as base and lowering
728 // must allow RIP.
729 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000730 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
731 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000732 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000733 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();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000738 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) 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 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000751
Chris Lattner18c59872009-06-27 04:16:01 +0000752 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() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000761 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000762 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 Gohman41d0b9d2009-07-22 23:26:55 +0000789bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
790 if (MatchAddressRecursively(N, AM, 0))
791 return true;
792
793 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
794 // a smaller encoding and avoids a scaled-index.
795 if (AM.Scale == 2 &&
796 AM.BaseType == X86ISelAddressMode::RegBase &&
797 AM.Base.Reg.getNode() == 0) {
798 AM.Base.Reg = AM.IndexReg;
799 AM.Scale = 1;
800 }
801
802 return false;
803}
804
805bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
806 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000807 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000808 DebugLoc dl = N.getDebugLoc();
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000809 DEBUG(errs() << "MatchAddress: "); DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000810 // Limit recursion.
811 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000812 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000813
814 CodeModel::Model M = TM.getCodeModel();
815
Chris Lattner18c59872009-06-27 04:16:01 +0000816 // If this is already a %rip relative address, we can only merge immediates
817 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000818 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000819 if (AM.isRIPRelative()) {
820 // FIXME: JumpTable and ExternalSymbol address currently don't like
821 // displacements. It isn't very important, but this should be fixed for
822 // consistency.
823 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000824
Chris Lattner18c59872009-06-27 04:16:01 +0000825 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
826 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000827 if (X86::isOffsetSuitableForCodeModel(Val, M,
828 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000829 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000830 return false;
831 }
832 }
833 return true;
834 }
835
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000836 switch (N.getOpcode()) {
837 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000838 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000839 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000840 if (!is64Bit ||
841 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
842 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000843 AM.Disp += Val;
844 return false;
845 }
846 break;
847 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000848
Rafael Espindola094fad32009-04-08 21:14:34 +0000849 case X86ISD::SegmentBaseAddress:
850 if (!MatchSegmentBaseAddress(N, AM))
851 return false;
852 break;
853
Rafael Espindola49a168d2009-04-12 21:55:03 +0000854 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000855 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000856 if (!MatchWrapper(N, AM))
857 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000858 break;
859
Rafael Espindola094fad32009-04-08 21:14:34 +0000860 case ISD::LOAD:
861 if (!MatchLoad(N, AM))
862 return false;
863 break;
864
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000865 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000866 if (AM.BaseType == X86ISelAddressMode::RegBase
867 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000868 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
869 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
870 return false;
871 }
872 break;
Evan Chengec693f72005-12-08 02:01:35 +0000873
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000874 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000875 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000876 break;
877
Gabor Greif93c53e52008-08-31 15:37:04 +0000878 if (ConstantSDNode
879 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000880 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000881 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
882 // that the base operand remains free for further matching. If
883 // the base doesn't end up getting used, a post-processing step
884 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000885 if (Val == 1 || Val == 2 || Val == 3) {
886 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000887 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000888
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000889 // Okay, we know that we have a scale by now. However, if the scaled
890 // value is an add of something and a constant, we can fold the
891 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000892 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
893 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
894 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000895 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000896 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000897 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000898 if (!is64Bit ||
899 X86::isOffsetSuitableForCodeModel(Disp, M,
900 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000901 AM.Disp = Disp;
902 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000903 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000904 } else {
905 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000906 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000907 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000908 }
909 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000910 }
Evan Chengec693f72005-12-08 02:01:35 +0000911
Dan Gohman83688052007-10-22 20:22:24 +0000912 case ISD::SMUL_LOHI:
913 case ISD::UMUL_LOHI:
914 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000915 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000916 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000917 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000918 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000919 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000920 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000921 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000922 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000923 if (ConstantSDNode
924 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000925 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
926 CN->getZExtValue() == 9) {
927 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000928
Gabor Greifba36cb52008-08-28 21:40:38 +0000929 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000930 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000931
932 // Okay, we know that we have a scale by now. However, if the scaled
933 // value is an add of something and a constant, we can fold the
934 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000935 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
936 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
937 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000938 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000939 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000940 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000941 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000942 if (!is64Bit ||
943 X86::isOffsetSuitableForCodeModel(Disp, M,
944 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000945 AM.Disp = Disp;
946 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000947 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000948 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000949 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000950 }
951
952 AM.IndexReg = AM.Base.Reg = Reg;
953 return false;
954 }
Chris Lattner62412262007-02-04 20:18:17 +0000955 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000956 break;
957
Dan Gohman3cd90a12009-05-11 18:02:53 +0000958 case ISD::SUB: {
959 // Given A-B, if A can be completely folded into the address and
960 // the index field with the index field unused, use -B as the index.
961 // This is a win if a has multiple parts that can be folded into
962 // the address. Also, this saves a mov if the base register has
963 // other uses, since it avoids a two-address sub instruction, however
964 // it costs an additional mov if the index register has other uses.
965
966 // Test if the LHS of the sub can be folded.
967 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000968 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000969 AM = Backup;
970 break;
971 }
972 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000973 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000974 AM = Backup;
975 break;
976 }
977 int Cost = 0;
978 SDValue RHS = N.getNode()->getOperand(1);
979 // If the RHS involves a register with multiple uses, this
980 // transformation incurs an extra mov, due to the neg instruction
981 // clobbering its operand.
982 if (!RHS.getNode()->hasOneUse() ||
983 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
984 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
985 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
986 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
987 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
988 ++Cost;
989 // If the base is a register with multiple uses, this
990 // transformation may save a mov.
991 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
992 AM.Base.Reg.getNode() &&
993 !AM.Base.Reg.getNode()->hasOneUse()) ||
994 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
995 --Cost;
996 // If the folded LHS was interesting, this transformation saves
997 // address arithmetic.
998 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
999 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1000 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1001 --Cost;
1002 // If it doesn't look like it may be an overall win, don't do it.
1003 if (Cost >= 0) {
1004 AM = Backup;
1005 break;
1006 }
1007
1008 // Ok, the transformation is legal and appears profitable. Go for it.
1009 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1010 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1011 AM.IndexReg = Neg;
1012 AM.Scale = 1;
1013
1014 // Insert the new nodes into the topological ordering.
1015 if (Zero.getNode()->getNodeId() == -1 ||
1016 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1017 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1018 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1019 }
1020 if (Neg.getNode()->getNodeId() == -1 ||
1021 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1022 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1023 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1024 }
1025 return false;
1026 }
1027
Evan Cheng8e278262009-01-17 07:09:27 +00001028 case ISD::ADD: {
1029 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001030 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1031 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001032 return false;
1033 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001034 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1035 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001036 return false;
1037 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001038
1039 // If we couldn't fold both operands into the address at the same time,
1040 // see if we can just put each operand into a register and fold at least
1041 // the add.
1042 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1043 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001044 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001045 AM.Base.Reg = N.getNode()->getOperand(0);
1046 AM.IndexReg = N.getNode()->getOperand(1);
1047 AM.Scale = 1;
1048 return false;
1049 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001050 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001051 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001052
Chris Lattner62412262007-02-04 20:18:17 +00001053 case ISD::OR:
1054 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001055 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1056 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001057 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001058 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001059 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001060 // Address could not have picked a GV address for the displacement.
1061 AM.GV == NULL &&
1062 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001063 (!is64Bit ||
1064 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1065 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001066 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001067 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001068 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001069 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001070 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001071 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001072 }
1073 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001074
1075 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001076 // Perform some heroic transforms on an and of a constant-count shift
1077 // with a constant to enable use of the scaled offset field.
1078
Dan Gohman475871a2008-07-27 21:46:04 +00001079 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001080 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001081
Evan Cheng1314b002007-12-13 00:43:27 +00001082 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001083 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001084
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001085 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001086 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1087 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1088 if (!C1 || !C2) break;
1089
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001090 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1091 // allows us to convert the shift and and into an h-register extract and
1092 // a scaled index.
1093 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1094 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001095 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001096 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1097 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1098 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1099 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1100 X, Eight);
1101 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1102 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +00001103 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1104 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1105 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001106
1107 // Insert the new nodes into the topological ordering.
1108 if (Eight.getNode()->getNodeId() == -1 ||
1109 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1110 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1111 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1112 }
1113 if (Mask.getNode()->getNodeId() == -1 ||
1114 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1115 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1116 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1117 }
1118 if (Srl.getNode()->getNodeId() == -1 ||
1119 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1120 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1121 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1122 }
1123 if (And.getNode()->getNodeId() == -1 ||
1124 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1125 CurDAG->RepositionNode(N.getNode(), And.getNode());
1126 And.getNode()->setNodeId(N.getNode()->getNodeId());
1127 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001128 if (ShlCount.getNode()->getNodeId() == -1 ||
1129 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1130 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1131 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1132 }
1133 if (Shl.getNode()->getNodeId() == -1 ||
1134 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1135 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1136 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1137 }
1138 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001139 AM.IndexReg = And;
1140 AM.Scale = (1 << ScaleLog);
1141 return false;
1142 }
1143 }
1144
1145 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1146 // allows us to fold the shift into this addressing mode.
1147 if (Shift.getOpcode() != ISD::SHL) break;
1148
Evan Cheng1314b002007-12-13 00:43:27 +00001149 // Not likely to be profitable if either the AND or SHIFT node has more
1150 // than one use (unless all uses are for address computation). Besides,
1151 // isel mechanism requires their node ids to be reused.
1152 if (!N.hasOneUse() || !Shift.hasOneUse())
1153 break;
1154
1155 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001156 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001157 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1158 break;
1159
1160 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001161 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001162 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001163 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1164 NewANDMask);
1165 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001166 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001167
1168 // Insert the new nodes into the topological ordering.
1169 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1170 CurDAG->RepositionNode(X.getNode(), C1);
1171 C1->setNodeId(X.getNode()->getNodeId());
1172 }
1173 if (NewANDMask.getNode()->getNodeId() == -1 ||
1174 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1175 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1176 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1177 }
1178 if (NewAND.getNode()->getNodeId() == -1 ||
1179 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1180 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1181 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1182 }
1183 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1184 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1185 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1186 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1187 }
1188
Dan Gohman7b8e9642008-10-13 20:52:04 +00001189 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001190
1191 AM.Scale = 1 << ShiftCst;
1192 AM.IndexReg = NewAND;
1193 return false;
1194 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001195 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001196
Rafael Espindola523249f2009-03-31 16:16:57 +00001197 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001198}
1199
1200/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1201/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001202bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001203 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001204 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001205 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001206 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001207 AM.IndexReg = N;
1208 AM.Scale = 1;
1209 return false;
1210 }
1211
1212 // Otherwise, we cannot select it.
1213 return true;
1214 }
1215
1216 // Default, generate it as a register.
1217 AM.BaseType = X86ISelAddressMode::RegBase;
1218 AM.Base.Reg = N;
1219 return false;
1220}
1221
Evan Chengec693f72005-12-08 02:01:35 +00001222/// SelectAddr - returns true if it is able pattern match an addressing mode.
1223/// It returns the operands which make up the maximal addressing mode it can
1224/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001225bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1226 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001227 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001228 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001229 bool Done = false;
1230 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1231 unsigned Opcode = N.getOpcode();
1232 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001233 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001234 // If we are able to fold N into addressing mode, then we'll allow it even
1235 // if N has multiple uses. In general, addressing computation is used as
1236 // addresses by all of its uses. But watch out for CopyToReg uses, that
1237 // means the address computation is liveout. It will be computed by a LEA
1238 // so we want to avoid computing the address twice.
1239 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1240 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1241 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001242 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001243 Done = true;
1244 break;
1245 }
1246 }
1247 }
1248 }
1249
1250 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001251 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001252
Duncan Sands83ec4b62008-06-06 12:08:01 +00001253 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001254 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001255 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001256 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001257 }
Evan Cheng8700e142006-01-11 06:09:51 +00001258
Gabor Greifba36cb52008-08-28 21:40:38 +00001259 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001260 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001261
Rafael Espindola094fad32009-04-08 21:14:34 +00001262 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001263 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001264}
1265
Chris Lattner3a7cd952006-10-07 21:55:32 +00001266/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1267/// match a load whose top elements are either undef or zeros. The load flavor
1268/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001269bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1270 SDValue N, SDValue &Base,
1271 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001272 SDValue &Disp, SDValue &Segment,
1273 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001274 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001275 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001276 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001277 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001278 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001279 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001280 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001281 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001282 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001283 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001284 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001285 return true;
1286 }
1287 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001288
1289 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001290 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001291 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001292 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001293 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001294 N.getOperand(0).getNode()->hasOneUse() &&
1295 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001296 N.getOperand(0).getOperand(0).hasOneUse()) {
1297 // Okay, this is a zero extending load. Fold it.
1298 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001299 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001300 return false;
1301 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001302 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001303 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001304 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001305 return false;
1306}
1307
1308
Evan Cheng51a9ed92006-02-25 10:09:08 +00001309/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1310/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001311bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1312 SDValue &Base, SDValue &Scale,
1313 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001314 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001315
1316 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1317 // segments.
1318 SDValue Copy = AM.Segment;
1319 SDValue T = CurDAG->getRegister(0, MVT::i32);
1320 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001321 if (MatchAddress(N, AM))
1322 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001323 assert (T == AM.Segment);
1324 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001325
Duncan Sands83ec4b62008-06-06 12:08:01 +00001326 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001327 unsigned Complexity = 0;
1328 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001329 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001330 Complexity = 1;
1331 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001332 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001333 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1334 Complexity = 4;
1335
Gabor Greifba36cb52008-08-28 21:40:38 +00001336 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001337 Complexity++;
1338 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001339 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001340
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001341 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1342 // a simple shift.
1343 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001344 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001345
1346 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1347 // to a LEA. This is determined with some expermentation but is by no means
1348 // optimal (especially for code size consideration). LEA is nice because of
1349 // its three-address nature. Tweak the cost function again when we can run
1350 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001351 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001352 // For X86-64, we should always use lea to materialize RIP relative
1353 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001354 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001355 Complexity = 4;
1356 else
1357 Complexity += 2;
1358 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001359
Gabor Greifba36cb52008-08-28 21:40:38 +00001360 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001361 Complexity++;
1362
Chris Lattner25142782009-07-11 22:50:33 +00001363 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001364 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001365 return false;
1366
1367 SDValue Segment;
1368 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1369 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001370}
1371
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001372/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1373bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1374 SDValue &Scale, SDValue &Index,
1375 SDValue &Disp) {
1376 assert(Op.getOpcode() == X86ISD::TLSADDR);
1377 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1378 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1379
1380 X86ISelAddressMode AM;
1381 AM.GV = GA->getGlobal();
1382 AM.Disp += GA->getOffset();
1383 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001384 AM.SymbolFlags = GA->getTargetFlags();
1385
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001386 if (N.getValueType() == MVT::i32) {
1387 AM.Scale = 1;
1388 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1389 } else {
1390 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1391 }
1392
1393 SDValue Segment;
1394 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1395 return true;
1396}
1397
1398
Dan Gohman475871a2008-07-27 21:46:04 +00001399bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1400 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001401 SDValue &Index, SDValue &Disp,
1402 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001403 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001404 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001405 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001406 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001407 return false;
1408}
1409
Dan Gohman8b746962008-09-23 18:22:58 +00001410/// getGlobalBaseReg - Return an SDNode that returns the value of
1411/// the global base register. Output instructions required to
1412/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001413///
Evan Cheng9ade2182006-08-26 05:34:46 +00001414SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001415 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001416 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001417}
1418
Evan Chengb245d922006-05-20 01:36:52 +00001419static SDNode *FindCallStartFromCall(SDNode *Node) {
1420 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1421 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1422 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001423 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001424}
1425
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001426SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1427 SDValue Chain = Node->getOperand(0);
1428 SDValue In1 = Node->getOperand(1);
1429 SDValue In2L = Node->getOperand(2);
1430 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001431 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1432 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001433 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001434 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001435 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001436 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1437 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001438 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001439}
Christopher Lambc59e5212007-08-10 21:48:46 +00001440
Evan Cheng37b73872009-07-30 08:33:02 +00001441SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, MVT NVT) {
1442 if (Node->hasAnyUseOfValue(0))
1443 return 0;
1444
1445 // Optimize common patterns for __sync_add_and_fetch and
1446 // __sync_sub_and_fetch where the result is not used. This allows us
1447 // to use "lock" version of add, sub, inc, dec instructions.
1448 // FIXME: Do not use special instructions but instead add the "lock"
1449 // prefix to the target node somehow. The extra information will then be
1450 // transferred to machine instruction and it denotes the prefix.
1451 SDValue Chain = Node->getOperand(0);
1452 SDValue Ptr = Node->getOperand(1);
1453 SDValue Val = Node->getOperand(2);
1454 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1455 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1456 return 0;
1457
1458 bool isInc = false, isDec = false, isSub = false, isCN = false;
1459 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1460 if (CN) {
1461 isCN = true;
1462 int64_t CNVal = CN->getSExtValue();
1463 if (CNVal == 1)
1464 isInc = true;
1465 else if (CNVal == -1)
1466 isDec = true;
1467 else if (CNVal >= 0)
1468 Val = CurDAG->getTargetConstant(CNVal, NVT);
1469 else {
1470 isSub = true;
1471 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1472 }
1473 } else if (Val.hasOneUse() &&
1474 Val.getOpcode() == ISD::SUB &&
1475 X86::isZeroNode(Val.getOperand(0))) {
1476 isSub = true;
1477 Val = Val.getOperand(1);
1478 }
1479
1480 unsigned Opc = 0;
1481 switch (NVT.getSimpleVT()) {
1482 default: return 0;
1483 case MVT::i8:
1484 if (isInc)
1485 Opc = X86::LOCK_INC8m;
1486 else if (isDec)
1487 Opc = X86::LOCK_DEC8m;
1488 else if (isSub) {
1489 if (isCN)
1490 Opc = X86::LOCK_SUB8mi;
1491 else
1492 Opc = X86::LOCK_SUB8mr;
1493 } else {
1494 if (isCN)
1495 Opc = X86::LOCK_ADD8mi;
1496 else
1497 Opc = X86::LOCK_ADD8mr;
1498 }
1499 break;
1500 case MVT::i16:
1501 if (isInc)
1502 Opc = X86::LOCK_INC16m;
1503 else if (isDec)
1504 Opc = X86::LOCK_DEC16m;
1505 else if (isSub) {
1506 if (isCN) {
1507 if (Predicate_i16immSExt8(Val.getNode()))
1508 Opc = X86::LOCK_SUB16mi8;
1509 else
1510 Opc = X86::LOCK_SUB16mi;
1511 } else
1512 Opc = X86::LOCK_SUB16mr;
1513 } else {
1514 if (isCN) {
1515 if (Predicate_i16immSExt8(Val.getNode()))
1516 Opc = X86::LOCK_ADD16mi8;
1517 else
1518 Opc = X86::LOCK_ADD16mi;
1519 } else
1520 Opc = X86::LOCK_ADD16mr;
1521 }
1522 break;
1523 case MVT::i32:
1524 if (isInc)
1525 Opc = X86::LOCK_INC32m;
1526 else if (isDec)
1527 Opc = X86::LOCK_DEC32m;
1528 else if (isSub) {
1529 if (isCN) {
1530 if (Predicate_i32immSExt8(Val.getNode()))
1531 Opc = X86::LOCK_SUB32mi8;
1532 else
1533 Opc = X86::LOCK_SUB32mi;
1534 } else
1535 Opc = X86::LOCK_SUB32mr;
1536 } else {
1537 if (isCN) {
1538 if (Predicate_i32immSExt8(Val.getNode()))
1539 Opc = X86::LOCK_ADD32mi8;
1540 else
1541 Opc = X86::LOCK_ADD32mi;
1542 } else
1543 Opc = X86::LOCK_ADD32mr;
1544 }
1545 break;
1546 case MVT::i64:
1547 if (isInc)
1548 Opc = X86::LOCK_INC64m;
1549 else if (isDec)
1550 Opc = X86::LOCK_DEC64m;
1551 else if (isSub) {
1552 Opc = X86::LOCK_SUB64mr;
1553 if (isCN) {
1554 if (Predicate_i64immSExt8(Val.getNode()))
1555 Opc = X86::LOCK_SUB64mi8;
1556 else if (Predicate_i64immSExt32(Val.getNode()))
1557 Opc = X86::LOCK_SUB64mi32;
1558 }
1559 } else {
1560 Opc = X86::LOCK_ADD64mr;
1561 if (isCN) {
1562 if (Predicate_i64immSExt8(Val.getNode()))
1563 Opc = X86::LOCK_ADD64mi8;
1564 else if (Predicate_i64immSExt32(Val.getNode()))
1565 Opc = X86::LOCK_ADD64mi32;
1566 }
1567 }
1568 break;
1569 }
1570
1571 DebugLoc dl = Node->getDebugLoc();
1572 SDValue Undef = SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1573 dl, NVT), 0);
1574 SDValue MemOp = CurDAG->getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
1575 if (isInc || isDec) {
1576 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, MemOp, Chain };
1577 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 7), 0);
1578 SDValue RetVals[] = { Undef, Ret };
1579 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1580 } else {
1581 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, MemOp, Chain };
1582 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 8), 0);
1583 SDValue RetVals[] = { Undef, Ret };
1584 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1585 }
1586}
1587
Dan Gohman475871a2008-07-27 21:46:04 +00001588SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001589 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001590 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001591 unsigned Opc, MOpc;
1592 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001593 DebugLoc dl = Node->getDebugLoc();
1594
Evan Chengf597dc72006-02-10 22:24:32 +00001595#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001596 DEBUG(errs() << std::string(Indent, ' ') << "Selecting: ");
Evan Chengf597dc72006-02-10 22:24:32 +00001597 DEBUG(Node->dump(CurDAG));
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001598 DEBUG(errs() << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001599 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001600#endif
1601
Dan Gohmane8be6c62008-07-17 19:10:17 +00001602 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001603#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001604 DEBUG(errs() << std::string(Indent-2, ' ') << "== ");
Evan Chengf597dc72006-02-10 22:24:32 +00001605 DEBUG(Node->dump(CurDAG));
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001606 DEBUG(errs() << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001607 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001608#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001609 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001610 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001611
Evan Cheng0114e942006-01-06 20:36:21 +00001612 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001613 default: break;
1614 case X86ISD::GlobalBaseReg:
1615 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001616
Dan Gohman72677342009-08-02 16:10:52 +00001617 case X86ISD::ATOMOR64_DAG:
1618 return SelectAtomic64(Node, X86::ATOMOR6432);
1619 case X86ISD::ATOMXOR64_DAG:
1620 return SelectAtomic64(Node, X86::ATOMXOR6432);
1621 case X86ISD::ATOMADD64_DAG:
1622 return SelectAtomic64(Node, X86::ATOMADD6432);
1623 case X86ISD::ATOMSUB64_DAG:
1624 return SelectAtomic64(Node, X86::ATOMSUB6432);
1625 case X86ISD::ATOMNAND64_DAG:
1626 return SelectAtomic64(Node, X86::ATOMNAND6432);
1627 case X86ISD::ATOMAND64_DAG:
1628 return SelectAtomic64(Node, X86::ATOMAND6432);
1629 case X86ISD::ATOMSWAP64_DAG:
1630 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001631
Dan Gohman72677342009-08-02 16:10:52 +00001632 case ISD::ATOMIC_LOAD_ADD: {
1633 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1634 if (RetVal)
1635 return RetVal;
1636 break;
1637 }
1638
1639 case ISD::SMUL_LOHI:
1640 case ISD::UMUL_LOHI: {
1641 SDValue N0 = Node->getOperand(0);
1642 SDValue N1 = Node->getOperand(1);
1643
1644 bool isSigned = Opcode == ISD::SMUL_LOHI;
1645 if (!isSigned)
1646 switch (NVT.getSimpleVT()) {
1647 default: llvm_unreachable("Unsupported VT!");
1648 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1649 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1650 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1651 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
1652 }
1653 else
1654 switch (NVT.getSimpleVT()) {
1655 default: llvm_unreachable("Unsupported VT!");
1656 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1657 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1658 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1659 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
1660 }
1661
1662 unsigned LoReg, HiReg;
1663 switch (NVT.getSimpleVT()) {
1664 default: llvm_unreachable("Unsupported VT!");
1665 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1666 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1667 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1668 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
1669 }
1670
1671 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1672 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1673 // multiplty is commmutative
1674 if (!foldedLoad) {
1675 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1676 if (foldedLoad)
1677 std::swap(N0, N1);
1678 }
1679
1680 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1681 N0, SDValue()).getValue(1);
1682
1683 if (foldedLoad) {
1684 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1685 InFlag };
1686 SDNode *CNode =
1687 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1688 array_lengthof(Ops));
1689 InFlag = SDValue(CNode, 1);
1690 // Update the chain.
1691 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1692 } else {
1693 InFlag =
1694 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
1695 }
1696
1697 // Copy the low half of the result, if it is needed.
1698 if (!N.getValue(0).use_empty()) {
1699 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1700 LoReg, NVT, InFlag);
1701 InFlag = Result.getValue(2);
1702 ReplaceUses(N.getValue(0), Result);
1703#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001704 DEBUG(errs() << std::string(Indent-2, ' ') << "=> ");
Dan Gohman72677342009-08-02 16:10:52 +00001705 DEBUG(Result.getNode()->dump(CurDAG));
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001706 DEBUG(errs() << "\n");
Dan Gohman72677342009-08-02 16:10:52 +00001707#endif
1708 }
1709 // Copy the high half of the result, if it is needed.
1710 if (!N.getValue(1).use_empty()) {
1711 SDValue Result;
1712 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1713 // Prevent use of AH in a REX instruction by referencing AX instead.
1714 // Shift it down 8 bits.
1715 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1716 X86::AX, MVT::i16, InFlag);
1717 InFlag = Result.getValue(2);
1718 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1719 Result,
1720 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1721 // Then truncate it down to i8.
1722 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
1723 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
1724 MVT::i8, Result, SRIdx), 0);
1725 } else {
1726 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1727 HiReg, NVT, InFlag);
1728 InFlag = Result.getValue(2);
1729 }
1730 ReplaceUses(N.getValue(1), Result);
1731#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001732 DEBUG(errs() << std::string(Indent-2, ' ') << "=> ");
Dan Gohman72677342009-08-02 16:10:52 +00001733 DEBUG(Result.getNode()->dump(CurDAG));
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001734 DEBUG(errs() << "\n");
Dan Gohman72677342009-08-02 16:10:52 +00001735#endif
1736 }
1737
1738#ifndef NDEBUG
1739 Indent -= 2;
1740#endif
1741
1742 return NULL;
1743 }
1744
1745 case ISD::SDIVREM:
1746 case ISD::UDIVREM: {
1747 SDValue N0 = Node->getOperand(0);
1748 SDValue N1 = Node->getOperand(1);
1749
1750 bool isSigned = Opcode == ISD::SDIVREM;
1751 if (!isSigned)
1752 switch (NVT.getSimpleVT()) {
1753 default: llvm_unreachable("Unsupported VT!");
1754 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1755 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1756 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1757 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
1758 }
1759 else
1760 switch (NVT.getSimpleVT()) {
1761 default: llvm_unreachable("Unsupported VT!");
1762 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1763 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1764 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1765 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
1766 }
1767
1768 unsigned LoReg, HiReg;
1769 unsigned ClrOpcode, SExtOpcode;
1770 switch (NVT.getSimpleVT()) {
1771 default: llvm_unreachable("Unsupported VT!");
1772 case MVT::i8:
1773 LoReg = X86::AL; HiReg = X86::AH;
1774 ClrOpcode = 0;
1775 SExtOpcode = X86::CBW;
1776 break;
1777 case MVT::i16:
1778 LoReg = X86::AX; HiReg = X86::DX;
1779 ClrOpcode = X86::MOV16r0;
1780 SExtOpcode = X86::CWD;
1781 break;
1782 case MVT::i32:
1783 LoReg = X86::EAX; HiReg = X86::EDX;
1784 ClrOpcode = X86::MOV32r0;
1785 SExtOpcode = X86::CDQ;
1786 break;
1787 case MVT::i64:
1788 LoReg = X86::RAX; HiReg = X86::RDX;
1789 ClrOpcode = ~0U; // NOT USED.
1790 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001791 break;
1792 }
1793
Dan Gohman72677342009-08-02 16:10:52 +00001794 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1795 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1796 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001797
Dan Gohman72677342009-08-02 16:10:52 +00001798 SDValue InFlag;
1799 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
1800 // Special case for div8, just use a move with zero extension to AX to
1801 // clear the upper 8 bits (AH).
1802 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1803 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1804 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1805 Move =
1806 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
1807 MVT::Other, Ops,
1808 array_lengthof(Ops)), 0);
1809 Chain = Move.getValue(1);
1810 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001811 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001812 Move =
1813 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
1814 Chain = CurDAG->getEntryNode();
1815 }
1816 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1817 InFlag = Chain.getValue(1);
1818 } else {
1819 InFlag =
1820 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1821 LoReg, N0, SDValue()).getValue(1);
1822 if (isSigned && !signBitIsZero) {
1823 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001824 InFlag =
Dan Gohman72677342009-08-02 16:10:52 +00001825 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
1826 } else {
1827 // Zero out the high part, effectively zero extending the input.
1828 SDValue ClrNode;
Evan Cheng0114e942006-01-06 20:36:21 +00001829
Dan Gohman72677342009-08-02 16:10:52 +00001830 if (NVT.getSimpleVT() == MVT::i64) {
1831 ClrNode = SDValue(CurDAG->getTargetNode(X86::MOV32r0, dl, MVT::i32),
1832 0);
1833 // We just did a 32-bit clear, insert it into a 64-bit register to
1834 // clear the whole 64-bit reg.
1835 SDValue Undef =
1836 SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1837 dl, MVT::i64), 0);
1838 SDValue SubRegNo =
1839 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
1840 ClrNode =
1841 SDValue(CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl,
1842 MVT::i64, Undef, ClrNode, SubRegNo),
1843 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001844 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001845 ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001846 }
Dan Gohman72677342009-08-02 16:10:52 +00001847
1848 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
1849 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001850 }
Evan Cheng948f3432006-01-06 23:19:29 +00001851 }
Dan Gohman525178c2007-10-08 18:33:35 +00001852
Dan Gohman72677342009-08-02 16:10:52 +00001853 if (foldedLoad) {
1854 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1855 InFlag };
1856 SDNode *CNode =
1857 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1858 array_lengthof(Ops));
1859 InFlag = SDValue(CNode, 1);
1860 // Update the chain.
1861 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1862 } else {
1863 InFlag =
1864 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
1865 }
Evan Cheng948f3432006-01-06 23:19:29 +00001866
Dan Gohman72677342009-08-02 16:10:52 +00001867 // Copy the division (low) result, if it is needed.
1868 if (!N.getValue(0).use_empty()) {
1869 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1870 LoReg, NVT, InFlag);
1871 InFlag = Result.getValue(2);
1872 ReplaceUses(N.getValue(0), Result);
1873#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001874 DEBUG(errs() << std::string(Indent-2, ' ') << "=> ");
Dan Gohman72677342009-08-02 16:10:52 +00001875 DEBUG(Result.getNode()->dump(CurDAG));
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001876 DEBUG(errs() << "\n");
Dan Gohman72677342009-08-02 16:10:52 +00001877#endif
1878 }
1879 // Copy the remainder (high) result, if it is needed.
1880 if (!N.getValue(1).use_empty()) {
1881 SDValue Result;
1882 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1883 // Prevent use of AH in a REX instruction by referencing AX instead.
1884 // Shift it down 8 bits.
1885 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1886 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001887 InFlag = Result.getValue(2);
Dan Gohman72677342009-08-02 16:10:52 +00001888 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1889 Result,
1890 CurDAG->getTargetConstant(8, MVT::i8)),
1891 0);
1892 // Then truncate it down to i8.
1893 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
1894 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
1895 MVT::i8, Result, SRIdx), 0);
1896 } else {
1897 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1898 HiReg, NVT, InFlag);
1899 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00001900 }
Dan Gohman72677342009-08-02 16:10:52 +00001901 ReplaceUses(N.getValue(1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001902#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001903 DEBUG(errs() << std::string(Indent-2, ' ') << "=> ");
Dan Gohman72677342009-08-02 16:10:52 +00001904 DEBUG(Result.getNode()->dump(CurDAG));
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001905 DEBUG(errs() << "\n");
Dan Gohmana37c9f72007-09-25 18:23:27 +00001906#endif
Dan Gohman72677342009-08-02 16:10:52 +00001907 }
Evan Chengf597dc72006-02-10 22:24:32 +00001908
1909#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00001910 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001911#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001912
Dan Gohman72677342009-08-02 16:10:52 +00001913 return NULL;
1914 }
1915
1916 case ISD::DECLARE: {
1917 // Handle DECLARE nodes here because the second operand may have been
1918 // wrapped in X86ISD::Wrapper.
1919 SDValue Chain = Node->getOperand(0);
1920 SDValue N1 = Node->getOperand(1);
1921 SDValue N2 = Node->getOperand(2);
1922 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
1923
1924 // FIXME: We need to handle this for VLAs.
1925 if (!FINode) {
1926 ReplaceUses(N.getValue(0), Chain);
Evan Cheng64a752f2006-08-11 09:08:15 +00001927 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001928 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001929
Dan Gohman72677342009-08-02 16:10:52 +00001930 if (N2.getOpcode() == ISD::ADD &&
1931 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1932 N2 = N2.getOperand(1);
1933
1934 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1935 // somehow, just ignore it.
1936 if (N2.getOpcode() != X86ISD::Wrapper &&
1937 N2.getOpcode() != X86ISD::WrapperRIP) {
1938 ReplaceUses(N.getValue(0), Chain);
1939 return NULL;
Evan Cheng851bc042008-06-17 02:01:22 +00001940 }
Dan Gohman72677342009-08-02 16:10:52 +00001941 GlobalAddressSDNode *GVNode =
1942 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
1943 if (GVNode == 0) {
1944 ReplaceUses(N.getValue(0), Chain);
1945 return NULL;
1946 }
1947 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1948 TLI.getPointerTy());
1949 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1950 TLI.getPointerTy());
1951 SDValue Ops[] = { Tmp1, Tmp2, Chain };
1952 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
1953 MVT::Other, Ops,
1954 array_lengthof(Ops));
1955 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001956 }
1957
Evan Cheng9ade2182006-08-26 05:34:46 +00001958 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001959
Evan Chengf597dc72006-02-10 22:24:32 +00001960#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001961 DEBUG(errs() << std::string(Indent-2, ' ') << "=> ");
Gabor Greifba36cb52008-08-28 21:40:38 +00001962 if (ResNode == NULL || ResNode == N.getNode())
1963 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001964 else
1965 DEBUG(ResNode->dump(CurDAG));
Bill Wendling0ea8bf32009-08-03 00:11:34 +00001966 DEBUG(errs() << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001967 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001968#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001969
1970 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001971}
1972
Chris Lattnerc0bad572006-06-08 18:03:49 +00001973bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001974SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001975 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001976 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001977 switch (ConstraintCode) {
1978 case 'o': // offsetable ??
1979 case 'v': // not offsetable ??
1980 default: return true;
1981 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001982 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001983 return true;
1984 break;
1985 }
1986
Evan Cheng04699902006-08-26 01:05:16 +00001987 OutOps.push_back(Op0);
1988 OutOps.push_back(Op1);
1989 OutOps.push_back(Op2);
1990 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001991 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001992 return false;
1993}
1994
Chris Lattnerc961eea2005-11-16 01:54:32 +00001995/// createX86ISelDag - This pass converts a legalized DAG into a
1996/// X86-specific DAG, ready for instruction scheduling.
1997///
Bill Wendling98a366d2009-04-29 23:29:43 +00001998FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1999 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002000 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002001}