Package soot.toolkits.graph

Examples of soot.toolkits.graph.Block


    try{
      //System.out.println("##entered weakRegionDFS for region " + r);
      this.m_regions.get(new Integer(r)).add(v);
     
      DominatorNode parentOfV = this.m_dom.getParentOf(this.m_dom.getDode(v));
      Block u2 = (parentOfV == null) ? null : (Block)parentOfV.getGode();
     
      List children = this.m_pdom.getChildrenOf(this.m_pdom.getDode(v));
      for(int i = 0; i < children.size(); i++)
      {
        DominatorNode w = (DominatorNode)children.get(i);
        Block u1 = (Block)w.getGode();
 
        if(u2 != null && u1.equals(u2))
        {
          this.weakRegionDFS((Block)w.getGode(), r);     
        }
        else
        {
View Full Code Here


    //regions keep an implicit order of the contained blocks so it matters where blocks are added
    //below.
    this.m_regions.get(new Integer(r)).add2Back(v);
   
    DominatorNode parentOfV = this.m_pdom.getParentOf(this.m_pdom.getDode(v));
    Block u2 = (parentOfV == null) ? null : (Block)parentOfV.getGode();
   
    List children = this.m_dom.getChildrenOf(this.m_dom.getDode(v));
    for(int i = 0; i < children.size(); i++)
    {
      DominatorNode w = (DominatorNode)children.get(i);
      Block u1 = (Block)w.getGode();
     
      if(u2 != null && u1.equals(u2))
      {
        this.weakRegionDFS2((Block)w.getGode(), r);     
      }
      else
      {
View Full Code Here

      {
        Region r = itr.next();
        List<Block> blocks = r.getBlocks();
        for (Iterator<Block> itr1 = blocks.iterator(); itr1.hasNext();)
        {
          Block u = itr1.next();
          m_block2region.put(u, r);
       
        }
      }
    }
View Full Code Here

  {
    String s = new String("");
    s += "Headers: " + cfg.getHeads().size() + " " + cfg.getHeads();
    for (Iterator<Block> it = cfg.iterator(); it.hasNext(); )
    {
        Block node = it.next();
        s += "Node = " + node.toShortString() + "\n";
        s += "Preds:\n";
        for (Iterator<Block> predsIt = cfg.getPredsOf(node).iterator(); predsIt.hasNext(); )
        {
          s += "     ";
          s += predsIt.next().toShortString() + "\n";
        }
        s += "Succs:\n";
        for (Iterator<Block> succsIt = cfg.getSuccsOf(node).iterator(); succsIt.hasNext(); )
        {
          s += "     ";
          s += succsIt.next().toShortString() + "\n";
        }
    }
   
    if(blockDetail)
    {
      s += "Blocks Detail:";
      for (Iterator<Block> it = cfg.iterator(); it.hasNext(); )
      {
          Block node = it.next();
          s += node + "\n";
      }
     
    }
   
View Full Code Here

      for(Iterator<Block> itr = blocks.iterator(); itr.hasNext();)
      {
        /*
         * Add the PDG node corresponding to the CFG block node.
         */
        Block a = itr.next();
        PDGNode pdgNodeOfA = null;
        if(!this.m_obj2pdgNode.containsKey(a))
        {
          pdgNodeOfA = new PDGNode(a, PDGNode.Type.CFGNODE);
          this.addNode(pdgNodeOfA);
          this.m_obj2pdgNode.put(a, pdgNodeOfA);
        }
        else
          pdgNodeOfA = this.m_obj2pdgNode.get(a);
               
        this.addEdge(pdgnode, pdgNodeOfA, "dependency");
        pdgnode.addDependent(pdgNodeOfA);
        //
       
        curNodeInRegion = pdgNodeOfA;
       
        /*
         * For each successor B of A, if B does not post-dominate A, add all the
         * nodes on the path from B to the L in the post-dominator tree, where L is the least
         * common ancestor of A and B in the post-dominator tree (L will be either A itself or
         * the parent of A.).
         */
       
        List<Block> bs = this.m_blockCFG.getSuccsOf(a);
        for(Iterator<Block> bItr = bs.iterator(); bItr.hasNext();)
        {
          List<Block> dependents = new ArrayList<Block>();
         
          Block b = bItr.next();
         
          if(b.equals(a))
            throw new RuntimeException("PDG construction: A and B are not supposed to be the same node!");
         
         
          DominatorNode aDode = pdom.getDode(a);
          DominatorNode bDode = pdom.getDode(b);
         
          //If B post-dominates A, go to the next successor.
          if(pdom.isDominatorOf(bDode, aDode))
            continue;
         
          //FIXME: what if the parent is null?!!
          DominatorNode aParentDode = aDode.getParent();
         
          DominatorNode dode = bDode;
          while(dode != aParentDode)
          {
            dependents.add((Block)dode.getGode());
           
            //This occurs if the CFG is multi-tailed and therefore the pdom is a forest.
            if(dode.getParent() == null)
              //throw new RuntimeException("parent dode in pdom is null: dode is " + aDode);
              break;
            dode = dode.getParent();           
          }
         
          /*
           * If node A is in the dependent list of A, then A is the header of a loop.
           * Otherwise, A could still be the header of a loop or just a simple predicate.
           */
         
          //first make A's pdg node be a conditional (predicate) pdgnode, if it's not already.         
          if(pdgNodeOfA.getAttrib() != PDGNode.Attribute.CONDHEADER)
          {
            PDGNode oldA = pdgNodeOfA;
            pdgNodeOfA = new ConditionalPDGNode(pdgNodeOfA);
            this.replaceInGraph(pdgNodeOfA, oldA);
            pdgnode.removeDependent(oldA);
            this.m_obj2pdgNode.put(a, pdgNodeOfA);
            pdgnode.addDependent(pdgNodeOfA);
            pdgNodeOfA.setAttrib(PDGNode.Attribute.CONDHEADER);
           
            curNodeInRegion = pdgNodeOfA;
          }   
         
          List<Block> copyOfDependents = new ArrayList<Block>();
          copyOfDependents.addAll(dependents);
         
          //First, add the dependency for B and its corresponding region.
          Region regionOfB = block2region.get(b);
          PDGNode pdgnodeOfBRegion = null;
          if(!this.m_obj2pdgNode.containsKey(regionOfB))
          {
            pdgnodeOfBRegion = new PDGNode(regionOfB, PDGNode.Type.REGION);
            this.addNode(pdgnodeOfBRegion);
            this.m_obj2pdgNode.put(regionOfB, pdgnodeOfBRegion);
           
          }
          else
            pdgnodeOfBRegion = this.m_obj2pdgNode.get(regionOfB);
         
          //set the region hierarchy
          regionOfB.setParent(r);
          r.addChildRegion(regionOfB);
         
          //add the dependency edges
          this.addEdge(pdgNodeOfA, pdgnodeOfBRegion, "dependency");
          pdgNodeOfA.addDependent(pdgnodeOfBRegion);
          regions2process.add(regionOfB);
          //now remove b and all the nodes in the same weak region from the list of dependents
          copyOfDependents.remove(b);
          copyOfDependents.removeAll(regionOfB.getBlocks());
       
         
          /* What remains here in the dependence set needs to be processed separately. For
           * each node X remained in the dependency set, find the corresponding PDG region node
           * and add a dependency edge from the region of B to the region of X. If X's weak
           * region contains other nodes not in the dependency set of A, create a new region
           * for X and add the proper dependency edges (this actually happens if X is the header
           * of a loop and B is a predicate guarding a break/continue.)
           *
           * Note: it seems the only case that there is a node remained in the dependents is when there
           * is a path from b to the header of a loop.
           */
         
          while(!copyOfDependents.isEmpty())
          {
            Block depB = copyOfDependents.remove(0);
            Region rdepB = block2region.get(depB);
           
            /*
             * Actually, there are cases when depB is not the header of a loop and therefore would not dominate the current node
             * (A) and therefore might not have been created yet. This has happened when an inner loop breaks out of the outer
             * loop but could have other cases too.
             */
            PDGNode depBPDGNode = this.m_obj2pdgNode.get(depB);
            if(depBPDGNode == null)
            {           
              //First, add the dependency for depB and its corresponding region.
             
              PDGNode pdgnodeOfdepBRegion = null;
              if(!this.m_obj2pdgNode.containsKey(rdepB))
              {
                pdgnodeOfdepBRegion = new PDGNode(rdepB, PDGNode.Type.REGION);
                this.addNode(pdgnodeOfdepBRegion);
                this.m_obj2pdgNode.put(rdepB, pdgnodeOfdepBRegion);
               
              }
              else
                pdgnodeOfdepBRegion = this.m_obj2pdgNode.get(rdepB);
             
              //set the region hierarchy
              rdepB.setParent(regionOfB);
              regionOfB.addChildRegion(rdepB);
             
              //add the dependency edges
              this.addEdge(pdgnodeOfBRegion, pdgnodeOfdepBRegion, "dependency");
              pdgnodeOfBRegion.addDependent(pdgnodeOfdepBRegion);
              regions2process.add(rdepB);
             
              //now remove all the nodes in the same weak region from the list of dependents
             
              copyOfDependents.removeAll(rdepB.getBlocks());
             
              continue;

            }
           
            /**
             * If all the nodes in the weak region of depB are dependent on A, then add
             * an edge from the region of B to the region of depB.
             *
             * else, a new region has to be created to contain the dependences of depB, if
             * not already created.
             */
            if(dependents.containsAll(rdepB.getBlocks()))
            {
              /*
               * Just add an edge to the pdg node of the existing depB region.
               *
               */
              //add the dependency edges
              //First, add the dependency for depB and its corresponding region.
             
              PDGNode pdgnodeOfdepBRegion = null;
              if(!this.m_obj2pdgNode.containsKey(rdepB))
              {
                pdgnodeOfdepBRegion = new PDGNode(rdepB, PDGNode.Type.REGION);
                this.addNode(pdgnodeOfdepBRegion);
                this.m_obj2pdgNode.put(rdepB, pdgnodeOfdepBRegion);
               
              }
              else
                pdgnodeOfdepBRegion = this.m_obj2pdgNode.get(rdepB);
             
              //set the region hierarchy
              //Do not set this because the region was created before so must have the
              //proper parent already.
              //rdepB.setParent(regionOfB);
              //regionOfB.addChildRegion(rdepB);
             
              this.addEdge(pdgnodeOfBRegion, pdgnodeOfdepBRegion, "dependency");
              pdgnodeOfBRegion.addDependent(pdgnodeOfdepBRegion);
              regions2process.add(rdepB);
             
              //now remove all the nodes in the same weak region from the list of dependents
             
              copyOfDependents.removeAll(rdepB.getBlocks());
 
              continue;
            }
            else
            {
              PDGNode predPDGofdepB = (PDGNode) this.getPredsOf(depBPDGNode).get(0);
              assert(this.m_obj2pdgNode.containsKey(rdepB));             
              PDGNode pdgnodeOfdepBRegion = this.m_obj2pdgNode.get(rdepB);
              //If the loop header has not been separated from its weak region yet
              if(predPDGofdepB == pdgnodeOfdepBRegion)
              {
                /*
                 * Create a new region to represent the whole loop. In fact, this is a strong
                 * region as opposed to the weak regions that were created in the RegionAnalysis.
                 * This strong region only contains the header of the loop, A, and is dependent
                 * on it. Also, A is dependent on this strong region as well.
                 */
               
                Region newRegion = new Region(this.m_strongRegionStartID++, topLevelRegion.getSootMethod(), topLevelRegion.getSootClass(), this.m_cfg);
                newRegion.add(depB);
               
                this.m_strongRegions.add(newRegion);
                               
                //toBeRemoved.add(depB);
                List<Block> blocks2BRemoved;
                if(toBeRemoved.contains(predPDGofdepB))
                  blocks2BRemoved = toBeRemoved.get(predPDGofdepB);
                else
                {
                  blocks2BRemoved = new ArrayList<Block>();
                  toBeRemoved.put(rdepB, blocks2BRemoved);
                }
                blocks2BRemoved.add(depB);
               
               
               
                PDGNode newpdgnode = new LoopedPDGNode(newRegion, PDGNode.Type.REGION, depBPDGNode);
                this.addNode(newpdgnode);
                this.m_obj2pdgNode.put(newRegion, newpdgnode);
                newpdgnode.setAttrib(PDGNode.Attribute.LOOPHEADER);
                depBPDGNode.setAttrib(PDGNode.Attribute.LOOPHEADER);
               
                this.removeEdge(pdgnodeOfdepBRegion, depBPDGNode, "dependency");
                pdgnodeOfdepBRegion.removeDependent(depBPDGNode);
                this.addEdge(pdgnodeOfdepBRegion, newpdgnode, "dependency");
                this.addEdge(newpdgnode, depBPDGNode, "dependency");
                pdgnodeOfdepBRegion.addDependent(newpdgnode);
                newpdgnode.addDependent(depBPDGNode);
               
               
               
                //If a is dependent on itself (simple loop)
                if(depB == a)
                {
                  PDGNode loopBodyPDGNode = (PDGNode) this.getSuccsOf(depBPDGNode).get(0);
                  this.addEdge(depBPDGNode, newpdgnode, "dependency-back");
                  ((LoopedPDGNode)newpdgnode).setBody(loopBodyPDGNode);
                 
                  depBPDGNode.addBackDependent(newpdgnode);
                 
                  //This is needed to correctly adjust the prev/next pointers for the new loop node. We should not need
                  //to adjust the old loop header node because the prev/next should not have been set previously for it.
                  curNodeInRegion = newpdgnode;
                 
                }
                else
                {
                 
                  //this is a back-dependency
                  pdgnodeOfBRegion.addBackDependent(newpdgnode);
                  this.addEdge(pdgnodeOfBRegion, newpdgnode, "dependency-back");
                 
                  //DEtermine which dependent of the loop header is actually the loop body region
                  PDGNode loopBodyPDGNode = null;
                  List<PDGNode> successors = this.getSuccsOf(depBPDGNode);
                  Iterator<PDGNode> succItr = successors.iterator();
                  while(succItr.hasNext())
                  {
                    PDGNode succRPDGNode = succItr.next();
                   
                    assert(succRPDGNode.getType() == PDGNode.Type.REGION);
                    Region succR = (Region) succRPDGNode.getNode();
                    Block h = succR.getBlocks().get(0);
                   
                    DominatorNode hdode = dom.getDode(h);
                    DominatorNode adode = dom.getDode(a);
                   
                    if(dom.isDominatorOf(hdode, adode))
View Full Code Here

        // this will help us figure out where locals are defined.
        //SimpleLocalDefs localDefs = new SimpleLocalDefs(unitGraph);
        //SimpleLiveLocals liveLocals = new SimpleLiveLocals(unitGraph);

        for (Iterator blocks = graph.iterator(); blocks.hasNext();) {
            Block block = (Block) blocks.next();

            // filter out anything that doesn't look like a loop block.
            if ((block.getPreds().size() != 1)
                    || (block.getSuccs().size() != 1)) {
                continue;
            }

            // filter out anything that isn't attached to something
            // that looks like a conditional jump.
            Block conditional = (Block) block.getSuccs().get(0);

            if ((conditional != block.getPreds().get(0))
                    || (conditional.getPreds().size() != 2)
                    || (conditional.getSuccs().size() != 2)) {
                continue;
            }

            Block whilePredecessor = (Block) conditional.getPreds().get(0);

            if (whilePredecessor == block) {
                whilePredecessor = (Block) conditional.getPreds().get(1);
            }
View Full Code Here

            JimpleBody body = (JimpleBody) method.retrieveActiveBody();

            BlockGraph graph = new CompleteBlockGraph(body);

            for (Iterator blocks = graph.iterator(); blocks.hasNext();) {
                Block block = (Block) blocks.next();

                // System.out.println("body = " + block);
                // filter out anything that doesn't look like a loop body.
                if ((block.getPreds().size() != 1)
                        || (block.getSuccs().size() != 1)) {
                    continue;
                }

                // filter out anything that isn't attached to something
                // that looks like a conditional jump.
                Block whileCond = (Block) block.getSuccs().get(0);

                //  System.out.println("cond = " + whileCond);
                if ((whileCond != block.getPreds().get(0))
                        || (whileCond.getPreds().size() != 2)
                        || (whileCond.getSuccs().size() != 2)) {
                    continue;
                }

                // filter out anything that doesn't start with a call
                // to hasNext().
                if (!(whileCond.getHead() instanceof DefinitionStmt)) {
                    continue;
                }

                DefinitionStmt stmt = (DefinitionStmt) whileCond.getHead();

                if (!(stmt.getRightOp() instanceof InterfaceInvokeExpr)) {
                    continue;
                }

                InterfaceInvokeExpr expr = (InterfaceInvokeExpr) stmt
                        .getRightOp();

                if (expr.getMethod() != iteratorHasNextMethod) {
                    continue;
                }

                // At this point we know we have a while (hasNext()) loop.
                // Now go check for iterator is defined...  it should be just
                // above
                Local iteratorLocal = (Local) expr.getBase();
                Block whilePredecessor = (Block) whileCond.getPreds().get(0);

                if (whilePredecessor == block) {
                    whilePredecessor = (Block) whileCond.getPreds().get(1);
                }

                // System.out.println("whilePredecessor = " + whilePredecessor);
                Unit unit = whilePredecessor.getTail();
                boolean found = false;

                // walk backwards until we find a definition of the iterator.
                while ((unit != whilePredecessor.getHead()) && !found) {
                    if (unit instanceof DefinitionStmt
                            && ((DefinitionStmt) unit).getLeftOp().equals(
                                    iteratorLocal)) {
                        found = true;
                    } else {
                        unit = whilePredecessor.getPredOf(unit);
                    }
                }

                //  System.out.println("iterator def = " + unit);
                DefinitionStmt iteratorDefinition = ((DefinitionStmt) unit);

                if (!(iteratorDefinition.getRightOp() instanceof InterfaceInvokeExpr)
                        || !((InterfaceInvokeExpr) iteratorDefinition
                                .getRightOp()).getMethod().getName().equals(
                                "iterator")) {
                    continue;
                }

                Local collectionLocal = (Local) ((InterfaceInvokeExpr) iteratorDefinition
                        .getRightOp()).getBase();

                //  System.out.println("collection Local = " + collectionLocal);
                found = false;

                // Walk backward again until we reach the definition
                // of the collection.
                while ((unit != whilePredecessor.getHead()) && !found) {
                    if (unit instanceof DefinitionStmt
                            && ((DefinitionStmt) unit).getLeftOp().equals(
                                    collectionLocal)) {
                        found = true;
                    } else {
                        unit = whilePredecessor.getPredOf(unit);
                    }
                }

                //  System.out.println("collection def = " + unit);
                // System.out.println("field = " + field);
View Full Code Here

TOP

Related Classes of soot.toolkits.graph.Block

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.