Skip to content
This repository was archived by the owner on Jul 8, 2020. It is now read-only.

Commit 245d899

Browse files
committed
havlak4
1 parent b78dac1 commit 245d899

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

‎havlak/havlak.go‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,15 @@ func DFS(currentNode *BasicBlock, nodes []*UnionFindNode, number []int, last []i
251251
return lastid
252252
}
253253

254+
func appendUnique(a []int, x int) []int {
255+
for _, y := range a {
256+
if x == y {
257+
return a
258+
}
259+
}
260+
return append(a, x)
261+
}
262+
254263
// FindLoops
255264
//
256265
// Find loops and build loop forest using Havlak's algorithm, which
@@ -265,7 +274,7 @@ func FindLoops(cfgraph *CFG, lsgraph *LSG) {
265274

266275
size := cfgraph.NumNodes()
267276

268-
nonBackPreds := make([]map[int]bool, size)
277+
nonBackPreds := make([][]int, size)
269278
backPreds := make([][]int, size)
270279

271280
number := make([]int, size)
@@ -283,9 +292,8 @@ func FindLoops(cfgraph *CFG, lsgraph *LSG) {
283292
// - depth-first traversal and numbering.
284293
// - unreached BB's are marked as dead.
285294
//
286-
for i, bb := range cfgraph.Blocks {
295+
for _, bb := range cfgraph.Blocks {
287296
number[bb.Name] = unvisited
288-
nonBackPreds[i] = make(map[int]bool)
289297
}
290298

291299
DFS(cfgraph.Start, nodes, number, last, 0)
@@ -320,7 +328,7 @@ func FindLoops(cfgraph *CFG, lsgraph *LSG) {
320328
if isAncestor(w, v, last) {
321329
backPreds[w] = append(backPreds[w], v)
322330
} else {
323-
nonBackPreds[w][v] = true
331+
nonBackPreds[w] = appendUnique(nonBackPreds[w], v)
324332
}
325333
}
326334
}
@@ -390,13 +398,13 @@ func FindLoops(cfgraph *CFG, lsgraph *LSG) {
390398
return
391399
}
392400

393-
for iter := range nonBackPreds[x.dfsNumber] {
401+
for _, iter := range nonBackPreds[x.dfsNumber] {
394402
y := nodes[iter]
395403
ydash := y.FindSet()
396404

397405
if !isAncestor(w, ydash.dfsNumber, last) {
398406
types[w] = bbIrreducible
399-
nonBackPreds[w][ydash.dfsNumber] = true
407+
nonBackPreds[w] = appendUnique(nonBackPreds[w], ydash.dfsNumber)
400408
} else {
401409
if ydash.dfsNumber != w {
402410
if !listContainsNode(nodePool, ydash) {
@@ -473,8 +481,8 @@ func FindHavlakLoops(cfgraph *CFG, lsgraph *LSG) int {
473481
//
474482
type SimpleLoop struct {
475483
// No set, use map to bool
476-
basicBlocks map[*BasicBlock]bool
477-
Children map[*SimpleLoop]bool
484+
basicBlocks []*BasicBlock
485+
Children []*SimpleLoop
478486
Parent *SimpleLoop
479487
header *BasicBlock
480488

@@ -486,11 +494,11 @@ type SimpleLoop struct {
486494
}
487495

488496
func (loop *SimpleLoop) AddNode(bb *BasicBlock) {
489-
loop.basicBlocks[bb] = true
497+
loop.basicBlocks = append(loop.basicBlocks, bb)
490498
}
491499

492500
func (loop *SimpleLoop) AddChildLoop(child *SimpleLoop) {
493-
loop.Children[child] = true
501+
loop.Children = append(loop.Children, child)
494502
}
495503

496504
func (loop *SimpleLoop) Dump(indent int) {
@@ -508,13 +516,13 @@ func (loop *SimpleLoop) Dump(indent int) {
508516
// must have > 0
509517
if len(loop.Children) > 0 {
510518
fmt.Printf("Children: ")
511-
for ll := range loop.Children {
519+
for _, ll := range loop.Children {
512520
fmt.Printf("loop-%d", ll.Counter)
513521
}
514522
}
515523
if len(loop.basicBlocks) > 0 {
516524
fmt.Printf("(")
517-
for bb := range loop.basicBlocks {
525+
for _, bb := range loop.basicBlocks {
518526
fmt.Printf("BB#%06d ", bb.Name)
519527
if loop.header == bb {
520528
fmt.Printf("*")
@@ -577,8 +585,6 @@ func NewLSG() *LSG {
577585

578586
func (lsg *LSG) NewLoop() *SimpleLoop {
579587
loop := new(SimpleLoop)
580-
loop.basicBlocks = make(map[*BasicBlock]bool)
581-
loop.Children = make(map[*SimpleLoop]bool)
582588
loop.Parent = nil
583589
loop.header = nil
584590

@@ -598,7 +604,7 @@ func (lsg *LSG) Dump() {
598604
func (lsg *LSG) dump(loop *SimpleLoop, indent int) {
599605
loop.Dump(indent)
600606

601-
for ll := range loop.Children {
607+
for _, ll := range loop.Children {
602608
lsg.dump(ll, indent+1)
603609
}
604610
}
@@ -617,7 +623,7 @@ func (lsg *LSG) CalculateNestingLevel() {
617623

618624
func (lsg *LSG) calculateNestingLevel(loop *SimpleLoop, depth int) {
619625
loop.DepthLevel = depth
620-
for ll := range loop.Children {
626+
for _, ll := range loop.Children {
621627
lsg.calculateNestingLevel(ll, depth+1)
622628

623629
ll.NestingLevel = max(loop.NestingLevel, ll.NestingLevel+1)

0 commit comments

Comments
 (0)