[NFC][InstCombine] Make use of unknown profile info clear in the API name (#162766)

Making the choice more clear from the API name, otherwise it'd be very easy for one to just "not bother" with the `MDFrom`​, especially since it is optional and follows the optional `Name`​ - but this time we'd have a harder time detecting it's effectivelly dropped metadata.
This commit is contained in:
Mircea Trofin
2025-10-10 09:33:45 -07:00
committed by GitHub
parent bd6da1feaa
commit 8aa49974df
5 changed files with 18 additions and 14 deletions

View File

@@ -1715,7 +1715,7 @@ public:
static SelectInst *Create(Value *C, Value *S1, Value *S2,
const Twine &NameStr = "",
InsertPosition InsertBefore = nullptr,
Instruction *MDFrom = nullptr) {
const Instruction *MDFrom = nullptr) {
SelectInst *Sel =
new (AllocMarker) SelectInst(C, S1, S2, NameStr, InsertBefore);
if (MDFrom)

View File

@@ -880,11 +880,13 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
// zext(bool) + C -> bool ? C + 1 : C
if (match(Op0, m_ZExt(m_Value(X))) &&
X->getType()->getScalarSizeInBits() == 1)
return createSelectInst(X, InstCombiner::AddOne(Op1C), Op1);
return createSelectInstWithUnknownProfile(X, InstCombiner::AddOne(Op1C),
Op1);
// sext(bool) + C -> bool ? C - 1 : C
if (match(Op0, m_SExt(m_Value(X))) &&
X->getType()->getScalarSizeInBits() == 1)
return createSelectInst(X, InstCombiner::SubOne(Op1C), Op1);
return createSelectInstWithUnknownProfile(X, InstCombiner::SubOne(Op1C),
Op1);
// ~X + C --> (C-1) - X
if (match(Op0, m_Not(m_Value(X)))) {

View File

@@ -471,15 +471,16 @@ private:
Value *simplifyNonNullOperand(Value *V, bool HasDereferenceable,
unsigned Depth = 0);
SelectInst *createSelectInst(Value *C, Value *S1, Value *S2,
const Twine &NameStr = "",
InsertPosition InsertBefore = nullptr,
Instruction *MDFrom = nullptr) {
SelectInst *SI =
SelectInst::Create(C, S1, S2, NameStr, InsertBefore, MDFrom);
if (!MDFrom)
setExplicitlyUnknownBranchWeightsIfProfiled(*SI, F, DEBUG_TYPE);
return SI;
/// Create `select C, S1, S2`. Use only when the profile cannot be calculated
/// from existing profile metadata: if the Function has profiles, this will
/// set the profile of this select to "unknown".
SelectInst *
createSelectInstWithUnknownProfile(Value *C, Value *S1, Value *S2,
const Twine &NameStr = "",
InsertPosition InsertBefore = nullptr) {
auto *Sel = SelectInst::Create(C, S1, S2, NameStr, InsertBefore, nullptr);
setExplicitlyUnknownBranchWeightsIfProfiled(*Sel, F, DEBUG_TYPE);
return Sel;
}
public:

View File

@@ -1253,7 +1253,8 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
// shl (zext i1 X), C1 --> select (X, 1 << C1, 0)
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
auto *NewC = Builder.CreateShl(ConstantInt::get(Ty, 1), C1);
return createSelectInst(X, NewC, ConstantInt::getNullValue(Ty));
return createSelectInstWithUnknownProfile(X, NewC,
ConstantInt::getNullValue(Ty));
}
}

View File

@@ -1737,7 +1737,7 @@ Instruction *InstCombinerImpl::foldBinopOfSextBoolToSelect(BinaryOperator &BO) {
Constant *Zero = ConstantInt::getNullValue(BO.getType());
Value *TVal = Builder.CreateBinOp(BO.getOpcode(), Ones, C);
Value *FVal = Builder.CreateBinOp(BO.getOpcode(), Zero, C);
return createSelectInst(X, TVal, FVal);
return createSelectInstWithUnknownProfile(X, TVal, FVal);
}
static Value *simplifyOperationIntoSelectOperand(Instruction &I, SelectInst *SI,