1 package Analysis.Loops;
4 import java.util.Iterator;
5 import java.util.Hashtable;
6 import java.util.HashSet;
10 public class CopyPropagation {
11 public CopyPropagation() {
14 public void optimize(FlatMethod fm) {
16 Hashtable<FlatNode, Hashtable<TempDescriptor, TempDescriptor>> table
17 =new Hashtable<FlatNode, Hashtable<TempDescriptor, TempDescriptor>>();
18 boolean changed=false;
19 TempDescriptor bogustd=new TempDescriptor("bogus");
22 HashSet tovisit=new HashSet();
24 while(!tovisit.isEmpty()) {
25 FlatNode fn=(FlatNode) tovisit.iterator().next();
28 Hashtable<TempDescriptor, TempDescriptor> tab;
29 if (fn.numPrev()>=1&&table.containsKey(fn.getPrev(0)))
30 tab=new Hashtable<TempDescriptor, TempDescriptor>(table.get(fn.getPrev(0)));
32 tab=new Hashtable<TempDescriptor, TempDescriptor>();
33 //Compute intersection
35 for(int i=1;i<fn.numPrev();i++) {
36 Hashtable<TempDescriptor, TempDescriptor> tp=table.get(fn.getPrev(i));
39 for(Iterator tmpit=tp.entrySet().iterator();tmpit.hasNext();) {
40 Map.Entry t=(Map.Entry)tmpit.next();
41 TempDescriptor tmp=(TempDescriptor)t.getKey();
43 if (!tab.containsKey(tmp))
44 tab.put(tmp, tp.get(tmp));
45 else if (tab.get(tmp)!=tp.get(tmp)) {
46 tab.put(tmp, bogustd);
51 HashSet<TempDescriptor> toremove=new HashSet<TempDescriptor>();
52 TempDescriptor[]writes=fn.writesTemps();
53 for(int i=0;i<writes.length;i++) {
54 TempDescriptor tmp=writes[i];
56 for(Iterator<TempDescriptor> tmpit=tab.keySet().iterator();tmpit.hasNext();) {
57 TempDescriptor tmp2=tmpit.next();
58 if (tmp==tab.get(tmp2))
63 for(Iterator<TempDescriptor> tmpit=toremove.iterator();tmpit.hasNext();) {
64 TempDescriptor tmp=tmpit.next();
65 tab.put(tmp, bogustd);
68 if (fn.kind()==FKind.FlatOpNode) {
69 FlatOpNode fon=(FlatOpNode)fn;
70 if (fon.getOp().getOp()==Operation.ASSIGN) {
71 tab.put(fon.getDest(), fon.getLeft());
74 if (!table.containsKey(fn)||!table.get(fn).equals(tab)) {
77 for(int i=0;i<fn.numNext();i++) {
78 FlatNode nnext=fn.getNext(i);
82 } //end of dataflow while loop
84 Set<FlatNode> nodeset=fm.getNodeSet();
86 for(Iterator<FlatNode> it=fm.getNodeSet().iterator();it.hasNext();) {
87 FlatNode fn=it.next();
91 Hashtable<TempDescriptor, TempDescriptor> tab=new Hashtable<TempDescriptor, TempDescriptor>();
93 for(int i=0;i<fn.numPrev();i++) {
94 Hashtable<TempDescriptor, TempDescriptor> tp=table.get(fn.getPrev(i));
95 for(Iterator tmpit=tp.entrySet().iterator();tmpit.hasNext();) {
96 Map.Entry t=(Map.Entry)tmpit.next();
97 TempDescriptor tmp=(TempDescriptor)t.getKey();
99 if (!tab.containsKey(tmp))
100 tab.put(tmp, tp.get(tmp));
101 else if (tab.get(tmp)!=tp.get(tmp)) {
102 tab.put(tmp, bogustd);
108 TempDescriptor[]reads=fn.readsTemps();
109 for(int i=0;i<reads.length;i++) {
110 TempDescriptor tmp=reads[i];
111 if (tab.containsKey(tmp)&&tab.get(tmp)!=bogustd) {
114 tmap.addPair(tmp, tab.get(tmp));
119 } //end of remapping for loop