blob: 9cedafc8d9348780e03bd45e980c5afa0174c0c3 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Dale Johannesen50dd1d02008-08-11 23:46:25 +000039#include "llvm/Support/Streams.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Evan Cheng4d952322009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Chris Lattner95b2c7d2006-12-19 22:59:26 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
Evan Chengbe3bf422008-02-07 08:53:49 +000068 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000070 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000071 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000072 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000073 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000074 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000075 const char *ES;
76 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000077 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078
79 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000080 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
Rafael Espindola094fad32009-04-08 21:14:34 +000081 Segment(), GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000082 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000083
84 bool hasSymbolicDisplacement() const {
85 return GV != 0 || CP != 0 || ES != 0 || JT != -1;
86 }
87
Dale Johannesen50dd1d02008-08-11 23:46:25 +000088 void dump() {
89 cerr << "X86ISelAddressMode " << this << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000090 cerr << "Base.Reg ";
91 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
92 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000093 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
94 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n";
Gabor Greif93c53e52008-08-31 15:37:04 +000095 cerr << "IndexReg ";
96 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
97 else cerr << "nul";
Dale Johannesen50dd1d02008-08-11 23:46:25 +000098 cerr << " Disp " << Disp << "\n";
99 cerr << "GV "; if (GV) GV->dump();
100 else cerr << "nul";
101 cerr << " CP "; if (CP) CP->dump();
102 else cerr << "nul";
103 cerr << "\n";
104 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul";
105 cerr << " JT" << JT << " Align" << Align << "\n";
106 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000107 };
108}
109
110namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000111 //===--------------------------------------------------------------------===//
112 /// ISel - X86 specific code to select X86 machine instructions for
113 /// SelectionDAG operations.
114 ///
Chris Lattner2c79de82006-06-28 23:27:49 +0000115 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000116 /// X86Lowering - This object fully describes how to lower LLVM code to an
117 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000118 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000119
120 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
121 /// make the right decision when generating code for different targets.
122 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000123
Evan Chengdb8d56b2008-06-30 20:45:06 +0000124 /// CurBB - Current BB being isel'd.
125 ///
126 MachineBasicBlock *CurBB;
127
Evan Chengb7a75a52008-09-26 23:41:32 +0000128 /// OptForSize - If true, selector should try to optimize for code size
129 /// instead of performance.
130 bool OptForSize;
131
Chris Lattnerc961eea2005-11-16 01:54:32 +0000132 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000133 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000134 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000135 X86Lowering(*tm.getTargetLowering()),
136 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000137 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000138
139 virtual const char *getPassName() const {
140 return "X86 DAG->DAG Instruction Selection";
141 }
142
Evan Chengdb8d56b2008-06-30 20:45:06 +0000143 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000144 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000145 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000146
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000147 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
148
Evan Cheng884c70c2008-11-27 00:49:46 +0000149 virtual
150 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000151
Chris Lattnerc961eea2005-11-16 01:54:32 +0000152// Include the pieces autogenerated from the target description.
153#include "X86GenDAGISel.inc"
154
155 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000156 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000157 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000158
Rafael Espindola094fad32009-04-08 21:14:34 +0000159 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
160 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000161 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000162 bool MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000163 unsigned Depth = 0);
164 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000165 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000166 SDValue &Scale, SDValue &Index, SDValue &Disp,
167 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000168 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
169 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000170 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
171 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000172 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
173 SDValue N, SDValue &Base, SDValue &Scale,
174 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000175 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000176 SDValue &InChain, SDValue &OutChain);
177 bool TryFoldLoad(SDValue P, SDValue N,
178 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000179 SDValue &Index, SDValue &Disp,
180 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000181 void PreprocessForRMW();
182 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000183
Chris Lattnerc0bad572006-06-08 18:03:49 +0000184 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
185 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000186 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000187 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000188 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000189
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000190 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
191
Dan Gohman475871a2008-07-27 21:46:04 +0000192 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
193 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000194 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000195 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000196 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
197 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000198 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000199 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000200 // These are 32-bit even in 64-bit mode since RIP relative offset
201 // is 32-bit.
202 if (AM.GV)
203 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
204 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000205 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
206 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000207 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000208 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 else if (AM.JT != -1)
210 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
211 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000212 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000213
214 if (AM.Segment.getNode())
215 Segment = AM.Segment;
216 else
217 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000218 }
219
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000220 /// getI8Imm - Return a target constant with the specified value, of type
221 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000222 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000223 return CurDAG->getTargetConstant(Imm, MVT::i8);
224 }
225
Chris Lattnerc961eea2005-11-16 01:54:32 +0000226 /// getI16Imm - Return a target constant with the specified value, of type
227 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000228 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000229 return CurDAG->getTargetConstant(Imm, MVT::i16);
230 }
231
232 /// getI32Imm - Return a target constant with the specified value, of type
233 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000234 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000235 return CurDAG->getTargetConstant(Imm, MVT::i32);
236 }
Evan Chengf597dc72006-02-10 22:24:32 +0000237
Dan Gohman8b746962008-09-23 18:22:58 +0000238 /// getGlobalBaseReg - Return an SDNode that returns the value of
239 /// the global base register. Output instructions required to
240 /// initialize the global base register, if necessary.
241 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000242 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000243
Dan Gohmanc5534622009-06-03 20:20:00 +0000244 /// getTargetMachine - Return a reference to the TargetMachine, casted
245 /// to the target-specific type.
246 const X86TargetMachine &getTargetMachine() {
247 return static_cast<const X86TargetMachine &>(TM);
248 }
249
250 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
251 /// to the target-specific type.
252 const X86InstrInfo *getInstrInfo() {
253 return getTargetMachine().getInstrInfo();
254 }
255
Evan Cheng23addc02006-02-10 22:46:26 +0000256#ifndef NDEBUG
257 unsigned Indent;
258#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000259 };
260}
261
Evan Chengf4b4c412006-08-08 00:31:00 +0000262
Evan Cheng884c70c2008-11-27 00:49:46 +0000263bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
264 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000265 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000266
Evan Cheng884c70c2008-11-27 00:49:46 +0000267 if (U == Root)
268 switch (U->getOpcode()) {
269 default: break;
270 case ISD::ADD:
271 case ISD::ADDC:
272 case ISD::ADDE:
273 case ISD::AND:
274 case ISD::OR:
275 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000276 SDValue Op1 = U->getOperand(1);
277
Evan Cheng884c70c2008-11-27 00:49:46 +0000278 // If the other operand is a 8-bit immediate we should fold the immediate
279 // instead. This reduces code size.
280 // e.g.
281 // movl 4(%esp), %eax
282 // addl $4, %eax
283 // vs.
284 // movl $4, %eax
285 // addl 4(%esp), %eax
286 // The former is 2 bytes shorter. In case where the increment is 1, then
287 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000288 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000289 if (Imm->getAPIntValue().isSignedIntN(8))
290 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000291
292 // If the other operand is a TLS address, we should fold it instead.
293 // This produces
294 // movl %gs:0, %eax
295 // leal i@NTPOFF(%eax), %eax
296 // instead of
297 // movl $i@NTPOFF, %eax
298 // addl %gs:0, %eax
299 // if the block also has an access to a second TLS address this will save
300 // a load.
301 // FIXME: This is probably also true for non TLS addresses.
302 if (Op1.getOpcode() == X86ISD::Wrapper) {
303 SDValue Val = Op1.getOperand(0);
304 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
305 return false;
306 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000307 }
308 }
309
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000310 // Proceed to 'generic' cycle finder code
311 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000312}
313
Evan Cheng70e674e2006-08-28 20:10:17 +0000314/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
315/// and move load below the TokenFactor. Replace store's chain operand with
316/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000317static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000318 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000319 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000320 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
321 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000322 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000323 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000324 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000325 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
326 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
327 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
328 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000329}
330
Evan Chengcd0baf22008-05-23 21:23:16 +0000331/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
332///
Dan Gohman475871a2008-07-27 21:46:04 +0000333static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
334 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000335 if (N.getOpcode() == ISD::BIT_CONVERT)
336 N = N.getOperand(0);
337
338 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
339 if (!LD || LD->isVolatile())
340 return false;
341 if (LD->getAddressingMode() != ISD::UNINDEXED)
342 return false;
343
344 ISD::LoadExtType ExtType = LD->getExtensionType();
345 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
346 return false;
347
348 if (N.hasOneUse() &&
349 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000350 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000351 Load = N;
352 return true;
353 }
354 return false;
355}
356
Evan Chengab6c3bb2008-08-25 21:27:18 +0000357/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
358/// operand and move load below the call's chain operand.
359static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000360 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000361 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000362 SDValue Chain = CallSeqStart.getOperand(0);
363 if (Chain.getNode() == Load.getNode())
364 Ops.push_back(Load.getOperand(0));
365 else {
366 assert(Chain.getOpcode() == ISD::TokenFactor &&
367 "Unexpected CallSeqStart chain operand");
368 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
369 if (Chain.getOperand(i).getNode() == Load.getNode())
370 Ops.push_back(Load.getOperand(0));
371 else
372 Ops.push_back(Chain.getOperand(i));
373 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000374 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
375 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000376 Ops.clear();
377 Ops.push_back(NewChain);
378 }
379 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
380 Ops.push_back(CallSeqStart.getOperand(i));
381 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000382 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
383 Load.getOperand(1), Load.getOperand(2));
384 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000385 Ops.push_back(SDValue(Load.getNode(), 1));
386 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000387 Ops.push_back(Call.getOperand(i));
388 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
389}
390
391/// isCalleeLoad - Return true if call address is a load and it can be
392/// moved below CALLSEQ_START and the chains leading up to the call.
393/// Return the CALLSEQ_START by reference as a second output.
394static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000395 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000396 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000397 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000398 if (!LD ||
399 LD->isVolatile() ||
400 LD->getAddressingMode() != ISD::UNINDEXED ||
401 LD->getExtensionType() != ISD::NON_EXTLOAD)
402 return false;
403
404 // Now let's find the callseq_start.
405 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
406 if (!Chain.hasOneUse())
407 return false;
408 Chain = Chain.getOperand(0);
409 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000410
411 if (Chain.getOperand(0).getNode() == Callee.getNode())
412 return true;
413 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
414 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
415 return true;
416 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000417}
418
419
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000420/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000421/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000422/// This allows the instruction selector to pick more read-modify-write
423/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000424///
425/// [Load chain]
426/// ^
427/// |
428/// [Load]
429/// ^ ^
430/// | |
431/// / \-
432/// / |
433/// [TokenFactor] [Op]
434/// ^ ^
435/// | |
436/// \ /
437/// \ /
438/// [Store]
439///
440/// The fact the store's chain operand != load's chain will prevent the
441/// (store (op (load))) instruction from being selected. We can transform it to:
442///
443/// [Load chain]
444/// ^
445/// |
446/// [TokenFactor]
447/// ^
448/// |
449/// [Load]
450/// ^ ^
451/// | |
452/// | \-
453/// | |
454/// | [Op]
455/// | ^
456/// | |
457/// \ /
458/// \ /
459/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000460void X86DAGToDAGISel::PreprocessForRMW() {
461 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
462 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000463 if (I->getOpcode() == X86ISD::CALL) {
464 /// Also try moving call address load from outside callseq_start to just
465 /// before the call to allow it to be folded.
466 ///
467 /// [Load chain]
468 /// ^
469 /// |
470 /// [Load]
471 /// ^ ^
472 /// | |
473 /// / \--
474 /// / |
475 ///[CALLSEQ_START] |
476 /// ^ |
477 /// | |
478 /// [LOAD/C2Reg] |
479 /// | |
480 /// \ /
481 /// \ /
482 /// [CALL]
483 SDValue Chain = I->getOperand(0);
484 SDValue Load = I->getOperand(1);
485 if (!isCalleeLoad(Load, Chain))
486 continue;
487 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
488 ++NumLoadMoved;
489 continue;
490 }
491
Evan Cheng8b2794a2006-10-13 21:14:26 +0000492 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000493 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000494 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000495
Gabor Greifba36cb52008-08-28 21:40:38 +0000496 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000497 continue;
498
Dan Gohman475871a2008-07-27 21:46:04 +0000499 SDValue N1 = I->getOperand(1);
500 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000501 if ((N1.getValueType().isFloatingPoint() &&
502 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000503 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000504 continue;
505
506 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000507 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000508 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000509 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000510 case ISD::ADD:
511 case ISD::MUL:
512 case ISD::AND:
513 case ISD::OR:
514 case ISD::XOR:
515 case ISD::ADDC:
516 case ISD::ADDE:
517 case ISD::VECTOR_SHUFFLE: {
518 SDValue N10 = N1.getOperand(0);
519 SDValue N11 = N1.getOperand(1);
520 RModW = isRMWLoad(N10, Chain, N2, Load);
521 if (!RModW)
522 RModW = isRMWLoad(N11, Chain, N2, Load);
523 break;
524 }
525 case ISD::SUB:
526 case ISD::SHL:
527 case ISD::SRA:
528 case ISD::SRL:
529 case ISD::ROTL:
530 case ISD::ROTR:
531 case ISD::SUBC:
532 case ISD::SUBE:
533 case X86ISD::SHLD:
534 case X86ISD::SHRD: {
535 SDValue N10 = N1.getOperand(0);
536 RModW = isRMWLoad(N10, Chain, N2, Load);
537 break;
538 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000539 }
540
Evan Cheng82a35b32006-08-29 06:44:17 +0000541 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000542 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000543 ++NumLoadMoved;
544 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000545 }
546}
547
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000548
549/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
550/// nodes that target the FP stack to be store and load to the stack. This is a
551/// gross hack. We would like to simply mark these as being illegal, but when
552/// we do that, legalize produces these when it expands calls, then expands
553/// these in the same legalize pass. We would like dag combine to be able to
554/// hack on these between the call expansion and the node legalization. As such
555/// this pass basically does "really late" legalization of these inline with the
556/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000557void X86DAGToDAGISel::PreprocessForFPConvert() {
558 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
559 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000560 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
561 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
562 continue;
563
564 // If the source and destination are SSE registers, then this is a legal
565 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000566 MVT SrcVT = N->getOperand(0).getValueType();
567 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000568 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
569 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
570 if (SrcIsSSE && DstIsSSE)
571 continue;
572
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000573 if (!SrcIsSSE && !DstIsSSE) {
574 // If this is an FPStack extension, it is a noop.
575 if (N->getOpcode() == ISD::FP_EXTEND)
576 continue;
577 // If this is a value-preserving FPStack truncation, it is a noop.
578 if (N->getConstantOperandVal(1))
579 continue;
580 }
581
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000582 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
583 // FPStack has extload and truncstore. SSE can fold direct loads into other
584 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000585 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000586 if (N->getOpcode() == ISD::FP_ROUND)
587 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
588 else
589 MemVT = SrcIsSSE ? SrcVT : DstVT;
590
Dan Gohmanf350b272008-08-23 02:25:05 +0000591 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000592 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000593
594 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000595 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000596 N->getOperand(0),
597 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000598 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000599 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000600
601 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
602 // extload we created. This will cause general havok on the dag because
603 // anything below the conversion could be folded into other existing nodes.
604 // To avoid invalidating 'I', back it up to the convert node.
605 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000606 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000607
608 // Now that we did that, the node is dead. Increment the iterator to the
609 // next node to process, then delete N.
610 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000611 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000612 }
613}
614
Chris Lattnerc961eea2005-11-16 01:54:32 +0000615/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
616/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000617void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000618 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000619 const Function *F = CurDAG->getMachineFunction().getFunction();
620 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000621
Evan Chengdb8d56b2008-06-30 20:45:06 +0000622 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000623 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000624 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000625
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000626 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000627 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000628
Chris Lattnerc961eea2005-11-16 01:54:32 +0000629 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000630#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000631 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000632 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000633#endif
David Greene8ad4c002008-10-27 21:56:29 +0000634 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000635#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000636 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000637#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000638
Dan Gohmanf350b272008-08-23 02:25:05 +0000639 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000640}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000641
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000642/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
643/// the main function.
644void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
645 MachineFrameInfo *MFI) {
646 const TargetInstrInfo *TII = TM.getInstrInfo();
647 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000648 BuildMI(BB, DebugLoc::getUnknownLoc(),
649 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000650}
651
652void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
653 // If this is main, emit special code for main.
654 MachineBasicBlock *BB = MF.begin();
655 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
656 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
657}
658
Rafael Espindola094fad32009-04-08 21:14:34 +0000659
660bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
661 X86ISelAddressMode &AM) {
662 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
663 SDValue Segment = N.getOperand(0);
664
665 if (AM.Segment.getNode() == 0) {
666 AM.Segment = Segment;
667 return false;
668 }
669
670 return true;
671}
672
673bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
674 // This optimization is valid because the GNU TLS model defines that
675 // gs:0 (or fs:0 on X86-64) contains its own address.
676 // For more information see http://people.redhat.com/drepper/tls.pdf
677
678 SDValue Address = N.getOperand(1);
679 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
680 !MatchSegmentBaseAddress (Address, AM))
681 return false;
682
683 return true;
684}
685
Rafael Espindola49a168d2009-04-12 21:55:03 +0000686bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Dan Gohmanc5534622009-06-03 20:20:00 +0000687 bool SymbolicAddressesAreRIPRel =
688 getTargetMachine().symbolicAddressesAreRIPRel();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000689 bool is64Bit = Subtarget->is64Bit();
690 DOUT << "Wrapper: 64bit " << is64Bit;
691 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Rafael Espindolab2157762009-04-12 23:00:38 +0000692
Rafael Espindola49a168d2009-04-12 21:55:03 +0000693 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Rafael Espindolab2157762009-04-12 23:00:38 +0000694 if (is64Bit && (TM.getCodeModel() != CodeModel::Small))
Rafael Espindola49a168d2009-04-12 21:55:03 +0000695 return true;
Rafael Espindolab2157762009-04-12 23:00:38 +0000696
697 // Base and index reg must be 0 in order to use rip as base.
698 bool canUsePICRel = !AM.Base.Reg.getNode() && !AM.IndexReg.getNode();
Dan Gohmanc5534622009-06-03 20:20:00 +0000699 if (is64Bit && !canUsePICRel && SymbolicAddressesAreRIPRel)
Rafael Espindolab2157762009-04-12 23:00:38 +0000700 return true;
701
Rafael Espindola49a168d2009-04-12 21:55:03 +0000702 if (AM.hasSymbolicDisplacement())
703 return true;
704 // If value is available in a register both base and index components have
705 // been picked, we can't fit the result available in the register in the
706 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
707
708 SDValue N0 = N.getOperand(0);
709 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
710 uint64_t Offset = G->getOffset();
711 if (!is64Bit || isInt32(AM.Disp + Offset)) {
712 GlobalValue *GV = G->getGlobal();
Dan Gohmanc5534622009-06-03 20:20:00 +0000713 bool isRIPRel = SymbolicAddressesAreRIPRel;
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000714 if (N0.getOpcode() == llvm::ISD::TargetGlobalTLSAddress) {
715 TLSModel::Model model =
716 getTLSModel (GV, TM.getRelocationModel());
717 if (is64Bit && model == TLSModel::InitialExec)
718 isRIPRel = true;
719 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000720 AM.GV = GV;
721 AM.Disp += Offset;
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000722 AM.isRIPRel = isRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000723 return false;
724 }
725 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
726 uint64_t Offset = CP->getOffset();
727 if (!is64Bit || isInt32(AM.Disp + Offset)) {
728 AM.CP = CP->getConstVal();
729 AM.Align = CP->getAlignment();
730 AM.Disp += Offset;
Dan Gohmanc5534622009-06-03 20:20:00 +0000731 AM.isRIPRel = SymbolicAddressesAreRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000732 return false;
733 }
734 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
735 AM.ES = S->getSymbol();
Dan Gohmanc5534622009-06-03 20:20:00 +0000736 AM.isRIPRel = SymbolicAddressesAreRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000737 return false;
738 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
739 AM.JT = J->getIndex();
Dan Gohmanc5534622009-06-03 20:20:00 +0000740 AM.isRIPRel = SymbolicAddressesAreRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000741 return false;
742 }
743
744 return true;
745}
746
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000747/// MatchAddress - Add the specified node to the specified addressing mode,
748/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000749/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000750bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000751 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000752 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000753 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000754 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000755 // Limit recursion.
756 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000757 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000758
Evan Cheng25ab6902006-09-08 06:48:29 +0000759 // RIP relative addressing: %rip + 32-bit displacement!
760 if (AM.isRIPRel) {
761 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000762 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000763 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000764 AM.Disp += Val;
765 return false;
766 }
767 }
768 return true;
769 }
770
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000771 switch (N.getOpcode()) {
772 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000773 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000774 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000775 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000776 AM.Disp += Val;
777 return false;
778 }
779 break;
780 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000781
Rafael Espindola094fad32009-04-08 21:14:34 +0000782 case X86ISD::SegmentBaseAddress:
783 if (!MatchSegmentBaseAddress(N, AM))
784 return false;
785 break;
786
Rafael Espindola49a168d2009-04-12 21:55:03 +0000787 case X86ISD::Wrapper:
788 if (!MatchWrapper(N, AM))
789 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000790 break;
791
Rafael Espindola094fad32009-04-08 21:14:34 +0000792 case ISD::LOAD:
793 if (!MatchLoad(N, AM))
794 return false;
795 break;
796
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000797 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000798 if (AM.BaseType == X86ISelAddressMode::RegBase
799 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000800 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
801 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
802 return false;
803 }
804 break;
Evan Chengec693f72005-12-08 02:01:35 +0000805
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000806 case ISD::SHL:
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000807 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000808 break;
809
Gabor Greif93c53e52008-08-31 15:37:04 +0000810 if (ConstantSDNode
811 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000812 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000813 if (Val == 1 || Val == 2 || Val == 3) {
814 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000815 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000816
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000817 // Okay, we know that we have a scale by now. However, if the scaled
818 // value is an add of something and a constant, we can fold the
819 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000820 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
821 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
822 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000823 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000824 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000825 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000826 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000827 AM.Disp = Disp;
828 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000829 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000830 } else {
831 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000832 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000833 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000834 }
835 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000836 }
Evan Chengec693f72005-12-08 02:01:35 +0000837
Dan Gohman83688052007-10-22 20:22:24 +0000838 case ISD::SMUL_LOHI:
839 case ISD::UMUL_LOHI:
840 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000841 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000842 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000843 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000844 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000845 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000846 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000847 AM.Base.Reg.getNode() == 0 &&
848 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000849 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000850 if (ConstantSDNode
851 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000852 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
853 CN->getZExtValue() == 9) {
854 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000855
Gabor Greifba36cb52008-08-28 21:40:38 +0000856 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000857 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000858
859 // Okay, we know that we have a scale by now. However, if the scaled
860 // value is an add of something and a constant, we can fold the
861 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000862 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
863 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
864 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000865 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000866 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000867 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000868 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000869 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000870 AM.Disp = Disp;
871 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000872 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000873 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000874 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000875 }
876
877 AM.IndexReg = AM.Base.Reg = Reg;
878 return false;
879 }
Chris Lattner62412262007-02-04 20:18:17 +0000880 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000881 break;
882
Dan Gohman3cd90a12009-05-11 18:02:53 +0000883 case ISD::SUB: {
884 // Given A-B, if A can be completely folded into the address and
885 // the index field with the index field unused, use -B as the index.
886 // This is a win if a has multiple parts that can be folded into
887 // the address. Also, this saves a mov if the base register has
888 // other uses, since it avoids a two-address sub instruction, however
889 // it costs an additional mov if the index register has other uses.
890
891 // Test if the LHS of the sub can be folded.
892 X86ISelAddressMode Backup = AM;
893 if (MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) {
894 AM = Backup;
895 break;
896 }
897 // Test if the index field is free for use.
898 if (AM.IndexReg.getNode() || AM.isRIPRel) {
899 AM = Backup;
900 break;
901 }
902 int Cost = 0;
903 SDValue RHS = N.getNode()->getOperand(1);
904 // If the RHS involves a register with multiple uses, this
905 // transformation incurs an extra mov, due to the neg instruction
906 // clobbering its operand.
907 if (!RHS.getNode()->hasOneUse() ||
908 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
909 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
910 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
911 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
912 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
913 ++Cost;
914 // If the base is a register with multiple uses, this
915 // transformation may save a mov.
916 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
917 AM.Base.Reg.getNode() &&
918 !AM.Base.Reg.getNode()->hasOneUse()) ||
919 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
920 --Cost;
921 // If the folded LHS was interesting, this transformation saves
922 // address arithmetic.
923 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
924 ((AM.Disp != 0) && (Backup.Disp == 0)) +
925 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
926 --Cost;
927 // If it doesn't look like it may be an overall win, don't do it.
928 if (Cost >= 0) {
929 AM = Backup;
930 break;
931 }
932
933 // Ok, the transformation is legal and appears profitable. Go for it.
934 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
935 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
936 AM.IndexReg = Neg;
937 AM.Scale = 1;
938
939 // Insert the new nodes into the topological ordering.
940 if (Zero.getNode()->getNodeId() == -1 ||
941 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
942 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
943 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
944 }
945 if (Neg.getNode()->getNodeId() == -1 ||
946 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
947 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
948 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
949 }
950 return false;
951 }
952
Evan Cheng8e278262009-01-17 07:09:27 +0000953 case ISD::ADD: {
954 X86ISelAddressMode Backup = AM;
Rafael Espindola523249f2009-03-31 16:16:57 +0000955 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
956 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000957 return false;
958 AM = Backup;
Rafael Espindola523249f2009-03-31 16:16:57 +0000959 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
960 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000961 return false;
962 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000963
964 // If we couldn't fold both operands into the address at the same time,
965 // see if we can just put each operand into a register and fold at least
966 // the add.
967 if (AM.BaseType == X86ISelAddressMode::RegBase &&
968 !AM.Base.Reg.getNode() &&
969 !AM.IndexReg.getNode() &&
970 !AM.isRIPRel) {
971 AM.Base.Reg = N.getNode()->getOperand(0);
972 AM.IndexReg = N.getNode()->getOperand(1);
973 AM.Scale = 1;
974 return false;
975 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000976 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000977 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000978
Chris Lattner62412262007-02-04 20:18:17 +0000979 case ISD::OR:
980 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000981 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
982 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000983 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000984 // Start with the LHS as an addr mode.
Rafael Espindola523249f2009-03-31 16:16:57 +0000985 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000986 // Address could not have picked a GV address for the displacement.
987 AM.GV == NULL &&
988 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +0000989 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000990 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000991 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000992 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000993 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000994 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000995 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000996 }
997 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000998
999 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001000 // Perform some heroic transforms on an and of a constant-count shift
1001 // with a constant to enable use of the scaled offset field.
1002
Dan Gohman475871a2008-07-27 21:46:04 +00001003 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001004 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001005
Evan Cheng1314b002007-12-13 00:43:27 +00001006 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001007 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001008
1009 // Not when RIP is used as the base.
1010 if (AM.isRIPRel) break;
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001011
1012 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001013 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1014 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1015 if (!C1 || !C2) break;
1016
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001017 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1018 // allows us to convert the shift and and into an h-register extract and
1019 // a scaled index.
1020 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1021 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001022 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001023 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1024 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1025 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1026 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1027 X, Eight);
1028 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1029 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +00001030 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1031 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1032 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001033
1034 // Insert the new nodes into the topological ordering.
1035 if (Eight.getNode()->getNodeId() == -1 ||
1036 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1037 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1038 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1039 }
1040 if (Mask.getNode()->getNodeId() == -1 ||
1041 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1042 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1043 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1044 }
1045 if (Srl.getNode()->getNodeId() == -1 ||
1046 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1047 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1048 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1049 }
1050 if (And.getNode()->getNodeId() == -1 ||
1051 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1052 CurDAG->RepositionNode(N.getNode(), And.getNode());
1053 And.getNode()->setNodeId(N.getNode()->getNodeId());
1054 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001055 if (ShlCount.getNode()->getNodeId() == -1 ||
1056 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1057 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1058 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1059 }
1060 if (Shl.getNode()->getNodeId() == -1 ||
1061 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1062 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1063 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1064 }
1065 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001066 AM.IndexReg = And;
1067 AM.Scale = (1 << ScaleLog);
1068 return false;
1069 }
1070 }
1071
1072 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1073 // allows us to fold the shift into this addressing mode.
1074 if (Shift.getOpcode() != ISD::SHL) break;
1075
Evan Cheng1314b002007-12-13 00:43:27 +00001076 // Not likely to be profitable if either the AND or SHIFT node has more
1077 // than one use (unless all uses are for address computation). Besides,
1078 // isel mechanism requires their node ids to be reused.
1079 if (!N.hasOneUse() || !Shift.hasOneUse())
1080 break;
1081
1082 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001083 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001084 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1085 break;
1086
1087 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001088 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001089 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001090 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1091 NewANDMask);
1092 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001093 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001094
1095 // Insert the new nodes into the topological ordering.
1096 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1097 CurDAG->RepositionNode(X.getNode(), C1);
1098 C1->setNodeId(X.getNode()->getNodeId());
1099 }
1100 if (NewANDMask.getNode()->getNodeId() == -1 ||
1101 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1102 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1103 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1104 }
1105 if (NewAND.getNode()->getNodeId() == -1 ||
1106 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1107 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1108 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1109 }
1110 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1111 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1112 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1113 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1114 }
1115
Dan Gohman7b8e9642008-10-13 20:52:04 +00001116 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001117
1118 AM.Scale = 1 << ShiftCst;
1119 AM.IndexReg = NewAND;
1120 return false;
1121 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001122 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001123
Rafael Espindola523249f2009-03-31 16:16:57 +00001124 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001125}
1126
1127/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1128/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001129bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001130 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001131 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001132 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001133 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001134 AM.IndexReg = N;
1135 AM.Scale = 1;
1136 return false;
1137 }
1138
1139 // Otherwise, we cannot select it.
1140 return true;
1141 }
1142
1143 // Default, generate it as a register.
1144 AM.BaseType = X86ISelAddressMode::RegBase;
1145 AM.Base.Reg = N;
1146 return false;
1147}
1148
Evan Chengec693f72005-12-08 02:01:35 +00001149/// SelectAddr - returns true if it is able pattern match an addressing mode.
1150/// It returns the operands which make up the maximal addressing mode it can
1151/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001152bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1153 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001154 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001155 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001156 bool Done = false;
1157 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1158 unsigned Opcode = N.getOpcode();
1159 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
1160 Opcode != X86ISD::Wrapper) {
1161 // If we are able to fold N into addressing mode, then we'll allow it even
1162 // if N has multiple uses. In general, addressing computation is used as
1163 // addresses by all of its uses. But watch out for CopyToReg uses, that
1164 // means the address computation is liveout. It will be computed by a LEA
1165 // so we want to avoid computing the address twice.
1166 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1167 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1168 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001169 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001170 Done = true;
1171 break;
1172 }
1173 }
1174 }
1175 }
1176
1177 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001178 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001179
Duncan Sands83ec4b62008-06-06 12:08:01 +00001180 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001181 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001182 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001183 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001184 }
Evan Cheng8700e142006-01-11 06:09:51 +00001185
Gabor Greifba36cb52008-08-28 21:40:38 +00001186 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001187 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001188
Rafael Espindola094fad32009-04-08 21:14:34 +00001189 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001190 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001191}
1192
Chris Lattner3a7cd952006-10-07 21:55:32 +00001193/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1194/// match a load whose top elements are either undef or zeros. The load flavor
1195/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001196bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1197 SDValue N, SDValue &Base,
1198 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001199 SDValue &Disp, SDValue &Segment,
1200 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001201 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001202 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001203 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001204 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001205 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001206 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001207 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001208 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001209 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001210 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001211 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001212 return true;
1213 }
1214 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001215
1216 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001217 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001218 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001219 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001220 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001221 N.getOperand(0).getNode()->hasOneUse() &&
1222 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001223 N.getOperand(0).getOperand(0).hasOneUse()) {
1224 // Okay, this is a zero extending load. Fold it.
1225 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001226 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001227 return false;
1228 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001229 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001230 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001231 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001232 return false;
1233}
1234
1235
Evan Cheng51a9ed92006-02-25 10:09:08 +00001236/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1237/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001238bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1239 SDValue &Base, SDValue &Scale,
1240 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001241 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001242
1243 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1244 // segments.
1245 SDValue Copy = AM.Segment;
1246 SDValue T = CurDAG->getRegister(0, MVT::i32);
1247 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001248 if (MatchAddress(N, AM))
1249 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001250 assert (T == AM.Segment);
1251 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001252
Duncan Sands83ec4b62008-06-06 12:08:01 +00001253 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001254 unsigned Complexity = 0;
1255 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001256 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001257 Complexity = 1;
1258 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001259 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001260 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1261 Complexity = 4;
1262
Gabor Greifba36cb52008-08-28 21:40:38 +00001263 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001264 Complexity++;
1265 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001266 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001267
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001268 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1269 // a simple shift.
1270 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001271 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001272
1273 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1274 // to a LEA. This is determined with some expermentation but is by no means
1275 // optimal (especially for code size consideration). LEA is nice because of
1276 // its three-address nature. Tweak the cost function again when we can run
1277 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001278 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001279 // For X86-64, we should always use lea to materialize RIP relative
1280 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001281 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001282 Complexity = 4;
1283 else
1284 Complexity += 2;
1285 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001286
Gabor Greifba36cb52008-08-28 21:40:38 +00001287 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001288 Complexity++;
1289
1290 if (Complexity > 2) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001291 SDValue Segment;
1292 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001293 return true;
1294 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001295 return false;
1296}
1297
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001298/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1299bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1300 SDValue &Scale, SDValue &Index,
1301 SDValue &Disp) {
1302 assert(Op.getOpcode() == X86ISD::TLSADDR);
1303 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1304 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1305
1306 X86ISelAddressMode AM;
1307 AM.GV = GA->getGlobal();
1308 AM.Disp += GA->getOffset();
1309 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
1310
1311 if (N.getValueType() == MVT::i32) {
1312 AM.Scale = 1;
1313 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1314 } else {
1315 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1316 }
1317
1318 SDValue Segment;
1319 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1320 return true;
1321}
1322
1323
Dan Gohman475871a2008-07-27 21:46:04 +00001324bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1325 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001326 SDValue &Index, SDValue &Disp,
1327 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001328 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001329 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001330 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001331 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001332 return false;
1333}
1334
Dan Gohman8b746962008-09-23 18:22:58 +00001335/// getGlobalBaseReg - Return an SDNode that returns the value of
1336/// the global base register. Output instructions required to
1337/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001338///
Evan Cheng9ade2182006-08-26 05:34:46 +00001339SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001340 MachineFunction *MF = CurBB->getParent();
Dan Gohmanc5534622009-06-03 20:20:00 +00001341 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001342 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001343}
1344
Evan Chengb245d922006-05-20 01:36:52 +00001345static SDNode *FindCallStartFromCall(SDNode *Node) {
1346 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1347 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1348 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001349 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001350}
1351
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001352SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1353 SDValue Chain = Node->getOperand(0);
1354 SDValue In1 = Node->getOperand(1);
1355 SDValue In2L = Node->getOperand(2);
1356 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001357 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1358 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001359 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001360 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001361 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001362 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1363 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001364 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001365}
Christopher Lambc59e5212007-08-10 21:48:46 +00001366
Dan Gohman475871a2008-07-27 21:46:04 +00001367SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001368 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001369 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001370 unsigned Opc, MOpc;
1371 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001372 DebugLoc dl = Node->getDebugLoc();
1373
Evan Chengf597dc72006-02-10 22:24:32 +00001374#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001375 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001376 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001377 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001378 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001379#endif
1380
Dan Gohmane8be6c62008-07-17 19:10:17 +00001381 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001382#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001383 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001384 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001385 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001386 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001387#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001388 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001389 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001390
Evan Cheng0114e942006-01-06 20:36:21 +00001391 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001392 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001393 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001394 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001395
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001396 case X86ISD::ATOMOR64_DAG:
1397 return SelectAtomic64(Node, X86::ATOMOR6432);
1398 case X86ISD::ATOMXOR64_DAG:
1399 return SelectAtomic64(Node, X86::ATOMXOR6432);
1400 case X86ISD::ATOMADD64_DAG:
1401 return SelectAtomic64(Node, X86::ATOMADD6432);
1402 case X86ISD::ATOMSUB64_DAG:
1403 return SelectAtomic64(Node, X86::ATOMSUB6432);
1404 case X86ISD::ATOMNAND64_DAG:
1405 return SelectAtomic64(Node, X86::ATOMNAND6432);
1406 case X86ISD::ATOMAND64_DAG:
1407 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001408 case X86ISD::ATOMSWAP64_DAG:
1409 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001410
Dan Gohman525178c2007-10-08 18:33:35 +00001411 case ISD::SMUL_LOHI:
1412 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001413 SDValue N0 = Node->getOperand(0);
1414 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001415
Dan Gohman525178c2007-10-08 18:33:35 +00001416 bool isSigned = Opcode == ISD::SMUL_LOHI;
1417 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001418 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001419 default: assert(0 && "Unsupported VT!");
1420 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1421 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1422 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001423 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001424 }
1425 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001426 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001427 default: assert(0 && "Unsupported VT!");
1428 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1429 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1430 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001431 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001432 }
1433
1434 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001435 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001436 default: assert(0 && "Unsupported VT!");
1437 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1438 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1439 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001440 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001441 }
1442
Rafael Espindola094fad32009-04-08 21:14:34 +00001443 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1444 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001445 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001446 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001447 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001448 if (foldedLoad)
1449 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001450 }
1451
Dale Johannesendd64c412009-02-04 00:33:20 +00001452 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001453 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001454
1455 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001456 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1457 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001458 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001459 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001460 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001461 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001462 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001463 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001464 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001465 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001466 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001467 }
1468
Dan Gohman525178c2007-10-08 18:33:35 +00001469 // Copy the low half of the result, if it is needed.
1470 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001471 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001472 LoReg, NVT, InFlag);
1473 InFlag = Result.getValue(2);
1474 ReplaceUses(N.getValue(0), Result);
1475#ifndef NDEBUG
1476 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001477 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001478 DOUT << "\n";
1479#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001480 }
Dan Gohman525178c2007-10-08 18:33:35 +00001481 // Copy the high half of the result, if it is needed.
1482 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001483 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001484 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1485 // Prevent use of AH in a REX instruction by referencing AX instead.
1486 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001487 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001488 X86::AX, MVT::i16, InFlag);
1489 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001490 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1491 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001492 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001493 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001494 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001495 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001496 MVT::i8, Result, SRIdx), 0);
1497 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001498 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001499 HiReg, NVT, InFlag);
1500 InFlag = Result.getValue(2);
1501 }
1502 ReplaceUses(N.getValue(1), Result);
1503#ifndef NDEBUG
1504 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001505 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001506 DOUT << "\n";
1507#endif
1508 }
Evan Cheng34167212006-02-09 00:37:58 +00001509
Evan Chengf597dc72006-02-10 22:24:32 +00001510#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001511 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001512#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001513
Evan Cheng64a752f2006-08-11 09:08:15 +00001514 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001515 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001516
Dan Gohman525178c2007-10-08 18:33:35 +00001517 case ISD::SDIVREM:
1518 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001519 SDValue N0 = Node->getOperand(0);
1520 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001521
1522 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001523 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001524 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001525 default: assert(0 && "Unsupported VT!");
1526 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1527 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1528 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001529 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001530 }
1531 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001532 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001533 default: assert(0 && "Unsupported VT!");
1534 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1535 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1536 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001537 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001538 }
1539
1540 unsigned LoReg, HiReg;
1541 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001542 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001543 default: assert(0 && "Unsupported VT!");
1544 case MVT::i8:
1545 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001546 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001547 SExtOpcode = X86::CBW;
1548 break;
1549 case MVT::i16:
1550 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001551 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001552 SExtOpcode = X86::CWD;
1553 break;
1554 case MVT::i32:
1555 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001556 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001557 SExtOpcode = X86::CDQ;
1558 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001559 case MVT::i64:
1560 LoReg = X86::RAX; HiReg = X86::RDX;
1561 ClrOpcode = X86::MOV64r0;
1562 SExtOpcode = X86::CQO;
1563 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001564 }
1565
Rafael Espindola094fad32009-04-08 21:14:34 +00001566 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1567 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001568 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001569
Dan Gohman475871a2008-07-27 21:46:04 +00001570 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001571 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001572 // Special case for div8, just use a move with zero extension to AX to
1573 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001574 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1575 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1576 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001577 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001578 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001579 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001580 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001581 Chain = Move.getValue(1);
1582 ReplaceUses(N0.getValue(1), Chain);
1583 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001584 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001585 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001586 Chain = CurDAG->getEntryNode();
1587 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001588 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001589 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001590 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001591 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001592 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001593 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001594 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001595 // Sign extend the low part into the high part.
1596 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001597 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001598 } else {
1599 // Zero out the high part, effectively zero extending the input.
Dale Johannesend8392542009-02-03 21:48:12 +00001600 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1601 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00001602 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001603 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001604 }
Evan Cheng948f3432006-01-06 23:19:29 +00001605 }
1606
1607 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001608 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1609 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001610 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001611 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001612 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001613 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001614 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001615 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001616 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001617 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001618 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001619 }
1620
Dan Gohmana37c9f72007-09-25 18:23:27 +00001621 // Copy the division (low) result, if it is needed.
1622 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001623 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001624 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001625 InFlag = Result.getValue(2);
1626 ReplaceUses(N.getValue(0), Result);
1627#ifndef NDEBUG
1628 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001629 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001630 DOUT << "\n";
1631#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001632 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001633 // Copy the remainder (high) result, if it is needed.
1634 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001635 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001636 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1637 // Prevent use of AH in a REX instruction by referencing AX instead.
1638 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001639 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001640 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001641 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001642 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1643 Result,
1644 CurDAG->getTargetConstant(8, MVT::i8)),
1645 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001646 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001647 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001648 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001649 MVT::i8, Result, SRIdx), 0);
1650 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001651 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001652 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001653 InFlag = Result.getValue(2);
1654 }
1655 ReplaceUses(N.getValue(1), Result);
1656#ifndef NDEBUG
1657 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001658 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001659 DOUT << "\n";
1660#endif
1661 }
Evan Chengf597dc72006-02-10 22:24:32 +00001662
1663#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001664 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001665#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001666
1667 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001668 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001669
Evan Cheng851bc042008-06-17 02:01:22 +00001670 case ISD::DECLARE: {
1671 // Handle DECLARE nodes here because the second operand may have been
1672 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001673 SDValue Chain = Node->getOperand(0);
1674 SDValue N1 = Node->getOperand(1);
1675 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001676 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001677
1678 // FIXME: We need to handle this for VLAs.
1679 if (!FINode) {
1680 ReplaceUses(N.getValue(0), Chain);
1681 return NULL;
1682 }
1683
Evan Chengfab83872008-06-18 02:48:27 +00001684 if (N2.getOpcode() == ISD::ADD &&
1685 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1686 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001687
1688 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1689 // somehow, just ignore it.
1690 if (N2.getOpcode() != X86ISD::Wrapper) {
1691 ReplaceUses(N.getValue(0), Chain);
1692 return NULL;
1693 }
Evan Chengf2accb52009-01-10 03:33:22 +00001694 GlobalAddressSDNode *GVNode =
1695 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001696 if (GVNode == 0) {
1697 ReplaceUses(N.getValue(0), Chain);
1698 return NULL;
1699 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001700 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1701 TLI.getPointerTy());
1702 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1703 TLI.getPointerTy());
1704 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001705 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001706 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001707 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001708 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001709 }
1710
Evan Cheng9ade2182006-08-26 05:34:46 +00001711 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001712
Evan Chengf597dc72006-02-10 22:24:32 +00001713#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001714 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001715 if (ResNode == NULL || ResNode == N.getNode())
1716 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001717 else
1718 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001719 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001720 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001721#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001722
1723 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001724}
1725
Chris Lattnerc0bad572006-06-08 18:03:49 +00001726bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001727SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001728 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001729 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001730 switch (ConstraintCode) {
1731 case 'o': // offsetable ??
1732 case 'v': // not offsetable ??
1733 default: return true;
1734 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001735 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001736 return true;
1737 break;
1738 }
1739
Evan Cheng04699902006-08-26 01:05:16 +00001740 OutOps.push_back(Op0);
1741 OutOps.push_back(Op1);
1742 OutOps.push_back(Op2);
1743 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001744 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001745 return false;
1746}
1747
Chris Lattnerc961eea2005-11-16 01:54:32 +00001748/// createX86ISelDag - This pass converts a legalized DAG into a
1749/// X86-specific DAG, ready for instruction scheduling.
1750///
Bill Wendling98a366d2009-04-29 23:29:43 +00001751FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1752 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001753 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001754}