blob: 2c4f85af7a50bc72dc078be4919c666a628dbbd5 [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>
Chris Lattnere25738c2004-06-02 04:28:06 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner5cdcc582005-01-09 20:52:51 +000034static bool isCommutativeBinOp(unsigned Opcode) {
35 switch (Opcode) {
36 case ISD::ADD:
37 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000038 case ISD::MULHU:
39 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000040 case ISD::FADD:
41 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000042 case ISD::AND:
43 case ISD::OR:
44 case ISD::XOR: return true;
45 default: return false; // FIXME: Need commutative info for user ops!
46 }
47}
48
Chris Lattner5cdcc582005-01-09 20:52:51 +000049// isInvertibleForFree - Return true if there is no cost to emitting the logical
50// inverse of this node.
51static bool isInvertibleForFree(SDOperand N) {
52 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000053 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000054 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000055 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000056}
57
Jim Laskey58b968b2005-08-17 20:08:02 +000058//===----------------------------------------------------------------------===//
59// ConstantFPSDNode Class
60//===----------------------------------------------------------------------===//
61
62/// isExactlyValue - We don't rely on operator== working on double values, as
63/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
64/// As such, this method can be used to do an exact bit-for-bit comparison of
65/// two floating point values.
66bool ConstantFPSDNode::isExactlyValue(double V) const {
67 return DoubleToBits(V) == DoubleToBits(Value);
68}
69
70//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000071// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000072//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000073
Evan Chenga8df1662006-03-27 06:58:47 +000074/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000075/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000076bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000077 // Look through a bit convert.
78 if (N->getOpcode() == ISD::BIT_CONVERT)
79 N = N->getOperand(0).Val;
80
Evan Chenga8df1662006-03-27 06:58:47 +000081 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000082
83 unsigned i = 0, e = N->getNumOperands();
84
85 // Skip over all of the undef values.
86 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
87 ++i;
88
89 // Do not accept an all-undef vector.
90 if (i == e) return false;
91
92 // Do not accept build_vectors that aren't all constants or which have non-~0
93 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000094 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000095 if (isa<ConstantSDNode>(NotZero)) {
96 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
97 return false;
98 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000099 MVT::ValueType VT = NotZero.getValueType();
100 if (VT== MVT::f64) {
101 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
102 (uint64_t)-1)
103 return false;
104 } else {
105 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
106 (uint32_t)-1)
107 return false;
108 }
Evan Chenga8df1662006-03-27 06:58:47 +0000109 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000110 return false;
111
112 // Okay, we have at least one ~0 value, check to see if the rest match or are
113 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000114 for (++i; i != e; ++i)
115 if (N->getOperand(i) != NotZero &&
116 N->getOperand(i).getOpcode() != ISD::UNDEF)
117 return false;
118 return true;
119}
120
121
Evan Cheng4a147842006-03-26 09:50:58 +0000122/// isBuildVectorAllZeros - Return true if the specified node is a
123/// BUILD_VECTOR where all of the elements are 0 or undef.
124bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000125 // Look through a bit convert.
126 if (N->getOpcode() == ISD::BIT_CONVERT)
127 N = N->getOperand(0).Val;
128
Evan Cheng4a147842006-03-26 09:50:58 +0000129 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000130
131 unsigned i = 0, e = N->getNumOperands();
132
133 // Skip over all of the undef values.
134 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
135 ++i;
136
137 // Do not accept an all-undef vector.
138 if (i == e) return false;
139
140 // Do not accept build_vectors that aren't all constants or which have non-~0
141 // elements.
142 SDOperand Zero = N->getOperand(i);
143 if (isa<ConstantSDNode>(Zero)) {
144 if (!cast<ConstantSDNode>(Zero)->isNullValue())
145 return false;
146 } else if (isa<ConstantFPSDNode>(Zero)) {
147 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
148 return false;
149 } else
150 return false;
151
152 // Okay, we have at least one ~0 value, check to see if the rest match or are
153 // undefs.
154 for (++i; i != e; ++i)
155 if (N->getOperand(i) != Zero &&
156 N->getOperand(i).getOpcode() != ISD::UNDEF)
157 return false;
158 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000159}
160
Chris Lattnerc3aae252005-01-07 07:46:32 +0000161/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
162/// when given the operation for (X op Y).
163ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
164 // To perform this operation, we just need to swap the L and G bits of the
165 // operation.
166 unsigned OldL = (Operation >> 2) & 1;
167 unsigned OldG = (Operation >> 1) & 1;
168 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
169 (OldL << 1) | // New G bit
170 (OldG << 2)); // New L bit.
171}
172
173/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
174/// 'op' is a valid SetCC operation.
175ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
176 unsigned Operation = Op;
177 if (isInteger)
178 Operation ^= 7; // Flip L, G, E bits, but not U.
179 else
180 Operation ^= 15; // Flip all of the condition bits.
181 if (Operation > ISD::SETTRUE2)
182 Operation &= ~8; // Don't let N and U bits get set.
183 return ISD::CondCode(Operation);
184}
185
186
187/// isSignedOp - For an integer comparison, return 1 if the comparison is a
188/// signed operation and 2 if the result is an unsigned comparison. Return zero
189/// if the operation does not depend on the sign of the input (setne and seteq).
190static int isSignedOp(ISD::CondCode Opcode) {
191 switch (Opcode) {
192 default: assert(0 && "Illegal integer setcc operation!");
193 case ISD::SETEQ:
194 case ISD::SETNE: return 0;
195 case ISD::SETLT:
196 case ISD::SETLE:
197 case ISD::SETGT:
198 case ISD::SETGE: return 1;
199 case ISD::SETULT:
200 case ISD::SETULE:
201 case ISD::SETUGT:
202 case ISD::SETUGE: return 2;
203 }
204}
205
206/// getSetCCOrOperation - Return the result of a logical OR between different
207/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
208/// returns SETCC_INVALID if it is not possible to represent the resultant
209/// comparison.
210ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
211 bool isInteger) {
212 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
213 // Cannot fold a signed integer setcc with an unsigned integer setcc.
214 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000215
Chris Lattnerc3aae252005-01-07 07:46:32 +0000216 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000217
Chris Lattnerc3aae252005-01-07 07:46:32 +0000218 // If the N and U bits get set then the resultant comparison DOES suddenly
219 // care about orderedness, and is true when ordered.
220 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000221 Op &= ~16; // Clear the U bit if the N bit is set.
222
223 // Canonicalize illegal integer setcc's.
224 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
225 Op = ISD::SETNE;
226
Chris Lattnerc3aae252005-01-07 07:46:32 +0000227 return ISD::CondCode(Op);
228}
229
230/// getSetCCAndOperation - Return the result of a logical AND between different
231/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
232/// function returns zero if it is not possible to represent the resultant
233/// comparison.
234ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
235 bool isInteger) {
236 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
237 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000238 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000239
240 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000241 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
242
243 // Canonicalize illegal integer setcc's.
244 if (isInteger) {
245 switch (Result) {
246 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000247 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
248 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
249 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
250 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000251 }
252 }
253
254 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000255}
256
Chris Lattnerb48da392005-01-23 04:39:44 +0000257const TargetMachine &SelectionDAG::getTarget() const {
258 return TLI.getTargetMachine();
259}
260
Jim Laskey58b968b2005-08-17 20:08:02 +0000261//===----------------------------------------------------------------------===//
262// SelectionDAG Class
263//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000264
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000265/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000266/// SelectionDAG.
267void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000268 // Create a dummy node (which is not added to allnodes), that adds a reference
269 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000270 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000271
Chris Lattner190a4182006-08-04 17:45:20 +0000272 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000273
Chris Lattner190a4182006-08-04 17:45:20 +0000274 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000275 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000276 if (I->use_empty())
277 DeadNodes.push_back(I);
278
279 // Process the worklist, deleting the nodes and adding their uses to the
280 // worklist.
281 while (!DeadNodes.empty()) {
282 SDNode *N = DeadNodes.back();
283 DeadNodes.pop_back();
284
285 // Take the node out of the appropriate CSE map.
286 RemoveNodeFromCSEMaps(N);
287
288 // Next, brutally remove the operand list. This is safe to do, as there are
289 // no cycles in the graph.
290 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
291 SDNode *Operand = I->Val;
292 Operand->removeUser(N);
293
294 // Now that we removed this operand, see if there are no uses of it left.
295 if (Operand->use_empty())
296 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000297 }
Chris Lattner190a4182006-08-04 17:45:20 +0000298 delete[] N->OperandList;
299 N->OperandList = 0;
300 N->NumOperands = 0;
301
302 // Finally, remove N itself.
303 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000304 }
305
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000307 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000308}
309
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000310void SelectionDAG::DeleteNode(SDNode *N) {
311 assert(N->use_empty() && "Cannot delete a node that is not dead!");
312
313 // First take this out of the appropriate CSE map.
314 RemoveNodeFromCSEMaps(N);
315
Chris Lattner1e111c72005-09-07 05:37:01 +0000316 // Finally, remove uses due to operands of this node, remove from the
317 // AllNodes list, and delete the node.
318 DeleteNodeNotInCSEMaps(N);
319}
320
321void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
322
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000323 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000324 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000325
326 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000327 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
328 I->Val->removeUser(N);
329 delete[] N->OperandList;
330 N->OperandList = 0;
331 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000332
333 delete N;
334}
335
Chris Lattner149c58c2005-08-16 18:17:10 +0000336/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
337/// correspond to it. This is useful when we're about to delete or repurpose
338/// the node. We don't want future request for structurally identical nodes
339/// to return N anymore.
340void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000342 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000343 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000344 case ISD::STRING:
345 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
346 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000347 case ISD::CONDCODE:
348 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
349 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000350 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000351 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
352 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000353 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000354 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000355 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000356 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000357 Erased =
358 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000359 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000360 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000361 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000362 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
363 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000364 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000365 // Remove it from the CSE Map.
366 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000367 break;
368 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369#ifndef NDEBUG
370 // Verify that the node was actually in one of the CSE maps, unless it has a
371 // flag result (which cannot be CSE'd) or is one of the special cases that are
372 // not subject to CSE.
373 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000374 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000375 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000376 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000377 assert(0 && "Node is not in map!");
378 }
379#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000380}
381
Chris Lattner8b8749f2005-08-17 19:00:20 +0000382/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
383/// has been taken out and modified in some way. If the specified node already
384/// exists in the CSE maps, do not modify the maps, but return the existing node
385/// instead. If it doesn't exist, add it and return null.
386///
387SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
388 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000389 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000390 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000391
Chris Lattner9f8cc692005-12-19 22:21:21 +0000392 // Check that remaining values produced are not flags.
393 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
394 if (N->getValueType(i) == MVT::Flag)
395 return 0; // Never CSE anything that produces a flag.
396
Chris Lattnera5682852006-08-07 23:03:03 +0000397 SDNode *New = CSEMap.GetOrInsertNode(N);
398 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000399 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000400}
401
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000402/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
403/// were replaced with those specified. If this node is never memoized,
404/// return null, otherwise return a pointer to the slot it would take. If a
405/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000406SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
407 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000408 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000409 return 0; // Never add these nodes.
410
411 // Check that remaining values produced are not flags.
412 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
413 if (N->getValueType(i) == MVT::Flag)
414 return 0; // Never CSE anything that produces a flag.
415
Chris Lattnera5682852006-08-07 23:03:03 +0000416 SelectionDAGCSEMap::NodeID ID;
417 ID.SetOpcode(N->getOpcode());
418 ID.SetValueTypes(N->value_begin());
419 ID.SetOperands(Op);
420 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000421}
422
423/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
424/// were replaced with those specified. If this node is never memoized,
425/// return null, otherwise return a pointer to the slot it would take. If a
426/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000427SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
428 SDOperand Op1, SDOperand Op2,
429 void *&InsertPos) {
430 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
431 return 0; // Never add these nodes.
432
433 // Check that remaining values produced are not flags.
434 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
435 if (N->getValueType(i) == MVT::Flag)
436 return 0; // Never CSE anything that produces a flag.
437
438 SelectionDAGCSEMap::NodeID ID;
439 ID.SetOpcode(N->getOpcode());
440 ID.SetValueTypes(N->value_begin());
441 ID.SetOperands(Op1, Op2);
442 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
443}
444
445
446/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
447/// were replaced with those specified. If this node is never memoized,
448/// return null, otherwise return a pointer to the slot it would take. If a
449/// node already exists with these operands, the slot will be non-null.
450SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000451 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000452 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000453 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000454 return 0; // Never add these nodes.
455
456 // Check that remaining values produced are not flags.
457 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
458 if (N->getValueType(i) == MVT::Flag)
459 return 0; // Never CSE anything that produces a flag.
460
Chris Lattnera5682852006-08-07 23:03:03 +0000461 SelectionDAGCSEMap::NodeID ID;
462 ID.SetOpcode(N->getOpcode());
463 ID.SetValueTypes(N->value_begin());
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000464 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000465 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000466}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000467
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000468
Chris Lattner78ec3112003-08-11 14:57:33 +0000469SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000470 while (!AllNodes.empty()) {
471 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000472 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000473 delete [] N->OperandList;
474 N->OperandList = 0;
475 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000476 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000477 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000478}
479
Chris Lattner0f2287b2005-04-13 02:38:18 +0000480SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000481 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000482 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000483 return getNode(ISD::AND, Op.getValueType(), Op,
484 getConstant(Imm, Op.getValueType()));
485}
486
Chris Lattner36ce6912005-11-29 06:21:05 +0000487SDOperand SelectionDAG::getString(const std::string &Val) {
488 StringSDNode *&N = StringNodes[Val];
489 if (!N) {
490 N = new StringSDNode(Val);
491 AllNodes.push_back(N);
492 }
493 return SDOperand(N, 0);
494}
495
Chris Lattner61b09412006-08-11 21:01:22 +0000496SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000497 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000498 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000499
Chris Lattner61b09412006-08-11 21:01:22 +0000500 // Mask out any bits that are not valid for this constant.
501 Val &= MVT::getIntVTBitMask(VT);
502
503 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
504 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
505 ID.AddInteger(Val);
506 void *IP = 0;
507 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
508 return SDOperand(E, 0);
509 SDNode *N = new ConstantSDNode(isT, Val, VT);
510 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000511 AllNodes.push_back(N);
512 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000513}
514
Chris Lattner61b09412006-08-11 21:01:22 +0000515
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000516SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
517 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000518 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
519 if (VT == MVT::f32)
520 Val = (float)Val; // Mask out extra precision.
521
Chris Lattnerd8658612005-02-17 20:17:32 +0000522 // Do the map lookup using the actual bit pattern for the floating point
523 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
524 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000525 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
526 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
527 ID.AddInteger(DoubleToBits(Val));
528 void *IP = 0;
529 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
530 return SDOperand(E, 0);
531 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
532 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000533 AllNodes.push_back(N);
534 return SDOperand(N, 0);
535}
536
Chris Lattnerc3aae252005-01-07 07:46:32 +0000537SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000538 MVT::ValueType VT, int Offset,
539 bool isTargetGA) {
540 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
541 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
542 ID.AddPointer(GV);
543 ID.AddInteger(Offset);
544 void *IP = 0;
545 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
546 return SDOperand(E, 0);
547 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
548 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000549 AllNodes.push_back(N);
550 return SDOperand(N, 0);
551}
552
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000553SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
554 bool isTarget) {
555 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
556 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
557 ID.AddInteger(FI);
558 void *IP = 0;
559 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
560 return SDOperand(E, 0);
561 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
562 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000563 AllNodes.push_back(N);
564 return SDOperand(N, 0);
565}
566
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000567SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
568 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
569 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
570 ID.AddInteger(JTI);
571 void *IP = 0;
572 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
573 return SDOperand(E, 0);
574 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
575 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000576 AllNodes.push_back(N);
577 return SDOperand(N, 0);
578}
579
Evan Chengb8973bd2006-01-31 22:23:14 +0000580SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000581 unsigned Alignment, int Offset,
582 bool isTarget) {
583 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
584 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
585 ID.AddInteger(Alignment);
586 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000587 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000588 void *IP = 0;
589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
590 return SDOperand(E, 0);
591 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
592 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000593 AllNodes.push_back(N);
594 return SDOperand(N, 0);
595}
596
Chris Lattnerc3aae252005-01-07 07:46:32 +0000597
598SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner61b09412006-08-11 21:01:22 +0000599 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getNodeValueTypes(MVT::Other));
600 ID.AddPointer(MBB);
601 void *IP = 0;
602 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
603 return SDOperand(E, 0);
604 SDNode *N = new BasicBlockSDNode(MBB);
605 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000606 AllNodes.push_back(N);
607 return SDOperand(N, 0);
608}
609
Chris Lattner15e4b012005-07-10 00:07:11 +0000610SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
611 if ((unsigned)VT >= ValueTypeNodes.size())
612 ValueTypeNodes.resize(VT+1);
613 if (ValueTypeNodes[VT] == 0) {
614 ValueTypeNodes[VT] = new VTSDNode(VT);
615 AllNodes.push_back(ValueTypeNodes[VT]);
616 }
617
618 return SDOperand(ValueTypeNodes[VT], 0);
619}
620
Chris Lattnerc3aae252005-01-07 07:46:32 +0000621SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
622 SDNode *&N = ExternalSymbols[Sym];
623 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000624 N = new ExternalSymbolSDNode(false, Sym, VT);
625 AllNodes.push_back(N);
626 return SDOperand(N, 0);
627}
628
Chris Lattner809ec112006-01-28 10:09:25 +0000629SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
630 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000631 SDNode *&N = TargetExternalSymbols[Sym];
632 if (N) return SDOperand(N, 0);
633 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000634 AllNodes.push_back(N);
635 return SDOperand(N, 0);
636}
637
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000638SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
639 if ((unsigned)Cond >= CondCodeNodes.size())
640 CondCodeNodes.resize(Cond+1);
641
Chris Lattner079a27a2005-08-09 20:40:02 +0000642 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000643 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000644 AllNodes.push_back(CondCodeNodes[Cond]);
645 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000646 return SDOperand(CondCodeNodes[Cond], 0);
647}
648
Chris Lattner0fdd7682005-08-30 22:38:38 +0000649SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner61b09412006-08-11 21:01:22 +0000650 SelectionDAGCSEMap::NodeID ID(ISD::Register, getNodeValueTypes(VT));
651 ID.AddInteger(RegNo);
652 void *IP = 0;
653 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
654 return SDOperand(E, 0);
655 SDNode *N = new RegisterSDNode(RegNo, VT);
656 CSEMap.InsertNode(N, IP);
657 AllNodes.push_back(N);
658 return SDOperand(N, 0);
659}
660
661SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
662 assert((!V || isa<PointerType>(V->getType())) &&
663 "SrcValue is not a pointer?");
664
665 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getNodeValueTypes(MVT::Other));
666 ID.AddPointer(V);
667 ID.AddInteger(Offset);
668 void *IP = 0;
669 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
670 return SDOperand(E, 0);
671 SDNode *N = new SrcValueSDNode(V, Offset);
672 CSEMap.InsertNode(N, IP);
673 AllNodes.push_back(N);
674 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000675}
676
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000677SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
678 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000679 // These setcc operations always fold.
680 switch (Cond) {
681 default: break;
682 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000683 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000684 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000685 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000686
687 case ISD::SETOEQ:
688 case ISD::SETOGT:
689 case ISD::SETOGE:
690 case ISD::SETOLT:
691 case ISD::SETOLE:
692 case ISD::SETONE:
693 case ISD::SETO:
694 case ISD::SETUO:
695 case ISD::SETUEQ:
696 case ISD::SETUNE:
697 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
698 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000699 }
700
Chris Lattner67255a12005-04-07 18:14:58 +0000701 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
702 uint64_t C2 = N2C->getValue();
703 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
704 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000705
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706 // Sign extend the operands if required
707 if (ISD::isSignedIntSetCC(Cond)) {
708 C1 = N1C->getSignExtended();
709 C2 = N2C->getSignExtended();
710 }
711
712 switch (Cond) {
713 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000714 case ISD::SETEQ: return getConstant(C1 == C2, VT);
715 case ISD::SETNE: return getConstant(C1 != C2, VT);
716 case ISD::SETULT: return getConstant(C1 < C2, VT);
717 case ISD::SETUGT: return getConstant(C1 > C2, VT);
718 case ISD::SETULE: return getConstant(C1 <= C2, VT);
719 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
720 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
721 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
722 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
723 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000724 }
Chris Lattner24673922005-04-07 18:58:54 +0000725 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000726 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000727 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
728 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
729
730 // If the comparison constant has bits in the upper part, the
731 // zero-extended value could never match.
732 if (C2 & (~0ULL << InSize)) {
733 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
734 switch (Cond) {
735 case ISD::SETUGT:
736 case ISD::SETUGE:
737 case ISD::SETEQ: return getConstant(0, VT);
738 case ISD::SETULT:
739 case ISD::SETULE:
740 case ISD::SETNE: return getConstant(1, VT);
741 case ISD::SETGT:
742 case ISD::SETGE:
743 // True if the sign bit of C2 is set.
744 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
745 case ISD::SETLT:
746 case ISD::SETLE:
747 // True if the sign bit of C2 isn't set.
748 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
749 default:
750 break;
751 }
752 }
753
754 // Otherwise, we can perform the comparison with the low bits.
755 switch (Cond) {
756 case ISD::SETEQ:
757 case ISD::SETNE:
758 case ISD::SETUGT:
759 case ISD::SETUGE:
760 case ISD::SETULT:
761 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000762 return getSetCC(VT, N1.getOperand(0),
763 getConstant(C2, N1.getOperand(0).getValueType()),
764 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000765 default:
766 break; // todo, be more careful with signed comparisons
767 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000768 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
769 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
770 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
771 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
772 MVT::ValueType ExtDstTy = N1.getValueType();
773 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000774
Chris Lattner7b2880c2005-08-24 22:44:39 +0000775 // If the extended part has any inconsistent bits, it cannot ever
776 // compare equal. In other words, they have to be all ones or all
777 // zeros.
778 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000779 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000780 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
781 return getConstant(Cond == ISD::SETNE, VT);
782
783 // Otherwise, make this a use of a zext.
784 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000785 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000786 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000787 }
788
Chris Lattner67255a12005-04-07 18:14:58 +0000789 uint64_t MinVal, MaxVal;
790 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
791 if (ISD::isSignedIntSetCC(Cond)) {
792 MinVal = 1ULL << (OperandBitSize-1);
793 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
794 MaxVal = ~0ULL >> (65-OperandBitSize);
795 else
796 MaxVal = 0;
797 } else {
798 MinVal = 0;
799 MaxVal = ~0ULL >> (64-OperandBitSize);
800 }
801
802 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
803 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
804 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
805 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000806 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
807 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000808 }
809
810 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
811 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
812 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000813 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
814 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000815 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000816
Nate Begeman72ea2812005-04-14 08:56:52 +0000817 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
818 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000819
Nate Begeman72ea2812005-04-14 08:56:52 +0000820 // Canonicalize setgt X, Min --> setne X, Min
821 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000822 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000823
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000824 // If we have setult X, 1, turn it into seteq X, 0
825 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000826 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
827 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000828 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000829 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000830 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
831 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000832
833 // If we have "setcc X, C1", check to see if we can shrink the immediate
834 // by changing cc.
835
836 // SETUGT X, SINTMAX -> SETLT X, 0
837 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
838 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000839 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000840
841 // FIXME: Implement the rest of these.
842
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000843
844 // Fold bit comparisons when we can.
845 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
846 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
847 if (ConstantSDNode *AndRHS =
848 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
849 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
850 // Perform the xform if the AND RHS is a single bit.
851 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
852 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000853 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000854 TLI.getShiftAmountTy()));
855 }
856 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
857 // (X & 8) == 8 --> (X & 8) >> 3
858 // Perform the xform if C2 is a single bit.
859 if ((C2 & (C2-1)) == 0) {
860 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000861 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000862 }
863 }
864 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000865 }
Chris Lattner67255a12005-04-07 18:14:58 +0000866 } else if (isa<ConstantSDNode>(N1.Val)) {
867 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000868 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000869 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000870
871 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
872 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
873 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000874
Chris Lattnerc3aae252005-01-07 07:46:32 +0000875 switch (Cond) {
876 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000877 case ISD::SETEQ: return getConstant(C1 == C2, VT);
878 case ISD::SETNE: return getConstant(C1 != C2, VT);
879 case ISD::SETLT: return getConstant(C1 < C2, VT);
880 case ISD::SETGT: return getConstant(C1 > C2, VT);
881 case ISD::SETLE: return getConstant(C1 <= C2, VT);
882 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000883 }
884 } else {
885 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000886 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000887 }
888
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000889 // Could not fold it.
890 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000891}
892
Chris Lattnerc3aae252005-01-07 07:46:32 +0000893/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000894///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000895SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner4a283e92006-08-11 18:38:11 +0000896 MVT::ValueType *VTs = getNodeValueTypes(VT);
897 SelectionDAGCSEMap::NodeID ID(Opcode, VTs);
898 void *IP = 0;
899 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
900 return SDOperand(E, 0);
901 SDNode *N = new SDNode(Opcode, VT);
902 CSEMap.InsertNode(N, IP);
903
904 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000905 return SDOperand(N, 0);
906}
907
Chris Lattnerc3aae252005-01-07 07:46:32 +0000908SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
909 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000910 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000911 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000912 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
913 uint64_t Val = C->getValue();
914 switch (Opcode) {
915 default: break;
916 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000917 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000918 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
919 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000920 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
921 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000922 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000923 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000924 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000925 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000926 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000927 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000928 case ISD::BSWAP:
929 switch(VT) {
930 default: assert(0 && "Invalid bswap!"); break;
931 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
932 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
933 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
934 }
935 break;
936 case ISD::CTPOP:
937 switch(VT) {
938 default: assert(0 && "Invalid ctpop!"); break;
939 case MVT::i1: return getConstant(Val != 0, VT);
940 case MVT::i8:
941 Tmp1 = (unsigned)Val & 0xFF;
942 return getConstant(CountPopulation_32(Tmp1), VT);
943 case MVT::i16:
944 Tmp1 = (unsigned)Val & 0xFFFF;
945 return getConstant(CountPopulation_32(Tmp1), VT);
946 case MVT::i32:
947 return getConstant(CountPopulation_32((unsigned)Val), VT);
948 case MVT::i64:
949 return getConstant(CountPopulation_64(Val), VT);
950 }
951 case ISD::CTLZ:
952 switch(VT) {
953 default: assert(0 && "Invalid ctlz!"); break;
954 case MVT::i1: return getConstant(Val == 0, VT);
955 case MVT::i8:
956 Tmp1 = (unsigned)Val & 0xFF;
957 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
958 case MVT::i16:
959 Tmp1 = (unsigned)Val & 0xFFFF;
960 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
961 case MVT::i32:
962 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
963 case MVT::i64:
964 return getConstant(CountLeadingZeros_64(Val), VT);
965 }
966 case ISD::CTTZ:
967 switch(VT) {
968 default: assert(0 && "Invalid cttz!"); break;
969 case MVT::i1: return getConstant(Val == 0, VT);
970 case MVT::i8:
971 Tmp1 = (unsigned)Val | 0x100;
972 return getConstant(CountTrailingZeros_32(Tmp1), VT);
973 case MVT::i16:
974 Tmp1 = (unsigned)Val | 0x10000;
975 return getConstant(CountTrailingZeros_32(Tmp1), VT);
976 case MVT::i32:
977 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
978 case MVT::i64:
979 return getConstant(CountTrailingZeros_64(Val), VT);
980 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000981 }
982 }
983
Chris Lattner94683772005-12-23 05:30:37 +0000984 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000985 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
986 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000987 case ISD::FNEG:
988 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000989 case ISD::FABS:
990 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000991 case ISD::FP_ROUND:
992 case ISD::FP_EXTEND:
993 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000994 case ISD::FP_TO_SINT:
995 return getConstant((int64_t)C->getValue(), VT);
996 case ISD::FP_TO_UINT:
997 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000998 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000999 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001000 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001001 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001002 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001003 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004 }
1005
1006 unsigned OpOpcode = Operand.Val->getOpcode();
1007 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001008 case ISD::TokenFactor:
1009 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001010 case ISD::SIGN_EXTEND:
1011 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001012 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1014 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1015 break;
1016 case ISD::ZERO_EXTEND:
1017 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001018 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001019 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001020 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001021 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001022 case ISD::ANY_EXTEND:
1023 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001024 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001025 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1026 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1027 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1028 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001029 case ISD::TRUNCATE:
1030 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001031 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001032 if (OpOpcode == ISD::TRUNCATE)
1033 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001034 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1035 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001036 // If the source is smaller than the dest, we still need an extend.
1037 if (Operand.Val->getOperand(0).getValueType() < VT)
1038 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1039 else if (Operand.Val->getOperand(0).getValueType() > VT)
1040 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1041 else
1042 return Operand.Val->getOperand(0);
1043 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001044 break;
Chris Lattner35481892005-12-23 00:16:34 +00001045 case ISD::BIT_CONVERT:
1046 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001047 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001048 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001049 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001050 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1051 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001052 if (OpOpcode == ISD::UNDEF)
1053 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001054 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001055 case ISD::SCALAR_TO_VECTOR:
1056 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1057 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1058 "Illegal SCALAR_TO_VECTOR node!");
1059 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001060 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001061 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1062 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001063 Operand.Val->getOperand(0));
1064 if (OpOpcode == ISD::FNEG) // --X -> X
1065 return Operand.Val->getOperand(0);
1066 break;
1067 case ISD::FABS:
1068 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1069 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1070 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 }
1072
Chris Lattner43247a12005-08-25 19:12:10 +00001073 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001074 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001075 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001076 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1077 void *IP = 0;
1078 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1079 return SDOperand(E, 0);
1080 N = new SDNode(Opcode, Operand);
1081 N->setValueTypes(VTs, 1);
1082 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001083 } else {
1084 N = new SDNode(Opcode, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001085 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001086 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001087 AllNodes.push_back(N);
1088 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001089}
1090
Chris Lattner36019aa2005-04-18 03:48:41 +00001091
1092
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1094 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001095#ifndef NDEBUG
1096 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001097 case ISD::TokenFactor:
1098 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1099 N2.getValueType() == MVT::Other && "Invalid token factor!");
1100 break;
Chris Lattner76365122005-01-16 02:23:22 +00001101 case ISD::AND:
1102 case ISD::OR:
1103 case ISD::XOR:
1104 case ISD::UDIV:
1105 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001106 case ISD::MULHU:
1107 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001108 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1109 // fall through
1110 case ISD::ADD:
1111 case ISD::SUB:
1112 case ISD::MUL:
1113 case ISD::SDIV:
1114 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001115 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1116 // fall through.
1117 case ISD::FADD:
1118 case ISD::FSUB:
1119 case ISD::FMUL:
1120 case ISD::FDIV:
1121 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001122 assert(N1.getValueType() == N2.getValueType() &&
1123 N1.getValueType() == VT && "Binary operator types must match!");
1124 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001125 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1126 assert(N1.getValueType() == VT &&
1127 MVT::isFloatingPoint(N1.getValueType()) &&
1128 MVT::isFloatingPoint(N2.getValueType()) &&
1129 "Invalid FCOPYSIGN!");
1130 break;
Chris Lattner76365122005-01-16 02:23:22 +00001131 case ISD::SHL:
1132 case ISD::SRA:
1133 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001134 case ISD::ROTL:
1135 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001136 assert(VT == N1.getValueType() &&
1137 "Shift operators return type must be the same as their first arg");
1138 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001139 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001140 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001141 case ISD::FP_ROUND_INREG: {
1142 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1143 assert(VT == N1.getValueType() && "Not an inreg round!");
1144 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1145 "Cannot FP_ROUND_INREG integer types");
1146 assert(EVT <= VT && "Not rounding down!");
1147 break;
1148 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001149 case ISD::AssertSext:
1150 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001151 case ISD::SIGN_EXTEND_INREG: {
1152 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1153 assert(VT == N1.getValueType() && "Not an inreg extend!");
1154 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1155 "Cannot *_EXTEND_INREG FP types");
1156 assert(EVT <= VT && "Not extending!");
1157 }
1158
Chris Lattner76365122005-01-16 02:23:22 +00001159 default: break;
1160 }
1161#endif
1162
Chris Lattnerc3aae252005-01-07 07:46:32 +00001163 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1164 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1165 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001166 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1167 int64_t Val = N1C->getValue();
1168 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1169 Val <<= 64-FromBits;
1170 Val >>= 64-FromBits;
1171 return getConstant(Val, VT);
1172 }
1173
Chris Lattnerc3aae252005-01-07 07:46:32 +00001174 if (N2C) {
1175 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1176 switch (Opcode) {
1177 case ISD::ADD: return getConstant(C1 + C2, VT);
1178 case ISD::SUB: return getConstant(C1 - C2, VT);
1179 case ISD::MUL: return getConstant(C1 * C2, VT);
1180 case ISD::UDIV:
1181 if (C2) return getConstant(C1 / C2, VT);
1182 break;
1183 case ISD::UREM :
1184 if (C2) return getConstant(C1 % C2, VT);
1185 break;
1186 case ISD::SDIV :
1187 if (C2) return getConstant(N1C->getSignExtended() /
1188 N2C->getSignExtended(), VT);
1189 break;
1190 case ISD::SREM :
1191 if (C2) return getConstant(N1C->getSignExtended() %
1192 N2C->getSignExtended(), VT);
1193 break;
1194 case ISD::AND : return getConstant(C1 & C2, VT);
1195 case ISD::OR : return getConstant(C1 | C2, VT);
1196 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001197 case ISD::SHL : return getConstant(C1 << C2, VT);
1198 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001199 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001200 case ISD::ROTL :
1201 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1202 VT);
1203 case ISD::ROTR :
1204 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1205 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001206 default: break;
1207 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001208 } else { // Cannonicalize constant to RHS if commutative
1209 if (isCommutativeBinOp(Opcode)) {
1210 std::swap(N1C, N2C);
1211 std::swap(N1, N2);
1212 }
1213 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214 }
1215
1216 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1217 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001218 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 if (N2CFP) {
1220 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1221 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001222 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1223 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1224 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1225 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001226 if (C2) return getConstantFP(C1 / C2, VT);
1227 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001228 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001229 if (C2) return getConstantFP(fmod(C1, C2), VT);
1230 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001231 case ISD::FCOPYSIGN: {
1232 union {
1233 double F;
1234 uint64_t I;
1235 } u1;
1236 union {
1237 double F;
1238 int64_t I;
1239 } u2;
1240 u1.F = C1;
1241 u2.F = C2;
1242 if (u2.I < 0) // Sign bit of RHS set?
1243 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1244 else
1245 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1246 return getConstantFP(u1.F, VT);
1247 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001248 default: break;
1249 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001250 } else { // Cannonicalize constant to RHS if commutative
1251 if (isCommutativeBinOp(Opcode)) {
1252 std::swap(N1CFP, N2CFP);
1253 std::swap(N1, N2);
1254 }
1255 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001256 }
Chris Lattner62b57722006-04-20 05:39:12 +00001257
1258 // Canonicalize an UNDEF to the RHS, even over a constant.
1259 if (N1.getOpcode() == ISD::UNDEF) {
1260 if (isCommutativeBinOp(Opcode)) {
1261 std::swap(N1, N2);
1262 } else {
1263 switch (Opcode) {
1264 case ISD::FP_ROUND_INREG:
1265 case ISD::SIGN_EXTEND_INREG:
1266 case ISD::SUB:
1267 case ISD::FSUB:
1268 case ISD::FDIV:
1269 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001270 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001271 return N1; // fold op(undef, arg2) -> undef
1272 case ISD::UDIV:
1273 case ISD::SDIV:
1274 case ISD::UREM:
1275 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001276 case ISD::SRL:
1277 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001278 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1279 }
1280 }
1281 }
1282
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001283 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001284 if (N2.getOpcode() == ISD::UNDEF) {
1285 switch (Opcode) {
1286 case ISD::ADD:
1287 case ISD::SUB:
1288 case ISD::FADD:
1289 case ISD::FSUB:
1290 case ISD::FMUL:
1291 case ISD::FDIV:
1292 case ISD::FREM:
1293 case ISD::UDIV:
1294 case ISD::SDIV:
1295 case ISD::UREM:
1296 case ISD::SREM:
1297 case ISD::XOR:
1298 return N2; // fold op(arg1, undef) -> undef
1299 case ISD::MUL:
1300 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001301 case ISD::SRL:
1302 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001303 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1304 case ISD::OR:
1305 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001306 case ISD::SRA:
1307 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001308 }
1309 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001310
Chris Lattnerc3aae252005-01-07 07:46:32 +00001311 // Finally, fold operations that do not require constants.
1312 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001313 case ISD::FP_ROUND_INREG:
1314 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1315 break;
1316 case ISD::SIGN_EXTEND_INREG: {
1317 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1318 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001319 break;
1320 }
1321
Nate Begemaneea805e2005-04-13 21:23:31 +00001322 // FIXME: figure out how to safely handle things like
1323 // int foo(int x) { return 1 << (x & 255); }
1324 // int bar() { return foo(256); }
1325#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001326 case ISD::SHL:
1327 case ISD::SRL:
1328 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001329 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001330 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001331 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001332 else if (N2.getOpcode() == ISD::AND)
1333 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1334 // If the and is only masking out bits that cannot effect the shift,
1335 // eliminate the and.
1336 unsigned NumBits = MVT::getSizeInBits(VT);
1337 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1338 return getNode(Opcode, VT, N1, N2.getOperand(0));
1339 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001340 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001341#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001342 }
1343
Chris Lattner27e9b412005-05-11 18:57:39 +00001344 // Memoize this node if possible.
1345 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001346 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001347 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001348 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1349 void *IP = 0;
1350 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1351 return SDOperand(E, 0);
1352 N = new SDNode(Opcode, N1, N2);
1353 N->setValueTypes(VTs, 1);
1354 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001355 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001356 N = new SDNode(Opcode, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001357 N->setValueTypes(VTs, 1);
Chris Lattner27e9b412005-05-11 18:57:39 +00001358 }
1359
Chris Lattnerc3aae252005-01-07 07:46:32 +00001360 AllNodes.push_back(N);
1361 return SDOperand(N, 0);
1362}
1363
Chris Lattnerc3aae252005-01-07 07:46:32 +00001364SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1365 SDOperand N1, SDOperand N2, SDOperand N3) {
1366 // Perform various simplifications.
1367 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1368 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001369 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001370 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001371 case ISD::SETCC: {
1372 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001373 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001374 if (Simp.Val) return Simp;
1375 break;
1376 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001377 case ISD::SELECT:
1378 if (N1C)
1379 if (N1C->getValue())
1380 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001381 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001382 return N3; // select false, X, Y -> Y
1383
1384 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001385 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001386 case ISD::BRCOND:
1387 if (N2C)
1388 if (N2C->getValue()) // Unconditional branch
1389 return getNode(ISD::BR, MVT::Other, N1, N3);
1390 else
1391 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001392 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001393 case ISD::VECTOR_SHUFFLE:
1394 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1395 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1396 N3.getOpcode() == ISD::BUILD_VECTOR &&
1397 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1398 "Illegal VECTOR_SHUFFLE node!");
1399 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001400 }
1401
Chris Lattner43247a12005-08-25 19:12:10 +00001402 // Memoize node if it doesn't produce a flag.
1403 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001404 MVT::ValueType *VTs = getNodeValueTypes(VT);
1405
Chris Lattner43247a12005-08-25 19:12:10 +00001406 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001407 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1408 void *IP = 0;
1409 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1410 return SDOperand(E, 0);
1411 N = new SDNode(Opcode, N1, N2, N3);
1412 N->setValueTypes(VTs, 1);
1413 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001414 } else {
1415 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001416 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001417 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001418 AllNodes.push_back(N);
1419 return SDOperand(N, 0);
1420}
1421
1422SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001423 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001424 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001425 SDOperand Ops[] = { N1, N2, N3, N4 };
1426 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001427}
1428
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001429SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1430 SDOperand N1, SDOperand N2, SDOperand N3,
1431 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001432 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1433 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001434}
1435
Evan Cheng7038daf2005-12-10 00:37:58 +00001436SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1437 SDOperand Chain, SDOperand Ptr,
1438 SDOperand SV) {
Chris Lattnera5682852006-08-07 23:03:03 +00001439 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1440
1441 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1442 void *IP = 0;
1443 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1444 return SDOperand(E, 0);
1445 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1446 N->setValueTypes(VTs, 2);
1447 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001448 AllNodes.push_back(N);
1449 return SDOperand(N, 0);
1450}
1451
1452SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1453 SDOperand Chain, SDOperand Ptr,
1454 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001455 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1456 getValueType(EVT) };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001457 // Add token chain.
1458 MVT::ValueType *VTs = getNodeValueTypes(MVT::Vector, MVT::Other);
1459 return getNode(ISD::VLOAD, VTs, 2, Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001460}
1461
1462SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1463 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1464 MVT::ValueType EVT) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001465 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001466 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1467 return getNode(Opcode, VTs, 2, Ops, 4);
Evan Cheng7038daf2005-12-10 00:37:58 +00001468}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001469
Nate Begemanacc398c2006-01-25 18:21:52 +00001470SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1471 SDOperand Chain, SDOperand Ptr,
1472 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001473 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001474 // Add token chain.
1475 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1476 return getNode(ISD::VAARG, VTs, 2, Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001477}
1478
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001479SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001480 const SDOperand *Ops, unsigned NumOps) {
1481 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001482 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001483 case 1: return getNode(Opcode, VT, Ops[0]);
1484 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1485 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001486 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001487 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001488
Chris Lattneref847df2005-04-09 03:27:28 +00001489 switch (Opcode) {
1490 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001491 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001492 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001493 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1494#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1495 // If this is a truncating store of a constant, convert to the desired type
1496 // and store it instead.
1497 if (isa<Constant>(Ops[0])) {
1498 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1499 if (isa<Constant>(Op))
1500 N1 = Op;
1501 }
1502 // Also for ConstantFP?
1503#endif
1504 if (Ops[0].getValueType() == EVT) // Normal store?
1505 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1506 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1507 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1508 "Can't do FP-INT conversion!");
1509 break;
1510 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001511 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001512 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001513 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1514 "LHS and RHS of condition must have same type!");
1515 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1516 "True and False arms of SelectCC must have same type!");
1517 assert(Ops[2].getValueType() == VT &&
1518 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001519 break;
1520 }
1521 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001522 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001523 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1524 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001525 break;
1526 }
Chris Lattneref847df2005-04-09 03:27:28 +00001527 }
1528
Chris Lattner385328c2005-05-14 07:42:29 +00001529 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001530 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001531 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001532 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001533 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001534 void *IP = 0;
1535 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1536 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001537 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001538 N->setValueTypes(VTs, 1);
1539 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001540 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001541 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001542 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001543 }
Chris Lattneref847df2005-04-09 03:27:28 +00001544 AllNodes.push_back(N);
1545 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001546}
1547
Chris Lattner89c34632005-05-14 06:20:26 +00001548SDOperand SelectionDAG::getNode(unsigned Opcode,
1549 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001550 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001551 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1552 Ops, NumOps);
1553}
1554
1555SDOperand SelectionDAG::getNode(unsigned Opcode,
1556 const MVT::ValueType *VTs, unsigned NumVTs,
1557 const SDOperand *Ops, unsigned NumOps) {
1558 if (NumVTs == 1)
1559 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001560
Chris Lattner5f056bf2005-07-10 01:55:33 +00001561 switch (Opcode) {
1562 case ISD::EXTLOAD:
1563 case ISD::SEXTLOAD:
1564 case ISD::ZEXTLOAD: {
1565 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001566 assert(NumOps == 4 && NumVTs == 2 && "Bad *EXTLOAD!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001567 // If they are asking for an extending load from/to the same thing, return a
1568 // normal load.
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001569 if (VTs[0] == EVT)
1570 return getLoad(VTs[0], Ops[0], Ops[1], Ops[2]);
1571 if (MVT::isVector(VTs[0])) {
1572 assert(EVT == MVT::getVectorBaseType(VTs[0]) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001573 "Invalid vector extload!");
1574 } else {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001575 assert(EVT < VTs[0] &&
Chris Lattnerce872152006-03-19 06:31:19 +00001576 "Should only be an extending load, not truncating!");
1577 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001578 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VTs[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001579 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001580 assert(MVT::isInteger(VTs[0]) == MVT::isInteger(EVT) &&
Chris Lattner5f056bf2005-07-10 01:55:33 +00001581 "Cannot convert from FP to Int or Int -> FP!");
1582 break;
1583 }
1584
Chris Lattnere89083a2005-05-14 07:25:05 +00001585 // FIXME: figure out how to safely handle things like
1586 // int foo(int x) { return 1 << (x & 255); }
1587 // int bar() { return foo(256); }
1588#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001589 case ISD::SRA_PARTS:
1590 case ISD::SRL_PARTS:
1591 case ISD::SHL_PARTS:
1592 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001593 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001594 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1595 else if (N3.getOpcode() == ISD::AND)
1596 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1597 // If the and is only masking out bits that cannot effect the shift,
1598 // eliminate the and.
1599 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1600 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1601 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1602 }
1603 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001604#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001605 }
Chris Lattner89c34632005-05-14 06:20:26 +00001606
Chris Lattner43247a12005-08-25 19:12:10 +00001607 // Memoize the node unless it returns a flag.
1608 SDNode *N;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001609 if (VTs[NumVTs-1] != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001610 SelectionDAGCSEMap::NodeID ID;
1611 ID.SetOpcode(Opcode);
1612 ID.SetValueTypes(VTs);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001613 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001614 void *IP = 0;
1615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1616 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001617 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001618 N->setValueTypes(VTs, NumVTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001619 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001620 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001621 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001622 N->setValueTypes(VTs, NumVTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001623 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001624 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001625 return SDOperand(N, 0);
1626}
1627
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001628
Chris Lattnera5682852006-08-07 23:03:03 +00001629MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1630 return SDNode::getValueTypeList(VT);
1631}
1632
1633MVT::ValueType *SelectionDAG::getNodeValueTypes(
1634 std::vector<MVT::ValueType> &RetVals) {
Chris Lattnera3255112005-11-08 23:30:28 +00001635 switch (RetVals.size()) {
Chris Lattnera5682852006-08-07 23:03:03 +00001636 case 0: assert(0 && "Cannot have nodes without results!");
1637 case 1: return SDNode::getValueTypeList(RetVals[0]);
1638 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
Chris Lattnera3255112005-11-08 23:30:28 +00001639 default: break;
1640 }
1641
1642 std::list<std::vector<MVT::ValueType> >::iterator I =
1643 std::find(VTList.begin(), VTList.end(), RetVals);
1644 if (I == VTList.end()) {
1645 VTList.push_front(RetVals);
1646 I = VTList.begin();
1647 }
1648
Chris Lattnera5682852006-08-07 23:03:03 +00001649 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001650}
1651
Chris Lattnera5682852006-08-07 23:03:03 +00001652MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1653 MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001654 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1655 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001656 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1657 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001658 }
1659 std::vector<MVT::ValueType> V;
1660 V.push_back(VT1);
1661 V.push_back(VT2);
1662 VTList.push_front(V);
Chris Lattnera5682852006-08-07 23:03:03 +00001663 return &(*VTList.begin())[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001664}
1665
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001666MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1667 MVT::ValueType VT2,
1668 MVT::ValueType VT3) {
1669 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1670 E = VTList.end(); I != E; ++I) {
1671 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1672 (*I)[2] == VT3)
1673 return &(*I)[0];
1674 }
1675 std::vector<MVT::ValueType> V;
1676 V.push_back(VT1);
1677 V.push_back(VT2);
1678 V.push_back(VT3);
1679 VTList.push_front(V);
1680 return &(*VTList.begin())[0];
1681}
1682
1683
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001684/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1685/// specified operands. If the resultant node already exists in the DAG,
1686/// this does not modify the specified node, instead it returns the node that
1687/// already exists. If the resultant node does not exist in the DAG, the
1688/// input node is returned. As a degenerate case, if you specify the same
1689/// input operands as the node already has, the input node is returned.
1690SDOperand SelectionDAG::
1691UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1692 SDNode *N = InN.Val;
1693 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1694
1695 // Check to see if there is no change.
1696 if (Op == N->getOperand(0)) return InN;
1697
1698 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001699 void *InsertPos = 0;
1700 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1701 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001702
1703 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001704 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001705 RemoveNodeFromCSEMaps(N);
1706
1707 // Now we update the operands.
1708 N->OperandList[0].Val->removeUser(N);
1709 Op.Val->addUser(N);
1710 N->OperandList[0] = Op;
1711
1712 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001713 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001714 return InN;
1715}
1716
1717SDOperand SelectionDAG::
1718UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1719 SDNode *N = InN.Val;
1720 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1721
1722 // Check to see if there is no change.
1723 bool AnyChange = false;
1724 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1725 return InN; // No operands changed, just return the input node.
1726
1727 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001728 void *InsertPos = 0;
1729 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1730 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001731
1732 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001733 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001734 RemoveNodeFromCSEMaps(N);
1735
1736 // Now we update the operands.
1737 if (N->OperandList[0] != Op1) {
1738 N->OperandList[0].Val->removeUser(N);
1739 Op1.Val->addUser(N);
1740 N->OperandList[0] = Op1;
1741 }
1742 if (N->OperandList[1] != Op2) {
1743 N->OperandList[1].Val->removeUser(N);
1744 Op2.Val->addUser(N);
1745 N->OperandList[1] = Op2;
1746 }
1747
1748 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001749 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001750 return InN;
1751}
1752
1753SDOperand SelectionDAG::
1754UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001755 SDOperand Ops[] = { Op1, Op2, Op3 };
1756 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001757}
1758
1759SDOperand SelectionDAG::
1760UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1761 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001762 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1763 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001764}
1765
1766SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001767UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1768 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001769 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1770 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001771}
1772
1773
1774SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001775UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001776 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001777 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001778 "Update with wrong number of operands");
1779
1780 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001781 bool AnyChange = false;
1782 for (unsigned i = 0; i != NumOps; ++i) {
1783 if (Ops[i] != N->getOperand(i)) {
1784 AnyChange = true;
1785 break;
1786 }
1787 }
1788
1789 // No operands changed, just return the input node.
1790 if (!AnyChange) return InN;
1791
1792 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001793 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001794 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001795 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001796
1797 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001798 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001799 RemoveNodeFromCSEMaps(N);
1800
1801 // Now we update the operands.
1802 for (unsigned i = 0; i != NumOps; ++i) {
1803 if (N->OperandList[i] != Ops[i]) {
1804 N->OperandList[i].Val->removeUser(N);
1805 Ops[i].Val->addUser(N);
1806 N->OperandList[i] = Ops[i];
1807 }
1808 }
1809
1810 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001811 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001812 return InN;
1813}
1814
1815
1816
Chris Lattner149c58c2005-08-16 18:17:10 +00001817
1818/// SelectNodeTo - These are used for target selectors to *mutate* the
1819/// specified node to have the specified return type, Target opcode, and
1820/// operands. Note that target opcodes are stored as
1821/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001822///
1823/// Note that SelectNodeTo returns the resultant node. If there is already a
1824/// node of the specified opcode and operands, it returns that node instead of
1825/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001826SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1827 MVT::ValueType VT) {
Chris Lattner4a283e92006-08-11 18:38:11 +00001828 MVT::ValueType *VTs = getNodeValueTypes(VT);
1829 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1830 void *IP = 0;
1831 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1832 return SDOperand(ON, 0);
1833
Chris Lattner7651fa42005-08-24 23:00:29 +00001834 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001835
Chris Lattner7651fa42005-08-24 23:00:29 +00001836 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001837 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001838
Chris Lattner4a283e92006-08-11 18:38:11 +00001839 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001840 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001841}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001842
Chris Lattnereb19e402005-11-30 22:45:14 +00001843SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1844 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001845 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001846 MVT::ValueType *VTs = getNodeValueTypes(VT);
1847 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1848 void *IP = 0;
1849 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1850 return SDOperand(ON, 0);
1851
Chris Lattner149c58c2005-08-16 18:17:10 +00001852 RemoveNodeFromCSEMaps(N);
1853 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001854 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001855 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001856 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001857 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001858}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001859
Chris Lattnereb19e402005-11-30 22:45:14 +00001860SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1861 MVT::ValueType VT, SDOperand Op1,
1862 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001863 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001864 MVT::ValueType *VTs = getNodeValueTypes(VT);
1865 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1866 void *IP = 0;
1867 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1868 return SDOperand(ON, 0);
1869
Chris Lattner149c58c2005-08-16 18:17:10 +00001870 RemoveNodeFromCSEMaps(N);
1871 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001872 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001873 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001874
Chris Lattnera5682852006-08-07 23:03:03 +00001875 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001876 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001877}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001878
Chris Lattnereb19e402005-11-30 22:45:14 +00001879SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1880 MVT::ValueType VT, SDOperand Op1,
1881 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001882 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001883 MVT::ValueType *VTs = getNodeValueTypes(VT);
1884 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1885 void *IP = 0;
1886 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1887 return SDOperand(ON, 0);
1888
Chris Lattner149c58c2005-08-16 18:17:10 +00001889 RemoveNodeFromCSEMaps(N);
1890 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001891 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001892 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001893
Chris Lattnera5682852006-08-07 23:03:03 +00001894 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001895 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001896}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001897
Chris Lattnereb19e402005-11-30 22:45:14 +00001898SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1899 MVT::ValueType VT, SDOperand Op1,
1900 SDOperand Op2, SDOperand Op3,
1901 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001902 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001903 MVT::ValueType *VTs = getNodeValueTypes(VT);
1904 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1905 ID.AddOperand(Op1);
1906 ID.AddOperand(Op2);
1907 ID.AddOperand(Op3);
1908 ID.AddOperand(Op4);
1909 void *IP = 0;
1910 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1911 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001912
Nate Begeman294a0a12005-08-18 07:30:15 +00001913 RemoveNodeFromCSEMaps(N);
1914 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001915 N->setValueTypes(VTs, 1);
Nate Begeman294a0a12005-08-18 07:30:15 +00001916 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001917
Chris Lattnera5682852006-08-07 23:03:03 +00001918 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001919 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001920}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001921
Chris Lattnereb19e402005-11-30 22:45:14 +00001922SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1923 MVT::ValueType VT, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00001924 SDOperand Op2, SDOperand Op3,
1925 SDOperand Op4, SDOperand Op5) {
1926 MVT::ValueType *VTs = getNodeValueTypes(VT);
1927 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1928 ID.AddOperand(Op1);
1929 ID.AddOperand(Op2);
1930 ID.AddOperand(Op3);
1931 ID.AddOperand(Op4);
1932 ID.AddOperand(Op5);
1933 void *IP = 0;
1934 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1935 return SDOperand(ON, 0);
1936
Chris Lattner6b09a292005-08-21 18:49:33 +00001937 RemoveNodeFromCSEMaps(N);
1938 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001939 N->setValueTypes(VTs, 1);
Chris Lattner6b09a292005-08-21 18:49:33 +00001940 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001941
Chris Lattnera5682852006-08-07 23:03:03 +00001942 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001943 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001944}
Chris Lattner149c58c2005-08-16 18:17:10 +00001945
Chris Lattnereb19e402005-11-30 22:45:14 +00001946SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1947 MVT::ValueType VT, SDOperand Op1,
1948 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1949 SDOperand Op5, SDOperand Op6) {
Chris Lattnera5682852006-08-07 23:03:03 +00001950 MVT::ValueType *VTs = getNodeValueTypes(VT);
1951 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1952 ID.AddOperand(Op1);
1953 ID.AddOperand(Op2);
1954 ID.AddOperand(Op3);
1955 ID.AddOperand(Op4);
1956 ID.AddOperand(Op5);
1957 ID.AddOperand(Op6);
1958 void *IP = 0;
1959 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1960 return SDOperand(ON, 0);
1961
Evan Cheng61ca74b2005-11-30 02:04:11 +00001962 RemoveNodeFromCSEMaps(N);
1963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001964 N->setValueTypes(VTs, 1);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001965 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001966
Chris Lattnera5682852006-08-07 23:03:03 +00001967 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001968 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001969}
1970
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001971SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1972 MVT::ValueType VT, SDOperand Op1,
1973 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1974 SDOperand Op5, SDOperand Op6,
1975 SDOperand Op7) {
Chris Lattnera5682852006-08-07 23:03:03 +00001976 MVT::ValueType *VTs = getNodeValueTypes(VT);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001977 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001978 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1979 ID.AddOperand(Op1);
1980 ID.AddOperand(Op2);
1981 ID.AddOperand(Op3);
1982 ID.AddOperand(Op4);
1983 ID.AddOperand(Op5);
1984 ID.AddOperand(Op6);
1985 ID.AddOperand(Op7);
1986 void *IP = 0;
1987 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1988 return SDOperand(ON, 0);
1989
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001990 RemoveNodeFromCSEMaps(N);
1991 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001992 N->setValueTypes(VTs, 1);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001993 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1994
Chris Lattnera5682852006-08-07 23:03:03 +00001995 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001996 return SDOperand(N, 0);
1997}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001998SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1999 MVT::ValueType VT, SDOperand Op1,
2000 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2001 SDOperand Op5, SDOperand Op6,
2002 SDOperand Op7, SDOperand Op8) {
2003 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002004 MVT::ValueType *VTs = getNodeValueTypes(VT);
2005 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2006 ID.AddOperand(Op1);
2007 ID.AddOperand(Op2);
2008 ID.AddOperand(Op3);
2009 ID.AddOperand(Op4);
2010 ID.AddOperand(Op5);
2011 ID.AddOperand(Op6);
2012 ID.AddOperand(Op7);
2013 ID.AddOperand(Op8);
2014 void *IP = 0;
2015 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2016 return SDOperand(ON, 0);
2017
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002018 RemoveNodeFromCSEMaps(N);
2019 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002020 N->setValueTypes(VTs, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002021 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2022
Chris Lattnera5682852006-08-07 23:03:03 +00002023 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002024 return SDOperand(N, 0);
2025}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002026
Chris Lattnereb19e402005-11-30 22:45:14 +00002027SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2028 MVT::ValueType VT1, MVT::ValueType VT2,
2029 SDOperand Op1, SDOperand Op2) {
Chris Lattnera5682852006-08-07 23:03:03 +00002030 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2031 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2032 void *IP = 0;
2033 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2034 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002035
Chris Lattner0fb094f2005-11-19 01:44:53 +00002036 RemoveNodeFromCSEMaps(N);
2037 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002038 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002039 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002040
Chris Lattnera5682852006-08-07 23:03:03 +00002041 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002042 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002043}
2044
Chris Lattnereb19e402005-11-30 22:45:14 +00002045SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2046 MVT::ValueType VT1, MVT::ValueType VT2,
2047 SDOperand Op1, SDOperand Op2,
2048 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002049 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002050 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2051 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2052 Op1, Op2, Op3);
2053 void *IP = 0;
2054 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2055 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002056
Chris Lattner0fb094f2005-11-19 01:44:53 +00002057 RemoveNodeFromCSEMaps(N);
2058 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002059 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002060 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002061
Chris Lattnera5682852006-08-07 23:03:03 +00002062 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002063 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002064}
2065
Chris Lattnereb19e402005-11-30 22:45:14 +00002066SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2067 MVT::ValueType VT1, MVT::ValueType VT2,
2068 SDOperand Op1, SDOperand Op2,
2069 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002070 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002071 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2072 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2073 ID.AddOperand(Op1);
2074 ID.AddOperand(Op2);
2075 ID.AddOperand(Op3);
2076 ID.AddOperand(Op4);
2077 void *IP = 0;
2078 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2079 return SDOperand(ON, 0);
2080
Chris Lattner0fb094f2005-11-19 01:44:53 +00002081 RemoveNodeFromCSEMaps(N);
2082 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002083 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002084 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002085
Chris Lattnera5682852006-08-07 23:03:03 +00002086 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002087 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002088}
2089
Chris Lattnereb19e402005-11-30 22:45:14 +00002090SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2091 MVT::ValueType VT1, MVT::ValueType VT2,
2092 SDOperand Op1, SDOperand Op2,
2093 SDOperand Op3, SDOperand Op4,
2094 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002095 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002096 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2097 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2098 ID.AddOperand(Op1);
2099 ID.AddOperand(Op2);
2100 ID.AddOperand(Op3);
2101 ID.AddOperand(Op4);
2102 ID.AddOperand(Op5);
2103 void *IP = 0;
2104 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2105 return SDOperand(ON, 0);
2106
Chris Lattner0fb094f2005-11-19 01:44:53 +00002107 RemoveNodeFromCSEMaps(N);
2108 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002109 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002110 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002111
Chris Lattnera5682852006-08-07 23:03:03 +00002112 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002113 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002114}
2115
Evan Cheng6ae46c42006-02-09 07:15:23 +00002116/// getTargetNode - These are used for target selectors to create a new node
2117/// with specified return type(s), target opcode, and operands.
2118///
2119/// Note that getTargetNode returns the resultant node. If there is already a
2120/// node of the specified opcode and operands, it returns that node instead of
2121/// the current one.
2122SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2123 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2124}
2125SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2126 SDOperand Op1) {
2127 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2128}
2129SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2130 SDOperand Op1, SDOperand Op2) {
2131 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2132}
2133SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2134 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2135 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2136}
2137SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2138 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2139 SDOperand Op4) {
2140 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2141}
2142SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2143 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2144 SDOperand Op4, SDOperand Op5) {
2145 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2146}
2147SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2148 SDOperand Op1, SDOperand Op2, SDOperand Op3,
Chris Lattnera5682852006-08-07 23:03:03 +00002149 SDOperand Op4, SDOperand Op5,
2150 SDOperand Op6) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002151 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2152 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002153}
2154SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2155 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2156 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2157 SDOperand Op7) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002158 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2159 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002160}
2161SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2162 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2163 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2164 SDOperand Op7, SDOperand Op8) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002165 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8 };
2166 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 8).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002167}
2168SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002169 const SDOperand *Ops, unsigned NumOps) {
2170 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002171}
2172SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2173 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002174 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2175 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002176}
2177SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002178 MVT::ValueType VT2, SDOperand Op1,
2179 SDOperand Op2) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002180 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002181 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002182 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002183}
2184SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002185 MVT::ValueType VT2, SDOperand Op1,
2186 SDOperand Op2, SDOperand Op3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002187 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002188 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002189 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002190}
2191SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002192 MVT::ValueType VT2, SDOperand Op1,
2193 SDOperand Op2, SDOperand Op3,
2194 SDOperand Op4) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002195 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002196 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002197 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 4).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002198}
2199SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002200 MVT::ValueType VT2, SDOperand Op1,
2201 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2202 SDOperand Op5) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002203 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002204 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002205 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002206}
2207SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002208 MVT::ValueType VT2, SDOperand Op1,
2209 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2210 SDOperand Op5, SDOperand Op6) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002211 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002212 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002213 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002214}
2215SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002216 MVT::ValueType VT2, SDOperand Op1,
2217 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2218 SDOperand Op5, SDOperand Op6,
2219 SDOperand Op7) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002220 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002221 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002222 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002223}
2224SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2225 MVT::ValueType VT2, MVT::ValueType VT3,
2226 SDOperand Op1, SDOperand Op2) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002227 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002228 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002229 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002230}
2231SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2232 MVT::ValueType VT2, MVT::ValueType VT3,
2233 SDOperand Op1, SDOperand Op2,
Chris Lattnera5682852006-08-07 23:03:03 +00002234 SDOperand Op3, SDOperand Op4,
2235 SDOperand Op5) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002236 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002237 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002238 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002239}
2240SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2241 MVT::ValueType VT2, MVT::ValueType VT3,
2242 SDOperand Op1, SDOperand Op2,
2243 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2244 SDOperand Op6) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002245 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002246 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002247 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002248}
2249SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2250 MVT::ValueType VT2, MVT::ValueType VT3,
2251 SDOperand Op1, SDOperand Op2,
2252 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2253 SDOperand Op6, SDOperand Op7) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002254 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002255 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002256 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002257}
2258SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002259 MVT::ValueType VT2,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002260 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002261 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2262 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002263}
2264
Evan Cheng99157a02006-08-07 22:13:29 +00002265/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002266/// This can cause recursive merging of nodes in the DAG.
2267///
Chris Lattner8b52f212005-08-26 18:36:28 +00002268/// This version assumes From/To have a single result value.
2269///
Chris Lattner1e111c72005-09-07 05:37:01 +00002270void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2271 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002272 SDNode *From = FromN.Val, *To = ToN.Val;
2273 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2274 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002275 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002276
Chris Lattner8b8749f2005-08-17 19:00:20 +00002277 while (!From->use_empty()) {
2278 // Process users until they are all gone.
2279 SDNode *U = *From->use_begin();
2280
2281 // This node is about to morph, remove its old self from the CSE maps.
2282 RemoveNodeFromCSEMaps(U);
2283
Chris Lattner65113b22005-11-08 22:07:03 +00002284 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2285 I != E; ++I)
2286 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002287 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002288 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002289 To->addUser(U);
2290 }
2291
2292 // Now that we have modified U, add it back to the CSE maps. If it already
2293 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002294 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2295 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002296 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002297 if (Deleted) Deleted->push_back(U);
2298 DeleteNodeNotInCSEMaps(U);
2299 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002300 }
2301}
2302
2303/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2304/// This can cause recursive merging of nodes in the DAG.
2305///
2306/// This version assumes From/To have matching types and numbers of result
2307/// values.
2308///
Chris Lattner1e111c72005-09-07 05:37:01 +00002309void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2310 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002311 assert(From != To && "Cannot replace uses of with self");
2312 assert(From->getNumValues() == To->getNumValues() &&
2313 "Cannot use this version of ReplaceAllUsesWith!");
2314 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002315 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002316 return;
2317 }
2318
2319 while (!From->use_empty()) {
2320 // Process users until they are all gone.
2321 SDNode *U = *From->use_begin();
2322
2323 // This node is about to morph, remove its old self from the CSE maps.
2324 RemoveNodeFromCSEMaps(U);
2325
Chris Lattner65113b22005-11-08 22:07:03 +00002326 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2327 I != E; ++I)
2328 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002329 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002330 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002331 To->addUser(U);
2332 }
2333
2334 // Now that we have modified U, add it back to the CSE maps. If it already
2335 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002336 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2337 ReplaceAllUsesWith(U, Existing, Deleted);
2338 // U is now dead.
2339 if (Deleted) Deleted->push_back(U);
2340 DeleteNodeNotInCSEMaps(U);
2341 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002342 }
2343}
2344
Chris Lattner8b52f212005-08-26 18:36:28 +00002345/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2346/// This can cause recursive merging of nodes in the DAG.
2347///
2348/// This version can replace From with any result values. To must match the
2349/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002350void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002351 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002352 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002353 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002354 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002355 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002356 return;
2357 }
2358
2359 while (!From->use_empty()) {
2360 // Process users until they are all gone.
2361 SDNode *U = *From->use_begin();
2362
2363 // This node is about to morph, remove its old self from the CSE maps.
2364 RemoveNodeFromCSEMaps(U);
2365
Chris Lattner65113b22005-11-08 22:07:03 +00002366 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2367 I != E; ++I)
2368 if (I->Val == From) {
2369 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002370 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002371 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002372 ToOp.Val->addUser(U);
2373 }
2374
2375 // Now that we have modified U, add it back to the CSE maps. If it already
2376 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002377 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2378 ReplaceAllUsesWith(U, Existing, Deleted);
2379 // U is now dead.
2380 if (Deleted) Deleted->push_back(U);
2381 DeleteNodeNotInCSEMaps(U);
2382 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002383 }
2384}
2385
Chris Lattner012f2412006-02-17 21:58:01 +00002386/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2387/// uses of other values produced by From.Val alone. The Deleted vector is
2388/// handled the same was as for ReplaceAllUsesWith.
2389void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2390 std::vector<SDNode*> &Deleted) {
2391 assert(From != To && "Cannot replace a value with itself");
2392 // Handle the simple, trivial, case efficiently.
2393 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2394 ReplaceAllUsesWith(From, To, &Deleted);
2395 return;
2396 }
2397
2398 // Get all of the users in a nice, deterministically ordered, uniqued set.
2399 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2400
2401 while (!Users.empty()) {
2402 // We know that this user uses some value of From. If it is the right
2403 // value, update it.
2404 SDNode *User = Users.back();
2405 Users.pop_back();
2406
2407 for (SDOperand *Op = User->OperandList,
2408 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2409 if (*Op == From) {
2410 // Okay, we know this user needs to be updated. Remove its old self
2411 // from the CSE maps.
2412 RemoveNodeFromCSEMaps(User);
2413
2414 // Update all operands that match "From".
2415 for (; Op != E; ++Op) {
2416 if (*Op == From) {
2417 From.Val->removeUser(User);
2418 *Op = To;
2419 To.Val->addUser(User);
2420 }
2421 }
2422
2423 // Now that we have modified User, add it back to the CSE maps. If it
2424 // already exists there, recursively merge the results together.
2425 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2426 unsigned NumDeleted = Deleted.size();
2427 ReplaceAllUsesWith(User, Existing, &Deleted);
2428
2429 // User is now dead.
2430 Deleted.push_back(User);
2431 DeleteNodeNotInCSEMaps(User);
2432
2433 // We have to be careful here, because ReplaceAllUsesWith could have
2434 // deleted a user of From, which means there may be dangling pointers
2435 // in the "Users" setvector. Scan over the deleted node pointers and
2436 // remove them from the setvector.
2437 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2438 Users.remove(Deleted[i]);
2439 }
2440 break; // Exit the operand scanning loop.
2441 }
2442 }
2443 }
2444}
2445
Chris Lattner7b2880c2005-08-24 22:44:39 +00002446
Evan Chenge6f35d82006-08-01 08:20:41 +00002447/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2448/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002449unsigned SelectionDAG::AssignNodeIds() {
2450 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002451 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2452 SDNode *N = I;
2453 N->setNodeId(Id++);
2454 }
2455 return Id;
2456}
2457
Evan Chenge6f35d82006-08-01 08:20:41 +00002458/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002459/// based on their topological order. It returns the maximum id and a vector
2460/// of the SDNodes* in assigned order by reference.
2461unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002462 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002463 std::vector<unsigned> InDegree(DAGSize);
2464 std::vector<SDNode*> Sources;
2465
2466 // Use a two pass approach to avoid using a std::map which is slow.
2467 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002468 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2469 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002470 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002471 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002472 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002473 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002474 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002475 }
2476
Evan Cheng99157a02006-08-07 22:13:29 +00002477 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002478 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002479 SDNode *N = Sources.back();
2480 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002481 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002482 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2483 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002484 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002485 if (Degree == 0)
2486 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002487 }
2488 }
2489
Evan Chengc384d6c2006-08-02 22:00:34 +00002490 // Second pass, assign the actual topological order as node ids.
2491 Id = 0;
2492 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2493 TI != TE; ++TI)
2494 (*TI)->setNodeId(Id++);
2495
2496 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002497}
2498
2499
Evan Cheng091cba12006-07-27 06:39:06 +00002500
Jim Laskey58b968b2005-08-17 20:08:02 +00002501//===----------------------------------------------------------------------===//
2502// SDNode Class
2503//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002504
Chris Lattner917d2c92006-07-19 00:00:37 +00002505// Out-of-line virtual method to give class a home.
2506void SDNode::ANCHOR() {
2507}
2508
Chris Lattnera3255112005-11-08 23:30:28 +00002509/// getValueTypeList - Return a pointer to the specified value type.
2510///
2511MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2512 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2513 VTs[VT] = VT;
2514 return &VTs[VT];
2515}
Chris Lattnera5682852006-08-07 23:03:03 +00002516
Chris Lattner5c884562005-01-12 18:37:47 +00002517/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2518/// indicated value. This method ignores uses of other values defined by this
2519/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002520bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002521 assert(Value < getNumValues() && "Bad value!");
2522
2523 // If there is only one value, this is easy.
2524 if (getNumValues() == 1)
2525 return use_size() == NUses;
2526 if (Uses.size() < NUses) return false;
2527
Evan Cheng4ee62112006-02-05 06:29:23 +00002528 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002529
2530 std::set<SDNode*> UsersHandled;
2531
Evan Cheng4ee62112006-02-05 06:29:23 +00002532 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002533 UI != E; ++UI) {
2534 SDNode *User = *UI;
2535 if (User->getNumOperands() == 1 ||
2536 UsersHandled.insert(User).second) // First time we've seen this?
2537 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2538 if (User->getOperand(i) == TheValue) {
2539 if (NUses == 0)
2540 return false; // too many uses
2541 --NUses;
2542 }
2543 }
2544
2545 // Found exactly the right number of uses?
2546 return NUses == 0;
2547}
2548
2549
Evan Cheng4ee62112006-02-05 06:29:23 +00002550// isOnlyUse - Return true if this node is the only use of N.
2551bool SDNode::isOnlyUse(SDNode *N) const {
2552 bool Seen = false;
2553 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2554 SDNode *User = *I;
2555 if (User == this)
2556 Seen = true;
2557 else
2558 return false;
2559 }
2560
2561 return Seen;
2562}
2563
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002564// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002565bool SDOperand::isOperand(SDNode *N) const {
2566 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2567 if (*this == N->getOperand(i))
2568 return true;
2569 return false;
2570}
2571
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002572bool SDNode::isOperand(SDNode *N) const {
2573 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2574 if (this == N->OperandList[i].Val)
2575 return true;
2576 return false;
2577}
Evan Cheng4ee62112006-02-05 06:29:23 +00002578
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002579const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002580 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002581 default:
2582 if (getOpcode() < ISD::BUILTIN_OP_END)
2583 return "<<Unknown DAG Node>>";
2584 else {
Evan Cheng72261582005-12-20 06:22:03 +00002585 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002586 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002587 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2588 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002589
Evan Cheng72261582005-12-20 06:22:03 +00002590 TargetLowering &TLI = G->getTargetLoweringInfo();
2591 const char *Name =
2592 TLI.getTargetNodeName(getOpcode());
2593 if (Name) return Name;
2594 }
2595
2596 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002597 }
2598
Andrew Lenharth95762122005-03-31 21:24:06 +00002599 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002600 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002601 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002602 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002603 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002604 case ISD::AssertSext: return "AssertSext";
2605 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002606
2607 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002608 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002609 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002610 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002611
2612 case ISD::Constant: return "Constant";
2613 case ISD::ConstantFP: return "ConstantFP";
2614 case ISD::GlobalAddress: return "GlobalAddress";
2615 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002616 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002617 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002618 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002619 case ISD::INTRINSIC_WO_CHAIN: {
2620 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2621 return Intrinsic::getName((Intrinsic::ID)IID);
2622 }
2623 case ISD::INTRINSIC_VOID:
2624 case ISD::INTRINSIC_W_CHAIN: {
2625 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002626 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002627 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002628
Chris Lattnerb2827b02006-03-19 00:52:58 +00002629 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002630 case ISD::TargetConstant: return "TargetConstant";
2631 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002632 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2633 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002634 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002635 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002636 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002637
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002638 case ISD::CopyToReg: return "CopyToReg";
2639 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002640 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002641 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002642 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002643 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002644 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002645 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002646
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002647 // Unary operators
2648 case ISD::FABS: return "fabs";
2649 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002650 case ISD::FSQRT: return "fsqrt";
2651 case ISD::FSIN: return "fsin";
2652 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002653
2654 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002655 case ISD::ADD: return "add";
2656 case ISD::SUB: return "sub";
2657 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002658 case ISD::MULHU: return "mulhu";
2659 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002660 case ISD::SDIV: return "sdiv";
2661 case ISD::UDIV: return "udiv";
2662 case ISD::SREM: return "srem";
2663 case ISD::UREM: return "urem";
2664 case ISD::AND: return "and";
2665 case ISD::OR: return "or";
2666 case ISD::XOR: return "xor";
2667 case ISD::SHL: return "shl";
2668 case ISD::SRA: return "sra";
2669 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002670 case ISD::ROTL: return "rotl";
2671 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002672 case ISD::FADD: return "fadd";
2673 case ISD::FSUB: return "fsub";
2674 case ISD::FMUL: return "fmul";
2675 case ISD::FDIV: return "fdiv";
2676 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002677 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002678 case ISD::VADD: return "vadd";
2679 case ISD::VSUB: return "vsub";
2680 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002681 case ISD::VSDIV: return "vsdiv";
2682 case ISD::VUDIV: return "vudiv";
2683 case ISD::VAND: return "vand";
2684 case ISD::VOR: return "vor";
2685 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002686
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002687 case ISD::SETCC: return "setcc";
2688 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002689 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002690 case ISD::VSELECT: return "vselect";
2691 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2692 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2693 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002694 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002695 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2696 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2697 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2698 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2699 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002700 case ISD::ADDC: return "addc";
2701 case ISD::ADDE: return "adde";
2702 case ISD::SUBC: return "subc";
2703 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002704 case ISD::SHL_PARTS: return "shl_parts";
2705 case ISD::SRA_PARTS: return "sra_parts";
2706 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002707
Chris Lattner7f644642005-04-28 21:44:03 +00002708 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002709 case ISD::SIGN_EXTEND: return "sign_extend";
2710 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002711 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002712 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002713 case ISD::TRUNCATE: return "truncate";
2714 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002715 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002716 case ISD::FP_EXTEND: return "fp_extend";
2717
2718 case ISD::SINT_TO_FP: return "sint_to_fp";
2719 case ISD::UINT_TO_FP: return "uint_to_fp";
2720 case ISD::FP_TO_SINT: return "fp_to_sint";
2721 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002722 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002723
2724 // Control flow instructions
2725 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002726 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002727 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002728 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002729 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002730 case ISD::CALLSEQ_START: return "callseq_start";
2731 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002732
2733 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002734 case ISD::LOAD: return "load";
2735 case ISD::STORE: return "store";
2736 case ISD::VLOAD: return "vload";
2737 case ISD::EXTLOAD: return "extload";
2738 case ISD::SEXTLOAD: return "sextload";
2739 case ISD::ZEXTLOAD: return "zextload";
2740 case ISD::TRUNCSTORE: return "truncstore";
2741 case ISD::VAARG: return "vaarg";
2742 case ISD::VACOPY: return "vacopy";
2743 case ISD::VAEND: return "vaend";
2744 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002745 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002746 case ISD::EXTRACT_ELEMENT: return "extract_element";
2747 case ISD::BUILD_PAIR: return "build_pair";
2748 case ISD::STACKSAVE: return "stacksave";
2749 case ISD::STACKRESTORE: return "stackrestore";
2750
2751 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002752 case ISD::MEMSET: return "memset";
2753 case ISD::MEMCPY: return "memcpy";
2754 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002755
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002756 // Bit manipulation
2757 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002758 case ISD::CTPOP: return "ctpop";
2759 case ISD::CTTZ: return "cttz";
2760 case ISD::CTLZ: return "ctlz";
2761
Chris Lattner36ce6912005-11-29 06:21:05 +00002762 // Debug info
2763 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002764 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002765 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002766
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002767 case ISD::CONDCODE:
2768 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002769 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002770 case ISD::SETOEQ: return "setoeq";
2771 case ISD::SETOGT: return "setogt";
2772 case ISD::SETOGE: return "setoge";
2773 case ISD::SETOLT: return "setolt";
2774 case ISD::SETOLE: return "setole";
2775 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002776
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002777 case ISD::SETO: return "seto";
2778 case ISD::SETUO: return "setuo";
2779 case ISD::SETUEQ: return "setue";
2780 case ISD::SETUGT: return "setugt";
2781 case ISD::SETUGE: return "setuge";
2782 case ISD::SETULT: return "setult";
2783 case ISD::SETULE: return "setule";
2784 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002785
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002786 case ISD::SETEQ: return "seteq";
2787 case ISD::SETGT: return "setgt";
2788 case ISD::SETGE: return "setge";
2789 case ISD::SETLT: return "setlt";
2790 case ISD::SETLE: return "setle";
2791 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002792 }
2793 }
2794}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002795
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002796void SDNode::dump() const { dump(0); }
2797void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002798 std::cerr << (void*)this << ": ";
2799
2800 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2801 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002802 if (getValueType(i) == MVT::Other)
2803 std::cerr << "ch";
2804 else
2805 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002806 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002807 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002808
2809 std::cerr << " ";
2810 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2811 if (i) std::cerr << ", ";
2812 std::cerr << (void*)getOperand(i).Val;
2813 if (unsigned RN = getOperand(i).ResNo)
2814 std::cerr << ":" << RN;
2815 }
2816
2817 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2818 std::cerr << "<" << CSDN->getValue() << ">";
2819 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2820 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002821 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002822 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002823 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002824 std::cerr << "<";
2825 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002826 if (offset > 0)
2827 std::cerr << " + " << offset;
2828 else
2829 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002830 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002831 std::cerr << "<" << FIDN->getIndex() << ">";
2832 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002833 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002834 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002835 if (offset > 0)
2836 std::cerr << " + " << offset;
2837 else
2838 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002839 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002840 std::cerr << "<";
2841 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2842 if (LBB)
2843 std::cerr << LBB->getName() << " ";
2844 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002845 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002846 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002847 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2848 } else {
2849 std::cerr << " #" << R->getReg();
2850 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002851 } else if (const ExternalSymbolSDNode *ES =
2852 dyn_cast<ExternalSymbolSDNode>(this)) {
2853 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002854 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2855 if (M->getValue())
2856 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2857 else
2858 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002859 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2860 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002861 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002862}
2863
Chris Lattnerde202b32005-11-09 23:47:37 +00002864static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002865 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2866 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002867 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002868 else
2869 std::cerr << "\n" << std::string(indent+2, ' ')
2870 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002871
Chris Lattnerea946cd2005-01-09 20:38:33 +00002872
2873 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002874 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002875}
2876
Chris Lattnerc3aae252005-01-07 07:46:32 +00002877void SelectionDAG::dump() const {
2878 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002879 std::vector<const SDNode*> Nodes;
2880 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2881 I != E; ++I)
2882 Nodes.push_back(I);
2883
Chris Lattner49d24712005-01-09 20:26:36 +00002884 std::sort(Nodes.begin(), Nodes.end());
2885
2886 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002887 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002888 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002889 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002890
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002891 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002892
Chris Lattnerc3aae252005-01-07 07:46:32 +00002893 std::cerr << "\n\n";
2894}
2895