]> git.proxmox.com Git - rustc.git/blob - src/binaryen/src/ir/block-utils.h
New upstream version 1.23.0+dfsg1
[rustc.git] / src / binaryen / src / ir / block-utils.h
1 /*
2 * Copyright 2017 WebAssembly Community Group participants
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #ifndef wasm_ir_block_h
18 #define wasm_ir_block_h
19
20 #include "literal.h"
21 #include "wasm.h"
22 #include "ir/branch-utils.h"
23 #include "ir/effects.h"
24
25 namespace wasm {
26
27 namespace BlockUtils {
28 // if a block has just one element, it can often be replaced
29 // with that content
30 template<typename T>
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) {
42 return singleton;
43 } else {
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
49 // the parent's types
50 }
51 } else if (list.size() == 0) {
52 ExpressionManipulator::nop(block);
53 }
54 return block;
55 }
56
57 // similar, but when we allow the type to change while doing so
58 template<typename T>
59 inline Expression* simplifyToContentsWithPossibleTypeChange(Block* block, T* parent) {
60 return simplifyToContents(block, parent, true);
61 }
62 };
63
64 } // namespace wasm
65
66 #endif // wasm_ir_block_h
67