Skip to content

[DAGCombiner] zext (trunc nuw nsw (x)) incorrectly optimized awayย #144736

@dlee992

Description

@dlee992

๐Ÿ’ก Example: Trunc + Zext in LLVM IR

The following function demonstrates a trunc followed by a zext:

define i64 @bar(i64 %a) {
; CHECK-LABEL: bar:
; CHECK:       ; %bb.0:
; CHECK-NEXT:    ret 0
  %trunc = trunc nuw nsw i64 %a to i32
  %res = zext i32 %trunc to i64
  ret i64 %res
}

Ideally, LLVM should emit code that preserves the lower 32 bits of %a and zeros out the upper 32 bits. However, in the latest LLVM, it appears this truncation + zero-extension is incorrectly optimized into a constant 0. Since it's a new feature, I am not sure what's the correct semantics of nuw/nsw in trunc instruction.

This behavior is likely a regression and may be closely related to the following patch:
๐Ÿ‘‰ #113808

Metadata

Metadata

Assignees

No one assigned

    Labels

    llvm:SelectionDAGSelectionDAGISel as wellquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions