Skip to content

IR: de-duplicate two CmpInst routines (NFC) #116866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions llvm/include/llvm/IR/InstrTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -935,43 +935,6 @@ class CmpInst : public Instruction {
return isUnsigned(getPredicate());
}

/// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
/// @returns the signed version of the unsigned predicate pred.
/// return the signed version of a predicate
static Predicate getSignedPredicate(Predicate pred);

/// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
/// @returns the signed version of the predicate for this instruction (which
/// has to be an unsigned predicate).
/// return the signed version of a predicate
Predicate getSignedPredicate() {
return getSignedPredicate(getPredicate());
}

/// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
/// @returns the unsigned version of the signed predicate pred.
static Predicate getUnsignedPredicate(Predicate pred);

/// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
/// @returns the unsigned version of the predicate for this instruction (which
/// has to be an signed predicate).
/// return the unsigned version of a predicate
Predicate getUnsignedPredicate() {
return getUnsignedPredicate(getPredicate());
}

/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
/// @returns the unsigned version of the signed predicate pred or
/// the signed version of the signed predicate pred.
static Predicate getFlippedSignednessPredicate(Predicate pred);

/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
/// @returns the unsigned version of the signed predicate pred or
/// the signed version of the signed predicate pred.
Predicate getFlippedSignednessPredicate() {
return getFlippedSignednessPredicate(getPredicate());
}

