blob: 6fd8ba2a9ec4e56ef32da2e84aa062d1791bbd8c [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 Lattner65113b22005-11-08 22:07:03 +0000472 delete [] N->OperandList;
473 N->OperandList = 0;
474 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000475 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000476 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000477}
478
Chris Lattner0f2287b2005-04-13 02:38:18 +0000479SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000480 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000481 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000482 return getNode(ISD::AND, Op.getValueType(), Op,
483 getConstant(Imm, Op.getValueType()));
484}
485
Chris Lattner36ce6912005-11-29 06:21:05 +0000486SDOperand SelectionDAG::getString(const std::string &Val) {
487 StringSDNode *&N = StringNodes[Val];
488 if (!N) {
489 N = new StringSDNode(Val);
490 AllNodes.push_back(N);
491 }
492 return SDOperand(N, 0);
493}
494
Chris Lattner61b09412006-08-11 21:01:22 +0000495SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000496 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000497 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000498
Chris Lattner61b09412006-08-11 21:01:22 +0000499 // Mask out any bits that are not valid for this constant.
500 Val &= MVT::getIntVTBitMask(VT);
501
502 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
503 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
504 ID.AddInteger(Val);
505 void *IP = 0;
506 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
507 return SDOperand(E, 0);
508 SDNode *N = new ConstantSDNode(isT, Val, VT);
509 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000510 AllNodes.push_back(N);
511 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000512}
513
Chris Lattner61b09412006-08-11 21:01:22 +0000514
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000515SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
516 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000517 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
518 if (VT == MVT::f32)
519 Val = (float)Val; // Mask out extra precision.
520
Chris Lattnerd8658612005-02-17 20:17:32 +0000521 // Do the map lookup using the actual bit pattern for the floating point
522 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
523 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000524 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
525 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
526 ID.AddInteger(DoubleToBits(Val));
527 void *IP = 0;
528 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
529 return SDOperand(E, 0);
530 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
531 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000532 AllNodes.push_back(N);
533 return SDOperand(N, 0);
534}
535
Chris Lattnerc3aae252005-01-07 07:46:32 +0000536SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000537 MVT::ValueType VT, int Offset,
538 bool isTargetGA) {
539 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
540 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
541 ID.AddPointer(GV);
542 ID.AddInteger(Offset);
543 void *IP = 0;
544 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
545 return SDOperand(E, 0);
546 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
547 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000548 AllNodes.push_back(N);
549 return SDOperand(N, 0);
550}
551
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000552SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
553 bool isTarget) {
554 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
555 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
556 ID.AddInteger(FI);
557 void *IP = 0;
558 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
559 return SDOperand(E, 0);
560 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
561 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000562 AllNodes.push_back(N);
563 return SDOperand(N, 0);
564}
565
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000566SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
567 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
568 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
569 ID.AddInteger(JTI);
570 void *IP = 0;
571 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
572 return SDOperand(E, 0);
573 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
574 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000575 AllNodes.push_back(N);
576 return SDOperand(N, 0);
577}
578
Evan Chengb8973bd2006-01-31 22:23:14 +0000579SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000580 unsigned Alignment, int Offset,
581 bool isTarget) {
582 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
583 SelectionDAGCSEMap::NodeID ID(Opc, getNodeValueTypes(VT));
584 ID.AddInteger(Alignment);
585 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000586 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000587 void *IP = 0;
588 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
589 return SDOperand(E, 0);
590 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
591 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000592 AllNodes.push_back(N);
593 return SDOperand(N, 0);
594}
595
Chris Lattnerc3aae252005-01-07 07:46:32 +0000596
597SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner61b09412006-08-11 21:01:22 +0000598 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getNodeValueTypes(MVT::Other));
599 ID.AddPointer(MBB);
600 void *IP = 0;
601 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
602 return SDOperand(E, 0);
603 SDNode *N = new BasicBlockSDNode(MBB);
604 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000605 AllNodes.push_back(N);
606 return SDOperand(N, 0);
607}
608
Chris Lattner15e4b012005-07-10 00:07:11 +0000609SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
610 if ((unsigned)VT >= ValueTypeNodes.size())
611 ValueTypeNodes.resize(VT+1);
612 if (ValueTypeNodes[VT] == 0) {
613 ValueTypeNodes[VT] = new VTSDNode(VT);
614 AllNodes.push_back(ValueTypeNodes[VT]);
615 }
616
617 return SDOperand(ValueTypeNodes[VT], 0);
618}
619
Chris Lattnerc3aae252005-01-07 07:46:32 +0000620SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
621 SDNode *&N = ExternalSymbols[Sym];
622 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000623 N = new ExternalSymbolSDNode(false, Sym, VT);
624 AllNodes.push_back(N);
625 return SDOperand(N, 0);
626}
627
Chris Lattner809ec112006-01-28 10:09:25 +0000628SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
629 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000630 SDNode *&N = TargetExternalSymbols[Sym];
631 if (N) return SDOperand(N, 0);
632 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000633 AllNodes.push_back(N);
634 return SDOperand(N, 0);
635}
636
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000637SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
638 if ((unsigned)Cond >= CondCodeNodes.size())
639 CondCodeNodes.resize(Cond+1);
640
Chris Lattner079a27a2005-08-09 20:40:02 +0000641 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000642 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000643 AllNodes.push_back(CondCodeNodes[Cond]);
644 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000645 return SDOperand(CondCodeNodes[Cond], 0);
646}
647
Chris Lattner0fdd7682005-08-30 22:38:38 +0000648SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner61b09412006-08-11 21:01:22 +0000649 SelectionDAGCSEMap::NodeID ID(ISD::Register, getNodeValueTypes(VT));
650 ID.AddInteger(RegNo);
651 void *IP = 0;
652 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
653 return SDOperand(E, 0);
654 SDNode *N = new RegisterSDNode(RegNo, VT);
655 CSEMap.InsertNode(N, IP);
656 AllNodes.push_back(N);
657 return SDOperand(N, 0);
658}
659
660SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
661 assert((!V || isa<PointerType>(V->getType())) &&
662 "SrcValue is not a pointer?");
663
664 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getNodeValueTypes(MVT::Other));
665 ID.AddPointer(V);
666 ID.AddInteger(Offset);
667 void *IP = 0;
668 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
669 return SDOperand(E, 0);
670 SDNode *N = new SrcValueSDNode(V, Offset);
671 CSEMap.InsertNode(N, IP);
672 AllNodes.push_back(N);
673 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000674}
675
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000676SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
677 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000678 // These setcc operations always fold.
679 switch (Cond) {
680 default: break;
681 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000682 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000683 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000684 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000685
686 case ISD::SETOEQ:
687 case ISD::SETOGT:
688 case ISD::SETOGE:
689 case ISD::SETOLT:
690 case ISD::SETOLE:
691 case ISD::SETONE:
692 case ISD::SETO:
693 case ISD::SETUO:
694 case ISD::SETUEQ:
695 case ISD::SETUNE:
696 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
697 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000698 }
699
Chris Lattner67255a12005-04-07 18:14:58 +0000700 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
701 uint64_t C2 = N2C->getValue();
702 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
703 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000704
Chris Lattnerc3aae252005-01-07 07:46:32 +0000705 // Sign extend the operands if required
706 if (ISD::isSignedIntSetCC(Cond)) {
707 C1 = N1C->getSignExtended();
708 C2 = N2C->getSignExtended();
709 }
710
711 switch (Cond) {
712 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000713 case ISD::SETEQ: return getConstant(C1 == C2, VT);
714 case ISD::SETNE: return getConstant(C1 != C2, VT);
715 case ISD::SETULT: return getConstant(C1 < C2, VT);
716 case ISD::SETUGT: return getConstant(C1 > C2, VT);
717 case ISD::SETULE: return getConstant(C1 <= C2, VT);
718 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
719 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
720 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
721 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
722 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000723 }
Chris Lattner24673922005-04-07 18:58:54 +0000724 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000725 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000726 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
727 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
728
729 // If the comparison constant has bits in the upper part, the
730 // zero-extended value could never match.
731 if (C2 & (~0ULL << InSize)) {
732 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
733 switch (Cond) {
734 case ISD::SETUGT:
735 case ISD::SETUGE:
736 case ISD::SETEQ: return getConstant(0, VT);
737 case ISD::SETULT:
738 case ISD::SETULE:
739 case ISD::SETNE: return getConstant(1, VT);
740 case ISD::SETGT:
741 case ISD::SETGE:
742 // True if the sign bit of C2 is set.
743 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
744 case ISD::SETLT:
745 case ISD::SETLE:
746 // True if the sign bit of C2 isn't set.
747 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
748 default:
749 break;
750 }
751 }
752
753 // Otherwise, we can perform the comparison with the low bits.
754 switch (Cond) {
755 case ISD::SETEQ:
756 case ISD::SETNE:
757 case ISD::SETUGT:
758 case ISD::SETUGE:
759 case ISD::SETULT:
760 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000761 return getSetCC(VT, N1.getOperand(0),
762 getConstant(C2, N1.getOperand(0).getValueType()),
763 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000764 default:
765 break; // todo, be more careful with signed comparisons
766 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000767 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
768 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
769 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
770 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
771 MVT::ValueType ExtDstTy = N1.getValueType();
772 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000773
Chris Lattner7b2880c2005-08-24 22:44:39 +0000774 // If the extended part has any inconsistent bits, it cannot ever
775 // compare equal. In other words, they have to be all ones or all
776 // zeros.
777 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000778 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000779 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
780 return getConstant(Cond == ISD::SETNE, VT);
781
782 // Otherwise, make this a use of a zext.
783 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000784 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000785 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000786 }
787
Chris Lattner67255a12005-04-07 18:14:58 +0000788 uint64_t MinVal, MaxVal;
789 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
790 if (ISD::isSignedIntSetCC(Cond)) {
791 MinVal = 1ULL << (OperandBitSize-1);
792 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
793 MaxVal = ~0ULL >> (65-OperandBitSize);
794 else
795 MaxVal = 0;
796 } else {
797 MinVal = 0;
798 MaxVal = ~0ULL >> (64-OperandBitSize);
799 }
800
801 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
802 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
803 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
804 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000805 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
806 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000807 }
808
809 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
810 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
811 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000812 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
813 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000814 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000815
Nate Begeman72ea2812005-04-14 08:56:52 +0000816 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
817 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000818
Nate Begeman72ea2812005-04-14 08:56:52 +0000819 // Canonicalize setgt X, Min --> setne X, Min
820 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000821 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000822
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000823 // If we have setult X, 1, turn it into seteq X, 0
824 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000825 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
826 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000827 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000828 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000829 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
830 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000831
832 // If we have "setcc X, C1", check to see if we can shrink the immediate
833 // by changing cc.
834
835 // SETUGT X, SINTMAX -> SETLT X, 0
836 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
837 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000838 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000839
840 // FIXME: Implement the rest of these.
841
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000842
843 // Fold bit comparisons when we can.
844 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
845 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
846 if (ConstantSDNode *AndRHS =
847 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
848 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
849 // Perform the xform if the AND RHS is a single bit.
850 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
851 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000852 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000853 TLI.getShiftAmountTy()));
854 }
855 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
856 // (X & 8) == 8 --> (X & 8) >> 3
857 // Perform the xform if C2 is a single bit.
858 if ((C2 & (C2-1)) == 0) {
859 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000860 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000861 }
862 }
863 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000864 }
Chris Lattner67255a12005-04-07 18:14:58 +0000865 } else if (isa<ConstantSDNode>(N1.Val)) {
866 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000867 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000868 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000869
870 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
871 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
872 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000873
Chris Lattnerc3aae252005-01-07 07:46:32 +0000874 switch (Cond) {
875 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000876 case ISD::SETEQ: return getConstant(C1 == C2, VT);
877 case ISD::SETNE: return getConstant(C1 != C2, VT);
878 case ISD::SETLT: return getConstant(C1 < C2, VT);
879 case ISD::SETGT: return getConstant(C1 > C2, VT);
880 case ISD::SETLE: return getConstant(C1 <= C2, VT);
881 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000882 }
883 } else {
884 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000885 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000886 }
887
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000888 // Could not fold it.
889 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000890}
891
Chris Lattnerc3aae252005-01-07 07:46:32 +0000892/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000893///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000894SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner4a283e92006-08-11 18:38:11 +0000895 MVT::ValueType *VTs = getNodeValueTypes(VT);
896 SelectionDAGCSEMap::NodeID ID(Opcode, VTs);
897 void *IP = 0;
898 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
899 return SDOperand(E, 0);
900 SDNode *N = new SDNode(Opcode, VT);
901 CSEMap.InsertNode(N, IP);
902
903 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000904 return SDOperand(N, 0);
905}
906
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
908 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000909 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000910 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
912 uint64_t Val = C->getValue();
913 switch (Opcode) {
914 default: break;
915 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000916 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000917 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
918 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000919 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
920 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000921 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000922 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000923 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000924 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000925 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000926 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000927 case ISD::BSWAP:
928 switch(VT) {
929 default: assert(0 && "Invalid bswap!"); break;
930 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
931 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
932 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
933 }
934 break;
935 case ISD::CTPOP:
936 switch(VT) {
937 default: assert(0 && "Invalid ctpop!"); break;
938 case MVT::i1: return getConstant(Val != 0, VT);
939 case MVT::i8:
940 Tmp1 = (unsigned)Val & 0xFF;
941 return getConstant(CountPopulation_32(Tmp1), VT);
942 case MVT::i16:
943 Tmp1 = (unsigned)Val & 0xFFFF;
944 return getConstant(CountPopulation_32(Tmp1), VT);
945 case MVT::i32:
946 return getConstant(CountPopulation_32((unsigned)Val), VT);
947 case MVT::i64:
948 return getConstant(CountPopulation_64(Val), VT);
949 }
950 case ISD::CTLZ:
951 switch(VT) {
952 default: assert(0 && "Invalid ctlz!"); break;
953 case MVT::i1: return getConstant(Val == 0, VT);
954 case MVT::i8:
955 Tmp1 = (unsigned)Val & 0xFF;
956 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
957 case MVT::i16:
958 Tmp1 = (unsigned)Val & 0xFFFF;
959 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
960 case MVT::i32:
961 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
962 case MVT::i64:
963 return getConstant(CountLeadingZeros_64(Val), VT);
964 }
965 case ISD::CTTZ:
966 switch(VT) {
967 default: assert(0 && "Invalid cttz!"); break;
968 case MVT::i1: return getConstant(Val == 0, VT);
969 case MVT::i8:
970 Tmp1 = (unsigned)Val | 0x100;
971 return getConstant(CountTrailingZeros_32(Tmp1), VT);
972 case MVT::i16:
973 Tmp1 = (unsigned)Val | 0x10000;
974 return getConstant(CountTrailingZeros_32(Tmp1), VT);
975 case MVT::i32:
976 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
977 case MVT::i64:
978 return getConstant(CountTrailingZeros_64(Val), VT);
979 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000980 }
981 }
982
Chris Lattner94683772005-12-23 05:30:37 +0000983 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000984 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
985 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000986 case ISD::FNEG:
987 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000988 case ISD::FABS:
989 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000990 case ISD::FP_ROUND:
991 case ISD::FP_EXTEND:
992 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000993 case ISD::FP_TO_SINT:
994 return getConstant((int64_t)C->getValue(), VT);
995 case ISD::FP_TO_UINT:
996 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000997 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000998 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +0000999 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001000 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001001 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001002 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001003 }
1004
1005 unsigned OpOpcode = Operand.Val->getOpcode();
1006 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001007 case ISD::TokenFactor:
1008 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001009 case ISD::SIGN_EXTEND:
1010 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001011 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001012 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1013 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1014 break;
1015 case ISD::ZERO_EXTEND:
1016 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001017 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001018 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001019 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001020 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001021 case ISD::ANY_EXTEND:
1022 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001023 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001024 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1025 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1026 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1027 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001028 case ISD::TRUNCATE:
1029 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001030 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031 if (OpOpcode == ISD::TRUNCATE)
1032 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001033 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1034 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001035 // If the source is smaller than the dest, we still need an extend.
1036 if (Operand.Val->getOperand(0).getValueType() < VT)
1037 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1038 else if (Operand.Val->getOperand(0).getValueType() > VT)
1039 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1040 else
1041 return Operand.Val->getOperand(0);
1042 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001043 break;
Chris Lattner35481892005-12-23 00:16:34 +00001044 case ISD::BIT_CONVERT:
1045 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001046 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001047 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001048 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001049 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1050 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001051 if (OpOpcode == ISD::UNDEF)
1052 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001053 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001054 case ISD::SCALAR_TO_VECTOR:
1055 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1056 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1057 "Illegal SCALAR_TO_VECTOR node!");
1058 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001059 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001060 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1061 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001062 Operand.Val->getOperand(0));
1063 if (OpOpcode == ISD::FNEG) // --X -> X
1064 return Operand.Val->getOperand(0);
1065 break;
1066 case ISD::FABS:
1067 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1068 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1069 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070 }
1071
Chris Lattner43247a12005-08-25 19:12:10 +00001072 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001073 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001074 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001075 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1076 void *IP = 0;
1077 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1078 return SDOperand(E, 0);
1079 N = new SDNode(Opcode, Operand);
1080 N->setValueTypes(VTs, 1);
1081 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001082 } else {
1083 N = new SDNode(Opcode, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001084 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001085 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 AllNodes.push_back(N);
1087 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001088}
1089
Chris Lattner36019aa2005-04-18 03:48:41 +00001090
1091
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1093 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001094#ifndef NDEBUG
1095 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001096 case ISD::TokenFactor:
1097 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1098 N2.getValueType() == MVT::Other && "Invalid token factor!");
1099 break;
Chris Lattner76365122005-01-16 02:23:22 +00001100 case ISD::AND:
1101 case ISD::OR:
1102 case ISD::XOR:
1103 case ISD::UDIV:
1104 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001105 case ISD::MULHU:
1106 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001107 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1108 // fall through
1109 case ISD::ADD:
1110 case ISD::SUB:
1111 case ISD::MUL:
1112 case ISD::SDIV:
1113 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001114 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1115 // fall through.
1116 case ISD::FADD:
1117 case ISD::FSUB:
1118 case ISD::FMUL:
1119 case ISD::FDIV:
1120 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001121 assert(N1.getValueType() == N2.getValueType() &&
1122 N1.getValueType() == VT && "Binary operator types must match!");
1123 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001124 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1125 assert(N1.getValueType() == VT &&
1126 MVT::isFloatingPoint(N1.getValueType()) &&
1127 MVT::isFloatingPoint(N2.getValueType()) &&
1128 "Invalid FCOPYSIGN!");
1129 break;
Chris Lattner76365122005-01-16 02:23:22 +00001130 case ISD::SHL:
1131 case ISD::SRA:
1132 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001133 case ISD::ROTL:
1134 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001135 assert(VT == N1.getValueType() &&
1136 "Shift operators return type must be the same as their first arg");
1137 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001138 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001139 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001140 case ISD::FP_ROUND_INREG: {
1141 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1142 assert(VT == N1.getValueType() && "Not an inreg round!");
1143 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1144 "Cannot FP_ROUND_INREG integer types");
1145 assert(EVT <= VT && "Not rounding down!");
1146 break;
1147 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001148 case ISD::AssertSext:
1149 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001150 case ISD::SIGN_EXTEND_INREG: {
1151 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1152 assert(VT == N1.getValueType() && "Not an inreg extend!");
1153 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1154 "Cannot *_EXTEND_INREG FP types");
1155 assert(EVT <= VT && "Not extending!");
1156 }
1157
Chris Lattner76365122005-01-16 02:23:22 +00001158 default: break;
1159 }
1160#endif
1161
Chris Lattnerc3aae252005-01-07 07:46:32 +00001162 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1163 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1164 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001165 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1166 int64_t Val = N1C->getValue();
1167 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1168 Val <<= 64-FromBits;
1169 Val >>= 64-FromBits;
1170 return getConstant(Val, VT);
1171 }
1172
Chris Lattnerc3aae252005-01-07 07:46:32 +00001173 if (N2C) {
1174 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1175 switch (Opcode) {
1176 case ISD::ADD: return getConstant(C1 + C2, VT);
1177 case ISD::SUB: return getConstant(C1 - C2, VT);
1178 case ISD::MUL: return getConstant(C1 * C2, VT);
1179 case ISD::UDIV:
1180 if (C2) return getConstant(C1 / C2, VT);
1181 break;
1182 case ISD::UREM :
1183 if (C2) return getConstant(C1 % C2, VT);
1184 break;
1185 case ISD::SDIV :
1186 if (C2) return getConstant(N1C->getSignExtended() /
1187 N2C->getSignExtended(), VT);
1188 break;
1189 case ISD::SREM :
1190 if (C2) return getConstant(N1C->getSignExtended() %
1191 N2C->getSignExtended(), VT);
1192 break;
1193 case ISD::AND : return getConstant(C1 & C2, VT);
1194 case ISD::OR : return getConstant(C1 | C2, VT);
1195 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001196 case ISD::SHL : return getConstant(C1 << C2, VT);
1197 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001198 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001199 case ISD::ROTL :
1200 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1201 VT);
1202 case ISD::ROTR :
1203 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1204 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001205 default: break;
1206 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001207 } else { // Cannonicalize constant to RHS if commutative
1208 if (isCommutativeBinOp(Opcode)) {
1209 std::swap(N1C, N2C);
1210 std::swap(N1, N2);
1211 }
1212 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001213 }
1214
1215 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1216 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001217 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001218 if (N2CFP) {
1219 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1220 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001221 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1222 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1223 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1224 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001225 if (C2) return getConstantFP(C1 / C2, VT);
1226 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001227 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001228 if (C2) return getConstantFP(fmod(C1, C2), VT);
1229 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001230 case ISD::FCOPYSIGN: {
1231 union {
1232 double F;
1233 uint64_t I;
1234 } u1;
1235 union {
1236 double F;
1237 int64_t I;
1238 } u2;
1239 u1.F = C1;
1240 u2.F = C2;
1241 if (u2.I < 0) // Sign bit of RHS set?
1242 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1243 else
1244 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1245 return getConstantFP(u1.F, VT);
1246 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001247 default: break;
1248 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 } else { // Cannonicalize constant to RHS if commutative
1250 if (isCommutativeBinOp(Opcode)) {
1251 std::swap(N1CFP, N2CFP);
1252 std::swap(N1, N2);
1253 }
1254 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001255 }
Chris Lattner62b57722006-04-20 05:39:12 +00001256
1257 // Canonicalize an UNDEF to the RHS, even over a constant.
1258 if (N1.getOpcode() == ISD::UNDEF) {
1259 if (isCommutativeBinOp(Opcode)) {
1260 std::swap(N1, N2);
1261 } else {
1262 switch (Opcode) {
1263 case ISD::FP_ROUND_INREG:
1264 case ISD::SIGN_EXTEND_INREG:
1265 case ISD::SUB:
1266 case ISD::FSUB:
1267 case ISD::FDIV:
1268 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001269 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001270 return N1; // fold op(undef, arg2) -> undef
1271 case ISD::UDIV:
1272 case ISD::SDIV:
1273 case ISD::UREM:
1274 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001275 case ISD::SRL:
1276 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001277 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1278 }
1279 }
1280 }
1281
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001282 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001283 if (N2.getOpcode() == ISD::UNDEF) {
1284 switch (Opcode) {
1285 case ISD::ADD:
1286 case ISD::SUB:
1287 case ISD::FADD:
1288 case ISD::FSUB:
1289 case ISD::FMUL:
1290 case ISD::FDIV:
1291 case ISD::FREM:
1292 case ISD::UDIV:
1293 case ISD::SDIV:
1294 case ISD::UREM:
1295 case ISD::SREM:
1296 case ISD::XOR:
1297 return N2; // fold op(arg1, undef) -> undef
1298 case ISD::MUL:
1299 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001300 case ISD::SRL:
1301 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001302 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1303 case ISD::OR:
1304 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001305 case ISD::SRA:
1306 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001307 }
1308 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001309
Chris Lattnerc3aae252005-01-07 07:46:32 +00001310 // Finally, fold operations that do not require constants.
1311 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001312 case ISD::FP_ROUND_INREG:
1313 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1314 break;
1315 case ISD::SIGN_EXTEND_INREG: {
1316 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1317 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001318 break;
1319 }
1320
Nate Begemaneea805e2005-04-13 21:23:31 +00001321 // FIXME: figure out how to safely handle things like
1322 // int foo(int x) { return 1 << (x & 255); }
1323 // int bar() { return foo(256); }
1324#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001325 case ISD::SHL:
1326 case ISD::SRL:
1327 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001328 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001329 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001330 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001331 else if (N2.getOpcode() == ISD::AND)
1332 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1333 // If the and is only masking out bits that cannot effect the shift,
1334 // eliminate the and.
1335 unsigned NumBits = MVT::getSizeInBits(VT);
1336 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1337 return getNode(Opcode, VT, N1, N2.getOperand(0));
1338 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001339 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001340#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001341 }
1342
Chris Lattner27e9b412005-05-11 18:57:39 +00001343 // Memoize this node if possible.
1344 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001345 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001346 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001347 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1348 void *IP = 0;
1349 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1350 return SDOperand(E, 0);
1351 N = new SDNode(Opcode, N1, N2);
1352 N->setValueTypes(VTs, 1);
1353 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001354 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001355 N = new SDNode(Opcode, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00001356 N->setValueTypes(VTs, 1);
Chris Lattner27e9b412005-05-11 18:57:39 +00001357 }
1358
Chris Lattnerc3aae252005-01-07 07:46:32 +00001359 AllNodes.push_back(N);
1360 return SDOperand(N, 0);
1361}
1362
Chris Lattnerc3aae252005-01-07 07:46:32 +00001363SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1364 SDOperand N1, SDOperand N2, SDOperand N3) {
1365 // Perform various simplifications.
1366 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1367 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001368 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001369 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001370 case ISD::SETCC: {
1371 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001372 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001373 if (Simp.Val) return Simp;
1374 break;
1375 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001376 case ISD::SELECT:
1377 if (N1C)
1378 if (N1C->getValue())
1379 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001380 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001381 return N3; // select false, X, Y -> Y
1382
1383 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001384 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001385 case ISD::BRCOND:
1386 if (N2C)
1387 if (N2C->getValue()) // Unconditional branch
1388 return getNode(ISD::BR, MVT::Other, N1, N3);
1389 else
1390 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001391 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001392 case ISD::VECTOR_SHUFFLE:
1393 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1394 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1395 N3.getOpcode() == ISD::BUILD_VECTOR &&
1396 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1397 "Illegal VECTOR_SHUFFLE node!");
1398 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001399 }
1400
Chris Lattner43247a12005-08-25 19:12:10 +00001401 // Memoize node if it doesn't produce a flag.
1402 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001403 MVT::ValueType *VTs = getNodeValueTypes(VT);
1404
Chris Lattner43247a12005-08-25 19:12:10 +00001405 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001406 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1407 void *IP = 0;
1408 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1409 return SDOperand(E, 0);
1410 N = new SDNode(Opcode, N1, N2, N3);
1411 N->setValueTypes(VTs, 1);
1412 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001413 } else {
1414 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00001415 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001416 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001417 AllNodes.push_back(N);
1418 return SDOperand(N, 0);
1419}
1420
1421SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001422 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001423 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001424 SDOperand Ops[] = { N1, N2, N3, N4 };
1425 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001426}
1427
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001428SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1429 SDOperand N1, SDOperand N2, SDOperand N3,
1430 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001431 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1432 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001433}
1434
Evan Cheng7038daf2005-12-10 00:37:58 +00001435SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1436 SDOperand Chain, SDOperand Ptr,
1437 SDOperand SV) {
Chris Lattnera5682852006-08-07 23:03:03 +00001438 MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
1439
1440 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1441 void *IP = 0;
1442 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1443 return SDOperand(E, 0);
1444 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1445 N->setValueTypes(VTs, 2);
1446 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001447 AllNodes.push_back(N);
1448 return SDOperand(N, 0);
1449}
1450
1451SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1452 SDOperand Chain, SDOperand Ptr,
1453 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001454 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1455 getValueType(EVT) };
Evan Cheng7038daf2005-12-10 00:37:58 +00001456 std::vector<MVT::ValueType> VTs;
1457 VTs.reserve(2);
1458 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001459 return getNode(ISD::VLOAD, VTs, 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) };
Evan Cheng7038daf2005-12-10 00:37:58 +00001466 std::vector<MVT::ValueType> VTs;
1467 VTs.reserve(2);
1468 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001469 return getNode(Opcode, VTs, Ops, 4);
Evan Cheng7038daf2005-12-10 00:37:58 +00001470}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001471
Nate Begemanacc398c2006-01-25 18:21:52 +00001472SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1473 SDOperand Chain, SDOperand Ptr,
1474 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001475 SDOperand Ops[] = { Chain, Ptr, SV };
Nate Begemanacc398c2006-01-25 18:21:52 +00001476 std::vector<MVT::ValueType> VTs;
1477 VTs.reserve(2);
1478 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001479 return getNode(ISD::VAARG, VTs, Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001480}
1481
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001482SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001483 const SDOperand *Ops, unsigned NumOps) {
1484 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001485 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001486 case 1: return getNode(Opcode, VT, Ops[0]);
1487 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1488 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001489 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001490 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001491
Chris Lattneref847df2005-04-09 03:27:28 +00001492 switch (Opcode) {
1493 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001494 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001495 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001496 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1497#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1498 // If this is a truncating store of a constant, convert to the desired type
1499 // and store it instead.
1500 if (isa<Constant>(Ops[0])) {
1501 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1502 if (isa<Constant>(Op))
1503 N1 = Op;
1504 }
1505 // Also for ConstantFP?
1506#endif
1507 if (Ops[0].getValueType() == EVT) // Normal store?
1508 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1509 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1510 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1511 "Can't do FP-INT conversion!");
1512 break;
1513 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001514 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001515 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001516 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1517 "LHS and RHS of condition must have same type!");
1518 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1519 "True and False arms of SelectCC must have same type!");
1520 assert(Ops[2].getValueType() == VT &&
1521 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001522 break;
1523 }
1524 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001525 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001526 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1527 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001528 break;
1529 }
Chris Lattneref847df2005-04-09 03:27:28 +00001530 }
1531
Chris Lattner385328c2005-05-14 07:42:29 +00001532 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001533 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001534 MVT::ValueType *VTs = getNodeValueTypes(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001535 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001536 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001537 void *IP = 0;
1538 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1539 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001540 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001541 N->setValueTypes(VTs, 1);
1542 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001543 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001544 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001545 N->setValueTypes(VTs, 1);
Chris Lattner43247a12005-08-25 19:12:10 +00001546 }
Chris Lattneref847df2005-04-09 03:27:28 +00001547 AllNodes.push_back(N);
1548 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001549}
1550
Chris Lattner89c34632005-05-14 06:20:26 +00001551SDOperand SelectionDAG::getNode(unsigned Opcode,
1552 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001553 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner89c34632005-05-14 06:20:26 +00001554 if (ResultTys.size() == 1)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001555 return getNode(Opcode, ResultTys[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001556
Chris Lattner5f056bf2005-07-10 01:55:33 +00001557 switch (Opcode) {
1558 case ISD::EXTLOAD:
1559 case ISD::SEXTLOAD:
1560 case ISD::ZEXTLOAD: {
1561 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001562 assert(NumOps == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001563 // If they are asking for an extending load from/to the same thing, return a
1564 // normal load.
1565 if (ResultTys[0] == EVT)
1566 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001567 if (MVT::isVector(ResultTys[0])) {
1568 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1569 "Invalid vector extload!");
1570 } else {
1571 assert(EVT < ResultTys[0] &&
1572 "Should only be an extending load, not truncating!");
1573 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001574 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001575 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001576 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1577 "Cannot convert from FP to Int or Int -> FP!");
1578 break;
1579 }
1580
Chris Lattnere89083a2005-05-14 07:25:05 +00001581 // FIXME: figure out how to safely handle things like
1582 // int foo(int x) { return 1 << (x & 255); }
1583 // int bar() { return foo(256); }
1584#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001585 case ISD::SRA_PARTS:
1586 case ISD::SRL_PARTS:
1587 case ISD::SHL_PARTS:
1588 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001589 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001590 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1591 else if (N3.getOpcode() == ISD::AND)
1592 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1593 // If the and is only masking out bits that cannot effect the shift,
1594 // eliminate the and.
1595 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1596 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1597 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1598 }
1599 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001600#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001601 }
Chris Lattner89c34632005-05-14 06:20:26 +00001602
Chris Lattner43247a12005-08-25 19:12:10 +00001603 // Memoize the node unless it returns a flag.
1604 SDNode *N;
Chris Lattnera5682852006-08-07 23:03:03 +00001605 MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
Chris Lattner43247a12005-08-25 19:12:10 +00001606 if (ResultTys.back() != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001607 SelectionDAGCSEMap::NodeID ID;
1608 ID.SetOpcode(Opcode);
1609 ID.SetValueTypes(VTs);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001610 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001611 void *IP = 0;
1612 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1613 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001614 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001615 N->setValueTypes(VTs, ResultTys.size());
1616 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001617 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001618 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001619 N->setValueTypes(VTs, ResultTys.size());
Chris Lattner43247a12005-08-25 19:12:10 +00001620 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001621 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001622 return SDOperand(N, 0);
1623}
1624
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001625
Chris Lattnera5682852006-08-07 23:03:03 +00001626MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT) {
1627 return SDNode::getValueTypeList(VT);
1628}
1629
1630MVT::ValueType *SelectionDAG::getNodeValueTypes(
1631 std::vector<MVT::ValueType> &RetVals) {
Chris Lattnera3255112005-11-08 23:30:28 +00001632 switch (RetVals.size()) {
Chris Lattnera5682852006-08-07 23:03:03 +00001633 case 0: assert(0 && "Cannot have nodes without results!");
1634 case 1: return SDNode::getValueTypeList(RetVals[0]);
1635 case 2: return getNodeValueTypes(RetVals[0], RetVals[1]);
Chris Lattnera3255112005-11-08 23:30:28 +00001636 default: break;
1637 }
1638
1639 std::list<std::vector<MVT::ValueType> >::iterator I =
1640 std::find(VTList.begin(), VTList.end(), RetVals);
1641 if (I == VTList.end()) {
1642 VTList.push_front(RetVals);
1643 I = VTList.begin();
1644 }
1645
Chris Lattnera5682852006-08-07 23:03:03 +00001646 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001647}
1648
Chris Lattnera5682852006-08-07 23:03:03 +00001649MVT::ValueType *SelectionDAG::getNodeValueTypes(MVT::ValueType VT1,
1650 MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001651 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1652 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001653 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
1654 return &(*I)[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001655 }
1656 std::vector<MVT::ValueType> V;
1657 V.push_back(VT1);
1658 V.push_back(VT2);
1659 VTList.push_front(V);
Chris Lattnera5682852006-08-07 23:03:03 +00001660 return &(*VTList.begin())[0];
Chris Lattnera3255112005-11-08 23:30:28 +00001661}
1662
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001663/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1664/// specified operands. If the resultant node already exists in the DAG,
1665/// this does not modify the specified node, instead it returns the node that
1666/// already exists. If the resultant node does not exist in the DAG, the
1667/// input node is returned. As a degenerate case, if you specify the same
1668/// input operands as the node already has, the input node is returned.
1669SDOperand SelectionDAG::
1670UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1671 SDNode *N = InN.Val;
1672 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1673
1674 // Check to see if there is no change.
1675 if (Op == N->getOperand(0)) return InN;
1676
1677 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001678 void *InsertPos = 0;
1679 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1680 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001681
1682 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001683 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001684 RemoveNodeFromCSEMaps(N);
1685
1686 // Now we update the operands.
1687 N->OperandList[0].Val->removeUser(N);
1688 Op.Val->addUser(N);
1689 N->OperandList[0] = Op;
1690
1691 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001692 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001693 return InN;
1694}
1695
1696SDOperand SelectionDAG::
1697UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1698 SDNode *N = InN.Val;
1699 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1700
1701 // Check to see if there is no change.
1702 bool AnyChange = false;
1703 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1704 return InN; // No operands changed, just return the input node.
1705
1706 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001707 void *InsertPos = 0;
1708 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1709 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001710
1711 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001712 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001713 RemoveNodeFromCSEMaps(N);
1714
1715 // Now we update the operands.
1716 if (N->OperandList[0] != Op1) {
1717 N->OperandList[0].Val->removeUser(N);
1718 Op1.Val->addUser(N);
1719 N->OperandList[0] = Op1;
1720 }
1721 if (N->OperandList[1] != Op2) {
1722 N->OperandList[1].Val->removeUser(N);
1723 Op2.Val->addUser(N);
1724 N->OperandList[1] = Op2;
1725 }
1726
1727 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001728 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001729 return InN;
1730}
1731
1732SDOperand SelectionDAG::
1733UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001734 SDOperand Ops[] = { Op1, Op2, Op3 };
1735 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001736}
1737
1738SDOperand SelectionDAG::
1739UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1740 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001741 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1742 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001743}
1744
1745SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001746UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1747 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001748 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1749 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001750}
1751
1752
1753SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001754UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001755 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001756 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001757 "Update with wrong number of operands");
1758
1759 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001760 bool AnyChange = false;
1761 for (unsigned i = 0; i != NumOps; ++i) {
1762 if (Ops[i] != N->getOperand(i)) {
1763 AnyChange = true;
1764 break;
1765 }
1766 }
1767
1768 // No operands changed, just return the input node.
1769 if (!AnyChange) return InN;
1770
1771 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001772 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001773 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001774 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001775
1776 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001777 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001778 RemoveNodeFromCSEMaps(N);
1779
1780 // Now we update the operands.
1781 for (unsigned i = 0; i != NumOps; ++i) {
1782 if (N->OperandList[i] != Ops[i]) {
1783 N->OperandList[i].Val->removeUser(N);
1784 Ops[i].Val->addUser(N);
1785 N->OperandList[i] = Ops[i];
1786 }
1787 }
1788
1789 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001790 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001791 return InN;
1792}
1793
1794
1795
Chris Lattner149c58c2005-08-16 18:17:10 +00001796
1797/// SelectNodeTo - These are used for target selectors to *mutate* the
1798/// specified node to have the specified return type, Target opcode, and
1799/// operands. Note that target opcodes are stored as
1800/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001801///
1802/// Note that SelectNodeTo returns the resultant node. If there is already a
1803/// node of the specified opcode and operands, it returns that node instead of
1804/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001805SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1806 MVT::ValueType VT) {
Chris Lattner4a283e92006-08-11 18:38:11 +00001807 MVT::ValueType *VTs = getNodeValueTypes(VT);
1808 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1809 void *IP = 0;
1810 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1811 return SDOperand(ON, 0);
1812
Chris Lattner7651fa42005-08-24 23:00:29 +00001813 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001814
Chris Lattner7651fa42005-08-24 23:00:29 +00001815 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001816 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001817
Chris Lattner4a283e92006-08-11 18:38:11 +00001818 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001819 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001820}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001821
Chris Lattnereb19e402005-11-30 22:45:14 +00001822SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1823 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001824 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001825 MVT::ValueType *VTs = getNodeValueTypes(VT);
1826 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1827 void *IP = 0;
1828 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1829 return SDOperand(ON, 0);
1830
Chris Lattner149c58c2005-08-16 18:17:10 +00001831 RemoveNodeFromCSEMaps(N);
1832 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001833 N->setValueTypes(getNodeValueTypes(VT), 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001834 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001835 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001836 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001837}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001838
Chris Lattnereb19e402005-11-30 22:45:14 +00001839SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1840 MVT::ValueType VT, SDOperand Op1,
1841 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001842 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001843 MVT::ValueType *VTs = getNodeValueTypes(VT);
1844 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1845 void *IP = 0;
1846 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1847 return SDOperand(ON, 0);
1848
Chris Lattner149c58c2005-08-16 18:17:10 +00001849 RemoveNodeFromCSEMaps(N);
1850 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001851 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001852 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001853
Chris Lattnera5682852006-08-07 23:03:03 +00001854 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001855 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001856}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001857
Chris Lattnereb19e402005-11-30 22:45:14 +00001858SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1859 MVT::ValueType VT, SDOperand Op1,
1860 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001861 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001862 MVT::ValueType *VTs = getNodeValueTypes(VT);
1863 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2, Op3);
1864 void *IP = 0;
1865 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1866 return SDOperand(ON, 0);
1867
Chris Lattner149c58c2005-08-16 18:17:10 +00001868 RemoveNodeFromCSEMaps(N);
1869 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001870 N->setValueTypes(VTs, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00001871 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001872
Chris Lattnera5682852006-08-07 23:03:03 +00001873 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001874 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001875}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001876
Chris Lattnereb19e402005-11-30 22:45:14 +00001877SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1878 MVT::ValueType VT, SDOperand Op1,
1879 SDOperand Op2, SDOperand Op3,
1880 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001881 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001882 MVT::ValueType *VTs = getNodeValueTypes(VT);
1883 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1884 ID.AddOperand(Op1);
1885 ID.AddOperand(Op2);
1886 ID.AddOperand(Op3);
1887 ID.AddOperand(Op4);
1888 void *IP = 0;
1889 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1890 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001891
Nate Begeman294a0a12005-08-18 07:30:15 +00001892 RemoveNodeFromCSEMaps(N);
1893 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001894 N->setValueTypes(VTs, 1);
Nate Begeman294a0a12005-08-18 07:30:15 +00001895 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001896
Chris Lattnera5682852006-08-07 23:03:03 +00001897 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001898 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001899}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001900
Chris Lattnereb19e402005-11-30 22:45:14 +00001901SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1902 MVT::ValueType VT, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00001903 SDOperand Op2, SDOperand Op3,
1904 SDOperand Op4, SDOperand Op5) {
1905 MVT::ValueType *VTs = getNodeValueTypes(VT);
1906 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1907 ID.AddOperand(Op1);
1908 ID.AddOperand(Op2);
1909 ID.AddOperand(Op3);
1910 ID.AddOperand(Op4);
1911 ID.AddOperand(Op5);
1912 void *IP = 0;
1913 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1914 return SDOperand(ON, 0);
1915
Chris Lattner6b09a292005-08-21 18:49:33 +00001916 RemoveNodeFromCSEMaps(N);
1917 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001918 N->setValueTypes(VTs, 1);
Chris Lattner6b09a292005-08-21 18:49:33 +00001919 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001920
Chris Lattnera5682852006-08-07 23:03:03 +00001921 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001922 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001923}
Chris Lattner149c58c2005-08-16 18:17:10 +00001924
Chris Lattnereb19e402005-11-30 22:45:14 +00001925SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1926 MVT::ValueType VT, SDOperand Op1,
1927 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1928 SDOperand Op5, SDOperand Op6) {
Chris Lattnera5682852006-08-07 23:03:03 +00001929 MVT::ValueType *VTs = getNodeValueTypes(VT);
1930 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1931 ID.AddOperand(Op1);
1932 ID.AddOperand(Op2);
1933 ID.AddOperand(Op3);
1934 ID.AddOperand(Op4);
1935 ID.AddOperand(Op5);
1936 ID.AddOperand(Op6);
1937 void *IP = 0;
1938 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1939 return SDOperand(ON, 0);
1940
Evan Cheng61ca74b2005-11-30 02:04:11 +00001941 RemoveNodeFromCSEMaps(N);
1942 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001943 N->setValueTypes(VTs, 1);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001944 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001945
Chris Lattnera5682852006-08-07 23:03:03 +00001946 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001947 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001948}
1949
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001950SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1951 MVT::ValueType VT, SDOperand Op1,
1952 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1953 SDOperand Op5, SDOperand Op6,
1954 SDOperand Op7) {
Chris Lattnera5682852006-08-07 23:03:03 +00001955 MVT::ValueType *VTs = getNodeValueTypes(VT);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001956 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001957 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1958 ID.AddOperand(Op1);
1959 ID.AddOperand(Op2);
1960 ID.AddOperand(Op3);
1961 ID.AddOperand(Op4);
1962 ID.AddOperand(Op5);
1963 ID.AddOperand(Op6);
1964 ID.AddOperand(Op7);
1965 void *IP = 0;
1966 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1967 return SDOperand(ON, 0);
1968
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001969 RemoveNodeFromCSEMaps(N);
1970 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001971 N->setValueTypes(VTs, 1);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001972 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1973
Chris Lattnera5682852006-08-07 23:03:03 +00001974 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001975 return SDOperand(N, 0);
1976}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001977SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1978 MVT::ValueType VT, SDOperand Op1,
1979 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1980 SDOperand Op5, SDOperand Op6,
1981 SDOperand Op7, SDOperand Op8) {
1982 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001983 MVT::ValueType *VTs = getNodeValueTypes(VT);
1984 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1985 ID.AddOperand(Op1);
1986 ID.AddOperand(Op2);
1987 ID.AddOperand(Op3);
1988 ID.AddOperand(Op4);
1989 ID.AddOperand(Op5);
1990 ID.AddOperand(Op6);
1991 ID.AddOperand(Op7);
1992 ID.AddOperand(Op8);
1993 void *IP = 0;
1994 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1995 return SDOperand(ON, 0);
1996
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001997 RemoveNodeFromCSEMaps(N);
1998 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00001999 N->setValueTypes(VTs, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002000 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2001
Chris Lattnera5682852006-08-07 23:03:03 +00002002 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002003 return SDOperand(N, 0);
2004}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002005
Chris Lattnereb19e402005-11-30 22:45:14 +00002006SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2007 MVT::ValueType VT1, MVT::ValueType VT2,
2008 SDOperand Op1, SDOperand Op2) {
Chris Lattnera5682852006-08-07 23:03:03 +00002009 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2010 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2011 void *IP = 0;
2012 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2013 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002014
Chris Lattner0fb094f2005-11-19 01:44:53 +00002015 RemoveNodeFromCSEMaps(N);
2016 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002017 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002018 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002019
Chris Lattnera5682852006-08-07 23:03:03 +00002020 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002021 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002022}
2023
Chris Lattnereb19e402005-11-30 22:45:14 +00002024SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2025 MVT::ValueType VT1, MVT::ValueType VT2,
2026 SDOperand Op1, SDOperand Op2,
2027 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002028 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002029 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2030 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2031 Op1, Op2, Op3);
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, Op3);
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, SDOperand Op4) {
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 ID.AddOperand(Op1);
2053 ID.AddOperand(Op2);
2054 ID.AddOperand(Op3);
2055 ID.AddOperand(Op4);
2056 void *IP = 0;
2057 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2058 return SDOperand(ON, 0);
2059
Chris Lattner0fb094f2005-11-19 01:44:53 +00002060 RemoveNodeFromCSEMaps(N);
2061 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002062 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002063 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002064
Chris Lattnera5682852006-08-07 23:03:03 +00002065 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002066 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002067}
2068
Chris Lattnereb19e402005-11-30 22:45:14 +00002069SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2070 MVT::ValueType VT1, MVT::ValueType VT2,
2071 SDOperand Op1, SDOperand Op2,
2072 SDOperand Op3, SDOperand Op4,
2073 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002074 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00002075 MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
2076 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2077 ID.AddOperand(Op1);
2078 ID.AddOperand(Op2);
2079 ID.AddOperand(Op3);
2080 ID.AddOperand(Op4);
2081 ID.AddOperand(Op5);
2082 void *IP = 0;
2083 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2084 return SDOperand(ON, 0);
2085
Chris Lattner0fb094f2005-11-19 01:44:53 +00002086 RemoveNodeFromCSEMaps(N);
2087 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattnera5682852006-08-07 23:03:03 +00002088 N->setValueTypes(VTs, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002089 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002090
Chris Lattnera5682852006-08-07 23:03:03 +00002091 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002092 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002093}
2094
Evan Cheng6ae46c42006-02-09 07:15:23 +00002095/// getTargetNode - These are used for target selectors to create a new node
2096/// with specified return type(s), target opcode, and operands.
2097///
2098/// Note that getTargetNode returns the resultant node. If there is already a
2099/// node of the specified opcode and operands, it returns that node instead of
2100/// the current one.
2101SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2102 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2103}
2104SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2105 SDOperand Op1) {
2106 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2107}
2108SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2109 SDOperand Op1, SDOperand Op2) {
2110 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2111}
2112SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2113 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2114 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2115}
2116SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2117 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2118 SDOperand Op4) {
2119 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2120}
2121SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2122 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2123 SDOperand Op4, SDOperand Op5) {
2124 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2125}
2126SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2127 SDOperand Op1, SDOperand Op2, SDOperand Op3,
Chris Lattnera5682852006-08-07 23:03:03 +00002128 SDOperand Op4, SDOperand Op5,
2129 SDOperand Op6) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002130 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2131 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002132}
2133SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2134 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2135 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2136 SDOperand Op7) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002137 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2138 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002139}
2140SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2141 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2142 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2143 SDOperand Op7, SDOperand Op8) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002144 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8 };
2145 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 8).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002146}
2147SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002148 const SDOperand *Ops, unsigned NumOps) {
2149 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002150}
2151SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2152 MVT::ValueType VT2, SDOperand Op1) {
2153 std::vector<MVT::ValueType> ResultTys;
2154 ResultTys.push_back(VT1);
2155 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002156 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002157}
2158SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002159 MVT::ValueType VT2, SDOperand Op1,
2160 SDOperand Op2) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002161 std::vector<MVT::ValueType> ResultTys;
2162 ResultTys.push_back(VT1);
2163 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002164 SDOperand Ops[] = { Op1, Op2 };
2165 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002166}
2167SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002168 MVT::ValueType VT2, SDOperand Op1,
2169 SDOperand Op2, SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002170 std::vector<MVT::ValueType> ResultTys;
2171 ResultTys.push_back(VT1);
2172 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002173 SDOperand Ops[] = { Op1, Op2, Op3 };
2174 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002175}
2176SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002177 MVT::ValueType VT2, SDOperand Op1,
2178 SDOperand Op2, SDOperand Op3,
2179 SDOperand Op4) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002180 std::vector<MVT::ValueType> ResultTys;
2181 ResultTys.push_back(VT1);
2182 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002183 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2184 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 4).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002185}
2186SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002187 MVT::ValueType VT2, SDOperand Op1,
2188 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2189 SDOperand Op5) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002190 std::vector<MVT::ValueType> ResultTys;
2191 ResultTys.push_back(VT1);
2192 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002193 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2194 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002195}
2196SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002197 MVT::ValueType VT2, SDOperand Op1,
2198 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2199 SDOperand Op5, SDOperand Op6) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002200 std::vector<MVT::ValueType> ResultTys;
2201 ResultTys.push_back(VT1);
2202 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002203 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2204 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002205}
2206SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002207 MVT::ValueType VT2, SDOperand Op1,
2208 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2209 SDOperand Op5, SDOperand Op6,
2210 SDOperand Op7) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002211 std::vector<MVT::ValueType> ResultTys;
2212 ResultTys.push_back(VT1);
2213 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002214 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2215 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002216}
2217SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2218 MVT::ValueType VT2, MVT::ValueType VT3,
2219 SDOperand Op1, SDOperand Op2) {
2220 std::vector<MVT::ValueType> ResultTys;
2221 ResultTys.push_back(VT1);
2222 ResultTys.push_back(VT2);
2223 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002224 SDOperand Ops[] = { Op1, Op2 };
2225 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002226}
2227SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2228 MVT::ValueType VT2, MVT::ValueType VT3,
2229 SDOperand Op1, SDOperand Op2,
Chris Lattnera5682852006-08-07 23:03:03 +00002230 SDOperand Op3, SDOperand Op4,
2231 SDOperand Op5) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002232 std::vector<MVT::ValueType> ResultTys;
2233 ResultTys.push_back(VT1);
2234 ResultTys.push_back(VT2);
2235 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002236 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2237 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002238}
2239SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2240 MVT::ValueType VT2, MVT::ValueType VT3,
2241 SDOperand Op1, SDOperand Op2,
2242 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2243 SDOperand Op6) {
2244 std::vector<MVT::ValueType> ResultTys;
2245 ResultTys.push_back(VT1);
2246 ResultTys.push_back(VT2);
2247 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002248 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2249 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002250}
2251SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2252 MVT::ValueType VT2, MVT::ValueType VT3,
2253 SDOperand Op1, SDOperand Op2,
2254 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2255 SDOperand Op6, SDOperand Op7) {
2256 std::vector<MVT::ValueType> ResultTys;
2257 ResultTys.push_back(VT1);
2258 ResultTys.push_back(VT2);
2259 ResultTys.push_back(VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002260 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2261 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002262}
2263SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002264 MVT::ValueType VT2,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002265 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00002266 std::vector<MVT::ValueType> ResultTys;
2267 ResultTys.push_back(VT1);
2268 ResultTys.push_back(VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002269 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002270}
2271
Evan Cheng99157a02006-08-07 22:13:29 +00002272/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002273/// This can cause recursive merging of nodes in the DAG.
2274///
Chris Lattner8b52f212005-08-26 18:36:28 +00002275/// This version assumes From/To have a single result value.
2276///
Chris Lattner1e111c72005-09-07 05:37:01 +00002277void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2278 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002279 SDNode *From = FromN.Val, *To = ToN.Val;
2280 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2281 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002282 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002283
Chris Lattner8b8749f2005-08-17 19:00:20 +00002284 while (!From->use_empty()) {
2285 // Process users until they are all gone.
2286 SDNode *U = *From->use_begin();
2287
2288 // This node is about to morph, remove its old self from the CSE maps.
2289 RemoveNodeFromCSEMaps(U);
2290
Chris Lattner65113b22005-11-08 22:07:03 +00002291 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2292 I != E; ++I)
2293 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002294 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002295 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002296 To->addUser(U);
2297 }
2298
2299 // Now that we have modified U, add it back to the CSE maps. If it already
2300 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002301 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2302 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002303 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002304 if (Deleted) Deleted->push_back(U);
2305 DeleteNodeNotInCSEMaps(U);
2306 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002307 }
2308}
2309
2310/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2311/// This can cause recursive merging of nodes in the DAG.
2312///
2313/// This version assumes From/To have matching types and numbers of result
2314/// values.
2315///
Chris Lattner1e111c72005-09-07 05:37:01 +00002316void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2317 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002318 assert(From != To && "Cannot replace uses of with self");
2319 assert(From->getNumValues() == To->getNumValues() &&
2320 "Cannot use this version of ReplaceAllUsesWith!");
2321 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002322 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002323 return;
2324 }
2325
2326 while (!From->use_empty()) {
2327 // Process users until they are all gone.
2328 SDNode *U = *From->use_begin();
2329
2330 // This node is about to morph, remove its old self from the CSE maps.
2331 RemoveNodeFromCSEMaps(U);
2332
Chris Lattner65113b22005-11-08 22:07:03 +00002333 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2334 I != E; ++I)
2335 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002336 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002337 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002338 To->addUser(U);
2339 }
2340
2341 // Now that we have modified U, add it back to the CSE maps. If it already
2342 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002343 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2344 ReplaceAllUsesWith(U, Existing, Deleted);
2345 // U is now dead.
2346 if (Deleted) Deleted->push_back(U);
2347 DeleteNodeNotInCSEMaps(U);
2348 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002349 }
2350}
2351
Chris Lattner8b52f212005-08-26 18:36:28 +00002352/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2353/// This can cause recursive merging of nodes in the DAG.
2354///
2355/// This version can replace From with any result values. To must match the
2356/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002357void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002358 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002359 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002360 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002361 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002362 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002363 return;
2364 }
2365
2366 while (!From->use_empty()) {
2367 // Process users until they are all gone.
2368 SDNode *U = *From->use_begin();
2369
2370 // This node is about to morph, remove its old self from the CSE maps.
2371 RemoveNodeFromCSEMaps(U);
2372
Chris Lattner65113b22005-11-08 22:07:03 +00002373 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2374 I != E; ++I)
2375 if (I->Val == From) {
2376 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002377 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002378 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002379 ToOp.Val->addUser(U);
2380 }
2381
2382 // Now that we have modified U, add it back to the CSE maps. If it already
2383 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002384 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2385 ReplaceAllUsesWith(U, Existing, Deleted);
2386 // U is now dead.
2387 if (Deleted) Deleted->push_back(U);
2388 DeleteNodeNotInCSEMaps(U);
2389 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002390 }
2391}
2392
Chris Lattner012f2412006-02-17 21:58:01 +00002393/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2394/// uses of other values produced by From.Val alone. The Deleted vector is
2395/// handled the same was as for ReplaceAllUsesWith.
2396void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2397 std::vector<SDNode*> &Deleted) {
2398 assert(From != To && "Cannot replace a value with itself");
2399 // Handle the simple, trivial, case efficiently.
2400 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2401 ReplaceAllUsesWith(From, To, &Deleted);
2402 return;
2403 }
2404
2405 // Get all of the users in a nice, deterministically ordered, uniqued set.
2406 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2407
2408 while (!Users.empty()) {
2409 // We know that this user uses some value of From. If it is the right
2410 // value, update it.
2411 SDNode *User = Users.back();
2412 Users.pop_back();
2413
2414 for (SDOperand *Op = User->OperandList,
2415 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2416 if (*Op == From) {
2417 // Okay, we know this user needs to be updated. Remove its old self
2418 // from the CSE maps.
2419 RemoveNodeFromCSEMaps(User);
2420
2421 // Update all operands that match "From".
2422 for (; Op != E; ++Op) {
2423 if (*Op == From) {
2424 From.Val->removeUser(User);
2425 *Op = To;
2426 To.Val->addUser(User);
2427 }
2428 }
2429
2430 // Now that we have modified User, add it back to the CSE maps. If it
2431 // already exists there, recursively merge the results together.
2432 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2433 unsigned NumDeleted = Deleted.size();
2434 ReplaceAllUsesWith(User, Existing, &Deleted);
2435
2436 // User is now dead.
2437 Deleted.push_back(User);
2438 DeleteNodeNotInCSEMaps(User);
2439
2440 // We have to be careful here, because ReplaceAllUsesWith could have
2441 // deleted a user of From, which means there may be dangling pointers
2442 // in the "Users" setvector. Scan over the deleted node pointers and
2443 // remove them from the setvector.
2444 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2445 Users.remove(Deleted[i]);
2446 }
2447 break; // Exit the operand scanning loop.
2448 }
2449 }
2450 }
2451}
2452
Chris Lattner7b2880c2005-08-24 22:44:39 +00002453
Evan Chenge6f35d82006-08-01 08:20:41 +00002454/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2455/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002456unsigned SelectionDAG::AssignNodeIds() {
2457 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002458 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2459 SDNode *N = I;
2460 N->setNodeId(Id++);
2461 }
2462 return Id;
2463}
2464
Evan Chenge6f35d82006-08-01 08:20:41 +00002465/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002466/// based on their topological order. It returns the maximum id and a vector
2467/// of the SDNodes* in assigned order by reference.
2468unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002469 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002470 std::vector<unsigned> InDegree(DAGSize);
2471 std::vector<SDNode*> Sources;
2472
2473 // Use a two pass approach to avoid using a std::map which is slow.
2474 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002475 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2476 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002477 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002478 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002479 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002480 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002481 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002482 }
2483
Evan Cheng99157a02006-08-07 22:13:29 +00002484 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002485 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002486 SDNode *N = Sources.back();
2487 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002488 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002489 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2490 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002491 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002492 if (Degree == 0)
2493 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002494 }
2495 }
2496
Evan Chengc384d6c2006-08-02 22:00:34 +00002497 // Second pass, assign the actual topological order as node ids.
2498 Id = 0;
2499 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2500 TI != TE; ++TI)
2501 (*TI)->setNodeId(Id++);
2502
2503 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002504}
2505
2506
Evan Cheng091cba12006-07-27 06:39:06 +00002507
Jim Laskey58b968b2005-08-17 20:08:02 +00002508//===----------------------------------------------------------------------===//
2509// SDNode Class
2510//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002511
Chris Lattner917d2c92006-07-19 00:00:37 +00002512// Out-of-line virtual method to give class a home.
2513void SDNode::ANCHOR() {
2514}
2515
Chris Lattnera3255112005-11-08 23:30:28 +00002516/// getValueTypeList - Return a pointer to the specified value type.
2517///
2518MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2519 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2520 VTs[VT] = VT;
2521 return &VTs[VT];
2522}
Chris Lattnera5682852006-08-07 23:03:03 +00002523
Chris Lattner5c884562005-01-12 18:37:47 +00002524/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2525/// indicated value. This method ignores uses of other values defined by this
2526/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002527bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002528 assert(Value < getNumValues() && "Bad value!");
2529
2530 // If there is only one value, this is easy.
2531 if (getNumValues() == 1)
2532 return use_size() == NUses;
2533 if (Uses.size() < NUses) return false;
2534
Evan Cheng4ee62112006-02-05 06:29:23 +00002535 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002536
2537 std::set<SDNode*> UsersHandled;
2538
Evan Cheng4ee62112006-02-05 06:29:23 +00002539 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002540 UI != E; ++UI) {
2541 SDNode *User = *UI;
2542 if (User->getNumOperands() == 1 ||
2543 UsersHandled.insert(User).second) // First time we've seen this?
2544 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2545 if (User->getOperand(i) == TheValue) {
2546 if (NUses == 0)
2547 return false; // too many uses
2548 --NUses;
2549 }
2550 }
2551
2552 // Found exactly the right number of uses?
2553 return NUses == 0;
2554}
2555
2556
Evan Cheng4ee62112006-02-05 06:29:23 +00002557// isOnlyUse - Return true if this node is the only use of N.
2558bool SDNode::isOnlyUse(SDNode *N) const {
2559 bool Seen = false;
2560 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2561 SDNode *User = *I;
2562 if (User == this)
2563 Seen = true;
2564 else
2565 return false;
2566 }
2567
2568 return Seen;
2569}
2570
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002571// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002572bool SDOperand::isOperand(SDNode *N) const {
2573 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2574 if (*this == N->getOperand(i))
2575 return true;
2576 return false;
2577}
2578
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002579bool SDNode::isOperand(SDNode *N) const {
2580 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2581 if (this == N->OperandList[i].Val)
2582 return true;
2583 return false;
2584}
Evan Cheng4ee62112006-02-05 06:29:23 +00002585
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002586const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002587 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002588 default:
2589 if (getOpcode() < ISD::BUILTIN_OP_END)
2590 return "<<Unknown DAG Node>>";
2591 else {
Evan Cheng72261582005-12-20 06:22:03 +00002592 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002593 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002594 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2595 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002596
Evan Cheng72261582005-12-20 06:22:03 +00002597 TargetLowering &TLI = G->getTargetLoweringInfo();
2598 const char *Name =
2599 TLI.getTargetNodeName(getOpcode());
2600 if (Name) return Name;
2601 }
2602
2603 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002604 }
2605
Andrew Lenharth95762122005-03-31 21:24:06 +00002606 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002607 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002608 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002609 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002610 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002611 case ISD::AssertSext: return "AssertSext";
2612 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002613
2614 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002615 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002616 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002617 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002618
2619 case ISD::Constant: return "Constant";
2620 case ISD::ConstantFP: return "ConstantFP";
2621 case ISD::GlobalAddress: return "GlobalAddress";
2622 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002623 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002624 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002625 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002626 case ISD::INTRINSIC_WO_CHAIN: {
2627 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2628 return Intrinsic::getName((Intrinsic::ID)IID);
2629 }
2630 case ISD::INTRINSIC_VOID:
2631 case ISD::INTRINSIC_W_CHAIN: {
2632 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002633 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002634 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002635
Chris Lattnerb2827b02006-03-19 00:52:58 +00002636 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002637 case ISD::TargetConstant: return "TargetConstant";
2638 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002639 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2640 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002641 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002642 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002643 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002644
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002645 case ISD::CopyToReg: return "CopyToReg";
2646 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002647 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002648 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002649 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002650 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002651 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002652 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002653
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002654 // Unary operators
2655 case ISD::FABS: return "fabs";
2656 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002657 case ISD::FSQRT: return "fsqrt";
2658 case ISD::FSIN: return "fsin";
2659 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002660
2661 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002662 case ISD::ADD: return "add";
2663 case ISD::SUB: return "sub";
2664 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002665 case ISD::MULHU: return "mulhu";
2666 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002667 case ISD::SDIV: return "sdiv";
2668 case ISD::UDIV: return "udiv";
2669 case ISD::SREM: return "srem";
2670 case ISD::UREM: return "urem";
2671 case ISD::AND: return "and";
2672 case ISD::OR: return "or";
2673 case ISD::XOR: return "xor";
2674 case ISD::SHL: return "shl";
2675 case ISD::SRA: return "sra";
2676 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002677 case ISD::ROTL: return "rotl";
2678 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002679 case ISD::FADD: return "fadd";
2680 case ISD::FSUB: return "fsub";
2681 case ISD::FMUL: return "fmul";
2682 case ISD::FDIV: return "fdiv";
2683 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002684 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002685 case ISD::VADD: return "vadd";
2686 case ISD::VSUB: return "vsub";
2687 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002688 case ISD::VSDIV: return "vsdiv";
2689 case ISD::VUDIV: return "vudiv";
2690 case ISD::VAND: return "vand";
2691 case ISD::VOR: return "vor";
2692 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002693
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002694 case ISD::SETCC: return "setcc";
2695 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002696 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002697 case ISD::VSELECT: return "vselect";
2698 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2699 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2700 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002701 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002702 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2703 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2704 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2705 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2706 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002707 case ISD::ADDC: return "addc";
2708 case ISD::ADDE: return "adde";
2709 case ISD::SUBC: return "subc";
2710 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002711 case ISD::SHL_PARTS: return "shl_parts";
2712 case ISD::SRA_PARTS: return "sra_parts";
2713 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002714
Chris Lattner7f644642005-04-28 21:44:03 +00002715 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002716 case ISD::SIGN_EXTEND: return "sign_extend";
2717 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002718 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002719 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002720 case ISD::TRUNCATE: return "truncate";
2721 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002722 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002723 case ISD::FP_EXTEND: return "fp_extend";
2724
2725 case ISD::SINT_TO_FP: return "sint_to_fp";
2726 case ISD::UINT_TO_FP: return "uint_to_fp";
2727 case ISD::FP_TO_SINT: return "fp_to_sint";
2728 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002729 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002730
2731 // Control flow instructions
2732 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002733 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002734 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002735 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002736 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002737 case ISD::CALLSEQ_START: return "callseq_start";
2738 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002739
2740 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002741 case ISD::LOAD: return "load";
2742 case ISD::STORE: return "store";
2743 case ISD::VLOAD: return "vload";
2744 case ISD::EXTLOAD: return "extload";
2745 case ISD::SEXTLOAD: return "sextload";
2746 case ISD::ZEXTLOAD: return "zextload";
2747 case ISD::TRUNCSTORE: return "truncstore";
2748 case ISD::VAARG: return "vaarg";
2749 case ISD::VACOPY: return "vacopy";
2750 case ISD::VAEND: return "vaend";
2751 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002752 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002753 case ISD::EXTRACT_ELEMENT: return "extract_element";
2754 case ISD::BUILD_PAIR: return "build_pair";
2755 case ISD::STACKSAVE: return "stacksave";
2756 case ISD::STACKRESTORE: return "stackrestore";
2757
2758 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002759 case ISD::MEMSET: return "memset";
2760 case ISD::MEMCPY: return "memcpy";
2761 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002762
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002763 // Bit manipulation
2764 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002765 case ISD::CTPOP: return "ctpop";
2766 case ISD::CTTZ: return "cttz";
2767 case ISD::CTLZ: return "ctlz";
2768
Chris Lattner36ce6912005-11-29 06:21:05 +00002769 // Debug info
2770 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002771 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002772 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002773
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002774 case ISD::CONDCODE:
2775 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002776 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002777 case ISD::SETOEQ: return "setoeq";
2778 case ISD::SETOGT: return "setogt";
2779 case ISD::SETOGE: return "setoge";
2780 case ISD::SETOLT: return "setolt";
2781 case ISD::SETOLE: return "setole";
2782 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002783
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002784 case ISD::SETO: return "seto";
2785 case ISD::SETUO: return "setuo";
2786 case ISD::SETUEQ: return "setue";
2787 case ISD::SETUGT: return "setugt";
2788 case ISD::SETUGE: return "setuge";
2789 case ISD::SETULT: return "setult";
2790 case ISD::SETULE: return "setule";
2791 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002792
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002793 case ISD::SETEQ: return "seteq";
2794 case ISD::SETGT: return "setgt";
2795 case ISD::SETGE: return "setge";
2796 case ISD::SETLT: return "setlt";
2797 case ISD::SETLE: return "setle";
2798 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002799 }
2800 }
2801}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002802
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002803void SDNode::dump() const { dump(0); }
2804void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002805 std::cerr << (void*)this << ": ";
2806
2807 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2808 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002809 if (getValueType(i) == MVT::Other)
2810 std::cerr << "ch";
2811 else
2812 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002813 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002814 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002815
2816 std::cerr << " ";
2817 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2818 if (i) std::cerr << ", ";
2819 std::cerr << (void*)getOperand(i).Val;
2820 if (unsigned RN = getOperand(i).ResNo)
2821 std::cerr << ":" << RN;
2822 }
2823
2824 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2825 std::cerr << "<" << CSDN->getValue() << ">";
2826 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2827 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002828 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002829 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002830 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002831 std::cerr << "<";
2832 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002833 if (offset > 0)
2834 std::cerr << " + " << offset;
2835 else
2836 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002837 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002838 std::cerr << "<" << FIDN->getIndex() << ">";
2839 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002840 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002841 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002842 if (offset > 0)
2843 std::cerr << " + " << offset;
2844 else
2845 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002846 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002847 std::cerr << "<";
2848 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2849 if (LBB)
2850 std::cerr << LBB->getName() << " ";
2851 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002852 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002853 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002854 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2855 } else {
2856 std::cerr << " #" << R->getReg();
2857 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002858 } else if (const ExternalSymbolSDNode *ES =
2859 dyn_cast<ExternalSymbolSDNode>(this)) {
2860 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002861 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2862 if (M->getValue())
2863 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2864 else
2865 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002866 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2867 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002868 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002869}
2870
Chris Lattnerde202b32005-11-09 23:47:37 +00002871static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002872 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2873 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002874 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002875 else
2876 std::cerr << "\n" << std::string(indent+2, ' ')
2877 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002878
Chris Lattnerea946cd2005-01-09 20:38:33 +00002879
2880 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002881 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002882}
2883
Chris Lattnerc3aae252005-01-07 07:46:32 +00002884void SelectionDAG::dump() const {
2885 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002886 std::vector<const SDNode*> Nodes;
2887 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2888 I != E; ++I)
2889 Nodes.push_back(I);
2890
Chris Lattner49d24712005-01-09 20:26:36 +00002891 std::sort(Nodes.begin(), Nodes.end());
2892
2893 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002894 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002895 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002896 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002897
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002898 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002899
Chris Lattnerc3aae252005-01-07 07:46:32 +00002900 std::cerr << "\n\n";
2901}
2902
Evan Chengfae9f1c2006-02-09 22:11:03 +00002903/// InsertISelMapEntry - A helper function to insert a key / element pair
2904/// into a SDOperand to SDOperand map. This is added to avoid the map
2905/// insertion operator from being inlined.
2906void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2907 SDNode *Key, unsigned KeyResNo,
2908 SDNode *Element, unsigned ElementResNo) {
2909 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2910 SDOperand(Element, ElementResNo)));
2911}