blob: e1b52019e965ddf19bb9aedee46b39ca8cbf4b72 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Reid Spencer954da372004-07-04 12:19:56 +000019#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000020#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000021#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000022#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner5cdcc582005-01-09 20:52:51 +000025static bool isCommutativeBinOp(unsigned Opcode) {
26 switch (Opcode) {
27 case ISD::ADD:
28 case ISD::MUL:
29 case ISD::AND:
30 case ISD::OR:
31 case ISD::XOR: return true;
32 default: return false; // FIXME: Need commutative info for user ops!
33 }
34}
35
36static bool isAssociativeBinOp(unsigned Opcode) {
37 switch (Opcode) {
38 case ISD::ADD:
39 case ISD::MUL:
40 case ISD::AND:
41 case ISD::OR:
42 case ISD::XOR: return true;
43 default: return false; // FIXME: Need associative info for user ops!
44 }
45}
46
47static unsigned ExactLog2(uint64_t Val) {
48 unsigned Count = 0;
49 while (Val != 1) {
50 Val >>= 1;
51 ++Count;
52 }
53 return Count;
54}
55
56// isInvertibleForFree - Return true if there is no cost to emitting the logical
57// inverse of this node.
58static bool isInvertibleForFree(SDOperand N) {
59 if (isa<ConstantSDNode>(N.Val)) return true;
60 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
61 return true;
62 return false;
63}
64
65
Chris Lattnerc3aae252005-01-07 07:46:32 +000066/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
67/// when given the operation for (X op Y).
68ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
69 // To perform this operation, we just need to swap the L and G bits of the
70 // operation.
71 unsigned OldL = (Operation >> 2) & 1;
72 unsigned OldG = (Operation >> 1) & 1;
73 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
74 (OldL << 1) | // New G bit
75 (OldG << 2)); // New L bit.
76}
77
78/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
79/// 'op' is a valid SetCC operation.
80ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
81 unsigned Operation = Op;
82 if (isInteger)
83 Operation ^= 7; // Flip L, G, E bits, but not U.
84 else
85 Operation ^= 15; // Flip all of the condition bits.
86 if (Operation > ISD::SETTRUE2)
87 Operation &= ~8; // Don't let N and U bits get set.
88 return ISD::CondCode(Operation);
89}
90
91
92/// isSignedOp - For an integer comparison, return 1 if the comparison is a
93/// signed operation and 2 if the result is an unsigned comparison. Return zero
94/// if the operation does not depend on the sign of the input (setne and seteq).
95static int isSignedOp(ISD::CondCode Opcode) {
96 switch (Opcode) {
97 default: assert(0 && "Illegal integer setcc operation!");
98 case ISD::SETEQ:
99 case ISD::SETNE: return 0;
100 case ISD::SETLT:
101 case ISD::SETLE:
102 case ISD::SETGT:
103 case ISD::SETGE: return 1;
104 case ISD::SETULT:
105 case ISD::SETULE:
106 case ISD::SETUGT:
107 case ISD::SETUGE: return 2;
108 }
109}
110
111/// getSetCCOrOperation - Return the result of a logical OR between different
112/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
113/// returns SETCC_INVALID if it is not possible to represent the resultant
114/// comparison.
115ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
116 bool isInteger) {
117 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
118 // Cannot fold a signed integer setcc with an unsigned integer setcc.
119 return ISD::SETCC_INVALID;
120
121 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
122
123 // If the N and U bits get set then the resultant comparison DOES suddenly
124 // care about orderedness, and is true when ordered.
125 if (Op > ISD::SETTRUE2)
126 Op &= ~16; // Clear the N bit.
127 return ISD::CondCode(Op);
128}
129
130/// getSetCCAndOperation - Return the result of a logical AND between different
131/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
132/// function returns zero if it is not possible to represent the resultant
133/// comparison.
134ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
135 bool isInteger) {
136 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
137 // Cannot fold a signed setcc with an unsigned setcc.
138 return ISD::SETCC_INVALID;
139
140 // Combine all of the condition bits.
141 return ISD::CondCode(Op1 & Op2);
142}
143
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000144/// RemoveDeadNodes - This method deletes all unreachable nodes in the
145/// SelectionDAG, including nodes (like loads) that have uses of their token
146/// chain but no other uses and no side effect. If a node is passed in as an
147/// argument, it is used as the seed for node deletion.
148void SelectionDAG::RemoveDeadNodes(SDNode *N) {
149 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
150
151 // Create a dummy node (which is not added to allnodes), that adds a reference
152 // to the root node, preventing it from being deleted.
153 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
154
155 DeleteNodeIfDead(N, &AllNodeSet);
156
157 Restart:
158 unsigned NumNodes = AllNodeSet.size();
159 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
160 I != E; ++I) {
161 // Try to delete this node.
162 DeleteNodeIfDead(*I, &AllNodeSet);
163
164 // If we actually deleted any nodes, do not use invalid iterators in
165 // AllNodeSet.
166 if (AllNodeSet.size() != NumNodes)
167 goto Restart;
168 }
169
170 // Restore AllNodes.
171 if (AllNodes.size() != NumNodes)
172 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
173
174 // If the root changed (e.g. it was a dead load, update the root).
175 setRoot(DummyNode->getOperand(0));
176
177 // Now that we are done with the dummy node, delete it.
178 DummyNode->getOperand(0).Val->removeUser(DummyNode);
179 delete DummyNode;
180}
181
182void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
183 if (!N->use_empty())
184 return;
185
186 // Okay, we really are going to delete this node. First take this out of the
187 // appropriate CSE map.
188 switch (N->getOpcode()) {
189 case ISD::Constant:
190 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
191 N->getValueType(0)));
192 break;
193 case ISD::ConstantFP:
194 ConstantFPs.erase(std::make_pair(cast<ConstantFPSDNode>(N)->getValue(),
195 N->getValueType(0)));
196 break;
197 case ISD::GlobalAddress:
198 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
199 break;
200 case ISD::FrameIndex:
201 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
202 break;
203 case ISD::ConstantPool:
204 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
205 break;
206 case ISD::BasicBlock:
207 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
208 break;
209 case ISD::ExternalSymbol:
210 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
211 break;
212
213 case ISD::LOAD:
214 Loads.erase(std::make_pair(N->getOperand(1),
215 std::make_pair(N->getOperand(0),
216 N->getValueType(0))));
217 break;
218 case ISD::SETCC:
219 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
220 N->getOperand(1)),
221 cast<SetCCSDNode>(N)->getCondition()));
222 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000223 case ISD::TRUNCSTORE: {
224 EVTStruct NN;
225 NN.Opcode = ISD::TRUNCSTORE;
226 NN.VT = N->getValueType(0);
227 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
228 NN.Ops.push_back(N->getOperand(0));
229 NN.Ops.push_back(N->getOperand(1));
230 NN.Ops.push_back(N->getOperand(2));
231 MVTSDNodes.erase(NN);
232 break;
233 }
234 case ISD::EXTLOAD:
235 case ISD::SEXTLOAD:
236 case ISD::ZEXTLOAD: {
237 EVTStruct NN;
238 NN.Opcode = N->getOpcode();
239 NN.VT = N->getValueType(0);
240 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
241 NN.Ops.push_back(N->getOperand(0));
242 NN.Ops.push_back(N->getOperand(1));
243 MVTSDNodes.erase(NN);
244 break;
245 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000246 default:
247 if (N->getNumOperands() == 1)
248 UnaryOps.erase(std::make_pair(N->getOpcode(),
249 std::make_pair(N->getOperand(0),
250 N->getValueType(0))));
251 else if (N->getNumOperands() == 2)
252 BinaryOps.erase(std::make_pair(N->getOpcode(),
253 std::make_pair(N->getOperand(0),
254 N->getOperand(1))));
255 break;
256 }
257
258 // Next, brutally remove the operand list.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000259 while (!N->Operands.empty()) {
Chris Lattner7c68ec62005-01-07 23:32:00 +0000260 SDNode *O = N->Operands.back().Val;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 N->Operands.pop_back();
Chris Lattner7c68ec62005-01-07 23:32:00 +0000262 O->removeUser(N);
263
264 // Now that we removed this operand, see if there are no uses of it left.
265 DeleteNodeIfDead(O, NodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266 }
267
268 // Remove the node from the nodes set and delete it.
269 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
270 AllNodeSet.erase(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000271
272 // Now that the node is gone, check to see if any of the operands of this node
273 // are dead now.
Chris Lattner7c68ec62005-01-07 23:32:00 +0000274 delete N;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000275}
276
277
Chris Lattner78ec3112003-08-11 14:57:33 +0000278SelectionDAG::~SelectionDAG() {
279 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
280 delete AllNodes[i];
281}
282
Chris Lattnerc3aae252005-01-07 07:46:32 +0000283SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
284 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
285 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000286 if (VT != MVT::i64)
287 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
288
Chris Lattnerc3aae252005-01-07 07:46:32 +0000289 SDNode *&N = Constants[std::make_pair(Val, VT)];
290 if (N) return SDOperand(N, 0);
291 N = new ConstantSDNode(Val, VT);
292 AllNodes.push_back(N);
293 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000294}
295
Chris Lattnerc3aae252005-01-07 07:46:32 +0000296SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
297 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
298 if (VT == MVT::f32)
299 Val = (float)Val; // Mask out extra precision.
300
301 SDNode *&N = ConstantFPs[std::make_pair(Val, VT)];
302 if (N) return SDOperand(N, 0);
303 N = new ConstantFPSDNode(Val, VT);
304 AllNodes.push_back(N);
305 return SDOperand(N, 0);
306}
307
308
309
310SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
311 MVT::ValueType VT) {
312 SDNode *&N = GlobalValues[GV];
313 if (N) return SDOperand(N, 0);
314 N = new GlobalAddressSDNode(GV,VT);
315 AllNodes.push_back(N);
316 return SDOperand(N, 0);
317}
318
319SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
320 SDNode *&N = FrameIndices[FI];
321 if (N) return SDOperand(N, 0);
322 N = new FrameIndexSDNode(FI, VT);
323 AllNodes.push_back(N);
324 return SDOperand(N, 0);
325}
326
327SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
328 SDNode *N = ConstantPoolIndices[CPIdx];
329 if (N) return SDOperand(N, 0);
330 N = new ConstantPoolSDNode(CPIdx, VT);
331 AllNodes.push_back(N);
332 return SDOperand(N, 0);
333}
334
335SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
336 SDNode *&N = BBNodes[MBB];
337 if (N) return SDOperand(N, 0);
338 N = new BasicBlockSDNode(MBB);
339 AllNodes.push_back(N);
340 return SDOperand(N, 0);
341}
342
343SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
344 SDNode *&N = ExternalSymbols[Sym];
345 if (N) return SDOperand(N, 0);
346 N = new ExternalSymbolSDNode(Sym, VT);
347 AllNodes.push_back(N);
348 return SDOperand(N, 0);
349}
350
351SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, SDOperand N1,
352 SDOperand N2) {
353 // These setcc operations always fold.
354 switch (Cond) {
355 default: break;
356 case ISD::SETFALSE:
357 case ISD::SETFALSE2: return getConstant(0, MVT::i1);
358 case ISD::SETTRUE:
359 case ISD::SETTRUE2: return getConstant(1, MVT::i1);
360 }
361
362 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val))
363 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
364 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
365
366 // Sign extend the operands if required
367 if (ISD::isSignedIntSetCC(Cond)) {
368 C1 = N1C->getSignExtended();
369 C2 = N2C->getSignExtended();
370 }
371
372 switch (Cond) {
373 default: assert(0 && "Unknown integer setcc!");
374 case ISD::SETEQ: return getConstant(C1 == C2, MVT::i1);
375 case ISD::SETNE: return getConstant(C1 != C2, MVT::i1);
376 case ISD::SETULT: return getConstant(C1 < C2, MVT::i1);
377 case ISD::SETUGT: return getConstant(C1 > C2, MVT::i1);
378 case ISD::SETULE: return getConstant(C1 <= C2, MVT::i1);
379 case ISD::SETUGE: return getConstant(C1 >= C2, MVT::i1);
Chris Lattner87ae6ae2005-01-10 01:16:03 +0000380 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
381 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, MVT::i1);
382 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, MVT::i1);
383 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, MVT::i1);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000384 }
385 } else {
386 // Ensure that the constant occurs on the RHS.
387 Cond = ISD::getSetCCSwappedOperands(Cond);
388 std::swap(N1, N2);
389 }
390
391 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
392 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
393 double C1 = N1C->getValue(), C2 = N2C->getValue();
394
395 switch (Cond) {
396 default: break; // FIXME: Implement the rest of these!
397 case ISD::SETEQ: return getConstant(C1 == C2, MVT::i1);
398 case ISD::SETNE: return getConstant(C1 != C2, MVT::i1);
399 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
400 case ISD::SETGT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
401 case ISD::SETLE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
402 case ISD::SETGE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
403 }
404 } else {
405 // Ensure that the constant occurs on the RHS.
406 Cond = ISD::getSetCCSwappedOperands(Cond);
407 std::swap(N1, N2);
408 }
409
410 if (N1 == N2) {
411 // We can always fold X == Y for integer setcc's.
412 if (MVT::isInteger(N1.getValueType()))
413 return getConstant(ISD::isTrueWhenEqual(Cond), MVT::i1);
414 unsigned UOF = ISD::getUnorderedFlavor(Cond);
415 if (UOF == 2) // FP operators that are undefined on NaNs.
416 return getConstant(ISD::isTrueWhenEqual(Cond), MVT::i1);
417 if (UOF == ISD::isTrueWhenEqual(Cond))
418 return getConstant(UOF, MVT::i1);
419 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
420 // if it is not already.
421 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
422 }
423
Chris Lattner5cdcc582005-01-09 20:52:51 +0000424 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000425 MVT::isInteger(N1.getValueType())) {
426 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
427 N1.getOpcode() == ISD::XOR) {
428 // Simplify (X+Y) == (X+Z) --> Y == Z
429 if (N1.getOpcode() == N2.getOpcode()) {
430 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner5cdcc582005-01-09 20:52:51 +0000431 return getSetCC(Cond, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000432 if (N1.getOperand(1) == N2.getOperand(1))
433 return getSetCC(Cond, N1.getOperand(0), N2.getOperand(0));
434 if (isCommutativeBinOp(N1.getOpcode())) {
435 // If X op Y == Y op X, try other combinations.
436 if (N1.getOperand(0) == N2.getOperand(1))
437 return getSetCC(Cond, N1.getOperand(1), N2.getOperand(0));
438 if (N1.getOperand(1) == N2.getOperand(0))
439 return getSetCC(Cond, N1.getOperand(1), N2.getOperand(1));
440 }
441 }
442
443 // Simplify (X+Z) == X --> Z == 0
444 if (N1.getOperand(0) == N2)
445 return getSetCC(Cond, N1.getOperand(1),
446 getConstant(0, N1.getValueType()));
447 if (N1.getOperand(1) == N2) {
448 if (isCommutativeBinOp(N1.getOpcode()))
449 return getSetCC(Cond, N1.getOperand(0),
450 getConstant(0, N1.getValueType()));
451 else {
452 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
453 // (Z-X) == X --> Z == X<<1
454 return getSetCC(Cond, N1.getOperand(0),
455 getNode(ISD::SHL, N2.getValueType(),
456 N2, getConstant(1, MVT::i8)));
457 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000458 }
459 }
460
Chris Lattner68dc3102005-01-10 02:03:02 +0000461 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
462 N2.getOpcode() == ISD::XOR) {
463 // Simplify X == (X+Z) --> Z == 0
464 if (N2.getOperand(0) == N1)
465 return getSetCC(Cond, N2.getOperand(1),
466 getConstant(0, N2.getValueType()));
467 else if (N2.getOperand(1) == N1)
468 return getSetCC(Cond, N2.getOperand(0),
469 getConstant(0, N2.getValueType()));
470 }
471 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000472
473 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2), Cond)];
474 if (N) return SDOperand(N, 0);
475 N = new SetCCSDNode(Cond, N1, N2);
476 AllNodes.push_back(N);
477 return SDOperand(N, 0);
478}
479
480
481
482/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000483///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000484SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
485 SDNode *N = new SDNode(Opcode, VT);
486 AllNodes.push_back(N);
487 return SDOperand(N, 0);
488}
489
490static const Type *getTypeFor(MVT::ValueType VT) {
491 switch (VT) {
492 default: assert(0 && "Unknown MVT!");
493 case MVT::i1: return Type::BoolTy;
494 case MVT::i8: return Type::UByteTy;
495 case MVT::i16: return Type::UShortTy;
496 case MVT::i32: return Type::UIntTy;
497 case MVT::i64: return Type::ULongTy;
498 case MVT::f32: return Type::FloatTy;
499 case MVT::f64: return Type::DoubleTy;
Chris Lattner78ec3112003-08-11 14:57:33 +0000500 }
501}
502
Chris Lattnerc3aae252005-01-07 07:46:32 +0000503SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
504 SDOperand Operand) {
505 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
506 uint64_t Val = C->getValue();
507 switch (Opcode) {
508 default: break;
509 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
510 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
511 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000512 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
513 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000514 }
515 }
516
517 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
518 switch (Opcode) {
519 case ISD::FP_ROUND:
520 case ISD::FP_EXTEND:
521 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000522 case ISD::FP_TO_SINT:
523 return getConstant((int64_t)C->getValue(), VT);
524 case ISD::FP_TO_UINT:
525 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000526 }
527
528 unsigned OpOpcode = Operand.Val->getOpcode();
529 switch (Opcode) {
530 case ISD::SIGN_EXTEND:
531 if (Operand.getValueType() == VT) return Operand; // noop extension
532 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
533 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
534 break;
535 case ISD::ZERO_EXTEND:
536 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner2f0ca792005-01-12 18:51:15 +0000537 if (OpOpcode == ISD::ZERO_EXTEND)
538 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000539 break;
540 case ISD::TRUNCATE:
541 if (Operand.getValueType() == VT) return Operand; // noop truncate
542 if (OpOpcode == ISD::TRUNCATE)
543 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000544 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
545 // If the source is smaller than the dest, we still need an extend.
546 if (Operand.Val->getOperand(0).getValueType() < VT)
547 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
548 else if (Operand.Val->getOperand(0).getValueType() > VT)
549 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
550 else
551 return Operand.Val->getOperand(0);
552 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000553 break;
554 }
555
556 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
557 if (N) return SDOperand(N, 0);
558 N = new SDNode(Opcode, Operand);
559 N->setValueTypes(VT);
560 AllNodes.push_back(N);
561 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000562}
563
Chris Lattnerc3aae252005-01-07 07:46:32 +0000564SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
565 SDOperand N1, SDOperand N2) {
566 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
567 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
568 if (N1C) {
569 if (N2C) {
570 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
571 switch (Opcode) {
572 case ISD::ADD: return getConstant(C1 + C2, VT);
573 case ISD::SUB: return getConstant(C1 - C2, VT);
574 case ISD::MUL: return getConstant(C1 * C2, VT);
575 case ISD::UDIV:
576 if (C2) return getConstant(C1 / C2, VT);
577 break;
578 case ISD::UREM :
579 if (C2) return getConstant(C1 % C2, VT);
580 break;
581 case ISD::SDIV :
582 if (C2) return getConstant(N1C->getSignExtended() /
583 N2C->getSignExtended(), VT);
584 break;
585 case ISD::SREM :
586 if (C2) return getConstant(N1C->getSignExtended() %
587 N2C->getSignExtended(), VT);
588 break;
589 case ISD::AND : return getConstant(C1 & C2, VT);
590 case ISD::OR : return getConstant(C1 | C2, VT);
591 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000592 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
593 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
594 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000595 default: break;
596 }
597
598 } else { // Cannonicalize constant to RHS if commutative
599 if (isCommutativeBinOp(Opcode)) {
600 std::swap(N1C, N2C);
601 std::swap(N1, N2);
602 }
603 }
604 }
605
606 if (N2C) {
607 uint64_t C2 = N2C->getValue();
608
609 switch (Opcode) {
610 case ISD::ADD:
611 if (!C2) return N1; // add X, 0 -> X
612 break;
613 case ISD::SUB:
614 if (!C2) return N1; // sub X, 0 -> X
615 break;
616 case ISD::MUL:
617 if (!C2) return N2; // mul X, 0 -> 0
618 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
619 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
620
621 // FIXME: This should only be done if the target supports shift
622 // operations.
623 if ((C2 & C2-1) == 0) {
624 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
625 return getNode(ISD::SHL, VT, N1, ShAmt);
626 }
627 break;
628
629 case ISD::UDIV:
630 // FIXME: This should only be done if the target supports shift
631 // operations.
632 if ((C2 & C2-1) == 0 && C2) {
633 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
634 return getNode(ISD::SRL, VT, N1, ShAmt);
635 }
636 break;
637
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000638 case ISD::SHL:
639 case ISD::SRL:
640 case ISD::SRA:
641 if (C2 == 0) return N1;
642 break;
643
Chris Lattnerc3aae252005-01-07 07:46:32 +0000644 case ISD::AND:
645 if (!C2) return N2; // X and 0 -> 0
646 if (N2C->isAllOnesValue())
647 return N1; // X and -1 -> X
648 break;
649 case ISD::OR:
650 if (!C2)return N1; // X or 0 -> X
651 if (N2C->isAllOnesValue())
652 return N2; // X or -1 -> -1
653 break;
654 case ISD::XOR:
655 if (!C2) return N1; // X xor 0 -> X
656 if (N2C->isAllOnesValue()) {
657 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
658 // !(X op Y) -> (X !op Y)
659 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
660 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
661 SetCC->getOperand(0), SetCC->getOperand(1));
662 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
663 SDNode *Op = N1.Val;
664 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
665 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
666 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
667 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
668 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
669 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
670 if (Op->getOpcode() == ISD::AND)
671 return getNode(ISD::OR, VT, LHS, RHS);
672 return getNode(ISD::AND, VT, LHS, RHS);
673 }
674 }
675 // X xor -1 -> not(x) ?
676 }
677 break;
678 }
679
680 // Reassociate ((X op C1) op C2) if possible.
681 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
682 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +0000683 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000684 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
685 }
686
687 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
688 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
689 if (N1CFP)
690 if (N2CFP) {
691 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
692 switch (Opcode) {
693 case ISD::ADD: return getConstantFP(C1 + C2, VT);
694 case ISD::SUB: return getConstantFP(C1 - C2, VT);
695 case ISD::MUL: return getConstantFP(C1 * C2, VT);
696 case ISD::SDIV:
697 if (C2) return getConstantFP(C1 / C2, VT);
698 break;
699 case ISD::SREM :
700 if (C2) return getConstantFP(fmod(C1, C2), VT);
701 break;
702 default: break;
703 }
704
705 } else { // Cannonicalize constant to RHS if commutative
706 if (isCommutativeBinOp(Opcode)) {
707 std::swap(N1CFP, N2CFP);
708 std::swap(N1, N2);
709 }
710 }
711
712 // Finally, fold operations that do not require constants.
713 switch (Opcode) {
714 case ISD::AND:
715 case ISD::OR:
716 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
717 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
718 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
719 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
720 ISD::CondCode Op2 = RHS->getCondition();
721
722 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
723 if (LL == RR && LR == RL) {
724 Op2 = ISD::getSetCCSwappedOperands(Op2);
725 goto MatchedBackwards;
726 }
727
728 if (LL == RL && LR == RR) {
729 MatchedBackwards:
730 ISD::CondCode Result;
731 bool isInteger = MVT::isInteger(LL.getValueType());
732 if (Opcode == ISD::OR)
733 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
734 isInteger);
735 else
736 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
737 isInteger);
738 if (Result != ISD::SETCC_INVALID)
739 return getSetCC(Result, LL, LR);
740 }
741 }
742 break;
743 case ISD::XOR:
744 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
745 break;
Chris Lattnerabd21822005-01-09 20:09:57 +0000746 case ISD::SUB:
747 if (N1.getOpcode() == ISD::ADD) {
748 if (N1.Val->getOperand(0) == N2)
749 return N1.Val->getOperand(1); // (A+B)-A == B
750 if (N1.Val->getOperand(1) == N2)
751 return N1.Val->getOperand(0); // (A+B)-B == A
752 }
753 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000754 }
755
756 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
757 if (N) return SDOperand(N, 0);
758 N = new SDNode(Opcode, N1, N2);
759 N->setValueTypes(VT);
760
761 AllNodes.push_back(N);
762 return SDOperand(N, 0);
763}
764
765SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
766 SDOperand Chain, SDOperand Ptr) {
767 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
768 if (N) return SDOperand(N, 0);
769 N = new SDNode(ISD::LOAD, Chain, Ptr);
770
771 // Loads have a token chain.
772 N->setValueTypes(VT, MVT::Other);
773 AllNodes.push_back(N);
774 return SDOperand(N, 0);
775}
776
777
778SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
779 SDOperand N1, SDOperand N2, SDOperand N3) {
780 // Perform various simplifications.
781 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
782 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
783 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
784 switch (Opcode) {
785 case ISD::SELECT:
786 if (N1C)
787 if (N1C->getValue())
788 return N2; // select true, X, Y -> X
789 else
790 return N3; // select false, X, Y -> Y
791
792 if (N2 == N3) return N2; // select C, X, X -> X
793
794 if (VT == MVT::i1) { // Boolean SELECT
795 if (N2C) {
796 if (N3C) {
797 if (N2C->getValue()) // select C, 1, 0 -> C
798 return N1;
799 return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
800 }
801
802 if (N2C->getValue()) // select C, 1, X -> C | X
803 return getNode(ISD::OR, VT, N1, N3);
804 else // select C, 0, X -> ~C & X
805 return getNode(ISD::AND, VT,
806 getNode(ISD::XOR, N1.getValueType(), N1,
807 getConstant(1, N1.getValueType())), N3);
808 } else if (N3C) {
809 if (N3C->getValue()) // select C, X, 1 -> ~C | X
810 return getNode(ISD::OR, VT,
811 getNode(ISD::XOR, N1.getValueType(), N1,
812 getConstant(1, N1.getValueType())), N2);
813 else // select C, X, 0 -> C & X
814 return getNode(ISD::AND, VT, N1, N2);
815 }
816 }
817
818 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +0000819 case ISD::BRCOND:
820 if (N2C)
821 if (N2C->getValue()) // Unconditional branch
822 return getNode(ISD::BR, MVT::Other, N1, N3);
823 else
824 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +0000825 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000826 }
827
828 SDNode *N = new SDNode(Opcode, N1, N2, N3);
829 switch (Opcode) {
830 default:
831 N->setValueTypes(VT);
832 break;
833 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
834 N->setValueTypes(VT, MVT::Other);
835 break;
836 }
837
838 // FIXME: memoize NODES
839 AllNodes.push_back(N);
840 return SDOperand(N, 0);
841}
842
843SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
844 std::vector<SDOperand> &Children) {
845 switch (Children.size()) {
846 case 0: return getNode(Opcode, VT);
847 case 1: return getNode(Opcode, VT, Children[0]);
848 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
849 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
850 default:
851 // FIXME: MEMOIZE!!
852 SDNode *N = new SDNode(Opcode, Children);
853 N->setValueTypes(VT);
854 AllNodes.push_back(N);
855 return SDOperand(N, 0);
856 }
857}
858
Chris Lattner2ee743f2005-01-14 22:08:15 +0000859SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
860 SDOperand N2, MVT::ValueType EVT) {
861 switch (Opcode) {
862 default: assert(0 && "Bad opcode for this accessor!");
863 case ISD::EXTLOAD:
864 case ISD::SEXTLOAD:
865 case ISD::ZEXTLOAD:
866 // If they are asking for an extending loat from/to the same thing, return a
867 // normal load.
868 if (VT == EVT)
869 return getNode(ISD::LOAD, VT, N1, N2);
870 assert(EVT < VT && "Should only be an extending load, not truncating!");
871 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
872 "Cannot sign/zero extend a FP load!");
873 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
874 "Cannot convert from FP to Int or Int -> FP!");
875 break;
876 }
877
878 EVTStruct NN;
879 NN.Opcode = Opcode;
880 NN.VT = VT;
881 NN.EVT = EVT;
882 NN.Ops.push_back(N1);
883 NN.Ops.push_back(N2);
884
885 SDNode *&N = MVTSDNodes[NN];
886 if (N) return SDOperand(N, 0);
Chris Lattner69a52152005-01-14 22:38:01 +0000887 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner2ee743f2005-01-14 22:08:15 +0000888 AllNodes.push_back(N);
889 return SDOperand(N, 0);
890}
891
892SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
893 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
894 switch (Opcode) {
895 default: assert(0 && "Bad opcode for this accessor!");
896 case ISD::TRUNCSTORE:
897 if (N1.getValueType() == EVT) // Normal store?
898 return getNode(ISD::STORE, VT, N1, N2, N3);
899 assert(N1.getValueType() > EVT && "Not a truncation?");
900 assert(MVT::isInteger(N1.getValueType()) == MVT::isInteger(EVT) &&
901 "Can't do FP-INT conversion!");
902 break;
903 }
904
905 EVTStruct NN;
906 NN.Opcode = Opcode;
907 NN.VT = VT;
908 NN.EVT = EVT;
909 NN.Ops.push_back(N1);
910 NN.Ops.push_back(N2);
911 NN.Ops.push_back(N3);
912
913 SDNode *&N = MVTSDNodes[NN];
914 if (N) return SDOperand(N, 0);
915 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
916 AllNodes.push_back(N);
917 return SDOperand(N, 0);
918}
919
920
Chris Lattner5c884562005-01-12 18:37:47 +0000921/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
922/// indicated value. This method ignores uses of other values defined by this
923/// operation.
924bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
925 assert(Value < getNumValues() && "Bad value!");
926
927 // If there is only one value, this is easy.
928 if (getNumValues() == 1)
929 return use_size() == NUses;
930 if (Uses.size() < NUses) return false;
931
932 SDOperand TheValue(this, Value);
933
934 std::set<SDNode*> UsersHandled;
935
936 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
937 UI != E; ++UI) {
938 SDNode *User = *UI;
939 if (User->getNumOperands() == 1 ||
940 UsersHandled.insert(User).second) // First time we've seen this?
941 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
942 if (User->getOperand(i) == TheValue) {
943 if (NUses == 0)
944 return false; // too many uses
945 --NUses;
946 }
947 }
948
949 // Found exactly the right number of uses?
950 return NUses == 0;
951}
952
953
Chris Lattnerd75f19f2005-01-10 23:25:25 +0000954const char *SDNode::getOperationName() const {
955 switch (getOpcode()) {
956 default: return "<<Unknown>>";
957 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +0000958 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +0000959 case ISD::Constant: return "Constant";
960 case ISD::ConstantFP: return "ConstantFP";
961 case ISD::GlobalAddress: return "GlobalAddress";
962 case ISD::FrameIndex: return "FrameIndex";
963 case ISD::BasicBlock: return "BasicBlock";
964 case ISD::ExternalSymbol: return "ExternalSymbol";
965 case ISD::ConstantPool: return "ConstantPoolIndex";
966 case ISD::CopyToReg: return "CopyToReg";
967 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +0000968 case ISD::ImplicitDef: return "ImplicitDef";
Chris Lattnerc3aae252005-01-07 07:46:32 +0000969
Chris Lattnerd75f19f2005-01-10 23:25:25 +0000970 case ISD::ADD: return "add";
971 case ISD::SUB: return "sub";
972 case ISD::MUL: return "mul";
973 case ISD::SDIV: return "sdiv";
974 case ISD::UDIV: return "udiv";
975 case ISD::SREM: return "srem";
976 case ISD::UREM: return "urem";
977 case ISD::AND: return "and";
978 case ISD::OR: return "or";
979 case ISD::XOR: return "xor";
980 case ISD::SHL: return "shl";
981 case ISD::SRA: return "sra";
982 case ISD::SRL: return "srl";
983
984 case ISD::SELECT: return "select";
985 case ISD::ADDC: return "addc";
986 case ISD::SUBB: return "subb";
987
988 // Conversion operators.
989 case ISD::SIGN_EXTEND: return "sign_extend";
990 case ISD::ZERO_EXTEND: return "zero_extend";
991 case ISD::TRUNCATE: return "truncate";
992 case ISD::FP_ROUND: return "fp_round";
993 case ISD::FP_EXTEND: return "fp_extend";
994
995 case ISD::SINT_TO_FP: return "sint_to_fp";
996 case ISD::UINT_TO_FP: return "uint_to_fp";
997 case ISD::FP_TO_SINT: return "fp_to_sint";
998 case ISD::FP_TO_UINT: return "fp_to_uint";
999
1000 // Control flow instructions
1001 case ISD::BR: return "br";
1002 case ISD::BRCOND: return "brcond";
1003 case ISD::RET: return "ret";
1004 case ISD::CALL: return "call";
1005 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1006 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1007
1008 // Other operators
1009 case ISD::LOAD: return "load";
1010 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001011 case ISD::EXTLOAD: return "extload";
1012 case ISD::SEXTLOAD: return "sextload";
1013 case ISD::ZEXTLOAD: return "zextload";
1014 case ISD::TRUNCSTORE: return "truncstore";
1015
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001016 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1017 case ISD::EXTRACT_ELEMENT: return "extract_element";
1018 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001019 case ISD::MEMSET: return "memset";
1020 case ISD::MEMCPY: return "memcpy";
1021 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001022
1023 case ISD::SETCC:
1024 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1025 switch (SetCC->getCondition()) {
1026 default: assert(0 && "Unknown setcc condition!");
1027 case ISD::SETOEQ: return "setcc:setoeq";
1028 case ISD::SETOGT: return "setcc:setogt";
1029 case ISD::SETOGE: return "setcc:setoge";
1030 case ISD::SETOLT: return "setcc:setolt";
1031 case ISD::SETOLE: return "setcc:setole";
1032 case ISD::SETONE: return "setcc:setone";
1033
1034 case ISD::SETO: return "setcc:seto";
1035 case ISD::SETUO: return "setcc:setuo";
1036 case ISD::SETUEQ: return "setcc:setue";
1037 case ISD::SETUGT: return "setcc:setugt";
1038 case ISD::SETUGE: return "setcc:setuge";
1039 case ISD::SETULT: return "setcc:setult";
1040 case ISD::SETULE: return "setcc:setule";
1041 case ISD::SETUNE: return "setcc:setune";
1042
1043 case ISD::SETEQ: return "setcc:seteq";
1044 case ISD::SETGT: return "setcc:setgt";
1045 case ISD::SETGE: return "setcc:setge";
1046 case ISD::SETLT: return "setcc:setlt";
1047 case ISD::SETLE: return "setcc:setle";
1048 case ISD::SETNE: return "setcc:setne";
1049 }
1050 }
1051}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052
1053void SDNode::dump() const {
1054 std::cerr << (void*)this << ": ";
1055
1056 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1057 if (i) std::cerr << ",";
1058 switch (getValueType(i)) {
1059 default: assert(0 && "Unknown value type!");
1060 case MVT::i1: std::cerr << "i1"; break;
1061 case MVT::i8: std::cerr << "i8"; break;
1062 case MVT::i16: std::cerr << "i16"; break;
1063 case MVT::i32: std::cerr << "i32"; break;
1064 case MVT::i64: std::cerr << "i64"; break;
1065 case MVT::f32: std::cerr << "f32"; break;
1066 case MVT::f64: std::cerr << "f64"; break;
1067 case MVT::Other: std::cerr << "ch"; break;
1068 }
1069 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001070 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071
1072 std::cerr << " ";
1073 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1074 if (i) std::cerr << ", ";
1075 std::cerr << (void*)getOperand(i).Val;
1076 if (unsigned RN = getOperand(i).ResNo)
1077 std::cerr << ":" << RN;
1078 }
1079
1080 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1081 std::cerr << "<" << CSDN->getValue() << ">";
1082 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1083 std::cerr << "<" << CSDN->getValue() << ">";
1084 } else if (const GlobalAddressSDNode *GADN =
1085 dyn_cast<GlobalAddressSDNode>(this)) {
1086 std::cerr << "<";
1087 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1088 } else if (const FrameIndexSDNode *FIDN =
1089 dyn_cast<FrameIndexSDNode>(this)) {
1090 std::cerr << "<" << FIDN->getIndex() << ">";
1091 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1092 std::cerr << "<" << CP->getIndex() << ">";
1093 } else if (const BasicBlockSDNode *BBDN =
1094 dyn_cast<BasicBlockSDNode>(this)) {
1095 std::cerr << "<";
1096 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1097 if (LBB)
1098 std::cerr << LBB->getName() << " ";
1099 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001100 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001101 std::cerr << "<reg #" << C2V->getReg() << ">";
1102 } else if (const ExternalSymbolSDNode *ES =
1103 dyn_cast<ExternalSymbolSDNode>(this)) {
1104 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106}
1107
Chris Lattnerea946cd2005-01-09 20:38:33 +00001108static void DumpNodes(SDNode *N, unsigned indent) {
1109 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1110 if (N->getOperand(i).Val->hasOneUse())
1111 DumpNodes(N->getOperand(i).Val, indent+2);
1112 else
1113 std::cerr << "\n" << std::string(indent+2, ' ')
1114 << (void*)N->getOperand(i).Val << ": <multiple use>";
1115
1116
1117 std::cerr << "\n" << std::string(indent, ' ');
1118 N->dump();
1119}
1120
Chris Lattnerc3aae252005-01-07 07:46:32 +00001121void SelectionDAG::dump() const {
1122 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001123 std::vector<SDNode*> Nodes(AllNodes);
1124 std::sort(Nodes.begin(), Nodes.end());
1125
1126 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001127 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1128 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001129 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001130
1131 DumpNodes(getRoot().Val, 2);
1132
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133 std::cerr << "\n\n";
1134}
1135