blob: 5c996a0b3e6e6ba3ee1a4694a4962ab2148f426a [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"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner944fac72008-08-23 22:23:09 +000032#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/raw_ostream.h"
Chris Lattner012f2412006-02-17 21:58:01 +000034#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000035#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000036#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000037#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000038#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000039#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000040#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Chris Lattner0b3e5252006-08-15 19:11:05 +000043/// makeVTList - Return an instance of the SDVTList struct initialized with the
44/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000045static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000046 SDVTList Res = {VTs, NumVTs};
47 return Res;
48}
49
Duncan Sands83ec4b62008-06-06 12:08:01 +000050static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
51 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000052 default: assert(0 && "Unknown FP format");
53 case MVT::f32: return &APFloat::IEEEsingle;
54 case MVT::f64: return &APFloat::IEEEdouble;
55 case MVT::f80: return &APFloat::x87DoubleExtended;
56 case MVT::f128: return &APFloat::IEEEquad;
57 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
58 }
59}
60
Chris Lattnerf8dc0612008-02-03 06:49:24 +000061SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
62
Jim Laskey58b968b2005-08-17 20:08:02 +000063//===----------------------------------------------------------------------===//
64// ConstantFPSDNode Class
65//===----------------------------------------------------------------------===//
66
67/// isExactlyValue - We don't rely on operator== working on double values, as
68/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
69/// As such, this method can be used to do an exact bit-for-bit comparison of
70/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000071bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000072 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000073}
74
Duncan Sands83ec4b62008-06-06 12:08:01 +000075bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000076 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000077 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000078
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000079 // PPC long double cannot be converted to any other type.
80 if (VT == MVT::ppcf128 ||
81 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000082 return false;
83
Dale Johannesenf04afdb2007-08-30 00:23:21 +000084 // convert modifies in place, so make a copy.
85 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000086 return Val2.convert(*MVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000088}
89
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000093
Evan Chenga8df1662006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000097 // Look through a bit convert.
98 if (N->getOpcode() == ISD::BIT_CONVERT)
99 N = N->getOperand(0).Val;
100
Evan Chenga8df1662006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000102
103 unsigned i = 0, e = N->getNumOperands();
104
105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
108
109 // Do not accept an all-undef vector.
110 if (i == e) return false;
111
112 // Do not accept build_vectors that aren't all constants or which have non-~0
113 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000114 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000115 if (isa<ConstantSDNode>(NotZero)) {
116 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
117 return false;
118 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000119 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
120 convertToAPInt().isAllOnesValue())
121 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000122 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000123 return false;
124
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
130 return false;
131 return true;
132}
133
134
Evan Cheng4a147842006-03-26 09:50:58 +0000135/// isBuildVectorAllZeros - Return true if the specified node is a
136/// BUILD_VECTOR where all of the elements are 0 or undef.
137bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
140 N = N->getOperand(0).Val;
141
Evan Cheng4a147842006-03-26 09:50:58 +0000142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000143
144 unsigned i = 0, e = N->getNumOperands();
145
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
148 ++i;
149
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
152
153 // Do not accept build_vectors that aren't all constants or which have non-~0
154 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 return false;
159 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000161 return false;
162 } else
163 return false;
164
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 // undefs.
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
170 return false;
171 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000172}
173
Evan Chengefec7512008-02-18 23:04:32 +0000174/// isScalarToVector - Return true if the specified node is a
175/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
176/// element is not an undef.
177bool ISD::isScalarToVector(const SDNode *N) {
178 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
179 return true;
180
181 if (N->getOpcode() != ISD::BUILD_VECTOR)
182 return false;
183 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
184 return false;
185 unsigned NumElems = N->getNumOperands();
186 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000188 if (V.getOpcode() != ISD::UNDEF)
189 return false;
190 }
191 return true;
192}
193
194
Evan Chengbb81d972008-01-31 09:59:15 +0000195/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000196/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000197bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000199 if (N->getOpcode() == ISD::DBG_LABEL)
200 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000201 if (N->isMachineOpcode() &&
202 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000203 return true;
204 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000205}
206
Chris Lattnerc3aae252005-01-07 07:46:32 +0000207/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
208/// when given the operation for (X op Y).
209ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
210 // To perform this operation, we just need to swap the L and G bits of the
211 // operation.
212 unsigned OldL = (Operation >> 2) & 1;
213 unsigned OldG = (Operation >> 1) & 1;
214 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
215 (OldL << 1) | // New G bit
216 (OldG << 2)); // New L bit.
217}
218
219/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
220/// 'op' is a valid SetCC operation.
221ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
222 unsigned Operation = Op;
223 if (isInteger)
224 Operation ^= 7; // Flip L, G, E bits, but not U.
225 else
226 Operation ^= 15; // Flip all of the condition bits.
227 if (Operation > ISD::SETTRUE2)
228 Operation &= ~8; // Don't let N and U bits get set.
229 return ISD::CondCode(Operation);
230}
231
232
233/// isSignedOp - For an integer comparison, return 1 if the comparison is a
234/// signed operation and 2 if the result is an unsigned comparison. Return zero
235/// if the operation does not depend on the sign of the input (setne and seteq).
236static int isSignedOp(ISD::CondCode Opcode) {
237 switch (Opcode) {
238 default: assert(0 && "Illegal integer setcc operation!");
239 case ISD::SETEQ:
240 case ISD::SETNE: return 0;
241 case ISD::SETLT:
242 case ISD::SETLE:
243 case ISD::SETGT:
244 case ISD::SETGE: return 1;
245 case ISD::SETULT:
246 case ISD::SETULE:
247 case ISD::SETUGT:
248 case ISD::SETUGE: return 2;
249 }
250}
251
252/// getSetCCOrOperation - Return the result of a logical OR between different
253/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
254/// returns SETCC_INVALID if it is not possible to represent the resultant
255/// comparison.
256ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
257 bool isInteger) {
258 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
259 // Cannot fold a signed integer setcc with an unsigned integer setcc.
260 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000261
Chris Lattnerc3aae252005-01-07 07:46:32 +0000262 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattnerc3aae252005-01-07 07:46:32 +0000264 // If the N and U bits get set then the resultant comparison DOES suddenly
265 // care about orderedness, and is true when ordered.
266 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000267 Op &= ~16; // Clear the U bit if the N bit is set.
268
269 // Canonicalize illegal integer setcc's.
270 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
271 Op = ISD::SETNE;
272
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273 return ISD::CondCode(Op);
274}
275
276/// getSetCCAndOperation - Return the result of a logical AND between different
277/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
278/// function returns zero if it is not possible to represent the resultant
279/// comparison.
280ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
281 bool isInteger) {
282 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
283 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000284 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000285
286 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000287 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
288
289 // Canonicalize illegal integer setcc's.
290 if (isInteger) {
291 switch (Result) {
292 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000293 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000294 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000295 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
296 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
297 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000298 }
299 }
300
301 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000302}
303
Chris Lattnerb48da392005-01-23 04:39:44 +0000304const TargetMachine &SelectionDAG::getTarget() const {
305 return TLI.getTargetMachine();
306}
307
Jim Laskey58b968b2005-08-17 20:08:02 +0000308//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000309// SDNode Profile Support
310//===----------------------------------------------------------------------===//
311
Jim Laskeydef69b92006-10-27 23:52:51 +0000312/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
313///
Jim Laskey583bd472006-10-27 23:46:08 +0000314static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
315 ID.AddInteger(OpC);
316}
317
318/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
319/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000320static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000321 ID.AddPointer(VTList.VTs);
322}
323
Jim Laskeydef69b92006-10-27 23:52:51 +0000324/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
325///
Jim Laskey583bd472006-10-27 23:46:08 +0000326static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000327 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000328 for (; NumOps; --NumOps, ++Ops) {
329 ID.AddPointer(Ops->Val);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000330 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000331 }
Jim Laskey583bd472006-10-27 23:46:08 +0000332}
333
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000334/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
335///
336static void AddNodeIDOperands(FoldingSetNodeID &ID,
337 const SDUse *Ops, unsigned NumOps) {
338 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman475871a2008-07-27 21:46:04 +0000339 ID.AddPointer(Ops->getVal());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000340 ID.AddInteger(Ops->getSDValue().getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000341 }
342}
343
Jim Laskey583bd472006-10-27 23:46:08 +0000344static void AddNodeIDNode(FoldingSetNodeID &ID,
345 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000346 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000347 AddNodeIDOpcode(ID, OpC);
348 AddNodeIDValueTypes(ID, VTList);
349 AddNodeIDOperands(ID, OpList, N);
350}
351
Roman Levenstein9cac5252008-04-16 16:15:27 +0000352
Jim Laskeydef69b92006-10-27 23:52:51 +0000353/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
354/// data.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000355static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000356 AddNodeIDOpcode(ID, N->getOpcode());
357 // Add the return value info.
358 AddNodeIDValueTypes(ID, N->getVTList());
359 // Add the operand info.
360 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
361
362 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000363 switch (N->getOpcode()) {
364 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000365 case ISD::ARG_FLAGS:
366 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
367 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 case ISD::TargetConstant:
369 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000370 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000371 break;
372 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000373 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000374 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000376 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000377 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000378 case ISD::GlobalAddress:
379 case ISD::TargetGlobalTLSAddress:
380 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000381 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000382 ID.AddPointer(GA->getGlobal());
383 ID.AddInteger(GA->getOffset());
384 break;
385 }
386 case ISD::BasicBlock:
387 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
388 break;
389 case ISD::Register:
390 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
391 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000392 case ISD::DBG_STOPPOINT: {
393 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
394 ID.AddInteger(DSP->getLine());
395 ID.AddInteger(DSP->getColumn());
396 ID.AddPointer(DSP->getCompileUnit());
397 break;
398 }
Dan Gohman69de1932008-02-06 22:27:42 +0000399 case ISD::SRCVALUE:
400 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
401 break;
402 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000403 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000404 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000405 break;
406 }
407 case ISD::FrameIndex:
408 case ISD::TargetFrameIndex:
409 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
410 break;
411 case ISD::JumpTable:
412 case ISD::TargetJumpTable:
413 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
414 break;
415 case ISD::ConstantPool:
416 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000417 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000418 ID.AddInteger(CP->getAlignment());
419 ID.AddInteger(CP->getOffset());
420 if (CP->isMachineConstantPoolEntry())
421 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
422 else
423 ID.AddPointer(CP->getConstVal());
424 break;
425 }
426 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000427 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000428 ID.AddInteger(LD->getAddressingMode());
429 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000430 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 break;
433 }
434 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000435 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 ID.AddInteger(ST->getAddressingMode());
437 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000438 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000439 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 break;
441 }
Mon P Wang28873102008-06-25 08:15:39 +0000442 case ISD::ATOMIC_CMP_SWAP:
443 case ISD::ATOMIC_LOAD_ADD:
444 case ISD::ATOMIC_SWAP:
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);
455 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000456 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000457 }
Mon P Wang28873102008-06-25 08:15:39 +0000458 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000459}
460
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000461/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
462/// the CSE map that carries both alignment and volatility information.
463///
464static unsigned encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
465 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
466}
467
Jim Laskey583bd472006-10-27 23:46:08 +0000468//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000469// SelectionDAG Class
470//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000471
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000472/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000473/// SelectionDAG.
474void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000475 // Create a dummy node (which is not added to allnodes), that adds a reference
476 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000477 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000478
Chris Lattner190a4182006-08-04 17:45:20 +0000479 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000480
Chris Lattner190a4182006-08-04 17:45:20 +0000481 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000482 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000483 if (I->use_empty())
484 DeadNodes.push_back(I);
485
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000486 RemoveDeadNodes(DeadNodes);
487
488 // If the root changed (e.g. it was a dead load, update the root).
489 setRoot(Dummy.getValue());
490}
491
492/// RemoveDeadNodes - This method deletes the unreachable nodes in the
493/// given list, and any nodes that become unreachable as a result.
494void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
495 DAGUpdateListener *UpdateListener) {
496
Chris Lattner190a4182006-08-04 17:45:20 +0000497 // Process the worklist, deleting the nodes and adding their uses to the
498 // worklist.
499 while (!DeadNodes.empty()) {
500 SDNode *N = DeadNodes.back();
501 DeadNodes.pop_back();
502
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000503 if (UpdateListener)
504 UpdateListener->NodeDeleted(N, 0);
505
Chris Lattner190a4182006-08-04 17:45:20 +0000506 // Take the node out of the appropriate CSE map.
507 RemoveNodeFromCSEMaps(N);
508
509 // Next, brutally remove the operand list. This is safe to do, as there are
510 // no cycles in the graph.
511 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000512 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000513 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000514
515 // Now that we removed this operand, see if there are no uses of it left.
516 if (Operand->use_empty())
517 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000518 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000519 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000520 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000521 }
Chris Lattner190a4182006-08-04 17:45:20 +0000522 N->OperandList = 0;
523 N->NumOperands = 0;
524
525 // Finally, remove N itself.
Dan Gohman11467282008-08-26 01:44:34 +0000526 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerf469cb62005-11-08 18:52:27 +0000527 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000528}
529
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000530void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000531 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000532 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000533}
534
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000535void SelectionDAG::DeleteNode(SDNode *N) {
536 assert(N->use_empty() && "Cannot delete a node that is not dead!");
537
538 // First take this out of the appropriate CSE map.
539 RemoveNodeFromCSEMaps(N);
540
Chris Lattner1e111c72005-09-07 05:37:01 +0000541 // Finally, remove uses due to operands of this node, remove from the
542 // AllNodes list, and delete the node.
543 DeleteNodeNotInCSEMaps(N);
544}
545
546void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
547
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000548 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000549 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000550 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf350b272008-08-23 02:25:05 +0000551 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000552 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000553
Dan Gohman11467282008-08-26 01:44:34 +0000554 assert(N != AllNodes.begin());
555 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000556}
557
Chris Lattner149c58c2005-08-16 18:17:10 +0000558/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
559/// correspond to it. This is useful when we're about to delete or repurpose
560/// the node. We don't want future request for structurally identical nodes
561/// to return N anymore.
562void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000563 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000564 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000565 case ISD::EntryToken:
566 assert(0 && "EntryToken should not be in CSEMaps!");
567 return;
Chris Lattner95038592005-10-05 06:35:28 +0000568 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000569 case ISD::CONDCODE:
570 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
571 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000572 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000573 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
574 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000575 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000576 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000577 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000578 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000579 Erased =
580 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000581 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000582 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000583 MVT VT = cast<VTSDNode>(N)->getVT();
584 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000585 Erased = ExtendedValueTypeNodes.erase(VT);
586 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000587 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
588 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000589 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000590 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000591 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000592 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000593 // Remove it from the CSE Map.
594 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000595 break;
596 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000597#ifndef NDEBUG
598 // Verify that the node was actually in one of the CSE maps, unless it has a
599 // flag result (which cannot be CSE'd) or is one of the special cases that are
600 // not subject to CSE.
601 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Evan Cheng71e86852008-07-08 20:06:39 +0000602 !N->isTargetOpcode() &&
603 N->getOpcode() != ISD::DBG_LABEL &&
604 N->getOpcode() != ISD::DBG_STOPPOINT &&
605 N->getOpcode() != ISD::EH_LABEL &&
606 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000607 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000608 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000609 assert(0 && "Node is not in map!");
610 }
611#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000612}
613
Chris Lattner8b8749f2005-08-17 19:00:20 +0000614/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
615/// has been taken out and modified in some way. If the specified node already
616/// exists in the CSE maps, do not modify the maps, but return the existing node
617/// instead. If it doesn't exist, add it and return null.
618///
619SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
620 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000621
622 if (N->getValueType(0) == MVT::Flag)
623 return 0; // Never CSE anything that produces a flag.
624
625 switch (N->getOpcode()) {
626 default: break;
627 case ISD::HANDLENODE:
628 case ISD::DBG_LABEL:
629 case ISD::DBG_STOPPOINT:
630 case ISD::EH_LABEL:
631 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000632 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000633 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000634
Chris Lattner9f8cc692005-12-19 22:21:21 +0000635 // Check that remaining values produced are not flags.
636 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
637 if (N->getValueType(i) == MVT::Flag)
638 return 0; // Never CSE anything that produces a flag.
639
Chris Lattnera5682852006-08-07 23:03:03 +0000640 SDNode *New = CSEMap.GetOrInsertNode(N);
641 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000642 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000643}
644
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000645/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
646/// were replaced with those specified. If this node is never memoized,
647/// return null, otherwise return a pointer to the slot it would take. If a
648/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000649SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000650 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000651 if (N->getValueType(0) == MVT::Flag)
652 return 0; // Never CSE anything that produces a flag.
653
654 switch (N->getOpcode()) {
655 default: break;
656 case ISD::HANDLENODE:
657 case ISD::DBG_LABEL:
658 case ISD::DBG_STOPPOINT:
659 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000660 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000661 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000662
663 // Check that remaining values produced are not flags.
664 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
665 if (N->getValueType(i) == MVT::Flag)
666 return 0; // Never CSE anything that produces a flag.
667
Dan Gohman475871a2008-07-27 21:46:04 +0000668 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000669 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000670 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000671 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000672}
673
674/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
675/// were replaced with those specified. If this node is never memoized,
676/// return null, otherwise return a pointer to the slot it would take. If a
677/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000678SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000679 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000680 void *&InsertPos) {
681 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000682
683 // Check that remaining values produced are not flags.
684 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
685 if (N->getValueType(i) == MVT::Flag)
686 return 0; // Never CSE anything that produces a flag.
687
Dan Gohman475871a2008-07-27 21:46:04 +0000688 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000689 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000690 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000691 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
692}
693
694
695/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
696/// were replaced with those specified. If this node is never memoized,
697/// return null, otherwise return a pointer to the slot it would take. If a
698/// node already exists with these operands, the slot will be non-null.
699SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000700 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000701 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000702 if (N->getValueType(0) == MVT::Flag)
703 return 0; // Never CSE anything that produces a flag.
704
705 switch (N->getOpcode()) {
706 default: break;
707 case ISD::HANDLENODE:
708 case ISD::DBG_LABEL:
709 case ISD::DBG_STOPPOINT:
710 case ISD::EH_LABEL:
711 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000712 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000713 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000714
715 // Check that remaining values produced are not flags.
716 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
717 if (N->getValueType(i) == MVT::Flag)
718 return 0; // Never CSE anything that produces a flag.
719
Jim Laskey583bd472006-10-27 23:46:08 +0000720 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000721 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000722
Evan Cheng9629aba2006-10-11 01:47:58 +0000723 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
724 ID.AddInteger(LD->getAddressingMode());
725 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000726 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000727 ID.AddInteger(LD->getRawFlags());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000728 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
729 ID.AddInteger(ST->getAddressingMode());
730 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000731 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000732 ID.AddInteger(ST->getRawFlags());
Evan Cheng9629aba2006-10-11 01:47:58 +0000733 }
Jim Laskey583bd472006-10-27 23:46:08 +0000734
Chris Lattnera5682852006-08-07 23:03:03 +0000735 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000736}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000737
Duncan Sandsd038e042008-07-21 10:20:31 +0000738/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
739void SelectionDAG::VerifyNode(SDNode *N) {
740 switch (N->getOpcode()) {
741 default:
742 break;
743 case ISD::BUILD_VECTOR: {
744 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
745 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
746 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
747 "Wrong number of BUILD_VECTOR operands!");
748 MVT EltVT = N->getValueType(0).getVectorElementType();
749 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohman475871a2008-07-27 21:46:04 +0000750 assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd038e042008-07-21 10:20:31 +0000751 "Wrong BUILD_VECTOR operand type!");
752 break;
753 }
754 }
755}
756
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000757/// getMVTAlignment - Compute the default alignment value for the
758/// given type.
759///
760unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
761 const Type *Ty = VT == MVT::iPTR ?
762 PointerType::get(Type::Int8Ty, 0) :
763 VT.getTypeForMVT();
764
765 return TLI.getTargetData()->getABITypeAlignment(Ty);
766}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000767
Dan Gohman6f179662008-08-23 00:50:30 +0000768SelectionDAG::SelectionDAG(TargetLowering &tli, MachineFunction &mf,
Dan Gohmanf350b272008-08-23 02:25:05 +0000769 FunctionLoweringInfo &fli, MachineModuleInfo *mmi)
770 : TLI(tli), MF(mf), FLI(fli), MMI(mmi),
771 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
772 Root(getEntryNode()) {
773 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000774}
775
Chris Lattner78ec3112003-08-11 14:57:33 +0000776SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000777 allnodes_clear();
778}
779
780void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000781 assert(&*AllNodes.begin() == &EntryNode);
782 AllNodes.remove(AllNodes.begin());
Chris Lattnerde202b32005-11-09 23:47:37 +0000783 while (!AllNodes.empty()) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000784 SDNode *N = AllNodes.remove(AllNodes.begin());
Chris Lattner213a16c2006-08-14 22:19:25 +0000785 N->SetNextInBucket(0);
Dan Gohmanf350b272008-08-23 02:25:05 +0000786 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000787 delete [] N->OperandList;
Dan Gohman11467282008-08-26 01:44:34 +0000788 NodeAllocator.Deallocate(N);
Chris Lattner65113b22005-11-08 22:07:03 +0000789 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000790}
791
Dan Gohmanf350b272008-08-23 02:25:05 +0000792void SelectionDAG::reset() {
793 allnodes_clear();
794 OperandAllocator.Reset();
795 CSEMap.clear();
796
797 ExtendedValueTypeNodes.clear();
798 ExternalSymbols.clear();
799 TargetExternalSymbols.clear();
800 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
801 static_cast<CondCodeSDNode*>(0));
802 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
803 static_cast<SDNode*>(0));
804
805 EntryNode.Uses = 0;
806 AllNodes.push_back(&EntryNode);
807 Root = getEntryNode();
808}
809
Dan Gohman475871a2008-07-27 21:46:04 +0000810SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000811 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000812 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000813 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000814 return getNode(ISD::AND, Op.getValueType(), Op,
815 getConstant(Imm, Op.getValueType()));
816}
817
Dan Gohman475871a2008-07-27 21:46:04 +0000818SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000819 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000820 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000821}
822
Dan Gohman475871a2008-07-27 21:46:04 +0000823SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000824 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000825
Evan Cheng0ff39b32008-06-30 07:31:25 +0000826 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000827 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000828 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000829
830 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000831 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000832 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000833 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000834 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000835 SDNode *N = NULL;
836 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000837 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000838 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000839 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000840 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000841 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000842 CSEMap.InsertNode(N, IP);
843 AllNodes.push_back(N);
844 }
845
Dan Gohman475871a2008-07-27 21:46:04 +0000846 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000847 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000848 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000849 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000850 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
851 }
852 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000853}
854
Dan Gohman475871a2008-07-27 21:46:04 +0000855SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000856 return getConstant(Val, TLI.getPointerTy(), isTarget);
857}
858
859
Dan Gohman475871a2008-07-27 21:46:04 +0000860SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000861 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000862
Duncan Sands83ec4b62008-06-06 12:08:01 +0000863 MVT EltVT =
864 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000865
Chris Lattnerd8658612005-02-17 20:17:32 +0000866 // Do the map lookup using the actual bit pattern for the floating point
867 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
868 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000869 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000870 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000871 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000872 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000873 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000874 SDNode *N = NULL;
875 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000876 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000877 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000878 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000879 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000880 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000881 CSEMap.InsertNode(N, IP);
882 AllNodes.push_back(N);
883 }
884
Dan Gohman475871a2008-07-27 21:46:04 +0000885 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000886 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000887 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000888 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000889 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
890 }
891 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000892}
893
Dan Gohman475871a2008-07-27 21:46:04 +0000894SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000895 MVT EltVT =
896 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000897 if (EltVT==MVT::f32)
898 return getConstantFP(APFloat((float)Val), VT, isTarget);
899 else
900 return getConstantFP(APFloat(Val), VT, isTarget);
901}
902
Dan Gohman475871a2008-07-27 21:46:04 +0000903SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
904 MVT VT, int Offset,
905 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000906 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000907
908 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
909 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000910 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000911 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
912 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
913 }
914
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000915 if (GVar && GVar->isThreadLocal())
916 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
917 else
918 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000919
Jim Laskey583bd472006-10-27 23:46:08 +0000920 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000921 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000922 ID.AddPointer(GV);
923 ID.AddInteger(Offset);
924 void *IP = 0;
925 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000926 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000927 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000928 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000929 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000930 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000931 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000932}
933
Dan Gohman475871a2008-07-27 21:46:04 +0000934SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000935 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000936 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000937 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000938 ID.AddInteger(FI);
939 void *IP = 0;
940 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000941 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000942 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000943 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000944 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000945 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000946 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000947}
948
Dan Gohman475871a2008-07-27 21:46:04 +0000949SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000950 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000951 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000952 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000953 ID.AddInteger(JTI);
954 void *IP = 0;
955 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000956 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000957 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000958 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000959 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000960 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000961 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +0000962}
963
Dan Gohman475871a2008-07-27 21:46:04 +0000964SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
965 unsigned Alignment, int Offset,
966 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000967 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000968 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000969 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000970 ID.AddInteger(Alignment);
971 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000972 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000973 void *IP = 0;
974 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000975 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000976 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000977 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000978 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000979 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000980 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000981}
982
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983
Dan Gohman475871a2008-07-27 21:46:04 +0000984SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
985 unsigned Alignment, int Offset,
986 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000987 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000988 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000989 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000990 ID.AddInteger(Alignment);
991 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000992 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000993 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<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000997 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000998 CSEMap.InsertNode(N, IP);
999 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001000 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001001}
1002
1003
Dan Gohman475871a2008-07-27 21:46:04 +00001004SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001005 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001006 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001007 ID.AddPointer(MBB);
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<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001012 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001013 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001014 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001015 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001016}
1017
Dan Gohman475871a2008-07-27 21:46:04 +00001018SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001019 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001020 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001021 ID.AddInteger(Flags.getRawBits());
1022 void *IP = 0;
1023 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001024 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001025 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001026 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001027 CSEMap.InsertNode(N, IP);
1028 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001029 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001030}
1031
Dan Gohman475871a2008-07-27 21:46:04 +00001032SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001033 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1034 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001035
Duncan Sands83ec4b62008-06-06 12:08:01 +00001036 SDNode *&N = VT.isExtended() ?
1037 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001038
Dan Gohman475871a2008-07-27 21:46:04 +00001039 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001040 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001041 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001042 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001043 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001044}
1045
Dan Gohman475871a2008-07-27 21:46:04 +00001046SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001047 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001048 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001049 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001050 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001051 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001052 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001053}
1054
Dan Gohman475871a2008-07-27 21:46:04 +00001055SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001056 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001057 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001058 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001059 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001060 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001061 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062}
1063
Dan Gohman475871a2008-07-27 21:46:04 +00001064SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001065 if ((unsigned)Cond >= CondCodeNodes.size())
1066 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001067
Chris Lattner079a27a2005-08-09 20:40:02 +00001068 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001069 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001070 new (N) CondCodeSDNode(Cond);
1071 CondCodeNodes[Cond] = N;
1072 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001073 }
Dan Gohman475871a2008-07-27 21:46:04 +00001074 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001075}
1076
Dan Gohman475871a2008-07-27 21:46:04 +00001077SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001078 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001079 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001080 ID.AddInteger(RegNo);
1081 void *IP = 0;
1082 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001083 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001084 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001085 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001086 CSEMap.InsertNode(N, IP);
1087 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001088 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001089}
1090
Dan Gohman475871a2008-07-27 21:46:04 +00001091SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001092 unsigned Line, unsigned Col,
1093 const CompileUnitDesc *CU) {
1094 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001095 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001096 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001097 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001098}
1099
Dan Gohman475871a2008-07-27 21:46:04 +00001100SDValue SelectionDAG::getLabel(unsigned Opcode,
1101 SDValue Root,
1102 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001103 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001104 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001105 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1106 ID.AddInteger(LabelID);
1107 void *IP = 0;
1108 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001109 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001110 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001111 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001112 CSEMap.InsertNode(N, IP);
1113 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001114 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001115}
1116
Dan Gohman475871a2008-07-27 21:46:04 +00001117SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001118 assert((!V || isa<PointerType>(V->getType())) &&
1119 "SrcValue is not a pointer?");
1120
Jim Laskey583bd472006-10-27 23:46:08 +00001121 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001122 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001123 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001124
Chris Lattner61b09412006-08-11 21:01:22 +00001125 void *IP = 0;
1126 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001127 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001128
Dan Gohmanfed90b62008-07-28 21:51:04 +00001129 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001130 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001131 CSEMap.InsertNode(N, IP);
1132 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001133 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001134}
1135
Dan Gohman475871a2008-07-27 21:46:04 +00001136SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001137 const Value *v = MO.getValue();
1138 assert((!v || isa<PointerType>(v->getType())) &&
1139 "SrcValue is not a pointer?");
1140
1141 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001142 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001143 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001144
1145 void *IP = 0;
1146 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001147 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001148
Dan Gohmanfed90b62008-07-28 21:51:04 +00001149 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001150 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001151 CSEMap.InsertNode(N, IP);
1152 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001153 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001154}
1155
Chris Lattner37ce9df2007-10-15 17:47:20 +00001156/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1157/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001158SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001159 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001160 unsigned ByteSize = VT.getSizeInBits()/8;
1161 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001162 unsigned StackAlign =
1163 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1164
Chris Lattner37ce9df2007-10-15 17:47:20 +00001165 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1166 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1167}
1168
Dan Gohman475871a2008-07-27 21:46:04 +00001169SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1170 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001171 // These setcc operations always fold.
1172 switch (Cond) {
1173 default: break;
1174 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001175 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001176 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001177 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001178
1179 case ISD::SETOEQ:
1180 case ISD::SETOGT:
1181 case ISD::SETOGE:
1182 case ISD::SETOLT:
1183 case ISD::SETOLE:
1184 case ISD::SETONE:
1185 case ISD::SETO:
1186 case ISD::SETUO:
1187 case ISD::SETUEQ:
1188 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001189 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001190 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001191 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001192
Chris Lattner67255a12005-04-07 18:14:58 +00001193 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001194 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001195 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001196 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001197
Chris Lattnerc3aae252005-01-07 07:46:32 +00001198 switch (Cond) {
1199 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001200 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1201 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001202 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1203 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1204 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1205 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1206 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1207 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1208 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1209 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001210 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001211 }
Chris Lattner67255a12005-04-07 18:14:58 +00001212 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001213 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001215 // No compile time operations on this type yet.
1216 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001217 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001218
1219 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001221 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001222 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1223 return getNode(ISD::UNDEF, VT);
1224 // fall through
1225 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1226 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1227 return getNode(ISD::UNDEF, VT);
1228 // fall through
1229 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001230 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001231 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1232 return getNode(ISD::UNDEF, VT);
1233 // fall through
1234 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1235 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1236 return getNode(ISD::UNDEF, VT);
1237 // fall through
1238 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1239 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1240 return getNode(ISD::UNDEF, VT);
1241 // fall through
1242 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001243 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001244 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1245 return getNode(ISD::UNDEF, VT);
1246 // fall through
1247 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001248 R==APFloat::cmpEqual, VT);
1249 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1250 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1251 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1252 R==APFloat::cmpEqual, VT);
1253 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1254 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1255 R==APFloat::cmpLessThan, VT);
1256 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1257 R==APFloat::cmpUnordered, VT);
1258 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1259 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001260 }
1261 } else {
1262 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001263 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001264 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001265 }
1266
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001267 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001268 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001269}
1270
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001271/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1272/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001273bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001274 unsigned BitWidth = Op.getValueSizeInBits();
1275 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1276}
1277
Dan Gohmanea859be2007-06-22 14:59:07 +00001278/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1279/// this predicate to simplify operations downstream. Mask is known to be zero
1280/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001281bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001282 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001283 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001284 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1285 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1286 return (KnownZero & Mask) == Mask;
1287}
1288
1289/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1290/// known to be either zero or one and return them in the KnownZero/KnownOne
1291/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1292/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001293void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001294 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001295 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001296 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001297 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001298 "Mask size mismatches value type size!");
1299
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001300 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001301 if (Depth == 6 || Mask == 0)
1302 return; // Limit search depth.
1303
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001304 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001305
1306 switch (Op.getOpcode()) {
1307 case ISD::Constant:
1308 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001309 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001310 KnownZero = ~KnownOne & Mask;
1311 return;
1312 case ISD::AND:
1313 // If either the LHS or the RHS are Zero, the result is zero.
1314 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001315 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1316 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001317 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1318 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1319
1320 // Output known-1 bits are only known if set in both the LHS & RHS.
1321 KnownOne &= KnownOne2;
1322 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1323 KnownZero |= KnownZero2;
1324 return;
1325 case ISD::OR:
1326 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001327 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1328 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001329 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1330 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1331
1332 // Output known-0 bits are only known if clear in both the LHS & RHS.
1333 KnownZero &= KnownZero2;
1334 // Output known-1 are known to be set if set in either the LHS | RHS.
1335 KnownOne |= KnownOne2;
1336 return;
1337 case ISD::XOR: {
1338 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1339 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1340 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1341 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1342
1343 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001344 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001345 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1346 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1347 KnownZero = KnownZeroOut;
1348 return;
1349 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001350 case ISD::MUL: {
1351 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1352 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1353 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1354 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1355 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1356
1357 // If low bits are zero in either operand, output low known-0 bits.
1358 // Also compute a conserative estimate for high known-0 bits.
1359 // More trickiness is possible, but this is sufficient for the
1360 // interesting case of alignment computation.
1361 KnownOne.clear();
1362 unsigned TrailZ = KnownZero.countTrailingOnes() +
1363 KnownZero2.countTrailingOnes();
1364 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001365 KnownZero2.countLeadingOnes(),
1366 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001367
1368 TrailZ = std::min(TrailZ, BitWidth);
1369 LeadZ = std::min(LeadZ, BitWidth);
1370 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1371 APInt::getHighBitsSet(BitWidth, LeadZ);
1372 KnownZero &= Mask;
1373 return;
1374 }
1375 case ISD::UDIV: {
1376 // For the purposes of computing leading zeros we can conservatively
1377 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001378 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001379 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1380 ComputeMaskedBits(Op.getOperand(0),
1381 AllOnes, KnownZero2, KnownOne2, Depth+1);
1382 unsigned LeadZ = KnownZero2.countLeadingOnes();
1383
1384 KnownOne2.clear();
1385 KnownZero2.clear();
1386 ComputeMaskedBits(Op.getOperand(1),
1387 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001388 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1389 if (RHSUnknownLeadingOnes != BitWidth)
1390 LeadZ = std::min(BitWidth,
1391 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001392
1393 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1394 return;
1395 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001396 case ISD::SELECT:
1397 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1398 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1399 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1400 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1401
1402 // Only known if known in both the LHS and RHS.
1403 KnownOne &= KnownOne2;
1404 KnownZero &= KnownZero2;
1405 return;
1406 case ISD::SELECT_CC:
1407 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1408 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1409 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1410 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1411
1412 // Only known if known in both the LHS and RHS.
1413 KnownOne &= KnownOne2;
1414 KnownZero &= KnownZero2;
1415 return;
1416 case ISD::SETCC:
1417 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001418 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1419 BitWidth > 1)
1420 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001421 return;
1422 case ISD::SHL:
1423 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1424 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001425 unsigned ShAmt = SA->getValue();
1426
1427 // If the shift count is an invalid immediate, don't do anything.
1428 if (ShAmt >= BitWidth)
1429 return;
1430
1431 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001432 KnownZero, KnownOne, Depth+1);
1433 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001434 KnownZero <<= ShAmt;
1435 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001436 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001437 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001438 }
1439 return;
1440 case ISD::SRL:
1441 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1442 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001443 unsigned ShAmt = SA->getValue();
1444
Dan Gohmand4cf9922008-02-26 18:50:50 +00001445 // If the shift count is an invalid immediate, don't do anything.
1446 if (ShAmt >= BitWidth)
1447 return;
1448
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001449 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001450 KnownZero, KnownOne, Depth+1);
1451 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001452 KnownZero = KnownZero.lshr(ShAmt);
1453 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001454
Dan Gohman72d2fd52008-02-13 22:43:25 +00001455 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001456 KnownZero |= HighBits; // High bits known zero.
1457 }
1458 return;
1459 case ISD::SRA:
1460 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001461 unsigned ShAmt = SA->getValue();
1462
Dan Gohmand4cf9922008-02-26 18:50:50 +00001463 // If the shift count is an invalid immediate, don't do anything.
1464 if (ShAmt >= BitWidth)
1465 return;
1466
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001467 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001468 // If any of the demanded bits are produced by the sign extension, we also
1469 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001470 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1471 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001472 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001473
1474 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1475 Depth+1);
1476 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001477 KnownZero = KnownZero.lshr(ShAmt);
1478 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001479
1480 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001481 APInt SignBit = APInt::getSignBit(BitWidth);
1482 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001483
Dan Gohmanca93a432008-02-20 16:30:17 +00001484 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001485 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001486 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001487 KnownOne |= HighBits; // New bits are known one.
1488 }
1489 }
1490 return;
1491 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001492 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1493 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001494
1495 // Sign extension. Compute the demanded bits in the result that are not
1496 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001497 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001498
Dan Gohman977a76f2008-02-13 22:28:48 +00001499 APInt InSignBit = APInt::getSignBit(EBits);
1500 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001501
1502 // If the sign extended bits are demanded, we know that the sign
1503 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001504 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001505 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001506 InputDemandedBits |= InSignBit;
1507
1508 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1509 KnownZero, KnownOne, Depth+1);
1510 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1511
1512 // If the sign bit of the input is known set or clear, then we know the
1513 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001514 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001515 KnownZero |= NewBits;
1516 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001517 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001518 KnownOne |= NewBits;
1519 KnownZero &= ~NewBits;
1520 } else { // Input sign bit unknown
1521 KnownZero &= ~NewBits;
1522 KnownOne &= ~NewBits;
1523 }
1524 return;
1525 }
1526 case ISD::CTTZ:
1527 case ISD::CTLZ:
1528 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001529 unsigned LowBits = Log2_32(BitWidth)+1;
1530 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001531 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001532 return;
1533 }
1534 case ISD::LOAD: {
1535 if (ISD::isZEXTLoad(Op.Val)) {
1536 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001537 MVT VT = LD->getMemoryVT();
1538 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001539 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001540 }
1541 return;
1542 }
1543 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001544 MVT InVT = Op.getOperand(0).getValueType();
1545 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001546 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1547 APInt InMask = Mask;
1548 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001549 KnownZero.trunc(InBits);
1550 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001551 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001552 KnownZero.zext(BitWidth);
1553 KnownOne.zext(BitWidth);
1554 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001555 return;
1556 }
1557 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001558 MVT InVT = Op.getOperand(0).getValueType();
1559 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001560 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001561 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1562 APInt InMask = Mask;
1563 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001564
1565 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001566 // bit is demanded. Temporarily set this bit in the mask for our callee.
1567 if (NewBits.getBoolValue())
1568 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001569
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001570 KnownZero.trunc(InBits);
1571 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001572 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1573
1574 // Note if the sign bit is known to be zero or one.
1575 bool SignBitKnownZero = KnownZero.isNegative();
1576 bool SignBitKnownOne = KnownOne.isNegative();
1577 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1578 "Sign bit can't be known to be both zero and one!");
1579
1580 // If the sign bit wasn't actually demanded by our caller, we don't
1581 // want it set in the KnownZero and KnownOne result values. Reset the
1582 // mask and reapply it to the result values.
1583 InMask = Mask;
1584 InMask.trunc(InBits);
1585 KnownZero &= InMask;
1586 KnownOne &= InMask;
1587
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001588 KnownZero.zext(BitWidth);
1589 KnownOne.zext(BitWidth);
1590
Dan Gohman977a76f2008-02-13 22:28:48 +00001591 // If the sign bit is known zero or one, the top bits match.
1592 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001593 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001594 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001595 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001596 return;
1597 }
1598 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001599 MVT InVT = Op.getOperand(0).getValueType();
1600 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001601 APInt InMask = Mask;
1602 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001603 KnownZero.trunc(InBits);
1604 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001605 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001606 KnownZero.zext(BitWidth);
1607 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001608 return;
1609 }
1610 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001611 MVT InVT = Op.getOperand(0).getValueType();
1612 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001613 APInt InMask = Mask;
1614 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001615 KnownZero.zext(InBits);
1616 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001617 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001618 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001619 KnownZero.trunc(BitWidth);
1620 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001621 break;
1622 }
1623 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001624 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1625 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001626 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1627 KnownOne, Depth+1);
1628 KnownZero |= (~InMask) & Mask;
1629 return;
1630 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001631 case ISD::FGETSIGN:
1632 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001633 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001634 return;
1635
Dan Gohman23e8b712008-04-28 17:02:21 +00001636 case ISD::SUB: {
1637 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1638 // We know that the top bits of C-X are clear if X contains less bits
1639 // than C (i.e. no wrap-around can happen). For example, 20-X is
1640 // positive if we can prove that X is >= 0 and < 16.
1641 if (CLHS->getAPIntValue().isNonNegative()) {
1642 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1643 // NLZ can't be BitWidth with no sign bit
1644 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1645 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1646 Depth+1);
1647
1648 // If all of the MaskV bits are known to be zero, then we know the
1649 // output top bits are zero, because we now know that the output is
1650 // from [0-C].
1651 if ((KnownZero2 & MaskV) == MaskV) {
1652 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1653 // Top bits known zero.
1654 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1655 }
1656 }
1657 }
1658 }
1659 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001660 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001661 // Output known-0 bits are known if clear or set in both the low clear bits
1662 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1663 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001664 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1665 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1666 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1667 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1668
1669 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1670 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1671 KnownZeroOut = std::min(KnownZeroOut,
1672 KnownZero2.countTrailingOnes());
1673
1674 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001675 return;
1676 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001677 case ISD::SREM:
1678 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001679 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001680 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001681 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001682 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1683 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001684
Dan Gohmana60832b2008-08-13 23:12:35 +00001685 // If the sign bit of the first operand is zero, the sign bit of
1686 // the result is zero. If the first operand has no one bits below
1687 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001688 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1689 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001690
Dan Gohman23e8b712008-04-28 17:02:21 +00001691 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001692
1693 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001694 }
1695 }
1696 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001697 case ISD::UREM: {
1698 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001699 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001700 if (RA.isPowerOf2()) {
1701 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001702 APInt Mask2 = LowBits & Mask;
1703 KnownZero |= ~LowBits & Mask;
1704 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1705 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1706 break;
1707 }
1708 }
1709
1710 // Since the result is less than or equal to either operand, any leading
1711 // zero bits in either operand must also exist in the result.
1712 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1713 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1714 Depth+1);
1715 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1716 Depth+1);
1717
1718 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1719 KnownZero2.countLeadingOnes());
1720 KnownOne.clear();
1721 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1722 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001723 }
1724 default:
1725 // Allow the target to implement this method for its nodes.
1726 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1727 case ISD::INTRINSIC_WO_CHAIN:
1728 case ISD::INTRINSIC_W_CHAIN:
1729 case ISD::INTRINSIC_VOID:
1730 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1731 }
1732 return;
1733 }
1734}
1735
1736/// ComputeNumSignBits - Return the number of times the sign bit of the
1737/// register is replicated into the other bits. We know that at least 1 bit
1738/// is always equal to the sign bit (itself), but other cases can give us
1739/// information. For example, immediately after an "SRA X, 2", we know that
1740/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001741unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001742 MVT VT = Op.getValueType();
1743 assert(VT.isInteger() && "Invalid VT!");
1744 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001745 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001746 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001747
1748 if (Depth == 6)
1749 return 1; // Limit search depth.
1750
1751 switch (Op.getOpcode()) {
1752 default: break;
1753 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001754 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001755 return VTBits-Tmp+1;
1756 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001757 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001758 return VTBits-Tmp;
1759
1760 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001761 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1762 // If negative, return # leading ones.
1763 if (Val.isNegative())
1764 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001765
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001766 // Return # leading zeros.
1767 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001768 }
1769
1770 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001771 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001772 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1773
1774 case ISD::SIGN_EXTEND_INREG:
1775 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001776 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001777 Tmp = VTBits-Tmp+1;
1778
1779 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1780 return std::max(Tmp, Tmp2);
1781
1782 case ISD::SRA:
1783 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1784 // SRA X, C -> adds C sign bits.
1785 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1786 Tmp += C->getValue();
1787 if (Tmp > VTBits) Tmp = VTBits;
1788 }
1789 return Tmp;
1790 case ISD::SHL:
1791 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1792 // shl destroys sign bits.
1793 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1794 if (C->getValue() >= VTBits || // Bad shift.
1795 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1796 return Tmp - C->getValue();
1797 }
1798 break;
1799 case ISD::AND:
1800 case ISD::OR:
1801 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001802 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001803 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001804 if (Tmp != 1) {
1805 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1806 FirstAnswer = std::min(Tmp, Tmp2);
1807 // We computed what we know about the sign bits as our first
1808 // answer. Now proceed to the generic code that uses
1809 // ComputeMaskedBits, and pick whichever answer is better.
1810 }
1811 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001812
1813 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001814 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001815 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001816 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001817 return std::min(Tmp, Tmp2);
1818
1819 case ISD::SETCC:
1820 // If setcc returns 0/-1, all bits are sign bits.
1821 if (TLI.getSetCCResultContents() ==
1822 TargetLowering::ZeroOrNegativeOneSetCCResult)
1823 return VTBits;
1824 break;
1825 case ISD::ROTL:
1826 case ISD::ROTR:
1827 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1828 unsigned RotAmt = C->getValue() & (VTBits-1);
1829
1830 // Handle rotate right by N like a rotate left by 32-N.
1831 if (Op.getOpcode() == ISD::ROTR)
1832 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1833
1834 // If we aren't rotating out all of the known-in sign bits, return the
1835 // number that are left. This handles rotl(sext(x), 1) for example.
1836 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1837 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1838 }
1839 break;
1840 case ISD::ADD:
1841 // Add can have at most one carry bit. Thus we know that the output
1842 // is, at worst, one more bit than the inputs.
1843 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1844 if (Tmp == 1) return 1; // Early out.
1845
1846 // Special case decrementing a value (ADD X, -1):
1847 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1848 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001849 APInt KnownZero, KnownOne;
1850 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001851 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1852
1853 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1854 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001855 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001856 return VTBits;
1857
1858 // If we are subtracting one from a positive number, there is no carry
1859 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001860 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001861 return Tmp;
1862 }
1863
1864 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1865 if (Tmp2 == 1) return 1;
1866 return std::min(Tmp, Tmp2)-1;
1867 break;
1868
1869 case ISD::SUB:
1870 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1871 if (Tmp2 == 1) return 1;
1872
1873 // Handle NEG.
1874 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001875 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001876 APInt KnownZero, KnownOne;
1877 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001878 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1879 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1880 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001881 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001882 return VTBits;
1883
1884 // If the input is known to be positive (the sign bit is known clear),
1885 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001886 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001887 return Tmp2;
1888
1889 // Otherwise, we treat this like a SUB.
1890 }
1891
1892 // Sub can have at most one carry bit. Thus we know that the output
1893 // is, at worst, one more bit than the inputs.
1894 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1895 if (Tmp == 1) return 1; // Early out.
1896 return std::min(Tmp, Tmp2)-1;
1897 break;
1898 case ISD::TRUNCATE:
1899 // FIXME: it's tricky to do anything useful for this, but it is an important
1900 // case for targets like X86.
1901 break;
1902 }
1903
1904 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1905 if (Op.getOpcode() == ISD::LOAD) {
1906 LoadSDNode *LD = cast<LoadSDNode>(Op);
1907 unsigned ExtType = LD->getExtensionType();
1908 switch (ExtType) {
1909 default: break;
1910 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001911 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001912 return VTBits-Tmp+1;
1913 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001914 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001915 return VTBits-Tmp;
1916 }
1917 }
1918
1919 // Allow the target to implement this method for its nodes.
1920 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1921 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1922 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1923 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1924 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001925 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001926 }
1927
1928 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1929 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001930 APInt KnownZero, KnownOne;
1931 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001932 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1933
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001934 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001935 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001936 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001937 Mask = KnownOne;
1938 } else {
1939 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001940 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001941 }
1942
1943 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1944 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001945 Mask = ~Mask;
1946 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001947 // Return # leading zeros. We use 'min' here in case Val was zero before
1948 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001949 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001950}
1951
Chris Lattner51dabfb2006-10-14 00:41:01 +00001952
Dan Gohman475871a2008-07-27 21:46:04 +00001953bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00001954 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1955 if (!GA) return false;
1956 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1957 if (!GV) return false;
1958 MachineModuleInfo *MMI = getMachineModuleInfo();
1959 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1960}
1961
1962
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001963/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1964/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00001965SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001966 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001967 SDValue PermMask = N->getOperand(2);
1968 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00001969 if (Idx.getOpcode() == ISD::UNDEF)
1970 return getNode(ISD::UNDEF, VT.getVectorElementType());
1971 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001972 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001973 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00001974 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001975
1976 if (V.getOpcode() == ISD::BIT_CONVERT) {
1977 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001978 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00001979 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001980 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001981 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001982 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001983 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001984 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001985 return V.getOperand(Index);
1986 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1987 return getShuffleScalarElt(V.Val, Index);
Dan Gohman475871a2008-07-27 21:46:04 +00001988 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001989}
1990
1991
Chris Lattnerc3aae252005-01-07 07:46:32 +00001992/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001993///
Dan Gohman475871a2008-07-27 21:46:04 +00001994SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001995 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001996 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001997 void *IP = 0;
1998 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001999 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002000 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002001 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002002 CSEMap.InsertNode(N, IP);
2003
2004 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002005#ifndef NDEBUG
2006 VerifyNode(N);
2007#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002008 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002009}
2010
Dan Gohman475871a2008-07-27 21:46:04 +00002011SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002012 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00002014 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002015 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002016 switch (Opcode) {
2017 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002018 case ISD::SIGN_EXTEND:
2019 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002020 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002021 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002022 case ISD::TRUNCATE:
2023 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002024 case ISD::UINT_TO_FP:
2025 case ISD::SINT_TO_FP: {
2026 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002027 // No compile time operations on this type.
2028 if (VT==MVT::ppcf128)
2029 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002030 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2031 (void)apf.convertFromAPInt(Val,
2032 Opcode==ISD::SINT_TO_FP,
2033 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002034 return getConstantFP(apf, VT);
2035 }
Chris Lattner94683772005-12-23 05:30:37 +00002036 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002037 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002038 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002039 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002040 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002041 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002042 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002043 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002044 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002045 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002046 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002047 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002048 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002049 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002050 }
2051 }
2052
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002053 // Constant fold unary operations with a floating point constant operand.
2054 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2055 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002056 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002057 switch (Opcode) {
2058 case ISD::FNEG:
2059 V.changeSign();
2060 return getConstantFP(V, VT);
2061 case ISD::FABS:
2062 V.clearSign();
2063 return getConstantFP(V, VT);
2064 case ISD::FP_ROUND:
2065 case ISD::FP_EXTEND:
2066 // This can return overflow, underflow, or inexact; we don't care.
2067 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002068 (void)V.convert(*MVTToAPFloatSemantics(VT),
2069 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002070 return getConstantFP(V, VT);
2071 case ISD::FP_TO_SINT:
2072 case ISD::FP_TO_UINT: {
2073 integerPart x;
2074 assert(integerPartWidth >= 64);
2075 // FIXME need to be more flexible about rounding mode.
2076 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2077 Opcode==ISD::FP_TO_SINT,
2078 APFloat::rmTowardZero);
2079 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2080 break;
2081 return getConstant(x, VT);
2082 }
2083 case ISD::BIT_CONVERT:
2084 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2085 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2086 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2087 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002088 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002089 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002090 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002091 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002092
2093 unsigned OpOpcode = Operand.Val->getOpcode();
2094 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002095 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002096 case ISD::CONCAT_VECTORS:
2097 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002098 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002099 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002100 assert(VT.isFloatingPoint() &&
2101 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002102 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002103 if (Operand.getOpcode() == ISD::UNDEF)
2104 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002105 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002106 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002107 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002108 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002109 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002110 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002111 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002112 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2113 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2114 break;
2115 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002116 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002117 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002118 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002119 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002120 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002121 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002122 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002123 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002124 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002125 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002126 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002127 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002128 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002129 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002130 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2131 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2132 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2133 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002134 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002135 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002136 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002137 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002138 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002139 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002140 if (OpOpcode == ISD::TRUNCATE)
2141 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002142 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2143 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002144 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002145 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002146 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002147 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002148 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2149 else
2150 return Operand.Val->getOperand(0);
2151 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002152 break;
Chris Lattner35481892005-12-23 00:16:34 +00002153 case ISD::BIT_CONVERT:
2154 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002155 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002156 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002157 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002158 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2159 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002160 if (OpOpcode == ISD::UNDEF)
2161 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002162 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002163 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002164 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2165 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002166 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002167 if (OpOpcode == ISD::UNDEF)
2168 return getNode(ISD::UNDEF, VT);
2169 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2170 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2171 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2172 Operand.getConstantOperandVal(1) == 0 &&
2173 Operand.getOperand(0).getValueType() == VT)
2174 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002175 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002176 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002177 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2178 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002179 Operand.Val->getOperand(0));
2180 if (OpOpcode == ISD::FNEG) // --X -> X
2181 return Operand.Val->getOperand(0);
2182 break;
2183 case ISD::FABS:
2184 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2185 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2186 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002187 }
2188
Chris Lattner43247a12005-08-25 19:12:10 +00002189 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002190 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002191 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002192 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002193 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002194 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002195 void *IP = 0;
2196 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002197 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002198 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002199 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002200 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002201 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002202 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002203 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002204 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002205
Chris Lattnerc3aae252005-01-07 07:46:32 +00002206 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002207#ifndef NDEBUG
2208 VerifyNode(N);
2209#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002210 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002211}
2212
Dan Gohman475871a2008-07-27 21:46:04 +00002213SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2214 SDValue N1, SDValue N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002215 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2216 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002217 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002218 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002219 case ISD::TokenFactor:
2220 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2221 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002222 // Fold trivial token factors.
2223 if (N1.getOpcode() == ISD::EntryToken) return N2;
2224 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002225 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002226 case ISD::CONCAT_VECTORS:
2227 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2228 // one big BUILD_VECTOR.
2229 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2230 N2.getOpcode() == ISD::BUILD_VECTOR) {
2231 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2232 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2233 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2234 }
2235 break;
Chris Lattner76365122005-01-16 02:23:22 +00002236 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002237 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002238 N1.getValueType() == VT && "Binary operator types must match!");
2239 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2240 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002241 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002242 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002243 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2244 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002245 break;
Chris Lattner76365122005-01-16 02:23:22 +00002246 case ISD::OR:
2247 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002248 case ISD::ADD:
2249 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002250 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002251 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002252 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2253 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002254 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002255 return N1;
2256 break;
Chris Lattner76365122005-01-16 02:23:22 +00002257 case ISD::UDIV:
2258 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002259 case ISD::MULHU:
2260 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002262 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002263 case ISD::MUL:
2264 case ISD::SDIV:
2265 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002266 case ISD::FADD:
2267 case ISD::FSUB:
2268 case ISD::FMUL:
2269 case ISD::FDIV:
2270 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002271 assert(N1.getValueType() == N2.getValueType() &&
2272 N1.getValueType() == VT && "Binary operator types must match!");
2273 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002274 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2275 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002276 N1.getValueType().isFloatingPoint() &&
2277 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002278 "Invalid FCOPYSIGN!");
2279 break;
Chris Lattner76365122005-01-16 02:23:22 +00002280 case ISD::SHL:
2281 case ISD::SRA:
2282 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002283 case ISD::ROTL:
2284 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002285 assert(VT == N1.getValueType() &&
2286 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002287 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002288 "Shifts only work on integers");
2289
2290 // Always fold shifts of i1 values so the code generator doesn't need to
2291 // handle them. Since we know the size of the shift has to be less than the
2292 // size of the value, the shift/rotate count is guaranteed to be zero.
2293 if (VT == MVT::i1)
2294 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002295 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002296 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002297 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002298 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002299 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002300 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002301 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002302 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002303 break;
2304 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002305 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002306 assert(VT.isFloatingPoint() &&
2307 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002308 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002309 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002310 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002311 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002312 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002313 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002314 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002315 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002316 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002317 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002318 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002319 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002320 break;
2321 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002322 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002323 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002324 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002325 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002326 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002327 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002328 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002329
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002330 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002331 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002332 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002333 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002334 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002335 return getConstant(Val, VT);
2336 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002337 break;
2338 }
2339 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002340 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2341 if (N1.getOpcode() == ISD::UNDEF)
2342 return getNode(ISD::UNDEF, VT);
2343
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002344 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2345 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002346 if (N2C &&
2347 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002348 N1.getNumOperands() > 0) {
2349 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002350 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002351 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2352 N1.getOperand(N2C->getValue() / Factor),
2353 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2354 }
2355
2356 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2357 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002358 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002359 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002360
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002361 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2362 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002363 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2364 if (N1.getOperand(2) == N2)
2365 return N1.getOperand(1);
2366 else
2367 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2368 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002369 break;
2370 case ISD::EXTRACT_ELEMENT:
2371 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002372 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2373 (N1.getValueType().isInteger() == VT.isInteger()) &&
2374 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002375
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002376 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2377 // 64-bit integers into 32-bit parts. Instead of building the extract of
2378 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2379 if (N1.getOpcode() == ISD::BUILD_PAIR)
2380 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002381
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002382 // EXTRACT_ELEMENT of a constant int is also very common.
2383 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002384 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002385 unsigned Shift = ElementSize * N2C->getValue();
2386 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2387 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002388 }
2389 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002390 case ISD::EXTRACT_SUBVECTOR:
2391 if (N1.getValueType() == VT) // Trivial extraction.
2392 return N1;
2393 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002394 }
2395
2396 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002397 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002398 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002399 switch (Opcode) {
2400 case ISD::ADD: return getConstant(C1 + C2, VT);
2401 case ISD::SUB: return getConstant(C1 - C2, VT);
2402 case ISD::MUL: return getConstant(C1 * C2, VT);
2403 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002404 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002405 break;
2406 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002407 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002408 break;
2409 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002410 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002411 break;
2412 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002413 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002414 break;
2415 case ISD::AND : return getConstant(C1 & C2, VT);
2416 case ISD::OR : return getConstant(C1 | C2, VT);
2417 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002418 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002419 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2420 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2421 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2422 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002423 default: break;
2424 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002425 } else { // Cannonicalize constant to RHS if commutative
2426 if (isCommutativeBinOp(Opcode)) {
2427 std::swap(N1C, N2C);
2428 std::swap(N1, N2);
2429 }
2430 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002431 }
2432
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002433 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002434 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2435 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002436 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002437 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2438 // Cannonicalize constant to RHS if commutative
2439 std::swap(N1CFP, N2CFP);
2440 std::swap(N1, N2);
2441 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002442 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2443 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002444 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002445 case ISD::FADD:
2446 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002447 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002448 return getConstantFP(V1, VT);
2449 break;
2450 case ISD::FSUB:
2451 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2452 if (s!=APFloat::opInvalidOp)
2453 return getConstantFP(V1, VT);
2454 break;
2455 case ISD::FMUL:
2456 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2457 if (s!=APFloat::opInvalidOp)
2458 return getConstantFP(V1, VT);
2459 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002460 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002461 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2462 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2463 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002464 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002465 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002466 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2467 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2468 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002469 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002470 case ISD::FCOPYSIGN:
2471 V1.copySign(V2);
2472 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002473 default: break;
2474 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002475 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002476 }
Chris Lattner62b57722006-04-20 05:39:12 +00002477
2478 // Canonicalize an UNDEF to the RHS, even over a constant.
2479 if (N1.getOpcode() == ISD::UNDEF) {
2480 if (isCommutativeBinOp(Opcode)) {
2481 std::swap(N1, N2);
2482 } else {
2483 switch (Opcode) {
2484 case ISD::FP_ROUND_INREG:
2485 case ISD::SIGN_EXTEND_INREG:
2486 case ISD::SUB:
2487 case ISD::FSUB:
2488 case ISD::FDIV:
2489 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002490 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002491 return N1; // fold op(undef, arg2) -> undef
2492 case ISD::UDIV:
2493 case ISD::SDIV:
2494 case ISD::UREM:
2495 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002496 case ISD::SRL:
2497 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002498 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002499 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2500 // For vectors, we can't easily build an all zero vector, just return
2501 // the LHS.
2502 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002503 }
2504 }
2505 }
2506
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002507 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002508 if (N2.getOpcode() == ISD::UNDEF) {
2509 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002510 case ISD::XOR:
2511 if (N1.getOpcode() == ISD::UNDEF)
2512 // Handle undef ^ undef -> 0 special case. This is a common
2513 // idiom (misuse).
2514 return getConstant(0, VT);
2515 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002516 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002517 case ISD::ADDC:
2518 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002519 case ISD::SUB:
2520 case ISD::FADD:
2521 case ISD::FSUB:
2522 case ISD::FMUL:
2523 case ISD::FDIV:
2524 case ISD::FREM:
2525 case ISD::UDIV:
2526 case ISD::SDIV:
2527 case ISD::UREM:
2528 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002529 return N2; // fold op(arg1, undef) -> undef
2530 case ISD::MUL:
2531 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002532 case ISD::SRL:
2533 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002534 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002535 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2536 // For vectors, we can't easily build an all zero vector, just return
2537 // the LHS.
2538 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002539 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002540 if (!VT.isVector())
2541 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002542 // For vectors, we can't easily build an all one vector, just return
2543 // the LHS.
2544 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002545 case ISD::SRA:
2546 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002547 }
2548 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002549
Chris Lattner27e9b412005-05-11 18:57:39 +00002550 // Memoize this node if possible.
2551 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002552 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002553 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002554 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002555 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002556 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002557 void *IP = 0;
2558 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002559 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002560 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002561 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002562 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002563 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002564 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002565 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002566 }
2567
Chris Lattnerc3aae252005-01-07 07:46:32 +00002568 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002569#ifndef NDEBUG
2570 VerifyNode(N);
2571#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002572 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002573}
2574
Dan Gohman475871a2008-07-27 21:46:04 +00002575SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2576 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002577 // Perform various simplifications.
2578 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2579 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002580 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002581 case ISD::CONCAT_VECTORS:
2582 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2583 // one big BUILD_VECTOR.
2584 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2585 N2.getOpcode() == ISD::BUILD_VECTOR &&
2586 N3.getOpcode() == ISD::BUILD_VECTOR) {
2587 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2588 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2589 Elts.insert(Elts.end(), N3.Val->op_begin(), N3.Val->op_end());
2590 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2591 }
2592 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002593 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002594 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002595 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002596 if (Simp.Val) return Simp;
2597 break;
2598 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002599 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002600 if (N1C) {
2601 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002602 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002603 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002604 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002605 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002606
2607 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002608 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002609 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002610 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002611 if (N2C->getValue()) // Unconditional branch
2612 return getNode(ISD::BR, MVT::Other, N1, N3);
2613 else
2614 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002615 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002616 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002617 case ISD::VECTOR_SHUFFLE:
2618 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002619 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002620 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002621 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002622 "Illegal VECTOR_SHUFFLE node!");
2623 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002624 case ISD::BIT_CONVERT:
2625 // Fold bit_convert nodes from a type to themselves.
2626 if (N1.getValueType() == VT)
2627 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002628 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002629 }
2630
Chris Lattner43247a12005-08-25 19:12:10 +00002631 // Memoize node if it doesn't produce a flag.
2632 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002633 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002634 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002635 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002636 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002637 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002638 void *IP = 0;
2639 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002640 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002641 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002642 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002643 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002644 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002645 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002646 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002647 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002648 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002649#ifndef NDEBUG
2650 VerifyNode(N);
2651#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002652 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002653}
2654
Dan Gohman475871a2008-07-27 21:46:04 +00002655SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2656 SDValue N1, SDValue N2, SDValue N3,
2657 SDValue N4) {
2658 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002659 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002660}
2661
Dan Gohman475871a2008-07-27 21:46:04 +00002662SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2663 SDValue N1, SDValue N2, SDValue N3,
2664 SDValue N4, SDValue N5) {
2665 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002666 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002667}
2668
Dan Gohman707e0182008-04-12 04:36:06 +00002669/// getMemsetValue - Vectorized representation of the memset value
2670/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002671static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002672 unsigned NumBits = VT.isVector() ?
2673 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002674 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002675 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002676 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002677 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002678 Val = (Val << Shift) | Val;
2679 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002680 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002681 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002682 return DAG.getConstant(Val, VT);
2683 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002684 }
Evan Chengf0df0312008-05-15 08:39:06 +00002685
2686 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2687 unsigned Shift = 8;
2688 for (unsigned i = NumBits; i > 8; i >>= 1) {
2689 Value = DAG.getNode(ISD::OR, VT,
2690 DAG.getNode(ISD::SHL, VT, Value,
2691 DAG.getConstant(Shift, MVT::i8)), Value);
2692 Shift <<= 1;
2693 }
2694
2695 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002696}
2697
Dan Gohman707e0182008-04-12 04:36:06 +00002698/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2699/// used when a memcpy is turned into a memset when the source is a constant
2700/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002701static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002702 const TargetLowering &TLI,
2703 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002704 // Handle vector with all elements zero.
2705 if (Str.empty()) {
2706 if (VT.isInteger())
2707 return DAG.getConstant(0, VT);
2708 unsigned NumElts = VT.getVectorNumElements();
2709 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2710 return DAG.getNode(ISD::BIT_CONVERT, VT,
2711 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2712 }
2713
Duncan Sands83ec4b62008-06-06 12:08:01 +00002714 assert(!VT.isVector() && "Can't handle vector type here!");
2715 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002716 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002717 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002718 if (TLI.isLittleEndian())
2719 Offset = Offset + MSB - 1;
2720 for (unsigned i = 0; i != MSB; ++i) {
2721 Val = (Val << 8) | (unsigned char)Str[Offset];
2722 Offset += TLI.isLittleEndian() ? -1 : 1;
2723 }
2724 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002725}
2726
Dan Gohman707e0182008-04-12 04:36:06 +00002727/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002728///
Dan Gohman475871a2008-07-27 21:46:04 +00002729static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002730 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002731 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002732 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2733}
2734
Evan Chengf0df0312008-05-15 08:39:06 +00002735/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2736///
Dan Gohman475871a2008-07-27 21:46:04 +00002737static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002738 unsigned SrcDelta = 0;
2739 GlobalAddressSDNode *G = NULL;
2740 if (Src.getOpcode() == ISD::GlobalAddress)
2741 G = cast<GlobalAddressSDNode>(Src);
2742 else if (Src.getOpcode() == ISD::ADD &&
2743 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2744 Src.getOperand(1).getOpcode() == ISD::Constant) {
2745 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2746 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2747 }
2748 if (!G)
2749 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002750
Evan Chengf0df0312008-05-15 08:39:06 +00002751 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002752 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2753 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002754
Evan Chengf0df0312008-05-15 08:39:06 +00002755 return false;
2756}
Dan Gohman707e0182008-04-12 04:36:06 +00002757
Evan Chengf0df0312008-05-15 08:39:06 +00002758/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2759/// to replace the memset / memcpy is below the threshold. It also returns the
2760/// types of the sequence of memory ops to perform memset / memcpy.
2761static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002762bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002763 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002764 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002765 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002766 SelectionDAG &DAG,
2767 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002768 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002769 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002770 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002771 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002772 if (VT != MVT::iAny) {
2773 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002774 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002775 // If source is a string constant, this will require an unaligned load.
2776 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2777 if (Dst.getOpcode() != ISD::FrameIndex) {
2778 // Can't change destination alignment. It requires a unaligned store.
2779 if (AllowUnalign)
2780 VT = MVT::iAny;
2781 } else {
2782 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2783 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2784 if (MFI->isFixedObjectIndex(FI)) {
2785 // Can't change destination alignment. It requires a unaligned store.
2786 if (AllowUnalign)
2787 VT = MVT::iAny;
2788 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002789 // Give the stack frame object a larger alignment if needed.
2790 if (MFI->getObjectAlignment(FI) < NewAlign)
2791 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002792 Align = NewAlign;
2793 }
2794 }
2795 }
2796 }
2797
2798 if (VT == MVT::iAny) {
2799 if (AllowUnalign) {
2800 VT = MVT::i64;
2801 } else {
2802 switch (Align & 7) {
2803 case 0: VT = MVT::i64; break;
2804 case 4: VT = MVT::i32; break;
2805 case 2: VT = MVT::i16; break;
2806 default: VT = MVT::i8; break;
2807 }
2808 }
2809
Duncan Sands83ec4b62008-06-06 12:08:01 +00002810 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002811 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002812 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2813 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002814
Duncan Sands8e4eb092008-06-08 20:54:56 +00002815 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002816 VT = LVT;
2817 }
Dan Gohman707e0182008-04-12 04:36:06 +00002818
2819 unsigned NumMemOps = 0;
2820 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002821 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002822 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002823 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002824 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002825 VT = MVT::i64;
2826 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002827 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2828 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002829 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002830 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002831 VTSize >>= 1;
2832 }
Dan Gohman707e0182008-04-12 04:36:06 +00002833 }
Dan Gohman707e0182008-04-12 04:36:06 +00002834
2835 if (++NumMemOps > Limit)
2836 return false;
2837 MemOps.push_back(VT);
2838 Size -= VTSize;
2839 }
2840
2841 return true;
2842}
2843
Dan Gohman475871a2008-07-27 21:46:04 +00002844static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2845 SDValue Chain, SDValue Dst,
2846 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002847 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002848 const Value *DstSV, uint64_t DstSVOff,
2849 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002850 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2851
Dan Gohman21323f32008-05-29 19:42:22 +00002852 // Expand memcpy to a series of load and store ops if the size operand falls
2853 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002854 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002855 uint64_t Limit = -1;
2856 if (!AlwaysInline)
2857 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002858 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002859 std::string Str;
2860 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002861 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002862 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002863 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002864
Dan Gohman707e0182008-04-12 04:36:06 +00002865
Evan Cheng0ff39b32008-06-30 07:31:25 +00002866 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002867 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002868 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002869 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002870 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002871 MVT VT = MemOps[i];
2872 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002873 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002874
Evan Cheng0ff39b32008-06-30 07:31:25 +00002875 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002876 // It's unlikely a store of a vector immediate can be done in a single
2877 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002878 // We also handle store a vector with all zero's.
2879 // FIXME: Handle other cases where store of vector immediate is done in
2880 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002881 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002882 Store = DAG.getStore(Chain, Value,
2883 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002884 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002885 } else {
2886 Value = DAG.getLoad(VT, Chain,
2887 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002888 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002889 Store = DAG.getStore(Chain, Value,
2890 getMemBasePlusOffset(Dst, DstOff, DAG),
2891 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002892 }
2893 OutChains.push_back(Store);
2894 SrcOff += VTSize;
2895 DstOff += VTSize;
2896 }
2897
2898 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2899 &OutChains[0], OutChains.size());
2900}
2901
Dan Gohman475871a2008-07-27 21:46:04 +00002902static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2903 SDValue Chain, SDValue Dst,
2904 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002905 unsigned Align, bool AlwaysInline,
2906 const Value *DstSV, uint64_t DstSVOff,
2907 const Value *SrcSV, uint64_t SrcSVOff){
2908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2909
2910 // Expand memmove to a series of load and store ops if the size operand falls
2911 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002912 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002913 uint64_t Limit = -1;
2914 if (!AlwaysInline)
2915 Limit = TLI.getMaxStoresPerMemmove();
2916 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002917 std::string Str;
2918 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002919 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002920 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002921 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002922
Dan Gohman21323f32008-05-29 19:42:22 +00002923 uint64_t SrcOff = 0, DstOff = 0;
2924
Dan Gohman475871a2008-07-27 21:46:04 +00002925 SmallVector<SDValue, 8> LoadValues;
2926 SmallVector<SDValue, 8> LoadChains;
2927 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002928 unsigned NumMemOps = MemOps.size();
2929 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002930 MVT VT = MemOps[i];
2931 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002932 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002933
2934 Value = DAG.getLoad(VT, Chain,
2935 getMemBasePlusOffset(Src, SrcOff, DAG),
2936 SrcSV, SrcSVOff + SrcOff, false, Align);
2937 LoadValues.push_back(Value);
2938 LoadChains.push_back(Value.getValue(1));
2939 SrcOff += VTSize;
2940 }
2941 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2942 &LoadChains[0], LoadChains.size());
2943 OutChains.clear();
2944 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002945 MVT VT = MemOps[i];
2946 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002947 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002948
2949 Store = DAG.getStore(Chain, LoadValues[i],
2950 getMemBasePlusOffset(Dst, DstOff, DAG),
2951 DstSV, DstSVOff + DstOff, false, DstAlign);
2952 OutChains.push_back(Store);
2953 DstOff += VTSize;
2954 }
2955
2956 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2957 &OutChains[0], OutChains.size());
2958}
2959
Dan Gohman475871a2008-07-27 21:46:04 +00002960static SDValue getMemsetStores(SelectionDAG &DAG,
2961 SDValue Chain, SDValue Dst,
2962 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002963 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002964 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002965 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2966
2967 // Expand memset to a series of load/store ops if the size operand
2968 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002969 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002970 std::string Str;
2971 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002972 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002973 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002974 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002975
Dan Gohman475871a2008-07-27 21:46:04 +00002976 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002977 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002978
2979 unsigned NumMemOps = MemOps.size();
2980 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002981 MVT VT = MemOps[i];
2982 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002983 SDValue Value = getMemsetValue(Src, VT, DAG);
2984 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00002985 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002986 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002987 OutChains.push_back(Store);
2988 DstOff += VTSize;
2989 }
2990
2991 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2992 &OutChains[0], OutChains.size());
2993}
2994
Dan Gohman475871a2008-07-27 21:46:04 +00002995SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
2996 SDValue Src, SDValue Size,
2997 unsigned Align, bool AlwaysInline,
2998 const Value *DstSV, uint64_t DstSVOff,
2999 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003000
3001 // Check to see if we should lower the memcpy to loads and stores first.
3002 // For cases within the target-specified limits, this is the best choice.
3003 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3004 if (ConstantSize) {
3005 // Memcpy with size zero? Just return the original chain.
3006 if (ConstantSize->isNullValue())
3007 return Chain;
3008
Dan Gohman475871a2008-07-27 21:46:04 +00003009 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003010 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003011 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003012 if (Result.Val)
3013 return Result;
3014 }
3015
3016 // Then check to see if we should lower the memcpy with target-specific
3017 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003018 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003019 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3020 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003021 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003022 if (Result.Val)
3023 return Result;
3024
3025 // If we really need inline code and the target declined to provide it,
3026 // use a (potentially long) sequence of loads and stores.
3027 if (AlwaysInline) {
3028 assert(ConstantSize && "AlwaysInline requires a constant size!");
3029 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3030 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003031 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003032 }
3033
3034 // Emit a library call.
3035 TargetLowering::ArgListTy Args;
3036 TargetLowering::ArgListEntry Entry;
3037 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3038 Entry.Node = Dst; Args.push_back(Entry);
3039 Entry.Node = Src; Args.push_back(Entry);
3040 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003041 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003042 TLI.LowerCallTo(Chain, Type::VoidTy,
3043 false, false, false, CallingConv::C, false,
3044 getExternalSymbol("memcpy", TLI.getPointerTy()),
3045 Args, *this);
3046 return CallResult.second;
3047}
3048
Dan Gohman475871a2008-07-27 21:46:04 +00003049SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3050 SDValue Src, SDValue Size,
3051 unsigned Align,
3052 const Value *DstSV, uint64_t DstSVOff,
3053 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003054
Dan Gohman21323f32008-05-29 19:42:22 +00003055 // Check to see if we should lower the memmove to loads and stores first.
3056 // For cases within the target-specified limits, this is the best choice.
3057 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3058 if (ConstantSize) {
3059 // Memmove with size zero? Just return the original chain.
3060 if (ConstantSize->isNullValue())
3061 return Chain;
3062
Dan Gohman475871a2008-07-27 21:46:04 +00003063 SDValue Result =
Dan Gohman21323f32008-05-29 19:42:22 +00003064 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
3065 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
3066 if (Result.Val)
3067 return Result;
3068 }
Dan Gohman707e0182008-04-12 04:36:06 +00003069
3070 // Then check to see if we should lower the memmove with target-specific
3071 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003072 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003073 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003074 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003075 if (Result.Val)
3076 return Result;
3077
3078 // Emit a library call.
3079 TargetLowering::ArgListTy Args;
3080 TargetLowering::ArgListEntry Entry;
3081 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3082 Entry.Node = Dst; Args.push_back(Entry);
3083 Entry.Node = Src; Args.push_back(Entry);
3084 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003085 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003086 TLI.LowerCallTo(Chain, Type::VoidTy,
3087 false, false, false, CallingConv::C, false,
3088 getExternalSymbol("memmove", TLI.getPointerTy()),
3089 Args, *this);
3090 return CallResult.second;
3091}
3092
Dan Gohman475871a2008-07-27 21:46:04 +00003093SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3094 SDValue Src, SDValue Size,
3095 unsigned Align,
3096 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003097
3098 // Check to see if we should lower the memset to stores first.
3099 // For cases within the target-specified limits, this is the best choice.
3100 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3101 if (ConstantSize) {
3102 // Memset with size zero? Just return the original chain.
3103 if (ConstantSize->isNullValue())
3104 return Chain;
3105
Dan Gohman475871a2008-07-27 21:46:04 +00003106 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003107 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003108 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003109 if (Result.Val)
3110 return Result;
3111 }
3112
3113 // Then check to see if we should lower the memset with target-specific
3114 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003115 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003116 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003117 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003118 if (Result.Val)
3119 return Result;
3120
3121 // Emit a library call.
3122 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3123 TargetLowering::ArgListTy Args;
3124 TargetLowering::ArgListEntry Entry;
3125 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3126 Args.push_back(Entry);
3127 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003128 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003129 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3130 else
3131 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3132 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3133 Args.push_back(Entry);
3134 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3135 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003136 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003137 TLI.LowerCallTo(Chain, Type::VoidTy,
3138 false, false, false, CallingConv::C, false,
3139 getExternalSymbol("memset", TLI.getPointerTy()),
3140 Args, *this);
3141 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003142}
3143
Dan Gohman475871a2008-07-27 21:46:04 +00003144SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3145 SDValue Ptr, SDValue Cmp,
3146 SDValue Swp, const Value* PtrVal,
3147 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003148 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003149 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003150
3151 MVT VT = Cmp.getValueType();
3152
3153 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3154 Alignment = getMVTAlignment(VT);
3155
3156 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003157 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003158 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003159 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003160 void* IP = 0;
3161 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003162 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003163 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003164 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003165 CSEMap.InsertNode(N, IP);
3166 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003167 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003168}
3169
Dan Gohman475871a2008-07-27 21:46:04 +00003170SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3171 SDValue Ptr, SDValue Val,
3172 const Value* PtrVal,
3173 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003174 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003175 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3176 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003177 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003178 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3179 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003180 && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003181
3182 MVT VT = Val.getValueType();
3183
3184 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3185 Alignment = getMVTAlignment(VT);
3186
3187 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003188 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003189 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003190 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003191 void* IP = 0;
3192 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003193 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003194 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003195 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003196 CSEMap.InsertNode(N, IP);
3197 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003198 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003199}
3200
Duncan Sands4bdcb612008-07-02 17:40:58 +00003201/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3202/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003203SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3204 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003205 if (Simplify && NumOps == 1)
3206 return Ops[0];
3207
3208 SmallVector<MVT, 4> VTs;
3209 VTs.reserve(NumOps);
3210 for (unsigned i = 0; i < NumOps; ++i)
3211 VTs.push_back(Ops[i].getValueType());
3212 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3213}
3214
Dan Gohman475871a2008-07-27 21:46:04 +00003215SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003216SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003217 MVT VT, SDValue Chain,
3218 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003219 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003220 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003221 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3222 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003223
Duncan Sands14ea39c2008-03-27 20:23:40 +00003224 if (VT == EVT) {
3225 ExtType = ISD::NON_EXTLOAD;
3226 } else if (ExtType == ISD::NON_EXTLOAD) {
3227 assert(VT == EVT && "Non-extending load from different memory type!");
3228 } else {
3229 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003230 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003231 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3232 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003233 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003234 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003235 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003236 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003237 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003238 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003239 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003240 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003241
3242 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003243 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003244 "Unindexed load with an offset!");
3245
3246 SDVTList VTs = Indexed ?
3247 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003248 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003249 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003250 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003251 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003252 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003253 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003254 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003255 void *IP = 0;
3256 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003257 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003258 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003259 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3260 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003261 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003262 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003263 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003264}
3265
Dan Gohman475871a2008-07-27 21:46:04 +00003266SDValue SelectionDAG::getLoad(MVT VT,
3267 SDValue Chain, SDValue Ptr,
3268 const Value *SV, int SVOffset,
3269 bool isVolatile, unsigned Alignment) {
3270 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003271 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3272 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003273}
3274
Dan Gohman475871a2008-07-27 21:46:04 +00003275SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3276 SDValue Chain, SDValue Ptr,
3277 const Value *SV,
3278 int SVOffset, MVT EVT,
3279 bool isVolatile, unsigned Alignment) {
3280 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003281 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3282 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003283}
3284
Dan Gohman475871a2008-07-27 21:46:04 +00003285SDValue
3286SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3287 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003288 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003289 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3290 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003291 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3292 LD->getChain(), Base, Offset, LD->getSrcValue(),
3293 LD->getSrcValueOffset(), LD->getMemoryVT(),
3294 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003295}
3296
Dan Gohman475871a2008-07-27 21:46:04 +00003297SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3298 SDValue Ptr, const Value *SV, int SVOffset,
3299 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003300 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003301
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003302 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3303 Alignment = getMVTAlignment(VT);
3304
Evan Chengad071e12006-10-05 22:57:11 +00003305 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003306 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3307 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003308 FoldingSetNodeID ID;
3309 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003310 ID.AddInteger(ISD::UNINDEXED);
3311 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003312 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003313 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003314 void *IP = 0;
3315 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003316 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003317 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003318 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3319 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003320 CSEMap.InsertNode(N, IP);
3321 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003322 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003323}
3324
Dan Gohman475871a2008-07-27 21:46:04 +00003325SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3326 SDValue Ptr, const Value *SV,
3327 int SVOffset, MVT SVT,
3328 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003330
3331 if (VT == SVT)
3332 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003333
Duncan Sands8e4eb092008-06-08 20:54:56 +00003334 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003335 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003336 "Can't do FP-INT conversion!");
3337
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003338 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3339 Alignment = getMVTAlignment(VT);
3340
Evan Cheng8b2794a2006-10-13 21:14:26 +00003341 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003342 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3343 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003344 FoldingSetNodeID ID;
3345 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003346 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003347 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003348 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003349 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003350 void *IP = 0;
3351 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003352 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003353 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003354 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3355 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003356 CSEMap.InsertNode(N, IP);
3357 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003358 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003359}
3360
Dan Gohman475871a2008-07-27 21:46:04 +00003361SDValue
3362SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3363 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003364 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3365 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3366 "Store is already a indexed store!");
3367 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003368 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003369 FoldingSetNodeID ID;
3370 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3371 ID.AddInteger(AM);
3372 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003373 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003374 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003375 void *IP = 0;
3376 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003377 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003378 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003379 new (N) StoreSDNode(Ops, VTs, AM,
3380 ST->isTruncatingStore(), ST->getMemoryVT(),
3381 ST->getSrcValue(), ST->getSrcValueOffset(),
3382 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003383 CSEMap.InsertNode(N, IP);
3384 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003385 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003386}
3387
Dan Gohman475871a2008-07-27 21:46:04 +00003388SDValue SelectionDAG::getVAArg(MVT VT,
3389 SDValue Chain, SDValue Ptr,
3390 SDValue SV) {
3391 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003392 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003393}
3394
Dan Gohman475871a2008-07-27 21:46:04 +00003395SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3396 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003397 switch (NumOps) {
3398 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003399 case 1: return getNode(Opcode, VT, Ops[0]);
3400 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3401 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003402 default: break;
3403 }
3404
Dan Gohman475871a2008-07-27 21:46:04 +00003405 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003406 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003407 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3408 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003409}
3410
Dan Gohman475871a2008-07-27 21:46:04 +00003411SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3412 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003413 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003414 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003415 case 1: return getNode(Opcode, VT, Ops[0]);
3416 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3417 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003418 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003419 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003420
Chris Lattneref847df2005-04-09 03:27:28 +00003421 switch (Opcode) {
3422 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003423 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003424 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003425 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3426 "LHS and RHS of condition must have same type!");
3427 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3428 "True and False arms of SelectCC must have same type!");
3429 assert(Ops[2].getValueType() == VT &&
3430 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003431 break;
3432 }
3433 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003434 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003435 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3436 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003437 break;
3438 }
Chris Lattneref847df2005-04-09 03:27:28 +00003439 }
3440
Chris Lattner385328c2005-05-14 07:42:29 +00003441 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003442 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003443 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003444 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003445 FoldingSetNodeID ID;
3446 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003447 void *IP = 0;
3448 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003449 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003450 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003451 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003452 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003453 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003454 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003455 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003456 }
Chris Lattneref847df2005-04-09 03:27:28 +00003457 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003458#ifndef NDEBUG
3459 VerifyNode(N);
3460#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003461 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003462}
3463
Dan Gohman475871a2008-07-27 21:46:04 +00003464SDValue SelectionDAG::getNode(unsigned Opcode,
3465 const std::vector<MVT> &ResultTys,
3466 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003467 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3468 Ops, NumOps);
3469}
3470
Dan Gohman475871a2008-07-27 21:46:04 +00003471SDValue SelectionDAG::getNode(unsigned Opcode,
3472 const MVT *VTs, unsigned NumVTs,
3473 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003474 if (NumVTs == 1)
3475 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003476 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3477}
3478
Dan Gohman475871a2008-07-27 21:46:04 +00003479SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3480 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003481 if (VTList.NumVTs == 1)
3482 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003483
Chris Lattner5f056bf2005-07-10 01:55:33 +00003484 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003485 // FIXME: figure out how to safely handle things like
3486 // int foo(int x) { return 1 << (x & 255); }
3487 // int bar() { return foo(256); }
3488#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003489 case ISD::SRA_PARTS:
3490 case ISD::SRL_PARTS:
3491 case ISD::SHL_PARTS:
3492 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003493 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003494 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3495 else if (N3.getOpcode() == ISD::AND)
3496 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3497 // If the and is only masking out bits that cannot effect the shift,
3498 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003499 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003500 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3501 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3502 }
3503 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003504#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003505 }
Chris Lattner89c34632005-05-14 06:20:26 +00003506
Chris Lattner43247a12005-08-25 19:12:10 +00003507 // Memoize the node unless it returns a flag.
3508 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003509 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003510 FoldingSetNodeID ID;
3511 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003512 void *IP = 0;
3513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003514 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003515 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003516 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003517 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3518 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003519 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003520 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3521 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003522 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003523 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3524 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003525 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003526 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3527 }
Chris Lattnera5682852006-08-07 23:03:03 +00003528 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003529 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003530 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003531 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003532 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3533 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003534 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003535 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3536 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003537 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003538 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3539 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003540 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003541 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3542 }
Chris Lattner43247a12005-08-25 19:12:10 +00003543 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003544 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003545#ifndef NDEBUG
3546 VerifyNode(N);
3547#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003548 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003549}
3550
Dan Gohman475871a2008-07-27 21:46:04 +00003551SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003552 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003553}
3554
Dan Gohman475871a2008-07-27 21:46:04 +00003555SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3556 SDValue N1) {
3557 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003558 return getNode(Opcode, VTList, Ops, 1);
3559}
3560
Dan Gohman475871a2008-07-27 21:46:04 +00003561SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3562 SDValue N1, SDValue N2) {
3563 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003564 return getNode(Opcode, VTList, Ops, 2);
3565}
3566
Dan Gohman475871a2008-07-27 21:46:04 +00003567SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3568 SDValue N1, SDValue N2, SDValue N3) {
3569 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003570 return getNode(Opcode, VTList, Ops, 3);
3571}
3572
Dan Gohman475871a2008-07-27 21:46:04 +00003573SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3574 SDValue N1, SDValue N2, SDValue N3,
3575 SDValue N4) {
3576 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003577 return getNode(Opcode, VTList, Ops, 4);
3578}
3579
Dan Gohman475871a2008-07-27 21:46:04 +00003580SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3581 SDValue N1, SDValue N2, SDValue N3,
3582 SDValue N4, SDValue N5) {
3583 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003584 return getNode(Opcode, VTList, Ops, 5);
3585}
3586
Duncan Sands83ec4b62008-06-06 12:08:01 +00003587SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003588 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003589}
3590
Duncan Sands83ec4b62008-06-06 12:08:01 +00003591SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003592 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3593 E = VTList.rend(); I != E; ++I)
3594 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3595 return *I;
3596
3597 MVT *Array = Allocator.Allocate<MVT>(2);
3598 Array[0] = VT1;
3599 Array[1] = VT2;
3600 SDVTList Result = makeVTList(Array, 2);
3601 VTList.push_back(Result);
3602 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003603}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003604
3605SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3606 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3607 E = VTList.rend(); I != E; ++I)
3608 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3609 I->VTs[2] == VT3)
3610 return *I;
3611
3612 MVT *Array = Allocator.Allocate<MVT>(3);
3613 Array[0] = VT1;
3614 Array[1] = VT2;
3615 Array[2] = VT3;
3616 SDVTList Result = makeVTList(Array, 3);
3617 VTList.push_back(Result);
3618 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003619}
3620
Duncan Sands83ec4b62008-06-06 12:08:01 +00003621SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003622 switch (NumVTs) {
3623 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003624 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003625 case 2: return getVTList(VTs[0], VTs[1]);
3626 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3627 default: break;
3628 }
3629
Dan Gohmane8be6c62008-07-17 19:10:17 +00003630 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3631 E = VTList.rend(); I != E; ++I) {
3632 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3633 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003634
3635 bool NoMatch = false;
3636 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003637 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003638 NoMatch = true;
3639 break;
3640 }
3641 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003642 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003643 }
3644
Dan Gohmane8be6c62008-07-17 19:10:17 +00003645 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3646 std::copy(VTs, VTs+NumVTs, Array);
3647 SDVTList Result = makeVTList(Array, NumVTs);
3648 VTList.push_back(Result);
3649 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003650}
3651
3652
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003653/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3654/// specified operands. If the resultant node already exists in the DAG,
3655/// this does not modify the specified node, instead it returns the node that
3656/// already exists. If the resultant node does not exist in the DAG, the
3657/// input node is returned. As a degenerate case, if you specify the same
3658/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003659SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003660 SDNode *N = InN.Val;
3661 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3662
3663 // Check to see if there is no change.
3664 if (Op == N->getOperand(0)) return InN;
3665
3666 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003667 void *InsertPos = 0;
3668 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003669 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003670
Dan Gohman79acd2b2008-07-21 22:38:59 +00003671 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003672 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003673 RemoveNodeFromCSEMaps(N);
3674
3675 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003676 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003677 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003678 N->OperandList[0].setUser(N);
3679 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003680
3681 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003682 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003683 return InN;
3684}
3685
Dan Gohman475871a2008-07-27 21:46:04 +00003686SDValue SelectionDAG::
3687UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003688 SDNode *N = InN.Val;
3689 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3690
3691 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003692 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3693 return InN; // No operands changed, just return the input node.
3694
3695 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003696 void *InsertPos = 0;
3697 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003698 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003699
Dan Gohman79acd2b2008-07-21 22:38:59 +00003700 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003701 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003702 RemoveNodeFromCSEMaps(N);
3703
3704 // Now we update the operands.
3705 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003706 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003707 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003708 N->OperandList[0].setUser(N);
3709 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003710 }
3711 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003712 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003713 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003714 N->OperandList[1].setUser(N);
3715 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003716 }
3717
3718 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003719 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003720 return InN;
3721}
3722
Dan Gohman475871a2008-07-27 21:46:04 +00003723SDValue SelectionDAG::
3724UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3725 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003726 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003727}
3728
Dan Gohman475871a2008-07-27 21:46:04 +00003729SDValue SelectionDAG::
3730UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3731 SDValue Op3, SDValue Op4) {
3732 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003733 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003734}
3735
Dan Gohman475871a2008-07-27 21:46:04 +00003736SDValue SelectionDAG::
3737UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3738 SDValue Op3, SDValue Op4, SDValue Op5) {
3739 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003740 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003741}
3742
Dan Gohman475871a2008-07-27 21:46:04 +00003743SDValue SelectionDAG::
3744UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003745 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003746 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003747 "Update with wrong number of operands");
3748
3749 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003750 bool AnyChange = false;
3751 for (unsigned i = 0; i != NumOps; ++i) {
3752 if (Ops[i] != N->getOperand(i)) {
3753 AnyChange = true;
3754 break;
3755 }
3756 }
3757
3758 // No operands changed, just return the input node.
3759 if (!AnyChange) return InN;
3760
3761 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003762 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003763 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003764 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003765
Dan Gohman7ceda162008-05-02 00:05:03 +00003766 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003767 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003768 RemoveNodeFromCSEMaps(N);
3769
3770 // Now we update the operands.
3771 for (unsigned i = 0; i != NumOps; ++i) {
3772 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003773 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003774 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003775 N->OperandList[i].setUser(N);
3776 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003777 }
3778 }
3779
3780 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003781 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003782 return InN;
3783}
3784
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003785/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003786/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003787void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003788 // Unlike the code in MorphNodeTo that does this, we don't need to
3789 // watch for dead nodes here.
3790 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3791 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3792
3793 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003794}
Chris Lattner149c58c2005-08-16 18:17:10 +00003795
Dan Gohmane8be6c62008-07-17 19:10:17 +00003796/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3797/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003798///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003799SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003800 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003801 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003802 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003803}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003804
Dan Gohmane8be6c62008-07-17 19:10:17 +00003805SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003806 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003807 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003808 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003809 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003810}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003811
Dan Gohmane8be6c62008-07-17 19:10:17 +00003812SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003813 MVT VT, SDValue Op1,
3814 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003815 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003816 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003817 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003818}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003819
Dan Gohmane8be6c62008-07-17 19:10:17 +00003820SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003821 MVT VT, SDValue Op1,
3822 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003823 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003824 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003825 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003826}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003827
Dan Gohmane8be6c62008-07-17 19:10:17 +00003828SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003829 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003830 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003831 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003832 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003833}
3834
Dan Gohmane8be6c62008-07-17 19:10:17 +00003835SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003836 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003837 unsigned NumOps) {
3838 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003839 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003840}
3841
Dan Gohmane8be6c62008-07-17 19:10:17 +00003842SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003843 MVT VT1, MVT VT2) {
3844 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003845 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003846}
3847
Dan Gohmane8be6c62008-07-17 19:10:17 +00003848SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003849 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003850 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003851 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003852 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003853}
3854
Dan Gohmane8be6c62008-07-17 19:10:17 +00003855SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003856 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003857 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003858 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003859 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003860 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003861}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003862
Dan Gohmane8be6c62008-07-17 19:10:17 +00003863SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003864 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003865 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003866 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003867 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003868 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003869}
3870
Dan Gohmane8be6c62008-07-17 19:10:17 +00003871SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003872 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003873 SDValue Op1, SDValue Op2,
3874 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003875 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003876 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003877 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003878}
3879
Dan Gohmane8be6c62008-07-17 19:10:17 +00003880SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003881 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003882 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003883 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3884}
3885
3886SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3887 MVT VT) {
3888 SDVTList VTs = getVTList(VT);
3889 return MorphNodeTo(N, Opc, VTs, 0, 0);
3890}
3891
3892SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003893 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003894 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003895 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003896 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3897}
3898
3899SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003900 MVT VT, SDValue Op1,
3901 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003902 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003903 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003904 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3905}
3906
3907SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003908 MVT VT, SDValue Op1,
3909 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003910 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003911 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003912 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3913}
3914
3915SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003916 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003917 unsigned NumOps) {
3918 SDVTList VTs = getVTList(VT);
3919 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3920}
3921
3922SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003923 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003924 unsigned NumOps) {
3925 SDVTList VTs = getVTList(VT1, VT2);
3926 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3927}
3928
3929SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3930 MVT VT1, MVT VT2) {
3931 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003932 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003933}
3934
3935SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3936 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003937 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003938 SDVTList VTs = getVTList(VT1, VT2, VT3);
3939 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3940}
3941
3942SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3943 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003944 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003945 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003946 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003947 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3948}
3949
3950SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3951 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003952 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003953 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003954 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003955 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3956}
3957
3958SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3959 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003960 SDValue Op1, SDValue Op2,
3961 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003962 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003963 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003964 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3965}
3966
3967/// MorphNodeTo - These *mutate* the specified node to have the specified
3968/// return type, opcode, and operands.
3969///
3970/// Note that MorphNodeTo returns the resultant node. If there is already a
3971/// node of the specified opcode and operands, it returns that node instead of
3972/// the current one.
3973///
3974/// Using MorphNodeTo is faster than creating a new node and swapping it in
3975/// with ReplaceAllUsesWith both because it often avoids allocating a new
3976/// node, and because it doesn't require CSE recalulation for any of
3977/// the node's users.
3978///
3979SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003980 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003981 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003982 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00003983 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00003984 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
3985 FoldingSetNodeID ID;
3986 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
3987 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3988 return ON;
3989 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003990
Chris Lattner0fb094f2005-11-19 01:44:53 +00003991 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003992
Dan Gohmane8be6c62008-07-17 19:10:17 +00003993 // Start the morphing.
3994 N->NodeType = Opc;
3995 N->ValueList = VTs.VTs;
3996 N->NumValues = VTs.NumVTs;
3997
3998 // Clear the operands list, updating used nodes to remove this from their
3999 // use list. Keep track of any operands that become dead as a result.
4000 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4001 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4002 I != E; ++I) {
4003 SDNode *Used = I->getVal();
4004 Used->removeUser(std::distance(B, I), N);
4005 if (Used->use_empty())
4006 DeadNodeSet.insert(Used);
4007 }
4008
4009 // If NumOps is larger than the # of operands we currently have, reallocate
4010 // the operand list.
4011 if (NumOps > N->NumOperands) {
4012 if (N->OperandsNeedDelete)
4013 delete[] N->OperandList;
4014 if (N->isMachineOpcode()) {
4015 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004016 // remainder of the current SelectionDAG iteration, so we can allocate
4017 // the operands directly out of a pool with no recycling metadata.
4018 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004019 N->OperandsNeedDelete = false;
4020 } else {
4021 N->OperandList = new SDUse[NumOps];
4022 N->OperandsNeedDelete = true;
4023 }
4024 }
4025
4026 // Assign the new operands.
4027 N->NumOperands = NumOps;
4028 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4029 N->OperandList[i] = Ops[i];
4030 N->OperandList[i].setUser(N);
4031 SDNode *ToUse = N->OperandList[i].getVal();
4032 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004033 }
4034
4035 // Delete any nodes that are still dead after adding the uses for the
4036 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004037 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004038 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4039 E = DeadNodeSet.end(); I != E; ++I)
4040 if ((*I)->use_empty())
4041 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004042 RemoveDeadNodes(DeadNodes);
4043
Dan Gohmane8be6c62008-07-17 19:10:17 +00004044 if (IP)
4045 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004046 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004047}
4048
Chris Lattner0fb094f2005-11-19 01:44:53 +00004049
Evan Cheng6ae46c42006-02-09 07:15:23 +00004050/// getTargetNode - These are used for target selectors to create a new node
4051/// with specified return type(s), target opcode, and operands.
4052///
4053/// Note that getTargetNode returns the resultant node. If there is already a
4054/// node of the specified opcode and operands, it returns that node instead of
4055/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004056SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004057 return getNode(~Opcode, VT).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004058}
Dan Gohman475871a2008-07-27 21:46:04 +00004059SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004060 return getNode(~Opcode, VT, Op1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004061}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004062SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004063 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004064 return getNode(~Opcode, VT, Op1, Op2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004065}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004066SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004067 SDValue Op1, SDValue Op2,
4068 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004069 return getNode(~Opcode, VT, Op1, Op2, Op3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004070}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004071SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004072 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004073 return getNode(~Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004074}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004075SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4076 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004077 SDValue Op;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004078 return getNode(~Opcode, VTs, 2, &Op, 0).Val;
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004079}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004080SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004081 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004082 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004083 return getNode(~Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004084}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004085SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004086 MVT VT2, SDValue Op1,
4087 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004088 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004089 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004090 return getNode(~Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004091}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004092SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004093 MVT VT2, SDValue Op1,
4094 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004095 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004096 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004097 return getNode(~Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004098}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004099SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004100 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004102 return getNode(~Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004103}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004104SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004105 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004106 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004107 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004108 return getNode(~Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004109}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004110SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004111 SDValue Op1, SDValue Op2,
4112 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004113 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004114 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004115 return getNode(~Opcode, VTs, 3, Ops, 3).Val;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004116}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004117SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004118 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004119 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004120 return getNode(~Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004121}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004122SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4123 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004124 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004125 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004126 VTList.push_back(VT1);
4127 VTList.push_back(VT2);
4128 VTList.push_back(VT3);
4129 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004130 const MVT *VTs = getNodeValueTypes(VTList);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004131 return getNode(~Opcode, VTs, 4, Ops, NumOps).Val;
Evan Cheng05e69c12007-09-12 23:39:49 +00004132}
Evan Cheng39305cf2007-10-05 01:10:49 +00004133SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004134 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004135 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004136 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004137 return getNode(~Opcode, VTs, ResultTys.size(),
Evan Cheng39305cf2007-10-05 01:10:49 +00004138 Ops, NumOps).Val;
4139}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004140
Evan Cheng08b11732008-03-22 01:55:50 +00004141/// getNodeIfExists - Get the specified node if it's already available, or
4142/// else return NULL.
4143SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004144 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004145 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4146 FoldingSetNodeID ID;
4147 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4148 void *IP = 0;
4149 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4150 return E;
4151 }
4152 return NULL;
4153}
4154
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004155
Evan Cheng99157a02006-08-07 22:13:29 +00004156/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004157/// This can cause recursive merging of nodes in the DAG.
4158///
Chris Lattner11d049c2008-02-03 03:35:22 +00004159/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004160///
Dan Gohman475871a2008-07-27 21:46:04 +00004161void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004162 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004163 SDNode *From = FromN.Val;
Gabor Greif99a6cb92008-08-26 22:36:50 +00004164 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004165 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004166 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004167
Chris Lattner8b8749f2005-08-17 19:00:20 +00004168 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004169 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004170 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004171
Chris Lattner8b8749f2005-08-17 19:00:20 +00004172 // This node is about to morph, remove its old self from the CSE maps.
4173 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004174 int operandNum = 0;
4175 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4176 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004177 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004178 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004179 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004180 I->setUser(U);
4181 To.Val->addUser(operandNum, U);
4182 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004183
4184 // Now that we have modified U, add it back to the CSE maps. If it already
4185 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004186 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004187 ReplaceAllUsesWith(U, Existing, UpdateListener);
4188 // U is now dead. Inform the listener if it exists and delete it.
4189 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004190 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004191 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004192 } else {
4193 // If the node doesn't already exist, we updated it. Inform a listener if
4194 // it exists.
4195 if (UpdateListener)
4196 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004197 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004198 }
4199}
4200
4201/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4202/// This can cause recursive merging of nodes in the DAG.
4203///
4204/// This version assumes From/To have matching types and numbers of result
4205/// values.
4206///
Chris Lattner1e111c72005-09-07 05:37:01 +00004207void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004208 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004209 assert(From->getVTList().VTs == To->getVTList().VTs &&
4210 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004211 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004212
4213 // Handle the trivial case.
4214 if (From == To)
4215 return;
4216
Chris Lattner8b52f212005-08-26 18:36:28 +00004217 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004218 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004219 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004220
Chris Lattner8b52f212005-08-26 18:36:28 +00004221 // This node is about to morph, remove its old self from the CSE maps.
4222 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004223 int operandNum = 0;
4224 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4225 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004226 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004227 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004228 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004229 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004230 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004231
Chris Lattner8b8749f2005-08-17 19:00:20 +00004232 // Now that we have modified U, add it back to the CSE maps. If it already
4233 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004234 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004235 ReplaceAllUsesWith(U, Existing, UpdateListener);
4236 // U is now dead. Inform the listener if it exists and delete it.
4237 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004238 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004239 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004240 } else {
4241 // If the node doesn't already exist, we updated it. Inform a listener if
4242 // it exists.
4243 if (UpdateListener)
4244 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004245 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004246 }
4247}
4248
Chris Lattner8b52f212005-08-26 18:36:28 +00004249/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4250/// This can cause recursive merging of nodes in the DAG.
4251///
4252/// This version can replace From with any result values. To must match the
4253/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004254void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004255 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004256 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004257 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004258 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004259
4260 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004261 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004262 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004263
Chris Lattner7b2880c2005-08-24 22:44:39 +00004264 // This node is about to morph, remove its old self from the CSE maps.
4265 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004266 int operandNum = 0;
4267 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4268 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004269 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004270 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004271 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004272 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004273 I->setUser(U);
4274 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004275 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004276
Chris Lattner7b2880c2005-08-24 22:44:39 +00004277 // Now that we have modified U, add it back to the CSE maps. If it already
4278 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004279 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004280 ReplaceAllUsesWith(U, Existing, UpdateListener);
4281 // U is now dead. Inform the listener if it exists and delete it.
4282 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004283 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004284 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004285 } else {
4286 // If the node doesn't already exist, we updated it. Inform a listener if
4287 // it exists.
4288 if (UpdateListener)
4289 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004290 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004291 }
4292}
4293
Chris Lattner012f2412006-02-17 21:58:01 +00004294/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4295/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004296/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004297void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004298 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004299 // Handle the really simple, really trivial case efficiently.
4300 if (From == To) return;
4301
Chris Lattner012f2412006-02-17 21:58:01 +00004302 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004303 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004304 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004305 return;
4306 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004307
Chris Lattnercf5640b2007-02-04 00:14:31 +00004308 // Get all of the users of From.Val. We want these in a nice,
4309 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Dan Gohman89684502008-07-27 20:43:25 +00004310 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004311
4312 while (!Users.empty()) {
4313 // We know that this user uses some value of From. If it is the right
4314 // value, update it.
4315 SDNode *User = Users.back();
4316 Users.pop_back();
4317
Chris Lattner01d029b2007-10-15 06:10:22 +00004318 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004319 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004320 for (; Op != E; ++Op)
4321 if (*Op == From) break;
4322
4323 // If there are no matches, the user must use some other result of From.
4324 if (Op == E) continue;
4325
4326 // Okay, we know this user needs to be updated. Remove its old self
4327 // from the CSE maps.
4328 RemoveNodeFromCSEMaps(User);
4329
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004330 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004331 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004332 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004333 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004334 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004335 Op->setUser(User);
4336 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004337 }
4338 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004339
4340 // Now that we have modified User, add it back to the CSE maps. If it
4341 // already exists there, recursively merge the results together.
4342 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004343 if (!Existing) {
4344 if (UpdateListener) UpdateListener->NodeUpdated(User);
4345 continue; // Continue on to next user.
4346 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004347
4348 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004349 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004350 // recursive merging of other unrelated nodes down the line.
4351 ReplaceAllUsesWith(User, Existing, UpdateListener);
4352
4353 // User is now dead. Notify a listener if present.
4354 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4355 DeleteNodeNotInCSEMaps(User);
4356 }
4357}
4358
4359/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
4360/// uses of other values produced by From.Val alone. The same value may
4361/// appear in both the From and To list. The Deleted vector is
4362/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004363void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4364 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004365 unsigned Num,
4366 DAGUpdateListener *UpdateListener){
4367 // Handle the simple, trivial case efficiently.
4368 if (Num == 1)
4369 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4370
4371 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4372 for (unsigned i = 0; i != Num; ++i)
4373 for (SDNode::use_iterator UI = From[i].Val->use_begin(),
4374 E = From[i].Val->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004375 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004376
4377 while (!Users.empty()) {
4378 // We know that this user uses some value of From. If it is the right
4379 // value, update it.
4380 SDNode *User = Users.back().first;
4381 unsigned i = Users.back().second;
4382 Users.pop_back();
4383
4384 // Scan for an operand that matches From.
4385 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4386 for (; Op != E; ++Op)
4387 if (*Op == From[i]) break;
4388
4389 // If there are no matches, the user must use some other result of From.
4390 if (Op == E) continue;
4391
4392 // Okay, we know this user needs to be updated. Remove its old self
4393 // from the CSE maps.
4394 RemoveNodeFromCSEMaps(User);
4395
4396 // Update all operands that match "From" in case there are multiple uses.
4397 for (; Op != E; ++Op) {
4398 if (*Op == From[i]) {
4399 From[i].Val->removeUser(Op-User->op_begin(), User);
4400 *Op = To[i];
4401 Op->setUser(User);
4402 To[i].Val->addUser(Op-User->op_begin(), User);
4403 }
4404 }
4405
4406 // Now that we have modified User, add it back to the CSE maps. If it
4407 // already exists there, recursively merge the results together.
4408 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4409 if (!Existing) {
4410 if (UpdateListener) UpdateListener->NodeUpdated(User);
4411 continue; // Continue on to next user.
4412 }
4413
4414 // If there was already an existing matching node, use ReplaceAllUsesWith
4415 // to replace the dead one with the existing one. This can cause
4416 // recursive merging of other unrelated nodes down the line.
4417 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004418
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004419 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004420 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004421 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004422 }
4423}
4424
Evan Chenge6f35d82006-08-01 08:20:41 +00004425/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004426/// based on their topological order. It returns the maximum id and a vector
4427/// of the SDNodes* in assigned order by reference.
4428unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004429 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004430 std::vector<SDNode*> Sources;
4431
Evan Chenge6f35d82006-08-01 08:20:41 +00004432 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4433 SDNode *N = I;
4434 unsigned Degree = N->use_size();
Dan Gohman3200d922008-08-26 21:42:18 +00004435 // Temporarily use the Node Id as scratch space for the degree count.
4436 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004437 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004438 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004439 }
4440
Evan Cheng99157a02006-08-07 22:13:29 +00004441 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004442 TopOrder.reserve(DAGSize);
Dan Gohman3200d922008-08-26 21:42:18 +00004443 int Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004444 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004445 SDNode *N = Sources.back();
4446 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004447 TopOrder.push_back(N);
Dan Gohman3200d922008-08-26 21:42:18 +00004448 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004449 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004450 SDNode *P = I->getVal();
Dan Gohman3200d922008-08-26 21:42:18 +00004451 unsigned Degree = P->getNodeId();
4452 --Degree;
4453 P->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004454 if (Degree == 0)
4455 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004456 }
4457 }
4458
Evan Chengc384d6c2006-08-02 22:00:34 +00004459 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004460}
4461
4462
Evan Cheng091cba12006-07-27 06:39:06 +00004463
Jim Laskey58b968b2005-08-17 20:08:02 +00004464//===----------------------------------------------------------------------===//
4465// SDNode Class
4466//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004467
Chris Lattner917d2c92006-07-19 00:00:37 +00004468// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004469void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004470void UnarySDNode::ANCHOR() {}
4471void BinarySDNode::ANCHOR() {}
4472void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004473void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004474void ConstantSDNode::ANCHOR() {}
4475void ConstantFPSDNode::ANCHOR() {}
4476void GlobalAddressSDNode::ANCHOR() {}
4477void FrameIndexSDNode::ANCHOR() {}
4478void JumpTableSDNode::ANCHOR() {}
4479void ConstantPoolSDNode::ANCHOR() {}
4480void BasicBlockSDNode::ANCHOR() {}
4481void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004482void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004483void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004484void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004485void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004486void ExternalSymbolSDNode::ANCHOR() {}
4487void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004488void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004489void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004490void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004491void LoadSDNode::ANCHOR() {}
4492void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004493void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004494
Chris Lattner48b85922007-02-04 02:41:42 +00004495HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004496 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004497}
4498
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004499GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004500 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004501 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004502 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004503 // Thread Local
4504 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4505 // Non Thread Local
4506 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4507 getSDVTList(VT)), Offset(o) {
4508 TheGlobal = const_cast<GlobalValue*>(GA);
4509}
Chris Lattner48b85922007-02-04 02:41:42 +00004510
Dan Gohman1ea58a52008-07-09 22:08:04 +00004511MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004512 const Value *srcValue, int SVO,
4513 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004514 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004515 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004516
4517 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4518 assert(getAlignment() == alignment && "Alignment representation error!");
4519 assert(isVolatile() == vol && "Volatile representation error!");
4520}
4521
Dan Gohman36b5c132008-04-07 19:35:22 +00004522/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004523/// reference performed by this memory reference.
4524MachineMemOperand MemSDNode::getMemOperand() const {
4525 int Flags;
4526 if (isa<LoadSDNode>(this))
4527 Flags = MachineMemOperand::MOLoad;
4528 else if (isa<StoreSDNode>(this))
4529 Flags = MachineMemOperand::MOStore;
4530 else {
4531 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4532 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4533 }
4534
4535 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004536 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4537
Dan Gohman1ea58a52008-07-09 22:08:04 +00004538 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004539 const FrameIndexSDNode *FI =
4540 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4541 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004542 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4543 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004544 else
4545 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4546 Size, getAlignment());
4547}
4548
Jim Laskey583bd472006-10-27 23:46:08 +00004549/// Profile - Gather unique data for the node.
4550///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004551void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004552 AddNodeIDNode(ID, this);
4553}
4554
Chris Lattnera3255112005-11-08 23:30:28 +00004555/// getValueTypeList - Return a pointer to the specified value type.
4556///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004557const MVT *SDNode::getValueTypeList(MVT VT) {
4558 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004559 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004560 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004561 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004562 static MVT VTs[MVT::LAST_VALUETYPE];
4563 VTs[VT.getSimpleVT()] = VT;
4564 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004565 }
Chris Lattnera3255112005-11-08 23:30:28 +00004566}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004567
Chris Lattner5c884562005-01-12 18:37:47 +00004568/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4569/// indicated value. This method ignores uses of other values defined by this
4570/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004571bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004572 assert(Value < getNumValues() && "Bad value!");
4573
Roman Levensteindc1adac2008-04-07 10:06:32 +00004574 // TODO: Only iterate over uses of a given value of the node
4575 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004576 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004577 if (NUses == 0)
4578 return false;
4579 --NUses;
4580 }
Chris Lattner5c884562005-01-12 18:37:47 +00004581 }
4582
4583 // Found exactly the right number of uses?
4584 return NUses == 0;
4585}
4586
4587
Evan Cheng33d55952007-08-02 05:29:38 +00004588/// hasAnyUseOfValue - Return true if there are any use of the indicated
4589/// value. This method ignores uses of other values defined by this operation.
4590bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4591 assert(Value < getNumValues() && "Bad value!");
4592
Dan Gohman1373c1c2008-07-09 22:39:01 +00004593 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004594 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004595 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004596
4597 return false;
4598}
4599
4600
Dan Gohman2a629952008-07-27 18:06:42 +00004601/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004602///
Dan Gohman2a629952008-07-27 18:06:42 +00004603bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004604 bool Seen = false;
4605 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004606 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004607 if (User == this)
4608 Seen = true;
4609 else
4610 return false;
4611 }
4612
4613 return Seen;
4614}
4615
Evan Chenge6e97e62006-11-03 07:31:32 +00004616/// isOperand - Return true if this node is an operand of N.
4617///
Dan Gohman475871a2008-07-27 21:46:04 +00004618bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004619 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4620 if (*this == N->getOperand(i))
4621 return true;
4622 return false;
4623}
4624
Evan Cheng917be682008-03-04 00:41:45 +00004625bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004626 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004627 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004628 return true;
4629 return false;
4630}
Evan Cheng4ee62112006-02-05 06:29:23 +00004631
Chris Lattner572dee72008-01-16 05:49:24 +00004632/// reachesChainWithoutSideEffects - Return true if this operand (which must
4633/// be a chain) reaches the specified operand without crossing any
4634/// side-effecting instructions. In practice, this looks through token
4635/// factors and non-volatile loads. In order to remain efficient, this only
4636/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004637bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004638 unsigned Depth) const {
4639 if (*this == Dest) return true;
4640
4641 // Don't search too deeply, we just want to be able to see through
4642 // TokenFactor's etc.
4643 if (Depth == 0) return false;
4644
4645 // If this is a token factor, all inputs to the TF happen in parallel. If any
4646 // of the operands of the TF reach dest, then we can do the xform.
4647 if (getOpcode() == ISD::TokenFactor) {
4648 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4649 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4650 return true;
4651 return false;
4652 }
4653
4654 // Loads don't have side effects, look through them.
4655 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4656 if (!Ld->isVolatile())
4657 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4658 }
4659 return false;
4660}
4661
4662
Evan Chengc5fc57d2006-11-03 03:05:24 +00004663static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004664 SmallPtrSet<SDNode *, 32> &Visited) {
4665 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004666 return;
4667
4668 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4669 SDNode *Op = N->getOperand(i).Val;
4670 if (Op == P) {
4671 found = true;
4672 return;
4673 }
4674 findPredecessor(Op, P, found, Visited);
4675 }
4676}
4677
Evan Cheng917be682008-03-04 00:41:45 +00004678/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004679/// is either an operand of N or it can be reached by recursively traversing
4680/// up the operands.
4681/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004682bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004683 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004684 bool found = false;
4685 findPredecessor(N, this, found, Visited);
4686 return found;
4687}
4688
Evan Chengc5484282006-10-04 00:56:09 +00004689uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4690 assert(Num < NumOperands && "Invalid child # of SDNode!");
4691 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4692}
4693
Reid Spencer577cc322007-04-01 07:32:19 +00004694std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004695 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004696 default:
4697 if (getOpcode() < ISD::BUILTIN_OP_END)
4698 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004699 if (isMachineOpcode()) {
4700 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004701 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004702 if (getMachineOpcode() < TII->getNumOpcodes())
4703 return TII->get(getMachineOpcode()).getName();
4704 return "<<Unknown Machine Node>>";
4705 }
4706 if (G) {
4707 TargetLowering &TLI = G->getTargetLoweringInfo();
4708 const char *Name = TLI.getTargetNodeName(getOpcode());
4709 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004710 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004711 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004712 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004713
Dan Gohmane8be6c62008-07-17 19:10:17 +00004714#ifndef NDEBUG
4715 case ISD::DELETED_NODE:
4716 return "<<Deleted Node!>>";
4717#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004718 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004719 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004720 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4721 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4722 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004723 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4724 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4725 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004726 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004727 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4728 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4729 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4730 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4731 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004732 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004733 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004734 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004735 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004736 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004737 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004738 case ISD::AssertSext: return "AssertSext";
4739 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004740
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004741 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004742 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004743 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004744 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004745
4746 case ISD::Constant: return "Constant";
4747 case ISD::ConstantFP: return "ConstantFP";
4748 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004749 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004750 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004751 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004752 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004753 case ISD::RETURNADDR: return "RETURNADDR";
4754 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004755 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004756 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4757 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004758 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004759 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004760 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004761 case ISD::INTRINSIC_WO_CHAIN: {
4762 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4763 return Intrinsic::getName((Intrinsic::ID)IID);
4764 }
4765 case ISD::INTRINSIC_VOID:
4766 case ISD::INTRINSIC_W_CHAIN: {
4767 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004768 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004769 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004770
Chris Lattnerb2827b02006-03-19 00:52:58 +00004771 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004772 case ISD::TargetConstant: return "TargetConstant";
4773 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004774 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004775 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004776 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004777 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004778 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004779 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004780
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004781 case ISD::CopyToReg: return "CopyToReg";
4782 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004783 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004784 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004785 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004786 case ISD::DBG_LABEL: return "dbg_label";
4787 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004788 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004789 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004790 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004791 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004792
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004793 // Unary operators
4794 case ISD::FABS: return "fabs";
4795 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004796 case ISD::FSQRT: return "fsqrt";
4797 case ISD::FSIN: return "fsin";
4798 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004799 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004800 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004801 case ISD::FTRUNC: return "ftrunc";
4802 case ISD::FFLOOR: return "ffloor";
4803 case ISD::FCEIL: return "fceil";
4804 case ISD::FRINT: return "frint";
4805 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004806
4807 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004808 case ISD::ADD: return "add";
4809 case ISD::SUB: return "sub";
4810 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004811 case ISD::MULHU: return "mulhu";
4812 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004813 case ISD::SDIV: return "sdiv";
4814 case ISD::UDIV: return "udiv";
4815 case ISD::SREM: return "srem";
4816 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004817 case ISD::SMUL_LOHI: return "smul_lohi";
4818 case ISD::UMUL_LOHI: return "umul_lohi";
4819 case ISD::SDIVREM: return "sdivrem";
4820 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004821 case ISD::AND: return "and";
4822 case ISD::OR: return "or";
4823 case ISD::XOR: return "xor";
4824 case ISD::SHL: return "shl";
4825 case ISD::SRA: return "sra";
4826 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004827 case ISD::ROTL: return "rotl";
4828 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004829 case ISD::FADD: return "fadd";
4830 case ISD::FSUB: return "fsub";
4831 case ISD::FMUL: return "fmul";
4832 case ISD::FDIV: return "fdiv";
4833 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004834 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004835 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004836
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004837 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004838 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004839 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004840 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004841 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004842 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004843 case ISD::CONCAT_VECTORS: return "concat_vectors";
4844 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004845 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004846 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004847 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004848 case ISD::ADDC: return "addc";
4849 case ISD::ADDE: return "adde";
4850 case ISD::SUBC: return "subc";
4851 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004852 case ISD::SHL_PARTS: return "shl_parts";
4853 case ISD::SRA_PARTS: return "sra_parts";
4854 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004855
4856 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4857 case ISD::INSERT_SUBREG: return "insert_subreg";
4858
Chris Lattner7f644642005-04-28 21:44:03 +00004859 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004860 case ISD::SIGN_EXTEND: return "sign_extend";
4861 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004862 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004863 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004864 case ISD::TRUNCATE: return "truncate";
4865 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004866 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004867 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004868 case ISD::FP_EXTEND: return "fp_extend";
4869
4870 case ISD::SINT_TO_FP: return "sint_to_fp";
4871 case ISD::UINT_TO_FP: return "uint_to_fp";
4872 case ISD::FP_TO_SINT: return "fp_to_sint";
4873 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004874 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004875
4876 // Control flow instructions
4877 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004878 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004879 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004880 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004881 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004882 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004883 case ISD::CALLSEQ_START: return "callseq_start";
4884 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004885
4886 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004887 case ISD::LOAD: return "load";
4888 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004889 case ISD::VAARG: return "vaarg";
4890 case ISD::VACOPY: return "vacopy";
4891 case ISD::VAEND: return "vaend";
4892 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004893 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004894 case ISD::EXTRACT_ELEMENT: return "extract_element";
4895 case ISD::BUILD_PAIR: return "build_pair";
4896 case ISD::STACKSAVE: return "stacksave";
4897 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004898 case ISD::TRAP: return "trap";
4899
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004900 // Bit manipulation
4901 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004902 case ISD::CTPOP: return "ctpop";
4903 case ISD::CTTZ: return "cttz";
4904 case ISD::CTLZ: return "ctlz";
4905
Chris Lattner36ce6912005-11-29 06:21:05 +00004906 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004907 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004908 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004909
Duncan Sands36397f52007-07-27 12:58:54 +00004910 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004911 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004912
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004913 case ISD::CONDCODE:
4914 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004915 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004916 case ISD::SETOEQ: return "setoeq";
4917 case ISD::SETOGT: return "setogt";
4918 case ISD::SETOGE: return "setoge";
4919 case ISD::SETOLT: return "setolt";
4920 case ISD::SETOLE: return "setole";
4921 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004922
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004923 case ISD::SETO: return "seto";
4924 case ISD::SETUO: return "setuo";
4925 case ISD::SETUEQ: return "setue";
4926 case ISD::SETUGT: return "setugt";
4927 case ISD::SETUGE: return "setuge";
4928 case ISD::SETULT: return "setult";
4929 case ISD::SETULE: return "setule";
4930 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004931
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004932 case ISD::SETEQ: return "seteq";
4933 case ISD::SETGT: return "setgt";
4934 case ISD::SETGE: return "setge";
4935 case ISD::SETLT: return "setlt";
4936 case ISD::SETLE: return "setle";
4937 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004938 }
4939 }
4940}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004941
Evan Cheng144d8f02006-11-09 17:55:04 +00004942const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004943 switch (AM) {
4944 default:
4945 return "";
4946 case ISD::PRE_INC:
4947 return "<pre-inc>";
4948 case ISD::PRE_DEC:
4949 return "<pre-dec>";
4950 case ISD::POST_INC:
4951 return "<post-inc>";
4952 case ISD::POST_DEC:
4953 return "<post-dec>";
4954 }
4955}
4956
Duncan Sands276dcbd2008-03-21 09:14:45 +00004957std::string ISD::ArgFlagsTy::getArgFlagsString() {
4958 std::string S = "< ";
4959
4960 if (isZExt())
4961 S += "zext ";
4962 if (isSExt())
4963 S += "sext ";
4964 if (isInReg())
4965 S += "inreg ";
4966 if (isSRet())
4967 S += "sret ";
4968 if (isByVal())
4969 S += "byval ";
4970 if (isNest())
4971 S += "nest ";
4972 if (getByValAlign())
4973 S += "byval-align:" + utostr(getByValAlign()) + " ";
4974 if (getOrigAlign())
4975 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4976 if (getByValSize())
4977 S += "byval-size:" + utostr(getByValSize()) + " ";
4978 return S + ">";
4979}
4980
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004981void SDNode::dump() const { dump(0); }
4982void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00004983 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00004984 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00004985}
4986
4987void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
4988 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004989
4990 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00004991 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004992 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00004993 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004994 else
Chris Lattner944fac72008-08-23 22:23:09 +00004995 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00004996 }
Chris Lattner944fac72008-08-23 22:23:09 +00004997 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004998
Chris Lattner944fac72008-08-23 22:23:09 +00004999 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005000 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005001 if (i) OS << ", ";
5002 OS << (void*)getOperand(i).Val;
Gabor Greif99a6cb92008-08-26 22:36:50 +00005003 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005004 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005005 }
5006
Evan Chengce254432007-12-11 02:08:35 +00005007 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
5008 SDNode *Mask = getOperand(2).Val;
Chris Lattner944fac72008-08-23 22:23:09 +00005009 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005010 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005011 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005012 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005013 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005014 else
Chris Lattner944fac72008-08-23 22:23:09 +00005015 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
Evan Chengce254432007-12-11 02:08:35 +00005016 }
Chris Lattner944fac72008-08-23 22:23:09 +00005017 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005018 }
5019
Chris Lattnerc3aae252005-01-07 07:46:32 +00005020 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005021 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005022 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005023 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005024 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005025 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005026 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005027 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005028 OS << "<APFloat(";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005029 CSDN->getValueAPF().convertToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005030 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005031 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005032 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005033 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005034 int offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005035 OS << '<';
5036 WriteAsOperand(OS, GADN->getGlobal());
5037 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005038 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005039 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005040 else
Chris Lattner944fac72008-08-23 22:23:09 +00005041 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005042 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005043 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005044 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005045 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005046 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005047 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005048 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005049 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005050 else
Chris Lattner944fac72008-08-23 22:23:09 +00005051 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005052 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005053 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005054 else
Chris Lattner944fac72008-08-23 22:23:09 +00005055 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005056 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005057 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005058 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5059 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005060 OS << LBB->getName() << " ";
5061 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005062 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005063 if (G && R->getReg() &&
5064 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005065 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005066 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005067 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005068 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005069 } else if (const ExternalSymbolSDNode *ES =
5070 dyn_cast<ExternalSymbolSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005071 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005072 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5073 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005074 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005075 else
Chris Lattner944fac72008-08-23 22:23:09 +00005076 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005077 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5078 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005079 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005080 else
Chris Lattner944fac72008-08-23 22:23:09 +00005081 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005082 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005083 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005084 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005085 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005086 }
5087 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005088 const Value *SrcValue = LD->getSrcValue();
5089 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005090 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005091 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005092 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005093 else
Chris Lattner944fac72008-08-23 22:23:09 +00005094 OS << "null";
5095 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005096
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005097 bool doExt = true;
5098 switch (LD->getExtensionType()) {
5099 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005100 case ISD::EXTLOAD: OS << " <anyext "; break;
5101 case ISD::SEXTLOAD: OS << " <sext "; break;
5102 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005103 }
5104 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005105 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005106
Evan Cheng144d8f02006-11-09 17:55:04 +00005107 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005108 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005109 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005110 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005111 OS << " <volatile>";
5112 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005113 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005114 const Value *SrcValue = ST->getSrcValue();
5115 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005116 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005117 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005118 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005119 else
Chris Lattner944fac72008-08-23 22:23:09 +00005120 OS << "null";
5121 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005122
5123 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005124 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005125
5126 const char *AM = getIndexedModeName(ST->getAddressingMode());
5127 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005128 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005129 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005130 OS << " <volatile>";
5131 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005132 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5133 const Value *SrcValue = AT->getSrcValue();
5134 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005135 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005136 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005137 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005138 else
Chris Lattner944fac72008-08-23 22:23:09 +00005139 OS << "null";
5140 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005141 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005142 OS << " <volatile>";
5143 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005144 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005145}
5146
Chris Lattnerde202b32005-11-09 23:47:37 +00005147static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005148 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5149 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005150 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005151 else
Bill Wendling832171c2006-12-07 20:04:42 +00005152 cerr << "\n" << std::string(indent+2, ' ')
5153 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005154
Chris Lattnerea946cd2005-01-09 20:38:33 +00005155
Bill Wendling832171c2006-12-07 20:04:42 +00005156 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005157 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005158}
5159
Chris Lattnerc3aae252005-01-07 07:46:32 +00005160void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005161 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005162
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005163 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5164 I != E; ++I) {
5165 const SDNode *N = I;
5166 if (!N->hasOneUse() && N != getRoot().Val)
5167 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005168 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005169
Jim Laskey26f7fa72006-10-17 19:33:52 +00005170 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005171
Bill Wendling832171c2006-12-07 20:04:42 +00005172 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005173}
5174
Evan Chengd6594ae2006-09-12 21:00:35 +00005175const Type *ConstantPoolSDNode::getType() const {
5176 if (isMachineConstantPoolEntry())
5177 return Val.MachineCPVal->getType();
5178 return Val.ConstVal->getType();
5179}