Skip to content

xor undef, undef always simplifies to 0 #174220

@Veeloxfire

Description

@Veeloxfire

Tested on x64 clang trunk

int foo_xor() {
    int i;
    int j;

    return i ^ j;
}

During optimization this reaches xor undef, undef
xor undef, undef then "optimizes" to 0

This is special cased to allow a ^ a to always be 0:

case Instruction::Xor:
if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
// Handle undef ^ undef -> 0 special case. This is a common
// idiom (misuse).
return Constant::getNullValue(C1->getType());

I am aware this is UB so the behaviour doesn't necessarily matter but it feels unreasonable that the 2 "different" undef's here xor to 0 and we inject a concrete 0 that the optimizer has to work around
Not sure there is a way in llvm's internals to specify that 2 undef values are different (e.g. if you had a ^ a are both undefs the same pointer?) so it may not be worth investigating but I thought I would raise

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions