blob: 0bd1a4d2d082061a71c0b01b129886e8258d0617 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Chris Lattner944fac72008-08-23 22:23:09 +000032#include "llvm/Support/MathExtras.h"
33#include "llvm/Support/raw_ostream.h"
Chris Lattner012f2412006-02-17 21:58:01 +000034#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000035#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000036#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000037#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000038#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000039#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000040#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Chris Lattner0b3e5252006-08-15 19:11:05 +000043/// makeVTList - Return an instance of the SDVTList struct initialized with the
44/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000045static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000046 SDVTList Res = {VTs, NumVTs};
47 return Res;
48}
49
Duncan Sands83ec4b62008-06-06 12:08:01 +000050static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
51 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000052 default: assert(0 && "Unknown FP format");
53 case MVT::f32: return &APFloat::IEEEsingle;
54 case MVT::f64: return &APFloat::IEEEdouble;
55 case MVT::f80: return &APFloat::x87DoubleExtended;
56 case MVT::f128: return &APFloat::IEEEquad;
57 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
58 }
59}
60
Chris Lattnerf8dc0612008-02-03 06:49:24 +000061SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
62
Jim Laskey58b968b2005-08-17 20:08:02 +000063//===----------------------------------------------------------------------===//
64// ConstantFPSDNode Class
65//===----------------------------------------------------------------------===//
66
67/// isExactlyValue - We don't rely on operator== working on double values, as
68/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
69/// As such, this method can be used to do an exact bit-for-bit comparison of
70/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000071bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000072 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000073}
74
Duncan Sands83ec4b62008-06-06 12:08:01 +000075bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000076 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000077 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000078
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000079 // PPC long double cannot be converted to any other type.
80 if (VT == MVT::ppcf128 ||
81 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000082 return false;
83
Dale Johannesenf04afdb2007-08-30 00:23:21 +000084 // convert modifies in place, so make a copy.
85 APFloat Val2 = APFloat(Val);
Chris Lattnerec4a5672008-03-05 06:48:13 +000086 return Val2.convert(*MVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven) == APFloat::opOK;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000088}
89
Jim Laskey58b968b2005-08-17 20:08:02 +000090//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000091// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000093
Evan Chenga8df1662006-03-27 06:58:47 +000094/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000095/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000096bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000097 // Look through a bit convert.
98 if (N->getOpcode() == ISD::BIT_CONVERT)
99 N = N->getOperand(0).Val;
100
Evan Chenga8df1662006-03-27 06:58:47 +0000101 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000102
103 unsigned i = 0, e = N->getNumOperands();
104
105 // Skip over all of the undef values.
106 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
107 ++i;
108
109 // Do not accept an all-undef vector.
110 if (i == e) return false;
111
112 // Do not accept build_vectors that aren't all constants or which have non-~0
113 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000114 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000115 if (isa<ConstantSDNode>(NotZero)) {
116 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
117 return false;
118 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000119 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
120 convertToAPInt().isAllOnesValue())
121 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000122 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000123 return false;
124
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
130 return false;
131 return true;
132}
133
134
Evan Cheng4a147842006-03-26 09:50:58 +0000135/// isBuildVectorAllZeros - Return true if the specified node is a
136/// BUILD_VECTOR where all of the elements are 0 or undef.
137bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
140 N = N->getOperand(0).Val;
141
Evan Cheng4a147842006-03-26 09:50:58 +0000142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000143
144 unsigned i = 0, e = N->getNumOperands();
145
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
148 ++i;
149
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
152
153 // Do not accept build_vectors that aren't all constants or which have non-~0
154 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 return false;
159 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000161 return false;
162 } else
163 return false;
164
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 // undefs.
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
170 return false;
171 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000172}
173
Evan Chengefec7512008-02-18 23:04:32 +0000174/// isScalarToVector - Return true if the specified node is a
175/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
176/// element is not an undef.
177bool ISD::isScalarToVector(const SDNode *N) {
178 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
179 return true;
180
181 if (N->getOpcode() != ISD::BUILD_VECTOR)
182 return false;
183 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
184 return false;
185 unsigned NumElems = N->getNumOperands();
186 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000188 if (V.getOpcode() != ISD::UNDEF)
189 return false;
190 }
191 return true;
192}
193
194
Evan Chengbb81d972008-01-31 09:59:15 +0000195/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000196/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000197bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000199 if (N->getOpcode() == ISD::DBG_LABEL)
200 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000201 if (N->isMachineOpcode() &&
202 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000203 return true;
204 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000205}
206
Chris Lattnerc3aae252005-01-07 07:46:32 +0000207/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
208/// when given the operation for (X op Y).
209ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
210 // To perform this operation, we just need to swap the L and G bits of the
211 // operation.
212 unsigned OldL = (Operation >> 2) & 1;
213 unsigned OldG = (Operation >> 1) & 1;
214 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
215 (OldL << 1) | // New G bit
216 (OldG << 2)); // New L bit.
217}
218
219/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
220/// 'op' is a valid SetCC operation.
221ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
222 unsigned Operation = Op;
223 if (isInteger)
224 Operation ^= 7; // Flip L, G, E bits, but not U.
225 else
226 Operation ^= 15; // Flip all of the condition bits.
227 if (Operation > ISD::SETTRUE2)
228 Operation &= ~8; // Don't let N and U bits get set.
229 return ISD::CondCode(Operation);
230}
231
232
233/// isSignedOp - For an integer comparison, return 1 if the comparison is a
234/// signed operation and 2 if the result is an unsigned comparison. Return zero
235/// if the operation does not depend on the sign of the input (setne and seteq).
236static int isSignedOp(ISD::CondCode Opcode) {
237 switch (Opcode) {
238 default: assert(0 && "Illegal integer setcc operation!");
239 case ISD::SETEQ:
240 case ISD::SETNE: return 0;
241 case ISD::SETLT:
242 case ISD::SETLE:
243 case ISD::SETGT:
244 case ISD::SETGE: return 1;
245 case ISD::SETULT:
246 case ISD::SETULE:
247 case ISD::SETUGT:
248 case ISD::SETUGE: return 2;
249 }
250}
251
252/// getSetCCOrOperation - Return the result of a logical OR between different
253/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
254/// returns SETCC_INVALID if it is not possible to represent the resultant
255/// comparison.
256ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
257 bool isInteger) {
258 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
259 // Cannot fold a signed integer setcc with an unsigned integer setcc.
260 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000261
Chris Lattnerc3aae252005-01-07 07:46:32 +0000262 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattnerc3aae252005-01-07 07:46:32 +0000264 // If the N and U bits get set then the resultant comparison DOES suddenly
265 // care about orderedness, and is true when ordered.
266 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000267 Op &= ~16; // Clear the U bit if the N bit is set.
268
269 // Canonicalize illegal integer setcc's.
270 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
271 Op = ISD::SETNE;
272
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273 return ISD::CondCode(Op);
274}
275
276/// getSetCCAndOperation - Return the result of a logical AND between different
277/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
278/// function returns zero if it is not possible to represent the resultant
279/// comparison.
280ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
281 bool isInteger) {
282 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
283 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000284 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000285
286 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000287 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
288
289 // Canonicalize illegal integer setcc's.
290 if (isInteger) {
291 switch (Result) {
292 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000293 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000294 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000295 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
296 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
297 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000298 }
299 }
300
301 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000302}
303
Chris Lattnerb48da392005-01-23 04:39:44 +0000304const TargetMachine &SelectionDAG::getTarget() const {
305 return TLI.getTargetMachine();
306}
307
Jim Laskey58b968b2005-08-17 20:08:02 +0000308//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000309// SDNode Profile Support
310//===----------------------------------------------------------------------===//
311
Jim Laskeydef69b92006-10-27 23:52:51 +0000312/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
313///
Jim Laskey583bd472006-10-27 23:46:08 +0000314static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
315 ID.AddInteger(OpC);
316}
317
318/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
319/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000320static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000321 ID.AddPointer(VTList.VTs);
322}
323
Jim Laskeydef69b92006-10-27 23:52:51 +0000324/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
325///
Jim Laskey583bd472006-10-27 23:46:08 +0000326static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000327 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000328 for (; NumOps; --NumOps, ++Ops) {
329 ID.AddPointer(Ops->Val);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000330 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000331 }
Jim Laskey583bd472006-10-27 23:46:08 +0000332}
333
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000334/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
335///
336static void AddNodeIDOperands(FoldingSetNodeID &ID,
337 const SDUse *Ops, unsigned NumOps) {
338 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman475871a2008-07-27 21:46:04 +0000339 ID.AddPointer(Ops->getVal());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000340 ID.AddInteger(Ops->getSDValue().getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000341 }
342}
343
Jim Laskey583bd472006-10-27 23:46:08 +0000344static void AddNodeIDNode(FoldingSetNodeID &ID,
345 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000346 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000347 AddNodeIDOpcode(ID, OpC);
348 AddNodeIDValueTypes(ID, VTList);
349 AddNodeIDOperands(ID, OpList, N);
350}
351
Roman Levenstein9cac5252008-04-16 16:15:27 +0000352
Jim Laskeydef69b92006-10-27 23:52:51 +0000353/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
354/// data.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000355static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000356 AddNodeIDOpcode(ID, N->getOpcode());
357 // Add the return value info.
358 AddNodeIDValueTypes(ID, N->getVTList());
359 // Add the operand info.
360 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
361
362 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000363 switch (N->getOpcode()) {
364 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000365 case ISD::ARG_FLAGS:
366 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
367 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 case ISD::TargetConstant:
369 case ISD::Constant:
Chris Lattner19fc1d32008-02-20 06:28:01 +0000370 ID.Add(cast<ConstantSDNode>(N)->getAPIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000371 break;
372 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000373 case ISD::ConstantFP: {
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000374 ID.Add(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000376 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000377 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000378 case ISD::GlobalAddress:
379 case ISD::TargetGlobalTLSAddress:
380 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000381 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000382 ID.AddPointer(GA->getGlobal());
383 ID.AddInteger(GA->getOffset());
384 break;
385 }
386 case ISD::BasicBlock:
387 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
388 break;
389 case ISD::Register:
390 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
391 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000392 case ISD::DBG_STOPPOINT: {
393 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
394 ID.AddInteger(DSP->getLine());
395 ID.AddInteger(DSP->getColumn());
396 ID.AddPointer(DSP->getCompileUnit());
397 break;
398 }
Dan Gohman69de1932008-02-06 22:27:42 +0000399 case ISD::SRCVALUE:
400 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
401 break;
402 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000403 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000404 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000405 break;
406 }
407 case ISD::FrameIndex:
408 case ISD::TargetFrameIndex:
409 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
410 break;
411 case ISD::JumpTable:
412 case ISD::TargetJumpTable:
413 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
414 break;
415 case ISD::ConstantPool:
416 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000417 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000418 ID.AddInteger(CP->getAlignment());
419 ID.AddInteger(CP->getOffset());
420 if (CP->isMachineConstantPoolEntry())
421 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
422 else
423 ID.AddPointer(CP->getConstVal());
424 break;
425 }
426 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000427 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000428 ID.AddInteger(LD->getAddressingMode());
429 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000430 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 break;
433 }
434 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000435 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 ID.AddInteger(ST->getAddressingMode());
437 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000438 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000439 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 break;
441 }
Mon P Wang28873102008-06-25 08:15:39 +0000442 case ISD::ATOMIC_CMP_SWAP:
443 case ISD::ATOMIC_LOAD_ADD:
444 case ISD::ATOMIC_SWAP:
445 case ISD::ATOMIC_LOAD_SUB:
446 case ISD::ATOMIC_LOAD_AND:
447 case ISD::ATOMIC_LOAD_OR:
448 case ISD::ATOMIC_LOAD_XOR:
449 case ISD::ATOMIC_LOAD_NAND:
450 case ISD::ATOMIC_LOAD_MIN:
451 case ISD::ATOMIC_LOAD_MAX:
452 case ISD::ATOMIC_LOAD_UMIN:
453 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000454 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
455 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000456 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000457 }
Mon P Wang28873102008-06-25 08:15:39 +0000458 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000459}
460
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000461/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
462/// the CSE map that carries both alignment and volatility information.
463///
464static unsigned encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
465 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
466}
467
Jim Laskey583bd472006-10-27 23:46:08 +0000468//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000469// SelectionDAG Class
470//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000471
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000472/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000473/// SelectionDAG.
474void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000475 // Create a dummy node (which is not added to allnodes), that adds a reference
476 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000477 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000478
Chris Lattner190a4182006-08-04 17:45:20 +0000479 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000480
Chris Lattner190a4182006-08-04 17:45:20 +0000481 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000482 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000483 if (I->use_empty())
484 DeadNodes.push_back(I);
485
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000486 RemoveDeadNodes(DeadNodes);
487
488 // If the root changed (e.g. it was a dead load, update the root).
489 setRoot(Dummy.getValue());
490}
491
492/// RemoveDeadNodes - This method deletes the unreachable nodes in the
493/// given list, and any nodes that become unreachable as a result.
494void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
495 DAGUpdateListener *UpdateListener) {
496
Chris Lattner190a4182006-08-04 17:45:20 +0000497 // Process the worklist, deleting the nodes and adding their uses to the
498 // worklist.
499 while (!DeadNodes.empty()) {
500 SDNode *N = DeadNodes.back();
501 DeadNodes.pop_back();
502
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000503 if (UpdateListener)
504 UpdateListener->NodeDeleted(N, 0);
505
Chris Lattner190a4182006-08-04 17:45:20 +0000506 // Take the node out of the appropriate CSE map.
507 RemoveNodeFromCSEMaps(N);
508
509 // Next, brutally remove the operand list. This is safe to do, as there are
510 // no cycles in the graph.
511 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000512 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000513 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000514
515 // Now that we removed this operand, see if there are no uses of it left.
516 if (Operand->use_empty())
517 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000518 }
Roman Levensteindc1adac2008-04-07 10:06:32 +0000519 if (N->OperandsNeedDelete) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000520 delete[] N->OperandList;
Roman Levensteindc1adac2008-04-07 10:06:32 +0000521 }
Chris Lattner190a4182006-08-04 17:45:20 +0000522 N->OperandList = 0;
523 N->NumOperands = 0;
524
525 // Finally, remove N itself.
Dan Gohman11467282008-08-26 01:44:34 +0000526 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerf469cb62005-11-08 18:52:27 +0000527 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000528}
529
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000530void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000531 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000532 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000533}
534
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000535void SelectionDAG::DeleteNode(SDNode *N) {
536 assert(N->use_empty() && "Cannot delete a node that is not dead!");
537
538 // First take this out of the appropriate CSE map.
539 RemoveNodeFromCSEMaps(N);
540
Chris Lattner1e111c72005-09-07 05:37:01 +0000541 // Finally, remove uses due to operands of this node, remove from the
542 // AllNodes list, and delete the node.
543 DeleteNodeNotInCSEMaps(N);
544}
545
546void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
547
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000548 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000549 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000550 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Dan Gohmanf350b272008-08-23 02:25:05 +0000551 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000552 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000553
Dan Gohman11467282008-08-26 01:44:34 +0000554 assert(N != AllNodes.begin());
555 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000556}
557
Chris Lattner149c58c2005-08-16 18:17:10 +0000558/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
559/// correspond to it. This is useful when we're about to delete or repurpose
560/// the node. We don't want future request for structurally identical nodes
561/// to return N anymore.
562void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000563 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000564 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000565 case ISD::EntryToken:
566 assert(0 && "EntryToken should not be in CSEMaps!");
567 return;
Chris Lattner95038592005-10-05 06:35:28 +0000568 case ISD::HANDLENODE: return; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000569 case ISD::CONDCODE:
570 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
571 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000572 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000573 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
574 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000575 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000576 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000577 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000578 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000579 Erased =
580 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000581 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000582 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000583 MVT VT = cast<VTSDNode>(N)->getVT();
584 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000585 Erased = ExtendedValueTypeNodes.erase(VT);
586 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000587 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
588 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000589 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000590 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000591 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000592 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000593 // Remove it from the CSE Map.
594 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000595 break;
596 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000597#ifndef NDEBUG
598 // Verify that the node was actually in one of the CSE maps, unless it has a
599 // flag result (which cannot be CSE'd) or is one of the special cases that are
600 // not subject to CSE.
601 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Evan Cheng71e86852008-07-08 20:06:39 +0000602 !N->isTargetOpcode() &&
603 N->getOpcode() != ISD::DBG_LABEL &&
604 N->getOpcode() != ISD::DBG_STOPPOINT &&
605 N->getOpcode() != ISD::EH_LABEL &&
606 N->getOpcode() != ISD::DECLARE) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000607 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000608 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000609 assert(0 && "Node is not in map!");
610 }
611#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000612}
613
Chris Lattner8b8749f2005-08-17 19:00:20 +0000614/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
615/// has been taken out and modified in some way. If the specified node already
616/// exists in the CSE maps, do not modify the maps, but return the existing node
617/// instead. If it doesn't exist, add it and return null.
618///
619SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
620 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000621
622 if (N->getValueType(0) == MVT::Flag)
623 return 0; // Never CSE anything that produces a flag.
624
625 switch (N->getOpcode()) {
626 default: break;
627 case ISD::HANDLENODE:
628 case ISD::DBG_LABEL:
629 case ISD::DBG_STOPPOINT:
630 case ISD::EH_LABEL:
631 case ISD::DECLARE:
Chris Lattnerfe14b342005-12-01 23:14:50 +0000632 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000633 }
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000634
Chris Lattner9f8cc692005-12-19 22:21:21 +0000635 // Check that remaining values produced are not flags.
636 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
637 if (N->getValueType(i) == MVT::Flag)
638 return 0; // Never CSE anything that produces a flag.
639
Chris Lattnera5682852006-08-07 23:03:03 +0000640 SDNode *New = CSEMap.GetOrInsertNode(N);
641 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000642 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000643}
644
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000645/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
646/// were replaced with those specified. If this node is never memoized,
647/// return null, otherwise return a pointer to the slot it would take. If a
648/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000649SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000650 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000651 if (N->getValueType(0) == MVT::Flag)
652 return 0; // Never CSE anything that produces a flag.
653
654 switch (N->getOpcode()) {
655 default: break;
656 case ISD::HANDLENODE:
657 case ISD::DBG_LABEL:
658 case ISD::DBG_STOPPOINT:
659 case ISD::EH_LABEL:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000660 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000661 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000662
663 // Check that remaining values produced are not flags.
664 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
665 if (N->getValueType(i) == MVT::Flag)
666 return 0; // Never CSE anything that produces a flag.
667
Dan Gohman475871a2008-07-27 21:46:04 +0000668 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000669 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000670 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000671 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000672}
673
674/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
675/// were replaced with those specified. If this node is never memoized,
676/// return null, otherwise return a pointer to the slot it would take. If a
677/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000678SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000679 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000680 void *&InsertPos) {
681 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnera5682852006-08-07 23:03:03 +0000682
683 // Check that remaining values produced are not flags.
684 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
685 if (N->getValueType(i) == MVT::Flag)
686 return 0; // Never CSE anything that produces a flag.
687
Dan Gohman475871a2008-07-27 21:46:04 +0000688 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000689 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000690 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000691 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
692}
693
694
695/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
696/// were replaced with those specified. If this node is never memoized,
697/// return null, otherwise return a pointer to the slot it would take. If a
698/// node already exists with these operands, the slot will be non-null.
699SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000700 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000701 void *&InsertPos) {
Evan Cheng71e86852008-07-08 20:06:39 +0000702 if (N->getValueType(0) == MVT::Flag)
703 return 0; // Never CSE anything that produces a flag.
704
705 switch (N->getOpcode()) {
706 default: break;
707 case ISD::HANDLENODE:
708 case ISD::DBG_LABEL:
709 case ISD::DBG_STOPPOINT:
710 case ISD::EH_LABEL:
711 case ISD::DECLARE:
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000712 return 0; // Never add these nodes.
Evan Cheng71e86852008-07-08 20:06:39 +0000713 }
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000714
715 // Check that remaining values produced are not flags.
716 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
717 if (N->getValueType(i) == MVT::Flag)
718 return 0; // Never CSE anything that produces a flag.
719
Jim Laskey583bd472006-10-27 23:46:08 +0000720 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000721 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000722
Evan Cheng9629aba2006-10-11 01:47:58 +0000723 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
724 ID.AddInteger(LD->getAddressingMode());
725 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000726 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000727 ID.AddInteger(LD->getRawFlags());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000728 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
729 ID.AddInteger(ST->getAddressingMode());
730 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000731 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000732 ID.AddInteger(ST->getRawFlags());
Evan Cheng9629aba2006-10-11 01:47:58 +0000733 }
Jim Laskey583bd472006-10-27 23:46:08 +0000734
Chris Lattnera5682852006-08-07 23:03:03 +0000735 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000736}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000737
Duncan Sandsd038e042008-07-21 10:20:31 +0000738/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
739void SelectionDAG::VerifyNode(SDNode *N) {
740 switch (N->getOpcode()) {
741 default:
742 break;
743 case ISD::BUILD_VECTOR: {
744 assert(N->getNumValues() == 1 && "Too many results for BUILD_VECTOR!");
745 assert(N->getValueType(0).isVector() && "Wrong BUILD_VECTOR return type!");
746 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
747 "Wrong number of BUILD_VECTOR operands!");
748 MVT EltVT = N->getValueType(0).getVectorElementType();
749 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Dan Gohman475871a2008-07-27 21:46:04 +0000750 assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd038e042008-07-21 10:20:31 +0000751 "Wrong BUILD_VECTOR operand type!");
752 break;
753 }
754 }
755}
756
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000757/// getMVTAlignment - Compute the default alignment value for the
758/// given type.
759///
760unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
761 const Type *Ty = VT == MVT::iPTR ?
762 PointerType::get(Type::Int8Ty, 0) :
763 VT.getTypeForMVT();
764
765 return TLI.getTargetData()->getABITypeAlignment(Ty);
766}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000767
Dan Gohman7c3234c2008-08-27 23:52:12 +0000768SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
769 : TLI(tli), FLI(fli),
770 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000771 Root(getEntryNode()) {
772 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000773}
774
Dan Gohman7c3234c2008-08-27 23:52:12 +0000775void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi) {
776 MF = &mf;
777 MMI = mmi;
778}
779
Chris Lattner78ec3112003-08-11 14:57:33 +0000780SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000781 allnodes_clear();
782}
783
784void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000785 assert(&*AllNodes.begin() == &EntryNode);
786 AllNodes.remove(AllNodes.begin());
Chris Lattnerde202b32005-11-09 23:47:37 +0000787 while (!AllNodes.empty()) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000788 SDNode *N = AllNodes.remove(AllNodes.begin());
Chris Lattner213a16c2006-08-14 22:19:25 +0000789 N->SetNextInBucket(0);
Dan Gohmanf350b272008-08-23 02:25:05 +0000790 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000791 delete [] N->OperandList;
Dan Gohman11467282008-08-26 01:44:34 +0000792 NodeAllocator.Deallocate(N);
Chris Lattner65113b22005-11-08 22:07:03 +0000793 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000794}
795
Dan Gohman7c3234c2008-08-27 23:52:12 +0000796void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000797 allnodes_clear();
798 OperandAllocator.Reset();
799 CSEMap.clear();
800
801 ExtendedValueTypeNodes.clear();
802 ExternalSymbols.clear();
803 TargetExternalSymbols.clear();
804 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
805 static_cast<CondCodeSDNode*>(0));
806 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
807 static_cast<SDNode*>(0));
808
809 EntryNode.Uses = 0;
810 AllNodes.push_back(&EntryNode);
811 Root = getEntryNode();
812}
813
Dan Gohman475871a2008-07-27 21:46:04 +0000814SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000815 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000816 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000818 return getNode(ISD::AND, Op.getValueType(), Op,
819 getConstant(Imm, Op.getValueType()));
820}
821
Dan Gohman475871a2008-07-27 21:46:04 +0000822SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000823 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000824 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000825}
826
Dan Gohman475871a2008-07-27 21:46:04 +0000827SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000828 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000829
Evan Cheng0ff39b32008-06-30 07:31:25 +0000830 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000831 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000832 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000833
834 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000835 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000836 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000837 ID.Add(Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000838 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000839 SDNode *N = NULL;
840 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000841 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000842 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000843 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000844 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000845 new (N) ConstantSDNode(isT, Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000846 CSEMap.InsertNode(N, IP);
847 AllNodes.push_back(N);
848 }
849
Dan Gohman475871a2008-07-27 21:46:04 +0000850 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000851 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000852 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000854 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
855 }
856 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000857}
858
Dan Gohman475871a2008-07-27 21:46:04 +0000859SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000860 return getConstant(Val, TLI.getPointerTy(), isTarget);
861}
862
863
Dan Gohman475871a2008-07-27 21:46:04 +0000864SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000865 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000866
Duncan Sands83ec4b62008-06-06 12:08:01 +0000867 MVT EltVT =
868 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000869
Chris Lattnerd8658612005-02-17 20:17:32 +0000870 // Do the map lookup using the actual bit pattern for the floating point
871 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
872 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000873 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000874 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000875 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Ted Kremenek1f801fa2008-02-11 17:24:50 +0000876 ID.Add(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000877 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000878 SDNode *N = NULL;
879 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000880 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000881 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000882 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000883 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000884 new (N) ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000885 CSEMap.InsertNode(N, IP);
886 AllNodes.push_back(N);
887 }
888
Dan Gohman475871a2008-07-27 21:46:04 +0000889 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000890 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000891 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000892 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman7f321562007-06-25 16:23:39 +0000893 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
894 }
895 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000896}
897
Dan Gohman475871a2008-07-27 21:46:04 +0000898SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000899 MVT EltVT =
900 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000901 if (EltVT==MVT::f32)
902 return getConstantFP(APFloat((float)Val), VT, isTarget);
903 else
904 return getConstantFP(APFloat(Val), VT, isTarget);
905}
906
Dan Gohman475871a2008-07-27 21:46:04 +0000907SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
908 MVT VT, int Offset,
909 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000910 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000911
912 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
913 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000914 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000915 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
916 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal());
917 }
918
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000919 if (GVar && GVar->isThreadLocal())
920 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
921 else
922 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000923
Jim Laskey583bd472006-10-27 23:46:08 +0000924 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000925 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000926 ID.AddPointer(GV);
927 ID.AddInteger(Offset);
928 void *IP = 0;
929 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000930 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000931 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000932 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000933 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000934 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000935 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936}
937
Dan Gohman475871a2008-07-27 21:46:04 +0000938SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000939 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000940 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000941 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000942 ID.AddInteger(FI);
943 void *IP = 0;
944 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000945 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000946 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000947 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000948 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000949 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000950 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000951}
952
Dan Gohman475871a2008-07-27 21:46:04 +0000953SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000954 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000955 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000956 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000957 ID.AddInteger(JTI);
958 void *IP = 0;
959 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000960 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000961 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000962 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000963 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000964 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000965 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +0000966}
967
Dan Gohman475871a2008-07-27 21:46:04 +0000968SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
969 unsigned Alignment, int Offset,
970 bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000971 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000972 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000973 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000974 ID.AddInteger(Alignment);
975 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000976 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000977 void *IP = 0;
978 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000979 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000980 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000981 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000982 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000983 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000984 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000985}
986
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987
Dan Gohman475871a2008-07-27 21:46:04 +0000988SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
989 unsigned Alignment, int Offset,
990 bool isTarget) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000991 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000992 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000993 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000994 ID.AddInteger(Alignment);
995 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000996 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000997 void *IP = 0;
998 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000999 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001000 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001001 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001002 CSEMap.InsertNode(N, IP);
1003 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001004 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001005}
1006
1007
Dan Gohman475871a2008-07-27 21:46:04 +00001008SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001009 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001010 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001011 ID.AddPointer(MBB);
1012 void *IP = 0;
1013 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001014 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001015 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001016 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001017 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001018 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001019 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001020}
1021
Dan Gohman475871a2008-07-27 21:46:04 +00001022SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001023 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001024 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001025 ID.AddInteger(Flags.getRawBits());
1026 void *IP = 0;
1027 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001028 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001029 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001030 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001031 CSEMap.InsertNode(N, IP);
1032 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001033 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001034}
1035
Dan Gohman475871a2008-07-27 21:46:04 +00001036SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001037 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1038 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001039
Duncan Sands83ec4b62008-06-06 12:08:01 +00001040 SDNode *&N = VT.isExtended() ?
1041 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001042
Dan Gohman475871a2008-07-27 21:46:04 +00001043 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001044 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001045 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001046 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001047 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001048}
1049
Dan Gohman475871a2008-07-27 21:46:04 +00001050SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001051 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001052 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001053 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001054 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001055 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001056 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001057}
1058
Dan Gohman475871a2008-07-27 21:46:04 +00001059SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001060 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001061 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001062 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001063 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001064 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001065 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066}
1067
Dan Gohman475871a2008-07-27 21:46:04 +00001068SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001069 if ((unsigned)Cond >= CondCodeNodes.size())
1070 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001071
Chris Lattner079a27a2005-08-09 20:40:02 +00001072 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001073 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001074 new (N) CondCodeSDNode(Cond);
1075 CondCodeNodes[Cond] = N;
1076 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001077 }
Dan Gohman475871a2008-07-27 21:46:04 +00001078 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001079}
1080
Dan Gohman475871a2008-07-27 21:46:04 +00001081SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001082 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001083 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001084 ID.AddInteger(RegNo);
1085 void *IP = 0;
1086 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001087 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001088 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001089 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001090 CSEMap.InsertNode(N, IP);
1091 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001092 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001093}
1094
Dan Gohman475871a2008-07-27 21:46:04 +00001095SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Dan Gohmanfed90b62008-07-28 21:51:04 +00001096 unsigned Line, unsigned Col,
1097 const CompileUnitDesc *CU) {
1098 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001099 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001100 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001101 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001102}
1103
Dan Gohman475871a2008-07-27 21:46:04 +00001104SDValue SelectionDAG::getLabel(unsigned Opcode,
1105 SDValue Root,
1106 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001107 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001108 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001109 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1110 ID.AddInteger(LabelID);
1111 void *IP = 0;
1112 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001113 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001114 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001115 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001116 CSEMap.InsertNode(N, IP);
1117 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001118 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001119}
1120
Dan Gohman475871a2008-07-27 21:46:04 +00001121SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001122 assert((!V || isa<PointerType>(V->getType())) &&
1123 "SrcValue is not a pointer?");
1124
Jim Laskey583bd472006-10-27 23:46:08 +00001125 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001126 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001127 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001128
Chris Lattner61b09412006-08-11 21:01:22 +00001129 void *IP = 0;
1130 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001131 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001132
Dan Gohmanfed90b62008-07-28 21:51:04 +00001133 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001134 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001135 CSEMap.InsertNode(N, IP);
1136 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001137 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001138}
1139
Dan Gohman475871a2008-07-27 21:46:04 +00001140SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +00001141 const Value *v = MO.getValue();
1142 assert((!v || isa<PointerType>(v->getType())) &&
1143 "SrcValue is not a pointer?");
1144
1145 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001146 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001147 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001148
1149 void *IP = 0;
1150 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001151 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001152
Dan Gohmanfed90b62008-07-28 21:51:04 +00001153 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001154 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001155 CSEMap.InsertNode(N, IP);
1156 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001157 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001158}
1159
Chris Lattner37ce9df2007-10-15 17:47:20 +00001160/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1161/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001162SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001163 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001164 unsigned ByteSize = VT.getSizeInBits()/8;
1165 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001166 unsigned StackAlign =
1167 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1168
Chris Lattner37ce9df2007-10-15 17:47:20 +00001169 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1170 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1171}
1172
Dan Gohman475871a2008-07-27 21:46:04 +00001173SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1174 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001175 // These setcc operations always fold.
1176 switch (Cond) {
1177 default: break;
1178 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001179 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001180 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001181 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001182
1183 case ISD::SETOEQ:
1184 case ISD::SETOGT:
1185 case ISD::SETOGE:
1186 case ISD::SETOLT:
1187 case ISD::SETOLE:
1188 case ISD::SETONE:
1189 case ISD::SETO:
1190 case ISD::SETUO:
1191 case ISD::SETUEQ:
1192 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001193 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001194 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001196
Chris Lattner67255a12005-04-07 18:14:58 +00001197 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001198 const APInt &C2 = N2C->getAPIntValue();
Chris Lattner67255a12005-04-07 18:14:58 +00001199 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00001200 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001201
Chris Lattnerc3aae252005-01-07 07:46:32 +00001202 switch (Cond) {
1203 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001204 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1205 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001206 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1207 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1208 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1209 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1210 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1211 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1212 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1213 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001215 }
Chris Lattner67255a12005-04-07 18:14:58 +00001216 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001217 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001218 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001219 // No compile time operations on this type yet.
1220 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001221 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001222
1223 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001224 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001225 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001226 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1227 return getNode(ISD::UNDEF, VT);
1228 // fall through
1229 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1230 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1231 return getNode(ISD::UNDEF, VT);
1232 // fall through
1233 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001234 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001235 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1236 return getNode(ISD::UNDEF, VT);
1237 // fall through
1238 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1239 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1240 return getNode(ISD::UNDEF, VT);
1241 // fall through
1242 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1243 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1244 return getNode(ISD::UNDEF, VT);
1245 // fall through
1246 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001247 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001248 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1249 return getNode(ISD::UNDEF, VT);
1250 // fall through
1251 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001252 R==APFloat::cmpEqual, VT);
1253 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1254 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1255 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1256 R==APFloat::cmpEqual, VT);
1257 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1258 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1259 R==APFloat::cmpLessThan, VT);
1260 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1261 R==APFloat::cmpUnordered, VT);
1262 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1263 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001264 }
1265 } else {
1266 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001267 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001268 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001269 }
1270
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001271 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001272 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001273}
1274
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001275/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1276/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001277bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001278 unsigned BitWidth = Op.getValueSizeInBits();
1279 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1280}
1281
Dan Gohmanea859be2007-06-22 14:59:07 +00001282/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1283/// this predicate to simplify operations downstream. Mask is known to be zero
1284/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001285bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001286 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001287 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001288 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1289 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1290 return (KnownZero & Mask) == Mask;
1291}
1292
1293/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1294/// known to be either zero or one and return them in the KnownZero/KnownOne
1295/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1296/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001297void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001298 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001299 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001300 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001301 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001302 "Mask size mismatches value type size!");
1303
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001304 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001305 if (Depth == 6 || Mask == 0)
1306 return; // Limit search depth.
1307
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001308 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001309
1310 switch (Op.getOpcode()) {
1311 case ISD::Constant:
1312 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001313 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001314 KnownZero = ~KnownOne & Mask;
1315 return;
1316 case ISD::AND:
1317 // If either the LHS or the RHS are Zero, the result is zero.
1318 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001319 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1320 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001321 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1322 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1323
1324 // Output known-1 bits are only known if set in both the LHS & RHS.
1325 KnownOne &= KnownOne2;
1326 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1327 KnownZero |= KnownZero2;
1328 return;
1329 case ISD::OR:
1330 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001331 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1332 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001333 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1334 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1335
1336 // Output known-0 bits are only known if clear in both the LHS & RHS.
1337 KnownZero &= KnownZero2;
1338 // Output known-1 are known to be set if set in either the LHS | RHS.
1339 KnownOne |= KnownOne2;
1340 return;
1341 case ISD::XOR: {
1342 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1343 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1344 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1345 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1346
1347 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001348 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001349 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1350 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1351 KnownZero = KnownZeroOut;
1352 return;
1353 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001354 case ISD::MUL: {
1355 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1356 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1357 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1358 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1359 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1360
1361 // If low bits are zero in either operand, output low known-0 bits.
1362 // Also compute a conserative estimate for high known-0 bits.
1363 // More trickiness is possible, but this is sufficient for the
1364 // interesting case of alignment computation.
1365 KnownOne.clear();
1366 unsigned TrailZ = KnownZero.countTrailingOnes() +
1367 KnownZero2.countTrailingOnes();
1368 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001369 KnownZero2.countLeadingOnes(),
1370 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001371
1372 TrailZ = std::min(TrailZ, BitWidth);
1373 LeadZ = std::min(LeadZ, BitWidth);
1374 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1375 APInt::getHighBitsSet(BitWidth, LeadZ);
1376 KnownZero &= Mask;
1377 return;
1378 }
1379 case ISD::UDIV: {
1380 // For the purposes of computing leading zeros we can conservatively
1381 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001382 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001383 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1384 ComputeMaskedBits(Op.getOperand(0),
1385 AllOnes, KnownZero2, KnownOne2, Depth+1);
1386 unsigned LeadZ = KnownZero2.countLeadingOnes();
1387
1388 KnownOne2.clear();
1389 KnownZero2.clear();
1390 ComputeMaskedBits(Op.getOperand(1),
1391 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001392 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1393 if (RHSUnknownLeadingOnes != BitWidth)
1394 LeadZ = std::min(BitWidth,
1395 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001396
1397 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1398 return;
1399 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001400 case ISD::SELECT:
1401 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1402 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1403 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1404 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1405
1406 // Only known if known in both the LHS and RHS.
1407 KnownOne &= KnownOne2;
1408 KnownZero &= KnownZero2;
1409 return;
1410 case ISD::SELECT_CC:
1411 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1412 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1413 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1414 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1415
1416 // Only known if known in both the LHS and RHS.
1417 KnownOne &= KnownOne2;
1418 KnownZero &= KnownZero2;
1419 return;
1420 case ISD::SETCC:
1421 // If we know the result of a setcc has the top bits zero, use this info.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001422 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult &&
1423 BitWidth > 1)
1424 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001425 return;
1426 case ISD::SHL:
1427 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1428 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmand4cf9922008-02-26 18:50:50 +00001429 unsigned ShAmt = SA->getValue();
1430
1431 // If the shift count is an invalid immediate, don't do anything.
1432 if (ShAmt >= BitWidth)
1433 return;
1434
1435 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001436 KnownZero, KnownOne, Depth+1);
1437 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001438 KnownZero <<= ShAmt;
1439 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001440 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001441 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001442 }
1443 return;
1444 case ISD::SRL:
1445 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1446 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001447 unsigned ShAmt = SA->getValue();
1448
Dan Gohmand4cf9922008-02-26 18:50:50 +00001449 // If the shift count is an invalid immediate, don't do anything.
1450 if (ShAmt >= BitWidth)
1451 return;
1452
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001453 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001454 KnownZero, KnownOne, Depth+1);
1455 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001456 KnownZero = KnownZero.lshr(ShAmt);
1457 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001458
Dan Gohman72d2fd52008-02-13 22:43:25 +00001459 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001460 KnownZero |= HighBits; // High bits known zero.
1461 }
1462 return;
1463 case ISD::SRA:
1464 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001465 unsigned ShAmt = SA->getValue();
1466
Dan Gohmand4cf9922008-02-26 18:50:50 +00001467 // If the shift count is an invalid immediate, don't do anything.
1468 if (ShAmt >= BitWidth)
1469 return;
1470
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001471 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001472 // If any of the demanded bits are produced by the sign extension, we also
1473 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001474 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1475 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001476 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001477
1478 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1479 Depth+1);
1480 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001481 KnownZero = KnownZero.lshr(ShAmt);
1482 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001483
1484 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001485 APInt SignBit = APInt::getSignBit(BitWidth);
1486 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001487
Dan Gohmanca93a432008-02-20 16:30:17 +00001488 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001489 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001490 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001491 KnownOne |= HighBits; // New bits are known one.
1492 }
1493 }
1494 return;
1495 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001496 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1497 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001498
1499 // Sign extension. Compute the demanded bits in the result that are not
1500 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001501 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001502
Dan Gohman977a76f2008-02-13 22:28:48 +00001503 APInt InSignBit = APInt::getSignBit(EBits);
1504 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001505
1506 // If the sign extended bits are demanded, we know that the sign
1507 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001508 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001509 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001510 InputDemandedBits |= InSignBit;
1511
1512 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1513 KnownZero, KnownOne, Depth+1);
1514 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1515
1516 // If the sign bit of the input is known set or clear, then we know the
1517 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001518 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001519 KnownZero |= NewBits;
1520 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001521 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001522 KnownOne |= NewBits;
1523 KnownZero &= ~NewBits;
1524 } else { // Input sign bit unknown
1525 KnownZero &= ~NewBits;
1526 KnownOne &= ~NewBits;
1527 }
1528 return;
1529 }
1530 case ISD::CTTZ:
1531 case ISD::CTLZ:
1532 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001533 unsigned LowBits = Log2_32(BitWidth)+1;
1534 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001535 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001536 return;
1537 }
1538 case ISD::LOAD: {
1539 if (ISD::isZEXTLoad(Op.Val)) {
1540 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001541 MVT VT = LD->getMemoryVT();
1542 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001543 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001544 }
1545 return;
1546 }
1547 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001548 MVT InVT = Op.getOperand(0).getValueType();
1549 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001550 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1551 APInt InMask = Mask;
1552 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001553 KnownZero.trunc(InBits);
1554 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001555 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001556 KnownZero.zext(BitWidth);
1557 KnownOne.zext(BitWidth);
1558 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001559 return;
1560 }
1561 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001562 MVT InVT = Op.getOperand(0).getValueType();
1563 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001564 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001565 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1566 APInt InMask = Mask;
1567 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001568
1569 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001570 // bit is demanded. Temporarily set this bit in the mask for our callee.
1571 if (NewBits.getBoolValue())
1572 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001573
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001574 KnownZero.trunc(InBits);
1575 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001576 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1577
1578 // Note if the sign bit is known to be zero or one.
1579 bool SignBitKnownZero = KnownZero.isNegative();
1580 bool SignBitKnownOne = KnownOne.isNegative();
1581 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1582 "Sign bit can't be known to be both zero and one!");
1583
1584 // If the sign bit wasn't actually demanded by our caller, we don't
1585 // want it set in the KnownZero and KnownOne result values. Reset the
1586 // mask and reapply it to the result values.
1587 InMask = Mask;
1588 InMask.trunc(InBits);
1589 KnownZero &= InMask;
1590 KnownOne &= InMask;
1591
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001592 KnownZero.zext(BitWidth);
1593 KnownOne.zext(BitWidth);
1594
Dan Gohman977a76f2008-02-13 22:28:48 +00001595 // If the sign bit is known zero or one, the top bits match.
1596 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001597 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001598 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001599 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001600 return;
1601 }
1602 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001603 MVT InVT = Op.getOperand(0).getValueType();
1604 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001605 APInt InMask = Mask;
1606 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001607 KnownZero.trunc(InBits);
1608 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001609 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001610 KnownZero.zext(BitWidth);
1611 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001612 return;
1613 }
1614 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001615 MVT InVT = Op.getOperand(0).getValueType();
1616 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001617 APInt InMask = Mask;
1618 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001619 KnownZero.zext(InBits);
1620 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001621 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001622 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001623 KnownZero.trunc(BitWidth);
1624 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001625 break;
1626 }
1627 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001628 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1629 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001630 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1631 KnownOne, Depth+1);
1632 KnownZero |= (~InMask) & Mask;
1633 return;
1634 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001635 case ISD::FGETSIGN:
1636 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001637 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001638 return;
1639
Dan Gohman23e8b712008-04-28 17:02:21 +00001640 case ISD::SUB: {
1641 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1642 // We know that the top bits of C-X are clear if X contains less bits
1643 // than C (i.e. no wrap-around can happen). For example, 20-X is
1644 // positive if we can prove that X is >= 0 and < 16.
1645 if (CLHS->getAPIntValue().isNonNegative()) {
1646 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1647 // NLZ can't be BitWidth with no sign bit
1648 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1649 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1650 Depth+1);
1651
1652 // If all of the MaskV bits are known to be zero, then we know the
1653 // output top bits are zero, because we now know that the output is
1654 // from [0-C].
1655 if ((KnownZero2 & MaskV) == MaskV) {
1656 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1657 // Top bits known zero.
1658 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1659 }
1660 }
1661 }
1662 }
1663 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001664 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001665 // Output known-0 bits are known if clear or set in both the low clear bits
1666 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1667 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001668 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1669 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1670 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1671 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1672
1673 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1674 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1675 KnownZeroOut = std::min(KnownZeroOut,
1676 KnownZero2.countTrailingOnes());
1677
1678 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001679 return;
1680 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001681 case ISD::SREM:
1682 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001683 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001684 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001685 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001686 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1687 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001688
Dan Gohmana60832b2008-08-13 23:12:35 +00001689 // If the sign bit of the first operand is zero, the sign bit of
1690 // the result is zero. If the first operand has no one bits below
1691 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001692 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1693 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001694
Dan Gohman23e8b712008-04-28 17:02:21 +00001695 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001696
1697 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001698 }
1699 }
1700 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001701 case ISD::UREM: {
1702 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001703 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001704 if (RA.isPowerOf2()) {
1705 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001706 APInt Mask2 = LowBits & Mask;
1707 KnownZero |= ~LowBits & Mask;
1708 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1709 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1710 break;
1711 }
1712 }
1713
1714 // Since the result is less than or equal to either operand, any leading
1715 // zero bits in either operand must also exist in the result.
1716 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1717 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1718 Depth+1);
1719 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1720 Depth+1);
1721
1722 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1723 KnownZero2.countLeadingOnes());
1724 KnownOne.clear();
1725 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1726 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001727 }
1728 default:
1729 // Allow the target to implement this method for its nodes.
1730 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1731 case ISD::INTRINSIC_WO_CHAIN:
1732 case ISD::INTRINSIC_W_CHAIN:
1733 case ISD::INTRINSIC_VOID:
1734 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1735 }
1736 return;
1737 }
1738}
1739
1740/// ComputeNumSignBits - Return the number of times the sign bit of the
1741/// register is replicated into the other bits. We know that at least 1 bit
1742/// is always equal to the sign bit (itself), but other cases can give us
1743/// information. For example, immediately after an "SRA X, 2", we know that
1744/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001745unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001746 MVT VT = Op.getValueType();
1747 assert(VT.isInteger() && "Invalid VT!");
1748 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001749 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001750 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001751
1752 if (Depth == 6)
1753 return 1; // Limit search depth.
1754
1755 switch (Op.getOpcode()) {
1756 default: break;
1757 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001758 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001759 return VTBits-Tmp+1;
1760 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001761 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001762 return VTBits-Tmp;
1763
1764 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001765 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1766 // If negative, return # leading ones.
1767 if (Val.isNegative())
1768 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001769
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001770 // Return # leading zeros.
1771 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001772 }
1773
1774 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001775 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001776 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1777
1778 case ISD::SIGN_EXTEND_INREG:
1779 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001780 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001781 Tmp = VTBits-Tmp+1;
1782
1783 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1784 return std::max(Tmp, Tmp2);
1785
1786 case ISD::SRA:
1787 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1788 // SRA X, C -> adds C sign bits.
1789 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1790 Tmp += C->getValue();
1791 if (Tmp > VTBits) Tmp = VTBits;
1792 }
1793 return Tmp;
1794 case ISD::SHL:
1795 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1796 // shl destroys sign bits.
1797 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1798 if (C->getValue() >= VTBits || // Bad shift.
1799 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1800 return Tmp - C->getValue();
1801 }
1802 break;
1803 case ISD::AND:
1804 case ISD::OR:
1805 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001806 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001807 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001808 if (Tmp != 1) {
1809 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1810 FirstAnswer = std::min(Tmp, Tmp2);
1811 // We computed what we know about the sign bits as our first
1812 // answer. Now proceed to the generic code that uses
1813 // ComputeMaskedBits, and pick whichever answer is better.
1814 }
1815 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001816
1817 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001818 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001819 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001820 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001821 return std::min(Tmp, Tmp2);
1822
1823 case ISD::SETCC:
1824 // If setcc returns 0/-1, all bits are sign bits.
1825 if (TLI.getSetCCResultContents() ==
1826 TargetLowering::ZeroOrNegativeOneSetCCResult)
1827 return VTBits;
1828 break;
1829 case ISD::ROTL:
1830 case ISD::ROTR:
1831 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1832 unsigned RotAmt = C->getValue() & (VTBits-1);
1833
1834 // Handle rotate right by N like a rotate left by 32-N.
1835 if (Op.getOpcode() == ISD::ROTR)
1836 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1837
1838 // If we aren't rotating out all of the known-in sign bits, return the
1839 // number that are left. This handles rotl(sext(x), 1) for example.
1840 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1841 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1842 }
1843 break;
1844 case ISD::ADD:
1845 // Add can have at most one carry bit. Thus we know that the output
1846 // is, at worst, one more bit than the inputs.
1847 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1848 if (Tmp == 1) return 1; // Early out.
1849
1850 // Special case decrementing a value (ADD X, -1):
1851 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1852 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001853 APInt KnownZero, KnownOne;
1854 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001855 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1856
1857 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1858 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001859 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001860 return VTBits;
1861
1862 // If we are subtracting one from a positive number, there is no carry
1863 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001864 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001865 return Tmp;
1866 }
1867
1868 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1869 if (Tmp2 == 1) return 1;
1870 return std::min(Tmp, Tmp2)-1;
1871 break;
1872
1873 case ISD::SUB:
1874 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1875 if (Tmp2 == 1) return 1;
1876
1877 // Handle NEG.
1878 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001879 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001880 APInt KnownZero, KnownOne;
1881 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001882 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1883 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1884 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001885 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001886 return VTBits;
1887
1888 // If the input is known to be positive (the sign bit is known clear),
1889 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001890 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001891 return Tmp2;
1892
1893 // Otherwise, we treat this like a SUB.
1894 }
1895
1896 // Sub can have at most one carry bit. Thus we know that the output
1897 // is, at worst, one more bit than the inputs.
1898 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1899 if (Tmp == 1) return 1; // Early out.
1900 return std::min(Tmp, Tmp2)-1;
1901 break;
1902 case ISD::TRUNCATE:
1903 // FIXME: it's tricky to do anything useful for this, but it is an important
1904 // case for targets like X86.
1905 break;
1906 }
1907
1908 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1909 if (Op.getOpcode() == ISD::LOAD) {
1910 LoadSDNode *LD = cast<LoadSDNode>(Op);
1911 unsigned ExtType = LD->getExtensionType();
1912 switch (ExtType) {
1913 default: break;
1914 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001915 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001916 return VTBits-Tmp+1;
1917 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001918 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001919 return VTBits-Tmp;
1920 }
1921 }
1922
1923 // Allow the target to implement this method for its nodes.
1924 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1925 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1926 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1927 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1928 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00001929 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001930 }
1931
1932 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1933 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001934 APInt KnownZero, KnownOne;
1935 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001936 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1937
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001938 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001939 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001940 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001941 Mask = KnownOne;
1942 } else {
1943 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00001944 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00001945 }
1946
1947 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1948 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001949 Mask = ~Mask;
1950 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001951 // Return # leading zeros. We use 'min' here in case Val was zero before
1952 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00001953 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00001954}
1955
Chris Lattner51dabfb2006-10-14 00:41:01 +00001956
Dan Gohman475871a2008-07-27 21:46:04 +00001957bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00001958 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
1959 if (!GA) return false;
1960 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
1961 if (!GV) return false;
1962 MachineModuleInfo *MMI = getMachineModuleInfo();
1963 return MMI && MMI->hasDebugInfo() && MMI->isVerified(GV);
1964}
1965
1966
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001967/// getShuffleScalarElt - Returns the scalar element that will make up the ith
1968/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00001969SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001970 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue PermMask = N->getOperand(2);
1972 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00001973 if (Idx.getOpcode() == ISD::UNDEF)
1974 return getNode(ISD::UNDEF, VT.getVectorElementType());
1975 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001976 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001977 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00001978 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00001979
1980 if (V.getOpcode() == ISD::BIT_CONVERT) {
1981 V = V.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001982 if (V.getValueType().getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00001983 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001984 }
Evan Chengf26ffe92008-05-29 08:22:04 +00001985 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001986 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001987 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00001988 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00001989 return V.getOperand(Index);
1990 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
1991 return getShuffleScalarElt(V.Val, Index);
Dan Gohman475871a2008-07-27 21:46:04 +00001992 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00001993}
1994
1995
Chris Lattnerc3aae252005-01-07 07:46:32 +00001996/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001997///
Dan Gohman475871a2008-07-27 21:46:04 +00001998SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001999 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002000 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002001 void *IP = 0;
2002 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002003 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002004 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002005 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002006 CSEMap.InsertNode(N, IP);
2007
2008 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002009#ifndef NDEBUG
2010 VerifyNode(N);
2011#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002012 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002013}
2014
Dan Gohman475871a2008-07-27 21:46:04 +00002015SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002016 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002017 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
Dan Gohman6c231502008-02-29 01:47:35 +00002018 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002019 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002020 switch (Opcode) {
2021 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002022 case ISD::SIGN_EXTEND:
2023 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002024 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002025 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002026 case ISD::TRUNCATE:
2027 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002028 case ISD::UINT_TO_FP:
2029 case ISD::SINT_TO_FP: {
2030 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002031 // No compile time operations on this type.
2032 if (VT==MVT::ppcf128)
2033 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002034 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2035 (void)apf.convertFromAPInt(Val,
2036 Opcode==ISD::SINT_TO_FP,
2037 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002038 return getConstantFP(apf, VT);
2039 }
Chris Lattner94683772005-12-23 05:30:37 +00002040 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002041 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002042 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002043 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002044 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002045 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002046 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002047 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002048 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002049 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002050 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002051 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002052 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002053 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002054 }
2055 }
2056
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002057 // Constant fold unary operations with a floating point constant operand.
2058 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
2059 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002060 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002061 switch (Opcode) {
2062 case ISD::FNEG:
2063 V.changeSign();
2064 return getConstantFP(V, VT);
2065 case ISD::FABS:
2066 V.clearSign();
2067 return getConstantFP(V, VT);
2068 case ISD::FP_ROUND:
2069 case ISD::FP_EXTEND:
2070 // This can return overflow, underflow, or inexact; we don't care.
2071 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002072 (void)V.convert(*MVTToAPFloatSemantics(VT),
2073 APFloat::rmNearestTiesToEven);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002074 return getConstantFP(V, VT);
2075 case ISD::FP_TO_SINT:
2076 case ISD::FP_TO_UINT: {
2077 integerPart x;
2078 assert(integerPartWidth >= 64);
2079 // FIXME need to be more flexible about rounding mode.
2080 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2081 Opcode==ISD::FP_TO_SINT,
2082 APFloat::rmTowardZero);
2083 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2084 break;
2085 return getConstant(x, VT);
2086 }
2087 case ISD::BIT_CONVERT:
2088 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2089 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
2090 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2091 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002092 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002093 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002094 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002095 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002096
2097 unsigned OpOpcode = Operand.Val->getOpcode();
2098 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002099 case ISD::TokenFactor:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002100 case ISD::CONCAT_VECTORS:
2101 return Operand; // Factor or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002102 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002103 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002104 assert(VT.isFloatingPoint() &&
2105 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002106 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002107 if (Operand.getOpcode() == ISD::UNDEF)
2108 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002109 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002110 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002111 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002112 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002113 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002114 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002115 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002116 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2117 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2118 break;
2119 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002120 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002121 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002122 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002123 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002124 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002125 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00002126 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002127 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002128 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002129 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002130 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002131 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002132 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002133 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002134 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2135 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
2136 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
2137 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002138 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002139 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002140 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002141 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002142 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002143 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002144 if (OpOpcode == ISD::TRUNCATE)
2145 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002146 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2147 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002148 // If the source is smaller than the dest, we still need an extend.
Duncan Sands8e4eb092008-06-08 20:54:56 +00002149 if (Operand.Val->getOperand(0).getValueType().bitsLT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002150 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00002151 else if (Operand.Val->getOperand(0).getValueType().bitsGT(VT))
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002152 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
2153 else
2154 return Operand.Val->getOperand(0);
2155 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002156 break;
Chris Lattner35481892005-12-23 00:16:34 +00002157 case ISD::BIT_CONVERT:
2158 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002159 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002160 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002161 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002162 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2163 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002164 if (OpOpcode == ISD::UNDEF)
2165 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002166 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002167 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002168 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2169 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002170 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002171 if (OpOpcode == ISD::UNDEF)
2172 return getNode(ISD::UNDEF, VT);
2173 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2174 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2175 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2176 Operand.getConstantOperandVal(1) == 0 &&
2177 Operand.getOperand(0).getValueType() == VT)
2178 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002179 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002180 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002181 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
2182 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00002183 Operand.Val->getOperand(0));
2184 if (OpOpcode == ISD::FNEG) // --X -> X
2185 return Operand.Val->getOperand(0);
2186 break;
2187 case ISD::FABS:
2188 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
2189 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
2190 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002191 }
2192
Chris Lattner43247a12005-08-25 19:12:10 +00002193 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002194 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002195 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002196 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002197 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002198 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002199 void *IP = 0;
2200 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002201 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002202 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002203 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002204 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002205 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002206 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002207 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002208 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002209
Chris Lattnerc3aae252005-01-07 07:46:32 +00002210 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002211#ifndef NDEBUG
2212 VerifyNode(N);
2213#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002214 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002215}
2216
Dan Gohman475871a2008-07-27 21:46:04 +00002217SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2218 SDValue N1, SDValue N2) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002219 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2220 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattner76365122005-01-16 02:23:22 +00002221 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002222 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002223 case ISD::TokenFactor:
2224 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2225 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002226 // Fold trivial token factors.
2227 if (N1.getOpcode() == ISD::EntryToken) return N2;
2228 if (N2.getOpcode() == ISD::EntryToken) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002229 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002230 case ISD::CONCAT_VECTORS:
2231 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2232 // one big BUILD_VECTOR.
2233 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2234 N2.getOpcode() == ISD::BUILD_VECTOR) {
2235 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2236 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2237 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2238 }
2239 break;
Chris Lattner76365122005-01-16 02:23:22 +00002240 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002241 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002242 N1.getValueType() == VT && "Binary operator types must match!");
2243 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2244 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002245 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002246 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002247 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2248 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002249 break;
Chris Lattner76365122005-01-16 02:23:22 +00002250 case ISD::OR:
2251 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002252 case ISD::ADD:
2253 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002254 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002255 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002256 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2257 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002258 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002259 return N1;
2260 break;
Chris Lattner76365122005-01-16 02:23:22 +00002261 case ISD::UDIV:
2262 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002263 case ISD::MULHU:
2264 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002265 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002266 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002267 case ISD::MUL:
2268 case ISD::SDIV:
2269 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002270 case ISD::FADD:
2271 case ISD::FSUB:
2272 case ISD::FMUL:
2273 case ISD::FDIV:
2274 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002275 assert(N1.getValueType() == N2.getValueType() &&
2276 N1.getValueType() == VT && "Binary operator types must match!");
2277 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002278 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2279 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002280 N1.getValueType().isFloatingPoint() &&
2281 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002282 "Invalid FCOPYSIGN!");
2283 break;
Chris Lattner76365122005-01-16 02:23:22 +00002284 case ISD::SHL:
2285 case ISD::SRA:
2286 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002287 case ISD::ROTL:
2288 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002289 assert(VT == N1.getValueType() &&
2290 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002291 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002292 "Shifts only work on integers");
2293
2294 // Always fold shifts of i1 values so the code generator doesn't need to
2295 // handle them. Since we know the size of the shift has to be less than the
2296 // size of the value, the shift/rotate count is guaranteed to be zero.
2297 if (VT == MVT::i1)
2298 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002299 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002300 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002301 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002302 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002303 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002304 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002305 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002306 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002307 break;
2308 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002309 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002310 assert(VT.isFloatingPoint() &&
2311 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002312 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002313 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002314 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002315 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002316 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002317 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002318 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002319 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002320 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002321 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002322 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002323 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002324 break;
2325 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002326 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002327 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002328 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002329 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002330 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002331 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002332 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002333
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002334 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002335 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002336 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002337 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002338 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002339 return getConstant(Val, VT);
2340 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002341 break;
2342 }
2343 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002344 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2345 if (N1.getOpcode() == ISD::UNDEF)
2346 return getNode(ISD::UNDEF, VT);
2347
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002348 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2349 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002350 if (N2C &&
2351 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002352 N1.getNumOperands() > 0) {
2353 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002354 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002355 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2356 N1.getOperand(N2C->getValue() / Factor),
2357 getConstant(N2C->getValue() % Factor, N2.getValueType()));
2358 }
2359
2360 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2361 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002362 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002363 return N1.getOperand(N2C->getValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002364
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002365 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2366 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002367 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2368 if (N1.getOperand(2) == N2)
2369 return N1.getOperand(1);
2370 else
2371 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2372 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002373 break;
2374 case ISD::EXTRACT_ELEMENT:
2375 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002376 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2377 (N1.getValueType().isInteger() == VT.isInteger()) &&
2378 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002379
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002380 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2381 // 64-bit integers into 32-bit parts. Instead of building the extract of
2382 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2383 if (N1.getOpcode() == ISD::BUILD_PAIR)
2384 return N1.getOperand(N2C->getValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002385
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002386 // EXTRACT_ELEMENT of a constant int is also very common.
2387 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002388 unsigned ElementSize = VT.getSizeInBits();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002389 unsigned Shift = ElementSize * N2C->getValue();
2390 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2391 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002392 }
2393 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002394 case ISD::EXTRACT_SUBVECTOR:
2395 if (N1.getValueType() == VT) // Trivial extraction.
2396 return N1;
2397 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002398 }
2399
2400 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002401 if (N2C) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002402 const APInt &C1 = N1C->getAPIntValue(), &C2 = N2C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002403 switch (Opcode) {
2404 case ISD::ADD: return getConstant(C1 + C2, VT);
2405 case ISD::SUB: return getConstant(C1 - C2, VT);
2406 case ISD::MUL: return getConstant(C1 * C2, VT);
2407 case ISD::UDIV:
Dan Gohman6c231502008-02-29 01:47:35 +00002408 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002409 break;
2410 case ISD::UREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002411 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002412 break;
2413 case ISD::SDIV :
Dan Gohman6c231502008-02-29 01:47:35 +00002414 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002415 break;
2416 case ISD::SREM :
Dan Gohman6c231502008-02-29 01:47:35 +00002417 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002418 break;
2419 case ISD::AND : return getConstant(C1 & C2, VT);
2420 case ISD::OR : return getConstant(C1 | C2, VT);
2421 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00002422 case ISD::SHL : return getConstant(C1 << C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00002423 case ISD::SRL : return getConstant(C1.lshr(C2), VT);
2424 case ISD::SRA : return getConstant(C1.ashr(C2), VT);
2425 case ISD::ROTL : return getConstant(C1.rotl(C2), VT);
2426 case ISD::ROTR : return getConstant(C1.rotr(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002427 default: break;
2428 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002429 } else { // Cannonicalize constant to RHS if commutative
2430 if (isCommutativeBinOp(Opcode)) {
2431 std::swap(N1C, N2C);
2432 std::swap(N1, N2);
2433 }
2434 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002435 }
2436
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002437 // Constant fold FP operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002438 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
2439 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00002440 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002441 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2442 // Cannonicalize constant to RHS if commutative
2443 std::swap(N1CFP, N2CFP);
2444 std::swap(N1, N2);
2445 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002446 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2447 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002448 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002449 case ISD::FADD:
2450 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002451 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002452 return getConstantFP(V1, VT);
2453 break;
2454 case ISD::FSUB:
2455 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2456 if (s!=APFloat::opInvalidOp)
2457 return getConstantFP(V1, VT);
2458 break;
2459 case ISD::FMUL:
2460 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2461 if (s!=APFloat::opInvalidOp)
2462 return getConstantFP(V1, VT);
2463 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002464 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002465 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2466 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2467 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002468 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002469 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002470 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2471 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2472 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002473 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002474 case ISD::FCOPYSIGN:
2475 V1.copySign(V2);
2476 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002477 default: break;
2478 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002479 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002480 }
Chris Lattner62b57722006-04-20 05:39:12 +00002481
2482 // Canonicalize an UNDEF to the RHS, even over a constant.
2483 if (N1.getOpcode() == ISD::UNDEF) {
2484 if (isCommutativeBinOp(Opcode)) {
2485 std::swap(N1, N2);
2486 } else {
2487 switch (Opcode) {
2488 case ISD::FP_ROUND_INREG:
2489 case ISD::SIGN_EXTEND_INREG:
2490 case ISD::SUB:
2491 case ISD::FSUB:
2492 case ISD::FDIV:
2493 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002494 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002495 return N1; // fold op(undef, arg2) -> undef
2496 case ISD::UDIV:
2497 case ISD::SDIV:
2498 case ISD::UREM:
2499 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002500 case ISD::SRL:
2501 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002502 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002503 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2504 // For vectors, we can't easily build an all zero vector, just return
2505 // the LHS.
2506 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002507 }
2508 }
2509 }
2510
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002511 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002512 if (N2.getOpcode() == ISD::UNDEF) {
2513 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002514 case ISD::XOR:
2515 if (N1.getOpcode() == ISD::UNDEF)
2516 // Handle undef ^ undef -> 0 special case. This is a common
2517 // idiom (misuse).
2518 return getConstant(0, VT);
2519 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002520 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002521 case ISD::ADDC:
2522 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002523 case ISD::SUB:
2524 case ISD::FADD:
2525 case ISD::FSUB:
2526 case ISD::FMUL:
2527 case ISD::FDIV:
2528 case ISD::FREM:
2529 case ISD::UDIV:
2530 case ISD::SDIV:
2531 case ISD::UREM:
2532 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002533 return N2; // fold op(arg1, undef) -> undef
2534 case ISD::MUL:
2535 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002536 case ISD::SRL:
2537 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002538 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002539 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2540 // For vectors, we can't easily build an all zero vector, just return
2541 // the LHS.
2542 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002543 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 if (!VT.isVector())
2545 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002546 // For vectors, we can't easily build an all one vector, just return
2547 // the LHS.
2548 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002549 case ISD::SRA:
2550 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002551 }
2552 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002553
Chris Lattner27e9b412005-05-11 18:57:39 +00002554 // Memoize this node if possible.
2555 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002556 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002557 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002558 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002559 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002560 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002561 void *IP = 0;
2562 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002563 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002564 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002565 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002566 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002567 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002568 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002569 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002570 }
2571
Chris Lattnerc3aae252005-01-07 07:46:32 +00002572 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002573#ifndef NDEBUG
2574 VerifyNode(N);
2575#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002576 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002577}
2578
Dan Gohman475871a2008-07-27 21:46:04 +00002579SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2580 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002581 // Perform various simplifications.
2582 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2583 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002584 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002585 case ISD::CONCAT_VECTORS:
2586 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2587 // one big BUILD_VECTOR.
2588 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2589 N2.getOpcode() == ISD::BUILD_VECTOR &&
2590 N3.getOpcode() == ISD::BUILD_VECTOR) {
2591 SmallVector<SDValue, 16> Elts(N1.Val->op_begin(), N1.Val->op_end());
2592 Elts.insert(Elts.end(), N2.Val->op_begin(), N2.Val->op_end());
2593 Elts.insert(Elts.end(), N3.Val->op_begin(), N3.Val->op_end());
2594 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2595 }
2596 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002597 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002598 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002599 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002600 if (Simp.Val) return Simp;
2601 break;
2602 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002603 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002604 if (N1C) {
2605 if (N1C->getValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002606 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002607 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002608 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002609 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002610
2611 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002612 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002613 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002614 if (N2C) {
Chris Lattner5351e9b2005-01-07 22:49:57 +00002615 if (N2C->getValue()) // Unconditional branch
2616 return getNode(ISD::BR, MVT::Other, N1, N3);
2617 else
2618 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002619 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002620 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002621 case ISD::VECTOR_SHUFFLE:
2622 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002624 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002625 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002626 "Illegal VECTOR_SHUFFLE node!");
2627 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002628 case ISD::BIT_CONVERT:
2629 // Fold bit_convert nodes from a type to themselves.
2630 if (N1.getValueType() == VT)
2631 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002632 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002633 }
2634
Chris Lattner43247a12005-08-25 19:12:10 +00002635 // Memoize node if it doesn't produce a flag.
2636 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002637 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002638 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002639 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002640 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002641 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002642 void *IP = 0;
2643 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002644 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002645 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002646 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002647 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002648 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002649 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002650 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002651 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002652 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002653#ifndef NDEBUG
2654 VerifyNode(N);
2655#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002656 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002657}
2658
Dan Gohman475871a2008-07-27 21:46:04 +00002659SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2660 SDValue N1, SDValue N2, SDValue N3,
2661 SDValue N4) {
2662 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002663 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002664}
2665
Dan Gohman475871a2008-07-27 21:46:04 +00002666SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2667 SDValue N1, SDValue N2, SDValue N3,
2668 SDValue N4, SDValue N5) {
2669 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002670 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002671}
2672
Dan Gohman707e0182008-04-12 04:36:06 +00002673/// getMemsetValue - Vectorized representation of the memset value
2674/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002675static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002676 unsigned NumBits = VT.isVector() ?
2677 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002678 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Evan Chengf0df0312008-05-15 08:39:06 +00002679 APInt Val = APInt(NumBits, C->getValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002680 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002681 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002682 Val = (Val << Shift) | Val;
2683 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002684 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002685 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002686 return DAG.getConstant(Val, VT);
2687 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002688 }
Evan Chengf0df0312008-05-15 08:39:06 +00002689
2690 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2691 unsigned Shift = 8;
2692 for (unsigned i = NumBits; i > 8; i >>= 1) {
2693 Value = DAG.getNode(ISD::OR, VT,
2694 DAG.getNode(ISD::SHL, VT, Value,
2695 DAG.getConstant(Shift, MVT::i8)), Value);
2696 Shift <<= 1;
2697 }
2698
2699 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002700}
2701
Dan Gohman707e0182008-04-12 04:36:06 +00002702/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2703/// used when a memcpy is turned into a memset when the source is a constant
2704/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002705static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002706 const TargetLowering &TLI,
2707 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002708 // Handle vector with all elements zero.
2709 if (Str.empty()) {
2710 if (VT.isInteger())
2711 return DAG.getConstant(0, VT);
2712 unsigned NumElts = VT.getVectorNumElements();
2713 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2714 return DAG.getNode(ISD::BIT_CONVERT, VT,
2715 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2716 }
2717
Duncan Sands83ec4b62008-06-06 12:08:01 +00002718 assert(!VT.isVector() && "Can't handle vector type here!");
2719 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002720 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002721 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002722 if (TLI.isLittleEndian())
2723 Offset = Offset + MSB - 1;
2724 for (unsigned i = 0; i != MSB; ++i) {
2725 Val = (Val << 8) | (unsigned char)Str[Offset];
2726 Offset += TLI.isLittleEndian() ? -1 : 1;
2727 }
2728 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002729}
2730
Dan Gohman707e0182008-04-12 04:36:06 +00002731/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002732///
Dan Gohman475871a2008-07-27 21:46:04 +00002733static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002734 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002735 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002736 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2737}
2738
Evan Chengf0df0312008-05-15 08:39:06 +00002739/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2740///
Dan Gohman475871a2008-07-27 21:46:04 +00002741static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002742 unsigned SrcDelta = 0;
2743 GlobalAddressSDNode *G = NULL;
2744 if (Src.getOpcode() == ISD::GlobalAddress)
2745 G = cast<GlobalAddressSDNode>(Src);
2746 else if (Src.getOpcode() == ISD::ADD &&
2747 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2748 Src.getOperand(1).getOpcode() == ISD::Constant) {
2749 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
2750 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getValue();
2751 }
2752 if (!G)
2753 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002754
Evan Chengf0df0312008-05-15 08:39:06 +00002755 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002756 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2757 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002758
Evan Chengf0df0312008-05-15 08:39:06 +00002759 return false;
2760}
Dan Gohman707e0182008-04-12 04:36:06 +00002761
Evan Chengf0df0312008-05-15 08:39:06 +00002762/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2763/// to replace the memset / memcpy is below the threshold. It also returns the
2764/// types of the sequence of memory ops to perform memset / memcpy.
2765static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002766bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002767 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002768 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002769 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002770 SelectionDAG &DAG,
2771 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002772 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002773 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002774 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002775 MVT VT= TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002776 if (VT != MVT::iAny) {
2777 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002778 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002779 // If source is a string constant, this will require an unaligned load.
2780 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2781 if (Dst.getOpcode() != ISD::FrameIndex) {
2782 // Can't change destination alignment. It requires a unaligned store.
2783 if (AllowUnalign)
2784 VT = MVT::iAny;
2785 } else {
2786 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2787 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2788 if (MFI->isFixedObjectIndex(FI)) {
2789 // Can't change destination alignment. It requires a unaligned store.
2790 if (AllowUnalign)
2791 VT = MVT::iAny;
2792 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002793 // Give the stack frame object a larger alignment if needed.
2794 if (MFI->getObjectAlignment(FI) < NewAlign)
2795 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002796 Align = NewAlign;
2797 }
2798 }
2799 }
2800 }
2801
2802 if (VT == MVT::iAny) {
2803 if (AllowUnalign) {
2804 VT = MVT::i64;
2805 } else {
2806 switch (Align & 7) {
2807 case 0: VT = MVT::i64; break;
2808 case 4: VT = MVT::i32; break;
2809 case 2: VT = MVT::i16; break;
2810 default: VT = MVT::i8; break;
2811 }
2812 }
2813
Duncan Sands83ec4b62008-06-06 12:08:01 +00002814 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002815 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002816 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2817 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002818
Duncan Sands8e4eb092008-06-08 20:54:56 +00002819 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002820 VT = LVT;
2821 }
Dan Gohman707e0182008-04-12 04:36:06 +00002822
2823 unsigned NumMemOps = 0;
2824 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002825 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002826 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002827 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002828 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002829 VT = MVT::i64;
2830 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002831 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2832 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002833 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002834 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002835 VTSize >>= 1;
2836 }
Dan Gohman707e0182008-04-12 04:36:06 +00002837 }
Dan Gohman707e0182008-04-12 04:36:06 +00002838
2839 if (++NumMemOps > Limit)
2840 return false;
2841 MemOps.push_back(VT);
2842 Size -= VTSize;
2843 }
2844
2845 return true;
2846}
2847
Dan Gohman475871a2008-07-27 21:46:04 +00002848static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2849 SDValue Chain, SDValue Dst,
2850 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002851 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002852 const Value *DstSV, uint64_t DstSVOff,
2853 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002854 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2855
Dan Gohman21323f32008-05-29 19:42:22 +00002856 // Expand memcpy to a series of load and store ops if the size operand falls
2857 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002858 std::vector<MVT> MemOps;
Dan Gohman707e0182008-04-12 04:36:06 +00002859 uint64_t Limit = -1;
2860 if (!AlwaysInline)
2861 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002862 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002863 std::string Str;
2864 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002865 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002866 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002867 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002868
Dan Gohman707e0182008-04-12 04:36:06 +00002869
Evan Cheng0ff39b32008-06-30 07:31:25 +00002870 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002871 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002872 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002873 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002874 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002875 MVT VT = MemOps[i];
2876 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002877 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002878
Evan Cheng0ff39b32008-06-30 07:31:25 +00002879 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002880 // It's unlikely a store of a vector immediate can be done in a single
2881 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002882 // We also handle store a vector with all zero's.
2883 // FIXME: Handle other cases where store of vector immediate is done in
2884 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002885 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002886 Store = DAG.getStore(Chain, Value,
2887 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002888 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002889 } else {
2890 Value = DAG.getLoad(VT, Chain,
2891 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002892 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002893 Store = DAG.getStore(Chain, Value,
2894 getMemBasePlusOffset(Dst, DstOff, DAG),
2895 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002896 }
2897 OutChains.push_back(Store);
2898 SrcOff += VTSize;
2899 DstOff += VTSize;
2900 }
2901
2902 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2903 &OutChains[0], OutChains.size());
2904}
2905
Dan Gohman475871a2008-07-27 21:46:04 +00002906static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
2907 SDValue Chain, SDValue Dst,
2908 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00002909 unsigned Align, bool AlwaysInline,
2910 const Value *DstSV, uint64_t DstSVOff,
2911 const Value *SrcSV, uint64_t SrcSVOff){
2912 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2913
2914 // Expand memmove to a series of load and store ops if the size operand falls
2915 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002916 std::vector<MVT> MemOps;
Dan Gohman21323f32008-05-29 19:42:22 +00002917 uint64_t Limit = -1;
2918 if (!AlwaysInline)
2919 Limit = TLI.getMaxStoresPerMemmove();
2920 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002921 std::string Str;
2922 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00002923 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002924 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002925 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00002926
Dan Gohman21323f32008-05-29 19:42:22 +00002927 uint64_t SrcOff = 0, DstOff = 0;
2928
Dan Gohman475871a2008-07-27 21:46:04 +00002929 SmallVector<SDValue, 8> LoadValues;
2930 SmallVector<SDValue, 8> LoadChains;
2931 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00002932 unsigned NumMemOps = MemOps.size();
2933 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002934 MVT VT = MemOps[i];
2935 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002936 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002937
2938 Value = DAG.getLoad(VT, Chain,
2939 getMemBasePlusOffset(Src, SrcOff, DAG),
2940 SrcSV, SrcSVOff + SrcOff, false, Align);
2941 LoadValues.push_back(Value);
2942 LoadChains.push_back(Value.getValue(1));
2943 SrcOff += VTSize;
2944 }
2945 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2946 &LoadChains[0], LoadChains.size());
2947 OutChains.clear();
2948 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002949 MVT VT = MemOps[i];
2950 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002951 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00002952
2953 Store = DAG.getStore(Chain, LoadValues[i],
2954 getMemBasePlusOffset(Dst, DstOff, DAG),
2955 DstSV, DstSVOff + DstOff, false, DstAlign);
2956 OutChains.push_back(Store);
2957 DstOff += VTSize;
2958 }
2959
2960 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2961 &OutChains[0], OutChains.size());
2962}
2963
Dan Gohman475871a2008-07-27 21:46:04 +00002964static SDValue getMemsetStores(SelectionDAG &DAG,
2965 SDValue Chain, SDValue Dst,
2966 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00002967 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00002968 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00002969 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2970
2971 // Expand memset to a series of load/store ops if the size operand
2972 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002973 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00002974 std::string Str;
2975 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002976 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00002977 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002978 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002979
Dan Gohman475871a2008-07-27 21:46:04 +00002980 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00002981 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002982
2983 unsigned NumMemOps = MemOps.size();
2984 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002985 MVT VT = MemOps[i];
2986 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002987 SDValue Value = getMemsetValue(Src, VT, DAG);
2988 SDValue Store = DAG.getStore(Chain, Value,
Dan Gohman707e0182008-04-12 04:36:06 +00002989 getMemBasePlusOffset(Dst, DstOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002990 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00002991 OutChains.push_back(Store);
2992 DstOff += VTSize;
2993 }
2994
2995 return DAG.getNode(ISD::TokenFactor, MVT::Other,
2996 &OutChains[0], OutChains.size());
2997}
2998
Dan Gohman475871a2008-07-27 21:46:04 +00002999SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3000 SDValue Src, SDValue Size,
3001 unsigned Align, bool AlwaysInline,
3002 const Value *DstSV, uint64_t DstSVOff,
3003 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003004
3005 // Check to see if we should lower the memcpy to loads and stores first.
3006 // For cases within the target-specified limits, this is the best choice.
3007 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3008 if (ConstantSize) {
3009 // Memcpy with size zero? Just return the original chain.
3010 if (ConstantSize->isNullValue())
3011 return Chain;
3012
Dan Gohman475871a2008-07-27 21:46:04 +00003013 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003014 getMemcpyLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003015 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003016 if (Result.Val)
3017 return Result;
3018 }
3019
3020 // Then check to see if we should lower the memcpy with target-specific
3021 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003022 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003023 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3024 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003025 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003026 if (Result.Val)
3027 return Result;
3028
3029 // If we really need inline code and the target declined to provide it,
3030 // use a (potentially long) sequence of loads and stores.
3031 if (AlwaysInline) {
3032 assert(ConstantSize && "AlwaysInline requires a constant size!");
3033 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3034 ConstantSize->getValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003035 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003036 }
3037
3038 // Emit a library call.
3039 TargetLowering::ArgListTy Args;
3040 TargetLowering::ArgListEntry Entry;
3041 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3042 Entry.Node = Dst; Args.push_back(Entry);
3043 Entry.Node = Src; Args.push_back(Entry);
3044 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003045 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003046 TLI.LowerCallTo(Chain, Type::VoidTy,
3047 false, false, false, CallingConv::C, false,
3048 getExternalSymbol("memcpy", TLI.getPointerTy()),
3049 Args, *this);
3050 return CallResult.second;
3051}
3052
Dan Gohman475871a2008-07-27 21:46:04 +00003053SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3054 SDValue Src, SDValue Size,
3055 unsigned Align,
3056 const Value *DstSV, uint64_t DstSVOff,
3057 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003058
Dan Gohman21323f32008-05-29 19:42:22 +00003059 // Check to see if we should lower the memmove to loads and stores first.
3060 // For cases within the target-specified limits, this is the best choice.
3061 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3062 if (ConstantSize) {
3063 // Memmove with size zero? Just return the original chain.
3064 if (ConstantSize->isNullValue())
3065 return Chain;
3066
Dan Gohman475871a2008-07-27 21:46:04 +00003067 SDValue Result =
Dan Gohman21323f32008-05-29 19:42:22 +00003068 getMemmoveLoadsAndStores(*this, Chain, Dst, Src, ConstantSize->getValue(),
3069 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
3070 if (Result.Val)
3071 return Result;
3072 }
Dan Gohman707e0182008-04-12 04:36:06 +00003073
3074 // Then check to see if we should lower the memmove with target-specific
3075 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003076 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003077 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003078 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003079 if (Result.Val)
3080 return Result;
3081
3082 // Emit a library call.
3083 TargetLowering::ArgListTy Args;
3084 TargetLowering::ArgListEntry Entry;
3085 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3086 Entry.Node = Dst; Args.push_back(Entry);
3087 Entry.Node = Src; Args.push_back(Entry);
3088 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003089 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003090 TLI.LowerCallTo(Chain, Type::VoidTy,
3091 false, false, false, CallingConv::C, false,
3092 getExternalSymbol("memmove", TLI.getPointerTy()),
3093 Args, *this);
3094 return CallResult.second;
3095}
3096
Dan Gohman475871a2008-07-27 21:46:04 +00003097SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3098 SDValue Src, SDValue Size,
3099 unsigned Align,
3100 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003101
3102 // Check to see if we should lower the memset to stores first.
3103 // For cases within the target-specified limits, this is the best choice.
3104 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3105 if (ConstantSize) {
3106 // Memset with size zero? Just return the original chain.
3107 if (ConstantSize->isNullValue())
3108 return Chain;
3109
Dan Gohman475871a2008-07-27 21:46:04 +00003110 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003111 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getValue(), Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003112 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003113 if (Result.Val)
3114 return Result;
3115 }
3116
3117 // Then check to see if we should lower the memset with target-specific
3118 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003119 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003120 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003121 DstSV, DstSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003122 if (Result.Val)
3123 return Result;
3124
3125 // Emit a library call.
3126 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3127 TargetLowering::ArgListTy Args;
3128 TargetLowering::ArgListEntry Entry;
3129 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3130 Args.push_back(Entry);
3131 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003132 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003133 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3134 else
3135 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3136 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3137 Args.push_back(Entry);
3138 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3139 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003140 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003141 TLI.LowerCallTo(Chain, Type::VoidTy,
3142 false, false, false, CallingConv::C, false,
3143 getExternalSymbol("memset", TLI.getPointerTy()),
3144 Args, *this);
3145 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003146}
3147
Dan Gohman475871a2008-07-27 21:46:04 +00003148SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3149 SDValue Ptr, SDValue Cmp,
3150 SDValue Swp, const Value* PtrVal,
3151 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003152 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003153 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003154
3155 MVT VT = Cmp.getValueType();
3156
3157 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3158 Alignment = getMVTAlignment(VT);
3159
3160 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003161 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003162 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003163 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003164 void* IP = 0;
3165 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003166 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003167 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003168 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003169 CSEMap.InsertNode(N, IP);
3170 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003171 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003172}
3173
Dan Gohman475871a2008-07-27 21:46:04 +00003174SDValue SelectionDAG::getAtomic(unsigned Opcode, SDValue Chain,
3175 SDValue Ptr, SDValue Val,
3176 const Value* PtrVal,
3177 unsigned Alignment) {
Mon P Wang28873102008-06-25 08:15:39 +00003178 assert(( Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB
Mon P Wang63307c32008-05-05 19:05:59 +00003179 || Opcode == ISD::ATOMIC_SWAP || Opcode == ISD::ATOMIC_LOAD_AND
3180 || Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR
Andrew Lenharth507a58a2008-06-14 05:48:15 +00003181 || Opcode == ISD::ATOMIC_LOAD_NAND
Mon P Wang63307c32008-05-05 19:05:59 +00003182 || Opcode == ISD::ATOMIC_LOAD_MIN || Opcode == ISD::ATOMIC_LOAD_MAX
3183 || Opcode == ISD::ATOMIC_LOAD_UMIN || Opcode == ISD::ATOMIC_LOAD_UMAX)
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003184 && "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003185
3186 MVT VT = Val.getValueType();
3187
3188 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3189 Alignment = getMVTAlignment(VT);
3190
3191 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003192 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003193 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003194 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003195 void* IP = 0;
3196 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003197 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003198 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003199 new (N) AtomicSDNode(Opcode, VTs, Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003200 CSEMap.InsertNode(N, IP);
3201 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003202 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003203}
3204
Duncan Sands4bdcb612008-07-02 17:40:58 +00003205/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3206/// Allowed to return something different (and simpler) if Simplify is true.
Dan Gohman475871a2008-07-27 21:46:04 +00003207SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3208 bool Simplify) {
Duncan Sands4bdcb612008-07-02 17:40:58 +00003209 if (Simplify && NumOps == 1)
3210 return Ops[0];
3211
3212 SmallVector<MVT, 4> VTs;
3213 VTs.reserve(NumOps);
3214 for (unsigned i = 0; i < NumOps; ++i)
3215 VTs.push_back(Ops[i].getValueType());
3216 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3217}
3218
Dan Gohman475871a2008-07-27 21:46:04 +00003219SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003220SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003221 MVT VT, SDValue Chain,
3222 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003223 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003224 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003225 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3226 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003227
Duncan Sands14ea39c2008-03-27 20:23:40 +00003228 if (VT == EVT) {
3229 ExtType = ISD::NON_EXTLOAD;
3230 } else if (ExtType == ISD::NON_EXTLOAD) {
3231 assert(VT == EVT && "Non-extending load from different memory type!");
3232 } else {
3233 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003234 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003235 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3236 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003237 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003238 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003239 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003240 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003241 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003242 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003243 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003244 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003245
3246 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003247 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003248 "Unindexed load with an offset!");
3249
3250 SDVTList VTs = Indexed ?
3251 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003252 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003253 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003254 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003255 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003256 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003257 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003258 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003259 void *IP = 0;
3260 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003261 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003262 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003263 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3264 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003265 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003266 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003267 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003268}
3269
Dan Gohman475871a2008-07-27 21:46:04 +00003270SDValue SelectionDAG::getLoad(MVT VT,
3271 SDValue Chain, SDValue Ptr,
3272 const Value *SV, int SVOffset,
3273 bool isVolatile, unsigned Alignment) {
3274 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003275 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3276 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003277}
3278
Dan Gohman475871a2008-07-27 21:46:04 +00003279SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3280 SDValue Chain, SDValue Ptr,
3281 const Value *SV,
3282 int SVOffset, MVT EVT,
3283 bool isVolatile, unsigned Alignment) {
3284 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003285 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3286 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003287}
3288
Dan Gohman475871a2008-07-27 21:46:04 +00003289SDValue
3290SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3291 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003292 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003293 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3294 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003295 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3296 LD->getChain(), Base, Offset, LD->getSrcValue(),
3297 LD->getSrcValueOffset(), LD->getMemoryVT(),
3298 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003299}
3300
Dan Gohman475871a2008-07-27 21:46:04 +00003301SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3302 SDValue Ptr, const Value *SV, int SVOffset,
3303 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003304 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003305
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003306 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3307 Alignment = getMVTAlignment(VT);
3308
Evan Chengad071e12006-10-05 22:57:11 +00003309 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003310 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3311 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003312 FoldingSetNodeID ID;
3313 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003314 ID.AddInteger(ISD::UNINDEXED);
3315 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003316 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003317 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003318 void *IP = 0;
3319 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003320 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003321 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003322 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3323 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003324 CSEMap.InsertNode(N, IP);
3325 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003326 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003327}
3328
Dan Gohman475871a2008-07-27 21:46:04 +00003329SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3330 SDValue Ptr, const Value *SV,
3331 int SVOffset, MVT SVT,
3332 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003333 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003334
3335 if (VT == SVT)
3336 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003337
Duncan Sands8e4eb092008-06-08 20:54:56 +00003338 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003339 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003340 "Can't do FP-INT conversion!");
3341
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003342 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3343 Alignment = getMVTAlignment(VT);
3344
Evan Cheng8b2794a2006-10-13 21:14:26 +00003345 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003346 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3347 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003348 FoldingSetNodeID ID;
3349 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003350 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003351 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003352 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003353 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003354 void *IP = 0;
3355 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003356 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003357 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003358 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3359 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003360 CSEMap.InsertNode(N, IP);
3361 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003362 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003363}
3364
Dan Gohman475871a2008-07-27 21:46:04 +00003365SDValue
3366SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3367 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003368 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3369 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3370 "Store is already a indexed store!");
3371 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003372 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003373 FoldingSetNodeID ID;
3374 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3375 ID.AddInteger(AM);
3376 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003377 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003378 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003379 void *IP = 0;
3380 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003381 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003382 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003383 new (N) StoreSDNode(Ops, VTs, AM,
3384 ST->isTruncatingStore(), ST->getMemoryVT(),
3385 ST->getSrcValue(), ST->getSrcValueOffset(),
3386 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003387 CSEMap.InsertNode(N, IP);
3388 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003389 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003390}
3391
Dan Gohman475871a2008-07-27 21:46:04 +00003392SDValue SelectionDAG::getVAArg(MVT VT,
3393 SDValue Chain, SDValue Ptr,
3394 SDValue SV) {
3395 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003396 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003397}
3398
Dan Gohman475871a2008-07-27 21:46:04 +00003399SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3400 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003401 switch (NumOps) {
3402 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003403 case 1: return getNode(Opcode, VT, Ops[0]);
3404 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3405 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003406 default: break;
3407 }
3408
Dan Gohman475871a2008-07-27 21:46:04 +00003409 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003410 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003411 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3412 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003413}
3414
Dan Gohman475871a2008-07-27 21:46:04 +00003415SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3416 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003417 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003418 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003419 case 1: return getNode(Opcode, VT, Ops[0]);
3420 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3421 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003422 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003423 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003424
Chris Lattneref847df2005-04-09 03:27:28 +00003425 switch (Opcode) {
3426 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003427 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003428 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003429 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3430 "LHS and RHS of condition must have same type!");
3431 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3432 "True and False arms of SelectCC must have same type!");
3433 assert(Ops[2].getValueType() == VT &&
3434 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003435 break;
3436 }
3437 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003438 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003439 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3440 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003441 break;
3442 }
Chris Lattneref847df2005-04-09 03:27:28 +00003443 }
3444
Chris Lattner385328c2005-05-14 07:42:29 +00003445 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003446 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003447 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003448 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003449 FoldingSetNodeID ID;
3450 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003451 void *IP = 0;
3452 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003453 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003454 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003455 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003456 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003457 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003458 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003459 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003460 }
Chris Lattneref847df2005-04-09 03:27:28 +00003461 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003462#ifndef NDEBUG
3463 VerifyNode(N);
3464#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003465 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003466}
3467
Dan Gohman475871a2008-07-27 21:46:04 +00003468SDValue SelectionDAG::getNode(unsigned Opcode,
3469 const std::vector<MVT> &ResultTys,
3470 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003471 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3472 Ops, NumOps);
3473}
3474
Dan Gohman475871a2008-07-27 21:46:04 +00003475SDValue SelectionDAG::getNode(unsigned Opcode,
3476 const MVT *VTs, unsigned NumVTs,
3477 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003478 if (NumVTs == 1)
3479 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003480 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3481}
3482
Dan Gohman475871a2008-07-27 21:46:04 +00003483SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3484 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003485 if (VTList.NumVTs == 1)
3486 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003487
Chris Lattner5f056bf2005-07-10 01:55:33 +00003488 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003489 // FIXME: figure out how to safely handle things like
3490 // int foo(int x) { return 1 << (x & 255); }
3491 // int bar() { return foo(256); }
3492#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003493 case ISD::SRA_PARTS:
3494 case ISD::SRL_PARTS:
3495 case ISD::SHL_PARTS:
3496 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003497 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003498 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3499 else if (N3.getOpcode() == ISD::AND)
3500 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3501 // If the and is only masking out bits that cannot effect the shift,
3502 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003503 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003504 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3505 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3506 }
3507 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003508#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003509 }
Chris Lattner89c34632005-05-14 06:20:26 +00003510
Chris Lattner43247a12005-08-25 19:12:10 +00003511 // Memoize the node unless it returns a flag.
3512 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003513 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003514 FoldingSetNodeID ID;
3515 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003516 void *IP = 0;
3517 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003518 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003519 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003520 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003521 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3522 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003523 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003524 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3525 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003526 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003527 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3528 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003529 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003530 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3531 }
Chris Lattnera5682852006-08-07 23:03:03 +00003532 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003533 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003534 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003535 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003536 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3537 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003538 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003539 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3540 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003541 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003542 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3543 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003544 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003545 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3546 }
Chris Lattner43247a12005-08-25 19:12:10 +00003547 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003548 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003549#ifndef NDEBUG
3550 VerifyNode(N);
3551#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003552 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003553}
3554
Dan Gohman475871a2008-07-27 21:46:04 +00003555SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003556 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003557}
3558
Dan Gohman475871a2008-07-27 21:46:04 +00003559SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3560 SDValue N1) {
3561 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003562 return getNode(Opcode, VTList, Ops, 1);
3563}
3564
Dan Gohman475871a2008-07-27 21:46:04 +00003565SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3566 SDValue N1, SDValue N2) {
3567 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003568 return getNode(Opcode, VTList, Ops, 2);
3569}
3570
Dan Gohman475871a2008-07-27 21:46:04 +00003571SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3572 SDValue N1, SDValue N2, SDValue N3) {
3573 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003574 return getNode(Opcode, VTList, Ops, 3);
3575}
3576
Dan Gohman475871a2008-07-27 21:46:04 +00003577SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3578 SDValue N1, SDValue N2, SDValue N3,
3579 SDValue N4) {
3580 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003581 return getNode(Opcode, VTList, Ops, 4);
3582}
3583
Dan Gohman475871a2008-07-27 21:46:04 +00003584SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3585 SDValue N1, SDValue N2, SDValue N3,
3586 SDValue N4, SDValue N5) {
3587 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003588 return getNode(Opcode, VTList, Ops, 5);
3589}
3590
Duncan Sands83ec4b62008-06-06 12:08:01 +00003591SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003592 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003593}
3594
Duncan Sands83ec4b62008-06-06 12:08:01 +00003595SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003596 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3597 E = VTList.rend(); I != E; ++I)
3598 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3599 return *I;
3600
3601 MVT *Array = Allocator.Allocate<MVT>(2);
3602 Array[0] = VT1;
3603 Array[1] = VT2;
3604 SDVTList Result = makeVTList(Array, 2);
3605 VTList.push_back(Result);
3606 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003607}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003608
3609SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3610 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3611 E = VTList.rend(); I != E; ++I)
3612 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3613 I->VTs[2] == VT3)
3614 return *I;
3615
3616 MVT *Array = Allocator.Allocate<MVT>(3);
3617 Array[0] = VT1;
3618 Array[1] = VT2;
3619 Array[2] = VT3;
3620 SDVTList Result = makeVTList(Array, 3);
3621 VTList.push_back(Result);
3622 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003623}
3624
Duncan Sands83ec4b62008-06-06 12:08:01 +00003625SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003626 switch (NumVTs) {
3627 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003628 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003629 case 2: return getVTList(VTs[0], VTs[1]);
3630 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3631 default: break;
3632 }
3633
Dan Gohmane8be6c62008-07-17 19:10:17 +00003634 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3635 E = VTList.rend(); I != E; ++I) {
3636 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3637 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003638
3639 bool NoMatch = false;
3640 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003641 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003642 NoMatch = true;
3643 break;
3644 }
3645 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003646 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003647 }
3648
Dan Gohmane8be6c62008-07-17 19:10:17 +00003649 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3650 std::copy(VTs, VTs+NumVTs, Array);
3651 SDVTList Result = makeVTList(Array, NumVTs);
3652 VTList.push_back(Result);
3653 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003654}
3655
3656
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003657/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3658/// specified operands. If the resultant node already exists in the DAG,
3659/// this does not modify the specified node, instead it returns the node that
3660/// already exists. If the resultant node does not exist in the DAG, the
3661/// input node is returned. As a degenerate case, if you specify the same
3662/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003663SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003664 SDNode *N = InN.Val;
3665 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3666
3667 // Check to see if there is no change.
3668 if (Op == N->getOperand(0)) return InN;
3669
3670 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003671 void *InsertPos = 0;
3672 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003673 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003674
Dan Gohman79acd2b2008-07-21 22:38:59 +00003675 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003676 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003677 RemoveNodeFromCSEMaps(N);
3678
3679 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003680 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003681 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003682 N->OperandList[0].setUser(N);
3683 Op.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003684
3685 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003686 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003687 return InN;
3688}
3689
Dan Gohman475871a2008-07-27 21:46:04 +00003690SDValue SelectionDAG::
3691UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003692 SDNode *N = InN.Val;
3693 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3694
3695 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003696 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3697 return InN; // No operands changed, just return the input node.
3698
3699 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003700 void *InsertPos = 0;
3701 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003702 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003703
Dan Gohman79acd2b2008-07-21 22:38:59 +00003704 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003705 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003706 RemoveNodeFromCSEMaps(N);
3707
3708 // Now we update the operands.
3709 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003710 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003711 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003712 N->OperandList[0].setUser(N);
3713 Op1.Val->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003714 }
3715 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003716 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003717 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003718 N->OperandList[1].setUser(N);
3719 Op2.Val->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003720 }
3721
3722 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003723 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003724 return InN;
3725}
3726
Dan Gohman475871a2008-07-27 21:46:04 +00003727SDValue SelectionDAG::
3728UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3729 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003730 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003731}
3732
Dan Gohman475871a2008-07-27 21:46:04 +00003733SDValue SelectionDAG::
3734UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3735 SDValue Op3, SDValue Op4) {
3736 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003737 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003738}
3739
Dan Gohman475871a2008-07-27 21:46:04 +00003740SDValue SelectionDAG::
3741UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3742 SDValue Op3, SDValue Op4, SDValue Op5) {
3743 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003744 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003745}
3746
Dan Gohman475871a2008-07-27 21:46:04 +00003747SDValue SelectionDAG::
3748UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003749 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003750 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003751 "Update with wrong number of operands");
3752
3753 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003754 bool AnyChange = false;
3755 for (unsigned i = 0; i != NumOps; ++i) {
3756 if (Ops[i] != N->getOperand(i)) {
3757 AnyChange = true;
3758 break;
3759 }
3760 }
3761
3762 // No operands changed, just return the input node.
3763 if (!AnyChange) return InN;
3764
3765 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003766 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003767 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003768 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003769
Dan Gohman7ceda162008-05-02 00:05:03 +00003770 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003771 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003772 RemoveNodeFromCSEMaps(N);
3773
3774 // Now we update the operands.
3775 for (unsigned i = 0; i != NumOps; ++i) {
3776 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003777 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003778 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003779 N->OperandList[i].setUser(N);
3780 Ops[i].Val->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003781 }
3782 }
3783
3784 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003785 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003786 return InN;
3787}
3788
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003789/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003790/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003791void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003792 // Unlike the code in MorphNodeTo that does this, we don't need to
3793 // watch for dead nodes here.
3794 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3795 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3796
3797 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003798}
Chris Lattner149c58c2005-08-16 18:17:10 +00003799
Dan Gohmane8be6c62008-07-17 19:10:17 +00003800/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3801/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003802///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003803SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003804 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003805 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003806 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003807}
Chris Lattner0fb094f2005-11-19 01:44:53 +00003808
Dan Gohmane8be6c62008-07-17 19:10:17 +00003809SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003810 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003811 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003812 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003813 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
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) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003819 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003820 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003821 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00003822}
Chris Lattner0fb094f2005-11-19 01:44:53 +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, SDValue Op1,
3826 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003827 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003828 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003829 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00003830}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00003831
Dan Gohmane8be6c62008-07-17 19:10:17 +00003832SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003833 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00003834 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003835 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003836 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003837}
3838
Dan Gohmane8be6c62008-07-17 19:10:17 +00003839SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003840 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003841 unsigned NumOps) {
3842 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003843 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003844}
3845
Dan Gohmane8be6c62008-07-17 19:10:17 +00003846SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003847 MVT VT1, MVT VT2) {
3848 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003849 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00003850}
3851
Dan Gohmane8be6c62008-07-17 19:10:17 +00003852SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003853 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003854 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003855 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003856 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00003857}
3858
Dan Gohmane8be6c62008-07-17 19:10:17 +00003859SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00003860 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003861 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003862 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003863 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003864 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00003865}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00003866
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) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003870 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003871 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003872 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00003873}
3874
Dan Gohmane8be6c62008-07-17 19:10:17 +00003875SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003876 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003877 SDValue Op1, SDValue Op2,
3878 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003879 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003880 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003881 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00003882}
3883
Dan Gohmane8be6c62008-07-17 19:10:17 +00003884SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00003885 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00003886 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003887 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
3888}
3889
3890SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3891 MVT VT) {
3892 SDVTList VTs = getVTList(VT);
3893 return MorphNodeTo(N, Opc, VTs, 0, 0);
3894}
3895
3896SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003897 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003898 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003899 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003900 return MorphNodeTo(N, Opc, VTs, Ops, 1);
3901}
3902
3903SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003904 MVT VT, SDValue Op1,
3905 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003906 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003907 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003908 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3909}
3910
3911SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003912 MVT VT, SDValue Op1,
3913 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003914 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003915 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003916 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3917}
3918
3919SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003920 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003921 unsigned NumOps) {
3922 SDVTList VTs = getVTList(VT);
3923 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3924}
3925
3926SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003927 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003928 unsigned NumOps) {
3929 SDVTList VTs = getVTList(VT1, VT2);
3930 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3931}
3932
3933SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3934 MVT VT1, MVT VT2) {
3935 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003936 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003937}
3938
3939SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3940 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00003941 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003942 SDVTList VTs = getVTList(VT1, VT2, VT3);
3943 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
3944}
3945
3946SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3947 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003948 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003949 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003950 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003951 return MorphNodeTo(N, Opc, VTs, Ops, 1);
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) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003957 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003958 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003959 return MorphNodeTo(N, Opc, VTs, Ops, 2);
3960}
3961
3962SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
3963 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00003964 SDValue Op1, SDValue Op2,
3965 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003966 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00003967 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00003968 return MorphNodeTo(N, Opc, VTs, Ops, 3);
3969}
3970
3971/// MorphNodeTo - These *mutate* the specified node to have the specified
3972/// return type, opcode, and operands.
3973///
3974/// Note that MorphNodeTo returns the resultant node. If there is already a
3975/// node of the specified opcode and operands, it returns that node instead of
3976/// the current one.
3977///
3978/// Using MorphNodeTo is faster than creating a new node and swapping it in
3979/// with ReplaceAllUsesWith both because it often avoids allocating a new
3980/// node, and because it doesn't require CSE recalulation for any of
3981/// the node's users.
3982///
3983SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00003984 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00003985 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00003986 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00003987 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00003988 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
3989 FoldingSetNodeID ID;
3990 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
3991 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
3992 return ON;
3993 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003994
Chris Lattner0fb094f2005-11-19 01:44:53 +00003995 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003996
Dan Gohmane8be6c62008-07-17 19:10:17 +00003997 // Start the morphing.
3998 N->NodeType = Opc;
3999 N->ValueList = VTs.VTs;
4000 N->NumValues = VTs.NumVTs;
4001
4002 // Clear the operands list, updating used nodes to remove this from their
4003 // use list. Keep track of any operands that become dead as a result.
4004 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4005 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4006 I != E; ++I) {
4007 SDNode *Used = I->getVal();
4008 Used->removeUser(std::distance(B, I), N);
4009 if (Used->use_empty())
4010 DeadNodeSet.insert(Used);
4011 }
4012
4013 // If NumOps is larger than the # of operands we currently have, reallocate
4014 // the operand list.
4015 if (NumOps > N->NumOperands) {
4016 if (N->OperandsNeedDelete)
4017 delete[] N->OperandList;
4018 if (N->isMachineOpcode()) {
4019 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004020 // remainder of the current SelectionDAG iteration, so we can allocate
4021 // the operands directly out of a pool with no recycling metadata.
4022 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004023 N->OperandsNeedDelete = false;
4024 } else {
4025 N->OperandList = new SDUse[NumOps];
4026 N->OperandsNeedDelete = true;
4027 }
4028 }
4029
4030 // Assign the new operands.
4031 N->NumOperands = NumOps;
4032 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4033 N->OperandList[i] = Ops[i];
4034 N->OperandList[i].setUser(N);
4035 SDNode *ToUse = N->OperandList[i].getVal();
4036 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004037 }
4038
4039 // Delete any nodes that are still dead after adding the uses for the
4040 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004041 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004042 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4043 E = DeadNodeSet.end(); I != E; ++I)
4044 if ((*I)->use_empty())
4045 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004046 RemoveDeadNodes(DeadNodes);
4047
Dan Gohmane8be6c62008-07-17 19:10:17 +00004048 if (IP)
4049 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004050 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004051}
4052
Chris Lattner0fb094f2005-11-19 01:44:53 +00004053
Evan Cheng6ae46c42006-02-09 07:15:23 +00004054/// getTargetNode - These are used for target selectors to create a new node
4055/// with specified return type(s), target opcode, and operands.
4056///
4057/// Note that getTargetNode returns the resultant node. If there is already a
4058/// node of the specified opcode and operands, it returns that node instead of
4059/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004060SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004061 return getNode(~Opcode, VT).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004062}
Dan Gohman475871a2008-07-27 21:46:04 +00004063SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004064 return getNode(~Opcode, VT, Op1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004065}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004066SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004067 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004068 return getNode(~Opcode, VT, Op1, Op2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004069}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004070SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004071 SDValue Op1, SDValue Op2,
4072 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004073 return getNode(~Opcode, VT, Op1, Op2, Op3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004074}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004075SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004076 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004077 return getNode(~Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004078}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004079SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4080 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004081 SDValue Op;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004082 return getNode(~Opcode, VTs, 2, &Op, 0).Val;
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004083}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004084SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004085 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004086 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004087 return getNode(~Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004088}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004089SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004090 MVT VT2, SDValue Op1,
4091 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004092 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004093 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004094 return getNode(~Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004095}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004096SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004097 MVT VT2, SDValue Op1,
4098 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004099 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004100 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004101 return getNode(~Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004102}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004103SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004104 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004105 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004106 return getNode(~Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004107}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004108SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004109 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004110 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004111 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004112 return getNode(~Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004113}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004114SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004115 SDValue Op1, SDValue Op2,
4116 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004117 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004118 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004119 return getNode(~Opcode, VTs, 3, Ops, 3).Val;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004120}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004121SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004122 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004123 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004124 return getNode(~Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00004125}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004126SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4127 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004128 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004129 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004130 VTList.push_back(VT1);
4131 VTList.push_back(VT2);
4132 VTList.push_back(VT3);
4133 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004134 const MVT *VTs = getNodeValueTypes(VTList);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004135 return getNode(~Opcode, VTs, 4, Ops, NumOps).Val;
Evan Cheng05e69c12007-09-12 23:39:49 +00004136}
Evan Cheng39305cf2007-10-05 01:10:49 +00004137SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004138 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004139 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004140 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004141 return getNode(~Opcode, VTs, ResultTys.size(),
Evan Cheng39305cf2007-10-05 01:10:49 +00004142 Ops, NumOps).Val;
4143}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004144
Evan Cheng08b11732008-03-22 01:55:50 +00004145/// getNodeIfExists - Get the specified node if it's already available, or
4146/// else return NULL.
4147SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004148 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004149 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4150 FoldingSetNodeID ID;
4151 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4152 void *IP = 0;
4153 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4154 return E;
4155 }
4156 return NULL;
4157}
4158
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004159
Evan Cheng99157a02006-08-07 22:13:29 +00004160/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004161/// This can cause recursive merging of nodes in the DAG.
4162///
Chris Lattner11d049c2008-02-03 03:35:22 +00004163/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004164///
Dan Gohman475871a2008-07-27 21:46:04 +00004165void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004166 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004167 SDNode *From = FromN.Val;
Gabor Greif99a6cb92008-08-26 22:36:50 +00004168 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004169 "Cannot replace with this method!");
Chris Lattner11d049c2008-02-03 03:35:22 +00004170 assert(From != To.Val && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004171
Chris Lattner8b8749f2005-08-17 19:00:20 +00004172 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004173 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004174 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004175
Chris Lattner8b8749f2005-08-17 19:00:20 +00004176 // This node is about to morph, remove its old self from the CSE maps.
4177 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004178 int operandNum = 0;
4179 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4180 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004181 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004182 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004183 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004184 I->setUser(U);
4185 To.Val->addUser(operandNum, U);
4186 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004187
4188 // Now that we have modified U, add it back to the CSE maps. If it already
4189 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004190 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004191 ReplaceAllUsesWith(U, Existing, UpdateListener);
4192 // U is now dead. Inform the listener if it exists and delete it.
4193 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004194 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004195 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004196 } else {
4197 // If the node doesn't already exist, we updated it. Inform a listener if
4198 // it exists.
4199 if (UpdateListener)
4200 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004201 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004202 }
4203}
4204
4205/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4206/// This can cause recursive merging of nodes in the DAG.
4207///
4208/// This version assumes From/To have matching types and numbers of result
4209/// values.
4210///
Chris Lattner1e111c72005-09-07 05:37:01 +00004211void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004212 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004213 assert(From->getVTList().VTs == To->getVTList().VTs &&
4214 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004215 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004216
4217 // Handle the trivial case.
4218 if (From == To)
4219 return;
4220
Chris Lattner8b52f212005-08-26 18:36:28 +00004221 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004222 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004223 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004224
Chris Lattner8b52f212005-08-26 18:36:28 +00004225 // This node is about to morph, remove its old self from the CSE maps.
4226 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004227 int operandNum = 0;
4228 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4229 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004230 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004231 From->removeUser(operandNum, U);
Roman Levenstein9cac5252008-04-16 16:15:27 +00004232 I->getVal() = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004233 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004234 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004235
Chris Lattner8b8749f2005-08-17 19:00:20 +00004236 // Now that we have modified U, add it back to the CSE maps. If it already
4237 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004238 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004239 ReplaceAllUsesWith(U, Existing, UpdateListener);
4240 // U is now dead. Inform the listener if it exists and delete it.
4241 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004242 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004243 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004244 } else {
4245 // If the node doesn't already exist, we updated it. Inform a listener if
4246 // it exists.
4247 if (UpdateListener)
4248 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004249 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004250 }
4251}
4252
Chris Lattner8b52f212005-08-26 18:36:28 +00004253/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4254/// This can cause recursive merging of nodes in the DAG.
4255///
4256/// This version can replace From with any result values. To must match the
4257/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004258void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004259 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004260 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004261 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004262 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004263
4264 while (!From->use_empty()) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004265 SDNode::use_iterator UI = From->use_begin();
Dan Gohman89684502008-07-27 20:43:25 +00004266 SDNode *U = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004267
Chris Lattner7b2880c2005-08-24 22:44:39 +00004268 // This node is about to morph, remove its old self from the CSE maps.
4269 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004270 int operandNum = 0;
4271 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4272 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004273 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004274 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004275 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004276 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004277 I->setUser(U);
4278 ToOp.Val->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004279 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004280
Chris Lattner7b2880c2005-08-24 22:44:39 +00004281 // Now that we have modified U, add it back to the CSE maps. If it already
4282 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004283 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004284 ReplaceAllUsesWith(U, Existing, UpdateListener);
4285 // U is now dead. Inform the listener if it exists and delete it.
4286 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004287 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004288 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004289 } else {
4290 // If the node doesn't already exist, we updated it. Inform a listener if
4291 // it exists.
4292 if (UpdateListener)
4293 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004294 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004295 }
4296}
4297
Chris Lattner012f2412006-02-17 21:58:01 +00004298/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
4299/// uses of other values produced by From.Val alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004300/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004301void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004302 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004303 // Handle the really simple, really trivial case efficiently.
4304 if (From == To) return;
4305
Chris Lattner012f2412006-02-17 21:58:01 +00004306 // Handle the simple, trivial, case efficiently.
Chris Lattner11d049c2008-02-03 03:35:22 +00004307 if (From.Val->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004308 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004309 return;
4310 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004311
Chris Lattnercf5640b2007-02-04 00:14:31 +00004312 // Get all of the users of From.Val. We want these in a nice,
4313 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Dan Gohman89684502008-07-27 20:43:25 +00004314 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004315
4316 while (!Users.empty()) {
4317 // We know that this user uses some value of From. If it is the right
4318 // value, update it.
4319 SDNode *User = Users.back();
4320 Users.pop_back();
4321
Chris Lattner01d029b2007-10-15 06:10:22 +00004322 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004323 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004324 for (; Op != E; ++Op)
4325 if (*Op == From) break;
4326
4327 // If there are no matches, the user must use some other result of From.
4328 if (Op == E) continue;
4329
4330 // Okay, we know this user needs to be updated. Remove its old self
4331 // from the CSE maps.
4332 RemoveNodeFromCSEMaps(User);
4333
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004334 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004335 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004336 if (*Op == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004337 From.Val->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004338 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004339 Op->setUser(User);
4340 To.Val->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004341 }
4342 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004343
4344 // Now that we have modified User, add it back to the CSE maps. If it
4345 // already exists there, recursively merge the results together.
4346 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004347 if (!Existing) {
4348 if (UpdateListener) UpdateListener->NodeUpdated(User);
4349 continue; // Continue on to next user.
4350 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004351
4352 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004353 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004354 // recursive merging of other unrelated nodes down the line.
4355 ReplaceAllUsesWith(User, Existing, UpdateListener);
4356
4357 // User is now dead. Notify a listener if present.
4358 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4359 DeleteNodeNotInCSEMaps(User);
4360 }
4361}
4362
4363/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
4364/// uses of other values produced by From.Val alone. The same value may
4365/// appear in both the From and To list. The Deleted vector is
4366/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004367void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4368 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004369 unsigned Num,
4370 DAGUpdateListener *UpdateListener){
4371 // Handle the simple, trivial case efficiently.
4372 if (Num == 1)
4373 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4374
4375 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4376 for (unsigned i = 0; i != Num; ++i)
4377 for (SDNode::use_iterator UI = From[i].Val->use_begin(),
4378 E = From[i].Val->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004379 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004380
4381 while (!Users.empty()) {
4382 // We know that this user uses some value of From. If it is the right
4383 // value, update it.
4384 SDNode *User = Users.back().first;
4385 unsigned i = Users.back().second;
4386 Users.pop_back();
4387
4388 // Scan for an operand that matches From.
4389 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4390 for (; Op != E; ++Op)
4391 if (*Op == From[i]) break;
4392
4393 // If there are no matches, the user must use some other result of From.
4394 if (Op == E) continue;
4395
4396 // Okay, we know this user needs to be updated. Remove its old self
4397 // from the CSE maps.
4398 RemoveNodeFromCSEMaps(User);
4399
4400 // Update all operands that match "From" in case there are multiple uses.
4401 for (; Op != E; ++Op) {
4402 if (*Op == From[i]) {
4403 From[i].Val->removeUser(Op-User->op_begin(), User);
4404 *Op = To[i];
4405 Op->setUser(User);
4406 To[i].Val->addUser(Op-User->op_begin(), User);
4407 }
4408 }
4409
4410 // Now that we have modified User, add it back to the CSE maps. If it
4411 // already exists there, recursively merge the results together.
4412 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4413 if (!Existing) {
4414 if (UpdateListener) UpdateListener->NodeUpdated(User);
4415 continue; // Continue on to next user.
4416 }
4417
4418 // If there was already an existing matching node, use ReplaceAllUsesWith
4419 // to replace the dead one with the existing one. This can cause
4420 // recursive merging of other unrelated nodes down the line.
4421 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004422
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004423 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004424 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004425 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004426 }
4427}
4428
Evan Chenge6f35d82006-08-01 08:20:41 +00004429/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004430/// based on their topological order. It returns the maximum id and a vector
4431/// of the SDNodes* in assigned order by reference.
4432unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00004433 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00004434 std::vector<SDNode*> Sources;
4435
Evan Chenge6f35d82006-08-01 08:20:41 +00004436 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
4437 SDNode *N = I;
4438 unsigned Degree = N->use_size();
Dan Gohman3200d922008-08-26 21:42:18 +00004439 // Temporarily use the Node Id as scratch space for the degree count.
4440 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004441 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00004442 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00004443 }
4444
Evan Cheng99157a02006-08-07 22:13:29 +00004445 TopOrder.clear();
Dan Gohman0e5f1302008-07-07 23:02:41 +00004446 TopOrder.reserve(DAGSize);
Dan Gohman3200d922008-08-26 21:42:18 +00004447 int Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004448 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00004449 SDNode *N = Sources.back();
4450 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00004451 TopOrder.push_back(N);
Dan Gohman3200d922008-08-26 21:42:18 +00004452 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00004453 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00004454 SDNode *P = I->getVal();
Dan Gohman3200d922008-08-26 21:42:18 +00004455 unsigned Degree = P->getNodeId();
4456 --Degree;
4457 P->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004458 if (Degree == 0)
4459 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00004460 }
4461 }
4462
Evan Chengc384d6c2006-08-02 22:00:34 +00004463 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00004464}
4465
4466
Evan Cheng091cba12006-07-27 06:39:06 +00004467
Jim Laskey58b968b2005-08-17 20:08:02 +00004468//===----------------------------------------------------------------------===//
4469// SDNode Class
4470//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004471
Chris Lattner917d2c92006-07-19 00:00:37 +00004472// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004473void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00004474void UnarySDNode::ANCHOR() {}
4475void BinarySDNode::ANCHOR() {}
4476void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004477void HandleSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004478void ConstantSDNode::ANCHOR() {}
4479void ConstantFPSDNode::ANCHOR() {}
4480void GlobalAddressSDNode::ANCHOR() {}
4481void FrameIndexSDNode::ANCHOR() {}
4482void JumpTableSDNode::ANCHOR() {}
4483void ConstantPoolSDNode::ANCHOR() {}
4484void BasicBlockSDNode::ANCHOR() {}
4485void SrcValueSDNode::ANCHOR() {}
Dan Gohman69de1932008-02-06 22:27:42 +00004486void MemOperandSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004487void RegisterSDNode::ANCHOR() {}
Dan Gohman7f460202008-06-30 20:59:49 +00004488void DbgStopPointSDNode::ANCHOR() {}
Dan Gohman44066042008-07-01 00:05:16 +00004489void LabelSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004490void ExternalSymbolSDNode::ANCHOR() {}
4491void CondCodeSDNode::ANCHOR() {}
Duncan Sands276dcbd2008-03-21 09:14:45 +00004492void ARG_FLAGSSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004493void VTSDNode::ANCHOR() {}
Mon P Wang28873102008-06-25 08:15:39 +00004494void MemSDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00004495void LoadSDNode::ANCHOR() {}
4496void StoreSDNode::ANCHOR() {}
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004497void AtomicSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00004498
Chris Lattner48b85922007-02-04 02:41:42 +00004499HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004500 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004501}
4502
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004503GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004504 MVT VT, int o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004505 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004506 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004507 // Thread Local
4508 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4509 // Non Thread Local
4510 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4511 getSDVTList(VT)), Offset(o) {
4512 TheGlobal = const_cast<GlobalValue*>(GA);
4513}
Chris Lattner48b85922007-02-04 02:41:42 +00004514
Dan Gohman1ea58a52008-07-09 22:08:04 +00004515MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004516 const Value *srcValue, int SVO,
4517 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004518 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004519 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004520
4521 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4522 assert(getAlignment() == alignment && "Alignment representation error!");
4523 assert(isVolatile() == vol && "Volatile representation error!");
4524}
4525
Dan Gohman36b5c132008-04-07 19:35:22 +00004526/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004527/// reference performed by this memory reference.
4528MachineMemOperand MemSDNode::getMemOperand() const {
4529 int Flags;
4530 if (isa<LoadSDNode>(this))
4531 Flags = MachineMemOperand::MOLoad;
4532 else if (isa<StoreSDNode>(this))
4533 Flags = MachineMemOperand::MOStore;
4534 else {
4535 assert(isa<AtomicSDNode>(this) && "Unknown MemSDNode opcode!");
4536 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4537 }
4538
4539 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004540 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4541
Dan Gohman1ea58a52008-07-09 22:08:04 +00004542 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004543 const FrameIndexSDNode *FI =
4544 dyn_cast<const FrameIndexSDNode>(getBasePtr().Val);
4545 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004546 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4547 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004548 else
4549 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4550 Size, getAlignment());
4551}
4552
Jim Laskey583bd472006-10-27 23:46:08 +00004553/// Profile - Gather unique data for the node.
4554///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004555void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004556 AddNodeIDNode(ID, this);
4557}
4558
Chris Lattnera3255112005-11-08 23:30:28 +00004559/// getValueTypeList - Return a pointer to the specified value type.
4560///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004561const MVT *SDNode::getValueTypeList(MVT VT) {
4562 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004563 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004564 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004565 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004566 static MVT VTs[MVT::LAST_VALUETYPE];
4567 VTs[VT.getSimpleVT()] = VT;
4568 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004569 }
Chris Lattnera3255112005-11-08 23:30:28 +00004570}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004571
Chris Lattner5c884562005-01-12 18:37:47 +00004572/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4573/// indicated value. This method ignores uses of other values defined by this
4574/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004575bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004576 assert(Value < getNumValues() && "Bad value!");
4577
Roman Levensteindc1adac2008-04-07 10:06:32 +00004578 // TODO: Only iterate over uses of a given value of the node
4579 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004580 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004581 if (NUses == 0)
4582 return false;
4583 --NUses;
4584 }
Chris Lattner5c884562005-01-12 18:37:47 +00004585 }
4586
4587 // Found exactly the right number of uses?
4588 return NUses == 0;
4589}
4590
4591
Evan Cheng33d55952007-08-02 05:29:38 +00004592/// hasAnyUseOfValue - Return true if there are any use of the indicated
4593/// value. This method ignores uses of other values defined by this operation.
4594bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4595 assert(Value < getNumValues() && "Bad value!");
4596
Dan Gohman1373c1c2008-07-09 22:39:01 +00004597 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004598 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004599 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004600
4601 return false;
4602}
4603
4604
Dan Gohman2a629952008-07-27 18:06:42 +00004605/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004606///
Dan Gohman2a629952008-07-27 18:06:42 +00004607bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004608 bool Seen = false;
4609 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004610 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004611 if (User == this)
4612 Seen = true;
4613 else
4614 return false;
4615 }
4616
4617 return Seen;
4618}
4619
Evan Chenge6e97e62006-11-03 07:31:32 +00004620/// isOperand - Return true if this node is an operand of N.
4621///
Dan Gohman475871a2008-07-27 21:46:04 +00004622bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004623 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4624 if (*this == N->getOperand(i))
4625 return true;
4626 return false;
4627}
4628
Evan Cheng917be682008-03-04 00:41:45 +00004629bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004630 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004631 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004632 return true;
4633 return false;
4634}
Evan Cheng4ee62112006-02-05 06:29:23 +00004635
Chris Lattner572dee72008-01-16 05:49:24 +00004636/// reachesChainWithoutSideEffects - Return true if this operand (which must
4637/// be a chain) reaches the specified operand without crossing any
4638/// side-effecting instructions. In practice, this looks through token
4639/// factors and non-volatile loads. In order to remain efficient, this only
4640/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004641bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004642 unsigned Depth) const {
4643 if (*this == Dest) return true;
4644
4645 // Don't search too deeply, we just want to be able to see through
4646 // TokenFactor's etc.
4647 if (Depth == 0) return false;
4648
4649 // If this is a token factor, all inputs to the TF happen in parallel. If any
4650 // of the operands of the TF reach dest, then we can do the xform.
4651 if (getOpcode() == ISD::TokenFactor) {
4652 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4653 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4654 return true;
4655 return false;
4656 }
4657
4658 // Loads don't have side effects, look through them.
4659 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4660 if (!Ld->isVolatile())
4661 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4662 }
4663 return false;
4664}
4665
4666
Evan Chengc5fc57d2006-11-03 03:05:24 +00004667static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004668 SmallPtrSet<SDNode *, 32> &Visited) {
4669 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004670 return;
4671
4672 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
4673 SDNode *Op = N->getOperand(i).Val;
4674 if (Op == P) {
4675 found = true;
4676 return;
4677 }
4678 findPredecessor(Op, P, found, Visited);
4679 }
4680}
4681
Evan Cheng917be682008-03-04 00:41:45 +00004682/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004683/// is either an operand of N or it can be reached by recursively traversing
4684/// up the operands.
4685/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004686bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004687 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004688 bool found = false;
4689 findPredecessor(N, this, found, Visited);
4690 return found;
4691}
4692
Evan Chengc5484282006-10-04 00:56:09 +00004693uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4694 assert(Num < NumOperands && "Invalid child # of SDNode!");
4695 return cast<ConstantSDNode>(OperandList[Num])->getValue();
4696}
4697
Reid Spencer577cc322007-04-01 07:32:19 +00004698std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004699 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004700 default:
4701 if (getOpcode() < ISD::BUILTIN_OP_END)
4702 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004703 if (isMachineOpcode()) {
4704 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004705 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004706 if (getMachineOpcode() < TII->getNumOpcodes())
4707 return TII->get(getMachineOpcode()).getName();
4708 return "<<Unknown Machine Node>>";
4709 }
4710 if (G) {
4711 TargetLowering &TLI = G->getTargetLoweringInfo();
4712 const char *Name = TLI.getTargetNodeName(getOpcode());
4713 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004714 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004715 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004716 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004717
Dan Gohmane8be6c62008-07-17 19:10:17 +00004718#ifndef NDEBUG
4719 case ISD::DELETED_NODE:
4720 return "<<Deleted Node!>>";
4721#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004722 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004723 case ISD::MEMBARRIER: return "MemBarrier";
Mon P Wang28873102008-06-25 08:15:39 +00004724 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4725 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4726 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
Mon P Wang63307c32008-05-05 19:05:59 +00004727 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4728 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4729 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
Andrew Lenharth507a58a2008-06-14 05:48:15 +00004730 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
Mon P Wang63307c32008-05-05 19:05:59 +00004731 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4732 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4733 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4734 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
4735 case ISD::ATOMIC_SWAP: return "AtomicSWAP";
Andrew Lenharth95762122005-03-31 21:24:06 +00004736 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004737 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004738 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004739 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004740 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004741 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004742 case ISD::AssertSext: return "AssertSext";
4743 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004744
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004745 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004746 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004747 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004748 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004749
4750 case ISD::Constant: return "Constant";
4751 case ISD::ConstantFP: return "ConstantFP";
4752 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004753 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004754 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004755 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00004756 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00004757 case ISD::RETURNADDR: return "RETURNADDR";
4758 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004759 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00004760 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
4761 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004762 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00004763 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004764 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00004765 case ISD::INTRINSIC_WO_CHAIN: {
4766 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
4767 return Intrinsic::getName((Intrinsic::ID)IID);
4768 }
4769 case ISD::INTRINSIC_VOID:
4770 case ISD::INTRINSIC_W_CHAIN: {
4771 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00004772 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00004773 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00004774
Chris Lattnerb2827b02006-03-19 00:52:58 +00004775 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004776 case ISD::TargetConstant: return "TargetConstant";
4777 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004778 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004779 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004780 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00004781 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00004782 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004783 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004784
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004785 case ISD::CopyToReg: return "CopyToReg";
4786 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004787 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00004788 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004789 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00004790 case ISD::DBG_LABEL: return "dbg_label";
4791 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00004792 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00004793 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00004794 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00004795 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00004796
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004797 // Unary operators
4798 case ISD::FABS: return "fabs";
4799 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00004800 case ISD::FSQRT: return "fsqrt";
4801 case ISD::FSIN: return "fsin";
4802 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00004803 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00004804 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00004805 case ISD::FTRUNC: return "ftrunc";
4806 case ISD::FFLOOR: return "ffloor";
4807 case ISD::FCEIL: return "fceil";
4808 case ISD::FRINT: return "frint";
4809 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00004810
4811 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004812 case ISD::ADD: return "add";
4813 case ISD::SUB: return "sub";
4814 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00004815 case ISD::MULHU: return "mulhu";
4816 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004817 case ISD::SDIV: return "sdiv";
4818 case ISD::UDIV: return "udiv";
4819 case ISD::SREM: return "srem";
4820 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00004821 case ISD::SMUL_LOHI: return "smul_lohi";
4822 case ISD::UMUL_LOHI: return "umul_lohi";
4823 case ISD::SDIVREM: return "sdivrem";
4824 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004825 case ISD::AND: return "and";
4826 case ISD::OR: return "or";
4827 case ISD::XOR: return "xor";
4828 case ISD::SHL: return "shl";
4829 case ISD::SRA: return "sra";
4830 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00004831 case ISD::ROTL: return "rotl";
4832 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00004833 case ISD::FADD: return "fadd";
4834 case ISD::FSUB: return "fsub";
4835 case ISD::FMUL: return "fmul";
4836 case ISD::FDIV: return "fdiv";
4837 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00004838 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00004839 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00004840
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004841 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00004842 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004843 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00004844 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004845 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004846 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00004847 case ISD::CONCAT_VECTORS: return "concat_vectors";
4848 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004849 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00004850 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00004851 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00004852 case ISD::ADDC: return "addc";
4853 case ISD::ADDE: return "adde";
4854 case ISD::SUBC: return "subc";
4855 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00004856 case ISD::SHL_PARTS: return "shl_parts";
4857 case ISD::SRA_PARTS: return "sra_parts";
4858 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00004859
4860 case ISD::EXTRACT_SUBREG: return "extract_subreg";
4861 case ISD::INSERT_SUBREG: return "insert_subreg";
4862
Chris Lattner7f644642005-04-28 21:44:03 +00004863 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004864 case ISD::SIGN_EXTEND: return "sign_extend";
4865 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00004866 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00004867 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004868 case ISD::TRUNCATE: return "truncate";
4869 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00004870 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00004871 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004872 case ISD::FP_EXTEND: return "fp_extend";
4873
4874 case ISD::SINT_TO_FP: return "sint_to_fp";
4875 case ISD::UINT_TO_FP: return "uint_to_fp";
4876 case ISD::FP_TO_SINT: return "fp_to_sint";
4877 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00004878 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004879
4880 // Control flow instructions
4881 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00004882 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00004883 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004884 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00004885 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004886 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00004887 case ISD::CALLSEQ_START: return "callseq_start";
4888 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004889
4890 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00004891 case ISD::LOAD: return "load";
4892 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00004893 case ISD::VAARG: return "vaarg";
4894 case ISD::VACOPY: return "vacopy";
4895 case ISD::VAEND: return "vaend";
4896 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004897 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00004898 case ISD::EXTRACT_ELEMENT: return "extract_element";
4899 case ISD::BUILD_PAIR: return "build_pair";
4900 case ISD::STACKSAVE: return "stacksave";
4901 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004902 case ISD::TRAP: return "trap";
4903
Nate Begeman1b5db7a2006-01-16 08:07:10 +00004904 // Bit manipulation
4905 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00004906 case ISD::CTPOP: return "ctpop";
4907 case ISD::CTTZ: return "cttz";
4908 case ISD::CTLZ: return "ctlz";
4909
Chris Lattner36ce6912005-11-29 06:21:05 +00004910 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00004911 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00004912 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00004913
Duncan Sands36397f52007-07-27 12:58:54 +00004914 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00004915 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00004916
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004917 case ISD::CONDCODE:
4918 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004919 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004920 case ISD::SETOEQ: return "setoeq";
4921 case ISD::SETOGT: return "setogt";
4922 case ISD::SETOGE: return "setoge";
4923 case ISD::SETOLT: return "setolt";
4924 case ISD::SETOLE: return "setole";
4925 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004926
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004927 case ISD::SETO: return "seto";
4928 case ISD::SETUO: return "setuo";
4929 case ISD::SETUEQ: return "setue";
4930 case ISD::SETUGT: return "setugt";
4931 case ISD::SETUGE: return "setuge";
4932 case ISD::SETULT: return "setult";
4933 case ISD::SETULE: return "setule";
4934 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00004935
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00004936 case ISD::SETEQ: return "seteq";
4937 case ISD::SETGT: return "setgt";
4938 case ISD::SETGE: return "setge";
4939 case ISD::SETLT: return "setlt";
4940 case ISD::SETLE: return "setle";
4941 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004942 }
4943 }
4944}
Chris Lattnerc3aae252005-01-07 07:46:32 +00004945
Evan Cheng144d8f02006-11-09 17:55:04 +00004946const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00004947 switch (AM) {
4948 default:
4949 return "";
4950 case ISD::PRE_INC:
4951 return "<pre-inc>";
4952 case ISD::PRE_DEC:
4953 return "<pre-dec>";
4954 case ISD::POST_INC:
4955 return "<post-inc>";
4956 case ISD::POST_DEC:
4957 return "<post-dec>";
4958 }
4959}
4960
Duncan Sands276dcbd2008-03-21 09:14:45 +00004961std::string ISD::ArgFlagsTy::getArgFlagsString() {
4962 std::string S = "< ";
4963
4964 if (isZExt())
4965 S += "zext ";
4966 if (isSExt())
4967 S += "sext ";
4968 if (isInReg())
4969 S += "inreg ";
4970 if (isSRet())
4971 S += "sret ";
4972 if (isByVal())
4973 S += "byval ";
4974 if (isNest())
4975 S += "nest ";
4976 if (getByValAlign())
4977 S += "byval-align:" + utostr(getByValAlign()) + " ";
4978 if (getOrigAlign())
4979 S += "orig-align:" + utostr(getOrigAlign()) + " ";
4980 if (getByValSize())
4981 S += "byval-size:" + utostr(getByValSize()) + " ";
4982 return S + ">";
4983}
4984
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004985void SDNode::dump() const { dump(0); }
4986void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00004987 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00004988 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00004989}
4990
4991void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
4992 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00004993
4994 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00004995 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00004996 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00004997 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00004998 else
Chris Lattner944fac72008-08-23 22:23:09 +00004999 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005000 }
Chris Lattner944fac72008-08-23 22:23:09 +00005001 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005002
Chris Lattner944fac72008-08-23 22:23:09 +00005003 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005004 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005005 if (i) OS << ", ";
5006 OS << (void*)getOperand(i).Val;
Gabor Greif99a6cb92008-08-26 22:36:50 +00005007 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005008 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005009 }
5010
Evan Chengce254432007-12-11 02:08:35 +00005011 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
5012 SDNode *Mask = getOperand(2).Val;
Chris Lattner944fac72008-08-23 22:23:09 +00005013 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005014 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005015 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005016 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005017 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005018 else
Chris Lattner944fac72008-08-23 22:23:09 +00005019 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getValue();
Evan Chengce254432007-12-11 02:08:35 +00005020 }
Chris Lattner944fac72008-08-23 22:23:09 +00005021 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005022 }
5023
Chris Lattnerc3aae252005-01-07 07:46:32 +00005024 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005025 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005026 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005027 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005028 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005029 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005030 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005031 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005032 OS << "<APFloat(";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005033 CSDN->getValueAPF().convertToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005034 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005035 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005036 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005037 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00005038 int offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005039 OS << '<';
5040 WriteAsOperand(OS, GADN->getGlobal());
5041 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005042 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005043 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005044 else
Chris Lattner944fac72008-08-23 22:23:09 +00005045 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005046 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005047 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005048 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005049 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005050 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005051 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005052 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005053 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005054 else
Chris Lattner944fac72008-08-23 22:23:09 +00005055 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005056 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005057 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005058 else
Chris Lattner944fac72008-08-23 22:23:09 +00005059 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005060 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005061 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005062 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5063 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005064 OS << LBB->getName() << " ";
5065 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005066 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005067 if (G && R->getReg() &&
5068 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005069 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005070 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005071 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005072 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005073 } else if (const ExternalSymbolSDNode *ES =
5074 dyn_cast<ExternalSymbolSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005075 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005076 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5077 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005078 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005079 else
Chris Lattner944fac72008-08-23 22:23:09 +00005080 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005081 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5082 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005083 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005084 else
Chris Lattner944fac72008-08-23 22:23:09 +00005085 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005086 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005087 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005088 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005089 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005090 }
5091 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005092 const Value *SrcValue = LD->getSrcValue();
5093 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005094 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005095 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005096 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005097 else
Chris Lattner944fac72008-08-23 22:23:09 +00005098 OS << "null";
5099 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005100
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005101 bool doExt = true;
5102 switch (LD->getExtensionType()) {
5103 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005104 case ISD::EXTLOAD: OS << " <anyext "; break;
5105 case ISD::SEXTLOAD: OS << " <sext "; break;
5106 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005107 }
5108 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005109 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005110
Evan Cheng144d8f02006-11-09 17:55:04 +00005111 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005112 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005113 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005114 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005115 OS << " <volatile>";
5116 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005117 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005118 const Value *SrcValue = ST->getSrcValue();
5119 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005120 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005121 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005122 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005123 else
Chris Lattner944fac72008-08-23 22:23:09 +00005124 OS << "null";
5125 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005126
5127 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005128 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005129
5130 const char *AM = getIndexedModeName(ST->getAddressingMode());
5131 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005132 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005133 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005134 OS << " <volatile>";
5135 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005136 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5137 const Value *SrcValue = AT->getSrcValue();
5138 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005139 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005140 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005141 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005142 else
Chris Lattner944fac72008-08-23 22:23:09 +00005143 OS << "null";
5144 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005145 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005146 OS << " <volatile>";
5147 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005148 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005149}
5150
Chris Lattnerde202b32005-11-09 23:47:37 +00005151static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005152 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5153 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005154 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005155 else
Bill Wendling832171c2006-12-07 20:04:42 +00005156 cerr << "\n" << std::string(indent+2, ' ')
5157 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005158
Chris Lattnerea946cd2005-01-09 20:38:33 +00005159
Bill Wendling832171c2006-12-07 20:04:42 +00005160 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005161 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005162}
5163
Chris Lattnerc3aae252005-01-07 07:46:32 +00005164void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005165 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005166
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005167 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5168 I != E; ++I) {
5169 const SDNode *N = I;
5170 if (!N->hasOneUse() && N != getRoot().Val)
5171 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005172 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005173
Jim Laskey26f7fa72006-10-17 19:33:52 +00005174 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005175
Bill Wendling832171c2006-12-07 20:04:42 +00005176 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005177}
5178
Evan Chengd6594ae2006-09-12 21:00:35 +00005179const Type *ConstantPoolSDNode::getType() const {
5180 if (isMachineConstantPoolEntry())
5181 return Val.MachineCPVal->getType();
5182 return Val.ConstVal->getType();
5183}