-
Notifications
You must be signed in to change notification settings - Fork 753
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ==> unsigned(x) > POSITIVE_CONST" 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
) | ||
) | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type must be
i32
because the operation isGtSInt32
.