blob: b003efddd49906fb03585a735f67784e0140cd70 [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);
170 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
171 SDValue N, SDValue &Base, SDValue &Scale,
172 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000173 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000174 SDValue &InChain, SDValue &OutChain);
175 bool TryFoldLoad(SDValue P, SDValue N,
176 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000177 SDValue &Index, SDValue &Disp,
178 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000179 void PreprocessForRMW();
180 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000181
Chris Lattnerc0bad572006-06-08 18:03:49 +0000182 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
183 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000184 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000185 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000186 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000187
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000188 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
189
Dan Gohman475871a2008-07-27 21:46:04 +0000190 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
191 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000192 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000193 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000194 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
195 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000196 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000197 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000198 // These are 32-bit even in 64-bit mode since RIP relative offset
199 // is 32-bit.
200 if (AM.GV)
201 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
202 else if (AM.CP)
Gabor Greif93c53e52008-08-31 15:37:04 +0000203 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
204 AM.Align, AM.Disp);
Evan Cheng25ab6902006-09-08 06:48:29 +0000205 else if (AM.ES)
Bill Wendling056292f2008-09-16 21:48:12 +0000206 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
Evan Cheng25ab6902006-09-08 06:48:29 +0000207 else if (AM.JT != -1)
208 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
209 else
Dan Gohman27cae7b2008-11-11 15:52:29 +0000210 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000211
212 if (AM.Segment.getNode())
213 Segment = AM.Segment;
214 else
215 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000216 }
217
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000218 /// getI8Imm - Return a target constant with the specified value, of type
219 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000220 inline SDValue getI8Imm(unsigned Imm) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000221 return CurDAG->getTargetConstant(Imm, MVT::i8);
222 }
223
Chris Lattnerc961eea2005-11-16 01:54:32 +0000224 /// getI16Imm - Return a target constant with the specified value, of type
225 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000226 inline SDValue getI16Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000227 return CurDAG->getTargetConstant(Imm, MVT::i16);
228 }
229
230 /// getI32Imm - Return a target constant with the specified value, of type
231 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000232 inline SDValue getI32Imm(unsigned Imm) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000233 return CurDAG->getTargetConstant(Imm, MVT::i32);
234 }
Evan Chengf597dc72006-02-10 22:24:32 +0000235
Dan Gohman8b746962008-09-23 18:22:58 +0000236 /// getGlobalBaseReg - Return an SDNode that returns the value of
237 /// the global base register. Output instructions required to
238 /// initialize the global base register, if necessary.
239 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000240 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000241
Dan Gohmanc5534622009-06-03 20:20:00 +0000242 /// getTargetMachine - Return a reference to the TargetMachine, casted
243 /// to the target-specific type.
244 const X86TargetMachine &getTargetMachine() {
245 return static_cast<const X86TargetMachine &>(TM);
246 }
247
248 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
249 /// to the target-specific type.
250 const X86InstrInfo *getInstrInfo() {
251 return getTargetMachine().getInstrInfo();
252 }
253
Evan Cheng23addc02006-02-10 22:46:26 +0000254#ifndef NDEBUG
255 unsigned Indent;
256#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000257 };
258}
259
Evan Chengf4b4c412006-08-08 00:31:00 +0000260
Evan Cheng884c70c2008-11-27 00:49:46 +0000261bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
262 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000263 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000264
Evan Cheng884c70c2008-11-27 00:49:46 +0000265 if (U == Root)
266 switch (U->getOpcode()) {
267 default: break;
268 case ISD::ADD:
269 case ISD::ADDC:
270 case ISD::ADDE:
271 case ISD::AND:
272 case ISD::OR:
273 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000274 SDValue Op1 = U->getOperand(1);
275
Evan Cheng884c70c2008-11-27 00:49:46 +0000276 // If the other operand is a 8-bit immediate we should fold the immediate
277 // instead. This reduces code size.
278 // e.g.
279 // movl 4(%esp), %eax
280 // addl $4, %eax
281 // vs.
282 // movl $4, %eax
283 // addl 4(%esp), %eax
284 // The former is 2 bytes shorter. In case where the increment is 1, then
285 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000286 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000287 if (Imm->getAPIntValue().isSignedIntN(8))
288 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000289
290 // If the other operand is a TLS address, we should fold it instead.
291 // This produces
292 // movl %gs:0, %eax
293 // leal i@NTPOFF(%eax), %eax
294 // instead of
295 // movl $i@NTPOFF, %eax
296 // addl %gs:0, %eax
297 // if the block also has an access to a second TLS address this will save
298 // a load.
299 // FIXME: This is probably also true for non TLS addresses.
300 if (Op1.getOpcode() == X86ISD::Wrapper) {
301 SDValue Val = Op1.getOperand(0);
302 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
303 return false;
304 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000305 }
306 }
307
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000308 // Proceed to 'generic' cycle finder code
309 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000310}
311
Evan Cheng70e674e2006-08-28 20:10:17 +0000312/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
313/// and move load below the TokenFactor. Replace store's chain operand with
314/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000315static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000316 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000317 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000318 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
319 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000320 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000321 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000322 Ops.push_back(TF.getOperand(i));
Dan Gohmanf350b272008-08-23 02:25:05 +0000323 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
324 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
325 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
326 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000327}
328
Evan Chengcd0baf22008-05-23 21:23:16 +0000329/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
330///
Dan Gohman475871a2008-07-27 21:46:04 +0000331static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
332 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000333 if (N.getOpcode() == ISD::BIT_CONVERT)
334 N = N.getOperand(0);
335
336 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
337 if (!LD || LD->isVolatile())
338 return false;
339 if (LD->getAddressingMode() != ISD::UNINDEXED)
340 return false;
341
342 ISD::LoadExtType ExtType = LD->getExtensionType();
343 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
344 return false;
345
346 if (N.hasOneUse() &&
347 N.getOperand(1) == Address &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000348 N.getNode()->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000349 Load = N;
350 return true;
351 }
352 return false;
353}
354
Evan Chengab6c3bb2008-08-25 21:27:18 +0000355/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
356/// operand and move load below the call's chain operand.
357static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000358 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000359 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000360 SDValue Chain = CallSeqStart.getOperand(0);
361 if (Chain.getNode() == Load.getNode())
362 Ops.push_back(Load.getOperand(0));
363 else {
364 assert(Chain.getOpcode() == ISD::TokenFactor &&
365 "Unexpected CallSeqStart chain operand");
366 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
367 if (Chain.getOperand(i).getNode() == Load.getNode())
368 Ops.push_back(Load.getOperand(0));
369 else
370 Ops.push_back(Chain.getOperand(i));
371 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000372 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
373 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000374 Ops.clear();
375 Ops.push_back(NewChain);
376 }
377 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
378 Ops.push_back(CallSeqStart.getOperand(i));
379 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000380 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
381 Load.getOperand(1), Load.getOperand(2));
382 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000383 Ops.push_back(SDValue(Load.getNode(), 1));
384 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000385 Ops.push_back(Call.getOperand(i));
386 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
387}
388
389/// isCalleeLoad - Return true if call address is a load and it can be
390/// moved below CALLSEQ_START and the chains leading up to the call.
391/// Return the CALLSEQ_START by reference as a second output.
392static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000393 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000394 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000395 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000396 if (!LD ||
397 LD->isVolatile() ||
398 LD->getAddressingMode() != ISD::UNINDEXED ||
399 LD->getExtensionType() != ISD::NON_EXTLOAD)
400 return false;
401
402 // Now let's find the callseq_start.
403 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
404 if (!Chain.hasOneUse())
405 return false;
406 Chain = Chain.getOperand(0);
407 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000408
409 if (Chain.getOperand(0).getNode() == Callee.getNode())
410 return true;
411 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
412 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()))
413 return true;
414 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000415}
416
417
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000418/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000419/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000420/// This allows the instruction selector to pick more read-modify-write
421/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000422///
423/// [Load chain]
424/// ^
425/// |
426/// [Load]
427/// ^ ^
428/// | |
429/// / \-
430/// / |
431/// [TokenFactor] [Op]
432/// ^ ^
433/// | |
434/// \ /
435/// \ /
436/// [Store]
437///
438/// The fact the store's chain operand != load's chain will prevent the
439/// (store (op (load))) instruction from being selected. We can transform it to:
440///
441/// [Load chain]
442/// ^
443/// |
444/// [TokenFactor]
445/// ^
446/// |
447/// [Load]
448/// ^ ^
449/// | |
450/// | \-
451/// | |
452/// | [Op]
453/// | ^
454/// | |
455/// \ /
456/// \ /
457/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000458void X86DAGToDAGISel::PreprocessForRMW() {
459 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
460 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000461 if (I->getOpcode() == X86ISD::CALL) {
462 /// Also try moving call address load from outside callseq_start to just
463 /// before the call to allow it to be folded.
464 ///
465 /// [Load chain]
466 /// ^
467 /// |
468 /// [Load]
469 /// ^ ^
470 /// | |
471 /// / \--
472 /// / |
473 ///[CALLSEQ_START] |
474 /// ^ |
475 /// | |
476 /// [LOAD/C2Reg] |
477 /// | |
478 /// \ /
479 /// \ /
480 /// [CALL]
481 SDValue Chain = I->getOperand(0);
482 SDValue Load = I->getOperand(1);
483 if (!isCalleeLoad(Load, Chain))
484 continue;
485 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
486 ++NumLoadMoved;
487 continue;
488 }
489
Evan Cheng8b2794a2006-10-13 21:14:26 +0000490 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000491 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000492 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000493
Gabor Greifba36cb52008-08-28 21:40:38 +0000494 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000495 continue;
496
Dan Gohman475871a2008-07-27 21:46:04 +0000497 SDValue N1 = I->getOperand(1);
498 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000499 if ((N1.getValueType().isFloatingPoint() &&
500 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000501 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000502 continue;
503
504 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000505 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000506 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000507 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000508 case ISD::ADD:
509 case ISD::MUL:
510 case ISD::AND:
511 case ISD::OR:
512 case ISD::XOR:
513 case ISD::ADDC:
514 case ISD::ADDE:
515 case ISD::VECTOR_SHUFFLE: {
516 SDValue N10 = N1.getOperand(0);
517 SDValue N11 = N1.getOperand(1);
518 RModW = isRMWLoad(N10, Chain, N2, Load);
519 if (!RModW)
520 RModW = isRMWLoad(N11, Chain, N2, Load);
521 break;
522 }
523 case ISD::SUB:
524 case ISD::SHL:
525 case ISD::SRA:
526 case ISD::SRL:
527 case ISD::ROTL:
528 case ISD::ROTR:
529 case ISD::SUBC:
530 case ISD::SUBE:
531 case X86ISD::SHLD:
532 case X86ISD::SHRD: {
533 SDValue N10 = N1.getOperand(0);
534 RModW = isRMWLoad(N10, Chain, N2, Load);
535 break;
536 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000537 }
538
Evan Cheng82a35b32006-08-29 06:44:17 +0000539 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000540 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000541 ++NumLoadMoved;
542 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000543 }
544}
545
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000546
547/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
548/// nodes that target the FP stack to be store and load to the stack. This is a
549/// gross hack. We would like to simply mark these as being illegal, but when
550/// we do that, legalize produces these when it expands calls, then expands
551/// these in the same legalize pass. We would like dag combine to be able to
552/// hack on these between the call expansion and the node legalization. As such
553/// this pass basically does "really late" legalization of these inline with the
554/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000555void X86DAGToDAGISel::PreprocessForFPConvert() {
556 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
557 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000558 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
559 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
560 continue;
561
562 // If the source and destination are SSE registers, then this is a legal
563 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000564 MVT SrcVT = N->getOperand(0).getValueType();
565 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000566 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
567 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
568 if (SrcIsSSE && DstIsSSE)
569 continue;
570
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000571 if (!SrcIsSSE && !DstIsSSE) {
572 // If this is an FPStack extension, it is a noop.
573 if (N->getOpcode() == ISD::FP_EXTEND)
574 continue;
575 // If this is a value-preserving FPStack truncation, it is a noop.
576 if (N->getConstantOperandVal(1))
577 continue;
578 }
579
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000580 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
581 // FPStack has extload and truncstore. SSE can fold direct loads into other
582 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000583 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000584 if (N->getOpcode() == ISD::FP_ROUND)
585 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
586 else
587 MemVT = SrcIsSSE ? SrcVT : DstVT;
588
Dan Gohmanf350b272008-08-23 02:25:05 +0000589 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000590 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000591
592 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000593 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000594 N->getOperand(0),
595 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000596 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000597 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000598
599 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
600 // extload we created. This will cause general havok on the dag because
601 // anything below the conversion could be folded into other existing nodes.
602 // To avoid invalidating 'I', back it up to the convert node.
603 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000604 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000605
606 // Now that we did that, the node is dead. Increment the iterator to the
607 // next node to process, then delete N.
608 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000609 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000610 }
611}
612
Chris Lattnerc961eea2005-11-16 01:54:32 +0000613/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
614/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000615void X86DAGToDAGISel::InstructionSelect() {
Evan Chengdb8d56b2008-06-30 20:45:06 +0000616 CurBB = BB; // BB can change as result of isel.
Devang Patele76225a2008-10-06 18:03:39 +0000617 const Function *F = CurDAG->getMachineFunction().getFunction();
618 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000619
Evan Chengdb8d56b2008-06-30 20:45:06 +0000620 DEBUG(BB->dump());
Bill Wendling98a366d2009-04-29 23:29:43 +0000621 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000622 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000623
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000624 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000625 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000626
Chris Lattnerc961eea2005-11-16 01:54:32 +0000627 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000628#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000629 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000630 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000631#endif
David Greene8ad4c002008-10-27 21:56:29 +0000632 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000633#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000634 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000635#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000636
Dan Gohmanf350b272008-08-23 02:25:05 +0000637 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000638}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000639
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000640/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
641/// the main function.
642void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
643 MachineFrameInfo *MFI) {
644 const TargetInstrInfo *TII = TM.getInstrInfo();
645 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000646 BuildMI(BB, DebugLoc::getUnknownLoc(),
647 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000648}
649
650void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
651 // If this is main, emit special code for main.
652 MachineBasicBlock *BB = MF.begin();
653 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
654 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
655}
656
Rafael Espindola094fad32009-04-08 21:14:34 +0000657
658bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
659 X86ISelAddressMode &AM) {
660 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
661 SDValue Segment = N.getOperand(0);
662
663 if (AM.Segment.getNode() == 0) {
664 AM.Segment = Segment;
665 return false;
666 }
667
668 return true;
669}
670
671bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
672 // This optimization is valid because the GNU TLS model defines that
673 // gs:0 (or fs:0 on X86-64) contains its own address.
674 // For more information see http://people.redhat.com/drepper/tls.pdf
675
676 SDValue Address = N.getOperand(1);
677 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
678 !MatchSegmentBaseAddress (Address, AM))
679 return false;
680
681 return true;
682}
683
Rafael Espindola49a168d2009-04-12 21:55:03 +0000684bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Dan Gohmanc5534622009-06-03 20:20:00 +0000685 bool SymbolicAddressesAreRIPRel =
686 getTargetMachine().symbolicAddressesAreRIPRel();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000687 bool is64Bit = Subtarget->is64Bit();
688 DOUT << "Wrapper: 64bit " << is64Bit;
689 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n";
Rafael Espindolab2157762009-04-12 23:00:38 +0000690
Rafael Espindola49a168d2009-04-12 21:55:03 +0000691 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Rafael Espindolab2157762009-04-12 23:00:38 +0000692 if (is64Bit && (TM.getCodeModel() != CodeModel::Small))
Rafael Espindola49a168d2009-04-12 21:55:03 +0000693 return true;
Rafael Espindolab2157762009-04-12 23:00:38 +0000694
695 // Base and index reg must be 0 in order to use rip as base.
696 bool canUsePICRel = !AM.Base.Reg.getNode() && !AM.IndexReg.getNode();
Dan Gohmanc5534622009-06-03 20:20:00 +0000697 if (is64Bit && !canUsePICRel && SymbolicAddressesAreRIPRel)
Rafael Espindolab2157762009-04-12 23:00:38 +0000698 return true;
699
Rafael Espindola49a168d2009-04-12 21:55:03 +0000700 if (AM.hasSymbolicDisplacement())
701 return true;
702 // If value is available in a register both base and index components have
703 // been picked, we can't fit the result available in the register in the
704 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
705
706 SDValue N0 = N.getOperand(0);
707 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
708 uint64_t Offset = G->getOffset();
709 if (!is64Bit || isInt32(AM.Disp + Offset)) {
710 GlobalValue *GV = G->getGlobal();
Dan Gohmanc5534622009-06-03 20:20:00 +0000711 bool isRIPRel = SymbolicAddressesAreRIPRel;
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000712 if (N0.getOpcode() == llvm::ISD::TargetGlobalTLSAddress) {
713 TLSModel::Model model =
714 getTLSModel (GV, TM.getRelocationModel());
715 if (is64Bit && model == TLSModel::InitialExec)
716 isRIPRel = true;
717 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000718 AM.GV = GV;
719 AM.Disp += Offset;
Rafael Espindola7ff5bff2009-04-13 13:02:49 +0000720 AM.isRIPRel = isRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000721 return false;
722 }
723 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
724 uint64_t Offset = CP->getOffset();
725 if (!is64Bit || isInt32(AM.Disp + Offset)) {
726 AM.CP = CP->getConstVal();
727 AM.Align = CP->getAlignment();
728 AM.Disp += Offset;
Dan Gohmanc5534622009-06-03 20:20:00 +0000729 AM.isRIPRel = SymbolicAddressesAreRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000730 return false;
731 }
732 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
733 AM.ES = S->getSymbol();
Dan Gohmanc5534622009-06-03 20:20:00 +0000734 AM.isRIPRel = SymbolicAddressesAreRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000735 return false;
736 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
737 AM.JT = J->getIndex();
Dan Gohmanc5534622009-06-03 20:20:00 +0000738 AM.isRIPRel = SymbolicAddressesAreRIPRel;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000739 return false;
740 }
741
742 return true;
743}
744
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000745/// MatchAddress - Add the specified node to the specified addressing mode,
746/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000747/// addressing mode.
Dan Gohman475871a2008-07-27 21:46:04 +0000748bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM,
Rafael Espindola523249f2009-03-31 16:16:57 +0000749 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000750 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000751 DebugLoc dl = N.getDebugLoc();
Evan Chengda43bcf2008-09-24 00:05:32 +0000752 DOUT << "MatchAddress: "; DEBUG(AM.dump());
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000753 // Limit recursion.
754 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000755 return MatchAddressBase(N, AM);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000756
Evan Cheng25ab6902006-09-08 06:48:29 +0000757 // RIP relative addressing: %rip + 32-bit displacement!
758 if (AM.isRIPRel) {
759 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000760 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000761 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000762 AM.Disp += Val;
763 return false;
764 }
765 }
766 return true;
767 }
768
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000769 switch (N.getOpcode()) {
770 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000771 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000772 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000773 if (!is64Bit || isInt32(AM.Disp + Val)) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000774 AM.Disp += Val;
775 return false;
776 }
777 break;
778 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000779
Rafael Espindola094fad32009-04-08 21:14:34 +0000780 case X86ISD::SegmentBaseAddress:
781 if (!MatchSegmentBaseAddress(N, AM))
782 return false;
783 break;
784
Rafael Espindola49a168d2009-04-12 21:55:03 +0000785 case X86ISD::Wrapper:
786 if (!MatchWrapper(N, AM))
787 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000788 break;
789
Rafael Espindola094fad32009-04-08 21:14:34 +0000790 case ISD::LOAD:
791 if (!MatchLoad(N, AM))
792 return false;
793 break;
794
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000795 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000796 if (AM.BaseType == X86ISelAddressMode::RegBase
797 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000798 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
799 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
800 return false;
801 }
802 break;
Evan Chengec693f72005-12-08 02:01:35 +0000803
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000804 case ISD::SHL:
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000805 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000806 break;
807
Gabor Greif93c53e52008-08-31 15:37:04 +0000808 if (ConstantSDNode
809 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000810 unsigned Val = CN->getZExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000811 if (Val == 1 || Val == 2 || Val == 3) {
812 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000813 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000814
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000815 // Okay, we know that we have a scale by now. However, if the scaled
816 // value is an add of something and a constant, we can fold the
817 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000818 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
819 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
820 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000821 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000822 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000823 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Dan Gohman6520e202008-10-18 02:06:02 +0000824 if (!is64Bit || isInt32(Disp))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000825 AM.Disp = Disp;
826 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000827 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000828 } else {
829 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000830 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000831 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000832 }
833 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000834 }
Evan Chengec693f72005-12-08 02:01:35 +0000835
Dan Gohman83688052007-10-22 20:22:24 +0000836 case ISD::SMUL_LOHI:
837 case ISD::UMUL_LOHI:
838 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000839 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000840 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000841 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000842 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000843 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000844 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000845 AM.Base.Reg.getNode() == 0 &&
846 AM.IndexReg.getNode() == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000847 !AM.isRIPRel) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000848 if (ConstantSDNode
849 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000850 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
851 CN->getZExtValue() == 9) {
852 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000853
Gabor Greifba36cb52008-08-28 21:40:38 +0000854 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000855 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000856
857 // Okay, we know that we have a scale by now. However, if the scaled
858 // value is an add of something and a constant, we can fold the
859 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000860 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
861 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
862 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000863 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000864 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000865 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000866 CN->getZExtValue();
Dan Gohman6520e202008-10-18 02:06:02 +0000867 if (!is64Bit || isInt32(Disp))
Evan Cheng25ab6902006-09-08 06:48:29 +0000868 AM.Disp = Disp;
869 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000870 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000871 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000872 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000873 }
874
875 AM.IndexReg = AM.Base.Reg = Reg;
876 return false;
877 }
Chris Lattner62412262007-02-04 20:18:17 +0000878 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000879 break;
880
Dan Gohman3cd90a12009-05-11 18:02:53 +0000881 case ISD::SUB: {
882 // Given A-B, if A can be completely folded into the address and
883 // the index field with the index field unused, use -B as the index.
884 // This is a win if a has multiple parts that can be folded into
885 // the address. Also, this saves a mov if the base register has
886 // other uses, since it avoids a two-address sub instruction, however
887 // it costs an additional mov if the index register has other uses.
888
889 // Test if the LHS of the sub can be folded.
890 X86ISelAddressMode Backup = AM;
891 if (MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) {
892 AM = Backup;
893 break;
894 }
895 // Test if the index field is free for use.
896 if (AM.IndexReg.getNode() || AM.isRIPRel) {
897 AM = Backup;
898 break;
899 }
900 int Cost = 0;
901 SDValue RHS = N.getNode()->getOperand(1);
902 // If the RHS involves a register with multiple uses, this
903 // transformation incurs an extra mov, due to the neg instruction
904 // clobbering its operand.
905 if (!RHS.getNode()->hasOneUse() ||
906 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
907 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
908 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
909 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
910 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
911 ++Cost;
912 // If the base is a register with multiple uses, this
913 // transformation may save a mov.
914 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
915 AM.Base.Reg.getNode() &&
916 !AM.Base.Reg.getNode()->hasOneUse()) ||
917 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
918 --Cost;
919 // If the folded LHS was interesting, this transformation saves
920 // address arithmetic.
921 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
922 ((AM.Disp != 0) && (Backup.Disp == 0)) +
923 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
924 --Cost;
925 // If it doesn't look like it may be an overall win, don't do it.
926 if (Cost >= 0) {
927 AM = Backup;
928 break;
929 }
930
931 // Ok, the transformation is legal and appears profitable. Go for it.
932 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
933 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
934 AM.IndexReg = Neg;
935 AM.Scale = 1;
936
937 // Insert the new nodes into the topological ordering.
938 if (Zero.getNode()->getNodeId() == -1 ||
939 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
940 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
941 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
942 }
943 if (Neg.getNode()->getNodeId() == -1 ||
944 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
945 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
946 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
947 }
948 return false;
949 }
950
Evan Cheng8e278262009-01-17 07:09:27 +0000951 case ISD::ADD: {
952 X86ISelAddressMode Backup = AM;
Rafael Espindola523249f2009-03-31 16:16:57 +0000953 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
954 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000955 return false;
956 AM = Backup;
Rafael Espindola523249f2009-03-31 16:16:57 +0000957 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
958 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +0000959 return false;
960 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +0000961
962 // If we couldn't fold both operands into the address at the same time,
963 // see if we can just put each operand into a register and fold at least
964 // the add.
965 if (AM.BaseType == X86ISelAddressMode::RegBase &&
966 !AM.Base.Reg.getNode() &&
967 !AM.IndexReg.getNode() &&
968 !AM.isRIPRel) {
969 AM.Base.Reg = N.getNode()->getOperand(0);
970 AM.IndexReg = N.getNode()->getOperand(1);
971 AM.Scale = 1;
972 return false;
973 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000974 break;
Evan Cheng8e278262009-01-17 07:09:27 +0000975 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000976
Chris Lattner62412262007-02-04 20:18:17 +0000977 case ISD::OR:
978 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000979 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
980 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +0000981 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000982 // Start with the LHS as an addr mode.
Rafael Espindola523249f2009-03-31 16:16:57 +0000983 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000984 // Address could not have picked a GV address for the displacement.
985 AM.GV == NULL &&
986 // On x86-64, the resultant disp must fit in 32-bits.
Dan Gohman27cae7b2008-11-11 15:52:29 +0000987 (!is64Bit || isInt32(AM.Disp + Offset)) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000988 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000989 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000990 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000991 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000992 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000993 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000994 }
995 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000996
997 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +0000998 // Perform some heroic transforms on an and of a constant-count shift
999 // with a constant to enable use of the scaled offset field.
1000
Dan Gohman475871a2008-07-27 21:46:04 +00001001 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001002 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001003
Evan Cheng1314b002007-12-13 00:43:27 +00001004 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001005 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001006
1007 // Not when RIP is used as the base.
1008 if (AM.isRIPRel) break;
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001009
1010 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001011 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1012 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1013 if (!C1 || !C2) break;
1014
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001015 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1016 // allows us to convert the shift and and into an h-register extract and
1017 // a scaled index.
1018 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1019 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001020 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001021 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
1022 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
1023 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1024 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1025 X, Eight);
1026 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1027 Srl, Mask);
Dan Gohman62ad1382009-04-14 22:45:05 +00001028 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
1029 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1030 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001031
1032 // Insert the new nodes into the topological ordering.
1033 if (Eight.getNode()->getNodeId() == -1 ||
1034 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1035 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1036 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1037 }
1038 if (Mask.getNode()->getNodeId() == -1 ||
1039 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1040 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1041 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1042 }
1043 if (Srl.getNode()->getNodeId() == -1 ||
1044 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1045 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1046 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1047 }
1048 if (And.getNode()->getNodeId() == -1 ||
1049 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1050 CurDAG->RepositionNode(N.getNode(), And.getNode());
1051 And.getNode()->setNodeId(N.getNode()->getNodeId());
1052 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001053 if (ShlCount.getNode()->getNodeId() == -1 ||
1054 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1055 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1056 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1057 }
1058 if (Shl.getNode()->getNodeId() == -1 ||
1059 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1060 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1061 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1062 }
1063 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001064 AM.IndexReg = And;
1065 AM.Scale = (1 << ScaleLog);
1066 return false;
1067 }
1068 }
1069
1070 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1071 // allows us to fold the shift into this addressing mode.
1072 if (Shift.getOpcode() != ISD::SHL) break;
1073
Evan Cheng1314b002007-12-13 00:43:27 +00001074 // Not likely to be profitable if either the AND or SHIFT node has more
1075 // than one use (unless all uses are for address computation). Besides,
1076 // isel mechanism requires their node ids to be reused.
1077 if (!N.hasOneUse() || !Shift.hasOneUse())
1078 break;
1079
1080 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001081 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001082 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1083 break;
1084
1085 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001086 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001087 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001088 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1089 NewANDMask);
1090 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001091 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001092
1093 // Insert the new nodes into the topological ordering.
1094 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1095 CurDAG->RepositionNode(X.getNode(), C1);
1096 C1->setNodeId(X.getNode()->getNodeId());
1097 }
1098 if (NewANDMask.getNode()->getNodeId() == -1 ||
1099 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1100 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1101 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1102 }
1103 if (NewAND.getNode()->getNodeId() == -1 ||
1104 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1105 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1106 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1107 }
1108 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1109 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1110 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1111 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1112 }
1113
Dan Gohman7b8e9642008-10-13 20:52:04 +00001114 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001115
1116 AM.Scale = 1 << ShiftCst;
1117 AM.IndexReg = NewAND;
1118 return false;
1119 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001120 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001121
Rafael Espindola523249f2009-03-31 16:16:57 +00001122 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001123}
1124
1125/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1126/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001127bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001128 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001129 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001130 // If so, check to see if the scale index register is set.
Gabor Greifba36cb52008-08-28 21:40:38 +00001131 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001132 AM.IndexReg = N;
1133 AM.Scale = 1;
1134 return false;
1135 }
1136
1137 // Otherwise, we cannot select it.
1138 return true;
1139 }
1140
1141 // Default, generate it as a register.
1142 AM.BaseType = X86ISelAddressMode::RegBase;
1143 AM.Base.Reg = N;
1144 return false;
1145}
1146
Evan Chengec693f72005-12-08 02:01:35 +00001147/// SelectAddr - returns true if it is able pattern match an addressing mode.
1148/// It returns the operands which make up the maximal addressing mode it can
1149/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001150bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1151 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001152 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001153 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001154 bool Done = false;
1155 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1156 unsigned Opcode = N.getOpcode();
1157 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
1158 Opcode != X86ISD::Wrapper) {
1159 // If we are able to fold N into addressing mode, then we'll allow it even
1160 // if N has multiple uses. In general, addressing computation is used as
1161 // addresses by all of its uses. But watch out for CopyToReg uses, that
1162 // means the address computation is liveout. It will be computed by a LEA
1163 // so we want to avoid computing the address twice.
1164 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1165 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1166 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001167 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001168 Done = true;
1169 break;
1170 }
1171 }
1172 }
1173 }
1174
1175 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001176 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001177
Duncan Sands83ec4b62008-06-06 12:08:01 +00001178 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001179 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001180 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001181 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001182 }
Evan Cheng8700e142006-01-11 06:09:51 +00001183
Gabor Greifba36cb52008-08-28 21:40:38 +00001184 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001185 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001186
Rafael Espindola094fad32009-04-08 21:14:34 +00001187 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001188 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001189}
1190
Chris Lattner3a7cd952006-10-07 21:55:32 +00001191/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1192/// match a load whose top elements are either undef or zeros. The load flavor
1193/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001194bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1195 SDValue N, SDValue &Base,
1196 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001197 SDValue &Disp, SDValue &Segment,
1198 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001199 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001200 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001201 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001202 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001203 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001204 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001205 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001206 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001207 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001208 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001209 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001210 return true;
1211 }
1212 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001213
1214 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001215 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001216 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001217 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001218 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001219 N.getOperand(0).getNode()->hasOneUse() &&
1220 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001221 N.getOperand(0).getOperand(0).hasOneUse()) {
1222 // Okay, this is a zero extending load. Fold it.
1223 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001224 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001225 return false;
1226 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001227 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001228 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001229 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001230 return false;
1231}
1232
1233
Evan Cheng51a9ed92006-02-25 10:09:08 +00001234/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1235/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001236bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1237 SDValue &Base, SDValue &Scale,
1238 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001239 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001240
1241 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1242 // segments.
1243 SDValue Copy = AM.Segment;
1244 SDValue T = CurDAG->getRegister(0, MVT::i32);
1245 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001246 if (MatchAddress(N, AM))
1247 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001248 assert (T == AM.Segment);
1249 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001250
Duncan Sands83ec4b62008-06-06 12:08:01 +00001251 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001252 unsigned Complexity = 0;
1253 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001254 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001255 Complexity = 1;
1256 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001257 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001258 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1259 Complexity = 4;
1260
Gabor Greifba36cb52008-08-28 21:40:38 +00001261 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001262 Complexity++;
1263 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001264 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001265
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001266 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1267 // a simple shift.
1268 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001269 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001270
1271 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1272 // to a LEA. This is determined with some expermentation but is by no means
1273 // optimal (especially for code size consideration). LEA is nice because of
1274 // its three-address nature. Tweak the cost function again when we can run
1275 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001276 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001277 // For X86-64, we should always use lea to materialize RIP relative
1278 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001279 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001280 Complexity = 4;
1281 else
1282 Complexity += 2;
1283 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001284
Gabor Greifba36cb52008-08-28 21:40:38 +00001285 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001286 Complexity++;
1287
1288 if (Complexity > 2) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001289 SDValue Segment;
1290 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001291 return true;
1292 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001293 return false;
1294}
1295
Dan Gohman475871a2008-07-27 21:46:04 +00001296bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1297 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001298 SDValue &Index, SDValue &Disp,
1299 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001300 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001301 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001302 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001303 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001304 return false;
1305}
1306
Dan Gohman8b746962008-09-23 18:22:58 +00001307/// getGlobalBaseReg - Return an SDNode that returns the value of
1308/// the global base register. Output instructions required to
1309/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001310///
Evan Cheng9ade2182006-08-26 05:34:46 +00001311SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman57c3dac2008-09-30 00:58:23 +00001312 MachineFunction *MF = CurBB->getParent();
Dan Gohmanc5534622009-06-03 20:20:00 +00001313 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001314 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001315}
1316
Evan Chengb245d922006-05-20 01:36:52 +00001317static SDNode *FindCallStartFromCall(SDNode *Node) {
1318 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1319 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1320 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001321 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001322}
1323
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001324SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1325 SDValue Chain = Node->getOperand(0);
1326 SDValue In1 = Node->getOperand(1);
1327 SDValue In2L = Node->getOperand(2);
1328 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001329 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1330 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001331 return NULL;
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00001332 SDValue LSI = Node->getOperand(4); // MemOperand
Rafael Espindola094fad32009-04-08 21:14:34 +00001333 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, LSI, Chain};
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001334 return CurDAG->getTargetNode(Opc, Node->getDebugLoc(),
1335 MVT::i32, MVT::i32, MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001336 array_lengthof(Ops));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001337}
Christopher Lambc59e5212007-08-10 21:48:46 +00001338
Dan Gohman475871a2008-07-27 21:46:04 +00001339SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001340 SDNode *Node = N.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001341 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001342 unsigned Opc, MOpc;
1343 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001344 DebugLoc dl = Node->getDebugLoc();
1345
Evan Chengf597dc72006-02-10 22:24:32 +00001346#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001347 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001348 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001349 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001350 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001351#endif
1352
Dan Gohmane8be6c62008-07-17 19:10:17 +00001353 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001354#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001355 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001356 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001357 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001358 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001359#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001360 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001361 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001362
Evan Cheng0114e942006-01-06 20:36:21 +00001363 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001364 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001365 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001366 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001367
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001368 case X86ISD::ATOMOR64_DAG:
1369 return SelectAtomic64(Node, X86::ATOMOR6432);
1370 case X86ISD::ATOMXOR64_DAG:
1371 return SelectAtomic64(Node, X86::ATOMXOR6432);
1372 case X86ISD::ATOMADD64_DAG:
1373 return SelectAtomic64(Node, X86::ATOMADD6432);
1374 case X86ISD::ATOMSUB64_DAG:
1375 return SelectAtomic64(Node, X86::ATOMSUB6432);
1376 case X86ISD::ATOMNAND64_DAG:
1377 return SelectAtomic64(Node, X86::ATOMNAND6432);
1378 case X86ISD::ATOMAND64_DAG:
1379 return SelectAtomic64(Node, X86::ATOMAND6432);
Dale Johannesen880ae362008-10-03 22:25:52 +00001380 case X86ISD::ATOMSWAP64_DAG:
1381 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001382
Dan Gohman525178c2007-10-08 18:33:35 +00001383 case ISD::SMUL_LOHI:
1384 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +00001385 SDValue N0 = Node->getOperand(0);
1386 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001387
Dan Gohman525178c2007-10-08 18:33:35 +00001388 bool isSigned = Opcode == ISD::SMUL_LOHI;
1389 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001390 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001391 default: assert(0 && "Unsupported VT!");
1392 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1393 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1394 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001395 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001396 }
1397 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001398 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001399 default: assert(0 && "Unsupported VT!");
1400 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1401 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1402 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001403 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001404 }
1405
1406 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001407 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001408 default: assert(0 && "Unsupported VT!");
1409 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1410 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1411 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001412 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001413 }
1414
Rafael Espindola094fad32009-04-08 21:14:34 +00001415 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1416 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman525178c2007-10-08 18:33:35 +00001417 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001418 if (!foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001419 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Evan Cheng7afa1662007-08-02 05:48:35 +00001420 if (foldedLoad)
1421 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001422 }
1423
Dale Johannesendd64c412009-02-04 00:33:20 +00001424 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
Dan Gohman475871a2008-07-27 21:46:04 +00001425 N0, SDValue()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001426
1427 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001428 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1429 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001430 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001431 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001432 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001433 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001434 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001435 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001436 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001437 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001438 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001439 }
1440
Dan Gohman525178c2007-10-08 18:33:35 +00001441 // Copy the low half of the result, if it is needed.
1442 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001443 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001444 LoReg, NVT, InFlag);
1445 InFlag = Result.getValue(2);
1446 ReplaceUses(N.getValue(0), Result);
1447#ifndef NDEBUG
1448 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001449 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohman525178c2007-10-08 18:33:35 +00001450 DOUT << "\n";
1451#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001452 }
Dan Gohman525178c2007-10-08 18:33:35 +00001453 // Copy the high half of the result, if it is needed.
1454 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001455 SDValue Result;
Dan Gohman525178c2007-10-08 18:33:35 +00001456 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1457 // Prevent use of AH in a REX instruction by referencing AX instead.
1458 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001459 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001460 X86::AX, MVT::i16, InFlag);
1461 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001462 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1463 Result,
Gabor Greif93c53e52008-08-31 15:37:04 +00001464 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001465 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001466 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001467 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001468 MVT::i8, Result, SRIdx), 0);
1469 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001470 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001471 HiReg, NVT, InFlag);
1472 InFlag = Result.getValue(2);
1473 }
1474 ReplaceUses(N.getValue(1), 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
1480 }
Evan Cheng34167212006-02-09 00:37:58 +00001481
Evan Chengf597dc72006-02-10 22:24:32 +00001482#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001483 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001484#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001485
Evan Cheng64a752f2006-08-11 09:08:15 +00001486 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001487 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001488
Dan Gohman525178c2007-10-08 18:33:35 +00001489 case ISD::SDIVREM:
1490 case ISD::UDIVREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001491 SDValue N0 = Node->getOperand(0);
1492 SDValue N1 = Node->getOperand(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001493
1494 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001495 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001496 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001497 default: assert(0 && "Unsupported VT!");
1498 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1499 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1500 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001501 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001502 }
1503 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001504 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001505 default: assert(0 && "Unsupported VT!");
1506 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1507 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1508 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001509 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001510 }
1511
1512 unsigned LoReg, HiReg;
1513 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001514 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001515 default: assert(0 && "Unsupported VT!");
1516 case MVT::i8:
1517 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001518 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001519 SExtOpcode = X86::CBW;
1520 break;
1521 case MVT::i16:
1522 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001523 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001524 SExtOpcode = X86::CWD;
1525 break;
1526 case MVT::i32:
1527 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001528 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001529 SExtOpcode = X86::CDQ;
1530 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001531 case MVT::i64:
1532 LoReg = X86::RAX; HiReg = X86::RDX;
1533 ClrOpcode = X86::MOV64r0;
1534 SExtOpcode = X86::CQO;
1535 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001536 }
1537
Rafael Espindola094fad32009-04-08 21:14:34 +00001538 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1539 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001540 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001541
Dan Gohman475871a2008-07-27 21:46:04 +00001542 SDValue InFlag;
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001543 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001544 // Special case for div8, just use a move with zero extension to AX to
1545 // clear the upper 8 bits (AH).
Rafael Espindola094fad32009-04-08 21:14:34 +00001546 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1547 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1548 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
Evan Chengb1409ce2006-11-17 22:10:14 +00001549 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001550 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, dl, MVT::i16,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001551 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001552 array_lengthof(Ops)), 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001553 Chain = Move.getValue(1);
1554 ReplaceUses(N0.getValue(1), Chain);
1555 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001556 Move =
Dale Johannesend8392542009-02-03 21:48:12 +00001557 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001558 Chain = CurDAG->getEntryNode();
1559 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001560 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
Evan Cheng948f3432006-01-06 23:19:29 +00001561 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001562 } else {
Evan Chengb1409ce2006-11-17 22:10:14 +00001563 InFlag =
Dale Johannesendd64c412009-02-04 00:33:20 +00001564 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
Dan Gohman475871a2008-07-27 21:46:04 +00001565 LoReg, N0, SDValue()).getValue(1);
Dan Gohman1ef4d8f2009-01-21 14:50:16 +00001566 if (isSigned && !signBitIsZero) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001567 // Sign extend the low part into the high part.
1568 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001569 SDValue(CurDAG->getTargetNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001570 } else {
1571 // Zero out the high part, effectively zero extending the input.
Dale Johannesend8392542009-02-03 21:48:12 +00001572 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, dl, NVT),
1573 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00001574 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
Dan Gohman525178c2007-10-08 18:33:35 +00001575 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001576 }
Evan Cheng948f3432006-01-06 23:19:29 +00001577 }
1578
1579 if (foldedLoad) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001580 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1581 InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001582 SDNode *CNode =
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001583 CurDAG->getTargetNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001584 array_lengthof(Ops));
Dan Gohman475871a2008-07-27 21:46:04 +00001585 InFlag = SDValue(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001586 // Update the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00001587 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001588 } else {
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001589 InFlag =
Dale Johannesend8392542009-02-03 21:48:12 +00001590 SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001591 }
1592
Dan Gohmana37c9f72007-09-25 18:23:27 +00001593 // Copy the division (low) result, if it is needed.
1594 if (!N.getValue(0).use_empty()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001595 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001596 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001597 InFlag = Result.getValue(2);
1598 ReplaceUses(N.getValue(0), Result);
1599#ifndef NDEBUG
1600 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001601 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001602 DOUT << "\n";
1603#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001604 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001605 // Copy the remainder (high) result, if it is needed.
1606 if (!N.getValue(1).use_empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001607 SDValue Result;
Dan Gohmana37c9f72007-09-25 18:23:27 +00001608 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1609 // Prevent use of AH in a REX instruction by referencing AX instead.
1610 // Shift it down 8 bits.
Dale Johannesendd64c412009-02-04 00:33:20 +00001611 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001612 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001613 InFlag = Result.getValue(2);
Dale Johannesend8392542009-02-03 21:48:12 +00001614 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, dl, MVT::i16,
1615 Result,
1616 CurDAG->getTargetConstant(8, MVT::i8)),
1617 0);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001618 // Then truncate it down to i8.
Dan Gohman3cd0aa32009-04-13 15:14:03 +00001619 SDValue SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Dale Johannesend8392542009-02-03 21:48:12 +00001620 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, dl,
Dan Gohmana37c9f72007-09-25 18:23:27 +00001621 MVT::i8, Result, SRIdx), 0);
1622 } else {
Dale Johannesendd64c412009-02-04 00:33:20 +00001623 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Dan Gohman525178c2007-10-08 18:33:35 +00001624 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001625 InFlag = Result.getValue(2);
1626 }
1627 ReplaceUses(N.getValue(1), Result);
1628#ifndef NDEBUG
1629 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001630 DEBUG(Result.getNode()->dump(CurDAG));
Dan Gohmana37c9f72007-09-25 18:23:27 +00001631 DOUT << "\n";
1632#endif
1633 }
Evan Chengf597dc72006-02-10 22:24:32 +00001634
1635#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001636 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001637#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001638
1639 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001640 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001641
Evan Cheng851bc042008-06-17 02:01:22 +00001642 case ISD::DECLARE: {
1643 // Handle DECLARE nodes here because the second operand may have been
1644 // wrapped in X86ISD::Wrapper.
Dan Gohman475871a2008-07-27 21:46:04 +00001645 SDValue Chain = Node->getOperand(0);
1646 SDValue N1 = Node->getOperand(1);
1647 SDValue N2 = Node->getOperand(2);
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001648 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
Chris Lattner1823c922009-02-12 17:33:11 +00001649
1650 // FIXME: We need to handle this for VLAs.
1651 if (!FINode) {
1652 ReplaceUses(N.getValue(0), Chain);
1653 return NULL;
1654 }
1655
Evan Chengfab83872008-06-18 02:48:27 +00001656 if (N2.getOpcode() == ISD::ADD &&
1657 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1658 N2 = N2.getOperand(1);
Chris Lattner1823c922009-02-12 17:33:11 +00001659
1660 // If N2 is not Wrapper(decriptor) then the llvm.declare is mangled
1661 // somehow, just ignore it.
1662 if (N2.getOpcode() != X86ISD::Wrapper) {
1663 ReplaceUses(N.getValue(0), Chain);
1664 return NULL;
1665 }
Evan Chengf2accb52009-01-10 03:33:22 +00001666 GlobalAddressSDNode *GVNode =
1667 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0));
Chris Lattner1823c922009-02-12 17:33:11 +00001668 if (GVNode == 0) {
1669 ReplaceUses(N.getValue(0), Chain);
1670 return NULL;
1671 }
Evan Cheng6bb14ca2008-12-10 21:49:05 +00001672 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(),
1673 TLI.getPointerTy());
1674 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(),
1675 TLI.getPointerTy());
1676 SDValue Ops[] = { Tmp1, Tmp2, Chain };
Dale Johannesend8392542009-02-03 21:48:12 +00001677 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, dl,
Rafael Espindolae4d5d342009-03-27 15:45:05 +00001678 MVT::Other, Ops,
Rafael Espindolaa0a4f072009-03-28 19:02:18 +00001679 array_lengthof(Ops));
Evan Cheng851bc042008-06-17 02:01:22 +00001680 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001681 }
1682
Evan Cheng9ade2182006-08-26 05:34:46 +00001683 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001684
Evan Chengf597dc72006-02-10 22:24:32 +00001685#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001686 DOUT << std::string(Indent-2, ' ') << "=> ";
Gabor Greifba36cb52008-08-28 21:40:38 +00001687 if (ResNode == NULL || ResNode == N.getNode())
1688 DEBUG(N.getNode()->dump(CurDAG));
Evan Cheng9ade2182006-08-26 05:34:46 +00001689 else
1690 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001691 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001692 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001693#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001694
1695 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001696}
1697
Chris Lattnerc0bad572006-06-08 18:03:49 +00001698bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00001699SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00001700 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00001701 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00001702 switch (ConstraintCode) {
1703 case 'o': // offsetable ??
1704 case 'v': // not offsetable ??
1705 default: return true;
1706 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00001707 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001708 return true;
1709 break;
1710 }
1711
Evan Cheng04699902006-08-26 01:05:16 +00001712 OutOps.push_back(Op0);
1713 OutOps.push_back(Op1);
1714 OutOps.push_back(Op2);
1715 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001716 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001717 return false;
1718}
1719
Chris Lattnerc961eea2005-11-16 01:54:32 +00001720/// createX86ISelDag - This pass converts a legalized DAG into a
1721/// X86-specific DAG, ready for instruction scheduling.
1722///
Bill Wendling98a366d2009-04-29 23:29:43 +00001723FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
1724 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00001725 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001726}