SelectionDAG.cpp revision 954da37bb492b519f5c31dc360f2a142567e08b4
1//===-- SelectionDAG.cpp - Implement the SelectionDAG* classes ------------===//
2//
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//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG* classes, which are used to perform
11// DAG-based instruction selection in a target-specific manner.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/SelectionDAG.h"
16#include "llvm/Type.h"
17#include <iostream>
18
19using namespace llvm;
20
21SelectionDAG::~SelectionDAG() {
22  for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
23    delete AllNodes[i];
24}
25
26
27/// dump - Print out the current Selection DAG...
28void SelectionDAG::dump() const {
29  Root->dump();  // Print from the root...
30}
31
32/// getValueType - Return the ValueType for the specified LLVM type.  This
33/// method works on all scalar LLVM types.
34///
35MVT::ValueType SelectionDAG::getValueType(const Type *Ty) const {
36  switch (Ty->getTypeID()) {
37  case Type::VoidTyID: assert(0 && "Void type object in getValueType!");
38  default: assert(0 && "Unknown type in DAGBuilder!\n");
39  case Type::BoolTyID:    return MVT::i1;
40  case Type::SByteTyID:
41  case Type::UByteTyID:   return MVT::i8;
42  case Type::ShortTyID:
43  case Type::UShortTyID:  return MVT::i16;
44  case Type::IntTyID:
45  case Type::UIntTyID:    return MVT::i32;
46  case Type::LongTyID:
47  case Type::ULongTyID:   return MVT::i64;
48  case Type::FloatTyID:   return MVT::f32;
49  case Type::DoubleTyID:  return MVT::f64;
50  case Type::LabelTyID:
51  case Type::PointerTyID: return PointerType;
52  }
53}
54
55void SelectionDAGNode::dump() const {
56  // Print out the DAG in post-order
57  std::map<const SelectionDAGNode*, unsigned> NodeIDs;
58  unsigned ID = 0;
59  printit(0, ID, NodeIDs);
60}
61
62void SelectionDAGNode::printit(unsigned Offset, unsigned &LastID,
63                               std::map<const SelectionDAGNode*,
64                                        unsigned> &NodeIDs) const {
65  if (!NodeIDs.count(this)) {
66    // Emit all of the uses first...
67    for (unsigned i = 0, e = Uses.size(); i != e; ++i)
68      Uses[i]->printit(Offset+1, LastID, NodeIDs);
69
70    NodeIDs[this] = LastID++;
71
72    std::cerr << std::string(Offset, ' ') << "#" << LastID-1 << " ";
73  } else {
74    // Node has already been emitted...
75    std::cerr << std::string(Offset, ' ') << "#" << NodeIDs[this] << " ";
76  }
77
78  switch (ValueType) {
79  case MVT::isVoid: std::cerr << "V:"; break;
80  case MVT::i1:   std::cerr << "i1:"; break;
81  case MVT::i8:   std::cerr << "i8:"; break;
82  case MVT::i16:  std::cerr << "i16:"; break;
83  case MVT::i32:  std::cerr << "i32:"; break;
84  case MVT::i64:  std::cerr << "i64:"; break;
85  case MVT::f32:  std::cerr << "f32:"; break;
86  case MVT::f64:  std::cerr << "f64:"; break;
87  default: assert(0 && "Invalid node ValueType!");
88  }
89  switch (NodeType) {
90  case ISD::ChainNode:      std::cerr << "ChainNode"; break;
91  case ISD::BlockChainNode: std::cerr << "BlockChainNode"; break;
92  case ISD::ProtoNode:      std::cerr << "ProtoNode"; break;
93
94  case ISD::Constant:       std::cerr << "Constant"; break;
95  case ISD::FrameIndex:     std::cerr << "FrameIndex"; break;
96  case ISD::BasicBlock:     std::cerr << "BasicBlock"; break;
97
98  case ISD::Plus:           std::cerr << "Plus"; break;
99  case ISD::Minus:          std::cerr << "Minus"; break;
100  case ISD::Times:          std::cerr << "Times"; break;
101  case ISD::SDiv:           std::cerr << "SDiv"; break;
102  case ISD::UDiv:           std::cerr << "UDiv"; break;
103  case ISD::SRem:           std::cerr << "SRem"; break;
104  case ISD::URem:           std::cerr << "URem"; break;
105  case ISD::And:            std::cerr << "And"; break;
106  case ISD::Or:             std::cerr << "Or"; break;
107  case ISD::Xor:            std::cerr << "Xor"; break;
108
109  case ISD::SetEQ:          std::cerr << "SetEQ"; break;
110  case ISD::SetNE:          std::cerr << "SetNE"; break;
111  case ISD::SetLT:          std::cerr << "SetLT"; break;
112  case ISD::SetLE:          std::cerr << "SetLE"; break;
113  case ISD::SetGT:          std::cerr << "SetGT"; break;
114  case ISD::SetGE:          std::cerr << "SetGE"; break;
115
116  case ISD::Br:             std::cerr << "Br"; break;
117  case ISD::BrCond:         std::cerr << "BrCond"; break;
118  case ISD::Switch:         std::cerr << "Switch"; break;
119  case ISD::Ret:            std::cerr << "Ret"; break;
120  case ISD::RetVoid:        std::cerr << "RetVoid"; break;
121  case ISD::Load:           std::cerr << "Load"; break;
122  case ISD::Store:          std::cerr << "Store"; break;
123  case ISD::PHI:            std::cerr << "PHI"; break;
124  case ISD::Call:           std::cerr << "Call"; break;
125
126  case ISD::Unspec1:        std::cerr << "Unspec1"; break;
127  case ISD::Unspec2:        std::cerr << "Unspec2"; break;
128  }
129
130  std::cerr << "\n";
131}
132