Skip to content

Commit 8a5194f

Browse files
committed
[InstCombine] Make getKnownSign a member function of InstCombiner; NFC
This is prep for using `getKnownSign` outside of just InstCombineCalls.
1 parent 4924172 commit 8a5194f

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,10 +1041,9 @@ Instruction *InstCombinerImpl::foldIntrinsicIsFPClass(IntrinsicInst &II) {
10411041
return nullptr;
10421042
}
10431043

1044-
static std::optional<bool> getKnownSign(Value *Op, Instruction *CxtI,
1045-
const DataLayout &DL, AssumptionCache *AC,
1046-
DominatorTree *DT) {
1047-
KnownBits Known = computeKnownBits(Op, DL, 0, AC, CxtI, DT);
1044+
std::optional<bool> InstCombinerImpl::getKnownSign(Value *Op,
1045+
Instruction *CxtI) const {
1046+
KnownBits Known = llvm::computeKnownBits(Op, DL, /*Depth*/ 0, &AC, CxtI, &DT);
10481047
if (Known.isNonNegative())
10491048
return false;
10501049
if (Known.isNegative())
@@ -1058,13 +1057,10 @@ static std::optional<bool> getKnownSign(Value *Op, Instruction *CxtI,
10581057
ICmpInst::ICMP_SLT, Op, Constant::getNullValue(Op->getType()), CxtI, DL);
10591058
}
10601059

1061-
static std::optional<bool> getKnownSignOrZero(Value *Op, Instruction *CxtI,
1062-
const DataLayout &DL,
1063-
AssumptionCache *AC,
1064-
DominatorTree *DT) {
1065-
if (std::optional<bool> Sign = getKnownSign(Op, CxtI, DL, AC, DT))
1060+
std::optional<bool>
1061+
InstCombinerImpl::getKnownSignOrZero(Value *Op, Instruction *CxtI) const {
1062+
if (std::optional<bool> Sign = getKnownSign(Op, CxtI))
10661063
return Sign;
1067-
10681064
Value *X, *Y;
10691065
if (match(Op, m_NSWSub(m_Value(X), m_Value(Y))))
10701066
return isImpliedByDomCondition(ICmpInst::ICMP_SLE, X, Y, CxtI, DL);
@@ -1074,12 +1070,11 @@ static std::optional<bool> getKnownSignOrZero(Value *Op, Instruction *CxtI,
10741070

10751071
/// Return true if two values \p Op0 and \p Op1 are known to have the same sign.
10761072
static bool signBitMustBeTheSame(Value *Op0, Value *Op1, Instruction *CxtI,
1077-
const DataLayout &DL, AssumptionCache *AC,
1078-
DominatorTree *DT) {
1079-
std::optional<bool> Known1 = getKnownSign(Op1, CxtI, DL, AC, DT);
1073+
InstCombinerImpl &IC) {
1074+
std::optional<bool> Known1 = IC.getKnownSign(Op1, CxtI);
10801075
if (!Known1)
10811076
return false;
1082-
std::optional<bool> Known0 = getKnownSign(Op0, CxtI, DL, AC, DT);
1077+
std::optional<bool> Known0 = IC.getKnownSign(Op0, CxtI);
10831078
if (!Known0)
10841079
return false;
10851080
return *Known0 == *Known1;
@@ -1627,8 +1622,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
16271622
return replaceOperand(*II, 0, XY);
16281623
}
16291624

1630-
if (std::optional<bool> Known =
1631-
getKnownSignOrZero(IIOperand, II, DL, &AC, &DT)) {
1625+
if (std::optional<bool> Known = getKnownSignOrZero(IIOperand, II)) {
16321626
// abs(x) -> x if x >= 0 (include abs(x-y) --> x - y where x >= y)
16331627
// abs(x) -> x if x > 0 (include abs(x-y) --> x - y where x > y)
16341628
if (!*Known)
@@ -1753,7 +1747,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
17531747
bool UseAndN = IID == Intrinsic::smin || IID == Intrinsic::umin;
17541748

17551749
if (IID == Intrinsic::smax || IID == Intrinsic::smin) {
1756-
auto KnownSign = getKnownSign(X, II, DL, &AC, &DT);
1750+
auto KnownSign = getKnownSign(X, II);
17571751
if (KnownSign == std::nullopt) {
17581752
UseOr = false;
17591753
UseAndN = false;
@@ -2614,7 +2608,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
26142608
FastMathFlags InnerFlags = cast<FPMathOperator>(Src)->getFastMathFlags();
26152609

26162610
if ((FMF.allowReassoc() && InnerFlags.allowReassoc()) ||
2617-
signBitMustBeTheSame(Exp, InnerExp, II, DL, &AC, &DT)) {
2611+
signBitMustBeTheSame(Exp, InnerExp, II, *this)) {
26182612
// TODO: Add nsw/nuw probably safe if integer type exceeds exponent
26192613
// width.
26202614
Value *NewExp = Builder.CreateAdd(InnerExp, Exp);

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
491491
Instruction::BinaryOps BinaryOp, bool IsSigned,
492492
Value *LHS, Value *RHS, Instruction *CxtI) const;
493493

494+
// Return true if known negative, false if known positive, and nullopt if
495+
// unknown.
496+
std::optional<bool> getKnownSign(Value *Op, Instruction *CxtI) const;
497+
// Return true if known negative or zero, false if known non-zero positive,
498+
// and nullopt if unknown.
499+
std::optional<bool> getKnownSignOrZero(Value *Op, Instruction *CxtI) const;
500+
494501
/// Performs a few simplifications for operators which are associative
495502
/// or commutative.
496503
bool SimplifyAssociativeOrCommutative(BinaryOperator &I);

0 commit comments

Comments
 (0)