Skip to content

Commit

Permalink
Rename Cmp -> C
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Oct 2, 2024
1 parent b452dac commit c10cf88
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions llvm/include/llvm/Analysis/CmpInstAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ namespace llvm {
Constant *getPredForFCmpCode(unsigned Code, Type *OpTy,
CmpInst::Predicate &Pred);

/// Represents the operation icmp (X & Mask) pred Cmp, where pred can only be
/// Represents the operation icmp (X & Mask) pred C, where pred can only be
/// eq or ne.
struct DecomposedBitTest {
Value *X;
CmpInst::Predicate Pred;
APInt Mask;
APInt Cmp;
APInt C;
};

/// Decompose an icmp into the form ((X & Mask) pred Cmp) if possible.
/// Unless \p AllowNonZeroCmp is true, Cmp will always be 0.
/// Decompose an icmp into the form ((X & Mask) pred C) if possible.
/// Unless \p AllowNonZeroC is true, C will always be 0.
std::optional<DecomposedBitTest>
decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
bool LookThroughTrunc = true,
bool AllowNonZeroCmp = false);
bool AllowNonZeroC = false);

} // end namespace llvm

Expand Down
16 changes: 8 additions & 8 deletions llvm/lib/Analysis/CmpInstAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Constant *llvm::getPredForFCmpCode(unsigned Code, Type *OpTy,

std::optional<DecomposedBitTest>
llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
bool LookThruTrunc, bool AllowNonZeroCmp) {
bool LookThruTrunc, bool AllowNonZeroC) {
using namespace PatternMatch;

const APInt *OrigC;
Expand Down Expand Up @@ -104,7 +104,7 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
// X < 0 is equivalent to (X & SignMask) != 0.
if (C.isZero()) {
Result.Mask = APInt::getSignMask(C.getBitWidth());
Result.Cmp = APInt::getZero(C.getBitWidth());
Result.C = APInt::getZero(C.getBitWidth());
Result.Pred = ICmpInst::ICMP_NE;
break;
}
Expand All @@ -113,15 +113,15 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
if (FlippedSign.isPowerOf2()) {
// X s< 10000100 is equivalent to (X & 11111100 == 10000000)
Result.Mask = -FlippedSign;
Result.Cmp = APInt::getSignMask(C.getBitWidth());
Result.C = APInt::getSignMask(C.getBitWidth());
Result.Pred = ICmpInst::ICMP_EQ;
break;
}

if (FlippedSign.isNegatedPowerOf2()) {
// X s< 01111100 is equivalent to (X & 11111100 != 01111100)
Result.Mask = FlippedSign;
Result.Cmp = C;
Result.C = C;
Result.Pred = ICmpInst::ICMP_NE;
break;
}
Expand All @@ -132,23 +132,23 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
// X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
if (C.isPowerOf2()) {
Result.Mask = -C;
Result.Cmp = APInt::getZero(C.getBitWidth());
Result.C = APInt::getZero(C.getBitWidth());
Result.Pred = ICmpInst::ICMP_EQ;
break;
}

// X u< 11111100 is equivalent to (X & 11111100 != 11111100)
if (C.isNegatedPowerOf2()) {
Result.Mask = C;
Result.Cmp = C;
Result.C = C;
Result.Pred = ICmpInst::ICMP_NE;
break;
}

return std::nullopt;
}

if (!AllowNonZeroCmp && !Result.Cmp.isZero())
if (!AllowNonZeroC && !Result.C.isZero())
return std::nullopt;

if (Inverted)
Expand All @@ -158,7 +158,7 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
if (LookThruTrunc && match(LHS, m_Trunc(m_Value(X)))) {
Result.X = X;
Result.Mask = Result.Mask.zext(X->getType()->getScalarSizeInBits());
Result.Cmp = Result.Cmp.zext(X->getType()->getScalarSizeInBits());
Result.C = Result.C.zext(X->getType()->getScalarSizeInBits());
} else {
Result.X = LHS;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ static unsigned conjugateICmpMask(unsigned Mask) {
static bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
Value *&X, Value *&Y, Value *&Z) {
auto Res = llvm::decomposeBitTestICmp(
LHS, RHS, Pred, /*LookThroughTrunc=*/true, /*AllowNonZeroCmp=*/true);
LHS, RHS, Pred, /*LookThroughTrunc=*/true, /*AllowNonZeroC=*/true);
if (!Res)
return false;

Pred = Res->Pred;
X = Res->X;
Y = ConstantInt::get(X->getType(), Res->Mask);
Z = ConstantInt::get(X->getType(), Res->Cmp);
Z = ConstantInt::get(X->getType(), Res->C);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5920,9 +5920,9 @@ Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) {
// (trunc X) u< C --> (X & -C) == 0 (are all masked-high-bits clear?)
// (trunc X) u> C --> (X & ~C) != 0 (are any masked-high-bits set?)
if (auto Res = decomposeBitTestICmp(Op0, Op1, Pred, /*WithTrunc=*/true,
/*AllowNonZeroCmp=*/true)) {
/*AllowNonZeroC=*/true)) {
Value *And = Builder.CreateAnd(Res->X, Res->Mask);
Constant *Zero = ConstantInt::get(Res->X->getType(), Res->Cmp);
Constant *Zero = ConstantInt::get(Res->X->getType(), Res->C);
return new ICmpInst(Res->Pred, And, Zero);
}

Expand Down

0 comments on commit c10cf88

Please sign in to comment.