summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
blob: efa9409a712e1b8559720c8b9970b193a7a81d5a (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright 2016 WebAssembly Community Group participants
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//
// Optimize combinations of instructions
//

#include <algorithm>

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

namespace wasm {

struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions, Visitor<OptimizeInstructions>>> {
  bool isFunctionParallel() override { return true; }

  Pass* create() override { return new OptimizeInstructions; }

  void visitIf(If* curr) {
    // flip branches to get rid of an i32.eqz
    if (curr->ifFalse) {
      auto condition = curr->condition->dynCast<Unary>();
      if (condition && condition->op == EqZInt32 && condition->value->type == i32) {
        curr->condition = condition->value;
        std::swap(curr->ifTrue, curr->ifFalse);
      }
    }
  }
  void visitUnary(Unary* curr) {
    if (curr->op == EqZInt32) {
      // fold comparisons that flow into an EqZ
      auto* child = curr->value->dynCast<Binary>();
      if (child && (child->type == i32 || child->type == i64)) {
        switch (child->op) {
          case EqInt32:   child->op = NeInt32; break;
          case NeInt32:   child->op = EqInt32; break;
          case LtSInt32:  child->op = GeSInt32; break;
          case LtUInt32:  child->op = GeUInt32; break;
          case LeSInt32:  child->op = GtSInt32; break;
          case LeUInt32:  child->op = GtUInt32; break;
          case GtSInt32:  child->op = LeSInt32; break;
          case GtUInt32:  child->op = LeUInt32; break;
          case GeSInt32:  child->op = LtSInt32; break;
          case GeUInt32:  child->op = LtUInt32; break;
          case EqInt64:   child->op = NeInt64; break;
          case NeInt64:   child->op = EqInt64; break;
          case LtSInt64:  child->op = GeSInt64; break;
          case LtUInt64:  child->op = GeUInt64; break;
          case LeSInt64:  child->op = GtSInt64; break;
          case LeUInt64:  child->op = GtUInt64; break;
          case GtSInt64:  child->op = LeSInt64; break;
          case GtUInt64:  child->op = LeUInt64; break;
          case GeSInt64:  child->op = LtSInt64; break;
          case GeUInt64:  child->op = LtUInt64; break;
          case EqFloat32: child->op = NeFloat32; break;
          case NeFloat32: child->op = EqFloat32; break;
          case LtFloat32: child->op = GeFloat32; break;
          case LeFloat32: child->op = GtFloat32; break;
          case GtFloat32: child->op = LeFloat32; break;
          case GeFloat32: child->op = LtFloat32; break;
          case EqFloat64: child->op = NeFloat64; break;
          case NeFloat64: child->op = EqFloat64; break;
          case LtFloat64: child->op = GeFloat64; break;
          case LeFloat64: child->op = GtFloat64; break;
          case GtFloat64: child->op = LeFloat64; break;
          case GeFloat64: child->op = LtFloat64; break;
          default: return;
        }
        replaceCurrent(child);
      }
    }
  }
};

Pass *createOptimizeInstructionsPass() {
  return new OptimizeInstructions();
}

} // namespace wasm