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