blob: d3d44a82e080316371842a65965d35f02ae61473 [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 Lattner859157d2005-01-15 06:17:04 +0000223 case ISD::TRUNCSTORE:
224 case ISD::SIGN_EXTEND_INREG:
225 case ISD::ZERO_EXTEND_INREG:
226 case ISD::FP_ROUND_INREG:
Chris Lattner69a52152005-01-14 22:38:01 +0000227 case ISD::EXTLOAD:
228 case ISD::SEXTLOAD:
229 case ISD::ZEXTLOAD: {
230 EVTStruct NN;
Chris Lattner859157d2005-01-15 06:17:04 +0000231 NN.Opcode = ISD::TRUNCSTORE;
Chris Lattner69a52152005-01-14 22:38:01 +0000232 NN.VT = N->getValueType(0);
233 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner859157d2005-01-15 06:17:04 +0000234 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
235 NN.Ops.push_back(N->getOperand(i));
Chris Lattner69a52152005-01-14 22:38:01 +0000236 MVTSDNodes.erase(NN);
237 break;
238 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000239 default:
240 if (N->getNumOperands() == 1)
241 UnaryOps.erase(std::make_pair(N->getOpcode(),
242 std::make_pair(N->getOperand(0),
243 N->getValueType(0))));
244 else if (N->getNumOperands() == 2)
245 BinaryOps.erase(std::make_pair(N->getOpcode(),
246 std::make_pair(N->getOperand(0),
247 N->getOperand(1))));
248 break;
249 }
250
251 // Next, brutally remove the operand list.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000252 while (!N->Operands.empty()) {
Chris Lattner7c68ec62005-01-07 23:32:00 +0000253 SDNode *O = N->Operands.back().Val;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000254 N->Operands.pop_back();
Chris Lattner7c68ec62005-01-07 23:32:00 +0000255 O->removeUser(N);
256
257 // Now that we removed this operand, see if there are no uses of it left.
258 DeleteNodeIfDead(O, NodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000259 }
260
261 // Remove the node from the nodes set and delete it.
262 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
263 AllNodeSet.erase(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264
265 // Now that the node is gone, check to see if any of the operands of this node
266 // are dead now.
Chris Lattner7c68ec62005-01-07 23:32:00 +0000267 delete N;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000268}
269
270
Chris Lattner78ec3112003-08-11 14:57:33 +0000271SelectionDAG::~SelectionDAG() {
272 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
273 delete AllNodes[i];
274}
275
Chris Lattnerc3aae252005-01-07 07:46:32 +0000276SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
277 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
278 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000279 if (VT != MVT::i64)
280 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
281
Chris Lattnerc3aae252005-01-07 07:46:32 +0000282 SDNode *&N = Constants[std::make_pair(Val, VT)];
283 if (N) return SDOperand(N, 0);
284 N = new ConstantSDNode(Val, VT);
285 AllNodes.push_back(N);
286 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000287}
288
Chris Lattnerc3aae252005-01-07 07:46:32 +0000289SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
290 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
291 if (VT == MVT::f32)
292 Val = (float)Val; // Mask out extra precision.
293
294 SDNode *&N = ConstantFPs[std::make_pair(Val, VT)];
295 if (N) return SDOperand(N, 0);
296 N = new ConstantFPSDNode(Val, VT);
297 AllNodes.push_back(N);
298 return SDOperand(N, 0);
299}
300
301
302
303SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
304 MVT::ValueType VT) {
305 SDNode *&N = GlobalValues[GV];
306 if (N) return SDOperand(N, 0);
307 N = new GlobalAddressSDNode(GV,VT);
308 AllNodes.push_back(N);
309 return SDOperand(N, 0);
310}
311
312SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
313 SDNode *&N = FrameIndices[FI];
314 if (N) return SDOperand(N, 0);
315 N = new FrameIndexSDNode(FI, VT);
316 AllNodes.push_back(N);
317 return SDOperand(N, 0);
318}
319
320SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
321 SDNode *N = ConstantPoolIndices[CPIdx];
322 if (N) return SDOperand(N, 0);
323 N = new ConstantPoolSDNode(CPIdx, VT);
324 AllNodes.push_back(N);
325 return SDOperand(N, 0);
326}
327
328SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
329 SDNode *&N = BBNodes[MBB];
330 if (N) return SDOperand(N, 0);
331 N = new BasicBlockSDNode(MBB);
332 AllNodes.push_back(N);
333 return SDOperand(N, 0);
334}
335
336SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
337 SDNode *&N = ExternalSymbols[Sym];
338 if (N) return SDOperand(N, 0);
339 N = new ExternalSymbolSDNode(Sym, VT);
340 AllNodes.push_back(N);
341 return SDOperand(N, 0);
342}
343
344SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, SDOperand N1,
345 SDOperand N2) {
346 // These setcc operations always fold.
347 switch (Cond) {
348 default: break;
349 case ISD::SETFALSE:
350 case ISD::SETFALSE2: return getConstant(0, MVT::i1);
351 case ISD::SETTRUE:
352 case ISD::SETTRUE2: return getConstant(1, MVT::i1);
353 }
354
355 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val))
356 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
357 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
358
359 // Sign extend the operands if required
360 if (ISD::isSignedIntSetCC(Cond)) {
361 C1 = N1C->getSignExtended();
362 C2 = N2C->getSignExtended();
363 }
364
365 switch (Cond) {
366 default: assert(0 && "Unknown integer setcc!");
367 case ISD::SETEQ: return getConstant(C1 == C2, MVT::i1);
368 case ISD::SETNE: return getConstant(C1 != C2, MVT::i1);
369 case ISD::SETULT: return getConstant(C1 < C2, MVT::i1);
370 case ISD::SETUGT: return getConstant(C1 > C2, MVT::i1);
371 case ISD::SETULE: return getConstant(C1 <= C2, MVT::i1);
372 case ISD::SETUGE: return getConstant(C1 >= C2, MVT::i1);
Chris Lattner87ae6ae2005-01-10 01:16:03 +0000373 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
374 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, MVT::i1);
375 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, MVT::i1);
376 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, MVT::i1);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000377 }
378 } else {
379 // Ensure that the constant occurs on the RHS.
380 Cond = ISD::getSetCCSwappedOperands(Cond);
381 std::swap(N1, N2);
382 }
383
384 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
385 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
386 double C1 = N1C->getValue(), C2 = N2C->getValue();
387
388 switch (Cond) {
389 default: break; // FIXME: Implement the rest of these!
390 case ISD::SETEQ: return getConstant(C1 == C2, MVT::i1);
391 case ISD::SETNE: return getConstant(C1 != C2, MVT::i1);
392 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
393 case ISD::SETGT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
394 case ISD::SETLE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
395 case ISD::SETGE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
396 }
397 } else {
398 // Ensure that the constant occurs on the RHS.
399 Cond = ISD::getSetCCSwappedOperands(Cond);
400 std::swap(N1, N2);
401 }
402
403 if (N1 == N2) {
404 // We can always fold X == Y for integer setcc's.
405 if (MVT::isInteger(N1.getValueType()))
406 return getConstant(ISD::isTrueWhenEqual(Cond), MVT::i1);
407 unsigned UOF = ISD::getUnorderedFlavor(Cond);
408 if (UOF == 2) // FP operators that are undefined on NaNs.
409 return getConstant(ISD::isTrueWhenEqual(Cond), MVT::i1);
410 if (UOF == ISD::isTrueWhenEqual(Cond))
411 return getConstant(UOF, MVT::i1);
412 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
413 // if it is not already.
414 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
415 }
416
Chris Lattner5cdcc582005-01-09 20:52:51 +0000417 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000418 MVT::isInteger(N1.getValueType())) {
419 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
420 N1.getOpcode() == ISD::XOR) {
421 // Simplify (X+Y) == (X+Z) --> Y == Z
422 if (N1.getOpcode() == N2.getOpcode()) {
423 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner5cdcc582005-01-09 20:52:51 +0000424 return getSetCC(Cond, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000425 if (N1.getOperand(1) == N2.getOperand(1))
426 return getSetCC(Cond, N1.getOperand(0), N2.getOperand(0));
427 if (isCommutativeBinOp(N1.getOpcode())) {
428 // If X op Y == Y op X, try other combinations.
429 if (N1.getOperand(0) == N2.getOperand(1))
430 return getSetCC(Cond, N1.getOperand(1), N2.getOperand(0));
431 if (N1.getOperand(1) == N2.getOperand(0))
432 return getSetCC(Cond, N1.getOperand(1), N2.getOperand(1));
433 }
434 }
435
436 // Simplify (X+Z) == X --> Z == 0
437 if (N1.getOperand(0) == N2)
438 return getSetCC(Cond, N1.getOperand(1),
439 getConstant(0, N1.getValueType()));
440 if (N1.getOperand(1) == N2) {
441 if (isCommutativeBinOp(N1.getOpcode()))
442 return getSetCC(Cond, N1.getOperand(0),
443 getConstant(0, N1.getValueType()));
444 else {
445 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
446 // (Z-X) == X --> Z == X<<1
447 return getSetCC(Cond, N1.getOperand(0),
448 getNode(ISD::SHL, N2.getValueType(),
449 N2, getConstant(1, MVT::i8)));
450 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000451 }
452 }
453
Chris Lattner68dc3102005-01-10 02:03:02 +0000454 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
455 N2.getOpcode() == ISD::XOR) {
456 // Simplify X == (X+Z) --> Z == 0
457 if (N2.getOperand(0) == N1)
458 return getSetCC(Cond, N2.getOperand(1),
459 getConstant(0, N2.getValueType()));
460 else if (N2.getOperand(1) == N1)
461 return getSetCC(Cond, N2.getOperand(0),
462 getConstant(0, N2.getValueType()));
463 }
464 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000465
466 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2), Cond)];
467 if (N) return SDOperand(N, 0);
468 N = new SetCCSDNode(Cond, N1, N2);
469 AllNodes.push_back(N);
470 return SDOperand(N, 0);
471}
472
473
474
475/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000476///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000477SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
478 SDNode *N = new SDNode(Opcode, VT);
479 AllNodes.push_back(N);
480 return SDOperand(N, 0);
481}
482
Chris Lattnerc3aae252005-01-07 07:46:32 +0000483SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
484 SDOperand Operand) {
485 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
486 uint64_t Val = C->getValue();
487 switch (Opcode) {
488 default: break;
489 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
490 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
491 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000492 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
493 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000494 }
495 }
496
497 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
498 switch (Opcode) {
499 case ISD::FP_ROUND:
500 case ISD::FP_EXTEND:
501 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000502 case ISD::FP_TO_SINT:
503 return getConstant((int64_t)C->getValue(), VT);
504 case ISD::FP_TO_UINT:
505 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000506 }
507
508 unsigned OpOpcode = Operand.Val->getOpcode();
509 switch (Opcode) {
510 case ISD::SIGN_EXTEND:
511 if (Operand.getValueType() == VT) return Operand; // noop extension
512 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
513 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
514 break;
515 case ISD::ZERO_EXTEND:
516 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner2f0ca792005-01-12 18:51:15 +0000517 if (OpOpcode == ISD::ZERO_EXTEND)
518 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000519 break;
520 case ISD::TRUNCATE:
521 if (Operand.getValueType() == VT) return Operand; // noop truncate
522 if (OpOpcode == ISD::TRUNCATE)
523 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000524 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
525 // If the source is smaller than the dest, we still need an extend.
526 if (Operand.Val->getOperand(0).getValueType() < VT)
527 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
528 else if (Operand.Val->getOperand(0).getValueType() > VT)
529 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
530 else
531 return Operand.Val->getOperand(0);
532 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000533 break;
534 }
535
536 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
537 if (N) return SDOperand(N, 0);
538 N = new SDNode(Opcode, Operand);
539 N->setValueTypes(VT);
540 AllNodes.push_back(N);
541 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000542}
543
Chris Lattnerc3aae252005-01-07 07:46:32 +0000544SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
545 SDOperand N1, SDOperand N2) {
546 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
547 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
548 if (N1C) {
549 if (N2C) {
550 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
551 switch (Opcode) {
552 case ISD::ADD: return getConstant(C1 + C2, VT);
553 case ISD::SUB: return getConstant(C1 - C2, VT);
554 case ISD::MUL: return getConstant(C1 * C2, VT);
555 case ISD::UDIV:
556 if (C2) return getConstant(C1 / C2, VT);
557 break;
558 case ISD::UREM :
559 if (C2) return getConstant(C1 % C2, VT);
560 break;
561 case ISD::SDIV :
562 if (C2) return getConstant(N1C->getSignExtended() /
563 N2C->getSignExtended(), VT);
564 break;
565 case ISD::SREM :
566 if (C2) return getConstant(N1C->getSignExtended() %
567 N2C->getSignExtended(), VT);
568 break;
569 case ISD::AND : return getConstant(C1 & C2, VT);
570 case ISD::OR : return getConstant(C1 | C2, VT);
571 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000572 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
573 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
574 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000575 default: break;
576 }
577
578 } else { // Cannonicalize constant to RHS if commutative
579 if (isCommutativeBinOp(Opcode)) {
580 std::swap(N1C, N2C);
581 std::swap(N1, N2);
582 }
583 }
584 }
585
586 if (N2C) {
587 uint64_t C2 = N2C->getValue();
588
589 switch (Opcode) {
590 case ISD::ADD:
591 if (!C2) return N1; // add X, 0 -> X
592 break;
593 case ISD::SUB:
594 if (!C2) return N1; // sub X, 0 -> X
595 break;
596 case ISD::MUL:
597 if (!C2) return N2; // mul X, 0 -> 0
598 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
599 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
600
601 // FIXME: This should only be done if the target supports shift
602 // operations.
603 if ((C2 & C2-1) == 0) {
604 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
605 return getNode(ISD::SHL, VT, N1, ShAmt);
606 }
607 break;
608
609 case ISD::UDIV:
610 // FIXME: This should only be done if the target supports shift
611 // operations.
612 if ((C2 & C2-1) == 0 && C2) {
613 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
614 return getNode(ISD::SRL, VT, N1, ShAmt);
615 }
616 break;
617
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000618 case ISD::SHL:
619 case ISD::SRL:
620 case ISD::SRA:
621 if (C2 == 0) return N1;
622 break;
623
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624 case ISD::AND:
625 if (!C2) return N2; // X and 0 -> 0
626 if (N2C->isAllOnesValue())
627 return N1; // X and -1 -> X
628 break;
629 case ISD::OR:
630 if (!C2)return N1; // X or 0 -> X
631 if (N2C->isAllOnesValue())
632 return N2; // X or -1 -> -1
633 break;
634 case ISD::XOR:
635 if (!C2) return N1; // X xor 0 -> X
636 if (N2C->isAllOnesValue()) {
637 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
638 // !(X op Y) -> (X !op Y)
639 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
640 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
641 SetCC->getOperand(0), SetCC->getOperand(1));
642 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
643 SDNode *Op = N1.Val;
644 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
645 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
646 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
647 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
648 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
649 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
650 if (Op->getOpcode() == ISD::AND)
651 return getNode(ISD::OR, VT, LHS, RHS);
652 return getNode(ISD::AND, VT, LHS, RHS);
653 }
654 }
655 // X xor -1 -> not(x) ?
656 }
657 break;
658 }
659
660 // Reassociate ((X op C1) op C2) if possible.
661 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
662 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +0000663 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000664 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
665 }
666
667 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
668 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
669 if (N1CFP)
670 if (N2CFP) {
671 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
672 switch (Opcode) {
673 case ISD::ADD: return getConstantFP(C1 + C2, VT);
674 case ISD::SUB: return getConstantFP(C1 - C2, VT);
675 case ISD::MUL: return getConstantFP(C1 * C2, VT);
676 case ISD::SDIV:
677 if (C2) return getConstantFP(C1 / C2, VT);
678 break;
679 case ISD::SREM :
680 if (C2) return getConstantFP(fmod(C1, C2), VT);
681 break;
682 default: break;
683 }
684
685 } else { // Cannonicalize constant to RHS if commutative
686 if (isCommutativeBinOp(Opcode)) {
687 std::swap(N1CFP, N2CFP);
688 std::swap(N1, N2);
689 }
690 }
691
692 // Finally, fold operations that do not require constants.
693 switch (Opcode) {
694 case ISD::AND:
695 case ISD::OR:
696 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
697 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
698 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
699 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
700 ISD::CondCode Op2 = RHS->getCondition();
701
702 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
703 if (LL == RR && LR == RL) {
704 Op2 = ISD::getSetCCSwappedOperands(Op2);
705 goto MatchedBackwards;
706 }
707
708 if (LL == RL && LR == RR) {
709 MatchedBackwards:
710 ISD::CondCode Result;
711 bool isInteger = MVT::isInteger(LL.getValueType());
712 if (Opcode == ISD::OR)
713 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
714 isInteger);
715 else
716 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
717 isInteger);
718 if (Result != ISD::SETCC_INVALID)
719 return getSetCC(Result, LL, LR);
720 }
721 }
722 break;
723 case ISD::XOR:
724 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
725 break;
Chris Lattnerabd21822005-01-09 20:09:57 +0000726 case ISD::SUB:
727 if (N1.getOpcode() == ISD::ADD) {
728 if (N1.Val->getOperand(0) == N2)
729 return N1.Val->getOperand(1); // (A+B)-A == B
730 if (N1.Val->getOperand(1) == N2)
731 return N1.Val->getOperand(0); // (A+B)-B == A
732 }
733 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000734 }
735
736 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
737 if (N) return SDOperand(N, 0);
738 N = new SDNode(Opcode, N1, N2);
739 N->setValueTypes(VT);
740
741 AllNodes.push_back(N);
742 return SDOperand(N, 0);
743}
744
745SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
746 SDOperand Chain, SDOperand Ptr) {
747 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
748 if (N) return SDOperand(N, 0);
749 N = new SDNode(ISD::LOAD, Chain, Ptr);
750
751 // Loads have a token chain.
752 N->setValueTypes(VT, MVT::Other);
753 AllNodes.push_back(N);
754 return SDOperand(N, 0);
755}
756
757
758SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
759 SDOperand N1, SDOperand N2, SDOperand N3) {
760 // Perform various simplifications.
761 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
762 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
763 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
764 switch (Opcode) {
765 case ISD::SELECT:
766 if (N1C)
767 if (N1C->getValue())
768 return N2; // select true, X, Y -> X
769 else
770 return N3; // select false, X, Y -> Y
771
772 if (N2 == N3) return N2; // select C, X, X -> X
773
774 if (VT == MVT::i1) { // Boolean SELECT
775 if (N2C) {
776 if (N3C) {
777 if (N2C->getValue()) // select C, 1, 0 -> C
778 return N1;
779 return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
780 }
781
782 if (N2C->getValue()) // select C, 1, X -> C | X
783 return getNode(ISD::OR, VT, N1, N3);
784 else // select C, 0, X -> ~C & X
785 return getNode(ISD::AND, VT,
786 getNode(ISD::XOR, N1.getValueType(), N1,
787 getConstant(1, N1.getValueType())), N3);
788 } else if (N3C) {
789 if (N3C->getValue()) // select C, X, 1 -> ~C | X
790 return getNode(ISD::OR, VT,
791 getNode(ISD::XOR, N1.getValueType(), N1,
792 getConstant(1, N1.getValueType())), N2);
793 else // select C, X, 0 -> C & X
794 return getNode(ISD::AND, VT, N1, N2);
795 }
796 }
797
798 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +0000799 case ISD::BRCOND:
800 if (N2C)
801 if (N2C->getValue()) // Unconditional branch
802 return getNode(ISD::BR, MVT::Other, N1, N3);
803 else
804 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +0000805 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000806 }
807
808 SDNode *N = new SDNode(Opcode, N1, N2, N3);
809 switch (Opcode) {
810 default:
811 N->setValueTypes(VT);
812 break;
813 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
814 N->setValueTypes(VT, MVT::Other);
815 break;
816 }
817
818 // FIXME: memoize NODES
819 AllNodes.push_back(N);
820 return SDOperand(N, 0);
821}
822
823SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
824 std::vector<SDOperand> &Children) {
825 switch (Children.size()) {
826 case 0: return getNode(Opcode, VT);
827 case 1: return getNode(Opcode, VT, Children[0]);
828 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
829 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
830 default:
831 // FIXME: MEMOIZE!!
832 SDNode *N = new SDNode(Opcode, Children);
833 N->setValueTypes(VT);
834 AllNodes.push_back(N);
835 return SDOperand(N, 0);
836 }
837}
838
Chris Lattner2ee743f2005-01-14 22:08:15 +0000839SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner859157d2005-01-15 06:17:04 +0000840 MVT::ValueType EVT) {
841
842 switch (Opcode) {
843 default: assert(0 && "Bad opcode for this accessor!");
844 case ISD::FP_ROUND_INREG:
845 assert(VT == N1.getValueType() && "Not an inreg round!");
846 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
847 "Cannot FP_ROUND_INREG integer types");
848 if (EVT == VT) return N1; // Not actually rounding
849 assert(EVT < VT && "Not rounding down!");
850 break;
851 case ISD::ZERO_EXTEND_INREG:
852 case ISD::SIGN_EXTEND_INREG:
853 assert(VT == N1.getValueType() && "Not an inreg extend!");
854 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
855 "Cannot *_EXTEND_INREG FP types");
856 if (EVT == VT) return N1; // Not actually extending
857 assert(EVT < VT && "Not extending!");
Chris Lattner950aa3c2005-01-16 00:17:20 +0000858
859 // If we are sign extending an extension, use the original source.
860 if (N1.getOpcode() == ISD::ZERO_EXTEND_INREG ||
861 N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
862 if (N1.getOpcode() == Opcode &&
863 cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
864 return N1;
865 }
866
Chris Lattner859157d2005-01-15 06:17:04 +0000867 break;
868 }
869
870 EVTStruct NN;
871 NN.Opcode = Opcode;
872 NN.VT = VT;
873 NN.EVT = EVT;
874 NN.Ops.push_back(N1);
875
876 SDNode *&N = MVTSDNodes[NN];
877 if (N) return SDOperand(N, 0);
878 N = new MVTSDNode(Opcode, VT, N1, EVT);
879 AllNodes.push_back(N);
880 return SDOperand(N, 0);
881}
882
883SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner2ee743f2005-01-14 22:08:15 +0000884 SDOperand N2, MVT::ValueType EVT) {
885 switch (Opcode) {
886 default: assert(0 && "Bad opcode for this accessor!");
887 case ISD::EXTLOAD:
888 case ISD::SEXTLOAD:
889 case ISD::ZEXTLOAD:
890 // If they are asking for an extending loat from/to the same thing, return a
891 // normal load.
892 if (VT == EVT)
893 return getNode(ISD::LOAD, VT, N1, N2);
894 assert(EVT < VT && "Should only be an extending load, not truncating!");
895 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
896 "Cannot sign/zero extend a FP load!");
897 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
898 "Cannot convert from FP to Int or Int -> FP!");
899 break;
900 }
901
902 EVTStruct NN;
903 NN.Opcode = Opcode;
904 NN.VT = VT;
905 NN.EVT = EVT;
906 NN.Ops.push_back(N1);
907 NN.Ops.push_back(N2);
908
909 SDNode *&N = MVTSDNodes[NN];
910 if (N) return SDOperand(N, 0);
Chris Lattner69a52152005-01-14 22:38:01 +0000911 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner2ee743f2005-01-14 22:08:15 +0000912 AllNodes.push_back(N);
913 return SDOperand(N, 0);
914}
915
916SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
917 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
918 switch (Opcode) {
919 default: assert(0 && "Bad opcode for this accessor!");
920 case ISD::TRUNCSTORE:
Chris Lattner859157d2005-01-15 06:17:04 +0000921#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
922 // If this is a truncating store of a constant, convert to the desired type
923 // and store it instead.
924 if (isa<Constant>(N1)) {
925 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
926 if (isa<Constant>(Op))
927 N1 = Op;
928 }
929 // Also for ConstantFP?
930#endif
Chris Lattner2ee743f2005-01-14 22:08:15 +0000931 if (N1.getValueType() == EVT) // Normal store?
932 return getNode(ISD::STORE, VT, N1, N2, N3);
Chris Lattner859157d2005-01-15 06:17:04 +0000933 assert(N2.getValueType() > EVT && "Not a truncation?");
934 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner2ee743f2005-01-14 22:08:15 +0000935 "Can't do FP-INT conversion!");
936 break;
937 }
938
939 EVTStruct NN;
940 NN.Opcode = Opcode;
941 NN.VT = VT;
942 NN.EVT = EVT;
943 NN.Ops.push_back(N1);
944 NN.Ops.push_back(N2);
945 NN.Ops.push_back(N3);
946
947 SDNode *&N = MVTSDNodes[NN];
948 if (N) return SDOperand(N, 0);
949 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
950 AllNodes.push_back(N);
951 return SDOperand(N, 0);
952}
953
954
Chris Lattner5c884562005-01-12 18:37:47 +0000955/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
956/// indicated value. This method ignores uses of other values defined by this
957/// operation.
958bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
959 assert(Value < getNumValues() && "Bad value!");
960
961 // If there is only one value, this is easy.
962 if (getNumValues() == 1)
963 return use_size() == NUses;
964 if (Uses.size() < NUses) return false;
965
966 SDOperand TheValue(this, Value);
967
968 std::set<SDNode*> UsersHandled;
969
970 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
971 UI != E; ++UI) {
972 SDNode *User = *UI;
973 if (User->getNumOperands() == 1 ||
974 UsersHandled.insert(User).second) // First time we've seen this?
975 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
976 if (User->getOperand(i) == TheValue) {
977 if (NUses == 0)
978 return false; // too many uses
979 --NUses;
980 }
981 }
982
983 // Found exactly the right number of uses?
984 return NUses == 0;
985}
986
987
Chris Lattnerd75f19f2005-01-10 23:25:25 +0000988const char *SDNode::getOperationName() const {
989 switch (getOpcode()) {
990 default: return "<<Unknown>>";
991 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +0000992 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +0000993 case ISD::Constant: return "Constant";
994 case ISD::ConstantFP: return "ConstantFP";
995 case ISD::GlobalAddress: return "GlobalAddress";
996 case ISD::FrameIndex: return "FrameIndex";
997 case ISD::BasicBlock: return "BasicBlock";
998 case ISD::ExternalSymbol: return "ExternalSymbol";
999 case ISD::ConstantPool: return "ConstantPoolIndex";
1000 case ISD::CopyToReg: return "CopyToReg";
1001 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001002 case ISD::ImplicitDef: return "ImplicitDef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001003
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001004 case ISD::ADD: return "add";
1005 case ISD::SUB: return "sub";
1006 case ISD::MUL: return "mul";
1007 case ISD::SDIV: return "sdiv";
1008 case ISD::UDIV: return "udiv";
1009 case ISD::SREM: return "srem";
1010 case ISD::UREM: return "urem";
1011 case ISD::AND: return "and";
1012 case ISD::OR: return "or";
1013 case ISD::XOR: return "xor";
1014 case ISD::SHL: return "shl";
1015 case ISD::SRA: return "sra";
1016 case ISD::SRL: return "srl";
1017
1018 case ISD::SELECT: return "select";
1019 case ISD::ADDC: return "addc";
1020 case ISD::SUBB: return "subb";
1021
1022 // Conversion operators.
1023 case ISD::SIGN_EXTEND: return "sign_extend";
1024 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001025 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
1026 case ISD::ZERO_EXTEND_INREG: return "zero_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001027 case ISD::TRUNCATE: return "truncate";
1028 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001029 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001030 case ISD::FP_EXTEND: return "fp_extend";
1031
1032 case ISD::SINT_TO_FP: return "sint_to_fp";
1033 case ISD::UINT_TO_FP: return "uint_to_fp";
1034 case ISD::FP_TO_SINT: return "fp_to_sint";
1035 case ISD::FP_TO_UINT: return "fp_to_uint";
1036
1037 // Control flow instructions
1038 case ISD::BR: return "br";
1039 case ISD::BRCOND: return "brcond";
1040 case ISD::RET: return "ret";
1041 case ISD::CALL: return "call";
1042 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1043 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1044
1045 // Other operators
1046 case ISD::LOAD: return "load";
1047 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001048 case ISD::EXTLOAD: return "extload";
1049 case ISD::SEXTLOAD: return "sextload";
1050 case ISD::ZEXTLOAD: return "zextload";
1051 case ISD::TRUNCSTORE: return "truncstore";
1052
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001053 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1054 case ISD::EXTRACT_ELEMENT: return "extract_element";
1055 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001056 case ISD::MEMSET: return "memset";
1057 case ISD::MEMCPY: return "memcpy";
1058 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001059
1060 case ISD::SETCC:
1061 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1062 switch (SetCC->getCondition()) {
1063 default: assert(0 && "Unknown setcc condition!");
1064 case ISD::SETOEQ: return "setcc:setoeq";
1065 case ISD::SETOGT: return "setcc:setogt";
1066 case ISD::SETOGE: return "setcc:setoge";
1067 case ISD::SETOLT: return "setcc:setolt";
1068 case ISD::SETOLE: return "setcc:setole";
1069 case ISD::SETONE: return "setcc:setone";
1070
1071 case ISD::SETO: return "setcc:seto";
1072 case ISD::SETUO: return "setcc:setuo";
1073 case ISD::SETUEQ: return "setcc:setue";
1074 case ISD::SETUGT: return "setcc:setugt";
1075 case ISD::SETUGE: return "setcc:setuge";
1076 case ISD::SETULT: return "setcc:setult";
1077 case ISD::SETULE: return "setcc:setule";
1078 case ISD::SETUNE: return "setcc:setune";
1079
1080 case ISD::SETEQ: return "setcc:seteq";
1081 case ISD::SETGT: return "setcc:setgt";
1082 case ISD::SETGE: return "setcc:setge";
1083 case ISD::SETLT: return "setcc:setlt";
1084 case ISD::SETLE: return "setcc:setle";
1085 case ISD::SETNE: return "setcc:setne";
1086 }
1087 }
1088}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001089
1090void SDNode::dump() const {
1091 std::cerr << (void*)this << ": ";
1092
1093 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1094 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001095 if (getValueType(i) == MVT::Other)
1096 std::cerr << "ch";
1097 else
1098 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001099 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001100 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001101
1102 std::cerr << " ";
1103 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1104 if (i) std::cerr << ", ";
1105 std::cerr << (void*)getOperand(i).Val;
1106 if (unsigned RN = getOperand(i).ResNo)
1107 std::cerr << ":" << RN;
1108 }
1109
1110 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1111 std::cerr << "<" << CSDN->getValue() << ">";
1112 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1113 std::cerr << "<" << CSDN->getValue() << ">";
1114 } else if (const GlobalAddressSDNode *GADN =
1115 dyn_cast<GlobalAddressSDNode>(this)) {
1116 std::cerr << "<";
1117 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1118 } else if (const FrameIndexSDNode *FIDN =
1119 dyn_cast<FrameIndexSDNode>(this)) {
1120 std::cerr << "<" << FIDN->getIndex() << ">";
1121 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1122 std::cerr << "<" << CP->getIndex() << ">";
1123 } else if (const BasicBlockSDNode *BBDN =
1124 dyn_cast<BasicBlockSDNode>(this)) {
1125 std::cerr << "<";
1126 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1127 if (LBB)
1128 std::cerr << LBB->getName() << " ";
1129 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001130 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001131 std::cerr << "<reg #" << C2V->getReg() << ">";
1132 } else if (const ExternalSymbolSDNode *ES =
1133 dyn_cast<ExternalSymbolSDNode>(this)) {
1134 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner8a389bb2005-01-15 21:11:37 +00001135 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1136 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001138}
1139
Chris Lattnerea946cd2005-01-09 20:38:33 +00001140static void DumpNodes(SDNode *N, unsigned indent) {
1141 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1142 if (N->getOperand(i).Val->hasOneUse())
1143 DumpNodes(N->getOperand(i).Val, indent+2);
1144 else
1145 std::cerr << "\n" << std::string(indent+2, ' ')
1146 << (void*)N->getOperand(i).Val << ": <multiple use>";
1147
1148
1149 std::cerr << "\n" << std::string(indent, ' ');
1150 N->dump();
1151}
1152
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153void SelectionDAG::dump() const {
1154 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001155 std::vector<SDNode*> Nodes(AllNodes);
1156 std::sort(Nodes.begin(), Nodes.end());
1157
1158 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001159 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1160 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001161 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001162
1163 DumpNodes(getRoot().Val, 2);
1164
Chris Lattnerc3aae252005-01-07 07:46:32 +00001165 std::cerr << "\n\n";
1166}
1167