Skip to content

Missed optimization: clz and ctz #11725

@khagankhan

Description

@khagankhan

Missed optimization for leading (clz) and trailing (ctz) zeros.

Absolute minimum WAT file:

(module
  (func $main (export "main") (result i32)
    i32.const -1    
    i32.clz         
  )
)
Not optimized for a constant matching execution (0). Dumping CLIF from /tmp/.tmpZ8O1nN/wasm[0]--function[0]--main.clif:

=================== CLIF BEGIN ===================
;; Intermediate Representation of function <wasm[0]::function[0]::main>:
function u0:0(i64 vmctx, i64) -> i32 tail {
    gv0 = vmctx
    gv1 = load.i64 notrap aligned readonly gv0+8
    gv2 = load.i64 notrap aligned gv1+16
    stack_limit = gv2

                                block0(v0: i64, v1: i64):
@0025                               jump block1

                                block1:
@0022                               v3 = iconst.i32 -1
@0024                               v4 = clz v3  ; v3 = -1
@0025                               return v4
}

==================== CLIF END ====================
Aborted

P.S. I want to create issues and then create PRs, which, if deemed fit, may be added about missed optimizations. I would also suggest having a "missed-optimization" label like LLVM has, which can be helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions