Module: Furnace::CFG::Algorithms
- Included in:
- Graph
- Defined in:
- lib/furnace/cfg/algorithms.rb
Instance Method Summary collapse
-
#compute_generic_domination(start, forward) ⇒ Object
Shamelessly stolen from http://www.cs.colostate.edu/~mstrout/CS553/slides/lecture04.pdf.
- #dominators ⇒ Object
- #eliminate_unreachable! ⇒ Object
- #flush ⇒ Object
-
#identify_loops ⇒ Object
See also #dominators for references.
- #merge_redundant! ⇒ Object
- #postdominators ⇒ Object
Instance Method Details
#compute_generic_domination(start, forward) ⇒ Object
Shamelessly stolen from http://www.cs.colostate.edu/~mstrout/CS553/slides/lecture04.pdf
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 |
# File 'lib/furnace/cfg/algorithms.rb', line 92 def compute_generic_domination(start, forward) # values of β will give rise to dom! dom = { start => Set[start] } @nodes.each do |node| next if node == start dom[node] = @nodes.dup end change = true while change change = false @nodes.each do |node| next if node == start # Are we computing dominators or postdominators? if forward edges = node.sources + node.exception_sources elsif node.exception.nil? edges = node.targets else edges = node.targets + [ node.exception ] end # Key Idea [for dominators] # If a node dominates all # predecessors of node n, then it # also dominates node n. pred = edges.map do |source| dom[source] end.reduce(:&) # An exception handler header node has no regular sources. pred = [] if pred.nil? current = Set[node].merge(pred) if current != dom[node] dom[node] = current change = true end end end dom end |
#dominators ⇒ Object
138 139 140 |
# File 'lib/furnace/cfg/algorithms.rb', line 138 def dominators @dominators ||= compute_generic_domination(@entry, true) end |
#eliminate_unreachable! ⇒ Object
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 |
# File 'lib/furnace/cfg/algorithms.rb', line 3 def eliminate_unreachable! worklist = Set[ entry, exit ] reachable = Set[] while worklist.any? node = worklist.first worklist.delete node reachable.add node node.targets.each do |target| unless reachable.include? target worklist.add target end end if node.exception unless reachable.include? node.exception worklist.add node.exception end end end @nodes.each do |node| unless reachable.include? node @nodes.delete node yield node if block_given? end end flush end |
#flush ⇒ Object
186 187 188 189 190 191 |
# File 'lib/furnace/cfg/algorithms.rb', line 186 def flush @dominators = nil @postdominators = nil super if defined?(super) end |
#identify_loops ⇒ Object
See also #dominators for references.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/furnace/cfg/algorithms.rb', line 147 def identify_loops loops = Hash.new { |h,k| h[k] = Set.new } dom = dominators @nodes.each do |node| node.targets.each do |target| # Back edges # A back edge of a natural loop is one whose # target dominates its source. if dom[node].include? target loops[target].add node end end end # At this point, +loops+ contains a list of all nodes # which have a back edge to the loop header. Expand # it to the list of all nodes in the loop. loops.each do |header, nodes| # Natural loop # The natural loop of a back edge (m→n), where # n dominates m, is the set of nodes x such that n # dominates x and there is a path from x to m not # containing n. pre_header = dom[header] all_nodes = Set[header] nodes.each do |node| all_nodes.merge(dom[node] - pre_header) end nodes.replace all_nodes end loops.default = nil loops end |
#merge_redundant! ⇒ Object
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 |
# File 'lib/furnace/cfg/algorithms.rb', line 35 def merge_redundant! worklist = @nodes.dup while worklist.any? node = worklist.first worklist.delete node target = node.targets[0] next if target == @exit # Skip explicitly non-redundant nodes if node.[:keep] next end if node.targets.uniq == [target] && target.sources.uniq == [node] && node.exception == target.exception yield node, target if block_given? node.insns.delete node.cti @nodes.delete target worklist.delete target node.insns.concat target.insns node.cti = target.cti node.target_labels = target.target_labels worklist.add node flush elsif node.targets.count == 1 && node.insns.empty? target = node.targets.first yield target, node if block_given? node.sources.each do |source| index = source.targets.index(node) source.target_labels[index] = target.label end @nodes.delete node worklist.delete node if @entry == node @entry = target end flush end end end |
#postdominators ⇒ Object
142 143 144 |
# File 'lib/furnace/cfg/algorithms.rb', line 142 def postdominators @postdominators ||= compute_generic_domination(@exit, false) end |