blob: 0a6c43e62682b18f7340dc1c12be7f6d1b36017c [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();
477 assert(0 && "Node is not in map!");
478 }
479#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000480}
481
Chris Lattner8b8749f2005-08-17 19:00:20 +0000482/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
483/// has been taken out and modified in some way. If the specified node already
484/// exists in the CSE maps, do not modify the maps, but return the existing node
485/// instead. If it doesn't exist, add it and return null.
486///
487SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
488 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000489 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000490 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000491
Chris Lattner9f8cc692005-12-19 22:21:21 +0000492 // Check that remaining values produced are not flags.
493 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
494 if (N->getValueType(i) == MVT::Flag)
495 return 0; // Never CSE anything that produces a flag.
496
Chris Lattnerfe14b342005-12-01 23:14:50 +0000497 if (N->getNumValues() == 1) {
498 if (N->getNumOperands() == 1) {
499 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
500 std::make_pair(N->getOperand(0),
501 N->getValueType(0)))];
502 if (U) return U;
503 U = N;
504 } else if (N->getNumOperands() == 2) {
505 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
506 std::make_pair(N->getOperand(0),
507 N->getOperand(1)))];
508 if (B) return B;
509 B = N;
510 } else {
511 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
512 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000513 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000514 if (ORN) return ORN;
515 ORN = N;
516 }
517 } else {
518 if (N->getOpcode() == ISD::LOAD) {
519 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
520 std::make_pair(N->getOperand(0),
521 N->getValueType(0)))];
522 if (L) return L;
523 L = N;
524 } else {
525 // Remove the node from the ArbitraryNodes map.
526 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
527 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
528 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
529 std::make_pair(RV, Ops))];
530 if (AN) return AN;
531 AN = N;
532 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000533 }
534 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000535}
536
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000537/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
538/// were replaced with those specified. If this node is never memoized,
539/// return null, otherwise return a pointer to the slot it would take. If a
540/// node already exists with these operands, the slot will be non-null.
541SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000542 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000543 return 0; // Never add these nodes.
544
545 // Check that remaining values produced are not flags.
546 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
547 if (N->getValueType(i) == MVT::Flag)
548 return 0; // Never CSE anything that produces a flag.
549
550 if (N->getNumValues() == 1) {
551 return &UnaryOps[std::make_pair(N->getOpcode(),
552 std::make_pair(Op, N->getValueType(0)))];
553 } else {
554 // Remove the node from the ArbitraryNodes map.
555 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
556 std::vector<SDOperand> Ops;
557 Ops.push_back(Op);
558 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
559 std::make_pair(RV, Ops))];
560 }
561 return 0;
562}
563
564/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
565/// were replaced with those specified. If this node is never memoized,
566/// return null, otherwise return a pointer to the slot it would take. If a
567/// node already exists with these operands, the slot will be non-null.
568SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
569 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000570 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000571 return 0; // Never add these nodes.
572
573 // Check that remaining values produced are not flags.
574 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
575 if (N->getValueType(i) == MVT::Flag)
576 return 0; // Never CSE anything that produces a flag.
577
578 if (N->getNumValues() == 1) {
579 return &BinaryOps[std::make_pair(N->getOpcode(),
580 std::make_pair(Op1, Op2))];
581 } else {
582 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
583 std::vector<SDOperand> Ops;
584 Ops.push_back(Op1);
585 Ops.push_back(Op2);
586 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
587 std::make_pair(RV, Ops))];
588 }
589 return 0;
590}
591
592
593/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
594/// were replaced with those specified. If this node is never memoized,
595/// return null, otherwise return a pointer to the slot it would take. If a
596/// node already exists with these operands, the slot will be non-null.
597SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
598 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000599 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000600 return 0; // Never add these nodes.
601
602 // Check that remaining values produced are not flags.
603 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
604 if (N->getValueType(i) == MVT::Flag)
605 return 0; // Never CSE anything that produces a flag.
606
607 if (N->getNumValues() == 1) {
608 if (N->getNumOperands() == 1) {
609 return &UnaryOps[std::make_pair(N->getOpcode(),
610 std::make_pair(Ops[0],
611 N->getValueType(0)))];
612 } else if (N->getNumOperands() == 2) {
613 return &BinaryOps[std::make_pair(N->getOpcode(),
614 std::make_pair(Ops[0], Ops[1]))];
615 } else {
616 return &OneResultNodes[std::make_pair(N->getOpcode(),
617 std::make_pair(N->getValueType(0),
618 Ops))];
619 }
620 } else {
621 if (N->getOpcode() == ISD::LOAD) {
622 return &Loads[std::make_pair(Ops[1],
623 std::make_pair(Ops[0], N->getValueType(0)))];
624 } else {
625 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
626 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
627 std::make_pair(RV, Ops))];
628 }
629 }
630 return 0;
631}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000632
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000633
Chris Lattner78ec3112003-08-11 14:57:33 +0000634SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000635 while (!AllNodes.empty()) {
636 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000637 delete [] N->OperandList;
638 N->OperandList = 0;
639 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000640 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000641 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000642}
643
Chris Lattner0f2287b2005-04-13 02:38:18 +0000644SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000645 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000646 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000647 return getNode(ISD::AND, Op.getValueType(), Op,
648 getConstant(Imm, Op.getValueType()));
649}
650
Chris Lattnerc3aae252005-01-07 07:46:32 +0000651SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
652 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000653 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
654
Chris Lattnerc3aae252005-01-07 07:46:32 +0000655 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000656 if (VT != MVT::i64)
657 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000658
Chris Lattnerc3aae252005-01-07 07:46:32 +0000659 SDNode *&N = Constants[std::make_pair(Val, VT)];
660 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000661 N = new ConstantSDNode(false, Val, VT);
662 AllNodes.push_back(N);
663 return SDOperand(N, 0);
664}
665
Chris Lattner36ce6912005-11-29 06:21:05 +0000666SDOperand SelectionDAG::getString(const std::string &Val) {
667 StringSDNode *&N = StringNodes[Val];
668 if (!N) {
669 N = new StringSDNode(Val);
670 AllNodes.push_back(N);
671 }
672 return SDOperand(N, 0);
673}
674
Chris Lattner37bfbb42005-08-17 00:34:06 +0000675SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
676 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
677 // Mask out any bits that are not valid for this constant.
678 if (VT != MVT::i64)
679 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
680
681 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
682 if (N) return SDOperand(N, 0);
683 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000684 AllNodes.push_back(N);
685 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000686}
687
Chris Lattnerc3aae252005-01-07 07:46:32 +0000688SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
689 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
690 if (VT == MVT::f32)
691 Val = (float)Val; // Mask out extra precision.
692
Chris Lattnerd8658612005-02-17 20:17:32 +0000693 // Do the map lookup using the actual bit pattern for the floating point
694 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
695 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000696 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000697 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000698 N = new ConstantFPSDNode(false, Val, VT);
699 AllNodes.push_back(N);
700 return SDOperand(N, 0);
701}
702
703SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
704 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
705 if (VT == MVT::f32)
706 Val = (float)Val; // Mask out extra precision.
707
708 // Do the map lookup using the actual bit pattern for the floating point
709 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
710 // we don't have issues with SNANs.
711 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
712 if (N) return SDOperand(N, 0);
713 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000714 AllNodes.push_back(N);
715 return SDOperand(N, 0);
716}
717
Chris Lattnerc3aae252005-01-07 07:46:32 +0000718SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000719 MVT::ValueType VT, int offset) {
720 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000721 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000722 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000723 AllNodes.push_back(N);
724 return SDOperand(N, 0);
725}
726
727SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000728 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000729 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000730 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000731 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000732 AllNodes.push_back(N);
733 return SDOperand(N, 0);
734}
735
736SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
737 SDNode *&N = FrameIndices[FI];
738 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000739 N = new FrameIndexSDNode(FI, VT, false);
740 AllNodes.push_back(N);
741 return SDOperand(N, 0);
742}
743
744SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
745 SDNode *&N = TargetFrameIndices[FI];
746 if (N) return SDOperand(N, 0);
747 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000748 AllNodes.push_back(N);
749 return SDOperand(N, 0);
750}
751
Nate Begeman37efe672006-04-22 18:53:45 +0000752SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
753 SDNode *&N = JumpTableIndices[JTI];
754 if (N) return SDOperand(N, 0);
755 N = new JumpTableSDNode(JTI, VT, false);
756 AllNodes.push_back(N);
757 return SDOperand(N, 0);
758}
759
760SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
761 SDNode *&N = TargetJumpTableIndices[JTI];
762 if (N) return SDOperand(N, 0);
763 N = new JumpTableSDNode(JTI, VT, true);
764 AllNodes.push_back(N);
765 return SDOperand(N, 0);
766}
767
Evan Chengb8973bd2006-01-31 22:23:14 +0000768SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000769 unsigned Alignment, int Offset) {
770 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
771 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000772 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000773 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000774 AllNodes.push_back(N);
775 return SDOperand(N, 0);
776}
777
Evan Chengb8973bd2006-01-31 22:23:14 +0000778SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000779 unsigned Alignment, int Offset) {
780 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
781 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000782 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000783 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784 AllNodes.push_back(N);
785 return SDOperand(N, 0);
786}
787
788SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
789 SDNode *&N = BBNodes[MBB];
790 if (N) return SDOperand(N, 0);
791 N = new BasicBlockSDNode(MBB);
792 AllNodes.push_back(N);
793 return SDOperand(N, 0);
794}
795
Chris Lattner15e4b012005-07-10 00:07:11 +0000796SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
797 if ((unsigned)VT >= ValueTypeNodes.size())
798 ValueTypeNodes.resize(VT+1);
799 if (ValueTypeNodes[VT] == 0) {
800 ValueTypeNodes[VT] = new VTSDNode(VT);
801 AllNodes.push_back(ValueTypeNodes[VT]);
802 }
803
804 return SDOperand(ValueTypeNodes[VT], 0);
805}
806
Chris Lattnerc3aae252005-01-07 07:46:32 +0000807SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
808 SDNode *&N = ExternalSymbols[Sym];
809 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000810 N = new ExternalSymbolSDNode(false, Sym, VT);
811 AllNodes.push_back(N);
812 return SDOperand(N, 0);
813}
814
Chris Lattner809ec112006-01-28 10:09:25 +0000815SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
816 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000817 SDNode *&N = TargetExternalSymbols[Sym];
818 if (N) return SDOperand(N, 0);
819 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820 AllNodes.push_back(N);
821 return SDOperand(N, 0);
822}
823
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000824SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
825 if ((unsigned)Cond >= CondCodeNodes.size())
826 CondCodeNodes.resize(Cond+1);
827
Chris Lattner079a27a2005-08-09 20:40:02 +0000828 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000829 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000830 AllNodes.push_back(CondCodeNodes[Cond]);
831 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000832 return SDOperand(CondCodeNodes[Cond], 0);
833}
834
Chris Lattner0fdd7682005-08-30 22:38:38 +0000835SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
836 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
837 if (!Reg) {
838 Reg = new RegisterSDNode(RegNo, VT);
839 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000840 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000841 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000842}
843
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000844SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
845 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000846 // These setcc operations always fold.
847 switch (Cond) {
848 default: break;
849 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000850 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000851 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000852 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000853
854 case ISD::SETOEQ:
855 case ISD::SETOGT:
856 case ISD::SETOGE:
857 case ISD::SETOLT:
858 case ISD::SETOLE:
859 case ISD::SETONE:
860 case ISD::SETO:
861 case ISD::SETUO:
862 case ISD::SETUEQ:
863 case ISD::SETUNE:
864 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
865 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000866 }
867
Chris Lattner67255a12005-04-07 18:14:58 +0000868 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
869 uint64_t C2 = N2C->getValue();
870 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
871 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000872
Chris Lattnerc3aae252005-01-07 07:46:32 +0000873 // Sign extend the operands if required
874 if (ISD::isSignedIntSetCC(Cond)) {
875 C1 = N1C->getSignExtended();
876 C2 = N2C->getSignExtended();
877 }
878
879 switch (Cond) {
880 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000881 case ISD::SETEQ: return getConstant(C1 == C2, VT);
882 case ISD::SETNE: return getConstant(C1 != C2, VT);
883 case ISD::SETULT: return getConstant(C1 < C2, VT);
884 case ISD::SETUGT: return getConstant(C1 > C2, VT);
885 case ISD::SETULE: return getConstant(C1 <= C2, VT);
886 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
887 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
888 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
889 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
890 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000891 }
Chris Lattner24673922005-04-07 18:58:54 +0000892 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000893 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000894 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
895 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
896
897 // If the comparison constant has bits in the upper part, the
898 // zero-extended value could never match.
899 if (C2 & (~0ULL << InSize)) {
900 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
901 switch (Cond) {
902 case ISD::SETUGT:
903 case ISD::SETUGE:
904 case ISD::SETEQ: return getConstant(0, VT);
905 case ISD::SETULT:
906 case ISD::SETULE:
907 case ISD::SETNE: return getConstant(1, VT);
908 case ISD::SETGT:
909 case ISD::SETGE:
910 // True if the sign bit of C2 is set.
911 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
912 case ISD::SETLT:
913 case ISD::SETLE:
914 // True if the sign bit of C2 isn't set.
915 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
916 default:
917 break;
918 }
919 }
920
921 // Otherwise, we can perform the comparison with the low bits.
922 switch (Cond) {
923 case ISD::SETEQ:
924 case ISD::SETNE:
925 case ISD::SETUGT:
926 case ISD::SETUGE:
927 case ISD::SETULT:
928 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000929 return getSetCC(VT, N1.getOperand(0),
930 getConstant(C2, N1.getOperand(0).getValueType()),
931 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000932 default:
933 break; // todo, be more careful with signed comparisons
934 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000935 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
936 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
937 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
938 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
939 MVT::ValueType ExtDstTy = N1.getValueType();
940 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000941
Chris Lattner7b2880c2005-08-24 22:44:39 +0000942 // If the extended part has any inconsistent bits, it cannot ever
943 // compare equal. In other words, they have to be all ones or all
944 // zeros.
945 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000946 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000947 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
948 return getConstant(Cond == ISD::SETNE, VT);
949
950 // Otherwise, make this a use of a zext.
951 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000952 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000953 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000954 }
955
Chris Lattner67255a12005-04-07 18:14:58 +0000956 uint64_t MinVal, MaxVal;
957 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
958 if (ISD::isSignedIntSetCC(Cond)) {
959 MinVal = 1ULL << (OperandBitSize-1);
960 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
961 MaxVal = ~0ULL >> (65-OperandBitSize);
962 else
963 MaxVal = 0;
964 } else {
965 MinVal = 0;
966 MaxVal = ~0ULL >> (64-OperandBitSize);
967 }
968
969 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
970 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
971 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
972 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000973 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
974 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000975 }
976
977 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
978 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
979 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000980 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
981 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000982 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000983
Nate Begeman72ea2812005-04-14 08:56:52 +0000984 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
985 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000986
Nate Begeman72ea2812005-04-14 08:56:52 +0000987 // Canonicalize setgt X, Min --> setne X, Min
988 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000989 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000990
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000991 // If we have setult X, 1, turn it into seteq X, 0
992 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000993 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
994 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000995 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000996 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000997 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
998 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000999
1000 // If we have "setcc X, C1", check to see if we can shrink the immediate
1001 // by changing cc.
1002
1003 // SETUGT X, SINTMAX -> SETLT X, 0
1004 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1005 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001006 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +00001007
1008 // FIXME: Implement the rest of these.
1009
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001010
1011 // Fold bit comparisons when we can.
1012 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1013 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
1014 if (ConstantSDNode *AndRHS =
1015 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1016 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1017 // Perform the xform if the AND RHS is a single bit.
1018 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1019 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001020 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001021 TLI.getShiftAmountTy()));
1022 }
1023 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
1024 // (X & 8) == 8 --> (X & 8) >> 3
1025 // Perform the xform if C2 is a single bit.
1026 if ((C2 & (C2-1)) == 0) {
1027 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001028 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001029 }
1030 }
1031 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001032 }
Chris Lattner67255a12005-04-07 18:14:58 +00001033 } else if (isa<ConstantSDNode>(N1.Val)) {
1034 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001035 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +00001036 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001037
1038 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1039 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1040 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001041
Chris Lattnerc3aae252005-01-07 07:46:32 +00001042 switch (Cond) {
1043 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001044 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1045 case ISD::SETNE: return getConstant(C1 != C2, VT);
1046 case ISD::SETLT: return getConstant(C1 < C2, VT);
1047 case ISD::SETGT: return getConstant(C1 > C2, VT);
1048 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1049 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001050 }
1051 } else {
1052 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001053 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054 }
1055
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001056 // Could not fold it.
1057 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001058}
1059
Chris Lattnerc3aae252005-01-07 07:46:32 +00001060/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001061///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001063 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1064 if (!N) {
1065 N = new SDNode(Opcode, VT);
1066 AllNodes.push_back(N);
1067 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001068 return SDOperand(N, 0);
1069}
1070
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1072 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001073 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001074 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1076 uint64_t Val = C->getValue();
1077 switch (Opcode) {
1078 default: break;
1079 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001080 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001081 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1082 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001083 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1084 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001085 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001086 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001087 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001088 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001089 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001090 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001091 case ISD::BSWAP:
1092 switch(VT) {
1093 default: assert(0 && "Invalid bswap!"); break;
1094 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1095 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1096 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1097 }
1098 break;
1099 case ISD::CTPOP:
1100 switch(VT) {
1101 default: assert(0 && "Invalid ctpop!"); break;
1102 case MVT::i1: return getConstant(Val != 0, VT);
1103 case MVT::i8:
1104 Tmp1 = (unsigned)Val & 0xFF;
1105 return getConstant(CountPopulation_32(Tmp1), VT);
1106 case MVT::i16:
1107 Tmp1 = (unsigned)Val & 0xFFFF;
1108 return getConstant(CountPopulation_32(Tmp1), VT);
1109 case MVT::i32:
1110 return getConstant(CountPopulation_32((unsigned)Val), VT);
1111 case MVT::i64:
1112 return getConstant(CountPopulation_64(Val), VT);
1113 }
1114 case ISD::CTLZ:
1115 switch(VT) {
1116 default: assert(0 && "Invalid ctlz!"); break;
1117 case MVT::i1: return getConstant(Val == 0, VT);
1118 case MVT::i8:
1119 Tmp1 = (unsigned)Val & 0xFF;
1120 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1121 case MVT::i16:
1122 Tmp1 = (unsigned)Val & 0xFFFF;
1123 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1124 case MVT::i32:
1125 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1126 case MVT::i64:
1127 return getConstant(CountLeadingZeros_64(Val), VT);
1128 }
1129 case ISD::CTTZ:
1130 switch(VT) {
1131 default: assert(0 && "Invalid cttz!"); break;
1132 case MVT::i1: return getConstant(Val == 0, VT);
1133 case MVT::i8:
1134 Tmp1 = (unsigned)Val | 0x100;
1135 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1136 case MVT::i16:
1137 Tmp1 = (unsigned)Val | 0x10000;
1138 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1139 case MVT::i32:
1140 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1141 case MVT::i64:
1142 return getConstant(CountTrailingZeros_64(Val), VT);
1143 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001144 }
1145 }
1146
Chris Lattner94683772005-12-23 05:30:37 +00001147 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001148 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1149 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001150 case ISD::FNEG:
1151 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001152 case ISD::FABS:
1153 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001154 case ISD::FP_ROUND:
1155 case ISD::FP_EXTEND:
1156 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001157 case ISD::FP_TO_SINT:
1158 return getConstant((int64_t)C->getValue(), VT);
1159 case ISD::FP_TO_UINT:
1160 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001161 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001162 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001163 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001164 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001165 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001166 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001167 }
1168
1169 unsigned OpOpcode = Operand.Val->getOpcode();
1170 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001171 case ISD::TokenFactor:
1172 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001173 case ISD::SIGN_EXTEND:
1174 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001175 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001176 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1177 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1178 break;
1179 case ISD::ZERO_EXTEND:
1180 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001181 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001182 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001183 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001184 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001185 case ISD::ANY_EXTEND:
1186 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001187 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001188 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1189 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1190 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1191 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001192 case ISD::TRUNCATE:
1193 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001194 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195 if (OpOpcode == ISD::TRUNCATE)
1196 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001197 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1198 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001199 // If the source is smaller than the dest, we still need an extend.
1200 if (Operand.Val->getOperand(0).getValueType() < VT)
1201 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1202 else if (Operand.Val->getOperand(0).getValueType() > VT)
1203 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1204 else
1205 return Operand.Val->getOperand(0);
1206 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001207 break;
Chris Lattner35481892005-12-23 00:16:34 +00001208 case ISD::BIT_CONVERT:
1209 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001210 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001211 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001212 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001213 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1214 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001215 if (OpOpcode == ISD::UNDEF)
1216 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001217 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001218 case ISD::SCALAR_TO_VECTOR:
1219 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1220 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1221 "Illegal SCALAR_TO_VECTOR node!");
1222 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001223 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001224 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1225 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001226 Operand.Val->getOperand(0));
1227 if (OpOpcode == ISD::FNEG) // --X -> X
1228 return Operand.Val->getOperand(0);
1229 break;
1230 case ISD::FABS:
1231 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1232 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1233 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001234 }
1235
Chris Lattner43247a12005-08-25 19:12:10 +00001236 SDNode *N;
1237 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1238 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001239 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001240 E = N = new SDNode(Opcode, Operand);
1241 } else {
1242 N = new SDNode(Opcode, Operand);
1243 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001244 N->setValueTypes(VT);
1245 AllNodes.push_back(N);
1246 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001247}
1248
Chris Lattner36019aa2005-04-18 03:48:41 +00001249
1250
Chris Lattnerc3aae252005-01-07 07:46:32 +00001251SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1252 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001253#ifndef NDEBUG
1254 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001255 case ISD::TokenFactor:
1256 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1257 N2.getValueType() == MVT::Other && "Invalid token factor!");
1258 break;
Chris Lattner76365122005-01-16 02:23:22 +00001259 case ISD::AND:
1260 case ISD::OR:
1261 case ISD::XOR:
1262 case ISD::UDIV:
1263 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001264 case ISD::MULHU:
1265 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001266 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1267 // fall through
1268 case ISD::ADD:
1269 case ISD::SUB:
1270 case ISD::MUL:
1271 case ISD::SDIV:
1272 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001273 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1274 // fall through.
1275 case ISD::FADD:
1276 case ISD::FSUB:
1277 case ISD::FMUL:
1278 case ISD::FDIV:
1279 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001280 assert(N1.getValueType() == N2.getValueType() &&
1281 N1.getValueType() == VT && "Binary operator types must match!");
1282 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001283 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1284 assert(N1.getValueType() == VT &&
1285 MVT::isFloatingPoint(N1.getValueType()) &&
1286 MVT::isFloatingPoint(N2.getValueType()) &&
1287 "Invalid FCOPYSIGN!");
1288 break;
Chris Lattner76365122005-01-16 02:23:22 +00001289 case ISD::SHL:
1290 case ISD::SRA:
1291 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001292 case ISD::ROTL:
1293 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001294 assert(VT == N1.getValueType() &&
1295 "Shift operators return type must be the same as their first arg");
1296 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001297 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001298 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001299 case ISD::FP_ROUND_INREG: {
1300 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1301 assert(VT == N1.getValueType() && "Not an inreg round!");
1302 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1303 "Cannot FP_ROUND_INREG integer types");
1304 assert(EVT <= VT && "Not rounding down!");
1305 break;
1306 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001307 case ISD::AssertSext:
1308 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001309 case ISD::SIGN_EXTEND_INREG: {
1310 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1311 assert(VT == N1.getValueType() && "Not an inreg extend!");
1312 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1313 "Cannot *_EXTEND_INREG FP types");
1314 assert(EVT <= VT && "Not extending!");
1315 }
1316
Chris Lattner76365122005-01-16 02:23:22 +00001317 default: break;
1318 }
1319#endif
1320
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1322 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1323 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001324 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1325 int64_t Val = N1C->getValue();
1326 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1327 Val <<= 64-FromBits;
1328 Val >>= 64-FromBits;
1329 return getConstant(Val, VT);
1330 }
1331
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 if (N2C) {
1333 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1334 switch (Opcode) {
1335 case ISD::ADD: return getConstant(C1 + C2, VT);
1336 case ISD::SUB: return getConstant(C1 - C2, VT);
1337 case ISD::MUL: return getConstant(C1 * C2, VT);
1338 case ISD::UDIV:
1339 if (C2) return getConstant(C1 / C2, VT);
1340 break;
1341 case ISD::UREM :
1342 if (C2) return getConstant(C1 % C2, VT);
1343 break;
1344 case ISD::SDIV :
1345 if (C2) return getConstant(N1C->getSignExtended() /
1346 N2C->getSignExtended(), VT);
1347 break;
1348 case ISD::SREM :
1349 if (C2) return getConstant(N1C->getSignExtended() %
1350 N2C->getSignExtended(), VT);
1351 break;
1352 case ISD::AND : return getConstant(C1 & C2, VT);
1353 case ISD::OR : return getConstant(C1 | C2, VT);
1354 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001355 case ISD::SHL : return getConstant(C1 << C2, VT);
1356 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001357 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001358 case ISD::ROTL :
1359 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1360 VT);
1361 case ISD::ROTR :
1362 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1363 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001364 default: break;
1365 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001366 } else { // Cannonicalize constant to RHS if commutative
1367 if (isCommutativeBinOp(Opcode)) {
1368 std::swap(N1C, N2C);
1369 std::swap(N1, N2);
1370 }
1371 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001372 }
1373
1374 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1375 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001376 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001377 if (N2CFP) {
1378 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1379 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001380 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1381 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1382 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1383 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001384 if (C2) return getConstantFP(C1 / C2, VT);
1385 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001386 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001387 if (C2) return getConstantFP(fmod(C1, C2), VT);
1388 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001389 case ISD::FCOPYSIGN: {
1390 union {
1391 double F;
1392 uint64_t I;
1393 } u1;
1394 union {
1395 double F;
1396 int64_t I;
1397 } u2;
1398 u1.F = C1;
1399 u2.F = C2;
1400 if (u2.I < 0) // Sign bit of RHS set?
1401 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1402 else
1403 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1404 return getConstantFP(u1.F, VT);
1405 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001406 default: break;
1407 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001408 } else { // Cannonicalize constant to RHS if commutative
1409 if (isCommutativeBinOp(Opcode)) {
1410 std::swap(N1CFP, N2CFP);
1411 std::swap(N1, N2);
1412 }
1413 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001414 }
Chris Lattner62b57722006-04-20 05:39:12 +00001415
1416 // Canonicalize an UNDEF to the RHS, even over a constant.
1417 if (N1.getOpcode() == ISD::UNDEF) {
1418 if (isCommutativeBinOp(Opcode)) {
1419 std::swap(N1, N2);
1420 } else {
1421 switch (Opcode) {
1422 case ISD::FP_ROUND_INREG:
1423 case ISD::SIGN_EXTEND_INREG:
1424 case ISD::SUB:
1425 case ISD::FSUB:
1426 case ISD::FDIV:
1427 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001428 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001429 return N1; // fold op(undef, arg2) -> undef
1430 case ISD::UDIV:
1431 case ISD::SDIV:
1432 case ISD::UREM:
1433 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001434 case ISD::SRL:
1435 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001436 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1437 }
1438 }
1439 }
1440
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001441 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001442 if (N2.getOpcode() == ISD::UNDEF) {
1443 switch (Opcode) {
1444 case ISD::ADD:
1445 case ISD::SUB:
1446 case ISD::FADD:
1447 case ISD::FSUB:
1448 case ISD::FMUL:
1449 case ISD::FDIV:
1450 case ISD::FREM:
1451 case ISD::UDIV:
1452 case ISD::SDIV:
1453 case ISD::UREM:
1454 case ISD::SREM:
1455 case ISD::XOR:
1456 return N2; // fold op(arg1, undef) -> undef
1457 case ISD::MUL:
1458 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001459 case ISD::SRL:
1460 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001461 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1462 case ISD::OR:
1463 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001464 case ISD::SRA:
1465 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001466 }
1467 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001468
Chris Lattnerc3aae252005-01-07 07:46:32 +00001469 // Finally, fold operations that do not require constants.
1470 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001471 case ISD::FP_ROUND_INREG:
1472 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1473 break;
1474 case ISD::SIGN_EXTEND_INREG: {
1475 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1476 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001477 break;
1478 }
1479
Nate Begemaneea805e2005-04-13 21:23:31 +00001480 // FIXME: figure out how to safely handle things like
1481 // int foo(int x) { return 1 << (x & 255); }
1482 // int bar() { return foo(256); }
1483#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001484 case ISD::SHL:
1485 case ISD::SRL:
1486 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001487 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001488 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001489 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001490 else if (N2.getOpcode() == ISD::AND)
1491 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1492 // If the and is only masking out bits that cannot effect the shift,
1493 // eliminate the and.
1494 unsigned NumBits = MVT::getSizeInBits(VT);
1495 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1496 return getNode(Opcode, VT, N1, N2.getOperand(0));
1497 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001498 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001499#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001500 }
1501
Chris Lattner27e9b412005-05-11 18:57:39 +00001502 // Memoize this node if possible.
1503 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001504 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001505 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1506 if (BON) return SDOperand(BON, 0);
1507
1508 BON = N = new SDNode(Opcode, N1, N2);
1509 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001510 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001511 }
1512
Chris Lattner3e011362005-05-14 07:45:46 +00001513 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001514 AllNodes.push_back(N);
1515 return SDOperand(N, 0);
1516}
1517
Chris Lattnerc3aae252005-01-07 07:46:32 +00001518SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1519 SDOperand N1, SDOperand N2, SDOperand N3) {
1520 // Perform various simplifications.
1521 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1522 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001523 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001524 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001525 case ISD::SETCC: {
1526 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001527 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001528 if (Simp.Val) return Simp;
1529 break;
1530 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001531 case ISD::SELECT:
1532 if (N1C)
1533 if (N1C->getValue())
1534 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001535 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001536 return N3; // select false, X, Y -> Y
1537
1538 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001539 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001540 case ISD::BRCOND:
1541 if (N2C)
1542 if (N2C->getValue()) // Unconditional branch
1543 return getNode(ISD::BR, MVT::Other, N1, N3);
1544 else
1545 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001546 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001547 case ISD::VECTOR_SHUFFLE:
1548 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1549 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1550 N3.getOpcode() == ISD::BUILD_VECTOR &&
1551 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1552 "Illegal VECTOR_SHUFFLE node!");
1553 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001554 }
1555
Chris Lattner385328c2005-05-14 07:42:29 +00001556 std::vector<SDOperand> Ops;
1557 Ops.reserve(3);
1558 Ops.push_back(N1);
1559 Ops.push_back(N2);
1560 Ops.push_back(N3);
1561
Chris Lattner43247a12005-08-25 19:12:10 +00001562 // Memoize node if it doesn't produce a flag.
1563 SDNode *N;
1564 if (VT != MVT::Flag) {
1565 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1566 if (E) return SDOperand(E, 0);
1567 E = N = new SDNode(Opcode, N1, N2, N3);
1568 } else {
1569 N = new SDNode(Opcode, N1, N2, N3);
1570 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001571 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001572 AllNodes.push_back(N);
1573 return SDOperand(N, 0);
1574}
1575
1576SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001577 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001578 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001579 std::vector<SDOperand> Ops;
1580 Ops.reserve(4);
1581 Ops.push_back(N1);
1582 Ops.push_back(N2);
1583 Ops.push_back(N3);
1584 Ops.push_back(N4);
1585 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001586}
1587
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001588SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1589 SDOperand N1, SDOperand N2, SDOperand N3,
1590 SDOperand N4, SDOperand N5) {
1591 std::vector<SDOperand> Ops;
1592 Ops.reserve(5);
1593 Ops.push_back(N1);
1594 Ops.push_back(N2);
1595 Ops.push_back(N3);
1596 Ops.push_back(N4);
1597 Ops.push_back(N5);
1598 return getNode(Opcode, VT, Ops);
1599}
1600
Evan Cheng7038daf2005-12-10 00:37:58 +00001601SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1602 SDOperand Chain, SDOperand Ptr,
1603 SDOperand SV) {
1604 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1605 if (N) return SDOperand(N, 0);
1606 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1607
1608 // Loads have a token chain.
1609 setNodeValueTypes(N, VT, MVT::Other);
1610 AllNodes.push_back(N);
1611 return SDOperand(N, 0);
1612}
1613
1614SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1615 SDOperand Chain, SDOperand Ptr,
1616 SDOperand SV) {
Evan Cheng7038daf2005-12-10 00:37:58 +00001617 std::vector<SDOperand> Ops;
1618 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001619 Ops.push_back(Chain);
1620 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001621 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001622 Ops.push_back(getConstant(Count, MVT::i32));
1623 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001624 std::vector<MVT::ValueType> VTs;
1625 VTs.reserve(2);
1626 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1627 return getNode(ISD::VLOAD, VTs, Ops);
1628}
1629
1630SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1631 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1632 MVT::ValueType EVT) {
1633 std::vector<SDOperand> Ops;
1634 Ops.reserve(4);
1635 Ops.push_back(Chain);
1636 Ops.push_back(Ptr);
1637 Ops.push_back(SV);
1638 Ops.push_back(getValueType(EVT));
1639 std::vector<MVT::ValueType> VTs;
1640 VTs.reserve(2);
1641 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1642 return getNode(Opcode, VTs, Ops);
1643}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001644
Chris Lattner0437cdd2005-05-09 04:14:13 +00001645SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001646 assert((!V || isa<PointerType>(V->getType())) &&
1647 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001648 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1649 if (N) return SDOperand(N, 0);
1650
1651 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001652 AllNodes.push_back(N);
1653 return SDOperand(N, 0);
1654}
1655
Nate Begemanacc398c2006-01-25 18:21:52 +00001656SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1657 SDOperand Chain, SDOperand Ptr,
1658 SDOperand SV) {
1659 std::vector<SDOperand> Ops;
1660 Ops.reserve(3);
1661 Ops.push_back(Chain);
1662 Ops.push_back(Ptr);
1663 Ops.push_back(SV);
1664 std::vector<MVT::ValueType> VTs;
1665 VTs.reserve(2);
1666 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1667 return getNode(ISD::VAARG, VTs, Ops);
1668}
1669
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001670SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001671 std::vector<SDOperand> &Ops) {
1672 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001673 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001674 case 1: return getNode(Opcode, VT, Ops[0]);
1675 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1676 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001677 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001678 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001679
Chris Lattneref847df2005-04-09 03:27:28 +00001680 switch (Opcode) {
1681 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001682 case ISD::TRUNCSTORE: {
1683 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1684 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1685#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1686 // If this is a truncating store of a constant, convert to the desired type
1687 // and store it instead.
1688 if (isa<Constant>(Ops[0])) {
1689 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1690 if (isa<Constant>(Op))
1691 N1 = Op;
1692 }
1693 // Also for ConstantFP?
1694#endif
1695 if (Ops[0].getValueType() == EVT) // Normal store?
1696 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1697 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1698 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1699 "Can't do FP-INT conversion!");
1700 break;
1701 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001702 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001703 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001704 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1705 "LHS and RHS of condition must have same type!");
1706 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1707 "True and False arms of SelectCC must have same type!");
1708 assert(Ops[2].getValueType() == VT &&
1709 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001710 break;
1711 }
1712 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001713 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001714 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1715 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001716 break;
1717 }
Chris Lattneref847df2005-04-09 03:27:28 +00001718 }
1719
Chris Lattner385328c2005-05-14 07:42:29 +00001720 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001721 SDNode *N;
1722 if (VT != MVT::Flag) {
1723 SDNode *&E =
1724 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1725 if (E) return SDOperand(E, 0);
1726 E = N = new SDNode(Opcode, Ops);
1727 } else {
1728 N = new SDNode(Opcode, Ops);
1729 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001730 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001731 AllNodes.push_back(N);
1732 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001733}
1734
Chris Lattner89c34632005-05-14 06:20:26 +00001735SDOperand SelectionDAG::getNode(unsigned Opcode,
1736 std::vector<MVT::ValueType> &ResultTys,
1737 std::vector<SDOperand> &Ops) {
1738 if (ResultTys.size() == 1)
1739 return getNode(Opcode, ResultTys[0], Ops);
1740
Chris Lattner5f056bf2005-07-10 01:55:33 +00001741 switch (Opcode) {
1742 case ISD::EXTLOAD:
1743 case ISD::SEXTLOAD:
1744 case ISD::ZEXTLOAD: {
1745 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1746 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1747 // If they are asking for an extending load from/to the same thing, return a
1748 // normal load.
1749 if (ResultTys[0] == EVT)
1750 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001751 if (MVT::isVector(ResultTys[0])) {
1752 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1753 "Invalid vector extload!");
1754 } else {
1755 assert(EVT < ResultTys[0] &&
1756 "Should only be an extending load, not truncating!");
1757 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001758 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001759 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001760 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1761 "Cannot convert from FP to Int or Int -> FP!");
1762 break;
1763 }
1764
Chris Lattnere89083a2005-05-14 07:25:05 +00001765 // FIXME: figure out how to safely handle things like
1766 // int foo(int x) { return 1 << (x & 255); }
1767 // int bar() { return foo(256); }
1768#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001769 case ISD::SRA_PARTS:
1770 case ISD::SRL_PARTS:
1771 case ISD::SHL_PARTS:
1772 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001773 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001774 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1775 else if (N3.getOpcode() == ISD::AND)
1776 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1777 // If the and is only masking out bits that cannot effect the shift,
1778 // eliminate the and.
1779 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1780 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1781 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1782 }
1783 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001784#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001785 }
Chris Lattner89c34632005-05-14 06:20:26 +00001786
Chris Lattner43247a12005-08-25 19:12:10 +00001787 // Memoize the node unless it returns a flag.
1788 SDNode *N;
1789 if (ResultTys.back() != MVT::Flag) {
1790 SDNode *&E =
1791 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1792 if (E) return SDOperand(E, 0);
1793 E = N = new SDNode(Opcode, Ops);
1794 } else {
1795 N = new SDNode(Opcode, Ops);
1796 }
Chris Lattnera3255112005-11-08 23:30:28 +00001797 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001798 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001799 return SDOperand(N, 0);
1800}
1801
Chris Lattnera3255112005-11-08 23:30:28 +00001802void SelectionDAG::setNodeValueTypes(SDNode *N,
1803 std::vector<MVT::ValueType> &RetVals) {
1804 switch (RetVals.size()) {
1805 case 0: return;
1806 case 1: N->setValueTypes(RetVals[0]); return;
1807 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1808 default: break;
1809 }
1810
1811 std::list<std::vector<MVT::ValueType> >::iterator I =
1812 std::find(VTList.begin(), VTList.end(), RetVals);
1813 if (I == VTList.end()) {
1814 VTList.push_front(RetVals);
1815 I = VTList.begin();
1816 }
1817
1818 N->setValueTypes(&(*I)[0], I->size());
1819}
1820
1821void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1822 MVT::ValueType VT2) {
1823 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1824 E = VTList.end(); I != E; ++I) {
1825 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1826 N->setValueTypes(&(*I)[0], 2);
1827 return;
1828 }
1829 }
1830 std::vector<MVT::ValueType> V;
1831 V.push_back(VT1);
1832 V.push_back(VT2);
1833 VTList.push_front(V);
1834 N->setValueTypes(&(*VTList.begin())[0], 2);
1835}
1836
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001837/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1838/// specified operands. If the resultant node already exists in the DAG,
1839/// this does not modify the specified node, instead it returns the node that
1840/// already exists. If the resultant node does not exist in the DAG, the
1841/// input node is returned. As a degenerate case, if you specify the same
1842/// input operands as the node already has, the input node is returned.
1843SDOperand SelectionDAG::
1844UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1845 SDNode *N = InN.Val;
1846 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1847
1848 // Check to see if there is no change.
1849 if (Op == N->getOperand(0)) return InN;
1850
1851 // See if the modified node already exists.
1852 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1853 if (NewSlot && *NewSlot)
1854 return SDOperand(*NewSlot, InN.ResNo);
1855
1856 // Nope it doesn't. Remove the node from it's current place in the maps.
1857 if (NewSlot)
1858 RemoveNodeFromCSEMaps(N);
1859
1860 // Now we update the operands.
1861 N->OperandList[0].Val->removeUser(N);
1862 Op.Val->addUser(N);
1863 N->OperandList[0] = Op;
1864
1865 // If this gets put into a CSE map, add it.
1866 if (NewSlot) *NewSlot = N;
1867 return InN;
1868}
1869
1870SDOperand SelectionDAG::
1871UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1872 SDNode *N = InN.Val;
1873 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1874
1875 // Check to see if there is no change.
1876 bool AnyChange = false;
1877 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1878 return InN; // No operands changed, just return the input node.
1879
1880 // See if the modified node already exists.
1881 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1882 if (NewSlot && *NewSlot)
1883 return SDOperand(*NewSlot, InN.ResNo);
1884
1885 // Nope it doesn't. Remove the node from it's current place in the maps.
1886 if (NewSlot)
1887 RemoveNodeFromCSEMaps(N);
1888
1889 // Now we update the operands.
1890 if (N->OperandList[0] != Op1) {
1891 N->OperandList[0].Val->removeUser(N);
1892 Op1.Val->addUser(N);
1893 N->OperandList[0] = Op1;
1894 }
1895 if (N->OperandList[1] != Op2) {
1896 N->OperandList[1].Val->removeUser(N);
1897 Op2.Val->addUser(N);
1898 N->OperandList[1] = Op2;
1899 }
1900
1901 // If this gets put into a CSE map, add it.
1902 if (NewSlot) *NewSlot = N;
1903 return InN;
1904}
1905
1906SDOperand SelectionDAG::
1907UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1908 std::vector<SDOperand> Ops;
1909 Ops.push_back(Op1);
1910 Ops.push_back(Op2);
1911 Ops.push_back(Op3);
1912 return UpdateNodeOperands(N, Ops);
1913}
1914
1915SDOperand SelectionDAG::
1916UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1917 SDOperand Op3, SDOperand Op4) {
1918 std::vector<SDOperand> Ops;
1919 Ops.push_back(Op1);
1920 Ops.push_back(Op2);
1921 Ops.push_back(Op3);
1922 Ops.push_back(Op4);
1923 return UpdateNodeOperands(N, Ops);
1924}
1925
1926SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001927UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1928 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1929 std::vector<SDOperand> Ops;
1930 Ops.push_back(Op1);
1931 Ops.push_back(Op2);
1932 Ops.push_back(Op3);
1933 Ops.push_back(Op4);
1934 Ops.push_back(Op5);
1935 return UpdateNodeOperands(N, Ops);
1936}
1937
1938
1939SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001940UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1941 SDNode *N = InN.Val;
1942 assert(N->getNumOperands() == Ops.size() &&
1943 "Update with wrong number of operands");
1944
1945 // Check to see if there is no change.
1946 unsigned NumOps = Ops.size();
1947 bool AnyChange = false;
1948 for (unsigned i = 0; i != NumOps; ++i) {
1949 if (Ops[i] != N->getOperand(i)) {
1950 AnyChange = true;
1951 break;
1952 }
1953 }
1954
1955 // No operands changed, just return the input node.
1956 if (!AnyChange) return InN;
1957
1958 // See if the modified node already exists.
1959 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1960 if (NewSlot && *NewSlot)
1961 return SDOperand(*NewSlot, InN.ResNo);
1962
1963 // Nope it doesn't. Remove the node from it's current place in the maps.
1964 if (NewSlot)
1965 RemoveNodeFromCSEMaps(N);
1966
1967 // Now we update the operands.
1968 for (unsigned i = 0; i != NumOps; ++i) {
1969 if (N->OperandList[i] != Ops[i]) {
1970 N->OperandList[i].Val->removeUser(N);
1971 Ops[i].Val->addUser(N);
1972 N->OperandList[i] = Ops[i];
1973 }
1974 }
1975
1976 // If this gets put into a CSE map, add it.
1977 if (NewSlot) *NewSlot = N;
1978 return InN;
1979}
1980
1981
1982
Chris Lattner149c58c2005-08-16 18:17:10 +00001983
1984/// SelectNodeTo - These are used for target selectors to *mutate* the
1985/// specified node to have the specified return type, Target opcode, and
1986/// operands. Note that target opcodes are stored as
1987/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001988///
1989/// Note that SelectNodeTo returns the resultant node. If there is already a
1990/// node of the specified opcode and operands, it returns that node instead of
1991/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001992SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1993 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001994 // If an identical node already exists, use it.
1995 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1996 if (ON) return SDOperand(ON, 0);
1997
Chris Lattner7651fa42005-08-24 23:00:29 +00001998 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001999
Chris Lattner7651fa42005-08-24 23:00:29 +00002000 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2001 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002002
2003 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002004 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00002005}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002006
Chris Lattnereb19e402005-11-30 22:45:14 +00002007SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2008 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002009 // If an identical node already exists, use it.
2010 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2011 std::make_pair(Op1, VT))];
2012 if (ON) return SDOperand(ON, 0);
2013
Chris Lattner149c58c2005-08-16 18:17:10 +00002014 RemoveNodeFromCSEMaps(N);
2015 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2016 N->setValueTypes(VT);
2017 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002018
2019 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002020 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002021}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002022
Chris Lattnereb19e402005-11-30 22:45:14 +00002023SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2024 MVT::ValueType VT, SDOperand Op1,
2025 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002026 // If an identical node already exists, use it.
2027 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2028 std::make_pair(Op1, Op2))];
2029 if (ON) return SDOperand(ON, 0);
2030
Chris Lattner149c58c2005-08-16 18:17:10 +00002031 RemoveNodeFromCSEMaps(N);
2032 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2033 N->setValueTypes(VT);
2034 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002035
2036 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002037 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002038}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002039
Chris Lattnereb19e402005-11-30 22:45:14 +00002040SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2041 MVT::ValueType VT, SDOperand Op1,
2042 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002043 // If an identical node already exists, use it.
2044 std::vector<SDOperand> OpList;
2045 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2046 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2047 std::make_pair(VT, OpList))];
2048 if (ON) return SDOperand(ON, 0);
2049
Chris Lattner149c58c2005-08-16 18:17:10 +00002050 RemoveNodeFromCSEMaps(N);
2051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2052 N->setValueTypes(VT);
2053 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002054
2055 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002056 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002057}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002058
Chris Lattnereb19e402005-11-30 22:45:14 +00002059SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2060 MVT::ValueType VT, SDOperand Op1,
2061 SDOperand Op2, SDOperand Op3,
2062 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002063 // If an identical node already exists, use it.
2064 std::vector<SDOperand> OpList;
2065 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2066 OpList.push_back(Op4);
2067 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2068 std::make_pair(VT, OpList))];
2069 if (ON) return SDOperand(ON, 0);
2070
Nate Begeman294a0a12005-08-18 07:30:15 +00002071 RemoveNodeFromCSEMaps(N);
2072 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2073 N->setValueTypes(VT);
2074 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002075
2076 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002077 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00002078}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002079
Chris Lattnereb19e402005-11-30 22:45:14 +00002080SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2081 MVT::ValueType VT, SDOperand Op1,
2082 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2083 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002084 // If an identical node already exists, use it.
2085 std::vector<SDOperand> OpList;
2086 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2087 OpList.push_back(Op4); OpList.push_back(Op5);
2088 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2089 std::make_pair(VT, OpList))];
2090 if (ON) return SDOperand(ON, 0);
2091
Chris Lattner6b09a292005-08-21 18:49:33 +00002092 RemoveNodeFromCSEMaps(N);
2093 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2094 N->setValueTypes(VT);
2095 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002096
2097 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002098 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00002099}
Chris Lattner149c58c2005-08-16 18:17:10 +00002100
Chris Lattnereb19e402005-11-30 22:45:14 +00002101SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2102 MVT::ValueType VT, SDOperand Op1,
2103 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2104 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002105 // If an identical node already exists, use it.
2106 std::vector<SDOperand> OpList;
2107 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2108 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2109 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2110 std::make_pair(VT, OpList))];
2111 if (ON) return SDOperand(ON, 0);
2112
Evan Cheng61ca74b2005-11-30 02:04:11 +00002113 RemoveNodeFromCSEMaps(N);
2114 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2115 N->setValueTypes(VT);
2116 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002117
2118 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002119 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002120}
2121
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002122SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2123 MVT::ValueType VT, SDOperand Op1,
2124 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2125 SDOperand Op5, SDOperand Op6,
2126 SDOperand Op7) {
2127 // If an identical node already exists, use it.
2128 std::vector<SDOperand> OpList;
2129 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2130 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2131 OpList.push_back(Op7);
2132 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2133 std::make_pair(VT, OpList))];
2134 if (ON) return SDOperand(ON, 0);
2135
2136 RemoveNodeFromCSEMaps(N);
2137 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2138 N->setValueTypes(VT);
2139 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2140
2141 ON = N; // Memoize the new node.
2142 return SDOperand(N, 0);
2143}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002144SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2145 MVT::ValueType VT, SDOperand Op1,
2146 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2147 SDOperand Op5, SDOperand Op6,
2148 SDOperand Op7, SDOperand Op8) {
2149 // If an identical node already exists, use it.
2150 std::vector<SDOperand> OpList;
2151 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2152 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2153 OpList.push_back(Op7); OpList.push_back(Op8);
2154 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2155 std::make_pair(VT, OpList))];
2156 if (ON) return SDOperand(ON, 0);
2157
2158 RemoveNodeFromCSEMaps(N);
2159 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2160 N->setValueTypes(VT);
2161 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2162
2163 ON = N; // Memoize the new node.
2164 return SDOperand(N, 0);
2165}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002166
Chris Lattnereb19e402005-11-30 22:45:14 +00002167SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2168 MVT::ValueType VT1, MVT::ValueType VT2,
2169 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002170 // If an identical node already exists, use it.
2171 std::vector<SDOperand> OpList;
2172 OpList.push_back(Op1); OpList.push_back(Op2);
2173 std::vector<MVT::ValueType> VTList;
2174 VTList.push_back(VT1); VTList.push_back(VT2);
2175 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2176 std::make_pair(VTList, OpList))];
2177 if (ON) return SDOperand(ON, 0);
2178
Chris Lattner0fb094f2005-11-19 01:44:53 +00002179 RemoveNodeFromCSEMaps(N);
2180 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2181 setNodeValueTypes(N, VT1, VT2);
2182 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002183
2184 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002185 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002186}
2187
Chris Lattnereb19e402005-11-30 22:45:14 +00002188SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2189 MVT::ValueType VT1, MVT::ValueType VT2,
2190 SDOperand Op1, SDOperand Op2,
2191 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002192 // If an identical node already exists, use it.
2193 std::vector<SDOperand> OpList;
2194 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2195 std::vector<MVT::ValueType> VTList;
2196 VTList.push_back(VT1); VTList.push_back(VT2);
2197 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2198 std::make_pair(VTList, OpList))];
2199 if (ON) return SDOperand(ON, 0);
2200
Chris Lattner0fb094f2005-11-19 01:44:53 +00002201 RemoveNodeFromCSEMaps(N);
2202 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2203 setNodeValueTypes(N, VT1, VT2);
2204 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002205
2206 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002207 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002208}
2209
Chris Lattnereb19e402005-11-30 22:45:14 +00002210SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2211 MVT::ValueType VT1, MVT::ValueType VT2,
2212 SDOperand Op1, SDOperand Op2,
2213 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002214 // If an identical node already exists, use it.
2215 std::vector<SDOperand> OpList;
2216 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2217 OpList.push_back(Op4);
2218 std::vector<MVT::ValueType> VTList;
2219 VTList.push_back(VT1); VTList.push_back(VT2);
2220 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2221 std::make_pair(VTList, OpList))];
2222 if (ON) return SDOperand(ON, 0);
2223
Chris Lattner0fb094f2005-11-19 01:44:53 +00002224 RemoveNodeFromCSEMaps(N);
2225 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2226 setNodeValueTypes(N, VT1, VT2);
2227 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002228
2229 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002230 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002231}
2232
Chris Lattnereb19e402005-11-30 22:45:14 +00002233SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2234 MVT::ValueType VT1, MVT::ValueType VT2,
2235 SDOperand Op1, SDOperand Op2,
2236 SDOperand Op3, SDOperand Op4,
2237 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002238 // If an identical node already exists, use it.
2239 std::vector<SDOperand> OpList;
2240 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2241 OpList.push_back(Op4); OpList.push_back(Op5);
2242 std::vector<MVT::ValueType> VTList;
2243 VTList.push_back(VT1); VTList.push_back(VT2);
2244 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2245 std::make_pair(VTList, OpList))];
2246 if (ON) return SDOperand(ON, 0);
2247
Chris Lattner0fb094f2005-11-19 01:44:53 +00002248 RemoveNodeFromCSEMaps(N);
2249 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2250 setNodeValueTypes(N, VT1, VT2);
2251 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002252
2253 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002254 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002255}
2256
Evan Cheng6ae46c42006-02-09 07:15:23 +00002257/// getTargetNode - These are used for target selectors to create a new node
2258/// with specified return type(s), target opcode, and operands.
2259///
2260/// Note that getTargetNode returns the resultant node. If there is already a
2261/// node of the specified opcode and operands, it returns that node instead of
2262/// the current one.
2263SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2264 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2265}
2266SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2267 SDOperand Op1) {
2268 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2269}
2270SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2271 SDOperand Op1, SDOperand Op2) {
2272 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2273}
2274SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2275 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2276 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2277}
2278SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2279 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2280 SDOperand Op4) {
2281 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2282}
2283SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2284 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2285 SDOperand Op4, SDOperand Op5) {
2286 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2287}
2288SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2289 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2290 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2291 std::vector<SDOperand> Ops;
2292 Ops.reserve(6);
2293 Ops.push_back(Op1);
2294 Ops.push_back(Op2);
2295 Ops.push_back(Op3);
2296 Ops.push_back(Op4);
2297 Ops.push_back(Op5);
2298 Ops.push_back(Op6);
2299 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2300}
2301SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2302 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2303 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2304 SDOperand Op7) {
2305 std::vector<SDOperand> Ops;
2306 Ops.reserve(7);
2307 Ops.push_back(Op1);
2308 Ops.push_back(Op2);
2309 Ops.push_back(Op3);
2310 Ops.push_back(Op4);
2311 Ops.push_back(Op5);
2312 Ops.push_back(Op6);
2313 Ops.push_back(Op7);
2314 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2315}
2316SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2317 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2318 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2319 SDOperand Op7, SDOperand Op8) {
2320 std::vector<SDOperand> Ops;
2321 Ops.reserve(8);
2322 Ops.push_back(Op1);
2323 Ops.push_back(Op2);
2324 Ops.push_back(Op3);
2325 Ops.push_back(Op4);
2326 Ops.push_back(Op5);
2327 Ops.push_back(Op6);
2328 Ops.push_back(Op7);
2329 Ops.push_back(Op8);
2330 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2331}
2332SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2333 std::vector<SDOperand> &Ops) {
2334 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2335}
2336SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2337 MVT::ValueType VT2, SDOperand Op1) {
2338 std::vector<MVT::ValueType> ResultTys;
2339 ResultTys.push_back(VT1);
2340 ResultTys.push_back(VT2);
2341 std::vector<SDOperand> Ops;
2342 Ops.push_back(Op1);
2343 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2344}
2345SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2346 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2347 std::vector<MVT::ValueType> ResultTys;
2348 ResultTys.push_back(VT1);
2349 ResultTys.push_back(VT2);
2350 std::vector<SDOperand> Ops;
2351 Ops.push_back(Op1);
2352 Ops.push_back(Op2);
2353 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2354}
2355SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2356 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2357 SDOperand Op3) {
2358 std::vector<MVT::ValueType> ResultTys;
2359 ResultTys.push_back(VT1);
2360 ResultTys.push_back(VT2);
2361 std::vector<SDOperand> Ops;
2362 Ops.push_back(Op1);
2363 Ops.push_back(Op2);
2364 Ops.push_back(Op3);
2365 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2366}
2367SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2368 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2369 SDOperand Op3, SDOperand Op4) {
2370 std::vector<MVT::ValueType> ResultTys;
2371 ResultTys.push_back(VT1);
2372 ResultTys.push_back(VT2);
2373 std::vector<SDOperand> Ops;
2374 Ops.push_back(Op1);
2375 Ops.push_back(Op2);
2376 Ops.push_back(Op3);
2377 Ops.push_back(Op4);
2378 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2379}
2380SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2381 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2382 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2383 std::vector<MVT::ValueType> ResultTys;
2384 ResultTys.push_back(VT1);
2385 ResultTys.push_back(VT2);
2386 std::vector<SDOperand> Ops;
2387 Ops.push_back(Op1);
2388 Ops.push_back(Op2);
2389 Ops.push_back(Op3);
2390 Ops.push_back(Op4);
2391 Ops.push_back(Op5);
2392 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2393}
2394SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2395 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2396 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2397 SDOperand Op6) {
2398 std::vector<MVT::ValueType> ResultTys;
2399 ResultTys.push_back(VT1);
2400 ResultTys.push_back(VT2);
2401 std::vector<SDOperand> Ops;
2402 Ops.push_back(Op1);
2403 Ops.push_back(Op2);
2404 Ops.push_back(Op3);
2405 Ops.push_back(Op4);
2406 Ops.push_back(Op5);
2407 Ops.push_back(Op6);
2408 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2409}
2410SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2411 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2412 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2413 SDOperand Op6, SDOperand Op7) {
2414 std::vector<MVT::ValueType> ResultTys;
2415 ResultTys.push_back(VT1);
2416 ResultTys.push_back(VT2);
2417 std::vector<SDOperand> Ops;
2418 Ops.push_back(Op1);
2419 Ops.push_back(Op2);
2420 Ops.push_back(Op3);
2421 Ops.push_back(Op4);
2422 Ops.push_back(Op5);
2423 Ops.push_back(Op6);
2424 Ops.push_back(Op7);
2425 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2426}
2427SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2428 MVT::ValueType VT2, MVT::ValueType VT3,
2429 SDOperand Op1, SDOperand Op2) {
2430 std::vector<MVT::ValueType> ResultTys;
2431 ResultTys.push_back(VT1);
2432 ResultTys.push_back(VT2);
2433 ResultTys.push_back(VT3);
2434 std::vector<SDOperand> Ops;
2435 Ops.push_back(Op1);
2436 Ops.push_back(Op2);
2437 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2438}
2439SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2440 MVT::ValueType VT2, MVT::ValueType VT3,
2441 SDOperand Op1, SDOperand Op2,
2442 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2443 std::vector<MVT::ValueType> ResultTys;
2444 ResultTys.push_back(VT1);
2445 ResultTys.push_back(VT2);
2446 ResultTys.push_back(VT3);
2447 std::vector<SDOperand> Ops;
2448 Ops.push_back(Op1);
2449 Ops.push_back(Op2);
2450 Ops.push_back(Op3);
2451 Ops.push_back(Op4);
2452 Ops.push_back(Op5);
2453 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2454}
2455SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2456 MVT::ValueType VT2, MVT::ValueType VT3,
2457 SDOperand Op1, SDOperand Op2,
2458 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2459 SDOperand Op6) {
2460 std::vector<MVT::ValueType> ResultTys;
2461 ResultTys.push_back(VT1);
2462 ResultTys.push_back(VT2);
2463 ResultTys.push_back(VT3);
2464 std::vector<SDOperand> Ops;
2465 Ops.push_back(Op1);
2466 Ops.push_back(Op2);
2467 Ops.push_back(Op3);
2468 Ops.push_back(Op4);
2469 Ops.push_back(Op5);
2470 Ops.push_back(Op6);
2471 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2472}
2473SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2474 MVT::ValueType VT2, MVT::ValueType VT3,
2475 SDOperand Op1, SDOperand Op2,
2476 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2477 SDOperand Op6, SDOperand Op7) {
2478 std::vector<MVT::ValueType> ResultTys;
2479 ResultTys.push_back(VT1);
2480 ResultTys.push_back(VT2);
2481 ResultTys.push_back(VT3);
2482 std::vector<SDOperand> Ops;
2483 Ops.push_back(Op1);
2484 Ops.push_back(Op2);
2485 Ops.push_back(Op3);
2486 Ops.push_back(Op4);
2487 Ops.push_back(Op5);
2488 Ops.push_back(Op6);
2489 Ops.push_back(Op7);
2490 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2491}
2492SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2493 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2494 std::vector<MVT::ValueType> ResultTys;
2495 ResultTys.push_back(VT1);
2496 ResultTys.push_back(VT2);
2497 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2498}
2499
Chris Lattner0fb094f2005-11-19 01:44:53 +00002500// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002501/// This can cause recursive merging of nodes in the DAG.
2502///
Chris Lattner8b52f212005-08-26 18:36:28 +00002503/// This version assumes From/To have a single result value.
2504///
Chris Lattner1e111c72005-09-07 05:37:01 +00002505void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2506 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002507 SDNode *From = FromN.Val, *To = ToN.Val;
2508 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2509 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002510 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002511
Chris Lattner8b8749f2005-08-17 19:00:20 +00002512 while (!From->use_empty()) {
2513 // Process users until they are all gone.
2514 SDNode *U = *From->use_begin();
2515
2516 // This node is about to morph, remove its old self from the CSE maps.
2517 RemoveNodeFromCSEMaps(U);
2518
Chris Lattner65113b22005-11-08 22:07:03 +00002519 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2520 I != E; ++I)
2521 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002522 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002523 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002524 To->addUser(U);
2525 }
2526
2527 // Now that we have modified U, add it back to the CSE maps. If it already
2528 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002529 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2530 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002531 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002532 if (Deleted) Deleted->push_back(U);
2533 DeleteNodeNotInCSEMaps(U);
2534 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002535 }
2536}
2537
2538/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2539/// This can cause recursive merging of nodes in the DAG.
2540///
2541/// This version assumes From/To have matching types and numbers of result
2542/// values.
2543///
Chris Lattner1e111c72005-09-07 05:37:01 +00002544void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2545 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002546 assert(From != To && "Cannot replace uses of with self");
2547 assert(From->getNumValues() == To->getNumValues() &&
2548 "Cannot use this version of ReplaceAllUsesWith!");
2549 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002550 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002551 return;
2552 }
2553
2554 while (!From->use_empty()) {
2555 // Process users until they are all gone.
2556 SDNode *U = *From->use_begin();
2557
2558 // This node is about to morph, remove its old self from the CSE maps.
2559 RemoveNodeFromCSEMaps(U);
2560
Chris Lattner65113b22005-11-08 22:07:03 +00002561 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2562 I != E; ++I)
2563 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002564 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002565 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002566 To->addUser(U);
2567 }
2568
2569 // Now that we have modified U, add it back to the CSE maps. If it already
2570 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002571 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2572 ReplaceAllUsesWith(U, Existing, Deleted);
2573 // U is now dead.
2574 if (Deleted) Deleted->push_back(U);
2575 DeleteNodeNotInCSEMaps(U);
2576 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002577 }
2578}
2579
Chris Lattner8b52f212005-08-26 18:36:28 +00002580/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2581/// This can cause recursive merging of nodes in the DAG.
2582///
2583/// This version can replace From with any result values. To must match the
2584/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002585void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002586 const std::vector<SDOperand> &To,
2587 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002588 assert(From->getNumValues() == To.size() &&
2589 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002590 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002591 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002592 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002593 return;
2594 }
2595
2596 while (!From->use_empty()) {
2597 // Process users until they are all gone.
2598 SDNode *U = *From->use_begin();
2599
2600 // This node is about to morph, remove its old self from the CSE maps.
2601 RemoveNodeFromCSEMaps(U);
2602
Chris Lattner65113b22005-11-08 22:07:03 +00002603 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2604 I != E; ++I)
2605 if (I->Val == From) {
2606 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002607 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002608 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002609 ToOp.Val->addUser(U);
2610 }
2611
2612 // Now that we have modified U, add it back to the CSE maps. If it already
2613 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002614 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2615 ReplaceAllUsesWith(U, Existing, Deleted);
2616 // U is now dead.
2617 if (Deleted) Deleted->push_back(U);
2618 DeleteNodeNotInCSEMaps(U);
2619 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002620 }
2621}
2622
Chris Lattner012f2412006-02-17 21:58:01 +00002623/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2624/// uses of other values produced by From.Val alone. The Deleted vector is
2625/// handled the same was as for ReplaceAllUsesWith.
2626void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2627 std::vector<SDNode*> &Deleted) {
2628 assert(From != To && "Cannot replace a value with itself");
2629 // Handle the simple, trivial, case efficiently.
2630 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2631 ReplaceAllUsesWith(From, To, &Deleted);
2632 return;
2633 }
2634
2635 // Get all of the users in a nice, deterministically ordered, uniqued set.
2636 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2637
2638 while (!Users.empty()) {
2639 // We know that this user uses some value of From. If it is the right
2640 // value, update it.
2641 SDNode *User = Users.back();
2642 Users.pop_back();
2643
2644 for (SDOperand *Op = User->OperandList,
2645 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2646 if (*Op == From) {
2647 // Okay, we know this user needs to be updated. Remove its old self
2648 // from the CSE maps.
2649 RemoveNodeFromCSEMaps(User);
2650
2651 // Update all operands that match "From".
2652 for (; Op != E; ++Op) {
2653 if (*Op == From) {
2654 From.Val->removeUser(User);
2655 *Op = To;
2656 To.Val->addUser(User);
2657 }
2658 }
2659
2660 // Now that we have modified User, add it back to the CSE maps. If it
2661 // already exists there, recursively merge the results together.
2662 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2663 unsigned NumDeleted = Deleted.size();
2664 ReplaceAllUsesWith(User, Existing, &Deleted);
2665
2666 // User is now dead.
2667 Deleted.push_back(User);
2668 DeleteNodeNotInCSEMaps(User);
2669
2670 // We have to be careful here, because ReplaceAllUsesWith could have
2671 // deleted a user of From, which means there may be dangling pointers
2672 // in the "Users" setvector. Scan over the deleted node pointers and
2673 // remove them from the setvector.
2674 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2675 Users.remove(Deleted[i]);
2676 }
2677 break; // Exit the operand scanning loop.
2678 }
2679 }
2680 }
2681}
2682
Chris Lattner7b2880c2005-08-24 22:44:39 +00002683
Evan Chenge6f35d82006-08-01 08:20:41 +00002684/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2685/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002686unsigned SelectionDAG::AssignNodeIds() {
2687 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002688 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2689 SDNode *N = I;
2690 N->setNodeId(Id++);
2691 }
2692 return Id;
2693}
2694
Evan Chenge6f35d82006-08-01 08:20:41 +00002695/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002696/// based on their topological order. It returns the maximum id and a vector
2697/// of the SDNodes* in assigned order by reference.
2698unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002699 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002700 std::vector<unsigned> InDegree(DAGSize);
2701 std::vector<SDNode*> Sources;
2702
2703 // Use a two pass approach to avoid using a std::map which is slow.
2704 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002705 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2706 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002707 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002708 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002709 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002710 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002711 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002712 }
2713
Evan Chenge6f35d82006-08-01 08:20:41 +00002714 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002715 SDNode *N = Sources.back();
2716 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002717 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002718 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2719 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002720 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002721 if (Degree == 0)
2722 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002723 }
2724 }
2725
Evan Chengc384d6c2006-08-02 22:00:34 +00002726 // Second pass, assign the actual topological order as node ids.
2727 Id = 0;
2728 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2729 TI != TE; ++TI)
2730 (*TI)->setNodeId(Id++);
2731
2732 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002733}
2734
2735
Evan Cheng091cba12006-07-27 06:39:06 +00002736
Jim Laskey58b968b2005-08-17 20:08:02 +00002737//===----------------------------------------------------------------------===//
2738// SDNode Class
2739//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002740
Chris Lattner917d2c92006-07-19 00:00:37 +00002741// Out-of-line virtual method to give class a home.
2742void SDNode::ANCHOR() {
2743}
2744
Chris Lattnera3255112005-11-08 23:30:28 +00002745
2746/// getValueTypeList - Return a pointer to the specified value type.
2747///
2748MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2749 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2750 VTs[VT] = VT;
2751 return &VTs[VT];
2752}
2753
Chris Lattner5c884562005-01-12 18:37:47 +00002754/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2755/// indicated value. This method ignores uses of other values defined by this
2756/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002757bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002758 assert(Value < getNumValues() && "Bad value!");
2759
2760 // If there is only one value, this is easy.
2761 if (getNumValues() == 1)
2762 return use_size() == NUses;
2763 if (Uses.size() < NUses) return false;
2764
Evan Cheng4ee62112006-02-05 06:29:23 +00002765 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002766
2767 std::set<SDNode*> UsersHandled;
2768
Evan Cheng4ee62112006-02-05 06:29:23 +00002769 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002770 UI != E; ++UI) {
2771 SDNode *User = *UI;
2772 if (User->getNumOperands() == 1 ||
2773 UsersHandled.insert(User).second) // First time we've seen this?
2774 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2775 if (User->getOperand(i) == TheValue) {
2776 if (NUses == 0)
2777 return false; // too many uses
2778 --NUses;
2779 }
2780 }
2781
2782 // Found exactly the right number of uses?
2783 return NUses == 0;
2784}
2785
2786
Evan Cheng4ee62112006-02-05 06:29:23 +00002787// isOnlyUse - Return true if this node is the only use of N.
2788bool SDNode::isOnlyUse(SDNode *N) const {
2789 bool Seen = false;
2790 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2791 SDNode *User = *I;
2792 if (User == this)
2793 Seen = true;
2794 else
2795 return false;
2796 }
2797
2798 return Seen;
2799}
2800
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002801// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002802bool SDOperand::isOperand(SDNode *N) const {
2803 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2804 if (*this == N->getOperand(i))
2805 return true;
2806 return false;
2807}
2808
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002809bool SDNode::isOperand(SDNode *N) const {
2810 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2811 if (this == N->OperandList[i].Val)
2812 return true;
2813 return false;
2814}
Evan Cheng4ee62112006-02-05 06:29:23 +00002815
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002816const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002817 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002818 default:
2819 if (getOpcode() < ISD::BUILTIN_OP_END)
2820 return "<<Unknown DAG Node>>";
2821 else {
Evan Cheng72261582005-12-20 06:22:03 +00002822 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002823 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002824 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2825 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002826
Evan Cheng72261582005-12-20 06:22:03 +00002827 TargetLowering &TLI = G->getTargetLoweringInfo();
2828 const char *Name =
2829 TLI.getTargetNodeName(getOpcode());
2830 if (Name) return Name;
2831 }
2832
2833 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002834 }
2835
Andrew Lenharth95762122005-03-31 21:24:06 +00002836 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002837 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002838 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002839 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002840 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002841 case ISD::AssertSext: return "AssertSext";
2842 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002843
2844 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002845 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002846 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002847 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002848
2849 case ISD::Constant: return "Constant";
2850 case ISD::ConstantFP: return "ConstantFP";
2851 case ISD::GlobalAddress: return "GlobalAddress";
2852 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002853 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002854 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002855 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002856 case ISD::INTRINSIC_WO_CHAIN: {
2857 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2858 return Intrinsic::getName((Intrinsic::ID)IID);
2859 }
2860 case ISD::INTRINSIC_VOID:
2861 case ISD::INTRINSIC_W_CHAIN: {
2862 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002863 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002864 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002865
Chris Lattnerb2827b02006-03-19 00:52:58 +00002866 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002867 case ISD::TargetConstant: return "TargetConstant";
2868 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002869 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2870 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002871 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002872 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002873 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002874
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002875 case ISD::CopyToReg: return "CopyToReg";
2876 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002877 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002878 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002879 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002880 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002881 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002882 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002883
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002884 // Unary operators
2885 case ISD::FABS: return "fabs";
2886 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002887 case ISD::FSQRT: return "fsqrt";
2888 case ISD::FSIN: return "fsin";
2889 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002890
2891 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002892 case ISD::ADD: return "add";
2893 case ISD::SUB: return "sub";
2894 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002895 case ISD::MULHU: return "mulhu";
2896 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002897 case ISD::SDIV: return "sdiv";
2898 case ISD::UDIV: return "udiv";
2899 case ISD::SREM: return "srem";
2900 case ISD::UREM: return "urem";
2901 case ISD::AND: return "and";
2902 case ISD::OR: return "or";
2903 case ISD::XOR: return "xor";
2904 case ISD::SHL: return "shl";
2905 case ISD::SRA: return "sra";
2906 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002907 case ISD::ROTL: return "rotl";
2908 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002909 case ISD::FADD: return "fadd";
2910 case ISD::FSUB: return "fsub";
2911 case ISD::FMUL: return "fmul";
2912 case ISD::FDIV: return "fdiv";
2913 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002914 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002915 case ISD::VADD: return "vadd";
2916 case ISD::VSUB: return "vsub";
2917 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002918 case ISD::VSDIV: return "vsdiv";
2919 case ISD::VUDIV: return "vudiv";
2920 case ISD::VAND: return "vand";
2921 case ISD::VOR: return "vor";
2922 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002923
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002924 case ISD::SETCC: return "setcc";
2925 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002926 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002927 case ISD::VSELECT: return "vselect";
2928 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2929 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2930 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002931 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002932 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2933 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2934 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2935 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2936 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002937 case ISD::ADDC: return "addc";
2938 case ISD::ADDE: return "adde";
2939 case ISD::SUBC: return "subc";
2940 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002941 case ISD::SHL_PARTS: return "shl_parts";
2942 case ISD::SRA_PARTS: return "sra_parts";
2943 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002944
Chris Lattner7f644642005-04-28 21:44:03 +00002945 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002946 case ISD::SIGN_EXTEND: return "sign_extend";
2947 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002948 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002949 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002950 case ISD::TRUNCATE: return "truncate";
2951 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002952 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002953 case ISD::FP_EXTEND: return "fp_extend";
2954
2955 case ISD::SINT_TO_FP: return "sint_to_fp";
2956 case ISD::UINT_TO_FP: return "uint_to_fp";
2957 case ISD::FP_TO_SINT: return "fp_to_sint";
2958 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002959 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002960
2961 // Control flow instructions
2962 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002963 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002964 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002965 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002966 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002967 case ISD::CALLSEQ_START: return "callseq_start";
2968 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002969
2970 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002971 case ISD::LOAD: return "load";
2972 case ISD::STORE: return "store";
2973 case ISD::VLOAD: return "vload";
2974 case ISD::EXTLOAD: return "extload";
2975 case ISD::SEXTLOAD: return "sextload";
2976 case ISD::ZEXTLOAD: return "zextload";
2977 case ISD::TRUNCSTORE: return "truncstore";
2978 case ISD::VAARG: return "vaarg";
2979 case ISD::VACOPY: return "vacopy";
2980 case ISD::VAEND: return "vaend";
2981 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002982 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002983 case ISD::EXTRACT_ELEMENT: return "extract_element";
2984 case ISD::BUILD_PAIR: return "build_pair";
2985 case ISD::STACKSAVE: return "stacksave";
2986 case ISD::STACKRESTORE: return "stackrestore";
2987
2988 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002989 case ISD::MEMSET: return "memset";
2990 case ISD::MEMCPY: return "memcpy";
2991 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002992
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002993 // Bit manipulation
2994 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002995 case ISD::CTPOP: return "ctpop";
2996 case ISD::CTTZ: return "cttz";
2997 case ISD::CTLZ: return "ctlz";
2998
Chris Lattner36ce6912005-11-29 06:21:05 +00002999 // Debug info
3000 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003001 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00003002 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00003003
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003004 case ISD::CONDCODE:
3005 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003006 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003007 case ISD::SETOEQ: return "setoeq";
3008 case ISD::SETOGT: return "setogt";
3009 case ISD::SETOGE: return "setoge";
3010 case ISD::SETOLT: return "setolt";
3011 case ISD::SETOLE: return "setole";
3012 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003013
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003014 case ISD::SETO: return "seto";
3015 case ISD::SETUO: return "setuo";
3016 case ISD::SETUEQ: return "setue";
3017 case ISD::SETUGT: return "setugt";
3018 case ISD::SETUGE: return "setuge";
3019 case ISD::SETULT: return "setult";
3020 case ISD::SETULE: return "setule";
3021 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003022
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003023 case ISD::SETEQ: return "seteq";
3024 case ISD::SETGT: return "setgt";
3025 case ISD::SETGE: return "setge";
3026 case ISD::SETLT: return "setlt";
3027 case ISD::SETLE: return "setle";
3028 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003029 }
3030 }
3031}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003032
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003033void SDNode::dump() const { dump(0); }
3034void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003035 std::cerr << (void*)this << ": ";
3036
3037 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
3038 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003039 if (getValueType(i) == MVT::Other)
3040 std::cerr << "ch";
3041 else
3042 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003043 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003044 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003045
3046 std::cerr << " ";
3047 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3048 if (i) std::cerr << ", ";
3049 std::cerr << (void*)getOperand(i).Val;
3050 if (unsigned RN = getOperand(i).ResNo)
3051 std::cerr << ":" << RN;
3052 }
3053
3054 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3055 std::cerr << "<" << CSDN->getValue() << ">";
3056 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3057 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003058 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003059 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003060 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00003061 std::cerr << "<";
3062 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003063 if (offset > 0)
3064 std::cerr << " + " << offset;
3065 else
3066 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003067 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003068 std::cerr << "<" << FIDN->getIndex() << ">";
3069 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003070 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00003071 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003072 if (offset > 0)
3073 std::cerr << " + " << offset;
3074 else
3075 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003076 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003077 std::cerr << "<";
3078 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3079 if (LBB)
3080 std::cerr << LBB->getName() << " ";
3081 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003082 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003083 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00003084 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3085 } else {
3086 std::cerr << " #" << R->getReg();
3087 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003088 } else if (const ExternalSymbolSDNode *ES =
3089 dyn_cast<ExternalSymbolSDNode>(this)) {
3090 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003091 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3092 if (M->getValue())
3093 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3094 else
3095 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003096 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3097 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003098 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003099}
3100
Chris Lattnerde202b32005-11-09 23:47:37 +00003101static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003102 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3103 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003104 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003105 else
3106 std::cerr << "\n" << std::string(indent+2, ' ')
3107 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003108
Chris Lattnerea946cd2005-01-09 20:38:33 +00003109
3110 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003111 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003112}
3113
Chris Lattnerc3aae252005-01-07 07:46:32 +00003114void SelectionDAG::dump() const {
3115 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003116 std::vector<const SDNode*> Nodes;
3117 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3118 I != E; ++I)
3119 Nodes.push_back(I);
3120
Chris Lattner49d24712005-01-09 20:26:36 +00003121 std::sort(Nodes.begin(), Nodes.end());
3122
3123 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003124 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003125 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003126 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003127
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003128 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003129
Chris Lattnerc3aae252005-01-07 07:46:32 +00003130 std::cerr << "\n\n";
3131}
3132
Evan Chengfae9f1c2006-02-09 22:11:03 +00003133/// InsertISelMapEntry - A helper function to insert a key / element pair
3134/// into a SDOperand to SDOperand map. This is added to avoid the map
3135/// insertion operator from being inlined.
3136void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3137 SDNode *Key, unsigned KeyResNo,
3138 SDNode *Element, unsigned ElementResNo) {
3139 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3140 SDOperand(Element, ElementResNo)));
3141}