-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Expand file tree
/
Copy pathConditionalBranchFlattener.h
More file actions
69 lines (58 loc) · 2.07 KB
/
ConditionalBranchFlattener.h
File metadata and controls
69 lines (58 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/AST.h>
namespace solidity::yul
{
/**
* Replaces conditional execution with branchless bitwise operations.
*
* Rewrites:
* if c { x := a }
* to:
* let condition := iszero(iszero(c))
* x := xor(x, and(sub(0, condition), xor(a, x)))
*
* This transformation is applied if:
* - The body of the `if` statement contains a single assignment.
* - All RHS expressions in the assignment are movable.
* - There are no control flow statements in the body.
*
* The logic `xor(x, and(sub(0, condition), xor(a, x)))` effectively implements
* `condition ? a : x` using bitwise operations, relying on `condition` being 0 or 1.
* `sub(0, condition)` generates a mask of all ones if condition is 1, and all zeros if condition
* is 0.
*
* Prerequisites: Disambiguator
*/
class ConditionalBranchFlattener: public ASTModifier
{
public:
static constexpr char const* name = "ConditionalBranchFlattener";
static void run(OptimiserStepContext& _context, Block& _ast);
using ASTModifier::operator();
void operator()(Block& _block) override;
private:
ConditionalBranchFlattener(OptimiserStepContext& _context): m_context(_context) {}
FunctionCall createBuiltinCall(
langutil::DebugData::ConstPtr _debugData,
std::string const& _name,
std::vector<Expression> _arguments
);
OptimiserStepContext& m_context;
};
}