From: bdemsky Date: Fri, 30 Oct 2009 00:14:53 +0000 (+0000) Subject: use branch elimination optimization X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=288fdd2a0b40783b3167378246026af34cc60674;p=IRC.git use branch elimination optimization clever trick to simplify array bounds checks (used unsigned comparison) --- diff --git a/Robust/src/IR/Flat/BuildCode.java b/Robust/src/IR/Flat/BuildCode.java index d174ec65..86539904 100644 --- a/Robust/src/IR/Flat/BuildCode.java +++ b/Robust/src/IR/Flat/BuildCode.java @@ -20,6 +20,7 @@ import Analysis.Locality.LocalityAnalysis; import Analysis.Locality.LocalityBinding; import Analysis.Locality.DiscoverConflicts; import Analysis.Locality.DelayComputation; +import Analysis.Locality.BranchAnalysis; import Analysis.CallGraph.CallGraph; import Analysis.Prefetch.*; import Analysis.Loops.WriteBarrier; @@ -1563,6 +1564,8 @@ public class BuildCode { Hashtable atomicmethodmap; static int atomicmethodcount=0; + + BranchAnalysis branchanalysis; private void generateFlatMethod(FlatMethod fm, LocalityBinding lb, PrintWriter output) { if (State.PRINTFLAT) System.out.println(fm.printMethod()); @@ -1572,6 +1575,7 @@ public class BuildCode { ParamsObject objectparams=(ParamsObject)paramstable.get(lb!=null ? lb : md!=null ? md : task); HashSet arset=null; + branchanalysis=null; if (state.DELAYCOMP&&!lb.isAtomic()&&lb.getHasAtomic()) { //create map @@ -1582,6 +1586,9 @@ public class BuildCode { localsprefixaddr=localsprefix; localsprefixderef=localsprefix+"->"; arset=new HashSet(); + + //build branchanalysis + branchanalysis=new BranchAnalysis(locality, lb, delaycomp.getNotReady(lb), delaycomp.livecode(lb), state); //Generate commit methods here for(Iterator fnit=fm.getNodeSet().iterator();fnit.hasNext();) { @@ -2155,8 +2162,14 @@ public class BuildCode { PrintWriter output, boolean firstpass) { /* Assign labels to FlatNode's if necessary.*/ - Hashtable nodetolabel=assignLabels(first, stopset); + Hashtable nodetolabel; + + if (state.DELAYCOMP&&!firstpass) + nodetolabel=dcassignLabels(first, stopset); + else + nodetolabel=assignLabels(first, stopset); + Set storeset=null; HashSet genset=null; HashSet refset=null; @@ -2319,7 +2332,7 @@ public class BuildCode { generateFlatCondBranch(fm, lb, (FlatCondBranch)current_node, "L"+nodetolabel.get(current_node.getNext(1)), output); } else if (storeset.contains(current_node)) { //need to do branch - output.println("RESTOREANDBRANCH(L"+nodetolabel.get(current_node.getNext(1))+"); /* "+current_node.nodeid+" */"); + branchanalysis.generateGroupCode(current_node, output, nodetolabel); } else { //which side to execute computeside=true; @@ -2406,8 +2419,55 @@ public class BuildCode { } } } + /** Special label assignment for delaycomputation */ + protected Hashtable dcassignLabels(FlatNode first, Set lastset) { + HashSet tovisit=new HashSet(); + HashSet visited=new HashSet(); + int labelindex=0; + Hashtable nodetolabel=new Hashtable(); + + //Label targets of branches + Set targets=branchanalysis.getTargets(); + for(Iterator it=targets.iterator();it.hasNext();) { + nodetolabel.put(it.next(), new Integer(labelindex++)); + } + + + tovisit.add(first); + /*Assign labels first. A node needs a label if the previous + * node has two exits or this node is a join point. */ + + while(!tovisit.isEmpty()) { + FlatNode fn=(FlatNode)tovisit.iterator().next(); + tovisit.remove(fn); + visited.add(fn); + + + if(lastset!=null&&lastset.contains(fn)) { + // if last is not null and matches, don't go + // any further for assigning labels + continue; + } + + for(int i=0; i0) { + //1) Edge >1 of node + nodetolabel.put(nn,new Integer(labelindex++)); + } + if (!visited.contains(nn)&&!tovisit.contains(nn)) { + tovisit.add(nn); + } else { + //2) Join point + nodetolabel.put(nn,new Integer(labelindex++)); + } + } + } + return nodetolabel; + + } - /** This method assigns labels to FlatNodes */ protected Hashtable assignLabels(FlatNode first) { return assignLabels(first, null); } @@ -3752,7 +3812,7 @@ public class BuildCode { type=elementtype.getSafeSymbol()+" "; if (this.state.ARRAYBOUNDARYCHECK && fen.needsBoundsCheck()) { - output.println("if ("+generateTemp(fm, fen.getIndex(),lb)+"< 0 | "+generateTemp(fm, fen.getIndex(),lb)+" >= "+generateTemp(fm,fen.getSrc(),lb) + "->___length___)"); + output.println("if (((unsigned int)"+generateTemp(fm, fen.getIndex(),lb)+") >= "+generateTemp(fm,fen.getSrc(),lb) + "->___length___)"); output.println("failedboundschk();"); } if (state.SINGLETM) { @@ -3812,13 +3872,13 @@ public class BuildCode { type=elementtype.getSafeSymbol()+" "; if (this.state.ARRAYBOUNDARYCHECK && fsen.needsBoundsCheck()) { - output.println("if ("+generateTemp(fm, fsen.getIndex(),lb)+"< 0 | "+generateTemp(fm, fsen.getIndex(),lb)+" >= "+generateTemp(fm,fsen.getDst(),lb) + "->___length___)"); + output.println("if (((unsigned int)"+generateTemp(fm, fsen.getIndex(),lb)+") >= "+generateTemp(fm,fsen.getDst(),lb) + "->___length___)"); output.println("failedboundschk();"); } if (state.SINGLETM && locality.getAtomic(lb).get(fsen).intValue()>0) { //Transaction set element case - if (!state.STMARRAY&&wb.needBarrier(fsen)&& + if (wb.needBarrier(fsen)&& locality.getNodePreTempInfo(lb, fsen).get(fsen.getDst())!=LocalityAnalysis.SCRATCH) { output.println("*((unsigned int *)&("+generateTemp(fm,fsen.getDst(),lb)+"->___objstatus___))|=DIRTY;"); } @@ -4056,7 +4116,20 @@ public class BuildCode { } protected void generateStoreFlatCondBranch(FlatMethod fm, LocalityBinding lb, FlatCondBranch fcb, String label, PrintWriter output) { - output.println("STOREANDBRANCH(!"+generateTemp(fm, fcb.getTest(),lb)+", "+label+"); /* "+fcb.nodeid+" */"); + int left=-1; + int right=-1; + //only record if this group has more than one exit + if (branchanalysis.numJumps(fcb)>1) { + left=branchanalysis.jumpValue(fcb, 0); + right=branchanalysis.jumpValue(fcb, 1); + } + output.println("if (!"+generateTemp(fm, fcb.getTest(),lb)+") {"); + if (right!=-1) + output.println("STOREBRANCH("+right+");"); + output.println("goto "+label+";"); + output.println("}"); + if (left!=-1) + output.println("STOREBRANCH("+left+");"); } protected void generateFlatCondBranch(FlatMethod fm, LocalityBinding lb, FlatCondBranch fcb, String label, PrintWriter output) {