From ca093035e52dcd3e072cefc08ae1afe1b2e912c6 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 30 Oct 2009 00:14:10 +0000 Subject: [PATCH] finish branch elimination optimization for fission code --- .../src/Analysis/Locality/BranchAnalysis.java | 99 ++++++++++++++++--- 1 file changed, 84 insertions(+), 15 deletions(-) diff --git a/Robust/src/Analysis/Locality/BranchAnalysis.java b/Robust/src/Analysis/Locality/BranchAnalysis.java index 16ef0d20..31a14026 100644 --- a/Robust/src/Analysis/Locality/BranchAnalysis.java +++ b/Robust/src/Analysis/Locality/BranchAnalysis.java @@ -1,12 +1,16 @@ package Analysis.Locality; +import IR.State; +import IR.Flat.*; +import java.util.*; +import java.io.*; public class BranchAnalysis { LocalityAnalysis locality; State state; - public BranchAnalysis(Locality locality, LocalityAnalysis lb, Set nodeset, State state) { + public BranchAnalysis(LocalityAnalysis locality, LocalityBinding lb, Set nodeset, Set storeset, State state) { this.locality=locality; this.state=state; - doAnalysis(lb, nodeset); + doAnalysis(lb, nodeset, storeset); } Hashtable, Vector> table=new Hashtable, Vector>(); @@ -35,14 +39,78 @@ public class BranchAnalysis { return exits.size(); } - public void doAnalysis(LocalityAnalysis lb, Set nodeset) { + public Vector getJumps(FlatNode fn) { + Set group=groupmap.get(fn); + if (group==null) + throw new Error(); + Vector exits=table.get(group); + return exits; + } + + public Set getTargets() { + HashSet targets=new HashSet(); + Collection> groups=groupmap.values(); + for(Iterator> setit=groups.iterator();setit.hasNext();) { + Set group=setit.next(); + targets.addAll(table.get(group)); + } + return targets; + } + + int grouplabelindex=0; + + public boolean hasGroup(FlatNode fn) { + return groupmap.contains(fn); + } + + Hashtable, String> grouplabel=new Hashtable, String>(); + + private boolean seenGroup(FlatNode fn) { + return grouplabel.containsKey(groupmap.get(fn)); + } + + private String getGroup(FlatNode fn) { + if (!grouplabel.containsKey(groupmap.get(fn))) + grouplabel.put(groupmap.get(fn), new String("LG"+(grouplabelindex++))); + return grouplabel.get(groupmap.get(fn)); + } + + public void generateGroupCode(FlatNode fn, PrintWriter output, Hashtable nodetolabels) { + if (seenGroup(fn)) { + String label=getGroup(fn); + output.println("goto "+label+";"); + } else { + String label=getGroup(fn); + output.println(label+":"); + if (numJumps(fn)==1) { + FlatNode fndst=getJumps(fn).get(0); + output.println("goto "+nodetolabels.get(fndst)+";"); + } else if (numJumps(fn)==2) { + Vector exits=getJumps(fn); + output.println("if(RESTOREBRANCH())"); + output.println("goto L"+nodetolabels.get(exits.get(1))+";"); + output.println("else"); + output.println("goto L"+nodetolabels.get(exits.get(0))+";"); + } else { + Vector exits=getJumps(fn); + output.println("switch(RESTOREBRANCH()) {"); + for(int i=0;i nodeset, Set storeset) { Set transset=computeTransSet(lb); - fnmap=computeMap(transset, nodeset); + fnmap=computeMap(transset, nodeset, storeset); groupmap=new Hashtable>(); for(Iterator fnit=transset.iterator();fnit.hasNext();) { FlatNode fn=fnit.next(); - if (fn.numNext()>1) { + if (fn.numNext()>1&&storeset.contains(fn)) { FlatNode[] children=fnmap.get(fn); if (!groupmap.containsKey(fn)) { groupmap.put(fn, new HashSet()); @@ -50,9 +118,9 @@ public class BranchAnalysis { } for(int i=0;i1) + if (child.numNext()>1&&storeset.contains(child)) mergegroups(fn, child, groupmap); - } + } } } //now we have groupings... @@ -95,7 +163,7 @@ public class BranchAnalysis { } } - public Hashtable computeMap(Set transset, Set nodeset) { + public Hashtable computeMap(Set transset, Set nodeset, Set storeset) { Set toprocess=new HashSet(); toprocess.addAll(transset); Hashtable> fntotuple=new Hashtable>(); @@ -107,21 +175,22 @@ public class BranchAnalysis { for(int i=0;i tuple=fntotuple.get(fprev); - incomingtuples.addAll(tuple); + if (tuple!=null) + incomingtuples.addAll(tuple); } } - if (nodeset.contains(fn)) { + if (nodeset.contains(fn)||storeset.contains(fn)||fn.kind()==FKind.FlatAtomicExitNode) { //nodeset contains this node for(Iterator it=incomingtuples.iterator();it.hasNext();) { Object[] pair=it.next(); @@ -137,7 +206,7 @@ public class BranchAnalysis { //add if we need to update if (!fntotuple.containsKey(fn)|| !fntotuple.get(fn).equals(incomingtuples)) { - tntotuple.put(fn,incomingtuples); + fntotuple.put(fn,incomingtuples); for(int i=0;i computeTransSet(LocalityAnalysis lb) { + public Set computeTransSet(LocalityBinding lb) { Set transset=new HashSet(); Set tovisit=new HashSet(); tovisit.addAll(state.getMethodFlat(lb.getMethod()).getNodeSet()); while(!tovisit.isEmpty()) { FlatNode fn=tovisit.iterator().next(); tovisit.remove(fn); - if (locality.getAtomic(lb).get(fn).intValue()>0) + if (locality.getAtomic(lb).get(fn).intValue()>0||fn.kind()==FKind.FlatAtomicExitNode) transset.add(fn); } return transset; -- 2.34.1