Skip to content

Commit

Permalink
Optimize modifier CFGs (#1391)
Browse files Browse the repository at this point in the history
  • Loading branch information
xermicus committed Jun 26, 2023
1 parent a84b0ad commit d0d6528
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/codegen/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,10 @@ pub fn generate_cfg(
}

let mut cfg = function_cfg(contract_no, function_no, ns, opt);
let ast_fn = function_no
.map(ASTFunction::SolidityFunction)
.unwrap_or(ASTFunction::None);
optimize_and_check_cfg(&mut cfg, ns, ast_fn, opt);

if let Some(func_no) = function_no {
let func = &ns.functions[func_no];
Expand All @@ -1488,6 +1492,7 @@ pub fn generate_cfg(
ns,
opt,
);
optimize_and_check_cfg(&mut cfg, ns, ast_fn, opt);
}

cfg.public = public;
Expand All @@ -1496,17 +1501,6 @@ pub fn generate_cfg(
}
}

optimize_and_check_cfg(
&mut cfg,
ns,
if let Some(func_no) = function_no {
ASTFunction::SolidityFunction(func_no)
} else {
ASTFunction::None
},
opt,
);

all_cfgs[cfg_no] = cfg;
}

Expand Down
40 changes: 40 additions & 0 deletions tests/codegen_testcases/solidity/modifier_opt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: --target substrate --emit cfg

contract Test {
// BEGIN-CHECK: Test::testMethod::modifier0::m1
modifier m1() {
require(msg.value != 1 - 1);
_;
require(msg.value != 2 ** 128 - 1);

// CHECK: block0: # entry
// CHECK: branchcond (uint128((builtin Value ())) != uint128 0),
// NOT-CHECK: uint128 1 - uint128 1

// CHECK: branchcond (uint128((builtin Value ())) != uint128 -1),
// NOT-CHECK: uint128 2 ** uint128 127
}

// BEGIN-CHECK: Test::testMethod::modifier1::m2
modifier m2() {
require(msg.value != 2 ** 128 - 1);
_;
require(msg.value != 1 - 1);

// CHECK: block0: # entry
// CHECK: branchcond (uint128((builtin Value ())) != uint128 -1),
// NOT-CHECK: uint128 2 ** uint128 127,

// CHECK: branchcond (uint128((builtin Value ())) != uint128 0),
// NOT-CHECK: uint128 1 - uint128 1
}

// BEGIN-CHECK: Test::function::testMethod
function testMethod() public payable m1 m2 returns (uint256) {
return 2 ** 256 - 1;

// CHECK: block0: # entry
// CHECK: return uint256 -1
// NOT-CHECK: (uint256 2 ** uint256 256) - uint256 1
}
}

0 comments on commit d0d6528

Please sign in to comment.