def reach has to use PointerMethod to detect CFG edges that the analysis cares about
[IRC.git] / Robust / src / Analysis / Disjoint / DefiniteReachAnalysis.java
1 package Analysis.Disjoint;
2
3 import java.util.*;
4
5 import IR.*;
6 import IR.Flat.*;
7 import Util.*;
8
9
10 public class DefiniteReachAnalysis {
11   
12   // keep a state of definite reachability analysis for
13   // every program point
14   private Map<FlatNode, DefiniteReachState> fn2state;
15
16   private static PointerMethod pm;
17
18   public static void setPointerMethod( PointerMethod pm ) {
19     DefiniteReachAnalysis.pm = pm;
20   }
21
22   
23   public DefiniteReachAnalysis() {
24     // a class-wide initialization
25     DefiniteReachState.initBuilders();
26
27     fn2state = new HashMap<FlatNode, DefiniteReachState>();
28   }
29
30
31   public void methodEntry( FlatNode fn, 
32                            Set<TempDescriptor> parameters ) {
33     DefiniteReachState state = makeIn( fn );
34     state.methodEntry( parameters );
35     fn2state.put( fn, state );
36   }
37
38   public void copy( FlatNode fn, 
39                     TempDescriptor x,
40                     TempDescriptor y ) {
41     DefiniteReachState state = makeIn( fn );
42     state.copy( x, y );
43     fn2state.put( fn, state ); 
44   }
45
46   public void load( FlatNode fn, 
47                     TempDescriptor  x,
48                     TempDescriptor  y,
49                     FieldDescriptor f,
50                     Set<EdgeKey> edgeKeysForLoad ) {
51
52     DefiniteReachState state = makeIn( fn );
53     state.load( x, y, f, edgeKeysForLoad );
54     fn2state.put( fn, state ); 
55   }
56
57   public void store( FlatNode fn, 
58                      TempDescriptor  x,
59                      FieldDescriptor f,
60                      TempDescriptor  y,
61                      Set<EdgeKey> edgeKeysRemoved,
62                      Set<EdgeKey> edgeKeysAdded ) {
63
64     DefiniteReachState state = makeIn( fn );
65     state.store( x, f, y, edgeKeysRemoved, edgeKeysAdded );
66     fn2state.put( fn, state ); 
67   }
68
69   public void newObject( FlatNode fn, 
70                          TempDescriptor x ) {
71     DefiniteReachState state = makeIn( fn );
72     state.newObject( x );
73     fn2state.put( fn, state ); 
74   }
75
76   public void methodCall( FlatNode fn, 
77                           TempDescriptor retVal ) {
78     DefiniteReachState state = makeIn( fn );
79     if( retVal != null ) {
80       state.methodCall( retVal );
81     }
82     fn2state.put( fn, state ); 
83   }
84
85   public void otherStatement( FlatNode fn ) {
86     fn2state.put( fn, makeIn( fn ) ); 
87   }
88
89
90   public void writeState( FlatNode fn, String outputName ) {
91     makeIn( fn ).writeState( outputName );
92   }
93
94
95   // get the current state for just after the given
96   // program point
97   public DefiniteReachState get( FlatNode fn ) {
98     DefiniteReachState state = fn2state.get( fn );
99     if( state == null ) {
100       state = new DefiniteReachState();
101       fn2state.put( fn, state );
102     }
103     return state;
104   }
105
106   // get the current state for the program point just
107   // before the given program point by merging the out
108   // states of the predecessor statements
109   public DefiniteReachState makeIn( FlatNode fn ) {
110     if( pm.numPrev( fn ) == 0 ) {
111       return new DefiniteReachState();
112     }
113
114     DefiniteReachState stateIn = null;
115
116     for( int i = 0; i < pm.numPrev( fn ); ++i ) {
117
118       if( fn2state.containsKey( pm.getPrev( fn, i ) ) ) {
119         if( stateIn == null ) {
120           // duplicate the first partial result we find
121           stateIn = new DefiniteReachState( get( pm.getPrev( fn, i ) ) );
122         } else {
123           // merge other partial results into the rest
124           stateIn.merge( get( pm.getPrev( fn, i ) ) );
125         }
126       }
127     }
128       
129     // at least one predecessor was analyzed before this
130     assert( stateIn != null );
131     return stateIn;
132   }
133 }