blob: 30205d4ef5bee723529e17f28f969e41396d4347 [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 Lattner0b3e5252006-08-15 19:11:05 +000034/// makeVTList - Return an instance of the SDVTList struct initialized with the
35/// specified members.
36static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
37 SDVTList Res = {VTs, NumVTs};
38 return Res;
39}
40
Chris Lattner5cdcc582005-01-09 20:52:51 +000041static bool isCommutativeBinOp(unsigned Opcode) {
42 switch (Opcode) {
43 case ISD::ADD:
44 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000045 case ISD::MULHU:
46 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000047 case ISD::FADD:
48 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000049 case ISD::AND:
50 case ISD::OR:
51 case ISD::XOR: return true;
52 default: return false; // FIXME: Need commutative info for user ops!
53 }
54}
55
Chris Lattner5cdcc582005-01-09 20:52:51 +000056// isInvertibleForFree - Return true if there is no cost to emitting the logical
57// inverse of this node.
58static bool isInvertibleForFree(SDOperand N) {
59 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000060 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000061 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000062 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000063}
64
Jim Laskey58b968b2005-08-17 20:08:02 +000065//===----------------------------------------------------------------------===//
66// ConstantFPSDNode Class
67//===----------------------------------------------------------------------===//
68
69/// isExactlyValue - We don't rely on operator== working on double values, as
70/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
71/// As such, this method can be used to do an exact bit-for-bit comparison of
72/// two floating point values.
73bool ConstantFPSDNode::isExactlyValue(double V) const {
74 return DoubleToBits(V) == DoubleToBits(Value);
75}
76
77//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000078// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000079//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000080
Evan Chenga8df1662006-03-27 06:58:47 +000081/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000082/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000083bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000084 // Look through a bit convert.
85 if (N->getOpcode() == ISD::BIT_CONVERT)
86 N = N->getOperand(0).Val;
87
Evan Chenga8df1662006-03-27 06:58:47 +000088 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000089
90 unsigned i = 0, e = N->getNumOperands();
91
92 // Skip over all of the undef values.
93 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
94 ++i;
95
96 // Do not accept an all-undef vector.
97 if (i == e) return false;
98
99 // Do not accept build_vectors that aren't all constants or which have non-~0
100 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000101 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000102 if (isa<ConstantSDNode>(NotZero)) {
103 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
104 return false;
105 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000106 MVT::ValueType VT = NotZero.getValueType();
107 if (VT== MVT::f64) {
108 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
109 (uint64_t)-1)
110 return false;
111 } else {
112 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
113 (uint32_t)-1)
114 return false;
115 }
Evan Chenga8df1662006-03-27 06:58:47 +0000116 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000117 return false;
118
119 // Okay, we have at least one ~0 value, check to see if the rest match or are
120 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000121 for (++i; i != e; ++i)
122 if (N->getOperand(i) != NotZero &&
123 N->getOperand(i).getOpcode() != ISD::UNDEF)
124 return false;
125 return true;
126}
127
128
Evan Cheng4a147842006-03-26 09:50:58 +0000129/// isBuildVectorAllZeros - Return true if the specified node is a
130/// BUILD_VECTOR where all of the elements are 0 or undef.
131bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000132 // Look through a bit convert.
133 if (N->getOpcode() == ISD::BIT_CONVERT)
134 N = N->getOperand(0).Val;
135
Evan Cheng4a147842006-03-26 09:50:58 +0000136 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000137
138 unsigned i = 0, e = N->getNumOperands();
139
140 // Skip over all of the undef values.
141 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
142 ++i;
143
144 // Do not accept an all-undef vector.
145 if (i == e) return false;
146
147 // Do not accept build_vectors that aren't all constants or which have non-~0
148 // elements.
149 SDOperand Zero = N->getOperand(i);
150 if (isa<ConstantSDNode>(Zero)) {
151 if (!cast<ConstantSDNode>(Zero)->isNullValue())
152 return false;
153 } else if (isa<ConstantFPSDNode>(Zero)) {
154 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
155 return false;
156 } else
157 return false;
158
159 // Okay, we have at least one ~0 value, check to see if the rest match or are
160 // undefs.
161 for (++i; i != e; ++i)
162 if (N->getOperand(i) != Zero &&
163 N->getOperand(i).getOpcode() != ISD::UNDEF)
164 return false;
165 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000166}
167
Chris Lattnerc3aae252005-01-07 07:46:32 +0000168/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
169/// when given the operation for (X op Y).
170ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
171 // To perform this operation, we just need to swap the L and G bits of the
172 // operation.
173 unsigned OldL = (Operation >> 2) & 1;
174 unsigned OldG = (Operation >> 1) & 1;
175 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
176 (OldL << 1) | // New G bit
177 (OldG << 2)); // New L bit.
178}
179
180/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
181/// 'op' is a valid SetCC operation.
182ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
183 unsigned Operation = Op;
184 if (isInteger)
185 Operation ^= 7; // Flip L, G, E bits, but not U.
186 else
187 Operation ^= 15; // Flip all of the condition bits.
188 if (Operation > ISD::SETTRUE2)
189 Operation &= ~8; // Don't let N and U bits get set.
190 return ISD::CondCode(Operation);
191}
192
193
194/// isSignedOp - For an integer comparison, return 1 if the comparison is a
195/// signed operation and 2 if the result is an unsigned comparison. Return zero
196/// if the operation does not depend on the sign of the input (setne and seteq).
197static int isSignedOp(ISD::CondCode Opcode) {
198 switch (Opcode) {
199 default: assert(0 && "Illegal integer setcc operation!");
200 case ISD::SETEQ:
201 case ISD::SETNE: return 0;
202 case ISD::SETLT:
203 case ISD::SETLE:
204 case ISD::SETGT:
205 case ISD::SETGE: return 1;
206 case ISD::SETULT:
207 case ISD::SETULE:
208 case ISD::SETUGT:
209 case ISD::SETUGE: return 2;
210 }
211}
212
213/// getSetCCOrOperation - Return the result of a logical OR between different
214/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
215/// returns SETCC_INVALID if it is not possible to represent the resultant
216/// comparison.
217ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
218 bool isInteger) {
219 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
220 // Cannot fold a signed integer setcc with an unsigned integer setcc.
221 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000222
Chris Lattnerc3aae252005-01-07 07:46:32 +0000223 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000224
Chris Lattnerc3aae252005-01-07 07:46:32 +0000225 // If the N and U bits get set then the resultant comparison DOES suddenly
226 // care about orderedness, and is true when ordered.
227 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000228 Op &= ~16; // Clear the U bit if the N bit is set.
229
230 // Canonicalize illegal integer setcc's.
231 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
232 Op = ISD::SETNE;
233
Chris Lattnerc3aae252005-01-07 07:46:32 +0000234 return ISD::CondCode(Op);
235}
236
237/// getSetCCAndOperation - Return the result of a logical AND between different
238/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
239/// function returns zero if it is not possible to represent the resultant
240/// comparison.
241ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
242 bool isInteger) {
243 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
244 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000245 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000246
247 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000248 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
249
250 // Canonicalize illegal integer setcc's.
251 if (isInteger) {
252 switch (Result) {
253 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000254 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
255 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
256 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
257 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000258 }
259 }
260
261 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000262}
263
Chris Lattnerb48da392005-01-23 04:39:44 +0000264const TargetMachine &SelectionDAG::getTarget() const {
265 return TLI.getTargetMachine();
266}
267
Jim Laskey58b968b2005-08-17 20:08:02 +0000268//===----------------------------------------------------------------------===//
269// SelectionDAG Class
270//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000271
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000272/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000273/// SelectionDAG.
274void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000275 // Create a dummy node (which is not added to allnodes), that adds a reference
276 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000277 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000278
Chris Lattner190a4182006-08-04 17:45:20 +0000279 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000280
Chris Lattner190a4182006-08-04 17:45:20 +0000281 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000282 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000283 if (I->use_empty())
284 DeadNodes.push_back(I);
285
286 // Process the worklist, deleting the nodes and adding their uses to the
287 // worklist.
288 while (!DeadNodes.empty()) {
289 SDNode *N = DeadNodes.back();
290 DeadNodes.pop_back();
291
292 // Take the node out of the appropriate CSE map.
293 RemoveNodeFromCSEMaps(N);
294
295 // Next, brutally remove the operand list. This is safe to do, as there are
296 // no cycles in the graph.
297 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
298 SDNode *Operand = I->Val;
299 Operand->removeUser(N);
300
301 // Now that we removed this operand, see if there are no uses of it left.
302 if (Operand->use_empty())
303 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000304 }
Chris Lattner190a4182006-08-04 17:45:20 +0000305 delete[] N->OperandList;
306 N->OperandList = 0;
307 N->NumOperands = 0;
308
309 // Finally, remove N itself.
310 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000311 }
312
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000313 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000314 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315}
316
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000317void SelectionDAG::DeleteNode(SDNode *N) {
318 assert(N->use_empty() && "Cannot delete a node that is not dead!");
319
320 // First take this out of the appropriate CSE map.
321 RemoveNodeFromCSEMaps(N);
322
Chris Lattner1e111c72005-09-07 05:37:01 +0000323 // Finally, remove uses due to operands of this node, remove from the
324 // AllNodes list, and delete the node.
325 DeleteNodeNotInCSEMaps(N);
326}
327
328void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
329
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000330 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000331 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000332
333 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000334 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
335 I->Val->removeUser(N);
336 delete[] N->OperandList;
337 N->OperandList = 0;
338 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000339
340 delete N;
341}
342
Chris Lattner149c58c2005-08-16 18:17:10 +0000343/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
344/// correspond to it. This is useful when we're about to delete or repurpose
345/// the node. We don't want future request for structurally identical nodes
346/// to return N anymore.
347void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000348 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000349 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000350 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000351 case ISD::STRING:
352 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
353 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000354 case ISD::CONDCODE:
355 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
356 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000357 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000358 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
359 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000360 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000361 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000362 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000363 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000364 Erased =
365 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000366 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000367 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000368 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000369 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
370 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000371 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000372 // Remove it from the CSE Map.
373 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000374 break;
375 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000376#ifndef NDEBUG
377 // Verify that the node was actually in one of the CSE maps, unless it has a
378 // flag result (which cannot be CSE'd) or is one of the special cases that are
379 // not subject to CSE.
380 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000381 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000382 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000383 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000384 assert(0 && "Node is not in map!");
385 }
386#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000387}
388
Chris Lattner8b8749f2005-08-17 19:00:20 +0000389/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
390/// has been taken out and modified in some way. If the specified node already
391/// exists in the CSE maps, do not modify the maps, but return the existing node
392/// instead. If it doesn't exist, add it and return null.
393///
394SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
395 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000396 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000397 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000398
Chris Lattner9f8cc692005-12-19 22:21:21 +0000399 // Check that remaining values produced are not flags.
400 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
401 if (N->getValueType(i) == MVT::Flag)
402 return 0; // Never CSE anything that produces a flag.
403
Chris Lattnera5682852006-08-07 23:03:03 +0000404 SDNode *New = CSEMap.GetOrInsertNode(N);
405 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000406 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000407}
408
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000409/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
410/// were replaced with those specified. If this node is never memoized,
411/// return null, otherwise return a pointer to the slot it would take. If a
412/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000413SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
414 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000415 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000416 return 0; // Never add these nodes.
417
418 // Check that remaining values produced are not flags.
419 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
420 if (N->getValueType(i) == MVT::Flag)
421 return 0; // Never CSE anything that produces a flag.
422
Chris Lattnera5682852006-08-07 23:03:03 +0000423 SelectionDAGCSEMap::NodeID ID;
424 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000425 ID.SetValueTypes(N->getVTList());
Chris Lattnera5682852006-08-07 23:03:03 +0000426 ID.SetOperands(Op);
427 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000428}
429
430/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
431/// were replaced with those specified. If this node is never memoized,
432/// return null, otherwise return a pointer to the slot it would take. If a
433/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000434SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
435 SDOperand Op1, SDOperand Op2,
436 void *&InsertPos) {
437 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
438 return 0; // Never add these nodes.
439
440 // Check that remaining values produced are not flags.
441 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
442 if (N->getValueType(i) == MVT::Flag)
443 return 0; // Never CSE anything that produces a flag.
444
445 SelectionDAGCSEMap::NodeID ID;
446 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000447 ID.SetValueTypes(N->getVTList());
Chris Lattnera5682852006-08-07 23:03:03 +0000448 ID.SetOperands(Op1, Op2);
449 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
450}
451
452
453/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
454/// were replaced with those specified. If this node is never memoized,
455/// return null, otherwise return a pointer to the slot it would take. If a
456/// node already exists with these operands, the slot will be non-null.
457SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000458 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000459 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000460 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000461 return 0; // Never add these nodes.
462
463 // Check that remaining values produced are not flags.
464 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
465 if (N->getValueType(i) == MVT::Flag)
466 return 0; // Never CSE anything that produces a flag.
467
Chris Lattnera5682852006-08-07 23:03:03 +0000468 SelectionDAGCSEMap::NodeID ID;
469 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000470 ID.SetValueTypes(N->getVTList());
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000471 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000472 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000473}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000474
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000475
Chris Lattner78ec3112003-08-11 14:57:33 +0000476SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000477 while (!AllNodes.empty()) {
478 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000479 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000480 delete [] N->OperandList;
481 N->OperandList = 0;
482 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000483 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000484 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000485}
486
Chris Lattner0f2287b2005-04-13 02:38:18 +0000487SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000488 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000489 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000490 return getNode(ISD::AND, Op.getValueType(), Op,
491 getConstant(Imm, Op.getValueType()));
492}
493
Chris Lattner36ce6912005-11-29 06:21:05 +0000494SDOperand SelectionDAG::getString(const std::string &Val) {
495 StringSDNode *&N = StringNodes[Val];
496 if (!N) {
497 N = new StringSDNode(Val);
498 AllNodes.push_back(N);
499 }
500 return SDOperand(N, 0);
501}
502
Chris Lattner61b09412006-08-11 21:01:22 +0000503SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000504 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000505 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000506
Chris Lattner61b09412006-08-11 21:01:22 +0000507 // Mask out any bits that are not valid for this constant.
508 Val &= MVT::getIntVTBitMask(VT);
509
510 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000511 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000512 ID.AddInteger(Val);
513 void *IP = 0;
514 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
515 return SDOperand(E, 0);
516 SDNode *N = new ConstantSDNode(isT, Val, VT);
517 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000518 AllNodes.push_back(N);
519 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000520}
521
Chris Lattner61b09412006-08-11 21:01:22 +0000522
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000523SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
524 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000525 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
526 if (VT == MVT::f32)
527 Val = (float)Val; // Mask out extra precision.
528
Chris Lattnerd8658612005-02-17 20:17:32 +0000529 // Do the map lookup using the actual bit pattern for the floating point
530 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
531 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000532 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000533 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000534 ID.AddInteger(DoubleToBits(Val));
535 void *IP = 0;
536 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
537 return SDOperand(E, 0);
538 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
539 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000540 AllNodes.push_back(N);
541 return SDOperand(N, 0);
542}
543
Chris Lattnerc3aae252005-01-07 07:46:32 +0000544SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000545 MVT::ValueType VT, int Offset,
546 bool isTargetGA) {
547 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000548 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000549 ID.AddPointer(GV);
550 ID.AddInteger(Offset);
551 void *IP = 0;
552 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
553 return SDOperand(E, 0);
554 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
555 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000556 AllNodes.push_back(N);
557 return SDOperand(N, 0);
558}
559
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000560SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
561 bool isTarget) {
562 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000563 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000564 ID.AddInteger(FI);
565 void *IP = 0;
566 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
567 return SDOperand(E, 0);
568 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
569 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000570 AllNodes.push_back(N);
571 return SDOperand(N, 0);
572}
573
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000574SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
575 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000576 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000577 ID.AddInteger(JTI);
578 void *IP = 0;
579 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
580 return SDOperand(E, 0);
581 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
582 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000583 AllNodes.push_back(N);
584 return SDOperand(N, 0);
585}
586
Evan Chengb8973bd2006-01-31 22:23:14 +0000587SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000588 unsigned Alignment, int Offset,
589 bool isTarget) {
590 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000591 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000592 ID.AddInteger(Alignment);
593 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000594 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000595 void *IP = 0;
596 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
597 return SDOperand(E, 0);
598 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
599 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000600 AllNodes.push_back(N);
601 return SDOperand(N, 0);
602}
603
Chris Lattnerc3aae252005-01-07 07:46:32 +0000604
605SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000606 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000607 ID.AddPointer(MBB);
608 void *IP = 0;
609 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
610 return SDOperand(E, 0);
611 SDNode *N = new BasicBlockSDNode(MBB);
612 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000613 AllNodes.push_back(N);
614 return SDOperand(N, 0);
615}
616
Chris Lattner15e4b012005-07-10 00:07:11 +0000617SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
618 if ((unsigned)VT >= ValueTypeNodes.size())
619 ValueTypeNodes.resize(VT+1);
620 if (ValueTypeNodes[VT] == 0) {
621 ValueTypeNodes[VT] = new VTSDNode(VT);
622 AllNodes.push_back(ValueTypeNodes[VT]);
623 }
624
625 return SDOperand(ValueTypeNodes[VT], 0);
626}
627
Chris Lattnerc3aae252005-01-07 07:46:32 +0000628SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
629 SDNode *&N = ExternalSymbols[Sym];
630 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000631 N = new ExternalSymbolSDNode(false, Sym, VT);
632 AllNodes.push_back(N);
633 return SDOperand(N, 0);
634}
635
Chris Lattner809ec112006-01-28 10:09:25 +0000636SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
637 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000638 SDNode *&N = TargetExternalSymbols[Sym];
639 if (N) return SDOperand(N, 0);
640 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000641 AllNodes.push_back(N);
642 return SDOperand(N, 0);
643}
644
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000645SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
646 if ((unsigned)Cond >= CondCodeNodes.size())
647 CondCodeNodes.resize(Cond+1);
648
Chris Lattner079a27a2005-08-09 20:40:02 +0000649 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000650 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000651 AllNodes.push_back(CondCodeNodes[Cond]);
652 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000653 return SDOperand(CondCodeNodes[Cond], 0);
654}
655
Chris Lattner0fdd7682005-08-30 22:38:38 +0000656SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000657 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000658 ID.AddInteger(RegNo);
659 void *IP = 0;
660 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
661 return SDOperand(E, 0);
662 SDNode *N = new RegisterSDNode(RegNo, VT);
663 CSEMap.InsertNode(N, IP);
664 AllNodes.push_back(N);
665 return SDOperand(N, 0);
666}
667
668SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
669 assert((!V || isa<PointerType>(V->getType())) &&
670 "SrcValue is not a pointer?");
671
Chris Lattner0b3e5252006-08-15 19:11:05 +0000672 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000673 ID.AddPointer(V);
674 ID.AddInteger(Offset);
675 void *IP = 0;
676 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
677 return SDOperand(E, 0);
678 SDNode *N = new SrcValueSDNode(V, Offset);
679 CSEMap.InsertNode(N, IP);
680 AllNodes.push_back(N);
681 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000682}
683
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000684SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
685 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000686 // These setcc operations always fold.
687 switch (Cond) {
688 default: break;
689 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000690 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000691 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000692 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000693
694 case ISD::SETOEQ:
695 case ISD::SETOGT:
696 case ISD::SETOGE:
697 case ISD::SETOLT:
698 case ISD::SETOLE:
699 case ISD::SETONE:
700 case ISD::SETO:
701 case ISD::SETUO:
702 case ISD::SETUEQ:
703 case ISD::SETUNE:
704 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
705 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706 }
707
Chris Lattner67255a12005-04-07 18:14:58 +0000708 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
709 uint64_t C2 = N2C->getValue();
710 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
711 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000712
Chris Lattnerc3aae252005-01-07 07:46:32 +0000713 // Sign extend the operands if required
714 if (ISD::isSignedIntSetCC(Cond)) {
715 C1 = N1C->getSignExtended();
716 C2 = N2C->getSignExtended();
717 }
718
719 switch (Cond) {
720 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000721 case ISD::SETEQ: return getConstant(C1 == C2, VT);
722 case ISD::SETNE: return getConstant(C1 != C2, VT);
723 case ISD::SETULT: return getConstant(C1 < C2, VT);
724 case ISD::SETUGT: return getConstant(C1 > C2, VT);
725 case ISD::SETULE: return getConstant(C1 <= C2, VT);
726 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
727 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
728 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
729 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
730 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000731 }
Chris Lattner24673922005-04-07 18:58:54 +0000732 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000733 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000734 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
735 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
736
737 // If the comparison constant has bits in the upper part, the
738 // zero-extended value could never match.
739 if (C2 & (~0ULL << InSize)) {
740 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
741 switch (Cond) {
742 case ISD::SETUGT:
743 case ISD::SETUGE:
744 case ISD::SETEQ: return getConstant(0, VT);
745 case ISD::SETULT:
746 case ISD::SETULE:
747 case ISD::SETNE: return getConstant(1, VT);
748 case ISD::SETGT:
749 case ISD::SETGE:
750 // True if the sign bit of C2 is set.
751 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
752 case ISD::SETLT:
753 case ISD::SETLE:
754 // True if the sign bit of C2 isn't set.
755 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
756 default:
757 break;
758 }
759 }
760
761 // Otherwise, we can perform the comparison with the low bits.
762 switch (Cond) {
763 case ISD::SETEQ:
764 case ISD::SETNE:
765 case ISD::SETUGT:
766 case ISD::SETUGE:
767 case ISD::SETULT:
768 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000769 return getSetCC(VT, N1.getOperand(0),
770 getConstant(C2, N1.getOperand(0).getValueType()),
771 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000772 default:
773 break; // todo, be more careful with signed comparisons
774 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000775 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
776 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
777 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
778 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
779 MVT::ValueType ExtDstTy = N1.getValueType();
780 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000781
Chris Lattner7b2880c2005-08-24 22:44:39 +0000782 // If the extended part has any inconsistent bits, it cannot ever
783 // compare equal. In other words, they have to be all ones or all
784 // zeros.
785 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000786 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000787 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
788 return getConstant(Cond == ISD::SETNE, VT);
789
790 // Otherwise, make this a use of a zext.
791 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000792 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000793 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000794 }
795
Chris Lattner67255a12005-04-07 18:14:58 +0000796 uint64_t MinVal, MaxVal;
797 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
798 if (ISD::isSignedIntSetCC(Cond)) {
799 MinVal = 1ULL << (OperandBitSize-1);
800 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
801 MaxVal = ~0ULL >> (65-OperandBitSize);
802 else
803 MaxVal = 0;
804 } else {
805 MinVal = 0;
806 MaxVal = ~0ULL >> (64-OperandBitSize);
807 }
808
809 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
810 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
811 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
812 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000813 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
814 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000815 }
816
817 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
818 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
819 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000820 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
821 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000822 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000823
Nate Begeman72ea2812005-04-14 08:56:52 +0000824 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
825 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000826
Nate Begeman72ea2812005-04-14 08:56:52 +0000827 // Canonicalize setgt X, Min --> setne X, Min
828 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000829 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000830
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000831 // If we have setult X, 1, turn it into seteq X, 0
832 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000833 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
834 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000835 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000836 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000837 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
838 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000839
840 // If we have "setcc X, C1", check to see if we can shrink the immediate
841 // by changing cc.
842
843 // SETUGT X, SINTMAX -> SETLT X, 0
844 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
845 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000846 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000847
848 // FIXME: Implement the rest of these.
849
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000850
851 // Fold bit comparisons when we can.
852 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
853 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
854 if (ConstantSDNode *AndRHS =
855 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
856 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
857 // Perform the xform if the AND RHS is a single bit.
858 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
859 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000860 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000861 TLI.getShiftAmountTy()));
862 }
863 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
864 // (X & 8) == 8 --> (X & 8) >> 3
865 // Perform the xform if C2 is a single bit.
866 if ((C2 & (C2-1)) == 0) {
867 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000868 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000869 }
870 }
871 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000872 }
Chris Lattner67255a12005-04-07 18:14:58 +0000873 } else if (isa<ConstantSDNode>(N1.Val)) {
874 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000875 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000876 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000877
878 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
879 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
880 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000881
Chris Lattnerc3aae252005-01-07 07:46:32 +0000882 switch (Cond) {
883 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000884 case ISD::SETEQ: return getConstant(C1 == C2, VT);
885 case ISD::SETNE: return getConstant(C1 != C2, VT);
886 case ISD::SETLT: return getConstant(C1 < C2, VT);
887 case ISD::SETGT: return getConstant(C1 > C2, VT);
888 case ISD::SETLE: return getConstant(C1 <= C2, VT);
889 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000890 }
891 } else {
892 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000893 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000894 }
895
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000896 // Could not fold it.
897 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000898}
899
Chris Lattnerc3aae252005-01-07 07:46:32 +0000900/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000901///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000902SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000903 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000904 void *IP = 0;
905 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
906 return SDOperand(E, 0);
907 SDNode *N = new SDNode(Opcode, VT);
908 CSEMap.InsertNode(N, IP);
909
910 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 return SDOperand(N, 0);
912}
913
Chris Lattnerc3aae252005-01-07 07:46:32 +0000914SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
915 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000916 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000917 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000918 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
919 uint64_t Val = C->getValue();
920 switch (Opcode) {
921 default: break;
922 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000923 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000924 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
925 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000926 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
927 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000928 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000929 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000930 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000931 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000932 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000933 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000934 case ISD::BSWAP:
935 switch(VT) {
936 default: assert(0 && "Invalid bswap!"); break;
937 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
938 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
939 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
940 }
941 break;
942 case ISD::CTPOP:
943 switch(VT) {
944 default: assert(0 && "Invalid ctpop!"); break;
945 case MVT::i1: return getConstant(Val != 0, VT);
946 case MVT::i8:
947 Tmp1 = (unsigned)Val & 0xFF;
948 return getConstant(CountPopulation_32(Tmp1), VT);
949 case MVT::i16:
950 Tmp1 = (unsigned)Val & 0xFFFF;
951 return getConstant(CountPopulation_32(Tmp1), VT);
952 case MVT::i32:
953 return getConstant(CountPopulation_32((unsigned)Val), VT);
954 case MVT::i64:
955 return getConstant(CountPopulation_64(Val), VT);
956 }
957 case ISD::CTLZ:
958 switch(VT) {
959 default: assert(0 && "Invalid ctlz!"); break;
960 case MVT::i1: return getConstant(Val == 0, VT);
961 case MVT::i8:
962 Tmp1 = (unsigned)Val & 0xFF;
963 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
964 case MVT::i16:
965 Tmp1 = (unsigned)Val & 0xFFFF;
966 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
967 case MVT::i32:
968 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
969 case MVT::i64:
970 return getConstant(CountLeadingZeros_64(Val), VT);
971 }
972 case ISD::CTTZ:
973 switch(VT) {
974 default: assert(0 && "Invalid cttz!"); break;
975 case MVT::i1: return getConstant(Val == 0, VT);
976 case MVT::i8:
977 Tmp1 = (unsigned)Val | 0x100;
978 return getConstant(CountTrailingZeros_32(Tmp1), VT);
979 case MVT::i16:
980 Tmp1 = (unsigned)Val | 0x10000;
981 return getConstant(CountTrailingZeros_32(Tmp1), VT);
982 case MVT::i32:
983 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
984 case MVT::i64:
985 return getConstant(CountTrailingZeros_64(Val), VT);
986 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 }
988 }
989
Chris Lattner94683772005-12-23 05:30:37 +0000990 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000991 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
992 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000993 case ISD::FNEG:
994 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000995 case ISD::FABS:
996 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000997 case ISD::FP_ROUND:
998 case ISD::FP_EXTEND:
999 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001000 case ISD::FP_TO_SINT:
1001 return getConstant((int64_t)C->getValue(), VT);
1002 case ISD::FP_TO_UINT:
1003 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001004 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001005 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001006 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001007 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001008 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001009 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001010 }
1011
1012 unsigned OpOpcode = Operand.Val->getOpcode();
1013 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001014 case ISD::TokenFactor:
1015 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001016 case ISD::SIGN_EXTEND:
1017 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001018 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001019 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1020 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1021 break;
1022 case ISD::ZERO_EXTEND:
1023 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001024 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001025 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001026 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001027 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001028 case ISD::ANY_EXTEND:
1029 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001030 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001031 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1032 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1033 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1034 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001035 case ISD::TRUNCATE:
1036 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001037 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001038 if (OpOpcode == ISD::TRUNCATE)
1039 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001040 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1041 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001042 // If the source is smaller than the dest, we still need an extend.
1043 if (Operand.Val->getOperand(0).getValueType() < VT)
1044 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1045 else if (Operand.Val->getOperand(0).getValueType() > VT)
1046 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1047 else
1048 return Operand.Val->getOperand(0);
1049 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001050 break;
Chris Lattner35481892005-12-23 00:16:34 +00001051 case ISD::BIT_CONVERT:
1052 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001053 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001054 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001055 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001056 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1057 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001058 if (OpOpcode == ISD::UNDEF)
1059 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001060 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001061 case ISD::SCALAR_TO_VECTOR:
1062 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1063 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1064 "Illegal SCALAR_TO_VECTOR node!");
1065 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001066 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001067 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1068 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001069 Operand.Val->getOperand(0));
1070 if (OpOpcode == ISD::FNEG) // --X -> X
1071 return Operand.Val->getOperand(0);
1072 break;
1073 case ISD::FABS:
1074 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1075 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1076 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077 }
1078
Chris Lattner43247a12005-08-25 19:12:10 +00001079 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001080 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001081 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001082 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1083 void *IP = 0;
1084 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1085 return SDOperand(E, 0);
1086 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001087 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001088 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001089 } else {
1090 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001091 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001092 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 AllNodes.push_back(N);
1094 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001095}
1096
Chris Lattner36019aa2005-04-18 03:48:41 +00001097
1098
Chris Lattnerc3aae252005-01-07 07:46:32 +00001099SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1100 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001101#ifndef NDEBUG
1102 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001103 case ISD::TokenFactor:
1104 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1105 N2.getValueType() == MVT::Other && "Invalid token factor!");
1106 break;
Chris Lattner76365122005-01-16 02:23:22 +00001107 case ISD::AND:
1108 case ISD::OR:
1109 case ISD::XOR:
1110 case ISD::UDIV:
1111 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001112 case ISD::MULHU:
1113 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001114 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1115 // fall through
1116 case ISD::ADD:
1117 case ISD::SUB:
1118 case ISD::MUL:
1119 case ISD::SDIV:
1120 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001121 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1122 // fall through.
1123 case ISD::FADD:
1124 case ISD::FSUB:
1125 case ISD::FMUL:
1126 case ISD::FDIV:
1127 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001128 assert(N1.getValueType() == N2.getValueType() &&
1129 N1.getValueType() == VT && "Binary operator types must match!");
1130 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001131 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1132 assert(N1.getValueType() == VT &&
1133 MVT::isFloatingPoint(N1.getValueType()) &&
1134 MVT::isFloatingPoint(N2.getValueType()) &&
1135 "Invalid FCOPYSIGN!");
1136 break;
Chris Lattner76365122005-01-16 02:23:22 +00001137 case ISD::SHL:
1138 case ISD::SRA:
1139 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001140 case ISD::ROTL:
1141 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001142 assert(VT == N1.getValueType() &&
1143 "Shift operators return type must be the same as their first arg");
1144 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001145 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001146 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001147 case ISD::FP_ROUND_INREG: {
1148 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1149 assert(VT == N1.getValueType() && "Not an inreg round!");
1150 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1151 "Cannot FP_ROUND_INREG integer types");
1152 assert(EVT <= VT && "Not rounding down!");
1153 break;
1154 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001155 case ISD::AssertSext:
1156 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001157 case ISD::SIGN_EXTEND_INREG: {
1158 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1159 assert(VT == N1.getValueType() && "Not an inreg extend!");
1160 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1161 "Cannot *_EXTEND_INREG FP types");
1162 assert(EVT <= VT && "Not extending!");
1163 }
1164
Chris Lattner76365122005-01-16 02:23:22 +00001165 default: break;
1166 }
1167#endif
1168
Chris Lattnerc3aae252005-01-07 07:46:32 +00001169 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1170 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1171 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001172 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1173 int64_t Val = N1C->getValue();
1174 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1175 Val <<= 64-FromBits;
1176 Val >>= 64-FromBits;
1177 return getConstant(Val, VT);
1178 }
1179
Chris Lattnerc3aae252005-01-07 07:46:32 +00001180 if (N2C) {
1181 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1182 switch (Opcode) {
1183 case ISD::ADD: return getConstant(C1 + C2, VT);
1184 case ISD::SUB: return getConstant(C1 - C2, VT);
1185 case ISD::MUL: return getConstant(C1 * C2, VT);
1186 case ISD::UDIV:
1187 if (C2) return getConstant(C1 / C2, VT);
1188 break;
1189 case ISD::UREM :
1190 if (C2) return getConstant(C1 % C2, VT);
1191 break;
1192 case ISD::SDIV :
1193 if (C2) return getConstant(N1C->getSignExtended() /
1194 N2C->getSignExtended(), VT);
1195 break;
1196 case ISD::SREM :
1197 if (C2) return getConstant(N1C->getSignExtended() %
1198 N2C->getSignExtended(), VT);
1199 break;
1200 case ISD::AND : return getConstant(C1 & C2, VT);
1201 case ISD::OR : return getConstant(C1 | C2, VT);
1202 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001203 case ISD::SHL : return getConstant(C1 << C2, VT);
1204 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001205 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001206 case ISD::ROTL :
1207 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1208 VT);
1209 case ISD::ROTR :
1210 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1211 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001212 default: break;
1213 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214 } else { // Cannonicalize constant to RHS if commutative
1215 if (isCommutativeBinOp(Opcode)) {
1216 std::swap(N1C, N2C);
1217 std::swap(N1, N2);
1218 }
1219 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 }
1221
1222 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1223 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001224 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001225 if (N2CFP) {
1226 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1227 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001228 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1229 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1230 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1231 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001232 if (C2) return getConstantFP(C1 / C2, VT);
1233 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001234 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001235 if (C2) return getConstantFP(fmod(C1, C2), VT);
1236 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001237 case ISD::FCOPYSIGN: {
1238 union {
1239 double F;
1240 uint64_t I;
1241 } u1;
1242 union {
1243 double F;
1244 int64_t I;
1245 } u2;
1246 u1.F = C1;
1247 u2.F = C2;
1248 if (u2.I < 0) // Sign bit of RHS set?
1249 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1250 else
1251 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1252 return getConstantFP(u1.F, VT);
1253 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001254 default: break;
1255 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001256 } else { // Cannonicalize constant to RHS if commutative
1257 if (isCommutativeBinOp(Opcode)) {
1258 std::swap(N1CFP, N2CFP);
1259 std::swap(N1, N2);
1260 }
1261 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001262 }
Chris Lattner62b57722006-04-20 05:39:12 +00001263
1264 // Canonicalize an UNDEF to the RHS, even over a constant.
1265 if (N1.getOpcode() == ISD::UNDEF) {
1266 if (isCommutativeBinOp(Opcode)) {
1267 std::swap(N1, N2);
1268 } else {
1269 switch (Opcode) {
1270 case ISD::FP_ROUND_INREG:
1271 case ISD::SIGN_EXTEND_INREG:
1272 case ISD::SUB:
1273 case ISD::FSUB:
1274 case ISD::FDIV:
1275 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001276 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001277 return N1; // fold op(undef, arg2) -> undef
1278 case ISD::UDIV:
1279 case ISD::SDIV:
1280 case ISD::UREM:
1281 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001282 case ISD::SRL:
1283 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001284 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1285 }
1286 }
1287 }
1288
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001289 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001290 if (N2.getOpcode() == ISD::UNDEF) {
1291 switch (Opcode) {
1292 case ISD::ADD:
1293 case ISD::SUB:
1294 case ISD::FADD:
1295 case ISD::FSUB:
1296 case ISD::FMUL:
1297 case ISD::FDIV:
1298 case ISD::FREM:
1299 case ISD::UDIV:
1300 case ISD::SDIV:
1301 case ISD::UREM:
1302 case ISD::SREM:
1303 case ISD::XOR:
1304 return N2; // fold op(arg1, undef) -> undef
1305 case ISD::MUL:
1306 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001307 case ISD::SRL:
1308 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001309 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1310 case ISD::OR:
1311 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001312 case ISD::SRA:
1313 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001314 }
1315 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001316
Chris Lattnerc3aae252005-01-07 07:46:32 +00001317 // Finally, fold operations that do not require constants.
1318 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001319 case ISD::FP_ROUND_INREG:
1320 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1321 break;
1322 case ISD::SIGN_EXTEND_INREG: {
1323 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1324 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001325 break;
1326 }
1327
Nate Begemaneea805e2005-04-13 21:23:31 +00001328 // FIXME: figure out how to safely handle things like
1329 // int foo(int x) { return 1 << (x & 255); }
1330 // int bar() { return foo(256); }
1331#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001332 case ISD::SHL:
1333 case ISD::SRL:
1334 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001335 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001336 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001337 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001338 else if (N2.getOpcode() == ISD::AND)
1339 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1340 // If the and is only masking out bits that cannot effect the shift,
1341 // eliminate the and.
1342 unsigned NumBits = MVT::getSizeInBits(VT);
1343 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1344 return getNode(Opcode, VT, N1, N2.getOperand(0));
1345 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001346 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001347#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001348 }
1349
Chris Lattner27e9b412005-05-11 18:57:39 +00001350 // Memoize this node if possible.
1351 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001352 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001353 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001354 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1355 void *IP = 0;
1356 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1357 return SDOperand(E, 0);
1358 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001359 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001360 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001361 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001362 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001363 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001364 }
1365
Chris Lattnerc3aae252005-01-07 07:46:32 +00001366 AllNodes.push_back(N);
1367 return SDOperand(N, 0);
1368}
1369
Chris Lattnerc3aae252005-01-07 07:46:32 +00001370SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1371 SDOperand N1, SDOperand N2, SDOperand N3) {
1372 // Perform various simplifications.
1373 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1374 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001375 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001376 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001377 case ISD::SETCC: {
1378 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001379 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001380 if (Simp.Val) return Simp;
1381 break;
1382 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001383 case ISD::SELECT:
1384 if (N1C)
1385 if (N1C->getValue())
1386 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001387 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001388 return N3; // select false, X, Y -> Y
1389
1390 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001391 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001392 case ISD::BRCOND:
1393 if (N2C)
1394 if (N2C->getValue()) // Unconditional branch
1395 return getNode(ISD::BR, MVT::Other, N1, N3);
1396 else
1397 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001398 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001399 case ISD::VECTOR_SHUFFLE:
1400 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1401 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1402 N3.getOpcode() == ISD::BUILD_VECTOR &&
1403 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1404 "Illegal VECTOR_SHUFFLE node!");
1405 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001406 }
1407
Chris Lattner43247a12005-08-25 19:12:10 +00001408 // Memoize node if it doesn't produce a flag.
1409 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001410 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001411 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001412 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1413 void *IP = 0;
1414 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1415 return SDOperand(E, 0);
1416 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001417 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001418 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001419 } else {
1420 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001421 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001422 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001423 AllNodes.push_back(N);
1424 return SDOperand(N, 0);
1425}
1426
1427SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001428 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001429 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001430 SDOperand Ops[] = { N1, N2, N3, N4 };
1431 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001432}
1433
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001434SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1435 SDOperand N1, SDOperand N2, SDOperand N3,
1436 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001437 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1438 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001439}
1440
Evan Cheng7038daf2005-12-10 00:37:58 +00001441SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1442 SDOperand Chain, SDOperand Ptr,
1443 SDOperand SV) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001444 SDVTList VTs = getVTList(VT, MVT::Other);
Chris Lattnera5682852006-08-07 23:03:03 +00001445
1446 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1447 void *IP = 0;
1448 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1449 return SDOperand(E, 0);
1450 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001451 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001452 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001453 AllNodes.push_back(N);
1454 return SDOperand(N, 0);
1455}
1456
1457SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1458 SDOperand Chain, SDOperand Ptr,
1459 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001460 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1461 getValueType(EVT) };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001462 // Add token chain.
Chris Lattner70046e92006-08-15 17:46:01 +00001463 const MVT::ValueType *VTs = getNodeValueTypes(MVT::Vector, MVT::Other);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001464 return getNode(ISD::VLOAD, VTs, 2, Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001465}
1466
1467SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1468 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1469 MVT::ValueType EVT) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001470 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
Chris Lattner70046e92006-08-15 17:46:01 +00001471 const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001472 return getNode(Opcode, VTs, 2, Ops, 4);
Evan Cheng7038daf2005-12-10 00:37:58 +00001473}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001474
Nate Begemanacc398c2006-01-25 18:21:52 +00001475SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1476 SDOperand Chain, SDOperand Ptr,
1477 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001478 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001479 // Add token chain.
Chris Lattner70046e92006-08-15 17:46:01 +00001480 const MVT::ValueType *VTs = getNodeValueTypes(VT, MVT::Other);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001481 return getNode(ISD::VAARG, VTs, 2, Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001482}
1483
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001484SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001485 const SDOperand *Ops, unsigned NumOps) {
1486 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001487 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001488 case 1: return getNode(Opcode, VT, Ops[0]);
1489 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1490 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001491 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001492 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001493
Chris Lattneref847df2005-04-09 03:27:28 +00001494 switch (Opcode) {
1495 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001496 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001497 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001498 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1499#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1500 // If this is a truncating store of a constant, convert to the desired type
1501 // and store it instead.
1502 if (isa<Constant>(Ops[0])) {
1503 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1504 if (isa<Constant>(Op))
1505 N1 = Op;
1506 }
1507 // Also for ConstantFP?
1508#endif
1509 if (Ops[0].getValueType() == EVT) // Normal store?
1510 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1511 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1512 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1513 "Can't do FP-INT conversion!");
1514 break;
1515 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001516 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001517 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001518 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1519 "LHS and RHS of condition must have same type!");
1520 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1521 "True and False arms of SelectCC must have same type!");
1522 assert(Ops[2].getValueType() == VT &&
1523 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001524 break;
1525 }
1526 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001527 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001528 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1529 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001530 break;
1531 }
Chris Lattneref847df2005-04-09 03:27:28 +00001532 }
1533
Chris Lattner385328c2005-05-14 07:42:29 +00001534 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001535 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001536 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001537 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001538 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001539 void *IP = 0;
1540 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1541 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001542 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001543 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001544 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001545 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001546 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001547 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001548 }
Chris Lattneref847df2005-04-09 03:27:28 +00001549 AllNodes.push_back(N);
1550 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001551}
1552
Chris Lattner89c34632005-05-14 06:20:26 +00001553SDOperand SelectionDAG::getNode(unsigned Opcode,
1554 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001555 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001556 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1557 Ops, NumOps);
1558}
1559
1560SDOperand SelectionDAG::getNode(unsigned Opcode,
1561 const MVT::ValueType *VTs, unsigned NumVTs,
1562 const SDOperand *Ops, unsigned NumOps) {
1563 if (NumVTs == 1)
1564 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001565
Chris Lattner5f056bf2005-07-10 01:55:33 +00001566 switch (Opcode) {
1567 case ISD::EXTLOAD:
1568 case ISD::SEXTLOAD:
1569 case ISD::ZEXTLOAD: {
1570 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001571 assert(NumOps == 4 && NumVTs == 2 && "Bad *EXTLOAD!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001572 // If they are asking for an extending load from/to the same thing, return a
1573 // normal load.
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001574 if (VTs[0] == EVT)
1575 return getLoad(VTs[0], Ops[0], Ops[1], Ops[2]);
1576 if (MVT::isVector(VTs[0])) {
1577 assert(EVT == MVT::getVectorBaseType(VTs[0]) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001578 "Invalid vector extload!");
1579 } else {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001580 assert(EVT < VTs[0] &&
Chris Lattnerce872152006-03-19 06:31:19 +00001581 "Should only be an extending load, not truncating!");
1582 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001583 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VTs[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001584 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001585 assert(MVT::isInteger(VTs[0]) == MVT::isInteger(EVT) &&
Chris Lattner5f056bf2005-07-10 01:55:33 +00001586 "Cannot convert from FP to Int or Int -> FP!");
1587 break;
1588 }
1589
Chris Lattnere89083a2005-05-14 07:25:05 +00001590 // FIXME: figure out how to safely handle things like
1591 // int foo(int x) { return 1 << (x & 255); }
1592 // int bar() { return foo(256); }
1593#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001594 case ISD::SRA_PARTS:
1595 case ISD::SRL_PARTS:
1596 case ISD::SHL_PARTS:
1597 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001598 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001599 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1600 else if (N3.getOpcode() == ISD::AND)
1601 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1602 // If the and is only masking out bits that cannot effect the shift,
1603 // eliminate the and.
1604 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1605 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1606 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1607 }
1608 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001609#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001610 }
Chris Lattner89c34632005-05-14 06:20:26 +00001611
Chris Lattner43247a12005-08-25 19:12:10 +00001612 // Memoize the node unless it returns a flag.
1613 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001614 SDVTList VTList = makeVTList(VTs, NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001615 if (VTs[NumVTs-1] != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001616 SelectionDAGCSEMap::NodeID ID;
1617 ID.SetOpcode(Opcode);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001618 ID.SetValueTypes(VTList);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001619 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001620 void *IP = 0;
1621 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1622 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001623 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001624 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001625 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001626 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001627 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001628 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001629 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001630 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001631 return SDOperand(N, 0);
1632}
1633
Chris Lattner70046e92006-08-15 17:46:01 +00001634SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1635 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001636}
1637
Chris Lattner70046e92006-08-15 17:46:01 +00001638SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001639 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1640 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001641 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001642 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001643 }
1644 std::vector<MVT::ValueType> V;
1645 V.push_back(VT1);
1646 V.push_back(VT2);
1647 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001648 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001649}
Chris Lattner70046e92006-08-15 17:46:01 +00001650SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1651 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001652 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001653 E = VTList.end(); I != E; ++I) {
1654 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1655 (*I)[2] == VT3)
1656 return makeVTList(&(*I)[0], 3);
1657 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001658 std::vector<MVT::ValueType> V;
1659 V.push_back(VT1);
1660 V.push_back(VT2);
1661 V.push_back(VT3);
1662 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001663 return makeVTList(&(*VTList.begin())[0], 3);
1664}
1665
1666SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1667 switch (NumVTs) {
1668 case 0: assert(0 && "Cannot have nodes without results!");
1669 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1670 case 2: return getVTList(VTs[0], VTs[1]);
1671 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1672 default: break;
1673 }
1674
1675 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1676 E = VTList.end(); I != E; ++I) {
1677 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1678
1679 bool NoMatch = false;
1680 for (unsigned i = 2; i != NumVTs; ++i)
1681 if (VTs[i] != (*I)[i]) {
1682 NoMatch = true;
1683 break;
1684 }
1685 if (!NoMatch)
1686 return makeVTList(&*I->begin(), NumVTs);
1687 }
1688
1689 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1690 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001691}
1692
1693
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001694/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1695/// specified operands. If the resultant node already exists in the DAG,
1696/// this does not modify the specified node, instead it returns the node that
1697/// already exists. If the resultant node does not exist in the DAG, the
1698/// input node is returned. As a degenerate case, if you specify the same
1699/// input operands as the node already has, the input node is returned.
1700SDOperand SelectionDAG::
1701UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1702 SDNode *N = InN.Val;
1703 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1704
1705 // Check to see if there is no change.
1706 if (Op == N->getOperand(0)) return InN;
1707
1708 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001709 void *InsertPos = 0;
1710 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1711 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001712
1713 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001714 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001715 RemoveNodeFromCSEMaps(N);
1716
1717 // Now we update the operands.
1718 N->OperandList[0].Val->removeUser(N);
1719 Op.Val->addUser(N);
1720 N->OperandList[0] = Op;
1721
1722 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001723 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001724 return InN;
1725}
1726
1727SDOperand SelectionDAG::
1728UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1729 SDNode *N = InN.Val;
1730 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1731
1732 // Check to see if there is no change.
1733 bool AnyChange = false;
1734 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1735 return InN; // No operands changed, just return the input node.
1736
1737 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001738 void *InsertPos = 0;
1739 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1740 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001741
1742 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001743 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001744 RemoveNodeFromCSEMaps(N);
1745
1746 // Now we update the operands.
1747 if (N->OperandList[0] != Op1) {
1748 N->OperandList[0].Val->removeUser(N);
1749 Op1.Val->addUser(N);
1750 N->OperandList[0] = Op1;
1751 }
1752 if (N->OperandList[1] != Op2) {
1753 N->OperandList[1].Val->removeUser(N);
1754 Op2.Val->addUser(N);
1755 N->OperandList[1] = Op2;
1756 }
1757
1758 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001759 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001760 return InN;
1761}
1762
1763SDOperand SelectionDAG::
1764UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001765 SDOperand Ops[] = { Op1, Op2, Op3 };
1766 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001767}
1768
1769SDOperand SelectionDAG::
1770UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1771 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001772 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1773 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001774}
1775
1776SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001777UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1778 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001779 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1780 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001781}
1782
1783
1784SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001785UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001786 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001787 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001788 "Update with wrong number of operands");
1789
1790 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001791 bool AnyChange = false;
1792 for (unsigned i = 0; i != NumOps; ++i) {
1793 if (Ops[i] != N->getOperand(i)) {
1794 AnyChange = true;
1795 break;
1796 }
1797 }
1798
1799 // No operands changed, just return the input node.
1800 if (!AnyChange) return InN;
1801
1802 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001803 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001804 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001805 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001806
1807 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001808 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001809 RemoveNodeFromCSEMaps(N);
1810
1811 // Now we update the operands.
1812 for (unsigned i = 0; i != NumOps; ++i) {
1813 if (N->OperandList[i] != Ops[i]) {
1814 N->OperandList[i].Val->removeUser(N);
1815 Ops[i].Val->addUser(N);
1816 N->OperandList[i] = Ops[i];
1817 }
1818 }
1819
1820 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001821 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001822 return InN;
1823}
1824
1825
1826
Chris Lattner149c58c2005-08-16 18:17:10 +00001827
1828/// SelectNodeTo - These are used for target selectors to *mutate* the
1829/// specified node to have the specified return type, Target opcode, and
1830/// operands. Note that target opcodes are stored as
1831/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001832///
1833/// Note that SelectNodeTo returns the resultant node. If there is already a
1834/// node of the specified opcode and operands, it returns that node instead of
1835/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001836SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1837 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001838 SDVTList VTs = getVTList(VT);
Chris Lattner4a283e92006-08-11 18:38:11 +00001839 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1840 void *IP = 0;
1841 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1842 return SDOperand(ON, 0);
1843
Chris Lattner7651fa42005-08-24 23:00:29 +00001844 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001845
Chris Lattner7651fa42005-08-24 23:00:29 +00001846 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001847 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001848
Chris Lattner4a283e92006-08-11 18:38:11 +00001849 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001850 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001851}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001852
Chris Lattnereb19e402005-11-30 22:45:14 +00001853SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1854 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001855 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001856 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001857 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1858 void *IP = 0;
1859 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1860 return SDOperand(ON, 0);
1861
Chris Lattner149c58c2005-08-16 18:17:10 +00001862 RemoveNodeFromCSEMaps(N);
1863 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001864 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001865 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001866 CSEMap.InsertNode(N, IP);
Chris Lattnereb19e402005-11-30 22:45:14 +00001867 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001868}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001869
Chris Lattnereb19e402005-11-30 22:45:14 +00001870SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1871 MVT::ValueType VT, SDOperand Op1,
1872 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001873 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001874 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001875 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1876 void *IP = 0;
1877 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1878 return SDOperand(ON, 0);
1879
Chris Lattner149c58c2005-08-16 18:17:10 +00001880 RemoveNodeFromCSEMaps(N);
1881 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001882 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001883 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001884
Chris Lattnera5682852006-08-07 23:03:03 +00001885 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001886 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001887}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001888
Chris Lattnereb19e402005-11-30 22:45:14 +00001889SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1890 MVT::ValueType VT, SDOperand Op1,
1891 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001892 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001893 SDVTList VTs = getVTList(VT);
1894 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1895 Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00001896 void *IP = 0;
1897 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1898 return SDOperand(ON, 0);
1899
Chris Lattner149c58c2005-08-16 18:17:10 +00001900 RemoveNodeFromCSEMaps(N);
1901 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001902 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001903 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001904
Chris Lattnera5682852006-08-07 23:03:03 +00001905 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001906 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001907}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001908
Chris Lattnereb19e402005-11-30 22:45:14 +00001909SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1910 MVT::ValueType VT, SDOperand Op1,
1911 SDOperand Op2, SDOperand Op3,
1912 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001913 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001914 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001915 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1916 ID.AddOperand(Op1);
1917 ID.AddOperand(Op2);
1918 ID.AddOperand(Op3);
1919 ID.AddOperand(Op4);
1920 void *IP = 0;
1921 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1922 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001923
Nate Begeman294a0a12005-08-18 07:30:15 +00001924 RemoveNodeFromCSEMaps(N);
1925 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001926 N->setValueTypes(VTs);
Nate Begeman294a0a12005-08-18 07:30:15 +00001927 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001928
Chris Lattnera5682852006-08-07 23:03:03 +00001929 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001930 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001931}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001932
Chris Lattnereb19e402005-11-30 22:45:14 +00001933SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1934 MVT::ValueType VT, SDOperand Op1,
Chris Lattnera5682852006-08-07 23:03:03 +00001935 SDOperand Op2, SDOperand Op3,
1936 SDOperand Op4, SDOperand Op5) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001937 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001938 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1939 ID.AddOperand(Op1);
1940 ID.AddOperand(Op2);
1941 ID.AddOperand(Op3);
1942 ID.AddOperand(Op4);
1943 ID.AddOperand(Op5);
1944 void *IP = 0;
1945 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1946 return SDOperand(ON, 0);
1947
Chris Lattner6b09a292005-08-21 18:49:33 +00001948 RemoveNodeFromCSEMaps(N);
1949 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001950 N->setValueTypes(VTs);
Chris Lattner6b09a292005-08-21 18:49:33 +00001951 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001952
Chris Lattnera5682852006-08-07 23:03:03 +00001953 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001954 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001955}
Chris Lattner149c58c2005-08-16 18:17:10 +00001956
Chris Lattnereb19e402005-11-30 22:45:14 +00001957SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1958 MVT::ValueType VT, SDOperand Op1,
1959 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1960 SDOperand Op5, SDOperand Op6) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001961 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001962 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1963 ID.AddOperand(Op1);
1964 ID.AddOperand(Op2);
1965 ID.AddOperand(Op3);
1966 ID.AddOperand(Op4);
1967 ID.AddOperand(Op5);
1968 ID.AddOperand(Op6);
1969 void *IP = 0;
1970 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1971 return SDOperand(ON, 0);
1972
Evan Cheng61ca74b2005-11-30 02:04:11 +00001973 RemoveNodeFromCSEMaps(N);
1974 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001975 N->setValueTypes(VTs);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001976 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001977
Chris Lattnera5682852006-08-07 23:03:03 +00001978 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001979 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001980}
1981
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001982SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1983 MVT::ValueType VT, SDOperand Op1,
1984 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1985 SDOperand Op5, SDOperand Op6,
1986 SDOperand Op7) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001987 SDVTList VTs = getVTList(VT);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001988 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00001989 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1990 ID.AddOperand(Op1);
1991 ID.AddOperand(Op2);
1992 ID.AddOperand(Op3);
1993 ID.AddOperand(Op4);
1994 ID.AddOperand(Op5);
1995 ID.AddOperand(Op6);
1996 ID.AddOperand(Op7);
1997 void *IP = 0;
1998 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
1999 return SDOperand(ON, 0);
2000
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002001 RemoveNodeFromCSEMaps(N);
2002 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002003 N->setValueTypes(VTs);
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002004 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2005
Chris Lattnera5682852006-08-07 23:03:03 +00002006 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002007 return SDOperand(N, 0);
2008}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002009SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2010 MVT::ValueType VT, SDOperand Op1,
2011 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2012 SDOperand Op5, SDOperand Op6,
2013 SDOperand Op7, SDOperand Op8) {
2014 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002015 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00002016 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2017 ID.AddOperand(Op1);
2018 ID.AddOperand(Op2);
2019 ID.AddOperand(Op3);
2020 ID.AddOperand(Op4);
2021 ID.AddOperand(Op5);
2022 ID.AddOperand(Op6);
2023 ID.AddOperand(Op7);
2024 ID.AddOperand(Op8);
2025 void *IP = 0;
2026 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2027 return SDOperand(ON, 0);
2028
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002029 RemoveNodeFromCSEMaps(N);
2030 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002031 N->setValueTypes(VTs);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002032 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2033
Chris Lattnera5682852006-08-07 23:03:03 +00002034 CSEMap.InsertNode(N, IP); // Memoize the new node.
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002035 return SDOperand(N, 0);
2036}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002037
Chris Lattnereb19e402005-11-30 22:45:14 +00002038SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2039 MVT::ValueType VT1, MVT::ValueType VT2,
2040 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002041 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002042 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
2043 void *IP = 0;
2044 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2045 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002046
Chris Lattner0fb094f2005-11-19 01:44:53 +00002047 RemoveNodeFromCSEMaps(N);
2048 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002049 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002050 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002051
Chris Lattnera5682852006-08-07 23:03:03 +00002052 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002053 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002054}
2055
Chris Lattnereb19e402005-11-30 22:45:14 +00002056SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2057 MVT::ValueType VT1, MVT::ValueType VT2,
2058 SDOperand Op1, SDOperand Op2,
2059 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002060 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002061 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002062 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
2063 Op1, Op2, Op3);
2064 void *IP = 0;
2065 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2066 return SDOperand(ON, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002067
Chris Lattner0fb094f2005-11-19 01:44:53 +00002068 RemoveNodeFromCSEMaps(N);
2069 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002070 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002071 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002072
Chris Lattnera5682852006-08-07 23:03:03 +00002073 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002074 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002075}
2076
Chris Lattnereb19e402005-11-30 22:45:14 +00002077SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2078 MVT::ValueType VT1, MVT::ValueType VT2,
2079 SDOperand Op1, SDOperand Op2,
2080 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002081 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002082 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002083 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2084 ID.AddOperand(Op1);
2085 ID.AddOperand(Op2);
2086 ID.AddOperand(Op3);
2087 ID.AddOperand(Op4);
2088 void *IP = 0;
2089 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2090 return SDOperand(ON, 0);
2091
Chris Lattner0fb094f2005-11-19 01:44:53 +00002092 RemoveNodeFromCSEMaps(N);
2093 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002094 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002095 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002096
Chris Lattnera5682852006-08-07 23:03:03 +00002097 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002098 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002099}
2100
Chris Lattnereb19e402005-11-30 22:45:14 +00002101SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2102 MVT::ValueType VT1, MVT::ValueType VT2,
2103 SDOperand Op1, SDOperand Op2,
2104 SDOperand Op3, SDOperand Op4,
2105 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002106 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002107 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00002108 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
2109 ID.AddOperand(Op1);
2110 ID.AddOperand(Op2);
2111 ID.AddOperand(Op3);
2112 ID.AddOperand(Op4);
2113 ID.AddOperand(Op5);
2114 void *IP = 0;
2115 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
2116 return SDOperand(ON, 0);
2117
Chris Lattner0fb094f2005-11-19 01:44:53 +00002118 RemoveNodeFromCSEMaps(N);
2119 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00002120 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002121 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002122
Chris Lattnera5682852006-08-07 23:03:03 +00002123 CSEMap.InsertNode(N, IP); // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002124 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002125}
2126
Evan Cheng6ae46c42006-02-09 07:15:23 +00002127/// getTargetNode - These are used for target selectors to create a new node
2128/// with specified return type(s), target opcode, and operands.
2129///
2130/// Note that getTargetNode returns the resultant node. If there is already a
2131/// node of the specified opcode and operands, it returns that node instead of
2132/// the current one.
2133SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2134 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2135}
2136SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2137 SDOperand Op1) {
2138 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2139}
2140SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2141 SDOperand Op1, SDOperand Op2) {
2142 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2143}
2144SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2145 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2146 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2147}
2148SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2149 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2150 SDOperand Op4) {
2151 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2152}
2153SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2154 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2155 SDOperand Op4, SDOperand Op5) {
2156 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2157}
2158SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2159 SDOperand Op1, SDOperand Op2, SDOperand Op3,
Chris Lattnera5682852006-08-07 23:03:03 +00002160 SDOperand Op4, SDOperand Op5,
2161 SDOperand Op6) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002162 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
2163 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002164}
2165SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2166 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2167 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2168 SDOperand Op7) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002169 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
2170 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002171}
2172SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2173 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2174 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2175 SDOperand Op7, SDOperand Op8) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002176 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8 };
2177 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, 8).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002178}
2179SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002180 const SDOperand *Ops, unsigned NumOps) {
2181 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002182}
2183SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2184 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00002185 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002186 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002187}
2188SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002189 MVT::ValueType VT2, SDOperand Op1,
2190 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002191 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002192 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002193 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002194}
2195SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002196 MVT::ValueType VT2, SDOperand Op1,
2197 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00002198 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002199 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002200 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002201}
2202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002203 MVT::ValueType VT2, SDOperand Op1,
2204 SDOperand Op2, SDOperand Op3,
2205 SDOperand Op4) {
Chris Lattner70046e92006-08-15 17:46:01 +00002206 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002207 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002208 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 4).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002209}
2210SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002211 MVT::ValueType VT2, SDOperand Op1,
2212 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2213 SDOperand Op5) {
Chris Lattner70046e92006-08-15 17:46:01 +00002214 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002215 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002216 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 5).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002217}
2218SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002219 MVT::ValueType VT2, SDOperand Op1,
2220 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2221 SDOperand Op5, SDOperand Op6) {
Chris Lattner70046e92006-08-15 17:46:01 +00002222 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002223 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002224 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002225}
2226SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002227 MVT::ValueType VT2, SDOperand Op1,
2228 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2229 SDOperand Op5, SDOperand Op6,
2230 SDOperand Op7) {
Chris Lattner70046e92006-08-15 17:46:01 +00002231 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002232 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002233 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002234}
2235SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2236 MVT::ValueType VT2, MVT::ValueType VT3,
2237 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002238 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002239 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002240 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002241}
2242SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2243 MVT::ValueType VT2, MVT::ValueType VT3,
2244 SDOperand Op1, SDOperand Op2,
Chris Lattnera5682852006-08-07 23:03:03 +00002245 SDOperand Op3, SDOperand Op4,
2246 SDOperand Op5) {
Chris Lattner70046e92006-08-15 17:46:01 +00002247 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002248 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002249 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 5).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) {
Chris Lattner70046e92006-08-15 17:46:01 +00002256 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002257 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002258 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 6).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002259}
2260SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2261 MVT::ValueType VT2, MVT::ValueType VT3,
2262 SDOperand Op1, SDOperand Op2,
2263 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2264 SDOperand Op6, SDOperand Op7) {
Chris Lattner70046e92006-08-15 17:46:01 +00002265 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002266 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5, Op6, Op7 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002267 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 7).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002268}
2269SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00002270 MVT::ValueType VT2,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002271 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002272 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002273 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002274}
2275
Evan Cheng99157a02006-08-07 22:13:29 +00002276/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002277/// This can cause recursive merging of nodes in the DAG.
2278///
Chris Lattner8b52f212005-08-26 18:36:28 +00002279/// This version assumes From/To have a single result value.
2280///
Chris Lattner1e111c72005-09-07 05:37:01 +00002281void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2282 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002283 SDNode *From = FromN.Val, *To = ToN.Val;
2284 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2285 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002286 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002287
Chris Lattner8b8749f2005-08-17 19:00:20 +00002288 while (!From->use_empty()) {
2289 // Process users until they are all gone.
2290 SDNode *U = *From->use_begin();
2291
2292 // This node is about to morph, remove its old self from the CSE maps.
2293 RemoveNodeFromCSEMaps(U);
2294
Chris Lattner65113b22005-11-08 22:07:03 +00002295 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2296 I != E; ++I)
2297 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002298 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002299 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002300 To->addUser(U);
2301 }
2302
2303 // Now that we have modified U, add it back to the CSE maps. If it already
2304 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002305 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2306 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002307 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002308 if (Deleted) Deleted->push_back(U);
2309 DeleteNodeNotInCSEMaps(U);
2310 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002311 }
2312}
2313
2314/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2315/// This can cause recursive merging of nodes in the DAG.
2316///
2317/// This version assumes From/To have matching types and numbers of result
2318/// values.
2319///
Chris Lattner1e111c72005-09-07 05:37:01 +00002320void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2321 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002322 assert(From != To && "Cannot replace uses of with self");
2323 assert(From->getNumValues() == To->getNumValues() &&
2324 "Cannot use this version of ReplaceAllUsesWith!");
2325 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002326 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002327 return;
2328 }
2329
2330 while (!From->use_empty()) {
2331 // Process users until they are all gone.
2332 SDNode *U = *From->use_begin();
2333
2334 // This node is about to morph, remove its old self from the CSE maps.
2335 RemoveNodeFromCSEMaps(U);
2336
Chris Lattner65113b22005-11-08 22:07:03 +00002337 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2338 I != E; ++I)
2339 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002340 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002341 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002342 To->addUser(U);
2343 }
2344
2345 // Now that we have modified U, add it back to the CSE maps. If it already
2346 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002347 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2348 ReplaceAllUsesWith(U, Existing, Deleted);
2349 // U is now dead.
2350 if (Deleted) Deleted->push_back(U);
2351 DeleteNodeNotInCSEMaps(U);
2352 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002353 }
2354}
2355
Chris Lattner8b52f212005-08-26 18:36:28 +00002356/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2357/// This can cause recursive merging of nodes in the DAG.
2358///
2359/// This version can replace From with any result values. To must match the
2360/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002361void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002362 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002363 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002364 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002365 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002366 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002367 return;
2368 }
2369
2370 while (!From->use_empty()) {
2371 // Process users until they are all gone.
2372 SDNode *U = *From->use_begin();
2373
2374 // This node is about to morph, remove its old self from the CSE maps.
2375 RemoveNodeFromCSEMaps(U);
2376
Chris Lattner65113b22005-11-08 22:07:03 +00002377 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2378 I != E; ++I)
2379 if (I->Val == From) {
2380 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002381 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002382 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002383 ToOp.Val->addUser(U);
2384 }
2385
2386 // Now that we have modified U, add it back to the CSE maps. If it already
2387 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002388 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2389 ReplaceAllUsesWith(U, Existing, Deleted);
2390 // U is now dead.
2391 if (Deleted) Deleted->push_back(U);
2392 DeleteNodeNotInCSEMaps(U);
2393 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002394 }
2395}
2396
Chris Lattner012f2412006-02-17 21:58:01 +00002397/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2398/// uses of other values produced by From.Val alone. The Deleted vector is
2399/// handled the same was as for ReplaceAllUsesWith.
2400void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2401 std::vector<SDNode*> &Deleted) {
2402 assert(From != To && "Cannot replace a value with itself");
2403 // Handle the simple, trivial, case efficiently.
2404 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2405 ReplaceAllUsesWith(From, To, &Deleted);
2406 return;
2407 }
2408
2409 // Get all of the users in a nice, deterministically ordered, uniqued set.
2410 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2411
2412 while (!Users.empty()) {
2413 // We know that this user uses some value of From. If it is the right
2414 // value, update it.
2415 SDNode *User = Users.back();
2416 Users.pop_back();
2417
2418 for (SDOperand *Op = User->OperandList,
2419 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2420 if (*Op == From) {
2421 // Okay, we know this user needs to be updated. Remove its old self
2422 // from the CSE maps.
2423 RemoveNodeFromCSEMaps(User);
2424
2425 // Update all operands that match "From".
2426 for (; Op != E; ++Op) {
2427 if (*Op == From) {
2428 From.Val->removeUser(User);
2429 *Op = To;
2430 To.Val->addUser(User);
2431 }
2432 }
2433
2434 // Now that we have modified User, add it back to the CSE maps. If it
2435 // already exists there, recursively merge the results together.
2436 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2437 unsigned NumDeleted = Deleted.size();
2438 ReplaceAllUsesWith(User, Existing, &Deleted);
2439
2440 // User is now dead.
2441 Deleted.push_back(User);
2442 DeleteNodeNotInCSEMaps(User);
2443
2444 // We have to be careful here, because ReplaceAllUsesWith could have
2445 // deleted a user of From, which means there may be dangling pointers
2446 // in the "Users" setvector. Scan over the deleted node pointers and
2447 // remove them from the setvector.
2448 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2449 Users.remove(Deleted[i]);
2450 }
2451 break; // Exit the operand scanning loop.
2452 }
2453 }
2454 }
2455}
2456
Chris Lattner7b2880c2005-08-24 22:44:39 +00002457
Evan Chenge6f35d82006-08-01 08:20:41 +00002458/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2459/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002460unsigned SelectionDAG::AssignNodeIds() {
2461 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002462 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2463 SDNode *N = I;
2464 N->setNodeId(Id++);
2465 }
2466 return Id;
2467}
2468
Evan Chenge6f35d82006-08-01 08:20:41 +00002469/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002470/// based on their topological order. It returns the maximum id and a vector
2471/// of the SDNodes* in assigned order by reference.
2472unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002473 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002474 std::vector<unsigned> InDegree(DAGSize);
2475 std::vector<SDNode*> Sources;
2476
2477 // Use a two pass approach to avoid using a std::map which is slow.
2478 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002479 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2480 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002481 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002482 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002483 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002484 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002485 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002486 }
2487
Evan Cheng99157a02006-08-07 22:13:29 +00002488 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002489 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002490 SDNode *N = Sources.back();
2491 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002492 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002493 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2494 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002495 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002496 if (Degree == 0)
2497 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002498 }
2499 }
2500
Evan Chengc384d6c2006-08-02 22:00:34 +00002501 // Second pass, assign the actual topological order as node ids.
2502 Id = 0;
2503 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2504 TI != TE; ++TI)
2505 (*TI)->setNodeId(Id++);
2506
2507 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002508}
2509
2510
Evan Cheng091cba12006-07-27 06:39:06 +00002511
Jim Laskey58b968b2005-08-17 20:08:02 +00002512//===----------------------------------------------------------------------===//
2513// SDNode Class
2514//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002515
Chris Lattner917d2c92006-07-19 00:00:37 +00002516// Out-of-line virtual method to give class a home.
2517void SDNode::ANCHOR() {
2518}
2519
Chris Lattnera3255112005-11-08 23:30:28 +00002520/// getValueTypeList - Return a pointer to the specified value type.
2521///
2522MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2523 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2524 VTs[VT] = VT;
2525 return &VTs[VT];
2526}
Chris Lattnera5682852006-08-07 23:03:03 +00002527
Chris Lattner5c884562005-01-12 18:37:47 +00002528/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2529/// indicated value. This method ignores uses of other values defined by this
2530/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002531bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002532 assert(Value < getNumValues() && "Bad value!");
2533
2534 // If there is only one value, this is easy.
2535 if (getNumValues() == 1)
2536 return use_size() == NUses;
2537 if (Uses.size() < NUses) return false;
2538
Evan Cheng4ee62112006-02-05 06:29:23 +00002539 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002540
2541 std::set<SDNode*> UsersHandled;
2542
Evan Cheng4ee62112006-02-05 06:29:23 +00002543 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002544 UI != E; ++UI) {
2545 SDNode *User = *UI;
2546 if (User->getNumOperands() == 1 ||
2547 UsersHandled.insert(User).second) // First time we've seen this?
2548 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2549 if (User->getOperand(i) == TheValue) {
2550 if (NUses == 0)
2551 return false; // too many uses
2552 --NUses;
2553 }
2554 }
2555
2556 // Found exactly the right number of uses?
2557 return NUses == 0;
2558}
2559
2560
Evan Cheng4ee62112006-02-05 06:29:23 +00002561// isOnlyUse - Return true if this node is the only use of N.
2562bool SDNode::isOnlyUse(SDNode *N) const {
2563 bool Seen = false;
2564 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2565 SDNode *User = *I;
2566 if (User == this)
2567 Seen = true;
2568 else
2569 return false;
2570 }
2571
2572 return Seen;
2573}
2574
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002575// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002576bool SDOperand::isOperand(SDNode *N) const {
2577 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2578 if (*this == N->getOperand(i))
2579 return true;
2580 return false;
2581}
2582
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002583bool SDNode::isOperand(SDNode *N) const {
2584 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2585 if (this == N->OperandList[i].Val)
2586 return true;
2587 return false;
2588}
Evan Cheng4ee62112006-02-05 06:29:23 +00002589
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002590const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002591 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002592 default:
2593 if (getOpcode() < ISD::BUILTIN_OP_END)
2594 return "<<Unknown DAG Node>>";
2595 else {
Evan Cheng72261582005-12-20 06:22:03 +00002596 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002597 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002598 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2599 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002600
Evan Cheng72261582005-12-20 06:22:03 +00002601 TargetLowering &TLI = G->getTargetLoweringInfo();
2602 const char *Name =
2603 TLI.getTargetNodeName(getOpcode());
2604 if (Name) return Name;
2605 }
2606
2607 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002608 }
2609
Andrew Lenharth95762122005-03-31 21:24:06 +00002610 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002611 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002612 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002613 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002614 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002615 case ISD::AssertSext: return "AssertSext";
2616 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002617
2618 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002619 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002620 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002621 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002622
2623 case ISD::Constant: return "Constant";
2624 case ISD::ConstantFP: return "ConstantFP";
2625 case ISD::GlobalAddress: return "GlobalAddress";
2626 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002627 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002628 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002629 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002630 case ISD::INTRINSIC_WO_CHAIN: {
2631 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2632 return Intrinsic::getName((Intrinsic::ID)IID);
2633 }
2634 case ISD::INTRINSIC_VOID:
2635 case ISD::INTRINSIC_W_CHAIN: {
2636 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002637 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002638 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002639
Chris Lattnerb2827b02006-03-19 00:52:58 +00002640 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002641 case ISD::TargetConstant: return "TargetConstant";
2642 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002643 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2644 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002645 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002646 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002647 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002648
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002649 case ISD::CopyToReg: return "CopyToReg";
2650 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002651 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002652 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002653 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002654 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002655 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002656 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002657
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002658 // Unary operators
2659 case ISD::FABS: return "fabs";
2660 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002661 case ISD::FSQRT: return "fsqrt";
2662 case ISD::FSIN: return "fsin";
2663 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002664
2665 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002666 case ISD::ADD: return "add";
2667 case ISD::SUB: return "sub";
2668 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002669 case ISD::MULHU: return "mulhu";
2670 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002671 case ISD::SDIV: return "sdiv";
2672 case ISD::UDIV: return "udiv";
2673 case ISD::SREM: return "srem";
2674 case ISD::UREM: return "urem";
2675 case ISD::AND: return "and";
2676 case ISD::OR: return "or";
2677 case ISD::XOR: return "xor";
2678 case ISD::SHL: return "shl";
2679 case ISD::SRA: return "sra";
2680 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002681 case ISD::ROTL: return "rotl";
2682 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002683 case ISD::FADD: return "fadd";
2684 case ISD::FSUB: return "fsub";
2685 case ISD::FMUL: return "fmul";
2686 case ISD::FDIV: return "fdiv";
2687 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002688 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002689 case ISD::VADD: return "vadd";
2690 case ISD::VSUB: return "vsub";
2691 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002692 case ISD::VSDIV: return "vsdiv";
2693 case ISD::VUDIV: return "vudiv";
2694 case ISD::VAND: return "vand";
2695 case ISD::VOR: return "vor";
2696 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002697
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002698 case ISD::SETCC: return "setcc";
2699 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002700 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002701 case ISD::VSELECT: return "vselect";
2702 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2703 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2704 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002705 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002706 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2707 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2708 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2709 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2710 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002711 case ISD::ADDC: return "addc";
2712 case ISD::ADDE: return "adde";
2713 case ISD::SUBC: return "subc";
2714 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002715 case ISD::SHL_PARTS: return "shl_parts";
2716 case ISD::SRA_PARTS: return "sra_parts";
2717 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002718
Chris Lattner7f644642005-04-28 21:44:03 +00002719 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002720 case ISD::SIGN_EXTEND: return "sign_extend";
2721 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002722 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002723 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002724 case ISD::TRUNCATE: return "truncate";
2725 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002726 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002727 case ISD::FP_EXTEND: return "fp_extend";
2728
2729 case ISD::SINT_TO_FP: return "sint_to_fp";
2730 case ISD::UINT_TO_FP: return "uint_to_fp";
2731 case ISD::FP_TO_SINT: return "fp_to_sint";
2732 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002733 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002734
2735 // Control flow instructions
2736 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002737 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002738 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002739 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002740 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002741 case ISD::CALLSEQ_START: return "callseq_start";
2742 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002743
2744 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002745 case ISD::LOAD: return "load";
2746 case ISD::STORE: return "store";
2747 case ISD::VLOAD: return "vload";
2748 case ISD::EXTLOAD: return "extload";
2749 case ISD::SEXTLOAD: return "sextload";
2750 case ISD::ZEXTLOAD: return "zextload";
2751 case ISD::TRUNCSTORE: return "truncstore";
2752 case ISD::VAARG: return "vaarg";
2753 case ISD::VACOPY: return "vacopy";
2754 case ISD::VAEND: return "vaend";
2755 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002756 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002757 case ISD::EXTRACT_ELEMENT: return "extract_element";
2758 case ISD::BUILD_PAIR: return "build_pair";
2759 case ISD::STACKSAVE: return "stacksave";
2760 case ISD::STACKRESTORE: return "stackrestore";
2761
2762 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002763 case ISD::MEMSET: return "memset";
2764 case ISD::MEMCPY: return "memcpy";
2765 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002766
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002767 // Bit manipulation
2768 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002769 case ISD::CTPOP: return "ctpop";
2770 case ISD::CTTZ: return "cttz";
2771 case ISD::CTLZ: return "ctlz";
2772
Chris Lattner36ce6912005-11-29 06:21:05 +00002773 // Debug info
2774 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002775 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002776 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002777
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002778 case ISD::CONDCODE:
2779 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002780 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002781 case ISD::SETOEQ: return "setoeq";
2782 case ISD::SETOGT: return "setogt";
2783 case ISD::SETOGE: return "setoge";
2784 case ISD::SETOLT: return "setolt";
2785 case ISD::SETOLE: return "setole";
2786 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002787
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002788 case ISD::SETO: return "seto";
2789 case ISD::SETUO: return "setuo";
2790 case ISD::SETUEQ: return "setue";
2791 case ISD::SETUGT: return "setugt";
2792 case ISD::SETUGE: return "setuge";
2793 case ISD::SETULT: return "setult";
2794 case ISD::SETULE: return "setule";
2795 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002796
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002797 case ISD::SETEQ: return "seteq";
2798 case ISD::SETGT: return "setgt";
2799 case ISD::SETGE: return "setge";
2800 case ISD::SETLT: return "setlt";
2801 case ISD::SETLE: return "setle";
2802 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002803 }
2804 }
2805}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002806
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002807void SDNode::dump() const { dump(0); }
2808void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002809 std::cerr << (void*)this << ": ";
2810
2811 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2812 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002813 if (getValueType(i) == MVT::Other)
2814 std::cerr << "ch";
2815 else
2816 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002817 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002818 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002819
2820 std::cerr << " ";
2821 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2822 if (i) std::cerr << ", ";
2823 std::cerr << (void*)getOperand(i).Val;
2824 if (unsigned RN = getOperand(i).ResNo)
2825 std::cerr << ":" << RN;
2826 }
2827
2828 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2829 std::cerr << "<" << CSDN->getValue() << ">";
2830 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2831 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002832 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002833 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002834 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002835 std::cerr << "<";
2836 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002837 if (offset > 0)
2838 std::cerr << " + " << offset;
2839 else
2840 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002841 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002842 std::cerr << "<" << FIDN->getIndex() << ">";
2843 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002844 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002845 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002846 if (offset > 0)
2847 std::cerr << " + " << offset;
2848 else
2849 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002850 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002851 std::cerr << "<";
2852 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2853 if (LBB)
2854 std::cerr << LBB->getName() << " ";
2855 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002856 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002857 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002858 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2859 } else {
2860 std::cerr << " #" << R->getReg();
2861 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002862 } else if (const ExternalSymbolSDNode *ES =
2863 dyn_cast<ExternalSymbolSDNode>(this)) {
2864 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002865 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2866 if (M->getValue())
2867 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2868 else
2869 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002870 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2871 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002872 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002873}
2874
Chris Lattnerde202b32005-11-09 23:47:37 +00002875static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002876 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2877 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002878 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002879 else
2880 std::cerr << "\n" << std::string(indent+2, ' ')
2881 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002882
Chris Lattnerea946cd2005-01-09 20:38:33 +00002883
2884 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002885 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002886}
2887
Chris Lattnerc3aae252005-01-07 07:46:32 +00002888void SelectionDAG::dump() const {
2889 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002890 std::vector<const SDNode*> Nodes;
2891 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2892 I != E; ++I)
2893 Nodes.push_back(I);
2894
Chris Lattner49d24712005-01-09 20:26:36 +00002895 std::sort(Nodes.begin(), Nodes.end());
2896
2897 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002898 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002899 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002900 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002901
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002902 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002903
Chris Lattnerc3aae252005-01-07 07:46:32 +00002904 std::cerr << "\n\n";
2905}
2906