blob: 8db55c9bfd45b1500c1cec4cb6af55d098641c97 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000018#include "llvm/Assembly/Writer.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000020#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000021#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000022#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000023#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000025#include "llvm/ADT/SetVector.h"
Chris Lattner190a4182006-08-04 17:45:20 +000026#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000027#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000028#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000029#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000030#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000031#include <algorithm>
Evan Chengc384d6c2006-08-02 22:00:34 +000032#include <queue>
Chris Lattnere25738c2004-06-02 04:28:06 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner5cdcc582005-01-09 20:52:51 +000035static bool isCommutativeBinOp(unsigned Opcode) {
36 switch (Opcode) {
37 case ISD::ADD:
38 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000039 case ISD::MULHU:
40 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000041 case ISD::FADD:
42 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000043 case ISD::AND:
44 case ISD::OR:
45 case ISD::XOR: return true;
46 default: return false; // FIXME: Need commutative info for user ops!
47 }
48}
49
Chris Lattner5cdcc582005-01-09 20:52:51 +000050// isInvertibleForFree - Return true if there is no cost to emitting the logical
51// inverse of this node.
52static bool isInvertibleForFree(SDOperand N) {
53 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000054 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000055 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000056 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000057}
58
Jim Laskey58b968b2005-08-17 20:08:02 +000059//===----------------------------------------------------------------------===//
60// ConstantFPSDNode Class
61//===----------------------------------------------------------------------===//
62
63/// isExactlyValue - We don't rely on operator== working on double values, as
64/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
65/// As such, this method can be used to do an exact bit-for-bit comparison of
66/// two floating point values.
67bool ConstantFPSDNode::isExactlyValue(double V) const {
68 return DoubleToBits(V) == DoubleToBits(Value);
69}
70
71//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000072// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000073//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000074
Evan Chenga8df1662006-03-27 06:58:47 +000075/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000076/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000077bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000078 // Look through a bit convert.
79 if (N->getOpcode() == ISD::BIT_CONVERT)
80 N = N->getOperand(0).Val;
81
Evan Chenga8df1662006-03-27 06:58:47 +000082 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000083
84 unsigned i = 0, e = N->getNumOperands();
85
86 // Skip over all of the undef values.
87 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
88 ++i;
89
90 // Do not accept an all-undef vector.
91 if (i == e) return false;
92
93 // Do not accept build_vectors that aren't all constants or which have non-~0
94 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000095 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000096 if (isa<ConstantSDNode>(NotZero)) {
97 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
98 return false;
99 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000100 MVT::ValueType VT = NotZero.getValueType();
101 if (VT== MVT::f64) {
102 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
103 (uint64_t)-1)
104 return false;
105 } else {
106 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
107 (uint32_t)-1)
108 return false;
109 }
Evan Chenga8df1662006-03-27 06:58:47 +0000110 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000111 return false;
112
113 // Okay, we have at least one ~0 value, check to see if the rest match or are
114 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000115 for (++i; i != e; ++i)
116 if (N->getOperand(i) != NotZero &&
117 N->getOperand(i).getOpcode() != ISD::UNDEF)
118 return false;
119 return true;
120}
121
122
Evan Cheng4a147842006-03-26 09:50:58 +0000123/// isBuildVectorAllZeros - Return true if the specified node is a
124/// BUILD_VECTOR where all of the elements are 0 or undef.
125bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000126 // Look through a bit convert.
127 if (N->getOpcode() == ISD::BIT_CONVERT)
128 N = N->getOperand(0).Val;
129
Evan Cheng4a147842006-03-26 09:50:58 +0000130 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000131
132 unsigned i = 0, e = N->getNumOperands();
133
134 // Skip over all of the undef values.
135 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
136 ++i;
137
138 // Do not accept an all-undef vector.
139 if (i == e) return false;
140
141 // Do not accept build_vectors that aren't all constants or which have non-~0
142 // elements.
143 SDOperand Zero = N->getOperand(i);
144 if (isa<ConstantSDNode>(Zero)) {
145 if (!cast<ConstantSDNode>(Zero)->isNullValue())
146 return false;
147 } else if (isa<ConstantFPSDNode>(Zero)) {
148 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
149 return false;
150 } else
151 return false;
152
153 // Okay, we have at least one ~0 value, check to see if the rest match or are
154 // undefs.
155 for (++i; i != e; ++i)
156 if (N->getOperand(i) != Zero &&
157 N->getOperand(i).getOpcode() != ISD::UNDEF)
158 return false;
159 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000160}
161
Chris Lattnerc3aae252005-01-07 07:46:32 +0000162/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
163/// when given the operation for (X op Y).
164ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
165 // To perform this operation, we just need to swap the L and G bits of the
166 // operation.
167 unsigned OldL = (Operation >> 2) & 1;
168 unsigned OldG = (Operation >> 1) & 1;
169 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
170 (OldL << 1) | // New G bit
171 (OldG << 2)); // New L bit.
172}
173
174/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
175/// 'op' is a valid SetCC operation.
176ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
177 unsigned Operation = Op;
178 if (isInteger)
179 Operation ^= 7; // Flip L, G, E bits, but not U.
180 else
181 Operation ^= 15; // Flip all of the condition bits.
182 if (Operation > ISD::SETTRUE2)
183 Operation &= ~8; // Don't let N and U bits get set.
184 return ISD::CondCode(Operation);
185}
186
187
188/// isSignedOp - For an integer comparison, return 1 if the comparison is a
189/// signed operation and 2 if the result is an unsigned comparison. Return zero
190/// if the operation does not depend on the sign of the input (setne and seteq).
191static int isSignedOp(ISD::CondCode Opcode) {
192 switch (Opcode) {
193 default: assert(0 && "Illegal integer setcc operation!");
194 case ISD::SETEQ:
195 case ISD::SETNE: return 0;
196 case ISD::SETLT:
197 case ISD::SETLE:
198 case ISD::SETGT:
199 case ISD::SETGE: return 1;
200 case ISD::SETULT:
201 case ISD::SETULE:
202 case ISD::SETUGT:
203 case ISD::SETUGE: return 2;
204 }
205}
206
207/// getSetCCOrOperation - Return the result of a logical OR between different
208/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
209/// returns SETCC_INVALID if it is not possible to represent the resultant
210/// comparison.
211ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
212 bool isInteger) {
213 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
214 // Cannot fold a signed integer setcc with an unsigned integer setcc.
215 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000216
Chris Lattnerc3aae252005-01-07 07:46:32 +0000217 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000218
Chris Lattnerc3aae252005-01-07 07:46:32 +0000219 // If the N and U bits get set then the resultant comparison DOES suddenly
220 // care about orderedness, and is true when ordered.
221 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000222 Op &= ~16; // Clear the U bit if the N bit is set.
223
224 // Canonicalize illegal integer setcc's.
225 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
226 Op = ISD::SETNE;
227
Chris Lattnerc3aae252005-01-07 07:46:32 +0000228 return ISD::CondCode(Op);
229}
230
231/// getSetCCAndOperation - Return the result of a logical AND between different
232/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
233/// function returns zero if it is not possible to represent the resultant
234/// comparison.
235ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
236 bool isInteger) {
237 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
238 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000239 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000240
241 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000242 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
243
244 // Canonicalize illegal integer setcc's.
245 if (isInteger) {
246 switch (Result) {
247 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000248 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
249 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
250 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
251 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000252 }
253 }
254
255 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000256}
257
Chris Lattnerb48da392005-01-23 04:39:44 +0000258const TargetMachine &SelectionDAG::getTarget() const {
259 return TLI.getTargetMachine();
260}
261
Jim Laskey58b968b2005-08-17 20:08:02 +0000262//===----------------------------------------------------------------------===//
263// SelectionDAG Class
264//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000265
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000267/// SelectionDAG.
268void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000269 // Create a dummy node (which is not added to allnodes), that adds a reference
270 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000271 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000272
Chris Lattner190a4182006-08-04 17:45:20 +0000273 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000274
Chris Lattner190a4182006-08-04 17:45:20 +0000275 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000276 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000277 if (I->use_empty())
278 DeadNodes.push_back(I);
279
280 // Process the worklist, deleting the nodes and adding their uses to the
281 // worklist.
282 while (!DeadNodes.empty()) {
283 SDNode *N = DeadNodes.back();
284 DeadNodes.pop_back();
285
286 // Take the node out of the appropriate CSE map.
287 RemoveNodeFromCSEMaps(N);
288
289 // Next, brutally remove the operand list. This is safe to do, as there are
290 // no cycles in the graph.
291 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
292 SDNode *Operand = I->Val;
293 Operand->removeUser(N);
294
295 // Now that we removed this operand, see if there are no uses of it left.
296 if (Operand->use_empty())
297 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000298 }
Chris Lattner190a4182006-08-04 17:45:20 +0000299 delete[] N->OperandList;
300 N->OperandList = 0;
301 N->NumOperands = 0;
302
303 // Finally, remove N itself.
304 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000305 }
306
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000308 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000309}
310
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000311void SelectionDAG::DeleteNode(SDNode *N) {
312 assert(N->use_empty() && "Cannot delete a node that is not dead!");
313
314 // First take this out of the appropriate CSE map.
315 RemoveNodeFromCSEMaps(N);
316
Chris Lattner1e111c72005-09-07 05:37:01 +0000317 // Finally, remove uses due to operands of this node, remove from the
318 // AllNodes list, and delete the node.
319 DeleteNodeNotInCSEMaps(N);
320}
321
322void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
323
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000324 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000325 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000326
327 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000328 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
329 I->Val->removeUser(N);
330 delete[] N->OperandList;
331 N->OperandList = 0;
332 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000333
334 delete N;
335}
336
Chris Lattner149c58c2005-08-16 18:17:10 +0000337/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
338/// correspond to it. This is useful when we're about to delete or repurpose
339/// the node. We don't want future request for structurally identical nodes
340/// to return N anymore.
341void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000342 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000343 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000344 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000345 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
347 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000348 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000349 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000350 Erased = TargetConstants.erase(std::make_pair(
351 cast<ConstantSDNode>(N)->getValue(),
352 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000353 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000354 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000355 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000356 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000357 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000358 }
Chris Lattner3181a772006-01-29 06:26:56 +0000359 case ISD::TargetConstantFP: {
360 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
361 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
362 break;
363 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000364 case ISD::STRING:
365 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
366 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000367 case ISD::CONDCODE:
368 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
369 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000370 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000371 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
372 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000373 case ISD::GlobalAddress: {
374 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
375 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
376 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000377 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000378 }
379 case ISD::TargetGlobalAddress: {
380 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
381 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
382 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000383 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000384 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000385 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000386 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000387 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000388 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000389 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000390 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000391 case ISD::JumpTable:
392 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
393 break;
394 case ISD::TargetJumpTable:
395 Erased =
396 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
397 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000398 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000399 Erased = ConstantPoolIndices.
400 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000401 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
402 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000403 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000404 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000405 Erased = TargetConstantPoolIndices.
406 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000407 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
408 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000409 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000410 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000411 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000412 break;
413 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000414 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000415 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000416 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000417 Erased =
418 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000419 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000420 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000421 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000422 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
423 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000424 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000425 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
426 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000427 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000428 case ISD::SRCVALUE: {
429 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000430 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000431 break;
432 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000433 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000434 Erased = Loads.erase(std::make_pair(N->getOperand(1),
435 std::make_pair(N->getOperand(0),
436 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000437 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000438 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000439 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000440 if (N->getNumOperands() == 0) {
441 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
442 N->getValueType(0)));
443 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000444 Erased =
445 UnaryOps.erase(std::make_pair(N->getOpcode(),
446 std::make_pair(N->getOperand(0),
447 N->getValueType(0))));
448 } else if (N->getNumOperands() == 2) {
449 Erased =
450 BinaryOps.erase(std::make_pair(N->getOpcode(),
451 std::make_pair(N->getOperand(0),
452 N->getOperand(1))));
453 } else {
454 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
455 Erased =
456 OneResultNodes.erase(std::make_pair(N->getOpcode(),
457 std::make_pair(N->getValueType(0),
458 Ops)));
459 }
Chris Lattner385328c2005-05-14 07:42:29 +0000460 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000461 // Remove the node from the ArbitraryNodes map.
462 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
463 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000464 Erased =
465 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
466 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000467 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000468 break;
469 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000470#ifndef NDEBUG
471 // Verify that the node was actually in one of the CSE maps, unless it has a
472 // flag result (which cannot be CSE'd) or is one of the special cases that are
473 // not subject to CSE.
474 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000475 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000476 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000477 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000478 assert(0 && "Node is not in map!");
479 }
480#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000481}
482
Chris Lattner8b8749f2005-08-17 19:00:20 +0000483/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
484/// has been taken out and modified in some way. If the specified node already
485/// exists in the CSE maps, do not modify the maps, but return the existing node
486/// instead. If it doesn't exist, add it and return null.
487///
488SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
489 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000490 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000491 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000492
Chris Lattner9f8cc692005-12-19 22:21:21 +0000493 // Check that remaining values produced are not flags.
494 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
495 if (N->getValueType(i) == MVT::Flag)
496 return 0; // Never CSE anything that produces a flag.
497
Chris Lattnerfe14b342005-12-01 23:14:50 +0000498 if (N->getNumValues() == 1) {
499 if (N->getNumOperands() == 1) {
500 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
501 std::make_pair(N->getOperand(0),
502 N->getValueType(0)))];
503 if (U) return U;
504 U = N;
505 } else if (N->getNumOperands() == 2) {
506 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
507 std::make_pair(N->getOperand(0),
508 N->getOperand(1)))];
509 if (B) return B;
510 B = N;
511 } else {
512 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
513 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000514 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000515 if (ORN) return ORN;
516 ORN = N;
517 }
518 } else {
519 if (N->getOpcode() == ISD::LOAD) {
520 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
521 std::make_pair(N->getOperand(0),
522 N->getValueType(0)))];
523 if (L) return L;
524 L = N;
525 } else {
526 // Remove the node from the ArbitraryNodes map.
527 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
528 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
529 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
530 std::make_pair(RV, Ops))];
531 if (AN) return AN;
532 AN = N;
533 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000534 }
535 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000536}
537
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000538/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
539/// were replaced with those specified. If this node is never memoized,
540/// return null, otherwise return a pointer to the slot it would take. If a
541/// node already exists with these operands, the slot will be non-null.
542SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000543 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000544 return 0; // Never add these nodes.
545
546 // Check that remaining values produced are not flags.
547 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
548 if (N->getValueType(i) == MVT::Flag)
549 return 0; // Never CSE anything that produces a flag.
550
551 if (N->getNumValues() == 1) {
552 return &UnaryOps[std::make_pair(N->getOpcode(),
553 std::make_pair(Op, N->getValueType(0)))];
554 } else {
555 // Remove the node from the ArbitraryNodes map.
556 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
557 std::vector<SDOperand> Ops;
558 Ops.push_back(Op);
559 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
560 std::make_pair(RV, Ops))];
561 }
562 return 0;
563}
564
565/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
566/// were replaced with those specified. If this node is never memoized,
567/// return null, otherwise return a pointer to the slot it would take. If a
568/// node already exists with these operands, the slot will be non-null.
569SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
570 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000571 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000572 return 0; // Never add these nodes.
573
574 // Check that remaining values produced are not flags.
575 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
576 if (N->getValueType(i) == MVT::Flag)
577 return 0; // Never CSE anything that produces a flag.
578
579 if (N->getNumValues() == 1) {
580 return &BinaryOps[std::make_pair(N->getOpcode(),
581 std::make_pair(Op1, Op2))];
582 } else {
583 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
584 std::vector<SDOperand> Ops;
585 Ops.push_back(Op1);
586 Ops.push_back(Op2);
587 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
588 std::make_pair(RV, Ops))];
589 }
590 return 0;
591}
592
593
594/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
595/// were replaced with those specified. If this node is never memoized,
596/// return null, otherwise return a pointer to the slot it would take. If a
597/// node already exists with these operands, the slot will be non-null.
598SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
599 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000600 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000601 return 0; // Never add these nodes.
602
603 // Check that remaining values produced are not flags.
604 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
605 if (N->getValueType(i) == MVT::Flag)
606 return 0; // Never CSE anything that produces a flag.
607
608 if (N->getNumValues() == 1) {
609 if (N->getNumOperands() == 1) {
610 return &UnaryOps[std::make_pair(N->getOpcode(),
611 std::make_pair(Ops[0],
612 N->getValueType(0)))];
613 } else if (N->getNumOperands() == 2) {
614 return &BinaryOps[std::make_pair(N->getOpcode(),
615 std::make_pair(Ops[0], Ops[1]))];
616 } else {
617 return &OneResultNodes[std::make_pair(N->getOpcode(),
618 std::make_pair(N->getValueType(0),
619 Ops))];
620 }
621 } else {
622 if (N->getOpcode() == ISD::LOAD) {
623 return &Loads[std::make_pair(Ops[1],
624 std::make_pair(Ops[0], N->getValueType(0)))];
625 } else {
626 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
627 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
628 std::make_pair(RV, Ops))];
629 }
630 }
631 return 0;
632}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000633
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000634
Chris Lattner78ec3112003-08-11 14:57:33 +0000635SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000636 while (!AllNodes.empty()) {
637 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000638 delete [] N->OperandList;
639 N->OperandList = 0;
640 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000641 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000642 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000643}
644
Chris Lattner0f2287b2005-04-13 02:38:18 +0000645SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000646 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000647 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000648 return getNode(ISD::AND, Op.getValueType(), Op,
649 getConstant(Imm, Op.getValueType()));
650}
651
Chris Lattnerc3aae252005-01-07 07:46:32 +0000652SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
653 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000654 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
655
Chris Lattnerc3aae252005-01-07 07:46:32 +0000656 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000657 if (VT != MVT::i64)
658 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000659
Chris Lattnerc3aae252005-01-07 07:46:32 +0000660 SDNode *&N = Constants[std::make_pair(Val, VT)];
661 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000662 N = new ConstantSDNode(false, Val, VT);
663 AllNodes.push_back(N);
664 return SDOperand(N, 0);
665}
666
Chris Lattner36ce6912005-11-29 06:21:05 +0000667SDOperand SelectionDAG::getString(const std::string &Val) {
668 StringSDNode *&N = StringNodes[Val];
669 if (!N) {
670 N = new StringSDNode(Val);
671 AllNodes.push_back(N);
672 }
673 return SDOperand(N, 0);
674}
675
Chris Lattner37bfbb42005-08-17 00:34:06 +0000676SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
677 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
678 // Mask out any bits that are not valid for this constant.
679 if (VT != MVT::i64)
680 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
681
682 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
683 if (N) return SDOperand(N, 0);
684 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000685 AllNodes.push_back(N);
686 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000687}
688
Chris Lattnerc3aae252005-01-07 07:46:32 +0000689SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
690 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
691 if (VT == MVT::f32)
692 Val = (float)Val; // Mask out extra precision.
693
Chris Lattnerd8658612005-02-17 20:17:32 +0000694 // Do the map lookup using the actual bit pattern for the floating point
695 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
696 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000697 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000698 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000699 N = new ConstantFPSDNode(false, Val, VT);
700 AllNodes.push_back(N);
701 return SDOperand(N, 0);
702}
703
704SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
705 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
706 if (VT == MVT::f32)
707 Val = (float)Val; // Mask out extra precision.
708
709 // Do the map lookup using the actual bit pattern for the floating point
710 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
711 // we don't have issues with SNANs.
712 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
713 if (N) return SDOperand(N, 0);
714 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000715 AllNodes.push_back(N);
716 return SDOperand(N, 0);
717}
718
Chris Lattnerc3aae252005-01-07 07:46:32 +0000719SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000720 MVT::ValueType VT, int offset) {
721 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000722 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000723 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000724 AllNodes.push_back(N);
725 return SDOperand(N, 0);
726}
727
728SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000729 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000730 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000731 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000732 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733 AllNodes.push_back(N);
734 return SDOperand(N, 0);
735}
736
737SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
738 SDNode *&N = FrameIndices[FI];
739 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000740 N = new FrameIndexSDNode(FI, VT, false);
741 AllNodes.push_back(N);
742 return SDOperand(N, 0);
743}
744
745SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
746 SDNode *&N = TargetFrameIndices[FI];
747 if (N) return SDOperand(N, 0);
748 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000749 AllNodes.push_back(N);
750 return SDOperand(N, 0);
751}
752
Nate Begeman37efe672006-04-22 18:53:45 +0000753SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
754 SDNode *&N = JumpTableIndices[JTI];
755 if (N) return SDOperand(N, 0);
756 N = new JumpTableSDNode(JTI, VT, false);
757 AllNodes.push_back(N);
758 return SDOperand(N, 0);
759}
760
761SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
762 SDNode *&N = TargetJumpTableIndices[JTI];
763 if (N) return SDOperand(N, 0);
764 N = new JumpTableSDNode(JTI, VT, true);
765 AllNodes.push_back(N);
766 return SDOperand(N, 0);
767}
768
Evan Chengb8973bd2006-01-31 22:23:14 +0000769SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000770 unsigned Alignment, int Offset) {
771 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
772 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000773 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000774 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000775 AllNodes.push_back(N);
776 return SDOperand(N, 0);
777}
778
Evan Chengb8973bd2006-01-31 22:23:14 +0000779SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000780 unsigned Alignment, int Offset) {
781 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
782 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000783 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000784 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000785 AllNodes.push_back(N);
786 return SDOperand(N, 0);
787}
788
789SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
790 SDNode *&N = BBNodes[MBB];
791 if (N) return SDOperand(N, 0);
792 N = new BasicBlockSDNode(MBB);
793 AllNodes.push_back(N);
794 return SDOperand(N, 0);
795}
796
Chris Lattner15e4b012005-07-10 00:07:11 +0000797SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
798 if ((unsigned)VT >= ValueTypeNodes.size())
799 ValueTypeNodes.resize(VT+1);
800 if (ValueTypeNodes[VT] == 0) {
801 ValueTypeNodes[VT] = new VTSDNode(VT);
802 AllNodes.push_back(ValueTypeNodes[VT]);
803 }
804
805 return SDOperand(ValueTypeNodes[VT], 0);
806}
807
Chris Lattnerc3aae252005-01-07 07:46:32 +0000808SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
809 SDNode *&N = ExternalSymbols[Sym];
810 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000811 N = new ExternalSymbolSDNode(false, Sym, VT);
812 AllNodes.push_back(N);
813 return SDOperand(N, 0);
814}
815
Chris Lattner809ec112006-01-28 10:09:25 +0000816SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
817 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000818 SDNode *&N = TargetExternalSymbols[Sym];
819 if (N) return SDOperand(N, 0);
820 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821 AllNodes.push_back(N);
822 return SDOperand(N, 0);
823}
824
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000825SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
826 if ((unsigned)Cond >= CondCodeNodes.size())
827 CondCodeNodes.resize(Cond+1);
828
Chris Lattner079a27a2005-08-09 20:40:02 +0000829 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000830 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000831 AllNodes.push_back(CondCodeNodes[Cond]);
832 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000833 return SDOperand(CondCodeNodes[Cond], 0);
834}
835
Chris Lattner0fdd7682005-08-30 22:38:38 +0000836SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
837 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
838 if (!Reg) {
839 Reg = new RegisterSDNode(RegNo, VT);
840 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000841 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000842 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000843}
844
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000845SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
846 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000847 // These setcc operations always fold.
848 switch (Cond) {
849 default: break;
850 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000851 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000852 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000853 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000854
855 case ISD::SETOEQ:
856 case ISD::SETOGT:
857 case ISD::SETOGE:
858 case ISD::SETOLT:
859 case ISD::SETOLE:
860 case ISD::SETONE:
861 case ISD::SETO:
862 case ISD::SETUO:
863 case ISD::SETUEQ:
864 case ISD::SETUNE:
865 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
866 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000867 }
868
Chris Lattner67255a12005-04-07 18:14:58 +0000869 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
870 uint64_t C2 = N2C->getValue();
871 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
872 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000873
Chris Lattnerc3aae252005-01-07 07:46:32 +0000874 // Sign extend the operands if required
875 if (ISD::isSignedIntSetCC(Cond)) {
876 C1 = N1C->getSignExtended();
877 C2 = N2C->getSignExtended();
878 }
879
880 switch (Cond) {
881 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000882 case ISD::SETEQ: return getConstant(C1 == C2, VT);
883 case ISD::SETNE: return getConstant(C1 != C2, VT);
884 case ISD::SETULT: return getConstant(C1 < C2, VT);
885 case ISD::SETUGT: return getConstant(C1 > C2, VT);
886 case ISD::SETULE: return getConstant(C1 <= C2, VT);
887 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
888 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
889 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
890 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
891 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000892 }
Chris Lattner24673922005-04-07 18:58:54 +0000893 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000894 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000895 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
896 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
897
898 // If the comparison constant has bits in the upper part, the
899 // zero-extended value could never match.
900 if (C2 & (~0ULL << InSize)) {
901 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
902 switch (Cond) {
903 case ISD::SETUGT:
904 case ISD::SETUGE:
905 case ISD::SETEQ: return getConstant(0, VT);
906 case ISD::SETULT:
907 case ISD::SETULE:
908 case ISD::SETNE: return getConstant(1, VT);
909 case ISD::SETGT:
910 case ISD::SETGE:
911 // True if the sign bit of C2 is set.
912 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
913 case ISD::SETLT:
914 case ISD::SETLE:
915 // True if the sign bit of C2 isn't set.
916 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
917 default:
918 break;
919 }
920 }
921
922 // Otherwise, we can perform the comparison with the low bits.
923 switch (Cond) {
924 case ISD::SETEQ:
925 case ISD::SETNE:
926 case ISD::SETUGT:
927 case ISD::SETUGE:
928 case ISD::SETULT:
929 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000930 return getSetCC(VT, N1.getOperand(0),
931 getConstant(C2, N1.getOperand(0).getValueType()),
932 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000933 default:
934 break; // todo, be more careful with signed comparisons
935 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000936 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
937 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
938 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
939 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
940 MVT::ValueType ExtDstTy = N1.getValueType();
941 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000942
Chris Lattner7b2880c2005-08-24 22:44:39 +0000943 // If the extended part has any inconsistent bits, it cannot ever
944 // compare equal. In other words, they have to be all ones or all
945 // zeros.
946 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000947 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000948 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
949 return getConstant(Cond == ISD::SETNE, VT);
950
951 // Otherwise, make this a use of a zext.
952 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000953 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000954 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000955 }
956
Chris Lattner67255a12005-04-07 18:14:58 +0000957 uint64_t MinVal, MaxVal;
958 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
959 if (ISD::isSignedIntSetCC(Cond)) {
960 MinVal = 1ULL << (OperandBitSize-1);
961 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
962 MaxVal = ~0ULL >> (65-OperandBitSize);
963 else
964 MaxVal = 0;
965 } else {
966 MinVal = 0;
967 MaxVal = ~0ULL >> (64-OperandBitSize);
968 }
969
970 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
971 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
972 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
973 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000974 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
975 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000976 }
977
978 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
979 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
980 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000981 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
982 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000983 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000984
Nate Begeman72ea2812005-04-14 08:56:52 +0000985 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
986 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000987
Nate Begeman72ea2812005-04-14 08:56:52 +0000988 // Canonicalize setgt X, Min --> setne X, Min
989 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000990 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000991
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000992 // If we have setult X, 1, turn it into seteq X, 0
993 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000994 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
995 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000996 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000997 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000998 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
999 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +00001000
1001 // If we have "setcc X, C1", check to see if we can shrink the immediate
1002 // by changing cc.
1003
1004 // SETUGT X, SINTMAX -> SETLT X, 0
1005 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1006 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001007 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +00001008
1009 // FIXME: Implement the rest of these.
1010
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001011
1012 // Fold bit comparisons when we can.
1013 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1014 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
1015 if (ConstantSDNode *AndRHS =
1016 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1017 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1018 // Perform the xform if the AND RHS is a single bit.
1019 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1020 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001021 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001022 TLI.getShiftAmountTy()));
1023 }
1024 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
1025 // (X & 8) == 8 --> (X & 8) >> 3
1026 // Perform the xform if C2 is a single bit.
1027 if ((C2 & (C2-1)) == 0) {
1028 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001029 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001030 }
1031 }
1032 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001033 }
Chris Lattner67255a12005-04-07 18:14:58 +00001034 } else if (isa<ConstantSDNode>(N1.Val)) {
1035 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001036 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +00001037 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001038
1039 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1040 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1041 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001042
Chris Lattnerc3aae252005-01-07 07:46:32 +00001043 switch (Cond) {
1044 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001045 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1046 case ISD::SETNE: return getConstant(C1 != C2, VT);
1047 case ISD::SETLT: return getConstant(C1 < C2, VT);
1048 case ISD::SETGT: return getConstant(C1 > C2, VT);
1049 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1050 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001051 }
1052 } else {
1053 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001054 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055 }
1056
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001057 // Could not fold it.
1058 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001059}
1060
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001062///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001063SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001064 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1065 if (!N) {
1066 N = new SDNode(Opcode, VT);
1067 AllNodes.push_back(N);
1068 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001069 return SDOperand(N, 0);
1070}
1071
Chris Lattnerc3aae252005-01-07 07:46:32 +00001072SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1073 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001074 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001075 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1077 uint64_t Val = C->getValue();
1078 switch (Opcode) {
1079 default: break;
1080 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001081 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1083 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001084 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1085 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001086 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001087 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001088 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001089 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001090 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001091 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001092 case ISD::BSWAP:
1093 switch(VT) {
1094 default: assert(0 && "Invalid bswap!"); break;
1095 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1096 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1097 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1098 }
1099 break;
1100 case ISD::CTPOP:
1101 switch(VT) {
1102 default: assert(0 && "Invalid ctpop!"); break;
1103 case MVT::i1: return getConstant(Val != 0, VT);
1104 case MVT::i8:
1105 Tmp1 = (unsigned)Val & 0xFF;
1106 return getConstant(CountPopulation_32(Tmp1), VT);
1107 case MVT::i16:
1108 Tmp1 = (unsigned)Val & 0xFFFF;
1109 return getConstant(CountPopulation_32(Tmp1), VT);
1110 case MVT::i32:
1111 return getConstant(CountPopulation_32((unsigned)Val), VT);
1112 case MVT::i64:
1113 return getConstant(CountPopulation_64(Val), VT);
1114 }
1115 case ISD::CTLZ:
1116 switch(VT) {
1117 default: assert(0 && "Invalid ctlz!"); break;
1118 case MVT::i1: return getConstant(Val == 0, VT);
1119 case MVT::i8:
1120 Tmp1 = (unsigned)Val & 0xFF;
1121 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1122 case MVT::i16:
1123 Tmp1 = (unsigned)Val & 0xFFFF;
1124 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1125 case MVT::i32:
1126 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1127 case MVT::i64:
1128 return getConstant(CountLeadingZeros_64(Val), VT);
1129 }
1130 case ISD::CTTZ:
1131 switch(VT) {
1132 default: assert(0 && "Invalid cttz!"); break;
1133 case MVT::i1: return getConstant(Val == 0, VT);
1134 case MVT::i8:
1135 Tmp1 = (unsigned)Val | 0x100;
1136 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1137 case MVT::i16:
1138 Tmp1 = (unsigned)Val | 0x10000;
1139 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1140 case MVT::i32:
1141 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1142 case MVT::i64:
1143 return getConstant(CountTrailingZeros_64(Val), VT);
1144 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 }
1146 }
1147
Chris Lattner94683772005-12-23 05:30:37 +00001148 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001149 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1150 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001151 case ISD::FNEG:
1152 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001153 case ISD::FABS:
1154 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001155 case ISD::FP_ROUND:
1156 case ISD::FP_EXTEND:
1157 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001158 case ISD::FP_TO_SINT:
1159 return getConstant((int64_t)C->getValue(), VT);
1160 case ISD::FP_TO_UINT:
1161 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001162 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001163 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001164 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001165 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001166 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001167 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001168 }
1169
1170 unsigned OpOpcode = Operand.Val->getOpcode();
1171 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001172 case ISD::TokenFactor:
1173 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001174 case ISD::SIGN_EXTEND:
1175 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001176 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001177 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1178 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1179 break;
1180 case ISD::ZERO_EXTEND:
1181 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001182 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001183 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001184 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001186 case ISD::ANY_EXTEND:
1187 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001188 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001189 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1190 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1191 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1192 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001193 case ISD::TRUNCATE:
1194 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001195 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001196 if (OpOpcode == ISD::TRUNCATE)
1197 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001198 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1199 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001200 // If the source is smaller than the dest, we still need an extend.
1201 if (Operand.Val->getOperand(0).getValueType() < VT)
1202 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1203 else if (Operand.Val->getOperand(0).getValueType() > VT)
1204 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1205 else
1206 return Operand.Val->getOperand(0);
1207 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001208 break;
Chris Lattner35481892005-12-23 00:16:34 +00001209 case ISD::BIT_CONVERT:
1210 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001211 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001212 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001213 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001214 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1215 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001216 if (OpOpcode == ISD::UNDEF)
1217 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001218 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001219 case ISD::SCALAR_TO_VECTOR:
1220 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1221 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1222 "Illegal SCALAR_TO_VECTOR node!");
1223 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001224 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001225 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1226 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001227 Operand.Val->getOperand(0));
1228 if (OpOpcode == ISD::FNEG) // --X -> X
1229 return Operand.Val->getOperand(0);
1230 break;
1231 case ISD::FABS:
1232 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1233 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1234 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001235 }
1236
Chris Lattner43247a12005-08-25 19:12:10 +00001237 SDNode *N;
1238 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1239 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001240 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001241 E = N = new SDNode(Opcode, Operand);
1242 } else {
1243 N = new SDNode(Opcode, Operand);
1244 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001245 N->setValueTypes(VT);
1246 AllNodes.push_back(N);
1247 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001248}
1249
Chris Lattner36019aa2005-04-18 03:48:41 +00001250
1251
Chris Lattnerc3aae252005-01-07 07:46:32 +00001252SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1253 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001254#ifndef NDEBUG
1255 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001256 case ISD::TokenFactor:
1257 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1258 N2.getValueType() == MVT::Other && "Invalid token factor!");
1259 break;
Chris Lattner76365122005-01-16 02:23:22 +00001260 case ISD::AND:
1261 case ISD::OR:
1262 case ISD::XOR:
1263 case ISD::UDIV:
1264 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001265 case ISD::MULHU:
1266 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001267 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1268 // fall through
1269 case ISD::ADD:
1270 case ISD::SUB:
1271 case ISD::MUL:
1272 case ISD::SDIV:
1273 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001274 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1275 // fall through.
1276 case ISD::FADD:
1277 case ISD::FSUB:
1278 case ISD::FMUL:
1279 case ISD::FDIV:
1280 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001281 assert(N1.getValueType() == N2.getValueType() &&
1282 N1.getValueType() == VT && "Binary operator types must match!");
1283 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001284 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1285 assert(N1.getValueType() == VT &&
1286 MVT::isFloatingPoint(N1.getValueType()) &&
1287 MVT::isFloatingPoint(N2.getValueType()) &&
1288 "Invalid FCOPYSIGN!");
1289 break;
Chris Lattner76365122005-01-16 02:23:22 +00001290 case ISD::SHL:
1291 case ISD::SRA:
1292 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001293 case ISD::ROTL:
1294 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001295 assert(VT == N1.getValueType() &&
1296 "Shift operators return type must be the same as their first arg");
1297 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001298 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001299 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001300 case ISD::FP_ROUND_INREG: {
1301 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1302 assert(VT == N1.getValueType() && "Not an inreg round!");
1303 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1304 "Cannot FP_ROUND_INREG integer types");
1305 assert(EVT <= VT && "Not rounding down!");
1306 break;
1307 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001308 case ISD::AssertSext:
1309 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001310 case ISD::SIGN_EXTEND_INREG: {
1311 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1312 assert(VT == N1.getValueType() && "Not an inreg extend!");
1313 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1314 "Cannot *_EXTEND_INREG FP types");
1315 assert(EVT <= VT && "Not extending!");
1316 }
1317
Chris Lattner76365122005-01-16 02:23:22 +00001318 default: break;
1319 }
1320#endif
1321
Chris Lattnerc3aae252005-01-07 07:46:32 +00001322 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1323 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1324 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001325 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1326 int64_t Val = N1C->getValue();
1327 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1328 Val <<= 64-FromBits;
1329 Val >>= 64-FromBits;
1330 return getConstant(Val, VT);
1331 }
1332
Chris Lattnerc3aae252005-01-07 07:46:32 +00001333 if (N2C) {
1334 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1335 switch (Opcode) {
1336 case ISD::ADD: return getConstant(C1 + C2, VT);
1337 case ISD::SUB: return getConstant(C1 - C2, VT);
1338 case ISD::MUL: return getConstant(C1 * C2, VT);
1339 case ISD::UDIV:
1340 if (C2) return getConstant(C1 / C2, VT);
1341 break;
1342 case ISD::UREM :
1343 if (C2) return getConstant(C1 % C2, VT);
1344 break;
1345 case ISD::SDIV :
1346 if (C2) return getConstant(N1C->getSignExtended() /
1347 N2C->getSignExtended(), VT);
1348 break;
1349 case ISD::SREM :
1350 if (C2) return getConstant(N1C->getSignExtended() %
1351 N2C->getSignExtended(), VT);
1352 break;
1353 case ISD::AND : return getConstant(C1 & C2, VT);
1354 case ISD::OR : return getConstant(C1 | C2, VT);
1355 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001356 case ISD::SHL : return getConstant(C1 << C2, VT);
1357 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001358 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001359 case ISD::ROTL :
1360 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1361 VT);
1362 case ISD::ROTR :
1363 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1364 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001365 default: break;
1366 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001367 } else { // Cannonicalize constant to RHS if commutative
1368 if (isCommutativeBinOp(Opcode)) {
1369 std::swap(N1C, N2C);
1370 std::swap(N1, N2);
1371 }
1372 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001373 }
1374
1375 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1376 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001377 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001378 if (N2CFP) {
1379 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1380 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001381 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1382 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1383 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1384 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001385 if (C2) return getConstantFP(C1 / C2, VT);
1386 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001387 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001388 if (C2) return getConstantFP(fmod(C1, C2), VT);
1389 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001390 case ISD::FCOPYSIGN: {
1391 union {
1392 double F;
1393 uint64_t I;
1394 } u1;
1395 union {
1396 double F;
1397 int64_t I;
1398 } u2;
1399 u1.F = C1;
1400 u2.F = C2;
1401 if (u2.I < 0) // Sign bit of RHS set?
1402 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1403 else
1404 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1405 return getConstantFP(u1.F, VT);
1406 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001407 default: break;
1408 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001409 } else { // Cannonicalize constant to RHS if commutative
1410 if (isCommutativeBinOp(Opcode)) {
1411 std::swap(N1CFP, N2CFP);
1412 std::swap(N1, N2);
1413 }
1414 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001415 }
Chris Lattner62b57722006-04-20 05:39:12 +00001416
1417 // Canonicalize an UNDEF to the RHS, even over a constant.
1418 if (N1.getOpcode() == ISD::UNDEF) {
1419 if (isCommutativeBinOp(Opcode)) {
1420 std::swap(N1, N2);
1421 } else {
1422 switch (Opcode) {
1423 case ISD::FP_ROUND_INREG:
1424 case ISD::SIGN_EXTEND_INREG:
1425 case ISD::SUB:
1426 case ISD::FSUB:
1427 case ISD::FDIV:
1428 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001429 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001430 return N1; // fold op(undef, arg2) -> undef
1431 case ISD::UDIV:
1432 case ISD::SDIV:
1433 case ISD::UREM:
1434 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001435 case ISD::SRL:
1436 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001437 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1438 }
1439 }
1440 }
1441
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001442 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001443 if (N2.getOpcode() == ISD::UNDEF) {
1444 switch (Opcode) {
1445 case ISD::ADD:
1446 case ISD::SUB:
1447 case ISD::FADD:
1448 case ISD::FSUB:
1449 case ISD::FMUL:
1450 case ISD::FDIV:
1451 case ISD::FREM:
1452 case ISD::UDIV:
1453 case ISD::SDIV:
1454 case ISD::UREM:
1455 case ISD::SREM:
1456 case ISD::XOR:
1457 return N2; // fold op(arg1, undef) -> undef
1458 case ISD::MUL:
1459 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001460 case ISD::SRL:
1461 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001462 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1463 case ISD::OR:
1464 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001465 case ISD::SRA:
1466 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001467 }
1468 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001469
Chris Lattnerc3aae252005-01-07 07:46:32 +00001470 // Finally, fold operations that do not require constants.
1471 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001472 case ISD::FP_ROUND_INREG:
1473 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1474 break;
1475 case ISD::SIGN_EXTEND_INREG: {
1476 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1477 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001478 break;
1479 }
1480
Nate Begemaneea805e2005-04-13 21:23:31 +00001481 // FIXME: figure out how to safely handle things like
1482 // int foo(int x) { return 1 << (x & 255); }
1483 // int bar() { return foo(256); }
1484#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001485 case ISD::SHL:
1486 case ISD::SRL:
1487 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001488 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001489 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001490 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001491 else if (N2.getOpcode() == ISD::AND)
1492 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1493 // If the and is only masking out bits that cannot effect the shift,
1494 // eliminate the and.
1495 unsigned NumBits = MVT::getSizeInBits(VT);
1496 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1497 return getNode(Opcode, VT, N1, N2.getOperand(0));
1498 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001499 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001500#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001501 }
1502
Chris Lattner27e9b412005-05-11 18:57:39 +00001503 // Memoize this node if possible.
1504 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001505 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001506 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1507 if (BON) return SDOperand(BON, 0);
1508
1509 BON = N = new SDNode(Opcode, N1, N2);
1510 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001511 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001512 }
1513
Chris Lattner3e011362005-05-14 07:45:46 +00001514 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001515 AllNodes.push_back(N);
1516 return SDOperand(N, 0);
1517}
1518
Chris Lattnerc3aae252005-01-07 07:46:32 +00001519SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1520 SDOperand N1, SDOperand N2, SDOperand N3) {
1521 // Perform various simplifications.
1522 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1523 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001524 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001525 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001526 case ISD::SETCC: {
1527 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001528 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001529 if (Simp.Val) return Simp;
1530 break;
1531 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001532 case ISD::SELECT:
1533 if (N1C)
1534 if (N1C->getValue())
1535 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001536 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001537 return N3; // select false, X, Y -> Y
1538
1539 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001540 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001541 case ISD::BRCOND:
1542 if (N2C)
1543 if (N2C->getValue()) // Unconditional branch
1544 return getNode(ISD::BR, MVT::Other, N1, N3);
1545 else
1546 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001547 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001548 case ISD::VECTOR_SHUFFLE:
1549 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1550 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1551 N3.getOpcode() == ISD::BUILD_VECTOR &&
1552 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1553 "Illegal VECTOR_SHUFFLE node!");
1554 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001555 }
1556
Chris Lattner385328c2005-05-14 07:42:29 +00001557 std::vector<SDOperand> Ops;
1558 Ops.reserve(3);
1559 Ops.push_back(N1);
1560 Ops.push_back(N2);
1561 Ops.push_back(N3);
1562
Chris Lattner43247a12005-08-25 19:12:10 +00001563 // Memoize node if it doesn't produce a flag.
1564 SDNode *N;
1565 if (VT != MVT::Flag) {
1566 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1567 if (E) return SDOperand(E, 0);
1568 E = N = new SDNode(Opcode, N1, N2, N3);
1569 } else {
1570 N = new SDNode(Opcode, N1, N2, N3);
1571 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001572 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001573 AllNodes.push_back(N);
1574 return SDOperand(N, 0);
1575}
1576
1577SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001578 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001579 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001580 std::vector<SDOperand> Ops;
1581 Ops.reserve(4);
1582 Ops.push_back(N1);
1583 Ops.push_back(N2);
1584 Ops.push_back(N3);
1585 Ops.push_back(N4);
1586 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001587}
1588
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001589SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1590 SDOperand N1, SDOperand N2, SDOperand N3,
1591 SDOperand N4, SDOperand N5) {
1592 std::vector<SDOperand> Ops;
1593 Ops.reserve(5);
1594 Ops.push_back(N1);
1595 Ops.push_back(N2);
1596 Ops.push_back(N3);
1597 Ops.push_back(N4);
1598 Ops.push_back(N5);
1599 return getNode(Opcode, VT, Ops);
1600}
1601
Evan Cheng7038daf2005-12-10 00:37:58 +00001602SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1603 SDOperand Chain, SDOperand Ptr,
1604 SDOperand SV) {
1605 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1606 if (N) return SDOperand(N, 0);
1607 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1608
1609 // Loads have a token chain.
1610 setNodeValueTypes(N, VT, MVT::Other);
1611 AllNodes.push_back(N);
1612 return SDOperand(N, 0);
1613}
1614
1615SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1616 SDOperand Chain, SDOperand Ptr,
1617 SDOperand SV) {
Evan Cheng7038daf2005-12-10 00:37:58 +00001618 std::vector<SDOperand> Ops;
1619 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001620 Ops.push_back(Chain);
1621 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001622 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001623 Ops.push_back(getConstant(Count, MVT::i32));
1624 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001625 std::vector<MVT::ValueType> VTs;
1626 VTs.reserve(2);
1627 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1628 return getNode(ISD::VLOAD, VTs, Ops);
1629}
1630
1631SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1632 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1633 MVT::ValueType EVT) {
1634 std::vector<SDOperand> Ops;
1635 Ops.reserve(4);
1636 Ops.push_back(Chain);
1637 Ops.push_back(Ptr);
1638 Ops.push_back(SV);
1639 Ops.push_back(getValueType(EVT));
1640 std::vector<MVT::ValueType> VTs;
1641 VTs.reserve(2);
1642 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1643 return getNode(Opcode, VTs, Ops);
1644}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001645
Chris Lattner0437cdd2005-05-09 04:14:13 +00001646SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001647 assert((!V || isa<PointerType>(V->getType())) &&
1648 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001649 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1650 if (N) return SDOperand(N, 0);
1651
1652 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001653 AllNodes.push_back(N);
1654 return SDOperand(N, 0);
1655}
1656
Nate Begemanacc398c2006-01-25 18:21:52 +00001657SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1658 SDOperand Chain, SDOperand Ptr,
1659 SDOperand SV) {
1660 std::vector<SDOperand> Ops;
1661 Ops.reserve(3);
1662 Ops.push_back(Chain);
1663 Ops.push_back(Ptr);
1664 Ops.push_back(SV);
1665 std::vector<MVT::ValueType> VTs;
1666 VTs.reserve(2);
1667 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1668 return getNode(ISD::VAARG, VTs, Ops);
1669}
1670
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001671SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001672 std::vector<SDOperand> &Ops) {
1673 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001674 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001675 case 1: return getNode(Opcode, VT, Ops[0]);
1676 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1677 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001678 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001679 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001680
Chris Lattneref847df2005-04-09 03:27:28 +00001681 switch (Opcode) {
1682 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001683 case ISD::TRUNCSTORE: {
1684 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1685 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1686#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1687 // If this is a truncating store of a constant, convert to the desired type
1688 // and store it instead.
1689 if (isa<Constant>(Ops[0])) {
1690 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1691 if (isa<Constant>(Op))
1692 N1 = Op;
1693 }
1694 // Also for ConstantFP?
1695#endif
1696 if (Ops[0].getValueType() == EVT) // Normal store?
1697 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1698 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1699 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1700 "Can't do FP-INT conversion!");
1701 break;
1702 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001703 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001704 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001705 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1706 "LHS and RHS of condition must have same type!");
1707 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1708 "True and False arms of SelectCC must have same type!");
1709 assert(Ops[2].getValueType() == VT &&
1710 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001711 break;
1712 }
1713 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001714 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001715 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1716 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001717 break;
1718 }
Chris Lattneref847df2005-04-09 03:27:28 +00001719 }
1720
Chris Lattner385328c2005-05-14 07:42:29 +00001721 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001722 SDNode *N;
1723 if (VT != MVT::Flag) {
1724 SDNode *&E =
1725 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1726 if (E) return SDOperand(E, 0);
1727 E = N = new SDNode(Opcode, Ops);
1728 } else {
1729 N = new SDNode(Opcode, Ops);
1730 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001731 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001732 AllNodes.push_back(N);
1733 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001734}
1735
Chris Lattner89c34632005-05-14 06:20:26 +00001736SDOperand SelectionDAG::getNode(unsigned Opcode,
1737 std::vector<MVT::ValueType> &ResultTys,
1738 std::vector<SDOperand> &Ops) {
1739 if (ResultTys.size() == 1)
1740 return getNode(Opcode, ResultTys[0], Ops);
1741
Chris Lattner5f056bf2005-07-10 01:55:33 +00001742 switch (Opcode) {
1743 case ISD::EXTLOAD:
1744 case ISD::SEXTLOAD:
1745 case ISD::ZEXTLOAD: {
1746 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1747 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1748 // If they are asking for an extending load from/to the same thing, return a
1749 // normal load.
1750 if (ResultTys[0] == EVT)
1751 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001752 if (MVT::isVector(ResultTys[0])) {
1753 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1754 "Invalid vector extload!");
1755 } else {
1756 assert(EVT < ResultTys[0] &&
1757 "Should only be an extending load, not truncating!");
1758 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001759 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001760 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001761 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1762 "Cannot convert from FP to Int or Int -> FP!");
1763 break;
1764 }
1765
Chris Lattnere89083a2005-05-14 07:25:05 +00001766 // FIXME: figure out how to safely handle things like
1767 // int foo(int x) { return 1 << (x & 255); }
1768 // int bar() { return foo(256); }
1769#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001770 case ISD::SRA_PARTS:
1771 case ISD::SRL_PARTS:
1772 case ISD::SHL_PARTS:
1773 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001774 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001775 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1776 else if (N3.getOpcode() == ISD::AND)
1777 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1778 // If the and is only masking out bits that cannot effect the shift,
1779 // eliminate the and.
1780 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1781 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1782 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1783 }
1784 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001785#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001786 }
Chris Lattner89c34632005-05-14 06:20:26 +00001787
Chris Lattner43247a12005-08-25 19:12:10 +00001788 // Memoize the node unless it returns a flag.
1789 SDNode *N;
1790 if (ResultTys.back() != MVT::Flag) {
1791 SDNode *&E =
1792 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1793 if (E) return SDOperand(E, 0);
1794 E = N = new SDNode(Opcode, Ops);
1795 } else {
1796 N = new SDNode(Opcode, Ops);
1797 }
Chris Lattnera3255112005-11-08 23:30:28 +00001798 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001799 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001800 return SDOperand(N, 0);
1801}
1802
Chris Lattnera3255112005-11-08 23:30:28 +00001803void SelectionDAG::setNodeValueTypes(SDNode *N,
1804 std::vector<MVT::ValueType> &RetVals) {
1805 switch (RetVals.size()) {
1806 case 0: return;
1807 case 1: N->setValueTypes(RetVals[0]); return;
1808 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1809 default: break;
1810 }
1811
1812 std::list<std::vector<MVT::ValueType> >::iterator I =
1813 std::find(VTList.begin(), VTList.end(), RetVals);
1814 if (I == VTList.end()) {
1815 VTList.push_front(RetVals);
1816 I = VTList.begin();
1817 }
1818
1819 N->setValueTypes(&(*I)[0], I->size());
1820}
1821
1822void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1823 MVT::ValueType VT2) {
1824 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1825 E = VTList.end(); I != E; ++I) {
1826 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1827 N->setValueTypes(&(*I)[0], 2);
1828 return;
1829 }
1830 }
1831 std::vector<MVT::ValueType> V;
1832 V.push_back(VT1);
1833 V.push_back(VT2);
1834 VTList.push_front(V);
1835 N->setValueTypes(&(*VTList.begin())[0], 2);
1836}
1837
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001838/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1839/// specified operands. If the resultant node already exists in the DAG,
1840/// this does not modify the specified node, instead it returns the node that
1841/// already exists. If the resultant node does not exist in the DAG, the
1842/// input node is returned. As a degenerate case, if you specify the same
1843/// input operands as the node already has, the input node is returned.
1844SDOperand SelectionDAG::
1845UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1846 SDNode *N = InN.Val;
1847 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1848
1849 // Check to see if there is no change.
1850 if (Op == N->getOperand(0)) return InN;
1851
1852 // See if the modified node already exists.
1853 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1854 if (NewSlot && *NewSlot)
1855 return SDOperand(*NewSlot, InN.ResNo);
1856
1857 // Nope it doesn't. Remove the node from it's current place in the maps.
1858 if (NewSlot)
1859 RemoveNodeFromCSEMaps(N);
1860
1861 // Now we update the operands.
1862 N->OperandList[0].Val->removeUser(N);
1863 Op.Val->addUser(N);
1864 N->OperandList[0] = Op;
1865
1866 // If this gets put into a CSE map, add it.
1867 if (NewSlot) *NewSlot = N;
1868 return InN;
1869}
1870
1871SDOperand SelectionDAG::
1872UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1873 SDNode *N = InN.Val;
1874 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1875
1876 // Check to see if there is no change.
1877 bool AnyChange = false;
1878 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1879 return InN; // No operands changed, just return the input node.
1880
1881 // See if the modified node already exists.
1882 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1883 if (NewSlot && *NewSlot)
1884 return SDOperand(*NewSlot, InN.ResNo);
1885
1886 // Nope it doesn't. Remove the node from it's current place in the maps.
1887 if (NewSlot)
1888 RemoveNodeFromCSEMaps(N);
1889
1890 // Now we update the operands.
1891 if (N->OperandList[0] != Op1) {
1892 N->OperandList[0].Val->removeUser(N);
1893 Op1.Val->addUser(N);
1894 N->OperandList[0] = Op1;
1895 }
1896 if (N->OperandList[1] != Op2) {
1897 N->OperandList[1].Val->removeUser(N);
1898 Op2.Val->addUser(N);
1899 N->OperandList[1] = Op2;
1900 }
1901
1902 // If this gets put into a CSE map, add it.
1903 if (NewSlot) *NewSlot = N;
1904 return InN;
1905}
1906
1907SDOperand SelectionDAG::
1908UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1909 std::vector<SDOperand> Ops;
1910 Ops.push_back(Op1);
1911 Ops.push_back(Op2);
1912 Ops.push_back(Op3);
1913 return UpdateNodeOperands(N, Ops);
1914}
1915
1916SDOperand SelectionDAG::
1917UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1918 SDOperand Op3, SDOperand Op4) {
1919 std::vector<SDOperand> Ops;
1920 Ops.push_back(Op1);
1921 Ops.push_back(Op2);
1922 Ops.push_back(Op3);
1923 Ops.push_back(Op4);
1924 return UpdateNodeOperands(N, Ops);
1925}
1926
1927SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001928UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1929 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1930 std::vector<SDOperand> Ops;
1931 Ops.push_back(Op1);
1932 Ops.push_back(Op2);
1933 Ops.push_back(Op3);
1934 Ops.push_back(Op4);
1935 Ops.push_back(Op5);
1936 return UpdateNodeOperands(N, Ops);
1937}
1938
1939
1940SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001941UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1942 SDNode *N = InN.Val;
1943 assert(N->getNumOperands() == Ops.size() &&
1944 "Update with wrong number of operands");
1945
1946 // Check to see if there is no change.
1947 unsigned NumOps = Ops.size();
1948 bool AnyChange = false;
1949 for (unsigned i = 0; i != NumOps; ++i) {
1950 if (Ops[i] != N->getOperand(i)) {
1951 AnyChange = true;
1952 break;
1953 }
1954 }
1955
1956 // No operands changed, just return the input node.
1957 if (!AnyChange) return InN;
1958
1959 // See if the modified node already exists.
1960 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1961 if (NewSlot && *NewSlot)
1962 return SDOperand(*NewSlot, InN.ResNo);
1963
1964 // Nope it doesn't. Remove the node from it's current place in the maps.
1965 if (NewSlot)
1966 RemoveNodeFromCSEMaps(N);
1967
1968 // Now we update the operands.
1969 for (unsigned i = 0; i != NumOps; ++i) {
1970 if (N->OperandList[i] != Ops[i]) {
1971 N->OperandList[i].Val->removeUser(N);
1972 Ops[i].Val->addUser(N);
1973 N->OperandList[i] = Ops[i];
1974 }
1975 }
1976
1977 // If this gets put into a CSE map, add it.
1978 if (NewSlot) *NewSlot = N;
1979 return InN;
1980}
1981
1982
1983
Chris Lattner149c58c2005-08-16 18:17:10 +00001984
1985/// SelectNodeTo - These are used for target selectors to *mutate* the
1986/// specified node to have the specified return type, Target opcode, and
1987/// operands. Note that target opcodes are stored as
1988/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001989///
1990/// Note that SelectNodeTo returns the resultant node. If there is already a
1991/// node of the specified opcode and operands, it returns that node instead of
1992/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001993SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1994 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001995 // If an identical node already exists, use it.
1996 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1997 if (ON) return SDOperand(ON, 0);
1998
Chris Lattner7651fa42005-08-24 23:00:29 +00001999 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002000
Chris Lattner7651fa42005-08-24 23:00:29 +00002001 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2002 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002003
2004 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002005 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00002006}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002007
Chris Lattnereb19e402005-11-30 22:45:14 +00002008SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2009 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002010 // If an identical node already exists, use it.
2011 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2012 std::make_pair(Op1, VT))];
2013 if (ON) return SDOperand(ON, 0);
2014
Chris Lattner149c58c2005-08-16 18:17:10 +00002015 RemoveNodeFromCSEMaps(N);
2016 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2017 N->setValueTypes(VT);
2018 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002019
2020 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002021 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002022}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002023
Chris Lattnereb19e402005-11-30 22:45:14 +00002024SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2025 MVT::ValueType VT, SDOperand Op1,
2026 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002027 // If an identical node already exists, use it.
2028 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2029 std::make_pair(Op1, Op2))];
2030 if (ON) return SDOperand(ON, 0);
2031
Chris Lattner149c58c2005-08-16 18:17:10 +00002032 RemoveNodeFromCSEMaps(N);
2033 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2034 N->setValueTypes(VT);
2035 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002036
2037 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002038 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002039}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002040
Chris Lattnereb19e402005-11-30 22:45:14 +00002041SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2042 MVT::ValueType VT, SDOperand Op1,
2043 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002044 // If an identical node already exists, use it.
2045 std::vector<SDOperand> OpList;
2046 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2047 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2048 std::make_pair(VT, OpList))];
2049 if (ON) return SDOperand(ON, 0);
2050
Chris Lattner149c58c2005-08-16 18:17:10 +00002051 RemoveNodeFromCSEMaps(N);
2052 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2053 N->setValueTypes(VT);
2054 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002055
2056 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002057 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002058}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002059
Chris Lattnereb19e402005-11-30 22:45:14 +00002060SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2061 MVT::ValueType VT, SDOperand Op1,
2062 SDOperand Op2, SDOperand Op3,
2063 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002064 // If an identical node already exists, use it.
2065 std::vector<SDOperand> OpList;
2066 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2067 OpList.push_back(Op4);
2068 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2069 std::make_pair(VT, OpList))];
2070 if (ON) return SDOperand(ON, 0);
2071
Nate Begeman294a0a12005-08-18 07:30:15 +00002072 RemoveNodeFromCSEMaps(N);
2073 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2074 N->setValueTypes(VT);
2075 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002076
2077 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002078 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00002079}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002080
Chris Lattnereb19e402005-11-30 22:45:14 +00002081SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2082 MVT::ValueType VT, SDOperand Op1,
2083 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2084 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002085 // If an identical node already exists, use it.
2086 std::vector<SDOperand> OpList;
2087 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2088 OpList.push_back(Op4); OpList.push_back(Op5);
2089 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2090 std::make_pair(VT, OpList))];
2091 if (ON) return SDOperand(ON, 0);
2092
Chris Lattner6b09a292005-08-21 18:49:33 +00002093 RemoveNodeFromCSEMaps(N);
2094 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2095 N->setValueTypes(VT);
2096 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002097
2098 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002099 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00002100}
Chris Lattner149c58c2005-08-16 18:17:10 +00002101
Chris Lattnereb19e402005-11-30 22:45:14 +00002102SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2103 MVT::ValueType VT, SDOperand Op1,
2104 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2105 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002106 // If an identical node already exists, use it.
2107 std::vector<SDOperand> OpList;
2108 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2109 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2110 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2111 std::make_pair(VT, OpList))];
2112 if (ON) return SDOperand(ON, 0);
2113
Evan Cheng61ca74b2005-11-30 02:04:11 +00002114 RemoveNodeFromCSEMaps(N);
2115 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2116 N->setValueTypes(VT);
2117 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002118
2119 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002120 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002121}
2122
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002123SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2124 MVT::ValueType VT, SDOperand Op1,
2125 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2126 SDOperand Op5, SDOperand Op6,
2127 SDOperand Op7) {
2128 // If an identical node already exists, use it.
2129 std::vector<SDOperand> OpList;
2130 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2131 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2132 OpList.push_back(Op7);
2133 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2134 std::make_pair(VT, OpList))];
2135 if (ON) return SDOperand(ON, 0);
2136
2137 RemoveNodeFromCSEMaps(N);
2138 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2139 N->setValueTypes(VT);
2140 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2141
2142 ON = N; // Memoize the new node.
2143 return SDOperand(N, 0);
2144}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002145SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2146 MVT::ValueType VT, SDOperand Op1,
2147 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2148 SDOperand Op5, SDOperand Op6,
2149 SDOperand Op7, SDOperand Op8) {
2150 // If an identical node already exists, use it.
2151 std::vector<SDOperand> OpList;
2152 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2153 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2154 OpList.push_back(Op7); OpList.push_back(Op8);
2155 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2156 std::make_pair(VT, OpList))];
2157 if (ON) return SDOperand(ON, 0);
2158
2159 RemoveNodeFromCSEMaps(N);
2160 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2161 N->setValueTypes(VT);
2162 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2163
2164 ON = N; // Memoize the new node.
2165 return SDOperand(N, 0);
2166}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002167
Chris Lattnereb19e402005-11-30 22:45:14 +00002168SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2169 MVT::ValueType VT1, MVT::ValueType VT2,
2170 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002171 // If an identical node already exists, use it.
2172 std::vector<SDOperand> OpList;
2173 OpList.push_back(Op1); OpList.push_back(Op2);
2174 std::vector<MVT::ValueType> VTList;
2175 VTList.push_back(VT1); VTList.push_back(VT2);
2176 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2177 std::make_pair(VTList, OpList))];
2178 if (ON) return SDOperand(ON, 0);
2179
Chris Lattner0fb094f2005-11-19 01:44:53 +00002180 RemoveNodeFromCSEMaps(N);
2181 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2182 setNodeValueTypes(N, VT1, VT2);
2183 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002184
2185 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002186 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002187}
2188
Chris Lattnereb19e402005-11-30 22:45:14 +00002189SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2190 MVT::ValueType VT1, MVT::ValueType VT2,
2191 SDOperand Op1, SDOperand Op2,
2192 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002193 // If an identical node already exists, use it.
2194 std::vector<SDOperand> OpList;
2195 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2196 std::vector<MVT::ValueType> VTList;
2197 VTList.push_back(VT1); VTList.push_back(VT2);
2198 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2199 std::make_pair(VTList, OpList))];
2200 if (ON) return SDOperand(ON, 0);
2201
Chris Lattner0fb094f2005-11-19 01:44:53 +00002202 RemoveNodeFromCSEMaps(N);
2203 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2204 setNodeValueTypes(N, VT1, VT2);
2205 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002206
2207 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002208 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002209}
2210
Chris Lattnereb19e402005-11-30 22:45:14 +00002211SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2212 MVT::ValueType VT1, MVT::ValueType VT2,
2213 SDOperand Op1, SDOperand Op2,
2214 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002215 // If an identical node already exists, use it.
2216 std::vector<SDOperand> OpList;
2217 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2218 OpList.push_back(Op4);
2219 std::vector<MVT::ValueType> VTList;
2220 VTList.push_back(VT1); VTList.push_back(VT2);
2221 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2222 std::make_pair(VTList, OpList))];
2223 if (ON) return SDOperand(ON, 0);
2224
Chris Lattner0fb094f2005-11-19 01:44:53 +00002225 RemoveNodeFromCSEMaps(N);
2226 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2227 setNodeValueTypes(N, VT1, VT2);
2228 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002229
2230 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002231 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002232}
2233
Chris Lattnereb19e402005-11-30 22:45:14 +00002234SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2235 MVT::ValueType VT1, MVT::ValueType VT2,
2236 SDOperand Op1, SDOperand Op2,
2237 SDOperand Op3, SDOperand Op4,
2238 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002239 // If an identical node already exists, use it.
2240 std::vector<SDOperand> OpList;
2241 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2242 OpList.push_back(Op4); OpList.push_back(Op5);
2243 std::vector<MVT::ValueType> VTList;
2244 VTList.push_back(VT1); VTList.push_back(VT2);
2245 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2246 std::make_pair(VTList, OpList))];
2247 if (ON) return SDOperand(ON, 0);
2248
Chris Lattner0fb094f2005-11-19 01:44:53 +00002249 RemoveNodeFromCSEMaps(N);
2250 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2251 setNodeValueTypes(N, VT1, VT2);
2252 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002253
2254 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002255 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002256}
2257
Evan Cheng6ae46c42006-02-09 07:15:23 +00002258/// getTargetNode - These are used for target selectors to create a new node
2259/// with specified return type(s), target opcode, and operands.
2260///
2261/// Note that getTargetNode returns the resultant node. If there is already a
2262/// node of the specified opcode and operands, it returns that node instead of
2263/// the current one.
2264SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2265 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2266}
2267SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2268 SDOperand Op1) {
2269 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2270}
2271SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2272 SDOperand Op1, SDOperand Op2) {
2273 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2274}
2275SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2276 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2277 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2278}
2279SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2280 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2281 SDOperand Op4) {
2282 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2283}
2284SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2285 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2286 SDOperand Op4, SDOperand Op5) {
2287 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2288}
2289SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2290 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2291 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2292 std::vector<SDOperand> Ops;
2293 Ops.reserve(6);
2294 Ops.push_back(Op1);
2295 Ops.push_back(Op2);
2296 Ops.push_back(Op3);
2297 Ops.push_back(Op4);
2298 Ops.push_back(Op5);
2299 Ops.push_back(Op6);
2300 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2301}
2302SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2303 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2304 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2305 SDOperand Op7) {
2306 std::vector<SDOperand> Ops;
2307 Ops.reserve(7);
2308 Ops.push_back(Op1);
2309 Ops.push_back(Op2);
2310 Ops.push_back(Op3);
2311 Ops.push_back(Op4);
2312 Ops.push_back(Op5);
2313 Ops.push_back(Op6);
2314 Ops.push_back(Op7);
2315 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2316}
2317SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2318 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2319 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2320 SDOperand Op7, SDOperand Op8) {
2321 std::vector<SDOperand> Ops;
2322 Ops.reserve(8);
2323 Ops.push_back(Op1);
2324 Ops.push_back(Op2);
2325 Ops.push_back(Op3);
2326 Ops.push_back(Op4);
2327 Ops.push_back(Op5);
2328 Ops.push_back(Op6);
2329 Ops.push_back(Op7);
2330 Ops.push_back(Op8);
2331 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2332}
2333SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2334 std::vector<SDOperand> &Ops) {
2335 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2336}
2337SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2338 MVT::ValueType VT2, SDOperand Op1) {
2339 std::vector<MVT::ValueType> ResultTys;
2340 ResultTys.push_back(VT1);
2341 ResultTys.push_back(VT2);
2342 std::vector<SDOperand> Ops;
2343 Ops.push_back(Op1);
2344 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2345}
2346SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2347 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2348 std::vector<MVT::ValueType> ResultTys;
2349 ResultTys.push_back(VT1);
2350 ResultTys.push_back(VT2);
2351 std::vector<SDOperand> Ops;
2352 Ops.push_back(Op1);
2353 Ops.push_back(Op2);
2354 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2355}
2356SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2357 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2358 SDOperand Op3) {
2359 std::vector<MVT::ValueType> ResultTys;
2360 ResultTys.push_back(VT1);
2361 ResultTys.push_back(VT2);
2362 std::vector<SDOperand> Ops;
2363 Ops.push_back(Op1);
2364 Ops.push_back(Op2);
2365 Ops.push_back(Op3);
2366 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2367}
2368SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2369 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2370 SDOperand Op3, SDOperand Op4) {
2371 std::vector<MVT::ValueType> ResultTys;
2372 ResultTys.push_back(VT1);
2373 ResultTys.push_back(VT2);
2374 std::vector<SDOperand> Ops;
2375 Ops.push_back(Op1);
2376 Ops.push_back(Op2);
2377 Ops.push_back(Op3);
2378 Ops.push_back(Op4);
2379 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2380}
2381SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2382 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2383 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2384 std::vector<MVT::ValueType> ResultTys;
2385 ResultTys.push_back(VT1);
2386 ResultTys.push_back(VT2);
2387 std::vector<SDOperand> Ops;
2388 Ops.push_back(Op1);
2389 Ops.push_back(Op2);
2390 Ops.push_back(Op3);
2391 Ops.push_back(Op4);
2392 Ops.push_back(Op5);
2393 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2394}
2395SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2396 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2397 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2398 SDOperand Op6) {
2399 std::vector<MVT::ValueType> ResultTys;
2400 ResultTys.push_back(VT1);
2401 ResultTys.push_back(VT2);
2402 std::vector<SDOperand> Ops;
2403 Ops.push_back(Op1);
2404 Ops.push_back(Op2);
2405 Ops.push_back(Op3);
2406 Ops.push_back(Op4);
2407 Ops.push_back(Op5);
2408 Ops.push_back(Op6);
2409 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2410}
2411SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2412 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2413 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2414 SDOperand Op6, SDOperand Op7) {
2415 std::vector<MVT::ValueType> ResultTys;
2416 ResultTys.push_back(VT1);
2417 ResultTys.push_back(VT2);
2418 std::vector<SDOperand> Ops;
2419 Ops.push_back(Op1);
2420 Ops.push_back(Op2);
2421 Ops.push_back(Op3);
2422 Ops.push_back(Op4);
2423 Ops.push_back(Op5);
2424 Ops.push_back(Op6);
2425 Ops.push_back(Op7);
2426 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2427}
2428SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2429 MVT::ValueType VT2, MVT::ValueType VT3,
2430 SDOperand Op1, SDOperand Op2) {
2431 std::vector<MVT::ValueType> ResultTys;
2432 ResultTys.push_back(VT1);
2433 ResultTys.push_back(VT2);
2434 ResultTys.push_back(VT3);
2435 std::vector<SDOperand> Ops;
2436 Ops.push_back(Op1);
2437 Ops.push_back(Op2);
2438 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2439}
2440SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2441 MVT::ValueType VT2, MVT::ValueType VT3,
2442 SDOperand Op1, SDOperand Op2,
2443 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2444 std::vector<MVT::ValueType> ResultTys;
2445 ResultTys.push_back(VT1);
2446 ResultTys.push_back(VT2);
2447 ResultTys.push_back(VT3);
2448 std::vector<SDOperand> Ops;
2449 Ops.push_back(Op1);
2450 Ops.push_back(Op2);
2451 Ops.push_back(Op3);
2452 Ops.push_back(Op4);
2453 Ops.push_back(Op5);
2454 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2455}
2456SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2457 MVT::ValueType VT2, MVT::ValueType VT3,
2458 SDOperand Op1, SDOperand Op2,
2459 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2460 SDOperand Op6) {
2461 std::vector<MVT::ValueType> ResultTys;
2462 ResultTys.push_back(VT1);
2463 ResultTys.push_back(VT2);
2464 ResultTys.push_back(VT3);
2465 std::vector<SDOperand> Ops;
2466 Ops.push_back(Op1);
2467 Ops.push_back(Op2);
2468 Ops.push_back(Op3);
2469 Ops.push_back(Op4);
2470 Ops.push_back(Op5);
2471 Ops.push_back(Op6);
2472 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2473}
2474SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2475 MVT::ValueType VT2, MVT::ValueType VT3,
2476 SDOperand Op1, SDOperand Op2,
2477 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2478 SDOperand Op6, SDOperand Op7) {
2479 std::vector<MVT::ValueType> ResultTys;
2480 ResultTys.push_back(VT1);
2481 ResultTys.push_back(VT2);
2482 ResultTys.push_back(VT3);
2483 std::vector<SDOperand> Ops;
2484 Ops.push_back(Op1);
2485 Ops.push_back(Op2);
2486 Ops.push_back(Op3);
2487 Ops.push_back(Op4);
2488 Ops.push_back(Op5);
2489 Ops.push_back(Op6);
2490 Ops.push_back(Op7);
2491 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2492}
2493SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2494 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2495 std::vector<MVT::ValueType> ResultTys;
2496 ResultTys.push_back(VT1);
2497 ResultTys.push_back(VT2);
2498 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2499}
2500
Evan Cheng99157a02006-08-07 22:13:29 +00002501/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002502/// This can cause recursive merging of nodes in the DAG.
2503///
Chris Lattner8b52f212005-08-26 18:36:28 +00002504/// This version assumes From/To have a single result value.
2505///
Chris Lattner1e111c72005-09-07 05:37:01 +00002506void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2507 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002508 SDNode *From = FromN.Val, *To = ToN.Val;
2509 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2510 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002511 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002512
Chris Lattner8b8749f2005-08-17 19:00:20 +00002513 while (!From->use_empty()) {
2514 // Process users until they are all gone.
2515 SDNode *U = *From->use_begin();
2516
2517 // This node is about to morph, remove its old self from the CSE maps.
2518 RemoveNodeFromCSEMaps(U);
2519
Chris Lattner65113b22005-11-08 22:07:03 +00002520 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2521 I != E; ++I)
2522 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002523 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002524 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002525 To->addUser(U);
2526 }
2527
2528 // Now that we have modified U, add it back to the CSE maps. If it already
2529 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002530 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2531 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002532 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002533 if (Deleted) Deleted->push_back(U);
2534 DeleteNodeNotInCSEMaps(U);
2535 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002536 }
2537}
2538
2539/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2540/// This can cause recursive merging of nodes in the DAG.
2541///
2542/// This version assumes From/To have matching types and numbers of result
2543/// values.
2544///
Chris Lattner1e111c72005-09-07 05:37:01 +00002545void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2546 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002547 assert(From != To && "Cannot replace uses of with self");
2548 assert(From->getNumValues() == To->getNumValues() &&
2549 "Cannot use this version of ReplaceAllUsesWith!");
2550 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002551 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002552 return;
2553 }
2554
2555 while (!From->use_empty()) {
2556 // Process users until they are all gone.
2557 SDNode *U = *From->use_begin();
2558
2559 // This node is about to morph, remove its old self from the CSE maps.
2560 RemoveNodeFromCSEMaps(U);
2561
Chris Lattner65113b22005-11-08 22:07:03 +00002562 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2563 I != E; ++I)
2564 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002565 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002566 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002567 To->addUser(U);
2568 }
2569
2570 // Now that we have modified U, add it back to the CSE maps. If it already
2571 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002572 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2573 ReplaceAllUsesWith(U, Existing, Deleted);
2574 // U is now dead.
2575 if (Deleted) Deleted->push_back(U);
2576 DeleteNodeNotInCSEMaps(U);
2577 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002578 }
2579}
2580
Chris Lattner8b52f212005-08-26 18:36:28 +00002581/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2582/// This can cause recursive merging of nodes in the DAG.
2583///
2584/// This version can replace From with any result values. To must match the
2585/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002586void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002587 const std::vector<SDOperand> &To,
2588 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002589 assert(From->getNumValues() == To.size() &&
2590 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002591 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002592 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002593 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002594 return;
2595 }
2596
2597 while (!From->use_empty()) {
2598 // Process users until they are all gone.
2599 SDNode *U = *From->use_begin();
2600
2601 // This node is about to morph, remove its old self from the CSE maps.
2602 RemoveNodeFromCSEMaps(U);
2603
Chris Lattner65113b22005-11-08 22:07:03 +00002604 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2605 I != E; ++I)
2606 if (I->Val == From) {
2607 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002608 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002609 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002610 ToOp.Val->addUser(U);
2611 }
2612
2613 // Now that we have modified U, add it back to the CSE maps. If it already
2614 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002615 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2616 ReplaceAllUsesWith(U, Existing, Deleted);
2617 // U is now dead.
2618 if (Deleted) Deleted->push_back(U);
2619 DeleteNodeNotInCSEMaps(U);
2620 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002621 }
2622}
2623
Chris Lattner012f2412006-02-17 21:58:01 +00002624/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2625/// uses of other values produced by From.Val alone. The Deleted vector is
2626/// handled the same was as for ReplaceAllUsesWith.
2627void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2628 std::vector<SDNode*> &Deleted) {
2629 assert(From != To && "Cannot replace a value with itself");
2630 // Handle the simple, trivial, case efficiently.
2631 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2632 ReplaceAllUsesWith(From, To, &Deleted);
2633 return;
2634 }
2635
2636 // Get all of the users in a nice, deterministically ordered, uniqued set.
2637 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2638
2639 while (!Users.empty()) {
2640 // We know that this user uses some value of From. If it is the right
2641 // value, update it.
2642 SDNode *User = Users.back();
2643 Users.pop_back();
2644
2645 for (SDOperand *Op = User->OperandList,
2646 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2647 if (*Op == From) {
2648 // Okay, we know this user needs to be updated. Remove its old self
2649 // from the CSE maps.
2650 RemoveNodeFromCSEMaps(User);
2651
2652 // Update all operands that match "From".
2653 for (; Op != E; ++Op) {
2654 if (*Op == From) {
2655 From.Val->removeUser(User);
2656 *Op = To;
2657 To.Val->addUser(User);
2658 }
2659 }
2660
2661 // Now that we have modified User, add it back to the CSE maps. If it
2662 // already exists there, recursively merge the results together.
2663 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2664 unsigned NumDeleted = Deleted.size();
2665 ReplaceAllUsesWith(User, Existing, &Deleted);
2666
2667 // User is now dead.
2668 Deleted.push_back(User);
2669 DeleteNodeNotInCSEMaps(User);
2670
2671 // We have to be careful here, because ReplaceAllUsesWith could have
2672 // deleted a user of From, which means there may be dangling pointers
2673 // in the "Users" setvector. Scan over the deleted node pointers and
2674 // remove them from the setvector.
2675 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2676 Users.remove(Deleted[i]);
2677 }
2678 break; // Exit the operand scanning loop.
2679 }
2680 }
2681 }
2682}
2683
Chris Lattner7b2880c2005-08-24 22:44:39 +00002684
Evan Chenge6f35d82006-08-01 08:20:41 +00002685/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2686/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002687unsigned SelectionDAG::AssignNodeIds() {
2688 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002689 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2690 SDNode *N = I;
2691 N->setNodeId(Id++);
2692 }
2693 return Id;
2694}
2695
Evan Chenge6f35d82006-08-01 08:20:41 +00002696/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002697/// based on their topological order. It returns the maximum id and a vector
2698/// of the SDNodes* in assigned order by reference.
2699unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002700 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002701 std::vector<unsigned> InDegree(DAGSize);
2702 std::vector<SDNode*> Sources;
2703
2704 // Use a two pass approach to avoid using a std::map which is slow.
2705 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002706 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2707 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002708 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002709 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002710 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002711 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002712 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002713 }
2714
Evan Cheng99157a02006-08-07 22:13:29 +00002715 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002716 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002717 SDNode *N = Sources.back();
2718 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002719 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002720 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2721 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002722 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002723 if (Degree == 0)
2724 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002725 }
2726 }
2727
Evan Chengc384d6c2006-08-02 22:00:34 +00002728 // Second pass, assign the actual topological order as node ids.
2729 Id = 0;
2730 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2731 TI != TE; ++TI)
2732 (*TI)->setNodeId(Id++);
2733
2734 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002735}
2736
2737
Evan Cheng091cba12006-07-27 06:39:06 +00002738
Jim Laskey58b968b2005-08-17 20:08:02 +00002739//===----------------------------------------------------------------------===//
2740// SDNode Class
2741//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002742
Chris Lattner917d2c92006-07-19 00:00:37 +00002743// Out-of-line virtual method to give class a home.
2744void SDNode::ANCHOR() {
2745}
2746
Chris Lattnera3255112005-11-08 23:30:28 +00002747
2748/// getValueTypeList - Return a pointer to the specified value type.
2749///
2750MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2751 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2752 VTs[VT] = VT;
2753 return &VTs[VT];
2754}
2755
Chris Lattner5c884562005-01-12 18:37:47 +00002756/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2757/// indicated value. This method ignores uses of other values defined by this
2758/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002759bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002760 assert(Value < getNumValues() && "Bad value!");
2761
2762 // If there is only one value, this is easy.
2763 if (getNumValues() == 1)
2764 return use_size() == NUses;
2765 if (Uses.size() < NUses) return false;
2766
Evan Cheng4ee62112006-02-05 06:29:23 +00002767 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002768
2769 std::set<SDNode*> UsersHandled;
2770
Evan Cheng4ee62112006-02-05 06:29:23 +00002771 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002772 UI != E; ++UI) {
2773 SDNode *User = *UI;
2774 if (User->getNumOperands() == 1 ||
2775 UsersHandled.insert(User).second) // First time we've seen this?
2776 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2777 if (User->getOperand(i) == TheValue) {
2778 if (NUses == 0)
2779 return false; // too many uses
2780 --NUses;
2781 }
2782 }
2783
2784 // Found exactly the right number of uses?
2785 return NUses == 0;
2786}
2787
2788
Evan Cheng4ee62112006-02-05 06:29:23 +00002789// isOnlyUse - Return true if this node is the only use of N.
2790bool SDNode::isOnlyUse(SDNode *N) const {
2791 bool Seen = false;
2792 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2793 SDNode *User = *I;
2794 if (User == this)
2795 Seen = true;
2796 else
2797 return false;
2798 }
2799
2800 return Seen;
2801}
2802
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002803// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002804bool SDOperand::isOperand(SDNode *N) const {
2805 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2806 if (*this == N->getOperand(i))
2807 return true;
2808 return false;
2809}
2810
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002811bool SDNode::isOperand(SDNode *N) const {
2812 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2813 if (this == N->OperandList[i].Val)
2814 return true;
2815 return false;
2816}
Evan Cheng4ee62112006-02-05 06:29:23 +00002817
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002818const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002819 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002820 default:
2821 if (getOpcode() < ISD::BUILTIN_OP_END)
2822 return "<<Unknown DAG Node>>";
2823 else {
Evan Cheng72261582005-12-20 06:22:03 +00002824 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002825 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002826 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2827 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002828
Evan Cheng72261582005-12-20 06:22:03 +00002829 TargetLowering &TLI = G->getTargetLoweringInfo();
2830 const char *Name =
2831 TLI.getTargetNodeName(getOpcode());
2832 if (Name) return Name;
2833 }
2834
2835 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002836 }
2837
Andrew Lenharth95762122005-03-31 21:24:06 +00002838 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002839 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002840 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002841 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002842 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002843 case ISD::AssertSext: return "AssertSext";
2844 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002845
2846 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002847 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002848 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002849 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002850
2851 case ISD::Constant: return "Constant";
2852 case ISD::ConstantFP: return "ConstantFP";
2853 case ISD::GlobalAddress: return "GlobalAddress";
2854 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002855 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002856 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002857 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002858 case ISD::INTRINSIC_WO_CHAIN: {
2859 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2860 return Intrinsic::getName((Intrinsic::ID)IID);
2861 }
2862 case ISD::INTRINSIC_VOID:
2863 case ISD::INTRINSIC_W_CHAIN: {
2864 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002865 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002866 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002867
Chris Lattnerb2827b02006-03-19 00:52:58 +00002868 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002869 case ISD::TargetConstant: return "TargetConstant";
2870 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002871 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2872 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002873 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002874 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002875 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002876
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002877 case ISD::CopyToReg: return "CopyToReg";
2878 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002879 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002880 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002881 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002882 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002883 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002884 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002885
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002886 // Unary operators
2887 case ISD::FABS: return "fabs";
2888 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002889 case ISD::FSQRT: return "fsqrt";
2890 case ISD::FSIN: return "fsin";
2891 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002892
2893 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002894 case ISD::ADD: return "add";
2895 case ISD::SUB: return "sub";
2896 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002897 case ISD::MULHU: return "mulhu";
2898 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002899 case ISD::SDIV: return "sdiv";
2900 case ISD::UDIV: return "udiv";
2901 case ISD::SREM: return "srem";
2902 case ISD::UREM: return "urem";
2903 case ISD::AND: return "and";
2904 case ISD::OR: return "or";
2905 case ISD::XOR: return "xor";
2906 case ISD::SHL: return "shl";
2907 case ISD::SRA: return "sra";
2908 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002909 case ISD::ROTL: return "rotl";
2910 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002911 case ISD::FADD: return "fadd";
2912 case ISD::FSUB: return "fsub";
2913 case ISD::FMUL: return "fmul";
2914 case ISD::FDIV: return "fdiv";
2915 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002916 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002917 case ISD::VADD: return "vadd";
2918 case ISD::VSUB: return "vsub";
2919 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002920 case ISD::VSDIV: return "vsdiv";
2921 case ISD::VUDIV: return "vudiv";
2922 case ISD::VAND: return "vand";
2923 case ISD::VOR: return "vor";
2924 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002925
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002926 case ISD::SETCC: return "setcc";
2927 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002928 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002929 case ISD::VSELECT: return "vselect";
2930 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2931 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2932 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002933 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002934 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2935 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2936 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2937 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2938 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002939 case ISD::ADDC: return "addc";
2940 case ISD::ADDE: return "adde";
2941 case ISD::SUBC: return "subc";
2942 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002943 case ISD::SHL_PARTS: return "shl_parts";
2944 case ISD::SRA_PARTS: return "sra_parts";
2945 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002946
Chris Lattner7f644642005-04-28 21:44:03 +00002947 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002948 case ISD::SIGN_EXTEND: return "sign_extend";
2949 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002950 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002951 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002952 case ISD::TRUNCATE: return "truncate";
2953 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002954 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002955 case ISD::FP_EXTEND: return "fp_extend";
2956
2957 case ISD::SINT_TO_FP: return "sint_to_fp";
2958 case ISD::UINT_TO_FP: return "uint_to_fp";
2959 case ISD::FP_TO_SINT: return "fp_to_sint";
2960 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002961 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002962
2963 // Control flow instructions
2964 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002965 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002966 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002967 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002968 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002969 case ISD::CALLSEQ_START: return "callseq_start";
2970 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002971
2972 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002973 case ISD::LOAD: return "load";
2974 case ISD::STORE: return "store";
2975 case ISD::VLOAD: return "vload";
2976 case ISD::EXTLOAD: return "extload";
2977 case ISD::SEXTLOAD: return "sextload";
2978 case ISD::ZEXTLOAD: return "zextload";
2979 case ISD::TRUNCSTORE: return "truncstore";
2980 case ISD::VAARG: return "vaarg";
2981 case ISD::VACOPY: return "vacopy";
2982 case ISD::VAEND: return "vaend";
2983 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002984 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002985 case ISD::EXTRACT_ELEMENT: return "extract_element";
2986 case ISD::BUILD_PAIR: return "build_pair";
2987 case ISD::STACKSAVE: return "stacksave";
2988 case ISD::STACKRESTORE: return "stackrestore";
2989
2990 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002991 case ISD::MEMSET: return "memset";
2992 case ISD::MEMCPY: return "memcpy";
2993 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002994
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002995 // Bit manipulation
2996 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002997 case ISD::CTPOP: return "ctpop";
2998 case ISD::CTTZ: return "cttz";
2999 case ISD::CTLZ: return "ctlz";
3000
Chris Lattner36ce6912005-11-29 06:21:05 +00003001 // Debug info
3002 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003003 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00003004 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00003005
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003006 case ISD::CONDCODE:
3007 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003008 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003009 case ISD::SETOEQ: return "setoeq";
3010 case ISD::SETOGT: return "setogt";
3011 case ISD::SETOGE: return "setoge";
3012 case ISD::SETOLT: return "setolt";
3013 case ISD::SETOLE: return "setole";
3014 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003015
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003016 case ISD::SETO: return "seto";
3017 case ISD::SETUO: return "setuo";
3018 case ISD::SETUEQ: return "setue";
3019 case ISD::SETUGT: return "setugt";
3020 case ISD::SETUGE: return "setuge";
3021 case ISD::SETULT: return "setult";
3022 case ISD::SETULE: return "setule";
3023 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003024
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003025 case ISD::SETEQ: return "seteq";
3026 case ISD::SETGT: return "setgt";
3027 case ISD::SETGE: return "setge";
3028 case ISD::SETLT: return "setlt";
3029 case ISD::SETLE: return "setle";
3030 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003031 }
3032 }
3033}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003034
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003035void SDNode::dump() const { dump(0); }
3036void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003037 std::cerr << (void*)this << ": ";
3038
3039 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3040 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003041 if (getValueType(i) == MVT::Other)
3042 std::cerr << "ch";
3043 else
3044 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003045 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003046 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003047
3048 std::cerr << " ";
3049 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3050 if (i) std::cerr << ", ";
3051 std::cerr << (void*)getOperand(i).Val;
3052 if (unsigned RN = getOperand(i).ResNo)
3053 std::cerr << ":" << RN;
3054 }
3055
3056 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3057 std::cerr << "<" << CSDN->getValue() << ">";
3058 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3059 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003060 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003061 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003062 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00003063 std::cerr << "<";
3064 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003065 if (offset > 0)
3066 std::cerr << " + " << offset;
3067 else
3068 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003069 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003070 std::cerr << "<" << FIDN->getIndex() << ">";
3071 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003072 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00003073 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003074 if (offset > 0)
3075 std::cerr << " + " << offset;
3076 else
3077 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003078 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003079 std::cerr << "<";
3080 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3081 if (LBB)
3082 std::cerr << LBB->getName() << " ";
3083 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003084 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003085 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00003086 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3087 } else {
3088 std::cerr << " #" << R->getReg();
3089 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003090 } else if (const ExternalSymbolSDNode *ES =
3091 dyn_cast<ExternalSymbolSDNode>(this)) {
3092 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003093 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3094 if (M->getValue())
3095 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3096 else
3097 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003098 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3099 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003100 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003101}
3102
Chris Lattnerde202b32005-11-09 23:47:37 +00003103static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003104 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3105 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003106 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003107 else
3108 std::cerr << "\n" << std::string(indent+2, ' ')
3109 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003110
Chris Lattnerea946cd2005-01-09 20:38:33 +00003111
3112 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003113 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003114}
3115
Chris Lattnerc3aae252005-01-07 07:46:32 +00003116void SelectionDAG::dump() const {
3117 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003118 std::vector<const SDNode*> Nodes;
3119 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3120 I != E; ++I)
3121 Nodes.push_back(I);
3122
Chris Lattner49d24712005-01-09 20:26:36 +00003123 std::sort(Nodes.begin(), Nodes.end());
3124
3125 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003126 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003127 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003128 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003129
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003130 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003131
Chris Lattnerc3aae252005-01-07 07:46:32 +00003132 std::cerr << "\n\n";
3133}
3134
Evan Chengfae9f1c2006-02-09 22:11:03 +00003135/// InsertISelMapEntry - A helper function to insert a key / element pair
3136/// into a SDOperand to SDOperand map. This is added to avoid the map
3137/// insertion operator from being inlined.
3138void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3139 SDNode *Key, unsigned KeyResNo,
3140 SDNode *Element, unsigned ElementResNo) {
3141 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3142 SDOperand(Element, ElementResNo)));
3143}