blob: e1fda39b572b672e219149116700c13375a65d47 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000018#include "llvm/Assembly/Writer.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000021#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000022#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000024#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000026#include "llvm/ADT/SetVector.h"
Chris Lattner190a4182006-08-04 17:45:20 +000027#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000028#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000029#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000030#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000031#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000032#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner0b3e5252006-08-15 19:11:05 +000035/// makeVTList - Return an instance of the SDVTList struct initialized with the
36/// specified members.
37static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
38 SDVTList Res = {VTs, NumVTs};
39 return Res;
40}
41
Chris Lattner5cdcc582005-01-09 20:52:51 +000042// isInvertibleForFree - Return true if there is no cost to emitting the logical
43// inverse of this node.
44static bool isInvertibleForFree(SDOperand N) {
45 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000046 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000047 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000048 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000049}
50
Jim Laskey58b968b2005-08-17 20:08:02 +000051//===----------------------------------------------------------------------===//
52// ConstantFPSDNode Class
53//===----------------------------------------------------------------------===//
54
55/// isExactlyValue - We don't rely on operator== working on double values, as
56/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
57/// As such, this method can be used to do an exact bit-for-bit comparison of
58/// two floating point values.
59bool ConstantFPSDNode::isExactlyValue(double V) const {
60 return DoubleToBits(V) == DoubleToBits(Value);
61}
62
63//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000064// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000066
Evan Chenga8df1662006-03-27 06:58:47 +000067/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000068/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000069bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000070 // Look through a bit convert.
71 if (N->getOpcode() == ISD::BIT_CONVERT)
72 N = N->getOperand(0).Val;
73
Evan Chenga8df1662006-03-27 06:58:47 +000074 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000075
76 unsigned i = 0, e = N->getNumOperands();
77
78 // Skip over all of the undef values.
79 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
80 ++i;
81
82 // Do not accept an all-undef vector.
83 if (i == e) return false;
84
85 // Do not accept build_vectors that aren't all constants or which have non-~0
86 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000087 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000088 if (isa<ConstantSDNode>(NotZero)) {
89 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
90 return false;
91 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000092 MVT::ValueType VT = NotZero.getValueType();
93 if (VT== MVT::f64) {
94 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
95 (uint64_t)-1)
96 return false;
97 } else {
98 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
99 (uint32_t)-1)
100 return false;
101 }
Evan Chenga8df1662006-03-27 06:58:47 +0000102 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000103 return false;
104
105 // Okay, we have at least one ~0 value, check to see if the rest match or are
106 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000107 for (++i; i != e; ++i)
108 if (N->getOperand(i) != NotZero &&
109 N->getOperand(i).getOpcode() != ISD::UNDEF)
110 return false;
111 return true;
112}
113
114
Evan Cheng4a147842006-03-26 09:50:58 +0000115/// isBuildVectorAllZeros - Return true if the specified node is a
116/// BUILD_VECTOR where all of the elements are 0 or undef.
117bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000118 // Look through a bit convert.
119 if (N->getOpcode() == ISD::BIT_CONVERT)
120 N = N->getOperand(0).Val;
121
Evan Cheng4a147842006-03-26 09:50:58 +0000122 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000123
124 unsigned i = 0, e = N->getNumOperands();
125
126 // Skip over all of the undef values.
127 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
128 ++i;
129
130 // Do not accept an all-undef vector.
131 if (i == e) return false;
132
133 // Do not accept build_vectors that aren't all constants or which have non-~0
134 // elements.
135 SDOperand Zero = N->getOperand(i);
136 if (isa<ConstantSDNode>(Zero)) {
137 if (!cast<ConstantSDNode>(Zero)->isNullValue())
138 return false;
139 } else if (isa<ConstantFPSDNode>(Zero)) {
140 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
141 return false;
142 } else
143 return false;
144
145 // Okay, we have at least one ~0 value, check to see if the rest match or are
146 // undefs.
147 for (++i; i != e; ++i)
148 if (N->getOperand(i) != Zero &&
149 N->getOperand(i).getOpcode() != ISD::UNDEF)
150 return false;
151 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000152}
153
Chris Lattnerc3aae252005-01-07 07:46:32 +0000154/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
155/// when given the operation for (X op Y).
156ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
157 // To perform this operation, we just need to swap the L and G bits of the
158 // operation.
159 unsigned OldL = (Operation >> 2) & 1;
160 unsigned OldG = (Operation >> 1) & 1;
161 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
162 (OldL << 1) | // New G bit
163 (OldG << 2)); // New L bit.
164}
165
166/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
167/// 'op' is a valid SetCC operation.
168ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
169 unsigned Operation = Op;
170 if (isInteger)
171 Operation ^= 7; // Flip L, G, E bits, but not U.
172 else
173 Operation ^= 15; // Flip all of the condition bits.
174 if (Operation > ISD::SETTRUE2)
175 Operation &= ~8; // Don't let N and U bits get set.
176 return ISD::CondCode(Operation);
177}
178
179
180/// isSignedOp - For an integer comparison, return 1 if the comparison is a
181/// signed operation and 2 if the result is an unsigned comparison. Return zero
182/// if the operation does not depend on the sign of the input (setne and seteq).
183static int isSignedOp(ISD::CondCode Opcode) {
184 switch (Opcode) {
185 default: assert(0 && "Illegal integer setcc operation!");
186 case ISD::SETEQ:
187 case ISD::SETNE: return 0;
188 case ISD::SETLT:
189 case ISD::SETLE:
190 case ISD::SETGT:
191 case ISD::SETGE: return 1;
192 case ISD::SETULT:
193 case ISD::SETULE:
194 case ISD::SETUGT:
195 case ISD::SETUGE: return 2;
196 }
197}
198
199/// getSetCCOrOperation - Return the result of a logical OR between different
200/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
201/// returns SETCC_INVALID if it is not possible to represent the resultant
202/// comparison.
203ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
204 bool isInteger) {
205 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
206 // Cannot fold a signed integer setcc with an unsigned integer setcc.
207 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000208
Chris Lattnerc3aae252005-01-07 07:46:32 +0000209 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000210
Chris Lattnerc3aae252005-01-07 07:46:32 +0000211 // If the N and U bits get set then the resultant comparison DOES suddenly
212 // care about orderedness, and is true when ordered.
213 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000214 Op &= ~16; // Clear the U bit if the N bit is set.
215
216 // Canonicalize illegal integer setcc's.
217 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
218 Op = ISD::SETNE;
219
Chris Lattnerc3aae252005-01-07 07:46:32 +0000220 return ISD::CondCode(Op);
221}
222
223/// getSetCCAndOperation - Return the result of a logical AND between different
224/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
225/// function returns zero if it is not possible to represent the resultant
226/// comparison.
227ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
228 bool isInteger) {
229 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
230 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000231 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000232
233 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000234 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
235
236 // Canonicalize illegal integer setcc's.
237 if (isInteger) {
238 switch (Result) {
239 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000240 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
241 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
242 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
243 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000244 }
245 }
246
247 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000248}
249
Chris Lattnerb48da392005-01-23 04:39:44 +0000250const TargetMachine &SelectionDAG::getTarget() const {
251 return TLI.getTargetMachine();
252}
253
Jim Laskey58b968b2005-08-17 20:08:02 +0000254//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000255// SDNode Profile Support
256//===----------------------------------------------------------------------===//
257
258/// getNodeIDOpcode - Return the opcode that has been set for this NodeID.
259///
260static unsigned getNodeIDOpcode(FoldingSetNodeID &ID) {
261 return ID.getRawData(0);
262}
Jim Laskeydef69b92006-10-27 23:52:51 +0000263
264/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
265///
Jim Laskey583bd472006-10-27 23:46:08 +0000266static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
267 ID.AddInteger(OpC);
268}
269
270/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
271/// solely with their pointer.
272void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
273 ID.AddPointer(VTList.VTs);
274}
275
Jim Laskeydef69b92006-10-27 23:52:51 +0000276/// AddNodeIDOperand - Add an operands data to the NodeID data.
277///
Jim Laskey583bd472006-10-27 23:46:08 +0000278static void AddNodeIDOperand(FoldingSetNodeID &ID, SDOperand Op) {
279 ID.AddPointer(Op.Val);
280 ID.AddInteger(Op.ResNo);
281}
282
Jim Laskeydef69b92006-10-27 23:52:51 +0000283/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
284///
Jim Laskey583bd472006-10-27 23:46:08 +0000285static void AddNodeIDOperands(FoldingSetNodeID &ID) {
286}
Jim Laskeydef69b92006-10-27 23:52:51 +0000287static void AddNodeIDOperands(FoldingSetNodeID &ID, SDOperand Op) {
Jim Laskey583bd472006-10-27 23:46:08 +0000288 AddNodeIDOperand(ID, Op);
289}
290static void AddNodeIDOperands(FoldingSetNodeID &ID,
291 SDOperand Op1, SDOperand Op2) {
292 AddNodeIDOperand(ID, Op1);
293 AddNodeIDOperand(ID, Op2);
294}
295static void AddNodeIDOperands(FoldingSetNodeID &ID,
296 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
297 AddNodeIDOperand(ID, Op1);
298 AddNodeIDOperand(ID, Op2);
299 AddNodeIDOperand(ID, Op3);
300}
301static void AddNodeIDOperands(FoldingSetNodeID &ID,
302 const SDOperand *Ops, unsigned NumOps) {
303 for (; NumOps; --NumOps, ++Ops)
304 AddNodeIDOperand(ID, *Ops);
305}
306
Jim Laskeydef69b92006-10-27 23:52:51 +0000307/// AddNodeIDOperands - Various routines for adding node info to the NodeID
308/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000309static void AddNodeIDNode(FoldingSetNodeID &ID,
310 unsigned short OpC, SDVTList VTList) {
311 AddNodeIDOpcode(ID, OpC);
312 AddNodeIDValueTypes(ID, VTList);
313 AddNodeIDOperands(ID);
314}
315static void AddNodeIDNode(FoldingSetNodeID &ID,
316 unsigned short OpC, SDVTList VTList,
317 SDOperand Op) {
318 AddNodeIDOpcode(ID, OpC);
319 AddNodeIDValueTypes(ID, VTList);
320 AddNodeIDOperands(ID, Op);
321}
322static void AddNodeIDNode(FoldingSetNodeID &ID,
323 unsigned short OpC, SDVTList VTList,
324 SDOperand Op1, SDOperand Op2) {
325 AddNodeIDOpcode(ID, OpC);
326 AddNodeIDValueTypes(ID, VTList);
327 AddNodeIDOperands(ID, Op1, Op2);
328}
329static void AddNodeIDNode(FoldingSetNodeID &ID,
330 unsigned short OpC, SDVTList VTList,
331 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
332 AddNodeIDOpcode(ID, OpC);
333 AddNodeIDValueTypes(ID, VTList);
Chris Lattner6fb6ef42006-10-28 06:15:26 +0000334 AddNodeIDOperands(ID, Op1, Op2, Op3);
Jim Laskey583bd472006-10-27 23:46:08 +0000335}
336static void AddNodeIDNode(FoldingSetNodeID &ID,
337 unsigned short OpC, SDVTList VTList,
338 const SDOperand *OpList, unsigned N) {
339 AddNodeIDOpcode(ID, OpC);
340 AddNodeIDValueTypes(ID, VTList);
341 AddNodeIDOperands(ID, OpList, N);
342}
343
Jim Laskeydef69b92006-10-27 23:52:51 +0000344/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
345/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000346static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
347 AddNodeIDOpcode(ID, N->getOpcode());
348 // Add the return value info.
349 AddNodeIDValueTypes(ID, N->getVTList());
350 // Add the operand info.
351 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
352
353 // Handle SDNode leafs with special info.
354 if (N->getNumOperands() == 0) {
355 switch (N->getOpcode()) {
356 default: break; // Normal nodes don't need extra info.
357 case ISD::TargetConstant:
358 case ISD::Constant:
359 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
360 break;
361 case ISD::TargetConstantFP:
362 case ISD::ConstantFP:
363 ID.AddDouble(cast<ConstantFPSDNode>(N)->getValue());
364 break;
365 case ISD::TargetGlobalAddress:
Jim Laskey1c6f01a2006-10-28 17:25:28 +0000366 case ISD::GlobalAddress: {
367 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
368 ID.AddPointer(GA->getGlobal());
369 ID.AddInteger(GA->getOffset());
Jim Laskey583bd472006-10-27 23:46:08 +0000370 break;
Jim Laskey1c6f01a2006-10-28 17:25:28 +0000371 }
Jim Laskey583bd472006-10-27 23:46:08 +0000372 case ISD::BasicBlock:
373 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
374 break;
375 case ISD::Register:
376 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
377 break;
Jim Laskey1c6f01a2006-10-28 17:25:28 +0000378 case ISD::SRCVALUE: {
379 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
380 ID.AddPointer(SV->getValue());
381 ID.AddInteger(SV->getOffset());
Jim Laskey583bd472006-10-27 23:46:08 +0000382 break;
Jim Laskey1c6f01a2006-10-28 17:25:28 +0000383 }
Jim Laskey583bd472006-10-27 23:46:08 +0000384 case ISD::FrameIndex:
385 case ISD::TargetFrameIndex:
386 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
387 break;
388 case ISD::JumpTable:
389 case ISD::TargetJumpTable:
390 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
391 break;
392 case ISD::ConstantPool:
Jim Laskey1c6f01a2006-10-28 17:25:28 +0000393 case ISD::TargetConstantPool: {
394 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
395 ID.AddInteger(CP->getAlignment());
396 ID.AddInteger(CP->getOffset());
397 if (CP->isMachineConstantPoolEntry())
398 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
Jim Laskey583bd472006-10-27 23:46:08 +0000399 else
Jim Laskey1c6f01a2006-10-28 17:25:28 +0000400 ID.AddPointer(CP->getConstVal());
Jim Laskey583bd472006-10-27 23:46:08 +0000401 break;
402 }
Jim Laskey1c6f01a2006-10-28 17:25:28 +0000403 case ISD::VLOAD:
404 case ISD::EXTLOAD:
405 case ISD::LOAD: {
406 LoadSDNode *LD = cast<LoadSDNode>(N);
407 ID.AddInteger(LD->getAddressingMode());
408 ID.AddInteger(LD->getExtensionType());
409 ID.AddInteger(LD->getLoadedVT());
410 ID.AddPointer(LD->getSrcValue());
411 ID.AddInteger(LD->getSrcValueOffset());
412 ID.AddInteger(LD->getAlignment());
413 ID.AddInteger(LD->isVolatile());
414 break;
415 }
416 case ISD::STORE: {
417 StoreSDNode *ST = cast<StoreSDNode>(N);
418 ID.AddInteger(ST->getAddressingMode());
419 ID.AddInteger(ST->isTruncatingStore());
420 ID.AddInteger(ST->getStoredVT());
421 ID.AddPointer(ST->getSrcValue());
422 ID.AddInteger(ST->getSrcValueOffset());
423 ID.AddInteger(ST->getAlignment());
424 ID.AddInteger(ST->isVolatile());
425 break;
426 }
427 }
Jim Laskey583bd472006-10-27 23:46:08 +0000428 }
429}
430
431//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000432// SelectionDAG Class
433//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000434
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000435/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000436/// SelectionDAG.
437void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000438 // Create a dummy node (which is not added to allnodes), that adds a reference
439 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000440 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000441
Chris Lattner190a4182006-08-04 17:45:20 +0000442 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000443
Chris Lattner190a4182006-08-04 17:45:20 +0000444 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000445 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000446 if (I->use_empty())
447 DeadNodes.push_back(I);
448
449 // Process the worklist, deleting the nodes and adding their uses to the
450 // worklist.
451 while (!DeadNodes.empty()) {
452 SDNode *N = DeadNodes.back();
453 DeadNodes.pop_back();
454
455 // Take the node out of the appropriate CSE map.
456 RemoveNodeFromCSEMaps(N);
457
458 // Next, brutally remove the operand list. This is safe to do, as there are
459 // no cycles in the graph.
460 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
461 SDNode *Operand = I->Val;
462 Operand->removeUser(N);
463
464 // Now that we removed this operand, see if there are no uses of it left.
465 if (Operand->use_empty())
466 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000467 }
Chris Lattner190a4182006-08-04 17:45:20 +0000468 delete[] N->OperandList;
469 N->OperandList = 0;
470 N->NumOperands = 0;
471
472 // Finally, remove N itself.
473 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000474 }
475
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000476 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000477 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000478}
479
Evan Cheng130a6472006-10-12 20:34:05 +0000480void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
481 SmallVector<SDNode*, 16> DeadNodes;
482 DeadNodes.push_back(N);
483
484 // Process the worklist, deleting the nodes and adding their uses to the
485 // worklist.
486 while (!DeadNodes.empty()) {
487 SDNode *N = DeadNodes.back();
488 DeadNodes.pop_back();
489
490 // Take the node out of the appropriate CSE map.
491 RemoveNodeFromCSEMaps(N);
492
493 // Next, brutally remove the operand list. This is safe to do, as there are
494 // no cycles in the graph.
495 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
496 SDNode *Operand = I->Val;
497 Operand->removeUser(N);
498
499 // Now that we removed this operand, see if there are no uses of it left.
500 if (Operand->use_empty())
501 DeadNodes.push_back(Operand);
502 }
503 delete[] N->OperandList;
504 N->OperandList = 0;
505 N->NumOperands = 0;
506
507 // Finally, remove N itself.
508 Deleted.push_back(N);
509 AllNodes.erase(N);
510 }
511}
512
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000513void SelectionDAG::DeleteNode(SDNode *N) {
514 assert(N->use_empty() && "Cannot delete a node that is not dead!");
515
516 // First take this out of the appropriate CSE map.
517 RemoveNodeFromCSEMaps(N);
518
Chris Lattner1e111c72005-09-07 05:37:01 +0000519 // Finally, remove uses due to operands of this node, remove from the
520 // AllNodes list, and delete the node.
521 DeleteNodeNotInCSEMaps(N);
522}
523
524void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
525
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000526 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000527 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000528
529 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000530 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
531 I->Val->removeUser(N);
532 delete[] N->OperandList;
533 N->OperandList = 0;
534 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000535
536 delete N;
537}
538
Chris Lattner149c58c2005-08-16 18:17:10 +0000539/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
540/// correspond to it. This is useful when we're about to delete or repurpose
541/// the node. We don't want future request for structurally identical nodes
542/// to return N anymore.
543void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000544 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000545 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000546 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000547 case ISD::STRING:
548 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
549 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000550 case ISD::CONDCODE:
551 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
552 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000553 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000554 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
555 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000556 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000557 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000558 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000559 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000560 Erased =
561 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000562 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000563 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000564 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000565 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
566 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000567 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000568 // Remove it from the CSE Map.
569 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000570 break;
571 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000572#ifndef NDEBUG
573 // Verify that the node was actually in one of the CSE maps, unless it has a
574 // flag result (which cannot be CSE'd) or is one of the special cases that are
575 // not subject to CSE.
576 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000577 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000578 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000579 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000580 assert(0 && "Node is not in map!");
581 }
582#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000583}
584
Chris Lattner8b8749f2005-08-17 19:00:20 +0000585/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
586/// has been taken out and modified in some way. If the specified node already
587/// exists in the CSE maps, do not modify the maps, but return the existing node
588/// instead. If it doesn't exist, add it and return null.
589///
590SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
591 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000592 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000593 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000594
Chris Lattner9f8cc692005-12-19 22:21:21 +0000595 // Check that remaining values produced are not flags.
596 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
597 if (N->getValueType(i) == MVT::Flag)
598 return 0; // Never CSE anything that produces a flag.
599
Chris Lattnera5682852006-08-07 23:03:03 +0000600 SDNode *New = CSEMap.GetOrInsertNode(N);
601 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000602 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000603}
604
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000605/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
606/// were replaced with those specified. If this node is never memoized,
607/// return null, otherwise return a pointer to the slot it would take. If a
608/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000609SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
610 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000611 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000612 return 0; // Never add these nodes.
613
614 // Check that remaining values produced are not flags.
615 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
616 if (N->getValueType(i) == MVT::Flag)
617 return 0; // Never CSE anything that produces a flag.
618
Jim Laskey583bd472006-10-27 23:46:08 +0000619 FoldingSetNodeID ID;
620 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Op);
Chris Lattnera5682852006-08-07 23:03:03 +0000621 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000622}
623
624/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
625/// were replaced with those specified. If this node is never memoized,
626/// return null, otherwise return a pointer to the slot it would take. If a
627/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000628SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
629 SDOperand Op1, SDOperand Op2,
630 void *&InsertPos) {
631 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
632 return 0; // Never add these nodes.
633
634 // Check that remaining values produced are not flags.
635 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
636 if (N->getValueType(i) == MVT::Flag)
637 return 0; // Never CSE anything that produces a flag.
638
Jim Laskey583bd472006-10-27 23:46:08 +0000639 FoldingSetNodeID ID;
640 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +0000641 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
642}
643
644
645/// 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.
649SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000650 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000651 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000652 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000653 return 0; // Never add these nodes.
654
655 // Check that remaining values produced are not flags.
656 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
657 if (N->getValueType(i) == MVT::Flag)
658 return 0; // Never CSE anything that produces a flag.
659
Jim Laskey583bd472006-10-27 23:46:08 +0000660 FoldingSetNodeID ID;
661 AddNodeIDNode(ID, N->getOpcode(), N->getVTList());
662
Evan Cheng9629aba2006-10-11 01:47:58 +0000663 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
664 ID.AddInteger(LD->getAddressingMode());
665 ID.AddInteger(LD->getExtensionType());
Evan Cheng2e49f092006-10-11 07:10:22 +0000666 ID.AddInteger(LD->getLoadedVT());
Evan Cheng9629aba2006-10-11 01:47:58 +0000667 ID.AddPointer(LD->getSrcValue());
668 ID.AddInteger(LD->getSrcValueOffset());
669 ID.AddInteger(LD->getAlignment());
670 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000671 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
672 ID.AddInteger(ST->getAddressingMode());
673 ID.AddInteger(ST->isTruncatingStore());
674 ID.AddInteger(ST->getStoredVT());
675 ID.AddPointer(ST->getSrcValue());
676 ID.AddInteger(ST->getSrcValueOffset());
677 ID.AddInteger(ST->getAlignment());
678 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000679 }
Jim Laskey583bd472006-10-27 23:46:08 +0000680
681 AddNodeIDOperands(ID, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000682 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000683}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000684
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000685
Chris Lattner78ec3112003-08-11 14:57:33 +0000686SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000687 while (!AllNodes.empty()) {
688 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000689 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000690 delete [] N->OperandList;
691 N->OperandList = 0;
692 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000693 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000694 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000695}
696
Chris Lattner0f2287b2005-04-13 02:38:18 +0000697SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000698 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000699 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000700 return getNode(ISD::AND, Op.getValueType(), Op,
701 getConstant(Imm, Op.getValueType()));
702}
703
Chris Lattner36ce6912005-11-29 06:21:05 +0000704SDOperand SelectionDAG::getString(const std::string &Val) {
705 StringSDNode *&N = StringNodes[Val];
706 if (!N) {
707 N = new StringSDNode(Val);
708 AllNodes.push_back(N);
709 }
710 return SDOperand(N, 0);
711}
712
Chris Lattner61b09412006-08-11 21:01:22 +0000713SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000714 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000715 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000716
Chris Lattner61b09412006-08-11 21:01:22 +0000717 // Mask out any bits that are not valid for this constant.
718 Val &= MVT::getIntVTBitMask(VT);
719
720 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000721 FoldingSetNodeID ID;
722 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000723 ID.AddInteger(Val);
724 void *IP = 0;
725 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
726 return SDOperand(E, 0);
727 SDNode *N = new ConstantSDNode(isT, Val, VT);
728 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000729 AllNodes.push_back(N);
730 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000731}
732
Chris Lattner61b09412006-08-11 21:01:22 +0000733
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000734SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
735 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000736 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
737 if (VT == MVT::f32)
738 Val = (float)Val; // Mask out extra precision.
739
Chris Lattnerd8658612005-02-17 20:17:32 +0000740 // Do the map lookup using the actual bit pattern for the floating point
741 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
742 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000743 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000744 FoldingSetNodeID ID;
745 AddNodeIDNode(ID, Opc, getVTList(VT));
746 ID.AddDouble(Val);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000747 void *IP = 0;
748 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
749 return SDOperand(E, 0);
750 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
751 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000752 AllNodes.push_back(N);
753 return SDOperand(N, 0);
754}
755
Chris Lattnerc3aae252005-01-07 07:46:32 +0000756SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000757 MVT::ValueType VT, int Offset,
758 bool isTargetGA) {
759 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000760 FoldingSetNodeID ID;
761 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000762 ID.AddPointer(GV);
763 ID.AddInteger(Offset);
764 void *IP = 0;
765 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
766 return SDOperand(E, 0);
767 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
768 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000769 AllNodes.push_back(N);
770 return SDOperand(N, 0);
771}
772
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000773SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
774 bool isTarget) {
775 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000776 FoldingSetNodeID ID;
777 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000778 ID.AddInteger(FI);
779 void *IP = 0;
780 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
781 return SDOperand(E, 0);
782 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
783 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000784 AllNodes.push_back(N);
785 return SDOperand(N, 0);
786}
787
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000788SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
789 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000790 FoldingSetNodeID ID;
791 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000792 ID.AddInteger(JTI);
793 void *IP = 0;
794 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
795 return SDOperand(E, 0);
796 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
797 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000798 AllNodes.push_back(N);
799 return SDOperand(N, 0);
800}
801
Evan Chengb8973bd2006-01-31 22:23:14 +0000802SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000803 unsigned Alignment, int Offset,
804 bool isTarget) {
805 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000806 FoldingSetNodeID ID;
807 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000808 ID.AddInteger(Alignment);
809 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000810 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000811 void *IP = 0;
812 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
813 return SDOperand(E, 0);
814 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
815 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000816 AllNodes.push_back(N);
817 return SDOperand(N, 0);
818}
819
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820
Evan Chengd6594ae2006-09-12 21:00:35 +0000821SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
822 MVT::ValueType VT,
823 unsigned Alignment, int Offset,
824 bool isTarget) {
825 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000826 FoldingSetNodeID ID;
827 AddNodeIDNode(ID, Opc, getVTList(VT));
Evan Chengd6594ae2006-09-12 21:00:35 +0000828 ID.AddInteger(Alignment);
829 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000830 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000831 void *IP = 0;
832 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
833 return SDOperand(E, 0);
834 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
835 CSEMap.InsertNode(N, IP);
836 AllNodes.push_back(N);
837 return SDOperand(N, 0);
838}
839
840
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000842 FoldingSetNodeID ID;
843 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000844 ID.AddPointer(MBB);
845 void *IP = 0;
846 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
847 return SDOperand(E, 0);
848 SDNode *N = new BasicBlockSDNode(MBB);
849 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000850 AllNodes.push_back(N);
851 return SDOperand(N, 0);
852}
853
Chris Lattner15e4b012005-07-10 00:07:11 +0000854SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
855 if ((unsigned)VT >= ValueTypeNodes.size())
856 ValueTypeNodes.resize(VT+1);
857 if (ValueTypeNodes[VT] == 0) {
858 ValueTypeNodes[VT] = new VTSDNode(VT);
859 AllNodes.push_back(ValueTypeNodes[VT]);
860 }
861
862 return SDOperand(ValueTypeNodes[VT], 0);
863}
864
Chris Lattnerc3aae252005-01-07 07:46:32 +0000865SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
866 SDNode *&N = ExternalSymbols[Sym];
867 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000868 N = new ExternalSymbolSDNode(false, Sym, VT);
869 AllNodes.push_back(N);
870 return SDOperand(N, 0);
871}
872
Chris Lattner809ec112006-01-28 10:09:25 +0000873SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
874 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000875 SDNode *&N = TargetExternalSymbols[Sym];
876 if (N) return SDOperand(N, 0);
877 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000878 AllNodes.push_back(N);
879 return SDOperand(N, 0);
880}
881
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000882SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
883 if ((unsigned)Cond >= CondCodeNodes.size())
884 CondCodeNodes.resize(Cond+1);
885
Chris Lattner079a27a2005-08-09 20:40:02 +0000886 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000887 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000888 AllNodes.push_back(CondCodeNodes[Cond]);
889 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000890 return SDOperand(CondCodeNodes[Cond], 0);
891}
892
Chris Lattner0fdd7682005-08-30 22:38:38 +0000893SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000894 FoldingSetNodeID ID;
895 AddNodeIDNode(ID, ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000896 ID.AddInteger(RegNo);
897 void *IP = 0;
898 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
899 return SDOperand(E, 0);
900 SDNode *N = new RegisterSDNode(RegNo, VT);
901 CSEMap.InsertNode(N, IP);
902 AllNodes.push_back(N);
903 return SDOperand(N, 0);
904}
905
906SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
907 assert((!V || isa<PointerType>(V->getType())) &&
908 "SrcValue is not a pointer?");
909
Jim Laskey583bd472006-10-27 23:46:08 +0000910 FoldingSetNodeID ID;
911 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000912 ID.AddPointer(V);
913 ID.AddInteger(Offset);
914 void *IP = 0;
915 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
916 return SDOperand(E, 0);
917 SDNode *N = new SrcValueSDNode(V, Offset);
918 CSEMap.InsertNode(N, IP);
919 AllNodes.push_back(N);
920 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000921}
922
Chris Lattner51dabfb2006-10-14 00:41:01 +0000923SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
924 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000925 // These setcc operations always fold.
926 switch (Cond) {
927 default: break;
928 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000929 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000930 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000931 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000932
933 case ISD::SETOEQ:
934 case ISD::SETOGT:
935 case ISD::SETOGE:
936 case ISD::SETOLT:
937 case ISD::SETOLE:
938 case ISD::SETONE:
939 case ISD::SETO:
940 case ISD::SETUO:
941 case ISD::SETUEQ:
942 case ISD::SETUNE:
943 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
944 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000946
Chris Lattner67255a12005-04-07 18:14:58 +0000947 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
948 uint64_t C2 = N2C->getValue();
949 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
950 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000951
Chris Lattnerc3aae252005-01-07 07:46:32 +0000952 // Sign extend the operands if required
953 if (ISD::isSignedIntSetCC(Cond)) {
954 C1 = N1C->getSignExtended();
955 C2 = N2C->getSignExtended();
956 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000957
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 switch (Cond) {
959 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000960 case ISD::SETEQ: return getConstant(C1 == C2, VT);
961 case ISD::SETNE: return getConstant(C1 != C2, VT);
962 case ISD::SETULT: return getConstant(C1 < C2, VT);
963 case ISD::SETUGT: return getConstant(C1 > C2, VT);
964 case ISD::SETULE: return getConstant(C1 <= C2, VT);
965 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
966 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
967 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
968 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
969 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000970 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 }
Chris Lattner67255a12005-04-07 18:14:58 +0000972 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000973 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
974 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
975 double C1 = N1C->getValue(), C2 = N2C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000976
Chris Lattnerc3aae252005-01-07 07:46:32 +0000977 switch (Cond) {
978 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000979 case ISD::SETEQ: return getConstant(C1 == C2, VT);
980 case ISD::SETNE: return getConstant(C1 != C2, VT);
981 case ISD::SETLT: return getConstant(C1 < C2, VT);
982 case ISD::SETGT: return getConstant(C1 > C2, VT);
983 case ISD::SETLE: return getConstant(C1 <= C2, VT);
984 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000985 }
986 } else {
987 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000988 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000990
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000991 // Could not fold it.
992 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000993}
994
Chris Lattner51dabfb2006-10-14 00:41:01 +0000995
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000997///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000998SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000999 FoldingSetNodeID ID;
1000 AddNodeIDNode(ID, Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001001 void *IP = 0;
1002 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1003 return SDOperand(E, 0);
1004 SDNode *N = new SDNode(Opcode, VT);
1005 CSEMap.InsertNode(N, IP);
1006
1007 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001008 return SDOperand(N, 0);
1009}
1010
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1012 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001013 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001014 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001015 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1016 uint64_t Val = C->getValue();
1017 switch (Opcode) {
1018 default: break;
1019 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001020 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001021 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1022 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001023 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1024 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001025 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001026 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001027 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001028 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001029 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001030 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001031 case ISD::BSWAP:
1032 switch(VT) {
1033 default: assert(0 && "Invalid bswap!"); break;
1034 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1035 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1036 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1037 }
1038 break;
1039 case ISD::CTPOP:
1040 switch(VT) {
1041 default: assert(0 && "Invalid ctpop!"); break;
1042 case MVT::i1: return getConstant(Val != 0, VT);
1043 case MVT::i8:
1044 Tmp1 = (unsigned)Val & 0xFF;
1045 return getConstant(CountPopulation_32(Tmp1), VT);
1046 case MVT::i16:
1047 Tmp1 = (unsigned)Val & 0xFFFF;
1048 return getConstant(CountPopulation_32(Tmp1), VT);
1049 case MVT::i32:
1050 return getConstant(CountPopulation_32((unsigned)Val), VT);
1051 case MVT::i64:
1052 return getConstant(CountPopulation_64(Val), VT);
1053 }
1054 case ISD::CTLZ:
1055 switch(VT) {
1056 default: assert(0 && "Invalid ctlz!"); break;
1057 case MVT::i1: return getConstant(Val == 0, VT);
1058 case MVT::i8:
1059 Tmp1 = (unsigned)Val & 0xFF;
1060 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1061 case MVT::i16:
1062 Tmp1 = (unsigned)Val & 0xFFFF;
1063 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1064 case MVT::i32:
1065 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1066 case MVT::i64:
1067 return getConstant(CountLeadingZeros_64(Val), VT);
1068 }
1069 case ISD::CTTZ:
1070 switch(VT) {
1071 default: assert(0 && "Invalid cttz!"); break;
1072 case MVT::i1: return getConstant(Val == 0, VT);
1073 case MVT::i8:
1074 Tmp1 = (unsigned)Val | 0x100;
1075 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1076 case MVT::i16:
1077 Tmp1 = (unsigned)Val | 0x10000;
1078 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1079 case MVT::i32:
1080 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1081 case MVT::i64:
1082 return getConstant(CountTrailingZeros_64(Val), VT);
1083 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001084 }
1085 }
1086
Chris Lattner94683772005-12-23 05:30:37 +00001087 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1089 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001090 case ISD::FNEG:
1091 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001092 case ISD::FABS:
1093 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 case ISD::FP_ROUND:
1095 case ISD::FP_EXTEND:
1096 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001097 case ISD::FP_TO_SINT:
1098 return getConstant((int64_t)C->getValue(), VT);
1099 case ISD::FP_TO_UINT:
1100 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001101 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001102 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001103 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001104 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001105 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001106 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001107 }
1108
1109 unsigned OpOpcode = Operand.Val->getOpcode();
1110 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001111 case ISD::TokenFactor:
1112 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001113 case ISD::SIGN_EXTEND:
1114 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001115 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001116 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1117 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1118 break;
1119 case ISD::ZERO_EXTEND:
1120 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001121 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001122 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001123 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001125 case ISD::ANY_EXTEND:
1126 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001127 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001128 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1129 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1130 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1131 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001132 case ISD::TRUNCATE:
1133 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001134 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001135 if (OpOpcode == ISD::TRUNCATE)
1136 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001137 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1138 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001139 // If the source is smaller than the dest, we still need an extend.
1140 if (Operand.Val->getOperand(0).getValueType() < VT)
1141 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1142 else if (Operand.Val->getOperand(0).getValueType() > VT)
1143 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1144 else
1145 return Operand.Val->getOperand(0);
1146 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 break;
Chris Lattner35481892005-12-23 00:16:34 +00001148 case ISD::BIT_CONVERT:
1149 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001150 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001151 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001152 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001153 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1154 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001155 if (OpOpcode == ISD::UNDEF)
1156 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001157 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001158 case ISD::SCALAR_TO_VECTOR:
1159 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1160 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1161 "Illegal SCALAR_TO_VECTOR node!");
1162 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001163 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001164 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1165 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001166 Operand.Val->getOperand(0));
1167 if (OpOpcode == ISD::FNEG) // --X -> X
1168 return Operand.Val->getOperand(0);
1169 break;
1170 case ISD::FABS:
1171 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1172 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1173 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001174 }
1175
Chris Lattner43247a12005-08-25 19:12:10 +00001176 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001177 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001178 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001179 FoldingSetNodeID ID;
1180 AddNodeIDNode(ID, Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001181 void *IP = 0;
1182 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1183 return SDOperand(E, 0);
1184 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001185 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001186 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001187 } else {
1188 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001189 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001190 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001191 AllNodes.push_back(N);
1192 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001193}
1194
Chris Lattner36019aa2005-04-18 03:48:41 +00001195
1196
Chris Lattnerc3aae252005-01-07 07:46:32 +00001197SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1198 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001199#ifndef NDEBUG
1200 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001201 case ISD::TokenFactor:
1202 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1203 N2.getValueType() == MVT::Other && "Invalid token factor!");
1204 break;
Chris Lattner76365122005-01-16 02:23:22 +00001205 case ISD::AND:
1206 case ISD::OR:
1207 case ISD::XOR:
1208 case ISD::UDIV:
1209 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001210 case ISD::MULHU:
1211 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001212 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1213 // fall through
1214 case ISD::ADD:
1215 case ISD::SUB:
1216 case ISD::MUL:
1217 case ISD::SDIV:
1218 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001219 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1220 // fall through.
1221 case ISD::FADD:
1222 case ISD::FSUB:
1223 case ISD::FMUL:
1224 case ISD::FDIV:
1225 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001226 assert(N1.getValueType() == N2.getValueType() &&
1227 N1.getValueType() == VT && "Binary operator types must match!");
1228 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001229 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1230 assert(N1.getValueType() == VT &&
1231 MVT::isFloatingPoint(N1.getValueType()) &&
1232 MVT::isFloatingPoint(N2.getValueType()) &&
1233 "Invalid FCOPYSIGN!");
1234 break;
Chris Lattner76365122005-01-16 02:23:22 +00001235 case ISD::SHL:
1236 case ISD::SRA:
1237 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001238 case ISD::ROTL:
1239 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001240 assert(VT == N1.getValueType() &&
1241 "Shift operators return type must be the same as their first arg");
1242 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001243 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001244 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001245 case ISD::FP_ROUND_INREG: {
1246 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1247 assert(VT == N1.getValueType() && "Not an inreg round!");
1248 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1249 "Cannot FP_ROUND_INREG integer types");
1250 assert(EVT <= VT && "Not rounding down!");
1251 break;
1252 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001253 case ISD::AssertSext:
1254 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001255 case ISD::SIGN_EXTEND_INREG: {
1256 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1257 assert(VT == N1.getValueType() && "Not an inreg extend!");
1258 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1259 "Cannot *_EXTEND_INREG FP types");
1260 assert(EVT <= VT && "Not extending!");
1261 }
1262
Chris Lattner76365122005-01-16 02:23:22 +00001263 default: break;
1264 }
1265#endif
1266
Chris Lattnerc3aae252005-01-07 07:46:32 +00001267 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1268 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1269 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001270 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1271 int64_t Val = N1C->getValue();
1272 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1273 Val <<= 64-FromBits;
1274 Val >>= 64-FromBits;
1275 return getConstant(Val, VT);
1276 }
1277
Chris Lattnerc3aae252005-01-07 07:46:32 +00001278 if (N2C) {
1279 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1280 switch (Opcode) {
1281 case ISD::ADD: return getConstant(C1 + C2, VT);
1282 case ISD::SUB: return getConstant(C1 - C2, VT);
1283 case ISD::MUL: return getConstant(C1 * C2, VT);
1284 case ISD::UDIV:
1285 if (C2) return getConstant(C1 / C2, VT);
1286 break;
1287 case ISD::UREM :
1288 if (C2) return getConstant(C1 % C2, VT);
1289 break;
1290 case ISD::SDIV :
1291 if (C2) return getConstant(N1C->getSignExtended() /
1292 N2C->getSignExtended(), VT);
1293 break;
1294 case ISD::SREM :
1295 if (C2) return getConstant(N1C->getSignExtended() %
1296 N2C->getSignExtended(), VT);
1297 break;
1298 case ISD::AND : return getConstant(C1 & C2, VT);
1299 case ISD::OR : return getConstant(C1 | C2, VT);
1300 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001301 case ISD::SHL : return getConstant(C1 << C2, VT);
1302 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001303 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001304 case ISD::ROTL :
1305 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1306 VT);
1307 case ISD::ROTR :
1308 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1309 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001310 default: break;
1311 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001312 } else { // Cannonicalize constant to RHS if commutative
1313 if (isCommutativeBinOp(Opcode)) {
1314 std::swap(N1C, N2C);
1315 std::swap(N1, N2);
1316 }
1317 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001318 }
1319
1320 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1321 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001322 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001323 if (N2CFP) {
1324 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1325 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001326 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1327 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1328 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1329 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001330 if (C2) return getConstantFP(C1 / C2, VT);
1331 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001332 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001333 if (C2) return getConstantFP(fmod(C1, C2), VT);
1334 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001335 case ISD::FCOPYSIGN: {
1336 union {
1337 double F;
1338 uint64_t I;
1339 } u1;
1340 union {
1341 double F;
1342 int64_t I;
1343 } u2;
1344 u1.F = C1;
1345 u2.F = C2;
1346 if (u2.I < 0) // Sign bit of RHS set?
1347 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1348 else
1349 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1350 return getConstantFP(u1.F, VT);
1351 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001352 default: break;
1353 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001354 } else { // Cannonicalize constant to RHS if commutative
1355 if (isCommutativeBinOp(Opcode)) {
1356 std::swap(N1CFP, N2CFP);
1357 std::swap(N1, N2);
1358 }
1359 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001360 }
Chris Lattner62b57722006-04-20 05:39:12 +00001361
1362 // Canonicalize an UNDEF to the RHS, even over a constant.
1363 if (N1.getOpcode() == ISD::UNDEF) {
1364 if (isCommutativeBinOp(Opcode)) {
1365 std::swap(N1, N2);
1366 } else {
1367 switch (Opcode) {
1368 case ISD::FP_ROUND_INREG:
1369 case ISD::SIGN_EXTEND_INREG:
1370 case ISD::SUB:
1371 case ISD::FSUB:
1372 case ISD::FDIV:
1373 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001374 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001375 return N1; // fold op(undef, arg2) -> undef
1376 case ISD::UDIV:
1377 case ISD::SDIV:
1378 case ISD::UREM:
1379 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001380 case ISD::SRL:
1381 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001382 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1383 }
1384 }
1385 }
1386
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001387 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001388 if (N2.getOpcode() == ISD::UNDEF) {
1389 switch (Opcode) {
1390 case ISD::ADD:
1391 case ISD::SUB:
1392 case ISD::FADD:
1393 case ISD::FSUB:
1394 case ISD::FMUL:
1395 case ISD::FDIV:
1396 case ISD::FREM:
1397 case ISD::UDIV:
1398 case ISD::SDIV:
1399 case ISD::UREM:
1400 case ISD::SREM:
1401 case ISD::XOR:
1402 return N2; // fold op(arg1, undef) -> undef
1403 case ISD::MUL:
1404 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001405 case ISD::SRL:
1406 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001407 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1408 case ISD::OR:
1409 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001410 case ISD::SRA:
1411 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001412 }
1413 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001414
Chris Lattner51dabfb2006-10-14 00:41:01 +00001415 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001416 switch (Opcode) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001417 case ISD::AND:
1418 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1419 // worth handling here.
1420 if (N2C && N2C->getValue() == 0)
1421 return N2;
1422 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00001423 case ISD::OR:
1424 case ISD::XOR:
1425 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1426 // worth handling here.
1427 if (N2C && N2C->getValue() == 0)
1428 return N1;
1429 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001430 case ISD::FP_ROUND_INREG:
1431 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1432 break;
1433 case ISD::SIGN_EXTEND_INREG: {
1434 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1435 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001436 break;
1437 }
Chris Lattner5c6621c2006-09-19 04:51:23 +00001438 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00001439 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1440
Chris Lattner5c6621c2006-09-19 04:51:23 +00001441 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1442 // 64-bit integers into 32-bit parts. Instead of building the extract of
1443 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00001444 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00001445 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00001446
1447 // EXTRACT_ELEMENT of a constant int is also very common.
1448 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1449 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1450 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00001451 }
1452 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001453
Nate Begemaneea805e2005-04-13 21:23:31 +00001454 // FIXME: figure out how to safely handle things like
1455 // int foo(int x) { return 1 << (x & 255); }
1456 // int bar() { return foo(256); }
1457#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001458 case ISD::SHL:
1459 case ISD::SRL:
1460 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001461 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001462 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001463 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001464 else if (N2.getOpcode() == ISD::AND)
1465 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1466 // If the and is only masking out bits that cannot effect the shift,
1467 // eliminate the and.
1468 unsigned NumBits = MVT::getSizeInBits(VT);
1469 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1470 return getNode(Opcode, VT, N1, N2.getOperand(0));
1471 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001472 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001473#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001474 }
1475
Chris Lattner27e9b412005-05-11 18:57:39 +00001476 // Memoize this node if possible.
1477 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001478 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001479 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001480 FoldingSetNodeID ID;
1481 AddNodeIDNode(ID, Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001482 void *IP = 0;
1483 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1484 return SDOperand(E, 0);
1485 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001486 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001487 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001488 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001489 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001490 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001491 }
1492
Chris Lattnerc3aae252005-01-07 07:46:32 +00001493 AllNodes.push_back(N);
1494 return SDOperand(N, 0);
1495}
1496
Chris Lattnerc3aae252005-01-07 07:46:32 +00001497SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1498 SDOperand N1, SDOperand N2, SDOperand N3) {
1499 // Perform various simplifications.
1500 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1501 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001502 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001503 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001504 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001505 // Use FoldSetCC to simplify SETCC's.
1506 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001507 if (Simp.Val) return Simp;
1508 break;
1509 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001510 case ISD::SELECT:
1511 if (N1C)
1512 if (N1C->getValue())
1513 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001514 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001515 return N3; // select false, X, Y -> Y
1516
1517 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001518 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001519 case ISD::BRCOND:
1520 if (N2C)
1521 if (N2C->getValue()) // Unconditional branch
1522 return getNode(ISD::BR, MVT::Other, N1, N3);
1523 else
1524 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001525 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001526 case ISD::VECTOR_SHUFFLE:
1527 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1528 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1529 N3.getOpcode() == ISD::BUILD_VECTOR &&
1530 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1531 "Illegal VECTOR_SHUFFLE node!");
1532 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001533 }
1534
Chris Lattner43247a12005-08-25 19:12:10 +00001535 // Memoize node if it doesn't produce a flag.
1536 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001537 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001538 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001539 FoldingSetNodeID ID;
1540 AddNodeIDNode(ID, Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001541 void *IP = 0;
1542 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1543 return SDOperand(E, 0);
1544 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001545 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001546 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001547 } else {
1548 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001549 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001550 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001551 AllNodes.push_back(N);
1552 return SDOperand(N, 0);
1553}
1554
1555SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001556 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001557 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001558 SDOperand Ops[] = { N1, N2, N3, N4 };
1559 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001560}
1561
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001562SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1563 SDOperand N1, SDOperand N2, SDOperand N3,
1564 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001565 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1566 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001567}
1568
Evan Cheng7038daf2005-12-10 00:37:58 +00001569SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1570 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00001571 const Value *SV, int SVOffset,
1572 bool isVolatile) {
1573 // FIXME: Alignment == 1 for now.
1574 unsigned Alignment = 1;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001575 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001576 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jim Laskey583bd472006-10-27 23:46:08 +00001577 FoldingSetNodeID ID;
1578 AddNodeIDNode(ID, ISD::LOAD, VTs, Chain, Ptr, Undef);
Evan Cheng466685d2006-10-09 20:57:25 +00001579 ID.AddInteger(ISD::UNINDEXED);
1580 ID.AddInteger(ISD::NON_EXTLOAD);
1581 ID.AddInteger(VT);
1582 ID.AddPointer(SV);
1583 ID.AddInteger(SVOffset);
1584 ID.AddInteger(Alignment);
1585 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001586 void *IP = 0;
1587 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1588 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001589 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED,
1590 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
1591 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00001592 N->setValueTypes(VTs);
1593 CSEMap.InsertNode(N, IP);
1594 AllNodes.push_back(N);
1595 return SDOperand(N, 0);
1596}
1597
1598SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1599 SDOperand Chain, SDOperand Ptr, const Value *SV,
1600 int SVOffset, MVT::ValueType EVT,
1601 bool isVolatile) {
1602 // If they are asking for an extending load from/to the same thing, return a
1603 // normal load.
1604 if (VT == EVT)
1605 ExtType = ISD::NON_EXTLOAD;
1606
1607 if (MVT::isVector(VT))
1608 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1609 else
1610 assert(EVT < VT && "Should only be an extending load, not truncating!");
1611 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1612 "Cannot sign/zero extend a FP/Vector load!");
1613 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1614 "Cannot convert from FP to Int or Int -> FP!");
1615
1616 // FIXME: Alignment == 1 for now.
1617 unsigned Alignment = 1;
1618 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001619 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jim Laskey583bd472006-10-27 23:46:08 +00001620 FoldingSetNodeID ID;
1621 AddNodeIDNode(ID, ISD::LOAD, VTs, Chain, Ptr, Undef);
Evan Cheng466685d2006-10-09 20:57:25 +00001622 ID.AddInteger(ISD::UNINDEXED);
1623 ID.AddInteger(ExtType);
1624 ID.AddInteger(EVT);
1625 ID.AddPointer(SV);
1626 ID.AddInteger(SVOffset);
1627 ID.AddInteger(Alignment);
1628 ID.AddInteger(isVolatile);
1629 void *IP = 0;
1630 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1631 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001632 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED, ExtType, EVT,
1633 SV, SVOffset, Alignment, isVolatile);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001634 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001635 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001636 AllNodes.push_back(N);
1637 return SDOperand(N, 0);
1638}
1639
Evan Cheng5270cf12006-10-26 21:53:40 +00001640SDOperand SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
1641 SDOperand Offset, ISD::MemOpAddrMode AM){
Evan Cheng2caccca2006-10-17 21:14:32 +00001642 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00001643 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
1644 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00001645 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00001646 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Jim Laskey583bd472006-10-27 23:46:08 +00001647 FoldingSetNodeID ID;
1648 AddNodeIDNode(ID, ISD::LOAD, VTs, LD->getChain(), Base, Offset);
Evan Cheng2caccca2006-10-17 21:14:32 +00001649 ID.AddInteger(AM);
1650 ID.AddInteger(LD->getExtensionType());
1651 ID.AddInteger(LD->getLoadedVT());
1652 ID.AddPointer(LD->getSrcValue());
1653 ID.AddInteger(LD->getSrcValueOffset());
1654 ID.AddInteger(LD->getAlignment());
1655 ID.AddInteger(LD->isVolatile());
1656 void *IP = 0;
1657 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1658 return SDOperand(E, 0);
Evan Cheng5270cf12006-10-26 21:53:40 +00001659 SDNode *N = new LoadSDNode(LD->getChain(), Base, Offset, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00001660 LD->getExtensionType(), LD->getLoadedVT(),
1661 LD->getSrcValue(), LD->getSrcValueOffset(),
1662 LD->getAlignment(), LD->isVolatile());
1663 N->setValueTypes(VTs);
1664 CSEMap.InsertNode(N, IP);
1665 AllNodes.push_back(N);
1666 return SDOperand(N, 0);
1667}
1668
Evan Cheng7038daf2005-12-10 00:37:58 +00001669SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1670 SDOperand Chain, SDOperand Ptr,
1671 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001672 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1673 getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001674 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001675}
1676
Evan Chengad071e12006-10-05 22:57:11 +00001677SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001678 SDOperand Ptr, const Value *SV, int SVOffset,
1679 bool isVolatile) {
1680 MVT::ValueType VT = Value.getValueType();
1681
1682 // FIXME: Alignment == 1 for now.
1683 unsigned Alignment = 1;
Evan Chengad071e12006-10-05 22:57:11 +00001684 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001685 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1686 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001687 FoldingSetNodeID ID;
1688 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001689 ID.AddInteger(ISD::UNINDEXED);
1690 ID.AddInteger(false);
1691 ID.AddInteger(VT);
1692 ID.AddPointer(SV);
1693 ID.AddInteger(SVOffset);
1694 ID.AddInteger(Alignment);
1695 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001696 void *IP = 0;
1697 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1698 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001699 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, false,
1700 VT, SV, SVOffset, Alignment, isVolatile);
1701 N->setValueTypes(VTs);
1702 CSEMap.InsertNode(N, IP);
1703 AllNodes.push_back(N);
1704 return SDOperand(N, 0);
1705}
1706
1707SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Value,
1708 SDOperand Ptr, const Value *SV,
1709 int SVOffset, MVT::ValueType SVT,
1710 bool isVolatile) {
1711 MVT::ValueType VT = Value.getValueType();
1712 bool isTrunc = VT != SVT;
1713
1714 assert(VT > SVT && "Not a truncation?");
1715 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
1716 "Can't do FP-INT conversion!");
1717
1718 // FIXME: Alignment == 1 for now.
1719 unsigned Alignment = 1;
1720 SDVTList VTs = getVTList(MVT::Other);
1721 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1722 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001723 FoldingSetNodeID ID;
1724 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001725 ID.AddInteger(ISD::UNINDEXED);
1726 ID.AddInteger(isTrunc);
1727 ID.AddInteger(SVT);
1728 ID.AddPointer(SV);
1729 ID.AddInteger(SVOffset);
1730 ID.AddInteger(Alignment);
1731 ID.AddInteger(isVolatile);
1732 void *IP = 0;
1733 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1734 return SDOperand(E, 0);
1735 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, isTrunc,
1736 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001737 N->setValueTypes(VTs);
1738 CSEMap.InsertNode(N, IP);
1739 AllNodes.push_back(N);
1740 return SDOperand(N, 0);
1741}
1742
Nate Begemanacc398c2006-01-25 18:21:52 +00001743SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1744 SDOperand Chain, SDOperand Ptr,
1745 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001746 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001747 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001748}
1749
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001750SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001751 const SDOperand *Ops, unsigned NumOps) {
1752 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001753 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001754 case 1: return getNode(Opcode, VT, Ops[0]);
1755 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1756 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001757 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001758 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001759
Chris Lattneref847df2005-04-09 03:27:28 +00001760 switch (Opcode) {
1761 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001762 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001763 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001764 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1765 "LHS and RHS of condition must have same type!");
1766 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1767 "True and False arms of SelectCC must have same type!");
1768 assert(Ops[2].getValueType() == VT &&
1769 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001770 break;
1771 }
1772 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001773 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001774 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1775 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001776 break;
1777 }
Chris Lattneref847df2005-04-09 03:27:28 +00001778 }
1779
Chris Lattner385328c2005-05-14 07:42:29 +00001780 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001781 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001782 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001783 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001784 FoldingSetNodeID ID;
1785 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001786 void *IP = 0;
1787 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1788 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001789 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001790 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001791 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001792 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001793 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001794 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001795 }
Chris Lattneref847df2005-04-09 03:27:28 +00001796 AllNodes.push_back(N);
1797 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001798}
1799
Chris Lattner89c34632005-05-14 06:20:26 +00001800SDOperand SelectionDAG::getNode(unsigned Opcode,
1801 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001802 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001803 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1804 Ops, NumOps);
1805}
1806
1807SDOperand SelectionDAG::getNode(unsigned Opcode,
1808 const MVT::ValueType *VTs, unsigned NumVTs,
1809 const SDOperand *Ops, unsigned NumOps) {
1810 if (NumVTs == 1)
1811 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001812 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1813}
1814
1815SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1816 const SDOperand *Ops, unsigned NumOps) {
1817 if (VTList.NumVTs == 1)
1818 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001819
Chris Lattner5f056bf2005-07-10 01:55:33 +00001820 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00001821 // FIXME: figure out how to safely handle things like
1822 // int foo(int x) { return 1 << (x & 255); }
1823 // int bar() { return foo(256); }
1824#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001825 case ISD::SRA_PARTS:
1826 case ISD::SRL_PARTS:
1827 case ISD::SHL_PARTS:
1828 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001829 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001830 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1831 else if (N3.getOpcode() == ISD::AND)
1832 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1833 // If the and is only masking out bits that cannot effect the shift,
1834 // eliminate the and.
1835 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1836 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1837 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1838 }
1839 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001840#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001841 }
Chris Lattner89c34632005-05-14 06:20:26 +00001842
Chris Lattner43247a12005-08-25 19:12:10 +00001843 // Memoize the node unless it returns a flag.
1844 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001845 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001846 FoldingSetNodeID ID;
1847 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001848 void *IP = 0;
1849 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1850 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001851 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001852 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001853 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001854 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001855 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001856 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001857 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001858 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001859 return SDOperand(N, 0);
1860}
1861
Chris Lattner70046e92006-08-15 17:46:01 +00001862SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1863 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001864}
1865
Chris Lattner70046e92006-08-15 17:46:01 +00001866SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001867 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1868 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001869 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001870 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001871 }
1872 std::vector<MVT::ValueType> V;
1873 V.push_back(VT1);
1874 V.push_back(VT2);
1875 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001876 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001877}
Chris Lattner70046e92006-08-15 17:46:01 +00001878SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1879 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001880 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001881 E = VTList.end(); I != E; ++I) {
1882 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1883 (*I)[2] == VT3)
1884 return makeVTList(&(*I)[0], 3);
1885 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001886 std::vector<MVT::ValueType> V;
1887 V.push_back(VT1);
1888 V.push_back(VT2);
1889 V.push_back(VT3);
1890 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001891 return makeVTList(&(*VTList.begin())[0], 3);
1892}
1893
1894SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1895 switch (NumVTs) {
1896 case 0: assert(0 && "Cannot have nodes without results!");
1897 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1898 case 2: return getVTList(VTs[0], VTs[1]);
1899 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1900 default: break;
1901 }
1902
1903 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1904 E = VTList.end(); I != E; ++I) {
1905 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1906
1907 bool NoMatch = false;
1908 for (unsigned i = 2; i != NumVTs; ++i)
1909 if (VTs[i] != (*I)[i]) {
1910 NoMatch = true;
1911 break;
1912 }
1913 if (!NoMatch)
1914 return makeVTList(&*I->begin(), NumVTs);
1915 }
1916
1917 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1918 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001919}
1920
1921
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001922/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1923/// specified operands. If the resultant node already exists in the DAG,
1924/// this does not modify the specified node, instead it returns the node that
1925/// already exists. If the resultant node does not exist in the DAG, the
1926/// input node is returned. As a degenerate case, if you specify the same
1927/// input operands as the node already has, the input node is returned.
1928SDOperand SelectionDAG::
1929UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1930 SDNode *N = InN.Val;
1931 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1932
1933 // Check to see if there is no change.
1934 if (Op == N->getOperand(0)) return InN;
1935
1936 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001937 void *InsertPos = 0;
1938 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1939 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001940
1941 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001942 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001943 RemoveNodeFromCSEMaps(N);
1944
1945 // Now we update the operands.
1946 N->OperandList[0].Val->removeUser(N);
1947 Op.Val->addUser(N);
1948 N->OperandList[0] = Op;
1949
1950 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001951 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001952 return InN;
1953}
1954
1955SDOperand SelectionDAG::
1956UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1957 SDNode *N = InN.Val;
1958 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1959
1960 // Check to see if there is no change.
1961 bool AnyChange = false;
1962 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1963 return InN; // No operands changed, just return the input node.
1964
1965 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001966 void *InsertPos = 0;
1967 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1968 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001969
1970 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001971 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001972 RemoveNodeFromCSEMaps(N);
1973
1974 // Now we update the operands.
1975 if (N->OperandList[0] != Op1) {
1976 N->OperandList[0].Val->removeUser(N);
1977 Op1.Val->addUser(N);
1978 N->OperandList[0] = Op1;
1979 }
1980 if (N->OperandList[1] != Op2) {
1981 N->OperandList[1].Val->removeUser(N);
1982 Op2.Val->addUser(N);
1983 N->OperandList[1] = Op2;
1984 }
1985
1986 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001987 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001988 return InN;
1989}
1990
1991SDOperand SelectionDAG::
1992UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001993 SDOperand Ops[] = { Op1, Op2, Op3 };
1994 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001995}
1996
1997SDOperand SelectionDAG::
1998UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1999 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002000 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2001 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002002}
2003
2004SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002005UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2006 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002007 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2008 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002009}
2010
2011
2012SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002013UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002014 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002015 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002016 "Update with wrong number of operands");
2017
2018 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002019 bool AnyChange = false;
2020 for (unsigned i = 0; i != NumOps; ++i) {
2021 if (Ops[i] != N->getOperand(i)) {
2022 AnyChange = true;
2023 break;
2024 }
2025 }
2026
2027 // No operands changed, just return the input node.
2028 if (!AnyChange) return InN;
2029
2030 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002031 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002032 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002033 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002034
2035 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002036 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002037 RemoveNodeFromCSEMaps(N);
2038
2039 // Now we update the operands.
2040 for (unsigned i = 0; i != NumOps; ++i) {
2041 if (N->OperandList[i] != Ops[i]) {
2042 N->OperandList[i].Val->removeUser(N);
2043 Ops[i].Val->addUser(N);
2044 N->OperandList[i] = Ops[i];
2045 }
2046 }
2047
2048 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002049 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002050 return InN;
2051}
2052
2053
2054
Chris Lattner149c58c2005-08-16 18:17:10 +00002055
2056/// SelectNodeTo - These are used for target selectors to *mutate* the
2057/// specified node to have the specified return type, Target opcode, and
2058/// operands. Note that target opcodes are stored as
2059/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002060///
2061/// Note that SelectNodeTo returns the resultant node. If there is already a
2062/// node of the specified opcode and operands, it returns that node instead of
2063/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002064SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2065 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002066 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002067 FoldingSetNodeID ID;
2068 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs);
Chris Lattner4a283e92006-08-11 18:38:11 +00002069 void *IP = 0;
2070 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002071 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002072
Chris Lattner7651fa42005-08-24 23:00:29 +00002073 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002074
Chris Lattner7651fa42005-08-24 23:00:29 +00002075 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002076 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002077
Chris Lattner4a283e92006-08-11 18:38:11 +00002078 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002079 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002080}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002081
Evan Cheng95514ba2006-08-26 08:00:10 +00002082SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2083 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002084 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002085 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002086 FoldingSetNodeID ID;
2087 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00002088 void *IP = 0;
2089 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002090 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002091
Chris Lattner149c58c2005-08-16 18:17:10 +00002092 RemoveNodeFromCSEMaps(N);
2093 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002094 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002095 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00002096 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002097 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002098}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002099
Evan Cheng95514ba2006-08-26 08:00:10 +00002100SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2101 MVT::ValueType VT, SDOperand Op1,
2102 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002103 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002104 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002105 FoldingSetNodeID ID;
2106 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +00002107 void *IP = 0;
2108 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002109 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002110
Chris Lattner149c58c2005-08-16 18:17:10 +00002111 RemoveNodeFromCSEMaps(N);
2112 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002113 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002114 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002115
Chris Lattnera5682852006-08-07 23:03:03 +00002116 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002117 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002118}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002119
Evan Cheng95514ba2006-08-26 08:00:10 +00002120SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2121 MVT::ValueType VT, SDOperand Op1,
2122 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002123 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002124 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002125 FoldingSetNodeID ID;
2126 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00002127 void *IP = 0;
2128 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002129 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002130
Chris Lattner149c58c2005-08-16 18:17:10 +00002131 RemoveNodeFromCSEMaps(N);
2132 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002133 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002134 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002135
Chris Lattnera5682852006-08-07 23:03:03 +00002136 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002137 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002138}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002139
Evan Cheng95514ba2006-08-26 08:00:10 +00002140SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002141 MVT::ValueType VT, const SDOperand *Ops,
2142 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002143 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002144 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002145 FoldingSetNodeID ID;
2146 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002147 void *IP = 0;
2148 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002149 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002150
Chris Lattner6b09a292005-08-21 18:49:33 +00002151 RemoveNodeFromCSEMaps(N);
2152 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002153 N->setValueTypes(VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00002154 N->setOperands(Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002155
Chris Lattnera5682852006-08-07 23:03:03 +00002156 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002157 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002158}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002159
Evan Cheng95514ba2006-08-26 08:00:10 +00002160SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2161 MVT::ValueType VT1, MVT::ValueType VT2,
2162 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002163 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002164 FoldingSetNodeID ID;
2165 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +00002166 void *IP = 0;
2167 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002168 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002169
Chris Lattner0fb094f2005-11-19 01:44:53 +00002170 RemoveNodeFromCSEMaps(N);
2171 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002172 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002173 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002174
Chris Lattnera5682852006-08-07 23:03:03 +00002175 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002176 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002177}
2178
Evan Cheng95514ba2006-08-26 08:00:10 +00002179SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2180 MVT::ValueType VT1, MVT::ValueType VT2,
2181 SDOperand Op1, SDOperand Op2,
2182 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002183 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002184 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002185 FoldingSetNodeID ID;
2186 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00002187 void *IP = 0;
2188 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002189 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002190
Chris Lattner0fb094f2005-11-19 01:44:53 +00002191 RemoveNodeFromCSEMaps(N);
2192 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002193 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002194 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002195
Chris Lattnera5682852006-08-07 23:03:03 +00002196 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002197 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002198}
2199
Chris Lattner0fb094f2005-11-19 01:44:53 +00002200
Evan Cheng6ae46c42006-02-09 07:15:23 +00002201/// getTargetNode - These are used for target selectors to create a new node
2202/// with specified return type(s), target opcode, and operands.
2203///
2204/// Note that getTargetNode returns the resultant node. If there is already a
2205/// node of the specified opcode and operands, it returns that node instead of
2206/// the current one.
2207SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2208 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2209}
2210SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2211 SDOperand Op1) {
2212 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2213}
2214SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2215 SDOperand Op1, SDOperand Op2) {
2216 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2217}
2218SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2219 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2220 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2221}
2222SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002223 const SDOperand *Ops, unsigned NumOps) {
2224 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002225}
2226SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2227 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002228 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002229 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002230}
2231SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002232 MVT::ValueType VT2, SDOperand Op1,
2233 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002234 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002235 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002236 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002237}
2238SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002239 MVT::ValueType VT2, SDOperand Op1,
2240 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002241 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002242 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002243 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002244}
Evan Cheng694481e2006-08-27 08:08:54 +00002245SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2246 MVT::ValueType VT2,
2247 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002248 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002249 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002250}
2251SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2252 MVT::ValueType VT2, MVT::ValueType VT3,
2253 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002254 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002255 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002256 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002257}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002258SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002259 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002260 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002261 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2262 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002263}
2264
Evan Cheng99157a02006-08-07 22:13:29 +00002265/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002266/// This can cause recursive merging of nodes in the DAG.
2267///
Chris Lattner8b52f212005-08-26 18:36:28 +00002268/// This version assumes From/To have a single result value.
2269///
Chris Lattner1e111c72005-09-07 05:37:01 +00002270void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2271 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002272 SDNode *From = FromN.Val, *To = ToN.Val;
2273 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2274 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002275 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002276
Chris Lattner8b8749f2005-08-17 19:00:20 +00002277 while (!From->use_empty()) {
2278 // Process users until they are all gone.
2279 SDNode *U = *From->use_begin();
2280
2281 // This node is about to morph, remove its old self from the CSE maps.
2282 RemoveNodeFromCSEMaps(U);
2283
Chris Lattner65113b22005-11-08 22:07:03 +00002284 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2285 I != E; ++I)
2286 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002287 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002288 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002289 To->addUser(U);
2290 }
2291
2292 // Now that we have modified U, add it back to the CSE maps. If it already
2293 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002294 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2295 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002296 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002297 if (Deleted) Deleted->push_back(U);
2298 DeleteNodeNotInCSEMaps(U);
2299 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002300 }
2301}
2302
2303/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2304/// This can cause recursive merging of nodes in the DAG.
2305///
2306/// This version assumes From/To have matching types and numbers of result
2307/// values.
2308///
Chris Lattner1e111c72005-09-07 05:37:01 +00002309void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2310 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002311 assert(From != To && "Cannot replace uses of with self");
2312 assert(From->getNumValues() == To->getNumValues() &&
2313 "Cannot use this version of ReplaceAllUsesWith!");
2314 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002315 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002316 return;
2317 }
2318
2319 while (!From->use_empty()) {
2320 // Process users until they are all gone.
2321 SDNode *U = *From->use_begin();
2322
2323 // This node is about to morph, remove its old self from the CSE maps.
2324 RemoveNodeFromCSEMaps(U);
2325
Chris Lattner65113b22005-11-08 22:07:03 +00002326 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2327 I != E; ++I)
2328 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002329 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002330 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002331 To->addUser(U);
2332 }
2333
2334 // Now that we have modified U, add it back to the CSE maps. If it already
2335 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002336 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2337 ReplaceAllUsesWith(U, Existing, Deleted);
2338 // U is now dead.
2339 if (Deleted) Deleted->push_back(U);
2340 DeleteNodeNotInCSEMaps(U);
2341 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002342 }
2343}
2344
Chris Lattner8b52f212005-08-26 18:36:28 +00002345/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2346/// This can cause recursive merging of nodes in the DAG.
2347///
2348/// This version can replace From with any result values. To must match the
2349/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002350void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002351 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002352 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002353 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002354 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002355 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002356 return;
2357 }
2358
2359 while (!From->use_empty()) {
2360 // Process users until they are all gone.
2361 SDNode *U = *From->use_begin();
2362
2363 // This node is about to morph, remove its old self from the CSE maps.
2364 RemoveNodeFromCSEMaps(U);
2365
Chris Lattner65113b22005-11-08 22:07:03 +00002366 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2367 I != E; ++I)
2368 if (I->Val == From) {
2369 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002370 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002371 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002372 ToOp.Val->addUser(U);
2373 }
2374
2375 // Now that we have modified U, add it back to the CSE maps. If it already
2376 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002377 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2378 ReplaceAllUsesWith(U, Existing, Deleted);
2379 // U is now dead.
2380 if (Deleted) Deleted->push_back(U);
2381 DeleteNodeNotInCSEMaps(U);
2382 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002383 }
2384}
2385
Chris Lattner012f2412006-02-17 21:58:01 +00002386/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2387/// uses of other values produced by From.Val alone. The Deleted vector is
2388/// handled the same was as for ReplaceAllUsesWith.
2389void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2390 std::vector<SDNode*> &Deleted) {
2391 assert(From != To && "Cannot replace a value with itself");
2392 // Handle the simple, trivial, case efficiently.
2393 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2394 ReplaceAllUsesWith(From, To, &Deleted);
2395 return;
2396 }
2397
2398 // Get all of the users in a nice, deterministically ordered, uniqued set.
2399 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2400
2401 while (!Users.empty()) {
2402 // We know that this user uses some value of From. If it is the right
2403 // value, update it.
2404 SDNode *User = Users.back();
2405 Users.pop_back();
2406
2407 for (SDOperand *Op = User->OperandList,
2408 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2409 if (*Op == From) {
2410 // Okay, we know this user needs to be updated. Remove its old self
2411 // from the CSE maps.
2412 RemoveNodeFromCSEMaps(User);
2413
2414 // Update all operands that match "From".
2415 for (; Op != E; ++Op) {
2416 if (*Op == From) {
2417 From.Val->removeUser(User);
2418 *Op = To;
2419 To.Val->addUser(User);
2420 }
2421 }
2422
2423 // Now that we have modified User, add it back to the CSE maps. If it
2424 // already exists there, recursively merge the results together.
2425 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2426 unsigned NumDeleted = Deleted.size();
2427 ReplaceAllUsesWith(User, Existing, &Deleted);
2428
2429 // User is now dead.
2430 Deleted.push_back(User);
2431 DeleteNodeNotInCSEMaps(User);
2432
2433 // We have to be careful here, because ReplaceAllUsesWith could have
2434 // deleted a user of From, which means there may be dangling pointers
2435 // in the "Users" setvector. Scan over the deleted node pointers and
2436 // remove them from the setvector.
2437 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2438 Users.remove(Deleted[i]);
2439 }
2440 break; // Exit the operand scanning loop.
2441 }
2442 }
2443 }
2444}
2445
Chris Lattner7b2880c2005-08-24 22:44:39 +00002446
Evan Chenge6f35d82006-08-01 08:20:41 +00002447/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2448/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002449unsigned SelectionDAG::AssignNodeIds() {
2450 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002451 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2452 SDNode *N = I;
2453 N->setNodeId(Id++);
2454 }
2455 return Id;
2456}
2457
Evan Chenge6f35d82006-08-01 08:20:41 +00002458/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002459/// based on their topological order. It returns the maximum id and a vector
2460/// of the SDNodes* in assigned order by reference.
2461unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002462 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002463 std::vector<unsigned> InDegree(DAGSize);
2464 std::vector<SDNode*> Sources;
2465
2466 // Use a two pass approach to avoid using a std::map which is slow.
2467 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002468 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2469 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002470 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002471 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002472 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002473 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002474 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002475 }
2476
Evan Cheng99157a02006-08-07 22:13:29 +00002477 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002478 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002479 SDNode *N = Sources.back();
2480 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002481 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002482 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2483 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002484 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002485 if (Degree == 0)
2486 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002487 }
2488 }
2489
Evan Chengc384d6c2006-08-02 22:00:34 +00002490 // Second pass, assign the actual topological order as node ids.
2491 Id = 0;
2492 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2493 TI != TE; ++TI)
2494 (*TI)->setNodeId(Id++);
2495
2496 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002497}
2498
2499
Evan Cheng091cba12006-07-27 06:39:06 +00002500
Jim Laskey58b968b2005-08-17 20:08:02 +00002501//===----------------------------------------------------------------------===//
2502// SDNode Class
2503//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002504
Chris Lattner917d2c92006-07-19 00:00:37 +00002505// Out-of-line virtual method to give class a home.
2506void SDNode::ANCHOR() {
2507}
2508
Jim Laskey583bd472006-10-27 23:46:08 +00002509/// Profile - Gather unique data for the node.
2510///
2511void SDNode::Profile(FoldingSetNodeID &ID) {
2512 AddNodeIDNode(ID, this);
2513}
2514
Chris Lattnera3255112005-11-08 23:30:28 +00002515/// getValueTypeList - Return a pointer to the specified value type.
2516///
2517MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2518 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2519 VTs[VT] = VT;
2520 return &VTs[VT];
2521}
Chris Lattnera5682852006-08-07 23:03:03 +00002522
Chris Lattner5c884562005-01-12 18:37:47 +00002523/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2524/// indicated value. This method ignores uses of other values defined by this
2525/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002526bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002527 assert(Value < getNumValues() && "Bad value!");
2528
2529 // If there is only one value, this is easy.
2530 if (getNumValues() == 1)
2531 return use_size() == NUses;
2532 if (Uses.size() < NUses) return false;
2533
Evan Cheng4ee62112006-02-05 06:29:23 +00002534 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002535
2536 std::set<SDNode*> UsersHandled;
2537
Chris Lattnerf83482d2006-08-16 20:59:32 +00002538 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002539 SDNode *User = *UI;
2540 if (User->getNumOperands() == 1 ||
2541 UsersHandled.insert(User).second) // First time we've seen this?
2542 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2543 if (User->getOperand(i) == TheValue) {
2544 if (NUses == 0)
2545 return false; // too many uses
2546 --NUses;
2547 }
2548 }
2549
2550 // Found exactly the right number of uses?
2551 return NUses == 0;
2552}
2553
2554
Evan Cheng4ee62112006-02-05 06:29:23 +00002555// isOnlyUse - Return true if this node is the only use of N.
2556bool SDNode::isOnlyUse(SDNode *N) const {
2557 bool Seen = false;
2558 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2559 SDNode *User = *I;
2560 if (User == this)
2561 Seen = true;
2562 else
2563 return false;
2564 }
2565
2566 return Seen;
2567}
2568
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002569// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002570bool SDOperand::isOperand(SDNode *N) const {
2571 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2572 if (*this == N->getOperand(i))
2573 return true;
2574 return false;
2575}
2576
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002577bool SDNode::isOperand(SDNode *N) const {
2578 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2579 if (this == N->OperandList[i].Val)
2580 return true;
2581 return false;
2582}
Evan Cheng4ee62112006-02-05 06:29:23 +00002583
Evan Chengc5484282006-10-04 00:56:09 +00002584uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2585 assert(Num < NumOperands && "Invalid child # of SDNode!");
2586 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2587}
2588
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002589const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002590 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002591 default:
2592 if (getOpcode() < ISD::BUILTIN_OP_END)
2593 return "<<Unknown DAG Node>>";
2594 else {
Evan Cheng72261582005-12-20 06:22:03 +00002595 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002596 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002597 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2598 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002599
Evan Cheng72261582005-12-20 06:22:03 +00002600 TargetLowering &TLI = G->getTargetLoweringInfo();
2601 const char *Name =
2602 TLI.getTargetNodeName(getOpcode());
2603 if (Name) return Name;
2604 }
2605
2606 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002607 }
2608
Andrew Lenharth95762122005-03-31 21:24:06 +00002609 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002610 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002611 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002612 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002613 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002614 case ISD::AssertSext: return "AssertSext";
2615 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002616
2617 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002618 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002619 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002620 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002621
2622 case ISD::Constant: return "Constant";
2623 case ISD::ConstantFP: return "ConstantFP";
2624 case ISD::GlobalAddress: return "GlobalAddress";
2625 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002626 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00002627 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Chris Lattner5839bf22005-08-26 17:15:30 +00002628 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002629 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002630 case ISD::INTRINSIC_WO_CHAIN: {
2631 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2632 return Intrinsic::getName((Intrinsic::ID)IID);
2633 }
2634 case ISD::INTRINSIC_VOID:
2635 case ISD::INTRINSIC_W_CHAIN: {
2636 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002637 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002638 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002639
Chris Lattnerb2827b02006-03-19 00:52:58 +00002640 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002641 case ISD::TargetConstant: return "TargetConstant";
2642 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002643 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2644 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002645 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002646 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002647 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002648
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002649 case ISD::CopyToReg: return "CopyToReg";
2650 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002651 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002652 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002653 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002654 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002655 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002656 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002657
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002658 // Unary operators
2659 case ISD::FABS: return "fabs";
2660 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002661 case ISD::FSQRT: return "fsqrt";
2662 case ISD::FSIN: return "fsin";
2663 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002664 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002665
2666 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002667 case ISD::ADD: return "add";
2668 case ISD::SUB: return "sub";
2669 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002670 case ISD::MULHU: return "mulhu";
2671 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002672 case ISD::SDIV: return "sdiv";
2673 case ISD::UDIV: return "udiv";
2674 case ISD::SREM: return "srem";
2675 case ISD::UREM: return "urem";
2676 case ISD::AND: return "and";
2677 case ISD::OR: return "or";
2678 case ISD::XOR: return "xor";
2679 case ISD::SHL: return "shl";
2680 case ISD::SRA: return "sra";
2681 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002682 case ISD::ROTL: return "rotl";
2683 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002684 case ISD::FADD: return "fadd";
2685 case ISD::FSUB: return "fsub";
2686 case ISD::FMUL: return "fmul";
2687 case ISD::FDIV: return "fdiv";
2688 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002689 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002690 case ISD::VADD: return "vadd";
2691 case ISD::VSUB: return "vsub";
2692 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002693 case ISD::VSDIV: return "vsdiv";
2694 case ISD::VUDIV: return "vudiv";
2695 case ISD::VAND: return "vand";
2696 case ISD::VOR: return "vor";
2697 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002698
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002699 case ISD::SETCC: return "setcc";
2700 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002701 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002702 case ISD::VSELECT: return "vselect";
2703 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2704 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2705 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002706 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002707 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2708 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2709 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2710 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2711 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002712 case ISD::ADDC: return "addc";
2713 case ISD::ADDE: return "adde";
2714 case ISD::SUBC: return "subc";
2715 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002716 case ISD::SHL_PARTS: return "shl_parts";
2717 case ISD::SRA_PARTS: return "sra_parts";
2718 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002719
Chris Lattner7f644642005-04-28 21:44:03 +00002720 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002721 case ISD::SIGN_EXTEND: return "sign_extend";
2722 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002723 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002724 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002725 case ISD::TRUNCATE: return "truncate";
2726 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002727 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002728 case ISD::FP_EXTEND: return "fp_extend";
2729
2730 case ISD::SINT_TO_FP: return "sint_to_fp";
2731 case ISD::UINT_TO_FP: return "uint_to_fp";
2732 case ISD::FP_TO_SINT: return "fp_to_sint";
2733 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002734 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002735
2736 // Control flow instructions
2737 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002738 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002739 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002740 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002741 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002742 case ISD::CALLSEQ_START: return "callseq_start";
2743 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002744
2745 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002746 case ISD::LOAD: return "load";
2747 case ISD::STORE: return "store";
2748 case ISD::VLOAD: return "vload";
Nate Begemanacc398c2006-01-25 18:21:52 +00002749 case ISD::VAARG: return "vaarg";
2750 case ISD::VACOPY: return "vacopy";
2751 case ISD::VAEND: return "vaend";
2752 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002753 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002754 case ISD::EXTRACT_ELEMENT: return "extract_element";
2755 case ISD::BUILD_PAIR: return "build_pair";
2756 case ISD::STACKSAVE: return "stacksave";
2757 case ISD::STACKRESTORE: return "stackrestore";
2758
2759 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002760 case ISD::MEMSET: return "memset";
2761 case ISD::MEMCPY: return "memcpy";
2762 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002763
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002764 // Bit manipulation
2765 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002766 case ISD::CTPOP: return "ctpop";
2767 case ISD::CTTZ: return "cttz";
2768 case ISD::CTLZ: return "ctlz";
2769
Chris Lattner36ce6912005-11-29 06:21:05 +00002770 // Debug info
2771 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002772 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002773 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002774
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002775 case ISD::CONDCODE:
2776 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002777 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002778 case ISD::SETOEQ: return "setoeq";
2779 case ISD::SETOGT: return "setogt";
2780 case ISD::SETOGE: return "setoge";
2781 case ISD::SETOLT: return "setolt";
2782 case ISD::SETOLE: return "setole";
2783 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002784
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002785 case ISD::SETO: return "seto";
2786 case ISD::SETUO: return "setuo";
2787 case ISD::SETUEQ: return "setue";
2788 case ISD::SETUGT: return "setugt";
2789 case ISD::SETUGE: return "setuge";
2790 case ISD::SETULT: return "setult";
2791 case ISD::SETULE: return "setule";
2792 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002793
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002794 case ISD::SETEQ: return "seteq";
2795 case ISD::SETGT: return "setgt";
2796 case ISD::SETGE: return "setge";
2797 case ISD::SETLT: return "setlt";
2798 case ISD::SETLE: return "setle";
2799 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002800 }
2801 }
2802}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002803
Evan Cheng2caccca2006-10-17 21:14:32 +00002804const char *SDNode::getAddressingModeName(ISD::MemOpAddrMode AM) {
2805 switch (AM) {
2806 default:
2807 return "";
2808 case ISD::PRE_INC:
2809 return "<pre-inc>";
2810 case ISD::PRE_DEC:
2811 return "<pre-dec>";
2812 case ISD::POST_INC:
2813 return "<post-inc>";
2814 case ISD::POST_DEC:
2815 return "<post-dec>";
2816 }
2817}
2818
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002819void SDNode::dump() const { dump(0); }
2820void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002821 std::cerr << (void*)this << ": ";
2822
2823 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2824 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002825 if (getValueType(i) == MVT::Other)
2826 std::cerr << "ch";
2827 else
2828 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002829 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002830 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002831
2832 std::cerr << " ";
2833 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2834 if (i) std::cerr << ", ";
2835 std::cerr << (void*)getOperand(i).Val;
2836 if (unsigned RN = getOperand(i).ResNo)
2837 std::cerr << ":" << RN;
2838 }
2839
2840 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2841 std::cerr << "<" << CSDN->getValue() << ">";
2842 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2843 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002844 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002845 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002846 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002847 std::cerr << "<";
2848 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002849 if (offset > 0)
2850 std::cerr << " + " << offset;
2851 else
2852 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002853 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002854 std::cerr << "<" << FIDN->getIndex() << ">";
2855 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002856 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00002857 if (CP->isMachineConstantPoolEntry())
2858 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2859 else
2860 std::cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002861 if (offset > 0)
2862 std::cerr << " + " << offset;
2863 else
2864 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002865 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002866 std::cerr << "<";
2867 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2868 if (LBB)
2869 std::cerr << LBB->getName() << " ";
2870 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002871 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002872 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002873 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2874 } else {
2875 std::cerr << " #" << R->getReg();
2876 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002877 } else if (const ExternalSymbolSDNode *ES =
2878 dyn_cast<ExternalSymbolSDNode>(this)) {
2879 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002880 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2881 if (M->getValue())
2882 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2883 else
2884 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002885 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2886 std::cerr << ":" << getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002887 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2888 bool doExt = true;
2889 switch (LD->getExtensionType()) {
2890 default: doExt = false; break;
2891 case ISD::EXTLOAD:
2892 std::cerr << " <anyext ";
2893 break;
2894 case ISD::SEXTLOAD:
2895 std::cerr << " <sext ";
2896 break;
2897 case ISD::ZEXTLOAD:
2898 std::cerr << " <zext ";
2899 break;
2900 }
2901 if (doExt)
Evan Cheng2e49f092006-10-11 07:10:22 +00002902 std::cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002903
Evan Cheng2caccca2006-10-17 21:14:32 +00002904 const char *AM = getAddressingModeName(LD->getAddressingMode());
2905 if (AM != "")
2906 std::cerr << " " << AM;
2907 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
2908 if (ST->isTruncatingStore())
2909 std::cerr << " <trunc "
2910 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
2911
2912 const char *AM = getAddressingModeName(ST->getAddressingMode());
2913 if (AM != "")
2914 std::cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002915 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002916}
2917
Chris Lattnerde202b32005-11-09 23:47:37 +00002918static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002919 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2920 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002921 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002922 else
2923 std::cerr << "\n" << std::string(indent+2, ' ')
2924 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002925
Chris Lattnerea946cd2005-01-09 20:38:33 +00002926
2927 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002928 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002929}
2930
Chris Lattnerc3aae252005-01-07 07:46:32 +00002931void SelectionDAG::dump() const {
2932 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002933 std::vector<const SDNode*> Nodes;
2934 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2935 I != E; ++I)
2936 Nodes.push_back(I);
2937
Chris Lattner49d24712005-01-09 20:26:36 +00002938 std::sort(Nodes.begin(), Nodes.end());
2939
2940 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002941 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002942 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002943 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002944
Jim Laskey26f7fa72006-10-17 19:33:52 +00002945 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002946
Chris Lattnerc3aae252005-01-07 07:46:32 +00002947 std::cerr << "\n\n";
2948}
2949
Evan Chengd6594ae2006-09-12 21:00:35 +00002950const Type *ConstantPoolSDNode::getType() const {
2951 if (isMachineConstantPoolEntry())
2952 return Val.MachineCPVal->getType();
2953 return Val.ConstVal->getType();
2954}