Skip to content

Commit 494847b

Browse files
dtcxzywtstellar
authored andcommittedMay 14, 2024
[InstSimplify] Do not simplify freeze in simplifyWithOpReplaced (#91215)
See the LangRef: > All uses of a value returned by the same ‘freeze’ instruction are guaranteed to always observe the same value, while different ‘freeze’ instructions may yield different values. It is incorrect to replace freezes with the simplified value. Proof: https://alive2.llvm.org/ce/z/3Dn9Cd https://alive2.llvm.org/ce/z/Qyh5h6 Fixes #91178 (cherry picked from commit d085b42) Revert "[InstSimplify] Do not simplify freeze in `simplifyWithOpReplaced` (#91215)" This reverts commit 1c2eb18d52976fef89972e89c52d2ec5ed7e4868. [InstSimplify] Do not simplify freeze in `simplifyWithOpReplaced` (#91215) See the LangRef: > All uses of a value returned by the same ‘freeze’ instruction are guaranteed to always observe the same value, while different ‘freeze’ instructions may yield different values. It is incorrect to replace freezes with the simplified value. Proof: https://alive2.llvm.org/ce/z/3Dn9Cd https://alive2.llvm.org/ce/z/Qyh5h6 Fixes #91178 (cherry picked from commit d085b42)
1 parent fac122a commit 494847b

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed
 

‎llvm/lib/Analysis/InstructionSimplify.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -4322,6 +4322,10 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
43224322
if (match(I, m_Intrinsic<Intrinsic::is_constant>()))
43234323
return nullptr;
43244324

4325+
// Don't simplify freeze.
4326+
if (isa<FreezeInst>(I))
4327+
return nullptr;
4328+
43254329
// Replace Op with RepOp in instruction operands.
43264330
SmallVector<Value *, 8> NewOps;
43274331
bool AnyReplaced = false;

‎llvm/test/Transforms/InstCombine/icmp.ll

+15
Original file line numberDiff line numberDiff line change
@@ -5183,3 +5183,18 @@ entry:
51835183
%cmp = icmp eq i8 %add2, %add1
51845184
ret i1 %cmp
51855185
}
5186+
5187+
define i1 @icmp_freeze_sext(i16 %x, i16 %y) {
5188+
; CHECK-LABEL: @icmp_freeze_sext(
5189+
; CHECK-NEXT: [[CMP1:%.*]] = icmp uge i16 [[X:%.*]], [[Y:%.*]]
5190+
; CHECK-NEXT: [[CMP1_FR:%.*]] = freeze i1 [[CMP1]]
5191+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i16 [[Y]], 0
5192+
; CHECK-NEXT: [[CMP2:%.*]] = or i1 [[TMP1]], [[CMP1_FR]]
5193+
; CHECK-NEXT: ret i1 [[CMP2]]
5194+
;
5195+
%cmp1 = icmp uge i16 %x, %y
5196+
%ext = sext i1 %cmp1 to i16
5197+
%ext.fr = freeze i16 %ext
5198+
%cmp2 = icmp uge i16 %ext.fr, %y
5199+
ret i1 %cmp2
5200+
}

‎llvm/test/Transforms/InstCombine/select.ll

+32
Original file line numberDiff line numberDiff line change
@@ -3708,3 +3708,35 @@ define i32 @src_select_xxory_eq0_xorxy_y(i32 %x, i32 %y) {
37083708
%cond = select i1 %xor0, i32 %xor, i32 %y
37093709
ret i32 %cond
37103710
}
3711+
3712+
define i8 @test_replace_freeze_multiuse(i1 %x, i8 %y) {
3713+
; CHECK-LABEL: @test_replace_freeze_multiuse(
3714+
; CHECK-NEXT: [[EXT:%.*]] = zext i1 [[X:%.*]] to i8
3715+
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i8 [[EXT]], [[Y:%.*]]
3716+
; CHECK-NEXT: [[SHL_FR:%.*]] = freeze i8 [[SHL]]
3717+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[X]], i8 0, i8 [[SHL_FR]]
3718+
; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SHL_FR]], [[SEL]]
3719+
; CHECK-NEXT: ret i8 [[ADD]]
3720+
;
3721+
%ext = zext i1 %x to i8
3722+
%shl = shl nuw i8 %ext, %y
3723+
%shl.fr = freeze i8 %shl
3724+
%sel = select i1 %x, i8 0, i8 %shl.fr
3725+
%add = add i8 %shl.fr, %sel
3726+
ret i8 %add
3727+
}
3728+
3729+
define i8 @test_replace_freeze_oneuse(i1 %x, i8 %y) {
3730+
; CHECK-LABEL: @test_replace_freeze_oneuse(
3731+
; CHECK-NEXT: [[EXT:%.*]] = zext i1 [[X:%.*]] to i8
3732+
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i8 [[EXT]], [[Y:%.*]]
3733+
; CHECK-NEXT: [[SHL_FR:%.*]] = freeze i8 [[SHL]]
3734+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[X]], i8 0, i8 [[SHL_FR]]
3735+
; CHECK-NEXT: ret i8 [[SEL]]
3736+
;
3737+
%ext = zext i1 %x to i8
3738+
%shl = shl nuw i8 %ext, %y
3739+
%shl.fr = freeze i8 %shl
3740+
%sel = select i1 %x, i8 0, i8 %shl.fr
3741+
ret i8 %sel
3742+
}

‎llvm/test/Transforms/PGOProfile/chr.ll

+4-3
Original file line numberDiff line numberDiff line change
@@ -1298,11 +1298,12 @@ define i32 @test_chr_14(ptr %i, ptr %j, i32 %sum0, i1 %pred, i32 %z) !prof !14 {
12981298
; CHECK-NEXT: entry:
12991299
; CHECK-NEXT: [[Z_FR:%.*]] = freeze i32 [[Z:%.*]]
13001300
; CHECK-NEXT: [[I0:%.*]] = load i32, ptr [[I:%.*]], align 4
1301-
; CHECK-NEXT: [[V1:%.*]] = icmp eq i32 [[Z_FR]], 1
1302-
; CHECK-NEXT: br i1 [[V1]], label [[BB1:%.*]], label [[ENTRY_SPLIT_NONCHR:%.*]], !prof [[PROF15]]
1301+
; CHECK-NEXT: [[V1_NOT:%.*]] = icmp eq i32 [[Z_FR]], 1
1302+
; CHECK-NEXT: br i1 [[V1_NOT]], label [[BB1:%.*]], label [[ENTRY_SPLIT_NONCHR:%.*]], !prof [[PROF15]]
13031303
; CHECK: entry.split.nonchr:
1304+
; CHECK-NEXT: [[PRED_FR:%.*]] = freeze i1 [[PRED:%.*]]
13041305
; CHECK-NEXT: [[V0:%.*]] = icmp eq i32 [[Z_FR]], 0
1305-
; CHECK-NEXT: [[V3_NONCHR:%.*]] = and i1 [[V0]], [[PRED:%.*]]
1306+
; CHECK-NEXT: [[V3_NONCHR:%.*]] = and i1 [[V0]], [[PRED_FR]]
13061307
; CHECK-NEXT: br i1 [[V3_NONCHR]], label [[BB0_NONCHR:%.*]], label [[BB1]], !prof [[PROF16]]
13071308
; CHECK: bb0.nonchr:
13081309
; CHECK-NEXT: call void @foo()

0 commit comments

Comments
 (0)