Skip to content

Commit

Permalink
Refactor phi-function insertion module
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusluizfb committed Mar 24, 2021
1 parent 5cf7d66 commit 8330998
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/main/rascal/lang/jimple/toolkit/ssa/PhiFunctionInsertion.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ import List;
public FlowGraph insertPhiFunctions(FlowGraph flowGraph, map[&T, set[&T]] dominanceFrontier) {
newFlowGraph = { <origin, destination> | <origin, destination> <- flowGraph };
variableList = { getStmtVariable(graphNode) | <graphNode, _> <- flowGraph, isVariable(graphNode) };

for(Variable variable <- variableList) {
F = {}; // set of basic blocks where φ is added
W = {}; // set of basic blocks that contain definitions of v
phiBasicBlocks = {}; // "F" set of basic blocks where φ is added
basicBlocksContainsVariable = {}; // "W" set of basic blocks that contain definitions of v

for(variableNode <- blocksWithVariable(flowGraph, variable)) { // d ∈ Defs(v)
B = variableNode;
W = W + {B};
tempVariableNode = variableNode;
basicBlocksContainsVariable = basicBlocksContainsVariable + {tempVariableNode};
};
while(size(W) != 0) {

while(size(basicBlocksContainsVariable) != 0) {
// remove a basic block X from W
tuple[Node, set[Node]] elements = takeOneFrom(W);
tuple[Node, set[Node]] elements = takeOneFrom(basicBlocksContainsVariable);
X = elements[0];
W = elements[1];
basicBlocksContainsVariable = elements[1];

if(X in dominanceFrontier) { // Avoids NoSuchKey error
frontierNodes = dominanceFrontier[X];
for(Y <- frontierNodes) { // Y : basic block ∈ DF(X )
if(size({Y} & F) == 0 && isJoinNode(flowGraph, Y)) { // Y \notin F && Y is a join node
newFlowGraph = insertPhiFunction(newFlowGraph, Y, variable); // add v←φ(...) at entry of Y
F = F + {Y}; // F ← F ∪ {Y}
if(size([Y] & blocksWithVariable(flowGraph, variable)) == 0) { // Y \notin Defs(v)
W = W + {Y}; // W ←W ∪{Y}
for(frontierNode <- frontierNodes) { // Y : basic block ∈ DF(X )
if(size({frontierNode} & phiBasicBlocks) == 0 && isJoinNode(flowGraph, frontierNode)) { // Y \notin F && Y is a join node
newFlowGraph = insertPhiFunction(newFlowGraph, frontierNode, variable); // add v←φ(...) at entry of Y
phiBasicBlocks = phiBasicBlocks + {frontierNode}; // F ← F ∪ {Y}
if(size([frontierNode] & blocksWithVariable(flowGraph, variable)) == 0) { // Y \notin Defs(v)
basicBlocksContainsVariable = basicBlocksContainsVariable + {frontierNode}; // W ←W ∪{Y}
};
};
};
Expand Down Expand Up @@ -67,40 +67,40 @@ public list[Node] blocksWithVariable(FlowGraph flowGraph, Variable variable) {

public bool isVariable(Node graphNode) {
if (size(graphNode[..]) == 0) return false;

stmtNode(assignStatement) = graphNode;
if (size(assignStatement[..]) == 0) return false;

variableArg = assignStatement[0];
typeOfVariableArg = typeOf(variableArg);

if (size(typeOfVariableArg[..]) == 0) return false;

return typeOfVariableArg.name == "Variable";
}

public bool isSameVariable(Node graphNode, Variable variable) {
if (size(graphNode[..]) == 0) return false;

stmtNode(assignStatement) = graphNode;
if (size(assignStatement[..]) == 0) return false;

variableArg = assignStatement[0];
typeOfVariableArg = typeOf(variableArg);

if (size(typeOfVariableArg[..]) == 0) return false;

return variableArg == variable;
}

public FlowGraph insertPhiFunction(FlowGraph flowGraph, Node childNode, Variable variable) {
fatherNodes = predecessors(flowGraph, childNode);
phiFunctionStmt = stmtNode(assign(variable, phiFunction(variable, [])));

phiFunctionRelations = { <fatherNode, phiFunctionStmt> | fatherNode <- fatherNodes };
filteredFlowGraph = { <origin, destination> | <origin, destination> <- flowGraph, !(origin in fatherNodes) || !(childNode == destination) };

flowGraphWithPhiFunction = phiFunctionRelations + filteredFlowGraph + {<phiFunctionStmt, childNode>};

return flowGraphWithPhiFunction;
}
}

0 comments on commit 8330998

Please sign in to comment.