blob: 9c9df11bed2b4061db26acc8b9c6a2b03f6f6a52 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Evan Cheng115c0362005-12-19 23:11:49 +000024#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000025#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000026#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000027#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000028#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner5cdcc582005-01-09 20:52:51 +000031static bool isCommutativeBinOp(unsigned Opcode) {
32 switch (Opcode) {
33 case ISD::ADD:
34 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000035 case ISD::MULHU:
36 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000037 case ISD::FADD:
38 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000039 case ISD::AND:
40 case ISD::OR:
41 case ISD::XOR: return true;
42 default: return false; // FIXME: Need commutative info for user ops!
43 }
44}
45
Chris Lattner5cdcc582005-01-09 20:52:51 +000046// isInvertibleForFree - Return true if there is no cost to emitting the logical
47// inverse of this node.
48static bool isInvertibleForFree(SDOperand N) {
49 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000050 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000051 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000052 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000053}
54
Jim Laskey58b968b2005-08-17 20:08:02 +000055//===----------------------------------------------------------------------===//
56// ConstantFPSDNode Class
57//===----------------------------------------------------------------------===//
58
59/// isExactlyValue - We don't rely on operator== working on double values, as
60/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
61/// As such, this method can be used to do an exact bit-for-bit comparison of
62/// two floating point values.
63bool ConstantFPSDNode::isExactlyValue(double V) const {
64 return DoubleToBits(V) == DoubleToBits(Value);
65}
66
67//===----------------------------------------------------------------------===//
68// ISD Class
69//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000070
Chris Lattnerc3aae252005-01-07 07:46:32 +000071/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
72/// when given the operation for (X op Y).
73ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
74 // To perform this operation, we just need to swap the L and G bits of the
75 // operation.
76 unsigned OldL = (Operation >> 2) & 1;
77 unsigned OldG = (Operation >> 1) & 1;
78 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
79 (OldL << 1) | // New G bit
80 (OldG << 2)); // New L bit.
81}
82
83/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
84/// 'op' is a valid SetCC operation.
85ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
86 unsigned Operation = Op;
87 if (isInteger)
88 Operation ^= 7; // Flip L, G, E bits, but not U.
89 else
90 Operation ^= 15; // Flip all of the condition bits.
91 if (Operation > ISD::SETTRUE2)
92 Operation &= ~8; // Don't let N and U bits get set.
93 return ISD::CondCode(Operation);
94}
95
96
97/// isSignedOp - For an integer comparison, return 1 if the comparison is a
98/// signed operation and 2 if the result is an unsigned comparison. Return zero
99/// if the operation does not depend on the sign of the input (setne and seteq).
100static int isSignedOp(ISD::CondCode Opcode) {
101 switch (Opcode) {
102 default: assert(0 && "Illegal integer setcc operation!");
103 case ISD::SETEQ:
104 case ISD::SETNE: return 0;
105 case ISD::SETLT:
106 case ISD::SETLE:
107 case ISD::SETGT:
108 case ISD::SETGE: return 1;
109 case ISD::SETULT:
110 case ISD::SETULE:
111 case ISD::SETUGT:
112 case ISD::SETUGE: return 2;
113 }
114}
115
116/// getSetCCOrOperation - Return the result of a logical OR between different
117/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
118/// returns SETCC_INVALID if it is not possible to represent the resultant
119/// comparison.
120ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
121 bool isInteger) {
122 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
123 // Cannot fold a signed integer setcc with an unsigned integer setcc.
124 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000125
Chris Lattnerc3aae252005-01-07 07:46:32 +0000126 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000127
Chris Lattnerc3aae252005-01-07 07:46:32 +0000128 // If the N and U bits get set then the resultant comparison DOES suddenly
129 // care about orderedness, and is true when ordered.
130 if (Op > ISD::SETTRUE2)
131 Op &= ~16; // Clear the N bit.
132 return ISD::CondCode(Op);
133}
134
135/// getSetCCAndOperation - Return the result of a logical AND between different
136/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
137/// function returns zero if it is not possible to represent the resultant
138/// comparison.
139ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
140 bool isInteger) {
141 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
142 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000143 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000144
145 // Combine all of the condition bits.
146 return ISD::CondCode(Op1 & Op2);
147}
148
Chris Lattnerb48da392005-01-23 04:39:44 +0000149const TargetMachine &SelectionDAG::getTarget() const {
150 return TLI.getTargetMachine();
151}
152
Jim Laskey58b968b2005-08-17 20:08:02 +0000153//===----------------------------------------------------------------------===//
154// SelectionDAG Class
155//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000156
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000157/// RemoveDeadNodes - This method deletes all unreachable nodes in the
158/// SelectionDAG, including nodes (like loads) that have uses of their token
159/// chain but no other uses and no side effect. If a node is passed in as an
160/// argument, it is used as the seed for node deletion.
161void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000162 // Create a dummy node (which is not added to allnodes), that adds a reference
163 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000164 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000165
Chris Lattnerf469cb62005-11-08 18:52:27 +0000166 bool MadeChange = false;
167
Chris Lattner8b8749f2005-08-17 19:00:20 +0000168 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000169 if (N && N->use_empty()) {
170 DestroyDeadNode(N);
171 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000172 }
173
Chris Lattnerde202b32005-11-09 23:47:37 +0000174 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
175 if (I->use_empty() && I->getOpcode() != 65535) {
176 // Node is dead, recursively delete newly dead uses.
177 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000178 MadeChange = true;
179 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000180
181 // Walk the nodes list, removing the nodes we've marked as dead.
182 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000183 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
184 SDNode *N = I++;
185 if (N->use_empty())
186 AllNodes.erase(N);
187 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000188 }
189
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000190 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000191 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000192}
193
Chris Lattnerf469cb62005-11-08 18:52:27 +0000194/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
195/// graph. If it is the last user of any of its operands, recursively process
196/// them the same way.
197///
198void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000199 // Okay, we really are going to delete this node. First take this out of the
200 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000201 RemoveNodeFromCSEMaps(N);
202
203 // Next, brutally remove the operand list. This is safe to do, as there are
204 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000205 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
206 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000207 O->removeUser(N);
208
209 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000210 if (O->use_empty())
211 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000212 }
Chris Lattner65113b22005-11-08 22:07:03 +0000213 delete[] N->OperandList;
214 N->OperandList = 0;
215 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000216
217 // Mark the node as dead.
218 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000219}
220
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000221void SelectionDAG::DeleteNode(SDNode *N) {
222 assert(N->use_empty() && "Cannot delete a node that is not dead!");
223
224 // First take this out of the appropriate CSE map.
225 RemoveNodeFromCSEMaps(N);
226
Chris Lattner1e111c72005-09-07 05:37:01 +0000227 // Finally, remove uses due to operands of this node, remove from the
228 // AllNodes list, and delete the node.
229 DeleteNodeNotInCSEMaps(N);
230}
231
232void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
233
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000234 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000235 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000236
237 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000238 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
239 I->Val->removeUser(N);
240 delete[] N->OperandList;
241 N->OperandList = 0;
242 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000243
244 delete N;
245}
246
Chris Lattner149c58c2005-08-16 18:17:10 +0000247/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
248/// correspond to it. This is useful when we're about to delete or repurpose
249/// the node. We don't want future request for structurally identical nodes
250/// to return N anymore.
251void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000252 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000253 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000254 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000255 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000256 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
257 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000258 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000259 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000260 Erased = TargetConstants.erase(std::make_pair(
261 cast<ConstantSDNode>(N)->getValue(),
262 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000263 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000264 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000265 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000266 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000267 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000268 }
Chris Lattner3181a772006-01-29 06:26:56 +0000269 case ISD::TargetConstantFP: {
270 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
271 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
272 break;
273 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000274 case ISD::STRING:
275 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
276 break;
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;
Evan Cheng14229bb2005-11-30 02:49:21 +0000283 case ISD::GlobalAddress: {
284 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
285 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
286 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000287 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000288 }
289 case ISD::TargetGlobalAddress: {
290 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
291 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
292 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000293 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000294 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000295 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000296 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000297 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000298 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000299 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000300 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000302 Erased = ConstantPoolIndices.
303 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
304 cast<ConstantPoolSDNode>(N)->getAlignment()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000305 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000306 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000307 Erased = TargetConstantPoolIndices.
308 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
309 cast<ConstantPoolSDNode>(N)->getAlignment()));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000310 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000311 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000312 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000313 break;
314 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000315 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000316 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000317 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000318 Erased =
319 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000320 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000321 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000322 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000323 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
324 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000325 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000326 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
327 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000328 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000329 case ISD::SRCVALUE: {
330 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000331 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000332 break;
333 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000334 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000335 Erased = Loads.erase(std::make_pair(N->getOperand(1),
336 std::make_pair(N->getOperand(0),
337 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000338 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000339 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000340 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000341 if (N->getNumOperands() == 0) {
342 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
343 N->getValueType(0)));
344 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000345 Erased =
346 UnaryOps.erase(std::make_pair(N->getOpcode(),
347 std::make_pair(N->getOperand(0),
348 N->getValueType(0))));
349 } else if (N->getNumOperands() == 2) {
350 Erased =
351 BinaryOps.erase(std::make_pair(N->getOpcode(),
352 std::make_pair(N->getOperand(0),
353 N->getOperand(1))));
354 } else {
355 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
356 Erased =
357 OneResultNodes.erase(std::make_pair(N->getOpcode(),
358 std::make_pair(N->getValueType(0),
359 Ops)));
360 }
Chris Lattner385328c2005-05-14 07:42:29 +0000361 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000362 // Remove the node from the ArbitraryNodes map.
363 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
364 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000365 Erased =
366 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
367 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000368 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000369 break;
370 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000371#ifndef NDEBUG
372 // Verify that the node was actually in one of the CSE maps, unless it has a
373 // flag result (which cannot be CSE'd) or is one of the special cases that are
374 // not subject to CSE.
375 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000376 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000377 N->dump();
378 assert(0 && "Node is not in map!");
379 }
380#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000381}
382
Chris Lattner8b8749f2005-08-17 19:00:20 +0000383/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
384/// has been taken out and modified in some way. If the specified node already
385/// exists in the CSE maps, do not modify the maps, but return the existing node
386/// instead. If it doesn't exist, add it and return null.
387///
388SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
389 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000390 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000391 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000392
Chris Lattner9f8cc692005-12-19 22:21:21 +0000393 // Check that remaining values produced are not flags.
394 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
395 if (N->getValueType(i) == MVT::Flag)
396 return 0; // Never CSE anything that produces a flag.
397
Chris Lattnerfe14b342005-12-01 23:14:50 +0000398 if (N->getNumValues() == 1) {
399 if (N->getNumOperands() == 1) {
400 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
401 std::make_pair(N->getOperand(0),
402 N->getValueType(0)))];
403 if (U) return U;
404 U = N;
405 } else if (N->getNumOperands() == 2) {
406 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
407 std::make_pair(N->getOperand(0),
408 N->getOperand(1)))];
409 if (B) return B;
410 B = N;
411 } else {
412 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
413 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000414 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000415 if (ORN) return ORN;
416 ORN = N;
417 }
418 } else {
419 if (N->getOpcode() == ISD::LOAD) {
420 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
421 std::make_pair(N->getOperand(0),
422 N->getValueType(0)))];
423 if (L) return L;
424 L = N;
425 } else {
426 // Remove the node from the ArbitraryNodes map.
427 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
428 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
429 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
430 std::make_pair(RV, Ops))];
431 if (AN) return AN;
432 AN = N;
433 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000434 }
435 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000436}
437
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000438/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
439/// were replaced with those specified. If this node is never memoized,
440/// return null, otherwise return a pointer to the slot it would take. If a
441/// node already exists with these operands, the slot will be non-null.
442SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000443 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000444 return 0; // Never add these nodes.
445
446 // Check that remaining values produced are not flags.
447 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
448 if (N->getValueType(i) == MVT::Flag)
449 return 0; // Never CSE anything that produces a flag.
450
451 if (N->getNumValues() == 1) {
452 return &UnaryOps[std::make_pair(N->getOpcode(),
453 std::make_pair(Op, N->getValueType(0)))];
454 } else {
455 // Remove the node from the ArbitraryNodes map.
456 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
457 std::vector<SDOperand> Ops;
458 Ops.push_back(Op);
459 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
460 std::make_pair(RV, Ops))];
461 }
462 return 0;
463}
464
465/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
466/// were replaced with those specified. If this node is never memoized,
467/// return null, otherwise return a pointer to the slot it would take. If a
468/// node already exists with these operands, the slot will be non-null.
469SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
470 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000471 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000472 return 0; // Never add these nodes.
473
474 // Check that remaining values produced are not flags.
475 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
476 if (N->getValueType(i) == MVT::Flag)
477 return 0; // Never CSE anything that produces a flag.
478
479 if (N->getNumValues() == 1) {
480 return &BinaryOps[std::make_pair(N->getOpcode(),
481 std::make_pair(Op1, Op2))];
482 } else {
483 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
484 std::vector<SDOperand> Ops;
485 Ops.push_back(Op1);
486 Ops.push_back(Op2);
487 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
488 std::make_pair(RV, Ops))];
489 }
490 return 0;
491}
492
493
494/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
495/// were replaced with those specified. If this node is never memoized,
496/// return null, otherwise return a pointer to the slot it would take. If a
497/// node already exists with these operands, the slot will be non-null.
498SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
499 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000500 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000501 return 0; // Never add these nodes.
502
503 // Check that remaining values produced are not flags.
504 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
505 if (N->getValueType(i) == MVT::Flag)
506 return 0; // Never CSE anything that produces a flag.
507
508 if (N->getNumValues() == 1) {
509 if (N->getNumOperands() == 1) {
510 return &UnaryOps[std::make_pair(N->getOpcode(),
511 std::make_pair(Ops[0],
512 N->getValueType(0)))];
513 } else if (N->getNumOperands() == 2) {
514 return &BinaryOps[std::make_pair(N->getOpcode(),
515 std::make_pair(Ops[0], Ops[1]))];
516 } else {
517 return &OneResultNodes[std::make_pair(N->getOpcode(),
518 std::make_pair(N->getValueType(0),
519 Ops))];
520 }
521 } else {
522 if (N->getOpcode() == ISD::LOAD) {
523 return &Loads[std::make_pair(Ops[1],
524 std::make_pair(Ops[0], N->getValueType(0)))];
525 } else {
526 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
527 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
528 std::make_pair(RV, Ops))];
529 }
530 }
531 return 0;
532}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000533
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000534
Chris Lattner78ec3112003-08-11 14:57:33 +0000535SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000536 while (!AllNodes.empty()) {
537 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000538 delete [] N->OperandList;
539 N->OperandList = 0;
540 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000541 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000542 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000543}
544
Chris Lattner0f2287b2005-04-13 02:38:18 +0000545SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000546 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000547 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000548 return getNode(ISD::AND, Op.getValueType(), Op,
549 getConstant(Imm, Op.getValueType()));
550}
551
Chris Lattnerc3aae252005-01-07 07:46:32 +0000552SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
553 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
554 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000555 if (VT != MVT::i64)
556 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000557
Chris Lattnerc3aae252005-01-07 07:46:32 +0000558 SDNode *&N = Constants[std::make_pair(Val, VT)];
559 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000560 N = new ConstantSDNode(false, Val, VT);
561 AllNodes.push_back(N);
562 return SDOperand(N, 0);
563}
564
Chris Lattner36ce6912005-11-29 06:21:05 +0000565SDOperand SelectionDAG::getString(const std::string &Val) {
566 StringSDNode *&N = StringNodes[Val];
567 if (!N) {
568 N = new StringSDNode(Val);
569 AllNodes.push_back(N);
570 }
571 return SDOperand(N, 0);
572}
573
Chris Lattner37bfbb42005-08-17 00:34:06 +0000574SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
575 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
576 // Mask out any bits that are not valid for this constant.
577 if (VT != MVT::i64)
578 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
579
580 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
581 if (N) return SDOperand(N, 0);
582 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000583 AllNodes.push_back(N);
584 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000585}
586
Chris Lattnerc3aae252005-01-07 07:46:32 +0000587SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
588 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
589 if (VT == MVT::f32)
590 Val = (float)Val; // Mask out extra precision.
591
Chris Lattnerd8658612005-02-17 20:17:32 +0000592 // Do the map lookup using the actual bit pattern for the floating point
593 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
594 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000595 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000596 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000597 N = new ConstantFPSDNode(false, Val, VT);
598 AllNodes.push_back(N);
599 return SDOperand(N, 0);
600}
601
602SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
603 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
604 if (VT == MVT::f32)
605 Val = (float)Val; // Mask out extra precision.
606
607 // Do the map lookup using the actual bit pattern for the floating point
608 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
609 // we don't have issues with SNANs.
610 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
611 if (N) return SDOperand(N, 0);
612 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000613 AllNodes.push_back(N);
614 return SDOperand(N, 0);
615}
616
Chris Lattnerc3aae252005-01-07 07:46:32 +0000617SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000618 MVT::ValueType VT, int offset) {
619 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000620 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000621 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000622 AllNodes.push_back(N);
623 return SDOperand(N, 0);
624}
625
626SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000627 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000628 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000629 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000630 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000631 AllNodes.push_back(N);
632 return SDOperand(N, 0);
633}
634
635SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
636 SDNode *&N = FrameIndices[FI];
637 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000638 N = new FrameIndexSDNode(FI, VT, false);
639 AllNodes.push_back(N);
640 return SDOperand(N, 0);
641}
642
643SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
644 SDNode *&N = TargetFrameIndices[FI];
645 if (N) return SDOperand(N, 0);
646 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000647 AllNodes.push_back(N);
648 return SDOperand(N, 0);
649}
650
Evan Chengb8973bd2006-01-31 22:23:14 +0000651SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
652 unsigned Alignment) {
653 SDNode *&N = ConstantPoolIndices[std::make_pair(C, Alignment)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000654 if (N) return SDOperand(N, 0);
Evan Chengb8973bd2006-01-31 22:23:14 +0000655 N = new ConstantPoolSDNode(C, VT, Alignment, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000656 AllNodes.push_back(N);
657 return SDOperand(N, 0);
658}
659
Evan Chengb8973bd2006-01-31 22:23:14 +0000660SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
661 unsigned Alignment) {
662 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C, Alignment)];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000663 if (N) return SDOperand(N, 0);
Evan Chengb8973bd2006-01-31 22:23:14 +0000664 N = new ConstantPoolSDNode(C, VT, Alignment, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000665 AllNodes.push_back(N);
666 return SDOperand(N, 0);
667}
668
669SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
670 SDNode *&N = BBNodes[MBB];
671 if (N) return SDOperand(N, 0);
672 N = new BasicBlockSDNode(MBB);
673 AllNodes.push_back(N);
674 return SDOperand(N, 0);
675}
676
Chris Lattner15e4b012005-07-10 00:07:11 +0000677SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
678 if ((unsigned)VT >= ValueTypeNodes.size())
679 ValueTypeNodes.resize(VT+1);
680 if (ValueTypeNodes[VT] == 0) {
681 ValueTypeNodes[VT] = new VTSDNode(VT);
682 AllNodes.push_back(ValueTypeNodes[VT]);
683 }
684
685 return SDOperand(ValueTypeNodes[VT], 0);
686}
687
Chris Lattnerc3aae252005-01-07 07:46:32 +0000688SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
689 SDNode *&N = ExternalSymbols[Sym];
690 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000691 N = new ExternalSymbolSDNode(false, Sym, VT);
692 AllNodes.push_back(N);
693 return SDOperand(N, 0);
694}
695
Chris Lattner809ec112006-01-28 10:09:25 +0000696SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
697 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000698 SDNode *&N = TargetExternalSymbols[Sym];
699 if (N) return SDOperand(N, 0);
700 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000701 AllNodes.push_back(N);
702 return SDOperand(N, 0);
703}
704
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000705SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
706 if ((unsigned)Cond >= CondCodeNodes.size())
707 CondCodeNodes.resize(Cond+1);
708
Chris Lattner079a27a2005-08-09 20:40:02 +0000709 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000710 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000711 AllNodes.push_back(CondCodeNodes[Cond]);
712 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000713 return SDOperand(CondCodeNodes[Cond], 0);
714}
715
Chris Lattner0fdd7682005-08-30 22:38:38 +0000716SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
717 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
718 if (!Reg) {
719 Reg = new RegisterSDNode(RegNo, VT);
720 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000721 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000722 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000723}
724
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000725SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
726 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727 // These setcc operations always fold.
728 switch (Cond) {
729 default: break;
730 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000731 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000732 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000733 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000734 }
735
Chris Lattner67255a12005-04-07 18:14:58 +0000736 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
737 uint64_t C2 = N2C->getValue();
738 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
739 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000740
Chris Lattnerc3aae252005-01-07 07:46:32 +0000741 // Sign extend the operands if required
742 if (ISD::isSignedIntSetCC(Cond)) {
743 C1 = N1C->getSignExtended();
744 C2 = N2C->getSignExtended();
745 }
746
747 switch (Cond) {
748 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000749 case ISD::SETEQ: return getConstant(C1 == C2, VT);
750 case ISD::SETNE: return getConstant(C1 != C2, VT);
751 case ISD::SETULT: return getConstant(C1 < C2, VT);
752 case ISD::SETUGT: return getConstant(C1 > C2, VT);
753 case ISD::SETULE: return getConstant(C1 <= C2, VT);
754 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
755 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
756 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
757 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
758 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000759 }
Chris Lattner24673922005-04-07 18:58:54 +0000760 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000761 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000762 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
763 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
764
765 // If the comparison constant has bits in the upper part, the
766 // zero-extended value could never match.
767 if (C2 & (~0ULL << InSize)) {
768 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
769 switch (Cond) {
770 case ISD::SETUGT:
771 case ISD::SETUGE:
772 case ISD::SETEQ: return getConstant(0, VT);
773 case ISD::SETULT:
774 case ISD::SETULE:
775 case ISD::SETNE: return getConstant(1, VT);
776 case ISD::SETGT:
777 case ISD::SETGE:
778 // True if the sign bit of C2 is set.
779 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
780 case ISD::SETLT:
781 case ISD::SETLE:
782 // True if the sign bit of C2 isn't set.
783 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
784 default:
785 break;
786 }
787 }
788
789 // Otherwise, we can perform the comparison with the low bits.
790 switch (Cond) {
791 case ISD::SETEQ:
792 case ISD::SETNE:
793 case ISD::SETUGT:
794 case ISD::SETUGE:
795 case ISD::SETULT:
796 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000797 return getSetCC(VT, N1.getOperand(0),
798 getConstant(C2, N1.getOperand(0).getValueType()),
799 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000800 default:
801 break; // todo, be more careful with signed comparisons
802 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000803 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
804 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
805 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
806 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
807 MVT::ValueType ExtDstTy = N1.getValueType();
808 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000809
Chris Lattner7b2880c2005-08-24 22:44:39 +0000810 // If the extended part has any inconsistent bits, it cannot ever
811 // compare equal. In other words, they have to be all ones or all
812 // zeros.
813 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000814 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000815 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
816 return getConstant(Cond == ISD::SETNE, VT);
817
818 // Otherwise, make this a use of a zext.
819 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000820 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000821 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000822 }
823
Chris Lattner67255a12005-04-07 18:14:58 +0000824 uint64_t MinVal, MaxVal;
825 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
826 if (ISD::isSignedIntSetCC(Cond)) {
827 MinVal = 1ULL << (OperandBitSize-1);
828 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
829 MaxVal = ~0ULL >> (65-OperandBitSize);
830 else
831 MaxVal = 0;
832 } else {
833 MinVal = 0;
834 MaxVal = ~0ULL >> (64-OperandBitSize);
835 }
836
837 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
838 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
839 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
840 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000841 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
842 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000843 }
844
845 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
846 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
847 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000848 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
849 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000850 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000851
Nate Begeman72ea2812005-04-14 08:56:52 +0000852 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
853 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000854
Nate Begeman72ea2812005-04-14 08:56:52 +0000855 // Canonicalize setgt X, Min --> setne X, Min
856 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000857 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000858
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000859 // If we have setult X, 1, turn it into seteq X, 0
860 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000861 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
862 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000863 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000864 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000865 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
866 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000867
868 // If we have "setcc X, C1", check to see if we can shrink the immediate
869 // by changing cc.
870
871 // SETUGT X, SINTMAX -> SETLT X, 0
872 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
873 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000874 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000875
876 // FIXME: Implement the rest of these.
877
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000878
879 // Fold bit comparisons when we can.
880 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
881 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
882 if (ConstantSDNode *AndRHS =
883 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
884 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
885 // Perform the xform if the AND RHS is a single bit.
886 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
887 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000888 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000889 TLI.getShiftAmountTy()));
890 }
891 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
892 // (X & 8) == 8 --> (X & 8) >> 3
893 // Perform the xform if C2 is a single bit.
894 if ((C2 & (C2-1)) == 0) {
895 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000896 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000897 }
898 }
899 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000900 }
Chris Lattner67255a12005-04-07 18:14:58 +0000901 } else if (isa<ConstantSDNode>(N1.Val)) {
902 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000903 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000904 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000905
906 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
907 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
908 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000909
Chris Lattnerc3aae252005-01-07 07:46:32 +0000910 switch (Cond) {
911 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000912 case ISD::SETEQ: return getConstant(C1 == C2, VT);
913 case ISD::SETNE: return getConstant(C1 != C2, VT);
914 case ISD::SETLT: return getConstant(C1 < C2, VT);
915 case ISD::SETGT: return getConstant(C1 > C2, VT);
916 case ISD::SETLE: return getConstant(C1 <= C2, VT);
917 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000918 }
919 } else {
920 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000921 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000922 }
923
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000924 // Could not fold it.
925 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000926}
927
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000929///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000930SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000931 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
932 if (!N) {
933 N = new SDNode(Opcode, VT);
934 AllNodes.push_back(N);
935 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936 return SDOperand(N, 0);
937}
938
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
940 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000941 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000942 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000943 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
944 uint64_t Val = C->getValue();
945 switch (Opcode) {
946 default: break;
947 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000948 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000949 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
950 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000951 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
952 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000953 case ISD::BIT_CONVERT:
954 if (VT == MVT::f32) {
955 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
956 return getConstantFP(BitsToFloat(Val), VT);
957 } else if (VT == MVT::f64) {
958 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
959 return getConstantFP(BitsToDouble(Val), VT);
960 }
961 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000962 case ISD::BSWAP:
963 switch(VT) {
964 default: assert(0 && "Invalid bswap!"); break;
965 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
966 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
967 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
968 }
969 break;
970 case ISD::CTPOP:
971 switch(VT) {
972 default: assert(0 && "Invalid ctpop!"); break;
973 case MVT::i1: return getConstant(Val != 0, VT);
974 case MVT::i8:
975 Tmp1 = (unsigned)Val & 0xFF;
976 return getConstant(CountPopulation_32(Tmp1), VT);
977 case MVT::i16:
978 Tmp1 = (unsigned)Val & 0xFFFF;
979 return getConstant(CountPopulation_32(Tmp1), VT);
980 case MVT::i32:
981 return getConstant(CountPopulation_32((unsigned)Val), VT);
982 case MVT::i64:
983 return getConstant(CountPopulation_64(Val), VT);
984 }
985 case ISD::CTLZ:
986 switch(VT) {
987 default: assert(0 && "Invalid ctlz!"); break;
988 case MVT::i1: return getConstant(Val == 0, VT);
989 case MVT::i8:
990 Tmp1 = (unsigned)Val & 0xFF;
991 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
992 case MVT::i16:
993 Tmp1 = (unsigned)Val & 0xFFFF;
994 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
995 case MVT::i32:
996 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
997 case MVT::i64:
998 return getConstant(CountLeadingZeros_64(Val), VT);
999 }
1000 case ISD::CTTZ:
1001 switch(VT) {
1002 default: assert(0 && "Invalid cttz!"); break;
1003 case MVT::i1: return getConstant(Val == 0, VT);
1004 case MVT::i8:
1005 Tmp1 = (unsigned)Val | 0x100;
1006 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1007 case MVT::i16:
1008 Tmp1 = (unsigned)Val | 0x10000;
1009 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1010 case MVT::i32:
1011 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1012 case MVT::i64:
1013 return getConstant(CountTrailingZeros_64(Val), VT);
1014 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001015 }
1016 }
1017
Chris Lattner94683772005-12-23 05:30:37 +00001018 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001019 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1020 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001021 case ISD::FNEG:
1022 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001023 case ISD::FABS:
1024 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001025 case ISD::FP_ROUND:
1026 case ISD::FP_EXTEND:
1027 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001028 case ISD::FP_TO_SINT:
1029 return getConstant((int64_t)C->getValue(), VT);
1030 case ISD::FP_TO_UINT:
1031 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001032 case ISD::BIT_CONVERT:
1033 if (VT == MVT::i32) {
1034 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1035 return getConstant(FloatToBits(C->getValue()), VT);
1036 } else if (VT == MVT::i64) {
1037 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1038 return getConstant(DoubleToBits(C->getValue()), VT);
1039 }
1040 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001041 }
1042
1043 unsigned OpOpcode = Operand.Val->getOpcode();
1044 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001045 case ISD::TokenFactor:
1046 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001047 case ISD::SIGN_EXTEND:
1048 if (Operand.getValueType() == VT) return Operand; // noop extension
1049 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1050 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1051 break;
1052 case ISD::ZERO_EXTEND:
1053 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001054 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001055 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001057 case ISD::ANY_EXTEND:
1058 if (Operand.getValueType() == VT) return Operand; // noop extension
1059 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1060 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1061 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1062 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001063 case ISD::TRUNCATE:
1064 if (Operand.getValueType() == VT) return Operand; // noop truncate
1065 if (OpOpcode == ISD::TRUNCATE)
1066 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001067 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1068 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001069 // If the source is smaller than the dest, we still need an extend.
1070 if (Operand.Val->getOperand(0).getValueType() < VT)
1071 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1072 else if (Operand.Val->getOperand(0).getValueType() > VT)
1073 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1074 else
1075 return Operand.Val->getOperand(0);
1076 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077 break;
Chris Lattner35481892005-12-23 00:16:34 +00001078 case ISD::BIT_CONVERT:
1079 // Basic sanity checking.
1080 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1081 "Cannot BIT_CONVERT between two different types!");
1082 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001083 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1084 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001085 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001086 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001087 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1088 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001089 Operand.Val->getOperand(0));
1090 if (OpOpcode == ISD::FNEG) // --X -> X
1091 return Operand.Val->getOperand(0);
1092 break;
1093 case ISD::FABS:
1094 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1095 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1096 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001097 }
1098
Chris Lattner43247a12005-08-25 19:12:10 +00001099 SDNode *N;
1100 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1101 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001102 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001103 E = N = new SDNode(Opcode, Operand);
1104 } else {
1105 N = new SDNode(Opcode, Operand);
1106 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001107 N->setValueTypes(VT);
1108 AllNodes.push_back(N);
1109 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001110}
1111
Chris Lattner36019aa2005-04-18 03:48:41 +00001112
1113
Chris Lattnerc3aae252005-01-07 07:46:32 +00001114SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1115 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001116#ifndef NDEBUG
1117 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001118 case ISD::TokenFactor:
1119 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1120 N2.getValueType() == MVT::Other && "Invalid token factor!");
1121 break;
Chris Lattner76365122005-01-16 02:23:22 +00001122 case ISD::AND:
1123 case ISD::OR:
1124 case ISD::XOR:
1125 case ISD::UDIV:
1126 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001127 case ISD::MULHU:
1128 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001129 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1130 // fall through
1131 case ISD::ADD:
1132 case ISD::SUB:
1133 case ISD::MUL:
1134 case ISD::SDIV:
1135 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001136 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1137 // fall through.
1138 case ISD::FADD:
1139 case ISD::FSUB:
1140 case ISD::FMUL:
1141 case ISD::FDIV:
1142 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001143 assert(N1.getValueType() == N2.getValueType() &&
1144 N1.getValueType() == VT && "Binary operator types must match!");
1145 break;
1146
1147 case ISD::SHL:
1148 case ISD::SRA:
1149 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001150 case ISD::ROTL:
1151 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001152 assert(VT == N1.getValueType() &&
1153 "Shift operators return type must be the same as their first arg");
1154 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001155 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001156 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001157 case ISD::FP_ROUND_INREG: {
1158 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1159 assert(VT == N1.getValueType() && "Not an inreg round!");
1160 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1161 "Cannot FP_ROUND_INREG integer types");
1162 assert(EVT <= VT && "Not rounding down!");
1163 break;
1164 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001165 case ISD::AssertSext:
1166 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001167 case ISD::SIGN_EXTEND_INREG: {
1168 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1169 assert(VT == N1.getValueType() && "Not an inreg extend!");
1170 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1171 "Cannot *_EXTEND_INREG FP types");
1172 assert(EVT <= VT && "Not extending!");
1173 }
1174
Chris Lattner76365122005-01-16 02:23:22 +00001175 default: break;
1176 }
1177#endif
1178
Chris Lattnerc3aae252005-01-07 07:46:32 +00001179 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1180 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1181 if (N1C) {
1182 if (N2C) {
1183 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1184 switch (Opcode) {
1185 case ISD::ADD: return getConstant(C1 + C2, VT);
1186 case ISD::SUB: return getConstant(C1 - C2, VT);
1187 case ISD::MUL: return getConstant(C1 * C2, VT);
1188 case ISD::UDIV:
1189 if (C2) return getConstant(C1 / C2, VT);
1190 break;
1191 case ISD::UREM :
1192 if (C2) return getConstant(C1 % C2, VT);
1193 break;
1194 case ISD::SDIV :
1195 if (C2) return getConstant(N1C->getSignExtended() /
1196 N2C->getSignExtended(), VT);
1197 break;
1198 case ISD::SREM :
1199 if (C2) return getConstant(N1C->getSignExtended() %
1200 N2C->getSignExtended(), VT);
1201 break;
1202 case ISD::AND : return getConstant(C1 & C2, VT);
1203 case ISD::OR : return getConstant(C1 | C2, VT);
1204 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001205 case ISD::SHL : return getConstant(C1 << C2, VT);
1206 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001207 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001208 case ISD::ROTL :
1209 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1210 VT);
1211 case ISD::ROTR :
1212 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1213 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214 default: break;
1215 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001216 } else { // Cannonicalize constant to RHS if commutative
1217 if (isCommutativeBinOp(Opcode)) {
1218 std::swap(N1C, N2C);
1219 std::swap(N1, N2);
1220 }
1221 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001222 }
1223
1224 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1225 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001226 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001227 if (N2CFP) {
1228 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1229 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001230 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1231 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1232 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1233 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001234 if (C2) return getConstantFP(C1 / C2, VT);
1235 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001236 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001237 if (C2) return getConstantFP(fmod(C1, C2), VT);
1238 break;
1239 default: break;
1240 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001241 } else { // Cannonicalize constant to RHS if commutative
1242 if (isCommutativeBinOp(Opcode)) {
1243 std::swap(N1CFP, N2CFP);
1244 std::swap(N1, N2);
1245 }
1246 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001247 }
1248
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 // Finally, fold operations that do not require constants.
1250 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001251 case ISD::FP_ROUND_INREG:
1252 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1253 break;
1254 case ISD::SIGN_EXTEND_INREG: {
1255 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1256 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001257 break;
1258 }
1259
Nate Begemaneea805e2005-04-13 21:23:31 +00001260 // FIXME: figure out how to safely handle things like
1261 // int foo(int x) { return 1 << (x & 255); }
1262 // int bar() { return foo(256); }
1263#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001264 case ISD::SHL:
1265 case ISD::SRL:
1266 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001267 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001268 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001269 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001270 else if (N2.getOpcode() == ISD::AND)
1271 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1272 // If the and is only masking out bits that cannot effect the shift,
1273 // eliminate the and.
1274 unsigned NumBits = MVT::getSizeInBits(VT);
1275 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1276 return getNode(Opcode, VT, N1, N2.getOperand(0));
1277 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001278 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001279#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001280 }
1281
Chris Lattner27e9b412005-05-11 18:57:39 +00001282 // Memoize this node if possible.
1283 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001284 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001285 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1286 if (BON) return SDOperand(BON, 0);
1287
1288 BON = N = new SDNode(Opcode, N1, N2);
1289 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001290 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001291 }
1292
Chris Lattner3e011362005-05-14 07:45:46 +00001293 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001294 AllNodes.push_back(N);
1295 return SDOperand(N, 0);
1296}
1297
Chris Lattnerc3aae252005-01-07 07:46:32 +00001298SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1299 SDOperand N1, SDOperand N2, SDOperand N3) {
1300 // Perform various simplifications.
1301 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1302 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1303 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1304 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001305 case ISD::SETCC: {
1306 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001307 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001308 if (Simp.Val) return Simp;
1309 break;
1310 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001311 case ISD::SELECT:
1312 if (N1C)
1313 if (N1C->getValue())
1314 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001315 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001316 return N3; // select false, X, Y -> Y
1317
1318 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001319 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001320 case ISD::BRCOND:
1321 if (N2C)
1322 if (N2C->getValue()) // Unconditional branch
1323 return getNode(ISD::BR, MVT::Other, N1, N3);
1324 else
1325 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001326 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001327 }
1328
Chris Lattner385328c2005-05-14 07:42:29 +00001329 std::vector<SDOperand> Ops;
1330 Ops.reserve(3);
1331 Ops.push_back(N1);
1332 Ops.push_back(N2);
1333 Ops.push_back(N3);
1334
Chris Lattner43247a12005-08-25 19:12:10 +00001335 // Memoize node if it doesn't produce a flag.
1336 SDNode *N;
1337 if (VT != MVT::Flag) {
1338 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1339 if (E) return SDOperand(E, 0);
1340 E = N = new SDNode(Opcode, N1, N2, N3);
1341 } else {
1342 N = new SDNode(Opcode, N1, N2, N3);
1343 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001344 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001345 AllNodes.push_back(N);
1346 return SDOperand(N, 0);
1347}
1348
1349SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001350 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001351 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001352 std::vector<SDOperand> Ops;
1353 Ops.reserve(4);
1354 Ops.push_back(N1);
1355 Ops.push_back(N2);
1356 Ops.push_back(N3);
1357 Ops.push_back(N4);
1358 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001359}
1360
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001361SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1362 SDOperand N1, SDOperand N2, SDOperand N3,
1363 SDOperand N4, SDOperand N5) {
1364 std::vector<SDOperand> Ops;
1365 Ops.reserve(5);
1366 Ops.push_back(N1);
1367 Ops.push_back(N2);
1368 Ops.push_back(N3);
1369 Ops.push_back(N4);
1370 Ops.push_back(N5);
1371 return getNode(Opcode, VT, Ops);
1372}
1373
Evan Cheng7038daf2005-12-10 00:37:58 +00001374SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1375 SDOperand Chain, SDOperand Ptr,
1376 SDOperand SV) {
1377 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1378 if (N) return SDOperand(N, 0);
1379 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1380
1381 // Loads have a token chain.
1382 setNodeValueTypes(N, VT, MVT::Other);
1383 AllNodes.push_back(N);
1384 return SDOperand(N, 0);
1385}
1386
1387SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1388 SDOperand Chain, SDOperand Ptr,
1389 SDOperand SV) {
1390 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1391 if (N) return SDOperand(N, 0);
1392 std::vector<SDOperand> Ops;
1393 Ops.reserve(5);
1394 Ops.push_back(Chain);
1395 Ops.push_back(Ptr);
1396 Ops.push_back(getConstant(Count, MVT::i32));
1397 Ops.push_back(getValueType(EVT));
1398 Ops.push_back(SV);
1399 std::vector<MVT::ValueType> VTs;
1400 VTs.reserve(2);
1401 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1402 return getNode(ISD::VLOAD, VTs, Ops);
1403}
1404
1405SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1406 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1407 MVT::ValueType EVT) {
1408 std::vector<SDOperand> Ops;
1409 Ops.reserve(4);
1410 Ops.push_back(Chain);
1411 Ops.push_back(Ptr);
1412 Ops.push_back(SV);
1413 Ops.push_back(getValueType(EVT));
1414 std::vector<MVT::ValueType> VTs;
1415 VTs.reserve(2);
1416 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1417 return getNode(Opcode, VTs, Ops);
1418}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001419
Chris Lattner0437cdd2005-05-09 04:14:13 +00001420SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001421 assert((!V || isa<PointerType>(V->getType())) &&
1422 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001423 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1424 if (N) return SDOperand(N, 0);
1425
1426 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001427 AllNodes.push_back(N);
1428 return SDOperand(N, 0);
1429}
1430
Nate Begemanacc398c2006-01-25 18:21:52 +00001431SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1432 SDOperand Chain, SDOperand Ptr,
1433 SDOperand SV) {
1434 std::vector<SDOperand> Ops;
1435 Ops.reserve(3);
1436 Ops.push_back(Chain);
1437 Ops.push_back(Ptr);
1438 Ops.push_back(SV);
1439 std::vector<MVT::ValueType> VTs;
1440 VTs.reserve(2);
1441 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1442 return getNode(ISD::VAARG, VTs, Ops);
1443}
1444
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001445SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001446 std::vector<SDOperand> &Ops) {
1447 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001448 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001449 case 1: return getNode(Opcode, VT, Ops[0]);
1450 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1451 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001452 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001453 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001454
Chris Lattner89c34632005-05-14 06:20:26 +00001455 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001456 switch (Opcode) {
1457 default: break;
1458 case ISD::BRCONDTWOWAY:
1459 if (N1C)
1460 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001461 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001462 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001463 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001464 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001465 case ISD::BRTWOWAY_CC:
1466 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1467 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1468 "LHS and RHS of comparison must have same type!");
1469 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001470 case ISD::TRUNCSTORE: {
1471 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1472 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1473#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1474 // If this is a truncating store of a constant, convert to the desired type
1475 // and store it instead.
1476 if (isa<Constant>(Ops[0])) {
1477 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1478 if (isa<Constant>(Op))
1479 N1 = Op;
1480 }
1481 // Also for ConstantFP?
1482#endif
1483 if (Ops[0].getValueType() == EVT) // Normal store?
1484 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1485 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1486 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1487 "Can't do FP-INT conversion!");
1488 break;
1489 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001490 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001491 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001492 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1493 "LHS and RHS of condition must have same type!");
1494 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1495 "True and False arms of SelectCC must have same type!");
1496 assert(Ops[2].getValueType() == VT &&
1497 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001498 break;
1499 }
1500 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001501 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001502 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1503 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001504 break;
1505 }
Chris Lattneref847df2005-04-09 03:27:28 +00001506 }
1507
Chris Lattner385328c2005-05-14 07:42:29 +00001508 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001509 SDNode *N;
1510 if (VT != MVT::Flag) {
1511 SDNode *&E =
1512 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1513 if (E) return SDOperand(E, 0);
1514 E = N = new SDNode(Opcode, Ops);
1515 } else {
1516 N = new SDNode(Opcode, Ops);
1517 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001518 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001519 AllNodes.push_back(N);
1520 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001521}
1522
Chris Lattner89c34632005-05-14 06:20:26 +00001523SDOperand SelectionDAG::getNode(unsigned Opcode,
1524 std::vector<MVT::ValueType> &ResultTys,
1525 std::vector<SDOperand> &Ops) {
1526 if (ResultTys.size() == 1)
1527 return getNode(Opcode, ResultTys[0], Ops);
1528
Chris Lattner5f056bf2005-07-10 01:55:33 +00001529 switch (Opcode) {
1530 case ISD::EXTLOAD:
1531 case ISD::SEXTLOAD:
1532 case ISD::ZEXTLOAD: {
1533 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1534 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1535 // If they are asking for an extending load from/to the same thing, return a
1536 // normal load.
1537 if (ResultTys[0] == EVT)
1538 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1539 assert(EVT < ResultTys[0] &&
1540 "Should only be an extending load, not truncating!");
1541 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1542 "Cannot sign/zero extend a FP load!");
1543 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1544 "Cannot convert from FP to Int or Int -> FP!");
1545 break;
1546 }
1547
Chris Lattnere89083a2005-05-14 07:25:05 +00001548 // FIXME: figure out how to safely handle things like
1549 // int foo(int x) { return 1 << (x & 255); }
1550 // int bar() { return foo(256); }
1551#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001552 case ISD::SRA_PARTS:
1553 case ISD::SRL_PARTS:
1554 case ISD::SHL_PARTS:
1555 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001556 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001557 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1558 else if (N3.getOpcode() == ISD::AND)
1559 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1560 // If the and is only masking out bits that cannot effect the shift,
1561 // eliminate the and.
1562 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1563 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1564 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1565 }
1566 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001567#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001568 }
Chris Lattner89c34632005-05-14 06:20:26 +00001569
Chris Lattner43247a12005-08-25 19:12:10 +00001570 // Memoize the node unless it returns a flag.
1571 SDNode *N;
1572 if (ResultTys.back() != MVT::Flag) {
1573 SDNode *&E =
1574 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1575 if (E) return SDOperand(E, 0);
1576 E = N = new SDNode(Opcode, Ops);
1577 } else {
1578 N = new SDNode(Opcode, Ops);
1579 }
Chris Lattnera3255112005-11-08 23:30:28 +00001580 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001581 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001582 return SDOperand(N, 0);
1583}
1584
Chris Lattnera3255112005-11-08 23:30:28 +00001585void SelectionDAG::setNodeValueTypes(SDNode *N,
1586 std::vector<MVT::ValueType> &RetVals) {
1587 switch (RetVals.size()) {
1588 case 0: return;
1589 case 1: N->setValueTypes(RetVals[0]); return;
1590 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1591 default: break;
1592 }
1593
1594 std::list<std::vector<MVT::ValueType> >::iterator I =
1595 std::find(VTList.begin(), VTList.end(), RetVals);
1596 if (I == VTList.end()) {
1597 VTList.push_front(RetVals);
1598 I = VTList.begin();
1599 }
1600
1601 N->setValueTypes(&(*I)[0], I->size());
1602}
1603
1604void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1605 MVT::ValueType VT2) {
1606 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1607 E = VTList.end(); I != E; ++I) {
1608 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1609 N->setValueTypes(&(*I)[0], 2);
1610 return;
1611 }
1612 }
1613 std::vector<MVT::ValueType> V;
1614 V.push_back(VT1);
1615 V.push_back(VT2);
1616 VTList.push_front(V);
1617 N->setValueTypes(&(*VTList.begin())[0], 2);
1618}
1619
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001620/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1621/// specified operands. If the resultant node already exists in the DAG,
1622/// this does not modify the specified node, instead it returns the node that
1623/// already exists. If the resultant node does not exist in the DAG, the
1624/// input node is returned. As a degenerate case, if you specify the same
1625/// input operands as the node already has, the input node is returned.
1626SDOperand SelectionDAG::
1627UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1628 SDNode *N = InN.Val;
1629 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1630
1631 // Check to see if there is no change.
1632 if (Op == N->getOperand(0)) return InN;
1633
1634 // See if the modified node already exists.
1635 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1636 if (NewSlot && *NewSlot)
1637 return SDOperand(*NewSlot, InN.ResNo);
1638
1639 // Nope it doesn't. Remove the node from it's current place in the maps.
1640 if (NewSlot)
1641 RemoveNodeFromCSEMaps(N);
1642
1643 // Now we update the operands.
1644 N->OperandList[0].Val->removeUser(N);
1645 Op.Val->addUser(N);
1646 N->OperandList[0] = Op;
1647
1648 // If this gets put into a CSE map, add it.
1649 if (NewSlot) *NewSlot = N;
1650 return InN;
1651}
1652
1653SDOperand SelectionDAG::
1654UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1655 SDNode *N = InN.Val;
1656 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1657
1658 // Check to see if there is no change.
1659 bool AnyChange = false;
1660 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1661 return InN; // No operands changed, just return the input node.
1662
1663 // See if the modified node already exists.
1664 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1665 if (NewSlot && *NewSlot)
1666 return SDOperand(*NewSlot, InN.ResNo);
1667
1668 // Nope it doesn't. Remove the node from it's current place in the maps.
1669 if (NewSlot)
1670 RemoveNodeFromCSEMaps(N);
1671
1672 // Now we update the operands.
1673 if (N->OperandList[0] != Op1) {
1674 N->OperandList[0].Val->removeUser(N);
1675 Op1.Val->addUser(N);
1676 N->OperandList[0] = Op1;
1677 }
1678 if (N->OperandList[1] != Op2) {
1679 N->OperandList[1].Val->removeUser(N);
1680 Op2.Val->addUser(N);
1681 N->OperandList[1] = Op2;
1682 }
1683
1684 // If this gets put into a CSE map, add it.
1685 if (NewSlot) *NewSlot = N;
1686 return InN;
1687}
1688
1689SDOperand SelectionDAG::
1690UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1691 std::vector<SDOperand> Ops;
1692 Ops.push_back(Op1);
1693 Ops.push_back(Op2);
1694 Ops.push_back(Op3);
1695 return UpdateNodeOperands(N, Ops);
1696}
1697
1698SDOperand SelectionDAG::
1699UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1700 SDOperand Op3, SDOperand Op4) {
1701 std::vector<SDOperand> Ops;
1702 Ops.push_back(Op1);
1703 Ops.push_back(Op2);
1704 Ops.push_back(Op3);
1705 Ops.push_back(Op4);
1706 return UpdateNodeOperands(N, Ops);
1707}
1708
1709SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001710UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1711 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1712 std::vector<SDOperand> Ops;
1713 Ops.push_back(Op1);
1714 Ops.push_back(Op2);
1715 Ops.push_back(Op3);
1716 Ops.push_back(Op4);
1717 Ops.push_back(Op5);
1718 return UpdateNodeOperands(N, Ops);
1719}
1720
1721
1722SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001723UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1724 SDNode *N = InN.Val;
1725 assert(N->getNumOperands() == Ops.size() &&
1726 "Update with wrong number of operands");
1727
1728 // Check to see if there is no change.
1729 unsigned NumOps = Ops.size();
1730 bool AnyChange = false;
1731 for (unsigned i = 0; i != NumOps; ++i) {
1732 if (Ops[i] != N->getOperand(i)) {
1733 AnyChange = true;
1734 break;
1735 }
1736 }
1737
1738 // No operands changed, just return the input node.
1739 if (!AnyChange) return InN;
1740
1741 // See if the modified node already exists.
1742 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1743 if (NewSlot && *NewSlot)
1744 return SDOperand(*NewSlot, InN.ResNo);
1745
1746 // Nope it doesn't. Remove the node from it's current place in the maps.
1747 if (NewSlot)
1748 RemoveNodeFromCSEMaps(N);
1749
1750 // Now we update the operands.
1751 for (unsigned i = 0; i != NumOps; ++i) {
1752 if (N->OperandList[i] != Ops[i]) {
1753 N->OperandList[i].Val->removeUser(N);
1754 Ops[i].Val->addUser(N);
1755 N->OperandList[i] = Ops[i];
1756 }
1757 }
1758
1759 // If this gets put into a CSE map, add it.
1760 if (NewSlot) *NewSlot = N;
1761 return InN;
1762}
1763
1764
1765
Chris Lattner149c58c2005-08-16 18:17:10 +00001766
1767/// SelectNodeTo - These are used for target selectors to *mutate* the
1768/// specified node to have the specified return type, Target opcode, and
1769/// operands. Note that target opcodes are stored as
1770/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001771///
1772/// Note that SelectNodeTo returns the resultant node. If there is already a
1773/// node of the specified opcode and operands, it returns that node instead of
1774/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001775SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1776 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001777 // If an identical node already exists, use it.
1778 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1779 if (ON) return SDOperand(ON, 0);
1780
Chris Lattner7651fa42005-08-24 23:00:29 +00001781 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001782
Chris Lattner7651fa42005-08-24 23:00:29 +00001783 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1784 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001785
1786 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001787 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001788}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001789
Chris Lattnereb19e402005-11-30 22:45:14 +00001790SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1791 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001792 // If an identical node already exists, use it.
1793 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1794 std::make_pair(Op1, VT))];
1795 if (ON) return SDOperand(ON, 0);
1796
Chris Lattner149c58c2005-08-16 18:17:10 +00001797 RemoveNodeFromCSEMaps(N);
1798 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1799 N->setValueTypes(VT);
1800 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001801
1802 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001803 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001804}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001805
Chris Lattnereb19e402005-11-30 22:45:14 +00001806SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1807 MVT::ValueType VT, SDOperand Op1,
1808 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001809 // If an identical node already exists, use it.
1810 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1811 std::make_pair(Op1, Op2))];
1812 if (ON) return SDOperand(ON, 0);
1813
Chris Lattner149c58c2005-08-16 18:17:10 +00001814 RemoveNodeFromCSEMaps(N);
1815 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1816 N->setValueTypes(VT);
1817 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001818
1819 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001820 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001821}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001822
Chris Lattnereb19e402005-11-30 22:45:14 +00001823SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1824 MVT::ValueType VT, SDOperand Op1,
1825 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001826 // If an identical node already exists, use it.
1827 std::vector<SDOperand> OpList;
1828 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1829 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1830 std::make_pair(VT, OpList))];
1831 if (ON) return SDOperand(ON, 0);
1832
Chris Lattner149c58c2005-08-16 18:17:10 +00001833 RemoveNodeFromCSEMaps(N);
1834 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1835 N->setValueTypes(VT);
1836 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001837
1838 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001839 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001840}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001841
Chris Lattnereb19e402005-11-30 22:45:14 +00001842SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1843 MVT::ValueType VT, SDOperand Op1,
1844 SDOperand Op2, SDOperand Op3,
1845 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001846 // If an identical node already exists, use it.
1847 std::vector<SDOperand> OpList;
1848 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1849 OpList.push_back(Op4);
1850 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1851 std::make_pair(VT, OpList))];
1852 if (ON) return SDOperand(ON, 0);
1853
Nate Begeman294a0a12005-08-18 07:30:15 +00001854 RemoveNodeFromCSEMaps(N);
1855 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1856 N->setValueTypes(VT);
1857 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001858
1859 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001860 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001861}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001862
Chris Lattnereb19e402005-11-30 22:45:14 +00001863SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1864 MVT::ValueType VT, SDOperand Op1,
1865 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1866 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001867 // If an identical node already exists, use it.
1868 std::vector<SDOperand> OpList;
1869 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1870 OpList.push_back(Op4); OpList.push_back(Op5);
1871 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1872 std::make_pair(VT, OpList))];
1873 if (ON) return SDOperand(ON, 0);
1874
Chris Lattner6b09a292005-08-21 18:49:33 +00001875 RemoveNodeFromCSEMaps(N);
1876 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1877 N->setValueTypes(VT);
1878 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001879
1880 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001881 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001882}
Chris Lattner149c58c2005-08-16 18:17:10 +00001883
Chris Lattnereb19e402005-11-30 22:45:14 +00001884SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1885 MVT::ValueType VT, SDOperand Op1,
1886 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1887 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001888 // If an identical node already exists, use it.
1889 std::vector<SDOperand> OpList;
1890 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1891 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1892 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1893 std::make_pair(VT, OpList))];
1894 if (ON) return SDOperand(ON, 0);
1895
Evan Cheng61ca74b2005-11-30 02:04:11 +00001896 RemoveNodeFromCSEMaps(N);
1897 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1898 N->setValueTypes(VT);
1899 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001900
1901 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001902 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001903}
1904
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001905SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1906 MVT::ValueType VT, SDOperand Op1,
1907 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1908 SDOperand Op5, SDOperand Op6,
1909 SDOperand Op7) {
1910 // If an identical node already exists, use it.
1911 std::vector<SDOperand> OpList;
1912 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1913 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1914 OpList.push_back(Op7);
1915 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1916 std::make_pair(VT, OpList))];
1917 if (ON) return SDOperand(ON, 0);
1918
1919 RemoveNodeFromCSEMaps(N);
1920 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1921 N->setValueTypes(VT);
1922 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1923
1924 ON = N; // Memoize the new node.
1925 return SDOperand(N, 0);
1926}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001927SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1928 MVT::ValueType VT, SDOperand Op1,
1929 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1930 SDOperand Op5, SDOperand Op6,
1931 SDOperand Op7, SDOperand Op8) {
1932 // If an identical node already exists, use it.
1933 std::vector<SDOperand> OpList;
1934 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1935 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1936 OpList.push_back(Op7); OpList.push_back(Op8);
1937 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1938 std::make_pair(VT, OpList))];
1939 if (ON) return SDOperand(ON, 0);
1940
1941 RemoveNodeFromCSEMaps(N);
1942 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1943 N->setValueTypes(VT);
1944 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1945
1946 ON = N; // Memoize the new node.
1947 return SDOperand(N, 0);
1948}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001949
Chris Lattnereb19e402005-11-30 22:45:14 +00001950SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1951 MVT::ValueType VT1, MVT::ValueType VT2,
1952 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001953 // If an identical node already exists, use it.
1954 std::vector<SDOperand> OpList;
1955 OpList.push_back(Op1); OpList.push_back(Op2);
1956 std::vector<MVT::ValueType> VTList;
1957 VTList.push_back(VT1); VTList.push_back(VT2);
1958 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1959 std::make_pair(VTList, OpList))];
1960 if (ON) return SDOperand(ON, 0);
1961
Chris Lattner0fb094f2005-11-19 01:44:53 +00001962 RemoveNodeFromCSEMaps(N);
1963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1964 setNodeValueTypes(N, VT1, VT2);
1965 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001966
1967 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001968 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001969}
1970
Chris Lattnereb19e402005-11-30 22:45:14 +00001971SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1972 MVT::ValueType VT1, MVT::ValueType VT2,
1973 SDOperand Op1, SDOperand Op2,
1974 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001975 // If an identical node already exists, use it.
1976 std::vector<SDOperand> OpList;
1977 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1978 std::vector<MVT::ValueType> VTList;
1979 VTList.push_back(VT1); VTList.push_back(VT2);
1980 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1981 std::make_pair(VTList, OpList))];
1982 if (ON) return SDOperand(ON, 0);
1983
Chris Lattner0fb094f2005-11-19 01:44:53 +00001984 RemoveNodeFromCSEMaps(N);
1985 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1986 setNodeValueTypes(N, VT1, VT2);
1987 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001988
1989 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001990 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001991}
1992
Chris Lattnereb19e402005-11-30 22:45:14 +00001993SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1994 MVT::ValueType VT1, MVT::ValueType VT2,
1995 SDOperand Op1, SDOperand Op2,
1996 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001997 // If an identical node already exists, use it.
1998 std::vector<SDOperand> OpList;
1999 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2000 OpList.push_back(Op4);
2001 std::vector<MVT::ValueType> VTList;
2002 VTList.push_back(VT1); VTList.push_back(VT2);
2003 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2004 std::make_pair(VTList, OpList))];
2005 if (ON) return SDOperand(ON, 0);
2006
Chris Lattner0fb094f2005-11-19 01:44:53 +00002007 RemoveNodeFromCSEMaps(N);
2008 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2009 setNodeValueTypes(N, VT1, VT2);
2010 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002011
2012 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002013 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002014}
2015
Chris Lattnereb19e402005-11-30 22:45:14 +00002016SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2017 MVT::ValueType VT1, MVT::ValueType VT2,
2018 SDOperand Op1, SDOperand Op2,
2019 SDOperand Op3, SDOperand Op4,
2020 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002021 // If an identical node already exists, use it.
2022 std::vector<SDOperand> OpList;
2023 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2024 OpList.push_back(Op4); OpList.push_back(Op5);
2025 std::vector<MVT::ValueType> VTList;
2026 VTList.push_back(VT1); VTList.push_back(VT2);
2027 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2028 std::make_pair(VTList, OpList))];
2029 if (ON) return SDOperand(ON, 0);
2030
Chris Lattner0fb094f2005-11-19 01:44:53 +00002031 RemoveNodeFromCSEMaps(N);
2032 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2033 setNodeValueTypes(N, VT1, VT2);
2034 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002035
2036 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002037 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002038}
2039
Evan Cheng6ae46c42006-02-09 07:15:23 +00002040/// getTargetNode - These are used for target selectors to create a new node
2041/// with specified return type(s), target opcode, and operands.
2042///
2043/// Note that getTargetNode returns the resultant node. If there is already a
2044/// node of the specified opcode and operands, it returns that node instead of
2045/// the current one.
2046SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2047 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2048}
2049SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2050 SDOperand Op1) {
2051 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2052}
2053SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2054 SDOperand Op1, SDOperand Op2) {
2055 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2056}
2057SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2058 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2059 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2060}
2061SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2062 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2063 SDOperand Op4) {
2064 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2065}
2066SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2067 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2068 SDOperand Op4, SDOperand Op5) {
2069 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2070}
2071SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2072 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2073 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2074 std::vector<SDOperand> Ops;
2075 Ops.reserve(6);
2076 Ops.push_back(Op1);
2077 Ops.push_back(Op2);
2078 Ops.push_back(Op3);
2079 Ops.push_back(Op4);
2080 Ops.push_back(Op5);
2081 Ops.push_back(Op6);
2082 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2083}
2084SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2085 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2086 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2087 SDOperand Op7) {
2088 std::vector<SDOperand> Ops;
2089 Ops.reserve(7);
2090 Ops.push_back(Op1);
2091 Ops.push_back(Op2);
2092 Ops.push_back(Op3);
2093 Ops.push_back(Op4);
2094 Ops.push_back(Op5);
2095 Ops.push_back(Op6);
2096 Ops.push_back(Op7);
2097 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2098}
2099SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2100 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2101 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2102 SDOperand Op7, SDOperand Op8) {
2103 std::vector<SDOperand> Ops;
2104 Ops.reserve(8);
2105 Ops.push_back(Op1);
2106 Ops.push_back(Op2);
2107 Ops.push_back(Op3);
2108 Ops.push_back(Op4);
2109 Ops.push_back(Op5);
2110 Ops.push_back(Op6);
2111 Ops.push_back(Op7);
2112 Ops.push_back(Op8);
2113 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2114}
2115SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2116 std::vector<SDOperand> &Ops) {
2117 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2118}
2119SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2120 MVT::ValueType VT2, SDOperand Op1) {
2121 std::vector<MVT::ValueType> ResultTys;
2122 ResultTys.push_back(VT1);
2123 ResultTys.push_back(VT2);
2124 std::vector<SDOperand> Ops;
2125 Ops.push_back(Op1);
2126 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2127}
2128SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2129 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2130 std::vector<MVT::ValueType> ResultTys;
2131 ResultTys.push_back(VT1);
2132 ResultTys.push_back(VT2);
2133 std::vector<SDOperand> Ops;
2134 Ops.push_back(Op1);
2135 Ops.push_back(Op2);
2136 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2137}
2138SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2139 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2140 SDOperand Op3) {
2141 std::vector<MVT::ValueType> ResultTys;
2142 ResultTys.push_back(VT1);
2143 ResultTys.push_back(VT2);
2144 std::vector<SDOperand> Ops;
2145 Ops.push_back(Op1);
2146 Ops.push_back(Op2);
2147 Ops.push_back(Op3);
2148 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2149}
2150SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2151 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2152 SDOperand Op3, SDOperand Op4) {
2153 std::vector<MVT::ValueType> ResultTys;
2154 ResultTys.push_back(VT1);
2155 ResultTys.push_back(VT2);
2156 std::vector<SDOperand> Ops;
2157 Ops.push_back(Op1);
2158 Ops.push_back(Op2);
2159 Ops.push_back(Op3);
2160 Ops.push_back(Op4);
2161 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2162}
2163SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2164 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2165 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2166 std::vector<MVT::ValueType> ResultTys;
2167 ResultTys.push_back(VT1);
2168 ResultTys.push_back(VT2);
2169 std::vector<SDOperand> Ops;
2170 Ops.push_back(Op1);
2171 Ops.push_back(Op2);
2172 Ops.push_back(Op3);
2173 Ops.push_back(Op4);
2174 Ops.push_back(Op5);
2175 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2176}
2177SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2178 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2179 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2180 SDOperand Op6) {
2181 std::vector<MVT::ValueType> ResultTys;
2182 ResultTys.push_back(VT1);
2183 ResultTys.push_back(VT2);
2184 std::vector<SDOperand> Ops;
2185 Ops.push_back(Op1);
2186 Ops.push_back(Op2);
2187 Ops.push_back(Op3);
2188 Ops.push_back(Op4);
2189 Ops.push_back(Op5);
2190 Ops.push_back(Op6);
2191 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2192}
2193SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2194 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2195 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2196 SDOperand Op6, SDOperand Op7) {
2197 std::vector<MVT::ValueType> ResultTys;
2198 ResultTys.push_back(VT1);
2199 ResultTys.push_back(VT2);
2200 std::vector<SDOperand> Ops;
2201 Ops.push_back(Op1);
2202 Ops.push_back(Op2);
2203 Ops.push_back(Op3);
2204 Ops.push_back(Op4);
2205 Ops.push_back(Op5);
2206 Ops.push_back(Op6);
2207 Ops.push_back(Op7);
2208 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2209}
2210SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2211 MVT::ValueType VT2, MVT::ValueType VT3,
2212 SDOperand Op1, SDOperand Op2) {
2213 std::vector<MVT::ValueType> ResultTys;
2214 ResultTys.push_back(VT1);
2215 ResultTys.push_back(VT2);
2216 ResultTys.push_back(VT3);
2217 std::vector<SDOperand> Ops;
2218 Ops.push_back(Op1);
2219 Ops.push_back(Op2);
2220 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2221}
2222SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2223 MVT::ValueType VT2, MVT::ValueType VT3,
2224 SDOperand Op1, SDOperand Op2,
2225 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2226 std::vector<MVT::ValueType> ResultTys;
2227 ResultTys.push_back(VT1);
2228 ResultTys.push_back(VT2);
2229 ResultTys.push_back(VT3);
2230 std::vector<SDOperand> Ops;
2231 Ops.push_back(Op1);
2232 Ops.push_back(Op2);
2233 Ops.push_back(Op3);
2234 Ops.push_back(Op4);
2235 Ops.push_back(Op5);
2236 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2237}
2238SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2239 MVT::ValueType VT2, MVT::ValueType VT3,
2240 SDOperand Op1, SDOperand Op2,
2241 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2242 SDOperand Op6) {
2243 std::vector<MVT::ValueType> ResultTys;
2244 ResultTys.push_back(VT1);
2245 ResultTys.push_back(VT2);
2246 ResultTys.push_back(VT3);
2247 std::vector<SDOperand> Ops;
2248 Ops.push_back(Op1);
2249 Ops.push_back(Op2);
2250 Ops.push_back(Op3);
2251 Ops.push_back(Op4);
2252 Ops.push_back(Op5);
2253 Ops.push_back(Op6);
2254 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2255}
2256SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2257 MVT::ValueType VT2, MVT::ValueType VT3,
2258 SDOperand Op1, SDOperand Op2,
2259 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2260 SDOperand Op6, SDOperand Op7) {
2261 std::vector<MVT::ValueType> ResultTys;
2262 ResultTys.push_back(VT1);
2263 ResultTys.push_back(VT2);
2264 ResultTys.push_back(VT3);
2265 std::vector<SDOperand> Ops;
2266 Ops.push_back(Op1);
2267 Ops.push_back(Op2);
2268 Ops.push_back(Op3);
2269 Ops.push_back(Op4);
2270 Ops.push_back(Op5);
2271 Ops.push_back(Op6);
2272 Ops.push_back(Op7);
2273 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2274}
2275SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2276 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2277 std::vector<MVT::ValueType> ResultTys;
2278 ResultTys.push_back(VT1);
2279 ResultTys.push_back(VT2);
2280 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2281}
2282
Chris Lattner0fb094f2005-11-19 01:44:53 +00002283// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002284/// This can cause recursive merging of nodes in the DAG.
2285///
Chris Lattner8b52f212005-08-26 18:36:28 +00002286/// This version assumes From/To have a single result value.
2287///
Chris Lattner1e111c72005-09-07 05:37:01 +00002288void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2289 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002290 SDNode *From = FromN.Val, *To = ToN.Val;
2291 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2292 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002293 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002294
Chris Lattner8b8749f2005-08-17 19:00:20 +00002295 while (!From->use_empty()) {
2296 // Process users until they are all gone.
2297 SDNode *U = *From->use_begin();
2298
2299 // This node is about to morph, remove its old self from the CSE maps.
2300 RemoveNodeFromCSEMaps(U);
2301
Chris Lattner65113b22005-11-08 22:07:03 +00002302 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2303 I != E; ++I)
2304 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002305 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002306 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002307 To->addUser(U);
2308 }
2309
2310 // Now that we have modified U, add it back to the CSE maps. If it already
2311 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002312 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2313 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002314 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002315 if (Deleted) Deleted->push_back(U);
2316 DeleteNodeNotInCSEMaps(U);
2317 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002318 }
2319}
2320
2321/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2322/// This can cause recursive merging of nodes in the DAG.
2323///
2324/// This version assumes From/To have matching types and numbers of result
2325/// values.
2326///
Chris Lattner1e111c72005-09-07 05:37:01 +00002327void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2328 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002329 assert(From != To && "Cannot replace uses of with self");
2330 assert(From->getNumValues() == To->getNumValues() &&
2331 "Cannot use this version of ReplaceAllUsesWith!");
2332 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002333 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002334 return;
2335 }
2336
2337 while (!From->use_empty()) {
2338 // Process users until they are all gone.
2339 SDNode *U = *From->use_begin();
2340
2341 // This node is about to morph, remove its old self from the CSE maps.
2342 RemoveNodeFromCSEMaps(U);
2343
Chris Lattner65113b22005-11-08 22:07:03 +00002344 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2345 I != E; ++I)
2346 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002347 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002348 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002349 To->addUser(U);
2350 }
2351
2352 // Now that we have modified U, add it back to the CSE maps. If it already
2353 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002354 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2355 ReplaceAllUsesWith(U, Existing, Deleted);
2356 // U is now dead.
2357 if (Deleted) Deleted->push_back(U);
2358 DeleteNodeNotInCSEMaps(U);
2359 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002360 }
2361}
2362
Chris Lattner8b52f212005-08-26 18:36:28 +00002363/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2364/// This can cause recursive merging of nodes in the DAG.
2365///
2366/// This version can replace From with any result values. To must match the
2367/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002368void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002369 const std::vector<SDOperand> &To,
2370 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002371 assert(From->getNumValues() == To.size() &&
2372 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002373 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002374 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002375 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002376 return;
2377 }
2378
2379 while (!From->use_empty()) {
2380 // Process users until they are all gone.
2381 SDNode *U = *From->use_begin();
2382
2383 // This node is about to morph, remove its old self from the CSE maps.
2384 RemoveNodeFromCSEMaps(U);
2385
Chris Lattner65113b22005-11-08 22:07:03 +00002386 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2387 I != E; ++I)
2388 if (I->Val == From) {
2389 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002390 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002391 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002392 ToOp.Val->addUser(U);
2393 }
2394
2395 // Now that we have modified U, add it back to the CSE maps. If it already
2396 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002397 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2398 ReplaceAllUsesWith(U, Existing, Deleted);
2399 // U is now dead.
2400 if (Deleted) Deleted->push_back(U);
2401 DeleteNodeNotInCSEMaps(U);
2402 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002403 }
2404}
2405
2406
Jim Laskey58b968b2005-08-17 20:08:02 +00002407//===----------------------------------------------------------------------===//
2408// SDNode Class
2409//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002410
Chris Lattnera3255112005-11-08 23:30:28 +00002411
2412/// getValueTypeList - Return a pointer to the specified value type.
2413///
2414MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2415 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2416 VTs[VT] = VT;
2417 return &VTs[VT];
2418}
2419
Chris Lattner5c884562005-01-12 18:37:47 +00002420/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2421/// indicated value. This method ignores uses of other values defined by this
2422/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002423bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002424 assert(Value < getNumValues() && "Bad value!");
2425
2426 // If there is only one value, this is easy.
2427 if (getNumValues() == 1)
2428 return use_size() == NUses;
2429 if (Uses.size() < NUses) return false;
2430
Evan Cheng4ee62112006-02-05 06:29:23 +00002431 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002432
2433 std::set<SDNode*> UsersHandled;
2434
Evan Cheng4ee62112006-02-05 06:29:23 +00002435 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002436 UI != E; ++UI) {
2437 SDNode *User = *UI;
2438 if (User->getNumOperands() == 1 ||
2439 UsersHandled.insert(User).second) // First time we've seen this?
2440 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2441 if (User->getOperand(i) == TheValue) {
2442 if (NUses == 0)
2443 return false; // too many uses
2444 --NUses;
2445 }
2446 }
2447
2448 // Found exactly the right number of uses?
2449 return NUses == 0;
2450}
2451
2452
Evan Cheng4ee62112006-02-05 06:29:23 +00002453// isOnlyUse - Return true if this node is the only use of N.
2454bool SDNode::isOnlyUse(SDNode *N) const {
2455 bool Seen = false;
2456 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2457 SDNode *User = *I;
2458 if (User == this)
2459 Seen = true;
2460 else
2461 return false;
2462 }
2463
2464 return Seen;
2465}
2466
2467
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002468const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002469 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002470 default:
2471 if (getOpcode() < ISD::BUILTIN_OP_END)
2472 return "<<Unknown DAG Node>>";
2473 else {
Evan Cheng72261582005-12-20 06:22:03 +00002474 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002475 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002476 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2477 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002478
Evan Cheng72261582005-12-20 06:22:03 +00002479 TargetLowering &TLI = G->getTargetLoweringInfo();
2480 const char *Name =
2481 TLI.getTargetNodeName(getOpcode());
2482 if (Name) return Name;
2483 }
2484
2485 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002486 }
2487
Andrew Lenharth95762122005-03-31 21:24:06 +00002488 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002489 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002490 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002491 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00002492 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002493 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002494 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002495 case ISD::AssertSext: return "AssertSext";
2496 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002497 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002498 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002499 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00002500 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002501 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002502 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002503 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002504 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002505 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002506 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002507 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00002508 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002509 case ISD::ConstantPool: return "ConstantPool";
2510 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002511 case ISD::CopyToReg: return "CopyToReg";
2512 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002513 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002514 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002515 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002516 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002517
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002518 // Unary operators
2519 case ISD::FABS: return "fabs";
2520 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002521 case ISD::FSQRT: return "fsqrt";
2522 case ISD::FSIN: return "fsin";
2523 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002524
2525 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002526 case ISD::ADD: return "add";
2527 case ISD::SUB: return "sub";
2528 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002529 case ISD::MULHU: return "mulhu";
2530 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002531 case ISD::SDIV: return "sdiv";
2532 case ISD::UDIV: return "udiv";
2533 case ISD::SREM: return "srem";
2534 case ISD::UREM: return "urem";
2535 case ISD::AND: return "and";
2536 case ISD::OR: return "or";
2537 case ISD::XOR: return "xor";
2538 case ISD::SHL: return "shl";
2539 case ISD::SRA: return "sra";
2540 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002541 case ISD::ROTL: return "rotl";
2542 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002543 case ISD::FADD: return "fadd";
2544 case ISD::FSUB: return "fsub";
2545 case ISD::FMUL: return "fmul";
2546 case ISD::FDIV: return "fdiv";
2547 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002548 case ISD::VADD: return "vadd";
2549 case ISD::VSUB: return "vsub";
2550 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002551
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002552 case ISD::SETCC: return "setcc";
2553 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002554 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002555 case ISD::ADD_PARTS: return "add_parts";
2556 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002557 case ISD::SHL_PARTS: return "shl_parts";
2558 case ISD::SRA_PARTS: return "sra_parts";
2559 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002560
Chris Lattner7f644642005-04-28 21:44:03 +00002561 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002562 case ISD::SIGN_EXTEND: return "sign_extend";
2563 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002564 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002565 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002566 case ISD::TRUNCATE: return "truncate";
2567 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002568 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002569 case ISD::FP_EXTEND: return "fp_extend";
2570
2571 case ISD::SINT_TO_FP: return "sint_to_fp";
2572 case ISD::UINT_TO_FP: return "uint_to_fp";
2573 case ISD::FP_TO_SINT: return "fp_to_sint";
2574 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002575 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002576
2577 // Control flow instructions
2578 case ISD::BR: return "br";
2579 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002580 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002581 case ISD::BR_CC: return "br_cc";
2582 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002583 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002584 case ISD::CALLSEQ_START: return "callseq_start";
2585 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002586
2587 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002588 case ISD::LOAD: return "load";
2589 case ISD::STORE: return "store";
2590 case ISD::VLOAD: return "vload";
2591 case ISD::EXTLOAD: return "extload";
2592 case ISD::SEXTLOAD: return "sextload";
2593 case ISD::ZEXTLOAD: return "zextload";
2594 case ISD::TRUNCSTORE: return "truncstore";
2595 case ISD::VAARG: return "vaarg";
2596 case ISD::VACOPY: return "vacopy";
2597 case ISD::VAEND: return "vaend";
2598 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002599 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002600 case ISD::EXTRACT_ELEMENT: return "extract_element";
2601 case ISD::BUILD_PAIR: return "build_pair";
2602 case ISD::STACKSAVE: return "stacksave";
2603 case ISD::STACKRESTORE: return "stackrestore";
2604
2605 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002606 case ISD::MEMSET: return "memset";
2607 case ISD::MEMCPY: return "memcpy";
2608 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002609
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002610 // Bit manipulation
2611 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002612 case ISD::CTPOP: return "ctpop";
2613 case ISD::CTTZ: return "cttz";
2614 case ISD::CTLZ: return "ctlz";
2615
2616 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002617 case ISD::READPORT: return "readport";
2618 case ISD::WRITEPORT: return "writeport";
2619 case ISD::READIO: return "readio";
2620 case ISD::WRITEIO: return "writeio";
2621
Chris Lattner36ce6912005-11-29 06:21:05 +00002622 // Debug info
2623 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002624 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002625 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002626
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002627 case ISD::CONDCODE:
2628 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002629 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002630 case ISD::SETOEQ: return "setoeq";
2631 case ISD::SETOGT: return "setogt";
2632 case ISD::SETOGE: return "setoge";
2633 case ISD::SETOLT: return "setolt";
2634 case ISD::SETOLE: return "setole";
2635 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002636
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002637 case ISD::SETO: return "seto";
2638 case ISD::SETUO: return "setuo";
2639 case ISD::SETUEQ: return "setue";
2640 case ISD::SETUGT: return "setugt";
2641 case ISD::SETUGE: return "setuge";
2642 case ISD::SETULT: return "setult";
2643 case ISD::SETULE: return "setule";
2644 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002645
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002646 case ISD::SETEQ: return "seteq";
2647 case ISD::SETGT: return "setgt";
2648 case ISD::SETGE: return "setge";
2649 case ISD::SETLT: return "setlt";
2650 case ISD::SETLE: return "setle";
2651 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002652 }
2653 }
2654}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002655
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002656void SDNode::dump() const { dump(0); }
2657void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002658 std::cerr << (void*)this << ": ";
2659
2660 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2661 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002662 if (getValueType(i) == MVT::Other)
2663 std::cerr << "ch";
2664 else
2665 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002666 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002667 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002668
2669 std::cerr << " ";
2670 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2671 if (i) std::cerr << ", ";
2672 std::cerr << (void*)getOperand(i).Val;
2673 if (unsigned RN = getOperand(i).ResNo)
2674 std::cerr << ":" << RN;
2675 }
2676
2677 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2678 std::cerr << "<" << CSDN->getValue() << ">";
2679 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2680 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002681 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002682 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002683 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002684 std::cerr << "<";
2685 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002686 if (offset > 0)
2687 std::cerr << " + " << offset;
2688 else
2689 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002690 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002691 std::cerr << "<" << FIDN->getIndex() << ">";
2692 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002693 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002694 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002695 std::cerr << "<";
2696 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2697 if (LBB)
2698 std::cerr << LBB->getName() << " ";
2699 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002700 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002701 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002702 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2703 } else {
2704 std::cerr << " #" << R->getReg();
2705 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002706 } else if (const ExternalSymbolSDNode *ES =
2707 dyn_cast<ExternalSymbolSDNode>(this)) {
2708 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002709 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2710 if (M->getValue())
2711 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2712 else
2713 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002714 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2715 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002716 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002717}
2718
Chris Lattnerde202b32005-11-09 23:47:37 +00002719static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002720 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2721 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002722 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002723 else
2724 std::cerr << "\n" << std::string(indent+2, ' ')
2725 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002726
Chris Lattnerea946cd2005-01-09 20:38:33 +00002727
2728 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002729 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002730}
2731
Chris Lattnerc3aae252005-01-07 07:46:32 +00002732void SelectionDAG::dump() const {
2733 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002734 std::vector<const SDNode*> Nodes;
2735 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2736 I != E; ++I)
2737 Nodes.push_back(I);
2738
Chris Lattner49d24712005-01-09 20:26:36 +00002739 std::sort(Nodes.begin(), Nodes.end());
2740
2741 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002742 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002743 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002744 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002745
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002746 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002747
Chris Lattnerc3aae252005-01-07 07:46:32 +00002748 std::cerr << "\n\n";
2749}
2750