Skip to content

Commit

Permalink
don't conditionalize an unreachable binary, the if might have a diffe…
Browse files Browse the repository at this point in the history
…rent type
  • Loading branch information
kripken committed Aug 30, 2017
1 parent 2b73e69 commit f23a92d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,9 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
// expensive | cheap can be turned into cheap ? 1 : expensive,
// so that we can avoid one expensive computation, if it has no side effects.
Expression* conditionalizeExpensiveOnBitwise(Binary* binary) {
// if this contains dead code, don't bother trying to optimize it, the type
// might change (if might not be unreachable if just one arm is)
if (binary->type == unreachable) return nullptr;
// this operation can increase code size, so don't always do it
auto& options = getPassRunner()->options;
if (options.optimizeLevel < 2 || options.shrinkLevel > 0) return nullptr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(module
(type $0 (func (param i32 i32) (result i32)))
(type $1 (func (result f64)))
(memory $0 0)
(func $conditionals (type $0) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
Expand Down Expand Up @@ -321,4 +322,41 @@
(get_local $1)
)
)
(func $conditionalize-if-type-change (type $1) (result f64)
(local $0 i32)
(drop
(loop $label$1 (result f32)
(block $label$2 (result f32)
(drop
(block $label$3 (result f32)
(br_if $label$1
(i32.or
(f32.gt
(br_if $label$3
(f32.const 1)
(get_local $0)
)
(br $label$2
(f32.const 71)
)
)
(i64.eqz
(select
(i64.const 58)
(i64.const -982757)
(i64.eqz
(i64.const 0)
)
)
)
)
)
)
)
(f32.const 1)
)
)
)
(f64.const -nan:0xfffffffffffff)
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,43 @@
)
(return (get_local $1))
)

(func $conditionalize-if-type-change (result f64)
(local $0 i32)
(drop
(loop $label$1 (result f32)
(block $label$2 (result f32)
(drop
(block $label$3 (result f32)
(br_if $label$1
(i32.or ;; this turns into an if, but then the if might not be unreachable
(f32.gt
(br_if $label$3
(f32.const 1)
(get_local $0)
)
(br $label$2
(f32.const 71)
)
)
(i64.eqz
(select
(i64.const 58)
(i64.const -982757)
(i64.eqz
(i64.const 0)
)
)
)
)
)
)
)
(f32.const 1)
)
)
)
(f64.const -nan:0xfffffffffffff)
)
)

0 comments on commit f23a92d

Please sign in to comment.