Skip to content
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

Prune unreferenced functions from CFG #15592

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions libyul/backends/evm/ControlFlowGraphBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ namespace
/// Removes edges to blocks that are not reachable.
void cleanUnreachable(CFG& _cfg)
{
// If operation is a function call it adds the callee entry as child
auto const addFunctionsEntries = [&_cfg](CFG::BasicBlock* _node, auto&& _addChild)
{
for (auto const& operation: _node->operations)
{
if (auto const* functionCall = std::get_if<CFG::FunctionCall>(&operation.operation))
{
auto const functionInfo = _cfg.functionInfo.at(&(functionCall->function.get()));
_addChild(functionInfo.entry);
}
}
};

// Determine which blocks are reachable from the entry.
util::BreadthFirstSearch<CFG::BasicBlock*> reachabilityCheck{{_cfg.entry}};
for (auto const& functionInfo: _cfg.functionInfo | ranges::views::values)
reachabilityCheck.verticesToTraverse.emplace_back(functionInfo.entry);

reachabilityCheck.run([&](CFG::BasicBlock* _node, auto&& _addChild) {
addFunctionsEntries(_node, _addChild);
visit(util::GenericVisitor{
[&](CFG::BasicBlock::Jump const& _jump) {
_addChild(_jump.target);
Expand All @@ -77,6 +88,16 @@ void cleanUnreachable(CFG& _cfg)
cxx20::erase_if(node->entries, [&](CFG::BasicBlock* entry) -> bool {
return !reachabilityCheck.visited.count(entry);
});

// Remove functions which are never referenced.
_cfg.functions.erase(std::remove_if(_cfg.functions.begin(), _cfg.functions.end(), [&](auto const& item) {
return !reachabilityCheck.visited.count(_cfg.functionInfo.at(item).entry);
}), _cfg.functions.end());

// Remove functionInfos which are never referenced.
cxx20::erase_if(_cfg.functionInfo, [&](auto const& entry) -> bool {
return !reachabilityCheck.visited.count(entry.second.entry);
});
}

/// Sets the ``recursive`` member to ``true`` for all recursive function calls.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
{
function f(a, b, c) -> x { pop(address()) sstore(a, c) pop(callvalue()) x := b }
pop(f(0, 0, 0))
}
// ====
// stackOptimization: true
// ----
// /* "":0:88 */
// /* "":95:105 */
// tag_2
// /* "":103:104 */
// 0x00
// /* "":95:105 */
// dup1
// dup1
// tag_1
// jump // in
// tag_2:
// /* "":91:106 */
// pop
// /* "":0:108 */
// stop
// /* "":6:86 */
// tag_1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
mstore(0x80, x)
if calldataload(0) { sstore(y, y) }
}

f(0, 0)
}
// ====
// stackOptimization: true
// EVMVersion: >=shanghai
// stackOptimization: true
// ----
// /* "":0:88 */
// /* "":90:97 */
// tag_2
// /* "":95:96 */
// 0x00
// /* "":90:97 */
// dup1
// tag_1
// jump // in
// tag_2:
// /* "":0:99 */
// stop
// /* "":4:86 */
// tag_1:
Expand All @@ -21,18 +32,18 @@
// /* "":50:65 */
// calldataload
// /* "":47:82 */
// tag_2
// tag_3
// jumpi
// /* "":21:86 */
// tag_3:
// tag_4:
// /* "":4:86 */
// pop
// jump // out
// /* "":66:82 */
// tag_2:
// tag_3:
// /* "":68:80 */
// dup1
// sstore
// /* "":66:82 */
// 0x00
// jump(tag_3)
// jump(tag_4)
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,42 @@
mstore(0x0340, a19)
x := a20
}

pop(f(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
}
// ====
// stackOptimization: true
// ----
// /* "":0:662 */
// /* "":670:731 */
// tag_2
// /* "":729:730 */
// 0x00
// /* "":670:731 */
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// dup1
// tag_1
// jump // in
// tag_2:
// /* "":666:732 */
// pop
// /* "":0:734 */
// stop
// /* "":6:660 */
// tag_1:
Expand Down
13 changes: 12 additions & 1 deletion test/libyul/evmCodeTransform/stackReuse/function_params.yul
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
{
function f(a, b) { }

f(0, 0)
}
// ====
// stackOptimization: true
// ----
// /* "":0:28 */
// /* "":32:39 */
// tag_2
// /* "":37:38 */
// 0x00
// /* "":32:39 */
// dup1
// tag_1
// jump // in
// tag_2:
// /* "":0:41 */
// stop
// /* "":6:26 */
// tag_1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@
// can be reused.
{
function f(a, b, c, d) -> x, y { }

let x, y := f(0, 0, 0, 0)
}
// ====
// stackOptimization: true
// ----
// /* "":210:252 */
// /* "":268:281 */
// tag_2
// /* "":279:280 */
// 0x00
// /* "":268:281 */
// dup1
// dup1
// dup1
// tag_1
// jump // in
// tag_2:
// /* "":210:283 */
// stop
// /* "":216:250 */
// tag_1:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
{
function f(a, b, c, d) -> x, y { b := 3 let s := 9 y := 2 mstore(s, y) }

let x, y := f(0, 0, 0, 0)
}
// ====
// stackOptimization: true
// ----
// /* "":0:80 */
// /* "":97:110 */
// tag_2
// /* "":108:109 */
// 0x00
// /* "":97:110 */
// dup1
// dup1
// dup1
// tag_1
// jump // in
// tag_2:
// /* "":0:112 */
// stop
// /* "":6:78 */
// tag_1:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
function f() -> x, y { }

let x, y := f()
}
// ====
// stackOptimization: true
// ----
// /* "":0:32 */
// /* "":49:52 */
// tag_2
// tag_1
// jump // in
// tag_2:
// /* "":0:54 */
// stop
// /* "":6:30 */
// tag_1:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
function f() -> x { pop(address()) { pop(callvalue()) } }

pop(f())
}
// ====
// stackOptimization: true
// ----
// /* "":0:65 */
// /* "":74:77 */
// tag_2
// tag_1
// jump // in
// tag_2:
// /* "":70:78 */
// pop
// /* "":0:80 */
// stop
// /* "":6:63 */
// tag_1:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
function f() -> x { pop(address()) let y := callvalue() }

pop(f())
}
// ====
// stackOptimization: true
// ----
// /* "":0:65 */
// /* "":74:77 */
// tag_2
// tag_1
// jump // in
// tag_2:
// /* "":70:78 */
// pop
// /* "":0:80 */
// stop
// /* "":6:63 */
// tag_1:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
function f() -> x { pop(address()) for { pop(callvalue()) } 0 {} { } }

