Skip to content

Commit 045e09f

Browse files
authored
[InstCombine] Set !prof metadata on Selects identified by add.ll test (#158743)
These select instructions are created from non-branching instructions, so their branch weights are unknown. Tracking issue: #147390
1 parent 9df1099 commit 045e09f

File tree

9 files changed

+97
-20
lines changed

9 files changed

+97
-20
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
185185
LLVM_ABI void setExplicitlyUnknownBranchWeights(Instruction &I,
186186
StringRef PassName);
187187

188+
/// Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch
189+
/// weights in the new instruction if the parent function of the original
190+
/// instruction has an entry count. This is to not confuse users by injecting
191+
/// profile data into non-profiled functions.
192+
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I,
193+
Function &F,
194+
StringRef PassName);
195+
188196
/// Analogous to setExplicitlyUnknownBranchWeights, but for functions and their
189197
/// entry counts.
190198
LLVM_ABI void setExplicitlyUnknownFunctionEntryCount(Function &F,

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
6464
/// A worklist of the instructions that need to be simplified.
6565
InstructionWorklist &Worklist;
6666

67+
Function &F;
68+
6769
// Mode in which we are running the combiner.
6870
const bool MinimizeSize;
6971

@@ -98,17 +100,17 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
98100
bool ComputedBackEdges = false;
99101

100102
public:
101-
InstCombiner(InstructionWorklist &Worklist, BuilderTy &Builder,
102-
bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
103-
TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
104-
DominatorTree &DT, OptimizationRemarkEmitter &ORE,
105-
BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
106-
ProfileSummaryInfo *PSI, const DataLayout &DL,
103+
InstCombiner(InstructionWorklist &Worklist, BuilderTy &Builder, Function &F,
104+
AAResults *AA, AssumptionCache &AC, TargetLibraryInfo &TLI,
105+
TargetTransformInfo &TTI, DominatorTree &DT,
106+
OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
107+
BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI,
108+
const DataLayout &DL,
107109
ReversePostOrderTraversal<BasicBlock *> &RPOT)
108110
: TTIForTargetIntrinsicsOnly(TTI), Builder(Builder), Worklist(Worklist),
109-
MinimizeSize(MinimizeSize), AA(AA), AC(AC), TLI(TLI), DT(DT), DL(DL),
110-
SQ(DL, &TLI, &DT, &AC, nullptr, /*UseInstrInfo*/ true,
111-
/*CanUseUndef*/ true, &DC),
111+
F(F), MinimizeSize(F.hasMinSize()), AA(AA), AC(AC), TLI(TLI), DT(DT),
112+
DL(DL), SQ(DL, &TLI, &DT, &AC, nullptr, /*UseInstrInfo*/ true,
113+
/*CanUseUndef*/ true, &DC),
112114
ORE(ORE), BFI(BFI), BPI(BPI), PSI(PSI), RPOT(RPOT) {}
113115

114116
virtual ~InstCombiner() = default;

llvm/lib/IR/ProfDataUtils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,13 @@ void setExplicitlyUnknownBranchWeights(Instruction &I, StringRef PassName) {
252252
MDB.createString(PassName)}));
253253
}
254254

255+
void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, Function &F,
256+
StringRef PassName) {
257+
if (std::optional<Function::ProfileCount> EC = F.getEntryCount();
258+
EC && EC->getCount() > 0)
259+
setExplicitlyUnknownBranchWeights(I, PassName);
260+
}
261+
255262
void setExplicitlyUnknownFunctionEntryCount(Function &F, StringRef PassName) {
256263
MDBuilder MDB(F.getContext());
257264
F.setMetadata(

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,11 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
880880
// zext(bool) + C -> bool ? C + 1 : C
881881
if (match(Op0, m_ZExt(m_Value(X))) &&
882882
X->getType()->getScalarSizeInBits() == 1)
883-
return SelectInst::Create(X, InstCombiner::AddOne(Op1C), Op1);
883+
return createSelectInst(X, InstCombiner::AddOne(Op1C), Op1);
884884
// sext(bool) + C -> bool ? C - 1 : C
885885
if (match(Op0, m_SExt(m_Value(X))) &&
886886
X->getType()->getScalarSizeInBits() == 1)
887-
return SelectInst::Create(X, InstCombiner::SubOne(Op1C), Op1);
887+
return createSelectInst(X, InstCombiner::SubOne(Op1C), Op1);
888888

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

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/IR/IRBuilder.h"
2424
#include "llvm/IR/InstVisitor.h"
2525
#include "llvm/IR/PatternMatch.h"
26+
#include "llvm/IR/ProfDataUtils.h"
2627
#include "llvm/IR/Value.h"
2728
#include "llvm/Support/Debug.h"
2829
#include "llvm/Support/KnownBits.h"
@@ -62,14 +63,14 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
6263
public InstVisitor<InstCombinerImpl, Instruction *> {
6364
public:
6465
InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder,
65-
bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
66+
Function &F, AAResults *AA, AssumptionCache &AC,
6667
TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
6768
DominatorTree &DT, OptimizationRemarkEmitter &ORE,
6869
BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
6970
ProfileSummaryInfo *PSI, const DataLayout &DL,
7071
ReversePostOrderTraversal<BasicBlock *> &RPOT)
71-
: InstCombiner(Worklist, Builder, MinimizeSize, AA, AC, TLI, TTI, DT, ORE,
72-
BFI, BPI, PSI, DL, RPOT) {}
72+
: InstCombiner(Worklist, Builder, F, AA, AC, TLI, TTI, DT, ORE, BFI, BPI,
73+
PSI, DL, RPOT) {}
7374

7475
virtual ~InstCombinerImpl() = default;
7576

@@ -469,6 +470,17 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
469470
Value *simplifyNonNullOperand(Value *V, bool HasDereferenceable,
470471
unsigned Depth = 0);
471472

473+
SelectInst *createSelectInst(Value *C, Value *S1, Value *S2,
474+
const Twine &NameStr = "",
475+
InsertPosition InsertBefore = nullptr,
476+
Instruction *MDFrom = nullptr) {
477+
SelectInst *SI =
478+
SelectInst::Create(C, S1, S2, NameStr, InsertBefore, MDFrom);
479+
if (!MDFrom)
480+
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, F, DEBUG_TYPE);
481+
return SI;
482+
}
483+
472484
public:
473485
/// Create and insert the idiom we use to indicate a block is unreachable
474486
/// without having to rewrite the CFG from within InstCombine.

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
12531253
// shl (zext i1 X), C1 --> select (X, 1 << C1, 0)
12541254
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
12551255
auto *NewC = Builder.CreateShl(ConstantInt::get(Ty, 1), C1);
1256-
return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
1256+
return createSelectInst(X, NewC, ConstantInt::getNullValue(Ty));
12571257
}
12581258
}
12591259

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ Instruction *InstCombinerImpl::foldBinopOfSextBoolToSelect(BinaryOperator &BO) {
17351735
Constant *Zero = ConstantInt::getNullValue(BO.getType());
17361736
Value *TVal = Builder.CreateBinOp(BO.getOpcode(), Ones, C);
17371737
Value *FVal = Builder.CreateBinOp(BO.getOpcode(), Zero, C);
1738-
return SelectInst::Create(X, TVal, FVal);
1738+
return createSelectInst(X, TVal, FVal);
17391739
}
17401740

