Exemplo n.º 1
0
  protected void eliminateDeadCode(JilMethod method) {
    HashMap<String, Integer> labels = new HashMap();
    List<JilStmt> body = method.body();
    Stack<Integer> worklist = new Stack();
    HashSet<Integer> visited = new HashSet();

    // first, initialiser label map
    int pos = 0;
    for (JilStmt s : body) {
      if (s instanceof JilStmt.Label) {
        JilStmt.Label lab = (JilStmt.Label) s;
        labels.put(lab.label(), pos);
      }
      pos++;
    }

    worklist.push(0);
    visited.add(0);

    while (!worklist.isEmpty()) {
      int idx = worklist.pop();

      if (idx != body.size()) {
        JilStmt stmt = body.get(idx);
        addSuccessors(worklist, visited, labels, stmt, idx);
      }
    }

    // Ok, now eliminate any dead statements (if there are any)
    if (visited.size() <= body.size()) {
      pos = 0;
      int size = body.size();
      for (int i = 0; i != size; ++i) {
        if (!visited.contains(i)) {
          body.remove(pos);
        } else {
          pos = pos + 1;
        }
      }
    }
  }