pop(f())
}
// ====
// stackOptimization: true
// ----
// /* "":0:78 */
// /* "":86:89 */
// tag_2
// tag_1
// jump // in
// tag_2:
// /* "":82:90 */
// pop
// /* "":0:92 */
// stop
// /* "":6:76 */
// tag_1:
Expand Down
19 changes: 14 additions & 5 deletions test/libyul/evmCodeTransform/stackReuse/function_retparam_if.yul
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
function f() -> x { pop(address()) if 1 { pop(callvalue()) } }

pop(f())
}
// ====
// stackOptimization: true
// ----
// /* "":0:70 */
// /* "":78:81 */
// tag_2
// tag_1
// jump // in
// tag_2:
// /* "":74:82 */
// pop
// /* "":0:84 */
// stop
// /* "":6:68 */
// tag_1:
Expand All @@ -19,17 +28,17 @@
// /* "":26:40 */
// pop
// /* "":41:66 */
// tag_2
// tag_3
// jumpi
// /* "":24:68 */
// tag_3:
// tag_4:
// /* "":6:68 */
// jump // out
// /* "":46:66 */
// tag_2:
// tag_3:
// /* "":52:63 */
// callvalue
// /* "":48:64 */
// pop
// /* "":46:66 */
// jump(tag_3)
// jump(tag_4)
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{
function f() -> x { pop(address()) leave pop(callvalue()) }

pop(f())
}
// ====
// stackOptimization: true
// ----
// /* "":0:67 */
// /* "":75:78 */
// tag_2
// tag_1
// jump // in
// tag_2:
// /* "":71:79 */
// pop
// /* "":0:81 */
// stop
// /* "":6:65 */
// tag_1:
Expand Down
Loading