17411741
static Value *simplifyOperationIntoSelectOperand(Instruction &I, SelectInst *SI,
@@ -5934,8 +5934,8 @@ static bool combineInstructionsOverFunction(
59345934
LLVM_DEBUG(dbgs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
59355935
<< F.getName() << "\n");
59365936

5937-
InstCombinerImpl IC(Worklist, Builder, F.hasMinSize(), AA, AC, TLI, TTI, DT,
5938-
ORE, BFI, BPI, PSI, DL, RPOT);
5937+
InstCombinerImpl IC(Worklist, Builder, F, AA, AC, TLI, TTI, DT, ORE, BFI,
5938+
BPI, PSI, DL, RPOT);
59395939
IC.MaxArraySizeForCombine = MaxArraySize;
59405940
bool MadeChangeInThisIteration = IC.prepareWorklist(F);
59415941
MadeChangeInThisIteration |= IC.run();

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,59 @@ 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) !prof !0 {
50+
; CHECK-LABEL: define i32 @select_C_minus_1_or_C_from_bool(
51+
; CHECK-SAME: i1 [[X:%.*]]) !prof [[PROF0]] {
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) !prof !0 {
61+
; CHECK-LABEL: define i5 @and_add(
62+
; CHECK-SAME: i1 [[X:%.*]], i1 [[Y:%.*]]) !prof [[PROF0]] {
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) !prof !0 {
76+
; CHECK-LABEL: define i32 @add_zext_zext_i1(
77+
; CHECK-SAME: i1 [[A:%.*]]) !prof [[PROF0]] {
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+
define i32 @no_count_no_branch_weights(i1 %a) {
87+
; CHECK-LABEL: define i32 @no_count_no_branch_weights(
88+
; CHECK-SAME: i1 [[A:%.*]]) {
89+
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[A]], i32 2, i32 0
90+
; CHECK-NEXT: ret i32 [[ADD]]
91+
;
92+
%zext = zext i1 %a to i32
93+
%add = add i32 %zext, %zext
94+
ret i32 %add
95+
}
96+
97+
4998
!0 = !{!"function_entry_count", i64 1000}
5099
!1 = !{!"branch_weights", i32 2, i32 3}
51100
;.
52101
; CHECK: [[PROF0]] = !{!"function_entry_count", i64 1000}
53102
; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3}
103+
; CHECK: [[PROF2]] = !{!"unknown", !"instcombine"}
54104
;.

llvm/utils/profcheck-xfail.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,6 @@ Transforms/InstCombine/2011-02-14-InfLoop.ll
838838
Transforms/InstCombine/AArch64/sve-intrinsic-sel.ll
839839
Transforms/InstCombine/AArch64/sve-intrinsic-simplify-binop.ll
840840
Transforms/InstCombine/AArch64/sve-intrinsic-simplify-shift.ll
841-
Transforms/InstCombine/add2.ll
842-
Transforms/InstCombine/add.ll
843841
Transforms/InstCombine/add-mask.ll
844842
Transforms/InstCombine/add-shl-mul-umax.ll
845843
Transforms/InstCombine/add-shl-sdiv-to-srem.ll

0 commit comments

Comments
 (0)