25
25
import pascal .taie .analysis .MethodAnalysis ;
26
26
import pascal .taie .analysis .dataflow .analysis .constprop .CPFact ;
27
27
import pascal .taie .analysis .dataflow .analysis .constprop .ConstantPropagation ;
28
- import pascal .taie .analysis .dataflow .analysis .constprop .Value ;
29
28
import pascal .taie .analysis .dataflow .fact .DataflowResult ;
30
29
import pascal .taie .analysis .dataflow .fact .SetFact ;
31
30
import pascal .taie .analysis .graph .cfg .CFG ;
32
31
import pascal .taie .analysis .graph .cfg .CFGBuilder ;
33
- import pascal .taie .analysis .graph .cfg .Edge ;
34
32
import pascal .taie .config .AnalysisConfig ;
35
33
import pascal .taie .ir .IR ;
36
34
import pascal .taie .ir .exp .ArithmeticExp ;
40
38
import pascal .taie .ir .exp .NewExp ;
41
39
import pascal .taie .ir .exp .RValue ;
42
40
import pascal .taie .ir .exp .Var ;
43
- import pascal .taie .ir .stmt .AssignStmt ;
44
41
import pascal .taie .ir .stmt .If ;
45
42
import pascal .taie .ir .stmt .Stmt ;
46
43
import pascal .taie .ir .stmt .SwitchStmt ;
47
44
48
- import java .util .Comparator ;
49
- import java .util .Set ;
50
- import java .util .TreeSet ;
45
+ import java .util .*;
51
46
52
47
public class DeadCodeDetection extends MethodAnalysis {
53
48
@@ -71,6 +66,34 @@ public Set<Stmt> analyze(IR ir) {
71
66
Set <Stmt > deadCode = new TreeSet <>(Comparator .comparing (Stmt ::getIndex ));
72
67
// TODO - finish me
73
68
// Your task is to recognize dead code in ir and add it to deadCode
69
+
70
+ // not reachable analysis
71
+ // 1. control flow not reachable analysis
72
+ Set <Stmt > visit = new TreeSet <>(Comparator .comparing (Stmt ::getIndex ));
73
+ var entry = cfg .getEntry ();
74
+ Queue <Stmt > queue = new LinkedList <>();
75
+ queue .add (entry );
76
+ while (!queue .isEmpty ()) {
77
+ var stmt = queue .poll ();
78
+ visit .add (stmt );
79
+ if (stmt instanceof If ) {
80
+ var condition = ((If ) stmt ).getCondition ();
81
+ var fact = constants .getResult (stmt );
82
+ if (fact .get (condition .getOperand1 ()).isConstant ()
83
+ && fact .get (condition .getOperand2 ()).isConstant ()) {
84
+
85
+ }
86
+ }
87
+ if (stmt instanceof SwitchStmt ) {
88
+
89
+ }
90
+ queue .addAll (cfg .getSuccsOf (stmt ));
91
+ }
92
+ for (Stmt stmt : cfg ) {
93
+ if (!visit .contains (stmt )) {
94
+ deadCode .add (stmt );
95
+ }
96
+ }
74
97
return deadCode ;
75
98
}
76
99
0 commit comments