Skip to content

Commit

Permalink
[InstCombine] Avoid simplifying bitcast of undef to a zeroinitializer…
Browse files Browse the repository at this point in the history
… vector (llvm#108872)

In some cases, if an undef value is the product of another instcombine
simplification, a bitcast of undef is simplified to a zeroinitializer
vector instead of undef.
  • Loading branch information
AlexMaclean committed Sep 17, 2024
1 parent b846638 commit 790f2eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,11 @@ static Value *optimizeIntegerToVectorInsertions(BitCastInst &CI,
auto *DestVecTy = cast<FixedVectorType>(CI.getType());
Value *IntInput = CI.getOperand(0);

// if the int input is just an undef value do not try to optimize to vector
// insertions as it will prevent undef propagation
if (isa<UndefValue>(IntInput))
return nullptr;

SmallVector<Value*, 8> Elements(DestVecTy->getNumElements());
if (!collectInsertionElements(IntInput, 0, Elements,
DestVecTy->getElementType(),
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/Transforms/InstCombine/bitcast.ll
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,26 @@ define half @copysign_idiom_constant_wrong_type2(bfloat %x, i16 %mag) {
%y = bitcast i16 %res to half
ret half %y
}

define i16 @bitcast_undef_to_vector() {
; CHECK-LABEL: @bitcast_undef_to_vector(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[END:%.*]]
; CHECK: unreachable:
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: ret i16 undef
;
entry:
br label %end

unreachable: ; No predecessors!
%0 = extractvalue { i32, i32 } zeroinitializer, 1
br label %end

end: ; preds = %unreachable, %entry
%1 = phi i32 [ %0, %unreachable ], [ undef, %entry ]
%2 = bitcast i32 %1 to <2 x i16>
%3 = extractelement <2 x i16> %2, i64 0
ret i16 %3
}

0 comments on commit 790f2eb

Please sign in to comment.