Skip to content

Commit 7c396ad

Browse files
committed
create InstCombine-specific helper and pass function directly
1 parent cc4e8b4 commit 7c396ad

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

llvm/include/llvm/IR/ProfDataUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ LLVM_ABI void setExplicitlyUnknownBranchWeights(Instruction &I,
189189
/// weights in the new instruction if the parent function of the original
190190
/// instruction has function counts. This is to not confuse users by injecting
191191
/// profile data into non-profiled functions.
192-
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &New,
193-
Instruction &Original,
192+
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I,
193+
Function &F,
194194
StringRef PassName);
195195

196196
/// Analogous to setExplicitlyUnknownBranchWeights, but for functions and their

llvm/lib/IR/ProfDataUtils.cpp

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

255-
void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &New,
256-
Instruction &Original,
255+
void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, Function &F,
257256
StringRef PassName) {
258-
Function *F = Original.getFunction();
259-
assert(F && "instruction does not belong to a function!");
260-
std::optional<Function::ProfileCount> EC = F->getEntryCount();
261-
if (EC && EC->getCount() > 0)
262-
setExplicitlyUnknownBranchWeights(New, PassName);
257+
if (std::optional<Function::ProfileCount> EC = F.getEntryCount();
258+
EC && EC->getCount() > 0)
259+
setExplicitlyUnknownBranchWeights(I, PassName);
263260
}
264261

265262
void setExplicitlyUnknownFunctionEntryCount(Function &F, StringRef PassName) {

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,13 +882,14 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
882882
SelectInst *SI = nullptr;
883883
if (match(Op0, m_ZExt(m_Value(X))) &&
884884
X->getType()->getScalarSizeInBits() == 1)
885-
SI = SelectInst::Create(X, InstCombiner::AddOne(Op1C), Op1);
885+
SI = createSelectInstMaybeWithUnknownBranchWeights(
886+
X, InstCombiner::AddOne(Op1C), Op1, Add.getFunction());
886887
// sext(bool) + C -> bool ? C - 1 : C
887888
if (!SI && match(Op0, m_SExt(m_Value(X))) &&
888889
X->getType()->getScalarSizeInBits() == 1)
889-
SI = SelectInst::Create(X, InstCombiner::SubOne(Op1C), Op1);
890+
SI = createSelectInstMaybeWithUnknownBranchWeights(
891+
X, InstCombiner::SubOne(Op1C), Op1, Add.getFunction());
890892
if (SI) {
891-
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, Add, DEBUG_TYPE);
892893
return SI;
893894
}
894895

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 13 additions & 0 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"
@@ -469,6 +470,18 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
469470
Value *simplifyNonNullOperand(Value *V, bool HasDereferenceable,
470471
unsigned Depth = 0);
471472

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

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,9 +1254,8 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
12541254
// shl (zext i1 X), C1 --> select (X, 1 << C1, 0)
12551255
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
12561256
auto *NewC = Builder.CreateShl(ConstantInt::get(Ty, 1), C1);
1257-
auto *SI = SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
1258-
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, I, DEBUG_TYPE);
1259-
return SI;
1257+
return createSelectInstMaybeWithUnknownBranchWeights(
1258+
X, NewC, ConstantInt::getNullValue(Ty), I.getFunction());
12601259
}
12611260
}
12621261

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,9 +1736,8 @@ Instruction *InstCombinerImpl::foldBinopOfSextBoolToSelect(BinaryOperator &BO) {
17361736
Constant *Zero = ConstantInt::getNullValue(BO.getType());
17371737
Value *TVal = Builder.CreateBinOp(BO.getOpcode(), Ones, C);
17381738
Value *FVal = Builder.CreateBinOp(BO.getOpcode(), Zero, C);
1739-
SelectInst *SI = SelectInst::Create(X, TVal, FVal);
1740-
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, BO, DEBUG_TYPE);
1741-
return SI;
1739+
return createSelectInstMaybeWithUnknownBranchWeights(X, TVal, FVal,
1740+
BO.getFunction());
17421741
}
17431742

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

0 commit comments

Comments
 (0)