Skip to content

Commit 6dcdf27

Browse files
[InstCombine] Propagate Profile when Folding Implied Conditionals (#163412)
In the case where we have a conditional that is implied by a previous conditional (like x < 10 => x < 20 in a select), we can simply propagate the profile information along the select.
1 parent 4f75877 commit 6dcdf27

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,10 +2979,14 @@ Instruction *InstCombinerImpl::foldAndOrOfSelectUsingImpliedCond(Value *Op,
29792979
"Op must be either i1 or vector of i1.");
29802980
if (SI.getCondition()->getType() != Op->getType())
29812981
return nullptr;
2982-
if (Value *V = simplifyNestedSelectsUsingImpliedCond(SI, Op, IsAnd, DL))
2983-
return SelectInst::Create(Op,
2984-
IsAnd ? V : ConstantInt::getTrue(Op->getType()),
2985-
IsAnd ? ConstantInt::getFalse(Op->getType()) : V);
2982+
if (Value *V = simplifyNestedSelectsUsingImpliedCond(SI, Op, IsAnd, DL)) {
2983+
Instruction *MDFrom = nullptr;
2984+
if (!ProfcheckDisableMetadataFixes)
2985+
MDFrom = &SI;
2986+
return SelectInst::Create(
2987+
Op, IsAnd ? V : ConstantInt::getTrue(Op->getType()),
2988+
IsAnd ? ConstantInt::getFalse(Op->getType()) : V, "", nullptr, MDFrom);
2989+
}
29862990
return nullptr;
29872991
}
29882992

llvm/test/Transforms/InstCombine/select-safe-impliedcond-transforms.ll

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
33

44
define i1 @a_true_implies_b_true(i8 %z, i1 %X, i1 %Y) {
@@ -34,15 +34,15 @@ define <2 x i1> @a_true_implies_b_true_vec(i8 %z0, <2 x i1> %X, <2 x i1> %Y) {
3434
ret <2 x i1> %res
3535
}
3636

37-
define i1 @a_true_implies_b_true2(i8 %z, i1 %X, i1 %Y) {
37+
define i1 @a_true_implies_b_true2(i8 %z, i1 %X, i1 %Y) !prof !0 {
3838
; CHECK-LABEL: @a_true_implies_b_true2(
3939
; CHECK-NEXT: [[A:%.*]] = icmp ugt i8 [[Z:%.*]], 20
40-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 [[X:%.*]], i1 false
40+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A]], i1 [[X:%.*]], i1 false, !prof [[PROF1:![0-9]+]]
4141
; CHECK-NEXT: ret i1 [[RES]]
4242
;
4343
%a = icmp ugt i8 %z, 20
4444
%b = icmp ugt i8 %z, 10
45-
%sel = select i1 %b, i1 %X, i1 %Y
45+
%sel = select i1 %b, i1 %X, i1 %Y, !prof !1
4646
%res = and i1 %a, %sel
4747
ret i1 %res
4848
}
@@ -258,3 +258,10 @@ define i1 @neg_icmp_eq_implies_trunc(i8 %x, i1 %c) {
258258
%sel2 = select i1 %cmp, i1 true, i1 %sel1
259259
ret i1 %sel2
260260
}
261+
262+
!0 = !{!"function_entry_count", i64 1000}
263+
!1 = !{!"branch_weights", i32 2, i32 3}
264+
;.
265+
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i64 1000}
266+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3}
267+
;.

llvm/utils/profcheck-xfail.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,6 @@ Transforms/InstCombine/select_frexp.ll
906906
Transforms/InstCombine/select.ll
907907
Transforms/InstCombine/select-min-max.ll
908908
Transforms/InstCombine/select-of-symmetric-selects.ll
909-
Transforms/InstCombine/select-safe-impliedcond-transforms.ll
910909
Transforms/InstCombine/select-safe-transforms.ll
911910
Transforms/InstCombine/select-select.ll
912911
Transforms/InstCombine/select-with-extreme-eq-cond.ll
@@ -1237,7 +1236,6 @@ Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
12371236
Transforms/PGOProfile/chr-dead-pred.ll
12381237
Transforms/PGOProfile/chr-dup-threshold.ll
12391238
Transforms/PGOProfile/chr-lifetimes.ll
1240-
Transforms/PGOProfile/chr.ll
12411239
Transforms/PGOProfile/chr-poison.ll
12421240
Transforms/PGOProfile/comdat.ll
12431241
Transforms/PGOProfile/memop_profile_funclet_wasm.ll

0 commit comments

Comments
 (0)