Skip to content

Commit 7392a54

Browse files
authored
[InstCombine] Treat identical operands as one in pushFreezeToPreventPoisonFromPropagating (#145348)
To push a freeze through an instruction, only one operand may produce poison. However, this currently fails for identical operands which are treated as separate. This patch fixes this by treating them as a single operand.
1 parent 9e5470e commit 7392a54

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4899,13 +4899,14 @@ InstCombinerImpl::pushFreezeToPreventPoisonFromPropagating(FreezeInst &OrigFI) {
48994899
// If operand is guaranteed not to be poison, there is no need to add freeze
49004900
// to the operand. So we first find the operand that is not guaranteed to be
49014901
// poison.
4902-
Use *MaybePoisonOperand = nullptr;
4903-
for (Use &U : OrigOpInst->operands()) {
4904-
if (isa<MetadataAsValue>(U.get()) ||
4905-
isGuaranteedNotToBeUndefOrPoison(U.get()))
4902+
Value *MaybePoisonOperand = nullptr;
4903+
for (Value *V : OrigOpInst->operands()) {
4904+
if (isa<MetadataAsValue>(V) || isGuaranteedNotToBeUndefOrPoison(V) ||
4905+
// Treat identical operands as a single operand.
4906+
(MaybePoisonOperand && MaybePoisonOperand == V))
49064907
continue;
49074908
if (!MaybePoisonOperand)
4908-
MaybePoisonOperand = &U;
4909+
MaybePoisonOperand = V;
49094910
else
49104911
return nullptr;
49114912
}
@@ -4917,10 +4918,10 @@ InstCombinerImpl::pushFreezeToPreventPoisonFromPropagating(FreezeInst &OrigFI) {
49174918
return OrigOp;
49184919

49194920
Builder.SetInsertPoint(OrigOpInst);
4920-
auto *FrozenMaybePoisonOperand = Builder.CreateFreeze(
4921-
MaybePoisonOperand->get(), MaybePoisonOperand->get()->getName() + ".fr");
4921+
Value *FrozenMaybePoisonOperand = Builder.CreateFreeze(
4922+
MaybePoisonOperand, MaybePoisonOperand->getName() + ".fr");
49224923

4923-
replaceUse(*MaybePoisonOperand, FrozenMaybePoisonOperand);
4924+
OrigOpInst->replaceUsesOfWith(MaybePoisonOperand, FrozenMaybePoisonOperand);
49244925
return OrigOp;
49254926
}
49264927

llvm/test/Transforms/InstCombine/freeze.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ define i32 @early_freeze_test3(i32 %v1) {
142142
ret i32 %v4.fr
143143
}
144144

145+
define i32 @early_freeze_test4(i32 %v1) {
146+
; CHECK-LABEL: @early_freeze_test4(
147+
; CHECK-NEXT: [[V2_FR:%.*]] = freeze i32 [[V2:%.*]]
148+
; CHECK-NEXT: [[V3:%.*]] = mul i32 [[V2_FR]], [[V2_FR]]
149+
; CHECK-NEXT: ret i32 [[V3]]
150+
;
151+
%v2 = mul i32 %v1, %v1
152+
%v2.fr = freeze i32 %v2
153+
ret i32 %v2.fr
154+
}
155+
145156
; If replace all dominated uses of v to freeze(v).
146157

147158
define void @freeze_dominated_uses_test1(i32 %v) {

0 commit comments

Comments
 (0)