blob: fa73e73de0703ac1974c849e2d9d17e6ac81e083 [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);
330 ID.AddInteger(Ops->ResNo);
331 }
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());
340 ID.AddInteger(Ops->getSDValue().ResNo);
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 Gohmanfed90b62008-07-28 21:51:04 +0000526 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 Gohmanfed90b62008-07-28 21:51:04 +0000554 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000555}
556
Chris Lattner149c58c2005-08-16 18:17:10 +0000557/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
558/// correspond to it. This is useful when we're about to delete or repurpose
559/// the node. We don't want future request for structurally identical nodes
560/// to return N anymore.
561void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000562 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000563 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000564 case ISD::EntryToken:
565 assert(0 && "EntryToken should not be in CSEMaps!");
566 return;
Chris Lattner95038592005-10-05 06:35:28 +0000567 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000568 case ISD::CONDCODE:
569 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
570 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000571 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000572 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
573 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000574 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000575 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000576 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000577 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000578 Erased =
579 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000580 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000581 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000582 MVT VT = cast<VTSDNode>(N)->getVT();
583 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000584 Erased = ExtendedValueTypeNodes.erase(VT);
585 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000586 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
587 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000588 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000589 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000590 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000591 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000592 // Remove it from the CSE Map.
593 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000594 break;
595 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000596#ifndef NDEBUG
597 // Verify that the node was actually in one of the CSE maps, unless it has a
598 // flag result (which cannot be CSE'd) or is one of the special cases that are
599 // not subject to CSE.
600 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Evan Cheng71e86852008-07-08 20:06:39 +0000601 !N->isTargetOpcode() &&
602 N->getOpcode() != ISD::DBG_LABEL &&
603 N->getOpcode() != ISD::DBG_STOPPOINT &&
604 N->getOpcode() != ISD::EH_LABEL &&
605 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000606 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000607 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000608 assert(0 && "Node is not in map!");
609 }
610#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000611}
612
Chris Lattner8b8749f2005-08-17 19:00:20 +0000613/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
614/// has been taken out and modified in some way. If the specified node already
615/// exists in the CSE maps, do not modify the maps, but return the existing node
616/// instead. If it doesn't exist, add it and return null.
617///
618SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
619 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000620
621 if (N->getValueType(0) == MVT::Flag)
622 return 0; // Never CSE anything that produces a flag.
623
624 switch (N->getOpcode()) {
625 default: break;
626 case ISD::HANDLENODE:
627 case ISD::DBG_LABEL:
628 case ISD::DBG_STOPPOINT:
629 case ISD::EH_LABEL:
630 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000631 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000632 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000633
Chris Lattner9f8cc692005-12-19 22:21:21 +0000634 // Check that remaining values produced are not flags.
635 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
636 if (N->getValueType(i) == MVT::Flag)
637 return 0; // Never CSE anything that produces a flag.
638
Chris Lattnera5682852006-08-07 23:03:03 +0000639 SDNode *New = CSEMap.GetOrInsertNode(N);
640 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000641 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000642}
643
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000644/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
645/// were replaced with those specified. If this node is never memoized,
646/// return null, otherwise return a pointer to the slot it would take. If a
647/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000648SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000649 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000650 if (N->getValueType(0) == MVT::Flag)
651 return 0; // Never CSE anything that produces a flag.
652
653 switch (N->getOpcode()) {
654 default: break;
655 case ISD::HANDLENODE:
656 case ISD::DBG_LABEL:
657 case ISD::DBG_STOPPOINT:
658 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000659 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000660 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000661
662 // Check that remaining values produced are not flags.
663 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
664 if (N->getValueType(i) == MVT::Flag)
665 return 0; // Never CSE anything that produces a flag.
666
Dan Gohman475871a2008-07-27 21:46:04 +0000667 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000668 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000669 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000670 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000671}
672
673/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
674/// were replaced with those specified. If this node is never memoized,
675/// return null, otherwise return a pointer to the slot it would take. If a
676/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000677SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000678 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000679 void *&InsertPos) {
680 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000681
682 // Check that remaining values produced are not flags.
683 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
684 if (N->getValueType(i) == MVT::Flag)
685 return 0; // Never CSE anything that produces a flag.
686
Dan Gohman475871a2008-07-27 21:46:04 +0000687 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000688 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000689 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000690 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
691}
692
693
694/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
695/// were replaced with those specified. If this node is never memoized,
696/// return null, otherwise return a pointer to the slot it would take. If a
697/// node already exists with these operands, the slot will be non-null.
698SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000699 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000700 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000701 if (N->getValueType(0) == MVT::Flag)
702 return 0; // Never CSE anything that produces a flag.
703
704 switch (N->getOpcode()) {
705 default: break;
706 case ISD::HANDLENODE:
707 case ISD::DBG_LABEL:
708 case ISD::DBG_STOPPOINT:
709 case ISD::EH_LABEL:
710 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000711 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000712 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000713
714 // Check that remaining values produced are not flags.
715 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
716 if (N->getValueType(i) == MVT::Flag)
717 return 0; // Never CSE anything that produces a flag.
718
Jim Laskey583bd472006-10-27 23:46:08 +0000719 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000720 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000721
Evan Cheng9629aba2006-10-11 01:47:58 +0000722 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
723 ID.AddInteger(LD->getAddressingMode());
724 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000725 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000726 ID.AddInteger(LD->getRawFlags());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000727 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
728 ID.AddInteger(ST->getAddressingMode());
729 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000730 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000731 ID.AddInteger(ST->getRawFlags());
Evan Cheng9629aba2006-10-11 01:47:58 +0000732 }
Jim Laskey583bd472006-10-27 23:46:08 +0000733
Chris Lattnera5682852006-08-07 23:03:03 +0000734 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000735}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000736
Duncan Sandsd038e042008-07-21 10:20:31 +0000737/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
738void SelectionDAG::VerifyNode(SDNode *N) {
739 switch (N->getOpcode()) {
740 default:
741 break;
742 case ISD::BUILD_VECTOR: {
743 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
744 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
745 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
746 "Wrong number of BUILD_VECTOR operands!");
747 MVT EltVT = N->getValueType(0).getVectorElementType();
748 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohman475871a2008-07-27 21:46:04 +0000749 assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd038e042008-07-21 10:20:31 +0000750 "Wrong BUILD_VECTOR operand type!");
751 break;
752 }
753 }
754}
755
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000756/// getMVTAlignment - Compute the default alignment value for the
757/// given type.
758///
759unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
760 const Type *Ty = VT == MVT::iPTR ?
761 PointerType::get(Type::Int8Ty, 0) :
762 VT.getTypeForMVT();
763
764 return TLI.getTargetData()->getABITypeAlignment(Ty);
765}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000766
Dan Gohman6f179662008-08-23 00:50:30 +0000767SelectionDAG::SelectionDAG(TargetLowering &tli, MachineFunction &mf,
Dan Gohmanf350b272008-08-23 02:25:05 +0000768 FunctionLoweringInfo &fli, MachineModuleInfo *mmi)
769 : TLI(tli), MF(mf), FLI(fli), MMI(mmi),
770 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
771 Root(getEntryNode()) {
772 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000773}
774
Chris Lattner78ec3112003-08-11 14:57:33 +0000775SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000776 allnodes_clear();
777}
778
779void SelectionDAG::allnodes_clear() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000780 while (!AllNodes.empty()) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000781 SDNode *N = AllNodes.remove(AllNodes.begin());
Chris Lattner213a16c2006-08-14 22:19:25 +0000782 N->SetNextInBucket(0);
Dan Gohmanf350b272008-08-23 02:25:05 +0000783 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000784 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000785 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000786}
787
Dan Gohmanf350b272008-08-23 02:25:05 +0000788void SelectionDAG::reset() {
789 allnodes_clear();
790 OperandAllocator.Reset();
791 CSEMap.clear();
792
793 ExtendedValueTypeNodes.clear();
794 ExternalSymbols.clear();
795 TargetExternalSymbols.clear();
796 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
797 static_cast<CondCodeSDNode*>(0));
798 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
799 static_cast<SDNode*>(0));
800
801 EntryNode.Uses = 0;
802 AllNodes.push_back(&EntryNode);
803 Root = getEntryNode();
804}
805
Dan Gohman475871a2008-07-27 21:46:04 +0000806SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000807 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000808 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000809 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000810 return getNode(ISD::AND, Op.getValueType(), Op,
811 getConstant(Imm, Op.getValueType()));
812}
813
Dan Gohman475871a2008-07-27 21:46:04 +0000814SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000815 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000816 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000817}
818
Dan Gohman475871a2008-07-27 21:46:04 +0000819SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000820 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000821
Evan Cheng0ff39b32008-06-30 07:31:25 +0000822 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000823 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000824 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000825
826 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000827 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000828 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000829 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000830 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000831 SDNode *N = NULL;
832 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000833 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000834 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000835 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000836 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000837 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000838 CSEMap.InsertNode(N, IP);
839 AllNodes.push_back(N);
840 }
841
Dan Gohman475871a2008-07-27 21:46:04 +0000842 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000843 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000844 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000845 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000846 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
847 }
848 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000849}
850
Dan Gohman475871a2008-07-27 21:46:04 +0000851SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000852 return getConstant(Val, TLI.getPointerTy(), isTarget);
853}
854
855
Dan Gohman475871a2008-07-27 21:46:04 +0000856SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000857 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000858
Duncan Sands83ec4b62008-06-06 12:08:01 +0000859 MVT EltVT =
860 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000861
Chris Lattnerd8658612005-02-17 20:17:32 +0000862 // Do the map lookup using the actual bit pattern for the floating point
863 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
864 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000865 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000866 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000867 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000868 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000869 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000870 SDNode *N = NULL;
871 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000873 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000874 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000875 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000876 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000877 CSEMap.InsertNode(N, IP);
878 AllNodes.push_back(N);
879 }
880
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000882 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000883 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000884 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000885 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
886 }
887 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000888}
889
Dan Gohman475871a2008-07-27 21:46:04 +0000890SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000891 MVT EltVT =
892 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000893 if (EltVT==MVT::f32)
894 return getConstantFP(APFloat((float)Val), VT, isTarget);
895 else
896 return getConstantFP(APFloat(Val), VT, isTarget);
897}
898
Dan Gohman475871a2008-07-27 21:46:04 +0000899SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
900 MVT VT, int Offset,
901 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000902 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000903
904 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
905 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000906 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000907 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
908 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
909 }
910
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000911 if (GVar && GVar->isThreadLocal())
912 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
913 else
914 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000915
Jim Laskey583bd472006-10-27 23:46:08 +0000916 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000917 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000918 ID.AddPointer(GV);
919 ID.AddInteger(Offset);
920 void *IP = 0;
921 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000922 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000923 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000924 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000925 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000926 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000927 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928}
929
Dan Gohman475871a2008-07-27 21:46:04 +0000930SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000931 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000932 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000933 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000934 ID.AddInteger(FI);
935 void *IP = 0;
936 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000937 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000938 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000939 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000940 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000941 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000942 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000943}
944
Dan Gohman475871a2008-07-27 21:46:04 +0000945SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000946 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000947 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000948 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000949 ID.AddInteger(JTI);
950 void *IP = 0;
951 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000952 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000953 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000954 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000955 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000956 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000957 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +0000958}
959
Dan Gohman475871a2008-07-27 21:46:04 +0000960SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
961 unsigned Alignment, int Offset,
962 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000963 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000964 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000965 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000966 ID.AddInteger(Alignment);
967 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000968 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000969 void *IP = 0;
970 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000971 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000972 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000973 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000974 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000975 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000976 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000977}
978
Chris Lattnerc3aae252005-01-07 07:46:32 +0000979
Dan Gohman475871a2008-07-27 21:46:04 +0000980SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
981 unsigned Alignment, int Offset,
982 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000983 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000984 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000985 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000986 ID.AddInteger(Alignment);
987 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000988 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000989 void *IP = 0;
990 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000991 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000992 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000993 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +0000994 CSEMap.InsertNode(N, IP);
995 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000996 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000997}
998
999
Dan Gohman475871a2008-07-27 21:46:04 +00001000SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001001 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001002 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001003 ID.AddPointer(MBB);
1004 void *IP = 0;
1005 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001006 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001007 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001008 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001009 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001010 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001011 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001012}
1013
Dan Gohman475871a2008-07-27 21:46:04 +00001014SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001015 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001016 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001017 ID.AddInteger(Flags.getRawBits());
1018 void *IP = 0;
1019 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001020 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001021 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001022 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001023 CSEMap.InsertNode(N, IP);
1024 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001025 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001026}
1027
Dan Gohman475871a2008-07-27 21:46:04 +00001028SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001029 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1030 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001031
Duncan Sands83ec4b62008-06-06 12:08:01 +00001032 SDNode *&N = VT.isExtended() ?
1033 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001034
Dan Gohman475871a2008-07-27 21:46:04 +00001035 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001036 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001037 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001038 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001039 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001040}
1041
Dan Gohman475871a2008-07-27 21:46:04 +00001042SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001043 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001044 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001045 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001046 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001047 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001048 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001049}
1050
Dan Gohman475871a2008-07-27 21:46:04 +00001051SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001052 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001053 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001054 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001055 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001057 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001058}
1059
Dan Gohman475871a2008-07-27 21:46:04 +00001060SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001061 if ((unsigned)Cond >= CondCodeNodes.size())
1062 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001063
Chris Lattner079a27a2005-08-09 20:40:02 +00001064 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001065 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001066 new (N) CondCodeSDNode(Cond);
1067 CondCodeNodes[Cond] = N;
1068 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001069 }
Dan Gohman475871a2008-07-27 21:46:04 +00001070 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001071}
1072
Dan Gohman475871a2008-07-27 21:46:04 +00001073SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001074 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001075 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001076 ID.AddInteger(RegNo);
1077 void *IP = 0;
1078 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001079 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001080 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001081 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001082 CSEMap.InsertNode(N, IP);
1083 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001084 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001085}
1086
Dan Gohman475871a2008-07-27 21:46:04 +00001087SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001088 unsigned Line, unsigned Col,
1089 const CompileUnitDesc *CU) {
1090 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001091 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001092 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001093 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001094}
1095
Dan Gohman475871a2008-07-27 21:46:04 +00001096SDValue SelectionDAG::getLabel(unsigned Opcode,
1097 SDValue Root,
1098 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001099 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001100 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001101 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1102 ID.AddInteger(LabelID);
1103 void *IP = 0;
1104 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001105 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001106 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001107 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001108 CSEMap.InsertNode(N, IP);
1109 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001110 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001111}
1112
Dan Gohman475871a2008-07-27 21:46:04 +00001113SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001114 assert((!V || isa<PointerType>(V->getType())) &&
1115 "SrcValue is not a pointer?");
1116
Jim Laskey583bd472006-10-27 23:46:08 +00001117 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001118 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001119 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001120
Chris Lattner61b09412006-08-11 21:01:22 +00001121 void *IP = 0;
1122 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001123 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001124
Dan Gohmanfed90b62008-07-28 21:51:04 +00001125 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001126 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001127 CSEMap.InsertNode(N, IP);
1128 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001129 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001130}
1131
Dan Gohman475871a2008-07-27 21:46:04 +00001132SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001133 const Value *v = MO.getValue();
1134 assert((!v || isa<PointerType>(v->getType())) &&
1135 "SrcValue is not a pointer?");
1136
1137 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001138 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001139 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001140
1141 void *IP = 0;
1142 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001143 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001144
Dan Gohmanfed90b62008-07-28 21:51:04 +00001145 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001146 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001147 CSEMap.InsertNode(N, IP);
1148 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001149 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001150}
1151
Chris Lattner37ce9df2007-10-15 17:47:20 +00001152/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1153/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001154SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001155 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001156 unsigned ByteSize = VT.getSizeInBits()/8;
1157 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001158 unsigned StackAlign =
1159 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1160
Chris Lattner37ce9df2007-10-15 17:47:20 +00001161 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1162 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1163}
1164
Dan Gohman475871a2008-07-27 21:46:04 +00001165SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1166 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001167 // These setcc operations always fold.
1168 switch (Cond) {
1169 default: break;
1170 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001171 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001173 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001174
1175 case ISD::SETOEQ:
1176 case ISD::SETOGT:
1177 case ISD::SETOGE:
1178 case ISD::SETOLT:
1179 case ISD::SETOLE:
1180 case ISD::SETONE:
1181 case ISD::SETO:
1182 case ISD::SETUO:
1183 case ISD::SETUEQ:
1184 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001185 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001186 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001187 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001188
Chris Lattner67255a12005-04-07 18:14:58 +00001189 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001190 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001191 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001192 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001193
Chris Lattnerc3aae252005-01-07 07:46:32 +00001194 switch (Cond) {
1195 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001196 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1197 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001198 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1199 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1200 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1201 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1202 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1203 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1204 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1205 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001206 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001207 }
Chris Lattner67255a12005-04-07 18:14:58 +00001208 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001209 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001210 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001211 // No compile time operations on this type yet.
1212 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001213 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001214
1215 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001216 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001217 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001218 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1219 return getNode(ISD::UNDEF, VT);
1220 // fall through
1221 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1222 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1223 return getNode(ISD::UNDEF, VT);
1224 // fall through
1225 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001226 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001227 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1228 return getNode(ISD::UNDEF, VT);
1229 // fall through
1230 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1231 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1232 return getNode(ISD::UNDEF, VT);
1233 // fall through
1234 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1235 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1236 return getNode(ISD::UNDEF, VT);
1237 // fall through
1238 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001239 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001240 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1241 return getNode(ISD::UNDEF, VT);
1242 // fall through
1243 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001244 R==APFloat::cmpEqual, VT);
1245 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1246 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1247 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1248 R==APFloat::cmpEqual, VT);
1249 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1250 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1251 R==APFloat::cmpLessThan, VT);
1252 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1253 R==APFloat::cmpUnordered, VT);
1254 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1255 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001256 }
1257 } else {
1258 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001259 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001260 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001261 }
1262
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001263 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001264 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001265}
1266
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001267/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1268/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001269bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001270 unsigned BitWidth = Op.getValueSizeInBits();
1271 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1272}
1273
Dan Gohmanea859be2007-06-22 14:59:07 +00001274/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1275/// this predicate to simplify operations downstream. Mask is known to be zero
1276/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001277bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001278 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001279 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001280 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1281 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1282 return (KnownZero & Mask) == Mask;
1283}
1284
1285/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1286/// known to be either zero or one and return them in the KnownZero/KnownOne
1287/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1288/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001289void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001290 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001291 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001292 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001293 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001294 "Mask size mismatches value type size!");
1295
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001296 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001297 if (Depth == 6 || Mask == 0)
1298 return; // Limit search depth.
1299
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001300 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001301
1302 switch (Op.getOpcode()) {
1303 case ISD::Constant:
1304 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001305 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001306 KnownZero = ~KnownOne & Mask;
1307 return;
1308 case ISD::AND:
1309 // If either the LHS or the RHS are Zero, the result is zero.
1310 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001311 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1312 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001313 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1314 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1315
1316 // Output known-1 bits are only known if set in both the LHS & RHS.
1317 KnownOne &= KnownOne2;
1318 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1319 KnownZero |= KnownZero2;
1320 return;
1321 case ISD::OR:
1322 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001323 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1324 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001325 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1326 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1327
1328 // Output known-0 bits are only known if clear in both the LHS & RHS.
1329 KnownZero &= KnownZero2;
1330 // Output known-1 are known to be set if set in either the LHS | RHS.
1331 KnownOne |= KnownOne2;
1332 return;
1333 case ISD::XOR: {
1334 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1335 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1336 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1337 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1338
1339 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001340 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001341 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1342 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1343 KnownZero = KnownZeroOut;
1344 return;
1345 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001346 case ISD::MUL: {
1347 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1348 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1349 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1350 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1351 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1352
1353 // If low bits are zero in either operand, output low known-0 bits.
1354 // Also compute a conserative estimate for high known-0 bits.
1355 // More trickiness is possible, but this is sufficient for the
1356 // interesting case of alignment computation.
1357 KnownOne.clear();
1358 unsigned TrailZ = KnownZero.countTrailingOnes() +
1359 KnownZero2.countTrailingOnes();
1360 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001361 KnownZero2.countLeadingOnes(),
1362 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001363
1364 TrailZ = std::min(TrailZ, BitWidth);
1365 LeadZ = std::min(LeadZ, BitWidth);
1366 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1367 APInt::getHighBitsSet(BitWidth, LeadZ);
1368 KnownZero &= Mask;
1369 return;
1370 }
1371 case ISD::UDIV: {
1372 // For the purposes of computing leading zeros we can conservatively
1373 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001374 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001375 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1376 ComputeMaskedBits(Op.getOperand(0),
1377 AllOnes, KnownZero2, KnownOne2, Depth+1);
1378 unsigned LeadZ = KnownZero2.countLeadingOnes();
1379
1380 KnownOne2.clear();
1381 KnownZero2.clear();
1382 ComputeMaskedBits(Op.getOperand(1),
1383 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001384 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1385 if (RHSUnknownLeadingOnes != BitWidth)
1386 LeadZ = std::min(BitWidth,
1387 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001388
1389 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1390 return;
1391 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001392 case ISD::SELECT:
1393 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1394 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1395 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1396 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1397
1398 // Only known if known in both the LHS and RHS.
1399 KnownOne &= KnownOne2;
1400 KnownZero &= KnownZero2;
1401 return;
1402 case ISD::SELECT_CC:
1403 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1404 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1405 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1406 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1407
1408 // Only known if known in both the LHS and RHS.
1409 KnownOne &= KnownOne2;
1410 KnownZero &= KnownZero2;
1411 return;
1412 case ISD::SETCC:
1413 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001414 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1415 BitWidth > 1)
1416 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001417 return;
1418 case ISD::SHL:
1419 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1420 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001421 unsigned ShAmt = SA->getValue();
1422
1423 // If the shift count is an invalid immediate, don't do anything.
1424 if (ShAmt >= BitWidth)
1425 return;
1426
1427 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001428 KnownZero, KnownOne, Depth+1);
1429 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001430 KnownZero <<= ShAmt;
1431 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001432 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001433 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001434 }
1435 return;
1436 case ISD::SRL:
1437 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1438 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001439 unsigned ShAmt = SA->getValue();
1440
Dan Gohmand4cf9922008-02-26 18:50:50 +00001441 // If the shift count is an invalid immediate, don't do anything.
1442 if (ShAmt >= BitWidth)
1443 return;
1444
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001445 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001446 KnownZero, KnownOne, Depth+1);
1447 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001448 KnownZero = KnownZero.lshr(ShAmt);
1449 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001450
Dan Gohman72d2fd52008-02-13 22:43:25 +00001451 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001452 KnownZero |= HighBits; // High bits known zero.
1453 }
1454 return;
1455 case ISD::SRA:
1456 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001457 unsigned ShAmt = SA->getValue();
1458
Dan Gohmand4cf9922008-02-26 18:50:50 +00001459 // If the shift count is an invalid immediate, don't do anything.
1460 if (ShAmt >= BitWidth)
1461 return;
1462
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001463 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001464 // If any of the demanded bits are produced by the sign extension, we also
1465 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001466 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1467 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001468 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001469
1470 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1471 Depth+1);
1472 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001473 KnownZero = KnownZero.lshr(ShAmt);
1474 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001475
1476 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001477 APInt SignBit = APInt::getSignBit(BitWidth);
1478 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001479
Dan Gohmanca93a432008-02-20 16:30:17 +00001480 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001481 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001482 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001483 KnownOne |= HighBits; // New bits are known one.
1484 }
1485 }
1486 return;
1487 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001488 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1489 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001490
1491 // Sign extension. Compute the demanded bits in the result that are not
1492 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001493 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001494
Dan Gohman977a76f2008-02-13 22:28:48 +00001495 APInt InSignBit = APInt::getSignBit(EBits);
1496 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001497
1498 // If the sign extended bits are demanded, we know that the sign
1499 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001500 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001501 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001502 InputDemandedBits |= InSignBit;
1503
1504 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1505 KnownZero, KnownOne, Depth+1);
1506 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1507
1508 // If the sign bit of the input is known set or clear, then we know the
1509 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001510 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001511 KnownZero |= NewBits;
1512 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001513 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001514 KnownOne |= NewBits;
1515 KnownZero &= ~NewBits;
1516 } else { // Input sign bit unknown
1517 KnownZero &= ~NewBits;
1518 KnownOne &= ~NewBits;
1519 }
1520 return;
1521 }
1522 case ISD::CTTZ:
1523 case ISD::CTLZ:
1524 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001525 unsigned LowBits = Log2_32(BitWidth)+1;
1526 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001527 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001528 return;
1529 }
1530 case ISD::LOAD: {
1531 if (ISD::isZEXTLoad(Op.Val)) {
1532 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001533 MVT VT = LD->getMemoryVT();
1534 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001535 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001536 }
1537 return;
1538 }
1539 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001540 MVT InVT = Op.getOperand(0).getValueType();
1541 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001542 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1543 APInt InMask = Mask;
1544 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001545 KnownZero.trunc(InBits);
1546 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001547 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001548 KnownZero.zext(BitWidth);
1549 KnownOne.zext(BitWidth);
1550 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001551 return;
1552 }
1553 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001554 MVT InVT = Op.getOperand(0).getValueType();
1555 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001556 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001557 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1558 APInt InMask = Mask;
1559 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001560
1561 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001562 // bit is demanded. Temporarily set this bit in the mask for our callee.
1563 if (NewBits.getBoolValue())
1564 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001565
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001566 KnownZero.trunc(InBits);
1567 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001568 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1569
1570 // Note if the sign bit is known to be zero or one.
1571 bool SignBitKnownZero = KnownZero.isNegative();
1572 bool SignBitKnownOne = KnownOne.isNegative();
1573 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1574 "Sign bit can't be known to be both zero and one!");
1575
1576 // If the sign bit wasn't actually demanded by our caller, we don't
1577 // want it set in the KnownZero and KnownOne result values. Reset the
1578 // mask and reapply it to the result values.
1579 InMask = Mask;
1580 InMask.trunc(InBits);
1581 KnownZero &= InMask;
1582 KnownOne &= InMask;
1583
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001584 KnownZero.zext(BitWidth);
1585 KnownOne.zext(BitWidth);
1586
Dan Gohman977a76f2008-02-13 22:28:48 +00001587 // If the sign bit is known zero or one, the top bits match.
1588 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001589 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001590 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001591 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001592 return;
1593 }
1594 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001595 MVT InVT = Op.getOperand(0).getValueType();
1596 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001597 APInt InMask = Mask;
1598 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001599 KnownZero.trunc(InBits);
1600 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001601 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001602 KnownZero.zext(BitWidth);
1603 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001604 return;
1605 }
1606 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001607 MVT InVT = Op.getOperand(0).getValueType();
1608 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001609 APInt InMask = Mask;
1610 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001611 KnownZero.zext(InBits);
1612 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001613 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001614 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001615 KnownZero.trunc(BitWidth);
1616 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001617 break;
1618 }
1619 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001620 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1621 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001622 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1623 KnownOne, Depth+1);
1624 KnownZero |= (~InMask) & Mask;
1625 return;
1626 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001627 case ISD::FGETSIGN:
1628 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001629 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001630 return;
1631
Dan Gohman23e8b712008-04-28 17:02:21 +00001632 case ISD::SUB: {
1633 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1634 // We know that the top bits of C-X are clear if X contains less bits
1635 // than C (i.e. no wrap-around can happen). For example, 20-X is
1636 // positive if we can prove that X is >= 0 and < 16.
1637 if (CLHS->getAPIntValue().isNonNegative()) {
1638 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1639 // NLZ can't be BitWidth with no sign bit
1640 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1641 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1642 Depth+1);
1643
1644 // If all of the MaskV bits are known to be zero, then we know the
1645 // output top bits are zero, because we now know that the output is
1646 // from [0-C].
1647 if ((KnownZero2 & MaskV) == MaskV) {
1648 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1649 // Top bits known zero.
1650 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1651 }
1652 }
1653 }
1654 }
1655 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001656 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001657 // Output known-0 bits are known if clear or set in both the low clear bits
1658 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1659 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001660 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1661 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1662 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1663 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1664
1665 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1666 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1667 KnownZeroOut = std::min(KnownZeroOut,
1668 KnownZero2.countTrailingOnes());
1669
1670 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001671 return;
1672 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001673 case ISD::SREM:
1674 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001675 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001676 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001677 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001678 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1679 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001680
Dan Gohmana60832b2008-08-13 23:12:35 +00001681 // If the sign bit of the first operand is zero, the sign bit of
1682 // the result is zero. If the first operand has no one bits below
1683 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001684 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1685 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001686
Dan Gohman23e8b712008-04-28 17:02:21 +00001687 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001688
1689 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001690 }
1691 }
1692 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001693 case ISD::UREM: {
1694 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001695 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001696 if (RA.isPowerOf2()) {
1697 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001698 APInt Mask2 = LowBits & Mask;
1699 KnownZero |= ~LowBits & Mask;
1700 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1701 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1702 break;
1703 }
1704 }
1705
1706 // Since the result is less than or equal to either operand, any leading
1707 // zero bits in either operand must also exist in the result.
1708 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1709 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1710 Depth+1);
1711 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1712 Depth+1);
1713
1714 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1715 KnownZero2.countLeadingOnes());
1716 KnownOne.clear();
1717 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1718 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001719 }
1720 default:
1721 // Allow the target to implement this method for its nodes.
1722 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1723 case ISD::INTRINSIC_WO_CHAIN:
1724 case ISD::INTRINSIC_W_CHAIN:
1725 case ISD::INTRINSIC_VOID:
1726 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1727 }
1728 return;
1729 }
1730}
1731
1732/// ComputeNumSignBits - Return the number of times the sign bit of the
1733/// register is replicated into the other bits. We know that at least 1 bit
1734/// is always equal to the sign bit (itself), but other cases can give us
1735/// information. For example, immediately after an "SRA X, 2", we know that
1736/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001737unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001738 MVT VT = Op.getValueType();
1739 assert(VT.isInteger() && "Invalid VT!");
1740 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001741 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001742 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001743
1744 if (Depth == 6)
1745 return 1; // Limit search depth.
1746
1747 switch (Op.getOpcode()) {
1748 default: break;
1749 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001750 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001751 return VTBits-Tmp+1;
1752 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001753 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001754 return VTBits-Tmp;
1755
1756 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001757 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1758 // If negative, return # leading ones.
1759 if (Val.isNegative())
1760 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001761
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001762 // Return # leading zeros.
1763 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001764 }
1765
1766 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001767 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001768 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1769
1770 case ISD::SIGN_EXTEND_INREG:
1771 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001772 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001773 Tmp = VTBits-Tmp+1;
1774
1775 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1776 return std::max(Tmp, Tmp2);
1777
1778 case ISD::SRA:
1779 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1780 // SRA X, C -> adds C sign bits.
1781 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1782 Tmp += C->getValue();
1783 if (Tmp > VTBits) Tmp = VTBits;
1784 }
1785 return Tmp;
1786 case ISD::SHL:
1787 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1788 // shl destroys sign bits.
1789 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1790 if (C->getValue() >= VTBits || // Bad shift.
1791 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1792 return Tmp - C->getValue();
1793 }
1794 break;
1795 case ISD::AND:
1796 case ISD::OR:
1797 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001798 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001799 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001800 if (Tmp != 1) {
1801 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1802 FirstAnswer = std::min(Tmp, Tmp2);
1803 // We computed what we know about the sign bits as our first
1804 // answer. Now proceed to the generic code that uses
1805 // ComputeMaskedBits, and pick whichever answer is better.
1806 }
1807 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001808
1809 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001810 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001811 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001812 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001813 return std::min(Tmp, Tmp2);
1814
1815 case ISD::SETCC:
1816 // If setcc returns 0/-1, all bits are sign bits.
1817 if (TLI.getSetCCResultContents() ==
1818 TargetLowering::ZeroOrNegativeOneSetCCResult)
1819 return VTBits;
1820 break;
1821 case ISD::ROTL:
1822 case ISD::ROTR:
1823 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1824 unsigned RotAmt = C->getValue() & (VTBits-1);
1825
1826 // Handle rotate right by N like a rotate left by 32-N.
1827 if (Op.getOpcode() == ISD::ROTR)
1828 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1829
1830 // If we aren't rotating out all of the known-in sign bits, return the
1831 // number that are left. This handles rotl(sext(x), 1) for example.
1832 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1833 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1834 }
1835 break;
1836 case ISD::ADD:
1837 // Add can have at most one carry bit. Thus we know that the output
1838 // is, at worst, one more bit than the inputs.
1839 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1840 if (Tmp == 1) return 1; // Early out.
1841
1842 // Special case decrementing a value (ADD X, -1):
1843 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1844 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001845 APInt KnownZero, KnownOne;
1846 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001847 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1848
1849 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1850 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001851 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 return VTBits;
1853
1854 // If we are subtracting one from a positive number, there is no carry
1855 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001856 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001857 return Tmp;
1858 }
1859
1860 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1861 if (Tmp2 == 1) return 1;
1862 return std::min(Tmp, Tmp2)-1;
1863 break;
1864
1865 case ISD::SUB:
1866 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1867 if (Tmp2 == 1) return 1;
1868
1869 // Handle NEG.
1870 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001871 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001872 APInt KnownZero, KnownOne;
1873 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001874 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1875 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1876 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001877 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001878 return VTBits;
1879
1880 // If the input is known to be positive (the sign bit is known clear),
1881 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001882 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001883 return Tmp2;
1884
1885 // Otherwise, we treat this like a SUB.
1886 }
1887
1888 // Sub can have at most one carry bit. Thus we know that the output
1889 // is, at worst, one more bit than the inputs.
1890 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1891 if (Tmp == 1) return 1; // Early out.
1892 return std::min(Tmp, Tmp2)-1;
1893 break;
1894 case ISD::TRUNCATE:
1895 // FIXME: it's tricky to do anything useful for this, but it is an important
1896 // case for targets like X86.
1897 break;
1898 }
1899
1900 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1901 if (Op.getOpcode() == ISD::LOAD) {
1902 LoadSDNode *LD = cast<LoadSDNode>(Op);
1903 unsigned ExtType = LD->getExtensionType();
1904 switch (ExtType) {
1905 default: break;
1906 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001907 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001908 return VTBits-Tmp+1;
1909 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001910 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001911 return VTBits-Tmp;
1912 }
1913 }
1914
1915 // Allow the target to implement this method for its nodes.
1916 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1917 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1918 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1919 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1920 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001921 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001922 }
1923
1924 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1925 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001926 APInt KnownZero, KnownOne;
1927 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001928 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1929
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001930 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001931 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001932 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001933 Mask = KnownOne;
1934 } else {
1935 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001936 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001937 }
1938
1939 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1940 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001941 Mask = ~Mask;
1942 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001943 // Return # leading zeros. We use 'min' here in case Val was zero before
1944 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001945 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001946}
1947
Chris Lattner51dabfb2006-10-14 00:41:01 +00001948
Dan Gohman475871a2008-07-27 21:46:04 +00001949bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00001950 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1951 if (!GA) return false;
1952 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1953 if (!GV) return false;
1954 MachineModuleInfo *MMI = getMachineModuleInfo();
1955 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1956}
1957
1958
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001959/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1960/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00001961SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001962 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SDValue PermMask = N->getOperand(2);
1964 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00001965 if (Idx.getOpcode() == ISD::UNDEF)
1966 return getNode(ISD::UNDEF, VT.getVectorElementType());
1967 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001968 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001969 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00001970 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001971
1972 if (V.getOpcode() == ISD::BIT_CONVERT) {
1973 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001974 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00001975 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001976 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001977 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001978 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001979 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001980 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001981 return V.getOperand(Index);
1982 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1983 return getShuffleScalarElt(V.Val, Index);
Dan Gohman475871a2008-07-27 21:46:04 +00001984 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001985}
1986
1987
Chris Lattnerc3aae252005-01-07 07:46:32 +00001988/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001989///
Dan Gohman475871a2008-07-27 21:46:04 +00001990SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001991 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001992 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001993 void *IP = 0;
1994 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001995 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001996 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001997 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001998 CSEMap.InsertNode(N, IP);
1999
2000 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002001#ifndef NDEBUG
2002 VerifyNode(N);
2003#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002004 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002005}
2006
Dan Gohman475871a2008-07-27 21:46:04 +00002007SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002008 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002009 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00002010 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002011 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002012 switch (Opcode) {
2013 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002014 case ISD::SIGN_EXTEND:
2015 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002016 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002017 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002018 case ISD::TRUNCATE:
2019 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002020 case ISD::UINT_TO_FP:
2021 case ISD::SINT_TO_FP: {
2022 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002023 // No compile time operations on this type.
2024 if (VT==MVT::ppcf128)
2025 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002026 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2027 (void)apf.convertFromAPInt(Val,
2028 Opcode==ISD::SINT_TO_FP,
2029 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002030 return getConstantFP(apf, VT);
2031 }
Chris Lattner94683772005-12-23 05:30:37 +00002032 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002033 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002034 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002035 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002036 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002037 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002038 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002039 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002040 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002041 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002042 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002043 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002044 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002045 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002046 }
2047 }
2048
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002049 // Constant fold unary operations with a floating point constant operand.
2050 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2051 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002052 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002053 switch (Opcode) {
2054 case ISD::FNEG:
2055 V.changeSign();
2056 return getConstantFP(V, VT);
2057 case ISD::FABS:
2058 V.clearSign();
2059 return getConstantFP(V, VT);
2060 case ISD::FP_ROUND:
2061 case ISD::FP_EXTEND:
2062 // This can return overflow, underflow, or inexact; we don't care.
2063 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002064 (void)V.convert(*MVTToAPFloatSemantics(VT),
2065 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002066 return getConstantFP(V, VT);
2067 case ISD::FP_TO_SINT:
2068 case ISD::FP_TO_UINT: {
2069 integerPart x;
2070 assert(integerPartWidth >= 64);
2071 // FIXME need to be more flexible about rounding mode.
2072 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2073 Opcode==ISD::FP_TO_SINT,
2074 APFloat::rmTowardZero);
2075 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2076 break;
2077 return getConstant(x, VT);
2078 }
2079 case ISD::BIT_CONVERT:
2080 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2081 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2082 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2083 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002084 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002085 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002086 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002087 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002088
2089 unsigned OpOpcode = Operand.Val->getOpcode();
2090 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002091 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002092 case ISD::CONCAT_VECTORS:
2093 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002094 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002095 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002096 assert(VT.isFloatingPoint() &&
2097 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002098 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002099 if (Operand.getOpcode() == ISD::UNDEF)
2100 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002101 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002102 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002103 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002104 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002105 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002106 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002107 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002108 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2109 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2110 break;
2111 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002112 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002113 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002114 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002115 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002116 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002117 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002118 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002119 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002120 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002121 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002122 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002123 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002124 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002125 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002126 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2127 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2128 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2129 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002130 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002131 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002132 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002133 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002134 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002135 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002136 if (OpOpcode == ISD::TRUNCATE)
2137 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002138 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2139 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002140 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002141 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002142 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002143 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002144 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2145 else
2146 return Operand.Val->getOperand(0);
2147 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002148 break;
Chris Lattner35481892005-12-23 00:16:34 +00002149 case ISD::BIT_CONVERT:
2150 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002151 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002152 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002153 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002154 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2155 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002156 if (OpOpcode == ISD::UNDEF)
2157 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002158 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002159 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002160 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2161 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002162 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002163 if (OpOpcode == ISD::UNDEF)
2164 return getNode(ISD::UNDEF, VT);
2165 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2166 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2167 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2168 Operand.getConstantOperandVal(1) == 0 &&
2169 Operand.getOperand(0).getValueType() == VT)
2170 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002171 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002172 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002173 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2174 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002175 Operand.Val->getOperand(0));
2176 if (OpOpcode == ISD::FNEG) // --X -> X
2177 return Operand.Val->getOperand(0);
2178 break;
2179 case ISD::FABS:
2180 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2181 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2182 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002183 }
2184
Chris Lattner43247a12005-08-25 19:12:10 +00002185 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002186 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002187 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002188 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002189 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002190 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002191 void *IP = 0;
2192 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002193 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002194 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002195 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002196 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002197 } else {
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 Lattner43247a12005-08-25 19:12:10 +00002200 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002201
Chris Lattnerc3aae252005-01-07 07:46:32 +00002202 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002203#ifndef NDEBUG
2204 VerifyNode(N);
2205#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002206 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002207}
2208
Dan Gohman475871a2008-07-27 21:46:04 +00002209SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2210 SDValue N1, SDValue N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002211 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2212 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002213 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002214 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002215 case ISD::TokenFactor:
2216 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2217 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002218 // Fold trivial token factors.
2219 if (N1.getOpcode() == ISD::EntryToken) return N2;
2220 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002221 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002222 case ISD::CONCAT_VECTORS:
2223 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2224 // one big BUILD_VECTOR.
2225 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2226 N2.getOpcode() == ISD::BUILD_VECTOR) {
2227 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2228 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2229 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2230 }
2231 break;
Chris Lattner76365122005-01-16 02:23:22 +00002232 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002233 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002234 N1.getValueType() == VT && "Binary operator types must match!");
2235 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2236 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002237 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002238 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002239 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2240 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002241 break;
Chris Lattner76365122005-01-16 02:23:22 +00002242 case ISD::OR:
2243 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002244 case ISD::ADD:
2245 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002246 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002247 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002248 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2249 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002250 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002251 return N1;
2252 break;
Chris Lattner76365122005-01-16 02:23:22 +00002253 case ISD::UDIV:
2254 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002255 case ISD::MULHU:
2256 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002257 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002258 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002259 case ISD::MUL:
2260 case ISD::SDIV:
2261 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002262 case ISD::FADD:
2263 case ISD::FSUB:
2264 case ISD::FMUL:
2265 case ISD::FDIV:
2266 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002267 assert(N1.getValueType() == N2.getValueType() &&
2268 N1.getValueType() == VT && "Binary operator types must match!");
2269 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002270 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2271 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002272 N1.getValueType().isFloatingPoint() &&
2273 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002274 "Invalid FCOPYSIGN!");
2275 break;
Chris Lattner76365122005-01-16 02:23:22 +00002276 case ISD::SHL:
2277 case ISD::SRA:
2278 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002279 case ISD::ROTL:
2280 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002281 assert(VT == N1.getValueType() &&
2282 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002283 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002284 "Shifts only work on integers");
2285
2286 // Always fold shifts of i1 values so the code generator doesn't need to
2287 // handle them. Since we know the size of the shift has to be less than the
2288 // size of the value, the shift/rotate count is guaranteed to be zero.
2289 if (VT == MVT::i1)
2290 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002291 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002292 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002293 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002294 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002295 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002296 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002297 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002298 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002299 break;
2300 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002301 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002302 assert(VT.isFloatingPoint() &&
2303 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002304 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002305 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002306 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002307 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002308 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002309 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002310 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002311 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002312 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002313 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002314 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002315 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002316 break;
2317 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002318 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002319 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002320 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002321 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002322 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002323 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002324 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002325
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002326 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002327 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002328 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002329 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002330 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002331 return getConstant(Val, VT);
2332 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002333 break;
2334 }
2335 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002336 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2337 if (N1.getOpcode() == ISD::UNDEF)
2338 return getNode(ISD::UNDEF, VT);
2339
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002340 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2341 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002342 if (N2C &&
2343 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002344 N1.getNumOperands() > 0) {
2345 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002346 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002347 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2348 N1.getOperand(N2C->getValue() / Factor),
2349 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2350 }
2351
2352 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2353 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002354 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002355 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002356
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002357 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2358 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002359 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2360 if (N1.getOperand(2) == N2)
2361 return N1.getOperand(1);
2362 else
2363 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2364 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002365 break;
2366 case ISD::EXTRACT_ELEMENT:
2367 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002368 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2369 (N1.getValueType().isInteger() == VT.isInteger()) &&
2370 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002371
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002372 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2373 // 64-bit integers into 32-bit parts. Instead of building the extract of
2374 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2375 if (N1.getOpcode() == ISD::BUILD_PAIR)
2376 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002377
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002378 // EXTRACT_ELEMENT of a constant int is also very common.
2379 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002380 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002381 unsigned Shift = ElementSize * N2C->getValue();
2382 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2383 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002384 }
2385 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002386 case ISD::EXTRACT_SUBVECTOR:
2387 if (N1.getValueType() == VT) // Trivial extraction.
2388 return N1;
2389 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002390 }
2391
2392 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002393 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002394 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002395 switch (Opcode) {
2396 case ISD::ADD: return getConstant(C1 + C2, VT);
2397 case ISD::SUB: return getConstant(C1 - C2, VT);
2398 case ISD::MUL: return getConstant(C1 * C2, VT);
2399 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002400 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002401 break;
2402 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002403 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002404 break;
2405 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002406 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002407 break;
2408 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002409 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002410 break;
2411 case ISD::AND : return getConstant(C1 & C2, VT);
2412 case ISD::OR : return getConstant(C1 | C2, VT);
2413 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002414 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002415 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2416 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2417 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2418 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002419 default: break;
2420 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002421 } else { // Cannonicalize constant to RHS if commutative
2422 if (isCommutativeBinOp(Opcode)) {
2423 std::swap(N1C, N2C);
2424 std::swap(N1, N2);
2425 }
2426 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002427 }
2428
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002429 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002430 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2431 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002432 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002433 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2434 // Cannonicalize constant to RHS if commutative
2435 std::swap(N1CFP, N2CFP);
2436 std::swap(N1, N2);
2437 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002438 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2439 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002440 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002441 case ISD::FADD:
2442 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002443 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002444 return getConstantFP(V1, VT);
2445 break;
2446 case ISD::FSUB:
2447 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2448 if (s!=APFloat::opInvalidOp)
2449 return getConstantFP(V1, VT);
2450 break;
2451 case ISD::FMUL:
2452 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2453 if (s!=APFloat::opInvalidOp)
2454 return getConstantFP(V1, VT);
2455 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002456 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002457 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2458 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2459 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002460 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002461 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002462 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2463 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2464 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002465 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002466 case ISD::FCOPYSIGN:
2467 V1.copySign(V2);
2468 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002469 default: break;
2470 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002471 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002472 }
Chris Lattner62b57722006-04-20 05:39:12 +00002473
2474 // Canonicalize an UNDEF to the RHS, even over a constant.
2475 if (N1.getOpcode() == ISD::UNDEF) {
2476 if (isCommutativeBinOp(Opcode)) {
2477 std::swap(N1, N2);
2478 } else {
2479 switch (Opcode) {
2480 case ISD::FP_ROUND_INREG:
2481 case ISD::SIGN_EXTEND_INREG:
2482 case ISD::SUB:
2483 case ISD::FSUB:
2484 case ISD::FDIV:
2485 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002486 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002487 return N1; // fold op(undef, arg2) -> undef
2488 case ISD::UDIV:
2489 case ISD::SDIV:
2490 case ISD::UREM:
2491 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002492 case ISD::SRL:
2493 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002494 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002495 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2496 // For vectors, we can't easily build an all zero vector, just return
2497 // the LHS.
2498 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002499 }
2500 }
2501 }
2502
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002503 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002504 if (N2.getOpcode() == ISD::UNDEF) {
2505 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002506 case ISD::XOR:
2507 if (N1.getOpcode() == ISD::UNDEF)
2508 // Handle undef ^ undef -> 0 special case. This is a common
2509 // idiom (misuse).
2510 return getConstant(0, VT);
2511 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002512 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002513 case ISD::ADDC:
2514 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002515 case ISD::SUB:
2516 case ISD::FADD:
2517 case ISD::FSUB:
2518 case ISD::FMUL:
2519 case ISD::FDIV:
2520 case ISD::FREM:
2521 case ISD::UDIV:
2522 case ISD::SDIV:
2523 case ISD::UREM:
2524 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002525 return N2; // fold op(arg1, undef) -> undef
2526 case ISD::MUL:
2527 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002528 case ISD::SRL:
2529 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002530 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002531 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2532 // For vectors, we can't easily build an all zero vector, just return
2533 // the LHS.
2534 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002535 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002536 if (!VT.isVector())
2537 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002538 // For vectors, we can't easily build an all one vector, just return
2539 // the LHS.
2540 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002541 case ISD::SRA:
2542 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002543 }
2544 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002545
Chris Lattner27e9b412005-05-11 18:57:39 +00002546 // Memoize this node if possible.
2547 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002548 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002549 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002550 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002551 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002552 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002553 void *IP = 0;
2554 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002555 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002556 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002557 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002558 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002559 } else {
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 Lattner27e9b412005-05-11 18:57:39 +00002562 }
2563
Chris Lattnerc3aae252005-01-07 07:46:32 +00002564 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002565#ifndef NDEBUG
2566 VerifyNode(N);
2567#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002568 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002569}
2570
Dan Gohman475871a2008-07-27 21:46:04 +00002571SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2572 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002573 // Perform various simplifications.
2574 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2575 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002576 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002577 case ISD::CONCAT_VECTORS:
2578 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2579 // one big BUILD_VECTOR.
2580 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2581 N2.getOpcode() == ISD::BUILD_VECTOR &&
2582 N3.getOpcode() == ISD::BUILD_VECTOR) {
2583 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2584 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2585 Elts.insert(Elts.end(), N3.Val->op_begin(), N3.Val->op_end());
2586 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2587 }
2588 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002589 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002590 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002591 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002592 if (Simp.Val) return Simp;
2593 break;
2594 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002595 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002596 if (N1C) {
2597 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002598 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002599 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002600 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002601 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002602
2603 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002604 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002605 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002606 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002607 if (N2C->getValue()) // Unconditional branch
2608 return getNode(ISD::BR, MVT::Other, N1, N3);
2609 else
2610 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002611 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002612 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002613 case ISD::VECTOR_SHUFFLE:
2614 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002615 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002616 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002617 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002618 "Illegal VECTOR_SHUFFLE node!");
2619 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002620 case ISD::BIT_CONVERT:
2621 // Fold bit_convert nodes from a type to themselves.
2622 if (N1.getValueType() == VT)
2623 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002624 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002625 }
2626
Chris Lattner43247a12005-08-25 19:12:10 +00002627 // Memoize node if it doesn't produce a flag.
2628 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002629 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002630 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002631 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002632 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002633 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002634 void *IP = 0;
2635 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002636 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002637 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002638 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002639 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002640 } else {
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 Lattner43247a12005-08-25 19:12:10 +00002643 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002644 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002645#ifndef NDEBUG
2646 VerifyNode(N);
2647#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002648 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002649}
2650
Dan Gohman475871a2008-07-27 21:46:04 +00002651SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2652 SDValue N1, SDValue N2, SDValue N3,
2653 SDValue N4) {
2654 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002655 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002656}
2657
Dan Gohman475871a2008-07-27 21:46:04 +00002658SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2659 SDValue N1, SDValue N2, SDValue N3,
2660 SDValue N4, SDValue N5) {
2661 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002662 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002663}
2664
Dan Gohman707e0182008-04-12 04:36:06 +00002665/// getMemsetValue - Vectorized representation of the memset value
2666/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002667static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002668 unsigned NumBits = VT.isVector() ?
2669 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002670 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002671 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002672 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002673 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002674 Val = (Val << Shift) | Val;
2675 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002676 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002677 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002678 return DAG.getConstant(Val, VT);
2679 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002680 }
Evan Chengf0df0312008-05-15 08:39:06 +00002681
2682 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2683 unsigned Shift = 8;
2684 for (unsigned i = NumBits; i > 8; i >>= 1) {
2685 Value = DAG.getNode(ISD::OR, VT,
2686 DAG.getNode(ISD::SHL, VT, Value,
2687 DAG.getConstant(Shift, MVT::i8)), Value);
2688 Shift <<= 1;
2689 }
2690
2691 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002692}
2693
Dan Gohman707e0182008-04-12 04:36:06 +00002694/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2695/// used when a memcpy is turned into a memset when the source is a constant
2696/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002697static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002698 const TargetLowering &TLI,
2699 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002700 // Handle vector with all elements zero.
2701 if (Str.empty()) {
2702 if (VT.isInteger())
2703 return DAG.getConstant(0, VT);
2704 unsigned NumElts = VT.getVectorNumElements();
2705 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2706 return DAG.getNode(ISD::BIT_CONVERT, VT,
2707 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2708 }
2709
Duncan Sands83ec4b62008-06-06 12:08:01 +00002710 assert(!VT.isVector() && "Can't handle vector type here!");
2711 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002712 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002713 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002714 if (TLI.isLittleEndian())
2715 Offset = Offset + MSB - 1;
2716 for (unsigned i = 0; i != MSB; ++i) {
2717 Val = (Val << 8) | (unsigned char)Str[Offset];
2718 Offset += TLI.isLittleEndian() ? -1 : 1;
2719 }
2720 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002721}
2722
Dan Gohman707e0182008-04-12 04:36:06 +00002723/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002724///
Dan Gohman475871a2008-07-27 21:46:04 +00002725static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002726 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002727 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002728 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2729}
2730
Evan Chengf0df0312008-05-15 08:39:06 +00002731/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2732///
Dan Gohman475871a2008-07-27 21:46:04 +00002733static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002734 unsigned SrcDelta = 0;
2735 GlobalAddressSDNode *G = NULL;
2736 if (Src.getOpcode() == ISD::GlobalAddress)
2737 G = cast<GlobalAddressSDNode>(Src);
2738 else if (Src.getOpcode() == ISD::ADD &&
2739 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2740 Src.getOperand(1).getOpcode() == ISD::Constant) {
2741 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2742 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2743 }
2744 if (!G)
2745 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002746
Evan Chengf0df0312008-05-15 08:39:06 +00002747 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002748 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2749 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002750
Evan Chengf0df0312008-05-15 08:39:06 +00002751 return false;
2752}
Dan Gohman707e0182008-04-12 04:36:06 +00002753
Evan Chengf0df0312008-05-15 08:39:06 +00002754/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2755/// to replace the memset / memcpy is below the threshold. It also returns the
2756/// types of the sequence of memory ops to perform memset / memcpy.
2757static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002758bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002759 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002760 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002761 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002762 SelectionDAG &DAG,
2763 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002764 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002765 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002766 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002767 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002768 if (VT != MVT::iAny) {
2769 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002770 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002771 // If source is a string constant, this will require an unaligned load.
2772 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2773 if (Dst.getOpcode() != ISD::FrameIndex) {
2774 // Can't change destination alignment. It requires a unaligned store.
2775 if (AllowUnalign)
2776 VT = MVT::iAny;
2777 } else {
2778 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2779 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2780 if (MFI->isFixedObjectIndex(FI)) {
2781 // Can't change destination alignment. It requires a unaligned store.
2782 if (AllowUnalign)
2783 VT = MVT::iAny;
2784 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002785 // Give the stack frame object a larger alignment if needed.
2786 if (MFI->getObjectAlignment(FI) < NewAlign)
2787 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002788 Align = NewAlign;
2789 }
2790 }
2791 }
2792 }
2793
2794 if (VT == MVT::iAny) {
2795 if (AllowUnalign) {
2796 VT = MVT::i64;
2797 } else {
2798 switch (Align & 7) {
2799 case 0: VT = MVT::i64; break;
2800 case 4: VT = MVT::i32; break;
2801 case 2: VT = MVT::i16; break;
2802 default: VT = MVT::i8; break;
2803 }
2804 }
2805
Duncan Sands83ec4b62008-06-06 12:08:01 +00002806 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002807 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002808 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2809 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002810
Duncan Sands8e4eb092008-06-08 20:54:56 +00002811 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002812 VT = LVT;
2813 }
Dan Gohman707e0182008-04-12 04:36:06 +00002814
2815 unsigned NumMemOps = 0;
2816 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002817 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002818 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002819 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002820 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002821 VT = MVT::i64;
2822 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002823 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2824 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002825 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002826 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002827 VTSize >>= 1;
2828 }
Dan Gohman707e0182008-04-12 04:36:06 +00002829 }
Dan Gohman707e0182008-04-12 04:36:06 +00002830
2831 if (++NumMemOps > Limit)
2832 return false;
2833 MemOps.push_back(VT);
2834 Size -= VTSize;
2835 }
2836
2837 return true;
2838}
2839
Dan Gohman475871a2008-07-27 21:46:04 +00002840static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2841 SDValue Chain, SDValue Dst,
2842 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002843 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002844 const Value *DstSV, uint64_t DstSVOff,
2845 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002846 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2847
Dan Gohman21323f32008-05-29 19:42:22 +00002848 // Expand memcpy to a series of load and store ops if the size operand falls
2849 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002851 uint64_t Limit = -1;
2852 if (!AlwaysInline)
2853 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002854 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002855 std::string Str;
2856 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002857 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002858 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002859 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002860
Dan Gohman707e0182008-04-12 04:36:06 +00002861
Evan Cheng0ff39b32008-06-30 07:31:25 +00002862 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002863 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002864 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002865 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002866 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002867 MVT VT = MemOps[i];
2868 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002869 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002870
Evan Cheng0ff39b32008-06-30 07:31:25 +00002871 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002872 // It's unlikely a store of a vector immediate can be done in a single
2873 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002874 // We also handle store a vector with all zero's.
2875 // FIXME: Handle other cases where store of vector immediate is done in
2876 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002877 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002878 Store = DAG.getStore(Chain, Value,
2879 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002880 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002881 } else {
2882 Value = DAG.getLoad(VT, Chain,
2883 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002884 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002885 Store = DAG.getStore(Chain, Value,
2886 getMemBasePlusOffset(Dst, DstOff, DAG),
2887 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002888 }
2889 OutChains.push_back(Store);
2890 SrcOff += VTSize;
2891 DstOff += VTSize;
2892 }
2893
2894 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2895 &OutChains[0], OutChains.size());
2896}
2897
Dan Gohman475871a2008-07-27 21:46:04 +00002898static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2899 SDValue Chain, SDValue Dst,
2900 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002901 unsigned Align, bool AlwaysInline,
2902 const Value *DstSV, uint64_t DstSVOff,
2903 const Value *SrcSV, uint64_t SrcSVOff){
2904 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2905
2906 // Expand memmove to a series of load and store ops if the size operand falls
2907 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002908 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002909 uint64_t Limit = -1;
2910 if (!AlwaysInline)
2911 Limit = TLI.getMaxStoresPerMemmove();
2912 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002913 std::string Str;
2914 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002915 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002916 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002917 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002918
Dan Gohman21323f32008-05-29 19:42:22 +00002919 uint64_t SrcOff = 0, DstOff = 0;
2920
Dan Gohman475871a2008-07-27 21:46:04 +00002921 SmallVector<SDValue, 8> LoadValues;
2922 SmallVector<SDValue, 8> LoadChains;
2923 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002924 unsigned NumMemOps = MemOps.size();
2925 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002926 MVT VT = MemOps[i];
2927 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002928 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002929
2930 Value = DAG.getLoad(VT, Chain,
2931 getMemBasePlusOffset(Src, SrcOff, DAG),
2932 SrcSV, SrcSVOff + SrcOff, false, Align);
2933 LoadValues.push_back(Value);
2934 LoadChains.push_back(Value.getValue(1));
2935 SrcOff += VTSize;
2936 }
2937 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2938 &LoadChains[0], LoadChains.size());
2939 OutChains.clear();
2940 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002941 MVT VT = MemOps[i];
2942 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002943 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002944
2945 Store = DAG.getStore(Chain, LoadValues[i],
2946 getMemBasePlusOffset(Dst, DstOff, DAG),
2947 DstSV, DstSVOff + DstOff, false, DstAlign);
2948 OutChains.push_back(Store);
2949 DstOff += VTSize;
2950 }
2951
2952 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2953 &OutChains[0], OutChains.size());
2954}
2955
Dan Gohman475871a2008-07-27 21:46:04 +00002956static SDValue getMemsetStores(SelectionDAG &DAG,
2957 SDValue Chain, SDValue Dst,
2958 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002959 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002960 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002961 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2962
2963 // Expand memset to a series of load/store ops if the size operand
2964 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002965 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002966 std::string Str;
2967 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002968 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002969 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002970 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002971
Dan Gohman475871a2008-07-27 21:46:04 +00002972 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002973 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002974
2975 unsigned NumMemOps = MemOps.size();
2976 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002977 MVT VT = MemOps[i];
2978 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002979 SDValue Value = getMemsetValue(Src, VT, DAG);
2980 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00002981 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002982 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002983 OutChains.push_back(Store);
2984 DstOff += VTSize;
2985 }
2986
2987 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2988 &OutChains[0], OutChains.size());
2989}
2990
Dan Gohman475871a2008-07-27 21:46:04 +00002991SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
2992 SDValue Src, SDValue Size,
2993 unsigned Align, bool AlwaysInline,
2994 const Value *DstSV, uint64_t DstSVOff,
2995 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002996
2997 // Check to see if we should lower the memcpy to loads and stores first.
2998 // For cases within the target-specified limits, this is the best choice.
2999 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3000 if (ConstantSize) {
3001 // Memcpy with size zero? Just return the original chain.
3002 if (ConstantSize->isNullValue())
3003 return Chain;
3004
Dan Gohman475871a2008-07-27 21:46:04 +00003005 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003006 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003007 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003008 if (Result.Val)
3009 return Result;
3010 }
3011
3012 // Then check to see if we should lower the memcpy with target-specific
3013 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003014 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003015 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3016 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003017 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003018 if (Result.Val)
3019 return Result;
3020
3021 // If we really need inline code and the target declined to provide it,
3022 // use a (potentially long) sequence of loads and stores.
3023 if (AlwaysInline) {
3024 assert(ConstantSize && "AlwaysInline requires a constant size!");
3025 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3026 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003027 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003028 }
3029
3030 // Emit a library call.
3031 TargetLowering::ArgListTy Args;
3032 TargetLowering::ArgListEntry Entry;
3033 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3034 Entry.Node = Dst; Args.push_back(Entry);
3035 Entry.Node = Src; Args.push_back(Entry);
3036 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003037 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003038 TLI.LowerCallTo(Chain, Type::VoidTy,
3039 false, false, false, CallingConv::C, false,
3040 getExternalSymbol("memcpy", TLI.getPointerTy()),
3041 Args, *this);
3042 return CallResult.second;
3043}
3044
Dan Gohman475871a2008-07-27 21:46:04 +00003045SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3046 SDValue Src, SDValue Size,
3047 unsigned Align,
3048 const Value *DstSV, uint64_t DstSVOff,
3049 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003050
Dan Gohman21323f32008-05-29 19:42:22 +00003051 // Check to see if we should lower the memmove to loads and stores first.
3052 // For cases within the target-specified limits, this is the best choice.
3053 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3054 if (ConstantSize) {
3055 // Memmove with size zero? Just return the original chain.
3056 if (ConstantSize->isNullValue())
3057 return Chain;
3058
Dan Gohman475871a2008-07-27 21:46:04 +00003059 SDValue Result =
Dan Gohman21323f32008-05-29 19:42:22 +00003060 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
3061 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
3062 if (Result.Val)
3063 return Result;
3064 }
Dan Gohman707e0182008-04-12 04:36:06 +00003065
3066 // Then check to see if we should lower the memmove with target-specific
3067 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003068 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003069 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003070 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003071 if (Result.Val)
3072 return Result;
3073
3074 // Emit a library call.
3075 TargetLowering::ArgListTy Args;
3076 TargetLowering::ArgListEntry Entry;
3077 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3078 Entry.Node = Dst; Args.push_back(Entry);
3079 Entry.Node = Src; Args.push_back(Entry);
3080 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003081 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003082 TLI.LowerCallTo(Chain, Type::VoidTy,
3083 false, false, false, CallingConv::C, false,
3084 getExternalSymbol("memmove", TLI.getPointerTy()),
3085 Args, *this);
3086 return CallResult.second;
3087}
3088
Dan Gohman475871a2008-07-27 21:46:04 +00003089SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3090 SDValue Src, SDValue Size,
3091 unsigned Align,
3092 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003093
3094 // Check to see if we should lower the memset to stores first.
3095 // For cases within the target-specified limits, this is the best choice.
3096 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3097 if (ConstantSize) {
3098 // Memset with size zero? Just return the original chain.
3099 if (ConstantSize->isNullValue())
3100 return Chain;
3101
Dan Gohman475871a2008-07-27 21:46:04 +00003102 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003103 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003104 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003105 if (Result.Val)
3106 return Result;
3107 }
3108
3109 // Then check to see if we should lower the memset with target-specific
3110 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003111 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003112 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003113 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003114 if (Result.Val)
3115 return Result;
3116
3117 // Emit a library call.
3118 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3119 TargetLowering::ArgListTy Args;
3120 TargetLowering::ArgListEntry Entry;
3121 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3122 Args.push_back(Entry);
3123 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003124 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003125 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3126 else
3127 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3128 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3129 Args.push_back(Entry);
3130 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3131 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003132 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003133 TLI.LowerCallTo(Chain, Type::VoidTy,
3134 false, false, false, CallingConv::C, false,
3135 getExternalSymbol("memset", TLI.getPointerTy()),
3136 Args, *this);
3137 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003138}
3139
Dan Gohman475871a2008-07-27 21:46:04 +00003140SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3141 SDValue Ptr, SDValue Cmp,
3142 SDValue Swp, const Value* PtrVal,
3143 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003144 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003145 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003146
3147 MVT VT = Cmp.getValueType();
3148
3149 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3150 Alignment = getMVTAlignment(VT);
3151
3152 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003153 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003154 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003155 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003156 void* IP = 0;
3157 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003158 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003159 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003160 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003161 CSEMap.InsertNode(N, IP);
3162 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003163 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003164}
3165
Dan Gohman475871a2008-07-27 21:46:04 +00003166SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3167 SDValue Ptr, SDValue Val,
3168 const Value* PtrVal,
3169 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003170 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003171 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3172 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003173 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003174 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3175 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003176 && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003177
3178 MVT VT = Val.getValueType();
3179
3180 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3181 Alignment = getMVTAlignment(VT);
3182
3183 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003184 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003185 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003186 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003187 void* IP = 0;
3188 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003189 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003190 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003191 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003192 CSEMap.InsertNode(N, IP);
3193 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003194 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003195}
3196
Duncan Sands4bdcb612008-07-02 17:40:58 +00003197/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3198/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003199SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3200 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003201 if (Simplify && NumOps == 1)
3202 return Ops[0];
3203
3204 SmallVector<MVT, 4> VTs;
3205 VTs.reserve(NumOps);
3206 for (unsigned i = 0; i < NumOps; ++i)
3207 VTs.push_back(Ops[i].getValueType());
3208 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3209}
3210
Dan Gohman475871a2008-07-27 21:46:04 +00003211SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003212SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003213 MVT VT, SDValue Chain,
3214 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003215 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003216 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003217 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3218 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003219
Duncan Sands14ea39c2008-03-27 20:23:40 +00003220 if (VT == EVT) {
3221 ExtType = ISD::NON_EXTLOAD;
3222 } else if (ExtType == ISD::NON_EXTLOAD) {
3223 assert(VT == EVT && "Non-extending load from different memory type!");
3224 } else {
3225 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003226 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003227 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3228 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003229 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003230 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003231 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003232 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003233 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003234 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003235 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003236 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003237
3238 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003239 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003240 "Unindexed load with an offset!");
3241
3242 SDVTList VTs = Indexed ?
3243 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003244 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003245 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003246 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003247 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003248 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003249 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003250 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003251 void *IP = 0;
3252 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003253 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003254 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003255 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3256 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003257 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003258 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003259 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003260}
3261
Dan Gohman475871a2008-07-27 21:46:04 +00003262SDValue SelectionDAG::getLoad(MVT VT,
3263 SDValue Chain, SDValue Ptr,
3264 const Value *SV, int SVOffset,
3265 bool isVolatile, unsigned Alignment) {
3266 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003267 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3268 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003269}
3270
Dan Gohman475871a2008-07-27 21:46:04 +00003271SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3272 SDValue Chain, SDValue Ptr,
3273 const Value *SV,
3274 int SVOffset, MVT EVT,
3275 bool isVolatile, unsigned Alignment) {
3276 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003277 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3278 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003279}
3280
Dan Gohman475871a2008-07-27 21:46:04 +00003281SDValue
3282SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3283 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003284 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003285 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3286 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003287 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3288 LD->getChain(), Base, Offset, LD->getSrcValue(),
3289 LD->getSrcValueOffset(), LD->getMemoryVT(),
3290 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003291}
3292
Dan Gohman475871a2008-07-27 21:46:04 +00003293SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3294 SDValue Ptr, const Value *SV, int SVOffset,
3295 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003296 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003297
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003298 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3299 Alignment = getMVTAlignment(VT);
3300
Evan Chengad071e12006-10-05 22:57:11 +00003301 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003302 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3303 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003304 FoldingSetNodeID ID;
3305 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003306 ID.AddInteger(ISD::UNINDEXED);
3307 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003308 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003309 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003310 void *IP = 0;
3311 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003312 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003313 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003314 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3315 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003316 CSEMap.InsertNode(N, IP);
3317 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003318 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003319}
3320
Dan Gohman475871a2008-07-27 21:46:04 +00003321SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3322 SDValue Ptr, const Value *SV,
3323 int SVOffset, MVT SVT,
3324 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003325 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003326
3327 if (VT == SVT)
3328 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003329
Duncan Sands8e4eb092008-06-08 20:54:56 +00003330 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003331 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003332 "Can't do FP-INT conversion!");
3333
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003334 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3335 Alignment = getMVTAlignment(VT);
3336
Evan Cheng8b2794a2006-10-13 21:14:26 +00003337 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003338 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3339 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003340 FoldingSetNodeID ID;
3341 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003342 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003343 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003344 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003345 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003346 void *IP = 0;
3347 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003348 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003349 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003350 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3351 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003352 CSEMap.InsertNode(N, IP);
3353 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003354 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003355}
3356
Dan Gohman475871a2008-07-27 21:46:04 +00003357SDValue
3358SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3359 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003360 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3361 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3362 "Store is already a indexed store!");
3363 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003364 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003365 FoldingSetNodeID ID;
3366 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3367 ID.AddInteger(AM);
3368 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003369 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003370 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003371 void *IP = 0;
3372 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003373 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003374 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003375 new (N) StoreSDNode(Ops, VTs, AM,
3376 ST->isTruncatingStore(), ST->getMemoryVT(),
3377 ST->getSrcValue(), ST->getSrcValueOffset(),
3378 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003379 CSEMap.InsertNode(N, IP);
3380 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003381 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003382}
3383
Dan Gohman475871a2008-07-27 21:46:04 +00003384SDValue SelectionDAG::getVAArg(MVT VT,
3385 SDValue Chain, SDValue Ptr,
3386 SDValue SV) {
3387 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003388 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003389}
3390
Dan Gohman475871a2008-07-27 21:46:04 +00003391SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3392 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003393 switch (NumOps) {
3394 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003395 case 1: return getNode(Opcode, VT, Ops[0]);
3396 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3397 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003398 default: break;
3399 }
3400
Dan Gohman475871a2008-07-27 21:46:04 +00003401 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003402 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003403 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3404 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003405}
3406
Dan Gohman475871a2008-07-27 21:46:04 +00003407SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3408 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003409 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003410 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003411 case 1: return getNode(Opcode, VT, Ops[0]);
3412 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3413 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003414 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003415 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003416
Chris Lattneref847df2005-04-09 03:27:28 +00003417 switch (Opcode) {
3418 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003419 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003420 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003421 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3422 "LHS and RHS of condition must have same type!");
3423 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3424 "True and False arms of SelectCC must have same type!");
3425 assert(Ops[2].getValueType() == VT &&
3426 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003427 break;
3428 }
3429 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003430 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003431 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3432 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003433 break;
3434 }
Chris Lattneref847df2005-04-09 03:27:28 +00003435 }
3436
Chris Lattner385328c2005-05-14 07:42:29 +00003437 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003438 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003439 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003440 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003441 FoldingSetNodeID ID;
3442 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003443 void *IP = 0;
3444 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003445 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003446 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003447 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003448 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003449 } else {
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 Lattner43247a12005-08-25 19:12:10 +00003452 }
Chris Lattneref847df2005-04-09 03:27:28 +00003453 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003454#ifndef NDEBUG
3455 VerifyNode(N);
3456#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003457 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003458}
3459
Dan Gohman475871a2008-07-27 21:46:04 +00003460SDValue SelectionDAG::getNode(unsigned Opcode,
3461 const std::vector<MVT> &ResultTys,
3462 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003463 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3464 Ops, NumOps);
3465}
3466
Dan Gohman475871a2008-07-27 21:46:04 +00003467SDValue SelectionDAG::getNode(unsigned Opcode,
3468 const MVT *VTs, unsigned NumVTs,
3469 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003470 if (NumVTs == 1)
3471 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003472 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3473}
3474
Dan Gohman475871a2008-07-27 21:46:04 +00003475SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3476 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003477 if (VTList.NumVTs == 1)
3478 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003479
Chris Lattner5f056bf2005-07-10 01:55:33 +00003480 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003481 // FIXME: figure out how to safely handle things like
3482 // int foo(int x) { return 1 << (x & 255); }
3483 // int bar() { return foo(256); }
3484#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003485 case ISD::SRA_PARTS:
3486 case ISD::SRL_PARTS:
3487 case ISD::SHL_PARTS:
3488 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003489 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003490 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3491 else if (N3.getOpcode() == ISD::AND)
3492 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3493 // If the and is only masking out bits that cannot effect the shift,
3494 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003495 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003496 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3497 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3498 }
3499 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003500#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003501 }
Chris Lattner89c34632005-05-14 06:20:26 +00003502
Chris Lattner43247a12005-08-25 19:12:10 +00003503 // Memoize the node unless it returns a flag.
3504 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003505 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003506 FoldingSetNodeID ID;
3507 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003508 void *IP = 0;
3509 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003510 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003511 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003512 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003513 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3514 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003515 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003516 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3517 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003518 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003519 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3520 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003521 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003522 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3523 }
Chris Lattnera5682852006-08-07 23:03:03 +00003524 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003525 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003526 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003527 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003528 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3529 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003530 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003531 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3532 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003533 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003534 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3535 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003536 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003537 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3538 }
Chris Lattner43247a12005-08-25 19:12:10 +00003539 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003540 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003541#ifndef NDEBUG
3542 VerifyNode(N);
3543#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003544 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003545}
3546
Dan Gohman475871a2008-07-27 21:46:04 +00003547SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003548 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003549}
3550
Dan Gohman475871a2008-07-27 21:46:04 +00003551SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3552 SDValue N1) {
3553 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003554 return getNode(Opcode, VTList, Ops, 1);
3555}
3556
Dan Gohman475871a2008-07-27 21:46:04 +00003557SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3558 SDValue N1, SDValue N2) {
3559 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003560 return getNode(Opcode, VTList, Ops, 2);
3561}
3562
Dan Gohman475871a2008-07-27 21:46:04 +00003563SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3564 SDValue N1, SDValue N2, SDValue N3) {
3565 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003566 return getNode(Opcode, VTList, Ops, 3);
3567}
3568
Dan Gohman475871a2008-07-27 21:46:04 +00003569SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3570 SDValue N1, SDValue N2, SDValue N3,
3571 SDValue N4) {
3572 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003573 return getNode(Opcode, VTList, Ops, 4);
3574}
3575
Dan Gohman475871a2008-07-27 21:46:04 +00003576SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3577 SDValue N1, SDValue N2, SDValue N3,
3578 SDValue N4, SDValue N5) {
3579 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003580 return getNode(Opcode, VTList, Ops, 5);
3581}
3582
Duncan Sands83ec4b62008-06-06 12:08:01 +00003583SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003584 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003585}
3586
Duncan Sands83ec4b62008-06-06 12:08:01 +00003587SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003588 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3589 E = VTList.rend(); I != E; ++I)
3590 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3591 return *I;
3592
3593 MVT *Array = Allocator.Allocate<MVT>(2);
3594 Array[0] = VT1;
3595 Array[1] = VT2;
3596 SDVTList Result = makeVTList(Array, 2);
3597 VTList.push_back(Result);
3598 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003599}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003600
3601SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3602 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3603 E = VTList.rend(); I != E; ++I)
3604 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3605 I->VTs[2] == VT3)
3606 return *I;
3607
3608 MVT *Array = Allocator.Allocate<MVT>(3);
3609 Array[0] = VT1;
3610 Array[1] = VT2;
3611 Array[2] = VT3;
3612 SDVTList Result = makeVTList(Array, 3);
3613 VTList.push_back(Result);
3614 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003615}
3616
Duncan Sands83ec4b62008-06-06 12:08:01 +00003617SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003618 switch (NumVTs) {
3619 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003620 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003621 case 2: return getVTList(VTs[0], VTs[1]);
3622 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3623 default: break;
3624 }
3625
Dan Gohmane8be6c62008-07-17 19:10:17 +00003626 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3627 E = VTList.rend(); I != E; ++I) {
3628 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3629 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003630
3631 bool NoMatch = false;
3632 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003633 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003634 NoMatch = true;
3635 break;
3636 }
3637 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003638 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003639 }
3640
Dan Gohmane8be6c62008-07-17 19:10:17 +00003641 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3642 std::copy(VTs, VTs+NumVTs, Array);
3643 SDVTList Result = makeVTList(Array, NumVTs);
3644 VTList.push_back(Result);
3645 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003646}
3647
3648
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003649/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3650/// specified operands. If the resultant node already exists in the DAG,
3651/// this does not modify the specified node, instead it returns the node that
3652/// already exists. If the resultant node does not exist in the DAG, the
3653/// input node is returned. As a degenerate case, if you specify the same
3654/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003655SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003656 SDNode *N = InN.Val;
3657 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3658
3659 // Check to see if there is no change.
3660 if (Op == N->getOperand(0)) return InN;
3661
3662 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003663 void *InsertPos = 0;
3664 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003665 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003666
Dan Gohman79acd2b2008-07-21 22:38:59 +00003667 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003668 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003669 RemoveNodeFromCSEMaps(N);
3670
3671 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003672 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003673 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003674 N->OperandList[0].setUser(N);
3675 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003676
3677 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003678 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003679 return InN;
3680}
3681
Dan Gohman475871a2008-07-27 21:46:04 +00003682SDValue SelectionDAG::
3683UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003684 SDNode *N = InN.Val;
3685 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3686
3687 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003688 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3689 return InN; // No operands changed, just return the input node.
3690
3691 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003692 void *InsertPos = 0;
3693 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003694 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003695
Dan Gohman79acd2b2008-07-21 22:38:59 +00003696 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003697 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003698 RemoveNodeFromCSEMaps(N);
3699
3700 // Now we update the operands.
3701 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003702 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003703 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003704 N->OperandList[0].setUser(N);
3705 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003706 }
3707 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003708 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003709 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003710 N->OperandList[1].setUser(N);
3711 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003712 }
3713
3714 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003715 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003716 return InN;
3717}
3718
Dan Gohman475871a2008-07-27 21:46:04 +00003719SDValue SelectionDAG::
3720UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3721 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003722 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003723}
3724
Dan Gohman475871a2008-07-27 21:46:04 +00003725SDValue SelectionDAG::
3726UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3727 SDValue Op3, SDValue Op4) {
3728 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003729 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003730}
3731
Dan Gohman475871a2008-07-27 21:46:04 +00003732SDValue SelectionDAG::
3733UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3734 SDValue Op3, SDValue Op4, SDValue Op5) {
3735 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003736 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003737}
3738
Dan Gohman475871a2008-07-27 21:46:04 +00003739SDValue SelectionDAG::
3740UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003741 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003742 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003743 "Update with wrong number of operands");
3744
3745 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003746 bool AnyChange = false;
3747 for (unsigned i = 0; i != NumOps; ++i) {
3748 if (Ops[i] != N->getOperand(i)) {
3749 AnyChange = true;
3750 break;
3751 }
3752 }
3753
3754 // No operands changed, just return the input node.
3755 if (!AnyChange) return InN;
3756
3757 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003758 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003759 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman475871a2008-07-27 21:46:04 +00003760 return SDValue(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003761
Dan Gohman7ceda162008-05-02 00:05:03 +00003762 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003763 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003764 RemoveNodeFromCSEMaps(N);
3765
3766 // Now we update the operands.
3767 for (unsigned i = 0; i != NumOps; ++i) {
3768 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003769 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003770 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003771 N->OperandList[i].setUser(N);
3772 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003773 }
3774 }
3775
3776 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003777 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003778 return InN;
3779}
3780
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003781/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003782/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003783void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003784 // Unlike the code in MorphNodeTo that does this, we don't need to
3785 // watch for dead nodes here.
3786 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3787 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3788
3789 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003790}
Chris Lattner149c58c2005-08-16 18:17:10 +00003791
Dan Gohmane8be6c62008-07-17 19:10:17 +00003792/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3793/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003794///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003795SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003796 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003797 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003798 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003799}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003800
Dan Gohmane8be6c62008-07-17 19:10:17 +00003801SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003802 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003803 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003804 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003805 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00003806}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003807
Dan Gohmane8be6c62008-07-17 19:10:17 +00003808SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003809 MVT VT, SDValue Op1,
3810 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003811 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003812 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003813 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003814}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003815
Dan Gohmane8be6c62008-07-17 19:10:17 +00003816SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003817 MVT VT, SDValue Op1,
3818 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003819 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003820 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003821 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003822}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003823
Dan Gohmane8be6c62008-07-17 19:10:17 +00003824SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003825 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003826 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003827 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003828 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003829}
3830
Dan Gohmane8be6c62008-07-17 19:10:17 +00003831SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003832 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003833 unsigned NumOps) {
3834 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003835 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003836}
3837
Dan Gohmane8be6c62008-07-17 19:10:17 +00003838SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003839 MVT VT1, MVT VT2) {
3840 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003841 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003842}
3843
Dan Gohmane8be6c62008-07-17 19:10:17 +00003844SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003845 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003846 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003847 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003848 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003849}
3850
Dan Gohmane8be6c62008-07-17 19:10:17 +00003851SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003852 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003853 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003854 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003855 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003856 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003857}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003858
Dan Gohmane8be6c62008-07-17 19:10:17 +00003859SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003860 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003861 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003862 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003863 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003864 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003865}
3866
Dan Gohmane8be6c62008-07-17 19:10:17 +00003867SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003868 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003869 SDValue Op1, SDValue Op2,
3870 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003871 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003872 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003873 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003874}
3875
Dan Gohmane8be6c62008-07-17 19:10:17 +00003876SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003877 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003878 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003879 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3880}
3881
3882SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3883 MVT VT) {
3884 SDVTList VTs = getVTList(VT);
3885 return MorphNodeTo(N, Opc, VTs, 0, 0);
3886}
3887
3888SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003889 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003890 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003891 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003892 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3893}
3894
3895SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003896 MVT VT, SDValue Op1,
3897 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003898 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003899 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003900 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3901}
3902
3903SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003904 MVT VT, SDValue Op1,
3905 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003906 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003907 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003908 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3909}
3910
3911SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003912 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003913 unsigned NumOps) {
3914 SDVTList VTs = getVTList(VT);
3915 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3916}
3917
3918SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003919 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003920 unsigned NumOps) {
3921 SDVTList VTs = getVTList(VT1, VT2);
3922 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3923}
3924
3925SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3926 MVT VT1, MVT VT2) {
3927 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003928 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003929}
3930
3931SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3932 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003933 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003934 SDVTList VTs = getVTList(VT1, VT2, VT3);
3935 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3936}
3937
3938SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3939 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003940 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003941 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003942 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003943 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3944}
3945
3946SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3947 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003948 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003949 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003950 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003951 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3952}
3953
3954SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3955 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003956 SDValue Op1, SDValue Op2,
3957 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003958 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003959 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003960 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3961}
3962
3963/// MorphNodeTo - These *mutate* the specified node to have the specified
3964/// return type, opcode, and operands.
3965///
3966/// Note that MorphNodeTo returns the resultant node. If there is already a
3967/// node of the specified opcode and operands, it returns that node instead of
3968/// the current one.
3969///
3970/// Using MorphNodeTo is faster than creating a new node and swapping it in
3971/// with ReplaceAllUsesWith both because it often avoids allocating a new
3972/// node, and because it doesn't require CSE recalulation for any of
3973/// the node's users.
3974///
3975SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003976 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003977 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003978 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00003979 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00003980 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
3981 FoldingSetNodeID ID;
3982 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
3983 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3984 return ON;
3985 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003986
Chris Lattner0fb094f2005-11-19 01:44:53 +00003987 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003988
Dan Gohmane8be6c62008-07-17 19:10:17 +00003989 // Start the morphing.
3990 N->NodeType = Opc;
3991 N->ValueList = VTs.VTs;
3992 N->NumValues = VTs.NumVTs;
3993
3994 // Clear the operands list, updating used nodes to remove this from their
3995 // use list. Keep track of any operands that become dead as a result.
3996 SmallPtrSet<SDNode*, 16> DeadNodeSet;
3997 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
3998 I != E; ++I) {
3999 SDNode *Used = I->getVal();
4000 Used->removeUser(std::distance(B, I), N);
4001 if (Used->use_empty())
4002 DeadNodeSet.insert(Used);
4003 }
4004
4005 // If NumOps is larger than the # of operands we currently have, reallocate
4006 // the operand list.
4007 if (NumOps > N->NumOperands) {
4008 if (N->OperandsNeedDelete)
4009 delete[] N->OperandList;
4010 if (N->isMachineOpcode()) {
4011 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004012 // remainder of the current SelectionDAG iteration, so we can allocate
4013 // the operands directly out of a pool with no recycling metadata.
4014 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004015 N->OperandsNeedDelete = false;
4016 } else {
4017 N->OperandList = new SDUse[NumOps];
4018 N->OperandsNeedDelete = true;
4019 }
4020 }
4021
4022 // Assign the new operands.
4023 N->NumOperands = NumOps;
4024 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4025 N->OperandList[i] = Ops[i];
4026 N->OperandList[i].setUser(N);
4027 SDNode *ToUse = N->OperandList[i].getVal();
4028 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004029 }
4030
4031 // Delete any nodes that are still dead after adding the uses for the
4032 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004033 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004034 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4035 E = DeadNodeSet.end(); I != E; ++I)
4036 if ((*I)->use_empty())
4037 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004038 RemoveDeadNodes(DeadNodes);
4039
Dan Gohmane8be6c62008-07-17 19:10:17 +00004040 if (IP)
4041 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004042 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004043}
4044
Chris Lattner0fb094f2005-11-19 01:44:53 +00004045
Evan Cheng6ae46c42006-02-09 07:15:23 +00004046/// getTargetNode - These are used for target selectors to create a new node
4047/// with specified return type(s), target opcode, and operands.
4048///
4049/// Note that getTargetNode returns the resultant node. If there is already a
4050/// node of the specified opcode and operands, it returns that node instead of
4051/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004052SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004053 return getNode(~Opcode, VT).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004054}
Dan Gohman475871a2008-07-27 21:46:04 +00004055SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004056 return getNode(~Opcode, VT, Op1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004057}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004058SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004059 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004060 return getNode(~Opcode, VT, Op1, Op2).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,
4064 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004065 return getNode(~Opcode, VT, Op1, Op2, Op3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004066}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004067SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004068 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004069 return getNode(~Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004070}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004071SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4072 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004073 SDValue Op;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004074 return getNode(~Opcode, VTs, 2, &Op, 0).Val;
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004075}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004076SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004077 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004078 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004079 return getNode(~Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004080}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004081SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004082 MVT VT2, SDValue Op1,
4083 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004084 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004085 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004086 return getNode(~Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004087}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004088SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004089 MVT VT2, SDValue Op1,
4090 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004091 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004092 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004093 return getNode(~Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004094}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004095SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004096 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004097 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004098 return getNode(~Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004099}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004100SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004101 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004102 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004103 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004104 return getNode(~Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004105}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004106SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004107 SDValue Op1, SDValue Op2,
4108 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004109 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004110 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004111 return getNode(~Opcode, VTs, 3, Ops, 3).Val;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004112}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004113SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004114 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004115 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004116 return getNode(~Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004117}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004118SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4119 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004120 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004121 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004122 VTList.push_back(VT1);
4123 VTList.push_back(VT2);
4124 VTList.push_back(VT3);
4125 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004126 const MVT *VTs = getNodeValueTypes(VTList);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004127 return getNode(~Opcode, VTs, 4, Ops, NumOps).Val;
Evan Cheng05e69c12007-09-12 23:39:49 +00004128}
Evan Cheng39305cf2007-10-05 01:10:49 +00004129SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004130 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004131 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004132 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004133 return getNode(~Opcode, VTs, ResultTys.size(),
Evan Cheng39305cf2007-10-05 01:10:49 +00004134 Ops, NumOps).Val;
4135}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004136
Evan Cheng08b11732008-03-22 01:55:50 +00004137/// getNodeIfExists - Get the specified node if it's already available, or
4138/// else return NULL.
4139SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004140 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004141 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4142 FoldingSetNodeID ID;
4143 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4144 void *IP = 0;
4145 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4146 return E;
4147 }
4148 return NULL;
4149}
4150
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004151
Evan Cheng99157a02006-08-07 22:13:29 +00004152/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004153/// This can cause recursive merging of nodes in the DAG.
4154///
Chris Lattner11d049c2008-02-03 03:35:22 +00004155/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004156///
Dan Gohman475871a2008-07-27 21:46:04 +00004157void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004158 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004159 SDNode *From = FromN.Val;
Chris Lattner11d049c2008-02-03 03:35:22 +00004160 assert(From->getNumValues() == 1 && FromN.ResNo == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004161 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004162 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004163
Chris Lattner8b8749f2005-08-17 19:00:20 +00004164 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004165 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004166 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004167
Chris Lattner8b8749f2005-08-17 19:00:20 +00004168 // This node is about to morph, remove its old self from the CSE maps.
4169 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004170 int operandNum = 0;
4171 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4172 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004173 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004174 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004175 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004176 I->setUser(U);
4177 To.Val->addUser(operandNum, U);
4178 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004179
4180 // Now that we have modified U, add it back to the CSE maps. If it already
4181 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004182 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004183 ReplaceAllUsesWith(U, Existing, UpdateListener);
4184 // U is now dead. Inform the listener if it exists and delete it.
4185 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004186 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004187 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004188 } else {
4189 // If the node doesn't already exist, we updated it. Inform a listener if
4190 // it exists.
4191 if (UpdateListener)
4192 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004193 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004194 }
4195}
4196
4197/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4198/// This can cause recursive merging of nodes in the DAG.
4199///
4200/// This version assumes From/To have matching types and numbers of result
4201/// values.
4202///
Chris Lattner1e111c72005-09-07 05:37:01 +00004203void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004204 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004205 assert(From->getVTList().VTs == To->getVTList().VTs &&
4206 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004207 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004208
4209 // Handle the trivial case.
4210 if (From == To)
4211 return;
4212
Chris Lattner8b52f212005-08-26 18:36:28 +00004213 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004214 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004215 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004216
Chris Lattner8b52f212005-08-26 18:36:28 +00004217 // This node is about to morph, remove its old self from the CSE maps.
4218 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004219 int operandNum = 0;
4220 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4221 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004222 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004223 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004224 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004225 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004226 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004227
Chris Lattner8b8749f2005-08-17 19:00:20 +00004228 // Now that we have modified U, add it back to the CSE maps. If it already
4229 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004230 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004231 ReplaceAllUsesWith(U, Existing, UpdateListener);
4232 // U is now dead. Inform the listener if it exists and delete it.
4233 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004234 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004235 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004236 } else {
4237 // If the node doesn't already exist, we updated it. Inform a listener if
4238 // it exists.
4239 if (UpdateListener)
4240 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004241 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004242 }
4243}
4244
Chris Lattner8b52f212005-08-26 18:36:28 +00004245/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4246/// This can cause recursive merging of nodes in the DAG.
4247///
4248/// This version can replace From with any result values. To must match the
4249/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004250void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004251 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004252 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004253 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004254 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004255
4256 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004257 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004258 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004259
Chris Lattner7b2880c2005-08-24 22:44:39 +00004260 // This node is about to morph, remove its old self from the CSE maps.
4261 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004262 int operandNum = 0;
4263 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4264 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004265 if (I->getVal() == From) {
Dan Gohman475871a2008-07-27 21:46:04 +00004266 const SDValue &ToOp = To[I->getSDValue().ResNo];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004267 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004268 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004269 I->setUser(U);
4270 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004271 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004272
Chris Lattner7b2880c2005-08-24 22:44:39 +00004273 // Now that we have modified U, add it back to the CSE maps. If it already
4274 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004275 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004276 ReplaceAllUsesWith(U, Existing, UpdateListener);
4277 // U is now dead. Inform the listener if it exists and delete it.
4278 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004279 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004280 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004281 } else {
4282 // If the node doesn't already exist, we updated it. Inform a listener if
4283 // it exists.
4284 if (UpdateListener)
4285 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004286 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004287 }
4288}
4289
Chris Lattner012f2412006-02-17 21:58:01 +00004290/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4291/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004292/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004293void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004294 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004295 // Handle the really simple, really trivial case efficiently.
4296 if (From == To) return;
4297
Chris Lattner012f2412006-02-17 21:58:01 +00004298 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004299 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004300 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004301 return;
4302 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004303
Chris Lattnercf5640b2007-02-04 00:14:31 +00004304 // Get all of the users of From.Val. We want these in a nice,
4305 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Dan Gohman89684502008-07-27 20:43:25 +00004306 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004307
4308 while (!Users.empty()) {
4309 // We know that this user uses some value of From. If it is the right
4310 // value, update it.
4311 SDNode *User = Users.back();
4312 Users.pop_back();
4313
Chris Lattner01d029b2007-10-15 06:10:22 +00004314 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004315 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004316 for (; Op != E; ++Op)
4317 if (*Op == From) break;
4318
4319 // If there are no matches, the user must use some other result of From.
4320 if (Op == E) continue;
4321
4322 // Okay, we know this user needs to be updated. Remove its old self
4323 // from the CSE maps.
4324 RemoveNodeFromCSEMaps(User);
4325
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004326 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004327 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004328 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004329 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004330 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004331 Op->setUser(User);
4332 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004333 }
4334 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004335
4336 // Now that we have modified User, add it back to the CSE maps. If it
4337 // already exists there, recursively merge the results together.
4338 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004339 if (!Existing) {
4340 if (UpdateListener) UpdateListener->NodeUpdated(User);
4341 continue; // Continue on to next user.
4342 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004343
4344 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004345 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004346 // recursive merging of other unrelated nodes down the line.
4347 ReplaceAllUsesWith(User, Existing, UpdateListener);
4348
4349 // User is now dead. Notify a listener if present.
4350 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4351 DeleteNodeNotInCSEMaps(User);
4352 }
4353}
4354
4355/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
4356/// uses of other values produced by From.Val alone. The same value may
4357/// appear in both the From and To list. The Deleted vector is
4358/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004359void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4360 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004361 unsigned Num,
4362 DAGUpdateListener *UpdateListener){
4363 // Handle the simple, trivial case efficiently.
4364 if (Num == 1)
4365 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4366
4367 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4368 for (unsigned i = 0; i != Num; ++i)
4369 for (SDNode::use_iterator UI = From[i].Val->use_begin(),
4370 E = From[i].Val->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004371 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004372
4373 while (!Users.empty()) {
4374 // We know that this user uses some value of From. If it is the right
4375 // value, update it.
4376 SDNode *User = Users.back().first;
4377 unsigned i = Users.back().second;
4378 Users.pop_back();
4379
4380 // Scan for an operand that matches From.
4381 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4382 for (; Op != E; ++Op)
4383 if (*Op == From[i]) break;
4384
4385 // If there are no matches, the user must use some other result of From.
4386 if (Op == E) continue;
4387
4388 // Okay, we know this user needs to be updated. Remove its old self
4389 // from the CSE maps.
4390 RemoveNodeFromCSEMaps(User);
4391
4392 // Update all operands that match "From" in case there are multiple uses.
4393 for (; Op != E; ++Op) {
4394 if (*Op == From[i]) {
4395 From[i].Val->removeUser(Op-User->op_begin(), User);
4396 *Op = To[i];
4397 Op->setUser(User);
4398 To[i].Val->addUser(Op-User->op_begin(), User);
4399 }
4400 }
4401
4402 // Now that we have modified User, add it back to the CSE maps. If it
4403 // already exists there, recursively merge the results together.
4404 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4405 if (!Existing) {
4406 if (UpdateListener) UpdateListener->NodeUpdated(User);
4407 continue; // Continue on to next user.
4408 }
4409
4410 // If there was already an existing matching node, use ReplaceAllUsesWith
4411 // to replace the dead one with the existing one. This can cause
4412 // recursive merging of other unrelated nodes down the line.
4413 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004414
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004415 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004416 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004417 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004418 }
4419}
4420
Evan Chenge6f35d82006-08-01 08:20:41 +00004421/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004422/// based on their topological order. It returns the maximum id and a vector
4423/// of the SDNodes* in assigned order by reference.
4424unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004425 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004426 std::vector<unsigned> InDegree(DAGSize);
4427 std::vector<SDNode*> Sources;
4428
4429 // Use a two pass approach to avoid using a std::map which is slow.
4430 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004431 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4432 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00004433 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004434 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004435 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00004436 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004437 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004438 }
4439
Evan Cheng99157a02006-08-07 22:13:29 +00004440 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004441 TopOrder.reserve(DAGSize);
Evan Chenge6f35d82006-08-01 08:20:41 +00004442 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004443 SDNode *N = Sources.back();
4444 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004445 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004446 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004447 SDNode *P = I->getVal();
Evan Chengc384d6c2006-08-02 22:00:34 +00004448 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00004449 if (Degree == 0)
4450 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004451 }
4452 }
4453
Evan Chengc384d6c2006-08-02 22:00:34 +00004454 // Second pass, assign the actual topological order as node ids.
4455 Id = 0;
4456 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
4457 TI != TE; ++TI)
4458 (*TI)->setNodeId(Id++);
4459
4460 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004461}
4462
4463
Evan Cheng091cba12006-07-27 06:39:06 +00004464
Jim Laskey58b968b2005-08-17 20:08:02 +00004465//===----------------------------------------------------------------------===//
4466// SDNode Class
4467//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004468
Chris Lattner917d2c92006-07-19 00:00:37 +00004469// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004470void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004471void UnarySDNode::ANCHOR() {}
4472void BinarySDNode::ANCHOR() {}
4473void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004474void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004475void ConstantSDNode::ANCHOR() {}
4476void ConstantFPSDNode::ANCHOR() {}
4477void GlobalAddressSDNode::ANCHOR() {}
4478void FrameIndexSDNode::ANCHOR() {}
4479void JumpTableSDNode::ANCHOR() {}
4480void ConstantPoolSDNode::ANCHOR() {}
4481void BasicBlockSDNode::ANCHOR() {}
4482void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004483void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004484void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004485void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004486void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004487void ExternalSymbolSDNode::ANCHOR() {}
4488void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004489void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004490void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004491void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004492void LoadSDNode::ANCHOR() {}
4493void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004494void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004495
Chris Lattner48b85922007-02-04 02:41:42 +00004496HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004497 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004498}
4499
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004500GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004501 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004502 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004503 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004504 // Thread Local
4505 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4506 // Non Thread Local
4507 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4508 getSDVTList(VT)), Offset(o) {
4509 TheGlobal = const_cast<GlobalValue*>(GA);
4510}
Chris Lattner48b85922007-02-04 02:41:42 +00004511
Dan Gohman1ea58a52008-07-09 22:08:04 +00004512MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004513 const Value *srcValue, int SVO,
4514 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004515 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004516 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004517
4518 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4519 assert(getAlignment() == alignment && "Alignment representation error!");
4520 assert(isVolatile() == vol && "Volatile representation error!");
4521}
4522
Dan Gohman36b5c132008-04-07 19:35:22 +00004523/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004524/// reference performed by this memory reference.
4525MachineMemOperand MemSDNode::getMemOperand() const {
4526 int Flags;
4527 if (isa<LoadSDNode>(this))
4528 Flags = MachineMemOperand::MOLoad;
4529 else if (isa<StoreSDNode>(this))
4530 Flags = MachineMemOperand::MOStore;
4531 else {
4532 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4533 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4534 }
4535
4536 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004537 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4538
Dan Gohman1ea58a52008-07-09 22:08:04 +00004539 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004540 const FrameIndexSDNode *FI =
4541 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4542 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004543 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4544 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004545 else
4546 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4547 Size, getAlignment());
4548}
4549
Jim Laskey583bd472006-10-27 23:46:08 +00004550/// Profile - Gather unique data for the node.
4551///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004552void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004553 AddNodeIDNode(ID, this);
4554}
4555
Chris Lattnera3255112005-11-08 23:30:28 +00004556/// getValueTypeList - Return a pointer to the specified value type.
4557///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004558const MVT *SDNode::getValueTypeList(MVT VT) {
4559 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004560 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004561 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004562 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004563 static MVT VTs[MVT::LAST_VALUETYPE];
4564 VTs[VT.getSimpleVT()] = VT;
4565 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004566 }
Chris Lattnera3255112005-11-08 23:30:28 +00004567}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004568
Chris Lattner5c884562005-01-12 18:37:47 +00004569/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4570/// indicated value. This method ignores uses of other values defined by this
4571/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004572bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004573 assert(Value < getNumValues() && "Bad value!");
4574
Roman Levensteindc1adac2008-04-07 10:06:32 +00004575 // TODO: Only iterate over uses of a given value of the node
4576 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman475871a2008-07-27 21:46:04 +00004577 if (UI.getUse().getSDValue().ResNo == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004578 if (NUses == 0)
4579 return false;
4580 --NUses;
4581 }
Chris Lattner5c884562005-01-12 18:37:47 +00004582 }
4583
4584 // Found exactly the right number of uses?
4585 return NUses == 0;
4586}
4587
4588
Evan Cheng33d55952007-08-02 05:29:38 +00004589/// hasAnyUseOfValue - Return true if there are any use of the indicated
4590/// value. This method ignores uses of other values defined by this operation.
4591bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4592 assert(Value < getNumValues() && "Bad value!");
4593
Dan Gohman1373c1c2008-07-09 22:39:01 +00004594 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman475871a2008-07-27 21:46:04 +00004595 if (UI.getUse().getSDValue().ResNo == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004596 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004597
4598 return false;
4599}
4600
4601
Dan Gohman2a629952008-07-27 18:06:42 +00004602/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004603///
Dan Gohman2a629952008-07-27 18:06:42 +00004604bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004605 bool Seen = false;
4606 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004607 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004608 if (User == this)
4609 Seen = true;
4610 else
4611 return false;
4612 }
4613
4614 return Seen;
4615}
4616
Evan Chenge6e97e62006-11-03 07:31:32 +00004617/// isOperand - Return true if this node is an operand of N.
4618///
Dan Gohman475871a2008-07-27 21:46:04 +00004619bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004620 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4621 if (*this == N->getOperand(i))
4622 return true;
4623 return false;
4624}
4625
Evan Cheng917be682008-03-04 00:41:45 +00004626bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004627 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004628 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004629 return true;
4630 return false;
4631}
Evan Cheng4ee62112006-02-05 06:29:23 +00004632
Chris Lattner572dee72008-01-16 05:49:24 +00004633/// reachesChainWithoutSideEffects - Return true if this operand (which must
4634/// be a chain) reaches the specified operand without crossing any
4635/// side-effecting instructions. In practice, this looks through token
4636/// factors and non-volatile loads. In order to remain efficient, this only
4637/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004638bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004639 unsigned Depth) const {
4640 if (*this == Dest) return true;
4641
4642 // Don't search too deeply, we just want to be able to see through
4643 // TokenFactor's etc.
4644 if (Depth == 0) return false;
4645
4646 // If this is a token factor, all inputs to the TF happen in parallel. If any
4647 // of the operands of the TF reach dest, then we can do the xform.
4648 if (getOpcode() == ISD::TokenFactor) {
4649 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4650 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4651 return true;
4652 return false;
4653 }
4654
4655 // Loads don't have side effects, look through them.
4656 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4657 if (!Ld->isVolatile())
4658 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4659 }
4660 return false;
4661}
4662
4663
Evan Chengc5fc57d2006-11-03 03:05:24 +00004664static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004665 SmallPtrSet<SDNode *, 32> &Visited) {
4666 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004667 return;
4668
4669 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4670 SDNode *Op = N->getOperand(i).Val;
4671 if (Op == P) {
4672 found = true;
4673 return;
4674 }
4675 findPredecessor(Op, P, found, Visited);
4676 }
4677}
4678
Evan Cheng917be682008-03-04 00:41:45 +00004679/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004680/// is either an operand of N or it can be reached by recursively traversing
4681/// up the operands.
4682/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004683bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004684 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004685 bool found = false;
4686 findPredecessor(N, this, found, Visited);
4687 return found;
4688}
4689
Evan Chengc5484282006-10-04 00:56:09 +00004690uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4691 assert(Num < NumOperands && "Invalid child # of SDNode!");
4692 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4693}
4694
Reid Spencer577cc322007-04-01 07:32:19 +00004695std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004696 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004697 default:
4698 if (getOpcode() < ISD::BUILTIN_OP_END)
4699 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004700 if (isMachineOpcode()) {
4701 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004702 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004703 if (getMachineOpcode() < TII->getNumOpcodes())
4704 return TII->get(getMachineOpcode()).getName();
4705 return "<<Unknown Machine Node>>";
4706 }
4707 if (G) {
4708 TargetLowering &TLI = G->getTargetLoweringInfo();
4709 const char *Name = TLI.getTargetNodeName(getOpcode());
4710 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004711 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004712 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004713 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004714
Dan Gohmane8be6c62008-07-17 19:10:17 +00004715#ifndef NDEBUG
4716 case ISD::DELETED_NODE:
4717 return "<<Deleted Node!>>";
4718#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004719 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004720 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004721 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4722 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4723 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004724 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4725 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4726 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004727 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004728 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4729 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4730 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4731 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4732 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004733 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004734 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004735 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004736 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004737 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004738 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004739 case ISD::AssertSext: return "AssertSext";
4740 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004741
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004742 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004743 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004744 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004745 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004746
4747 case ISD::Constant: return "Constant";
4748 case ISD::ConstantFP: return "ConstantFP";
4749 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004750 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004751 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004752 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004753 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004754 case ISD::RETURNADDR: return "RETURNADDR";
4755 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004756 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004757 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4758 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004759 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004760 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004761 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004762 case ISD::INTRINSIC_WO_CHAIN: {
4763 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4764 return Intrinsic::getName((Intrinsic::ID)IID);
4765 }
4766 case ISD::INTRINSIC_VOID:
4767 case ISD::INTRINSIC_W_CHAIN: {
4768 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004769 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004770 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004771
Chris Lattnerb2827b02006-03-19 00:52:58 +00004772 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004773 case ISD::TargetConstant: return "TargetConstant";
4774 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004775 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004776 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004777 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004778 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004779 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004780 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004781
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004782 case ISD::CopyToReg: return "CopyToReg";
4783 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004784 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004785 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004786 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004787 case ISD::DBG_LABEL: return "dbg_label";
4788 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004789 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004790 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004791 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004792 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004793
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004794 // Unary operators
4795 case ISD::FABS: return "fabs";
4796 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004797 case ISD::FSQRT: return "fsqrt";
4798 case ISD::FSIN: return "fsin";
4799 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004800 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004801 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004802 case ISD::FTRUNC: return "ftrunc";
4803 case ISD::FFLOOR: return "ffloor";
4804 case ISD::FCEIL: return "fceil";
4805 case ISD::FRINT: return "frint";
4806 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004807
4808 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004809 case ISD::ADD: return "add";
4810 case ISD::SUB: return "sub";
4811 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004812 case ISD::MULHU: return "mulhu";
4813 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004814 case ISD::SDIV: return "sdiv";
4815 case ISD::UDIV: return "udiv";
4816 case ISD::SREM: return "srem";
4817 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004818 case ISD::SMUL_LOHI: return "smul_lohi";
4819 case ISD::UMUL_LOHI: return "umul_lohi";
4820 case ISD::SDIVREM: return "sdivrem";
4821 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004822 case ISD::AND: return "and";
4823 case ISD::OR: return "or";
4824 case ISD::XOR: return "xor";
4825 case ISD::SHL: return "shl";
4826 case ISD::SRA: return "sra";
4827 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004828 case ISD::ROTL: return "rotl";
4829 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004830 case ISD::FADD: return "fadd";
4831 case ISD::FSUB: return "fsub";
4832 case ISD::FMUL: return "fmul";
4833 case ISD::FDIV: return "fdiv";
4834 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004835 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004836 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004837
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004838 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004839 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004840 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004841 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004842 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004843 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004844 case ISD::CONCAT_VECTORS: return "concat_vectors";
4845 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004846 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004847 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004848 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004849 case ISD::ADDC: return "addc";
4850 case ISD::ADDE: return "adde";
4851 case ISD::SUBC: return "subc";
4852 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004853 case ISD::SHL_PARTS: return "shl_parts";
4854 case ISD::SRA_PARTS: return "sra_parts";
4855 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004856
4857 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4858 case ISD::INSERT_SUBREG: return "insert_subreg";
4859
Chris Lattner7f644642005-04-28 21:44:03 +00004860 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004861 case ISD::SIGN_EXTEND: return "sign_extend";
4862 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004863 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004864 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004865 case ISD::TRUNCATE: return "truncate";
4866 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004867 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004868 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004869 case ISD::FP_EXTEND: return "fp_extend";
4870
4871 case ISD::SINT_TO_FP: return "sint_to_fp";
4872 case ISD::UINT_TO_FP: return "uint_to_fp";
4873 case ISD::FP_TO_SINT: return "fp_to_sint";
4874 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004875 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004876
4877 // Control flow instructions
4878 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004879 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004880 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004881 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004882 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004883 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004884 case ISD::CALLSEQ_START: return "callseq_start";
4885 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004886
4887 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004888 case ISD::LOAD: return "load";
4889 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004890 case ISD::VAARG: return "vaarg";
4891 case ISD::VACOPY: return "vacopy";
4892 case ISD::VAEND: return "vaend";
4893 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004894 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004895 case ISD::EXTRACT_ELEMENT: return "extract_element";
4896 case ISD::BUILD_PAIR: return "build_pair";
4897 case ISD::STACKSAVE: return "stacksave";
4898 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004899 case ISD::TRAP: return "trap";
4900
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004901 // Bit manipulation
4902 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004903 case ISD::CTPOP: return "ctpop";
4904 case ISD::CTTZ: return "cttz";
4905 case ISD::CTLZ: return "ctlz";
4906
Chris Lattner36ce6912005-11-29 06:21:05 +00004907 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004908 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004909 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004910
Duncan Sands36397f52007-07-27 12:58:54 +00004911 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004912 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004913
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004914 case ISD::CONDCODE:
4915 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004916 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004917 case ISD::SETOEQ: return "setoeq";
4918 case ISD::SETOGT: return "setogt";
4919 case ISD::SETOGE: return "setoge";
4920 case ISD::SETOLT: return "setolt";
4921 case ISD::SETOLE: return "setole";
4922 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004923
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004924 case ISD::SETO: return "seto";
4925 case ISD::SETUO: return "setuo";
4926 case ISD::SETUEQ: return "setue";
4927 case ISD::SETUGT: return "setugt";
4928 case ISD::SETUGE: return "setuge";
4929 case ISD::SETULT: return "setult";
4930 case ISD::SETULE: return "setule";
4931 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004932
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004933 case ISD::SETEQ: return "seteq";
4934 case ISD::SETGT: return "setgt";
4935 case ISD::SETGE: return "setge";
4936 case ISD::SETLT: return "setlt";
4937 case ISD::SETLE: return "setle";
4938 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004939 }
4940 }
4941}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004942
Evan Cheng144d8f02006-11-09 17:55:04 +00004943const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004944 switch (AM) {
4945 default:
4946 return "";
4947 case ISD::PRE_INC:
4948 return "<pre-inc>";
4949 case ISD::PRE_DEC:
4950 return "<pre-dec>";
4951 case ISD::POST_INC:
4952 return "<post-inc>";
4953 case ISD::POST_DEC:
4954 return "<post-dec>";
4955 }
4956}
4957
Duncan Sands276dcbd2008-03-21 09:14:45 +00004958std::string ISD::ArgFlagsTy::getArgFlagsString() {
4959 std::string S = "< ";
4960
4961 if (isZExt())
4962 S += "zext ";
4963 if (isSExt())
4964 S += "sext ";
4965 if (isInReg())
4966 S += "inreg ";
4967 if (isSRet())
4968 S += "sret ";
4969 if (isByVal())
4970 S += "byval ";
4971 if (isNest())
4972 S += "nest ";
4973 if (getByValAlign())
4974 S += "byval-align:" + utostr(getByValAlign()) + " ";
4975 if (getOrigAlign())
4976 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4977 if (getByValSize())
4978 S += "byval-size:" + utostr(getByValSize()) + " ";
4979 return S + ">";
4980}
4981
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004982void SDNode::dump() const { dump(0); }
4983void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00004984 print(errs(), G);
4985}
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;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005003 if (unsigned RN = getOperand(i).ResNo)
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}