Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimization from x < 0 || x > POSITIVE_CONST to unsigned(x) > POSITIVE_CONST #6999

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,25 @@ struct OptimizeInstructions
return replaceCurrent(un);
}
}
{
// x < 0 || x > POSITIVE_CONST ==> x > POSITIVE_CONST (unsigned
// comparison)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// x < 0 || x > POSITIVE_CONST ==> x > POSITIVE_CONST (unsigned
// comparison)
// x < 0 || x > POSITIVE_CONST ==> unsigned(x) > POSITIVE_CONST

(this is the convention we have elsewhere in the file to indicate an unsigned comparison)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Binary* bin;
Expression *x_left, *x_right;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Expression *x_left, *x_right;
Expression *xLeft, *xRight;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Const* y;
if (matches(curr,
binary(&bin,
OrInt32,
binary(LtSInt32, pure(&x_left), i32(0)),
binary(GtSInt32, pure(&x_right), constant(&y)))) &&
y->type == Type::i32 && y->value.geti32() > 0 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
y->type == Type::i32 && y->value.geti32() > 0 &&
y->value.geti32() > 0 &&

The type must be i32 because the operation is GtSInt32.

ExpressionAnalyzer::equal(x_left, x_right)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using pure + ::equal, see areConsecutiveInputsEqualAndFoldable (they are consecutive aside from constants (which are not an issue), we need them to be equal, and we want to fold them together).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

bin->op = GtUInt32;
bin->left = x_left;
bin->right = y;
return;
}
}
{
// x <<>> (C & (31 | 63)) ==> x <<>> C'
// x <<>> (y & (31 | 63)) ==> x <<>> y
Expand Down
168 changes: 168 additions & 0 deletions test/lit/passes/optimize-instructions-cmps.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --optimize-instructions -S -o - | filecheck %s

;; Tests for "x < 0 || x > POSITIVE_CONST ==> x > POSITIVE_CONST (unsigned comparison)" optimization

(module
;; CHECK: (global $g (mut i32) (i32.const 0))
(global $g (mut i32) (i32.const 0))

;; CHECK: (func $cmp (param $0 i32) (result i32)
;; CHECK-NEXT: (i32.gt_u
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 255)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cmp (param $0 i32) (result i32)
(i32.or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments in the tests, like here, "we can optimize this to a single i32.gt_u".

(i32.lt_s
(local.get $0)
(i32.const 0)
)
(i32.gt_s
(local.get $0)
(i32.const 255)
)
)
)

;; CHECK: (func $cmp2 (param $0 i32) (result i32)
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (i32.lt_s
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.gt_s
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const -255)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cmp2 (param $0 i32) (result i32)
(i32.or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on this one could be "we cannot optimize here because the second constant is negative".

(i32.lt_s
(local.get $0)
(i32.const 0)
)
(i32.gt_s
(local.get $0)
(i32.const -255) ;; negative number
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a testcase where both constants are 0.

)
)

;; CHECK: (func $cmp3 (param $0 i32) (result i32)
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (i32.lt_s
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 10)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.gt_s
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 255)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cmp3 (param $0 i32) (result i32)
(i32.or
(i32.lt_s
(local.get $0)
(i32.const 10) ;; note this
)
(i32.gt_s
(local.get $0)
(i32.const 255)
)
)
)

;; CHECK: (func $set_global_and_return (param $x i32) (result i32)
;; CHECK-NEXT: (global.set $g
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
(func $set_global_and_return (param $x i32) (result i32)
(global.set $g (local.get $x)) ;; side-effect
(local.get $x)
)

;; CHECK: (func $cmp4 (result i32)
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (i32.lt_s
;; CHECK-NEXT: (call $set_global_and_return
;; CHECK-NEXT: (i32.const 10)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.gt_s
;; CHECK-NEXT: (call $set_global_and_return
;; CHECK-NEXT: (i32.const 10)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 255)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cmp4 (result i32)
(i32.or
(i32.lt_s
(call $set_global_and_return (i32.const 10)) ;; x with side-effect
(i32.const 0)
)
(i32.gt_s
(call $set_global_and_return (i32.const 10))
(i32.const 255)
)
)
)

;; CHECK: (func $cmp5 (param $0 i32) (param $1 i32) (result i32)
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (i32.lt_s
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.gt_s
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: (i32.const 255)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cmp5 (param $0 i32) (param $1 i32) (result i32)
(i32.or
(i32.lt_s
(local.get $0)
(i32.const 0)
)
(i32.gt_s
(local.get $1) ;; note this
(i32.const 255)
)
)
)

;; CHECK: (func $cmp6 (param $0 i32) (param $1 i32) (result i32)
;; CHECK-NEXT: (i32.or
;; CHECK-NEXT: (i32.lt_s
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.gt_s
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cmp6 (param $0 i32) (param $1 i32) (result i32)
(i32.or
(i32.lt_s
(local.get $0)
(i32.const 0)
)
(i32.gt_s
(local.get $0)
(local.get $1) ;; non-constant
)
)
)
)
Loading