blob: 53fbb0573656314da3ef61aefa4230e24d2d2bfc [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"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Evan Cheng115c0362005-12-19 23:11:49 +000024#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000025#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000026#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000027#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000028#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner5cdcc582005-01-09 20:52:51 +000031static bool isCommutativeBinOp(unsigned Opcode) {
32 switch (Opcode) {
33 case ISD::ADD:
34 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000035 case ISD::MULHU:
36 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000037 case ISD::FADD:
38 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000039 case ISD::AND:
40 case ISD::OR:
41 case ISD::XOR: return true;
42 default: return false; // FIXME: Need commutative info for user ops!
43 }
44}
45
46static bool isAssociativeBinOp(unsigned Opcode) {
47 switch (Opcode) {
48 case ISD::ADD:
49 case ISD::MUL:
50 case ISD::AND:
51 case ISD::OR:
52 case ISD::XOR: return true;
53 default: return false; // FIXME: Need associative info for user ops!
54 }
55}
56
Chris Lattner5cdcc582005-01-09 20:52:51 +000057// isInvertibleForFree - Return true if there is no cost to emitting the logical
58// inverse of this node.
59static bool isInvertibleForFree(SDOperand N) {
60 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000061 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000062 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000063 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000064}
65
Jim Laskey58b968b2005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
74bool ConstantFPSDNode::isExactlyValue(double V) const {
75 return DoubleToBits(V) == DoubleToBits(Value);
76}
77
78//===----------------------------------------------------------------------===//
79// ISD Class
80//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000081
Chris Lattnerc3aae252005-01-07 07:46:32 +000082/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
83/// when given the operation for (X op Y).
84ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
85 // To perform this operation, we just need to swap the L and G bits of the
86 // operation.
87 unsigned OldL = (Operation >> 2) & 1;
88 unsigned OldG = (Operation >> 1) & 1;
89 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
90 (OldL << 1) | // New G bit
91 (OldG << 2)); // New L bit.
92}
93
94/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
95/// 'op' is a valid SetCC operation.
96ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
97 unsigned Operation = Op;
98 if (isInteger)
99 Operation ^= 7; // Flip L, G, E bits, but not U.
100 else
101 Operation ^= 15; // Flip all of the condition bits.
102 if (Operation > ISD::SETTRUE2)
103 Operation &= ~8; // Don't let N and U bits get set.
104 return ISD::CondCode(Operation);
105}
106
107
108/// isSignedOp - For an integer comparison, return 1 if the comparison is a
109/// signed operation and 2 if the result is an unsigned comparison. Return zero
110/// if the operation does not depend on the sign of the input (setne and seteq).
111static int isSignedOp(ISD::CondCode Opcode) {
112 switch (Opcode) {
113 default: assert(0 && "Illegal integer setcc operation!");
114 case ISD::SETEQ:
115 case ISD::SETNE: return 0;
116 case ISD::SETLT:
117 case ISD::SETLE:
118 case ISD::SETGT:
119 case ISD::SETGE: return 1;
120 case ISD::SETULT:
121 case ISD::SETULE:
122 case ISD::SETUGT:
123 case ISD::SETUGE: return 2;
124 }
125}
126
127/// getSetCCOrOperation - Return the result of a logical OR between different
128/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
129/// returns SETCC_INVALID if it is not possible to represent the resultant
130/// comparison.
131ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
132 bool isInteger) {
133 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
134 // Cannot fold a signed integer setcc with an unsigned integer setcc.
135 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000136
Chris Lattnerc3aae252005-01-07 07:46:32 +0000137 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000138
Chris Lattnerc3aae252005-01-07 07:46:32 +0000139 // If the N and U bits get set then the resultant comparison DOES suddenly
140 // care about orderedness, and is true when ordered.
141 if (Op > ISD::SETTRUE2)
142 Op &= ~16; // Clear the N bit.
143 return ISD::CondCode(Op);
144}
145
146/// getSetCCAndOperation - Return the result of a logical AND between different
147/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
148/// function returns zero if it is not possible to represent the resultant
149/// comparison.
150ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
151 bool isInteger) {
152 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
153 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000154 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000155
156 // Combine all of the condition bits.
157 return ISD::CondCode(Op1 & Op2);
158}
159
Chris Lattnerb48da392005-01-23 04:39:44 +0000160const TargetMachine &SelectionDAG::getTarget() const {
161 return TLI.getTargetMachine();
162}
163
Jim Laskey58b968b2005-08-17 20:08:02 +0000164//===----------------------------------------------------------------------===//
165// SelectionDAG Class
166//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000167
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000168/// RemoveDeadNodes - This method deletes all unreachable nodes in the
169/// SelectionDAG, including nodes (like loads) that have uses of their token
170/// chain but no other uses and no side effect. If a node is passed in as an
171/// argument, it is used as the seed for node deletion.
172void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000173 // Create a dummy node (which is not added to allnodes), that adds a reference
174 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000175 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000176
Chris Lattnerf469cb62005-11-08 18:52:27 +0000177 bool MadeChange = false;
178
Chris Lattner8b8749f2005-08-17 19:00:20 +0000179 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000180 if (N && N->use_empty()) {
181 DestroyDeadNode(N);
182 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000183 }
184
Chris Lattnerde202b32005-11-09 23:47:37 +0000185 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
186 if (I->use_empty() && I->getOpcode() != 65535) {
187 // Node is dead, recursively delete newly dead uses.
188 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000189 MadeChange = true;
190 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000191
192 // Walk the nodes list, removing the nodes we've marked as dead.
193 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000194 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
195 SDNode *N = I++;
196 if (N->use_empty())
197 AllNodes.erase(N);
198 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000199 }
200
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000201 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000202 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000203}
204
Chris Lattnerf469cb62005-11-08 18:52:27 +0000205/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
206/// graph. If it is the last user of any of its operands, recursively process
207/// them the same way.
208///
209void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000210 // Okay, we really are going to delete this node. First take this out of the
211 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000212 RemoveNodeFromCSEMaps(N);
213
214 // Next, brutally remove the operand list. This is safe to do, as there are
215 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000216 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
217 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000218 O->removeUser(N);
219
220 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000221 if (O->use_empty())
222 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000223 }
Chris Lattner65113b22005-11-08 22:07:03 +0000224 delete[] N->OperandList;
225 N->OperandList = 0;
226 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000227
228 // Mark the node as dead.
229 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000230}
231
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000232void SelectionDAG::DeleteNode(SDNode *N) {
233 assert(N->use_empty() && "Cannot delete a node that is not dead!");
234
235 // First take this out of the appropriate CSE map.
236 RemoveNodeFromCSEMaps(N);
237
Chris Lattner1e111c72005-09-07 05:37:01 +0000238 // Finally, remove uses due to operands of this node, remove from the
239 // AllNodes list, and delete the node.
240 DeleteNodeNotInCSEMaps(N);
241}
242
243void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
244
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000245 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000246 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000247
248 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000249 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
250 I->Val->removeUser(N);
251 delete[] N->OperandList;
252 N->OperandList = 0;
253 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000254
255 delete N;
256}
257
Chris Lattner149c58c2005-08-16 18:17:10 +0000258/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
259/// correspond to it. This is useful when we're about to delete or repurpose
260/// the node. We don't want future request for structurally identical nodes
261/// to return N anymore.
262void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000263 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000265 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000267 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
268 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000269 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000270 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000271 Erased = TargetConstants.erase(std::make_pair(
272 cast<ConstantSDNode>(N)->getValue(),
273 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000274 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000275 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000276 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000277 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000278 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000279 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000280 case ISD::STRING:
281 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
282 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000283 case ISD::CONDCODE:
284 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
285 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000286 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000287 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
288 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000289 case ISD::GlobalAddress: {
290 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
291 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
292 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000293 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000294 }
295 case ISD::TargetGlobalAddress: {
296 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
297 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
298 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000299 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000300 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000302 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000303 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000304 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000305 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000306 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000308 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000309 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000310 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000311 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000312 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000313 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000314 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315 break;
316 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000317 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000318 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000319 case ISD::TargetExternalSymbol:
320 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
321 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000322 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000323 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000324 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
325 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000326 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000327 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
328 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000329 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000330 case ISD::SRCVALUE: {
331 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000332 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000333 break;
334 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000335 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000336 Erased = Loads.erase(std::make_pair(N->getOperand(1),
337 std::make_pair(N->getOperand(0),
338 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000339 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000340 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000342 if (N->getNumOperands() == 0) {
343 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
344 N->getValueType(0)));
345 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased =
347 UnaryOps.erase(std::make_pair(N->getOpcode(),
348 std::make_pair(N->getOperand(0),
349 N->getValueType(0))));
350 } else if (N->getNumOperands() == 2) {
351 Erased =
352 BinaryOps.erase(std::make_pair(N->getOpcode(),
353 std::make_pair(N->getOperand(0),
354 N->getOperand(1))));
355 } else {
356 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
357 Erased =
358 OneResultNodes.erase(std::make_pair(N->getOpcode(),
359 std::make_pair(N->getValueType(0),
360 Ops)));
361 }
Chris Lattner385328c2005-05-14 07:42:29 +0000362 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000363 // Remove the node from the ArbitraryNodes map.
364 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
365 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000366 Erased =
367 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
368 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000369 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000370 break;
371 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000372#ifndef NDEBUG
373 // Verify that the node was actually in one of the CSE maps, unless it has a
374 // flag result (which cannot be CSE'd) or is one of the special cases that are
375 // not subject to CSE.
376 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner0ff5c272006-01-28 00:18:58 +0000377 N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000378 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000379
380 N->dump();
381 assert(0 && "Node is not in map!");
382 }
383#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000384}
385
Chris Lattner8b8749f2005-08-17 19:00:20 +0000386/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
387/// has been taken out and modified in some way. If the specified node already
388/// exists in the CSE maps, do not modify the maps, but return the existing node
389/// instead. If it doesn't exist, add it and return null.
390///
391SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
392 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000393 if (N->getOpcode() == ISD::CALLSEQ_START ||
Chris Lattnerfe14b342005-12-01 23:14:50 +0000394 N->getOpcode() == ISD::CALLSEQ_END ||
Chris Lattner9f8cc692005-12-19 22:21:21 +0000395 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000396 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000397
Chris Lattner9f8cc692005-12-19 22:21:21 +0000398 // Check that remaining values produced are not flags.
399 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
400 if (N->getValueType(i) == MVT::Flag)
401 return 0; // Never CSE anything that produces a flag.
402
Chris Lattnerfe14b342005-12-01 23:14:50 +0000403 if (N->getNumValues() == 1) {
404 if (N->getNumOperands() == 1) {
405 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
406 std::make_pair(N->getOperand(0),
407 N->getValueType(0)))];
408 if (U) return U;
409 U = N;
410 } else if (N->getNumOperands() == 2) {
411 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
412 std::make_pair(N->getOperand(0),
413 N->getOperand(1)))];
414 if (B) return B;
415 B = N;
416 } else {
417 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
418 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
419 std::make_pair(N->getValueType(0), Ops))];
420 if (ORN) return ORN;
421 ORN = N;
422 }
423 } else {
424 if (N->getOpcode() == ISD::LOAD) {
425 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
426 std::make_pair(N->getOperand(0),
427 N->getValueType(0)))];
428 if (L) return L;
429 L = N;
430 } else {
431 // Remove the node from the ArbitraryNodes map.
432 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
433 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
434 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
435 std::make_pair(RV, Ops))];
436 if (AN) return AN;
437 AN = N;
438 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000439 }
440 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000441}
442
443
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000444
Chris Lattner78ec3112003-08-11 14:57:33 +0000445SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000446 while (!AllNodes.empty()) {
447 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000448 delete [] N->OperandList;
449 N->OperandList = 0;
450 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000451 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000452 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000453}
454
Chris Lattner0f2287b2005-04-13 02:38:18 +0000455SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000456 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000457 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000458 return getNode(ISD::AND, Op.getValueType(), Op,
459 getConstant(Imm, Op.getValueType()));
460}
461
Chris Lattnerc3aae252005-01-07 07:46:32 +0000462SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
463 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
464 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000465 if (VT != MVT::i64)
466 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000467
Chris Lattnerc3aae252005-01-07 07:46:32 +0000468 SDNode *&N = Constants[std::make_pair(Val, VT)];
469 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000470 N = new ConstantSDNode(false, Val, VT);
471 AllNodes.push_back(N);
472 return SDOperand(N, 0);
473}
474
Chris Lattner36ce6912005-11-29 06:21:05 +0000475SDOperand SelectionDAG::getString(const std::string &Val) {
476 StringSDNode *&N = StringNodes[Val];
477 if (!N) {
478 N = new StringSDNode(Val);
479 AllNodes.push_back(N);
480 }
481 return SDOperand(N, 0);
482}
483
Chris Lattner37bfbb42005-08-17 00:34:06 +0000484SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
485 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
486 // Mask out any bits that are not valid for this constant.
487 if (VT != MVT::i64)
488 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
489
490 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
491 if (N) return SDOperand(N, 0);
492 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000493 AllNodes.push_back(N);
494 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000495}
496
Chris Lattnerc3aae252005-01-07 07:46:32 +0000497SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
498 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
499 if (VT == MVT::f32)
500 Val = (float)Val; // Mask out extra precision.
501
Chris Lattnerd8658612005-02-17 20:17:32 +0000502 // Do the map lookup using the actual bit pattern for the floating point
503 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
504 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000505 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000506 if (N) return SDOperand(N, 0);
507 N = new ConstantFPSDNode(Val, VT);
508 AllNodes.push_back(N);
509 return SDOperand(N, 0);
510}
511
Chris Lattnerc3aae252005-01-07 07:46:32 +0000512SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000513 MVT::ValueType VT, int offset) {
514 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000515 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000516 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000517 AllNodes.push_back(N);
518 return SDOperand(N, 0);
519}
520
521SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000522 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000523 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000524 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000525 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000526 AllNodes.push_back(N);
527 return SDOperand(N, 0);
528}
529
530SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
531 SDNode *&N = FrameIndices[FI];
532 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000533 N = new FrameIndexSDNode(FI, VT, false);
534 AllNodes.push_back(N);
535 return SDOperand(N, 0);
536}
537
538SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
539 SDNode *&N = TargetFrameIndices[FI];
540 if (N) return SDOperand(N, 0);
541 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000542 AllNodes.push_back(N);
543 return SDOperand(N, 0);
544}
545
Chris Lattner5839bf22005-08-26 17:15:30 +0000546SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
547 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000548 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000549 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000550 AllNodes.push_back(N);
551 return SDOperand(N, 0);
552}
553
Chris Lattner5839bf22005-08-26 17:15:30 +0000554SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
555 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000556 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000557 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000558 AllNodes.push_back(N);
559 return SDOperand(N, 0);
560}
561
562SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
563 SDNode *&N = BBNodes[MBB];
564 if (N) return SDOperand(N, 0);
565 N = new BasicBlockSDNode(MBB);
566 AllNodes.push_back(N);
567 return SDOperand(N, 0);
568}
569
Chris Lattner15e4b012005-07-10 00:07:11 +0000570SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
571 if ((unsigned)VT >= ValueTypeNodes.size())
572 ValueTypeNodes.resize(VT+1);
573 if (ValueTypeNodes[VT] == 0) {
574 ValueTypeNodes[VT] = new VTSDNode(VT);
575 AllNodes.push_back(ValueTypeNodes[VT]);
576 }
577
578 return SDOperand(ValueTypeNodes[VT], 0);
579}
580
Chris Lattnerc3aae252005-01-07 07:46:32 +0000581SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
582 SDNode *&N = ExternalSymbols[Sym];
583 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000584 N = new ExternalSymbolSDNode(false, Sym, VT);
585 AllNodes.push_back(N);
586 return SDOperand(N, 0);
587}
588
589SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
590 SDNode *&N = TargetExternalSymbols[Sym];
591 if (N) return SDOperand(N, 0);
592 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000593 AllNodes.push_back(N);
594 return SDOperand(N, 0);
595}
596
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000597SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
598 if ((unsigned)Cond >= CondCodeNodes.size())
599 CondCodeNodes.resize(Cond+1);
600
Chris Lattner079a27a2005-08-09 20:40:02 +0000601 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000602 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000603 AllNodes.push_back(CondCodeNodes[Cond]);
604 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000605 return SDOperand(CondCodeNodes[Cond], 0);
606}
607
Chris Lattner0fdd7682005-08-30 22:38:38 +0000608SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
609 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
610 if (!Reg) {
611 Reg = new RegisterSDNode(RegNo, VT);
612 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000613 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000614 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000615}
616
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000617SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
618 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000619 // These setcc operations always fold.
620 switch (Cond) {
621 default: break;
622 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000623 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000625 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000626 }
627
Chris Lattner67255a12005-04-07 18:14:58 +0000628 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
629 uint64_t C2 = N2C->getValue();
630 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
631 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000632
Chris Lattnerc3aae252005-01-07 07:46:32 +0000633 // Sign extend the operands if required
634 if (ISD::isSignedIntSetCC(Cond)) {
635 C1 = N1C->getSignExtended();
636 C2 = N2C->getSignExtended();
637 }
638
639 switch (Cond) {
640 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000641 case ISD::SETEQ: return getConstant(C1 == C2, VT);
642 case ISD::SETNE: return getConstant(C1 != C2, VT);
643 case ISD::SETULT: return getConstant(C1 < C2, VT);
644 case ISD::SETUGT: return getConstant(C1 > C2, VT);
645 case ISD::SETULE: return getConstant(C1 <= C2, VT);
646 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
647 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
648 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
649 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
650 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000651 }
Chris Lattner24673922005-04-07 18:58:54 +0000652 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000653 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000654 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
655 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
656
657 // If the comparison constant has bits in the upper part, the
658 // zero-extended value could never match.
659 if (C2 & (~0ULL << InSize)) {
660 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
661 switch (Cond) {
662 case ISD::SETUGT:
663 case ISD::SETUGE:
664 case ISD::SETEQ: return getConstant(0, VT);
665 case ISD::SETULT:
666 case ISD::SETULE:
667 case ISD::SETNE: return getConstant(1, VT);
668 case ISD::SETGT:
669 case ISD::SETGE:
670 // True if the sign bit of C2 is set.
671 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
672 case ISD::SETLT:
673 case ISD::SETLE:
674 // True if the sign bit of C2 isn't set.
675 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
676 default:
677 break;
678 }
679 }
680
681 // Otherwise, we can perform the comparison with the low bits.
682 switch (Cond) {
683 case ISD::SETEQ:
684 case ISD::SETNE:
685 case ISD::SETUGT:
686 case ISD::SETUGE:
687 case ISD::SETULT:
688 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000689 return getSetCC(VT, N1.getOperand(0),
690 getConstant(C2, N1.getOperand(0).getValueType()),
691 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000692 default:
693 break; // todo, be more careful with signed comparisons
694 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000695 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
696 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
697 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
698 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
699 MVT::ValueType ExtDstTy = N1.getValueType();
700 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000701
Chris Lattner7b2880c2005-08-24 22:44:39 +0000702 // If the extended part has any inconsistent bits, it cannot ever
703 // compare equal. In other words, they have to be all ones or all
704 // zeros.
705 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000706 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000707 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
708 return getConstant(Cond == ISD::SETNE, VT);
709
710 // Otherwise, make this a use of a zext.
711 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000712 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000713 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000714 }
715
Chris Lattner67255a12005-04-07 18:14:58 +0000716 uint64_t MinVal, MaxVal;
717 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
718 if (ISD::isSignedIntSetCC(Cond)) {
719 MinVal = 1ULL << (OperandBitSize-1);
720 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
721 MaxVal = ~0ULL >> (65-OperandBitSize);
722 else
723 MaxVal = 0;
724 } else {
725 MinVal = 0;
726 MaxVal = ~0ULL >> (64-OperandBitSize);
727 }
728
729 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
730 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
731 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
732 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000733 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
734 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000735 }
736
737 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
738 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
739 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000740 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
741 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000742 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000743
Nate Begeman72ea2812005-04-14 08:56:52 +0000744 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
745 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000746
Nate Begeman72ea2812005-04-14 08:56:52 +0000747 // Canonicalize setgt X, Min --> setne X, Min
748 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000749 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000750
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000751 // If we have setult X, 1, turn it into seteq X, 0
752 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000753 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
754 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000755 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000756 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000757 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
758 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000759
760 // If we have "setcc X, C1", check to see if we can shrink the immediate
761 // by changing cc.
762
763 // SETUGT X, SINTMAX -> SETLT X, 0
764 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
765 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000766 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000767
768 // FIXME: Implement the rest of these.
769
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000770
771 // Fold bit comparisons when we can.
772 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
773 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
774 if (ConstantSDNode *AndRHS =
775 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
776 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
777 // Perform the xform if the AND RHS is a single bit.
778 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
779 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000780 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000781 TLI.getShiftAmountTy()));
782 }
783 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
784 // (X & 8) == 8 --> (X & 8) >> 3
785 // Perform the xform if C2 is a single bit.
786 if ((C2 & (C2-1)) == 0) {
787 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000788 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000789 }
790 }
791 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000792 }
Chris Lattner67255a12005-04-07 18:14:58 +0000793 } else if (isa<ConstantSDNode>(N1.Val)) {
794 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000795 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000796 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000797
798 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
799 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
800 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000801
Chris Lattnerc3aae252005-01-07 07:46:32 +0000802 switch (Cond) {
803 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000804 case ISD::SETEQ: return getConstant(C1 == C2, VT);
805 case ISD::SETNE: return getConstant(C1 != C2, VT);
806 case ISD::SETLT: return getConstant(C1 < C2, VT);
807 case ISD::SETGT: return getConstant(C1 > C2, VT);
808 case ISD::SETLE: return getConstant(C1 <= C2, VT);
809 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000810 }
811 } else {
812 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000813 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000814 }
815
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000816 // Could not fold it.
817 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000818}
819
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000821///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000822SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000823 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
824 if (!N) {
825 N = new SDNode(Opcode, VT);
826 AllNodes.push_back(N);
827 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000828 return SDOperand(N, 0);
829}
830
Chris Lattnerc3aae252005-01-07 07:46:32 +0000831SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
832 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000833 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000834 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000835 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
836 uint64_t Val = C->getValue();
837 switch (Opcode) {
838 default: break;
839 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000840 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
842 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000843 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
844 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000845 case ISD::BIT_CONVERT:
846 if (VT == MVT::f32) {
847 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
848 return getConstantFP(BitsToFloat(Val), VT);
849 } else if (VT == MVT::f64) {
850 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
851 return getConstantFP(BitsToDouble(Val), VT);
852 }
853 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000854 case ISD::BSWAP:
855 switch(VT) {
856 default: assert(0 && "Invalid bswap!"); break;
857 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
858 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
859 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
860 }
861 break;
862 case ISD::CTPOP:
863 switch(VT) {
864 default: assert(0 && "Invalid ctpop!"); break;
865 case MVT::i1: return getConstant(Val != 0, VT);
866 case MVT::i8:
867 Tmp1 = (unsigned)Val & 0xFF;
868 return getConstant(CountPopulation_32(Tmp1), VT);
869 case MVT::i16:
870 Tmp1 = (unsigned)Val & 0xFFFF;
871 return getConstant(CountPopulation_32(Tmp1), VT);
872 case MVT::i32:
873 return getConstant(CountPopulation_32((unsigned)Val), VT);
874 case MVT::i64:
875 return getConstant(CountPopulation_64(Val), VT);
876 }
877 case ISD::CTLZ:
878 switch(VT) {
879 default: assert(0 && "Invalid ctlz!"); break;
880 case MVT::i1: return getConstant(Val == 0, VT);
881 case MVT::i8:
882 Tmp1 = (unsigned)Val & 0xFF;
883 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
884 case MVT::i16:
885 Tmp1 = (unsigned)Val & 0xFFFF;
886 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
887 case MVT::i32:
888 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
889 case MVT::i64:
890 return getConstant(CountLeadingZeros_64(Val), VT);
891 }
892 case ISD::CTTZ:
893 switch(VT) {
894 default: assert(0 && "Invalid cttz!"); break;
895 case MVT::i1: return getConstant(Val == 0, VT);
896 case MVT::i8:
897 Tmp1 = (unsigned)Val | 0x100;
898 return getConstant(CountTrailingZeros_32(Tmp1), VT);
899 case MVT::i16:
900 Tmp1 = (unsigned)Val | 0x10000;
901 return getConstant(CountTrailingZeros_32(Tmp1), VT);
902 case MVT::i32:
903 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
904 case MVT::i64:
905 return getConstant(CountTrailingZeros_64(Val), VT);
906 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907 }
908 }
909
Chris Lattner94683772005-12-23 05:30:37 +0000910 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
912 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000913 case ISD::FNEG:
914 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000915 case ISD::FABS:
916 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000917 case ISD::FP_ROUND:
918 case ISD::FP_EXTEND:
919 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000920 case ISD::FP_TO_SINT:
921 return getConstant((int64_t)C->getValue(), VT);
922 case ISD::FP_TO_UINT:
923 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000924 case ISD::BIT_CONVERT:
925 if (VT == MVT::i32) {
926 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
927 return getConstant(FloatToBits(C->getValue()), VT);
928 } else if (VT == MVT::i64) {
929 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
930 return getConstant(DoubleToBits(C->getValue()), VT);
931 }
932 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 }
934
935 unsigned OpOpcode = Operand.Val->getOpcode();
936 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000937 case ISD::TokenFactor:
938 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939 case ISD::SIGN_EXTEND:
940 if (Operand.getValueType() == VT) return Operand; // noop extension
941 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
942 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
943 break;
944 case ISD::ZERO_EXTEND:
945 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000946 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000947 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000948 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000949 case ISD::ANY_EXTEND:
950 if (Operand.getValueType() == VT) return Operand; // noop extension
951 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
952 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
953 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
954 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000955 case ISD::TRUNCATE:
956 if (Operand.getValueType() == VT) return Operand; // noop truncate
957 if (OpOpcode == ISD::TRUNCATE)
958 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000959 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
960 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000961 // If the source is smaller than the dest, we still need an extend.
962 if (Operand.Val->getOperand(0).getValueType() < VT)
963 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
964 else if (Operand.Val->getOperand(0).getValueType() > VT)
965 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
966 else
967 return Operand.Val->getOperand(0);
968 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000969 break;
Chris Lattner35481892005-12-23 00:16:34 +0000970 case ISD::BIT_CONVERT:
971 // Basic sanity checking.
972 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
973 "Cannot BIT_CONVERT between two different types!");
974 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +0000975 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
976 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +0000977 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000978 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000979 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
980 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000981 Operand.Val->getOperand(0));
982 if (OpOpcode == ISD::FNEG) // --X -> X
983 return Operand.Val->getOperand(0);
984 break;
985 case ISD::FABS:
986 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
987 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
988 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989 }
990
Chris Lattner43247a12005-08-25 19:12:10 +0000991 SDNode *N;
992 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
993 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000994 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000995 E = N = new SDNode(Opcode, Operand);
996 } else {
997 N = new SDNode(Opcode, Operand);
998 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000999 N->setValueTypes(VT);
1000 AllNodes.push_back(N);
1001 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001002}
1003
Chris Lattner36019aa2005-04-18 03:48:41 +00001004
1005
Chris Lattnerc3aae252005-01-07 07:46:32 +00001006SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1007 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001008#ifndef NDEBUG
1009 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001010 case ISD::TokenFactor:
1011 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1012 N2.getValueType() == MVT::Other && "Invalid token factor!");
1013 break;
Chris Lattner76365122005-01-16 02:23:22 +00001014 case ISD::AND:
1015 case ISD::OR:
1016 case ISD::XOR:
1017 case ISD::UDIV:
1018 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001019 case ISD::MULHU:
1020 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001021 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1022 // fall through
1023 case ISD::ADD:
1024 case ISD::SUB:
1025 case ISD::MUL:
1026 case ISD::SDIV:
1027 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001028 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1029 // fall through.
1030 case ISD::FADD:
1031 case ISD::FSUB:
1032 case ISD::FMUL:
1033 case ISD::FDIV:
1034 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001035 assert(N1.getValueType() == N2.getValueType() &&
1036 N1.getValueType() == VT && "Binary operator types must match!");
1037 break;
1038
1039 case ISD::SHL:
1040 case ISD::SRA:
1041 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001042 case ISD::ROTL:
1043 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001044 assert(VT == N1.getValueType() &&
1045 "Shift operators return type must be the same as their first arg");
1046 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001047 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001048 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001049 case ISD::FP_ROUND_INREG: {
1050 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1051 assert(VT == N1.getValueType() && "Not an inreg round!");
1052 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1053 "Cannot FP_ROUND_INREG integer types");
1054 assert(EVT <= VT && "Not rounding down!");
1055 break;
1056 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001057 case ISD::AssertSext:
1058 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001059 case ISD::SIGN_EXTEND_INREG: {
1060 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1061 assert(VT == N1.getValueType() && "Not an inreg extend!");
1062 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1063 "Cannot *_EXTEND_INREG FP types");
1064 assert(EVT <= VT && "Not extending!");
1065 }
1066
Chris Lattner76365122005-01-16 02:23:22 +00001067 default: break;
1068 }
1069#endif
1070
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1072 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1073 if (N1C) {
1074 if (N2C) {
1075 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1076 switch (Opcode) {
1077 case ISD::ADD: return getConstant(C1 + C2, VT);
1078 case ISD::SUB: return getConstant(C1 - C2, VT);
1079 case ISD::MUL: return getConstant(C1 * C2, VT);
1080 case ISD::UDIV:
1081 if (C2) return getConstant(C1 / C2, VT);
1082 break;
1083 case ISD::UREM :
1084 if (C2) return getConstant(C1 % C2, VT);
1085 break;
1086 case ISD::SDIV :
1087 if (C2) return getConstant(N1C->getSignExtended() /
1088 N2C->getSignExtended(), VT);
1089 break;
1090 case ISD::SREM :
1091 if (C2) return getConstant(N1C->getSignExtended() %
1092 N2C->getSignExtended(), VT);
1093 break;
1094 case ISD::AND : return getConstant(C1 & C2, VT);
1095 case ISD::OR : return getConstant(C1 | C2, VT);
1096 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001097 case ISD::SHL : return getConstant(C1 << C2, VT);
1098 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001099 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001100 case ISD::ROTL :
1101 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1102 VT);
1103 case ISD::ROTR :
1104 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1105 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106 default: break;
1107 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001108 } else { // Cannonicalize constant to RHS if commutative
1109 if (isCommutativeBinOp(Opcode)) {
1110 std::swap(N1C, N2C);
1111 std::swap(N1, N2);
1112 }
1113 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001114 }
1115
1116 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1117 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001118 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001119 if (N2CFP) {
1120 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1121 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001122 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1123 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1124 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1125 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001126 if (C2) return getConstantFP(C1 / C2, VT);
1127 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001128 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001129 if (C2) return getConstantFP(fmod(C1, C2), VT);
1130 break;
1131 default: break;
1132 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133 } else { // Cannonicalize constant to RHS if commutative
1134 if (isCommutativeBinOp(Opcode)) {
1135 std::swap(N1CFP, N2CFP);
1136 std::swap(N1, N2);
1137 }
1138 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001139 }
1140
Chris Lattnerc3aae252005-01-07 07:46:32 +00001141 // Finally, fold operations that do not require constants.
1142 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001143 case ISD::FP_ROUND_INREG:
1144 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1145 break;
1146 case ISD::SIGN_EXTEND_INREG: {
1147 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1148 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001149 break;
1150 }
1151
Nate Begemaneea805e2005-04-13 21:23:31 +00001152 // FIXME: figure out how to safely handle things like
1153 // int foo(int x) { return 1 << (x & 255); }
1154 // int bar() { return foo(256); }
1155#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001156 case ISD::SHL:
1157 case ISD::SRL:
1158 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001159 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001160 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001161 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001162 else if (N2.getOpcode() == ISD::AND)
1163 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1164 // If the and is only masking out bits that cannot effect the shift,
1165 // eliminate the and.
1166 unsigned NumBits = MVT::getSizeInBits(VT);
1167 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1168 return getNode(Opcode, VT, N1, N2.getOperand(0));
1169 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001170 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001171#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 }
1173
Chris Lattner27e9b412005-05-11 18:57:39 +00001174 // Memoize this node if possible.
1175 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001176 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1177 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001178 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1179 if (BON) return SDOperand(BON, 0);
1180
1181 BON = N = new SDNode(Opcode, N1, N2);
1182 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001183 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001184 }
1185
Chris Lattner3e011362005-05-14 07:45:46 +00001186 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001187 AllNodes.push_back(N);
1188 return SDOperand(N, 0);
1189}
1190
Chris Lattnerc3aae252005-01-07 07:46:32 +00001191SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1192 SDOperand N1, SDOperand N2, SDOperand N3) {
1193 // Perform various simplifications.
1194 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1195 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1196 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1197 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001198 case ISD::SETCC: {
1199 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001200 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001201 if (Simp.Val) return Simp;
1202 break;
1203 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001204 case ISD::SELECT:
1205 if (N1C)
1206 if (N1C->getValue())
1207 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001208 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001209 return N3; // select false, X, Y -> Y
1210
1211 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001212 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001213 case ISD::BRCOND:
1214 if (N2C)
1215 if (N2C->getValue()) // Unconditional branch
1216 return getNode(ISD::BR, MVT::Other, N1, N3);
1217 else
1218 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001219 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 }
1221
Chris Lattner385328c2005-05-14 07:42:29 +00001222 std::vector<SDOperand> Ops;
1223 Ops.reserve(3);
1224 Ops.push_back(N1);
1225 Ops.push_back(N2);
1226 Ops.push_back(N3);
1227
Chris Lattner43247a12005-08-25 19:12:10 +00001228 // Memoize node if it doesn't produce a flag.
1229 SDNode *N;
1230 if (VT != MVT::Flag) {
1231 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1232 if (E) return SDOperand(E, 0);
1233 E = N = new SDNode(Opcode, N1, N2, N3);
1234 } else {
1235 N = new SDNode(Opcode, N1, N2, N3);
1236 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001237 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 AllNodes.push_back(N);
1239 return SDOperand(N, 0);
1240}
1241
1242SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001243 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001244 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001245 std::vector<SDOperand> Ops;
1246 Ops.reserve(4);
1247 Ops.push_back(N1);
1248 Ops.push_back(N2);
1249 Ops.push_back(N3);
1250 Ops.push_back(N4);
1251 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001252}
1253
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001254SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1255 SDOperand N1, SDOperand N2, SDOperand N3,
1256 SDOperand N4, SDOperand N5) {
1257 std::vector<SDOperand> Ops;
1258 Ops.reserve(5);
1259 Ops.push_back(N1);
1260 Ops.push_back(N2);
1261 Ops.push_back(N3);
1262 Ops.push_back(N4);
1263 Ops.push_back(N5);
1264 return getNode(Opcode, VT, Ops);
1265}
1266
Evan Cheng7038daf2005-12-10 00:37:58 +00001267// setAdjCallChain - This method changes the token chain of an
1268// CALLSEQ_START/END node to be the specified operand.
1269void SDNode::setAdjCallChain(SDOperand N) {
1270 assert(N.getValueType() == MVT::Other);
1271 assert((getOpcode() == ISD::CALLSEQ_START ||
1272 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1273
1274 OperandList[0].Val->removeUser(this);
1275 OperandList[0] = N;
1276 OperandList[0].Val->Uses.push_back(this);
1277}
1278
Chris Lattner6a542892006-01-24 05:48:21 +00001279// setAdjCallFlag - This method changes the flag input of an
1280// CALLSEQ_START/END node to be the specified operand.
1281void SDNode::setAdjCallFlag(SDOperand N) {
1282 assert(N.getValueType() == MVT::Flag);
1283 assert((getOpcode() == ISD::CALLSEQ_START ||
1284 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1285
1286 SDOperand &FlagOp = OperandList[getNumOperands()-1];
1287 assert(FlagOp.getValueType() == MVT::Flag);
1288 FlagOp.Val->removeUser(this);
1289 FlagOp = N;
1290 FlagOp.Val->Uses.push_back(this);
1291}
Evan Cheng7038daf2005-12-10 00:37:58 +00001292
1293
1294SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1295 SDOperand Chain, SDOperand Ptr,
1296 SDOperand SV) {
1297 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1298 if (N) return SDOperand(N, 0);
1299 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1300
1301 // Loads have a token chain.
1302 setNodeValueTypes(N, VT, MVT::Other);
1303 AllNodes.push_back(N);
1304 return SDOperand(N, 0);
1305}
1306
1307SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1308 SDOperand Chain, SDOperand Ptr,
1309 SDOperand SV) {
1310 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1311 if (N) return SDOperand(N, 0);
1312 std::vector<SDOperand> Ops;
1313 Ops.reserve(5);
1314 Ops.push_back(Chain);
1315 Ops.push_back(Ptr);
1316 Ops.push_back(getConstant(Count, MVT::i32));
1317 Ops.push_back(getValueType(EVT));
1318 Ops.push_back(SV);
1319 std::vector<MVT::ValueType> VTs;
1320 VTs.reserve(2);
1321 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1322 return getNode(ISD::VLOAD, VTs, Ops);
1323}
1324
1325SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1326 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1327 MVT::ValueType EVT) {
1328 std::vector<SDOperand> Ops;
1329 Ops.reserve(4);
1330 Ops.push_back(Chain);
1331 Ops.push_back(Ptr);
1332 Ops.push_back(SV);
1333 Ops.push_back(getValueType(EVT));
1334 std::vector<MVT::ValueType> VTs;
1335 VTs.reserve(2);
1336 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1337 return getNode(Opcode, VTs, Ops);
1338}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001339
Chris Lattner0437cdd2005-05-09 04:14:13 +00001340SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001341 assert((!V || isa<PointerType>(V->getType())) &&
1342 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001343 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1344 if (N) return SDOperand(N, 0);
1345
1346 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001347 AllNodes.push_back(N);
1348 return SDOperand(N, 0);
1349}
1350
Nate Begemanacc398c2006-01-25 18:21:52 +00001351SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1352 SDOperand Chain, SDOperand Ptr,
1353 SDOperand SV) {
1354 std::vector<SDOperand> Ops;
1355 Ops.reserve(3);
1356 Ops.push_back(Chain);
1357 Ops.push_back(Ptr);
1358 Ops.push_back(SV);
1359 std::vector<MVT::ValueType> VTs;
1360 VTs.reserve(2);
1361 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1362 return getNode(ISD::VAARG, VTs, Ops);
1363}
1364
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001365SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001366 std::vector<SDOperand> &Ops) {
1367 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001368 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001369 case 1: return getNode(Opcode, VT, Ops[0]);
1370 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1371 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001372 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001373 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001374
Chris Lattner89c34632005-05-14 06:20:26 +00001375 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001376 switch (Opcode) {
1377 default: break;
1378 case ISD::BRCONDTWOWAY:
1379 if (N1C)
1380 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001381 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001382 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001383 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001384 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001385 case ISD::BRTWOWAY_CC:
1386 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1387 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1388 "LHS and RHS of comparison must have same type!");
1389 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001390 case ISD::TRUNCSTORE: {
1391 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1392 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1393#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1394 // If this is a truncating store of a constant, convert to the desired type
1395 // and store it instead.
1396 if (isa<Constant>(Ops[0])) {
1397 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1398 if (isa<Constant>(Op))
1399 N1 = Op;
1400 }
1401 // Also for ConstantFP?
1402#endif
1403 if (Ops[0].getValueType() == EVT) // Normal store?
1404 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1405 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1406 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1407 "Can't do FP-INT conversion!");
1408 break;
1409 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001410 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001411 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001412 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1413 "LHS and RHS of condition must have same type!");
1414 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1415 "True and False arms of SelectCC must have same type!");
1416 assert(Ops[2].getValueType() == VT &&
1417 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001418 break;
1419 }
1420 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001421 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001422 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1423 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001424 break;
1425 }
Chris Lattneref847df2005-04-09 03:27:28 +00001426 }
1427
Chris Lattner385328c2005-05-14 07:42:29 +00001428 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001429 SDNode *N;
1430 if (VT != MVT::Flag) {
1431 SDNode *&E =
1432 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1433 if (E) return SDOperand(E, 0);
1434 E = N = new SDNode(Opcode, Ops);
1435 } else {
1436 N = new SDNode(Opcode, Ops);
1437 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001438 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001439 AllNodes.push_back(N);
1440 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001441}
1442
Chris Lattner89c34632005-05-14 06:20:26 +00001443SDOperand SelectionDAG::getNode(unsigned Opcode,
1444 std::vector<MVT::ValueType> &ResultTys,
1445 std::vector<SDOperand> &Ops) {
1446 if (ResultTys.size() == 1)
1447 return getNode(Opcode, ResultTys[0], Ops);
1448
Chris Lattner5f056bf2005-07-10 01:55:33 +00001449 switch (Opcode) {
1450 case ISD::EXTLOAD:
1451 case ISD::SEXTLOAD:
1452 case ISD::ZEXTLOAD: {
1453 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1454 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1455 // If they are asking for an extending load from/to the same thing, return a
1456 // normal load.
1457 if (ResultTys[0] == EVT)
1458 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1459 assert(EVT < ResultTys[0] &&
1460 "Should only be an extending load, not truncating!");
1461 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1462 "Cannot sign/zero extend a FP load!");
1463 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1464 "Cannot convert from FP to Int or Int -> FP!");
1465 break;
1466 }
1467
Chris Lattnere89083a2005-05-14 07:25:05 +00001468 // FIXME: figure out how to safely handle things like
1469 // int foo(int x) { return 1 << (x & 255); }
1470 // int bar() { return foo(256); }
1471#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001472 case ISD::SRA_PARTS:
1473 case ISD::SRL_PARTS:
1474 case ISD::SHL_PARTS:
1475 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001476 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001477 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1478 else if (N3.getOpcode() == ISD::AND)
1479 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1480 // If the and is only masking out bits that cannot effect the shift,
1481 // eliminate the and.
1482 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1483 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1484 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1485 }
1486 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001487#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001488 }
Chris Lattner89c34632005-05-14 06:20:26 +00001489
Chris Lattner43247a12005-08-25 19:12:10 +00001490 // Memoize the node unless it returns a flag.
1491 SDNode *N;
1492 if (ResultTys.back() != MVT::Flag) {
1493 SDNode *&E =
1494 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1495 if (E) return SDOperand(E, 0);
1496 E = N = new SDNode(Opcode, Ops);
1497 } else {
1498 N = new SDNode(Opcode, Ops);
1499 }
Chris Lattnera3255112005-11-08 23:30:28 +00001500 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001501 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001502 return SDOperand(N, 0);
1503}
1504
Chris Lattnera3255112005-11-08 23:30:28 +00001505void SelectionDAG::setNodeValueTypes(SDNode *N,
1506 std::vector<MVT::ValueType> &RetVals) {
1507 switch (RetVals.size()) {
1508 case 0: return;
1509 case 1: N->setValueTypes(RetVals[0]); return;
1510 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1511 default: break;
1512 }
1513
1514 std::list<std::vector<MVT::ValueType> >::iterator I =
1515 std::find(VTList.begin(), VTList.end(), RetVals);
1516 if (I == VTList.end()) {
1517 VTList.push_front(RetVals);
1518 I = VTList.begin();
1519 }
1520
1521 N->setValueTypes(&(*I)[0], I->size());
1522}
1523
1524void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1525 MVT::ValueType VT2) {
1526 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1527 E = VTList.end(); I != E; ++I) {
1528 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1529 N->setValueTypes(&(*I)[0], 2);
1530 return;
1531 }
1532 }
1533 std::vector<MVT::ValueType> V;
1534 V.push_back(VT1);
1535 V.push_back(VT2);
1536 VTList.push_front(V);
1537 N->setValueTypes(&(*VTList.begin())[0], 2);
1538}
1539
Chris Lattner149c58c2005-08-16 18:17:10 +00001540
1541/// SelectNodeTo - These are used for target selectors to *mutate* the
1542/// specified node to have the specified return type, Target opcode, and
1543/// operands. Note that target opcodes are stored as
1544/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001545///
1546/// Note that SelectNodeTo returns the resultant node. If there is already a
1547/// node of the specified opcode and operands, it returns that node instead of
1548/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001549SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1550 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001551 // If an identical node already exists, use it.
1552 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1553 if (ON) return SDOperand(ON, 0);
1554
Chris Lattner7651fa42005-08-24 23:00:29 +00001555 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001556
Chris Lattner7651fa42005-08-24 23:00:29 +00001557 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1558 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001559
1560 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001561 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001562}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001563
Chris Lattnereb19e402005-11-30 22:45:14 +00001564SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1565 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001566 // If an identical node already exists, use it.
1567 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1568 std::make_pair(Op1, VT))];
1569 if (ON) return SDOperand(ON, 0);
1570
Chris Lattner149c58c2005-08-16 18:17:10 +00001571 RemoveNodeFromCSEMaps(N);
1572 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1573 N->setValueTypes(VT);
1574 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001575
1576 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001577 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001578}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001579
Chris Lattnereb19e402005-11-30 22:45:14 +00001580SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1581 MVT::ValueType VT, SDOperand Op1,
1582 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001583 // If an identical node already exists, use it.
1584 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1585 std::make_pair(Op1, Op2))];
1586 if (ON) return SDOperand(ON, 0);
1587
Chris Lattner149c58c2005-08-16 18:17:10 +00001588 RemoveNodeFromCSEMaps(N);
1589 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1590 N->setValueTypes(VT);
1591 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001592
1593 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001594 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001595}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001596
Chris Lattnereb19e402005-11-30 22:45:14 +00001597SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1598 MVT::ValueType VT, SDOperand Op1,
1599 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001600 // If an identical node already exists, use it.
1601 std::vector<SDOperand> OpList;
1602 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1603 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1604 std::make_pair(VT, OpList))];
1605 if (ON) return SDOperand(ON, 0);
1606
Chris Lattner149c58c2005-08-16 18:17:10 +00001607 RemoveNodeFromCSEMaps(N);
1608 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1609 N->setValueTypes(VT);
1610 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001611
1612 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001613 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001614}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001615
Chris Lattnereb19e402005-11-30 22:45:14 +00001616SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1617 MVT::ValueType VT, SDOperand Op1,
1618 SDOperand Op2, SDOperand Op3,
1619 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001620 // If an identical node already exists, use it.
1621 std::vector<SDOperand> OpList;
1622 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1623 OpList.push_back(Op4);
1624 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1625 std::make_pair(VT, OpList))];
1626 if (ON) return SDOperand(ON, 0);
1627
Nate Begeman294a0a12005-08-18 07:30:15 +00001628 RemoveNodeFromCSEMaps(N);
1629 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1630 N->setValueTypes(VT);
1631 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001632
1633 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001634 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001635}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001636
Chris Lattnereb19e402005-11-30 22:45:14 +00001637SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1638 MVT::ValueType VT, SDOperand Op1,
1639 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1640 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001641 // If an identical node already exists, use it.
1642 std::vector<SDOperand> OpList;
1643 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1644 OpList.push_back(Op4); OpList.push_back(Op5);
1645 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1646 std::make_pair(VT, OpList))];
1647 if (ON) return SDOperand(ON, 0);
1648
Chris Lattner6b09a292005-08-21 18:49:33 +00001649 RemoveNodeFromCSEMaps(N);
1650 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1651 N->setValueTypes(VT);
1652 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001653
1654 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001655 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001656}
Chris Lattner149c58c2005-08-16 18:17:10 +00001657
Chris Lattnereb19e402005-11-30 22:45:14 +00001658SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1659 MVT::ValueType VT, SDOperand Op1,
1660 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1661 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001662 // If an identical node already exists, use it.
1663 std::vector<SDOperand> OpList;
1664 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1665 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1666 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1667 std::make_pair(VT, OpList))];
1668 if (ON) return SDOperand(ON, 0);
1669
Evan Cheng61ca74b2005-11-30 02:04:11 +00001670 RemoveNodeFromCSEMaps(N);
1671 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1672 N->setValueTypes(VT);
1673 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001674
1675 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001676 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001677}
1678
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001679SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1680 MVT::ValueType VT, SDOperand Op1,
1681 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1682 SDOperand Op5, SDOperand Op6,
1683 SDOperand Op7) {
1684 // If an identical node already exists, use it.
1685 std::vector<SDOperand> OpList;
1686 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1687 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1688 OpList.push_back(Op7);
1689 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1690 std::make_pair(VT, OpList))];
1691 if (ON) return SDOperand(ON, 0);
1692
1693 RemoveNodeFromCSEMaps(N);
1694 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1695 N->setValueTypes(VT);
1696 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1697
1698 ON = N; // Memoize the new node.
1699 return SDOperand(N, 0);
1700}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001701SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1702 MVT::ValueType VT, SDOperand Op1,
1703 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1704 SDOperand Op5, SDOperand Op6,
1705 SDOperand Op7, SDOperand Op8) {
1706 // If an identical node already exists, use it.
1707 std::vector<SDOperand> OpList;
1708 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1709 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1710 OpList.push_back(Op7); OpList.push_back(Op8);
1711 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1712 std::make_pair(VT, OpList))];
1713 if (ON) return SDOperand(ON, 0);
1714
1715 RemoveNodeFromCSEMaps(N);
1716 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1717 N->setValueTypes(VT);
1718 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1719
1720 ON = N; // Memoize the new node.
1721 return SDOperand(N, 0);
1722}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001723
Chris Lattnereb19e402005-11-30 22:45:14 +00001724SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1725 MVT::ValueType VT1, MVT::ValueType VT2,
1726 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001727 // If an identical node already exists, use it.
1728 std::vector<SDOperand> OpList;
1729 OpList.push_back(Op1); OpList.push_back(Op2);
1730 std::vector<MVT::ValueType> VTList;
1731 VTList.push_back(VT1); VTList.push_back(VT2);
1732 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1733 std::make_pair(VTList, OpList))];
1734 if (ON) return SDOperand(ON, 0);
1735
Chris Lattner0fb094f2005-11-19 01:44:53 +00001736 RemoveNodeFromCSEMaps(N);
1737 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1738 setNodeValueTypes(N, VT1, VT2);
1739 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001740
1741 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001742 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001743}
1744
Chris Lattnereb19e402005-11-30 22:45:14 +00001745SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1746 MVT::ValueType VT1, MVT::ValueType VT2,
1747 SDOperand Op1, SDOperand Op2,
1748 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001749 // If an identical node already exists, use it.
1750 std::vector<SDOperand> OpList;
1751 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1752 std::vector<MVT::ValueType> VTList;
1753 VTList.push_back(VT1); VTList.push_back(VT2);
1754 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1755 std::make_pair(VTList, OpList))];
1756 if (ON) return SDOperand(ON, 0);
1757
Chris Lattner0fb094f2005-11-19 01:44:53 +00001758 RemoveNodeFromCSEMaps(N);
1759 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1760 setNodeValueTypes(N, VT1, VT2);
1761 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001762
1763 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001764 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001765}
1766
Chris Lattnereb19e402005-11-30 22:45:14 +00001767SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1768 MVT::ValueType VT1, MVT::ValueType VT2,
1769 SDOperand Op1, SDOperand Op2,
1770 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001771 // If an identical node already exists, use it.
1772 std::vector<SDOperand> OpList;
1773 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1774 OpList.push_back(Op4);
1775 std::vector<MVT::ValueType> VTList;
1776 VTList.push_back(VT1); VTList.push_back(VT2);
1777 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1778 std::make_pair(VTList, OpList))];
1779 if (ON) return SDOperand(ON, 0);
1780
Chris Lattner0fb094f2005-11-19 01:44:53 +00001781 RemoveNodeFromCSEMaps(N);
1782 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1783 setNodeValueTypes(N, VT1, VT2);
1784 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001785
1786 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001787 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001788}
1789
Chris Lattnereb19e402005-11-30 22:45:14 +00001790SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1791 MVT::ValueType VT1, MVT::ValueType VT2,
1792 SDOperand Op1, SDOperand Op2,
1793 SDOperand Op3, SDOperand Op4,
1794 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001795 // If an identical node already exists, use it.
1796 std::vector<SDOperand> OpList;
1797 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1798 OpList.push_back(Op4); OpList.push_back(Op5);
1799 std::vector<MVT::ValueType> VTList;
1800 VTList.push_back(VT1); VTList.push_back(VT2);
1801 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1802 std::make_pair(VTList, OpList))];
1803 if (ON) return SDOperand(ON, 0);
1804
Chris Lattner0fb094f2005-11-19 01:44:53 +00001805 RemoveNodeFromCSEMaps(N);
1806 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1807 setNodeValueTypes(N, VT1, VT2);
1808 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001809
1810 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001811 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001812}
1813
1814// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00001815/// This can cause recursive merging of nodes in the DAG.
1816///
Chris Lattner8b52f212005-08-26 18:36:28 +00001817/// This version assumes From/To have a single result value.
1818///
Chris Lattner1e111c72005-09-07 05:37:01 +00001819void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1820 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001821 SDNode *From = FromN.Val, *To = ToN.Val;
1822 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1823 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001824 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001825
Chris Lattner8b8749f2005-08-17 19:00:20 +00001826 while (!From->use_empty()) {
1827 // Process users until they are all gone.
1828 SDNode *U = *From->use_begin();
1829
1830 // This node is about to morph, remove its old self from the CSE maps.
1831 RemoveNodeFromCSEMaps(U);
1832
Chris Lattner65113b22005-11-08 22:07:03 +00001833 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1834 I != E; ++I)
1835 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001836 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001837 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00001838 To->addUser(U);
1839 }
1840
1841 // Now that we have modified U, add it back to the CSE maps. If it already
1842 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001843 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1844 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001845 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001846 if (Deleted) Deleted->push_back(U);
1847 DeleteNodeNotInCSEMaps(U);
1848 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001849 }
1850}
1851
1852/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1853/// This can cause recursive merging of nodes in the DAG.
1854///
1855/// This version assumes From/To have matching types and numbers of result
1856/// values.
1857///
Chris Lattner1e111c72005-09-07 05:37:01 +00001858void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1859 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001860 assert(From != To && "Cannot replace uses of with self");
1861 assert(From->getNumValues() == To->getNumValues() &&
1862 "Cannot use this version of ReplaceAllUsesWith!");
1863 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001864 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001865 return;
1866 }
1867
1868 while (!From->use_empty()) {
1869 // Process users until they are all gone.
1870 SDNode *U = *From->use_begin();
1871
1872 // This node is about to morph, remove its old self from the CSE maps.
1873 RemoveNodeFromCSEMaps(U);
1874
Chris Lattner65113b22005-11-08 22:07:03 +00001875 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1876 I != E; ++I)
1877 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001878 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001879 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00001880 To->addUser(U);
1881 }
1882
1883 // Now that we have modified U, add it back to the CSE maps. If it already
1884 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001885 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1886 ReplaceAllUsesWith(U, Existing, Deleted);
1887 // U is now dead.
1888 if (Deleted) Deleted->push_back(U);
1889 DeleteNodeNotInCSEMaps(U);
1890 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001891 }
1892}
1893
Chris Lattner8b52f212005-08-26 18:36:28 +00001894/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1895/// This can cause recursive merging of nodes in the DAG.
1896///
1897/// This version can replace From with any result values. To must match the
1898/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001899void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001900 const std::vector<SDOperand> &To,
1901 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001902 assert(From->getNumValues() == To.size() &&
1903 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001904 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001905 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001906 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001907 return;
1908 }
1909
1910 while (!From->use_empty()) {
1911 // Process users until they are all gone.
1912 SDNode *U = *From->use_begin();
1913
1914 // This node is about to morph, remove its old self from the CSE maps.
1915 RemoveNodeFromCSEMaps(U);
1916
Chris Lattner65113b22005-11-08 22:07:03 +00001917 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1918 I != E; ++I)
1919 if (I->Val == From) {
1920 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001921 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001922 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001923 ToOp.Val->addUser(U);
1924 }
1925
1926 // Now that we have modified U, add it back to the CSE maps. If it already
1927 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001928 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1929 ReplaceAllUsesWith(U, Existing, Deleted);
1930 // U is now dead.
1931 if (Deleted) Deleted->push_back(U);
1932 DeleteNodeNotInCSEMaps(U);
1933 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001934 }
1935}
1936
1937
Jim Laskey58b968b2005-08-17 20:08:02 +00001938//===----------------------------------------------------------------------===//
1939// SDNode Class
1940//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001941
Chris Lattnera3255112005-11-08 23:30:28 +00001942
1943/// getValueTypeList - Return a pointer to the specified value type.
1944///
1945MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1946 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1947 VTs[VT] = VT;
1948 return &VTs[VT];
1949}
1950
Chris Lattner5c884562005-01-12 18:37:47 +00001951/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1952/// indicated value. This method ignores uses of other values defined by this
1953/// operation.
1954bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1955 assert(Value < getNumValues() && "Bad value!");
1956
1957 // If there is only one value, this is easy.
1958 if (getNumValues() == 1)
1959 return use_size() == NUses;
1960 if (Uses.size() < NUses) return false;
1961
1962 SDOperand TheValue(this, Value);
1963
1964 std::set<SDNode*> UsersHandled;
1965
1966 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1967 UI != E; ++UI) {
1968 SDNode *User = *UI;
1969 if (User->getNumOperands() == 1 ||
1970 UsersHandled.insert(User).second) // First time we've seen this?
1971 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1972 if (User->getOperand(i) == TheValue) {
1973 if (NUses == 0)
1974 return false; // too many uses
1975 --NUses;
1976 }
1977 }
1978
1979 // Found exactly the right number of uses?
1980 return NUses == 0;
1981}
1982
1983
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001984const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001985 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001986 default:
1987 if (getOpcode() < ISD::BUILTIN_OP_END)
1988 return "<<Unknown DAG Node>>";
1989 else {
Evan Cheng72261582005-12-20 06:22:03 +00001990 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001991 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001992 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1993 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00001994
Evan Cheng72261582005-12-20 06:22:03 +00001995 TargetLowering &TLI = G->getTargetLoweringInfo();
1996 const char *Name =
1997 TLI.getTargetNodeName(getOpcode());
1998 if (Name) return Name;
1999 }
2000
2001 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002002 }
2003
Andrew Lenharth95762122005-03-31 21:24:06 +00002004 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002005 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002006 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002007 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00002008 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002009 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002010 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002011 case ISD::AssertSext: return "AssertSext";
2012 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002013 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002014 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002015 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00002016 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002017 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002018 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002019 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002020 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002021 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002022 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002023 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00002024 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002025 case ISD::ConstantPool: return "ConstantPool";
2026 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002027 case ISD::CopyToReg: return "CopyToReg";
2028 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002029 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002030 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002031 case ISD::INLINEASM: return "inlineasm";
2032
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002033 // Unary operators
2034 case ISD::FABS: return "fabs";
2035 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002036 case ISD::FSQRT: return "fsqrt";
2037 case ISD::FSIN: return "fsin";
2038 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002039
2040 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002041 case ISD::ADD: return "add";
2042 case ISD::SUB: return "sub";
2043 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002044 case ISD::MULHU: return "mulhu";
2045 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002046 case ISD::SDIV: return "sdiv";
2047 case ISD::UDIV: return "udiv";
2048 case ISD::SREM: return "srem";
2049 case ISD::UREM: return "urem";
2050 case ISD::AND: return "and";
2051 case ISD::OR: return "or";
2052 case ISD::XOR: return "xor";
2053 case ISD::SHL: return "shl";
2054 case ISD::SRA: return "sra";
2055 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002056 case ISD::ROTL: return "rotl";
2057 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002058 case ISD::FADD: return "fadd";
2059 case ISD::FSUB: return "fsub";
2060 case ISD::FMUL: return "fmul";
2061 case ISD::FDIV: return "fdiv";
2062 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002063 case ISD::VADD: return "vadd";
2064 case ISD::VSUB: return "vsub";
2065 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002066
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002067 case ISD::SETCC: return "setcc";
2068 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002069 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002070 case ISD::ADD_PARTS: return "add_parts";
2071 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002072 case ISD::SHL_PARTS: return "shl_parts";
2073 case ISD::SRA_PARTS: return "sra_parts";
2074 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002075
Chris Lattner7f644642005-04-28 21:44:03 +00002076 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002077 case ISD::SIGN_EXTEND: return "sign_extend";
2078 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002079 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002080 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002081 case ISD::TRUNCATE: return "truncate";
2082 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002083 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002084 case ISD::FP_EXTEND: return "fp_extend";
2085
2086 case ISD::SINT_TO_FP: return "sint_to_fp";
2087 case ISD::UINT_TO_FP: return "uint_to_fp";
2088 case ISD::FP_TO_SINT: return "fp_to_sint";
2089 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002090 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002091
2092 // Control flow instructions
2093 case ISD::BR: return "br";
2094 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002095 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002096 case ISD::BR_CC: return "br_cc";
2097 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002098 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002099 case ISD::CALLSEQ_START: return "callseq_start";
2100 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002101
2102 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002103 case ISD::LOAD: return "load";
2104 case ISD::STORE: return "store";
2105 case ISD::VLOAD: return "vload";
2106 case ISD::EXTLOAD: return "extload";
2107 case ISD::SEXTLOAD: return "sextload";
2108 case ISD::ZEXTLOAD: return "zextload";
2109 case ISD::TRUNCSTORE: return "truncstore";
2110 case ISD::VAARG: return "vaarg";
2111 case ISD::VACOPY: return "vacopy";
2112 case ISD::VAEND: return "vaend";
2113 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002114 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002115 case ISD::EXTRACT_ELEMENT: return "extract_element";
2116 case ISD::BUILD_PAIR: return "build_pair";
2117 case ISD::STACKSAVE: return "stacksave";
2118 case ISD::STACKRESTORE: return "stackrestore";
2119
2120 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002121 case ISD::MEMSET: return "memset";
2122 case ISD::MEMCPY: return "memcpy";
2123 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002124
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002125 // Bit manipulation
2126 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002127 case ISD::CTPOP: return "ctpop";
2128 case ISD::CTTZ: return "cttz";
2129 case ISD::CTLZ: return "ctlz";
2130
2131 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002132 case ISD::READPORT: return "readport";
2133 case ISD::WRITEPORT: return "writeport";
2134 case ISD::READIO: return "readio";
2135 case ISD::WRITEIO: return "writeio";
2136
Chris Lattner36ce6912005-11-29 06:21:05 +00002137 // Debug info
2138 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002139 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002140 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002141
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002142 case ISD::CONDCODE:
2143 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002144 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002145 case ISD::SETOEQ: return "setoeq";
2146 case ISD::SETOGT: return "setogt";
2147 case ISD::SETOGE: return "setoge";
2148 case ISD::SETOLT: return "setolt";
2149 case ISD::SETOLE: return "setole";
2150 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002151
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002152 case ISD::SETO: return "seto";
2153 case ISD::SETUO: return "setuo";
2154 case ISD::SETUEQ: return "setue";
2155 case ISD::SETUGT: return "setugt";
2156 case ISD::SETUGE: return "setuge";
2157 case ISD::SETULT: return "setult";
2158 case ISD::SETULE: return "setule";
2159 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002160
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002161 case ISD::SETEQ: return "seteq";
2162 case ISD::SETGT: return "setgt";
2163 case ISD::SETGE: return "setge";
2164 case ISD::SETLT: return "setlt";
2165 case ISD::SETLE: return "setle";
2166 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002167 }
2168 }
2169}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002170
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002171void SDNode::dump() const { dump(0); }
2172void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002173 std::cerr << (void*)this << ": ";
2174
2175 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2176 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002177 if (getValueType(i) == MVT::Other)
2178 std::cerr << "ch";
2179 else
2180 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002181 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002182 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002183
2184 std::cerr << " ";
2185 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2186 if (i) std::cerr << ", ";
2187 std::cerr << (void*)getOperand(i).Val;
2188 if (unsigned RN = getOperand(i).ResNo)
2189 std::cerr << ":" << RN;
2190 }
2191
2192 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2193 std::cerr << "<" << CSDN->getValue() << ">";
2194 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2195 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002196 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002197 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002198 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002199 std::cerr << "<";
2200 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002201 if (offset > 0)
2202 std::cerr << " + " << offset;
2203 else
2204 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002205 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002206 std::cerr << "<" << FIDN->getIndex() << ">";
2207 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002208 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002209 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002210 std::cerr << "<";
2211 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2212 if (LBB)
2213 std::cerr << LBB->getName() << " ";
2214 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002215 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002216 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002217 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2218 } else {
2219 std::cerr << " #" << R->getReg();
2220 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002221 } else if (const ExternalSymbolSDNode *ES =
2222 dyn_cast<ExternalSymbolSDNode>(this)) {
2223 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002224 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2225 if (M->getValue())
2226 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2227 else
2228 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002229 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2230 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002231 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002232}
2233
Chris Lattnerde202b32005-11-09 23:47:37 +00002234static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002235 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2236 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002237 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002238 else
2239 std::cerr << "\n" << std::string(indent+2, ' ')
2240 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002241
Chris Lattnerea946cd2005-01-09 20:38:33 +00002242
2243 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002244 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002245}
2246
Chris Lattnerc3aae252005-01-07 07:46:32 +00002247void SelectionDAG::dump() const {
2248 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002249 std::vector<const SDNode*> Nodes;
2250 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2251 I != E; ++I)
2252 Nodes.push_back(I);
2253
Chris Lattner49d24712005-01-09 20:26:36 +00002254 std::sort(Nodes.begin(), Nodes.end());
2255
2256 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002257 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002258 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002259 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002260
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002261 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002262
Chris Lattnerc3aae252005-01-07 07:46:32 +00002263 std::cerr << "\n\n";
2264}
2265