Skip to content

Power of two assumption optimized away #128152

Open
@nikic

Description

@nikic

https://llvm.godbolt.org/z/z3E6641E1

; RUN: opt -S -passes=instsimplify < %s
define i1 @foo(i32 %x) {
  %ctpop = call i32 @llvm.ctpop.i32(i32 %x)
  %cond = icmp eq i32 %ctpop, 1
  %ext = zext i1 %cond to i8
  call void @llvm.assume(i1 %cond)
  %res = icmp eq i32 %x, 0
  ret i1 %res
}

Results in:

define i1 @foo(i32 %x) {
  %res = icmp eq i32 %x, 0
  ret i1 %res
}

Note that the dead %ext is relevant.

Without looking into it, I assume that the problem is that we're using

// ctpop(X) -> 1 iff X is non-zero power of 2.
if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ false, 0, Q.AC, Q.CxtI,
Q.DT))
return ConstantInt::get(Op0->getType(), 1);
with an ephemeral value, and the extra use in %ext is what makes use currently think it is non-ephemeral.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions