]>
git.proxmox.com Git - rustc.git/blob - src/binaryen/src/ir/block-utils.h
2 * Copyright 2017 WebAssembly Community Group participants
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef wasm_ir_block_h
18 #define wasm_ir_block_h
22 #include "ir/branch-utils.h"
23 #include "ir/effects.h"
27 namespace BlockUtils
{
28 // if a block has just one element, it can often be replaced
31 inline Expression
* simplifyToContents(Block
* block
, T
* parent
, bool allowTypeChange
= false) {
32 auto& list
= block
->list
;
33 if (list
.size() == 1 && !BranchUtils::BranchSeeker::hasNamed(list
[0], block
->name
)) {
34 // just one element. try to replace the block
35 auto* singleton
= list
[0];
36 auto sideEffects
= EffectAnalyzer(parent
->getPassOptions(), singleton
).hasSideEffects();
37 if (!sideEffects
&& !isConcreteWasmType(singleton
->type
)) {
38 // no side effects, and singleton is not returning a value, so we can throw away
39 // the block and its contents, basically
40 return Builder(*parent
->getModule()).replaceWithIdenticalType(block
);
41 } else if (block
->type
== singleton
->type
|| allowTypeChange
) {
44 // (side effects +) type change, must be block with declared value but inside is unreachable
45 // (if both concrete, must match, and since no name on block, we can't be
46 // branched to, so if singleton is unreachable, so is the block)
47 assert(isConcreteWasmType(block
->type
) && singleton
->type
== unreachable
);
48 // we could replace with unreachable, but would need to update all
51 } else if (list
.size() == 0) {
52 ExpressionManipulator::nop(block
);
57 // similar, but when we allow the type to change while doing so
59 inline Expression
* simplifyToContentsWithPossibleTypeChange(Block
* block
, T
* parent
) {
60 return simplifyToContents(block
, parent
, true);
66 #endif // wasm_ir_block_h