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