summaryrefslogtreecommitdiff
path: root/src/passes/LowerIfElse.cpp
blob: 1b8376ea4ccff146d47d44b447dfc719bea41472 (plain)
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
//
// Lowers if (x) y else z into
//
// L: {
//   if (x) break (y) L
//   z
// }
//
// This is useful for investigating how beneficial if_else is.
//

#include <memory>

#include <wasm.h>
#include <pass.h>

namespace wasm {

struct LowerIfElse : public Pass {
  MixedArena* allocator;
  std::unique_ptr<NameManager> namer;

  void prepare(PassRunner* runner, Module *module) override {
    allocator = runner->allocator;
    namer = std::unique_ptr<NameManager>(new NameManager());
    namer->run(runner, module);
  }

  void visitIf(If *curr) override {
    if (curr->ifFalse) {
      auto block = allocator->alloc<Block>();
      auto name = namer->getUnique("L"); // TODO: getUniqueInFunction
      block->name = name;
      block->list.push_back(curr);
      block->list.push_back(curr->ifFalse);
      curr->ifFalse = nullptr;
      auto break_ = allocator->alloc<Break>();
      break_->name = name;
      break_->value = curr->ifTrue;
      curr->ifTrue = break_;
      replaceCurrent(block);
    }
  }
};

static RegisterPass<LowerIfElse> registerPass("lower-if-else", "lowers if-elses into ifs, blocks and branches");

} // namespace wasm