blob: 104c3b58a71fc7895ff2a7c80f7913bfafe791ae [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 Gohmanf350b272008-08-23 02:25:05 +0000345 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
346 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
347 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
348 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000349}
350
Evan Chengcd0baf22008-05-23 21:23:16 +0000351/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
352///
Dan Gohman475871a2008-07-27 21:46:04 +0000353static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
354 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000355 if (N.getOpcode() == ISD::BIT_CONVERT)
356 N = N.getOperand(0);
357
358 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
359 if (!LD || LD->isVolatile())
360 return false;
361 if (LD->getAddressingMode() != ISD::UNINDEXED)
362 return false;
363
364 ISD::LoadExtType ExtType = LD->getExtensionType();
365 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
366 return false;
367
368 if (N.hasOneUse() &&
369 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000370 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000371 Load = N;
372 return true;
373 }
374 return false;
375}
376
Evan Chengab6c3bb2008-08-25 21:27:18 +0000377/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
378/// operand and move load below the call's chain operand.
379static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000380 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000381 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000382 SDValue Chain = CallSeqStart.getOperand(0);
383 if (Chain.getNode() == Load.getNode())
384 Ops.push_back(Load.getOperand(0));
385 else {
386 assert(Chain.getOpcode() == ISD::TokenFactor &&
387 "Unexpected CallSeqStart chain operand");
388 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
389 if (Chain.getOperand(i).getNode() == Load.getNode())
390 Ops.push_back(Load.getOperand(0));
391 else
392 Ops.push_back(Chain.getOperand(i));
393 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000394 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
395 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000396 Ops.clear();
397 Ops.push_back(NewChain);
398 }
399 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
400 Ops.push_back(CallSeqStart.getOperand(i));
401 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000402 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
403 Load.getOperand(1), Load.getOperand(2));
404 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000405 Ops.push_back(SDValue(Load.getNode(), 1));
406 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000407 Ops.push_back(Call.getOperand(i));
408 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
409}
410
411/// isCalleeLoad - Return true if call address is a load and it can be
412/// moved below CALLSEQ_START and the chains leading up to the call.
413/// Return the CALLSEQ_START by reference as a second output.
414static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000415 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000416 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000417 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000418 if (!LD ||
419 LD->isVolatile() ||
420 LD->getAddressingMode() != ISD::UNINDEXED ||
421 LD->getExtensionType() != ISD::NON_EXTLOAD)
422 return false;
423
424 // Now let's find the callseq_start.
425 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
426 if (!Chain.hasOneUse())
427 return false;
428 Chain = Chain.getOperand(0);
429 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000430
431 if (Chain.getOperand(0).getNode() == Callee.getNode())
432 return true;
433 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
434 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
435 return true;
436 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000437}
438
439
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000440/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000441/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000442/// This allows the instruction selector to pick more read-modify-write
443/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000444///
445/// [Load chain]
446/// ^
447/// |
448/// [Load]
449/// ^ ^
450/// | |
451/// / \-
452/// / |
453/// [TokenFactor] [Op]
454/// ^ ^
455/// | |
456/// \ /
457/// \ /
458/// [Store]
459///
460/// The fact the store's chain operand != load's chain will prevent the
461/// (store (op (load))) instruction from being selected. We can transform it to:
462///
463/// [Load chain]
464/// ^
465/// |
466/// [TokenFactor]
467/// ^
468/// |
469/// [Load]
470/// ^ ^
471/// | |
472/// | \-
473/// | |
474/// | [Op]
475/// | ^
476/// | |
477/// \ /
478/// \ /
479/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000480void X86DAGToDAGISel::PreprocessForRMW() {
481 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
482 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000483 if (I->getOpcode() == X86ISD::CALL) {
484 /// Also try moving call address load from outside callseq_start to just
485 /// before the call to allow it to be folded.
486 ///
487 /// [Load chain]
488 /// ^
489 /// |
490 /// [Load]
491 /// ^ ^
492 /// | |
493 /// / \--
494 /// / |
495 ///[CALLSEQ_START] |
496 /// ^ |
497 /// | |
498 /// [LOAD/C2Reg] |
499 /// | |
500 /// \ /
501 /// \ /
502 /// [CALL]
503 SDValue Chain = I->getOperand(0);
504 SDValue Load = I->getOperand(1);
505 if (!isCalleeLoad(Load, Chain))
506 continue;
507 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
508 ++NumLoadMoved;
509 continue;
510 }
511
Evan Cheng8b2794a2006-10-13 21:14:26 +0000512 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000513 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000514 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000515
Gabor Greifba36cb52008-08-28 21:40:38 +0000516 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000517 continue;
518
Dan Gohman475871a2008-07-27 21:46:04 +0000519 SDValue N1 = I->getOperand(1);
520 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000521 if ((N1.getValueType().isFloatingPoint() &&
522 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000523 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000524 continue;
525
526 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000527 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000528 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000529 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000530 case ISD::ADD:
531 case ISD::MUL:
532 case ISD::AND:
533 case ISD::OR:
534 case ISD::XOR:
535 case ISD::ADDC:
536 case ISD::ADDE:
537 case ISD::VECTOR_SHUFFLE: {
538 SDValue N10 = N1.getOperand(0);
539 SDValue N11 = N1.getOperand(1);
540 RModW = isRMWLoad(N10, Chain, N2, Load);
541 if (!RModW)
542 RModW = isRMWLoad(N11, Chain, N2, Load);
543 break;
544 }
545 case ISD::SUB:
546 case ISD::SHL:
547 case ISD::SRA:
548 case ISD::SRL:
549 case ISD::ROTL:
550 case ISD::ROTR:
551 case ISD::SUBC:
552 case ISD::SUBE:
553 case X86ISD::SHLD:
554 case X86ISD::SHRD: {
555 SDValue N10 = N1.getOperand(0);
556 RModW = isRMWLoad(N10, Chain, N2, Load);
557 break;
558 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000559 }
560
Evan Cheng82a35b32006-08-29 06:44:17 +0000561 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000562 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000563 ++NumLoadMoved;
564 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000565 }
566}
567
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000568
569/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
570/// nodes that target the FP stack to be store and load to the stack. This is a
571/// gross hack. We would like to simply mark these as being illegal, but when
572/// we do that, legalize produces these when it expands calls, then expands
573/// these in the same legalize pass. We would like dag combine to be able to
574/// hack on these between the call expansion and the node legalization. As such
575/// this pass basically does "really late" legalization of these inline with the
576/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000577void X86DAGToDAGISel::PreprocessForFPConvert() {
578 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
579 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000580 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
581 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
582 continue;
583
584 // If the source and destination are SSE registers, then this is a legal
585 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000586 MVT SrcVT = N->getOperand(0).getValueType();
587 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000588 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
589 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
590 if (SrcIsSSE && DstIsSSE)
591 continue;
592
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000593 if (!SrcIsSSE && !DstIsSSE) {
594 // If this is an FPStack extension, it is a noop.
595 if (N->getOpcode() == ISD::FP_EXTEND)
596 continue;
597 // If this is a value-preserving FPStack truncation, it is a noop.
598 if (N->getConstantOperandVal(1))
599 continue;
600 }
601
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000602 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
603 // FPStack has extload and truncstore. SSE can fold direct loads into other
604 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000605 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000606 if (N->getOpcode() == ISD::FP_ROUND)
607 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
608 else
609 MemVT = SrcIsSSE ? SrcVT : DstVT;
610
Dan Gohmanf350b272008-08-23 02:25:05 +0000611 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000612 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000613
614 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000615 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000616 N->getOperand(0),
617 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000618 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000619 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000620
621 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
622 // extload we created. This will cause general havok on the dag because
623 // anything below the conversion could be folded into other existing nodes.
624 // To avoid invalidating 'I', back it up to the convert node.
625 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000626 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000627
628 // Now that we did that, the node is dead. Increment the iterator to the
629 // next node to process, then delete N.
630 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000631 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000632 }
633}
634
Chris Lattnerc961eea2005-11-16 01:54:32 +0000635/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
636/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000637void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000638 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000639 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000640
Evan Chengdb8d56b2008-06-30 20:45:06 +0000641 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000642 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000643 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000644
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000645 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000646 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000647
Chris Lattnerc961eea2005-11-16 01:54:32 +0000648 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000649#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000650 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000651 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000652#endif
David Greene8ad4c002008-10-27 21:56:29 +0000653 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000654#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000655 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000656#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000657
Dan Gohmanf350b272008-08-23 02:25:05 +0000658 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000659}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000660
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000661/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
662/// the main function.
663void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
664 MachineFrameInfo *MFI) {
665 const TargetInstrInfo *TII = TM.getInstrInfo();
666 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000667 BuildMI(BB, DebugLoc::getUnknownLoc(),
668 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000669}
670
671void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
672 // If this is main, emit special code for main.
673 MachineBasicBlock *BB = MF.begin();
674 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
675 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
676}
677
Rafael Espindola094fad32009-04-08 21:14:34 +0000678
679bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
680 X86ISelAddressMode &AM) {
681 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
682 SDValue Segment = N.getOperand(0);
683
684 if (AM.Segment.getNode() == 0) {
685 AM.Segment = Segment;
686 return false;
687 }
688
689 return true;
690}
691
692bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
693 // This optimization is valid because the GNU TLS model defines that
694 // gs:0 (or fs:0 on X86-64) contains its own address.
695 // For more information see http://people.redhat.com/drepper/tls.pdf
696
697 SDValue Address = N.getOperand(1);
698 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
699 !MatchSegmentBaseAddress (Address, AM))
700 return false;
701
702 return true;
703}
704
Chris Lattner18c59872009-06-27 04:16:01 +0000705/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
706/// into an addressing mode. These wrap things that will resolve down into a
707/// symbol reference. If no match is possible, this returns true, otherwise it
708/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000709bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000710 // If the addressing mode already has a symbol as the displacement, we can
711 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000712 if (AM.hasSymbolicDisplacement())
713 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000714
715 SDValue N0 = N.getOperand(0);
Chris Lattner18c59872009-06-27 04:16:01 +0000716
717 // Handle X86-64 rip-relative addresses. We check this before checking direct
718 // folding because RIP is preferable to non-RIP accesses.
719 if (Subtarget->is64Bit() &&
720 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
721 // they cannot be folded into immediate fields.
722 // FIXME: This can be improved for kernel and other models?
723 TM.getCodeModel() == CodeModel::Small &&
724
725 // Base and index reg must be 0 in order to use %rip as base and lowering
726 // must allow RIP.
727 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
728
729 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
730 int64_t Offset = AM.Disp + G->getOffset();
731 if (!isInt32(Offset)) return true;
732 AM.GV = G->getGlobal();
733 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000734 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000735 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
736 int64_t Offset = AM.Disp + CP->getOffset();
737 if (!isInt32(Offset)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000738 AM.CP = CP->getConstVal();
739 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000740 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000741 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000742 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
743 AM.ES = S->getSymbol();
744 AM.SymbolFlags = S->getTargetFlags();
745 } else {
746 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
747 AM.JT = J->getIndex();
748 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000749 }
Chris Lattner18c59872009-06-27 04:16:01 +0000750
751 if (N.getOpcode() == X86ISD::WrapperRIP)
752 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000753 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000754 }
755
756 // Handle the case when globals fit in our immediate field: This is true for
757 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
758 // mode, this results in a non-RIP-relative computation.
759 if (!Subtarget->is64Bit() ||
760 (TM.getCodeModel() == CodeModel::Small &&
761 TM.getRelocationModel() == Reloc::Static)) {
762 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
763 AM.GV = G->getGlobal();
764 AM.Disp += G->getOffset();
765 AM.SymbolFlags = G->getTargetFlags();
766 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
767 AM.CP = CP->getConstVal();
768 AM.Align = CP->getAlignment();
769 AM.Disp += CP->getOffset();
770 AM.SymbolFlags = CP->getTargetFlags();
771 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
772 AM.ES = S->getSymbol();
773 AM.SymbolFlags = S->getTargetFlags();
774 } else {
775 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
776 AM.JT = J->getIndex();
777 AM.SymbolFlags = J->getTargetFlags();
778 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000779 return false;
780 }
781
782 return true;
783}
784
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000785/// MatchAddress - Add the specified node to the specified addressing mode,
786/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000787/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000788bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
789 if (MatchAddressRecursively(N, AM, 0))
790 return true;
791
792 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
793 // a smaller encoding and avoids a scaled-index.
794 if (AM.Scale == 2 &&
795 AM.BaseType == X86ISelAddressMode::RegBase &&
796 AM.Base.Reg.getNode() == 0) {
797 AM.Base.Reg = AM.IndexReg;
798 AM.Scale = 1;
799 }
800
801 return false;
802}
803
804bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
805 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000806 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000807 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000808 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000809 // Limit recursion.
810 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000811 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000812
Chris Lattner18c59872009-06-27 04:16:01 +0000813 // If this is already a %rip relative address, we can only merge immediates
814 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000815 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000816 if (AM.isRIPRelative()) {
817 // FIXME: JumpTable and ExternalSymbol address currently don't like
818 // displacements. It isn't very important, but this should be fixed for
819 // consistency.
820 if (!AM.ES && AM.JT != -1) return true;
821
822 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
823 int64_t Val = AM.Disp + Cst->getSExtValue();
824 if (isInt32(Val)) {
825 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000826 return false;
827 }
828 }
829 return true;
830 }
831
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000832 switch (N.getOpcode()) {
833 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000834 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000835 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000836 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000837 AM.Disp += Val;
838 return false;
839 }
840 break;
841 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000842
Rafael Espindola094fad32009-04-08 21:14:34 +0000843 case X86ISD::SegmentBaseAddress:
844 if (!MatchSegmentBaseAddress(N, AM))
845 return false;
846 break;
847
Rafael Espindola49a168d2009-04-12 21:55:03 +0000848 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000849 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000850 if (!MatchWrapper(N, AM))
851 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000852 break;
853
Rafael Espindola094fad32009-04-08 21:14:34 +0000854 case ISD::LOAD:
855 if (!MatchLoad(N, AM))
856 return false;
857 break;
858
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000859 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000860 if (AM.BaseType == X86ISelAddressMode::RegBase
861 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000862 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
863 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
864 return false;
865 }
866 break;
Evan Chengec693f72005-12-08 02:01:35 +0000867
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000868 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000869 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000870 break;
871
Gabor Greif93c53e52008-08-31 15:37:04 +0000872 if (ConstantSDNode
873 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000874 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000875 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
876 // that the base operand remains free for further matching. If
877 // the base doesn't end up getting used, a post-processing step
878 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000879 if (Val == 1 || Val == 2 || Val == 3) {
880 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000881 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000882
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000883 // Okay, we know that we have a scale by now. However, if the scaled
884 // value is an add of something and a constant, we can fold the
885 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000886 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
887 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
888 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000889 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000890 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000891 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000892 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000893 AM.Disp = Disp;
894 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000895 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000896 } else {
897 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000898 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000899 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000900 }
901 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000902 }
Evan Chengec693f72005-12-08 02:01:35 +0000903
Dan Gohman83688052007-10-22 20:22:24 +0000904 case ISD::SMUL_LOHI:
905 case ISD::UMUL_LOHI:
906 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000907 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000908 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000909 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000910 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000911 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000912 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000913 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000914 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000915 if (ConstantSDNode
916 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000917 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
918 CN->getZExtValue() == 9) {
919 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000920
Gabor Greifba36cb52008-08-28 21:40:38 +0000921 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000922 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000923
924 // Okay, we know that we have a scale by now. However, if the scaled
925 // value is an add of something and a constant, we can fold the
926 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000927 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
928 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
929 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000930 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000931 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000932 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000933 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000934 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000935 AM.Disp = Disp;
936 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000937 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000938 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000939 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000940 }
941
942 AM.IndexReg = AM.Base.Reg = Reg;
943 return false;
944 }
Chris Lattner62412262007-02-04 20:18:17 +0000945 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000946 break;
947
Dan Gohman3cd90a12009-05-11 18:02:53 +0000948 case ISD::SUB: {
949 // Given A-B, if A can be completely folded into the address and
950 // the index field with the index field unused, use -B as the index.
951 // This is a win if a has multiple parts that can be folded into
952 // the address. Also, this saves a mov if the base register has
953 // other uses, since it avoids a two-address sub instruction, however
954 // it costs an additional mov if the index register has other uses.
955
956 // Test if the LHS of the sub can be folded.
957 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000958 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000959 AM = Backup;
960 break;
961 }
962 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000963 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000964 AM = Backup;
965 break;
966 }
967 int Cost = 0;
968 SDValue RHS = N.getNode()->getOperand(1);
969 // If the RHS involves a register with multiple uses, this
970 // transformation incurs an extra mov, due to the neg instruction
971 // clobbering its operand.
972 if (!RHS.getNode()->hasOneUse() ||
973 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
974 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
975 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
976 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
977 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
978 ++Cost;
979 // If the base is a register with multiple uses, this
980 // transformation may save a mov.
981 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
982 AM.Base.Reg.getNode() &&
983 !AM.Base.Reg.getNode()->hasOneUse()) ||
984 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
985 --Cost;
986 // If the folded LHS was interesting, this transformation saves
987 // address arithmetic.
988 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
989 ((AM.Disp != 0) && (Backup.Disp == 0)) +
990 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
991 --Cost;
992 // If it doesn't look like it may be an overall win, don't do it.
993 if (Cost >= 0) {
994 AM = Backup;
995 break;
996 }
997
998 // Ok, the transformation is legal and appears profitable. Go for it.
999 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1000 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1001 AM.IndexReg = Neg;
1002 AM.Scale = 1;
1003
1004 // Insert the new nodes into the topological ordering.
1005 if (Zero.getNode()->getNodeId() == -1 ||
1006 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1007 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1008 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1009 }
1010 if (Neg.getNode()->getNodeId() == -1 ||
1011 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1012 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1013 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1014 }
1015 return false;
1016 }
1017
Evan Cheng8e278262009-01-17 07:09:27 +00001018 case ISD::ADD: {
1019 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001020 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1021 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001022 return false;
1023 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001024 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1025 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001026 return false;
1027 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001028
1029 // If we couldn't fold both operands into the address at the same time,
1030 // see if we can just put each operand into a register and fold at least
1031 // the add.
1032 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1033 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001034 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001035 AM.Base.Reg = N.getNode()->getOperand(0);
1036 AM.IndexReg = N.getNode()->getOperand(1);
1037 AM.Scale = 1;
1038 return false;
1039 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001040 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001041 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001042
Chris Lattner62412262007-02-04 20:18:17 +00001043 case ISD::OR:
1044 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001045 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1046 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001047 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001048 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001049 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001050 // Address could not have picked a GV address for the displacement.
1051 AM.GV == NULL &&
1052 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +00001053 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001054 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001055 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001056 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001057 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001058 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001059 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001060 }
1061 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001062
1063 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001064 // Perform some heroic transforms on an and of a constant-count shift
1065 // with a constant to enable use of the scaled offset field.
1066
Dan Gohman475871a2008-07-27 21:46:04 +00001067 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001068 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001069
Evan Cheng1314b002007-12-13 00:43:27 +00001070 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001071 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001072
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001073 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001074 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1075 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1076 if (!C1 || !C2) break;
1077
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001078 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1079 // allows us to convert the shift and and into an h-register extract and
1080 // a scaled index.
1081 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1082 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001083 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001084 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1085 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1086 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1087 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1088 X, Eight);
1089 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1090 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +00001091 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1092 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1093 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001094
1095 // Insert the new nodes into the topological ordering.
1096 if (Eight.getNode()->getNodeId() == -1 ||
1097 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1098 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1099 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1100 }
1101 if (Mask.getNode()->getNodeId() == -1 ||
1102 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1103 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1104 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1105 }
1106 if (Srl.getNode()->getNodeId() == -1 ||
1107 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1108 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1109 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1110 }
1111 if (And.getNode()->getNodeId() == -1 ||
1112 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1113 CurDAG->RepositionNode(N.getNode(), And.getNode());
1114 And.getNode()->setNodeId(N.getNode()->getNodeId());
1115 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001116 if (ShlCount.getNode()->getNodeId() == -1 ||
1117 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1118 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1119 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1120 }
1121 if (Shl.getNode()->getNodeId() == -1 ||
1122 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1123 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1124 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1125 }
1126 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001127 AM.IndexReg = And;
1128 AM.Scale = (1 << ScaleLog);
1129 return false;
1130 }
1131 }
1132
1133 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1134 // allows us to fold the shift into this addressing mode.
1135 if (Shift.getOpcode() != ISD::SHL) break;
1136
Evan Cheng1314b002007-12-13 00:43:27 +00001137 // Not likely to be profitable if either the AND or SHIFT node has more
1138 // than one use (unless all uses are for address computation). Besides,
1139 // isel mechanism requires their node ids to be reused.
1140 if (!N.hasOneUse() || !Shift.hasOneUse())
1141 break;
1142
1143 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001144 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001145 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1146 break;
1147
1148 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001149 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001150 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001151 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1152 NewANDMask);
1153 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001154 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001155
1156 // Insert the new nodes into the topological ordering.
1157 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1158 CurDAG->RepositionNode(X.getNode(), C1);
1159 C1->setNodeId(X.getNode()->getNodeId());
1160 }
1161 if (NewANDMask.getNode()->getNodeId() == -1 ||
1162 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1163 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1164 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1165 }
1166 if (NewAND.getNode()->getNodeId() == -1 ||
1167 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1168 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1169 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1170 }
1171 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1172 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1173 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1174 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1175 }
1176
Dan Gohman7b8e9642008-10-13 20:52:04 +00001177 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001178
1179 AM.Scale = 1 << ShiftCst;
1180 AM.IndexReg = NewAND;
1181 return false;
1182 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001183 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001184
Rafael Espindola523249f2009-03-31 16:16:57 +00001185 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001186}
1187
1188/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1189/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001190bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001191 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001192 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001193 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001194 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001195 AM.IndexReg = N;
1196 AM.Scale = 1;
1197 return false;
1198 }
1199
1200 // Otherwise, we cannot select it.
1201 return true;
1202 }
1203
1204 // Default, generate it as a register.
1205 AM.BaseType = X86ISelAddressMode::RegBase;
1206 AM.Base.Reg = N;
1207 return false;
1208}
1209
Evan Chengec693f72005-12-08 02:01:35 +00001210/// SelectAddr - returns true if it is able pattern match an addressing mode.
1211/// It returns the operands which make up the maximal addressing mode it can
1212/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001213bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1214 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001215 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001216 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001217 bool Done = false;
1218 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1219 unsigned Opcode = N.getOpcode();
1220 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001221 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001222 // If we are able to fold N into addressing mode, then we'll allow it even
1223 // if N has multiple uses. In general, addressing computation is used as
1224 // addresses by all of its uses. But watch out for CopyToReg uses, that
1225 // means the address computation is liveout. It will be computed by a LEA
1226 // so we want to avoid computing the address twice.
1227 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1228 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1229 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001230 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001231 Done = true;
1232 break;
1233 }
1234 }
1235 }
1236 }
1237
1238 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001239 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001240
Duncan Sands83ec4b62008-06-06 12:08:01 +00001241 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001242 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001243 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001244 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001245 }
Evan Cheng8700e142006-01-11 06:09:51 +00001246
Gabor Greifba36cb52008-08-28 21:40:38 +00001247 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001248 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001249
Rafael Espindola094fad32009-04-08 21:14:34 +00001250 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001251 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001252}
1253
Chris Lattner3a7cd952006-10-07 21:55:32 +00001254/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1255/// match a load whose top elements are either undef or zeros. The load flavor
1256/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001257bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1258 SDValue N, SDValue &Base,
1259 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001260 SDValue &Disp, SDValue &Segment,
1261 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001262 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001263 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001264 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001265 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001266 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001267 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001268 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001269 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001270 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001271 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001272 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001273 return true;
1274 }
1275 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001276
1277 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001278 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001279 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001280 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001281 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001282 N.getOperand(0).getNode()->hasOneUse() &&
1283 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001284 N.getOperand(0).getOperand(0).hasOneUse()) {
1285 // Okay, this is a zero extending load. Fold it.
1286 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001287 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001288 return false;
1289 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001290 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001291 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001292 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001293 return false;
1294}
1295
1296
Evan Cheng51a9ed92006-02-25 10:09:08 +00001297/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1298/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001299bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1300 SDValue &Base, SDValue &Scale,
1301 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001302 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001303
1304 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1305 // segments.
1306 SDValue Copy = AM.Segment;
1307 SDValue T = CurDAG->getRegister(0, MVT::i32);
1308 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001309 if (MatchAddress(N, AM))
1310 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001311 assert (T == AM.Segment);
1312 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001313
Duncan Sands83ec4b62008-06-06 12:08:01 +00001314 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001315 unsigned Complexity = 0;
1316 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001317 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001318 Complexity = 1;
1319 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001320 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001321 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1322 Complexity = 4;
1323
Gabor Greifba36cb52008-08-28 21:40:38 +00001324 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001325 Complexity++;
1326 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001327 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001328
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001329 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1330 // a simple shift.
1331 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001332 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001333
1334 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1335 // to a LEA. This is determined with some expermentation but is by no means
1336 // optimal (especially for code size consideration). LEA is nice because of
1337 // its three-address nature. Tweak the cost function again when we can run
1338 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001339 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001340 // For X86-64, we should always use lea to materialize RIP relative
1341 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001342 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001343 Complexity = 4;
1344 else
1345 Complexity += 2;
1346 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001347
Gabor Greifba36cb52008-08-28 21:40:38 +00001348 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001349 Complexity++;
1350
Chris Lattner25142782009-07-11 22:50:33 +00001351 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001352 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001353 return false;
1354
1355 SDValue Segment;
1356 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1357 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001358}
1359
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001360/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1361bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1362 SDValue &Scale, SDValue &Index,
1363 SDValue &Disp) {
1364 assert(Op.getOpcode() == X86ISD::TLSADDR);
1365 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1366 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1367
1368 X86ISelAddressMode AM;
1369 AM.GV = GA->getGlobal();
1370 AM.Disp += GA->getOffset();
1371 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001372 AM.SymbolFlags = GA->getTargetFlags();
1373
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001374 if (N.getValueType() == MVT::i32) {
1375 AM.Scale = 1;
1376 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1377 } else {
1378 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1379 }
1380
1381 SDValue Segment;
1382 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1383 return true;
1384}
1385
1386
Dan Gohman475871a2008-07-27 21:46:04 +00001387bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1388 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001389 SDValue &Index, SDValue &Disp,
1390 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001391 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001392 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001393 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001394 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001395 return false;
1396}
1397
Dan Gohman8b746962008-09-23 18:22:58 +00001398/// getGlobalBaseReg - Return an SDNode that returns the value of
1399/// the global base register. Output instructions required to
1400/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001401///
Evan Cheng9ade2182006-08-26 05:34:46 +00001402SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001403 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001404 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001405}
1406
Evan Chengb245d922006-05-20 01:36:52 +00001407static SDNode *FindCallStartFromCall(SDNode *Node) {
1408 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1409 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1410 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001411 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001412}
1413
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001414SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1415 SDValue Chain = Node->getOperand(0);
1416 SDValue In1 = Node->getOperand(1);
1417 SDValue In2L = Node->getOperand(2);
1418 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001419 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1420 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001421 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001422 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001423 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001424 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1425 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001426 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001427}
Christopher Lambc59e5212007-08-10 21:48:46 +00001428
Evan Cheng37b73872009-07-30 08:33:02 +00001429SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, MVT NVT) {
1430 if (Node->hasAnyUseOfValue(0))
1431 return 0;
1432
1433 // Optimize common patterns for __sync_add_and_fetch and
1434 // __sync_sub_and_fetch where the result is not used. This allows us
1435 // to use "lock" version of add, sub, inc, dec instructions.
1436 // FIXME: Do not use special instructions but instead add the "lock"
1437 // prefix to the target node somehow. The extra information will then be
1438 // transferred to machine instruction and it denotes the prefix.
1439 SDValue Chain = Node->getOperand(0);
1440 SDValue Ptr = Node->getOperand(1);
1441 SDValue Val = Node->getOperand(2);
1442 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1443 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1444 return 0;
1445
1446 bool isInc = false, isDec = false, isSub = false, isCN = false;
1447 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1448 if (CN) {
1449 isCN = true;
1450 int64_t CNVal = CN->getSExtValue();
1451 if (CNVal == 1)
1452 isInc = true;
1453 else if (CNVal == -1)
1454 isDec = true;
1455 else if (CNVal >= 0)
1456 Val = CurDAG->getTargetConstant(CNVal, NVT);
1457 else {
1458 isSub = true;
1459 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1460 }
1461 } else if (Val.hasOneUse() &&
1462 Val.getOpcode() == ISD::SUB &&
1463 X86::isZeroNode(Val.getOperand(0))) {
1464 isSub = true;
1465 Val = Val.getOperand(1);
1466 }
1467
1468 unsigned Opc = 0;
1469 switch (NVT.getSimpleVT()) {
1470 default: return 0;
1471 case MVT::i8:
1472 if (isInc)
1473 Opc = X86::LOCK_INC8m;
1474 else if (isDec)
1475 Opc = X86::LOCK_DEC8m;
1476 else if (isSub) {
1477 if (isCN)
1478 Opc = X86::LOCK_SUB8mi;
1479 else
1480 Opc = X86::LOCK_SUB8mr;
1481 } else {
1482 if (isCN)
1483 Opc = X86::LOCK_ADD8mi;
1484 else
1485 Opc = X86::LOCK_ADD8mr;
1486 }
1487 break;
1488 case MVT::i16:
1489 if (isInc)
1490 Opc = X86::LOCK_INC16m;
1491 else if (isDec)
1492 Opc = X86::LOCK_DEC16m;
1493 else if (isSub) {
1494 if (isCN) {
1495 if (Predicate_i16immSExt8(Val.getNode()))
1496 Opc = X86::LOCK_SUB16mi8;
1497 else
1498 Opc = X86::LOCK_SUB16mi;
1499 } else
1500 Opc = X86::LOCK_SUB16mr;
1501 } else {
1502 if (isCN) {
1503 if (Predicate_i16immSExt8(Val.getNode()))
1504 Opc = X86::LOCK_ADD16mi8;
1505 else
1506 Opc = X86::LOCK_ADD16mi;
1507 } else
1508 Opc = X86::LOCK_ADD16mr;
1509 }
1510 break;
1511 case MVT::i32:
1512 if (isInc)
1513 Opc = X86::LOCK_INC32m;
1514 else if (isDec)
1515 Opc = X86::LOCK_DEC32m;
1516 else if (isSub) {
1517 if (isCN) {
1518 if (Predicate_i32immSExt8(Val.getNode()))
1519 Opc = X86::LOCK_SUB32mi8;
1520 else
1521 Opc = X86::LOCK_SUB32mi;
1522 } else
1523 Opc = X86::LOCK_SUB32mr;
1524 } else {
1525 if (isCN) {
1526 if (Predicate_i32immSExt8(Val.getNode()))
1527 Opc = X86::LOCK_ADD32mi8;
1528 else
1529 Opc = X86::LOCK_ADD32mi;
1530 } else
1531 Opc = X86::LOCK_ADD32mr;
1532 }
1533 break;
1534 case MVT::i64:
1535 if (isInc)
1536 Opc = X86::LOCK_INC64m;
1537 else if (isDec)
1538 Opc = X86::LOCK_DEC64m;
1539 else if (isSub) {
1540 Opc = X86::LOCK_SUB64mr;
1541 if (isCN) {
1542 if (Predicate_i64immSExt8(Val.getNode()))
1543 Opc = X86::LOCK_SUB64mi8;
1544 else if (Predicate_i64immSExt32(Val.getNode()))
1545 Opc = X86::LOCK_SUB64mi32;
1546 }
1547 } else {
1548 Opc = X86::LOCK_ADD64mr;
1549 if (isCN) {
1550 if (Predicate_i64immSExt8(Val.getNode()))
1551 Opc = X86::LOCK_ADD64mi8;
1552 else if (Predicate_i64immSExt32(Val.getNode()))
1553 Opc = X86::LOCK_ADD64mi32;
1554 }
1555 }
1556 break;
1557 }
1558
1559 DebugLoc dl = Node->getDebugLoc();
1560 SDValue Undef = SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1561 dl, NVT), 0);
1562 SDValue MemOp = CurDAG->getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
1563 if (isInc || isDec) {
1564 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, MemOp, Chain };
1565 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 7), 0);
1566 SDValue RetVals[] = { Undef, Ret };
1567 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1568 } else {
1569 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, MemOp, Chain };
1570 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 8), 0);
1571 SDValue RetVals[] = { Undef, Ret };
1572 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1573 }
1574}
1575
Dan Gohman475871a2008-07-27 21:46:04 +00001576SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001577 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001578 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001579 unsigned Opc, MOpc;
1580 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001581 DebugLoc dl = Node->getDebugLoc();
1582
Evan Chengf597dc72006-02-10 22:24:32 +00001583#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001584 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001585 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001586 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001587 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001588#endif
1589
Dan Gohmane8be6c62008-07-17 19:10:17 +00001590 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001591#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001592 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001593 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001594 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001595 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001596#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001597 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001598 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001599
Evan Cheng0114e942006-01-06 20:36:21 +00001600 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001601 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001602 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001603 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001604
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001605 case X86ISD::ATOMOR64_DAG:
1606 return SelectAtomic64(Node, X86::ATOMOR6432);
1607 case X86ISD::ATOMXOR64_DAG:
1608 return SelectAtomic64(Node, X86::ATOMXOR6432);
1609 case X86ISD::ATOMADD64_DAG:
1610 return SelectAtomic64(Node, X86::ATOMADD6432);
1611 case X86ISD::ATOMSUB64_DAG:
1612 return SelectAtomic64(Node, X86::ATOMSUB6432);
1613 case X86ISD::ATOMNAND64_DAG:
1614 return SelectAtomic64(Node, X86::ATOMNAND6432);
1615 case X86ISD::ATOMAND64_DAG:
1616 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001617 case X86ISD::ATOMSWAP64_DAG:
1618 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001619
Evan Cheng37b73872009-07-30 08:33:02 +00001620 case ISD::ATOMIC_LOAD_ADD: {
1621 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1622 if (RetVal)
1623 return RetVal;
1624 break;
1625 }
1626
Dan Gohman525178c2007-10-08 18:33:35 +00001627 case ISD::SMUL_LOHI:
1628 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001629 SDValue N0 = Node->getOperand(0);
1630 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001631
Dan Gohman525178c2007-10-08 18:33:35 +00001632 bool isSigned = Opcode == ISD::SMUL_LOHI;
1633 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001634 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001635 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001636 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1637 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1638 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001639 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001640 }
1641 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001642 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001643 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001644 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1645 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1646 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001647 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001648 }
1649
1650 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001651 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001652 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001653 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1654 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1655 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001656 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001657 }
1658
Rafael Espindola094fad32009-04-08 21:14:34 +00001659 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1660 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001661 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001662 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001663 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001664 if (foldedLoad)
1665 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001666 }
1667
Dale Johannesendd64c412009-02-04 00:33:20 +00001668 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001669 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001670
1671 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001672 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1673 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001674 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001675 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001676 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001677 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001678 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001679 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001680 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001681 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001682 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001683 }
1684
Dan Gohman525178c2007-10-08 18:33:35 +00001685 // Copy the low half of the result, if it is needed.
1686 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001687 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001688 LoReg, NVT, InFlag);
1689 InFlag = Result.getValue(2);
1690 ReplaceUses(N.getValue(0), Result);
1691#ifndef NDEBUG
1692 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001693 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001694 DOUT << "\n";
1695#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001696 }
Dan Gohman525178c2007-10-08 18:33:35 +00001697 // Copy the high half of the result, if it is needed.
1698 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001699 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001700 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1701 // Prevent use of AH in a REX instruction by referencing AX instead.
1702 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001703 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001704 X86::AX, MVT::i16, InFlag);
1705 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001706 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1707 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001708 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001709 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001710 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001711 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001712 MVT::i8, Result, SRIdx), 0);
1713 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001714 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001715 HiReg, NVT, InFlag);
1716 InFlag = Result.getValue(2);
1717 }
1718 ReplaceUses(N.getValue(1), Result);
1719#ifndef NDEBUG
1720 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001721 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001722 DOUT << "\n";
1723#endif
1724 }
Evan Cheng34167212006-02-09 00:37:58 +00001725
Evan Chengf597dc72006-02-10 22:24:32 +00001726#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001727 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001728#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001729
Evan Cheng64a752f2006-08-11 09:08:15 +00001730 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001731 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001732
Dan Gohman525178c2007-10-08 18:33:35 +00001733 case ISD::SDIVREM:
1734 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001735 SDValue N0 = Node->getOperand(0);
1736 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001737
1738 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001739 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001740 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001741 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001742 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1743 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1744 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001745 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001746 }
1747 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001748 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001749 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001750 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1751 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1752 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001753 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001754 }
1755
1756 unsigned LoReg, HiReg;
1757 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001758 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001759 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001760 case MVT::i8:
1761 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001762 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001763 SExtOpcode = X86::CBW;
1764 break;
1765 case MVT::i16:
1766 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001767 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001768 SExtOpcode = X86::CWD;
1769 break;
1770 case MVT::i32:
1771 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001772 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001773 SExtOpcode = X86::CDQ;
1774 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001775 case MVT::i64:
1776 LoReg = X86::RAX; HiReg = X86::RDX;
Chris Lattner9ac75422009-07-14 20:19:57 +00001777 ClrOpcode = ~0U; // NOT USED.
Evan Cheng25ab6902006-09-08 06:48:29 +00001778 SExtOpcode = X86::CQO;
1779 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001780 }
1781
Rafael Espindola094fad32009-04-08 21:14:34 +00001782 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1783 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001784 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001785
Dan Gohman475871a2008-07-27 21:46:04 +00001786 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001787 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001788 // Special case for div8, just use a move with zero extension to AX to
1789 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001790 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1791 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1792 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001793 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001794 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001795 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001796 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001797 Chain = Move.getValue(1);
1798 ReplaceUses(N0.getValue(1), Chain);
1799 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001800 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001801 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001802 Chain = CurDAG->getEntryNode();
1803 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001804 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001805 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001806 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001807 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001808 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001809 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001810 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001811 // Sign extend the low part into the high part.
1812 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001813 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001814 } else {
1815 // Zero out the high part, effectively zero extending the input.
Chris Lattner9ac75422009-07-14 20:19:57 +00001816 SDValue ClrNode;
1817
1818 if (NVT.getSimpleVT() == MVT::i64) {
1819 ClrNode = SDValue(CurDAG->getTargetNode(X86::MOV32r0, dl, MVT::i32),
1820 0);
1821 // We just did a 32-bit clear, insert it into a 64-bit register to
1822 // clear the whole 64-bit reg.
1823 SDValue Undef =
1824 SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1825 dl, MVT::i64), 0);
1826 SDValue SubRegNo =
1827 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
1828 ClrNode =
1829 SDValue(CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl,
1830 MVT::i64, Undef, ClrNode, SubRegNo),
1831 0);
1832 } else {
1833 ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT), 0);
1834 }
1835
Dale Johannesendd64c412009-02-04 00:33:20 +00001836 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001837 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001838 }
Evan Cheng948f3432006-01-06 23:19:29 +00001839 }
1840
1841 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001842 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1843 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001844 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001845 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001846 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001847 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001848 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001849 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001850 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001851 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001852 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001853 }
1854
Dan Gohmana37c9f72007-09-25 18:23:27 +00001855 // Copy the division (low) result, if it is needed.
1856 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001857 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001858 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001859 InFlag = Result.getValue(2);
1860 ReplaceUses(N.getValue(0), Result);
1861#ifndef NDEBUG
1862 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001863 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001864 DOUT << "\n";
1865#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001866 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001867 // Copy the remainder (high) result, if it is needed.
1868 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001869 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001870 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1871 // Prevent use of AH in a REX instruction by referencing AX instead.
1872 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001873 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001874 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001875 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001876 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1877 Result,
1878 CurDAG->getTargetConstant(8, MVT::i8)),
1879 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001880 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001881 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001882 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001883 MVT::i8, Result, SRIdx), 0);
1884 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001885 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001886 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001887 InFlag = Result.getValue(2);
1888 }
1889 ReplaceUses(N.getValue(1), Result);
1890#ifndef NDEBUG
1891 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001892 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001893 DOUT << "\n";
1894#endif
1895 }
Evan Chengf597dc72006-02-10 22:24:32 +00001896
1897#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001898 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001899#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001900
1901 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001902 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001903
Evan Cheng851bc042008-06-17 02:01:22 +00001904 case ISD::DECLARE: {
1905 // Handle DECLARE nodes here because the second operand may have been
1906 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001907 SDValue Chain = Node->getOperand(0);
1908 SDValue N1 = Node->getOperand(1);
1909 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001910 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001911
1912 // FIXME: We need to handle this for VLAs.
1913 if (!FINode) {
1914 ReplaceUses(N.getValue(0), Chain);
1915 return NULL;
1916 }
1917
Evan Chengfab83872008-06-18 02:48:27 +00001918 if (N2.getOpcode() == ISD::ADD &&
1919 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1920 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001921
1922 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1923 // somehow, just ignore it.
Chris Lattner18c59872009-06-27 04:16:01 +00001924 if (N2.getOpcode() != X86ISD::Wrapper &&
1925 N2.getOpcode() != X86ISD::WrapperRIP) {
Chris Lattner1823c922009-02-12 17:33:11 +00001926 ReplaceUses(N.getValue(0), Chain);
1927 return NULL;
1928 }
Evan Chengf2accb52009-01-10 03:33:22 +00001929 GlobalAddressSDNode *GVNode =
1930 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001931 if (GVNode == 0) {
1932 ReplaceUses(N.getValue(0), Chain);
1933 return NULL;
1934 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001935 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1936 TLI.getPointerTy());
1937 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1938 TLI.getPointerTy());
1939 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001940 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001941 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001942 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001943 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001944 }
1945
Evan Cheng9ade2182006-08-26 05:34:46 +00001946 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001947
Evan Chengf597dc72006-02-10 22:24:32 +00001948#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001949 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001950 if (ResNode == NULL || ResNode == N.getNode())
1951 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001952 else
1953 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001954 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001955 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001956#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001957
1958 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001959}
1960
Chris Lattnerc0bad572006-06-08 18:03:49 +00001961bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001962SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001963 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001964 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001965 switch (ConstraintCode) {
1966 case 'o': // offsetable ??
1967 case 'v': // not offsetable ??
1968 default: return true;
1969 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001970 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001971 return true;
1972 break;
1973 }
1974
Evan Cheng04699902006-08-26 01:05:16 +00001975 OutOps.push_back(Op0);
1976 OutOps.push_back(Op1);
1977 OutOps.push_back(Op2);
1978 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001979 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001980 return false;
1981}
1982
Chris Lattnerc961eea2005-11-16 01:54:32 +00001983/// createX86ISelDag - This pass converts a legalized DAG into a
1984/// X86-specific DAG, ready for instruction scheduling.
1985///
Bill Wendling98a366d2009-04-29 23:29:43 +00001986FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1987 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001988 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001989}