-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/variable renaming #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mateusluizfb
wants to merge
81
commits into
PAMunb:master
Choose a base branch
from
mateusluizfb:feature/variable-renaming
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 63 commits
Commits
Show all changes
81 commits
Select commit
Hold shift + click to select a range
b3973dc
Implement dominance tree
mateusluizfb 5811942
Implement dominance frontier
mateusluizfb 4a01850
Fix some paths
mateusluizfb f163807
Implement a first version of the phi-function insertion
mateusluizfb 6d0da73
Some fixes in the phi insertion
mateusluizfb 17b8862
Refactor phi function insertion
mateusluizfb 0cfca0e
Create variable renaming module
mateusluizfb 2006b29
Remove phi function extracted code
mateusluizfb d51d120
Add Stack implementation code
mateusluizfb 56e18d5
Import stack in the variable renaming code
mateusluizfb ad0e7fa
Remove Util code and leave in the modules
mateusluizfb 5939bfb
Rename stack's empty() to emptyStack()
mateusluizfb 5a17d79
Implement first part of the variable renaming
mateusluizfb 74f80c0
Fix some mehods arguments
mateusluizfb 22245ce
Creating a new variable version
mateusluizfb 6f5480b
Fix stack peeek for int value
mateusluizfb b4ff2af
Correctly verify right hand side variables
mateusluizfb 1848186
Change right hand side list iteration
mateusluizfb 37f4e88
Fix right hand side iteration
mateusluizfb 0bcad7c
Only insert phi functions in join nodes
mateusluizfb f48f3d2
Replace a simple right hand side variable
mateusluizfb 767f49b
Fix left variable replacement
mateusluizfb 6733177
Usign block by block DFS instead the common variable iteration
mateusluizfb 9e9753a
Beginning to phi function renaming
mateusluizfb fee15d2
Change the block iteration strategy
mateusluizfb 0c7ee20
Adjust left and right hand side renaming
mateusluizfb 2bd3ae2
Adjust tests setup
mateusluizfb 25dc1f1
Pop queue in the end of each execution
mateusluizfb 684f585
Starting to rename another variable in deeper immediates
mateusluizfb 6135298
Refact Variable Renaming
mateusluizfb 2ea8a77
Refactor variable renaming
mateusluizfb fb3ff82
Adding and moving ssa tests
mateusluizfb f90cb73
Remove unused test from VariableRenaming
mateusluizfb ff486fd
Merge branch 'master' into feature/variable-renaming
mateusluizfb 1fc6056
Fix ssa tests namespace
mateusluizfb 488fa1e
Update phi function insertion
mateusluizfb 5063f8e
Chante test classfile
mateusluizfb 394a082
Update java 8 test file
mateusluizfb 55863c2
Rename left hand side of a phiFunction
mateusluizfb 6e3c843
Test a simple exception case
mateusluizfb 2b9645c
Refactor stack of variable version access
mateusluizfb 03d6a00
Refactor variable version stacks access
mateusluizfb 3f069ce
Use string as key in the variable version stack
mateusluizfb f523e55
Match only local immediates with name
mateusluizfb 43737be
Fix assignment check without args case
mateusluizfb 7561cd0
DFS through an idom tree instead the CFG
mateusluizfb 98c04df
Create a module to run the SSA transformation
mateusluizfb f79e8db
Add some tests to the SSA generator
mateusluizfb 62df263
Rename invoke args
mateusluizfb 6020750
Match method invoke with the right matchs
mateusluizfb b05df0f
Unsupport invokeStmt operators when applying SSA transformation
mateusluizfb 40c72e5
Adding some comments
mateusluizfb a366970
Rename rhs expressions
mateusluizfb f5fee38
Fix phiFunction renaming by looking up via DFS
mateusluizfb ccc7921
Adding support for left hand side array
mateusluizfb 453ddf2
Support right hand side array
mateusluizfb 140555e
Test soot example code
mateusluizfb de2ad22
Organazing some tests
mateusluizfb 0c08d53
Refacto SSA integration tests
mateusluizfb e34d1af
Add new test samples
mateusluizfb a08c8ef
Add test for all math problems
mateusluizfb 1628f80
Add comment to test
mateusluizfb 5cf7d66
Rename variable renaming stacks
mateusluizfb 8330998
Refactor phi-function insertion module
mateusluizfb 5da3143
Revert pom.xml
mateusluizfb 35477ef
Merge branch 'master' into feature/variable-renaming
mateusluizfb 960c69a
Extracting helper methods to another file
mateusluizfb e8a8731
Fix CI build issues
mateusluizfb 1b6f132
Fix tests
mateusluizfb 02452bf
Rewrite phi-function insertion algorithm
mateusluizfb 9d0c4e1
Rewrite dominance-frontier creation algorithm
mateusluizfb 66f2e5d
Minor fixes in the phi-function insertion
mateusluizfb d8a7384
Refactor dominance tree creation
mateusluizfb 6267bbc
Simpler dom frontier implementation
mateusluizfb 44b6cf2
Revert "Simpler dom frontier implementation"
mateusluizfb d457b36
Implement the more efficient way compute dom frontier
mateusluizfb 2fdcf78
Fix dominance frontier test
mateusluizfb f230dc5
Fix tests and run phi insertion for join nodes only
mateusluizfb 8fc6e15
Fixing some tests
mateusluizfb ad31c15
Simplify again the dominance frontier algorithm
mateusluizfb 493ce83
Fix some tests
mateusluizfb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/rascal/lang/jimple/toolkit/ssa/DominanceFrontier.rsc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| module lang::jimple::toolkit::ssa::DominanceFrontier | ||
|
|
||
| import Set; | ||
| import analysis::graphs::Graph; | ||
| import lang::jimple::toolkit::FlowGraph; | ||
| import lang::jimple::toolkit::ssa::DominanceTree; | ||
|
|
||
| public map[Node, set[Node]] createDominanceFrontier(FlowGraph flowGraph, map[&T, set[&T]] dominanceTree) { | ||
| dominanceFrontiers = (); | ||
|
|
||
| for(<origin, child> <- flowGraph) { | ||
| if(true) | ||
| dominanceFrontiers = calculateDominanceFrontier(origin, child, dominanceFrontiers, dominanceTree); | ||
| }; | ||
|
|
||
| return dominanceFrontiers; | ||
| } | ||
|
|
||
| public map[Node, set[Node]] calculateDominanceFrontier(Node origin, Node destination, map[Node, set[Node]] dominanceFrontier, map[&T, set[&T]] dominanceTree) { | ||
| temp = origin; | ||
|
|
||
| while(temp != findIdom(dominanceTree, destination)) { | ||
| originDominanceFrontierValue = if(dominanceFrontier[temp]?) dominanceFrontier[temp]; else {}; | ||
| dominanceFrontier[temp] = originDominanceFrontierValue + {destination}; | ||
|
|
||
| temp = findIdom(dominanceTree, temp); | ||
| }; | ||
|
|
||
| return dominanceFrontier; | ||
| } | ||
|
|
||
| public bool isJoinNode(FlowGraph flowGraph, Node child) { | ||
| return size(predecessors(flowGraph, child)) >= 2; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,62 @@ | ||
| module lang::jimple::toolkit::ssa::DominanceTree | ||
|
|
||
| import demo::Dominators; | ||
| import Set; | ||
| import Relation; | ||
| import lang::jimple::toolkit::FlowGraph; | ||
| import analysis::graphs::Graph; | ||
|
|
||
| import List; | ||
| public map[&T, set[&T]] createDominanceTree(Graph[&T] graph) { | ||
| PRED = graph; | ||
| ROOT = entryNode(); | ||
|
|
||
| public map[Node, Node] createDominanceTree(FlowGraph flowGraph) { | ||
| result = (computeDominator(flowGraph, a): a | <a, _> <- flowGraph); | ||
| return result; | ||
| set[&T] rootDominators = reachX(PRED, {ROOT}, {}); | ||
| set[&T] VERTICES = carrier(PRED); | ||
|
|
||
| temp = dominators(PRED, ROOT); | ||
|
|
||
| return (V: (rootDominators - reachX(removeNodeFromGraph(graph, V), {ROOT}, {V}) - {V}) | &T V <- VERTICES ); | ||
| } | ||
|
|
||
| public map[Node, list[Node]] createAdjacenciesMatrix(FlowGraph flowGraph) { | ||
| blockTree = ( origin: [] | <origin, _> <- flowGraph); | ||
|
|
||
| for(<origin, destination> <- flowGraph) { | ||
| blockTree[origin] = blockTree[origin] + [destination]; | ||
| }; | ||
|
|
||
| return blockTree; | ||
| } | ||
|
|
||
| public map[Node, list[Node]] createIdomTree(map[&T, set[&T]] dominanceTree) { | ||
| blockTree = ( treeKey: [] | treeKey <- dominanceTree); | ||
|
|
||
| for(treeKey <- dominanceTree) { | ||
| idom = findIdom(dominanceTree, treeKey); | ||
|
|
||
| if(treeKey != entryNode()) | ||
| blockTree[idom] = blockTree[idom] + [treeKey]; | ||
| }; | ||
|
|
||
| return blockTree; | ||
| } | ||
|
|
||
| public Graph[&T] removeNodeFromGraph(Graph[&T] graph, &T nodeToRemove) { | ||
| return { <father, graphChild> | <father, graphChild> <- graph, nodeToRemove != father || nodeToRemove == entryNode() }; | ||
| } | ||
|
|
||
| public Node computeDominator(FlowGraph flowGraph, Node currentNode) { | ||
| predecessorNodeList = [a | <a, b> <- flowGraph, b == currentNode]; | ||
| result = if(isEmpty(predecessorNodeList) == true) currentNode; else head(predecessorNodeList); | ||
| return result; | ||
|
|
||
| public Node findIdom(map[&T, set[&T]] dominanceTree, Node child) { | ||
| ROOT = entryNode(); | ||
| idom = entryNode(); | ||
|
|
||
| possibleIdoms = [ father | father <- dominanceTree, child in dominanceTree[father] ]; | ||
|
|
||
| for(possibleIdom <- possibleIdoms) { | ||
| if(size(dominanceTree[possibleIdom]) < size(dominanceTree[idom])) { | ||
| idom = possibleIdom; | ||
| }; | ||
| }; | ||
|
|
||
| return idom; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| module lang::jimple::toolkit::ssa::Generator | ||
|
|
||
| import List; | ||
|
|
||
| import lang::jimple::toolkit::FlowGraph; | ||
| import lang::jimple::core::Syntax; | ||
|
|
||
| import lang::jimple::toolkit::ssa::DominanceTree; | ||
| import lang::jimple::toolkit::ssa::PhiFunctionInsertion; | ||
| import lang::jimple::toolkit::ssa::DominanceFrontier; | ||
| import lang::jimple::toolkit::ssa::VariableRenaming; | ||
|
|
||
| public FlowGraph applySSATransformation(MethodBody methodBody) { | ||
| FlowGraph flowGraph = forwardFlowGraph(methodBody); | ||
|
|
||
| if(hasUnsupportedInstruction(flowGraph)) // Doest not support invokeStmt and fieldRef rename | ||
| return {}; | ||
|
|
||
| map[&T, set[&T]] dominanceTree = createDominanceTree(flowGraph); | ||
| map[&T, set[&T]] dominanceFrontier = createDominanceFrontier(flowGraph, dominanceTree); | ||
| FlowGraph phiFunctionFlowGraph = insertPhiFunctions(flowGraph, dominanceFrontier); | ||
| result = applyVariableRenaming(phiFunctionFlowGraph); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applies each SSA's stages |
||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public bool hasUnsupportedInstruction(FlowGraph flowGraph) { | ||
| unsupportedList = [ <origin, destination> | <origin, destination> <- flowGraph, unspportedStatement(origin) || unspportedStatement(destination)]; | ||
| return size(unsupportedList) != 0; | ||
| } | ||
| public bool unspportedStatement(Node nodeStatement) { | ||
| switch(nodeStatement) { | ||
| case stmtNode(invokeStmt(_)): return true; | ||
| case stmtNode(assign(fieldRef(_, _), _)): return true; | ||
| default: return false; | ||
| } | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
src/main/rascal/lang/jimple/toolkit/ssa/PhiFunctionInsertion.rsc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| module lang::jimple::toolkit::ssa::PhiFunctionInsertion | ||
|
|
||
| import demo::Dominators; | ||
| import Set; | ||
| import Relation; | ||
| import analysis::graphs::Graph; | ||
| import lang::jimple::toolkit::FlowGraph; | ||
| import lang::jimple::core::Syntax; | ||
| import Type; | ||
| import IO; | ||
| import Node; | ||
| 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 | ||
| for(variableNode <- blocksWithVariable(flowGraph, variable)) { // d ∈ Defs(v) | ||
| B = variableNode; | ||
| W = W + {B}; | ||
| }; | ||
| while(size(W) != 0) { | ||
| // remove a basic block X from W | ||
| tuple[Node, set[Node]] elements = takeOneFrom(W); | ||
| X = elements[0]; | ||
| W = 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} | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| return newFlowGraph; | ||
| } | ||
|
||
| public bool isJoinNode(FlowGraph flowGraph, Node frontierNode) { | ||
| int fathersSize = size([ fatherNode | <fatherNode, childNode> <- flowGraph, childNode == frontierNode ]); | ||
| return fathersSize > 1; | ||
| } | ||
| public &T getStmtVariable(Node graphNode) { | ||
| stmtNode(assignStatement) = graphNode; | ||
| variableArg = assignStatement[0]; | ||
| return variableArg; | ||
| } | ||
| public list[Node] blocksWithVariable(FlowGraph flowGraph, Variable variable) { | ||
| return [graphNode | <graphNode, _> <- flowGraph, isSameVariable(graphNode, 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; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on https://homepages.cwi.nl/~paulk/publications/rascal-gttse-final.pdf