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