blob: 1e8429221a66e2623e4878ef92bf06010d446e03 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000038#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000039#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000040#include "llvm/Support/Streams.h"
Torok Edwindac237e2009-07-08 20:53:28 +000041#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000043#include "llvm/ADT/Statistic.h"
44using namespace llvm;
45
Evan Cheng4d952322009-03-31 01:13:53 +000046#include "llvm/Support/CommandLine.h"
47static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
48
Chris Lattner95b2c7d2006-12-19 22:59:26 +000049STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
50
Chris Lattnerc961eea2005-11-16 01:54:32 +000051//===----------------------------------------------------------------------===//
52// Pattern Matcher Implementation
53//===----------------------------------------------------------------------===//
54
55namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000057 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 /// tree.
59 struct X86ISelAddressMode {
60 enum {
61 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000062 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000063 } BaseType;
64
65 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000066 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000067 int FrameIndex;
68 } Base;
69
70 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000071 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000072 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000073 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000075 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000076 const char *ES;
77 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000078 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000079 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000080
81 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000082 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattnerb8afeb92009-06-26 05:51:45 +000083 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0), SymbolFlags(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000084 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000085
86 bool hasSymbolicDisplacement() const {
87 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
88 }
Chris Lattner18c59872009-06-27 04:16:01 +000089
90 bool hasBaseOrIndexReg() const {
91 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
92 }
93
94 /// isRIPRelative - Return true if this addressing mode is already RIP
95 /// relative.
96 bool isRIPRelative() const {
97 if (BaseType != RegBase) return false;
98 if (RegisterSDNode *RegNode =
99 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
100 return RegNode->getReg() == X86::RIP;
101 return false;
102 }
103
104 void setBaseReg(SDValue Reg) {
105 BaseType = RegBase;
106 Base.Reg = Reg;
107 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000108
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000109 void dump() {
110 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000111 cerr << "Base.Reg ";
112 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
113 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000114 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
Chris Lattner18c59872009-06-27 04:16:01 +0000115 cerr << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +0000116 cerr << "IndexReg ";
117 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
118 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000119 cerr << " Disp " << Disp << "\n";
120 cerr << "GV "; if (GV) GV->dump();
121 else cerr << "nul";
122 cerr << " CP "; if (CP) CP->dump();
123 else cerr << "nul";
124 cerr << "\n";
125 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
126 cerr << " JT" << JT << " Align" << Align << "\n";
127 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000128 };
129}
130
131namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000132 //===--------------------------------------------------------------------===//
133 /// ISel - X86 specific code to select X86 machine instructions for
134 /// SelectionDAG operations.
135 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000136 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000137 /// X86Lowering - This object fully describes how to lower LLVM code to an
138 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000139 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000140
141 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
142 /// make the right decision when generating code for different targets.
143 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000144
Evan Chengdb8d56b2008-06-30 20:45:06 +0000145 /// CurBB - Current BB being isel'd.
146 ///
147 MachineBasicBlock *CurBB;
148
Evan Chengb7a75a52008-09-26 23:41:32 +0000149 /// OptForSize - If true, selector should try to optimize for code size
150 /// instead of performance.
151 bool OptForSize;
152
Chris Lattnerc961eea2005-11-16 01:54:32 +0000153 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000154 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000155 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000156 X86Lowering(*tm.getTargetLowering()),
157 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000158 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000159
160 virtual const char *getPassName() const {
161 return "X86 DAG->DAG Instruction Selection";
162 }
163
Evan Chengdb8d56b2008-06-30 20:45:06 +0000164 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000165 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000166 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000167
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000168 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
169
Evan Cheng884c70c2008-11-27 00:49:46 +0000170 virtual
171 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000172
Chris Lattnerc961eea2005-11-16 01:54:32 +0000173// Include the pieces autogenerated from the target description.
174#include "X86GenDAGISel.inc"
175
176 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000177 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000178 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000179
Rafael Espindola094fad32009-04-08 21:14:34 +0000180 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
181 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000182 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000183 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
184 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
185 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000186 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000187 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000188 SDValue &Scale, SDValue &Index, SDValue &Disp,
189 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000190 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
191 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000192 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
193 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000194 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
195 SDValue N, SDValue &Base, SDValue &Scale,
196 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000197 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue &InChain, SDValue &OutChain);
199 bool TryFoldLoad(SDValue P, SDValue N,
200 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000201 SDValue &Index, SDValue &Disp,
202 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000203 void PreprocessForRMW();
204 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000205
Chris Lattnerc0bad572006-06-08 18:03:49 +0000206 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
207 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000208 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000209 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000210 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000211
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000212 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
213
Dan Gohman475871a2008-07-27 21:46:04 +0000214 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
215 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000216 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000217 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000218 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
219 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000220 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000221 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 // These are 32-bit even in 64-bit mode since RIP relative offset
223 // is 32-bit.
224 if (AM.GV)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000225 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
226 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000227 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000228 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000229 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 else if (AM.ES)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000231 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 else if (AM.JT != -1)
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000233 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000235 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000236
237 if (AM.Segment.getNode())
238 Segment = AM.Segment;
239 else
240 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000241 }
242
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000243 /// getI8Imm - Return a target constant with the specified value, of type
244 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000245 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000246 return CurDAG->getTargetConstant(Imm, MVT::i8);
247 }
248
Chris Lattnerc961eea2005-11-16 01:54:32 +0000249 /// getI16Imm - Return a target constant with the specified value, of type
250 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000251 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000252 return CurDAG->getTargetConstant(Imm, MVT::i16);
253 }
254
255 /// getI32Imm - Return a target constant with the specified value, of type
256 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000257 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000258 return CurDAG->getTargetConstant(Imm, MVT::i32);
259 }
Evan Chengf597dc72006-02-10 22:24:32 +0000260
Dan Gohman8b746962008-09-23 18:22:58 +0000261 /// getGlobalBaseReg - Return an SDNode that returns the value of
262 /// the global base register. Output instructions required to
263 /// initialize the global base register, if necessary.
264 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000265 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000266
Dan Gohmanc5534622009-06-03 20:20:00 +0000267 /// getTargetMachine - Return a reference to the TargetMachine, casted
268 /// to the target-specific type.
269 const X86TargetMachine &getTargetMachine() {
270 return static_cast<const X86TargetMachine &>(TM);
271 }
272
273 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
274 /// to the target-specific type.
275 const X86InstrInfo *getInstrInfo() {
276 return getTargetMachine().getInstrInfo();
277 }
278
Evan Cheng23addc02006-02-10 22:46:26 +0000279#ifndef NDEBUG
280 unsigned Indent;
281#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000282 };
283}
284
Evan Chengf4b4c412006-08-08 00:31:00 +0000285
Evan Cheng884c70c2008-11-27 00:49:46 +0000286bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
287 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000288 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000289
Evan Cheng884c70c2008-11-27 00:49:46 +0000290 if (U == Root)
291 switch (U->getOpcode()) {
292 default: break;
293 case ISD::ADD:
294 case ISD::ADDC:
295 case ISD::ADDE:
296 case ISD::AND:
297 case ISD::OR:
298 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000299 SDValue Op1 = U->getOperand(1);
300
Evan Cheng884c70c2008-11-27 00:49:46 +0000301 // If the other operand is a 8-bit immediate we should fold the immediate
302 // instead. This reduces code size.
303 // e.g.
304 // movl 4(%esp), %eax
305 // addl $4, %eax
306 // vs.
307 // movl $4, %eax
308 // addl 4(%esp), %eax
309 // The former is 2 bytes shorter. In case where the increment is 1, then
310 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000311 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000312 if (Imm->getAPIntValue().isSignedIntN(8))
313 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000314
315 // If the other operand is a TLS address, we should fold it instead.
316 // This produces
317 // movl %gs:0, %eax
318 // leal i@NTPOFF(%eax), %eax
319 // instead of
320 // movl $i@NTPOFF, %eax
321 // addl %gs:0, %eax
322 // if the block also has an access to a second TLS address this will save
323 // a load.
324 // FIXME: This is probably also true for non TLS addresses.
325 if (Op1.getOpcode() == X86ISD::Wrapper) {
326 SDValue Val = Op1.getOperand(0);
327 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
328 return false;
329 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000330 }
331 }
332
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000333 // Proceed to 'generic' cycle finder code
334 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000335}
336
Evan Cheng70e674e2006-08-28 20:10:17 +0000337/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
338/// and move load below the TokenFactor. Replace store's chain operand with
339/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000340static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000341 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000342 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000343 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
344 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000345 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000346 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000347 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000348 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
349 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
350 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
351 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000352}
353
Evan Chengcd0baf22008-05-23 21:23:16 +0000354/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
355///
Dan Gohman475871a2008-07-27 21:46:04 +0000356static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
357 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000358 if (N.getOpcode() == ISD::BIT_CONVERT)
359 N = N.getOperand(0);
360
361 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
362 if (!LD || LD->isVolatile())
363 return false;
364 if (LD->getAddressingMode() != ISD::UNINDEXED)
365 return false;
366
367 ISD::LoadExtType ExtType = LD->getExtensionType();
368 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
369 return false;
370
371 if (N.hasOneUse() &&
372 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000373 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000374 Load = N;
375 return true;
376 }
377 return false;
378}
379
Evan Chengab6c3bb2008-08-25 21:27:18 +0000380/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
381/// operand and move load below the call's chain operand.
382static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000383 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000384 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000385 SDValue Chain = CallSeqStart.getOperand(0);
386 if (Chain.getNode() == Load.getNode())
387 Ops.push_back(Load.getOperand(0));
388 else {
389 assert(Chain.getOpcode() == ISD::TokenFactor &&
390 "Unexpected CallSeqStart chain operand");
391 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
392 if (Chain.getOperand(i).getNode() == Load.getNode())
393 Ops.push_back(Load.getOperand(0));
394 else
395 Ops.push_back(Chain.getOperand(i));
396 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000397 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
398 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000399 Ops.clear();
400 Ops.push_back(NewChain);
401 }
402 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
403 Ops.push_back(CallSeqStart.getOperand(i));
404 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000405 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
406 Load.getOperand(1), Load.getOperand(2));
407 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000408 Ops.push_back(SDValue(Load.getNode(), 1));
409 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000410 Ops.push_back(Call.getOperand(i));
411 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
412}
413
414/// isCalleeLoad - Return true if call address is a load and it can be
415/// moved below CALLSEQ_START and the chains leading up to the call.
416/// Return the CALLSEQ_START by reference as a second output.
417static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000418 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000419 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000420 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000421 if (!LD ||
422 LD->isVolatile() ||
423 LD->getAddressingMode() != ISD::UNINDEXED ||
424 LD->getExtensionType() != ISD::NON_EXTLOAD)
425 return false;
426
427 // Now let's find the callseq_start.
428 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
429 if (!Chain.hasOneUse())
430 return false;
431 Chain = Chain.getOperand(0);
432 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000433
434 if (Chain.getOperand(0).getNode() == Callee.getNode())
435 return true;
436 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
437 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
438 return true;
439 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000440}
441
442
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000443/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000444/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000445/// This allows the instruction selector to pick more read-modify-write
446/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000447///
448/// [Load chain]
449/// ^
450/// |
451/// [Load]
452/// ^ ^
453/// | |
454/// / \-
455/// / |
456/// [TokenFactor] [Op]
457/// ^ ^
458/// | |
459/// \ /
460/// \ /
461/// [Store]
462///
463/// The fact the store's chain operand != load's chain will prevent the
464/// (store (op (load))) instruction from being selected. We can transform it to:
465///
466/// [Load chain]
467/// ^
468/// |
469/// [TokenFactor]
470/// ^
471/// |
472/// [Load]
473/// ^ ^
474/// | |
475/// | \-
476/// | |
477/// | [Op]
478/// | ^
479/// | |
480/// \ /
481/// \ /
482/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000483void X86DAGToDAGISel::PreprocessForRMW() {
484 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
485 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000486 if (I->getOpcode() == X86ISD::CALL) {
487 /// Also try moving call address load from outside callseq_start to just
488 /// before the call to allow it to be folded.
489 ///
490 /// [Load chain]
491 /// ^
492 /// |
493 /// [Load]
494 /// ^ ^
495 /// | |
496 /// / \--
497 /// / |
498 ///[CALLSEQ_START] |
499 /// ^ |
500 /// | |
501 /// [LOAD/C2Reg] |
502 /// | |
503 /// \ /
504 /// \ /
505 /// [CALL]
506 SDValue Chain = I->getOperand(0);
507 SDValue Load = I->getOperand(1);
508 if (!isCalleeLoad(Load, Chain))
509 continue;
510 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
511 ++NumLoadMoved;
512 continue;
513 }
514
Evan Cheng8b2794a2006-10-13 21:14:26 +0000515 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000516 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000517 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000518
Gabor Greifba36cb52008-08-28 21:40:38 +0000519 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000520 continue;
521
Dan Gohman475871a2008-07-27 21:46:04 +0000522 SDValue N1 = I->getOperand(1);
523 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000524 if ((N1.getValueType().isFloatingPoint() &&
525 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000526 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000527 continue;
528
529 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000530 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000531 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000532 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000533 case ISD::ADD:
534 case ISD::MUL:
535 case ISD::AND:
536 case ISD::OR:
537 case ISD::XOR:
538 case ISD::ADDC:
539 case ISD::ADDE:
540 case ISD::VECTOR_SHUFFLE: {
541 SDValue N10 = N1.getOperand(0);
542 SDValue N11 = N1.getOperand(1);
543 RModW = isRMWLoad(N10, Chain, N2, Load);
544 if (!RModW)
545 RModW = isRMWLoad(N11, Chain, N2, Load);
546 break;
547 }
548 case ISD::SUB:
549 case ISD::SHL:
550 case ISD::SRA:
551 case ISD::SRL:
552 case ISD::ROTL:
553 case ISD::ROTR:
554 case ISD::SUBC:
555 case ISD::SUBE:
556 case X86ISD::SHLD:
557 case X86ISD::SHRD: {
558 SDValue N10 = N1.getOperand(0);
559 RModW = isRMWLoad(N10, Chain, N2, Load);
560 break;
561 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000562 }
563
Evan Cheng82a35b32006-08-29 06:44:17 +0000564 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000565 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000566 ++NumLoadMoved;
567 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000568 }
569}
570
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000571
572/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
573/// nodes that target the FP stack to be store and load to the stack. This is a
574/// gross hack. We would like to simply mark these as being illegal, but when
575/// we do that, legalize produces these when it expands calls, then expands
576/// these in the same legalize pass. We would like dag combine to be able to
577/// hack on these between the call expansion and the node legalization. As such
578/// this pass basically does "really late" legalization of these inline with the
579/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000580void X86DAGToDAGISel::PreprocessForFPConvert() {
581 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
582 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000583 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
584 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
585 continue;
586
587 // If the source and destination are SSE registers, then this is a legal
588 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000589 MVT SrcVT = N->getOperand(0).getValueType();
590 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000591 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
592 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
593 if (SrcIsSSE && DstIsSSE)
594 continue;
595
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000596 if (!SrcIsSSE && !DstIsSSE) {
597 // If this is an FPStack extension, it is a noop.
598 if (N->getOpcode() == ISD::FP_EXTEND)
599 continue;
600 // If this is a value-preserving FPStack truncation, it is a noop.
601 if (N->getConstantOperandVal(1))
602 continue;
603 }
604
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000605 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
606 // FPStack has extload and truncstore. SSE can fold direct loads into other
607 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000608 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000609 if (N->getOpcode() == ISD::FP_ROUND)
610 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
611 else
612 MemVT = SrcIsSSE ? SrcVT : DstVT;
613
Dan Gohmanf350b272008-08-23 02:25:05 +0000614 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000615 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000616
617 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000618 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000619 N->getOperand(0),
620 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000621 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000622 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000623
624 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
625 // extload we created. This will cause general havok on the dag because
626 // anything below the conversion could be folded into other existing nodes.
627 // To avoid invalidating 'I', back it up to the convert node.
628 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000629 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000630
631 // Now that we did that, the node is dead. Increment the iterator to the
632 // next node to process, then delete N.
633 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000634 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000635 }
636}
637
Chris Lattnerc961eea2005-11-16 01:54:32 +0000638/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
639/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000640void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000641 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000642 const Function *F = CurDAG->getMachineFunction().getFunction();
643 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000644
Evan Chengdb8d56b2008-06-30 20:45:06 +0000645 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000646 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000647 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000648
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000649 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000650 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000651
Chris Lattnerc961eea2005-11-16 01:54:32 +0000652 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000653#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000654 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000655 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000656#endif
David Greene8ad4c002008-10-27 21:56:29 +0000657 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000658#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000659 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000660#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000661
Dan Gohmanf350b272008-08-23 02:25:05 +0000662 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000663}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000664
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000665/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
666/// the main function.
667void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
668 MachineFrameInfo *MFI) {
669 const TargetInstrInfo *TII = TM.getInstrInfo();
670 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000671 BuildMI(BB, DebugLoc::getUnknownLoc(),
672 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000673}
674
675void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
676 // If this is main, emit special code for main.
677 MachineBasicBlock *BB = MF.begin();
678 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
679 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
680}
681
Rafael Espindola094fad32009-04-08 21:14:34 +0000682
683bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
684 X86ISelAddressMode &AM) {
685 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
686 SDValue Segment = N.getOperand(0);
687
688 if (AM.Segment.getNode() == 0) {
689 AM.Segment = Segment;
690 return false;
691 }
692
693 return true;
694}
695
696bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
697 // This optimization is valid because the GNU TLS model defines that
698 // gs:0 (or fs:0 on X86-64) contains its own address.
699 // For more information see http://people.redhat.com/drepper/tls.pdf
700
701 SDValue Address = N.getOperand(1);
702 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
703 !MatchSegmentBaseAddress (Address, AM))
704 return false;
705
706 return true;
707}
708
Chris Lattner18c59872009-06-27 04:16:01 +0000709/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
710/// into an addressing mode. These wrap things that will resolve down into a
711/// symbol reference. If no match is possible, this returns true, otherwise it
712/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000713bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000714 // If the addressing mode already has a symbol as the displacement, we can
715 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000716 if (AM.hasSymbolicDisplacement())
717 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000718
719 SDValue N0 = N.getOperand(0);
Chris Lattner18c59872009-06-27 04:16:01 +0000720
721 // Handle X86-64 rip-relative addresses. We check this before checking direct
722 // folding because RIP is preferable to non-RIP accesses.
723 if (Subtarget->is64Bit() &&
724 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
725 // they cannot be folded into immediate fields.
726 // FIXME: This can be improved for kernel and other models?
727 TM.getCodeModel() == CodeModel::Small &&
728
729 // Base and index reg must be 0 in order to use %rip as base and lowering
730 // must allow RIP.
731 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
732
733 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
734 int64_t Offset = AM.Disp + G->getOffset();
735 if (!isInt32(Offset)) return true;
736 AM.GV = G->getGlobal();
737 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000738 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000739 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
740 int64_t Offset = AM.Disp + CP->getOffset();
741 if (!isInt32(Offset)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000742 AM.CP = CP->getConstVal();
743 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000744 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000745 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000746 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
747 AM.ES = S->getSymbol();
748 AM.SymbolFlags = S->getTargetFlags();
749 } else {
750 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
751 AM.JT = J->getIndex();
752 AM.SymbolFlags = J->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000753 }
Chris Lattner18c59872009-06-27 04:16:01 +0000754
755 if (N.getOpcode() == X86ISD::WrapperRIP)
756 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000757 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000758 }
759
760 // Handle the case when globals fit in our immediate field: This is true for
761 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
762 // mode, this results in a non-RIP-relative computation.
763 if (!Subtarget->is64Bit() ||
764 (TM.getCodeModel() == CodeModel::Small &&
765 TM.getRelocationModel() == Reloc::Static)) {
766 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
767 AM.GV = G->getGlobal();
768 AM.Disp += G->getOffset();
769 AM.SymbolFlags = G->getTargetFlags();
770 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
771 AM.CP = CP->getConstVal();
772 AM.Align = CP->getAlignment();
773 AM.Disp += CP->getOffset();
774 AM.SymbolFlags = CP->getTargetFlags();
775 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
776 AM.ES = S->getSymbol();
777 AM.SymbolFlags = S->getTargetFlags();
778 } else {
779 JumpTableSDNode *J = cast<JumpTableSDNode>(N0);
780 AM.JT = J->getIndex();
781 AM.SymbolFlags = J->getTargetFlags();
782 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000783 return false;
784 }
785
786 return true;
787}
788
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000789/// MatchAddress - Add the specified node to the specified addressing mode,
790/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000791/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000792bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
793 if (MatchAddressRecursively(N, AM, 0))
794 return true;
795
796 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
797 // a smaller encoding and avoids a scaled-index.
798 if (AM.Scale == 2 &&
799 AM.BaseType == X86ISelAddressMode::RegBase &&
800 AM.Base.Reg.getNode() == 0) {
801 AM.Base.Reg = AM.IndexReg;
802 AM.Scale = 1;
803 }
804
805 return false;
806}
807
808bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
809 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000810 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000811 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000812 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000813 // Limit recursion.
814 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000815 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000816
Chris Lattner18c59872009-06-27 04:16:01 +0000817 // If this is already a %rip relative address, we can only merge immediates
818 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000819 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000820 if (AM.isRIPRelative()) {
821 // FIXME: JumpTable and ExternalSymbol address currently don't like
822 // displacements. It isn't very important, but this should be fixed for
823 // consistency.
824 if (!AM.ES && AM.JT != -1) return true;
825
826 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
827 int64_t Val = AM.Disp + Cst->getSExtValue();
828 if (isInt32(Val)) {
829 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000830 return false;
831 }
832 }
833 return true;
834 }
835
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000836 switch (N.getOpcode()) {
837 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000838 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000839 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000840 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000841 AM.Disp += Val;
842 return false;
843 }
844 break;
845 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000846
Rafael Espindola094fad32009-04-08 21:14:34 +0000847 case X86ISD::SegmentBaseAddress:
848 if (!MatchSegmentBaseAddress(N, AM))
849 return false;
850 break;
851
Rafael Espindola49a168d2009-04-12 21:55:03 +0000852 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000853 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000854 if (!MatchWrapper(N, AM))
855 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000856 break;
857
Rafael Espindola094fad32009-04-08 21:14:34 +0000858 case ISD::LOAD:
859 if (!MatchLoad(N, AM))
860 return false;
861 break;
862
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000863 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000864 if (AM.BaseType == X86ISelAddressMode::RegBase
865 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000866 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
867 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
868 return false;
869 }
870 break;
Evan Chengec693f72005-12-08 02:01:35 +0000871
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000872 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000873 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000874 break;
875
Gabor Greif93c53e52008-08-31 15:37:04 +0000876 if (ConstantSDNode
877 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000878 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000879 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
880 // that the base operand remains free for further matching. If
881 // the base doesn't end up getting used, a post-processing step
882 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000883 if (Val == 1 || Val == 2 || Val == 3) {
884 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000885 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000886
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000887 // Okay, we know that we have a scale by now. However, if the scaled
888 // value is an add of something and a constant, we can fold the
889 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000890 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
891 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
892 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000893 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000894 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000895 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000896 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000897 AM.Disp = Disp;
898 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000899 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000900 } else {
901 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000902 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000903 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000904 }
905 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000906 }
Evan Chengec693f72005-12-08 02:01:35 +0000907
Dan Gohman83688052007-10-22 20:22:24 +0000908 case ISD::SMUL_LOHI:
909 case ISD::UMUL_LOHI:
910 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000911 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000912 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000913 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000914 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000915 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000916 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000917 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000918 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000919 if (ConstantSDNode
920 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000921 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
922 CN->getZExtValue() == 9) {
923 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000924
Gabor Greifba36cb52008-08-28 21:40:38 +0000925 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000926 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000927
928 // Okay, we know that we have a scale by now. However, if the scaled
929 // value is an add of something and a constant, we can fold the
930 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000931 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
932 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
933 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000934 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000935 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000936 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000937 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000938 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000939 AM.Disp = Disp;
940 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000941 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000942 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000943 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000944 }
945
946 AM.IndexReg = AM.Base.Reg = Reg;
947 return false;
948 }
Chris Lattner62412262007-02-04 20:18:17 +0000949 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000950 break;
951
Dan Gohman3cd90a12009-05-11 18:02:53 +0000952 case ISD::SUB: {
953 // Given A-B, if A can be completely folded into the address and
954 // the index field with the index field unused, use -B as the index.
955 // This is a win if a has multiple parts that can be folded into
956 // the address. Also, this saves a mov if the base register has
957 // other uses, since it avoids a two-address sub instruction, however
958 // it costs an additional mov if the index register has other uses.
959
960 // Test if the LHS of the sub can be folded.
961 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000962 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000963 AM = Backup;
964 break;
965 }
966 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +0000967 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +0000968 AM = Backup;
969 break;
970 }
971 int Cost = 0;
972 SDValue RHS = N.getNode()->getOperand(1);
973 // If the RHS involves a register with multiple uses, this
974 // transformation incurs an extra mov, due to the neg instruction
975 // clobbering its operand.
976 if (!RHS.getNode()->hasOneUse() ||
977 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
978 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
979 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
980 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
981 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
982 ++Cost;
983 // If the base is a register with multiple uses, this
984 // transformation may save a mov.
985 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
986 AM.Base.Reg.getNode() &&
987 !AM.Base.Reg.getNode()->hasOneUse()) ||
988 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
989 --Cost;
990 // If the folded LHS was interesting, this transformation saves
991 // address arithmetic.
992 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
993 ((AM.Disp != 0) && (Backup.Disp == 0)) +
994 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
995 --Cost;
996 // If it doesn't look like it may be an overall win, don't do it.
997 if (Cost >= 0) {
998 AM = Backup;
999 break;
1000 }
1001
1002 // Ok, the transformation is legal and appears profitable. Go for it.
1003 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1004 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1005 AM.IndexReg = Neg;
1006 AM.Scale = 1;
1007
1008 // Insert the new nodes into the topological ordering.
1009 if (Zero.getNode()->getNodeId() == -1 ||
1010 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1011 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1012 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1013 }
1014 if (Neg.getNode()->getNodeId() == -1 ||
1015 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1016 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1017 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1018 }
1019 return false;
1020 }
1021
Evan Cheng8e278262009-01-17 07:09:27 +00001022 case ISD::ADD: {
1023 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001024 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1025 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001026 return false;
1027 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001028 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1029 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001030 return false;
1031 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001032
1033 // If we couldn't fold both operands into the address at the same time,
1034 // see if we can just put each operand into a register and fold at least
1035 // the add.
1036 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1037 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001038 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001039 AM.Base.Reg = N.getNode()->getOperand(0);
1040 AM.IndexReg = N.getNode()->getOperand(1);
1041 AM.Scale = 1;
1042 return false;
1043 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001044 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001045 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001046
Chris Lattner62412262007-02-04 20:18:17 +00001047 case ISD::OR:
1048 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001049 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1050 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001051 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001052 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001053 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001054 // Address could not have picked a GV address for the displacement.
1055 AM.GV == NULL &&
1056 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +00001057 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001058 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001059 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001060 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001061 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001062 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001063 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001064 }
1065 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001066
1067 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001068 // Perform some heroic transforms on an and of a constant-count shift
1069 // with a constant to enable use of the scaled offset field.
1070
Dan Gohman475871a2008-07-27 21:46:04 +00001071 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001072 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001073
Evan Cheng1314b002007-12-13 00:43:27 +00001074 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001076
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001077 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001078 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1079 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1080 if (!C1 || !C2) break;
1081
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001082 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1083 // allows us to convert the shift and and into an h-register extract and
1084 // a scaled index.
1085 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1086 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001087 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001088 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1089 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1090 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1091 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1092 X, Eight);
1093 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1094 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +00001095 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1096 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1097 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001098
1099 // Insert the new nodes into the topological ordering.
1100 if (Eight.getNode()->getNodeId() == -1 ||
1101 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1102 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1103 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1104 }
1105 if (Mask.getNode()->getNodeId() == -1 ||
1106 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1107 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1108 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1109 }
1110 if (Srl.getNode()->getNodeId() == -1 ||
1111 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1112 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1113 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1114 }
1115 if (And.getNode()->getNodeId() == -1 ||
1116 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1117 CurDAG->RepositionNode(N.getNode(), And.getNode());
1118 And.getNode()->setNodeId(N.getNode()->getNodeId());
1119 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001120 if (ShlCount.getNode()->getNodeId() == -1 ||
1121 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1122 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1123 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1124 }
1125 if (Shl.getNode()->getNodeId() == -1 ||
1126 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1127 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1128 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1129 }
1130 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001131 AM.IndexReg = And;
1132 AM.Scale = (1 << ScaleLog);
1133 return false;
1134 }
1135 }
1136
1137 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1138 // allows us to fold the shift into this addressing mode.
1139 if (Shift.getOpcode() != ISD::SHL) break;
1140
Evan Cheng1314b002007-12-13 00:43:27 +00001141 // Not likely to be profitable if either the AND or SHIFT node has more
1142 // than one use (unless all uses are for address computation). Besides,
1143 // isel mechanism requires their node ids to be reused.
1144 if (!N.hasOneUse() || !Shift.hasOneUse())
1145 break;
1146
1147 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001148 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001149 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1150 break;
1151
1152 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001153 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001154 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001155 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1156 NewANDMask);
1157 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001158 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001159
1160 // Insert the new nodes into the topological ordering.
1161 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1162 CurDAG->RepositionNode(X.getNode(), C1);
1163 C1->setNodeId(X.getNode()->getNodeId());
1164 }
1165 if (NewANDMask.getNode()->getNodeId() == -1 ||
1166 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1167 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1168 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1169 }
1170 if (NewAND.getNode()->getNodeId() == -1 ||
1171 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1172 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1173 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1174 }
1175 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1176 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1177 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1178 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1179 }
1180
Dan Gohman7b8e9642008-10-13 20:52:04 +00001181 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001182
1183 AM.Scale = 1 << ShiftCst;
1184 AM.IndexReg = NewAND;
1185 return false;
1186 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001187 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001188
Rafael Espindola523249f2009-03-31 16:16:57 +00001189 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001190}
1191
1192/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1193/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001194bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001195 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001196 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001197 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001198 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001199 AM.IndexReg = N;
1200 AM.Scale = 1;
1201 return false;
1202 }
1203
1204 // Otherwise, we cannot select it.
1205 return true;
1206 }
1207
1208 // Default, generate it as a register.
1209 AM.BaseType = X86ISelAddressMode::RegBase;
1210 AM.Base.Reg = N;
1211 return false;
1212}
1213
Evan Chengec693f72005-12-08 02:01:35 +00001214/// SelectAddr - returns true if it is able pattern match an addressing mode.
1215/// It returns the operands which make up the maximal addressing mode it can
1216/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001217bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1218 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001219 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001220 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001221 bool Done = false;
1222 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1223 unsigned Opcode = N.getOpcode();
1224 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001225 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001226 // If we are able to fold N into addressing mode, then we'll allow it even
1227 // if N has multiple uses. In general, addressing computation is used as
1228 // addresses by all of its uses. But watch out for CopyToReg uses, that
1229 // means the address computation is liveout. It will be computed by a LEA
1230 // so we want to avoid computing the address twice.
1231 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1232 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1233 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001234 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001235 Done = true;
1236 break;
1237 }
1238 }
1239 }
1240 }
1241
1242 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001243 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001244
Duncan Sands83ec4b62008-06-06 12:08:01 +00001245 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001246 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001247 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001248 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001249 }
Evan Cheng8700e142006-01-11 06:09:51 +00001250
Gabor Greifba36cb52008-08-28 21:40:38 +00001251 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001252 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001253
Rafael Espindola094fad32009-04-08 21:14:34 +00001254 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001255 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001256}
1257
Chris Lattner3a7cd952006-10-07 21:55:32 +00001258/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1259/// match a load whose top elements are either undef or zeros. The load flavor
1260/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001261bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1262 SDValue N, SDValue &Base,
1263 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001264 SDValue &Disp, SDValue &Segment,
1265 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001266 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001267 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001268 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001269 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001270 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001271 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001272 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001273 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001274 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001275 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001276 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001277 return true;
1278 }
1279 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001280
1281 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001282 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001283 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001284 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001285 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 N.getOperand(0).getNode()->hasOneUse() &&
1287 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001288 N.getOperand(0).getOperand(0).hasOneUse()) {
1289 // Okay, this is a zero extending load. Fold it.
1290 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001291 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001292 return false;
1293 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001294 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001295 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001296 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001297 return false;
1298}
1299
1300
Evan Cheng51a9ed92006-02-25 10:09:08 +00001301/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1302/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001303bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1304 SDValue &Base, SDValue &Scale,
1305 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001306 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001307
1308 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1309 // segments.
1310 SDValue Copy = AM.Segment;
1311 SDValue T = CurDAG->getRegister(0, MVT::i32);
1312 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001313 if (MatchAddress(N, AM))
1314 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001315 assert (T == AM.Segment);
1316 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001317
Duncan Sands83ec4b62008-06-06 12:08:01 +00001318 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001319 unsigned Complexity = 0;
1320 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001321 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001322 Complexity = 1;
1323 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001324 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001325 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1326 Complexity = 4;
1327
Gabor Greifba36cb52008-08-28 21:40:38 +00001328 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001329 Complexity++;
1330 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001331 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001332
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001333 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1334 // a simple shift.
1335 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001336 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001337
1338 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1339 // to a LEA. This is determined with some expermentation but is by no means
1340 // optimal (especially for code size consideration). LEA is nice because of
1341 // its three-address nature. Tweak the cost function again when we can run
1342 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001343 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001344 // For X86-64, we should always use lea to materialize RIP relative
1345 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001346 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001347 Complexity = 4;
1348 else
1349 Complexity += 2;
1350 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001351
Gabor Greifba36cb52008-08-28 21:40:38 +00001352 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001353 Complexity++;
1354
Chris Lattner25142782009-07-11 22:50:33 +00001355 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001356 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001357 return false;
1358
1359 SDValue Segment;
1360 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1361 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001362}
1363
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001364/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1365bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1366 SDValue &Scale, SDValue &Index,
1367 SDValue &Disp) {
1368 assert(Op.getOpcode() == X86ISD::TLSADDR);
1369 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1370 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1371
1372 X86ISelAddressMode AM;
1373 AM.GV = GA->getGlobal();
1374 AM.Disp += GA->getOffset();
1375 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001376 AM.SymbolFlags = GA->getTargetFlags();
1377
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001378 if (N.getValueType() == MVT::i32) {
1379 AM.Scale = 1;
1380 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1381 } else {
1382 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1383 }
1384
1385 SDValue Segment;
1386 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1387 return true;
1388}
1389
1390
Dan Gohman475871a2008-07-27 21:46:04 +00001391bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1392 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001393 SDValue &Index, SDValue &Disp,
1394 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001395 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001396 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001397 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001398 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001399 return false;
1400}
1401
Dan Gohman8b746962008-09-23 18:22:58 +00001402/// getGlobalBaseReg - Return an SDNode that returns the value of
1403/// the global base register. Output instructions required to
1404/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001405///
Evan Cheng9ade2182006-08-26 05:34:46 +00001406SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001407 MachineFunction *MF = CurBB->getParent();
Dan Gohmanc5534622009-06-03 20:20:00 +00001408 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001409 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001410}
1411
Evan Chengb245d922006-05-20 01:36:52 +00001412static SDNode *FindCallStartFromCall(SDNode *Node) {
1413 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1414 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1415 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001416 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001417}
1418
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001419SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1420 SDValue Chain = Node->getOperand(0);
1421 SDValue In1 = Node->getOperand(1);
1422 SDValue In2L = Node->getOperand(2);
1423 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001424 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1425 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001426 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001427 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001428 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001429 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1430 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001431 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001432}
Christopher Lambc59e5212007-08-10 21:48:46 +00001433
Dan Gohman475871a2008-07-27 21:46:04 +00001434SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001435 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001436 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001437 unsigned Opc, MOpc;
1438 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001439 DebugLoc dl = Node->getDebugLoc();
1440
Evan Chengf597dc72006-02-10 22:24:32 +00001441#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001442 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001443 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001444 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001445 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001446#endif
1447
Dan Gohmane8be6c62008-07-17 19:10:17 +00001448 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001449#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001450 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001451 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001452 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001453 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001454#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001455 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001456 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001457
Evan Cheng0114e942006-01-06 20:36:21 +00001458 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001459 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001460 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001461 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001462
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001463 case X86ISD::ATOMOR64_DAG:
1464 return SelectAtomic64(Node, X86::ATOMOR6432);
1465 case X86ISD::ATOMXOR64_DAG:
1466 return SelectAtomic64(Node, X86::ATOMXOR6432);
1467 case X86ISD::ATOMADD64_DAG:
1468 return SelectAtomic64(Node, X86::ATOMADD6432);
1469 case X86ISD::ATOMSUB64_DAG:
1470 return SelectAtomic64(Node, X86::ATOMSUB6432);
1471 case X86ISD::ATOMNAND64_DAG:
1472 return SelectAtomic64(Node, X86::ATOMNAND6432);
1473 case X86ISD::ATOMAND64_DAG:
1474 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001475 case X86ISD::ATOMSWAP64_DAG:
1476 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001477
Dan Gohman525178c2007-10-08 18:33:35 +00001478 case ISD::SMUL_LOHI:
1479 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001480 SDValue N0 = Node->getOperand(0);
1481 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001482
Dan Gohman525178c2007-10-08 18:33:35 +00001483 bool isSigned = Opcode == ISD::SMUL_LOHI;
1484 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001485 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001486 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001487 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1488 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1489 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001490 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001491 }
1492 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001493 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001494 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001495 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1496 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1497 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001498 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001499 }
1500
1501 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001502 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001503 default: llvm_unreachable("Unsupported VT!");
Evan Cheng0114e942006-01-06 20:36:21 +00001504 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1505 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1506 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001507 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001508 }
1509
Rafael Espindola094fad32009-04-08 21:14:34 +00001510 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1511 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001512 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001513 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001514 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001515 if (foldedLoad)
1516 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001517 }
1518
Dale Johannesendd64c412009-02-04 00:33:20 +00001519 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001520 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001521
1522 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001523 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1524 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001525 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001526 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001527 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001528 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001529 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001530 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001531 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001532 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001533 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001534 }
1535
Dan Gohman525178c2007-10-08 18:33:35 +00001536 // Copy the low half of the result, if it is needed.
1537 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001538 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001539 LoReg, NVT, InFlag);
1540 InFlag = Result.getValue(2);
1541 ReplaceUses(N.getValue(0), Result);
1542#ifndef NDEBUG
1543 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001544 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001545 DOUT << "\n";
1546#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001547 }
Dan Gohman525178c2007-10-08 18:33:35 +00001548 // Copy the high half of the result, if it is needed.
1549 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001550 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001551 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1552 // Prevent use of AH in a REX instruction by referencing AX instead.
1553 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001554 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001555 X86::AX, MVT::i16, InFlag);
1556 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001557 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1558 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001559 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001560 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001561 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001562 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001563 MVT::i8, Result, SRIdx), 0);
1564 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001565 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001566 HiReg, NVT, InFlag);
1567 InFlag = Result.getValue(2);
1568 }
1569 ReplaceUses(N.getValue(1), Result);
1570#ifndef NDEBUG
1571 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001572 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001573 DOUT << "\n";
1574#endif
1575 }
Evan Cheng34167212006-02-09 00:37:58 +00001576
Evan Chengf597dc72006-02-10 22:24:32 +00001577#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001578 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001579#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001580
Evan Cheng64a752f2006-08-11 09:08:15 +00001581 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001582 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001583
Dan Gohman525178c2007-10-08 18:33:35 +00001584 case ISD::SDIVREM:
1585 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001586 SDValue N0 = Node->getOperand(0);
1587 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001588
1589 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001590 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001591 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001592 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001593 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1594 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1595 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001596 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001597 }
1598 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001599 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001600 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001601 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1602 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1603 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001604 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001605 }
1606
1607 unsigned LoReg, HiReg;
1608 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001609 switch (NVT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001610 default: llvm_unreachable("Unsupported VT!");
Evan Cheng948f3432006-01-06 23:19:29 +00001611 case MVT::i8:
1612 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001613 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001614 SExtOpcode = X86::CBW;
1615 break;
1616 case MVT::i16:
1617 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001618 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001619 SExtOpcode = X86::CWD;
1620 break;
1621 case MVT::i32:
1622 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001623 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001624 SExtOpcode = X86::CDQ;
1625 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001626 case MVT::i64:
1627 LoReg = X86::RAX; HiReg = X86::RDX;
Chris Lattner9ac75422009-07-14 20:19:57 +00001628 ClrOpcode = ~0U; // NOT USED.
Evan Cheng25ab6902006-09-08 06:48:29 +00001629 SExtOpcode = X86::CQO;
1630 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001631 }
1632
Rafael Espindola094fad32009-04-08 21:14:34 +00001633 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1634 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001635 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001636
Dan Gohman475871a2008-07-27 21:46:04 +00001637 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001638 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001639 // Special case for div8, just use a move with zero extension to AX to
1640 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001641 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1642 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1643 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001644 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001645 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001646 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001647 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001648 Chain = Move.getValue(1);
1649 ReplaceUses(N0.getValue(1), Chain);
1650 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001651 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001652 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001653 Chain = CurDAG->getEntryNode();
1654 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001655 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001656 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001657 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001658 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001659 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001660 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001661 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001662 // Sign extend the low part into the high part.
1663 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001664 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001665 } else {
1666 // Zero out the high part, effectively zero extending the input.
Chris Lattner9ac75422009-07-14 20:19:57 +00001667 SDValue ClrNode;
1668
1669 if (NVT.getSimpleVT() == MVT::i64) {
1670 ClrNode = SDValue(CurDAG->getTargetNode(X86::MOV32r0, dl, MVT::i32),
1671 0);
1672 // We just did a 32-bit clear, insert it into a 64-bit register to
1673 // clear the whole 64-bit reg.
1674 SDValue Undef =
1675 SDValue(CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
1676 dl, MVT::i64), 0);
1677 SDValue SubRegNo =
1678 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
1679 ClrNode =
1680 SDValue(CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl,
1681 MVT::i64, Undef, ClrNode, SubRegNo),
1682 0);
1683 } else {
1684 ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT), 0);
1685 }
1686
Dale Johannesendd64c412009-02-04 00:33:20 +00001687 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001688 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001689 }
Evan Cheng948f3432006-01-06 23:19:29 +00001690 }
1691
1692 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001693 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1694 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001695 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001696 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001697 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001698 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001699 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001700 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001701 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001702 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001703 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001704 }
1705
Dan Gohmana37c9f72007-09-25 18:23:27 +00001706 // Copy the division (low) result, if it is needed.
1707 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001708 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001709 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001710 InFlag = Result.getValue(2);
1711 ReplaceUses(N.getValue(0), Result);
1712#ifndef NDEBUG
1713 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001714 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001715 DOUT << "\n";
1716#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001717 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001718 // Copy the remainder (high) result, if it is needed.
1719 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001720 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001721 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1722 // Prevent use of AH in a REX instruction by referencing AX instead.
1723 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001724 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001725 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001726 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001727 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1728 Result,
1729 CurDAG->getTargetConstant(8, MVT::i8)),
1730 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001731 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001732 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001733 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001734 MVT::i8, Result, SRIdx), 0);
1735 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001736 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001737 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001738 InFlag = Result.getValue(2);
1739 }
1740 ReplaceUses(N.getValue(1), Result);
1741#ifndef NDEBUG
1742 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001743 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001744 DOUT << "\n";
1745#endif
1746 }
Evan Chengf597dc72006-02-10 22:24:32 +00001747
1748#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001749 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001750#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001751
1752 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001753 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001754
Evan Cheng851bc042008-06-17 02:01:22 +00001755 case ISD::DECLARE: {
1756 // Handle DECLARE nodes here because the second operand may have been
1757 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue Chain = Node->getOperand(0);
1759 SDValue N1 = Node->getOperand(1);
1760 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001761 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001762
1763 // FIXME: We need to handle this for VLAs.
1764 if (!FINode) {
1765 ReplaceUses(N.getValue(0), Chain);
1766 return NULL;
1767 }
1768
Evan Chengfab83872008-06-18 02:48:27 +00001769 if (N2.getOpcode() == ISD::ADD &&
1770 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1771 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001772
1773 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1774 // somehow, just ignore it.
Chris Lattner18c59872009-06-27 04:16:01 +00001775 if (N2.getOpcode() != X86ISD::Wrapper &&
1776 N2.getOpcode() != X86ISD::WrapperRIP) {
Chris Lattner1823c922009-02-12 17:33:11 +00001777 ReplaceUses(N.getValue(0), Chain);
1778 return NULL;
1779 }
Evan Chengf2accb52009-01-10 03:33:22 +00001780 GlobalAddressSDNode *GVNode =
1781 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001782 if (GVNode == 0) {
1783 ReplaceUses(N.getValue(0), Chain);
1784 return NULL;
1785 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001786 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1787 TLI.getPointerTy());
1788 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1789 TLI.getPointerTy());
1790 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001791 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001792 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001793 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001794 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001795 }
1796
Evan Cheng9ade2182006-08-26 05:34:46 +00001797 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001798
Evan Chengf597dc72006-02-10 22:24:32 +00001799#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001800 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001801 if (ResNode == NULL || ResNode == N.getNode())
1802 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001803 else
1804 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001805 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001806 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001807#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001808
1809 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001810}
1811
Chris Lattnerc0bad572006-06-08 18:03:49 +00001812bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001813SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001814 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001815 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001816 switch (ConstraintCode) {
1817 case 'o': // offsetable ??
1818 case 'v': // not offsetable ??
1819 default: return true;
1820 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001821 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001822 return true;
1823 break;
1824 }
1825
Evan Cheng04699902006-08-26 01:05:16 +00001826 OutOps.push_back(Op0);
1827 OutOps.push_back(Op1);
1828 OutOps.push_back(Op2);
1829 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001830 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001831 return false;
1832}
1833
Chris Lattnerc961eea2005-11-16 01:54:32 +00001834/// createX86ISelDag - This pass converts a legalized DAG into a
1835/// X86-specific DAG, ready for instruction scheduling.
1836///
Bill Wendling98a366d2009-04-29 23:29:43 +00001837FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1838 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001839 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001840}