Skip to content

Commit 790f2eb

Browse files
authored
[InstCombine] Avoid simplifying bitcast of undef to a zeroinitializer vector (#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.
1 parent b846638 commit 790f2eb

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,11 @@ static Value *optimizeIntegerToVectorInsertions(BitCastInst &CI,
23232323
auto *DestVecTy = cast<FixedVectorType>(CI.getType());
23242324
Value *IntInput = CI.getOperand(0);
23252325

2326+
// if the int input is just an undef value do not try to optimize to vector
2327+
// insertions as it will prevent undef propagation
2328+
if (isa<UndefValue>(IntInput))
2329+
return nullptr;
2330+
23262331
SmallVector<Value*, 8> Elements(DestVecTy->getNumElements());
23272332
if (!collectInsertionElements(IntInput, 0, Elements,
23282333
DestVecTy->getElementType(),

llvm/test/Transforms/InstCombine/bitcast.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,26 @@ define half @copysign_idiom_constant_wrong_type2(bfloat %x, i16 %mag) {
879879
%y = bitcast i16 %res to half
880880
ret half %y
881881
}
882+
883+
define i16 @bitcast_undef_to_vector() {
884+
; CHECK-LABEL: @bitcast_undef_to_vector(
885+
; CHECK-NEXT: entry:
886+
; CHECK-NEXT: br label [[END:%.*]]
887+
; CHECK: unreachable:
888+
; CHECK-NEXT: br label [[END]]
889+
; CHECK: end:
890+
; CHECK-NEXT: ret i16 undef
891+
;
892+
entry:
893+
br label %end
894+
895+
unreachable: ; No predecessors!
896+
%0 = extractvalue { i32, i32 } zeroinitializer, 1
897+
br label %end
898+
899+
end: ; preds = %unreachable, %entry
900+
%1 = phi i32 [ %0, %unreachable ], [ undef, %entry ]
901+
%2 = bitcast i32 %1 to <2 x i16>
902+
%3 = extractelement <2 x i16> %2, i64 0
903+
ret i16 %3
904+
}

0 commit comments

Comments
 (0)