/* process the list of pending constraints */ protected void processWorklist() { int iter = 0; while (!w.isEmpty()) { int size = w.size(); Constraint c = w.removeFirst(); c.accept(this); size -= w.size(); iter++; } }
/* process field edge constraint */ public void process(FieldConstraint c) { Node csrc = c.src.getRep(); Node cdst = c.dst.getRep(); assert (csrc.graph == this); assert (cdst.graph == this); if (heapfix && csrc.isheap && !cdst.isheap) w.addFirst(new UnifyConstraint(csrc, cdst)); Collection<FieldEdge> fed = fedges.get(c.field); if (secondary_index[9]) { FieldEdge e = csrc.outfields.get(c.field); if (e != null) { if (e.dst != cdst) w.addFirst(new UnifyConstraint(e.dst, cdst)); return; } } else { if (fed != null) for (FieldEdge e : fed) { Node esrc = e.src.getRep(); Node edst = e.dst.getRep(); assert (e.field.equals(c.field)); if (esrc == csrc) { if (edst != cdst) w.addFirst(new UnifyConstraint(edst, cdst)); return; } } } FieldEdge e = new FieldEdge(csrc, cdst, c.field); if (fed == null) { fed = new ArrayList<FieldEdge>(); fedges.put(c.field, fed); } fed.add(e); if (secondary_index[1]) { csrc.outfields.put(e.field, e); addInField(cdst, e.field, e); } // Merge nodes according to Tag information Node find = Options.detailed.value ? reachesTag(csrc, cdst.tag, new HashSet<Node>()) : containsTag(cdst.tag); if (find != null) w.addFirst(new UnifyConstraint(cdst, find)); }
/* process call edge constraint */ public void process(CallConstraint c) { Node csrc = c.ncaller.getRep(); Node cdst = c.ncallee.getRep(); assert (cdst.graph == this); if (heapfix && csrc.graph == this && !csrc.isheap && cdst.isheap) w.addFirst(new UnifyConstraint(csrc, cdst)); Collection<CallEdge> ced = cedges.get(c.call); if (ced != null) for (CallEdge e : ced) { Node esrc = e.src.getRep(); Node edst = e.dst.getRep(); if (edst == cdst && e.call.equals(c.call)) { if (esrc != csrc) { Graph g = esrc.graph; assert (g != this || !Options.mergeGraphs.value); g.w.addFirst(new UnifyConstraint(esrc, csrc)); } return; } } else { ced = new ArrayList<CallEdge>(); cedges.put(c.call, ced); } ced.add(new CallEdge(csrc, cdst, c.call)); }
/* Processing regular assignments. Left and right hand side each may be a variable or the field of a variable. Variables include parameters and locals. */ public void assignExpr(Var x, Field f, Var y, Field g, boolean isBasic) { if (!isBasic) w.addFirst(new UnifyConstraint(getNode(x, f), getNode(y, g))); if (f != null) setTouched(x); if (g != null) setTouched(y); }
/* Processing assignments of actuals to formals at a call site. The first parameter is the call instruction. The second is the formal parameter. The last two represent the variable or the field being passed. For return values, the second parameter is the formal return variable in the callee, and the last two parameters are the variable/field that the call is assigned to. */ public void assignParams(Call call, Var p, Var x, Field f, boolean isBasic) { if (!isBasic) { Method callee = call.getCallee(); Graph gcallee = PointsToAnalysis.v().getGraph(callee); Node nodeL = gcallee.getNode(p, null); Node nodeR = this.getNode(x, f); if (Options.mergeGraphs.value && this == gcallee) { if (p.isReturn()) w.addFirst(new UnifyConstraint(nodeR, nodeL)); else w.addFirst(new UnifyConstraint(nodeL, nodeR)); } callsites.add(call); callees.add(callee); gcallee.callers.add(method); gcallee.w.add(new CallConstraint(nodeR, nodeL, call)); } if (f != null) setTouched(x); }
/* Lookup node for field, create new node if not present */ public Node getNode(Var var, Field field) { Map<Field, Node> map = varmap.get(var); if (map == null) { map = new HashMap<Field, Node>(); varmap.put(var, map); } Node fnode = map.get(field); if (fnode == null) { assert var != null; fnode = new Node(var, field, this); map.put(field, fnode); if (field != null) { Node node = getNode(var, null); w.add(new FieldConstraint(node, fnode, field)); } } return fnode; }
/* process unification constraints */ protected void process(UnifyConstraint c) { Node n1 = c.node1.getRep(); Node n2 = c.node2.getRep(); assert (n1.graph == this); assert (n2.graph == this); if (n1 == n2) return; if (heapfix && n2.isheap && !n1.isheap) { Node sw = n1; n1 = n2; n2 = sw; } n1.mergeTags(n2); n1.mergeFlags(n2); n2.parent = n1; n2.tag = null; n2.region = null; if (secondary_index[0]) { for (FieldEdge e : n2.outfields.values()) { assert (e.src == n2); if (e.dst == n2) w.addLast(new FieldConstraint(n1, n1, e.field)); else { w.addLast(new FieldConstraint(n1, e.dst, e.field)); e.dst.infields.get(e.field).remove(e); } fedges.get(e.field).remove(e); } for (Field f : n2.infields.keySet()) for (FieldEdge e : n2.infields.get(f)) { assert (e.dst == n2); e.dst = n1; addInField(n1, f, e); } n2.outfields = null; n2.infields = null; } else { HashSet<FieldEdge> fremove = new HashSet<FieldEdge>(); for (Field f : fedges.keySet()) { Collection<FieldEdge> fed = fedges.get(f); int size = fed.size(); fremove.clear(); for (FieldEdge e : fed) { if (e.src == n2) { if (e.dst == n2) w.addLast(new FieldConstraint(n1, n1, e.field)); else w.addLast(new FieldConstraint(n1, e.dst, e.field)); } else { if (e.dst == n2) e.dst = n1; continue; } if (!fremove.contains(e)) { fremove.add(e); size--; } } fed.removeAll(fremove); assert (fed.size() == size); } } HashSet<CallEdge> cremove = new HashSet<CallEdge>(); for (Call ce : cedges.keySet()) { Collection<CallEdge> ced = cedges.get(ce); cremove.clear(); int size = ced.size(); for (CallEdge e : ced) { if (e.dst == n2) { if (e.src == n2) w.addLast(new CallConstraint(n1, n1, e.call)); else w.addLast(new CallConstraint(e.src, n1, e.call)); } else { if (e.src == n2) e.src = n1; continue; } if (!cremove.contains(e)) { cremove.add(e); size--; } } ced.removeAll(cremove); assert (ced.size() == size); } }