blob: 524f5fffb5e2df802aeb45332d874104d7099bf1 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Dan Gohman760f86f2009-01-22 21:58:43 +000030#include "llvm/Target/TargetOptions.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000033#include "llvm/Support/CommandLine.h"
Chris Lattner944fac72008-08-23 22:23:09 +000034#include "llvm/Support/MathExtras.h"
35#include "llvm/Support/raw_ostream.h"
Chris Lattner012f2412006-02-17 21:58:01 +000036#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000037#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000038#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000039#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000040#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000041#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000042#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000043using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000044
Chris Lattner0b3e5252006-08-15 19:11:05 +000045/// makeVTList - Return an instance of the SDVTList struct initialized with the
46/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000047static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000048 SDVTList Res = {VTs, NumVTs};
49 return Res;
50}
51
Duncan Sands83ec4b62008-06-06 12:08:01 +000052static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
53 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000054 default: assert(0 && "Unknown FP format");
55 case MVT::f32: return &APFloat::IEEEsingle;
56 case MVT::f64: return &APFloat::IEEEdouble;
57 case MVT::f80: return &APFloat::x87DoubleExtended;
58 case MVT::f128: return &APFloat::IEEEquad;
59 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
60 }
61}
62
Chris Lattnerf8dc0612008-02-03 06:49:24 +000063SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
64
Jim Laskey58b968b2005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
66// ConstantFPSDNode Class
67//===----------------------------------------------------------------------===//
68
69/// isExactlyValue - We don't rely on operator== working on double values, as
70/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
71/// As such, this method can be used to do an exact bit-for-bit comparison of
72/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000073bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000074 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000075}
76
Duncan Sands83ec4b62008-06-06 12:08:01 +000077bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000078 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000079 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000080
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000081 // PPC long double cannot be converted to any other type.
82 if (VT == MVT::ppcf128 ||
83 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000084 return false;
85
Dale Johannesenf04afdb2007-08-30 00:23:21 +000086 // convert modifies in place, so make a copy.
87 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000088 bool losesInfo;
89 (void) Val2.convert(*MVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
90 &losesInfo);
91 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000092}
93
Jim Laskey58b968b2005-08-17 20:08:02 +000094//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000095// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000096//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000097
Evan Chenga8df1662006-03-27 06:58:47 +000098/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000099/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +0000100bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000101 // Look through a bit convert.
102 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000103 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000104
Evan Chenga8df1662006-03-27 06:58:47 +0000105 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000106
107 unsigned i = 0, e = N->getNumOperands();
108
109 // Skip over all of the undef values.
110 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
111 ++i;
112
113 // Do not accept an all-undef vector.
114 if (i == e) return false;
115
116 // Do not accept build_vectors that aren't all constants or which have non-~0
117 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000118 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000119 if (isa<ConstantSDNode>(NotZero)) {
120 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
121 return false;
122 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000123 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen23a98552008-10-09 23:00:39 +0000124 bitcastToAPInt().isAllOnesValue())
Dan Gohman6c231502008-02-29 01:47:35 +0000125 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000126 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000127 return false;
128
129 // Okay, we have at least one ~0 value, check to see if the rest match or are
130 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000131 for (++i; i != e; ++i)
132 if (N->getOperand(i) != NotZero &&
133 N->getOperand(i).getOpcode() != ISD::UNDEF)
134 return false;
135 return true;
136}
137
138
Evan Cheng4a147842006-03-26 09:50:58 +0000139/// isBuildVectorAllZeros - Return true if the specified node is a
140/// BUILD_VECTOR where all of the elements are 0 or undef.
141bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000142 // Look through a bit convert.
143 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000144 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000145
Evan Cheng4a147842006-03-26 09:50:58 +0000146 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000147
148 unsigned i = 0, e = N->getNumOperands();
149
150 // Skip over all of the undef values.
151 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
152 ++i;
153
154 // Do not accept an all-undef vector.
155 if (i == e) return false;
156
157 // Do not accept build_vectors that aren't all constants or which have non-~0
158 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000159 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000160 if (isa<ConstantSDNode>(Zero)) {
161 if (!cast<ConstantSDNode>(Zero)->isNullValue())
162 return false;
163 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000164 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000165 return false;
166 } else
167 return false;
168
169 // Okay, we have at least one ~0 value, check to see if the rest match or are
170 // undefs.
171 for (++i; i != e; ++i)
172 if (N->getOperand(i) != Zero &&
173 N->getOperand(i).getOpcode() != ISD::UNDEF)
174 return false;
175 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000176}
177
Evan Chengefec7512008-02-18 23:04:32 +0000178/// isScalarToVector - Return true if the specified node is a
179/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
180/// element is not an undef.
181bool ISD::isScalarToVector(const SDNode *N) {
182 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
183 return true;
184
185 if (N->getOpcode() != ISD::BUILD_VECTOR)
186 return false;
187 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
188 return false;
189 unsigned NumElems = N->getNumOperands();
190 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000191 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000192 if (V.getOpcode() != ISD::UNDEF)
193 return false;
194 }
195 return true;
196}
197
198
Evan Chengbb81d972008-01-31 09:59:15 +0000199/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000200/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000201bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000202 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000203 if (N->getOpcode() == ISD::DBG_LABEL)
204 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000205 if (N->isMachineOpcode() &&
206 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000207 return true;
208 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000209}
210
Chris Lattnerc3aae252005-01-07 07:46:32 +0000211/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
212/// when given the operation for (X op Y).
213ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
214 // To perform this operation, we just need to swap the L and G bits of the
215 // operation.
216 unsigned OldL = (Operation >> 2) & 1;
217 unsigned OldG = (Operation >> 1) & 1;
218 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
219 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000220 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000221}
222
223/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
224/// 'op' is a valid SetCC operation.
225ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
226 unsigned Operation = Op;
227 if (isInteger)
228 Operation ^= 7; // Flip L, G, E bits, but not U.
229 else
230 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000231
Chris Lattnerc3aae252005-01-07 07:46:32 +0000232 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000233 Operation &= ~8; // Don't let N and U bits get set.
234
Chris Lattnerc3aae252005-01-07 07:46:32 +0000235 return ISD::CondCode(Operation);
236}
237
238
239/// isSignedOp - For an integer comparison, return 1 if the comparison is a
240/// signed operation and 2 if the result is an unsigned comparison. Return zero
241/// if the operation does not depend on the sign of the input (setne and seteq).
242static int isSignedOp(ISD::CondCode Opcode) {
243 switch (Opcode) {
244 default: assert(0 && "Illegal integer setcc operation!");
245 case ISD::SETEQ:
246 case ISD::SETNE: return 0;
247 case ISD::SETLT:
248 case ISD::SETLE:
249 case ISD::SETGT:
250 case ISD::SETGE: return 1;
251 case ISD::SETULT:
252 case ISD::SETULE:
253 case ISD::SETUGT:
254 case ISD::SETUGE: return 2;
255 }
256}
257
258/// getSetCCOrOperation - Return the result of a logical OR between different
259/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
260/// returns SETCC_INVALID if it is not possible to represent the resultant
261/// comparison.
262ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
263 bool isInteger) {
264 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
265 // Cannot fold a signed integer setcc with an unsigned integer setcc.
266 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000267
Chris Lattnerc3aae252005-01-07 07:46:32 +0000268 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000269
Chris Lattnerc3aae252005-01-07 07:46:32 +0000270 // If the N and U bits get set then the resultant comparison DOES suddenly
271 // care about orderedness, and is true when ordered.
272 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000273 Op &= ~16; // Clear the U bit if the N bit is set.
274
275 // Canonicalize illegal integer setcc's.
276 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
277 Op = ISD::SETNE;
278
Chris Lattnerc3aae252005-01-07 07:46:32 +0000279 return ISD::CondCode(Op);
280}
281
282/// getSetCCAndOperation - Return the result of a logical AND between different
283/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
284/// function returns zero if it is not possible to represent the resultant
285/// comparison.
286ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
287 bool isInteger) {
288 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
289 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000290 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000291
292 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000293 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
294
295 // Canonicalize illegal integer setcc's.
296 if (isInteger) {
297 switch (Result) {
298 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000299 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000300 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000301 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
302 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
303 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000304 }
305 }
306
307 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000308}
309
Chris Lattnerb48da392005-01-23 04:39:44 +0000310const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000311 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000312}
313
Jim Laskey58b968b2005-08-17 20:08:02 +0000314//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000315// SDNode Profile Support
316//===----------------------------------------------------------------------===//
317
Jim Laskeydef69b92006-10-27 23:52:51 +0000318/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
319///
Jim Laskey583bd472006-10-27 23:46:08 +0000320static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
321 ID.AddInteger(OpC);
322}
323
324/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
325/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000326static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000327 ID.AddPointer(VTList.VTs);
328}
329
Jim Laskeydef69b92006-10-27 23:52:51 +0000330/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
331///
Jim Laskey583bd472006-10-27 23:46:08 +0000332static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000333 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000334 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000335 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000336 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000337 }
Jim Laskey583bd472006-10-27 23:46:08 +0000338}
339
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000340/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
341///
342static void AddNodeIDOperands(FoldingSetNodeID &ID,
343 const SDUse *Ops, unsigned NumOps) {
344 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000345 ID.AddPointer(Ops->getNode());
346 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000347 }
348}
349
Jim Laskey583bd472006-10-27 23:46:08 +0000350static void AddNodeIDNode(FoldingSetNodeID &ID,
351 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000352 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000353 AddNodeIDOpcode(ID, OpC);
354 AddNodeIDValueTypes(ID, VTList);
355 AddNodeIDOperands(ID, OpList, N);
356}
357
Duncan Sands0dc40452008-10-27 15:30:53 +0000358/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
359/// the NodeID data.
360static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000361 switch (N->getOpcode()) {
362 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000363 case ISD::ARG_FLAGS:
364 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
365 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 case ISD::TargetConstant:
367 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000368 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 break;
370 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000371 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000372 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000374 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000376 case ISD::GlobalAddress:
377 case ISD::TargetGlobalTLSAddress:
378 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000379 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 ID.AddPointer(GA->getGlobal());
381 ID.AddInteger(GA->getOffset());
382 break;
383 }
384 case ISD::BasicBlock:
385 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
386 break;
387 case ISD::Register:
388 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
389 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000390 case ISD::DBG_STOPPOINT: {
391 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
392 ID.AddInteger(DSP->getLine());
393 ID.AddInteger(DSP->getColumn());
394 ID.AddPointer(DSP->getCompileUnit());
395 break;
396 }
Dan Gohman69de1932008-02-06 22:27:42 +0000397 case ISD::SRCVALUE:
398 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
399 break;
400 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000401 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000402 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000403 break;
404 }
405 case ISD::FrameIndex:
406 case ISD::TargetFrameIndex:
407 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
408 break;
409 case ISD::JumpTable:
410 case ISD::TargetJumpTable:
411 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
412 break;
413 case ISD::ConstantPool:
414 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000415 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000416 ID.AddInteger(CP->getAlignment());
417 ID.AddInteger(CP->getOffset());
418 if (CP->isMachineConstantPoolEntry())
419 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
420 else
421 ID.AddPointer(CP->getConstVal());
422 break;
423 }
Dan Gohman5eb0cec2008-09-15 19:46:03 +0000424 case ISD::CALL: {
425 const CallSDNode *Call = cast<CallSDNode>(N);
426 ID.AddInteger(Call->getCallingConv());
427 ID.AddInteger(Call->isVarArg());
428 break;
429 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000430 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000432 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000433 ID.AddInteger(LD->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 break;
435 }
436 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000437 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000438 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000439 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 break;
441 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000442 case ISD::ATOMIC_CMP_SWAP:
443 case ISD::ATOMIC_SWAP:
444 case ISD::ATOMIC_LOAD_ADD:
445 case ISD::ATOMIC_LOAD_SUB:
446 case ISD::ATOMIC_LOAD_AND:
447 case ISD::ATOMIC_LOAD_OR:
448 case ISD::ATOMIC_LOAD_XOR:
449 case ISD::ATOMIC_LOAD_NAND:
450 case ISD::ATOMIC_LOAD_MIN:
451 case ISD::ATOMIC_LOAD_MAX:
452 case ISD::ATOMIC_LOAD_UMIN:
453 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000454 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000455 ID.AddInteger(AT->getMemoryVT().getRawBits());
456 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang28873102008-06-25 08:15:39 +0000457 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000458 }
Mon P Wang28873102008-06-25 08:15:39 +0000459 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000460}
461
Duncan Sands0dc40452008-10-27 15:30:53 +0000462/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
463/// data.
464static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
465 AddNodeIDOpcode(ID, N->getOpcode());
466 // Add the return value info.
467 AddNodeIDValueTypes(ID, N->getVTList());
468 // Add the operand info.
469 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
470
471 // Handle SDNode leafs with special info.
472 AddNodeIDCustom(ID, N);
473}
474
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000475/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
Dan Gohmana7ce7412009-02-03 00:08:45 +0000476/// the CSE map that carries alignment, volatility, indexing mode, and
477/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000478///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000479static inline unsigned
Dan Gohmana7ce7412009-02-03 00:08:45 +0000480encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM,
481 bool isVolatile, unsigned Alignment) {
482 assert((ConvType & 3) == ConvType &&
483 "ConvType may not require more than 2 bits!");
484 assert((AM & 7) == AM &&
485 "AM may not require more than 3 bits!");
486 return ConvType |
487 (AM << 2) |
488 (isVolatile << 5) |
489 ((Log2_32(Alignment) + 1) << 6);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000490}
491
Jim Laskey583bd472006-10-27 23:46:08 +0000492//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000493// SelectionDAG Class
494//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000495
Duncan Sands0dc40452008-10-27 15:30:53 +0000496/// doNotCSE - Return true if CSE should not be performed for this node.
497static bool doNotCSE(SDNode *N) {
498 if (N->getValueType(0) == MVT::Flag)
499 return true; // Never CSE anything that produces a flag.
500
501 switch (N->getOpcode()) {
502 default: break;
503 case ISD::HANDLENODE:
504 case ISD::DBG_LABEL:
505 case ISD::DBG_STOPPOINT:
506 case ISD::EH_LABEL:
507 case ISD::DECLARE:
508 return true; // Never CSE these nodes.
509 }
510
511 // Check that remaining values produced are not flags.
512 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
513 if (N->getValueType(i) == MVT::Flag)
514 return true; // Never CSE anything that produces a flag.
515
516 return false;
517}
518
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000519/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000520/// SelectionDAG.
521void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000522 // Create a dummy node (which is not added to allnodes), that adds a reference
523 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000524 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000525
Chris Lattner190a4182006-08-04 17:45:20 +0000526 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000527
Chris Lattner190a4182006-08-04 17:45:20 +0000528 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000529 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000530 if (I->use_empty())
531 DeadNodes.push_back(I);
532
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000533 RemoveDeadNodes(DeadNodes);
534
535 // If the root changed (e.g. it was a dead load, update the root).
536 setRoot(Dummy.getValue());
537}
538
539/// RemoveDeadNodes - This method deletes the unreachable nodes in the
540/// given list, and any nodes that become unreachable as a result.
541void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
542 DAGUpdateListener *UpdateListener) {
543
Chris Lattner190a4182006-08-04 17:45:20 +0000544 // Process the worklist, deleting the nodes and adding their uses to the
545 // worklist.
546 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000547 SDNode *N = DeadNodes.pop_back_val();
Chris Lattner190a4182006-08-04 17:45:20 +0000548
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000549 if (UpdateListener)
550 UpdateListener->NodeDeleted(N, 0);
551
Chris Lattner190a4182006-08-04 17:45:20 +0000552 // Take the node out of the appropriate CSE map.
553 RemoveNodeFromCSEMaps(N);
554
555 // Next, brutally remove the operand list. This is safe to do, as there are
556 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000557 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
558 SDUse &Use = *I++;
559 SDNode *Operand = Use.getNode();
560 Use.set(SDValue());
561
Chris Lattner190a4182006-08-04 17:45:20 +0000562 // Now that we removed this operand, see if there are no uses of it left.
563 if (Operand->use_empty())
564 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000565 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000566
Dan Gohmanc5336122009-01-19 22:39:36 +0000567 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000568 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000569}
570
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000571void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000572 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000573 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000574}
575
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000576void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000577 // First take this out of the appropriate CSE map.
578 RemoveNodeFromCSEMaps(N);
579
Chris Lattner1e111c72005-09-07 05:37:01 +0000580 // Finally, remove uses due to operands of this node, remove from the
581 // AllNodes list, and delete the node.
582 DeleteNodeNotInCSEMaps(N);
583}
584
585void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000586 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
587 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000588
Dan Gohmanf06c8352008-09-30 18:30:35 +0000589 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000590 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000591
Dan Gohmanc5336122009-01-19 22:39:36 +0000592 DeallocateNode(N);
593}
594
595void SelectionDAG::DeallocateNode(SDNode *N) {
596 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000597 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000598
Dan Gohmanc5336122009-01-19 22:39:36 +0000599 // Set the opcode to DELETED_NODE to help catch bugs when node
600 // memory is reallocated.
601 N->NodeType = ISD::DELETED_NODE;
602
Dan Gohman11467282008-08-26 01:44:34 +0000603 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000604}
605
Chris Lattner149c58c2005-08-16 18:17:10 +0000606/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
607/// correspond to it. This is useful when we're about to delete or repurpose
608/// the node. We don't want future request for structurally identical nodes
609/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000610bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000611 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000612 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000613 case ISD::EntryToken:
614 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000615 return false;
616 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000617 case ISD::CONDCODE:
618 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
619 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000620 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000621 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
622 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000623 case ISD::ExternalSymbol:
624 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000625 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000626 case ISD::TargetExternalSymbol:
627 Erased =
628 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000629 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000630 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000631 MVT VT = cast<VTSDNode>(N)->getVT();
632 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000633 Erased = ExtendedValueTypeNodes.erase(VT);
634 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000635 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
636 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000637 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000638 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000639 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000640 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000641 // Remove it from the CSE Map.
642 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000643 break;
644 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000645#ifndef NDEBUG
646 // Verify that the node was actually in one of the CSE maps, unless it has a
647 // flag result (which cannot be CSE'd) or is one of the special cases that are
648 // not subject to CSE.
649 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000650 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000651 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000652 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000653 assert(0 && "Node is not in map!");
654 }
655#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000656 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000657}
658
Dan Gohman39946102009-01-25 16:29:12 +0000659/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
660/// maps and modified in place. Add it back to the CSE maps, unless an identical
661/// node already exists, in which case transfer all its users to the existing
662/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000663///
Dan Gohman39946102009-01-25 16:29:12 +0000664void
665SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
666 DAGUpdateListener *UpdateListener) {
667 // For node types that aren't CSE'd, just act as if no identical node
668 // already exists.
669 if (!doNotCSE(N)) {
670 SDNode *Existing = CSEMap.GetOrInsertNode(N);
671 if (Existing != N) {
672 // If there was already an existing matching node, use ReplaceAllUsesWith
673 // to replace the dead one with the existing one. This can cause
674 // recursive merging of other unrelated nodes down the line.
675 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Cheng71e86852008-07-08 20:06:39 +0000676
Dan Gohman39946102009-01-25 16:29:12 +0000677 // N is now dead. Inform the listener if it exists and delete it.
678 if (UpdateListener)
679 UpdateListener->NodeDeleted(N, Existing);
680 DeleteNodeNotInCSEMaps(N);
681 return;
682 }
683 }
Evan Cheng71e86852008-07-08 20:06:39 +0000684
Dan Gohman39946102009-01-25 16:29:12 +0000685 // If the node doesn't already exist, we updated it. Inform a listener if
686 // it exists.
687 if (UpdateListener)
688 UpdateListener->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000689}
690
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000691/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
692/// were replaced with those specified. If this node is never memoized,
693/// return null, otherwise return a pointer to the slot it would take. If a
694/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000695SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000696 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000697 if (doNotCSE(N))
698 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000699
Dan Gohman475871a2008-07-27 21:46:04 +0000700 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000701 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000702 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000703 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000704 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000705}
706
707/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
708/// were replaced with those specified. If this node is never memoized,
709/// return null, otherwise return a pointer to the slot it would take. If a
710/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000711SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000712 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000713 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000714 if (doNotCSE(N))
715 return 0;
716
Dan Gohman475871a2008-07-27 21:46:04 +0000717 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000718 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000719 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000720 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000721 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
722}
723
724
725/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
726/// were replaced with those specified. If this node is never memoized,
727/// return null, otherwise return a pointer to the slot it would take. If a
728/// node already exists with these operands, the slot will be non-null.
729SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000730 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000731 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000732 if (doNotCSE(N))
733 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000734
Jim Laskey583bd472006-10-27 23:46:08 +0000735 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000736 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000737 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000738 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000739}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000740
Duncan Sandsd038e042008-07-21 10:20:31 +0000741/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
742void SelectionDAG::VerifyNode(SDNode *N) {
743 switch (N->getOpcode()) {
744 default:
745 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000746 case ISD::BUILD_PAIR: {
747 MVT VT = N->getValueType(0);
748 assert(N->getNumValues() == 1 && "Too many results!");
749 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
750 "Wrong return type!");
751 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
752 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
753 "Mismatched operand types!");
754 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
755 "Wrong operand type!");
756 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
757 "Wrong return type size");
758 break;
759 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000760 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000761 assert(N->getNumValues() == 1 && "Too many results!");
762 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000763 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000764 "Wrong number of operands!");
Duncan Sands0954aef2008-10-22 09:00:33 +0000765 // FIXME: Change vector_shuffle to a variadic node with mask elements being
766 // operands of the node. Currently the mask is a BUILD_VECTOR passed as an
767 // operand, and it is not always possible to legalize it. Turning off the
768 // following checks at least makes it possible to legalize most of the time.
769// MVT EltVT = N->getValueType(0).getVectorElementType();
770// for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohmane7852d02009-01-26 04:35:06 +0000771// assert(I->getValueType() == EltVT &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000772// "Wrong operand type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000773 break;
774 }
775 }
776}
777
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000778/// getMVTAlignment - Compute the default alignment value for the
779/// given type.
780///
781unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
782 const Type *Ty = VT == MVT::iPTR ?
783 PointerType::get(Type::Int8Ty, 0) :
784 VT.getTypeForMVT();
785
786 return TLI.getTargetData()->getABITypeAlignment(Ty);
787}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000788
Dan Gohman7c3234c2008-08-27 23:52:12 +0000789SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
Devang Patelb51d40c2009-02-03 18:46:32 +0000790 : TLI(tli), FLI(fli), DW(0),
Dan Gohman7c3234c2008-08-27 23:52:12 +0000791 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000792 Root(getEntryNode()) {
793 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000794}
795
Devang Patel6e7a1612009-01-09 19:11:50 +0000796void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
797 DwarfWriter *dw) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000798 MF = &mf;
799 MMI = mmi;
Devang Patel6e7a1612009-01-09 19:11:50 +0000800 DW = dw;
Dan Gohman7c3234c2008-08-27 23:52:12 +0000801}
802
Chris Lattner78ec3112003-08-11 14:57:33 +0000803SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000804 allnodes_clear();
805}
806
807void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000808 assert(&*AllNodes.begin() == &EntryNode);
809 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000810 while (!AllNodes.empty())
811 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000812}
813
Dan Gohman7c3234c2008-08-27 23:52:12 +0000814void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000815 allnodes_clear();
816 OperandAllocator.Reset();
817 CSEMap.clear();
818
819 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000820 ExternalSymbols.clear();
821 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000822 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
823 static_cast<CondCodeSDNode*>(0));
824 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
825 static_cast<SDNode*>(0));
826
Dan Gohmane7852d02009-01-26 04:35:06 +0000827 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000828 AllNodes.push_back(&EntryNode);
829 Root = getEntryNode();
830}
831
Bill Wendling6ce610f2009-01-30 22:23:15 +0000832SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, MVT VT) {
833 if (Op.getValueType() == VT) return Op;
834 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
835 VT.getSizeInBits());
836 return getNode(ISD::AND, DL, Op.getValueType(), Op,
837 getConstant(Imm, Op.getValueType()));
838}
839
Bob Wilson4c245462009-01-22 17:39:32 +0000840/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
841///
Bill Wendling41b9d272009-01-30 22:11:22 +0000842SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, MVT VT) {
843 SDValue NegOne;
844 if (VT.isVector()) {
845 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +0000846 SDValue NegOneElt =
847 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), EltVT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000848 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOneElt);
Dale Johannesendbfd8db2009-02-03 01:55:44 +0000849 NegOne = getNode(ISD::BUILD_VECTOR, DL, VT, &NegOnes[0], NegOnes.size());
Bill Wendling41b9d272009-01-30 22:11:22 +0000850 } else {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +0000851 NegOne = getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000852 }
Bill Wendling41b9d272009-01-30 22:11:22 +0000853 return getNode(ISD::XOR, DL, VT, Val, NegOne);
854}
855
Dan Gohman475871a2008-07-27 21:46:04 +0000856SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000857 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmance9bc122009-01-27 20:39:34 +0000858 assert((EltVT.getSizeInBits() >= 64 ||
859 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
860 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000861 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000862}
863
Dan Gohman475871a2008-07-27 21:46:04 +0000864SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000865 return getConstant(*ConstantInt::get(Val), VT, isT);
866}
867
868SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000869 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000870
Evan Cheng0ff39b32008-06-30 07:31:25 +0000871 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000873 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000874
875 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000876 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000877 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000878 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000879 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000880 SDNode *N = NULL;
881 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000882 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000883 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000884 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000885 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000886 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000887 CSEMap.InsertNode(N, IP);
888 AllNodes.push_back(N);
889 }
890
Dan Gohman475871a2008-07-27 21:46:04 +0000891 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000892 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000893 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000894 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000895 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
896 }
897 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000898}
899
Dan Gohman475871a2008-07-27 21:46:04 +0000900SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000901 return getConstant(Val, TLI.getPointerTy(), isTarget);
902}
903
904
Dan Gohman475871a2008-07-27 21:46:04 +0000905SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000906 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
907}
908
909SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000910 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000911
Duncan Sands83ec4b62008-06-06 12:08:01 +0000912 MVT EltVT =
913 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000914
Chris Lattnerd8658612005-02-17 20:17:32 +0000915 // Do the map lookup using the actual bit pattern for the floating point
916 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
917 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000918 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000919 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000920 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000921 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000922 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000923 SDNode *N = NULL;
924 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000925 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000926 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000927 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000928 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000929 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000930 CSEMap.InsertNode(N, IP);
931 AllNodes.push_back(N);
932 }
933
Dan Gohman475871a2008-07-27 21:46:04 +0000934 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000935 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000937 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000938 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
939 }
940 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941}
942
Dan Gohman475871a2008-07-27 21:46:04 +0000943SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000944 MVT EltVT =
945 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000946 if (EltVT==MVT::f32)
947 return getConstantFP(APFloat((float)Val), VT, isTarget);
948 else
949 return getConstantFP(APFloat(Val), VT, isTarget);
950}
951
Dan Gohman475871a2008-07-27 21:46:04 +0000952SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Dan Gohman6520e202008-10-18 02:06:02 +0000953 MVT VT, int64_t Offset,
Dan Gohman475871a2008-07-27 21:46:04 +0000954 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000955 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000956
Dan Gohman6520e202008-10-18 02:06:02 +0000957 // Truncate (with sign-extension) the offset value to the pointer size.
Dan Gohman44013612008-10-21 03:38:42 +0000958 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +0000959 if (BitWidth < 64)
960 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
961
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000962 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
963 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000964 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000965 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000966 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000967 }
968
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000969 if (GVar && GVar->isThreadLocal())
970 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
971 else
972 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000973
Jim Laskey583bd472006-10-27 23:46:08 +0000974 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000975 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000976 ID.AddPointer(GV);
977 ID.AddInteger(Offset);
978 void *IP = 0;
979 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +0000980 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000981 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000982 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000983 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000984 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000985 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986}
987
Dan Gohman475871a2008-07-27 21:46:04 +0000988SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000989 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000990 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000991 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000992 ID.AddInteger(FI);
993 void *IP = 0;
994 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000995 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000996 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000997 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000998 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000999 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001000 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001001}
1002
Dan Gohman475871a2008-07-27 21:46:04 +00001003SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001004 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001005 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001006 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001007 ID.AddInteger(JTI);
1008 void *IP = 0;
1009 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001010 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001011 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001012 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001013 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001014 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001015 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001016}
1017
Dan Gohman475871a2008-07-27 21:46:04 +00001018SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1019 unsigned Alignment, int Offset,
1020 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +00001021 if (Alignment == 0)
1022 Alignment =
1023 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001024 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001025 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001026 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001027 ID.AddInteger(Alignment);
1028 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001029 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001030 void *IP = 0;
1031 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001032 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001033 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001034 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001035 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001036 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001037 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001038}
1039
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040
Dan Gohman475871a2008-07-27 21:46:04 +00001041SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1042 unsigned Alignment, int Offset,
1043 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +00001044 if (Alignment == 0)
1045 Alignment =
1046 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001047 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001048 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001049 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001050 ID.AddInteger(Alignment);
1051 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001052 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001053 void *IP = 0;
1054 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001055 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001056 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001057 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001058 CSEMap.InsertNode(N, IP);
1059 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001060 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001061}
1062
Dan Gohman475871a2008-07-27 21:46:04 +00001063SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001064 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001065 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001066 ID.AddPointer(MBB);
1067 void *IP = 0;
1068 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001069 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001070 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001071 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001072 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001073 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001074 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001075}
1076
Dale Johannesene8c17332009-01-29 00:47:48 +00001077SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB, DebugLoc dl) {
1078 FoldingSetNodeID ID;
1079 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
1080 ID.AddPointer(MBB);
1081 void *IP = 0;
1082 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1083 return SDValue(E, 0);
1084 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
1085 new (N) BasicBlockSDNode(MBB, dl);
1086 CSEMap.InsertNode(N, IP);
1087 AllNodes.push_back(N);
1088 return SDValue(N, 0);
1089}
1090
Dan Gohman475871a2008-07-27 21:46:04 +00001091SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001092 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001093 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001094 ID.AddInteger(Flags.getRawBits());
1095 void *IP = 0;
1096 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001097 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001098 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001099 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001100 CSEMap.InsertNode(N, IP);
1101 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001102 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001103}
1104
Dan Gohman475871a2008-07-27 21:46:04 +00001105SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001106 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1107 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001108
Duncan Sands83ec4b62008-06-06 12:08:01 +00001109 SDNode *&N = VT.isExtended() ?
1110 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001111
Dan Gohman475871a2008-07-27 21:46:04 +00001112 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001113 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001114 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001115 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001116 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001117}
1118
Bill Wendling056292f2008-09-16 21:48:12 +00001119SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1120 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001121 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001122 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1123 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001124 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001125 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001126}
1127
Dale Johannesene8c17332009-01-29 00:47:48 +00001128SDValue SelectionDAG::getExternalSymbol(const char *Sym, DebugLoc dl, MVT VT) {
1129 SDNode *&N = ExternalSymbols[Sym];
1130 if (N) return SDValue(N, 0);
1131 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1132 new (N) ExternalSymbolSDNode(false, dl, Sym, VT);
1133 AllNodes.push_back(N);
1134 return SDValue(N, 0);
1135}
1136
Bill Wendling056292f2008-09-16 21:48:12 +00001137SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
1138 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001139 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001140 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1141 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001142 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001143 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001144}
1145
Dale Johannesene8c17332009-01-29 00:47:48 +00001146SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, DebugLoc dl,
1147 MVT VT) {
1148 SDNode *&N = TargetExternalSymbols[Sym];
1149 if (N) return SDValue(N, 0);
1150 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1151 new (N) ExternalSymbolSDNode(true, dl, Sym, VT);
1152 AllNodes.push_back(N);
1153 return SDValue(N, 0);
1154}
1155
Dan Gohman475871a2008-07-27 21:46:04 +00001156SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001157 if ((unsigned)Cond >= CondCodeNodes.size())
1158 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001159
Chris Lattner079a27a2005-08-09 20:40:02 +00001160 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001161 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001162 new (N) CondCodeSDNode(Cond);
1163 CondCodeNodes[Cond] = N;
1164 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001165 }
Dan Gohman475871a2008-07-27 21:46:04 +00001166 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001167}
1168
Dale Johannesena04b7572009-02-03 23:04:43 +00001169SDValue SelectionDAG::getConvertRndSat(MVT VT, DebugLoc dl,
1170 SDValue Val, SDValue DTy,
1171 SDValue STy, SDValue Rnd, SDValue Sat,
1172 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001173 // If the src and dest types are the same and the conversion is between
1174 // integer types of the same sign or two floats, no conversion is necessary.
1175 if (DTy == STy &&
1176 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001177 return Val;
1178
1179 FoldingSetNodeID ID;
1180 void* IP = 0;
1181 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1182 return SDValue(E, 0);
1183 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1184 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1185 new (N) CvtRndSatSDNode(VT, dl, Ops, 5, Code);
1186 CSEMap.InsertNode(N, IP);
1187 AllNodes.push_back(N);
1188 return SDValue(N, 0);
1189}
1190
Dan Gohman475871a2008-07-27 21:46:04 +00001191SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001192 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001193 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001194 ID.AddInteger(RegNo);
1195 void *IP = 0;
1196 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001197 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001198 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001199 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001200 CSEMap.InsertNode(N, IP);
1201 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001202 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001203}
1204
Dan Gohman475871a2008-07-27 21:46:04 +00001205SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Devang Patel83489bb2009-01-13 00:35:13 +00001206 unsigned Line, unsigned Col,
1207 Value *CU) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001208 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001209 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001210 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001211 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001212}
1213
Dale Johannesene8c17332009-01-29 00:47:48 +00001214SDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl,
1215 SDValue Root,
1216 unsigned LabelID) {
1217 FoldingSetNodeID ID;
1218 SDValue Ops[] = { Root };
1219 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1220 ID.AddInteger(LabelID);
1221 void *IP = 0;
1222 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1223 return SDValue(E, 0);
1224 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
1225 new (N) LabelSDNode(Opcode, dl, Root, LabelID);
1226 CSEMap.InsertNode(N, IP);
1227 AllNodes.push_back(N);
1228 return SDValue(N, 0);
1229}
1230
Dan Gohman475871a2008-07-27 21:46:04 +00001231SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001232 assert((!V || isa<PointerType>(V->getType())) &&
1233 "SrcValue is not a pointer?");
1234
Jim Laskey583bd472006-10-27 23:46:08 +00001235 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001236 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001237 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001238
Chris Lattner61b09412006-08-11 21:01:22 +00001239 void *IP = 0;
1240 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001241 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001242
Dan Gohmanfed90b62008-07-28 21:51:04 +00001243 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001244 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001245 CSEMap.InsertNode(N, IP);
1246 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001247 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001248}
1249
Dan Gohman475871a2008-07-27 21:46:04 +00001250SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Cheng24ac4082008-11-24 07:09:49 +00001251#ifndef NDEBUG
Dan Gohman69de1932008-02-06 22:27:42 +00001252 const Value *v = MO.getValue();
1253 assert((!v || isa<PointerType>(v->getType())) &&
1254 "SrcValue is not a pointer?");
Evan Cheng24ac4082008-11-24 07:09:49 +00001255#endif
Dan Gohman69de1932008-02-06 22:27:42 +00001256
1257 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001258 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001259 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001260
1261 void *IP = 0;
1262 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001263 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001264
Dan Gohmanfed90b62008-07-28 21:51:04 +00001265 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001266 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001267 CSEMap.InsertNode(N, IP);
1268 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001269 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001270}
1271
Duncan Sands92abc622009-01-31 15:50:11 +00001272/// getShiftAmountOperand - Return the specified value casted to
1273/// the target's desired shift amount type.
1274SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
1275 MVT OpTy = Op.getValueType();
1276 MVT ShTy = TLI.getShiftAmountTy();
1277 if (OpTy == ShTy || OpTy.isVector()) return Op;
1278
1279 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
1280 return getNode(Opcode, ShTy, Op);
1281}
1282
Chris Lattner37ce9df2007-10-15 17:47:20 +00001283/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1284/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001285SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001286 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands9304f2c2008-12-04 18:08:40 +00001287 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001288 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001289 unsigned StackAlign =
1290 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1291
Chris Lattner37ce9df2007-10-15 17:47:20 +00001292 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1293 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1294}
1295
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001296/// CreateStackTemporary - Create a stack temporary suitable for holding
1297/// either of the specified value types.
1298SDValue SelectionDAG::CreateStackTemporary(MVT VT1, MVT VT2) {
1299 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1300 VT2.getStoreSizeInBits())/8;
1301 const Type *Ty1 = VT1.getTypeForMVT();
1302 const Type *Ty2 = VT2.getTypeForMVT();
1303 const TargetData *TD = TLI.getTargetData();
1304 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1305 TD->getPrefTypeAlignment(Ty2));
1306
1307 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1308 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1309 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1310}
1311
Dan Gohman475871a2008-07-27 21:46:04 +00001312SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001313 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001314 // These setcc operations always fold.
1315 switch (Cond) {
1316 default: break;
1317 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001318 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001319 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001320 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001321
1322 case ISD::SETOEQ:
1323 case ISD::SETOGT:
1324 case ISD::SETOGE:
1325 case ISD::SETOLT:
1326 case ISD::SETOLE:
1327 case ISD::SETONE:
1328 case ISD::SETO:
1329 case ISD::SETUO:
1330 case ISD::SETUEQ:
1331 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001332 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001333 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001334 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001335
Gabor Greifba36cb52008-08-28 21:40:38 +00001336 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001337 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001338 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001339 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001340
Chris Lattnerc3aae252005-01-07 07:46:32 +00001341 switch (Cond) {
1342 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001343 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1344 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001345 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1346 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1347 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1348 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1349 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1350 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1351 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1352 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001353 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001354 }
Chris Lattner67255a12005-04-07 18:14:58 +00001355 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001356 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1357 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001358 // No compile time operations on this type yet.
1359 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001360 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001361
1362 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001363 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001364 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001365 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001366 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001367 // fall through
1368 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1369 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001370 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001371 // fall through
1372 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001373 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001374 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001375 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001376 // fall through
1377 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1378 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001379 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001380 // fall through
1381 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1382 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001383 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001384 // fall through
1385 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001386 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001387 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001388 return getNode(ISD::UNDEF, dl, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001389 // fall through
1390 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001391 R==APFloat::cmpEqual, VT);
1392 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1393 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1394 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1395 R==APFloat::cmpEqual, VT);
1396 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1397 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1398 R==APFloat::cmpLessThan, VT);
1399 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1400 R==APFloat::cmpUnordered, VT);
1401 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1402 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001403 }
1404 } else {
1405 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001406 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001407 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001408 }
1409
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001410 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001411 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001412}
1413
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001414/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1415/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001416bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001417 unsigned BitWidth = Op.getValueSizeInBits();
1418 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1419}
1420
Dan Gohmanea859be2007-06-22 14:59:07 +00001421/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1422/// this predicate to simplify operations downstream. Mask is known to be zero
1423/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001424bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001425 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001426 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001427 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1428 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1429 return (KnownZero & Mask) == Mask;
1430}
1431
1432/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1433/// known to be either zero or one and return them in the KnownZero/KnownOne
1434/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1435/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001436void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001437 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001438 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001439 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001440 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001441 "Mask size mismatches value type size!");
1442
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001443 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001444 if (Depth == 6 || Mask == 0)
1445 return; // Limit search depth.
1446
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001447 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001448
1449 switch (Op.getOpcode()) {
1450 case ISD::Constant:
1451 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001452 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001453 KnownZero = ~KnownOne & Mask;
1454 return;
1455 case ISD::AND:
1456 // If either the LHS or the RHS are Zero, the result is zero.
1457 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001458 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1459 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001460 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1461 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1462
1463 // Output known-1 bits are only known if set in both the LHS & RHS.
1464 KnownOne &= KnownOne2;
1465 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1466 KnownZero |= KnownZero2;
1467 return;
1468 case ISD::OR:
1469 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001470 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1471 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001472 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1473 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1474
1475 // Output known-0 bits are only known if clear in both the LHS & RHS.
1476 KnownZero &= KnownZero2;
1477 // Output known-1 are known to be set if set in either the LHS | RHS.
1478 KnownOne |= KnownOne2;
1479 return;
1480 case ISD::XOR: {
1481 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1482 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1483 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1484 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1485
1486 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001487 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001488 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1489 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1490 KnownZero = KnownZeroOut;
1491 return;
1492 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001493 case ISD::MUL: {
1494 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1495 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1496 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1497 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1498 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1499
1500 // If low bits are zero in either operand, output low known-0 bits.
1501 // Also compute a conserative estimate for high known-0 bits.
1502 // More trickiness is possible, but this is sufficient for the
1503 // interesting case of alignment computation.
1504 KnownOne.clear();
1505 unsigned TrailZ = KnownZero.countTrailingOnes() +
1506 KnownZero2.countTrailingOnes();
1507 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001508 KnownZero2.countLeadingOnes(),
1509 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001510
1511 TrailZ = std::min(TrailZ, BitWidth);
1512 LeadZ = std::min(LeadZ, BitWidth);
1513 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1514 APInt::getHighBitsSet(BitWidth, LeadZ);
1515 KnownZero &= Mask;
1516 return;
1517 }
1518 case ISD::UDIV: {
1519 // For the purposes of computing leading zeros we can conservatively
1520 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001521 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001522 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1523 ComputeMaskedBits(Op.getOperand(0),
1524 AllOnes, KnownZero2, KnownOne2, Depth+1);
1525 unsigned LeadZ = KnownZero2.countLeadingOnes();
1526
1527 KnownOne2.clear();
1528 KnownZero2.clear();
1529 ComputeMaskedBits(Op.getOperand(1),
1530 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001531 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1532 if (RHSUnknownLeadingOnes != BitWidth)
1533 LeadZ = std::min(BitWidth,
1534 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001535
1536 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1537 return;
1538 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001539 case ISD::SELECT:
1540 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1541 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1542 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1543 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1544
1545 // Only known if known in both the LHS and RHS.
1546 KnownOne &= KnownOne2;
1547 KnownZero &= KnownZero2;
1548 return;
1549 case ISD::SELECT_CC:
1550 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1551 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1552 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1553 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1554
1555 // Only known if known in both the LHS and RHS.
1556 KnownOne &= KnownOne2;
1557 KnownZero &= KnownZero2;
1558 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001559 case ISD::SADDO:
1560 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001561 case ISD::SSUBO:
1562 case ISD::USUBO:
1563 case ISD::SMULO:
1564 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001565 if (Op.getResNo() != 1)
1566 return;
Duncan Sands03228082008-11-23 15:47:28 +00001567 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001568 case ISD::SETCC:
1569 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands03228082008-11-23 15:47:28 +00001570 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001571 BitWidth > 1)
1572 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001573 return;
1574 case ISD::SHL:
1575 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1576 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001577 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001578
1579 // If the shift count is an invalid immediate, don't do anything.
1580 if (ShAmt >= BitWidth)
1581 return;
1582
1583 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001584 KnownZero, KnownOne, Depth+1);
1585 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001586 KnownZero <<= ShAmt;
1587 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001588 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001589 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001590 }
1591 return;
1592 case ISD::SRL:
1593 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1594 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001595 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001596
Dan Gohmand4cf9922008-02-26 18:50:50 +00001597 // If the shift count is an invalid immediate, don't do anything.
1598 if (ShAmt >= BitWidth)
1599 return;
1600
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001601 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001602 KnownZero, KnownOne, Depth+1);
1603 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001604 KnownZero = KnownZero.lshr(ShAmt);
1605 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001606
Dan Gohman72d2fd52008-02-13 22:43:25 +00001607 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001608 KnownZero |= HighBits; // High bits known zero.
1609 }
1610 return;
1611 case ISD::SRA:
1612 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001613 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001614
Dan Gohmand4cf9922008-02-26 18:50:50 +00001615 // If the shift count is an invalid immediate, don't do anything.
1616 if (ShAmt >= BitWidth)
1617 return;
1618
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001619 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001620 // If any of the demanded bits are produced by the sign extension, we also
1621 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001622 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1623 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001624 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001625
1626 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1627 Depth+1);
1628 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001629 KnownZero = KnownZero.lshr(ShAmt);
1630 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001631
1632 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001633 APInt SignBit = APInt::getSignBit(BitWidth);
1634 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001635
Dan Gohmanca93a432008-02-20 16:30:17 +00001636 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001637 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001638 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001639 KnownOne |= HighBits; // New bits are known one.
1640 }
1641 }
1642 return;
1643 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001644 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1645 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001646
1647 // Sign extension. Compute the demanded bits in the result that are not
1648 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001649 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001650
Dan Gohman977a76f2008-02-13 22:28:48 +00001651 APInt InSignBit = APInt::getSignBit(EBits);
1652 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001653
1654 // If the sign extended bits are demanded, we know that the sign
1655 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001656 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001657 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001658 InputDemandedBits |= InSignBit;
1659
1660 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1661 KnownZero, KnownOne, Depth+1);
1662 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1663
1664 // If the sign bit of the input is known set or clear, then we know the
1665 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001666 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001667 KnownZero |= NewBits;
1668 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001669 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001670 KnownOne |= NewBits;
1671 KnownZero &= ~NewBits;
1672 } else { // Input sign bit unknown
1673 KnownZero &= ~NewBits;
1674 KnownOne &= ~NewBits;
1675 }
1676 return;
1677 }
1678 case ISD::CTTZ:
1679 case ISD::CTLZ:
1680 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001681 unsigned LowBits = Log2_32(BitWidth)+1;
1682 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001683 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001684 return;
1685 }
1686 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001687 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001688 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001689 MVT VT = LD->getMemoryVT();
1690 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001691 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001692 }
1693 return;
1694 }
1695 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001696 MVT InVT = Op.getOperand(0).getValueType();
1697 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001698 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1699 APInt InMask = Mask;
1700 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001701 KnownZero.trunc(InBits);
1702 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001703 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001704 KnownZero.zext(BitWidth);
1705 KnownOne.zext(BitWidth);
1706 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001707 return;
1708 }
1709 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001710 MVT InVT = Op.getOperand(0).getValueType();
1711 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001712 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001713 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1714 APInt InMask = Mask;
1715 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001716
1717 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001718 // bit is demanded. Temporarily set this bit in the mask for our callee.
1719 if (NewBits.getBoolValue())
1720 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001721
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001722 KnownZero.trunc(InBits);
1723 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001724 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1725
1726 // Note if the sign bit is known to be zero or one.
1727 bool SignBitKnownZero = KnownZero.isNegative();
1728 bool SignBitKnownOne = KnownOne.isNegative();
1729 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1730 "Sign bit can't be known to be both zero and one!");
1731
1732 // If the sign bit wasn't actually demanded by our caller, we don't
1733 // want it set in the KnownZero and KnownOne result values. Reset the
1734 // mask and reapply it to the result values.
1735 InMask = Mask;
1736 InMask.trunc(InBits);
1737 KnownZero &= InMask;
1738 KnownOne &= InMask;
1739
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001740 KnownZero.zext(BitWidth);
1741 KnownOne.zext(BitWidth);
1742
Dan Gohman977a76f2008-02-13 22:28:48 +00001743 // If the sign bit is known zero or one, the top bits match.
1744 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001745 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001746 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001747 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001748 return;
1749 }
1750 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001751 MVT InVT = Op.getOperand(0).getValueType();
1752 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001753 APInt InMask = Mask;
1754 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001755 KnownZero.trunc(InBits);
1756 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001757 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001758 KnownZero.zext(BitWidth);
1759 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001760 return;
1761 }
1762 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001763 MVT InVT = Op.getOperand(0).getValueType();
1764 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001765 APInt InMask = Mask;
1766 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001767 KnownZero.zext(InBits);
1768 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001769 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001770 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001771 KnownZero.trunc(BitWidth);
1772 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001773 break;
1774 }
1775 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001776 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1777 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001778 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1779 KnownOne, Depth+1);
1780 KnownZero |= (~InMask) & Mask;
1781 return;
1782 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001783 case ISD::FGETSIGN:
1784 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001785 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001786 return;
1787
Dan Gohman23e8b712008-04-28 17:02:21 +00001788 case ISD::SUB: {
1789 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1790 // We know that the top bits of C-X are clear if X contains less bits
1791 // than C (i.e. no wrap-around can happen). For example, 20-X is
1792 // positive if we can prove that X is >= 0 and < 16.
1793 if (CLHS->getAPIntValue().isNonNegative()) {
1794 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1795 // NLZ can't be BitWidth with no sign bit
1796 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1797 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1798 Depth+1);
1799
1800 // If all of the MaskV bits are known to be zero, then we know the
1801 // output top bits are zero, because we now know that the output is
1802 // from [0-C].
1803 if ((KnownZero2 & MaskV) == MaskV) {
1804 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1805 // Top bits known zero.
1806 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1807 }
1808 }
1809 }
1810 }
1811 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001812 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001813 // Output known-0 bits are known if clear or set in both the low clear bits
1814 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1815 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001816 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1817 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1818 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1819 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1820
1821 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1822 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1823 KnownZeroOut = std::min(KnownZeroOut,
1824 KnownZero2.countTrailingOnes());
1825
1826 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001827 return;
1828 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001829 case ISD::SREM:
1830 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001831 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001832 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001833 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001834 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1835 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001836
Dan Gohmana60832b2008-08-13 23:12:35 +00001837 // If the sign bit of the first operand is zero, the sign bit of
1838 // the result is zero. If the first operand has no one bits below
1839 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001840 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1841 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001842
Dan Gohman23e8b712008-04-28 17:02:21 +00001843 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001844
1845 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001846 }
1847 }
1848 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001849 case ISD::UREM: {
1850 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001851 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001852 if (RA.isPowerOf2()) {
1853 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001854 APInt Mask2 = LowBits & Mask;
1855 KnownZero |= ~LowBits & Mask;
1856 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1857 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1858 break;
1859 }
1860 }
1861
1862 // Since the result is less than or equal to either operand, any leading
1863 // zero bits in either operand must also exist in the result.
1864 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1865 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1866 Depth+1);
1867 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1868 Depth+1);
1869
1870 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1871 KnownZero2.countLeadingOnes());
1872 KnownOne.clear();
1873 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1874 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001875 }
1876 default:
1877 // Allow the target to implement this method for its nodes.
1878 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1879 case ISD::INTRINSIC_WO_CHAIN:
1880 case ISD::INTRINSIC_W_CHAIN:
1881 case ISD::INTRINSIC_VOID:
1882 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1883 }
1884 return;
1885 }
1886}
1887
1888/// ComputeNumSignBits - Return the number of times the sign bit of the
1889/// register is replicated into the other bits. We know that at least 1 bit
1890/// is always equal to the sign bit (itself), but other cases can give us
1891/// information. For example, immediately after an "SRA X, 2", we know that
1892/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001893unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001894 MVT VT = Op.getValueType();
1895 assert(VT.isInteger() && "Invalid VT!");
1896 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001898 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001899
1900 if (Depth == 6)
1901 return 1; // Limit search depth.
1902
1903 switch (Op.getOpcode()) {
1904 default: break;
1905 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001906 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001907 return VTBits-Tmp+1;
1908 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001909 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001910 return VTBits-Tmp;
1911
1912 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001913 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1914 // If negative, return # leading ones.
1915 if (Val.isNegative())
1916 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001917
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001918 // Return # leading zeros.
1919 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001920 }
1921
1922 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001923 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001924 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1925
1926 case ISD::SIGN_EXTEND_INREG:
1927 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001928 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001929 Tmp = VTBits-Tmp+1;
1930
1931 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1932 return std::max(Tmp, Tmp2);
1933
1934 case ISD::SRA:
1935 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1936 // SRA X, C -> adds C sign bits.
1937 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001938 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001939 if (Tmp > VTBits) Tmp = VTBits;
1940 }
1941 return Tmp;
1942 case ISD::SHL:
1943 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1944 // shl destroys sign bits.
1945 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001946 if (C->getZExtValue() >= VTBits || // Bad shift.
1947 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1948 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001949 }
1950 break;
1951 case ISD::AND:
1952 case ISD::OR:
1953 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001954 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001955 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001956 if (Tmp != 1) {
1957 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1958 FirstAnswer = std::min(Tmp, Tmp2);
1959 // We computed what we know about the sign bits as our first
1960 // answer. Now proceed to the generic code that uses
1961 // ComputeMaskedBits, and pick whichever answer is better.
1962 }
1963 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001964
1965 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001966 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001967 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001968 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001969 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00001970
1971 case ISD::SADDO:
1972 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001973 case ISD::SSUBO:
1974 case ISD::USUBO:
1975 case ISD::SMULO:
1976 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001977 if (Op.getResNo() != 1)
1978 break;
Duncan Sands03228082008-11-23 15:47:28 +00001979 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001980 case ISD::SETCC:
1981 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands03228082008-11-23 15:47:28 +00001982 if (TLI.getBooleanContents() ==
1983 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00001984 return VTBits;
1985 break;
1986 case ISD::ROTL:
1987 case ISD::ROTR:
1988 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001989 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001990
1991 // Handle rotate right by N like a rotate left by 32-N.
1992 if (Op.getOpcode() == ISD::ROTR)
1993 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1994
1995 // If we aren't rotating out all of the known-in sign bits, return the
1996 // number that are left. This handles rotl(sext(x), 1) for example.
1997 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1998 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1999 }
2000 break;
2001 case ISD::ADD:
2002 // Add can have at most one carry bit. Thus we know that the output
2003 // is, at worst, one more bit than the inputs.
2004 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2005 if (Tmp == 1) return 1; // Early out.
2006
2007 // Special case decrementing a value (ADD X, -1):
2008 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
2009 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002010 APInt KnownZero, KnownOne;
2011 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002012 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
2013
2014 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2015 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002016 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002017 return VTBits;
2018
2019 // If we are subtracting one from a positive number, there is no carry
2020 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002021 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002022 return Tmp;
2023 }
2024
2025 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2026 if (Tmp2 == 1) return 1;
2027 return std::min(Tmp, Tmp2)-1;
2028 break;
2029
2030 case ISD::SUB:
2031 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2032 if (Tmp2 == 1) return 1;
2033
2034 // Handle NEG.
2035 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002036 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002037 APInt KnownZero, KnownOne;
2038 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002039 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2040 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2041 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002042 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002043 return VTBits;
2044
2045 // If the input is known to be positive (the sign bit is known clear),
2046 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002047 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002048 return Tmp2;
2049
2050 // Otherwise, we treat this like a SUB.
2051 }
2052
2053 // Sub can have at most one carry bit. Thus we know that the output
2054 // is, at worst, one more bit than the inputs.
2055 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2056 if (Tmp == 1) return 1; // Early out.
2057 return std::min(Tmp, Tmp2)-1;
2058 break;
2059 case ISD::TRUNCATE:
2060 // FIXME: it's tricky to do anything useful for this, but it is an important
2061 // case for targets like X86.
2062 break;
2063 }
2064
2065 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2066 if (Op.getOpcode() == ISD::LOAD) {
2067 LoadSDNode *LD = cast<LoadSDNode>(Op);
2068 unsigned ExtType = LD->getExtensionType();
2069 switch (ExtType) {
2070 default: break;
2071 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002072 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002073 return VTBits-Tmp+1;
2074 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002075 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002076 return VTBits-Tmp;
2077 }
2078 }
2079
2080 // Allow the target to implement this method for its nodes.
2081 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
2082 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
2083 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2084 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2085 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002086 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002087 }
2088
2089 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2090 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002091 APInt KnownZero, KnownOne;
2092 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002093 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
2094
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002095 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002096 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002097 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002098 Mask = KnownOne;
2099 } else {
2100 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002101 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002102 }
2103
2104 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2105 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002106 Mask = ~Mask;
2107 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002108 // Return # leading zeros. We use 'min' here in case Val was zero before
2109 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002110 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002111}
2112
Chris Lattner51dabfb2006-10-14 00:41:01 +00002113
Dan Gohman475871a2008-07-27 21:46:04 +00002114bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002115 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2116 if (!GA) return false;
Dan Gohman6520e202008-10-18 02:06:02 +00002117 if (GA->getOffset() != 0) return false;
Evan Chenga844bde2008-02-02 04:07:54 +00002118 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2119 if (!GV) return false;
Devang Patel35fe7342009-01-13 22:54:57 +00002120 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patelbbdc8202009-01-13 23:54:55 +00002121 return MMI && MMI->hasDebugInfo();
Evan Chenga844bde2008-02-02 04:07:54 +00002122}
2123
2124
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002125/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2126/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002127SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002128 MVT VT = N->getValueType(0);
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002129 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002130 SDValue PermMask = N->getOperand(2);
2131 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002132 if (Idx.getOpcode() == ISD::UNDEF)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002133 return getNode(ISD::UNDEF, dl, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002134 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002135 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002136 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002137 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002138
2139 if (V.getOpcode() == ISD::BIT_CONVERT) {
2140 V = V.getOperand(0);
Mon P Wange9f10152008-12-09 05:46:39 +00002141 MVT VVT = V.getValueType();
2142 if (!VVT.isVector() || VVT.getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002143 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002144 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002145 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002146 return (Index == 0) ? V.getOperand(0)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002147 : getNode(ISD::UNDEF, dl, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002148 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002149 return V.getOperand(Index);
2150 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002151 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002152 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002153}
2154
2155
Chris Lattnerc3aae252005-01-07 07:46:32 +00002156/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002157///
Dan Gohman475871a2008-07-27 21:46:04 +00002158SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002159 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT);
2160}
2161
2162SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002163 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002164 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002165 void *IP = 0;
2166 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002167 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002168 SDNode *N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002169 new (N) SDNode(Opcode, DL, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002170 CSEMap.InsertNode(N, IP);
2171
2172 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002173#ifndef NDEBUG
2174 VerifyNode(N);
2175#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002176 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002177}
2178
Dan Gohman475871a2008-07-27 21:46:04 +00002179SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002180 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Operand);
2181}
2182
2183SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
2184 MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002185 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002186 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002187 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002188 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002189 switch (Opcode) {
2190 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002191 case ISD::SIGN_EXTEND:
2192 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002193 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002194 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002195 case ISD::TRUNCATE:
2196 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002197 case ISD::UINT_TO_FP:
2198 case ISD::SINT_TO_FP: {
2199 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002200 // No compile time operations on this type.
2201 if (VT==MVT::ppcf128)
2202 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002203 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2204 (void)apf.convertFromAPInt(Val,
2205 Opcode==ISD::SINT_TO_FP,
2206 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002207 return getConstantFP(apf, VT);
2208 }
Chris Lattner94683772005-12-23 05:30:37 +00002209 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002210 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002211 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002212 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002213 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002214 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002215 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002216 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002217 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002218 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002219 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002220 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002221 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002222 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002223 }
2224 }
2225
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002226 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002227 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002228 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002229 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002230 switch (Opcode) {
2231 case ISD::FNEG:
2232 V.changeSign();
2233 return getConstantFP(V, VT);
2234 case ISD::FABS:
2235 V.clearSign();
2236 return getConstantFP(V, VT);
2237 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002238 case ISD::FP_EXTEND: {
2239 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002240 // This can return overflow, underflow, or inexact; we don't care.
2241 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002242 (void)V.convert(*MVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002243 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002244 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002245 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002246 case ISD::FP_TO_SINT:
2247 case ISD::FP_TO_UINT: {
2248 integerPart x;
Dale Johannesen23a98552008-10-09 23:00:39 +00002249 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002250 assert(integerPartWidth >= 64);
2251 // FIXME need to be more flexible about rounding mode.
2252 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2253 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002254 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002255 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2256 break;
2257 return getConstant(x, VT);
2258 }
2259 case ISD::BIT_CONVERT:
2260 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002261 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002262 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002263 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002264 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002265 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002266 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002267 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002268
Gabor Greifba36cb52008-08-28 21:40:38 +00002269 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002270 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002271 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002272 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002273 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002274 return Operand; // Factor, merge or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002275 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002276 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002277 assert(VT.isFloatingPoint() &&
2278 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002279 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002280 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002281 return getNode(ISD::UNDEF, DL, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002282 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002283 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002284 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002285 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002286 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002287 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002288 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002289 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002290 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002291 break;
2292 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002293 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002294 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002295 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002296 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002297 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002298 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002299 return getNode(ISD::ZERO_EXTEND, DL, VT,
2300 Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002301 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002302 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002303 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002304 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002305 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002306 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002307 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002308 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2309 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002310 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002311 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002312 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002313 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002314 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002315 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002316 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002317 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002318 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002319 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002320 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2321 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002322 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002323 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002324 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002325 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002326 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002327 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002328 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002329 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002330 break;
Chris Lattner35481892005-12-23 00:16:34 +00002331 case ISD::BIT_CONVERT:
2332 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002333 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002334 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002335 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002336 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002337 return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002338 if (OpOpcode == ISD::UNDEF)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002339 return getNode(ISD::UNDEF, DL, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002340 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002341 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002342 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2343 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002344 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002345 if (OpOpcode == ISD::UNDEF)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002346 return getNode(ISD::UNDEF, DL, VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002347 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2348 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2349 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2350 Operand.getConstantOperandVal(1) == 0 &&
2351 Operand.getOperand(0).getValueType() == VT)
2352 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002353 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002354 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002355 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
2356 if (UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002357 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002358 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002359 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002360 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002361 break;
2362 case ISD::FABS:
2363 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002364 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002365 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002366 }
2367
Chris Lattner43247a12005-08-25 19:12:10 +00002368 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002369 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002370 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002371 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002372 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002373 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002374 void *IP = 0;
2375 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002376 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002377 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002378 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002379 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002380 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002381 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002382 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002383 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002384
Chris Lattnerc3aae252005-01-07 07:46:32 +00002385 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002386#ifndef NDEBUG
2387 VerifyNode(N);
2388#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002389 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002390}
2391
Bill Wendling688d1c42008-09-24 10:16:24 +00002392SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
2393 MVT VT,
2394 ConstantSDNode *Cst1,
2395 ConstantSDNode *Cst2) {
2396 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2397
2398 switch (Opcode) {
2399 case ISD::ADD: return getConstant(C1 + C2, VT);
2400 case ISD::SUB: return getConstant(C1 - C2, VT);
2401 case ISD::MUL: return getConstant(C1 * C2, VT);
2402 case ISD::UDIV:
2403 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2404 break;
2405 case ISD::UREM:
2406 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2407 break;
2408 case ISD::SDIV:
2409 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2410 break;
2411 case ISD::SREM:
2412 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2413 break;
2414 case ISD::AND: return getConstant(C1 & C2, VT);
2415 case ISD::OR: return getConstant(C1 | C2, VT);
2416 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2417 case ISD::SHL: return getConstant(C1 << C2, VT);
2418 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2419 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2420 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2421 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2422 default: break;
2423 }
2424
2425 return SDValue();
2426}
2427
Dan Gohman475871a2008-07-27 21:46:04 +00002428SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2429 SDValue N1, SDValue N2) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002430 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2);
2431}
2432
2433SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2434 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002435 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2436 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002437 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002438 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002439 case ISD::TokenFactor:
2440 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2441 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002442 // Fold trivial token factors.
2443 if (N1.getOpcode() == ISD::EntryToken) return N2;
2444 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002445 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002446 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002447 case ISD::CONCAT_VECTORS:
2448 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2449 // one big BUILD_VECTOR.
2450 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2451 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002452 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2453 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002454 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002455 }
2456 break;
Chris Lattner76365122005-01-16 02:23:22 +00002457 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002458 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002459 N1.getValueType() == VT && "Binary operator types must match!");
2460 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2461 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002462 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002463 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002464 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2465 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002466 break;
Chris Lattner76365122005-01-16 02:23:22 +00002467 case ISD::OR:
2468 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002469 case ISD::ADD:
2470 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002471 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002472 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002473 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2474 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002475 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002476 return N1;
2477 break;
Chris Lattner76365122005-01-16 02:23:22 +00002478 case ISD::UDIV:
2479 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002480 case ISD::MULHU:
2481 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002482 case ISD::MUL:
2483 case ISD::SDIV:
2484 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002485 assert(VT.isInteger() && "This operator does not apply to FP types!");
2486 // fall through
Chris Lattner01b3d732005-09-28 22:28:18 +00002487 case ISD::FADD:
2488 case ISD::FSUB:
2489 case ISD::FMUL:
2490 case ISD::FDIV:
2491 case ISD::FREM:
Dan Gohmana90c8e62009-01-23 19:10:37 +00002492 if (UnsafeFPMath) {
2493 if (Opcode == ISD::FADD) {
2494 // 0+x --> x
2495 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2496 if (CFP->getValueAPF().isZero())
2497 return N2;
2498 // x+0 --> x
2499 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2500 if (CFP->getValueAPF().isZero())
2501 return N1;
2502 } else if (Opcode == ISD::FSUB) {
2503 // x-0 --> x
2504 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2505 if (CFP->getValueAPF().isZero())
2506 return N1;
2507 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002508 }
Chris Lattner76365122005-01-16 02:23:22 +00002509 assert(N1.getValueType() == N2.getValueType() &&
2510 N1.getValueType() == VT && "Binary operator types must match!");
2511 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002512 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2513 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002514 N1.getValueType().isFloatingPoint() &&
2515 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002516 "Invalid FCOPYSIGN!");
2517 break;
Chris Lattner76365122005-01-16 02:23:22 +00002518 case ISD::SHL:
2519 case ISD::SRA:
2520 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002521 case ISD::ROTL:
2522 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002523 assert(VT == N1.getValueType() &&
2524 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002525 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002526 "Shifts only work on integers");
2527
2528 // Always fold shifts of i1 values so the code generator doesn't need to
2529 // handle them. Since we know the size of the shift has to be less than the
2530 // size of the value, the shift/rotate count is guaranteed to be zero.
2531 if (VT == MVT::i1)
2532 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002533 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002534 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002535 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002536 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002537 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002538 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002539 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002540 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002541 break;
2542 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002543 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 assert(VT.isFloatingPoint() &&
2545 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002546 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002547 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002548 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002549 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002550 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002551 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002552 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002553 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002554 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002555 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002556 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002557 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002558 break;
2559 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002560 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002561 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002562 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002563 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002564 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002565 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002566 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002567
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002568 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002569 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002570 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002571 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002572 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002573 return getConstant(Val, VT);
2574 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002575 break;
2576 }
2577 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002578 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2579 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002580 return getNode(ISD::UNDEF, DL, VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002581
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002582 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2583 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002584 if (N2C &&
2585 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002586 N1.getNumOperands() > 0) {
2587 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002588 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002589 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002590 N1.getOperand(N2C->getZExtValue() / Factor),
2591 getConstant(N2C->getZExtValue() % Factor,
2592 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002593 }
2594
2595 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2596 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002597 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002598 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002599
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002600 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2601 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002602 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Dan Gohman197e88f2009-01-29 16:10:46 +00002603 // If the indices are the same, return the inserted element.
Dan Gohman6ab64222008-08-13 21:51:37 +00002604 if (N1.getOperand(2) == N2)
2605 return N1.getOperand(1);
Dan Gohman197e88f2009-01-29 16:10:46 +00002606 // If the indices are known different, extract the element from
2607 // the original vector.
2608 else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2609 isa<ConstantSDNode>(N2))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002610 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Dan Gohman6ab64222008-08-13 21:51:37 +00002611 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002612 break;
2613 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002614 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002615 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2616 (N1.getValueType().isInteger() == VT.isInteger()) &&
2617 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002618
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002619 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2620 // 64-bit integers into 32-bit parts. Instead of building the extract of
2621 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2622 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002623 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002624
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002625 // EXTRACT_ELEMENT of a constant int is also very common.
2626 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002627 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002628 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002629 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2630 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002631 }
2632 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002633 case ISD::EXTRACT_SUBVECTOR:
2634 if (N1.getValueType() == VT) // Trivial extraction.
2635 return N1;
2636 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002637 }
2638
2639 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002640 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002641 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2642 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002643 } else { // Cannonicalize constant to RHS if commutative
2644 if (isCommutativeBinOp(Opcode)) {
2645 std::swap(N1C, N2C);
2646 std::swap(N1, N2);
2647 }
2648 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002649 }
2650
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002651 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002652 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2653 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002654 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002655 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2656 // Cannonicalize constant to RHS if commutative
2657 std::swap(N1CFP, N2CFP);
2658 std::swap(N1, N2);
2659 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002660 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2661 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002662 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002663 case ISD::FADD:
2664 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002665 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002666 return getConstantFP(V1, VT);
2667 break;
2668 case ISD::FSUB:
2669 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2670 if (s!=APFloat::opInvalidOp)
2671 return getConstantFP(V1, VT);
2672 break;
2673 case ISD::FMUL:
2674 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2675 if (s!=APFloat::opInvalidOp)
2676 return getConstantFP(V1, VT);
2677 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002678 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002679 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2680 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2681 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002682 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002683 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002684 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2685 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2686 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002687 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002688 case ISD::FCOPYSIGN:
2689 V1.copySign(V2);
2690 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002691 default: break;
2692 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002693 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002694 }
Chris Lattner62b57722006-04-20 05:39:12 +00002695
2696 // Canonicalize an UNDEF to the RHS, even over a constant.
2697 if (N1.getOpcode() == ISD::UNDEF) {
2698 if (isCommutativeBinOp(Opcode)) {
2699 std::swap(N1, N2);
2700 } else {
2701 switch (Opcode) {
2702 case ISD::FP_ROUND_INREG:
2703 case ISD::SIGN_EXTEND_INREG:
2704 case ISD::SUB:
2705 case ISD::FSUB:
2706 case ISD::FDIV:
2707 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002708 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002709 return N1; // fold op(undef, arg2) -> undef
2710 case ISD::UDIV:
2711 case ISD::SDIV:
2712 case ISD::UREM:
2713 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002714 case ISD::SRL:
2715 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002716 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002717 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2718 // For vectors, we can't easily build an all zero vector, just return
2719 // the LHS.
2720 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002721 }
2722 }
2723 }
2724
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002725 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002726 if (N2.getOpcode() == ISD::UNDEF) {
2727 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002728 case ISD::XOR:
2729 if (N1.getOpcode() == ISD::UNDEF)
2730 // Handle undef ^ undef -> 0 special case. This is a common
2731 // idiom (misuse).
2732 return getConstant(0, VT);
2733 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002734 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002735 case ISD::ADDC:
2736 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002737 case ISD::SUB:
2738 case ISD::FADD:
2739 case ISD::FSUB:
2740 case ISD::FMUL:
2741 case ISD::FDIV:
2742 case ISD::FREM:
2743 case ISD::UDIV:
2744 case ISD::SDIV:
2745 case ISD::UREM:
2746 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002747 return N2; // fold op(arg1, undef) -> undef
2748 case ISD::MUL:
2749 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002750 case ISD::SRL:
2751 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002752 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002753 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2754 // For vectors, we can't easily build an all zero vector, just return
2755 // the LHS.
2756 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002757 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002758 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002759 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002760 // For vectors, we can't easily build an all one vector, just return
2761 // the LHS.
2762 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002763 case ISD::SRA:
2764 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002765 }
2766 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002767
Chris Lattner27e9b412005-05-11 18:57:39 +00002768 // Memoize this node if possible.
2769 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002770 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002771 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002772 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002773 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002774 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002775 void *IP = 0;
2776 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002777 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002778 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002779 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002780 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002781 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002782 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002783 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002784 }
2785
Chris Lattnerc3aae252005-01-07 07:46:32 +00002786 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002787#ifndef NDEBUG
2788 VerifyNode(N);
2789#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002790 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002791}
2792
Dan Gohman475871a2008-07-27 21:46:04 +00002793SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2794 SDValue N1, SDValue N2, SDValue N3) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002795 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3);
2796}
2797
2798SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2799 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002800 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002801 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2802 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002803 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002804 case ISD::CONCAT_VECTORS:
2805 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2806 // one big BUILD_VECTOR.
2807 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2808 N2.getOpcode() == ISD::BUILD_VECTOR &&
2809 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002810 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2811 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2812 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002813 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002814 }
2815 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002816 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002817 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002818 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002819 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002820 break;
2821 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002822 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002823 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002824 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002825 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002826 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002827 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002828 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002829
2830 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002831 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002832 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002833 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002834 if (N2C->getZExtValue()) // Unconditional branch
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002835 return getNode(ISD::BR, DL, MVT::Other, N1, N3);
Chris Lattner5351e9b2005-01-07 22:49:57 +00002836 else
2837 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002838 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002839 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002840 case ISD::VECTOR_SHUFFLE:
Mon P Wangaeb06d22008-11-10 04:46:22 +00002841 assert(N1.getValueType() == N2.getValueType() &&
2842 N1.getValueType().isVector() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002843 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002844 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002845 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002846 "Illegal VECTOR_SHUFFLE node!");
2847 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002848 case ISD::BIT_CONVERT:
2849 // Fold bit_convert nodes from a type to themselves.
2850 if (N1.getValueType() == VT)
2851 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002852 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002853 }
2854
Chris Lattner43247a12005-08-25 19:12:10 +00002855 // Memoize node if it doesn't produce a flag.
2856 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002857 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002858 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002859 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002860 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002861 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002862 void *IP = 0;
2863 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002864 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002865 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002866 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002867 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002868 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002869 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002870 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002871 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002872 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002873#ifndef NDEBUG
2874 VerifyNode(N);
2875#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002876 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002877}
2878
Dan Gohman475871a2008-07-27 21:46:04 +00002879SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2880 SDValue N1, SDValue N2, SDValue N3,
2881 SDValue N4) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002882 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3, N4);
2883}
2884
2885SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2886 SDValue N1, SDValue N2, SDValue N3,
2887 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00002888 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002889 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002890}
2891
Dan Gohman475871a2008-07-27 21:46:04 +00002892SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2893 SDValue N1, SDValue N2, SDValue N3,
2894 SDValue N4, SDValue N5) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00002895 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, N1, N2, N3, N4, N5);
2896}
2897
2898SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2899 SDValue N1, SDValue N2, SDValue N3,
2900 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00002901 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002902 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002903}
2904
Dan Gohman707e0182008-04-12 04:36:06 +00002905/// getMemsetValue - Vectorized representation of the memset value
2906/// operand.
Dale Johannesen0f502f62009-02-03 22:26:09 +00002907static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG,
2908 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002909 unsigned NumBits = VT.isVector() ?
2910 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002911 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002912 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002913 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002914 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002915 Val = (Val << Shift) | Val;
2916 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002917 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002918 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002919 return DAG.getConstant(Val, VT);
2920 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002921 }
Evan Chengf0df0312008-05-15 08:39:06 +00002922
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002923 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dale Johannesen0f502f62009-02-03 22:26:09 +00002924 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Evan Chengf0df0312008-05-15 08:39:06 +00002925 unsigned Shift = 8;
2926 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00002927 Value = DAG.getNode(ISD::OR, dl, VT,
2928 DAG.getNode(ISD::SHL, dl, VT, Value,
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002929 DAG.getConstant(Shift,
2930 TLI.getShiftAmountTy())),
2931 Value);
Evan Chengf0df0312008-05-15 08:39:06 +00002932 Shift <<= 1;
2933 }
2934
2935 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002936}
2937
Dan Gohman707e0182008-04-12 04:36:06 +00002938/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2939/// used when a memcpy is turned into a memset when the source is a constant
2940/// string ptr.
Dale Johannesen0f502f62009-02-03 22:26:09 +00002941static SDValue getMemsetStringVal(MVT VT, DebugLoc dl, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002942 const TargetLowering &TLI,
2943 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002944 // Handle vector with all elements zero.
2945 if (Str.empty()) {
2946 if (VT.isInteger())
2947 return DAG.getConstant(0, VT);
2948 unsigned NumElts = VT.getVectorNumElements();
2949 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Dale Johannesen0f502f62009-02-03 22:26:09 +00002950 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002951 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2952 }
2953
Duncan Sands83ec4b62008-06-06 12:08:01 +00002954 assert(!VT.isVector() && "Can't handle vector type here!");
2955 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002956 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002957 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002958 if (TLI.isLittleEndian())
2959 Offset = Offset + MSB - 1;
2960 for (unsigned i = 0; i != MSB; ++i) {
2961 Val = (Val << 8) | (unsigned char)Str[Offset];
2962 Offset += TLI.isLittleEndian() ? -1 : 1;
2963 }
2964 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002965}
2966
Dan Gohman707e0182008-04-12 04:36:06 +00002967/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002968///
Dan Gohman475871a2008-07-27 21:46:04 +00002969static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002970 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002971 MVT VT = Base.getValueType();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002972 return DAG.getNode(ISD::ADD, Base.getNode()->getDebugLoc(),
2973 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00002974}
2975
Evan Chengf0df0312008-05-15 08:39:06 +00002976/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2977///
Dan Gohman475871a2008-07-27 21:46:04 +00002978static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002979 unsigned SrcDelta = 0;
2980 GlobalAddressSDNode *G = NULL;
2981 if (Src.getOpcode() == ISD::GlobalAddress)
2982 G = cast<GlobalAddressSDNode>(Src);
2983 else if (Src.getOpcode() == ISD::ADD &&
2984 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2985 Src.getOperand(1).getOpcode() == ISD::Constant) {
2986 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002987 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00002988 }
2989 if (!G)
2990 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002991
Evan Chengf0df0312008-05-15 08:39:06 +00002992 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002993 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2994 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002995
Evan Chengf0df0312008-05-15 08:39:06 +00002996 return false;
2997}
Dan Gohman707e0182008-04-12 04:36:06 +00002998
Evan Chengf0df0312008-05-15 08:39:06 +00002999/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3000/// to replace the memset / memcpy is below the threshold. It also returns the
3001/// types of the sequence of memory ops to perform memset / memcpy.
3002static
Duncan Sands83ec4b62008-06-06 12:08:01 +00003003bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00003004 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00003005 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003006 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00003007 SelectionDAG &DAG,
3008 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003009 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00003010 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00003011 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Chris Lattner7fe5e182008-10-28 07:10:51 +00003012 MVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00003013 if (VT != MVT::iAny) {
3014 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00003015 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00003016 // If source is a string constant, this will require an unaligned load.
3017 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
3018 if (Dst.getOpcode() != ISD::FrameIndex) {
3019 // Can't change destination alignment. It requires a unaligned store.
3020 if (AllowUnalign)
3021 VT = MVT::iAny;
3022 } else {
3023 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
3024 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3025 if (MFI->isFixedObjectIndex(FI)) {
3026 // Can't change destination alignment. It requires a unaligned store.
3027 if (AllowUnalign)
3028 VT = MVT::iAny;
3029 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00003030 // Give the stack frame object a larger alignment if needed.
3031 if (MFI->getObjectAlignment(FI) < NewAlign)
3032 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00003033 Align = NewAlign;
3034 }
3035 }
3036 }
3037 }
3038
3039 if (VT == MVT::iAny) {
3040 if (AllowUnalign) {
3041 VT = MVT::i64;
3042 } else {
3043 switch (Align & 7) {
3044 case 0: VT = MVT::i64; break;
3045 case 4: VT = MVT::i32; break;
3046 case 2: VT = MVT::i16; break;
3047 default: VT = MVT::i8; break;
3048 }
3049 }
3050
Duncan Sands83ec4b62008-06-06 12:08:01 +00003051 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003052 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003053 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
3054 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003055
Duncan Sands8e4eb092008-06-08 20:54:56 +00003056 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003057 VT = LVT;
3058 }
Dan Gohman707e0182008-04-12 04:36:06 +00003059
3060 unsigned NumMemOps = 0;
3061 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003062 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003063 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003064 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003065 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00003066 VT = MVT::i64;
3067 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003068 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
3069 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003070 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003071 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003072 VTSize >>= 1;
3073 }
Dan Gohman707e0182008-04-12 04:36:06 +00003074 }
Dan Gohman707e0182008-04-12 04:36:06 +00003075
3076 if (++NumMemOps > Limit)
3077 return false;
3078 MemOps.push_back(VT);
3079 Size -= VTSize;
3080 }
3081
3082 return true;
3083}
3084
Dale Johannesen0f502f62009-02-03 22:26:09 +00003085static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003086 SDValue Chain, SDValue Dst,
3087 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00003088 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003089 const Value *DstSV, uint64_t DstSVOff,
3090 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00003091 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3092
Dan Gohman21323f32008-05-29 19:42:22 +00003093 // Expand memcpy to a series of load and store ops if the size operand falls
3094 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003096 uint64_t Limit = -1ULL;
Dan Gohman707e0182008-04-12 04:36:06 +00003097 if (!AlwaysInline)
3098 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00003099 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003100 std::string Str;
3101 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003102 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003103 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003104 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003105
Dan Gohman707e0182008-04-12 04:36:06 +00003106
Evan Cheng0ff39b32008-06-30 07:31:25 +00003107 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00003108 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003109 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003110 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003111 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003112 MVT VT = MemOps[i];
3113 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003114 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003115
Evan Cheng0ff39b32008-06-30 07:31:25 +00003116 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00003117 // It's unlikely a store of a vector immediate can be done in a single
3118 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003119 // We also handle store a vector with all zero's.
3120 // FIXME: Handle other cases where store of vector immediate is done in
3121 // a single instruction.
Dale Johannesen0f502f62009-02-03 22:26:09 +00003122 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
3123 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003124 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00003125 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003126 } else {
Dale Johannesen0f502f62009-02-03 22:26:09 +00003127 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00003128 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00003129 SrcSV, SrcSVOff + SrcOff, false, Align);
Dale Johannesen0f502f62009-02-03 22:26:09 +00003130 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003131 getMemBasePlusOffset(Dst, DstOff, DAG),
3132 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003133 }
3134 OutChains.push_back(Store);
3135 SrcOff += VTSize;
3136 DstOff += VTSize;
3137 }
3138
Dale Johannesen0f502f62009-02-03 22:26:09 +00003139 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003140 &OutChains[0], OutChains.size());
3141}
3142
Dale Johannesen0f502f62009-02-03 22:26:09 +00003143static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003144 SDValue Chain, SDValue Dst,
3145 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00003146 unsigned Align, bool AlwaysInline,
3147 const Value *DstSV, uint64_t DstSVOff,
3148 const Value *SrcSV, uint64_t SrcSVOff){
3149 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3150
3151 // Expand memmove to a series of load and store ops if the size operand falls
3152 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003153 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003154 uint64_t Limit = -1ULL;
Dan Gohman21323f32008-05-29 19:42:22 +00003155 if (!AlwaysInline)
3156 Limit = TLI.getMaxStoresPerMemmove();
3157 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003158 std::string Str;
3159 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00003160 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003161 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003162 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003163
Dan Gohman21323f32008-05-29 19:42:22 +00003164 uint64_t SrcOff = 0, DstOff = 0;
3165
Dan Gohman475871a2008-07-27 21:46:04 +00003166 SmallVector<SDValue, 8> LoadValues;
3167 SmallVector<SDValue, 8> LoadChains;
3168 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003169 unsigned NumMemOps = MemOps.size();
3170 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003171 MVT VT = MemOps[i];
3172 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003173 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003174
Dale Johannesen0f502f62009-02-03 22:26:09 +00003175 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003176 getMemBasePlusOffset(Src, SrcOff, DAG),
3177 SrcSV, SrcSVOff + SrcOff, false, Align);
3178 LoadValues.push_back(Value);
3179 LoadChains.push_back(Value.getValue(1));
3180 SrcOff += VTSize;
3181 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00003182 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003183 &LoadChains[0], LoadChains.size());
3184 OutChains.clear();
3185 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003186 MVT VT = MemOps[i];
3187 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003188 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003189
Dale Johannesen0f502f62009-02-03 22:26:09 +00003190 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003191 getMemBasePlusOffset(Dst, DstOff, DAG),
3192 DstSV, DstSVOff + DstOff, false, DstAlign);
3193 OutChains.push_back(Store);
3194 DstOff += VTSize;
3195 }
3196
Dale Johannesen0f502f62009-02-03 22:26:09 +00003197 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003198 &OutChains[0], OutChains.size());
3199}
3200
Dale Johannesen0f502f62009-02-03 22:26:09 +00003201static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003202 SDValue Chain, SDValue Dst,
3203 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003204 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003205 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003206 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3207
3208 // Expand memset to a series of load/store ops if the size operand
3209 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003210 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003211 std::string Str;
3212 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003213 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003214 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003215 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003216
Dan Gohman475871a2008-07-27 21:46:04 +00003217 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003218 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003219
3220 unsigned NumMemOps = MemOps.size();
3221 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003222 MVT VT = MemOps[i];
3223 unsigned VTSize = VT.getSizeInBits() / 8;
Dale Johannesen0f502f62009-02-03 22:26:09 +00003224 SDValue Value = getMemsetValue(Src, VT, DAG, dl);
3225 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003226 getMemBasePlusOffset(Dst, DstOff, DAG),
3227 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003228 OutChains.push_back(Store);
3229 DstOff += VTSize;
3230 }
3231
Dale Johannesen0f502f62009-02-03 22:26:09 +00003232 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003233 &OutChains[0], OutChains.size());
3234}
3235
Dale Johannesen0f502f62009-02-03 22:26:09 +00003236SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003237 SDValue Src, SDValue Size,
3238 unsigned Align, bool AlwaysInline,
3239 const Value *DstSV, uint64_t DstSVOff,
3240 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003241
3242 // Check to see if we should lower the memcpy to loads and stores first.
3243 // For cases within the target-specified limits, this is the best choice.
3244 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3245 if (ConstantSize) {
3246 // Memcpy with size zero? Just return the original chain.
3247 if (ConstantSize->isNullValue())
3248 return Chain;
3249
Dan Gohman475871a2008-07-27 21:46:04 +00003250 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003251 getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003252 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003253 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003254 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003255 return Result;
3256 }
3257
3258 // Then check to see if we should lower the memcpy with target-specific
3259 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003260 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003261 TLI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman707e0182008-04-12 04:36:06 +00003262 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003263 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003264 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003265 return Result;
3266
3267 // If we really need inline code and the target declined to provide it,
3268 // use a (potentially long) sequence of loads and stores.
3269 if (AlwaysInline) {
3270 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003271 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003272 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003273 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003274 }
3275
3276 // Emit a library call.
3277 TargetLowering::ArgListTy Args;
3278 TargetLowering::ArgListEntry Entry;
3279 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3280 Entry.Node = Dst; Args.push_back(Entry);
3281 Entry.Node = Src; Args.push_back(Entry);
3282 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003283 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003284 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003285 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003286 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003287 getExternalSymbol("memcpy", TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003288 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003289 return CallResult.second;
3290}
3291
Dale Johannesen0f502f62009-02-03 22:26:09 +00003292SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003293 SDValue Src, SDValue Size,
3294 unsigned Align,
3295 const Value *DstSV, uint64_t DstSVOff,
3296 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003297
Dan Gohman21323f32008-05-29 19:42:22 +00003298 // Check to see if we should lower the memmove to loads and stores first.
3299 // For cases within the target-specified limits, this is the best choice.
3300 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3301 if (ConstantSize) {
3302 // Memmove with size zero? Just return the original chain.
3303 if (ConstantSize->isNullValue())
3304 return Chain;
3305
Dan Gohman475871a2008-07-27 21:46:04 +00003306 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003307 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003308 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003309 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003310 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003311 return Result;
3312 }
Dan Gohman707e0182008-04-12 04:36:06 +00003313
3314 // Then check to see if we should lower the memmove with target-specific
3315 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003316 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003317 TLI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003318 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003319 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003320 return Result;
3321
3322 // Emit a library call.
3323 TargetLowering::ArgListTy Args;
3324 TargetLowering::ArgListEntry Entry;
3325 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3326 Entry.Node = Dst; Args.push_back(Entry);
3327 Entry.Node = Src; Args.push_back(Entry);
3328 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003329 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003330 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003331 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003332 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003333 getExternalSymbol("memmove", TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003334 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003335 return CallResult.second;
3336}
3337
Dale Johannesen0f502f62009-02-03 22:26:09 +00003338SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003339 SDValue Src, SDValue Size,
3340 unsigned Align,
3341 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003342
3343 // Check to see if we should lower the memset to stores first.
3344 // For cases within the target-specified limits, this is the best choice.
3345 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3346 if (ConstantSize) {
3347 // Memset with size zero? Just return the original chain.
3348 if (ConstantSize->isNullValue())
3349 return Chain;
3350
Dan Gohman475871a2008-07-27 21:46:04 +00003351 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003352 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003353 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003354 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003355 return Result;
3356 }
3357
3358 // Then check to see if we should lower the memset with target-specific
3359 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003360 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003361 TLI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align,
Bill Wendling6158d842008-10-01 00:59:58 +00003362 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003363 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003364 return Result;
3365
3366 // Emit a library call.
3367 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3368 TargetLowering::ArgListTy Args;
3369 TargetLowering::ArgListEntry Entry;
3370 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3371 Args.push_back(Entry);
3372 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003373 if (Src.getValueType().bitsGT(MVT::i32))
Dale Johannesen0f502f62009-02-03 22:26:09 +00003374 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003375 else
Dale Johannesen0f502f62009-02-03 22:26:09 +00003376 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003377 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3378 Args.push_back(Entry);
3379 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3380 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003381 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003382 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003383 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003384 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003385 getExternalSymbol("memset", TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003386 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003387 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003388}
3389
Dale Johannesene8c17332009-01-29 00:47:48 +00003390SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3391 SDValue Chain,
3392 SDValue Ptr, SDValue Cmp,
3393 SDValue Swp, const Value* PtrVal,
3394 unsigned Alignment) {
3395 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3396 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3397
3398 MVT VT = Cmp.getValueType();
3399
3400 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3401 Alignment = getMVTAlignment(MemVT);
3402
3403 SDVTList VTs = getVTList(VT, MVT::Other);
3404 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003405 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003406 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3407 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3408 void* IP = 0;
3409 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3410 return SDValue(E, 0);
3411 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3412 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3413 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
3414 CSEMap.InsertNode(N, IP);
3415 AllNodes.push_back(N);
3416 return SDValue(N, 0);
3417}
3418
Dale Johannesene8c17332009-01-29 00:47:48 +00003419SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3420 SDValue Chain,
3421 SDValue Ptr, SDValue Val,
3422 const Value* PtrVal,
3423 unsigned Alignment) {
3424 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3425 Opcode == ISD::ATOMIC_LOAD_SUB ||
3426 Opcode == ISD::ATOMIC_LOAD_AND ||
3427 Opcode == ISD::ATOMIC_LOAD_OR ||
3428 Opcode == ISD::ATOMIC_LOAD_XOR ||
3429 Opcode == ISD::ATOMIC_LOAD_NAND ||
3430 Opcode == ISD::ATOMIC_LOAD_MIN ||
3431 Opcode == ISD::ATOMIC_LOAD_MAX ||
3432 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3433 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3434 Opcode == ISD::ATOMIC_SWAP) &&
3435 "Invalid Atomic Op");
3436
3437 MVT VT = Val.getValueType();
3438
3439 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3440 Alignment = getMVTAlignment(MemVT);
3441
3442 SDVTList VTs = getVTList(VT, MVT::Other);
3443 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003444 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003445 SDValue Ops[] = {Chain, Ptr, Val};
3446 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3447 void* IP = 0;
3448 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3449 return SDValue(E, 0);
3450 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3451 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3452 Chain, Ptr, Val, PtrVal, Alignment);
3453 CSEMap.InsertNode(N, IP);
3454 AllNodes.push_back(N);
3455 return SDValue(N, 0);
3456}
3457
Duncan Sands4bdcb612008-07-02 17:40:58 +00003458/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3459/// Allowed to return something different (and simpler) if Simplify is true.
Dale Johannesen54c94522009-02-02 20:47:48 +00003460SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3461 DebugLoc dl) {
3462 if (NumOps == 1)
3463 return Ops[0];
3464
3465 SmallVector<MVT, 4> VTs;
3466 VTs.reserve(NumOps);
3467 for (unsigned i = 0; i < NumOps; ++i)
3468 VTs.push_back(Ops[i].getValueType());
3469 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
3470 Ops, NumOps);
3471}
3472
Dan Gohman475871a2008-07-27 21:46:04 +00003473SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003474SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
3475 const MVT *VTs, unsigned NumVTs,
3476 const SDValue *Ops, unsigned NumOps,
3477 MVT MemVT, const Value *srcValue, int SVOff,
3478 unsigned Align, bool Vol,
3479 bool ReadMem, bool WriteMem) {
3480 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3481 MemVT, srcValue, SVOff, Align, Vol,
3482 ReadMem, WriteMem);
3483}
3484
3485SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003486SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3487 const SDValue *Ops, unsigned NumOps,
3488 MVT MemVT, const Value *srcValue, int SVOff,
3489 unsigned Align, bool Vol,
3490 bool ReadMem, bool WriteMem) {
3491 // Memoize the node unless it returns a flag.
3492 MemIntrinsicSDNode *N;
3493 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3494 FoldingSetNodeID ID;
3495 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3496 void *IP = 0;
3497 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3498 return SDValue(E, 0);
3499
3500 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3501 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3502 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3503 CSEMap.InsertNode(N, IP);
3504 } else {
3505 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3506 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3507 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3508 }
3509 AllNodes.push_back(N);
3510 return SDValue(N, 0);
3511}
3512
3513SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003514SelectionDAG::getCall(unsigned CallingConv, DebugLoc dl, bool IsVarArgs,
3515 bool IsTailCall, bool IsInreg, SDVTList VTs,
3516 const SDValue *Operands, unsigned NumOperands) {
3517 // Do not include isTailCall in the folding set profile.
3518 FoldingSetNodeID ID;
3519 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3520 ID.AddInteger(CallingConv);
3521 ID.AddInteger(IsVarArgs);
3522 void *IP = 0;
3523 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3524 // Instead of including isTailCall in the folding set, we just
3525 // set the flag of the existing node.
3526 if (!IsTailCall)
3527 cast<CallSDNode>(E)->setNotTailCall();
3528 return SDValue(E, 0);
3529 }
3530 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
3531 new (N) CallSDNode(CallingConv, dl, IsVarArgs, IsTailCall, IsInreg,
3532 VTs, Operands, NumOperands);
3533 CSEMap.InsertNode(N, IP);
3534 AllNodes.push_back(N);
3535 return SDValue(N, 0);
3536}
3537
3538SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003539SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
3540 ISD::LoadExtType ExtType, MVT VT, SDValue Chain,
3541 SDValue Ptr, SDValue Offset,
3542 const Value *SV, int SVOffset, MVT EVT,
3543 bool isVolatile, unsigned Alignment) {
3544 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3545 Alignment = getMVTAlignment(VT);
3546
3547 if (VT == EVT) {
3548 ExtType = ISD::NON_EXTLOAD;
3549 } else if (ExtType == ISD::NON_EXTLOAD) {
3550 assert(VT == EVT && "Non-extending load from different memory type!");
3551 } else {
3552 // Extending load.
3553 if (VT.isVector())
3554 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3555 "Invalid vector extload!");
3556 else
3557 assert(EVT.bitsLT(VT) &&
3558 "Should only be an extending load, not truncating!");
3559 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
3560 "Cannot sign/zero extend a FP/Vector load!");
3561 assert(VT.isInteger() == EVT.isInteger() &&
3562 "Cannot convert from FP to Int or Int -> FP!");
3563 }
3564
3565 bool Indexed = AM != ISD::UNINDEXED;
3566 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3567 "Unindexed load with an offset!");
3568
3569 SDVTList VTs = Indexed ?
3570 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3571 SDValue Ops[] = { Chain, Ptr, Offset };
3572 FoldingSetNodeID ID;
3573 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dale Johannesene8c17332009-01-29 00:47:48 +00003574 ID.AddInteger(EVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003575 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003576 void *IP = 0;
3577 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3578 return SDValue(E, 0);
3579 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
3580 new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, EVT, SV, SVOffset,
3581 Alignment, isVolatile);
3582 CSEMap.InsertNode(N, IP);
3583 AllNodes.push_back(N);
3584 return SDValue(N, 0);
3585}
3586
Dale Johannesene8c17332009-01-29 00:47:48 +00003587SDValue SelectionDAG::getLoad(MVT VT, DebugLoc dl,
3588 SDValue Chain, SDValue Ptr,
3589 const Value *SV, int SVOffset,
3590 bool isVolatile, unsigned Alignment) {
3591 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3592 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3593 SV, SVOffset, VT, isVolatile, Alignment);
3594}
3595
Dale Johannesene8c17332009-01-29 00:47:48 +00003596SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, MVT VT,
3597 SDValue Chain, SDValue Ptr,
3598 const Value *SV,
3599 int SVOffset, MVT EVT,
3600 bool isVolatile, unsigned Alignment) {
3601 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3602 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
3603 SV, SVOffset, EVT, isVolatile, Alignment);
3604}
3605
Dan Gohman475871a2008-07-27 21:46:04 +00003606SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003607SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3608 SDValue Offset, ISD::MemIndexedMode AM) {
3609 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3610 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3611 "Load is already a indexed load!");
3612 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3613 LD->getChain(), Base, Offset, LD->getSrcValue(),
3614 LD->getSrcValueOffset(), LD->getMemoryVT(),
3615 LD->isVolatile(), LD->getAlignment());
3616}
3617
Dale Johannesene8c17332009-01-29 00:47:48 +00003618SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3619 SDValue Ptr, const Value *SV, int SVOffset,
3620 bool isVolatile, unsigned Alignment) {
3621 MVT VT = Val.getValueType();
3622
3623 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3624 Alignment = getMVTAlignment(VT);
3625
3626 SDVTList VTs = getVTList(MVT::Other);
3627 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3628 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3629 FoldingSetNodeID ID;
3630 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003631 ID.AddInteger(VT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003632 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED,
3633 isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003634 void *IP = 0;
3635 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3636 return SDValue(E, 0);
3637 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3638 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false,
3639 VT, SV, SVOffset, Alignment, isVolatile);
3640 CSEMap.InsertNode(N, IP);
3641 AllNodes.push_back(N);
3642 return SDValue(N, 0);
3643}
3644
Dale Johannesene8c17332009-01-29 00:47:48 +00003645SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3646 SDValue Ptr, const Value *SV,
3647 int SVOffset, MVT SVT,
3648 bool isVolatile, unsigned Alignment) {
3649 MVT VT = Val.getValueType();
3650
3651 if (VT == SVT)
3652 return getStore(Chain, dl, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
3653
3654 assert(VT.bitsGT(SVT) && "Not a truncation?");
3655 assert(VT.isInteger() == SVT.isInteger() &&
3656 "Can't do FP-INT conversion!");
3657
3658 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3659 Alignment = getMVTAlignment(VT);
3660
3661 SDVTList VTs = getVTList(MVT::Other);
3662 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3663 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3664 FoldingSetNodeID ID;
3665 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003666 ID.AddInteger(SVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003667 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED,
3668 isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003669 void *IP = 0;
3670 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3671 return SDValue(E, 0);
3672 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3673 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true,
3674 SVT, SV, SVOffset, Alignment, isVolatile);
3675 CSEMap.InsertNode(N, IP);
3676 AllNodes.push_back(N);
3677 return SDValue(N, 0);
3678}
3679
Dan Gohman475871a2008-07-27 21:46:04 +00003680SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003681SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
3682 SDValue Offset, ISD::MemIndexedMode AM) {
3683 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3684 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3685 "Store is already a indexed store!");
3686 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3687 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3688 FoldingSetNodeID ID;
3689 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003690 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003691 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesene8c17332009-01-29 00:47:48 +00003692 void *IP = 0;
3693 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3694 return SDValue(E, 0);
3695 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3696 new (N) StoreSDNode(Ops, dl, VTs, AM,
3697 ST->isTruncatingStore(), ST->getMemoryVT(),
3698 ST->getSrcValue(), ST->getSrcValueOffset(),
3699 ST->getAlignment(), ST->isVolatile());
3700 CSEMap.InsertNode(N, IP);
3701 AllNodes.push_back(N);
3702 return SDValue(N, 0);
3703}
3704
Dale Johannesen0f502f62009-02-03 22:26:09 +00003705SDValue SelectionDAG::getVAArg(MVT VT, DebugLoc dl,
3706 SDValue Chain, SDValue Ptr,
3707 SDValue SV) {
3708 SDValue Ops[] = { Chain, Ptr, SV };
3709 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 3);
3710}
3711
Dan Gohman475871a2008-07-27 21:46:04 +00003712SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3713 const SDUse *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003714 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Ops, NumOps);
3715}
3716
3717SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
3718 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003719 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003720 case 0: return getNode(Opcode, DL, VT);
3721 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3722 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3723 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003724 default: break;
3725 }
3726
Dan Gohman475871a2008-07-27 21:46:04 +00003727 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003728 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003729 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003730 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003731}
3732
Dan Gohman475871a2008-07-27 21:46:04 +00003733SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3734 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003735 return getNode(Opcode, DebugLoc::getUnknownLoc(), VT, Ops, NumOps);
3736}
3737
3738SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
3739 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003740 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003741 case 0: return getNode(Opcode, DL, VT);
3742 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3743 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3744 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003745 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003746 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003747
Chris Lattneref847df2005-04-09 03:27:28 +00003748 switch (Opcode) {
3749 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003750 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003751 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003752 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3753 "LHS and RHS of condition must have same type!");
3754 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3755 "True and False arms of SelectCC must have same type!");
3756 assert(Ops[2].getValueType() == VT &&
3757 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003758 break;
3759 }
3760 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003761 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003762 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3763 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003764 break;
3765 }
Chris Lattneref847df2005-04-09 03:27:28 +00003766 }
3767
Chris Lattner385328c2005-05-14 07:42:29 +00003768 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003769 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003770 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003771
Chris Lattner43247a12005-08-25 19:12:10 +00003772 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003773 FoldingSetNodeID ID;
3774 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003775 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00003776
Chris Lattnera5682852006-08-07 23:03:03 +00003777 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003778 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003779
Dan Gohmanfed90b62008-07-28 21:51:04 +00003780 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003781 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003782 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003783 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003784 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003785 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003786 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00003787
Chris Lattneref847df2005-04-09 03:27:28 +00003788 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003789#ifndef NDEBUG
3790 VerifyNode(N);
3791#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003792 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003793}
3794
Dan Gohman475871a2008-07-27 21:46:04 +00003795SDValue SelectionDAG::getNode(unsigned Opcode,
3796 const std::vector<MVT> &ResultTys,
3797 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003798 return getNode(Opcode, DebugLoc::getUnknownLoc(), ResultTys, Ops, NumOps);
3799}
3800
3801SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
3802 const std::vector<MVT> &ResultTys,
3803 const SDValue *Ops, unsigned NumOps) {
3804 return getNode(Opcode, DL, getNodeValueTypes(ResultTys), ResultTys.size(),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003805 Ops, NumOps);
3806}
3807
Dan Gohman475871a2008-07-27 21:46:04 +00003808SDValue SelectionDAG::getNode(unsigned Opcode,
3809 const MVT *VTs, unsigned NumVTs,
3810 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003811 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTs, NumVTs, Ops, NumOps);
3812}
3813
3814SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
3815 const MVT *VTs, unsigned NumVTs,
3816 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003817 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003818 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
3819 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003820}
3821
Dan Gohman475871a2008-07-27 21:46:04 +00003822SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3823 const SDValue *Ops, unsigned NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003824 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, Ops, NumOps);
3825}
3826
3827SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3828 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003829 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003830 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003831
Chris Lattner5f056bf2005-07-10 01:55:33 +00003832 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003833 // FIXME: figure out how to safely handle things like
3834 // int foo(int x) { return 1 << (x & 255); }
3835 // int bar() { return foo(256); }
3836#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003837 case ISD::SRA_PARTS:
3838 case ISD::SRL_PARTS:
3839 case ISD::SHL_PARTS:
3840 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003841 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003842 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00003843 else if (N3.getOpcode() == ISD::AND)
3844 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3845 // If the and is only masking out bits that cannot effect the shift,
3846 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003847 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003848 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003849 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00003850 }
3851 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003852#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003853 }
Chris Lattner89c34632005-05-14 06:20:26 +00003854
Chris Lattner43247a12005-08-25 19:12:10 +00003855 // Memoize the node unless it returns a flag.
3856 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003857 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003858 FoldingSetNodeID ID;
3859 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003860 void *IP = 0;
3861 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003862 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003863 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003864 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003865 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003866 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003867 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003868 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003869 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003870 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003871 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003872 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003873 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003874 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003875 }
Chris Lattnera5682852006-08-07 23:03:03 +00003876 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003877 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003878 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003879 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003880 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003881 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003882 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003883 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003884 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003885 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003886 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003887 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003888 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003889 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003890 }
Chris Lattner43247a12005-08-25 19:12:10 +00003891 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003892 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003893#ifndef NDEBUG
3894 VerifyNode(N);
3895#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003896 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003897}
3898
Dan Gohman475871a2008-07-27 21:46:04 +00003899SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003900 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList);
3901}
3902
3903SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
3904 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003905}
3906
Dan Gohman475871a2008-07-27 21:46:04 +00003907SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003908 SDValue N1) {
3909 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1);
3910}
3911
3912SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3913 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00003914 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003915 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00003916}
3917
Dan Gohman475871a2008-07-27 21:46:04 +00003918SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3919 SDValue N1, SDValue N2) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003920 return getNode(Opcode, DebugLoc::getUnknownLoc(), VTList, N1, N2);
3921}
3922
3923SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3924 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00003925 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003926 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00003927}
3928
Bill Wendling7ade28c2009-01-28 22:17:52 +00003929SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3930 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00003931 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003932 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00003933}
3934
Bill Wendling7ade28c2009-01-28 22:17:52 +00003935SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3936 SDValue N1, SDValue N2, SDValue N3,
3937 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003938 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003939 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00003940}
3941
Bill Wendling7ade28c2009-01-28 22:17:52 +00003942SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3943 SDValue N1, SDValue N2, SDValue N3,
3944 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003945 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003946 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00003947}
3948
Duncan Sands83ec4b62008-06-06 12:08:01 +00003949SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003950 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003951}
3952
Duncan Sands83ec4b62008-06-06 12:08:01 +00003953SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003954 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3955 E = VTList.rend(); I != E; ++I)
3956 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3957 return *I;
3958
3959 MVT *Array = Allocator.Allocate<MVT>(2);
3960 Array[0] = VT1;
3961 Array[1] = VT2;
3962 SDVTList Result = makeVTList(Array, 2);
3963 VTList.push_back(Result);
3964 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003965}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003966
3967SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3968 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3969 E = VTList.rend(); I != E; ++I)
3970 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3971 I->VTs[2] == VT3)
3972 return *I;
3973
3974 MVT *Array = Allocator.Allocate<MVT>(3);
3975 Array[0] = VT1;
3976 Array[1] = VT2;
3977 Array[2] = VT3;
3978 SDVTList Result = makeVTList(Array, 3);
3979 VTList.push_back(Result);
3980 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003981}
3982
Bill Wendling13d6d442008-12-01 23:28:22 +00003983SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
3984 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3985 E = VTList.rend(); I != E; ++I)
3986 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3987 I->VTs[2] == VT3 && I->VTs[3] == VT4)
3988 return *I;
3989
3990 MVT *Array = Allocator.Allocate<MVT>(3);
3991 Array[0] = VT1;
3992 Array[1] = VT2;
3993 Array[2] = VT3;
3994 Array[3] = VT4;
3995 SDVTList Result = makeVTList(Array, 4);
3996 VTList.push_back(Result);
3997 return Result;
3998}
3999
Duncan Sands83ec4b62008-06-06 12:08:01 +00004000SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004001 switch (NumVTs) {
4002 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004003 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004004 case 2: return getVTList(VTs[0], VTs[1]);
4005 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
4006 default: break;
4007 }
4008
Dan Gohmane8be6c62008-07-17 19:10:17 +00004009 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4010 E = VTList.rend(); I != E; ++I) {
4011 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4012 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00004013
4014 bool NoMatch = false;
4015 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004016 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004017 NoMatch = true;
4018 break;
4019 }
4020 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004021 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004022 }
4023
Dan Gohmane8be6c62008-07-17 19:10:17 +00004024 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
4025 std::copy(VTs, VTs+NumVTs, Array);
4026 SDVTList Result = makeVTList(Array, NumVTs);
4027 VTList.push_back(Result);
4028 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004029}
4030
4031
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004032/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4033/// specified operands. If the resultant node already exists in the DAG,
4034/// this does not modify the specified node, instead it returns the node that
4035/// already exists. If the resultant node does not exist in the DAG, the
4036/// input node is returned. As a degenerate case, if you specify the same
4037/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00004038SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004039 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004040 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
4041
4042 // Check to see if there is no change.
4043 if (Op == N->getOperand(0)) return InN;
4044
4045 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004046 void *InsertPos = 0;
4047 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004048 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004049
Dan Gohman79acd2b2008-07-21 22:38:59 +00004050 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004051 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004052 if (!RemoveNodeFromCSEMaps(N))
4053 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004054
4055 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004056 N->OperandList[0].set(Op);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004057
4058 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004059 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004060 return InN;
4061}
4062
Dan Gohman475871a2008-07-27 21:46:04 +00004063SDValue SelectionDAG::
4064UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004065 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004066 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
4067
4068 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004069 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4070 return InN; // No operands changed, just return the input node.
4071
4072 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004073 void *InsertPos = 0;
4074 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004075 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004076
Dan Gohman79acd2b2008-07-21 22:38:59 +00004077 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004078 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004079 if (!RemoveNodeFromCSEMaps(N))
4080 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004081
4082 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004083 if (N->OperandList[0] != Op1)
4084 N->OperandList[0].set(Op1);
4085 if (N->OperandList[1] != Op2)
4086 N->OperandList[1].set(Op2);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004087
4088 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004089 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004090 return InN;
4091}
4092
Dan Gohman475871a2008-07-27 21:46:04 +00004093SDValue SelectionDAG::
4094UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4095 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004096 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004097}
4098
Dan Gohman475871a2008-07-27 21:46:04 +00004099SDValue SelectionDAG::
4100UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4101 SDValue Op3, SDValue Op4) {
4102 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004103 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004104}
4105
Dan Gohman475871a2008-07-27 21:46:04 +00004106SDValue SelectionDAG::
4107UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4108 SDValue Op3, SDValue Op4, SDValue Op5) {
4109 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004110 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004111}
4112
Dan Gohman475871a2008-07-27 21:46:04 +00004113SDValue SelectionDAG::
4114UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004115 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004116 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004117 "Update with wrong number of operands");
4118
4119 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004120 bool AnyChange = false;
4121 for (unsigned i = 0; i != NumOps; ++i) {
4122 if (Ops[i] != N->getOperand(i)) {
4123 AnyChange = true;
4124 break;
4125 }
4126 }
4127
4128 // No operands changed, just return the input node.
4129 if (!AnyChange) return InN;
4130
4131 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004132 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004133 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004134 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004135
Dan Gohman7ceda162008-05-02 00:05:03 +00004136 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004137 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004138 if (!RemoveNodeFromCSEMaps(N))
4139 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004140
4141 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004142 for (unsigned i = 0; i != NumOps; ++i)
4143 if (N->OperandList[i] != Ops[i])
4144 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004145
4146 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004147 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004148 return InN;
4149}
4150
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004151/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004152/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004153void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004154 // Unlike the code in MorphNodeTo that does this, we don't need to
4155 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004156 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4157 SDUse &Use = *I++;
4158 Use.set(SDValue());
4159 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004160}
Chris Lattner149c58c2005-08-16 18:17:10 +00004161
Dan Gohmane8be6c62008-07-17 19:10:17 +00004162/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4163/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004164///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004165SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004166 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004167 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004168 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004169}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004170
Dan Gohmane8be6c62008-07-17 19:10:17 +00004171SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004172 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004173 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004174 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004175 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004176}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004177
Dan Gohmane8be6c62008-07-17 19:10:17 +00004178SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004179 MVT VT, SDValue Op1,
4180 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004181 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004182 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004183 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004184}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004185
Dan Gohmane8be6c62008-07-17 19:10:17 +00004186SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004187 MVT VT, SDValue Op1,
4188 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004189 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004190 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004191 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004192}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004193
Dan Gohmane8be6c62008-07-17 19:10:17 +00004194SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004195 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004196 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004197 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004198 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004199}
4200
Dan Gohmane8be6c62008-07-17 19:10:17 +00004201SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004202 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004203 unsigned NumOps) {
4204 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004205 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004206}
4207
Dan Gohmane8be6c62008-07-17 19:10:17 +00004208SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004209 MVT VT1, MVT VT2) {
4210 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004211 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004212}
4213
Dan Gohmane8be6c62008-07-17 19:10:17 +00004214SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004215 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004216 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004217 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004218 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004219}
4220
Bill Wendling13d6d442008-12-01 23:28:22 +00004221SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
4222 MVT VT1, MVT VT2, MVT VT3, MVT VT4,
4223 const SDValue *Ops, unsigned NumOps) {
4224 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4225 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4226}
4227
Dan Gohmane8be6c62008-07-17 19:10:17 +00004228SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004229 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004230 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004231 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004232 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004233 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004234}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004235
Dan Gohmane8be6c62008-07-17 19:10:17 +00004236SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004237 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004238 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004239 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004240 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004241 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004242}
4243
Dan Gohmane8be6c62008-07-17 19:10:17 +00004244SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004245 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004246 SDValue Op1, SDValue Op2,
4247 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004248 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004249 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004250 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004251}
4252
Dan Gohmane8be6c62008-07-17 19:10:17 +00004253SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Bill Wendling13d6d442008-12-01 23:28:22 +00004254 MVT VT1, MVT VT2, MVT VT3,
4255 SDValue Op1, SDValue Op2,
4256 SDValue Op3) {
4257 SDVTList VTs = getVTList(VT1, VT2, VT3);
4258 SDValue Ops[] = { Op1, Op2, Op3 };
4259 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4260}
4261
4262SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004263 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004264 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004265 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4266}
4267
4268SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4269 MVT VT) {
4270 SDVTList VTs = getVTList(VT);
4271 return MorphNodeTo(N, Opc, VTs, 0, 0);
4272}
4273
4274SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004275 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004276 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004277 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004278 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4279}
4280
4281SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004282 MVT VT, SDValue Op1,
4283 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004284 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004285 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004286 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4287}
4288
4289SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004290 MVT VT, SDValue Op1,
4291 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004292 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004293 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004294 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4295}
4296
4297SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004298 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004299 unsigned NumOps) {
4300 SDVTList VTs = getVTList(VT);
4301 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4302}
4303
4304SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004305 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004306 unsigned NumOps) {
4307 SDVTList VTs = getVTList(VT1, VT2);
4308 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4309}
4310
4311SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4312 MVT VT1, MVT VT2) {
4313 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004314 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004315}
4316
4317SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4318 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004319 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004320 SDVTList VTs = getVTList(VT1, VT2, VT3);
4321 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4322}
4323
4324SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4325 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004326 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004327 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004328 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004329 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4330}
4331
4332SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4333 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004334 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004335 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004336 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004337 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4338}
4339
4340SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4341 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004342 SDValue Op1, SDValue Op2,
4343 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004344 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004345 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004346 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4347}
4348
4349/// MorphNodeTo - These *mutate* the specified node to have the specified
4350/// return type, opcode, and operands.
4351///
4352/// Note that MorphNodeTo returns the resultant node. If there is already a
4353/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00004354/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004355///
4356/// Using MorphNodeTo is faster than creating a new node and swapping it in
4357/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004358/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004359/// the node's users.
4360///
4361SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004362 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004363 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004364 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004365 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004366 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4367 FoldingSetNodeID ID;
4368 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4369 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4370 return ON;
4371 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004372
Dan Gohman095cc292008-09-13 01:54:27 +00004373 if (!RemoveNodeFromCSEMaps(N))
4374 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004375
Dan Gohmane8be6c62008-07-17 19:10:17 +00004376 // Start the morphing.
4377 N->NodeType = Opc;
4378 N->ValueList = VTs.VTs;
4379 N->NumValues = VTs.NumVTs;
4380
4381 // Clear the operands list, updating used nodes to remove this from their
4382 // use list. Keep track of any operands that become dead as a result.
4383 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004384 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4385 SDUse &Use = *I++;
4386 SDNode *Used = Use.getNode();
4387 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004388 if (Used->use_empty())
4389 DeadNodeSet.insert(Used);
4390 }
4391
4392 // If NumOps is larger than the # of operands we currently have, reallocate
4393 // the operand list.
4394 if (NumOps > N->NumOperands) {
4395 if (N->OperandsNeedDelete)
4396 delete[] N->OperandList;
Bill Wendlinga1dc6022008-10-19 20:51:12 +00004397
Dan Gohmane8be6c62008-07-17 19:10:17 +00004398 if (N->isMachineOpcode()) {
4399 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004400 // remainder of the current SelectionDAG iteration, so we can allocate
4401 // the operands directly out of a pool with no recycling metadata.
4402 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004403 N->OperandsNeedDelete = false;
4404 } else {
4405 N->OperandList = new SDUse[NumOps];
4406 N->OperandsNeedDelete = true;
4407 }
4408 }
4409
4410 // Assign the new operands.
4411 N->NumOperands = NumOps;
4412 for (unsigned i = 0, e = NumOps; i != e; ++i) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004413 N->OperandList[i].setUser(N);
Dan Gohmane7852d02009-01-26 04:35:06 +00004414 N->OperandList[i].setInitial(Ops[i]);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004415 }
4416
4417 // Delete any nodes that are still dead after adding the uses for the
4418 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004419 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004420 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4421 E = DeadNodeSet.end(); I != E; ++I)
4422 if ((*I)->use_empty())
4423 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004424 RemoveDeadNodes(DeadNodes);
4425
Dan Gohmane8be6c62008-07-17 19:10:17 +00004426 if (IP)
4427 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004428 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004429}
4430
Chris Lattner0fb094f2005-11-19 01:44:53 +00004431
Evan Cheng6ae46c42006-02-09 07:15:23 +00004432/// getTargetNode - These are used for target selectors to create a new node
4433/// with specified return type(s), target opcode, and operands.
4434///
4435/// Note that getTargetNode returns the resultant node. If there is already a
4436/// node of the specified opcode and operands, it returns that node instead of
4437/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004438SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004439 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004440}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004441SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT) {
4442 return getNode(~Opcode, dl, VT).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004443}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004444
Dan Gohman475871a2008-07-27 21:46:04 +00004445SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004446 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004447}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004448SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4449 SDValue Op1) {
4450 return getNode(~Opcode, dl, VT, Op1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004451}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004452
Duncan Sands83ec4b62008-06-06 12:08:01 +00004453SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004454 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004455 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004456}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004457SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004458 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004459 return getNode(~Opcode, dl, VT, Op1, Op2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004460}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004461
Duncan Sands83ec4b62008-06-06 12:08:01 +00004462SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004463 SDValue Op1, SDValue Op2,
4464 SDValue Op3) {
4465 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004466}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004467SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4468 SDValue Op1, SDValue Op2,
4469 SDValue Op3) {
4470 return getNode(~Opcode, dl, VT, Op1, Op2, Op3).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004471}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004472
Duncan Sands83ec4b62008-06-06 12:08:01 +00004473SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004474 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004475 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004476}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004477SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004478 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004479 return getNode(~Opcode, dl, VT, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004480}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004481
4482SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4483 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4484 SDValue Op;
4485 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004486}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004487SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004488 MVT VT1, MVT VT2) {
4489 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4490 SDValue Op;
Bill Wendling56ab1a22009-01-29 09:01:55 +00004491 return getNode(~Opcode, dl, VTs, 2, &Op, 0).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004492}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004493
Duncan Sands83ec4b62008-06-06 12:08:01 +00004494SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004495 MVT VT2, SDValue Op1) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004496 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4497 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004498}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004499SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
Dale Johannesene8c17332009-01-29 00:47:48 +00004500 MVT VT2, SDValue Op1) {
4501 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004502 return getNode(~Opcode, dl, VTs, 2, &Op1, 1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004503}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004504
Duncan Sands83ec4b62008-06-06 12:08:01 +00004505SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004506 MVT VT2, SDValue Op1,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004507 SDValue Op2) {
4508 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4509 SDValue Ops[] = { Op1, Op2 };
4510 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Bill Wendling6e1bb382009-01-29 05:27:31 +00004511}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004512SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4513 MVT VT2, SDValue Op1,
4514 SDValue Op2) {
4515 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4516 SDValue Ops[] = { Op1, Op2 };
4517 return getNode(~Opcode, dl, VTs, 2, Ops, 2).getNode();
4518}
4519
4520SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Bill Wendling6e1bb382009-01-29 05:27:31 +00004521 MVT VT2, SDValue Op1,
4522 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004523 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004524 SDValue Ops[] = { Op1, Op2, Op3 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004525 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004526}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004527SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4528 MVT VT2, SDValue Op1,
4529 SDValue Op2, SDValue Op3) {
4530 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4531 SDValue Ops[] = { Op1, Op2, Op3 };
4532 return getNode(~Opcode, dl, VTs, 2, Ops, 3).getNode();
4533}
4534
4535SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004536 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004537 const MVT *VTs = getNodeValueTypes(VT1, VT2);
4538 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004539}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004540SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004541 MVT VT1, MVT VT2,
4542 const SDValue *Ops, unsigned NumOps) {
4543 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004544 return getNode(~Opcode, dl, VTs, 2, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004545}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004546
4547SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004548 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004549 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4550 SDValue Ops[] = { Op1, Op2 };
4551 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004552}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004553SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004554 MVT VT1, MVT VT2, MVT VT3,
4555 SDValue Op1, SDValue Op2) {
4556 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4557 SDValue Ops[] = { Op1, Op2 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004558 return getNode(~Opcode, dl, VTs, 3, Ops, 2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004559}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004560
4561SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
4562 SDValue Op1, SDValue Op2,
4563 SDValue Op3) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004564 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4565 SDValue Ops[] = { Op1, Op2, Op3 };
Bill Wendling56ab1a22009-01-29 09:01:55 +00004566 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004567}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004568SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Bill Wendling6e1bb382009-01-29 05:27:31 +00004569 MVT VT1, MVT VT2, MVT VT3,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004570 SDValue Op1, SDValue Op2,
4571 SDValue Op3) {
4572 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4573 SDValue Ops[] = { Op1, Op2, Op3 };
4574 return getNode(~Opcode, dl, VTs, 3, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004575}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004576
4577SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
4578 const SDValue *Ops, unsigned NumOps) {
4579 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
4580 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
4581}
4582SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004583 MVT VT1, MVT VT2, MVT VT3,
4584 const SDValue *Ops, unsigned NumOps) {
4585 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dale Johannesendbfd8db2009-02-03 01:55:44 +00004586 return getNode(~Opcode, dl, VTs, 3, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004587}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004588
4589SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4590 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004591 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004592 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004593 VTList.push_back(VT1);
4594 VTList.push_back(VT2);
4595 VTList.push_back(VT3);
4596 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004597 const MVT *VTs = getNodeValueTypes(VTList);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004598 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004599}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004600SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4601 MVT VT2, MVT VT3, MVT VT4,
4602 const SDValue *Ops, unsigned NumOps) {
4603 std::vector<MVT> VTList;
4604 VTList.push_back(VT1);
4605 VTList.push_back(VT2);
4606 VTList.push_back(VT3);
4607 VTList.push_back(VT4);
4608 const MVT *VTs = getNodeValueTypes(VTList);
4609 return getNode(~Opcode, dl, VTs, 4, Ops, NumOps).getNode();
4610}
4611
Evan Cheng39305cf2007-10-05 01:10:49 +00004612SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004613 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004614 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004615 const MVT *VTs = getNodeValueTypes(ResultTys);
4616 return getNode(~Opcode, VTs, ResultTys.size(),
4617 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004618}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004619SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004620 const std::vector<MVT> &ResultTys,
4621 const SDValue *Ops, unsigned NumOps) {
4622 const MVT *VTs = getNodeValueTypes(ResultTys);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004623 return getNode(~Opcode, dl, VTs, ResultTys.size(),
Dale Johannesene8c17332009-01-29 00:47:48 +00004624 Ops, NumOps).getNode();
4625}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004626
Evan Cheng08b11732008-03-22 01:55:50 +00004627/// getNodeIfExists - Get the specified node if it's already available, or
4628/// else return NULL.
4629SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004630 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004631 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4632 FoldingSetNodeID ID;
4633 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4634 void *IP = 0;
4635 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4636 return E;
4637 }
4638 return NULL;
4639}
4640
Evan Cheng99157a02006-08-07 22:13:29 +00004641/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004642/// This can cause recursive merging of nodes in the DAG.
4643///
Chris Lattner11d049c2008-02-03 03:35:22 +00004644/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004645///
Dan Gohman475871a2008-07-27 21:46:04 +00004646void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004647 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004648 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004649 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004650 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004651 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004652
Dan Gohman39946102009-01-25 16:29:12 +00004653 // Iterate over all the existing uses of From. New uses will be added
4654 // to the beginning of the use list, which we avoid visiting.
4655 // This specifically avoids visiting uses of From that arise while the
4656 // replacement is happening, because any such uses would be the result
4657 // of CSE: If an existing node looks like From after one of its operands
4658 // is replaced by To, we don't want to replace of all its users with To
4659 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00004660 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4661 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004662 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004663
Chris Lattner8b8749f2005-08-17 19:00:20 +00004664 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004665 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00004666
Dan Gohman39946102009-01-25 16:29:12 +00004667 // A user can appear in a use list multiple times, and when this
4668 // happens the uses are usually next to each other in the list.
4669 // To help reduce the number of CSE recomputations, process all
4670 // the uses of this user that we can find this way.
4671 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004672 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004673 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004674 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00004675 } while (UI != UE && *UI == User);
4676
4677 // Now that we have modified User, add it back to the CSE maps. If it
4678 // already exists there, recursively merge the results together.
4679 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00004680 }
4681}
4682
4683/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4684/// This can cause recursive merging of nodes in the DAG.
4685///
4686/// This version assumes From/To have matching types and numbers of result
4687/// values.
4688///
Chris Lattner1e111c72005-09-07 05:37:01 +00004689void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004690 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004691 assert(From->getVTList().VTs == To->getVTList().VTs &&
4692 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004693 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004694
4695 // Handle the trivial case.
4696 if (From == To)
4697 return;
4698
Dan Gohmandbe664a2009-01-19 21:44:21 +00004699 // Iterate over just the existing users of From. See the comments in
4700 // the ReplaceAllUsesWith above.
4701 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4702 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004703 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004704
Chris Lattner8b52f212005-08-26 18:36:28 +00004705 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004706 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004707
Dan Gohman39946102009-01-25 16:29:12 +00004708 // A user can appear in a use list multiple times, and when this
4709 // happens the uses are usually next to each other in the list.
4710 // To help reduce the number of CSE recomputations, process all
4711 // the uses of this user that we can find this way.
4712 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004713 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004714 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004715 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00004716 } while (UI != UE && *UI == User);
4717
4718 // Now that we have modified User, add it back to the CSE maps. If it
4719 // already exists there, recursively merge the results together.
4720 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004721 }
4722}
4723
Chris Lattner8b52f212005-08-26 18:36:28 +00004724/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4725/// This can cause recursive merging of nodes in the DAG.
4726///
4727/// This version can replace From with any result values. To must match the
4728/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004729void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004730 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004731 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004732 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004733 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004734
Dan Gohmandbe664a2009-01-19 21:44:21 +00004735 // Iterate over just the existing users of From. See the comments in
4736 // the ReplaceAllUsesWith above.
4737 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4738 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004739 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004740
Chris Lattner7b2880c2005-08-24 22:44:39 +00004741 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004742 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004743
Dan Gohman39946102009-01-25 16:29:12 +00004744 // A user can appear in a use list multiple times, and when this
4745 // happens the uses are usually next to each other in the list.
4746 // To help reduce the number of CSE recomputations, process all
4747 // the uses of this user that we can find this way.
4748 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004749 SDUse &Use = UI.getUse();
4750 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00004751 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004752 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00004753 } while (UI != UE && *UI == User);
4754
4755 // Now that we have modified User, add it back to the CSE maps. If it
4756 // already exists there, recursively merge the results together.
4757 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004758 }
4759}
4760
Chris Lattner012f2412006-02-17 21:58:01 +00004761/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00004762/// uses of other values produced by From.getNode() alone. The Deleted
4763/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004764void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004765 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004766 // Handle the really simple, really trivial case efficiently.
4767 if (From == To) return;
4768
Chris Lattner012f2412006-02-17 21:58:01 +00004769 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004770 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004771 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004772 return;
4773 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004774
Dan Gohman39946102009-01-25 16:29:12 +00004775 // Iterate over just the existing users of From. See the comments in
4776 // the ReplaceAllUsesWith above.
4777 SDNode::use_iterator UI = From.getNode()->use_begin(),
4778 UE = From.getNode()->use_end();
4779 while (UI != UE) {
4780 SDNode *User = *UI;
4781 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00004782
Dan Gohman39946102009-01-25 16:29:12 +00004783 // A user can appear in a use list multiple times, and when this
4784 // happens the uses are usually next to each other in the list.
4785 // To help reduce the number of CSE recomputations, process all
4786 // the uses of this user that we can find this way.
4787 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004788 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004789
4790 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00004791 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00004792 ++UI;
4793 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00004794 }
Dan Gohman39946102009-01-25 16:29:12 +00004795
4796 // If this node hasn't been modified yet, it's still in the CSE maps,
4797 // so remove its old self from the CSE maps.
4798 if (!UserRemovedFromCSEMaps) {
4799 RemoveNodeFromCSEMaps(User);
4800 UserRemovedFromCSEMaps = true;
4801 }
4802
4803 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004804 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00004805 } while (UI != UE && *UI == User);
4806
4807 // We are iterating over all uses of the From node, so if a use
4808 // doesn't use the specific value, no changes are made.
4809 if (!UserRemovedFromCSEMaps)
4810 continue;
4811
Chris Lattner01d029b2007-10-15 06:10:22 +00004812 // Now that we have modified User, add it back to the CSE maps. If it
4813 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00004814 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004815 }
4816}
4817
Dan Gohman39946102009-01-25 16:29:12 +00004818namespace {
4819 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
4820 /// to record information about a use.
4821 struct UseMemo {
4822 SDNode *User;
4823 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00004824 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00004825 };
Dan Gohmane7852d02009-01-26 04:35:06 +00004826
4827 /// operator< - Sort Memos by User.
4828 bool operator<(const UseMemo &L, const UseMemo &R) {
4829 return (intptr_t)L.User < (intptr_t)R.User;
4830 }
Dan Gohman39946102009-01-25 16:29:12 +00004831}
4832
Dan Gohmane8be6c62008-07-17 19:10:17 +00004833/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00004834/// uses of other values produced by From.getNode() alone. The same value
4835/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00004836/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004837void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4838 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004839 unsigned Num,
4840 DAGUpdateListener *UpdateListener){
4841 // Handle the simple, trivial case efficiently.
4842 if (Num == 1)
4843 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4844
Dan Gohman39946102009-01-25 16:29:12 +00004845 // Read up all the uses and make records of them. This helps
4846 // processing new uses that are introduced during the
4847 // replacement process.
4848 SmallVector<UseMemo, 4> Uses;
4849 for (unsigned i = 0; i != Num; ++i) {
4850 unsigned FromResNo = From[i].getResNo();
4851 SDNode *FromNode = From[i].getNode();
4852 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00004853 E = FromNode->use_end(); UI != E; ++UI) {
4854 SDUse &Use = UI.getUse();
4855 if (Use.getResNo() == FromResNo) {
4856 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00004857 Uses.push_back(Memo);
4858 }
Dan Gohmane7852d02009-01-26 04:35:06 +00004859 }
Dan Gohman39946102009-01-25 16:29:12 +00004860 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004861
Dan Gohmane7852d02009-01-26 04:35:06 +00004862 // Sort the uses, so that all the uses from a given User are together.
4863 std::sort(Uses.begin(), Uses.end());
4864
Dan Gohman39946102009-01-25 16:29:12 +00004865 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
4866 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004867 // We know that this user uses some value of From. If it is the right
4868 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00004869 SDNode *User = Uses[UseIndex].User;
4870
4871 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004872 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00004873
Dan Gohmane7852d02009-01-26 04:35:06 +00004874 // The Uses array is sorted, so all the uses for a given User
4875 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00004876 // To help reduce the number of CSE recomputations, process all
4877 // the uses of this user that we can find this way.
4878 do {
4879 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00004880 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00004881 ++UseIndex;
4882
Dan Gohmane7852d02009-01-26 04:35:06 +00004883 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00004884 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
4885
Dan Gohmane8be6c62008-07-17 19:10:17 +00004886 // Now that we have modified User, add it back to the CSE maps. If it
4887 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00004888 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004889 }
4890}
4891
Evan Chenge6f35d82006-08-01 08:20:41 +00004892/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004893/// based on their topological order. It returns the maximum id and a vector
4894/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00004895unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00004896
Dan Gohmanf06c8352008-09-30 18:30:35 +00004897 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004898
Dan Gohmanf06c8352008-09-30 18:30:35 +00004899 // SortedPos tracks the progress of the algorithm. Nodes before it are
4900 // sorted, nodes after it are unsorted. When the algorithm completes
4901 // it is at the end of the list.
4902 allnodes_iterator SortedPos = allnodes_begin();
4903
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004904 // Visit all the nodes. Move nodes with no operands to the front of
4905 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00004906 // operand count. Before we do this, the Node Id fields of the nodes
4907 // may contain arbitrary values. After, the Node Id fields for nodes
4908 // before SortedPos will contain the topological sort index, and the
4909 // Node Id fields for nodes At SortedPos and after will contain the
4910 // count of outstanding operands.
4911 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
4912 SDNode *N = I++;
4913 unsigned Degree = N->getNumOperands();
4914 if (Degree == 0) {
4915 // A node with no uses, add it to the result array immediately.
4916 N->setNodeId(DAGSize++);
4917 allnodes_iterator Q = N;
4918 if (Q != SortedPos)
4919 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
4920 ++SortedPos;
4921 } else {
4922 // Temporarily use the Node Id as scratch space for the degree count.
4923 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004924 }
4925 }
4926
Dan Gohmanf06c8352008-09-30 18:30:35 +00004927 // Visit all the nodes. As we iterate, moves nodes into sorted order,
4928 // such that by the time the end is reached all nodes will be sorted.
4929 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
4930 SDNode *N = I;
4931 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4932 UI != UE; ++UI) {
4933 SDNode *P = *UI;
4934 unsigned Degree = P->getNodeId();
4935 --Degree;
4936 if (Degree == 0) {
4937 // All of P's operands are sorted, so P may sorted now.
4938 P->setNodeId(DAGSize++);
4939 if (P != SortedPos)
4940 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
4941 ++SortedPos;
4942 } else {
4943 // Update P's outstanding operand count.
4944 P->setNodeId(Degree);
4945 }
4946 }
4947 }
4948
4949 assert(SortedPos == AllNodes.end() &&
4950 "Topological sort incomplete!");
4951 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
4952 "First node in topological sort is not the entry token!");
4953 assert(AllNodes.front().getNodeId() == 0 &&
4954 "First node in topological sort has non-zero id!");
4955 assert(AllNodes.front().getNumOperands() == 0 &&
4956 "First node in topological sort has operands!");
4957 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
4958 "Last node in topologic sort has unexpected id!");
4959 assert(AllNodes.back().use_empty() &&
4960 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004961 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00004962 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00004963}
4964
4965
Evan Cheng091cba12006-07-27 06:39:06 +00004966
Jim Laskey58b968b2005-08-17 20:08:02 +00004967//===----------------------------------------------------------------------===//
4968// SDNode Class
4969//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004970
Chris Lattner48b85922007-02-04 02:41:42 +00004971HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004972 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004973}
4974
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004975GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Dan Gohman6520e202008-10-18 02:06:02 +00004976 MVT VT, int64_t o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004977 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004978 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004979 // Thread Local
4980 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4981 // Non Thread Local
4982 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4983 getSDVTList(VT)), Offset(o) {
4984 TheGlobal = const_cast<GlobalValue*>(GA);
4985}
Chris Lattner48b85922007-02-04 02:41:42 +00004986
Dan Gohman1ea58a52008-07-09 22:08:04 +00004987MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004988 const Value *srcValue, int SVO,
4989 unsigned alignment, bool vol)
Dan Gohmana7ce7412009-02-03 00:08:45 +00004990 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
4991 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dan Gohman492f2762008-07-09 21:23:02 +00004992 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4993 assert(getAlignment() == alignment && "Alignment representation error!");
4994 assert(isVolatile() == vol && "Volatile representation error!");
4995}
4996
Mon P Wangc4d10212008-10-17 18:22:58 +00004997MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops,
4998 unsigned NumOps, MVT memvt, const Value *srcValue,
4999 int SVO, unsigned alignment, bool vol)
5000 : SDNode(Opc, VTs, Ops, NumOps),
Dan Gohmana7ce7412009-02-03 00:08:45 +00005001 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5002 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Mon P Wangc4d10212008-10-17 18:22:58 +00005003 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5004 assert(getAlignment() == alignment && "Alignment representation error!");
5005 assert(isVolatile() == vol && "Volatile representation error!");
5006}
5007
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005008MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, MVT memvt,
5009 const Value *srcValue, int SVO,
5010 unsigned alignment, bool vol)
Dan Gohmana7ce7412009-02-03 00:08:45 +00005011 : SDNode(Opc, dl, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5012 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005013 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5014 assert(getAlignment() == alignment && "Alignment representation error!");
5015 assert(isVolatile() == vol && "Volatile representation error!");
5016}
5017
5018MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
5019 const SDValue *Ops,
5020 unsigned NumOps, MVT memvt, const Value *srcValue,
5021 int SVO, unsigned alignment, bool vol)
5022 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmana7ce7412009-02-03 00:08:45 +00005023 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
5024 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005025 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
5026 assert(getAlignment() == alignment && "Alignment representation error!");
5027 assert(isVolatile() == vol && "Volatile representation error!");
5028}
5029
Dan Gohman36b5c132008-04-07 19:35:22 +00005030/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00005031/// reference performed by this memory reference.
5032MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesenfea90882008-10-24 01:06:58 +00005033 int Flags = 0;
Dan Gohman1ea58a52008-07-09 22:08:04 +00005034 if (isa<LoadSDNode>(this))
5035 Flags = MachineMemOperand::MOLoad;
5036 else if (isa<StoreSDNode>(this))
5037 Flags = MachineMemOperand::MOStore;
Mon P Wangc4d10212008-10-17 18:22:58 +00005038 else if (isa<AtomicSDNode>(this)) {
Dan Gohman1ea58a52008-07-09 22:08:04 +00005039 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
5040 }
Mon P Wangc4d10212008-10-17 18:22:58 +00005041 else {
5042 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
5043 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
5044 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
5045 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
5046 }
Dan Gohman1ea58a52008-07-09 22:08:04 +00005047
5048 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00005049 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
5050
Dan Gohman1ea58a52008-07-09 22:08:04 +00005051 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00005052 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00005053 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00005054 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00005055 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
5056 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00005057 else
5058 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
5059 Size, getAlignment());
5060}
5061
Jim Laskey583bd472006-10-27 23:46:08 +00005062/// Profile - Gather unique data for the node.
5063///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005064void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005065 AddNodeIDNode(ID, this);
5066}
5067
Chris Lattnera3255112005-11-08 23:30:28 +00005068/// getValueTypeList - Return a pointer to the specified value type.
5069///
Duncan Sands83ec4b62008-06-06 12:08:01 +00005070const MVT *SDNode::getValueTypeList(MVT VT) {
5071 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005072 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00005073 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005074 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005075 static MVT VTs[MVT::LAST_VALUETYPE];
5076 VTs[VT.getSimpleVT()] = VT;
5077 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005078 }
Chris Lattnera3255112005-11-08 23:30:28 +00005079}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005080
Chris Lattner5c884562005-01-12 18:37:47 +00005081/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5082/// indicated value. This method ignores uses of other values defined by this
5083/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005084bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005085 assert(Value < getNumValues() && "Bad value!");
5086
Roman Levensteindc1adac2008-04-07 10:06:32 +00005087 // TODO: Only iterate over uses of a given value of the node
5088 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005089 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005090 if (NUses == 0)
5091 return false;
5092 --NUses;
5093 }
Chris Lattner5c884562005-01-12 18:37:47 +00005094 }
5095
5096 // Found exactly the right number of uses?
5097 return NUses == 0;
5098}
5099
5100
Evan Cheng33d55952007-08-02 05:29:38 +00005101/// hasAnyUseOfValue - Return true if there are any use of the indicated
5102/// value. This method ignores uses of other values defined by this operation.
5103bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5104 assert(Value < getNumValues() && "Bad value!");
5105
Dan Gohman1373c1c2008-07-09 22:39:01 +00005106 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005107 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005108 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005109
5110 return false;
5111}
5112
5113
Dan Gohman2a629952008-07-27 18:06:42 +00005114/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005115///
Dan Gohman2a629952008-07-27 18:06:42 +00005116bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005117 bool Seen = false;
5118 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005119 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005120 if (User == this)
5121 Seen = true;
5122 else
5123 return false;
5124 }
5125
5126 return Seen;
5127}
5128
Evan Chenge6e97e62006-11-03 07:31:32 +00005129/// isOperand - Return true if this node is an operand of N.
5130///
Dan Gohman475871a2008-07-27 21:46:04 +00005131bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005132 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5133 if (*this == N->getOperand(i))
5134 return true;
5135 return false;
5136}
5137
Evan Cheng917be682008-03-04 00:41:45 +00005138bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005139 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005140 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005141 return true;
5142 return false;
5143}
Evan Cheng4ee62112006-02-05 06:29:23 +00005144
Chris Lattner572dee72008-01-16 05:49:24 +00005145/// reachesChainWithoutSideEffects - Return true if this operand (which must
5146/// be a chain) reaches the specified operand without crossing any
5147/// side-effecting instructions. In practice, this looks through token
5148/// factors and non-volatile loads. In order to remain efficient, this only
5149/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00005150bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005151 unsigned Depth) const {
5152 if (*this == Dest) return true;
5153
5154 // Don't search too deeply, we just want to be able to see through
5155 // TokenFactor's etc.
5156 if (Depth == 0) return false;
5157
5158 // If this is a token factor, all inputs to the TF happen in parallel. If any
5159 // of the operands of the TF reach dest, then we can do the xform.
5160 if (getOpcode() == ISD::TokenFactor) {
5161 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5162 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5163 return true;
5164 return false;
5165 }
5166
5167 // Loads don't have side effects, look through them.
5168 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5169 if (!Ld->isVolatile())
5170 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5171 }
5172 return false;
5173}
5174
5175
Evan Chengc5fc57d2006-11-03 03:05:24 +00005176static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005177 SmallPtrSet<SDNode *, 32> &Visited) {
5178 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00005179 return;
5180
5181 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005182 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00005183 if (Op == P) {
5184 found = true;
5185 return;
5186 }
5187 findPredecessor(Op, P, found, Visited);
5188 }
5189}
5190
Evan Cheng917be682008-03-04 00:41:45 +00005191/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00005192/// is either an operand of N or it can be reached by recursively traversing
5193/// up the operands.
5194/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00005195bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005196 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005197 bool found = false;
5198 findPredecessor(N, this, found, Visited);
5199 return found;
5200}
5201
Evan Chengc5484282006-10-04 00:56:09 +00005202uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5203 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005204 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005205}
5206
Reid Spencer577cc322007-04-01 07:32:19 +00005207std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005208 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005209 default:
5210 if (getOpcode() < ISD::BUILTIN_OP_END)
5211 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00005212 if (isMachineOpcode()) {
5213 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005214 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00005215 if (getMachineOpcode() < TII->getNumOpcodes())
5216 return TII->get(getMachineOpcode()).getName();
5217 return "<<Unknown Machine Node>>";
5218 }
5219 if (G) {
Dan Gohmane9530ec2009-01-15 16:58:17 +00005220 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmane8be6c62008-07-17 19:10:17 +00005221 const char *Name = TLI.getTargetNodeName(getOpcode());
5222 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00005223 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005224 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005225 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005226
Dan Gohmane8be6c62008-07-17 19:10:17 +00005227#ifndef NDEBUG
5228 case ISD::DELETED_NODE:
5229 return "<<Deleted Node!>>";
5230#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00005231 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00005232 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohman0b1d4a72008-12-23 21:37:04 +00005233 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5234 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5235 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5236 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5237 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5238 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5239 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5240 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5241 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5242 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5243 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5244 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Andrew Lenharth95762122005-03-31 21:24:06 +00005245 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00005246 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005247 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00005248 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005249 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00005250 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00005251 case ISD::AssertSext: return "AssertSext";
5252 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005253
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005254 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005255 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005256 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00005257 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005258
5259 case ISD::Constant: return "Constant";
5260 case ISD::ConstantFP: return "ConstantFP";
5261 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005262 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005263 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005264 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00005265 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00005266 case ISD::RETURNADDR: return "RETURNADDR";
5267 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005268 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00005269 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00005270 case ISD::EHSELECTION: return "EHSELECTION";
5271 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00005272 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005273 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00005274 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005275 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00005276 return Intrinsic::getName((Intrinsic::ID)IID);
5277 }
5278 case ISD::INTRINSIC_VOID:
5279 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005280 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00005281 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00005282 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00005283
Chris Lattnerb2827b02006-03-19 00:52:58 +00005284 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005285 case ISD::TargetConstant: return "TargetConstant";
5286 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005287 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005288 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005289 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005290 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00005291 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005292 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005293
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005294 case ISD::CopyToReg: return "CopyToReg";
5295 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005296 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00005297 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005298 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00005299 case ISD::DBG_LABEL: return "dbg_label";
5300 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00005301 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00005302 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00005303 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00005304 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005305
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005306 // Unary operators
5307 case ISD::FABS: return "fabs";
5308 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00005309 case ISD::FSQRT: return "fsqrt";
5310 case ISD::FSIN: return "fsin";
5311 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00005312 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00005313 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00005314 case ISD::FTRUNC: return "ftrunc";
5315 case ISD::FFLOOR: return "ffloor";
5316 case ISD::FCEIL: return "fceil";
5317 case ISD::FRINT: return "frint";
5318 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005319
5320 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005321 case ISD::ADD: return "add";
5322 case ISD::SUB: return "sub";
5323 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00005324 case ISD::MULHU: return "mulhu";
5325 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005326 case ISD::SDIV: return "sdiv";
5327 case ISD::UDIV: return "udiv";
5328 case ISD::SREM: return "srem";
5329 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00005330 case ISD::SMUL_LOHI: return "smul_lohi";
5331 case ISD::UMUL_LOHI: return "umul_lohi";
5332 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00005333 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005334 case ISD::AND: return "and";
5335 case ISD::OR: return "or";
5336 case ISD::XOR: return "xor";
5337 case ISD::SHL: return "shl";
5338 case ISD::SRA: return "sra";
5339 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00005340 case ISD::ROTL: return "rotl";
5341 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00005342 case ISD::FADD: return "fadd";
5343 case ISD::FSUB: return "fsub";
5344 case ISD::FMUL: return "fmul";
5345 case ISD::FDIV: return "fdiv";
5346 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00005347 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00005348 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005349
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005350 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005351 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005352 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005353 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005354 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005355 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005356 case ISD::CONCAT_VECTORS: return "concat_vectors";
5357 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005358 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005359 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00005360 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005361 case ISD::ADDC: return "addc";
5362 case ISD::ADDE: return "adde";
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005363 case ISD::SADDO: return "saddo";
5364 case ISD::UADDO: return "uaddo";
Bill Wendling74c37652008-12-09 22:08:41 +00005365 case ISD::SSUBO: return "ssubo";
5366 case ISD::USUBO: return "usubo";
5367 case ISD::SMULO: return "smulo";
5368 case ISD::UMULO: return "umulo";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005369 case ISD::SUBC: return "subc";
5370 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005371 case ISD::SHL_PARTS: return "shl_parts";
5372 case ISD::SRA_PARTS: return "sra_parts";
5373 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00005374
5375 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5376 case ISD::INSERT_SUBREG: return "insert_subreg";
5377
Chris Lattner7f644642005-04-28 21:44:03 +00005378 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005379 case ISD::SIGN_EXTEND: return "sign_extend";
5380 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005381 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005382 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005383 case ISD::TRUNCATE: return "truncate";
5384 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005385 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005386 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005387 case ISD::FP_EXTEND: return "fp_extend";
5388
5389 case ISD::SINT_TO_FP: return "sint_to_fp";
5390 case ISD::UINT_TO_FP: return "uint_to_fp";
5391 case ISD::FP_TO_SINT: return "fp_to_sint";
5392 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005393 case ISD::BIT_CONVERT: return "bit_convert";
Mon P Wang77cdf302008-11-10 20:54:11 +00005394
5395 case ISD::CONVERT_RNDSAT: {
5396 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
5397 default: assert(0 && "Unknown cvt code!");
5398 case ISD::CVT_FF: return "cvt_ff";
5399 case ISD::CVT_FS: return "cvt_fs";
5400 case ISD::CVT_FU: return "cvt_fu";
5401 case ISD::CVT_SF: return "cvt_sf";
5402 case ISD::CVT_UF: return "cvt_uf";
5403 case ISD::CVT_SS: return "cvt_ss";
5404 case ISD::CVT_SU: return "cvt_su";
5405 case ISD::CVT_US: return "cvt_us";
5406 case ISD::CVT_UU: return "cvt_uu";
5407 }
5408 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005409
5410 // Control flow instructions
5411 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005412 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005413 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005414 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005415 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005416 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005417 case ISD::CALLSEQ_START: return "callseq_start";
5418 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005419
5420 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005421 case ISD::LOAD: return "load";
5422 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005423 case ISD::VAARG: return "vaarg";
5424 case ISD::VACOPY: return "vacopy";
5425 case ISD::VAEND: return "vaend";
5426 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005427 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005428 case ISD::EXTRACT_ELEMENT: return "extract_element";
5429 case ISD::BUILD_PAIR: return "build_pair";
5430 case ISD::STACKSAVE: return "stacksave";
5431 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005432 case ISD::TRAP: return "trap";
5433
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005434 // Bit manipulation
5435 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005436 case ISD::CTPOP: return "ctpop";
5437 case ISD::CTTZ: return "cttz";
5438 case ISD::CTLZ: return "ctlz";
5439
Chris Lattner36ce6912005-11-29 06:21:05 +00005440 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005441 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005442 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005443
Duncan Sands36397f52007-07-27 12:58:54 +00005444 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005445 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005446
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005447 case ISD::CONDCODE:
5448 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005449 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005450 case ISD::SETOEQ: return "setoeq";
5451 case ISD::SETOGT: return "setogt";
5452 case ISD::SETOGE: return "setoge";
5453 case ISD::SETOLT: return "setolt";
5454 case ISD::SETOLE: return "setole";
5455 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005456
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005457 case ISD::SETO: return "seto";
5458 case ISD::SETUO: return "setuo";
5459 case ISD::SETUEQ: return "setue";
5460 case ISD::SETUGT: return "setugt";
5461 case ISD::SETUGE: return "setuge";
5462 case ISD::SETULT: return "setult";
5463 case ISD::SETULE: return "setule";
5464 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005465
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005466 case ISD::SETEQ: return "seteq";
5467 case ISD::SETGT: return "setgt";
5468 case ISD::SETGE: return "setge";
5469 case ISD::SETLT: return "setlt";
5470 case ISD::SETLE: return "setle";
5471 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005472 }
5473 }
5474}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005475
Evan Cheng144d8f02006-11-09 17:55:04 +00005476const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005477 switch (AM) {
5478 default:
5479 return "";
5480 case ISD::PRE_INC:
5481 return "<pre-inc>";
5482 case ISD::PRE_DEC:
5483 return "<pre-dec>";
5484 case ISD::POST_INC:
5485 return "<post-inc>";
5486 case ISD::POST_DEC:
5487 return "<post-dec>";
5488 }
5489}
5490
Duncan Sands276dcbd2008-03-21 09:14:45 +00005491std::string ISD::ArgFlagsTy::getArgFlagsString() {
5492 std::string S = "< ";
5493
5494 if (isZExt())
5495 S += "zext ";
5496 if (isSExt())
5497 S += "sext ";
5498 if (isInReg())
5499 S += "inreg ";
5500 if (isSRet())
5501 S += "sret ";
5502 if (isByVal())
5503 S += "byval ";
5504 if (isNest())
5505 S += "nest ";
5506 if (getByValAlign())
5507 S += "byval-align:" + utostr(getByValAlign()) + " ";
5508 if (getOrigAlign())
5509 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5510 if (getByValSize())
5511 S += "byval-size:" + utostr(getByValSize()) + " ";
5512 return S + ">";
5513}
5514
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005515void SDNode::dump() const { dump(0); }
5516void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005517 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005518 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005519}
5520
Stuart Hastings80d69772009-02-04 16:46:19 +00005521void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005522 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005523
5524 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005525 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005526 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005527 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005528 else
Chris Lattner944fac72008-08-23 22:23:09 +00005529 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005530 }
Chris Lattner944fac72008-08-23 22:23:09 +00005531 OS << " = " << getOperationName(G);
Stuart Hastings80d69772009-02-04 16:46:19 +00005532}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005533
Stuart Hastings80d69772009-02-04 16:46:19 +00005534void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
Evan Chengce254432007-12-11 02:08:35 +00005535 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005536 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005537 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005538 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005539 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005540 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005541 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005542 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005543 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005544 }
Chris Lattner944fac72008-08-23 22:23:09 +00005545 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005546 }
5547
Chris Lattnerc3aae252005-01-07 07:46:32 +00005548 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005549 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005550 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005551 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005552 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005553 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005554 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005555 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005556 OS << "<APFloat(";
Dale Johannesen23a98552008-10-09 23:00:39 +00005557 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005558 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005559 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005560 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005561 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohman668aff62008-10-18 18:22:42 +00005562 int64_t offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005563 OS << '<';
5564 WriteAsOperand(OS, GADN->getGlobal());
5565 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005566 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005567 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005568 else
Chris Lattner944fac72008-08-23 22:23:09 +00005569 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005570 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005571 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005572 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005573 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005574 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005575 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005576 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005577 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005578 else
Chris Lattner944fac72008-08-23 22:23:09 +00005579 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005580 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005581 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005582 else
Chris Lattner944fac72008-08-23 22:23:09 +00005583 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005584 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005585 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005586 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5587 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005588 OS << LBB->getName() << " ";
5589 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005590 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005591 if (G && R->getReg() &&
5592 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005593 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005594 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005595 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005596 }
Bill Wendling056292f2008-09-16 21:48:12 +00005597 } else if (const ExternalSymbolSDNode *ES =
5598 dyn_cast<ExternalSymbolSDNode>(this)) {
5599 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005600 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5601 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005602 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005603 else
Chris Lattner944fac72008-08-23 22:23:09 +00005604 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005605 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5606 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005607 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005608 else
Chris Lattner944fac72008-08-23 22:23:09 +00005609 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005610 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005611 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005612 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005613 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005614 }
5615 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005616 const Value *SrcValue = LD->getSrcValue();
5617 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005618 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005619 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005620 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005621 else
Chris Lattner944fac72008-08-23 22:23:09 +00005622 OS << "null";
5623 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005624
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005625 bool doExt = true;
5626 switch (LD->getExtensionType()) {
5627 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005628 case ISD::EXTLOAD: OS << " <anyext "; break;
5629 case ISD::SEXTLOAD: OS << " <sext "; break;
5630 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005631 }
5632 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005633 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005634
Evan Cheng144d8f02006-11-09 17:55:04 +00005635 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005636 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005637 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005638 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005639 OS << " <volatile>";
5640 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005641 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005642 const Value *SrcValue = ST->getSrcValue();
5643 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005644 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005645 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005646 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005647 else
Chris Lattner944fac72008-08-23 22:23:09 +00005648 OS << "null";
5649 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005650
5651 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005652 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005653
5654 const char *AM = getIndexedModeName(ST->getAddressingMode());
5655 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005656 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005657 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005658 OS << " <volatile>";
5659 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005660 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5661 const Value *SrcValue = AT->getSrcValue();
5662 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005663 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005664 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005665 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005666 else
Chris Lattner944fac72008-08-23 22:23:09 +00005667 OS << "null";
5668 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005669 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005670 OS << " <volatile>";
5671 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005672 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005673}
5674
Stuart Hastings80d69772009-02-04 16:46:19 +00005675void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5676 print_types(OS, G);
5677 OS << " ";
5678 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
5679 if (i) OS << ", ";
5680 OS << (void*)getOperand(i).getNode();
5681 if (unsigned RN = getOperand(i).getResNo())
5682 OS << ":" << RN;
5683 }
5684 print_details(OS, G);
5685}
5686
Chris Lattnerde202b32005-11-09 23:47:37 +00005687static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005688 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005689 if (N->getOperand(i).getNode()->hasOneUse())
5690 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005691 else
Bill Wendling832171c2006-12-07 20:04:42 +00005692 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005693 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005694
Chris Lattnerea946cd2005-01-09 20:38:33 +00005695
Bill Wendling832171c2006-12-07 20:04:42 +00005696 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005697 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005698}
5699
Chris Lattnerc3aae252005-01-07 07:46:32 +00005700void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005701 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005702
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005703 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5704 I != E; ++I) {
5705 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005706 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005707 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005708 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005709
Gabor Greifba36cb52008-08-28 21:40:38 +00005710 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005711
Bill Wendling832171c2006-12-07 20:04:42 +00005712 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005713}
5714
Stuart Hastings80d69772009-02-04 16:46:19 +00005715void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
5716 print_types(OS, G);
5717 print_details(OS, G);
5718}
5719
5720typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
Stuart Hastings4caa0422009-02-04 20:30:10 +00005721static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
5722 const SelectionDAG *G, VisitedSDNodeSet &once) {
Stuart Hastings80d69772009-02-04 16:46:19 +00005723 if (!once.insert(N)) // If we've been here before, return now.
5724 return;
5725 // Dump the current SDNode, but don't end the line yet.
5726 OS << std::string(indent, ' ');
5727 N->printr(OS, G);
5728 // Having printed this SDNode, walk the children:
5729 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5730 const SDNode *child = N->getOperand(i).getNode();
5731 if (i) OS << ",";
5732 OS << " ";
5733 if (child->getNumOperands() == 0) {
5734 // This child has no grandchildren; print it inline right here.
5735 child->printr(OS, G);
5736 once.insert(child);
5737 } else { // Just the address. FIXME: also print the child's opcode
5738 OS << (void*)child;
5739 if (unsigned RN = N->getOperand(i).getResNo())
5740 OS << ":" << RN;
5741 }
5742 }
5743 OS << "\n";
5744 // Dump children that have grandchildren on their own line(s).
5745 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5746 const SDNode *child = N->getOperand(i).getNode();
5747 DumpNodesr(OS, child, indent+2, G, once);
5748 }
5749}
5750
5751void SDNode::dumpr() const {
5752 VisitedSDNodeSet once;
5753 DumpNodesr(errs(), this, 0, 0, once);
5754 errs().flush();
5755}
5756
Evan Chengd6594ae2006-09-12 21:00:35 +00005757const Type *ConstantPoolSDNode::getType() const {
5758 if (isMachineConstantPoolEntry())
5759 return Val.MachineCPVal->getType();
5760 return Val.ConstVal->getType();
5761}