blob: 5fb496ba0b4448f125c6547fae6f2c72b55c73a9 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000038#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000039#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000040#include "llvm/Support/Streams.h"
Torok Edwindac237e2009-07-08 20:53:28 +000041#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000043#include "llvm/ADT/Statistic.h"
44using namespace llvm;
45
Evan Cheng4d952322009-03-31 01:13:53 +000046#include "llvm/Support/CommandLine.h"
47static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
48
Chris Lattner95b2c7d2006-12-19 22:59:26 +000049STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
50
Chris Lattnerc961eea2005-11-16 01:54:32 +000051//===----------------------------------------------------------------------===//
52// Pattern Matcher Implementation
53//===----------------------------------------------------------------------===//
54
55namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000057 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 /// tree.
59 struct X86ISelAddressMode {
60 enum {
61 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000062 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000063 } BaseType;
64
65 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000066 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000067 int FrameIndex;
68 } Base;
69
70 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000071 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000072 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000073 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000075 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000076 const char *ES;
77 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000078 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000079 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000080
81 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000082 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattnerb8afeb92009-06-26 05:51:45 +000083 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0), SymbolFlags(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000084 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000085
86 bool hasSymbolicDisplacement() const {
87 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
88 }
Chris Lattner18c59872009-06-27 04:16:01 +000089
90 bool hasBaseOrIndexReg() const {
91 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
92 }
93
94 /// isRIPRelative - Return true if this addressing mode is already RIP
95 /// relative.
96 bool isRIPRelative() const {
97 if (BaseType != RegBase) return false;
98 if (RegisterSDNode *RegNode =
99 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
100 return RegNode->getReg() == X86::RIP;
101 return false;
102 }
103
104 void setBaseReg(SDValue Reg) {
105 BaseType = RegBase;
106 Base.Reg = Reg;
107 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000108
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000109 void dump() {
110 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000111 cerr << "Base.Reg ";
112 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
113 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000114 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
Chris Lattner18c59872009-06-27 04:16:01 +0000115 cerr << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000116 cerr << "IndexReg ";
117 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
118 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000119 cerr << " Disp " << Disp << "\n";
120 cerr << "GV "; if (GV) GV->dump();
121 else cerr << "nul";
122 cerr << " CP "; if (CP) CP->dump();
123 else cerr << "nul";
124 cerr << "\n";
125 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
126 cerr << " JT" << JT << " Align" << Align << "\n";
127 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000128 };
129}
130
131namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000132 //===--------------------------------------------------------------------===//
133 /// ISel - X86 specific code to select X86 machine instructions for
134 /// SelectionDAG operations.
135 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000136 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000137 /// X86Lowering - This object fully describes how to lower LLVM code to an
138 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000139 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000140
141 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
142 /// make the right decision when generating code for different targets.
143 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000144
Evan Chengdb8d56b2008-06-30 20:45:06 +0000145 /// CurBB - Current BB being isel'd.
146 ///
147 MachineBasicBlock *CurBB;
148
Evan Chengb7a75a52008-09-26 23:41:32 +0000149 /// OptForSize - If true, selector should try to optimize for code size
150 /// instead of performance.
151 bool OptForSize;
152
Chris Lattnerc961eea2005-11-16 01:54:32 +0000153 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000154 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000155 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000156 X86Lowering(*tm.getTargetLowering()),
157 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000158 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000159
160 virtual const char *getPassName() const {
161 return "X86 DAG->DAG Instruction Selection";
162 }
163
Evan Chengdb8d56b2008-06-30 20:45:06 +0000164 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000165 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000166 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000167
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000168 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
169
Evan Cheng884c70c2008-11-27 00:49:46 +0000170 virtual
171 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000172
Chris Lattnerc961eea2005-11-16 01:54:32 +0000173// Include the pieces autogenerated from the target description.
174#include "X86GenDAGISel.inc"
175
176 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000177 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000178 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Evan Cheng37b73872009-07-30 08:33:02 +0000179 SDNode *SelectAtomicLoadAdd(SDNode *Node, MVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000180
Rafael Espindola094fad32009-04-08 21:14:34 +0000181 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
182 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000183 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000184 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
185 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
186 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000187 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000188 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000189 SDValue &Scale, SDValue &Index, SDValue &Disp,
190 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000191 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
192 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000193 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
194 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000195 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
196 SDValue N, SDValue &Base, SDValue &Scale,
197 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000198 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000199 SDValue &InChain, SDValue &OutChain);
200 bool TryFoldLoad(SDValue P, SDValue N,
201 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000202 SDValue &Index, SDValue &Disp,
203 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000204 void PreprocessForRMW();
205 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000206
Chris Lattnerc0bad572006-06-08 18:03:49 +0000207 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
208 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000209 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000210 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000211 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000212
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000213 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
214
Dan Gohman475871a2008-07-27 21:46:04 +0000215 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
216 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000217 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000218 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
220 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000221 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000222 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 // These are 32-bit even in 64-bit mode since RIP relative offset
224 // is 32-bit.
225 if (AM.GV)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000226 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
227 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000228 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000229 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000230 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 else if (AM.ES)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000232 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000233 else if (AM.JT != -1)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000234 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000235 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000236 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000237
238 if (AM.Segment.getNode())
239 Segment = AM.Segment;
240 else
241 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000242 }
243
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000244 /// getI8Imm - Return a target constant with the specified value, of type
245 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000246 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000247 return CurDAG->getTargetConstant(Imm, MVT::i8);
248 }
249
Chris Lattnerc961eea2005-11-16 01:54:32 +0000250 /// getI16Imm - Return a target constant with the specified value, of type
251 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000252 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000253 return CurDAG->getTargetConstant(Imm, MVT::i16);
254 }
255
256 /// getI32Imm - Return a target constant with the specified value, of type
257 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000258 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000259 return CurDAG->getTargetConstant(Imm, MVT::i32);
260 }
Evan Chengf597dc72006-02-10 22:24:32 +0000261
Dan Gohman8b746962008-09-23 18:22:58 +0000262 /// getGlobalBaseReg - Return an SDNode that returns the value of
263 /// the global base register. Output instructions required to
264 /// initialize the global base register, if necessary.
265 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000266 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000267
Dan Gohmanc5534622009-06-03 20:20:00 +0000268 /// getTargetMachine - Return a reference to the TargetMachine, casted
269 /// to the target-specific type.
270 const X86TargetMachine &getTargetMachine() {
271 return static_cast<const X86TargetMachine &>(TM);
272 }
273
274 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
275 /// to the target-specific type.
276 const X86InstrInfo *getInstrInfo() {
277 return getTargetMachine().getInstrInfo();
278 }
279
Evan Cheng23addc02006-02-10 22:46:26 +0000280#ifndef NDEBUG
281 unsigned Indent;
282#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000283 };
284}
285
Evan Chengf4b4c412006-08-08 00:31:00 +0000286
Evan Cheng884c70c2008-11-27 00:49:46 +0000287bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
288 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000289 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000290
Evan Cheng884c70c2008-11-27 00:49:46 +0000291 if (U == Root)
292 switch (U->getOpcode()) {
293 default: break;
294 case ISD::ADD:
295 case ISD::ADDC:
296 case ISD::ADDE:
297 case ISD::AND:
298 case ISD::OR:
299 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000300 SDValue Op1 = U->getOperand(1);
301
Evan Cheng884c70c2008-11-27 00:49:46 +0000302 // If the other operand is a 8-bit immediate we should fold the immediate
303 // instead. This reduces code size.
304 // e.g.
305 // movl 4(%esp), %eax
306 // addl $4, %eax
307 // vs.
308 // movl $4, %eax
309 // addl 4(%esp), %eax
310 // The former is 2 bytes shorter. In case where the increment is 1, then
311 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000312 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000313 if (Imm->getAPIntValue().isSignedIntN(8))
314 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000315
316 // If the other operand is a TLS address, we should fold it instead.
317 // This produces
318 // movl %gs:0, %eax
319 // leal i@NTPOFF(%eax), %eax
320 // instead of
321 // movl $i@NTPOFF, %eax
322 // addl %gs:0, %eax
323 // if the block also has an access to a second TLS address this will save
324 // a load.
325 // FIXME: This is probably also true for non TLS addresses.
326 if (Op1.getOpcode() == X86ISD::Wrapper) {
327 SDValue Val = Op1.getOperand(0);
328 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
329 return false;
330 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000331 }
332 }
333
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000334 // Proceed to 'generic' cycle finder code
335 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000336}
337
Evan Cheng70e674e2006-08-28 20:10:17 +0000338/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
339/// and move load below the TokenFactor. Replace store's chain operand with
340/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000341static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000342 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000343 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000344 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
345 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000346 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000347 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000348 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000349 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
350 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
351 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
352 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000353}
354
Evan Chengcd0baf22008-05-23 21:23:16 +0000355/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
356///
Dan Gohman475871a2008-07-27 21:46:04 +0000357static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
358 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000359 if (N.getOpcode() == ISD::BIT_CONVERT)
360 N = N.getOperand(0);
361
362 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
363 if (!LD || LD->isVolatile())
364 return false;
365 if (LD->getAddressingMode() != ISD::UNINDEXED)
366 return false;
367
368 ISD::LoadExtType ExtType = LD->getExtensionType();
369 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
370 return false;
371
372 if (N.hasOneUse() &&
373 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000374 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000375 Load = N;
376 return true;
377 }
378 return false;
379}
380
Evan Chengab6c3bb2008-08-25 21:27:18 +0000381/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
382/// operand and move load below the call's chain operand.
383static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000384 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000385 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000386 SDValue Chain = CallSeqStart.getOperand(0);
387 if (Chain.getNode() == Load.getNode())
388 Ops.push_back(Load.getOperand(0));
389 else {
390 assert(Chain.getOpcode() == ISD::TokenFactor &&
391 "Unexpected CallSeqStart chain operand");
392 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
393 if (Chain.getOperand(i).getNode() == Load.getNode())
394 Ops.push_back(Load.getOperand(0));
395 else
396 Ops.push_back(Chain.getOperand(i));
397 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000398 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
399 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000400 Ops.clear();
401 Ops.push_back(NewChain);
402 }
403 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
404 Ops.push_back(CallSeqStart.getOperand(i));
405 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000406 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
407 Load.getOperand(1), Load.getOperand(2));
408 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000409 Ops.push_back(SDValue(Load.getNode(), 1));
410 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000411 Ops.push_back(Call.getOperand(i));
412 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
413}
414
415/// isCalleeLoad - Return true if call address is a load and it can be
416/// moved below CALLSEQ_START and the chains leading up to the call.
417/// Return the CALLSEQ_START by reference as a second output.
418static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000419 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000420 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000421 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000422 if (!LD ||
423 LD->isVolatile() ||
424 LD->getAddressingMode() != ISD::UNINDEXED ||
425 LD->getExtensionType() != ISD::NON_EXTLOAD)
426 return false;
427
428 // Now let's find the callseq_start.
429 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
430 if (!Chain.hasOneUse())
431 return false;
432 Chain = Chain.getOperand(0);
433 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000434
435 if (Chain.getOperand(0).getNode() == Callee.getNode())
436 return true;
437 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
438 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
439 return true;
440 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000441}
442
443
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000444/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000445/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000446/// This allows the instruction selector to pick more read-modify-write
447/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000448///
449/// [Load chain]
450/// ^
451/// |
452/// [Load]
453/// ^ ^
454/// | |
455/// / \-
456/// / |
457/// [TokenFactor] [Op]
458/// ^ ^
459/// | |
460/// \ /
461/// \ /
462/// [Store]
463///
464/// The fact the store's chain operand != load's chain will prevent the
465/// (store (op (load))) instruction from being selected. We can transform it to:
466///
467/// [Load chain]
468/// ^
469/// |
470/// [TokenFactor]
471/// ^
472/// |
473/// [Load]
474/// ^ ^
475/// | |
476/// | \-
477/// | |
478/// | [Op]
479/// | ^
480/// | |
481/// \ /
482/// \ /
483/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000484void X86DAGToDAGISel::PreprocessForRMW() {
485 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
486 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000487 if (I->getOpcode() == X86ISD::CALL) {
488 /// Also try moving call address load from outside callseq_start to just
489 /// before the call to allow it to be folded.
490 ///
491 /// [Load chain]
492 /// ^
493 /// |
494 /// [Load]
495 /// ^ ^
496 /// | |
497 /// / \--
498 /// / |
499 ///[CALLSEQ_START] |
500 /// ^ |
501 /// | |
502 /// [LOAD/C2Reg] |
503 /// | |
504 /// \ /
505 /// \ /
506 /// [CALL]
507 SDValue Chain = I->getOperand(0);
508 SDValue Load = I->getOperand(1);
509 if (!isCalleeLoad(Load, Chain))
510 continue;
511 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
512 ++NumLoadMoved;
513 continue;
514 }
515
Evan Cheng8b2794a2006-10-13 21:14:26 +0000516 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000517 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000518 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000519
Gabor Greifba36cb52008-08-28 21:40:38 +0000520 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000521 continue;
522
Dan Gohman475871a2008-07-27 21:46:04 +0000523 SDValue N1 = I->getOperand(1);
524 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000525 if ((N1.getValueType().isFloatingPoint() &&
526 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000527 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000528 continue;
529
530 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000531 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000532 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000533 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000534 case ISD::ADD:
535 case ISD::MUL:
536 case ISD::AND:
537 case ISD::OR:
538 case ISD::XOR:
539 case ISD::ADDC:
540 case ISD::ADDE:
541 case ISD::VECTOR_SHUFFLE: {
542 SDValue N10 = N1.getOperand(0);
543 SDValue N11 = N1.getOperand(1);
544 RModW = isRMWLoad(N10, Chain, N2, Load);
545 if (!RModW)
546 RModW = isRMWLoad(N11, Chain, N2, Load);
547 break;
548 }
549 case ISD::SUB:
550 case ISD::SHL:
551 case ISD::SRA:
552 case ISD::SRL:
553 case ISD::ROTL:
554 case ISD::ROTR:
555 case ISD::SUBC:
556 case ISD::SUBE:
557 case X86ISD::SHLD:
558 case X86ISD::SHRD: {
559 SDValue N10 = N1.getOperand(0);
560 RModW = isRMWLoad(N10, Chain, N2, Load);
561 break;
562 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000563 }
564
Evan Cheng82a35b32006-08-29 06:44:17 +0000565 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000566 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000567 ++NumLoadMoved;
568 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000569 }
570}
571
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000572
573/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
574/// nodes that target the FP stack to be store and load to the stack. This is a
575/// gross hack. We would like to simply mark these as being illegal, but when
576/// we do that, legalize produces these when it expands calls, then expands
577/// these in the same legalize pass. We would like dag combine to be able to
578/// hack on these between the call expansion and the node legalization. As such
579/// this pass basically does "really late" legalization of these inline with the
580/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000581void X86DAGToDAGISel::PreprocessForFPConvert() {
582 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
583 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000584 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
585 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
586 continue;
587
588 // If the source and destination are SSE registers, then this is a legal
589 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000590 MVT SrcVT = N->getOperand(0).getValueType();
591 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000592 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
593 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
594 if (SrcIsSSE && DstIsSSE)
595 continue;
596
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000597 if (!SrcIsSSE && !DstIsSSE) {
598 // If this is an FPStack extension, it is a noop.
599 if (N->getOpcode() == ISD::FP_EXTEND)
600 continue;
601 // If this is a value-preserving FPStack truncation, it is a noop.
602 if (N->getConstantOperandVal(1))
603 continue;
604 }
605
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000606 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
607 // FPStack has extload and truncstore. SSE can fold direct loads into other
608 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000609 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000610 if (N->getOpcode() == ISD::FP_ROUND)
611 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
612 else
613 MemVT = SrcIsSSE ? SrcVT : DstVT;
614
Dan Gohmanf350b272008-08-23 02:25:05 +0000615 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000616 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000617
618 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000619 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000620 N->getOperand(0),
621 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000622 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000623 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000624
625 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
626 // extload we created. This will cause general havok on the dag because
627 // anything below the conversion could be folded into other existing nodes.
628 // To avoid invalidating 'I', back it up to the convert node.
629 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000630 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000631
632 // Now that we did that, the node is dead. Increment the iterator to the
633 // next node to process, then delete N.
634 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000635 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000636 }
637}
638
Chris Lattnerc961eea2005-11-16 01:54:32 +0000639/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
640/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000641void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000642 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000643 const Function *F = CurDAG->getMachineFunction().getFunction();
644 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000645
Evan Chengdb8d56b2008-06-30 20:45:06 +0000646 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000647 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000648 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000649
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000650 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000651 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000652
Chris Lattnerc961eea2005-11-16 01:54:32 +0000653 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000654#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000655 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000656 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000657#endif
David Greene8ad4c002008-10-27 21:56:29 +0000658 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000659#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000660 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000661#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000662
Dan Gohmanf350b272008-08-23 02:25:05 +0000663 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000664}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000665
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000666/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
667/// the main function.
668void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
669 MachineFrameInfo *MFI) {
670 const TargetInstrInfo *TII = TM.getInstrInfo();
671 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000672 BuildMI(BB, DebugLoc::getUnknownLoc(),
673 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000674}
675
676void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
677 // If this is main, emit special code for main.
678 MachineBasicBlock *BB = MF.begin();
679 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
680 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
681}
682
Rafael Espindola094fad32009-04-08 21:14:34 +0000683
684bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
685 X86ISelAddressMode &AM) {
686 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
687 SDValue Segment = N.getOperand(0);
688
689 if (AM.Segment.getNode() == 0) {
690 AM.Segment = Segment;
691 return false;
692 }
693
694 return true;
695}
696
697bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
698 // This optimization is valid because the GNU TLS model defines that
699 // gs:0 (or fs:0 on X86-64) contains its own address.
700 // For more information see http://people.redhat.com/drepper/tls.pdf
701
702 SDValue Address = N.getOperand(1);
703 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
704 !MatchSegmentBaseAddress (Address, AM))
705 return false;
706
707 return true;
708}
709
Chris Lattner18c59872009-06-27 04:16:01 +0000710/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
711/// into an addressing mode. These wrap things that will resolve down into a
712/// symbol reference. If no match is possible, this returns true, otherwise it
713/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000714bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000715 // If the addressing mode already has a symbol as the displacement, we can
716 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000717 if (AM.hasSymbolicDisplacement())
718 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000719
720 SDValue N0 = N.getOperand(0);
Chris Lattner18c59872009-06-27 04:16:01 +0000721
722 // Handle X86-64 rip-relative addresses. We check this before checking direct
723 // folding because RIP is preferable to non-RIP accesses.
724 if (Subtarget->is64Bit() &&
725 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
726 // they cannot be folded into immediate fields.
727 // FIXME: This can be improved for kernel and other models?
728 TM.getCodeModel() == CodeModel::Small &&
729
730 // Base and index reg must be 0 in order to use %rip as base and lowering
731 // must allow RIP.
732 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
733
734 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
735 int64_t Offset = AM.Disp + G->getOffset();
736 if (!isInt32(Offset)) return true;
737 AM.GV = G->getGlobal();
738 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000739 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000740 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
741 int64_t Offset = AM.Disp + CP->getOffset();
742 if (!isInt32(Offset)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000743 AM.CP = CP->getConstVal();
744 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000745 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000746 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000747 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
748 AM.ES = S->getSymbol();
749 AM.SymbolFlags = S->getTargetFlags();
750 } else {
751 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
752 AM.JT = J->getIndex();
753 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000754 }
Chris Lattner18c59872009-06-27 04:16:01 +0000755
756 if (N.getOpcode() == X86ISD::WrapperRIP)
757 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000758 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000759 }
760
761 // Handle the case when globals fit in our immediate field: This is true for
762 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
763 // mode, this results in a non-RIP-relative computation.
764 if (!Subtarget->is64Bit() ||
765 (TM.getCodeModel() == CodeModel::Small &&
766 TM.getRelocationModel() == Reloc::Static)) {
767 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
768 AM.GV = G->getGlobal();
769 AM.Disp += G->getOffset();
770 AM.SymbolFlags = G->getTargetFlags();
771 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
772 AM.CP = CP->getConstVal();
773 AM.Align = CP->getAlignment();
774 AM.Disp += CP->getOffset();
775 AM.SymbolFlags = CP->getTargetFlags();
776 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
777 AM.ES = S->getSymbol();
778 AM.SymbolFlags = S->getTargetFlags();
779 } else {
780 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
781 AM.JT = J->getIndex();
782 AM.SymbolFlags = J->getTargetFlags();
783 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000784 return false;
785 }
786
787 return true;
788}
789
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000790/// MatchAddress - Add the specified node to the specified addressing mode,
791/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000792/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000793bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
794 if (MatchAddressRecursively(N, AM, 0))
795 return true;
796
797 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
798 // a smaller encoding and avoids a scaled-index.
799 if (AM.Scale == 2 &&
800 AM.BaseType == X86ISelAddressMode::RegBase &&
801 AM.Base.Reg.getNode() == 0) {
802 AM.Base.Reg = AM.IndexReg;
803 AM.Scale = 1;
804 }
805
806 return false;
807}
808
809bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
810 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000811 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000812 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000813 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000814 // Limit recursion.
815 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000816 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000817
Chris Lattner18c59872009-06-27 04:16:01 +0000818 // If this is already a %rip relative address, we can only merge immediates
819 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000820 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000821 if (AM.isRIPRelative()) {
822 // FIXME: JumpTable and ExternalSymbol address currently don't like
823 // displacements. It isn't very important, but this should be fixed for
824 // consistency.
825 if (!AM.ES && AM.JT != -1) return true;
826
827 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
828 int64_t Val = AM.Disp + Cst->getSExtValue();
829 if (isInt32(Val)) {
830 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000831 return false;
832 }
833 }
834 return true;
835 }
836
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000837 switch (N.getOpcode()) {
838 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000839 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000840 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000841 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000842 AM.Disp += Val;
843 return false;
844 }
845 break;
846 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000847
Rafael Espindola094fad32009-04-08 21:14:34 +0000848 case X86ISD::SegmentBaseAddress:
849 if (!MatchSegmentBaseAddress(N, AM))
850 return false;
851 break;
852
Rafael Espindola49a168d2009-04-12 21:55:03 +0000853 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000854 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000855 if (!MatchWrapper(N, AM))
856 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000857 break;
858
Rafael Espindola094fad32009-04-08 21:14:34 +0000859 case ISD::LOAD:
860 if (!MatchLoad(N, AM))
861 return false;
862 break;
863
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000864 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000865 if (AM.BaseType == X86ISelAddressMode::RegBase
866 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000867 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
868 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
869 return false;
870 }
871 break;
Evan Chengec693f72005-12-08 02:01:35 +0000872
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000873 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000874 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000875 break;
876
Gabor Greif93c53e52008-08-31 15:37:04 +0000877 if (ConstantSDNode
878 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000879 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000880 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
881 // that the base operand remains free for further matching. If
882 // the base doesn't end up getting used, a post-processing step
883 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000884 if (Val == 1 || Val == 2 || Val == 3) {
885 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000886 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000887
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000888 // Okay, we know that we have a scale by now. However, if the scaled
889 // value is an add of something and a constant, we can fold the
890 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000891 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
892 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
893 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000894 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000895 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000896 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000897 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000898 AM.Disp = Disp;
899 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000900 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000901 } else {
902 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000903 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000904 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000905 }
906 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000907 }
Evan Chengec693f72005-12-08 02:01:35 +0000908
Dan Gohman83688052007-10-22 20:22:24 +0000909 case ISD::SMUL_LOHI:
910 case ISD::UMUL_LOHI:
911 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000912 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000913 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000914 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000915 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000916 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000917 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000918 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000919 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000920 if (ConstantSDNode
921 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000922 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
923 CN->getZExtValue() == 9) {
924 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000925
Gabor Greifba36cb52008-08-28 21:40:38 +0000926 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000927 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000928
929 // Okay, we know that we have a scale by now. However, if the scaled
930 // value is an add of something and a constant, we can fold the
931 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000932 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
933 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
934 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000935 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000936 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000937 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000938 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000939 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000940 AM.Disp = Disp;
941 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000942 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000943 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000944 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000945 }
946
947 AM.IndexReg = AM.Base.Reg = Reg;
948 return false;
949 }
Chris Lattner62412262007-02-04 20:18:17 +0000950 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000951 break;
952
Dan Gohman3cd90a12009-05-11 18:02:53 +0000953 case ISD::SUB: {
954 // Given A-B, if A can be completely folded into the address and
955 // the index field with the index field unused, use -B as the index.
956 // This is a win if a has multiple parts that can be folded into
957 // the address. Also, this saves a mov if the base register has
958 // other uses, since it avoids a two-address sub instruction, however
959 // it costs an additional mov if the index register has other uses.
960
961 // Test if the LHS of the sub can be folded.
962 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000963 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000964 AM = Backup;
965 break;
966 }
967 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000968 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000969 AM = Backup;
970 break;
971 }
972 int Cost = 0;
973 SDValue RHS = N.getNode()->getOperand(1);
974 // If the RHS involves a register with multiple uses, this
975 // transformation incurs an extra mov, due to the neg instruction
976 // clobbering its operand.
977 if (!RHS.getNode()->hasOneUse() ||
978 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
979 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
980 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
981 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
982 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
983 ++Cost;
984 // If the base is a register with multiple uses, this
985 // transformation may save a mov.
986 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
987 AM.Base.Reg.getNode() &&
988 !AM.Base.Reg.getNode()->hasOneUse()) ||
989 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
990 --Cost;
991 // If the folded LHS was interesting, this transformation saves
992 // address arithmetic.
993 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
994 ((AM.Disp != 0) && (Backup.Disp == 0)) +
995 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
996 --Cost;
997 // If it doesn't look like it may be an overall win, don't do it.
998 if (Cost >= 0) {
999 AM = Backup;
1000 break;
1001 }
1002
1003 // Ok, the transformation is legal and appears profitable. Go for it.
1004 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1005 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1006 AM.IndexReg = Neg;
1007 AM.Scale = 1;
1008
1009 // Insert the new nodes into the topological ordering.
1010 if (Zero.getNode()->getNodeId() == -1 ||
1011 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1012 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1013 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1014 }
1015 if (Neg.getNode()->getNodeId() == -1 ||
1016 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1017 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1018 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1019 }
1020 return false;
1021 }
1022
Evan Cheng8e278262009-01-17 07:09:27 +00001023 case ISD::ADD: {
1024 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001025 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1026 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001027 return false;
1028 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001029 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1030 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001031 return false;
1032 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001033
1034 // If we couldn't fold both operands into the address at the same time,
1035 // see if we can just put each operand into a register and fold at least
1036 // the add.
1037 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1038 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001039 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001040 AM.Base.Reg = N.getNode()->getOperand(0);
1041 AM.IndexReg = N.getNode()->getOperand(1);
1042 AM.Scale = 1;
1043 return false;
1044 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001045 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001046 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001047
Chris Lattner62412262007-02-04 20:18:17 +00001048 case ISD::OR:
1049 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001050 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1051 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001052 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001053 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001054 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001055 // Address could not have picked a GV address for the displacement.
1056 AM.GV == NULL &&
1057 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +00001058 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001059 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001060 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001061 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001062 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001063 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001064 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001065 }
1066 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001067
1068 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001069 // Perform some heroic transforms on an and of a constant-count shift
1070 // with a constant to enable use of the scaled offset field.
1071
Dan Gohman475871a2008-07-27 21:46:04 +00001072 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001073 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001074
Evan Cheng1314b002007-12-13 00:43:27 +00001075 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001076 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001077
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001078 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001079 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1080 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1081 if (!C1 || !C2) break;
1082
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001083 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1084 // allows us to convert the shift and and into an h-register extract and
1085 // a scaled index.
1086 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1087 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001088 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001089 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1090 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1091 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1092 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1093 X, Eight);
1094 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1095 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +00001096 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1097 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1098 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001099
1100 // Insert the new nodes into the topological ordering.
1101 if (Eight.getNode()->getNodeId() == -1 ||
1102 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1103 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1104 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1105 }
1106 if (Mask.getNode()->getNodeId() == -1 ||
1107 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1108 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1109 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1110 }
1111 if (Srl.getNode()->getNodeId() == -1 ||
1112 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1113 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1114 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1115 }
1116 if (And.getNode()->getNodeId() == -1 ||
1117 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1118 CurDAG->RepositionNode(N.getNode(), And.getNode());
1119 And.getNode()->setNodeId(N.getNode()->getNodeId());
1120 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001121 if (ShlCount.getNode()->getNodeId() == -1 ||
1122 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1123 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1124 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1125 }
1126 if (Shl.getNode()->getNodeId() == -1 ||
1127 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1128 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1129 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1130 }
1131 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001132 AM.IndexReg = And;
1133 AM.Scale = (1 << ScaleLog);
1134 return false;
1135 }
1136 }
1137
1138 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1139 // allows us to fold the shift into this addressing mode.
1140 if (Shift.getOpcode() != ISD::SHL) break;
1141
Evan Cheng1314b002007-12-13 00:43:27 +00001142 // Not likely to be profitable if either the AND or SHIFT node has more
1143 // than one use (unless all uses are for address computation). Besides,
1144 // isel mechanism requires their node ids to be reused.
1145 if (!N.hasOneUse() || !Shift.hasOneUse())
1146 break;
1147
1148 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001149 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001150 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1151 break;
1152
1153 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001154 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001155 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001156 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1157 NewANDMask);
1158 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001159 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001160
1161 // Insert the new nodes into the topological ordering.
1162 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1163 CurDAG->RepositionNode(X.getNode(), C1);
1164 C1->setNodeId(X.getNode()->getNodeId());
1165 }
1166 if (NewANDMask.getNode()->getNodeId() == -1 ||
1167 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1168 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1169 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1170 }
1171 if (NewAND.getNode()->getNodeId() == -1 ||
1172 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1173 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1174 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1175 }
1176 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1177 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1178 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1179 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1180 }
1181
Dan Gohman7b8e9642008-10-13 20:52:04 +00001182 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001183
1184 AM.Scale = 1 << ShiftCst;
1185 AM.IndexReg = NewAND;
1186 return false;
1187 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001188 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001189
Rafael Espindola523249f2009-03-31 16:16:57 +00001190 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001191}
1192
1193/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1194/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001195bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001196 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001197 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001198 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001199 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001200 AM.IndexReg = N;
1201 AM.Scale = 1;
1202 return false;
1203 }
1204
1205 // Otherwise, we cannot select it.
1206 return true;
1207 }
1208
1209 // Default, generate it as a register.
1210 AM.BaseType = X86ISelAddressMode::RegBase;
1211 AM.Base.Reg = N;
1212 return false;
1213}
1214
Evan Chengec693f72005-12-08 02:01:35 +00001215/// SelectAddr - returns true if it is able pattern match an addressing mode.
1216/// It returns the operands which make up the maximal addressing mode it can
1217/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001218bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1219 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001220 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001221 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001222 bool Done = false;
1223 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1224 unsigned Opcode = N.getOpcode();
1225 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001226 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001227 // If we are able to fold N into addressing mode, then we'll allow it even
1228 // if N has multiple uses. In general, addressing computation is used as
1229 // addresses by all of its uses. But watch out for CopyToReg uses, that
1230 // means the address computation is liveout. It will be computed by a LEA
1231 // so we want to avoid computing the address twice.
1232 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1233 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1234 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001235 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001236 Done = true;
1237 break;
1238 }
1239 }
1240 }
1241 }
1242
1243 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001244 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001245
Duncan Sands83ec4b62008-06-06 12:08:01 +00001246 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001247 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001248 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001249 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001250 }
Evan Cheng8700e142006-01-11 06:09:51 +00001251
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001253 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001254
Rafael Espindola094fad32009-04-08 21:14:34 +00001255 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001256 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001257}
1258
Chris Lattner3a7cd952006-10-07 21:55:32 +00001259/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1260/// match a load whose top elements are either undef or zeros. The load flavor
1261/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001262bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1263 SDValue N, SDValue &Base,
1264 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001265 SDValue &Disp, SDValue &Segment,
1266 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001267 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001268 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001269 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001270 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001271 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001272 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001273 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001274 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001275 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001276 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001277 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001278 return true;
1279 }
1280 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001281
1282 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001283 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001284 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001285 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001286 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001287 N.getOperand(0).getNode()->hasOneUse() &&
1288 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001289 N.getOperand(0).getOperand(0).hasOneUse()) {
1290 // Okay, this is a zero extending load. Fold it.
1291 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001292 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001293 return false;
1294 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001295 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001296 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001297 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001298 return false;
1299}
1300
1301
Evan Cheng51a9ed92006-02-25 10:09:08 +00001302/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1303/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001304bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1305 SDValue &Base, SDValue &Scale,
1306 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001307 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001308
1309 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1310 // segments.
1311 SDValue Copy = AM.Segment;
1312 SDValue T = CurDAG->getRegister(0, MVT::i32);
1313 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001314 if (MatchAddress(N, AM))
1315 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001316 assert (T == AM.Segment);
1317 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001318
Duncan Sands83ec4b62008-06-06 12:08:01 +00001319 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001320 unsigned Complexity = 0;
1321 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001322 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001323 Complexity = 1;
1324 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001325 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001326 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1327 Complexity = 4;
1328
Gabor Greifba36cb52008-08-28 21:40:38 +00001329 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001330 Complexity++;
1331 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001332 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001333
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001334 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1335 // a simple shift.
1336 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001337 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001338
1339 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1340 // to a LEA. This is determined with some expermentation but is by no means
1341 // optimal (especially for code size consideration). LEA is nice because of
1342 // its three-address nature. Tweak the cost function again when we can run
1343 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001344 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001345 // For X86-64, we should always use lea to materialize RIP relative
1346 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001347 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001348 Complexity = 4;
1349 else
1350 Complexity += 2;
1351 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001352
Gabor Greifba36cb52008-08-28 21:40:38 +00001353 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001354 Complexity++;
1355
Chris Lattner25142782009-07-11 22:50:33 +00001356 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001357 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001358 return false;
1359
1360 SDValue Segment;
1361 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1362 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001363}
1364
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001365/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1366bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1367 SDValue &Scale, SDValue &Index,
1368 SDValue &Disp) {
1369 assert(Op.getOpcode() == X86ISD::TLSADDR);
1370 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1371 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1372
1373 X86ISelAddressMode AM;
1374 AM.GV = GA->getGlobal();
1375 AM.Disp += GA->getOffset();
1376 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001377 AM.SymbolFlags = GA->getTargetFlags();
1378
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001379 if (N.getValueType() == MVT::i32) {
1380 AM.Scale = 1;
1381 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1382 } else {
1383 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1384 }
1385
1386 SDValue Segment;
1387 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1388 return true;
1389}
1390
1391
Dan Gohman475871a2008-07-27 21:46:04 +00001392bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1393 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001394 SDValue &Index, SDValue &Disp,
1395 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001396 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001397 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001398 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001399 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001400 return false;
1401}
1402
Dan Gohman8b746962008-09-23 18:22:58 +00001403/// getGlobalBaseReg - Return an SDNode that returns the value of
1404/// the global base register. Output instructions required to
1405/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001406///
Evan Cheng9ade2182006-08-26 05:34:46 +00001407SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001408 MachineFunction *MF = CurBB->getParent();
Dan Gohmanc5534622009-06-03 20:20:00 +00001409 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001410 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001411}
1412
Evan Chengb245d922006-05-20 01:36:52 +00001413static SDNode *FindCallStartFromCall(SDNode *Node) {
1414 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1415 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1416 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001417 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001418}
1419
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001420SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1421 SDValue Chain = Node->getOperand(0);
1422 SDValue In1 = Node->getOperand(1);
1423 SDValue In2L = Node->getOperand(2);
1424 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001425 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1426 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001427 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001428 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001429 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001430 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1431 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001432 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001433}
Christopher Lambc59e5212007-08-10 21:48:46 +00001434
Evan Cheng37b73872009-07-30 08:33:02 +00001435SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, MVT NVT) {
1436 if (Node->hasAnyUseOfValue(0))
1437 return 0;
1438
1439 // Optimize common patterns for __sync_add_and_fetch and
1440 // __sync_sub_and_fetch where the result is not used. This allows us
1441 // to use "lock" version of add, sub, inc, dec instructions.
1442 // FIXME: Do not use special instructions but instead add the "lock"
1443 // prefix to the target node somehow. The extra information will then be
1444 // transferred to machine instruction and it denotes the prefix.
1445 SDValue Chain = Node->getOperand(0);
1446 SDValue Ptr = Node->getOperand(1);
1447 SDValue Val = Node->getOperand(2);
1448 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1449 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1450 return 0;
1451
1452 bool isInc = false, isDec = false, isSub = false, isCN = false;
1453 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1454 if (CN) {
1455 isCN = true;
1456 int64_t CNVal = CN->getSExtValue();
1457 if (CNVal == 1)
1458 isInc = true;
1459 else if (CNVal == -1)
1460 isDec = true;
1461 else if (CNVal >= 0)
1462 Val = CurDAG->getTargetConstant(CNVal, NVT);
1463 else {
1464 isSub = true;
1465 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1466 }
1467 } else if (Val.hasOneUse() &&
1468 Val.getOpcode() == ISD::SUB &&
1469 X86::isZeroNode(Val.getOperand(0))) {
1470 isSub = true;
1471 Val = Val.getOperand(1);
1472 }
1473
1474 unsigned Opc = 0;
1475 switch (NVT.getSimpleVT()) {
1476 default: return 0;
1477 case MVT::i8:
1478 if (isInc)
1479 Opc = X86::LOCK_INC8m;
1480 else if (isDec)
1481 Opc = X86::LOCK_DEC8m;
1482 else if (isSub) {
1483 if (isCN)
1484 Opc = X86::LOCK_SUB8mi;
1485 else
1486 Opc = X86::LOCK_SUB8mr;
1487 } else {
1488 if (isCN)
1489 Opc = X86::LOCK_ADD8mi;
1490 else
1491 Opc = X86::LOCK_ADD8mr;
1492 }
1493 break;
1494 case MVT::i16:
1495 if (isInc)
1496 Opc = X86::LOCK_INC16m;
1497 else if (isDec)
1498 Opc = X86::LOCK_DEC16m;
1499 else if (isSub) {
1500 if (isCN) {
1501 if (Predicate_i16immSExt8(Val.getNode()))
1502 Opc = X86::LOCK_SUB16mi8;
1503 else
1504 Opc = X86::LOCK_SUB16mi;
1505 } else
1506 Opc = X86::LOCK_SUB16mr;
1507 } else {
1508 if (isCN) {
1509 if (Predicate_i16immSExt8(Val.getNode()))
1510 Opc = X86::LOCK_ADD16mi8;
1511 else
1512 Opc = X86::LOCK_ADD16mi;
1513 } else
1514 Opc = X86::LOCK_ADD16mr;
1515 }
1516 break;
1517 case MVT::i32:
1518 if (isInc)
1519 Opc = X86::LOCK_INC32m;
1520 else if (isDec)
1521 Opc = X86::LOCK_DEC32m;
1522 else if (isSub) {
1523 if (isCN) {
1524 if (Predicate_i32immSExt8(Val.getNode()))
1525 Opc = X86::LOCK_SUB32mi8;
1526 else
1527 Opc = X86::LOCK_SUB32mi;
1528 } else
1529 Opc = X86::LOCK_SUB32mr;
1530 } else {
1531 if (isCN) {
1532 if (Predicate_i32immSExt8(Val.getNode()))
1533 Opc = X86::LOCK_ADD32mi8;
1534 else
1535 Opc = X86::LOCK_ADD32mi;
1536 } else
1537 Opc = X86::LOCK_ADD32mr;
1538 }
1539 break;
1540 case MVT::i64:
1541 if (isInc)
1542 Opc = X86::LOCK_INC64m;
1543 else if (isDec)
1544 Opc = X86::LOCK_DEC64m;
1545 else if (isSub) {
1546 Opc = X86::LOCK_SUB64mr;
1547 if (isCN) {
1548 if (Predicate_i64immSExt8(Val.getNode()))
1549 Opc = X86::LOCK_SUB64mi8;
1550 else if (Predicate_i64immSExt32(Val.getNode()))
1551 Opc = X86::LOCK_SUB64mi32;
1552 }
1553 } else {
1554 Opc = X86::LOCK_ADD64mr;
1555 if (isCN) {
1556 if (Predicate_i64immSExt8(Val.getNode()))
1557 Opc = X86::LOCK_ADD64mi8;
1558 else if (Predicate_i64immSExt32(Val.getNode()))
1559 Opc = X86::LOCK_ADD64mi32;
1560 }
1561 }
1562 break;
1563 }
1564
1565 DebugLoc dl = Node->getDebugLoc();
1566 SDValue Undef = SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1567 dl, NVT), 0);
1568 SDValue MemOp = CurDAG->getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
1569 if (isInc || isDec) {
1570 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, MemOp, Chain };
1571 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 7), 0);
1572 SDValue RetVals[] = { Undef, Ret };
1573 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1574 } else {
1575 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, MemOp, Chain };
1576 SDValue Ret = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Ops, 8), 0);
1577 SDValue RetVals[] = { Undef, Ret };
1578 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1579 }
1580}
1581
Dan Gohman475871a2008-07-27 21:46:04 +00001582SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001583 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001584 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001585 unsigned Opc, MOpc;
1586 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001587 DebugLoc dl = Node->getDebugLoc();
1588
Evan Chengf597dc72006-02-10 22:24:32 +00001589#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001590 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001591 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001592 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001593 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001594#endif
1595
Dan Gohmane8be6c62008-07-17 19:10:17 +00001596 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001597#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001598 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001599 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001600 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001601 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001602#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001603 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001604 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001605
Evan Cheng0114e942006-01-06 20:36:21 +00001606 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001607 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001608 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001609 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001610
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001611 case X86ISD::ATOMOR64_DAG:
1612 return SelectAtomic64(Node, X86::ATOMOR6432);
1613 case X86ISD::ATOMXOR64_DAG:
1614 return SelectAtomic64(Node, X86::ATOMXOR6432);
1615 case X86ISD::ATOMADD64_DAG:
1616 return SelectAtomic64(Node, X86::ATOMADD6432);
1617 case X86ISD::ATOMSUB64_DAG:
1618 return SelectAtomic64(Node, X86::ATOMSUB6432);
1619 case X86ISD::ATOMNAND64_DAG:
1620 return SelectAtomic64(Node, X86::ATOMNAND6432);
1621 case X86ISD::ATOMAND64_DAG:
1622 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001623 case X86ISD::ATOMSWAP64_DAG:
1624 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001625
Evan Cheng37b73872009-07-30 08:33:02 +00001626 case ISD::ATOMIC_LOAD_ADD: {
1627 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1628 if (RetVal)
1629 return RetVal;
1630 break;
1631 }
1632
Dan Gohman525178c2007-10-08 18:33:35 +00001633 case ISD::SMUL_LOHI:
1634 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001635 SDValue N0 = Node->getOperand(0);
1636 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001637
Dan Gohman525178c2007-10-08 18:33:35 +00001638 bool isSigned = Opcode == ISD::SMUL_LOHI;
1639 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001640 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001641 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001642 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1643 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1644 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001645 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001646 }
1647 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001648 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001649 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001650 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1651 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1652 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001653 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001654 }
1655
1656 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001657 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001658 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001659 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1660 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1661 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001662 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001663 }
1664
Rafael Espindola094fad32009-04-08 21:14:34 +00001665 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1666 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001667 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001668 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001669 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001670 if (foldedLoad)
1671 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001672 }
1673
Dale Johannesendd64c412009-02-04 00:33:20 +00001674 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001675 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001676
1677 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001678 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1679 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001680 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001681 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001682 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001683 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001684 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001685 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001686 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001687 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001688 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001689 }
1690
Dan Gohman525178c2007-10-08 18:33:35 +00001691 // Copy the low half of the result, if it is needed.
1692 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001693 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001694 LoReg, NVT, InFlag);
1695 InFlag = Result.getValue(2);
1696 ReplaceUses(N.getValue(0), Result);
1697#ifndef NDEBUG
1698 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001699 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001700 DOUT << "\n";
1701#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001702 }
Dan Gohman525178c2007-10-08 18:33:35 +00001703 // Copy the high half of the result, if it is needed.
1704 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001705 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001706 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1707 // Prevent use of AH in a REX instruction by referencing AX instead.
1708 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001709 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001710 X86::AX, MVT::i16, InFlag);
1711 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001712 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1713 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001714 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001715 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001716 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001717 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001718 MVT::i8, Result, SRIdx), 0);
1719 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001720 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001721 HiReg, NVT, InFlag);
1722 InFlag = Result.getValue(2);
1723 }
1724 ReplaceUses(N.getValue(1), Result);
1725#ifndef NDEBUG
1726 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001727 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001728 DOUT << "\n";
1729#endif
1730 }
Evan Cheng34167212006-02-09 00:37:58 +00001731
Evan Chengf597dc72006-02-10 22:24:32 +00001732#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001733 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001734#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001735
Evan Cheng64a752f2006-08-11 09:08:15 +00001736 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001737 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001738
Dan Gohman525178c2007-10-08 18:33:35 +00001739 case ISD::SDIVREM:
1740 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001741 SDValue N0 = Node->getOperand(0);
1742 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001743
1744 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001745 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001746 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001747 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001748 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1749 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1750 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001751 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001752 }
1753 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001754 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001755 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001756 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1757 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1758 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001759 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001760 }
1761
1762 unsigned LoReg, HiReg;
1763 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001764 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001765 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001766 case MVT::i8:
1767 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001768 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001769 SExtOpcode = X86::CBW;
1770 break;
1771 case MVT::i16:
1772 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001773 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001774 SExtOpcode = X86::CWD;
1775 break;
1776 case MVT::i32:
1777 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001778 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001779 SExtOpcode = X86::CDQ;
1780 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001781 case MVT::i64:
1782 LoReg = X86::RAX; HiReg = X86::RDX;
Chris Lattner9ac75422009-07-14 20:19:57 +00001783 ClrOpcode = ~0U; // NOT USED.
Evan Cheng25ab6902006-09-08 06:48:29 +00001784 SExtOpcode = X86::CQO;
1785 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001786 }
1787
Rafael Espindola094fad32009-04-08 21:14:34 +00001788 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1789 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001790 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001791
Dan Gohman475871a2008-07-27 21:46:04 +00001792 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001793 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001794 // Special case for div8, just use a move with zero extension to AX to
1795 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001796 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1797 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1798 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001799 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001800 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001801 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001802 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001803 Chain = Move.getValue(1);
1804 ReplaceUses(N0.getValue(1), Chain);
1805 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001806 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001807 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001808 Chain = CurDAG->getEntryNode();
1809 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001810 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001811 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001812 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001813 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001814 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001815 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001816 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001817 // Sign extend the low part into the high part.
1818 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001819 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001820 } else {
1821 // Zero out the high part, effectively zero extending the input.
Chris Lattner9ac75422009-07-14 20:19:57 +00001822 SDValue ClrNode;
1823
1824 if (NVT.getSimpleVT() == MVT::i64) {
1825 ClrNode = SDValue(CurDAG->getTargetNode(X86::MOV32r0, dl, MVT::i32),
1826 0);
1827 // We just did a 32-bit clear, insert it into a 64-bit register to
1828 // clear the whole 64-bit reg.
1829 SDValue Undef =
1830 SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1831 dl, MVT::i64), 0);
1832 SDValue SubRegNo =
1833 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
1834 ClrNode =
1835 SDValue(CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl,
1836 MVT::i64, Undef, ClrNode, SubRegNo),
1837 0);
1838 } else {
1839 ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT), 0);
1840 }
1841
Dale Johannesendd64c412009-02-04 00:33:20 +00001842 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001843 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001844 }
Evan Cheng948f3432006-01-06 23:19:29 +00001845 }
1846
1847 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001848 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1849 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001850 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001851 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001852 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001853 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001854 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001855 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001856 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001857 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001858 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001859 }
1860
Dan Gohmana37c9f72007-09-25 18:23:27 +00001861 // Copy the division (low) result, if it is needed.
1862 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001863 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001864 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001865 InFlag = Result.getValue(2);
1866 ReplaceUses(N.getValue(0), Result);
1867#ifndef NDEBUG
1868 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001869 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001870 DOUT << "\n";
1871#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001872 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001873 // Copy the remainder (high) result, if it is needed.
1874 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001876 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1877 // Prevent use of AH in a REX instruction by referencing AX instead.
1878 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001879 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001880 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001881 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001882 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1883 Result,
1884 CurDAG->getTargetConstant(8, MVT::i8)),
1885 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001886 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001887 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001888 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001889 MVT::i8, Result, SRIdx), 0);
1890 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001891 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001892 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001893 InFlag = Result.getValue(2);
1894 }
1895 ReplaceUses(N.getValue(1), Result);
1896#ifndef NDEBUG
1897 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001898 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001899 DOUT << "\n";
1900#endif
1901 }
Evan Chengf597dc72006-02-10 22:24:32 +00001902
1903#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001904 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001905#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001906
1907 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001908 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001909
Evan Cheng851bc042008-06-17 02:01:22 +00001910 case ISD::DECLARE: {
1911 // Handle DECLARE nodes here because the second operand may have been
1912 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001913 SDValue Chain = Node->getOperand(0);
1914 SDValue N1 = Node->getOperand(1);
1915 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001916 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001917
1918 // FIXME: We need to handle this for VLAs.
1919 if (!FINode) {
1920 ReplaceUses(N.getValue(0), Chain);
1921 return NULL;
1922 }
1923
Evan Chengfab83872008-06-18 02:48:27 +00001924 if (N2.getOpcode() == ISD::ADD &&
1925 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1926 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001927
1928 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1929 // somehow, just ignore it.
Chris Lattner18c59872009-06-27 04:16:01 +00001930 if (N2.getOpcode() != X86ISD::Wrapper &&
1931 N2.getOpcode() != X86ISD::WrapperRIP) {
Chris Lattner1823c922009-02-12 17:33:11 +00001932 ReplaceUses(N.getValue(0), Chain);
1933 return NULL;
1934 }
Evan Chengf2accb52009-01-10 03:33:22 +00001935 GlobalAddressSDNode *GVNode =
1936 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001937 if (GVNode == 0) {
1938 ReplaceUses(N.getValue(0), Chain);
1939 return NULL;
1940 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001941 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1942 TLI.getPointerTy());
1943 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1944 TLI.getPointerTy());
1945 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001946 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001947 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001948 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001949 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001950 }
1951
Evan Cheng9ade2182006-08-26 05:34:46 +00001952 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001953
Evan Chengf597dc72006-02-10 22:24:32 +00001954#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001955 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 if (ResNode == NULL || ResNode == N.getNode())
1957 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001958 else
1959 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001960 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001961 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001962#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001963
1964 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001965}
1966
Chris Lattnerc0bad572006-06-08 18:03:49 +00001967bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001968SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001969 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001970 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001971 switch (ConstraintCode) {
1972 case 'o': // offsetable ??
1973 case 'v': // not offsetable ??
1974 default: return true;
1975 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001976 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001977 return true;
1978 break;
1979 }
1980
Evan Cheng04699902006-08-26 01:05:16 +00001981 OutOps.push_back(Op0);
1982 OutOps.push_back(Op1);
1983 OutOps.push_back(Op2);
1984 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001985 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001986 return false;
1987}
1988
Chris Lattnerc961eea2005-11-16 01:54:32 +00001989/// createX86ISelDag - This pass converts a legalized DAG into a
1990/// X86-specific DAG, ready for instruction scheduling.
1991///
Bill Wendling98a366d2009-04-29 23:29:43 +00001992FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1993 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001994 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001995}