File size: 2,581 Bytes
d1be154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <ot/timer/pin.hpp>
#include <ot/timer/arc.hpp>
#include <ot/timer/scc.hpp>

namespace ot {

// Constructor
SCC::SCC(std::vector<Pin*>&& pins) : _pins {std::move(pins)} {

  for(auto pin : _pins) {
    pin->_scc = this;
  }
}

// Function: _is_entry
bool SCC::_is_entry(const Pin& to) const {

  assert(to._scc == this);

  for(auto arc : to._fanin) {
    if(auto& from = arc->_from; from._scc != this) {
      return true;
    }
  }

  return false;
}

// Function: _is_exit
bool SCC::_is_exit(const Pin& from) const {

  assert(from._scc == this);

  for(auto arc : from._fanout) {
    if(auto& to = arc->_to; to._scc != this) {
      return true;
    }
  }

  return false;

}

// Function: _dump
std::string SCC::_dump() const {
  
  std::ostringstream os;

  os << "digraph SCC {\n";
  for(auto from : _pins) {
    os << "  \"" << from->_name << "\";\n";

    for(auto& arc : from->_fanout) {
      if(from->_scc == arc->_to._scc) {
        os << "  \"" 
           << from->_name << "\" -> \"" << arc->_to._name 
           << "\";\n";  
      }
    }
  }

  os << "}\n";

  return os.str();
}

// Procedure: _clear
void SCC::_clear() {

  // unmark the scc pins and arcs
  for(auto pin : _pins) {
    
    for(auto arc : pin->_fanin) {
      if(arc->_from._scc == this) {
        arc->_remove_state(Arc::LOOP_BREAKER);
      }
    }

    for(auto arc : pin->_fanout) {
      if(arc->_to._scc == this) {
        arc->_remove_state(Arc::LOOP_BREAKER);
      }
    }

    pin->_scc = nullptr;
  }

  _pins.clear();
}

// Procedure: _unloop
void SCC::_unloop(Pin& from) {
 
  assert(!from._has_state(Pin::UNLOOP_CAND) && !from._has_state(Pin::IN_UNLOOP_STACK));

  from._insert_state(Pin::UNLOOP_CAND | Pin::IN_UNLOOP_STACK);

  for(auto arc : from._fanout) {

    if(auto& to = arc->_to; to._scc != this) {
      continue;
    }
    else if(!to._has_state(Pin::UNLOOP_CAND)) {
      _unloop(to);
    }
    else if(to._has_state(Pin::IN_UNLOOP_STACK)) {
      //OT_LOGD("mark arc ", arc->name(), " as loop breaker");
      arc->_insert_state(Arc::LOOP_BREAKER);
    }
  }
  
  from._remove_state(Pin::IN_UNLOOP_STACK);
}

// Procedure: _unloop
// Find the starting pin to break the loop.
void SCC::_unloop() {

  assert(!_pins.empty());

  Pin* entry {nullptr};
  
  for(auto pin : _pins) {
    if(!_is_entry(*pin)) continue;
    if(!entry || pin->is_input()) { 
      entry = pin;
    }
  }

  if(entry == nullptr) {
    entry = _pins[0];
  }
  _unloop(*entry);
}


};  // end of namespace ot. -----------------------------------------------------------------------