blob: 16c0718c7d3848261443060b16da45cf7bda7922 [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::LOAD: {
404 LoadSDNode *LD = cast<LoadSDNode>(N);
405 ID.AddInteger(LD->getAddressingMode());
406 ID.AddInteger(LD->getExtensionType());
407 ID.AddInteger(LD->getLoadedVT());
408 ID.AddPointer(LD->getSrcValue());
409 ID.AddInteger(LD->getSrcValueOffset());
410 ID.AddInteger(LD->getAlignment());
411 ID.AddInteger(LD->isVolatile());
412 break;
413 }
414 case ISD::STORE: {
415 StoreSDNode *ST = cast<StoreSDNode>(N);
416 ID.AddInteger(ST->getAddressingMode());
417 ID.AddInteger(ST->isTruncatingStore());
418 ID.AddInteger(ST->getStoredVT());
419 ID.AddPointer(ST->getSrcValue());
420 ID.AddInteger(ST->getSrcValueOffset());
421 ID.AddInteger(ST->getAlignment());
422 ID.AddInteger(ST->isVolatile());
423 break;
424 }
425 }
Jim Laskey583bd472006-10-27 23:46:08 +0000426 }
427}
428
429//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000430// SelectionDAG Class
431//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000432
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000433/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000434/// SelectionDAG.
435void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000436 // Create a dummy node (which is not added to allnodes), that adds a reference
437 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000438 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000439
Chris Lattner190a4182006-08-04 17:45:20 +0000440 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000441
Chris Lattner190a4182006-08-04 17:45:20 +0000442 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000443 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000444 if (I->use_empty())
445 DeadNodes.push_back(I);
446
447 // Process the worklist, deleting the nodes and adding their uses to the
448 // worklist.
449 while (!DeadNodes.empty()) {
450 SDNode *N = DeadNodes.back();
451 DeadNodes.pop_back();
452
453 // Take the node out of the appropriate CSE map.
454 RemoveNodeFromCSEMaps(N);
455
456 // Next, brutally remove the operand list. This is safe to do, as there are
457 // no cycles in the graph.
458 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
459 SDNode *Operand = I->Val;
460 Operand->removeUser(N);
461
462 // Now that we removed this operand, see if there are no uses of it left.
463 if (Operand->use_empty())
464 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000465 }
Chris Lattner190a4182006-08-04 17:45:20 +0000466 delete[] N->OperandList;
467 N->OperandList = 0;
468 N->NumOperands = 0;
469
470 // Finally, remove N itself.
471 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000472 }
473
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000474 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000475 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000476}
477
Evan Cheng130a6472006-10-12 20:34:05 +0000478void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
479 SmallVector<SDNode*, 16> DeadNodes;
480 DeadNodes.push_back(N);
481
482 // Process the worklist, deleting the nodes and adding their uses to the
483 // worklist.
484 while (!DeadNodes.empty()) {
485 SDNode *N = DeadNodes.back();
486 DeadNodes.pop_back();
487
488 // Take the node out of the appropriate CSE map.
489 RemoveNodeFromCSEMaps(N);
490
491 // Next, brutally remove the operand list. This is safe to do, as there are
492 // no cycles in the graph.
493 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
494 SDNode *Operand = I->Val;
495 Operand->removeUser(N);
496
497 // Now that we removed this operand, see if there are no uses of it left.
498 if (Operand->use_empty())
499 DeadNodes.push_back(Operand);
500 }
501 delete[] N->OperandList;
502 N->OperandList = 0;
503 N->NumOperands = 0;
504
505 // Finally, remove N itself.
506 Deleted.push_back(N);
507 AllNodes.erase(N);
508 }
509}
510
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000511void SelectionDAG::DeleteNode(SDNode *N) {
512 assert(N->use_empty() && "Cannot delete a node that is not dead!");
513
514 // First take this out of the appropriate CSE map.
515 RemoveNodeFromCSEMaps(N);
516
Chris Lattner1e111c72005-09-07 05:37:01 +0000517 // Finally, remove uses due to operands of this node, remove from the
518 // AllNodes list, and delete the node.
519 DeleteNodeNotInCSEMaps(N);
520}
521
522void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
523
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000524 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000525 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000526
527 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000528 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
529 I->Val->removeUser(N);
530 delete[] N->OperandList;
531 N->OperandList = 0;
532 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000533
534 delete N;
535}
536
Chris Lattner149c58c2005-08-16 18:17:10 +0000537/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
538/// correspond to it. This is useful when we're about to delete or repurpose
539/// the node. We don't want future request for structurally identical nodes
540/// to return N anymore.
541void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000542 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000543 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000544 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000545 case ISD::STRING:
546 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
547 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000548 case ISD::CONDCODE:
549 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
550 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000551 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000552 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
553 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000554 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000555 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000556 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000557 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000558 Erased =
559 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000560 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000561 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000562 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000563 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
564 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000565 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000566 // Remove it from the CSE Map.
567 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000568 break;
569 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000570#ifndef NDEBUG
571 // Verify that the node was actually in one of the CSE maps, unless it has a
572 // flag result (which cannot be CSE'd) or is one of the special cases that are
573 // not subject to CSE.
574 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000575 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000576 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000577 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000578 assert(0 && "Node is not in map!");
579 }
580#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000581}
582
Chris Lattner8b8749f2005-08-17 19:00:20 +0000583/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
584/// has been taken out and modified in some way. If the specified node already
585/// exists in the CSE maps, do not modify the maps, but return the existing node
586/// instead. If it doesn't exist, add it and return null.
587///
588SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
589 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000590 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000591 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000592
Chris Lattner9f8cc692005-12-19 22:21:21 +0000593 // Check that remaining values produced are not flags.
594 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
595 if (N->getValueType(i) == MVT::Flag)
596 return 0; // Never CSE anything that produces a flag.
597
Chris Lattnera5682852006-08-07 23:03:03 +0000598 SDNode *New = CSEMap.GetOrInsertNode(N);
599 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000600 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000601}
602
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000603/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
604/// were replaced with those specified. If this node is never memoized,
605/// return null, otherwise return a pointer to the slot it would take. If a
606/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000607SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
608 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000609 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000610 return 0; // Never add these nodes.
611
612 // Check that remaining values produced are not flags.
613 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
614 if (N->getValueType(i) == MVT::Flag)
615 return 0; // Never CSE anything that produces a flag.
616
Jim Laskey583bd472006-10-27 23:46:08 +0000617 FoldingSetNodeID ID;
618 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Op);
Chris Lattnera5682852006-08-07 23:03:03 +0000619 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000620}
621
622/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
623/// were replaced with those specified. If this node is never memoized,
624/// return null, otherwise return a pointer to the slot it would take. If a
625/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000626SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
627 SDOperand Op1, SDOperand Op2,
628 void *&InsertPos) {
629 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
630 return 0; // Never add these nodes.
631
632 // Check that remaining values produced are not flags.
633 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
634 if (N->getValueType(i) == MVT::Flag)
635 return 0; // Never CSE anything that produces a flag.
636
Jim Laskey583bd472006-10-27 23:46:08 +0000637 FoldingSetNodeID ID;
638 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +0000639 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
640}
641
642
643/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
644/// were replaced with those specified. If this node is never memoized,
645/// return null, otherwise return a pointer to the slot it would take. If a
646/// node already exists with these operands, the slot will be non-null.
647SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000648 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000649 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000650 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000651 return 0; // Never add these nodes.
652
653 // Check that remaining values produced are not flags.
654 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
655 if (N->getValueType(i) == MVT::Flag)
656 return 0; // Never CSE anything that produces a flag.
657
Jim Laskey583bd472006-10-27 23:46:08 +0000658 FoldingSetNodeID ID;
659 AddNodeIDNode(ID, N->getOpcode(), N->getVTList());
660
Evan Cheng9629aba2006-10-11 01:47:58 +0000661 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
662 ID.AddInteger(LD->getAddressingMode());
663 ID.AddInteger(LD->getExtensionType());
Evan Cheng2e49f092006-10-11 07:10:22 +0000664 ID.AddInteger(LD->getLoadedVT());
Evan Cheng9629aba2006-10-11 01:47:58 +0000665 ID.AddPointer(LD->getSrcValue());
666 ID.AddInteger(LD->getSrcValueOffset());
667 ID.AddInteger(LD->getAlignment());
668 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000669 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
670 ID.AddInteger(ST->getAddressingMode());
671 ID.AddInteger(ST->isTruncatingStore());
672 ID.AddInteger(ST->getStoredVT());
673 ID.AddPointer(ST->getSrcValue());
674 ID.AddInteger(ST->getSrcValueOffset());
675 ID.AddInteger(ST->getAlignment());
676 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000677 }
Jim Laskey583bd472006-10-27 23:46:08 +0000678
679 AddNodeIDOperands(ID, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000680 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000681}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000682
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000683
Chris Lattner78ec3112003-08-11 14:57:33 +0000684SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000685 while (!AllNodes.empty()) {
686 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000687 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000688 delete [] N->OperandList;
689 N->OperandList = 0;
690 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000691 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000692 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000693}
694
Chris Lattner0f2287b2005-04-13 02:38:18 +0000695SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000696 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000697 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000698 return getNode(ISD::AND, Op.getValueType(), Op,
699 getConstant(Imm, Op.getValueType()));
700}
701
Chris Lattner36ce6912005-11-29 06:21:05 +0000702SDOperand SelectionDAG::getString(const std::string &Val) {
703 StringSDNode *&N = StringNodes[Val];
704 if (!N) {
705 N = new StringSDNode(Val);
706 AllNodes.push_back(N);
707 }
708 return SDOperand(N, 0);
709}
710
Chris Lattner61b09412006-08-11 21:01:22 +0000711SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000712 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000713 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000714
Chris Lattner61b09412006-08-11 21:01:22 +0000715 // Mask out any bits that are not valid for this constant.
716 Val &= MVT::getIntVTBitMask(VT);
717
718 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000719 FoldingSetNodeID ID;
720 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000721 ID.AddInteger(Val);
722 void *IP = 0;
723 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
724 return SDOperand(E, 0);
725 SDNode *N = new ConstantSDNode(isT, Val, VT);
726 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727 AllNodes.push_back(N);
728 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000729}
730
Chris Lattner61b09412006-08-11 21:01:22 +0000731
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000732SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
733 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000734 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
735 if (VT == MVT::f32)
736 Val = (float)Val; // Mask out extra precision.
737
Chris Lattnerd8658612005-02-17 20:17:32 +0000738 // Do the map lookup using the actual bit pattern for the floating point
739 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
740 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000741 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000742 FoldingSetNodeID ID;
743 AddNodeIDNode(ID, Opc, getVTList(VT));
744 ID.AddDouble(Val);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000745 void *IP = 0;
746 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
747 return SDOperand(E, 0);
748 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
749 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000750 AllNodes.push_back(N);
751 return SDOperand(N, 0);
752}
753
Chris Lattnerc3aae252005-01-07 07:46:32 +0000754SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000755 MVT::ValueType VT, int Offset,
756 bool isTargetGA) {
757 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000758 FoldingSetNodeID ID;
759 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000760 ID.AddPointer(GV);
761 ID.AddInteger(Offset);
762 void *IP = 0;
763 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
764 return SDOperand(E, 0);
765 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
766 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000767 AllNodes.push_back(N);
768 return SDOperand(N, 0);
769}
770
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000771SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
772 bool isTarget) {
773 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000774 FoldingSetNodeID ID;
775 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000776 ID.AddInteger(FI);
777 void *IP = 0;
778 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
779 return SDOperand(E, 0);
780 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
781 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000782 AllNodes.push_back(N);
783 return SDOperand(N, 0);
784}
785
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000786SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
787 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000788 FoldingSetNodeID ID;
789 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000790 ID.AddInteger(JTI);
791 void *IP = 0;
792 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
793 return SDOperand(E, 0);
794 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
795 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000796 AllNodes.push_back(N);
797 return SDOperand(N, 0);
798}
799
Evan Chengb8973bd2006-01-31 22:23:14 +0000800SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000801 unsigned Alignment, int Offset,
802 bool isTarget) {
803 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000804 FoldingSetNodeID ID;
805 AddNodeIDNode(ID, Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000806 ID.AddInteger(Alignment);
807 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000808 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000809 void *IP = 0;
810 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
811 return SDOperand(E, 0);
812 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
813 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000814 AllNodes.push_back(N);
815 return SDOperand(N, 0);
816}
817
Chris Lattnerc3aae252005-01-07 07:46:32 +0000818
Evan Chengd6594ae2006-09-12 21:00:35 +0000819SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
820 MVT::ValueType VT,
821 unsigned Alignment, int Offset,
822 bool isTarget) {
823 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000824 FoldingSetNodeID ID;
825 AddNodeIDNode(ID, Opc, getVTList(VT));
Evan Chengd6594ae2006-09-12 21:00:35 +0000826 ID.AddInteger(Alignment);
827 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000828 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000829 void *IP = 0;
830 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
831 return SDOperand(E, 0);
832 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
833 CSEMap.InsertNode(N, IP);
834 AllNodes.push_back(N);
835 return SDOperand(N, 0);
836}
837
838
Chris Lattnerc3aae252005-01-07 07:46:32 +0000839SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000840 FoldingSetNodeID ID;
841 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000842 ID.AddPointer(MBB);
843 void *IP = 0;
844 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
845 return SDOperand(E, 0);
846 SDNode *N = new BasicBlockSDNode(MBB);
847 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000848 AllNodes.push_back(N);
849 return SDOperand(N, 0);
850}
851
Chris Lattner15e4b012005-07-10 00:07:11 +0000852SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
853 if ((unsigned)VT >= ValueTypeNodes.size())
854 ValueTypeNodes.resize(VT+1);
855 if (ValueTypeNodes[VT] == 0) {
856 ValueTypeNodes[VT] = new VTSDNode(VT);
857 AllNodes.push_back(ValueTypeNodes[VT]);
858 }
859
860 return SDOperand(ValueTypeNodes[VT], 0);
861}
862
Chris Lattnerc3aae252005-01-07 07:46:32 +0000863SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
864 SDNode *&N = ExternalSymbols[Sym];
865 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000866 N = new ExternalSymbolSDNode(false, Sym, VT);
867 AllNodes.push_back(N);
868 return SDOperand(N, 0);
869}
870
Chris Lattner809ec112006-01-28 10:09:25 +0000871SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
872 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000873 SDNode *&N = TargetExternalSymbols[Sym];
874 if (N) return SDOperand(N, 0);
875 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000876 AllNodes.push_back(N);
877 return SDOperand(N, 0);
878}
879
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000880SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
881 if ((unsigned)Cond >= CondCodeNodes.size())
882 CondCodeNodes.resize(Cond+1);
883
Chris Lattner079a27a2005-08-09 20:40:02 +0000884 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000885 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000886 AllNodes.push_back(CondCodeNodes[Cond]);
887 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000888 return SDOperand(CondCodeNodes[Cond], 0);
889}
890
Chris Lattner0fdd7682005-08-30 22:38:38 +0000891SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000892 FoldingSetNodeID ID;
893 AddNodeIDNode(ID, ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000894 ID.AddInteger(RegNo);
895 void *IP = 0;
896 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
897 return SDOperand(E, 0);
898 SDNode *N = new RegisterSDNode(RegNo, VT);
899 CSEMap.InsertNode(N, IP);
900 AllNodes.push_back(N);
901 return SDOperand(N, 0);
902}
903
904SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
905 assert((!V || isa<PointerType>(V->getType())) &&
906 "SrcValue is not a pointer?");
907
Jim Laskey583bd472006-10-27 23:46:08 +0000908 FoldingSetNodeID ID;
909 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000910 ID.AddPointer(V);
911 ID.AddInteger(Offset);
912 void *IP = 0;
913 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
914 return SDOperand(E, 0);
915 SDNode *N = new SrcValueSDNode(V, Offset);
916 CSEMap.InsertNode(N, IP);
917 AllNodes.push_back(N);
918 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000919}
920
Chris Lattner51dabfb2006-10-14 00:41:01 +0000921SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
922 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 // These setcc operations always fold.
924 switch (Cond) {
925 default: break;
926 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000927 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000929 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000930
931 case ISD::SETOEQ:
932 case ISD::SETOGT:
933 case ISD::SETOGE:
934 case ISD::SETOLT:
935 case ISD::SETOLE:
936 case ISD::SETONE:
937 case ISD::SETO:
938 case ISD::SETUO:
939 case ISD::SETUEQ:
940 case ISD::SETUNE:
941 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
942 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000943 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000944
Chris Lattner67255a12005-04-07 18:14:58 +0000945 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
946 uint64_t C2 = N2C->getValue();
947 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
948 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000949
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950 // Sign extend the operands if required
951 if (ISD::isSignedIntSetCC(Cond)) {
952 C1 = N1C->getSignExtended();
953 C2 = N2C->getSignExtended();
954 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000955
Chris Lattnerc3aae252005-01-07 07:46:32 +0000956 switch (Cond) {
957 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000958 case ISD::SETEQ: return getConstant(C1 == C2, VT);
959 case ISD::SETNE: return getConstant(C1 != C2, VT);
960 case ISD::SETULT: return getConstant(C1 < C2, VT);
961 case ISD::SETUGT: return getConstant(C1 > C2, VT);
962 case ISD::SETULE: return getConstant(C1 <= C2, VT);
963 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
964 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
965 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
966 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
967 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000968 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000969 }
Chris Lattner67255a12005-04-07 18:14:58 +0000970 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
972 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
973 double C1 = N1C->getValue(), C2 = N2C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000974
Chris Lattnerc3aae252005-01-07 07:46:32 +0000975 switch (Cond) {
976 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000977 case ISD::SETEQ: return getConstant(C1 == C2, VT);
978 case ISD::SETNE: return getConstant(C1 != C2, VT);
979 case ISD::SETLT: return getConstant(C1 < C2, VT);
980 case ISD::SETGT: return getConstant(C1 > C2, VT);
981 case ISD::SETLE: return getConstant(C1 <= C2, VT);
982 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983 }
984 } else {
985 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000986 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000988
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000989 // Could not fold it.
990 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000991}
992
Chris Lattner51dabfb2006-10-14 00:41:01 +0000993
Chris Lattnerc3aae252005-01-07 07:46:32 +0000994/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000995///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000997 FoldingSetNodeID ID;
998 AddNodeIDNode(ID, Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000999 void *IP = 0;
1000 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1001 return SDOperand(E, 0);
1002 SDNode *N = new SDNode(Opcode, VT);
1003 CSEMap.InsertNode(N, IP);
1004
1005 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001006 return SDOperand(N, 0);
1007}
1008
Chris Lattnerc3aae252005-01-07 07:46:32 +00001009SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1010 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001011 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001012 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1014 uint64_t Val = C->getValue();
1015 switch (Opcode) {
1016 default: break;
1017 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001018 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001019 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1020 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001021 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1022 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001023 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001024 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001025 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001026 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001027 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001028 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001029 case ISD::BSWAP:
1030 switch(VT) {
1031 default: assert(0 && "Invalid bswap!"); break;
1032 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1033 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1034 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1035 }
1036 break;
1037 case ISD::CTPOP:
1038 switch(VT) {
1039 default: assert(0 && "Invalid ctpop!"); break;
1040 case MVT::i1: return getConstant(Val != 0, VT);
1041 case MVT::i8:
1042 Tmp1 = (unsigned)Val & 0xFF;
1043 return getConstant(CountPopulation_32(Tmp1), VT);
1044 case MVT::i16:
1045 Tmp1 = (unsigned)Val & 0xFFFF;
1046 return getConstant(CountPopulation_32(Tmp1), VT);
1047 case MVT::i32:
1048 return getConstant(CountPopulation_32((unsigned)Val), VT);
1049 case MVT::i64:
1050 return getConstant(CountPopulation_64(Val), VT);
1051 }
1052 case ISD::CTLZ:
1053 switch(VT) {
1054 default: assert(0 && "Invalid ctlz!"); break;
1055 case MVT::i1: return getConstant(Val == 0, VT);
1056 case MVT::i8:
1057 Tmp1 = (unsigned)Val & 0xFF;
1058 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1059 case MVT::i16:
1060 Tmp1 = (unsigned)Val & 0xFFFF;
1061 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1062 case MVT::i32:
1063 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1064 case MVT::i64:
1065 return getConstant(CountLeadingZeros_64(Val), VT);
1066 }
1067 case ISD::CTTZ:
1068 switch(VT) {
1069 default: assert(0 && "Invalid cttz!"); break;
1070 case MVT::i1: return getConstant(Val == 0, VT);
1071 case MVT::i8:
1072 Tmp1 = (unsigned)Val | 0x100;
1073 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1074 case MVT::i16:
1075 Tmp1 = (unsigned)Val | 0x10000;
1076 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1077 case MVT::i32:
1078 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1079 case MVT::i64:
1080 return getConstant(CountTrailingZeros_64(Val), VT);
1081 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 }
1083 }
1084
Chris Lattner94683772005-12-23 05:30:37 +00001085 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1087 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001088 case ISD::FNEG:
1089 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001090 case ISD::FABS:
1091 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092 case ISD::FP_ROUND:
1093 case ISD::FP_EXTEND:
1094 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001095 case ISD::FP_TO_SINT:
1096 return getConstant((int64_t)C->getValue(), VT);
1097 case ISD::FP_TO_UINT:
1098 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001099 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001100 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001101 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001102 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001103 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001104 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105 }
1106
1107 unsigned OpOpcode = Operand.Val->getOpcode();
1108 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001109 case ISD::TokenFactor:
1110 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001111 case ISD::SIGN_EXTEND:
1112 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001113 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001114 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1115 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1116 break;
1117 case ISD::ZERO_EXTEND:
1118 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001119 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001120 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001121 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001122 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001123 case ISD::ANY_EXTEND:
1124 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001125 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001126 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1127 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1128 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1129 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001130 case ISD::TRUNCATE:
1131 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001132 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133 if (OpOpcode == ISD::TRUNCATE)
1134 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001135 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1136 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001137 // If the source is smaller than the dest, we still need an extend.
1138 if (Operand.Val->getOperand(0).getValueType() < VT)
1139 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1140 else if (Operand.Val->getOperand(0).getValueType() > VT)
1141 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1142 else
1143 return Operand.Val->getOperand(0);
1144 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 break;
Chris Lattner35481892005-12-23 00:16:34 +00001146 case ISD::BIT_CONVERT:
1147 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001148 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001149 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001150 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001151 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1152 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001153 if (OpOpcode == ISD::UNDEF)
1154 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001155 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001156 case ISD::SCALAR_TO_VECTOR:
1157 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1158 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1159 "Illegal SCALAR_TO_VECTOR node!");
1160 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001161 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001162 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1163 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001164 Operand.Val->getOperand(0));
1165 if (OpOpcode == ISD::FNEG) // --X -> X
1166 return Operand.Val->getOperand(0);
1167 break;
1168 case ISD::FABS:
1169 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1170 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1171 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 }
1173
Chris Lattner43247a12005-08-25 19:12:10 +00001174 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001175 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001176 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001177 FoldingSetNodeID ID;
1178 AddNodeIDNode(ID, Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001179 void *IP = 0;
1180 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1181 return SDOperand(E, 0);
1182 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001183 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001184 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001185 } else {
1186 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001187 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001188 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001189 AllNodes.push_back(N);
1190 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001191}
1192
Chris Lattner36019aa2005-04-18 03:48:41 +00001193
1194
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1196 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001197#ifndef NDEBUG
1198 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001199 case ISD::TokenFactor:
1200 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1201 N2.getValueType() == MVT::Other && "Invalid token factor!");
1202 break;
Chris Lattner76365122005-01-16 02:23:22 +00001203 case ISD::AND:
1204 case ISD::OR:
1205 case ISD::XOR:
1206 case ISD::UDIV:
1207 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001208 case ISD::MULHU:
1209 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001210 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1211 // fall through
1212 case ISD::ADD:
1213 case ISD::SUB:
1214 case ISD::MUL:
1215 case ISD::SDIV:
1216 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001217 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1218 // fall through.
1219 case ISD::FADD:
1220 case ISD::FSUB:
1221 case ISD::FMUL:
1222 case ISD::FDIV:
1223 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001224 assert(N1.getValueType() == N2.getValueType() &&
1225 N1.getValueType() == VT && "Binary operator types must match!");
1226 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001227 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1228 assert(N1.getValueType() == VT &&
1229 MVT::isFloatingPoint(N1.getValueType()) &&
1230 MVT::isFloatingPoint(N2.getValueType()) &&
1231 "Invalid FCOPYSIGN!");
1232 break;
Chris Lattner76365122005-01-16 02:23:22 +00001233 case ISD::SHL:
1234 case ISD::SRA:
1235 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001236 case ISD::ROTL:
1237 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001238 assert(VT == N1.getValueType() &&
1239 "Shift operators return type must be the same as their first arg");
1240 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001241 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001242 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001243 case ISD::FP_ROUND_INREG: {
1244 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1245 assert(VT == N1.getValueType() && "Not an inreg round!");
1246 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1247 "Cannot FP_ROUND_INREG integer types");
1248 assert(EVT <= VT && "Not rounding down!");
1249 break;
1250 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001251 case ISD::AssertSext:
1252 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001253 case ISD::SIGN_EXTEND_INREG: {
1254 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1255 assert(VT == N1.getValueType() && "Not an inreg extend!");
1256 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1257 "Cannot *_EXTEND_INREG FP types");
1258 assert(EVT <= VT && "Not extending!");
1259 }
1260
Chris Lattner76365122005-01-16 02:23:22 +00001261 default: break;
1262 }
1263#endif
1264
Chris Lattnerc3aae252005-01-07 07:46:32 +00001265 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1266 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1267 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001268 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1269 int64_t Val = N1C->getValue();
1270 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1271 Val <<= 64-FromBits;
1272 Val >>= 64-FromBits;
1273 return getConstant(Val, VT);
1274 }
1275
Chris Lattnerc3aae252005-01-07 07:46:32 +00001276 if (N2C) {
1277 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1278 switch (Opcode) {
1279 case ISD::ADD: return getConstant(C1 + C2, VT);
1280 case ISD::SUB: return getConstant(C1 - C2, VT);
1281 case ISD::MUL: return getConstant(C1 * C2, VT);
1282 case ISD::UDIV:
1283 if (C2) return getConstant(C1 / C2, VT);
1284 break;
1285 case ISD::UREM :
1286 if (C2) return getConstant(C1 % C2, VT);
1287 break;
1288 case ISD::SDIV :
1289 if (C2) return getConstant(N1C->getSignExtended() /
1290 N2C->getSignExtended(), VT);
1291 break;
1292 case ISD::SREM :
1293 if (C2) return getConstant(N1C->getSignExtended() %
1294 N2C->getSignExtended(), VT);
1295 break;
1296 case ISD::AND : return getConstant(C1 & C2, VT);
1297 case ISD::OR : return getConstant(C1 | C2, VT);
1298 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001299 case ISD::SHL : return getConstant(C1 << C2, VT);
1300 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001301 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001302 case ISD::ROTL :
1303 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1304 VT);
1305 case ISD::ROTR :
1306 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1307 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001308 default: break;
1309 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001310 } else { // Cannonicalize constant to RHS if commutative
1311 if (isCommutativeBinOp(Opcode)) {
1312 std::swap(N1C, N2C);
1313 std::swap(N1, N2);
1314 }
1315 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001316 }
1317
1318 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1319 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001320 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 if (N2CFP) {
1322 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1323 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001324 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1325 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1326 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1327 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001328 if (C2) return getConstantFP(C1 / C2, VT);
1329 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001330 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001331 if (C2) return getConstantFP(fmod(C1, C2), VT);
1332 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001333 case ISD::FCOPYSIGN: {
1334 union {
1335 double F;
1336 uint64_t I;
1337 } u1;
1338 union {
1339 double F;
1340 int64_t I;
1341 } u2;
1342 u1.F = C1;
1343 u2.F = C2;
1344 if (u2.I < 0) // Sign bit of RHS set?
1345 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1346 else
1347 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1348 return getConstantFP(u1.F, VT);
1349 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001350 default: break;
1351 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001352 } else { // Cannonicalize constant to RHS if commutative
1353 if (isCommutativeBinOp(Opcode)) {
1354 std::swap(N1CFP, N2CFP);
1355 std::swap(N1, N2);
1356 }
1357 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001358 }
Chris Lattner62b57722006-04-20 05:39:12 +00001359
1360 // Canonicalize an UNDEF to the RHS, even over a constant.
1361 if (N1.getOpcode() == ISD::UNDEF) {
1362 if (isCommutativeBinOp(Opcode)) {
1363 std::swap(N1, N2);
1364 } else {
1365 switch (Opcode) {
1366 case ISD::FP_ROUND_INREG:
1367 case ISD::SIGN_EXTEND_INREG:
1368 case ISD::SUB:
1369 case ISD::FSUB:
1370 case ISD::FDIV:
1371 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001372 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001373 return N1; // fold op(undef, arg2) -> undef
1374 case ISD::UDIV:
1375 case ISD::SDIV:
1376 case ISD::UREM:
1377 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001378 case ISD::SRL:
1379 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001380 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1381 }
1382 }
1383 }
1384
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001385 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001386 if (N2.getOpcode() == ISD::UNDEF) {
1387 switch (Opcode) {
1388 case ISD::ADD:
1389 case ISD::SUB:
1390 case ISD::FADD:
1391 case ISD::FSUB:
1392 case ISD::FMUL:
1393 case ISD::FDIV:
1394 case ISD::FREM:
1395 case ISD::UDIV:
1396 case ISD::SDIV:
1397 case ISD::UREM:
1398 case ISD::SREM:
1399 case ISD::XOR:
1400 return N2; // fold op(arg1, undef) -> undef
1401 case ISD::MUL:
1402 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001403 case ISD::SRL:
1404 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001405 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1406 case ISD::OR:
1407 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001408 case ISD::SRA:
1409 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001410 }
1411 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001412
Chris Lattner51dabfb2006-10-14 00:41:01 +00001413 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001414 switch (Opcode) {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001415 case ISD::AND:
1416 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
1417 // worth handling here.
1418 if (N2C && N2C->getValue() == 0)
1419 return N2;
1420 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00001421 case ISD::OR:
1422 case ISD::XOR:
1423 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
1424 // worth handling here.
1425 if (N2C && N2C->getValue() == 0)
1426 return N1;
1427 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001428 case ISD::FP_ROUND_INREG:
1429 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1430 break;
1431 case ISD::SIGN_EXTEND_INREG: {
1432 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1433 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001434 break;
1435 }
Chris Lattner5c6621c2006-09-19 04:51:23 +00001436 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00001437 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
1438
Chris Lattner5c6621c2006-09-19 04:51:23 +00001439 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
1440 // 64-bit integers into 32-bit parts. Instead of building the extract of
1441 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00001442 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00001443 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00001444
1445 // EXTRACT_ELEMENT of a constant int is also very common.
1446 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
1447 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
1448 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00001449 }
1450 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001451
Nate Begemaneea805e2005-04-13 21:23:31 +00001452 // FIXME: figure out how to safely handle things like
1453 // int foo(int x) { return 1 << (x & 255); }
1454 // int bar() { return foo(256); }
1455#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001456 case ISD::SHL:
1457 case ISD::SRL:
1458 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001459 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001460 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001461 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001462 else if (N2.getOpcode() == ISD::AND)
1463 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1464 // If the and is only masking out bits that cannot effect the shift,
1465 // eliminate the and.
1466 unsigned NumBits = MVT::getSizeInBits(VT);
1467 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1468 return getNode(Opcode, VT, N1, N2.getOperand(0));
1469 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001470 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001471#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001472 }
1473
Chris Lattner27e9b412005-05-11 18:57:39 +00001474 // Memoize this node if possible.
1475 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001476 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001477 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001478 FoldingSetNodeID ID;
1479 AddNodeIDNode(ID, Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001480 void *IP = 0;
1481 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1482 return SDOperand(E, 0);
1483 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001484 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001485 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001486 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001487 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001488 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001489 }
1490
Chris Lattnerc3aae252005-01-07 07:46:32 +00001491 AllNodes.push_back(N);
1492 return SDOperand(N, 0);
1493}
1494
Chris Lattnerc3aae252005-01-07 07:46:32 +00001495SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1496 SDOperand N1, SDOperand N2, SDOperand N3) {
1497 // Perform various simplifications.
1498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1499 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001500 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001501 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001502 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00001503 // Use FoldSetCC to simplify SETCC's.
1504 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001505 if (Simp.Val) return Simp;
1506 break;
1507 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001508 case ISD::SELECT:
1509 if (N1C)
1510 if (N1C->getValue())
1511 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001512 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001513 return N3; // select false, X, Y -> Y
1514
1515 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001516 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001517 case ISD::BRCOND:
1518 if (N2C)
1519 if (N2C->getValue()) // Unconditional branch
1520 return getNode(ISD::BR, MVT::Other, N1, N3);
1521 else
1522 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001523 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001524 case ISD::VECTOR_SHUFFLE:
1525 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1526 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1527 N3.getOpcode() == ISD::BUILD_VECTOR &&
1528 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1529 "Illegal VECTOR_SHUFFLE node!");
1530 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001531 }
1532
Chris Lattner43247a12005-08-25 19:12:10 +00001533 // Memoize node if it doesn't produce a flag.
1534 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001535 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001536 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001537 FoldingSetNodeID ID;
1538 AddNodeIDNode(ID, Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001539 void *IP = 0;
1540 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1541 return SDOperand(E, 0);
1542 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001543 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001544 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001545 } else {
1546 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001547 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001548 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001549 AllNodes.push_back(N);
1550 return SDOperand(N, 0);
1551}
1552
1553SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001554 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001555 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001556 SDOperand Ops[] = { N1, N2, N3, N4 };
1557 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001558}
1559
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001560SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1561 SDOperand N1, SDOperand N2, SDOperand N3,
1562 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001563 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1564 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001565}
1566
Evan Cheng7038daf2005-12-10 00:37:58 +00001567SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1568 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00001569 const Value *SV, int SVOffset,
1570 bool isVolatile) {
1571 // FIXME: Alignment == 1 for now.
1572 unsigned Alignment = 1;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001573 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001574 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jim Laskey583bd472006-10-27 23:46:08 +00001575 FoldingSetNodeID ID;
1576 AddNodeIDNode(ID, ISD::LOAD, VTs, Chain, Ptr, Undef);
Evan Cheng466685d2006-10-09 20:57:25 +00001577 ID.AddInteger(ISD::UNINDEXED);
1578 ID.AddInteger(ISD::NON_EXTLOAD);
1579 ID.AddInteger(VT);
1580 ID.AddPointer(SV);
1581 ID.AddInteger(SVOffset);
1582 ID.AddInteger(Alignment);
1583 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00001584 void *IP = 0;
1585 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1586 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001587 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED,
1588 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
1589 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00001590 N->setValueTypes(VTs);
1591 CSEMap.InsertNode(N, IP);
1592 AllNodes.push_back(N);
1593 return SDOperand(N, 0);
1594}
1595
1596SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
1597 SDOperand Chain, SDOperand Ptr, const Value *SV,
1598 int SVOffset, MVT::ValueType EVT,
1599 bool isVolatile) {
1600 // If they are asking for an extending load from/to the same thing, return a
1601 // normal load.
1602 if (VT == EVT)
1603 ExtType = ISD::NON_EXTLOAD;
1604
1605 if (MVT::isVector(VT))
1606 assert(EVT == MVT::getVectorBaseType(VT) && "Invalid vector extload!");
1607 else
1608 assert(EVT < VT && "Should only be an extending load, not truncating!");
1609 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1610 "Cannot sign/zero extend a FP/Vector load!");
1611 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1612 "Cannot convert from FP to Int or Int -> FP!");
1613
1614 // FIXME: Alignment == 1 for now.
1615 unsigned Alignment = 1;
1616 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001617 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jim Laskey583bd472006-10-27 23:46:08 +00001618 FoldingSetNodeID ID;
1619 AddNodeIDNode(ID, ISD::LOAD, VTs, Chain, Ptr, Undef);
Evan Cheng466685d2006-10-09 20:57:25 +00001620 ID.AddInteger(ISD::UNINDEXED);
1621 ID.AddInteger(ExtType);
1622 ID.AddInteger(EVT);
1623 ID.AddPointer(SV);
1624 ID.AddInteger(SVOffset);
1625 ID.AddInteger(Alignment);
1626 ID.AddInteger(isVolatile);
1627 void *IP = 0;
1628 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1629 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001630 SDNode *N = new LoadSDNode(Chain, Ptr, Undef, ISD::UNINDEXED, ExtType, EVT,
1631 SV, SVOffset, Alignment, isVolatile);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001632 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001633 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001634 AllNodes.push_back(N);
1635 return SDOperand(N, 0);
1636}
1637
Evan Cheng5270cf12006-10-26 21:53:40 +00001638SDOperand SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
1639 SDOperand Offset, ISD::MemOpAddrMode AM){
Evan Cheng2caccca2006-10-17 21:14:32 +00001640 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00001641 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
1642 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00001643 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00001644 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Jim Laskey583bd472006-10-27 23:46:08 +00001645 FoldingSetNodeID ID;
1646 AddNodeIDNode(ID, ISD::LOAD, VTs, LD->getChain(), Base, Offset);
Evan Cheng2caccca2006-10-17 21:14:32 +00001647 ID.AddInteger(AM);
1648 ID.AddInteger(LD->getExtensionType());
1649 ID.AddInteger(LD->getLoadedVT());
1650 ID.AddPointer(LD->getSrcValue());
1651 ID.AddInteger(LD->getSrcValueOffset());
1652 ID.AddInteger(LD->getAlignment());
1653 ID.AddInteger(LD->isVolatile());
1654 void *IP = 0;
1655 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1656 return SDOperand(E, 0);
Evan Cheng5270cf12006-10-26 21:53:40 +00001657 SDNode *N = new LoadSDNode(LD->getChain(), Base, Offset, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00001658 LD->getExtensionType(), LD->getLoadedVT(),
1659 LD->getSrcValue(), LD->getSrcValueOffset(),
1660 LD->getAlignment(), LD->isVolatile());
1661 N->setValueTypes(VTs);
1662 CSEMap.InsertNode(N, IP);
1663 AllNodes.push_back(N);
1664 return SDOperand(N, 0);
1665}
1666
Evan Cheng7038daf2005-12-10 00:37:58 +00001667SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1668 SDOperand Chain, SDOperand Ptr,
1669 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001670 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1671 getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001672 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001673}
1674
Evan Chengad071e12006-10-05 22:57:11 +00001675SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Value,
Evan Cheng8b2794a2006-10-13 21:14:26 +00001676 SDOperand Ptr, const Value *SV, int SVOffset,
1677 bool isVolatile) {
1678 MVT::ValueType VT = Value.getValueType();
1679
1680 // FIXME: Alignment == 1 for now.
1681 unsigned Alignment = 1;
Evan Chengad071e12006-10-05 22:57:11 +00001682 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001683 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1684 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001685 FoldingSetNodeID ID;
1686 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001687 ID.AddInteger(ISD::UNINDEXED);
1688 ID.AddInteger(false);
1689 ID.AddInteger(VT);
1690 ID.AddPointer(SV);
1691 ID.AddInteger(SVOffset);
1692 ID.AddInteger(Alignment);
1693 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001694 void *IP = 0;
1695 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1696 return SDOperand(E, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001697 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, false,
1698 VT, SV, SVOffset, Alignment, isVolatile);
1699 N->setValueTypes(VTs);
1700 CSEMap.InsertNode(N, IP);
1701 AllNodes.push_back(N);
1702 return SDOperand(N, 0);
1703}
1704
1705SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Value,
1706 SDOperand Ptr, const Value *SV,
1707 int SVOffset, MVT::ValueType SVT,
1708 bool isVolatile) {
1709 MVT::ValueType VT = Value.getValueType();
1710 bool isTrunc = VT != SVT;
1711
1712 assert(VT > SVT && "Not a truncation?");
1713 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
1714 "Can't do FP-INT conversion!");
1715
1716 // FIXME: Alignment == 1 for now.
1717 unsigned Alignment = 1;
1718 SDVTList VTs = getVTList(MVT::Other);
1719 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
1720 SDOperand Ops[] = { Chain, Value, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00001721 FoldingSetNodeID ID;
1722 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001723 ID.AddInteger(ISD::UNINDEXED);
1724 ID.AddInteger(isTrunc);
1725 ID.AddInteger(SVT);
1726 ID.AddPointer(SV);
1727 ID.AddInteger(SVOffset);
1728 ID.AddInteger(Alignment);
1729 ID.AddInteger(isVolatile);
1730 void *IP = 0;
1731 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1732 return SDOperand(E, 0);
1733 SDNode *N = new StoreSDNode(Chain, Value, Ptr, Undef, ISD::UNINDEXED, isTrunc,
1734 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00001735 N->setValueTypes(VTs);
1736 CSEMap.InsertNode(N, IP);
1737 AllNodes.push_back(N);
1738 return SDOperand(N, 0);
1739}
1740
Nate Begemanacc398c2006-01-25 18:21:52 +00001741SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1742 SDOperand Chain, SDOperand Ptr,
1743 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001744 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001745 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001746}
1747
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001748SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001749 const SDOperand *Ops, unsigned NumOps) {
1750 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001751 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001752 case 1: return getNode(Opcode, VT, Ops[0]);
1753 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1754 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001755 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001756 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001757
Chris Lattneref847df2005-04-09 03:27:28 +00001758 switch (Opcode) {
1759 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001760 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001761 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001762 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1763 "LHS and RHS of condition must have same type!");
1764 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1765 "True and False arms of SelectCC must have same type!");
1766 assert(Ops[2].getValueType() == VT &&
1767 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001768 break;
1769 }
1770 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001771 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001772 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1773 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001774 break;
1775 }
Chris Lattneref847df2005-04-09 03:27:28 +00001776 }
1777
Chris Lattner385328c2005-05-14 07:42:29 +00001778 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001779 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001780 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001781 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001782 FoldingSetNodeID ID;
1783 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001784 void *IP = 0;
1785 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1786 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001787 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001788 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001789 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001790 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001791 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001792 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001793 }
Chris Lattneref847df2005-04-09 03:27:28 +00001794 AllNodes.push_back(N);
1795 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001796}
1797
Chris Lattner89c34632005-05-14 06:20:26 +00001798SDOperand SelectionDAG::getNode(unsigned Opcode,
1799 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001800 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001801 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1802 Ops, NumOps);
1803}
1804
1805SDOperand SelectionDAG::getNode(unsigned Opcode,
1806 const MVT::ValueType *VTs, unsigned NumVTs,
1807 const SDOperand *Ops, unsigned NumOps) {
1808 if (NumVTs == 1)
1809 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001810 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1811}
1812
1813SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1814 const SDOperand *Ops, unsigned NumOps) {
1815 if (VTList.NumVTs == 1)
1816 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001817
Chris Lattner5f056bf2005-07-10 01:55:33 +00001818 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00001819 // FIXME: figure out how to safely handle things like
1820 // int foo(int x) { return 1 << (x & 255); }
1821 // int bar() { return foo(256); }
1822#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001823 case ISD::SRA_PARTS:
1824 case ISD::SRL_PARTS:
1825 case ISD::SHL_PARTS:
1826 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001827 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001828 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1829 else if (N3.getOpcode() == ISD::AND)
1830 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1831 // If the and is only masking out bits that cannot effect the shift,
1832 // eliminate the and.
1833 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1834 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1835 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1836 }
1837 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001838#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001839 }
Chris Lattner89c34632005-05-14 06:20:26 +00001840
Chris Lattner43247a12005-08-25 19:12:10 +00001841 // Memoize the node unless it returns a flag.
1842 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001843 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00001844 FoldingSetNodeID ID;
1845 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001846 void *IP = 0;
1847 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1848 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001849 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001850 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001851 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001852 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001853 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001854 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001855 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001856 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001857 return SDOperand(N, 0);
1858}
1859
Chris Lattner70046e92006-08-15 17:46:01 +00001860SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1861 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001862}
1863
Chris Lattner70046e92006-08-15 17:46:01 +00001864SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001865 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1866 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001867 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001868 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001869 }
1870 std::vector<MVT::ValueType> V;
1871 V.push_back(VT1);
1872 V.push_back(VT2);
1873 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001874 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001875}
Chris Lattner70046e92006-08-15 17:46:01 +00001876SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1877 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001878 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001879 E = VTList.end(); I != E; ++I) {
1880 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1881 (*I)[2] == VT3)
1882 return makeVTList(&(*I)[0], 3);
1883 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001884 std::vector<MVT::ValueType> V;
1885 V.push_back(VT1);
1886 V.push_back(VT2);
1887 V.push_back(VT3);
1888 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001889 return makeVTList(&(*VTList.begin())[0], 3);
1890}
1891
1892SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1893 switch (NumVTs) {
1894 case 0: assert(0 && "Cannot have nodes without results!");
1895 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1896 case 2: return getVTList(VTs[0], VTs[1]);
1897 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1898 default: break;
1899 }
1900
1901 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1902 E = VTList.end(); I != E; ++I) {
1903 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1904
1905 bool NoMatch = false;
1906 for (unsigned i = 2; i != NumVTs; ++i)
1907 if (VTs[i] != (*I)[i]) {
1908 NoMatch = true;
1909 break;
1910 }
1911 if (!NoMatch)
1912 return makeVTList(&*I->begin(), NumVTs);
1913 }
1914
1915 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1916 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001917}
1918
1919
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001920/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1921/// specified operands. If the resultant node already exists in the DAG,
1922/// this does not modify the specified node, instead it returns the node that
1923/// already exists. If the resultant node does not exist in the DAG, the
1924/// input node is returned. As a degenerate case, if you specify the same
1925/// input operands as the node already has, the input node is returned.
1926SDOperand SelectionDAG::
1927UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1928 SDNode *N = InN.Val;
1929 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1930
1931 // Check to see if there is no change.
1932 if (Op == N->getOperand(0)) return InN;
1933
1934 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001935 void *InsertPos = 0;
1936 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1937 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001938
1939 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001940 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001941 RemoveNodeFromCSEMaps(N);
1942
1943 // Now we update the operands.
1944 N->OperandList[0].Val->removeUser(N);
1945 Op.Val->addUser(N);
1946 N->OperandList[0] = Op;
1947
1948 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001949 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001950 return InN;
1951}
1952
1953SDOperand SelectionDAG::
1954UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1955 SDNode *N = InN.Val;
1956 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1957
1958 // Check to see if there is no change.
1959 bool AnyChange = false;
1960 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1961 return InN; // No operands changed, just return the input node.
1962
1963 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001964 void *InsertPos = 0;
1965 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1966 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001967
1968 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001969 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001970 RemoveNodeFromCSEMaps(N);
1971
1972 // Now we update the operands.
1973 if (N->OperandList[0] != Op1) {
1974 N->OperandList[0].Val->removeUser(N);
1975 Op1.Val->addUser(N);
1976 N->OperandList[0] = Op1;
1977 }
1978 if (N->OperandList[1] != Op2) {
1979 N->OperandList[1].Val->removeUser(N);
1980 Op2.Val->addUser(N);
1981 N->OperandList[1] = Op2;
1982 }
1983
1984 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001985 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001986 return InN;
1987}
1988
1989SDOperand SelectionDAG::
1990UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001991 SDOperand Ops[] = { Op1, Op2, Op3 };
1992 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001993}
1994
1995SDOperand SelectionDAG::
1996UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1997 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001998 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1999 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002000}
2001
2002SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002003UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2004 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002005 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2006 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002007}
2008
2009
2010SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002011UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002012 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002013 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002014 "Update with wrong number of operands");
2015
2016 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002017 bool AnyChange = false;
2018 for (unsigned i = 0; i != NumOps; ++i) {
2019 if (Ops[i] != N->getOperand(i)) {
2020 AnyChange = true;
2021 break;
2022 }
2023 }
2024
2025 // No operands changed, just return the input node.
2026 if (!AnyChange) return InN;
2027
2028 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002029 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002030 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002031 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002032
2033 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002034 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002035 RemoveNodeFromCSEMaps(N);
2036
2037 // Now we update the operands.
2038 for (unsigned i = 0; i != NumOps; ++i) {
2039 if (N->OperandList[i] != Ops[i]) {
2040 N->OperandList[i].Val->removeUser(N);
2041 Ops[i].Val->addUser(N);
2042 N->OperandList[i] = Ops[i];
2043 }
2044 }
2045
2046 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002047 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002048 return InN;
2049}
2050
2051
2052
Chris Lattner149c58c2005-08-16 18:17:10 +00002053
2054/// SelectNodeTo - These are used for target selectors to *mutate* the
2055/// specified node to have the specified return type, Target opcode, and
2056/// operands. Note that target opcodes are stored as
2057/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002058///
2059/// Note that SelectNodeTo returns the resultant node. If there is already a
2060/// node of the specified opcode and operands, it returns that node instead of
2061/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002062SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2063 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002064 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002065 FoldingSetNodeID ID;
2066 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs);
Chris Lattner4a283e92006-08-11 18:38:11 +00002067 void *IP = 0;
2068 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002069 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002070
Chris Lattner7651fa42005-08-24 23:00:29 +00002071 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002072
Chris Lattner7651fa42005-08-24 23:00:29 +00002073 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002074 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002075
Chris Lattner4a283e92006-08-11 18:38:11 +00002076 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002077 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002078}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002079
Evan Cheng95514ba2006-08-26 08:00:10 +00002080SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2081 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002082 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002083 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002084 FoldingSetNodeID ID;
2085 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00002086 void *IP = 0;
2087 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002088 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002089
Chris Lattner149c58c2005-08-16 18:17:10 +00002090 RemoveNodeFromCSEMaps(N);
2091 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002092 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002093 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00002094 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002095 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002096}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002097
Evan Cheng95514ba2006-08-26 08:00:10 +00002098SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2099 MVT::ValueType VT, SDOperand Op1,
2100 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002101 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002102 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002103 FoldingSetNodeID ID;
2104 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +00002105 void *IP = 0;
2106 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002107 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002108
Chris Lattner149c58c2005-08-16 18:17:10 +00002109 RemoveNodeFromCSEMaps(N);
2110 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002111 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002112 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002113
Chris Lattnera5682852006-08-07 23:03:03 +00002114 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002115 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002116}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002117
Evan Cheng95514ba2006-08-26 08:00:10 +00002118SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2119 MVT::ValueType VT, SDOperand Op1,
2120 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002121 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002122 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002123 FoldingSetNodeID ID;
2124 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00002125 void *IP = 0;
2126 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002127 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002128
Chris Lattner149c58c2005-08-16 18:17:10 +00002129 RemoveNodeFromCSEMaps(N);
2130 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002131 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00002132 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002133
Chris Lattnera5682852006-08-07 23:03:03 +00002134 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002135 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002136}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002137
Evan Cheng95514ba2006-08-26 08:00:10 +00002138SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002139 MVT::ValueType VT, const SDOperand *Ops,
2140 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002141 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002142 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002143 FoldingSetNodeID ID;
2144 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002145 void *IP = 0;
2146 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002147 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002148
Chris Lattner6b09a292005-08-21 18:49:33 +00002149 RemoveNodeFromCSEMaps(N);
2150 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002151 N->setValueTypes(VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00002152 N->setOperands(Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002153
Chris Lattnera5682852006-08-07 23:03:03 +00002154 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002155 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002156}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002157
Evan Cheng95514ba2006-08-26 08:00:10 +00002158SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2159 MVT::ValueType VT1, MVT::ValueType VT2,
2160 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002161 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002162 FoldingSetNodeID ID;
2163 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
Chris Lattnera5682852006-08-07 23:03:03 +00002164 void *IP = 0;
2165 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002166 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002167
Chris Lattner0fb094f2005-11-19 01:44:53 +00002168 RemoveNodeFromCSEMaps(N);
2169 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002170 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002171 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002172
Chris Lattnera5682852006-08-07 23:03:03 +00002173 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002174 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002175}
2176
Evan Cheng95514ba2006-08-26 08:00:10 +00002177SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2178 MVT::ValueType VT1, MVT::ValueType VT2,
2179 SDOperand Op1, SDOperand Op2,
2180 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002181 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002182 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002183 FoldingSetNodeID ID;
2184 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00002185 void *IP = 0;
2186 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002187 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002188
Chris Lattner0fb094f2005-11-19 01:44:53 +00002189 RemoveNodeFromCSEMaps(N);
2190 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002191 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002192 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002193
Chris Lattnera5682852006-08-07 23:03:03 +00002194 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002195 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00002196}
2197
Chris Lattner0fb094f2005-11-19 01:44:53 +00002198
Evan Cheng6ae46c42006-02-09 07:15:23 +00002199/// getTargetNode - These are used for target selectors to create a new node
2200/// with specified return type(s), target opcode, and operands.
2201///
2202/// Note that getTargetNode returns the resultant node. If there is already a
2203/// node of the specified opcode and operands, it returns that node instead of
2204/// the current one.
2205SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2206 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2207}
2208SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2209 SDOperand Op1) {
2210 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2211}
2212SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2213 SDOperand Op1, SDOperand Op2) {
2214 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2215}
2216SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2217 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2218 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2219}
2220SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002221 const SDOperand *Ops, unsigned NumOps) {
2222 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002223}
2224SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2225 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002226 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002227 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002228}
2229SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002230 MVT::ValueType VT2, SDOperand Op1,
2231 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002232 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002233 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002234 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002235}
2236SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002237 MVT::ValueType VT2, SDOperand Op1,
2238 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002239 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002240 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002241 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002242}
Evan Cheng694481e2006-08-27 08:08:54 +00002243SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2244 MVT::ValueType VT2,
2245 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002246 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002247 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002248}
2249SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2250 MVT::ValueType VT2, MVT::ValueType VT3,
2251 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002252 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002253 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002254 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002255}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002256SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002257 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002258 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002259 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2260 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002261}
2262
Evan Cheng99157a02006-08-07 22:13:29 +00002263/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002264/// This can cause recursive merging of nodes in the DAG.
2265///
Chris Lattner8b52f212005-08-26 18:36:28 +00002266/// This version assumes From/To have a single result value.
2267///
Chris Lattner1e111c72005-09-07 05:37:01 +00002268void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2269 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002270 SDNode *From = FromN.Val, *To = ToN.Val;
2271 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2272 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002273 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002274
Chris Lattner8b8749f2005-08-17 19:00:20 +00002275 while (!From->use_empty()) {
2276 // Process users until they are all gone.
2277 SDNode *U = *From->use_begin();
2278
2279 // This node is about to morph, remove its old self from the CSE maps.
2280 RemoveNodeFromCSEMaps(U);
2281
Chris Lattner65113b22005-11-08 22:07:03 +00002282 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2283 I != E; ++I)
2284 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002285 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002286 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002287 To->addUser(U);
2288 }
2289
2290 // Now that we have modified U, add it back to the CSE maps. If it already
2291 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002292 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2293 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002294 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002295 if (Deleted) Deleted->push_back(U);
2296 DeleteNodeNotInCSEMaps(U);
2297 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002298 }
2299}
2300
2301/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2302/// This can cause recursive merging of nodes in the DAG.
2303///
2304/// This version assumes From/To have matching types and numbers of result
2305/// values.
2306///
Chris Lattner1e111c72005-09-07 05:37:01 +00002307void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2308 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002309 assert(From != To && "Cannot replace uses of with self");
2310 assert(From->getNumValues() == To->getNumValues() &&
2311 "Cannot use this version of ReplaceAllUsesWith!");
2312 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002313 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002314 return;
2315 }
2316
2317 while (!From->use_empty()) {
2318 // Process users until they are all gone.
2319 SDNode *U = *From->use_begin();
2320
2321 // This node is about to morph, remove its old self from the CSE maps.
2322 RemoveNodeFromCSEMaps(U);
2323
Chris Lattner65113b22005-11-08 22:07:03 +00002324 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2325 I != E; ++I)
2326 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002327 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002328 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002329 To->addUser(U);
2330 }
2331
2332 // Now that we have modified U, add it back to the CSE maps. If it already
2333 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002334 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2335 ReplaceAllUsesWith(U, Existing, Deleted);
2336 // U is now dead.
2337 if (Deleted) Deleted->push_back(U);
2338 DeleteNodeNotInCSEMaps(U);
2339 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002340 }
2341}
2342
Chris Lattner8b52f212005-08-26 18:36:28 +00002343/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2344/// This can cause recursive merging of nodes in the DAG.
2345///
2346/// This version can replace From with any result values. To must match the
2347/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002348void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002349 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002350 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002351 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002352 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002353 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002354 return;
2355 }
2356
2357 while (!From->use_empty()) {
2358 // Process users until they are all gone.
2359 SDNode *U = *From->use_begin();
2360
2361 // This node is about to morph, remove its old self from the CSE maps.
2362 RemoveNodeFromCSEMaps(U);
2363
Chris Lattner65113b22005-11-08 22:07:03 +00002364 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2365 I != E; ++I)
2366 if (I->Val == From) {
2367 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002368 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002369 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002370 ToOp.Val->addUser(U);
2371 }
2372
2373 // Now that we have modified U, add it back to the CSE maps. If it already
2374 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002375 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2376 ReplaceAllUsesWith(U, Existing, Deleted);
2377 // U is now dead.
2378 if (Deleted) Deleted->push_back(U);
2379 DeleteNodeNotInCSEMaps(U);
2380 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002381 }
2382}
2383
Chris Lattner012f2412006-02-17 21:58:01 +00002384/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2385/// uses of other values produced by From.Val alone. The Deleted vector is
2386/// handled the same was as for ReplaceAllUsesWith.
2387void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2388 std::vector<SDNode*> &Deleted) {
2389 assert(From != To && "Cannot replace a value with itself");
2390 // Handle the simple, trivial, case efficiently.
2391 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2392 ReplaceAllUsesWith(From, To, &Deleted);
2393 return;
2394 }
2395
2396 // Get all of the users in a nice, deterministically ordered, uniqued set.
2397 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2398
2399 while (!Users.empty()) {
2400 // We know that this user uses some value of From. If it is the right
2401 // value, update it.
2402 SDNode *User = Users.back();
2403 Users.pop_back();
2404
2405 for (SDOperand *Op = User->OperandList,
2406 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2407 if (*Op == From) {
2408 // Okay, we know this user needs to be updated. Remove its old self
2409 // from the CSE maps.
2410 RemoveNodeFromCSEMaps(User);
2411
2412 // Update all operands that match "From".
2413 for (; Op != E; ++Op) {
2414 if (*Op == From) {
2415 From.Val->removeUser(User);
2416 *Op = To;
2417 To.Val->addUser(User);
2418 }
2419 }
2420
2421 // Now that we have modified User, add it back to the CSE maps. If it
2422 // already exists there, recursively merge the results together.
2423 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2424 unsigned NumDeleted = Deleted.size();
2425 ReplaceAllUsesWith(User, Existing, &Deleted);
2426
2427 // User is now dead.
2428 Deleted.push_back(User);
2429 DeleteNodeNotInCSEMaps(User);
2430
2431 // We have to be careful here, because ReplaceAllUsesWith could have
2432 // deleted a user of From, which means there may be dangling pointers
2433 // in the "Users" setvector. Scan over the deleted node pointers and
2434 // remove them from the setvector.
2435 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2436 Users.remove(Deleted[i]);
2437 }
2438 break; // Exit the operand scanning loop.
2439 }
2440 }
2441 }
2442}
2443
Chris Lattner7b2880c2005-08-24 22:44:39 +00002444
Evan Chenge6f35d82006-08-01 08:20:41 +00002445/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2446/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002447unsigned SelectionDAG::AssignNodeIds() {
2448 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002449 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2450 SDNode *N = I;
2451 N->setNodeId(Id++);
2452 }
2453 return Id;
2454}
2455
Evan Chenge6f35d82006-08-01 08:20:41 +00002456/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002457/// based on their topological order. It returns the maximum id and a vector
2458/// of the SDNodes* in assigned order by reference.
2459unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002460 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002461 std::vector<unsigned> InDegree(DAGSize);
2462 std::vector<SDNode*> Sources;
2463
2464 // Use a two pass approach to avoid using a std::map which is slow.
2465 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002466 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2467 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002468 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002469 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002470 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002471 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002472 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002473 }
2474
Evan Cheng99157a02006-08-07 22:13:29 +00002475 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002476 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002477 SDNode *N = Sources.back();
2478 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002479 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002480 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2481 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002482 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002483 if (Degree == 0)
2484 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002485 }
2486 }
2487
Evan Chengc384d6c2006-08-02 22:00:34 +00002488 // Second pass, assign the actual topological order as node ids.
2489 Id = 0;
2490 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2491 TI != TE; ++TI)
2492 (*TI)->setNodeId(Id++);
2493
2494 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002495}
2496
2497
Evan Cheng091cba12006-07-27 06:39:06 +00002498
Jim Laskey58b968b2005-08-17 20:08:02 +00002499//===----------------------------------------------------------------------===//
2500// SDNode Class
2501//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002502
Chris Lattner917d2c92006-07-19 00:00:37 +00002503// Out-of-line virtual method to give class a home.
2504void SDNode::ANCHOR() {
2505}
2506
Jim Laskey583bd472006-10-27 23:46:08 +00002507/// Profile - Gather unique data for the node.
2508///
2509void SDNode::Profile(FoldingSetNodeID &ID) {
2510 AddNodeIDNode(ID, this);
2511}
2512
Chris Lattnera3255112005-11-08 23:30:28 +00002513/// getValueTypeList - Return a pointer to the specified value type.
2514///
2515MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2516 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2517 VTs[VT] = VT;
2518 return &VTs[VT];
2519}
Chris Lattnera5682852006-08-07 23:03:03 +00002520
Chris Lattner5c884562005-01-12 18:37:47 +00002521/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2522/// indicated value. This method ignores uses of other values defined by this
2523/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002524bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002525 assert(Value < getNumValues() && "Bad value!");
2526
2527 // If there is only one value, this is easy.
2528 if (getNumValues() == 1)
2529 return use_size() == NUses;
2530 if (Uses.size() < NUses) return false;
2531
Evan Cheng4ee62112006-02-05 06:29:23 +00002532 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002533
2534 std::set<SDNode*> UsersHandled;
2535
Chris Lattnerf83482d2006-08-16 20:59:32 +00002536 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002537 SDNode *User = *UI;
2538 if (User->getNumOperands() == 1 ||
2539 UsersHandled.insert(User).second) // First time we've seen this?
2540 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2541 if (User->getOperand(i) == TheValue) {
2542 if (NUses == 0)
2543 return false; // too many uses
2544 --NUses;
2545 }
2546 }
2547
2548 // Found exactly the right number of uses?
2549 return NUses == 0;
2550}
2551
2552
Evan Cheng4ee62112006-02-05 06:29:23 +00002553// isOnlyUse - Return true if this node is the only use of N.
2554bool SDNode::isOnlyUse(SDNode *N) const {
2555 bool Seen = false;
2556 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2557 SDNode *User = *I;
2558 if (User == this)
2559 Seen = true;
2560 else
2561 return false;
2562 }
2563
2564 return Seen;
2565}
2566
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002567// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002568bool SDOperand::isOperand(SDNode *N) const {
2569 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2570 if (*this == N->getOperand(i))
2571 return true;
2572 return false;
2573}
2574
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002575bool SDNode::isOperand(SDNode *N) const {
2576 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2577 if (this == N->OperandList[i].Val)
2578 return true;
2579 return false;
2580}
Evan Cheng4ee62112006-02-05 06:29:23 +00002581
Evan Chengc5484282006-10-04 00:56:09 +00002582uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
2583 assert(Num < NumOperands && "Invalid child # of SDNode!");
2584 return cast<ConstantSDNode>(OperandList[Num])->getValue();
2585}
2586
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002587const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002588 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002589 default:
2590 if (getOpcode() < ISD::BUILTIN_OP_END)
2591 return "<<Unknown DAG Node>>";
2592 else {
Evan Cheng72261582005-12-20 06:22:03 +00002593 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002594 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002595 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2596 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002597
Evan Cheng72261582005-12-20 06:22:03 +00002598 TargetLowering &TLI = G->getTargetLoweringInfo();
2599 const char *Name =
2600 TLI.getTargetNodeName(getOpcode());
2601 if (Name) return Name;
2602 }
2603
2604 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002605 }
2606
Andrew Lenharth95762122005-03-31 21:24:06 +00002607 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002608 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002609 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002610 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002611 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002612 case ISD::AssertSext: return "AssertSext";
2613 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002614
2615 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002616 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002617 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002618 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002619
2620 case ISD::Constant: return "Constant";
2621 case ISD::ConstantFP: return "ConstantFP";
2622 case ISD::GlobalAddress: return "GlobalAddress";
2623 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002624 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00002625 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Chris Lattner5839bf22005-08-26 17:15:30 +00002626 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002627 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002628 case ISD::INTRINSIC_WO_CHAIN: {
2629 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2630 return Intrinsic::getName((Intrinsic::ID)IID);
2631 }
2632 case ISD::INTRINSIC_VOID:
2633 case ISD::INTRINSIC_W_CHAIN: {
2634 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002635 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002636 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002637
Chris Lattnerb2827b02006-03-19 00:52:58 +00002638 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002639 case ISD::TargetConstant: return "TargetConstant";
2640 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002641 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2642 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002643 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002644 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002645 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002646
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002647 case ISD::CopyToReg: return "CopyToReg";
2648 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002649 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002650 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002651 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002652 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002653 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002654 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002655
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002656 // Unary operators
2657 case ISD::FABS: return "fabs";
2658 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002659 case ISD::FSQRT: return "fsqrt";
2660 case ISD::FSIN: return "fsin";
2661 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002662 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002663
2664 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002665 case ISD::ADD: return "add";
2666 case ISD::SUB: return "sub";
2667 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002668 case ISD::MULHU: return "mulhu";
2669 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002670 case ISD::SDIV: return "sdiv";
2671 case ISD::UDIV: return "udiv";
2672 case ISD::SREM: return "srem";
2673 case ISD::UREM: return "urem";
2674 case ISD::AND: return "and";
2675 case ISD::OR: return "or";
2676 case ISD::XOR: return "xor";
2677 case ISD::SHL: return "shl";
2678 case ISD::SRA: return "sra";
2679 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002680 case ISD::ROTL: return "rotl";
2681 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002682 case ISD::FADD: return "fadd";
2683 case ISD::FSUB: return "fsub";
2684 case ISD::FMUL: return "fmul";
2685 case ISD::FDIV: return "fdiv";
2686 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002687 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002688 case ISD::VADD: return "vadd";
2689 case ISD::VSUB: return "vsub";
2690 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002691 case ISD::VSDIV: return "vsdiv";
2692 case ISD::VUDIV: return "vudiv";
2693 case ISD::VAND: return "vand";
2694 case ISD::VOR: return "vor";
2695 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002696
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002697 case ISD::SETCC: return "setcc";
2698 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002699 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002700 case ISD::VSELECT: return "vselect";
2701 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2702 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2703 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002704 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002705 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2706 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2707 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2708 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2709 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002710 case ISD::ADDC: return "addc";
2711 case ISD::ADDE: return "adde";
2712 case ISD::SUBC: return "subc";
2713 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002714 case ISD::SHL_PARTS: return "shl_parts";
2715 case ISD::SRA_PARTS: return "sra_parts";
2716 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002717
Chris Lattner7f644642005-04-28 21:44:03 +00002718 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002719 case ISD::SIGN_EXTEND: return "sign_extend";
2720 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002721 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002722 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002723 case ISD::TRUNCATE: return "truncate";
2724 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002725 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002726 case ISD::FP_EXTEND: return "fp_extend";
2727
2728 case ISD::SINT_TO_FP: return "sint_to_fp";
2729 case ISD::UINT_TO_FP: return "uint_to_fp";
2730 case ISD::FP_TO_SINT: return "fp_to_sint";
2731 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002732 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002733
2734 // Control flow instructions
2735 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002736 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00002737 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002738 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002739 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002740 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002741 case ISD::CALLSEQ_START: return "callseq_start";
2742 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002743
2744 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002745 case ISD::LOAD: return "load";
2746 case ISD::STORE: return "store";
2747 case ISD::VLOAD: return "vload";
Nate Begemanacc398c2006-01-25 18:21:52 +00002748 case ISD::VAARG: return "vaarg";
2749 case ISD::VACOPY: return "vacopy";
2750 case ISD::VAEND: return "vaend";
2751 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002752 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002753 case ISD::EXTRACT_ELEMENT: return "extract_element";
2754 case ISD::BUILD_PAIR: return "build_pair";
2755 case ISD::STACKSAVE: return "stacksave";
2756 case ISD::STACKRESTORE: return "stackrestore";
2757
2758 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002759 case ISD::MEMSET: return "memset";
2760 case ISD::MEMCPY: return "memcpy";
2761 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002762
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002763 // Bit manipulation
2764 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002765 case ISD::CTPOP: return "ctpop";
2766 case ISD::CTTZ: return "cttz";
2767 case ISD::CTLZ: return "ctlz";
2768
Chris Lattner36ce6912005-11-29 06:21:05 +00002769 // Debug info
2770 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002771 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002772 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002773
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002774 case ISD::CONDCODE:
2775 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002776 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002777 case ISD::SETOEQ: return "setoeq";
2778 case ISD::SETOGT: return "setogt";
2779 case ISD::SETOGE: return "setoge";
2780 case ISD::SETOLT: return "setolt";
2781 case ISD::SETOLE: return "setole";
2782 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002783
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002784 case ISD::SETO: return "seto";
2785 case ISD::SETUO: return "setuo";
2786 case ISD::SETUEQ: return "setue";
2787 case ISD::SETUGT: return "setugt";
2788 case ISD::SETUGE: return "setuge";
2789 case ISD::SETULT: return "setult";
2790 case ISD::SETULE: return "setule";
2791 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002792
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002793 case ISD::SETEQ: return "seteq";
2794 case ISD::SETGT: return "setgt";
2795 case ISD::SETGE: return "setge";
2796 case ISD::SETLT: return "setlt";
2797 case ISD::SETLE: return "setle";
2798 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002799 }
2800 }
2801}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002802
Evan Cheng2caccca2006-10-17 21:14:32 +00002803const char *SDNode::getAddressingModeName(ISD::MemOpAddrMode AM) {
2804 switch (AM) {
2805 default:
2806 return "";
2807 case ISD::PRE_INC:
2808 return "<pre-inc>";
2809 case ISD::PRE_DEC:
2810 return "<pre-dec>";
2811 case ISD::POST_INC:
2812 return "<post-inc>";
2813 case ISD::POST_DEC:
2814 return "<post-dec>";
2815 }
2816}
2817
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002818void SDNode::dump() const { dump(0); }
2819void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002820 std::cerr << (void*)this << ": ";
2821
2822 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2823 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002824 if (getValueType(i) == MVT::Other)
2825 std::cerr << "ch";
2826 else
2827 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002828 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002829 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002830
2831 std::cerr << " ";
2832 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2833 if (i) std::cerr << ", ";
2834 std::cerr << (void*)getOperand(i).Val;
2835 if (unsigned RN = getOperand(i).ResNo)
2836 std::cerr << ":" << RN;
2837 }
2838
2839 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2840 std::cerr << "<" << CSDN->getValue() << ">";
2841 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2842 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002843 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002844 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002845 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002846 std::cerr << "<";
2847 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002848 if (offset > 0)
2849 std::cerr << " + " << offset;
2850 else
2851 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002852 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002853 std::cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00002854 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
2855 std::cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002856 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002857 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00002858 if (CP->isMachineConstantPoolEntry())
2859 std::cerr << "<" << *CP->getMachineCPVal() << ">";
2860 else
2861 std::cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002862 if (offset > 0)
2863 std::cerr << " + " << offset;
2864 else
2865 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002866 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002867 std::cerr << "<";
2868 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2869 if (LBB)
2870 std::cerr << LBB->getName() << " ";
2871 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002872 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002873 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002874 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2875 } else {
2876 std::cerr << " #" << R->getReg();
2877 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002878 } else if (const ExternalSymbolSDNode *ES =
2879 dyn_cast<ExternalSymbolSDNode>(this)) {
2880 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002881 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2882 if (M->getValue())
2883 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2884 else
2885 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002886 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2887 std::cerr << ":" << getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002888 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
2889 bool doExt = true;
2890 switch (LD->getExtensionType()) {
2891 default: doExt = false; break;
2892 case ISD::EXTLOAD:
2893 std::cerr << " <anyext ";
2894 break;
2895 case ISD::SEXTLOAD:
2896 std::cerr << " <sext ";
2897 break;
2898 case ISD::ZEXTLOAD:
2899 std::cerr << " <zext ";
2900 break;
2901 }
2902 if (doExt)
Evan Cheng2e49f092006-10-11 07:10:22 +00002903 std::cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00002904
Evan Cheng2caccca2006-10-17 21:14:32 +00002905 const char *AM = getAddressingModeName(LD->getAddressingMode());
2906 if (AM != "")
2907 std::cerr << " " << AM;
2908 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
2909 if (ST->isTruncatingStore())
2910 std::cerr << " <trunc "
2911 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
2912
2913 const char *AM = getAddressingModeName(ST->getAddressingMode());
2914 if (AM != "")
2915 std::cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002916 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002917}
2918
Chris Lattnerde202b32005-11-09 23:47:37 +00002919static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002920 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2921 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002922 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002923 else
2924 std::cerr << "\n" << std::string(indent+2, ' ')
2925 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002926
Chris Lattnerea946cd2005-01-09 20:38:33 +00002927
2928 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002929 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002930}
2931
Chris Lattnerc3aae252005-01-07 07:46:32 +00002932void SelectionDAG::dump() const {
2933 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002934 std::vector<const SDNode*> Nodes;
2935 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2936 I != E; ++I)
2937 Nodes.push_back(I);
2938
Chris Lattner49d24712005-01-09 20:26:36 +00002939 std::sort(Nodes.begin(), Nodes.end());
2940
2941 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002942 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002943 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002944 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002945
Jim Laskey26f7fa72006-10-17 19:33:52 +00002946 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002947
Chris Lattnerc3aae252005-01-07 07:46:32 +00002948 std::cerr << "\n\n";
2949}
2950
Evan Chengd6594ae2006-09-12 21:00:35 +00002951const Type *ConstantPoolSDNode::getType() const {
2952 if (isMachineConstantPoolEntry())
2953 return Val.MachineCPVal->getType();
2954 return Val.ConstVal->getType();
2955}