blob: 044a2fa976fbf6e52f39bbd643c142cc1db46388 [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 +000041// isInvertibleForFree - Return true if there is no cost to emitting the logical
42// inverse of this node.
43static bool isInvertibleForFree(SDOperand N) {
44 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000045 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000046 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000047 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000048}
49
Jim Laskey58b968b2005-08-17 20:08:02 +000050//===----------------------------------------------------------------------===//
51// ConstantFPSDNode Class
52//===----------------------------------------------------------------------===//
53
54/// isExactlyValue - We don't rely on operator== working on double values, as
55/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
56/// As such, this method can be used to do an exact bit-for-bit comparison of
57/// two floating point values.
58bool ConstantFPSDNode::isExactlyValue(double V) const {
59 return DoubleToBits(V) == DoubleToBits(Value);
60}
61
62//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000063// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000064//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000065
Evan Chenga8df1662006-03-27 06:58:47 +000066/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000067/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000068bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000069 // Look through a bit convert.
70 if (N->getOpcode() == ISD::BIT_CONVERT)
71 N = N->getOperand(0).Val;
72
Evan Chenga8df1662006-03-27 06:58:47 +000073 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000074
75 unsigned i = 0, e = N->getNumOperands();
76
77 // Skip over all of the undef values.
78 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
79 ++i;
80
81 // Do not accept an all-undef vector.
82 if (i == e) return false;
83
84 // Do not accept build_vectors that aren't all constants or which have non-~0
85 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000086 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000087 if (isa<ConstantSDNode>(NotZero)) {
88 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
89 return false;
90 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000091 MVT::ValueType VT = NotZero.getValueType();
92 if (VT== MVT::f64) {
93 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
94 (uint64_t)-1)
95 return false;
96 } else {
97 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
98 (uint32_t)-1)
99 return false;
100 }
Evan Chenga8df1662006-03-27 06:58:47 +0000101 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000102 return false;
103
104 // Okay, we have at least one ~0 value, check to see if the rest match or are
105 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000106 for (++i; i != e; ++i)
107 if (N->getOperand(i) != NotZero &&
108 N->getOperand(i).getOpcode() != ISD::UNDEF)
109 return false;
110 return true;
111}
112
113
Evan Cheng4a147842006-03-26 09:50:58 +0000114/// isBuildVectorAllZeros - Return true if the specified node is a
115/// BUILD_VECTOR where all of the elements are 0 or undef.
116bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000117 // Look through a bit convert.
118 if (N->getOpcode() == ISD::BIT_CONVERT)
119 N = N->getOperand(0).Val;
120
Evan Cheng4a147842006-03-26 09:50:58 +0000121 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000122
123 unsigned i = 0, e = N->getNumOperands();
124
125 // Skip over all of the undef values.
126 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
127 ++i;
128
129 // Do not accept an all-undef vector.
130 if (i == e) return false;
131
132 // Do not accept build_vectors that aren't all constants or which have non-~0
133 // elements.
134 SDOperand Zero = N->getOperand(i);
135 if (isa<ConstantSDNode>(Zero)) {
136 if (!cast<ConstantSDNode>(Zero)->isNullValue())
137 return false;
138 } else if (isa<ConstantFPSDNode>(Zero)) {
139 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
140 return false;
141 } else
142 return false;
143
144 // Okay, we have at least one ~0 value, check to see if the rest match or are
145 // undefs.
146 for (++i; i != e; ++i)
147 if (N->getOperand(i) != Zero &&
148 N->getOperand(i).getOpcode() != ISD::UNDEF)
149 return false;
150 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000151}
152
Chris Lattnerc3aae252005-01-07 07:46:32 +0000153/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
154/// when given the operation for (X op Y).
155ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
156 // To perform this operation, we just need to swap the L and G bits of the
157 // operation.
158 unsigned OldL = (Operation >> 2) & 1;
159 unsigned OldG = (Operation >> 1) & 1;
160 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
161 (OldL << 1) | // New G bit
162 (OldG << 2)); // New L bit.
163}
164
165/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
166/// 'op' is a valid SetCC operation.
167ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
168 unsigned Operation = Op;
169 if (isInteger)
170 Operation ^= 7; // Flip L, G, E bits, but not U.
171 else
172 Operation ^= 15; // Flip all of the condition bits.
173 if (Operation > ISD::SETTRUE2)
174 Operation &= ~8; // Don't let N and U bits get set.
175 return ISD::CondCode(Operation);
176}
177
178
179/// isSignedOp - For an integer comparison, return 1 if the comparison is a
180/// signed operation and 2 if the result is an unsigned comparison. Return zero
181/// if the operation does not depend on the sign of the input (setne and seteq).
182static int isSignedOp(ISD::CondCode Opcode) {
183 switch (Opcode) {
184 default: assert(0 && "Illegal integer setcc operation!");
185 case ISD::SETEQ:
186 case ISD::SETNE: return 0;
187 case ISD::SETLT:
188 case ISD::SETLE:
189 case ISD::SETGT:
190 case ISD::SETGE: return 1;
191 case ISD::SETULT:
192 case ISD::SETULE:
193 case ISD::SETUGT:
194 case ISD::SETUGE: return 2;
195 }
196}
197
198/// getSetCCOrOperation - Return the result of a logical OR between different
199/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
200/// returns SETCC_INVALID if it is not possible to represent the resultant
201/// comparison.
202ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
203 bool isInteger) {
204 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
205 // Cannot fold a signed integer setcc with an unsigned integer setcc.
206 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000207
Chris Lattnerc3aae252005-01-07 07:46:32 +0000208 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000209
Chris Lattnerc3aae252005-01-07 07:46:32 +0000210 // If the N and U bits get set then the resultant comparison DOES suddenly
211 // care about orderedness, and is true when ordered.
212 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000213 Op &= ~16; // Clear the U bit if the N bit is set.
214
215 // Canonicalize illegal integer setcc's.
216 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
217 Op = ISD::SETNE;
218
Chris Lattnerc3aae252005-01-07 07:46:32 +0000219 return ISD::CondCode(Op);
220}
221
222/// getSetCCAndOperation - Return the result of a logical AND between different
223/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
224/// function returns zero if it is not possible to represent the resultant
225/// comparison.
226ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
227 bool isInteger) {
228 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
229 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000230 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000231
232 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000233 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
234
235 // Canonicalize illegal integer setcc's.
236 if (isInteger) {
237 switch (Result) {
238 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000239 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
240 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
241 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
242 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000243 }
244 }
245
246 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000247}
248
Chris Lattnerb48da392005-01-23 04:39:44 +0000249const TargetMachine &SelectionDAG::getTarget() const {
250 return TLI.getTargetMachine();
251}
252
Jim Laskey58b968b2005-08-17 20:08:02 +0000253//===----------------------------------------------------------------------===//
254// SelectionDAG Class
255//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000256
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000257/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000258/// SelectionDAG.
259void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000260 // Create a dummy node (which is not added to allnodes), that adds a reference
261 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000262 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000263
Chris Lattner190a4182006-08-04 17:45:20 +0000264 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000265
Chris Lattner190a4182006-08-04 17:45:20 +0000266 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000267 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000268 if (I->use_empty())
269 DeadNodes.push_back(I);
270
271 // Process the worklist, deleting the nodes and adding their uses to the
272 // worklist.
273 while (!DeadNodes.empty()) {
274 SDNode *N = DeadNodes.back();
275 DeadNodes.pop_back();
276
277 // Take the node out of the appropriate CSE map.
278 RemoveNodeFromCSEMaps(N);
279
280 // Next, brutally remove the operand list. This is safe to do, as there are
281 // no cycles in the graph.
282 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
283 SDNode *Operand = I->Val;
284 Operand->removeUser(N);
285
286 // Now that we removed this operand, see if there are no uses of it left.
287 if (Operand->use_empty())
288 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000289 }
Chris Lattner190a4182006-08-04 17:45:20 +0000290 delete[] N->OperandList;
291 N->OperandList = 0;
292 N->NumOperands = 0;
293
294 // Finally, remove N itself.
295 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000296 }
297
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000298 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000299 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000300}
301
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000302void SelectionDAG::DeleteNode(SDNode *N) {
303 assert(N->use_empty() && "Cannot delete a node that is not dead!");
304
305 // First take this out of the appropriate CSE map.
306 RemoveNodeFromCSEMaps(N);
307
Chris Lattner1e111c72005-09-07 05:37:01 +0000308 // Finally, remove uses due to operands of this node, remove from the
309 // AllNodes list, and delete the node.
310 DeleteNodeNotInCSEMaps(N);
311}
312
313void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
314
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000315 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000316 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000317
318 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000319 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
320 I->Val->removeUser(N);
321 delete[] N->OperandList;
322 N->OperandList = 0;
323 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000324
325 delete N;
326}
327
Chris Lattner149c58c2005-08-16 18:17:10 +0000328/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
329/// correspond to it. This is useful when we're about to delete or repurpose
330/// the node. We don't want future request for structurally identical nodes
331/// to return N anymore.
332void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000333 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000334 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000335 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000336 case ISD::STRING:
337 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
338 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000339 case ISD::CONDCODE:
340 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
341 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000342 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000343 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
344 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000345 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000347 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000348 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000349 Erased =
350 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000351 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000352 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000353 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000354 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
355 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000356 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000357 // Remove it from the CSE Map.
358 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000359 break;
360 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000361#ifndef NDEBUG
362 // Verify that the node was actually in one of the CSE maps, unless it has a
363 // flag result (which cannot be CSE'd) or is one of the special cases that are
364 // not subject to CSE.
365 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000366 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000367 N->dump();
Evan Cheng99157a02006-08-07 22:13:29 +0000368 std::cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369 assert(0 && "Node is not in map!");
370 }
371#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000372}
373
Chris Lattner8b8749f2005-08-17 19:00:20 +0000374/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
375/// has been taken out and modified in some way. If the specified node already
376/// exists in the CSE maps, do not modify the maps, but return the existing node
377/// instead. If it doesn't exist, add it and return null.
378///
379SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
380 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000381 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000382 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000383
Chris Lattner9f8cc692005-12-19 22:21:21 +0000384 // Check that remaining values produced are not flags.
385 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
386 if (N->getValueType(i) == MVT::Flag)
387 return 0; // Never CSE anything that produces a flag.
388
Chris Lattnera5682852006-08-07 23:03:03 +0000389 SDNode *New = CSEMap.GetOrInsertNode(N);
390 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000391 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000392}
393
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000394/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
395/// were replaced with those specified. If this node is never memoized,
396/// return null, otherwise return a pointer to the slot it would take. If a
397/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000398SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
399 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000400 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000401 return 0; // Never add these nodes.
402
403 // Check that remaining values produced are not flags.
404 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
405 if (N->getValueType(i) == MVT::Flag)
406 return 0; // Never CSE anything that produces a flag.
407
Chris Lattnera5682852006-08-07 23:03:03 +0000408 SelectionDAGCSEMap::NodeID ID;
409 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000410 ID.SetValueTypes(N->getVTList());
Chris Lattnera5682852006-08-07 23:03:03 +0000411 ID.SetOperands(Op);
412 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000413}
414
415/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
416/// were replaced with those specified. If this node is never memoized,
417/// return null, otherwise return a pointer to the slot it would take. If a
418/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000419SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
420 SDOperand Op1, SDOperand Op2,
421 void *&InsertPos) {
422 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
423 return 0; // Never add these nodes.
424
425 // Check that remaining values produced are not flags.
426 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
427 if (N->getValueType(i) == MVT::Flag)
428 return 0; // Never CSE anything that produces a flag.
429
430 SelectionDAGCSEMap::NodeID ID;
431 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000432 ID.SetValueTypes(N->getVTList());
Chris Lattnera5682852006-08-07 23:03:03 +0000433 ID.SetOperands(Op1, Op2);
434 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
435}
436
437
438/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
439/// were replaced with those specified. If this node is never memoized,
440/// return null, otherwise return a pointer to the slot it would take. If a
441/// node already exists with these operands, the slot will be non-null.
442SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000443 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000444 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000445 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000446 return 0; // Never add these nodes.
447
448 // Check that remaining values produced are not flags.
449 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
450 if (N->getValueType(i) == MVT::Flag)
451 return 0; // Never CSE anything that produces a flag.
452
Chris Lattnera5682852006-08-07 23:03:03 +0000453 SelectionDAGCSEMap::NodeID ID;
454 ID.SetOpcode(N->getOpcode());
Chris Lattner0b3e5252006-08-15 19:11:05 +0000455 ID.SetValueTypes(N->getVTList());
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000456 ID.SetOperands(Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +0000457 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000458}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000459
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000460
Chris Lattner78ec3112003-08-11 14:57:33 +0000461SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000462 while (!AllNodes.empty()) {
463 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000464 N->SetNextInBucket(0);
Chris Lattner65113b22005-11-08 22:07:03 +0000465 delete [] N->OperandList;
466 N->OperandList = 0;
467 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000468 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000469 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000470}
471
Chris Lattner0f2287b2005-04-13 02:38:18 +0000472SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000473 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000474 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000475 return getNode(ISD::AND, Op.getValueType(), Op,
476 getConstant(Imm, Op.getValueType()));
477}
478
Chris Lattner36ce6912005-11-29 06:21:05 +0000479SDOperand SelectionDAG::getString(const std::string &Val) {
480 StringSDNode *&N = StringNodes[Val];
481 if (!N) {
482 N = new StringSDNode(Val);
483 AllNodes.push_back(N);
484 }
485 return SDOperand(N, 0);
486}
487
Chris Lattner61b09412006-08-11 21:01:22 +0000488SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000489 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000490 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000491
Chris Lattner61b09412006-08-11 21:01:22 +0000492 // Mask out any bits that are not valid for this constant.
493 Val &= MVT::getIntVTBitMask(VT);
494
495 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000496 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000497 ID.AddInteger(Val);
498 void *IP = 0;
499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
500 return SDOperand(E, 0);
501 SDNode *N = new ConstantSDNode(isT, Val, VT);
502 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000503 AllNodes.push_back(N);
504 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000505}
506
Chris Lattner61b09412006-08-11 21:01:22 +0000507
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000508SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
509 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000510 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
511 if (VT == MVT::f32)
512 Val = (float)Val; // Mask out extra precision.
513
Chris Lattnerd8658612005-02-17 20:17:32 +0000514 // Do the map lookup using the actual bit pattern for the floating point
515 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
516 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000517 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000518 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000519 ID.AddInteger(DoubleToBits(Val));
520 void *IP = 0;
521 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
522 return SDOperand(E, 0);
523 SDNode *N = new ConstantFPSDNode(isTarget, Val, VT);
524 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000525 AllNodes.push_back(N);
526 return SDOperand(N, 0);
527}
528
Chris Lattnerc3aae252005-01-07 07:46:32 +0000529SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000530 MVT::ValueType VT, int Offset,
531 bool isTargetGA) {
532 unsigned Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000533 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000534 ID.AddPointer(GV);
535 ID.AddInteger(Offset);
536 void *IP = 0;
537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
538 return SDOperand(E, 0);
539 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
540 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000541 AllNodes.push_back(N);
542 return SDOperand(N, 0);
543}
544
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000545SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
546 bool isTarget) {
547 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000548 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000549 ID.AddInteger(FI);
550 void *IP = 0;
551 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
552 return SDOperand(E, 0);
553 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
554 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000555 AllNodes.push_back(N);
556 return SDOperand(N, 0);
557}
558
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000559SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
560 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000561 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000562 ID.AddInteger(JTI);
563 void *IP = 0;
564 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
565 return SDOperand(E, 0);
566 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
567 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000568 AllNodes.push_back(N);
569 return SDOperand(N, 0);
570}
571
Evan Chengb8973bd2006-01-31 22:23:14 +0000572SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000573 unsigned Alignment, int Offset,
574 bool isTarget) {
575 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Chris Lattner0b3e5252006-08-15 19:11:05 +0000576 SelectionDAGCSEMap::NodeID ID(Opc, getVTList(VT));
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000577 ID.AddInteger(Alignment);
578 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000579 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000580 void *IP = 0;
581 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
582 return SDOperand(E, 0);
583 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
584 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000585 AllNodes.push_back(N);
586 return SDOperand(N, 0);
587}
588
Chris Lattnerc3aae252005-01-07 07:46:32 +0000589
590SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000591 SelectionDAGCSEMap::NodeID ID(ISD::BasicBlock, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000592 ID.AddPointer(MBB);
593 void *IP = 0;
594 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
595 return SDOperand(E, 0);
596 SDNode *N = new BasicBlockSDNode(MBB);
597 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000598 AllNodes.push_back(N);
599 return SDOperand(N, 0);
600}
601
Chris Lattner15e4b012005-07-10 00:07:11 +0000602SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
603 if ((unsigned)VT >= ValueTypeNodes.size())
604 ValueTypeNodes.resize(VT+1);
605 if (ValueTypeNodes[VT] == 0) {
606 ValueTypeNodes[VT] = new VTSDNode(VT);
607 AllNodes.push_back(ValueTypeNodes[VT]);
608 }
609
610 return SDOperand(ValueTypeNodes[VT], 0);
611}
612
Chris Lattnerc3aae252005-01-07 07:46:32 +0000613SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
614 SDNode *&N = ExternalSymbols[Sym];
615 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000616 N = new ExternalSymbolSDNode(false, Sym, VT);
617 AllNodes.push_back(N);
618 return SDOperand(N, 0);
619}
620
Chris Lattner809ec112006-01-28 10:09:25 +0000621SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
622 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000623 SDNode *&N = TargetExternalSymbols[Sym];
624 if (N) return SDOperand(N, 0);
625 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000626 AllNodes.push_back(N);
627 return SDOperand(N, 0);
628}
629
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000630SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
631 if ((unsigned)Cond >= CondCodeNodes.size())
632 CondCodeNodes.resize(Cond+1);
633
Chris Lattner079a27a2005-08-09 20:40:02 +0000634 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000635 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000636 AllNodes.push_back(CondCodeNodes[Cond]);
637 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000638 return SDOperand(CondCodeNodes[Cond], 0);
639}
640
Chris Lattner0fdd7682005-08-30 22:38:38 +0000641SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000642 SelectionDAGCSEMap::NodeID ID(ISD::Register, getVTList(VT));
Chris Lattner61b09412006-08-11 21:01:22 +0000643 ID.AddInteger(RegNo);
644 void *IP = 0;
645 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
646 return SDOperand(E, 0);
647 SDNode *N = new RegisterSDNode(RegNo, VT);
648 CSEMap.InsertNode(N, IP);
649 AllNodes.push_back(N);
650 return SDOperand(N, 0);
651}
652
653SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
654 assert((!V || isa<PointerType>(V->getType())) &&
655 "SrcValue is not a pointer?");
656
Chris Lattner0b3e5252006-08-15 19:11:05 +0000657 SelectionDAGCSEMap::NodeID ID(ISD::SRCVALUE, getVTList(MVT::Other));
Chris Lattner61b09412006-08-11 21:01:22 +0000658 ID.AddPointer(V);
659 ID.AddInteger(Offset);
660 void *IP = 0;
661 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
662 return SDOperand(E, 0);
663 SDNode *N = new SrcValueSDNode(V, Offset);
664 CSEMap.InsertNode(N, IP);
665 AllNodes.push_back(N);
666 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000667}
668
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000669SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
670 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000671 // These setcc operations always fold.
672 switch (Cond) {
673 default: break;
674 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000675 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000676 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000677 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000678
679 case ISD::SETOEQ:
680 case ISD::SETOGT:
681 case ISD::SETOGE:
682 case ISD::SETOLT:
683 case ISD::SETOLE:
684 case ISD::SETONE:
685 case ISD::SETO:
686 case ISD::SETUO:
687 case ISD::SETUEQ:
688 case ISD::SETUNE:
689 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
690 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000691 }
692
Chris Lattner67255a12005-04-07 18:14:58 +0000693 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
694 uint64_t C2 = N2C->getValue();
695 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
696 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000697
Chris Lattnerc3aae252005-01-07 07:46:32 +0000698 // Sign extend the operands if required
699 if (ISD::isSignedIntSetCC(Cond)) {
700 C1 = N1C->getSignExtended();
701 C2 = N2C->getSignExtended();
702 }
703
704 switch (Cond) {
705 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000706 case ISD::SETEQ: return getConstant(C1 == C2, VT);
707 case ISD::SETNE: return getConstant(C1 != C2, VT);
708 case ISD::SETULT: return getConstant(C1 < C2, VT);
709 case ISD::SETUGT: return getConstant(C1 > C2, VT);
710 case ISD::SETULE: return getConstant(C1 <= C2, VT);
711 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
712 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
713 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
714 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
715 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000716 }
Chris Lattner24673922005-04-07 18:58:54 +0000717 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000718 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000719 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
720 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
721
722 // If the comparison constant has bits in the upper part, the
723 // zero-extended value could never match.
724 if (C2 & (~0ULL << InSize)) {
725 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
726 switch (Cond) {
727 case ISD::SETUGT:
728 case ISD::SETUGE:
729 case ISD::SETEQ: return getConstant(0, VT);
730 case ISD::SETULT:
731 case ISD::SETULE:
732 case ISD::SETNE: return getConstant(1, VT);
733 case ISD::SETGT:
734 case ISD::SETGE:
735 // True if the sign bit of C2 is set.
736 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
737 case ISD::SETLT:
738 case ISD::SETLE:
739 // True if the sign bit of C2 isn't set.
740 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
741 default:
742 break;
743 }
744 }
745
746 // Otherwise, we can perform the comparison with the low bits.
747 switch (Cond) {
748 case ISD::SETEQ:
749 case ISD::SETNE:
750 case ISD::SETUGT:
751 case ISD::SETUGE:
752 case ISD::SETULT:
753 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000754 return getSetCC(VT, N1.getOperand(0),
755 getConstant(C2, N1.getOperand(0).getValueType()),
756 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000757 default:
758 break; // todo, be more careful with signed comparisons
759 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000760 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
761 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
762 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
763 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
764 MVT::ValueType ExtDstTy = N1.getValueType();
765 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000766
Chris Lattner7b2880c2005-08-24 22:44:39 +0000767 // If the extended part has any inconsistent bits, it cannot ever
768 // compare equal. In other words, they have to be all ones or all
769 // zeros.
770 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000771 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000772 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
773 return getConstant(Cond == ISD::SETNE, VT);
774
775 // Otherwise, make this a use of a zext.
776 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000777 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000778 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000779 }
780
Chris Lattner67255a12005-04-07 18:14:58 +0000781 uint64_t MinVal, MaxVal;
782 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
783 if (ISD::isSignedIntSetCC(Cond)) {
784 MinVal = 1ULL << (OperandBitSize-1);
785 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
786 MaxVal = ~0ULL >> (65-OperandBitSize);
787 else
788 MaxVal = 0;
789 } else {
790 MinVal = 0;
791 MaxVal = ~0ULL >> (64-OperandBitSize);
792 }
793
794 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
795 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
796 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
797 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000798 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
799 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000800 }
801
802 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
803 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
804 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000805 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
806 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000807 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000808
Nate Begeman72ea2812005-04-14 08:56:52 +0000809 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
810 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000811
Nate Begeman72ea2812005-04-14 08:56:52 +0000812 // Canonicalize setgt X, Min --> setne X, Min
813 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000814 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000815
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000816 // If we have setult X, 1, turn it into seteq X, 0
817 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000818 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
819 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000820 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000821 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000822 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
823 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000824
825 // If we have "setcc X, C1", check to see if we can shrink the immediate
826 // by changing cc.
827
828 // SETUGT X, SINTMAX -> SETLT X, 0
829 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
830 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000831 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000832
833 // FIXME: Implement the rest of these.
834
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000835
836 // Fold bit comparisons when we can.
837 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
838 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
839 if (ConstantSDNode *AndRHS =
840 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
841 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
842 // Perform the xform if the AND RHS is a single bit.
843 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
844 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000845 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000846 TLI.getShiftAmountTy()));
847 }
848 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
849 // (X & 8) == 8 --> (X & 8) >> 3
850 // Perform the xform if C2 is a single bit.
851 if ((C2 & (C2-1)) == 0) {
852 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000853 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000854 }
855 }
856 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000857 }
Chris Lattner67255a12005-04-07 18:14:58 +0000858 } else if (isa<ConstantSDNode>(N1.Val)) {
859 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000860 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000861 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000862
863 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
864 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
865 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000866
Chris Lattnerc3aae252005-01-07 07:46:32 +0000867 switch (Cond) {
868 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000869 case ISD::SETEQ: return getConstant(C1 == C2, VT);
870 case ISD::SETNE: return getConstant(C1 != C2, VT);
871 case ISD::SETLT: return getConstant(C1 < C2, VT);
872 case ISD::SETGT: return getConstant(C1 > C2, VT);
873 case ISD::SETLE: return getConstant(C1 <= C2, VT);
874 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000875 }
876 } else {
877 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000878 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000879 }
880
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000881 // Could not fold it.
882 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000883}
884
Chris Lattnerc3aae252005-01-07 07:46:32 +0000885/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000886///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000887SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +0000888 SelectionDAGCSEMap::NodeID ID(Opcode, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +0000889 void *IP = 0;
890 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
891 return SDOperand(E, 0);
892 SDNode *N = new SDNode(Opcode, VT);
893 CSEMap.InsertNode(N, IP);
894
895 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000896 return SDOperand(N, 0);
897}
898
Chris Lattnerc3aae252005-01-07 07:46:32 +0000899SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
900 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000901 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000902 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
904 uint64_t Val = C->getValue();
905 switch (Opcode) {
906 default: break;
907 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000908 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000909 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
910 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000911 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
912 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000913 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000914 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000915 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000916 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000917 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000918 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000919 case ISD::BSWAP:
920 switch(VT) {
921 default: assert(0 && "Invalid bswap!"); break;
922 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
923 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
924 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
925 }
926 break;
927 case ISD::CTPOP:
928 switch(VT) {
929 default: assert(0 && "Invalid ctpop!"); break;
930 case MVT::i1: return getConstant(Val != 0, VT);
931 case MVT::i8:
932 Tmp1 = (unsigned)Val & 0xFF;
933 return getConstant(CountPopulation_32(Tmp1), VT);
934 case MVT::i16:
935 Tmp1 = (unsigned)Val & 0xFFFF;
936 return getConstant(CountPopulation_32(Tmp1), VT);
937 case MVT::i32:
938 return getConstant(CountPopulation_32((unsigned)Val), VT);
939 case MVT::i64:
940 return getConstant(CountPopulation_64(Val), VT);
941 }
942 case ISD::CTLZ:
943 switch(VT) {
944 default: assert(0 && "Invalid ctlz!"); break;
945 case MVT::i1: return getConstant(Val == 0, VT);
946 case MVT::i8:
947 Tmp1 = (unsigned)Val & 0xFF;
948 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
949 case MVT::i16:
950 Tmp1 = (unsigned)Val & 0xFFFF;
951 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
952 case MVT::i32:
953 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
954 case MVT::i64:
955 return getConstant(CountLeadingZeros_64(Val), VT);
956 }
957 case ISD::CTTZ:
958 switch(VT) {
959 default: assert(0 && "Invalid cttz!"); break;
960 case MVT::i1: return getConstant(Val == 0, VT);
961 case MVT::i8:
962 Tmp1 = (unsigned)Val | 0x100;
963 return getConstant(CountTrailingZeros_32(Tmp1), VT);
964 case MVT::i16:
965 Tmp1 = (unsigned)Val | 0x10000;
966 return getConstant(CountTrailingZeros_32(Tmp1), VT);
967 case MVT::i32:
968 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
969 case MVT::i64:
970 return getConstant(CountTrailingZeros_64(Val), VT);
971 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000972 }
973 }
974
Chris Lattner94683772005-12-23 05:30:37 +0000975 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000976 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
977 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000978 case ISD::FNEG:
979 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000980 case ISD::FABS:
981 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000982 case ISD::FP_ROUND:
983 case ISD::FP_EXTEND:
984 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000985 case ISD::FP_TO_SINT:
986 return getConstant((int64_t)C->getValue(), VT);
987 case ISD::FP_TO_UINT:
988 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000989 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000990 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +0000991 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000992 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +0000993 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000994 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000995 }
996
997 unsigned OpOpcode = Operand.Val->getOpcode();
998 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000999 case ISD::TokenFactor:
1000 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001001 case ISD::SIGN_EXTEND:
1002 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001003 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1005 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1006 break;
1007 case ISD::ZERO_EXTEND:
1008 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001009 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001010 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001011 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001012 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001013 case ISD::ANY_EXTEND:
1014 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001015 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001016 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1017 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1018 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1019 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001020 case ISD::TRUNCATE:
1021 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001022 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023 if (OpOpcode == ISD::TRUNCATE)
1024 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001025 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1026 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001027 // If the source is smaller than the dest, we still need an extend.
1028 if (Operand.Val->getOperand(0).getValueType() < VT)
1029 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1030 else if (Operand.Val->getOperand(0).getValueType() > VT)
1031 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1032 else
1033 return Operand.Val->getOperand(0);
1034 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001035 break;
Chris Lattner35481892005-12-23 00:16:34 +00001036 case ISD::BIT_CONVERT:
1037 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001038 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001039 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001040 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001041 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1042 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001043 if (OpOpcode == ISD::UNDEF)
1044 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001045 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001046 case ISD::SCALAR_TO_VECTOR:
1047 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1048 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1049 "Illegal SCALAR_TO_VECTOR node!");
1050 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001051 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001052 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1053 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001054 Operand.Val->getOperand(0));
1055 if (OpOpcode == ISD::FNEG) // --X -> X
1056 return Operand.Val->getOperand(0);
1057 break;
1058 case ISD::FABS:
1059 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1060 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1061 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062 }
1063
Chris Lattner43247a12005-08-25 19:12:10 +00001064 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001065 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001066 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Chris Lattnera5682852006-08-07 23:03:03 +00001067 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Operand);
1068 void *IP = 0;
1069 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1070 return SDOperand(E, 0);
1071 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001072 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001073 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001074 } else {
1075 N = new SDNode(Opcode, Operand);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001076 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001077 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078 AllNodes.push_back(N);
1079 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001080}
1081
Chris Lattner36019aa2005-04-18 03:48:41 +00001082
1083
Chris Lattnerc3aae252005-01-07 07:46:32 +00001084SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1085 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001086#ifndef NDEBUG
1087 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001088 case ISD::TokenFactor:
1089 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1090 N2.getValueType() == MVT::Other && "Invalid token factor!");
1091 break;
Chris Lattner76365122005-01-16 02:23:22 +00001092 case ISD::AND:
1093 case ISD::OR:
1094 case ISD::XOR:
1095 case ISD::UDIV:
1096 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001097 case ISD::MULHU:
1098 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001099 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1100 // fall through
1101 case ISD::ADD:
1102 case ISD::SUB:
1103 case ISD::MUL:
1104 case ISD::SDIV:
1105 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001106 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1107 // fall through.
1108 case ISD::FADD:
1109 case ISD::FSUB:
1110 case ISD::FMUL:
1111 case ISD::FDIV:
1112 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001113 assert(N1.getValueType() == N2.getValueType() &&
1114 N1.getValueType() == VT && "Binary operator types must match!");
1115 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001116 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1117 assert(N1.getValueType() == VT &&
1118 MVT::isFloatingPoint(N1.getValueType()) &&
1119 MVT::isFloatingPoint(N2.getValueType()) &&
1120 "Invalid FCOPYSIGN!");
1121 break;
Chris Lattner76365122005-01-16 02:23:22 +00001122 case ISD::SHL:
1123 case ISD::SRA:
1124 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001125 case ISD::ROTL:
1126 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001127 assert(VT == N1.getValueType() &&
1128 "Shift operators return type must be the same as their first arg");
1129 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001130 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001131 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001132 case ISD::FP_ROUND_INREG: {
1133 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1134 assert(VT == N1.getValueType() && "Not an inreg round!");
1135 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1136 "Cannot FP_ROUND_INREG integer types");
1137 assert(EVT <= VT && "Not rounding down!");
1138 break;
1139 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001140 case ISD::AssertSext:
1141 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001142 case ISD::SIGN_EXTEND_INREG: {
1143 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1144 assert(VT == N1.getValueType() && "Not an inreg extend!");
1145 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1146 "Cannot *_EXTEND_INREG FP types");
1147 assert(EVT <= VT && "Not extending!");
1148 }
1149
Chris Lattner76365122005-01-16 02:23:22 +00001150 default: break;
1151 }
1152#endif
1153
Chris Lattnerc3aae252005-01-07 07:46:32 +00001154 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1155 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1156 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001157 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1158 int64_t Val = N1C->getValue();
1159 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1160 Val <<= 64-FromBits;
1161 Val >>= 64-FromBits;
1162 return getConstant(Val, VT);
1163 }
1164
Chris Lattnerc3aae252005-01-07 07:46:32 +00001165 if (N2C) {
1166 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1167 switch (Opcode) {
1168 case ISD::ADD: return getConstant(C1 + C2, VT);
1169 case ISD::SUB: return getConstant(C1 - C2, VT);
1170 case ISD::MUL: return getConstant(C1 * C2, VT);
1171 case ISD::UDIV:
1172 if (C2) return getConstant(C1 / C2, VT);
1173 break;
1174 case ISD::UREM :
1175 if (C2) return getConstant(C1 % C2, VT);
1176 break;
1177 case ISD::SDIV :
1178 if (C2) return getConstant(N1C->getSignExtended() /
1179 N2C->getSignExtended(), VT);
1180 break;
1181 case ISD::SREM :
1182 if (C2) return getConstant(N1C->getSignExtended() %
1183 N2C->getSignExtended(), VT);
1184 break;
1185 case ISD::AND : return getConstant(C1 & C2, VT);
1186 case ISD::OR : return getConstant(C1 | C2, VT);
1187 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001188 case ISD::SHL : return getConstant(C1 << C2, VT);
1189 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001190 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001191 case ISD::ROTL :
1192 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1193 VT);
1194 case ISD::ROTR :
1195 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1196 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001197 default: break;
1198 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001199 } else { // Cannonicalize constant to RHS if commutative
1200 if (isCommutativeBinOp(Opcode)) {
1201 std::swap(N1C, N2C);
1202 std::swap(N1, N2);
1203 }
1204 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001205 }
1206
1207 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1208 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001209 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001210 if (N2CFP) {
1211 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1212 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001213 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1214 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1215 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1216 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001217 if (C2) return getConstantFP(C1 / C2, VT);
1218 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001219 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 if (C2) return getConstantFP(fmod(C1, C2), VT);
1221 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001222 case ISD::FCOPYSIGN: {
1223 union {
1224 double F;
1225 uint64_t I;
1226 } u1;
1227 union {
1228 double F;
1229 int64_t I;
1230 } u2;
1231 u1.F = C1;
1232 u2.F = C2;
1233 if (u2.I < 0) // Sign bit of RHS set?
1234 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1235 else
1236 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1237 return getConstantFP(u1.F, VT);
1238 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001239 default: break;
1240 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001241 } else { // Cannonicalize constant to RHS if commutative
1242 if (isCommutativeBinOp(Opcode)) {
1243 std::swap(N1CFP, N2CFP);
1244 std::swap(N1, N2);
1245 }
1246 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001247 }
Chris Lattner62b57722006-04-20 05:39:12 +00001248
1249 // Canonicalize an UNDEF to the RHS, even over a constant.
1250 if (N1.getOpcode() == ISD::UNDEF) {
1251 if (isCommutativeBinOp(Opcode)) {
1252 std::swap(N1, N2);
1253 } else {
1254 switch (Opcode) {
1255 case ISD::FP_ROUND_INREG:
1256 case ISD::SIGN_EXTEND_INREG:
1257 case ISD::SUB:
1258 case ISD::FSUB:
1259 case ISD::FDIV:
1260 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001261 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001262 return N1; // fold op(undef, arg2) -> undef
1263 case ISD::UDIV:
1264 case ISD::SDIV:
1265 case ISD::UREM:
1266 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001267 case ISD::SRL:
1268 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001269 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1270 }
1271 }
1272 }
1273
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001274 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001275 if (N2.getOpcode() == ISD::UNDEF) {
1276 switch (Opcode) {
1277 case ISD::ADD:
1278 case ISD::SUB:
1279 case ISD::FADD:
1280 case ISD::FSUB:
1281 case ISD::FMUL:
1282 case ISD::FDIV:
1283 case ISD::FREM:
1284 case ISD::UDIV:
1285 case ISD::SDIV:
1286 case ISD::UREM:
1287 case ISD::SREM:
1288 case ISD::XOR:
1289 return N2; // fold op(arg1, undef) -> undef
1290 case ISD::MUL:
1291 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001292 case ISD::SRL:
1293 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001294 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1295 case ISD::OR:
1296 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001297 case ISD::SRA:
1298 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001299 }
1300 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001301
Chris Lattnerc3aae252005-01-07 07:46:32 +00001302 // Finally, fold operations that do not require constants.
1303 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001304 case ISD::FP_ROUND_INREG:
1305 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1306 break;
1307 case ISD::SIGN_EXTEND_INREG: {
1308 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1309 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001310 break;
1311 }
1312
Nate Begemaneea805e2005-04-13 21:23:31 +00001313 // FIXME: figure out how to safely handle things like
1314 // int foo(int x) { return 1 << (x & 255); }
1315 // int bar() { return foo(256); }
1316#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001317 case ISD::SHL:
1318 case ISD::SRL:
1319 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001320 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001321 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001322 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001323 else if (N2.getOpcode() == ISD::AND)
1324 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1325 // If the and is only masking out bits that cannot effect the shift,
1326 // eliminate the and.
1327 unsigned NumBits = MVT::getSizeInBits(VT);
1328 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1329 return getNode(Opcode, VT, N1, N2.getOperand(0));
1330 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001331 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001332#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001333 }
1334
Chris Lattner27e9b412005-05-11 18:57:39 +00001335 // Memoize this node if possible.
1336 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001337 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00001338 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001339 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2);
1340 void *IP = 0;
1341 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1342 return SDOperand(E, 0);
1343 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001344 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001345 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00001346 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001347 N = new SDNode(Opcode, N1, N2);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001348 N->setValueTypes(VTs);
Chris Lattner27e9b412005-05-11 18:57:39 +00001349 }
1350
Chris Lattnerc3aae252005-01-07 07:46:32 +00001351 AllNodes.push_back(N);
1352 return SDOperand(N, 0);
1353}
1354
Chris Lattnerc3aae252005-01-07 07:46:32 +00001355SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1356 SDOperand N1, SDOperand N2, SDOperand N3) {
1357 // Perform various simplifications.
1358 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1359 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001360 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001362 case ISD::SETCC: {
1363 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001364 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001365 if (Simp.Val) return Simp;
1366 break;
1367 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001368 case ISD::SELECT:
1369 if (N1C)
1370 if (N1C->getValue())
1371 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001372 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001373 return N3; // select false, X, Y -> Y
1374
1375 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001376 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001377 case ISD::BRCOND:
1378 if (N2C)
1379 if (N2C->getValue()) // Unconditional branch
1380 return getNode(ISD::BR, MVT::Other, N1, N3);
1381 else
1382 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001383 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001384 case ISD::VECTOR_SHUFFLE:
1385 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1386 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1387 N3.getOpcode() == ISD::BUILD_VECTOR &&
1388 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1389 "Illegal VECTOR_SHUFFLE node!");
1390 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001391 }
1392
Chris Lattner43247a12005-08-25 19:12:10 +00001393 // Memoize node if it doesn't produce a flag.
1394 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001395 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001396 if (VT != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001397 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, N1, N2, N3);
1398 void *IP = 0;
1399 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1400 return SDOperand(E, 0);
1401 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001402 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001403 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001404 } else {
1405 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001406 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001407 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001408 AllNodes.push_back(N);
1409 return SDOperand(N, 0);
1410}
1411
1412SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001413 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001414 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001415 SDOperand Ops[] = { N1, N2, N3, N4 };
1416 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001417}
1418
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001419SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1420 SDOperand N1, SDOperand N2, SDOperand N3,
1421 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001422 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
1423 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001424}
1425
Evan Cheng7038daf2005-12-10 00:37:58 +00001426SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1427 SDOperand Chain, SDOperand Ptr,
1428 SDOperand SV) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001429 SDVTList VTs = getVTList(VT, MVT::Other);
Chris Lattnera5682852006-08-07 23:03:03 +00001430
1431 SelectionDAGCSEMap::NodeID ID(ISD::LOAD, VTs, Chain, Ptr, SV);
1432 void *IP = 0;
1433 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1434 return SDOperand(E, 0);
1435 SDNode *N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001436 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001437 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00001438 AllNodes.push_back(N);
1439 return SDOperand(N, 0);
1440}
1441
1442SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1443 SDOperand Chain, SDOperand Ptr,
1444 SDOperand SV) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001445 SDOperand Ops[] = { Chain, Ptr, SV, getConstant(Count, MVT::i32),
1446 getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001447 return getNode(ISD::VLOAD, getVTList(MVT::Vector, MVT::Other), Ops, 5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001448}
1449
1450SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1451 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1452 MVT::ValueType EVT) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001453 SDOperand Ops[] = { Chain, Ptr, SV, getValueType(EVT) };
Chris Lattnerbe384162006-08-16 22:57:46 +00001454 return getNode(Opcode, getVTList(VT, MVT::Other), Ops, 4);
Evan Cheng7038daf2005-12-10 00:37:58 +00001455}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001456
Nate Begemanacc398c2006-01-25 18:21:52 +00001457SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1458 SDOperand Chain, SDOperand Ptr,
1459 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001460 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00001461 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00001462}
1463
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001464SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001465 const SDOperand *Ops, unsigned NumOps) {
1466 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001467 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001468 case 1: return getNode(Opcode, VT, Ops[0]);
1469 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1470 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001471 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001472 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001473
Chris Lattneref847df2005-04-09 03:27:28 +00001474 switch (Opcode) {
1475 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001476 case ISD::TRUNCSTORE: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001477 assert(NumOps == 5 && "TRUNCSTORE takes 5 operands!");
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001478 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1479#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1480 // If this is a truncating store of a constant, convert to the desired type
1481 // and store it instead.
1482 if (isa<Constant>(Ops[0])) {
1483 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1484 if (isa<Constant>(Op))
1485 N1 = Op;
1486 }
1487 // Also for ConstantFP?
1488#endif
1489 if (Ops[0].getValueType() == EVT) // Normal store?
1490 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1491 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1492 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1493 "Can't do FP-INT conversion!");
1494 break;
1495 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001496 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001497 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001498 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1499 "LHS and RHS of condition must have same type!");
1500 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1501 "True and False arms of SelectCC must have same type!");
1502 assert(Ops[2].getValueType() == VT &&
1503 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001504 break;
1505 }
1506 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001507 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001508 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1509 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001510 break;
1511 }
Chris Lattneref847df2005-04-09 03:27:28 +00001512 }
1513
Chris Lattner385328c2005-05-14 07:42:29 +00001514 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001515 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001516 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001517 if (VT != MVT::Flag) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001518 SelectionDAGCSEMap::NodeID ID(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001519 void *IP = 0;
1520 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1521 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001522 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001523 N->setValueTypes(VTs);
Chris Lattnera5682852006-08-07 23:03:03 +00001524 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001525 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001526 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001527 N->setValueTypes(VTs);
Chris Lattner43247a12005-08-25 19:12:10 +00001528 }
Chris Lattneref847df2005-04-09 03:27:28 +00001529 AllNodes.push_back(N);
1530 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001531}
1532
Chris Lattner89c34632005-05-14 06:20:26 +00001533SDOperand SelectionDAG::getNode(unsigned Opcode,
1534 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001535 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001536 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
1537 Ops, NumOps);
1538}
1539
1540SDOperand SelectionDAG::getNode(unsigned Opcode,
1541 const MVT::ValueType *VTs, unsigned NumVTs,
1542 const SDOperand *Ops, unsigned NumOps) {
1543 if (NumVTs == 1)
1544 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00001545 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
1546}
1547
1548SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
1549 const SDOperand *Ops, unsigned NumOps) {
1550 if (VTList.NumVTs == 1)
1551 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00001552
Chris Lattner5f056bf2005-07-10 01:55:33 +00001553 switch (Opcode) {
1554 case ISD::EXTLOAD:
1555 case ISD::SEXTLOAD:
1556 case ISD::ZEXTLOAD: {
1557 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
Chris Lattnerbe384162006-08-16 22:57:46 +00001558 assert(NumOps == 4 && VTList.NumVTs == 2 && "Bad *EXTLOAD!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001559 // If they are asking for an extending load from/to the same thing, return a
1560 // normal load.
Chris Lattnerbe384162006-08-16 22:57:46 +00001561 if (VTList.VTs[0] == EVT)
1562 return getLoad(VTList.VTs[0], Ops[0], Ops[1], Ops[2]);
1563 if (MVT::isVector(VTList.VTs[0])) {
1564 assert(EVT == MVT::getVectorBaseType(VTList.VTs[0]) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001565 "Invalid vector extload!");
1566 } else {
Chris Lattnerbe384162006-08-16 22:57:46 +00001567 assert(EVT < VTList.VTs[0] &&
Chris Lattnerce872152006-03-19 06:31:19 +00001568 "Should only be an extending load, not truncating!");
1569 }
Chris Lattnerbe384162006-08-16 22:57:46 +00001570 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VTList.VTs[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001571 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattnerbe384162006-08-16 22:57:46 +00001572 assert(MVT::isInteger(VTList.VTs[0]) == MVT::isInteger(EVT) &&
Chris Lattner5f056bf2005-07-10 01:55:33 +00001573 "Cannot convert from FP to Int or Int -> FP!");
1574 break;
1575 }
1576
Chris Lattnere89083a2005-05-14 07:25:05 +00001577 // FIXME: figure out how to safely handle things like
1578 // int foo(int x) { return 1 << (x & 255); }
1579 // int bar() { return foo(256); }
1580#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001581 case ISD::SRA_PARTS:
1582 case ISD::SRL_PARTS:
1583 case ISD::SHL_PARTS:
1584 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001585 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001586 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1587 else if (N3.getOpcode() == ISD::AND)
1588 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1589 // If the and is only masking out bits that cannot effect the shift,
1590 // eliminate the and.
1591 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1592 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1593 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1594 }
1595 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001596#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001597 }
Chris Lattner89c34632005-05-14 06:20:26 +00001598
Chris Lattner43247a12005-08-25 19:12:10 +00001599 // Memoize the node unless it returns a flag.
1600 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00001601 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Chris Lattnera5682852006-08-07 23:03:03 +00001602 SelectionDAGCSEMap::NodeID ID;
1603 ID.SetOpcode(Opcode);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001604 ID.SetValueTypes(VTList);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001605 ID.SetOperands(&Ops[0], NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00001606 void *IP = 0;
1607 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1608 return SDOperand(E, 0);
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001609 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001610 N->setValueTypes(VTList);
Chris Lattnera5682852006-08-07 23:03:03 +00001611 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001612 } else {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001613 N = new SDNode(Opcode, Ops, NumOps);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001614 N->setValueTypes(VTList);
Chris Lattner43247a12005-08-25 19:12:10 +00001615 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001616 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001617 return SDOperand(N, 0);
1618}
1619
Chris Lattner70046e92006-08-15 17:46:01 +00001620SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
1621 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00001622}
1623
Chris Lattner70046e92006-08-15 17:46:01 +00001624SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00001625 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1626 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00001627 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00001628 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001629 }
1630 std::vector<MVT::ValueType> V;
1631 V.push_back(VT1);
1632 V.push_back(VT2);
1633 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001634 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00001635}
Chris Lattner70046e92006-08-15 17:46:01 +00001636SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
1637 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001638 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00001639 E = VTList.end(); I != E; ++I) {
1640 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
1641 (*I)[2] == VT3)
1642 return makeVTList(&(*I)[0], 3);
1643 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001644 std::vector<MVT::ValueType> V;
1645 V.push_back(VT1);
1646 V.push_back(VT2);
1647 V.push_back(VT3);
1648 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00001649 return makeVTList(&(*VTList.begin())[0], 3);
1650}
1651
1652SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
1653 switch (NumVTs) {
1654 case 0: assert(0 && "Cannot have nodes without results!");
1655 case 1: return makeVTList(SDNode::getValueTypeList(VTs[0]), 1);
1656 case 2: return getVTList(VTs[0], VTs[1]);
1657 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
1658 default: break;
1659 }
1660
1661 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1662 E = VTList.end(); I != E; ++I) {
1663 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
1664
1665 bool NoMatch = false;
1666 for (unsigned i = 2; i != NumVTs; ++i)
1667 if (VTs[i] != (*I)[i]) {
1668 NoMatch = true;
1669 break;
1670 }
1671 if (!NoMatch)
1672 return makeVTList(&*I->begin(), NumVTs);
1673 }
1674
1675 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
1676 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001677}
1678
1679
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001680/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1681/// specified operands. If the resultant node already exists in the DAG,
1682/// this does not modify the specified node, instead it returns the node that
1683/// already exists. If the resultant node does not exist in the DAG, the
1684/// input node is returned. As a degenerate case, if you specify the same
1685/// input operands as the node already has, the input node is returned.
1686SDOperand SelectionDAG::
1687UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1688 SDNode *N = InN.Val;
1689 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1690
1691 // Check to see if there is no change.
1692 if (Op == N->getOperand(0)) return InN;
1693
1694 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001695 void *InsertPos = 0;
1696 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
1697 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001698
1699 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001700 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001701 RemoveNodeFromCSEMaps(N);
1702
1703 // Now we update the operands.
1704 N->OperandList[0].Val->removeUser(N);
1705 Op.Val->addUser(N);
1706 N->OperandList[0] = Op;
1707
1708 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001709 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001710 return InN;
1711}
1712
1713SDOperand SelectionDAG::
1714UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1715 SDNode *N = InN.Val;
1716 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1717
1718 // Check to see if there is no change.
1719 bool AnyChange = false;
1720 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1721 return InN; // No operands changed, just return the input node.
1722
1723 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001724 void *InsertPos = 0;
1725 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
1726 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001727
1728 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001729 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001730 RemoveNodeFromCSEMaps(N);
1731
1732 // Now we update the operands.
1733 if (N->OperandList[0] != Op1) {
1734 N->OperandList[0].Val->removeUser(N);
1735 Op1.Val->addUser(N);
1736 N->OperandList[0] = Op1;
1737 }
1738 if (N->OperandList[1] != Op2) {
1739 N->OperandList[1].Val->removeUser(N);
1740 Op2.Val->addUser(N);
1741 N->OperandList[1] = Op2;
1742 }
1743
1744 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001745 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001746 return InN;
1747}
1748
1749SDOperand SelectionDAG::
1750UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001751 SDOperand Ops[] = { Op1, Op2, Op3 };
1752 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001753}
1754
1755SDOperand SelectionDAG::
1756UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1757 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001758 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
1759 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001760}
1761
1762SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001763UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1764 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001765 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
1766 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00001767}
1768
1769
1770SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001771UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001772 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001773 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001774 "Update with wrong number of operands");
1775
1776 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001777 bool AnyChange = false;
1778 for (unsigned i = 0; i != NumOps; ++i) {
1779 if (Ops[i] != N->getOperand(i)) {
1780 AnyChange = true;
1781 break;
1782 }
1783 }
1784
1785 // No operands changed, just return the input node.
1786 if (!AnyChange) return InN;
1787
1788 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00001789 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001790 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00001791 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001792
1793 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00001794 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001795 RemoveNodeFromCSEMaps(N);
1796
1797 // Now we update the operands.
1798 for (unsigned i = 0; i != NumOps; ++i) {
1799 if (N->OperandList[i] != Ops[i]) {
1800 N->OperandList[i].Val->removeUser(N);
1801 Ops[i].Val->addUser(N);
1802 N->OperandList[i] = Ops[i];
1803 }
1804 }
1805
1806 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00001807 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001808 return InN;
1809}
1810
1811
1812
Chris Lattner149c58c2005-08-16 18:17:10 +00001813
1814/// SelectNodeTo - These are used for target selectors to *mutate* the
1815/// specified node to have the specified return type, Target opcode, and
1816/// operands. Note that target opcodes are stored as
1817/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001818///
1819/// Note that SelectNodeTo returns the resultant node. If there is already a
1820/// node of the specified opcode and operands, it returns that node instead of
1821/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00001822SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1823 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001824 SDVTList VTs = getVTList(VT);
Chris Lattner4a283e92006-08-11 18:38:11 +00001825 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
1826 void *IP = 0;
1827 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001828 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00001829
Chris Lattner7651fa42005-08-24 23:00:29 +00001830 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001831
Chris Lattner7651fa42005-08-24 23:00:29 +00001832 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001833 N->setValueTypes(VTs);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001834
Chris Lattner4a283e92006-08-11 18:38:11 +00001835 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001836 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00001837}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001838
Evan Cheng95514ba2006-08-26 08:00:10 +00001839SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1840 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001841 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001842 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001843 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1);
1844 void *IP = 0;
1845 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001846 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001847
Chris Lattner149c58c2005-08-16 18:17:10 +00001848 RemoveNodeFromCSEMaps(N);
1849 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001850 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001851 N->setOperands(Op1);
Chris Lattnera5682852006-08-07 23:03:03 +00001852 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00001853 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001854}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001855
Evan Cheng95514ba2006-08-26 08:00:10 +00001856SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1857 MVT::ValueType VT, SDOperand Op1,
1858 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001859 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001860 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001861 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1862 void *IP = 0;
1863 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001864 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001865
Chris Lattner149c58c2005-08-16 18:17:10 +00001866 RemoveNodeFromCSEMaps(N);
1867 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001868 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001869 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001870
Chris Lattnera5682852006-08-07 23:03:03 +00001871 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001872 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001873}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001874
Evan Cheng95514ba2006-08-26 08:00:10 +00001875SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1876 MVT::ValueType VT, SDOperand Op1,
1877 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001878 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001879 SDVTList VTs = getVTList(VT);
1880 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1881 Op1, Op2, Op3);
Chris Lattnera5682852006-08-07 23:03:03 +00001882 void *IP = 0;
1883 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001884 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001885
Chris Lattner149c58c2005-08-16 18:17:10 +00001886 RemoveNodeFromCSEMaps(N);
1887 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001888 N->setValueTypes(VTs);
Chris Lattner149c58c2005-08-16 18:17:10 +00001889 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001890
Chris Lattnera5682852006-08-07 23:03:03 +00001891 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001892 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00001893}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001894
Evan Cheng95514ba2006-08-26 08:00:10 +00001895SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00001896 MVT::ValueType VT, const SDOperand *Ops,
1897 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001898 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001899 SDVTList VTs = getVTList(VT);
Chris Lattnera5682852006-08-07 23:03:03 +00001900 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00001901 for (unsigned i = 0; i != NumOps; ++i)
1902 ID.AddOperand(Ops[i]);
Chris Lattnera5682852006-08-07 23:03:03 +00001903 void *IP = 0;
1904 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001905 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00001906
Chris Lattner6b09a292005-08-21 18:49:33 +00001907 RemoveNodeFromCSEMaps(N);
1908 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001909 N->setValueTypes(VTs);
Evan Cheng694481e2006-08-27 08:08:54 +00001910 N->setOperands(Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001911
Chris Lattnera5682852006-08-07 23:03:03 +00001912 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001913 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001914}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001915
Evan Cheng95514ba2006-08-26 08:00:10 +00001916SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1917 MVT::ValueType VT1, MVT::ValueType VT2,
1918 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00001919 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00001920 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs, Op1, Op2);
1921 void *IP = 0;
1922 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001923 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001924
Chris Lattner0fb094f2005-11-19 01:44:53 +00001925 RemoveNodeFromCSEMaps(N);
1926 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001927 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001928 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001929
Chris Lattnera5682852006-08-07 23:03:03 +00001930 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001931 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00001932}
1933
Evan Cheng95514ba2006-08-26 08:00:10 +00001934SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1935 MVT::ValueType VT1, MVT::ValueType VT2,
1936 SDOperand Op1, SDOperand Op2,
1937 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001938 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00001939 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattnera5682852006-08-07 23:03:03 +00001940 SelectionDAGCSEMap::NodeID ID(ISD::BUILTIN_OP_END+TargetOpc, VTs,
1941 Op1, Op2, Op3);
1942 void *IP = 0;
1943 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00001944 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001945
Chris Lattner0fb094f2005-11-19 01:44:53 +00001946 RemoveNodeFromCSEMaps(N);
1947 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
Chris Lattner0b3e5252006-08-15 19:11:05 +00001948 N->setValueTypes(VTs);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001949 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001950
Chris Lattnera5682852006-08-07 23:03:03 +00001951 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00001952 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00001953}
1954
Chris Lattner0fb094f2005-11-19 01:44:53 +00001955
Evan Cheng6ae46c42006-02-09 07:15:23 +00001956/// getTargetNode - These are used for target selectors to create a new node
1957/// with specified return type(s), target opcode, and operands.
1958///
1959/// Note that getTargetNode returns the resultant node. If there is already a
1960/// node of the specified opcode and operands, it returns that node instead of
1961/// the current one.
1962SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
1963 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
1964}
1965SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
1966 SDOperand Op1) {
1967 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
1968}
1969SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
1970 SDOperand Op1, SDOperand Op2) {
1971 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
1972}
1973SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
1974 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1975 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
1976}
1977SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001978 const SDOperand *Ops, unsigned NumOps) {
1979 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00001980}
1981SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
1982 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00001983 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001984 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00001985}
1986SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00001987 MVT::ValueType VT2, SDOperand Op1,
1988 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00001989 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001990 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001991 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00001992}
1993SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00001994 MVT::ValueType VT2, SDOperand Op1,
1995 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00001996 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001997 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00001998 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00001999}
Evan Cheng694481e2006-08-27 08:08:54 +00002000SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2001 MVT::ValueType VT2,
2002 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00002003 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00002004 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002005}
2006SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2007 MVT::ValueType VT2, MVT::ValueType VT3,
2008 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00002009 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002010 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002011 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002012}
Evan Cheng6ae46c42006-02-09 07:15:23 +00002013SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00002014 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002015 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00002016 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
2017 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00002018}
2019
Evan Cheng99157a02006-08-07 22:13:29 +00002020/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002021/// This can cause recursive merging of nodes in the DAG.
2022///
Chris Lattner8b52f212005-08-26 18:36:28 +00002023/// This version assumes From/To have a single result value.
2024///
Chris Lattner1e111c72005-09-07 05:37:01 +00002025void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2026 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002027 SDNode *From = FromN.Val, *To = ToN.Val;
2028 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2029 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002030 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002031
Chris Lattner8b8749f2005-08-17 19:00:20 +00002032 while (!From->use_empty()) {
2033 // Process users until they are all gone.
2034 SDNode *U = *From->use_begin();
2035
2036 // This node is about to morph, remove its old self from the CSE maps.
2037 RemoveNodeFromCSEMaps(U);
2038
Chris Lattner65113b22005-11-08 22:07:03 +00002039 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2040 I != E; ++I)
2041 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002042 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002043 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002044 To->addUser(U);
2045 }
2046
2047 // Now that we have modified U, add it back to the CSE maps. If it already
2048 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002049 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2050 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002051 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002052 if (Deleted) Deleted->push_back(U);
2053 DeleteNodeNotInCSEMaps(U);
2054 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002055 }
2056}
2057
2058/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2059/// This can cause recursive merging of nodes in the DAG.
2060///
2061/// This version assumes From/To have matching types and numbers of result
2062/// values.
2063///
Chris Lattner1e111c72005-09-07 05:37:01 +00002064void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2065 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002066 assert(From != To && "Cannot replace uses of with self");
2067 assert(From->getNumValues() == To->getNumValues() &&
2068 "Cannot use this version of ReplaceAllUsesWith!");
2069 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002070 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002071 return;
2072 }
2073
2074 while (!From->use_empty()) {
2075 // Process users until they are all gone.
2076 SDNode *U = *From->use_begin();
2077
2078 // This node is about to morph, remove its old self from the CSE maps.
2079 RemoveNodeFromCSEMaps(U);
2080
Chris Lattner65113b22005-11-08 22:07:03 +00002081 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2082 I != E; ++I)
2083 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002084 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002085 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002086 To->addUser(U);
2087 }
2088
2089 // Now that we have modified U, add it back to the CSE maps. If it already
2090 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002091 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2092 ReplaceAllUsesWith(U, Existing, Deleted);
2093 // U is now dead.
2094 if (Deleted) Deleted->push_back(U);
2095 DeleteNodeNotInCSEMaps(U);
2096 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002097 }
2098}
2099
Chris Lattner8b52f212005-08-26 18:36:28 +00002100/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2101/// This can cause recursive merging of nodes in the DAG.
2102///
2103/// This version can replace From with any result values. To must match the
2104/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002105void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002106 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00002107 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00002108 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002109 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002110 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002111 return;
2112 }
2113
2114 while (!From->use_empty()) {
2115 // Process users until they are all gone.
2116 SDNode *U = *From->use_begin();
2117
2118 // This node is about to morph, remove its old self from the CSE maps.
2119 RemoveNodeFromCSEMaps(U);
2120
Chris Lattner65113b22005-11-08 22:07:03 +00002121 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2122 I != E; ++I)
2123 if (I->Val == From) {
2124 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002125 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002126 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002127 ToOp.Val->addUser(U);
2128 }
2129
2130 // Now that we have modified U, add it back to the CSE maps. If it already
2131 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002132 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2133 ReplaceAllUsesWith(U, Existing, Deleted);
2134 // U is now dead.
2135 if (Deleted) Deleted->push_back(U);
2136 DeleteNodeNotInCSEMaps(U);
2137 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002138 }
2139}
2140
Chris Lattner012f2412006-02-17 21:58:01 +00002141/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2142/// uses of other values produced by From.Val alone. The Deleted vector is
2143/// handled the same was as for ReplaceAllUsesWith.
2144void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2145 std::vector<SDNode*> &Deleted) {
2146 assert(From != To && "Cannot replace a value with itself");
2147 // Handle the simple, trivial, case efficiently.
2148 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2149 ReplaceAllUsesWith(From, To, &Deleted);
2150 return;
2151 }
2152
2153 // Get all of the users in a nice, deterministically ordered, uniqued set.
2154 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2155
2156 while (!Users.empty()) {
2157 // We know that this user uses some value of From. If it is the right
2158 // value, update it.
2159 SDNode *User = Users.back();
2160 Users.pop_back();
2161
2162 for (SDOperand *Op = User->OperandList,
2163 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2164 if (*Op == From) {
2165 // Okay, we know this user needs to be updated. Remove its old self
2166 // from the CSE maps.
2167 RemoveNodeFromCSEMaps(User);
2168
2169 // Update all operands that match "From".
2170 for (; Op != E; ++Op) {
2171 if (*Op == From) {
2172 From.Val->removeUser(User);
2173 *Op = To;
2174 To.Val->addUser(User);
2175 }
2176 }
2177
2178 // Now that we have modified User, add it back to the CSE maps. If it
2179 // already exists there, recursively merge the results together.
2180 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2181 unsigned NumDeleted = Deleted.size();
2182 ReplaceAllUsesWith(User, Existing, &Deleted);
2183
2184 // User is now dead.
2185 Deleted.push_back(User);
2186 DeleteNodeNotInCSEMaps(User);
2187
2188 // We have to be careful here, because ReplaceAllUsesWith could have
2189 // deleted a user of From, which means there may be dangling pointers
2190 // in the "Users" setvector. Scan over the deleted node pointers and
2191 // remove them from the setvector.
2192 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2193 Users.remove(Deleted[i]);
2194 }
2195 break; // Exit the operand scanning loop.
2196 }
2197 }
2198 }
2199}
2200
Chris Lattner7b2880c2005-08-24 22:44:39 +00002201
Evan Chenge6f35d82006-08-01 08:20:41 +00002202/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
2203/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00002204unsigned SelectionDAG::AssignNodeIds() {
2205 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00002206 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
2207 SDNode *N = I;
2208 N->setNodeId(Id++);
2209 }
2210 return Id;
2211}
2212
Evan Chenge6f35d82006-08-01 08:20:41 +00002213/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00002214/// based on their topological order. It returns the maximum id and a vector
2215/// of the SDNodes* in assigned order by reference.
2216unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00002217 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002218 std::vector<unsigned> InDegree(DAGSize);
2219 std::vector<SDNode*> Sources;
2220
2221 // Use a two pass approach to avoid using a std::map which is slow.
2222 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00002223 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
2224 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00002225 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00002226 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00002227 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00002228 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00002229 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002230 }
2231
Evan Cheng99157a02006-08-07 22:13:29 +00002232 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00002233 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00002234 SDNode *N = Sources.back();
2235 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00002236 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00002237 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
2238 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00002239 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00002240 if (Degree == 0)
2241 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00002242 }
2243 }
2244
Evan Chengc384d6c2006-08-02 22:00:34 +00002245 // Second pass, assign the actual topological order as node ids.
2246 Id = 0;
2247 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
2248 TI != TE; ++TI)
2249 (*TI)->setNodeId(Id++);
2250
2251 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00002252}
2253
2254
Evan Cheng091cba12006-07-27 06:39:06 +00002255
Jim Laskey58b968b2005-08-17 20:08:02 +00002256//===----------------------------------------------------------------------===//
2257// SDNode Class
2258//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002259
Chris Lattner917d2c92006-07-19 00:00:37 +00002260// Out-of-line virtual method to give class a home.
2261void SDNode::ANCHOR() {
2262}
2263
Chris Lattnera3255112005-11-08 23:30:28 +00002264/// getValueTypeList - Return a pointer to the specified value type.
2265///
2266MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2267 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2268 VTs[VT] = VT;
2269 return &VTs[VT];
2270}
Chris Lattnera5682852006-08-07 23:03:03 +00002271
Chris Lattner5c884562005-01-12 18:37:47 +00002272/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2273/// indicated value. This method ignores uses of other values defined by this
2274/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002275bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002276 assert(Value < getNumValues() && "Bad value!");
2277
2278 // If there is only one value, this is easy.
2279 if (getNumValues() == 1)
2280 return use_size() == NUses;
2281 if (Uses.size() < NUses) return false;
2282
Evan Cheng4ee62112006-02-05 06:29:23 +00002283 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002284
2285 std::set<SDNode*> UsersHandled;
2286
Chris Lattnerf83482d2006-08-16 20:59:32 +00002287 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00002288 SDNode *User = *UI;
2289 if (User->getNumOperands() == 1 ||
2290 UsersHandled.insert(User).second) // First time we've seen this?
2291 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2292 if (User->getOperand(i) == TheValue) {
2293 if (NUses == 0)
2294 return false; // too many uses
2295 --NUses;
2296 }
2297 }
2298
2299 // Found exactly the right number of uses?
2300 return NUses == 0;
2301}
2302
2303
Evan Cheng4ee62112006-02-05 06:29:23 +00002304// isOnlyUse - Return true if this node is the only use of N.
2305bool SDNode::isOnlyUse(SDNode *N) const {
2306 bool Seen = false;
2307 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2308 SDNode *User = *I;
2309 if (User == this)
2310 Seen = true;
2311 else
2312 return false;
2313 }
2314
2315 return Seen;
2316}
2317
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002318// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002319bool SDOperand::isOperand(SDNode *N) const {
2320 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2321 if (*this == N->getOperand(i))
2322 return true;
2323 return false;
2324}
2325
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002326bool SDNode::isOperand(SDNode *N) const {
2327 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2328 if (this == N->OperandList[i].Val)
2329 return true;
2330 return false;
2331}
Evan Cheng4ee62112006-02-05 06:29:23 +00002332
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002333const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002334 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002335 default:
2336 if (getOpcode() < ISD::BUILTIN_OP_END)
2337 return "<<Unknown DAG Node>>";
2338 else {
Evan Cheng72261582005-12-20 06:22:03 +00002339 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002340 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002341 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2342 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002343
Evan Cheng72261582005-12-20 06:22:03 +00002344 TargetLowering &TLI = G->getTargetLoweringInfo();
2345 const char *Name =
2346 TLI.getTargetNodeName(getOpcode());
2347 if (Name) return Name;
2348 }
2349
2350 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002351 }
2352
Andrew Lenharth95762122005-03-31 21:24:06 +00002353 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002354 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002355 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002356 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002357 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002358 case ISD::AssertSext: return "AssertSext";
2359 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002360
2361 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002362 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002363 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002364 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002365
2366 case ISD::Constant: return "Constant";
2367 case ISD::ConstantFP: return "ConstantFP";
2368 case ISD::GlobalAddress: return "GlobalAddress";
2369 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002370 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002371 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002372 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002373 case ISD::INTRINSIC_WO_CHAIN: {
2374 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2375 return Intrinsic::getName((Intrinsic::ID)IID);
2376 }
2377 case ISD::INTRINSIC_VOID:
2378 case ISD::INTRINSIC_W_CHAIN: {
2379 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002380 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002381 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002382
Chris Lattnerb2827b02006-03-19 00:52:58 +00002383 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002384 case ISD::TargetConstant: return "TargetConstant";
2385 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002386 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2387 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002388 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002389 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002390 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002391
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002392 case ISD::CopyToReg: return "CopyToReg";
2393 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002394 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002395 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002396 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002397 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002398 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00002399 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002400
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002401 // Unary operators
2402 case ISD::FABS: return "fabs";
2403 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002404 case ISD::FSQRT: return "fsqrt";
2405 case ISD::FSIN: return "fsin";
2406 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00002407 case ISD::FPOWI: return "fpowi";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002408
2409 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002410 case ISD::ADD: return "add";
2411 case ISD::SUB: return "sub";
2412 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002413 case ISD::MULHU: return "mulhu";
2414 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002415 case ISD::SDIV: return "sdiv";
2416 case ISD::UDIV: return "udiv";
2417 case ISD::SREM: return "srem";
2418 case ISD::UREM: return "urem";
2419 case ISD::AND: return "and";
2420 case ISD::OR: return "or";
2421 case ISD::XOR: return "xor";
2422 case ISD::SHL: return "shl";
2423 case ISD::SRA: return "sra";
2424 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002425 case ISD::ROTL: return "rotl";
2426 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002427 case ISD::FADD: return "fadd";
2428 case ISD::FSUB: return "fsub";
2429 case ISD::FMUL: return "fmul";
2430 case ISD::FDIV: return "fdiv";
2431 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002432 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002433 case ISD::VADD: return "vadd";
2434 case ISD::VSUB: return "vsub";
2435 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002436 case ISD::VSDIV: return "vsdiv";
2437 case ISD::VUDIV: return "vudiv";
2438 case ISD::VAND: return "vand";
2439 case ISD::VOR: return "vor";
2440 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002441
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002442 case ISD::SETCC: return "setcc";
2443 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002444 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002445 case ISD::VSELECT: return "vselect";
2446 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2447 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2448 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002449 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002450 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2451 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2452 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2453 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2454 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002455 case ISD::ADDC: return "addc";
2456 case ISD::ADDE: return "adde";
2457 case ISD::SUBC: return "subc";
2458 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002459 case ISD::SHL_PARTS: return "shl_parts";
2460 case ISD::SRA_PARTS: return "sra_parts";
2461 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002462
Chris Lattner7f644642005-04-28 21:44:03 +00002463 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002464 case ISD::SIGN_EXTEND: return "sign_extend";
2465 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002466 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002467 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002468 case ISD::TRUNCATE: return "truncate";
2469 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002470 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002471 case ISD::FP_EXTEND: return "fp_extend";
2472
2473 case ISD::SINT_TO_FP: return "sint_to_fp";
2474 case ISD::UINT_TO_FP: return "uint_to_fp";
2475 case ISD::FP_TO_SINT: return "fp_to_sint";
2476 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002477 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002478
2479 // Control flow instructions
2480 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002481 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002482 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002483 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002484 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002485 case ISD::CALLSEQ_START: return "callseq_start";
2486 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002487
2488 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002489 case ISD::LOAD: return "load";
2490 case ISD::STORE: return "store";
2491 case ISD::VLOAD: return "vload";
2492 case ISD::EXTLOAD: return "extload";
2493 case ISD::SEXTLOAD: return "sextload";
2494 case ISD::ZEXTLOAD: return "zextload";
2495 case ISD::TRUNCSTORE: return "truncstore";
2496 case ISD::VAARG: return "vaarg";
2497 case ISD::VACOPY: return "vacopy";
2498 case ISD::VAEND: return "vaend";
2499 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002500 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002501 case ISD::EXTRACT_ELEMENT: return "extract_element";
2502 case ISD::BUILD_PAIR: return "build_pair";
2503 case ISD::STACKSAVE: return "stacksave";
2504 case ISD::STACKRESTORE: return "stackrestore";
2505
2506 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002507 case ISD::MEMSET: return "memset";
2508 case ISD::MEMCPY: return "memcpy";
2509 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002510
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002511 // Bit manipulation
2512 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002513 case ISD::CTPOP: return "ctpop";
2514 case ISD::CTTZ: return "cttz";
2515 case ISD::CTLZ: return "ctlz";
2516
Chris Lattner36ce6912005-11-29 06:21:05 +00002517 // Debug info
2518 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002519 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002520 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002521
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002522 case ISD::CONDCODE:
2523 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002524 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002525 case ISD::SETOEQ: return "setoeq";
2526 case ISD::SETOGT: return "setogt";
2527 case ISD::SETOGE: return "setoge";
2528 case ISD::SETOLT: return "setolt";
2529 case ISD::SETOLE: return "setole";
2530 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002531
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002532 case ISD::SETO: return "seto";
2533 case ISD::SETUO: return "setuo";
2534 case ISD::SETUEQ: return "setue";
2535 case ISD::SETUGT: return "setugt";
2536 case ISD::SETUGE: return "setuge";
2537 case ISD::SETULT: return "setult";
2538 case ISD::SETULE: return "setule";
2539 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002540
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002541 case ISD::SETEQ: return "seteq";
2542 case ISD::SETGT: return "setgt";
2543 case ISD::SETGE: return "setge";
2544 case ISD::SETLT: return "setlt";
2545 case ISD::SETLE: return "setle";
2546 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002547 }
2548 }
2549}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002550
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002551void SDNode::dump() const { dump(0); }
2552void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002553 std::cerr << (void*)this << ": ";
2554
2555 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2556 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002557 if (getValueType(i) == MVT::Other)
2558 std::cerr << "ch";
2559 else
2560 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002561 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002562 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002563
2564 std::cerr << " ";
2565 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2566 if (i) std::cerr << ", ";
2567 std::cerr << (void*)getOperand(i).Val;
2568 if (unsigned RN = getOperand(i).ResNo)
2569 std::cerr << ":" << RN;
2570 }
2571
2572 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2573 std::cerr << "<" << CSDN->getValue() << ">";
2574 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2575 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002576 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002577 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002578 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002579 std::cerr << "<";
2580 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002581 if (offset > 0)
2582 std::cerr << " + " << offset;
2583 else
2584 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002585 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002586 std::cerr << "<" << FIDN->getIndex() << ">";
2587 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002588 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002589 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002590 if (offset > 0)
2591 std::cerr << " + " << offset;
2592 else
2593 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002594 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002595 std::cerr << "<";
2596 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2597 if (LBB)
2598 std::cerr << LBB->getName() << " ";
2599 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002600 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002601 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002602 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2603 } else {
2604 std::cerr << " #" << R->getReg();
2605 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002606 } else if (const ExternalSymbolSDNode *ES =
2607 dyn_cast<ExternalSymbolSDNode>(this)) {
2608 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002609 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2610 if (M->getValue())
2611 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2612 else
2613 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002614 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2615 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002616 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002617}
2618
Chris Lattnerde202b32005-11-09 23:47:37 +00002619static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002620 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2621 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002622 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002623 else
2624 std::cerr << "\n" << std::string(indent+2, ' ')
2625 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002626
Chris Lattnerea946cd2005-01-09 20:38:33 +00002627
2628 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002629 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002630}
2631
Chris Lattnerc3aae252005-01-07 07:46:32 +00002632void SelectionDAG::dump() const {
2633 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002634 std::vector<const SDNode*> Nodes;
2635 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2636 I != E; ++I)
2637 Nodes.push_back(I);
2638
Chris Lattner49d24712005-01-09 20:26:36 +00002639 std::sort(Nodes.begin(), Nodes.end());
2640
2641 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002642 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002643 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002644 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002645
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002646 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002647
Chris Lattnerc3aae252005-01-07 07:46:32 +00002648 std::cerr << "\n\n";
2649}
2650