blob: 7676ead3551f83a90a87615596317e48b910f929 [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"
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000025#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000026#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000027#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner5cdcc582005-01-09 20:52:51 +000030static bool isCommutativeBinOp(unsigned Opcode) {
31 switch (Opcode) {
32 case ISD::ADD:
33 case ISD::MUL:
Chris Lattner01b3d732005-09-28 22:28:18 +000034 case ISD::FADD:
35 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000036 case ISD::AND:
37 case ISD::OR:
38 case ISD::XOR: return true;
39 default: return false; // FIXME: Need commutative info for user ops!
40 }
41}
42
43static bool isAssociativeBinOp(unsigned Opcode) {
44 switch (Opcode) {
45 case ISD::ADD:
46 case ISD::MUL:
47 case ISD::AND:
48 case ISD::OR:
49 case ISD::XOR: return true;
50 default: return false; // FIXME: Need associative info for user ops!
51 }
52}
53
Chris Lattner5cdcc582005-01-09 20:52:51 +000054// isInvertibleForFree - Return true if there is no cost to emitting the logical
55// inverse of this node.
56static bool isInvertibleForFree(SDOperand N) {
57 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000058 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000059 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000060 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000061}
62
Jim Laskey58b968b2005-08-17 20:08:02 +000063//===----------------------------------------------------------------------===//
64// ConstantFPSDNode Class
65//===----------------------------------------------------------------------===//
66
67/// isExactlyValue - We don't rely on operator== working on double values, as
68/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
69/// As such, this method can be used to do an exact bit-for-bit comparison of
70/// two floating point values.
71bool ConstantFPSDNode::isExactlyValue(double V) const {
72 return DoubleToBits(V) == DoubleToBits(Value);
73}
74
75//===----------------------------------------------------------------------===//
76// ISD Class
77//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000078
Chris Lattnerc3aae252005-01-07 07:46:32 +000079/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
80/// when given the operation for (X op Y).
81ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
82 // To perform this operation, we just need to swap the L and G bits of the
83 // operation.
84 unsigned OldL = (Operation >> 2) & 1;
85 unsigned OldG = (Operation >> 1) & 1;
86 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
87 (OldL << 1) | // New G bit
88 (OldG << 2)); // New L bit.
89}
90
91/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
92/// 'op' is a valid SetCC operation.
93ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
94 unsigned Operation = Op;
95 if (isInteger)
96 Operation ^= 7; // Flip L, G, E bits, but not U.
97 else
98 Operation ^= 15; // Flip all of the condition bits.
99 if (Operation > ISD::SETTRUE2)
100 Operation &= ~8; // Don't let N and U bits get set.
101 return ISD::CondCode(Operation);
102}
103
104
105/// isSignedOp - For an integer comparison, return 1 if the comparison is a
106/// signed operation and 2 if the result is an unsigned comparison. Return zero
107/// if the operation does not depend on the sign of the input (setne and seteq).
108static int isSignedOp(ISD::CondCode Opcode) {
109 switch (Opcode) {
110 default: assert(0 && "Illegal integer setcc operation!");
111 case ISD::SETEQ:
112 case ISD::SETNE: return 0;
113 case ISD::SETLT:
114 case ISD::SETLE:
115 case ISD::SETGT:
116 case ISD::SETGE: return 1;
117 case ISD::SETULT:
118 case ISD::SETULE:
119 case ISD::SETUGT:
120 case ISD::SETUGE: return 2;
121 }
122}
123
124/// getSetCCOrOperation - Return the result of a logical OR between different
125/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
126/// returns SETCC_INVALID if it is not possible to represent the resultant
127/// comparison.
128ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
129 bool isInteger) {
130 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
131 // Cannot fold a signed integer setcc with an unsigned integer setcc.
132 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattnerc3aae252005-01-07 07:46:32 +0000134 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000135
Chris Lattnerc3aae252005-01-07 07:46:32 +0000136 // If the N and U bits get set then the resultant comparison DOES suddenly
137 // care about orderedness, and is true when ordered.
138 if (Op > ISD::SETTRUE2)
139 Op &= ~16; // Clear the N bit.
140 return ISD::CondCode(Op);
141}
142
143/// getSetCCAndOperation - Return the result of a logical AND between different
144/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
145/// function returns zero if it is not possible to represent the resultant
146/// comparison.
147ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
148 bool isInteger) {
149 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
150 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000151 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000152
153 // Combine all of the condition bits.
154 return ISD::CondCode(Op1 & Op2);
155}
156
Chris Lattnerb48da392005-01-23 04:39:44 +0000157const TargetMachine &SelectionDAG::getTarget() const {
158 return TLI.getTargetMachine();
159}
160
Jim Laskey58b968b2005-08-17 20:08:02 +0000161//===----------------------------------------------------------------------===//
162// SelectionDAG Class
163//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000164
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000165/// RemoveDeadNodes - This method deletes all unreachable nodes in the
166/// SelectionDAG, including nodes (like loads) that have uses of their token
167/// chain but no other uses and no side effect. If a node is passed in as an
168/// argument, it is used as the seed for node deletion.
169void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000170 // Create a dummy node (which is not added to allnodes), that adds a reference
171 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000172 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000173
Chris Lattnerf469cb62005-11-08 18:52:27 +0000174 bool MadeChange = false;
175
Chris Lattner8b8749f2005-08-17 19:00:20 +0000176 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000177 if (N && N->use_empty()) {
178 DestroyDeadNode(N);
179 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000180 }
181
Chris Lattnerde202b32005-11-09 23:47:37 +0000182 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
183 if (I->use_empty() && I->getOpcode() != 65535) {
184 // Node is dead, recursively delete newly dead uses.
185 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000186 MadeChange = true;
187 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000188
189 // Walk the nodes list, removing the nodes we've marked as dead.
190 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000191 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
192 SDNode *N = I++;
193 if (N->use_empty())
194 AllNodes.erase(N);
195 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000196 }
197
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000198 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000199 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000200}
201
Chris Lattnerf469cb62005-11-08 18:52:27 +0000202/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
203/// graph. If it is the last user of any of its operands, recursively process
204/// them the same way.
205///
206void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000207 // Okay, we really are going to delete this node. First take this out of the
208 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000209 RemoveNodeFromCSEMaps(N);
210
211 // Next, brutally remove the operand list. This is safe to do, as there are
212 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000213 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
214 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000215 O->removeUser(N);
216
217 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000218 if (O->use_empty())
219 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000220 }
Chris Lattner65113b22005-11-08 22:07:03 +0000221 delete[] N->OperandList;
222 N->OperandList = 0;
223 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000224
225 // Mark the node as dead.
226 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000227}
228
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000229void SelectionDAG::DeleteNode(SDNode *N) {
230 assert(N->use_empty() && "Cannot delete a node that is not dead!");
231
232 // First take this out of the appropriate CSE map.
233 RemoveNodeFromCSEMaps(N);
234
Chris Lattner1e111c72005-09-07 05:37:01 +0000235 // Finally, remove uses due to operands of this node, remove from the
236 // AllNodes list, and delete the node.
237 DeleteNodeNotInCSEMaps(N);
238}
239
240void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
241
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000242 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000243 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000244
245 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000246 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
247 I->Val->removeUser(N);
248 delete[] N->OperandList;
249 N->OperandList = 0;
250 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000251
252 delete N;
253}
254
Chris Lattner149c58c2005-08-16 18:17:10 +0000255/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
256/// correspond to it. This is useful when we're about to delete or repurpose
257/// the node. We don't want future request for structurally identical nodes
258/// to return N anymore.
259void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000260 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000262 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000263 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000264 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
265 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000267 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000268 Erased = TargetConstants.erase(std::make_pair(
269 cast<ConstantSDNode>(N)->getValue(),
270 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000271 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000272 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000273 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000274 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000275 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000276 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000277 case ISD::CONDCODE:
278 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
279 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000280 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000281 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
282 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000283 case ISD::GlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000284 Erased = GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000285 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000286 case ISD::TargetGlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000287 Erased =TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000288 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000289 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000290 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000291 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000292 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000293 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000294 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000295 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000296 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000297 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000298 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000299 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000300 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000302 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000303 break;
304 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000305 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000307 case ISD::TargetExternalSymbol:
308 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
309 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000310 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000311 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000312 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
313 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000314 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000315 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
316 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000317 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000318 case ISD::SRCVALUE: {
319 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000320 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000321 break;
322 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000323 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000324 Erased = Loads.erase(std::make_pair(N->getOperand(1),
325 std::make_pair(N->getOperand(0),
326 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000327 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000328 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000329 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000330 if (N->getNumOperands() == 0) {
331 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
332 N->getValueType(0)));
333 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000334 Erased =
335 UnaryOps.erase(std::make_pair(N->getOpcode(),
336 std::make_pair(N->getOperand(0),
337 N->getValueType(0))));
338 } else if (N->getNumOperands() == 2) {
339 Erased =
340 BinaryOps.erase(std::make_pair(N->getOpcode(),
341 std::make_pair(N->getOperand(0),
342 N->getOperand(1))));
343 } else {
344 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
345 Erased =
346 OneResultNodes.erase(std::make_pair(N->getOpcode(),
347 std::make_pair(N->getValueType(0),
348 Ops)));
349 }
Chris Lattner385328c2005-05-14 07:42:29 +0000350 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000351 // Remove the node from the ArbitraryNodes map.
352 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
353 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000354 Erased =
355 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
356 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000357 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000358 break;
359 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000360#ifndef NDEBUG
361 // Verify that the node was actually in one of the CSE maps, unless it has a
362 // flag result (which cannot be CSE'd) or is one of the special cases that are
363 // not subject to CSE.
364 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
365 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000366 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000367
368 N->dump();
369 assert(0 && "Node is not in map!");
370 }
371#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000372}
373
Chris Lattner8b8749f2005-08-17 19:00:20 +0000374/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
375/// has been taken out and modified in some way. If the specified node already
376/// exists in the CSE maps, do not modify the maps, but return the existing node
377/// instead. If it doesn't exist, add it and return null.
378///
379SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
380 assert(N->getNumOperands() && "This is a leaf node!");
381 if (N->getOpcode() == ISD::LOAD) {
382 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
383 std::make_pair(N->getOperand(0),
384 N->getValueType(0)))];
385 if (L) return L;
386 L = N;
Chris Lattner95038592005-10-05 06:35:28 +0000387 } else if (N->getOpcode() == ISD::HANDLENODE) {
388 return 0; // never add it.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000389 } else if (N->getNumOperands() == 1) {
390 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
391 std::make_pair(N->getOperand(0),
392 N->getValueType(0)))];
393 if (U) return U;
394 U = N;
395 } else if (N->getNumOperands() == 2) {
396 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
397 std::make_pair(N->getOperand(0),
398 N->getOperand(1)))];
399 if (B) return B;
400 B = N;
401 } else if (N->getNumValues() == 1) {
402 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
403 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
404 std::make_pair(N->getValueType(0), Ops))];
405 if (ORN) return ORN;
406 ORN = N;
407 } else {
408 // Remove the node from the ArbitraryNodes map.
409 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
410 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
411 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
412 std::make_pair(RV, Ops))];
413 if (AN) return AN;
414 AN = N;
415 }
416 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000417}
418
419
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000420
Chris Lattner78ec3112003-08-11 14:57:33 +0000421SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000422 while (!AllNodes.empty()) {
423 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000424 delete [] N->OperandList;
425 N->OperandList = 0;
426 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000427 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000428 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000429}
430
Chris Lattner0f2287b2005-04-13 02:38:18 +0000431SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000432 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000433 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000434 return getNode(ISD::AND, Op.getValueType(), Op,
435 getConstant(Imm, Op.getValueType()));
436}
437
Chris Lattnerc3aae252005-01-07 07:46:32 +0000438SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
439 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
440 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000441 if (VT != MVT::i64)
442 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000443
Chris Lattnerc3aae252005-01-07 07:46:32 +0000444 SDNode *&N = Constants[std::make_pair(Val, VT)];
445 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000446 N = new ConstantSDNode(false, Val, VT);
447 AllNodes.push_back(N);
448 return SDOperand(N, 0);
449}
450
451SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
452 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
453 // Mask out any bits that are not valid for this constant.
454 if (VT != MVT::i64)
455 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
456
457 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
458 if (N) return SDOperand(N, 0);
459 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000460 AllNodes.push_back(N);
461 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000462}
463
Chris Lattnerc3aae252005-01-07 07:46:32 +0000464SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
465 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
466 if (VT == MVT::f32)
467 Val = (float)Val; // Mask out extra precision.
468
Chris Lattnerd8658612005-02-17 20:17:32 +0000469 // Do the map lookup using the actual bit pattern for the floating point
470 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
471 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000472 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000473 if (N) return SDOperand(N, 0);
474 N = new ConstantFPSDNode(Val, VT);
475 AllNodes.push_back(N);
476 return SDOperand(N, 0);
477}
478
479
480
481SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
482 MVT::ValueType VT) {
483 SDNode *&N = GlobalValues[GV];
484 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000485 N = new GlobalAddressSDNode(false, GV, VT);
486 AllNodes.push_back(N);
487 return SDOperand(N, 0);
488}
489
490SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
491 MVT::ValueType VT) {
492 SDNode *&N = TargetGlobalValues[GV];
493 if (N) return SDOperand(N, 0);
494 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000495 AllNodes.push_back(N);
496 return SDOperand(N, 0);
497}
498
499SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
500 SDNode *&N = FrameIndices[FI];
501 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000502 N = new FrameIndexSDNode(FI, VT, false);
503 AllNodes.push_back(N);
504 return SDOperand(N, 0);
505}
506
507SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
508 SDNode *&N = TargetFrameIndices[FI];
509 if (N) return SDOperand(N, 0);
510 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000511 AllNodes.push_back(N);
512 return SDOperand(N, 0);
513}
514
Chris Lattner5839bf22005-08-26 17:15:30 +0000515SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
516 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000517 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000518 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000519 AllNodes.push_back(N);
520 return SDOperand(N, 0);
521}
522
Chris Lattner5839bf22005-08-26 17:15:30 +0000523SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
524 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000525 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000526 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000527 AllNodes.push_back(N);
528 return SDOperand(N, 0);
529}
530
531SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
532 SDNode *&N = BBNodes[MBB];
533 if (N) return SDOperand(N, 0);
534 N = new BasicBlockSDNode(MBB);
535 AllNodes.push_back(N);
536 return SDOperand(N, 0);
537}
538
Chris Lattner15e4b012005-07-10 00:07:11 +0000539SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
540 if ((unsigned)VT >= ValueTypeNodes.size())
541 ValueTypeNodes.resize(VT+1);
542 if (ValueTypeNodes[VT] == 0) {
543 ValueTypeNodes[VT] = new VTSDNode(VT);
544 AllNodes.push_back(ValueTypeNodes[VT]);
545 }
546
547 return SDOperand(ValueTypeNodes[VT], 0);
548}
549
Chris Lattnerc3aae252005-01-07 07:46:32 +0000550SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
551 SDNode *&N = ExternalSymbols[Sym];
552 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000553 N = new ExternalSymbolSDNode(false, Sym, VT);
554 AllNodes.push_back(N);
555 return SDOperand(N, 0);
556}
557
558SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
559 SDNode *&N = TargetExternalSymbols[Sym];
560 if (N) return SDOperand(N, 0);
561 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000562 AllNodes.push_back(N);
563 return SDOperand(N, 0);
564}
565
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000566SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
567 if ((unsigned)Cond >= CondCodeNodes.size())
568 CondCodeNodes.resize(Cond+1);
569
Chris Lattner079a27a2005-08-09 20:40:02 +0000570 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000571 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000572 AllNodes.push_back(CondCodeNodes[Cond]);
573 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000574 return SDOperand(CondCodeNodes[Cond], 0);
575}
576
Chris Lattner0fdd7682005-08-30 22:38:38 +0000577SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
578 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
579 if (!Reg) {
580 Reg = new RegisterSDNode(RegNo, VT);
581 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000582 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000583 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000584}
585
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000586SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
587 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000588 // These setcc operations always fold.
589 switch (Cond) {
590 default: break;
591 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000592 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000593 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000594 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000595 }
596
Chris Lattner67255a12005-04-07 18:14:58 +0000597 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
598 uint64_t C2 = N2C->getValue();
599 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
600 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000601
Chris Lattnerc3aae252005-01-07 07:46:32 +0000602 // Sign extend the operands if required
603 if (ISD::isSignedIntSetCC(Cond)) {
604 C1 = N1C->getSignExtended();
605 C2 = N2C->getSignExtended();
606 }
607
608 switch (Cond) {
609 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000610 case ISD::SETEQ: return getConstant(C1 == C2, VT);
611 case ISD::SETNE: return getConstant(C1 != C2, VT);
612 case ISD::SETULT: return getConstant(C1 < C2, VT);
613 case ISD::SETUGT: return getConstant(C1 > C2, VT);
614 case ISD::SETULE: return getConstant(C1 <= C2, VT);
615 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
616 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
617 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
618 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
619 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000620 }
Chris Lattner24673922005-04-07 18:58:54 +0000621 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000622 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000623 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
624 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
625
626 // If the comparison constant has bits in the upper part, the
627 // zero-extended value could never match.
628 if (C2 & (~0ULL << InSize)) {
629 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
630 switch (Cond) {
631 case ISD::SETUGT:
632 case ISD::SETUGE:
633 case ISD::SETEQ: return getConstant(0, VT);
634 case ISD::SETULT:
635 case ISD::SETULE:
636 case ISD::SETNE: return getConstant(1, VT);
637 case ISD::SETGT:
638 case ISD::SETGE:
639 // True if the sign bit of C2 is set.
640 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
641 case ISD::SETLT:
642 case ISD::SETLE:
643 // True if the sign bit of C2 isn't set.
644 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
645 default:
646 break;
647 }
648 }
649
650 // Otherwise, we can perform the comparison with the low bits.
651 switch (Cond) {
652 case ISD::SETEQ:
653 case ISD::SETNE:
654 case ISD::SETUGT:
655 case ISD::SETUGE:
656 case ISD::SETULT:
657 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000658 return getSetCC(VT, N1.getOperand(0),
659 getConstant(C2, N1.getOperand(0).getValueType()),
660 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000661 default:
662 break; // todo, be more careful with signed comparisons
663 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000664 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
665 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
666 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
667 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
668 MVT::ValueType ExtDstTy = N1.getValueType();
669 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000670
Chris Lattner7b2880c2005-08-24 22:44:39 +0000671 // If the extended part has any inconsistent bits, it cannot ever
672 // compare equal. In other words, they have to be all ones or all
673 // zeros.
674 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000675 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000676 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
677 return getConstant(Cond == ISD::SETNE, VT);
678
679 // Otherwise, make this a use of a zext.
680 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000681 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000682 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000683 }
684
Chris Lattner67255a12005-04-07 18:14:58 +0000685 uint64_t MinVal, MaxVal;
686 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
687 if (ISD::isSignedIntSetCC(Cond)) {
688 MinVal = 1ULL << (OperandBitSize-1);
689 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
690 MaxVal = ~0ULL >> (65-OperandBitSize);
691 else
692 MaxVal = 0;
693 } else {
694 MinVal = 0;
695 MaxVal = ~0ULL >> (64-OperandBitSize);
696 }
697
698 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
699 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
700 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
701 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000702 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
703 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000704 }
705
706 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
707 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
708 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000709 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
710 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000711 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000712
Nate Begeman72ea2812005-04-14 08:56:52 +0000713 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
714 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000715
Nate Begeman72ea2812005-04-14 08:56:52 +0000716 // Canonicalize setgt X, Min --> setne X, Min
717 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000718 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000719
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000720 // If we have setult X, 1, turn it into seteq X, 0
721 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000722 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
723 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000724 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000725 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000726 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
727 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000728
729 // If we have "setcc X, C1", check to see if we can shrink the immediate
730 // by changing cc.
731
732 // SETUGT X, SINTMAX -> SETLT X, 0
733 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
734 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000735 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000736
737 // FIXME: Implement the rest of these.
738
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000739
740 // Fold bit comparisons when we can.
741 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
742 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
743 if (ConstantSDNode *AndRHS =
744 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
745 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
746 // Perform the xform if the AND RHS is a single bit.
747 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
748 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000749 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000750 TLI.getShiftAmountTy()));
751 }
752 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
753 // (X & 8) == 8 --> (X & 8) >> 3
754 // Perform the xform if C2 is a single bit.
755 if ((C2 & (C2-1)) == 0) {
756 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000757 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000758 }
759 }
760 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000761 }
Chris Lattner67255a12005-04-07 18:14:58 +0000762 } else if (isa<ConstantSDNode>(N1.Val)) {
763 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000764 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000765 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000766
767 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
768 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
769 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000770
Chris Lattnerc3aae252005-01-07 07:46:32 +0000771 switch (Cond) {
772 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000773 case ISD::SETEQ: return getConstant(C1 == C2, VT);
774 case ISD::SETNE: return getConstant(C1 != C2, VT);
775 case ISD::SETLT: return getConstant(C1 < C2, VT);
776 case ISD::SETGT: return getConstant(C1 > C2, VT);
777 case ISD::SETLE: return getConstant(C1 <= C2, VT);
778 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000779 }
780 } else {
781 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000782 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783 }
784
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000785 // Could not fold it.
786 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000787}
788
Chris Lattnerc3aae252005-01-07 07:46:32 +0000789/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000790///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000791SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000792 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
793 if (!N) {
794 N = new SDNode(Opcode, VT);
795 AllNodes.push_back(N);
796 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000797 return SDOperand(N, 0);
798}
799
Chris Lattnerc3aae252005-01-07 07:46:32 +0000800SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
801 SDOperand Operand) {
802 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
803 uint64_t Val = C->getValue();
804 switch (Opcode) {
805 default: break;
806 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000807 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000808 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
809 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000810 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
811 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812 }
813 }
814
815 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
816 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000817 case ISD::FNEG:
818 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000819 case ISD::FP_ROUND:
820 case ISD::FP_EXTEND:
821 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000822 case ISD::FP_TO_SINT:
823 return getConstant((int64_t)C->getValue(), VT);
824 case ISD::FP_TO_UINT:
825 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000826 }
827
828 unsigned OpOpcode = Operand.Val->getOpcode();
829 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000830 case ISD::TokenFactor:
831 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000832 case ISD::SIGN_EXTEND:
833 if (Operand.getValueType() == VT) return Operand; // noop extension
834 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
835 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
836 break;
837 case ISD::ZERO_EXTEND:
838 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000839 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000840 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000842 case ISD::ANY_EXTEND:
843 if (Operand.getValueType() == VT) return Operand; // noop extension
844 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
845 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
846 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
847 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000848 case ISD::TRUNCATE:
849 if (Operand.getValueType() == VT) return Operand; // noop truncate
850 if (OpOpcode == ISD::TRUNCATE)
851 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000852 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
853 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000854 // If the source is smaller than the dest, we still need an extend.
855 if (Operand.Val->getOperand(0).getValueType() < VT)
856 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
857 else if (Operand.Val->getOperand(0).getValueType() > VT)
858 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
859 else
860 return Operand.Val->getOperand(0);
861 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000862 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000863 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000864 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
865 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000866 Operand.Val->getOperand(0));
867 if (OpOpcode == ISD::FNEG) // --X -> X
868 return Operand.Val->getOperand(0);
869 break;
870 case ISD::FABS:
871 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
872 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
873 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000874 }
875
Chris Lattner43247a12005-08-25 19:12:10 +0000876 SDNode *N;
877 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
878 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000879 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000880 E = N = new SDNode(Opcode, Operand);
881 } else {
882 N = new SDNode(Opcode, Operand);
883 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000884 N->setValueTypes(VT);
885 AllNodes.push_back(N);
886 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000887}
888
Chris Lattner36019aa2005-04-18 03:48:41 +0000889
890
Chris Lattnerc3aae252005-01-07 07:46:32 +0000891SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
892 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000893#ifndef NDEBUG
894 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000895 case ISD::TokenFactor:
896 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
897 N2.getValueType() == MVT::Other && "Invalid token factor!");
898 break;
Chris Lattner76365122005-01-16 02:23:22 +0000899 case ISD::AND:
900 case ISD::OR:
901 case ISD::XOR:
902 case ISD::UDIV:
903 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000904 case ISD::MULHU:
905 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000906 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
907 // fall through
908 case ISD::ADD:
909 case ISD::SUB:
910 case ISD::MUL:
911 case ISD::SDIV:
912 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +0000913 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
914 // fall through.
915 case ISD::FADD:
916 case ISD::FSUB:
917 case ISD::FMUL:
918 case ISD::FDIV:
919 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +0000920 assert(N1.getValueType() == N2.getValueType() &&
921 N1.getValueType() == VT && "Binary operator types must match!");
922 break;
923
924 case ISD::SHL:
925 case ISD::SRA:
926 case ISD::SRL:
927 assert(VT == N1.getValueType() &&
928 "Shift operators return type must be the same as their first arg");
929 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000930 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000931 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000932 case ISD::FP_ROUND_INREG: {
933 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
934 assert(VT == N1.getValueType() && "Not an inreg round!");
935 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
936 "Cannot FP_ROUND_INREG integer types");
937 assert(EVT <= VT && "Not rounding down!");
938 break;
939 }
Nate Begeman56eb8682005-08-30 02:44:00 +0000940 case ISD::AssertSext:
941 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +0000942 case ISD::SIGN_EXTEND_INREG: {
943 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
944 assert(VT == N1.getValueType() && "Not an inreg extend!");
945 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
946 "Cannot *_EXTEND_INREG FP types");
947 assert(EVT <= VT && "Not extending!");
948 }
949
Chris Lattner76365122005-01-16 02:23:22 +0000950 default: break;
951 }
952#endif
953
Chris Lattnerc3aae252005-01-07 07:46:32 +0000954 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
955 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
956 if (N1C) {
957 if (N2C) {
958 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
959 switch (Opcode) {
960 case ISD::ADD: return getConstant(C1 + C2, VT);
961 case ISD::SUB: return getConstant(C1 - C2, VT);
962 case ISD::MUL: return getConstant(C1 * C2, VT);
963 case ISD::UDIV:
964 if (C2) return getConstant(C1 / C2, VT);
965 break;
966 case ISD::UREM :
967 if (C2) return getConstant(C1 % C2, VT);
968 break;
969 case ISD::SDIV :
970 if (C2) return getConstant(N1C->getSignExtended() /
971 N2C->getSignExtended(), VT);
972 break;
973 case ISD::SREM :
974 if (C2) return getConstant(N1C->getSignExtended() %
975 N2C->getSignExtended(), VT);
976 break;
977 case ISD::AND : return getConstant(C1 & C2, VT);
978 case ISD::OR : return getConstant(C1 | C2, VT);
979 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +0000980 case ISD::SHL : return getConstant(C1 << C2, VT);
981 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000982 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983 default: break;
984 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000985 } else { // Cannonicalize constant to RHS if commutative
986 if (isCommutativeBinOp(Opcode)) {
987 std::swap(N1C, N2C);
988 std::swap(N1, N2);
989 }
990 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000991 }
992
993 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
994 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +0000995 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996 if (N2CFP) {
997 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
998 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +0000999 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1000 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1001 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1002 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001003 if (C2) return getConstantFP(C1 / C2, VT);
1004 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001005 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001006 if (C2) return getConstantFP(fmod(C1, C2), VT);
1007 break;
1008 default: break;
1009 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001010 } else { // Cannonicalize constant to RHS if commutative
1011 if (isCommutativeBinOp(Opcode)) {
1012 std::swap(N1CFP, N2CFP);
1013 std::swap(N1, N2);
1014 }
1015 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001016 }
1017
Chris Lattnerc3aae252005-01-07 07:46:32 +00001018 // Finally, fold operations that do not require constants.
1019 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001020 case ISD::FP_ROUND_INREG:
1021 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1022 break;
1023 case ISD::SIGN_EXTEND_INREG: {
1024 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1025 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001026 break;
1027 }
1028
Nate Begemaneea805e2005-04-13 21:23:31 +00001029 // FIXME: figure out how to safely handle things like
1030 // int foo(int x) { return 1 << (x & 255); }
1031 // int bar() { return foo(256); }
1032#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001033 case ISD::SHL:
1034 case ISD::SRL:
1035 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001036 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001037 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001038 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001039 else if (N2.getOpcode() == ISD::AND)
1040 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1041 // If the and is only masking out bits that cannot effect the shift,
1042 // eliminate the and.
1043 unsigned NumBits = MVT::getSizeInBits(VT);
1044 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1045 return getNode(Opcode, VT, N1, N2.getOperand(0));
1046 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001047 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001048#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001049 }
1050
Chris Lattner27e9b412005-05-11 18:57:39 +00001051 // Memoize this node if possible.
1052 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001053 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1054 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001055 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1056 if (BON) return SDOperand(BON, 0);
1057
1058 BON = N = new SDNode(Opcode, N1, N2);
1059 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001060 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001061 }
1062
Chris Lattner3e011362005-05-14 07:45:46 +00001063 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001064 AllNodes.push_back(N);
1065 return SDOperand(N, 0);
1066}
1067
Chris Lattner88de6e72005-05-12 00:17:04 +00001068// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001069// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001070void SDNode::setAdjCallChain(SDOperand N) {
1071 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001072 assert((getOpcode() == ISD::CALLSEQ_START ||
1073 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001074
Chris Lattner65113b22005-11-08 22:07:03 +00001075 OperandList[0].Val->removeUser(this);
1076 OperandList[0] = N;
1077 OperandList[0].Val->Uses.push_back(this);
Chris Lattner27e9b412005-05-11 18:57:39 +00001078}
1079
1080
1081
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001083 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001084 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001085 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1086 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001087 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088
1089 // Loads have a token chain.
Chris Lattnera3255112005-11-08 23:30:28 +00001090 setNodeValueTypes(N, VT, MVT::Other);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001091 AllNodes.push_back(N);
1092 return SDOperand(N, 0);
1093}
1094
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001095SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1096 SDOperand Chain, SDOperand Ptr,
1097 SDOperand SV) {
1098 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1099 if (N) return SDOperand(N, 0);
1100 std::vector<SDOperand> Ops;
1101 Ops.reserve(5);
1102 Ops.push_back(Chain);
1103 Ops.push_back(Ptr);
1104 Ops.push_back(getConstant(Count, MVT::i32));
1105 Ops.push_back(getValueType(EVT));
1106 Ops.push_back(SV);
1107 std::vector<MVT::ValueType> VTs;
1108 VTs.reserve(2);
Nate Begemanab48be32005-11-22 18:16:00 +00001109 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001110 return getNode(ISD::VLOAD, VTs, Ops);
1111}
Chris Lattner5f056bf2005-07-10 01:55:33 +00001112
1113SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1114 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1115 MVT::ValueType EVT) {
1116 std::vector<SDOperand> Ops;
1117 Ops.reserve(4);
1118 Ops.push_back(Chain);
1119 Ops.push_back(Ptr);
1120 Ops.push_back(SV);
1121 Ops.push_back(getValueType(EVT));
1122 std::vector<MVT::ValueType> VTs;
1123 VTs.reserve(2);
1124 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1125 return getNode(Opcode, VTs, Ops);
1126}
1127
Chris Lattnerc3aae252005-01-07 07:46:32 +00001128SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1129 SDOperand N1, SDOperand N2, SDOperand N3) {
1130 // Perform various simplifications.
1131 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1132 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1133 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1134 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001135 case ISD::SETCC: {
1136 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001137 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001138 if (Simp.Val) return Simp;
1139 break;
1140 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001141 case ISD::SELECT:
1142 if (N1C)
1143 if (N1C->getValue())
1144 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001145 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001146 return N3; // select false, X, Y -> Y
1147
1148 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001149 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001150 case ISD::BRCOND:
1151 if (N2C)
1152 if (N2C->getValue()) // Unconditional branch
1153 return getNode(ISD::BR, MVT::Other, N1, N3);
1154 else
1155 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001156 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001157 }
1158
Chris Lattner385328c2005-05-14 07:42:29 +00001159 std::vector<SDOperand> Ops;
1160 Ops.reserve(3);
1161 Ops.push_back(N1);
1162 Ops.push_back(N2);
1163 Ops.push_back(N3);
1164
Chris Lattner43247a12005-08-25 19:12:10 +00001165 // Memoize node if it doesn't produce a flag.
1166 SDNode *N;
1167 if (VT != MVT::Flag) {
1168 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1169 if (E) return SDOperand(E, 0);
1170 E = N = new SDNode(Opcode, N1, N2, N3);
1171 } else {
1172 N = new SDNode(Opcode, N1, N2, N3);
1173 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001174 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001175 AllNodes.push_back(N);
1176 return SDOperand(N, 0);
1177}
1178
1179SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001180 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001181 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001182 std::vector<SDOperand> Ops;
1183 Ops.reserve(4);
1184 Ops.push_back(N1);
1185 Ops.push_back(N2);
1186 Ops.push_back(N3);
1187 Ops.push_back(N4);
1188 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001189}
1190
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001191SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1192 SDOperand N1, SDOperand N2, SDOperand N3,
1193 SDOperand N4, SDOperand N5) {
1194 std::vector<SDOperand> Ops;
1195 Ops.reserve(5);
1196 Ops.push_back(N1);
1197 Ops.push_back(N2);
1198 Ops.push_back(N3);
1199 Ops.push_back(N4);
1200 Ops.push_back(N5);
1201 return getNode(Opcode, VT, Ops);
1202}
1203
1204
Chris Lattner0437cdd2005-05-09 04:14:13 +00001205SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001206 assert((!V || isa<PointerType>(V->getType())) &&
1207 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001208 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1209 if (N) return SDOperand(N, 0);
1210
1211 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001212 AllNodes.push_back(N);
1213 return SDOperand(N, 0);
1214}
1215
1216SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001217 std::vector<SDOperand> &Ops) {
1218 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001220 case 1: return getNode(Opcode, VT, Ops[0]);
1221 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1222 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001223 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001224 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001225
Chris Lattner89c34632005-05-14 06:20:26 +00001226 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001227 switch (Opcode) {
1228 default: break;
1229 case ISD::BRCONDTWOWAY:
1230 if (N1C)
1231 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001232 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001233 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001234 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001235 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001236 case ISD::BRTWOWAY_CC:
1237 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1238 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1239 "LHS and RHS of comparison must have same type!");
1240 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001241 case ISD::TRUNCSTORE: {
1242 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1243 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1244#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1245 // If this is a truncating store of a constant, convert to the desired type
1246 // and store it instead.
1247 if (isa<Constant>(Ops[0])) {
1248 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1249 if (isa<Constant>(Op))
1250 N1 = Op;
1251 }
1252 // Also for ConstantFP?
1253#endif
1254 if (Ops[0].getValueType() == EVT) // Normal store?
1255 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1256 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1257 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1258 "Can't do FP-INT conversion!");
1259 break;
1260 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001261 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001262 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001263 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1264 "LHS and RHS of condition must have same type!");
1265 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1266 "True and False arms of SelectCC must have same type!");
1267 assert(Ops[2].getValueType() == VT &&
1268 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001269 break;
1270 }
1271 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001272 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001273 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1274 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001275 break;
1276 }
Chris Lattneref847df2005-04-09 03:27:28 +00001277 }
1278
Chris Lattner385328c2005-05-14 07:42:29 +00001279 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001280 SDNode *N;
1281 if (VT != MVT::Flag) {
1282 SDNode *&E =
1283 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1284 if (E) return SDOperand(E, 0);
1285 E = N = new SDNode(Opcode, Ops);
1286 } else {
1287 N = new SDNode(Opcode, Ops);
1288 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001289 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001290 AllNodes.push_back(N);
1291 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001292}
1293
Chris Lattner89c34632005-05-14 06:20:26 +00001294SDOperand SelectionDAG::getNode(unsigned Opcode,
1295 std::vector<MVT::ValueType> &ResultTys,
1296 std::vector<SDOperand> &Ops) {
1297 if (ResultTys.size() == 1)
1298 return getNode(Opcode, ResultTys[0], Ops);
1299
Chris Lattner5f056bf2005-07-10 01:55:33 +00001300 switch (Opcode) {
1301 case ISD::EXTLOAD:
1302 case ISD::SEXTLOAD:
1303 case ISD::ZEXTLOAD: {
1304 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1305 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1306 // If they are asking for an extending load from/to the same thing, return a
1307 // normal load.
1308 if (ResultTys[0] == EVT)
1309 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1310 assert(EVT < ResultTys[0] &&
1311 "Should only be an extending load, not truncating!");
1312 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1313 "Cannot sign/zero extend a FP load!");
1314 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1315 "Cannot convert from FP to Int or Int -> FP!");
1316 break;
1317 }
1318
Chris Lattnere89083a2005-05-14 07:25:05 +00001319 // FIXME: figure out how to safely handle things like
1320 // int foo(int x) { return 1 << (x & 255); }
1321 // int bar() { return foo(256); }
1322#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001323 case ISD::SRA_PARTS:
1324 case ISD::SRL_PARTS:
1325 case ISD::SHL_PARTS:
1326 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001327 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001328 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1329 else if (N3.getOpcode() == ISD::AND)
1330 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1331 // If the and is only masking out bits that cannot effect the shift,
1332 // eliminate the and.
1333 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1334 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1335 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1336 }
1337 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001338#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001339 }
Chris Lattner89c34632005-05-14 06:20:26 +00001340
Chris Lattner43247a12005-08-25 19:12:10 +00001341 // Memoize the node unless it returns a flag.
1342 SDNode *N;
1343 if (ResultTys.back() != MVT::Flag) {
1344 SDNode *&E =
1345 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1346 if (E) return SDOperand(E, 0);
1347 E = N = new SDNode(Opcode, Ops);
1348 } else {
1349 N = new SDNode(Opcode, Ops);
1350 }
Chris Lattnera3255112005-11-08 23:30:28 +00001351 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001352 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001353 return SDOperand(N, 0);
1354}
1355
Chris Lattnera3255112005-11-08 23:30:28 +00001356void SelectionDAG::setNodeValueTypes(SDNode *N,
1357 std::vector<MVT::ValueType> &RetVals) {
1358 switch (RetVals.size()) {
1359 case 0: return;
1360 case 1: N->setValueTypes(RetVals[0]); return;
1361 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1362 default: break;
1363 }
1364
1365 std::list<std::vector<MVT::ValueType> >::iterator I =
1366 std::find(VTList.begin(), VTList.end(), RetVals);
1367 if (I == VTList.end()) {
1368 VTList.push_front(RetVals);
1369 I = VTList.begin();
1370 }
1371
1372 N->setValueTypes(&(*I)[0], I->size());
1373}
1374
1375void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1376 MVT::ValueType VT2) {
1377 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1378 E = VTList.end(); I != E; ++I) {
1379 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1380 N->setValueTypes(&(*I)[0], 2);
1381 return;
1382 }
1383 }
1384 std::vector<MVT::ValueType> V;
1385 V.push_back(VT1);
1386 V.push_back(VT2);
1387 VTList.push_front(V);
1388 N->setValueTypes(&(*VTList.begin())[0], 2);
1389}
1390
Chris Lattner149c58c2005-08-16 18:17:10 +00001391
1392/// SelectNodeTo - These are used for target selectors to *mutate* the
1393/// specified node to have the specified return type, Target opcode, and
1394/// operands. Note that target opcodes are stored as
1395/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001396void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1397 MVT::ValueType VT) {
Chris Lattner7651fa42005-08-24 23:00:29 +00001398 RemoveNodeFromCSEMaps(N);
1399 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1400 N->setValueTypes(VT);
1401}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001402
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001403void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1404 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner149c58c2005-08-16 18:17:10 +00001405 RemoveNodeFromCSEMaps(N);
1406 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1407 N->setValueTypes(VT);
1408 N->setOperands(Op1);
1409}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001410
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001411void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1412 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001413 SDOperand Op2) {
1414 RemoveNodeFromCSEMaps(N);
1415 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1416 N->setValueTypes(VT);
1417 N->setOperands(Op1, Op2);
1418}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001419
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001420void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1421 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001422 SDOperand Op2, SDOperand Op3) {
1423 RemoveNodeFromCSEMaps(N);
1424 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1425 N->setValueTypes(VT);
1426 N->setOperands(Op1, Op2, Op3);
1427}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001428
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001429void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1430 MVT::ValueType VT, SDOperand Op1,
Nate Begeman294a0a12005-08-18 07:30:15 +00001431 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1432 RemoveNodeFromCSEMaps(N);
1433 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1434 N->setValueTypes(VT);
1435 N->setOperands(Op1, Op2, Op3, Op4);
1436}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001437
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001438void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1439 MVT::ValueType VT, SDOperand Op1,
Chris Lattner6b09a292005-08-21 18:49:33 +00001440 SDOperand Op2, SDOperand Op3, SDOperand Op4,
1441 SDOperand Op5) {
1442 RemoveNodeFromCSEMaps(N);
1443 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1444 N->setValueTypes(VT);
1445 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1446}
Chris Lattner149c58c2005-08-16 18:17:10 +00001447
Chris Lattner0fb094f2005-11-19 01:44:53 +00001448void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1449 MVT::ValueType VT1, MVT::ValueType VT2,
1450 SDOperand Op1, SDOperand Op2) {
1451 RemoveNodeFromCSEMaps(N);
1452 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1453 setNodeValueTypes(N, VT1, VT2);
1454 N->setOperands(Op1, Op2);
1455}
1456
1457void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1458 MVT::ValueType VT1, MVT::ValueType VT2,
1459 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1460 RemoveNodeFromCSEMaps(N);
1461 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1462 setNodeValueTypes(N, VT1, VT2);
1463 N->setOperands(Op1, Op2, Op3);
1464}
1465
1466void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1467 MVT::ValueType VT1, MVT::ValueType VT2,
1468 SDOperand Op1, SDOperand Op2,
1469 SDOperand Op3, SDOperand Op4) {
1470 RemoveNodeFromCSEMaps(N);
1471 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1472 setNodeValueTypes(N, VT1, VT2);
1473 N->setOperands(Op1, Op2, Op3, Op4);
1474}
1475
1476void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1477 MVT::ValueType VT1, MVT::ValueType VT2,
1478 SDOperand Op1, SDOperand Op2,
1479 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1480 RemoveNodeFromCSEMaps(N);
1481 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1482 setNodeValueTypes(N, VT1, VT2);
1483 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1484}
1485
1486// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00001487/// This can cause recursive merging of nodes in the DAG.
1488///
Chris Lattner8b52f212005-08-26 18:36:28 +00001489/// This version assumes From/To have a single result value.
1490///
Chris Lattner1e111c72005-09-07 05:37:01 +00001491void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1492 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001493 SDNode *From = FromN.Val, *To = ToN.Val;
1494 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1495 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001496 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001497
Chris Lattner8b8749f2005-08-17 19:00:20 +00001498 while (!From->use_empty()) {
1499 // Process users until they are all gone.
1500 SDNode *U = *From->use_begin();
1501
1502 // This node is about to morph, remove its old self from the CSE maps.
1503 RemoveNodeFromCSEMaps(U);
1504
Chris Lattner65113b22005-11-08 22:07:03 +00001505 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1506 I != E; ++I)
1507 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001508 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001509 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00001510 To->addUser(U);
1511 }
1512
1513 // Now that we have modified U, add it back to the CSE maps. If it already
1514 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001515 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1516 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001517 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001518 if (Deleted) Deleted->push_back(U);
1519 DeleteNodeNotInCSEMaps(U);
1520 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001521 }
1522}
1523
1524/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1525/// This can cause recursive merging of nodes in the DAG.
1526///
1527/// This version assumes From/To have matching types and numbers of result
1528/// values.
1529///
Chris Lattner1e111c72005-09-07 05:37:01 +00001530void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1531 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001532 assert(From != To && "Cannot replace uses of with self");
1533 assert(From->getNumValues() == To->getNumValues() &&
1534 "Cannot use this version of ReplaceAllUsesWith!");
1535 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001536 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001537 return;
1538 }
1539
1540 while (!From->use_empty()) {
1541 // Process users until they are all gone.
1542 SDNode *U = *From->use_begin();
1543
1544 // This node is about to morph, remove its old self from the CSE maps.
1545 RemoveNodeFromCSEMaps(U);
1546
Chris Lattner65113b22005-11-08 22:07:03 +00001547 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1548 I != E; ++I)
1549 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001550 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001551 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00001552 To->addUser(U);
1553 }
1554
1555 // Now that we have modified U, add it back to the CSE maps. If it already
1556 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001557 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1558 ReplaceAllUsesWith(U, Existing, Deleted);
1559 // U is now dead.
1560 if (Deleted) Deleted->push_back(U);
1561 DeleteNodeNotInCSEMaps(U);
1562 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001563 }
1564}
1565
Chris Lattner8b52f212005-08-26 18:36:28 +00001566/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1567/// This can cause recursive merging of nodes in the DAG.
1568///
1569/// This version can replace From with any result values. To must match the
1570/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001571void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001572 const std::vector<SDOperand> &To,
1573 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001574 assert(From->getNumValues() == To.size() &&
1575 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001576 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001577 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001578 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001579 return;
1580 }
1581
1582 while (!From->use_empty()) {
1583 // Process users until they are all gone.
1584 SDNode *U = *From->use_begin();
1585
1586 // This node is about to morph, remove its old self from the CSE maps.
1587 RemoveNodeFromCSEMaps(U);
1588
Chris Lattner65113b22005-11-08 22:07:03 +00001589 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1590 I != E; ++I)
1591 if (I->Val == From) {
1592 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001593 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001594 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001595 ToOp.Val->addUser(U);
1596 }
1597
1598 // Now that we have modified U, add it back to the CSE maps. If it already
1599 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001600 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1601 ReplaceAllUsesWith(U, Existing, Deleted);
1602 // U is now dead.
1603 if (Deleted) Deleted->push_back(U);
1604 DeleteNodeNotInCSEMaps(U);
1605 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001606 }
1607}
1608
1609
Jim Laskey58b968b2005-08-17 20:08:02 +00001610//===----------------------------------------------------------------------===//
1611// SDNode Class
1612//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001613
Chris Lattnera3255112005-11-08 23:30:28 +00001614
1615/// getValueTypeList - Return a pointer to the specified value type.
1616///
1617MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1618 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1619 VTs[VT] = VT;
1620 return &VTs[VT];
1621}
1622
Chris Lattner5c884562005-01-12 18:37:47 +00001623/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1624/// indicated value. This method ignores uses of other values defined by this
1625/// operation.
1626bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1627 assert(Value < getNumValues() && "Bad value!");
1628
1629 // If there is only one value, this is easy.
1630 if (getNumValues() == 1)
1631 return use_size() == NUses;
1632 if (Uses.size() < NUses) return false;
1633
1634 SDOperand TheValue(this, Value);
1635
1636 std::set<SDNode*> UsersHandled;
1637
1638 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1639 UI != E; ++UI) {
1640 SDNode *User = *UI;
1641 if (User->getNumOperands() == 1 ||
1642 UsersHandled.insert(User).second) // First time we've seen this?
1643 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1644 if (User->getOperand(i) == TheValue) {
1645 if (NUses == 0)
1646 return false; // too many uses
1647 --NUses;
1648 }
1649 }
1650
1651 // Found exactly the right number of uses?
1652 return NUses == 0;
1653}
1654
1655
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001656const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001657 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001658 default:
1659 if (getOpcode() < ISD::BUILTIN_OP_END)
1660 return "<<Unknown DAG Node>>";
1661 else {
1662 if (G)
1663 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001664 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1665 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001666 return "<<Unknown Target Node>>";
1667 }
1668
Andrew Lenharth95762122005-03-31 21:24:06 +00001669 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001670 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001671 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001672 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001673 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001674 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00001675 case ISD::AssertSext: return "AssertSext";
1676 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001677 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001678 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001679 case ISD::ConstantFP: return "ConstantFP";
1680 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001681 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001682 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001683 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001684 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001685 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001686 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001687 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00001688 case ISD::ConstantPool: return "ConstantPool";
1689 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001690 case ISD::CopyToReg: return "CopyToReg";
1691 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001692 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001693 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001694
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001695 // Unary operators
1696 case ISD::FABS: return "fabs";
1697 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001698 case ISD::FSQRT: return "fsqrt";
1699 case ISD::FSIN: return "fsin";
1700 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001701
1702 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001703 case ISD::ADD: return "add";
1704 case ISD::SUB: return "sub";
1705 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001706 case ISD::MULHU: return "mulhu";
1707 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001708 case ISD::SDIV: return "sdiv";
1709 case ISD::UDIV: return "udiv";
1710 case ISD::SREM: return "srem";
1711 case ISD::UREM: return "urem";
1712 case ISD::AND: return "and";
1713 case ISD::OR: return "or";
1714 case ISD::XOR: return "xor";
1715 case ISD::SHL: return "shl";
1716 case ISD::SRA: return "sra";
1717 case ISD::SRL: return "srl";
Chris Lattner01b3d732005-09-28 22:28:18 +00001718 case ISD::FADD: return "fadd";
1719 case ISD::FSUB: return "fsub";
1720 case ISD::FMUL: return "fmul";
1721 case ISD::FDIV: return "fdiv";
1722 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001723 case ISD::VADD: return "vadd";
1724 case ISD::VSUB: return "vsub";
1725 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00001726
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001727 case ISD::SETCC: return "setcc";
1728 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001729 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001730 case ISD::ADD_PARTS: return "add_parts";
1731 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001732 case ISD::SHL_PARTS: return "shl_parts";
1733 case ISD::SRA_PARTS: return "sra_parts";
1734 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001735
Chris Lattner7f644642005-04-28 21:44:03 +00001736 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001737 case ISD::SIGN_EXTEND: return "sign_extend";
1738 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00001739 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001740 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001741 case ISD::TRUNCATE: return "truncate";
1742 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001743 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001744 case ISD::FP_EXTEND: return "fp_extend";
1745
1746 case ISD::SINT_TO_FP: return "sint_to_fp";
1747 case ISD::UINT_TO_FP: return "uint_to_fp";
1748 case ISD::FP_TO_SINT: return "fp_to_sint";
1749 case ISD::FP_TO_UINT: return "fp_to_uint";
1750
1751 // Control flow instructions
1752 case ISD::BR: return "br";
1753 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001754 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001755 case ISD::BR_CC: return "br_cc";
1756 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001757 case ISD::RET: return "ret";
1758 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001759 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001760 case ISD::CALLSEQ_START: return "callseq_start";
1761 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001762
1763 // Other operators
1764 case ISD::LOAD: return "load";
1765 case ISD::STORE: return "store";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001766 case ISD::VLOAD: return "vload";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001767 case ISD::EXTLOAD: return "extload";
1768 case ISD::SEXTLOAD: return "sextload";
1769 case ISD::ZEXTLOAD: return "zextload";
1770 case ISD::TRUNCSTORE: return "truncstore";
1771
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001772 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1773 case ISD::EXTRACT_ELEMENT: return "extract_element";
1774 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001775 case ISD::MEMSET: return "memset";
1776 case ISD::MEMCPY: return "memcpy";
1777 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001778
Chris Lattner276260b2005-05-11 04:50:30 +00001779 // Bit counting
1780 case ISD::CTPOP: return "ctpop";
1781 case ISD::CTTZ: return "cttz";
1782 case ISD::CTLZ: return "ctlz";
1783
1784 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001785 case ISD::READPORT: return "readport";
1786 case ISD::WRITEPORT: return "writeport";
1787 case ISD::READIO: return "readio";
1788 case ISD::WRITEIO: return "writeio";
1789
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001790 case ISD::CONDCODE:
1791 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001792 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001793 case ISD::SETOEQ: return "setoeq";
1794 case ISD::SETOGT: return "setogt";
1795 case ISD::SETOGE: return "setoge";
1796 case ISD::SETOLT: return "setolt";
1797 case ISD::SETOLE: return "setole";
1798 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001799
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001800 case ISD::SETO: return "seto";
1801 case ISD::SETUO: return "setuo";
1802 case ISD::SETUEQ: return "setue";
1803 case ISD::SETUGT: return "setugt";
1804 case ISD::SETUGE: return "setuge";
1805 case ISD::SETULT: return "setult";
1806 case ISD::SETULE: return "setule";
1807 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001808
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001809 case ISD::SETEQ: return "seteq";
1810 case ISD::SETGT: return "setgt";
1811 case ISD::SETGE: return "setge";
1812 case ISD::SETLT: return "setlt";
1813 case ISD::SETLE: return "setle";
1814 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001815 }
1816 }
1817}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001818
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001819void SDNode::dump() const { dump(0); }
1820void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001821 std::cerr << (void*)this << ": ";
1822
1823 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1824 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001825 if (getValueType(i) == MVT::Other)
1826 std::cerr << "ch";
1827 else
1828 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001829 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001830 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001831
1832 std::cerr << " ";
1833 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1834 if (i) std::cerr << ", ";
1835 std::cerr << (void*)getOperand(i).Val;
1836 if (unsigned RN = getOperand(i).ResNo)
1837 std::cerr << ":" << RN;
1838 }
1839
1840 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1841 std::cerr << "<" << CSDN->getValue() << ">";
1842 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1843 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001844 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00001845 dyn_cast<GlobalAddressSDNode>(this)) {
1846 std::cerr << "<";
1847 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001848 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001849 std::cerr << "<" << FIDN->getIndex() << ">";
1850 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00001851 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001852 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001853 std::cerr << "<";
1854 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1855 if (LBB)
1856 std::cerr << LBB->getName() << " ";
1857 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00001858 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00001859 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
1860 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
1861 } else {
1862 std::cerr << " #" << R->getReg();
1863 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001864 } else if (const ExternalSymbolSDNode *ES =
1865 dyn_cast<ExternalSymbolSDNode>(this)) {
1866 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001867 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1868 if (M->getValue())
1869 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1870 else
1871 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00001872 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
1873 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001874 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001875}
1876
Chris Lattnerde202b32005-11-09 23:47:37 +00001877static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001878 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1879 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001880 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001881 else
1882 std::cerr << "\n" << std::string(indent+2, ' ')
1883 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001884
Chris Lattnerea946cd2005-01-09 20:38:33 +00001885
1886 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001887 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001888}
1889
Chris Lattnerc3aae252005-01-07 07:46:32 +00001890void SelectionDAG::dump() const {
1891 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00001892 std::vector<const SDNode*> Nodes;
1893 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
1894 I != E; ++I)
1895 Nodes.push_back(I);
1896
Chris Lattner49d24712005-01-09 20:26:36 +00001897 std::sort(Nodes.begin(), Nodes.end());
1898
1899 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001900 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001901 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001902 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001903
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001904 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001905
Chris Lattnerc3aae252005-01-07 07:46:32 +00001906 std::cerr << "\n\n";
1907}
1908