Skip to content

Commit 2f5c4f8

Browse files
committed
[InstCombine] Set !prof metadata on Selects identified by add.ll test
These select instructions are created from non-branching instructions, so their branch weights are unknown. Tracking issue: #147390
1 parent 4a8bb08 commit 2f5c4f8

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/IR/Instructions.h"
2525
#include "llvm/IR/Operator.h"
2626
#include "llvm/IR/PatternMatch.h"
27+
#include "llvm/IR/ProfDataUtils.h"
2728
#include "llvm/IR/Type.h"
2829
#include "llvm/IR/Value.h"
2930
#include "llvm/Support/AlignOf.h"
@@ -878,13 +879,18 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
878879
return BinaryOperator::CreateAdd(Builder.CreateNot(Y), X);
879880

880881
// zext(bool) + C -> bool ? C + 1 : C
882+
SelectInst *SI = nullptr;
881883
if (match(Op0, m_ZExt(m_Value(X))) &&
882884
X->getType()->getScalarSizeInBits() == 1)
883-
return SelectInst::Create(X, InstCombiner::AddOne(Op1C), Op1);
885+
SI = SelectInst::Create(X, InstCombiner::AddOne(Op1C), Op1);
884886
// sext(bool) + C -> bool ? C - 1 : C
885887
if (match(Op0, m_SExt(m_Value(X))) &&
886888
X->getType()->getScalarSizeInBits() == 1)
887-
return SelectInst::Create(X, InstCombiner::SubOne(Op1C), Op1);
889+
SI = SelectInst::Create(X, InstCombiner::SubOne(Op1C), Op1);
890+
if (SI) {
891+
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
892+
return SI;
893+
}
888894

889895
// ~X + C --> (C-1) - X
890896
if (match(Op0, m_Not(m_Value(X)))) {

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/Analysis/InstructionSimplify.h"
1515
#include "llvm/IR/IntrinsicInst.h"
1616
#include "llvm/IR/PatternMatch.h"
17+
#include "llvm/IR/ProfDataUtils.h"
1718
#include "llvm/Transforms/InstCombine/InstCombiner.h"
1819
using namespace llvm;
1920
using namespace PatternMatch;
@@ -1253,7 +1254,9 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
12531254
// shl (zext i1 X), C1 --> select (X, 1 << C1, 0)
12541255
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
12551256
auto *NewC = Builder.CreateShl(ConstantInt::get(Ty, 1), C1);
1256-
return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
1257+
auto *SI = SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
1258+
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
1259+
return SI;
12571260
}
12581261
}
12591262

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#include "llvm/IR/Operator.h"
8282
#include "llvm/IR/PassManager.h"
8383
#include "llvm/IR/PatternMatch.h"
84+
#include "llvm/IR/ProfDataUtils.h"
8485
#include "llvm/IR/Type.h"
8586
#include "llvm/IR/Use.h"
8687
#include "llvm/IR/User.h"
@@ -1735,7 +1736,9 @@ Instruction *InstCombinerImpl::foldBinopOfSextBoolToSelect(BinaryOperator &BO) {
17351736
Constant *Zero = ConstantInt::getNullValue(BO.getType());
17361737
Value *TVal = Builder.CreateBinOp(BO.getOpcode(), Ones, C);
17371738
Value *FVal = Builder.CreateBinOp(BO.getOpcode(), Zero, C);
1738-
return SelectInst::Create(X, TVal, FVal);
1739+
SelectInst *SI = SelectInst::Create(X, TVal, FVal);
1740+
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
1741+
return SI;
17391742
}
17401743

17411744
static Value *simplifyOperationIntoSelectOperand(Instruction &I, SelectInst *SI,

llvm/test/Transforms/InstCombine/preserve-profile.ll

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,48 @@ define i32 @NegBin(i1 %C) !prof !0 {
4646
ret i32 %V
4747
}
4848

49+
define i32 @select_C_minus_1_or_C_from_bool(i1 %x) {
50+
; CHECK-LABEL: define i32 @select_C_minus_1_or_C_from_bool(
51+
; CHECK-SAME: i1 [[X:%.*]]) {
52+
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[X]], i32 41, i32 42, !prof [[PROF2:![0-9]+]]
53+
; CHECK-NEXT: ret i32 [[ADD]]
54+
;
55+
%ext = sext i1 %x to i32
56+
%add = add i32 %ext, 42
57+
ret i32 %add
58+
}
59+
60+
define i5 @and_add(i1 %x, i1 %y) {
61+
; CHECK-LABEL: define i5 @and_add(
62+
; CHECK-SAME: i1 [[X:%.*]], i1 [[Y:%.*]]) {
63+
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[X]], true
64+
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[Y]], [[TMP1]]
65+
; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i5 -2, i5 0, !prof [[PROF2]]
66+
; CHECK-NEXT: ret i5 [[R]]
67+
;
68+
%xz = zext i1 %x to i5
69+
%ys = sext i1 %y to i5
70+
%sub = add i5 %xz, %ys
71+
%r = and i5 %sub, 30
72+
ret i5 %r
73+
}
74+
75+
define i32 @add_zext_zext_i1(i1 %a) {
76+
; CHECK-LABEL: define i32 @add_zext_zext_i1(
77+
; CHECK-SAME: i1 [[A:%.*]]) {
78+
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[A]], i32 2, i32 0, !prof [[PROF2]]
79+
; CHECK-NEXT: ret i32 [[ADD]]
80+
;
81+
%zext = zext i1 %a to i32
82+
%add = add i32 %zext, %zext
83+
ret i32 %add
84+
}
85+
86+
4987
!0 = !{!"function_entry_count", i64 1000}
5088
!1 = !{!"branch_weights", i32 2, i32 3}
5189
;.
5290
; CHECK: [[PROF0]] = !{!"function_entry_count", i64 1000}
5391
; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3}
92+
; CHECK: [[PROF2]] = !{!"unknown", !"instcombine"}
5493
;.

llvm/utils/profcheck-xfail.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,6 @@ Transforms/InstCombine/2011-02-14-InfLoop.ll
836836
Transforms/InstCombine/AArch64/sve-intrinsic-sel.ll
837837
Transforms/InstCombine/AArch64/sve-intrinsic-simplify-binop.ll
838838
Transforms/InstCombine/AArch64/sve-intrinsic-simplify-shift.ll
839-
Transforms/InstCombine/add2.ll
840-
Transforms/InstCombine/add.ll
841839
Transforms/InstCombine/add-mask.ll
842840
Transforms/InstCombine/add-shl-mul-umax.ll
843841
Transforms/InstCombine/add-shl-sdiv-to-srem.ll

0 commit comments

Comments
 (0)