/// This is just a convenience.
/// Determine if this is true when both operands are the same.
bool isTrueWhenEqual() const {
Expand Down
22 changes: 16 additions & 6 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -1206,27 +1206,37 @@ class ICmpInst: public CmpInst {
/// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
/// @returns the predicate that would be the result if the operand were
/// regarded as signed.
/// Return the signed version of the predicate
/// Return the signed version of the predicate.
Predicate getSignedPredicate() const {
return getSignedPredicate(getPredicate());
}

/// This is a static version that you can use without an instruction.
/// Return the signed version of the predicate.
/// Return the signed version of the predicate: static variant.
static Predicate getSignedPredicate(Predicate pred);

/// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
/// @returns the predicate that would be the result if the operand were
/// regarded as unsigned.
/// Return the unsigned version of the predicate
/// Return the unsigned version of the predicate.
Predicate getUnsignedPredicate() const {
return getUnsignedPredicate(getPredicate());
}

/// This is a static version that you can use without an instruction.
/// Return the unsigned version of the predicate.
/// Return the unsigned version of the predicate: static variant.
static Predicate getUnsignedPredicate(Predicate pred);

/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
/// @returns the unsigned version of the signed predicate pred or
/// the signed version of the signed predicate pred.
static Predicate getFlippedSignednessPredicate(Predicate pred);

/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
/// @returns the unsigned version of the signed predicate pred or
/// the signed version of the signed predicate pred.
Predicate getFlippedSignednessPredicate() const {
return getFlippedSignednessPredicate(getPredicate());
}

void setSameSign(bool B = true) {
SubclassOptionalData = (SubclassOptionalData & ~SameSign) | (B * SameSign);
}
Expand Down
4 changes: 1 addition & 3 deletions llvm/include/llvm/SandboxIR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2501,9 +2501,6 @@ class CmpInst : public SingleLLVMInstructionImpl<llvm::CmpInst> {
WRAP_BOTH(isEquality);
WRAP_BOTH(isRelational);
WRAP_BOTH(isSigned);
WRAP_BOTH(getSignedPredicate);
WRAP_BOTH(getUnsignedPredicate);
WRAP_BOTH(getFlippedSignednessPredicate);
WRAP_BOTH(isTrueWhenEqual);
WRAP_BOTH(isFalseWhenEqual);
WRAP_BOTH(isUnsigned);
Expand Down Expand Up @@ -2544,6 +2541,7 @@ class ICmpInst : public CmpInst {

WRAP_BOTH(getSignedPredicate);
WRAP_BOTH(getUnsignedPredicate);
WRAP_BOTH(getFlippedSignednessPredicate);
WRAP_BOTH(isEquality);
WRAP_MEMBER(isCommutative);
WRAP_MEMBER(isRelational);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11874,7 +11874,7 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
CmpInst::Predicate P2) {
assert(P1 != P2 && "Handled earlier!");
return CmpInst::isRelational(P2) &&
P1 == CmpInst::getFlippedSignednessPredicate(P2);
P1 == ICmpInst::getFlippedSignednessPredicate(P2);
};
if (IsSignFlippedPredicate(Pred, FoundPred)) {
// Unsigned comparison is the same as signed comparison when both the
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/ExpandMemCmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ Value *MemCmpExpansion::getMemCmpOneBlock() {
}
// Generate new code and remove the original memcmp call and the user
if (ICmpInst::isSigned(Pred)) {
Value *Cmp = Builder.CreateICmp(CmpInst::getUnsignedPredicate(Pred),
Value *Cmp = Builder.CreateICmp(ICmpInst::getUnsignedPredicate(Pred),
Loads.Lhs, Loads.Rhs);
auto *Result = NeedsZExt ? Builder.CreateZExt(Cmp, UI->getType()) : Cmp;
UI->replaceAllUsesWith(Result);
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/IR/ConstantRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/ConstantRange.h"
#include "llvm/ADT/APInt.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Operator.h"
Expand Down Expand Up @@ -191,7 +192,7 @@ CmpInst::Predicate ConstantRange::getEquivalentPredWithFlippedSignedness(
"Only for relational integer predicates!");

CmpInst::Predicate FlippedSignednessPred =
CmpInst::getFlippedSignednessPredicate(Pred);
ICmpInst::getFlippedSignednessPredicate(Pred);

if (areInsensitiveToSignednessOfICmpPredicate(CR1, CR2))
return FlippedSignednessPred;
Expand Down
36 changes: 1 addition & 35 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3718,40 +3718,6 @@ CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
llvm_unreachable("Unknown predicate!");
}

CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!");

switch (pred) {
default:
llvm_unreachable("Unknown predicate!");
case CmpInst::ICMP_ULT:
return CmpInst::ICMP_SLT;
case CmpInst::ICMP_ULE:
return CmpInst::ICMP_SLE;
case CmpInst::ICMP_UGT:
return CmpInst::ICMP_SGT;
case CmpInst::ICMP_UGE:
return CmpInst::ICMP_SGE;
}
}

CmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) {
assert(CmpInst::isSigned(pred) && "Call only with signed predicates!");

switch (pred) {
default:
llvm_unreachable("Unknown predicate!");
case CmpInst::ICMP_SLT:
return CmpInst::ICMP_ULT;
case CmpInst::ICMP_SLE:
return CmpInst::ICMP_ULE;
case CmpInst::ICMP_SGT:
return CmpInst::ICMP_UGT;
case CmpInst::ICMP_SGE:
return CmpInst::ICMP_UGE;
}
}

bool CmpInst::isUnsigned(Predicate predicate) {
switch (predicate) {
default: return false;
Expand Down Expand Up @@ -3867,7 +3833,7 @@ std::optional<bool> ICmpInst::compare(const KnownBits &LHS,
}
}

CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) {
CmpInst::Predicate ICmpInst::getFlippedSignednessPredicate(Predicate pred) {
assert(CmpInst::isRelational(pred) &&
"Call only with non-equality predicates!");

Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ ConstraintTy ConstraintInfo::getConstraintForSolving(CmpInst::Predicate Pred,
if (CmpInst::isSigned(Pred) &&
isKnownNonNegative(Op0, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1) &&
isKnownNonNegative(Op1, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
Pred = CmpInst::getUnsignedPredicate(Pred);
Pred = ICmpInst::getUnsignedPredicate(Pred);

SmallVector<Value *> NewVariables;
ConstraintTy R = getConstraint(Pred, Op0, Op1, NewVariables);
Expand Down Expand Up @@ -857,7 +857,7 @@ void ConstraintInfo::transferToOtherSystem(
if (IsKnownNonNegative(B)) {
addFact(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0), NumIn,
NumOut, DFSInStack);
addFact(CmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
addFact(ICmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
DFSInStack);
}
break;
Expand All @@ -867,7 +867,7 @@ void ConstraintInfo::transferToOtherSystem(
if (IsKnownNonNegative(A)) {
addFact(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0), NumIn,
NumOut, DFSInStack);
addFact(CmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
addFact(ICmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
DFSInStack);
}
break;
Expand Down
Loading