Skip to content

[ConstantFold][RFC] Add AllowLHSConstant parameter in getBinOpAbsorber #109736

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
Oct 1, 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
7 changes: 5 additions & 2 deletions llvm/include/llvm/IR/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -1210,8 +1210,11 @@ class ConstantExpr : public Constant {
/// Return the absorbing element for the given binary
/// operation, i.e. a constant C such that X op C = C and C op X = C for
/// every X. For example, this returns zero for integer multiplication.
/// It returns null if the operator doesn't have an absorbing element.
static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
/// If AllowLHSConstant is true, the LHS operand is a constant C that must be
/// defined as C op X = C. It returns null if the operator doesn't have
/// an absorbing element.
static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty,
bool AllowLHSConstant = false);

/// Transparently provide more efficient getOperand methods.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
Expand Down
32 changes: 8 additions & 24 deletions llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,11 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,

// Handle simplifications when the RHS is a constant int.
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
if (C2 == ConstantExpr::getBinOpAbsorber(Opcode, C2->getType(),
/*AllowLHSConstant*/ false))
return C2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks incorrect. You are using the Absorber value with AllowLHSAbsorber=true and then using it to check the RHS. This means that for example X << 0 could get incorrectly folded to 0, no? I think the only reason this works out here is by accident, because this case is already handled by the identity logic earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I modified it. Please help review it, thanks!


switch (Opcode) {
case Instruction::Mul:
if (CI2->isZero())
return C2; // X * 0 == 0
break;
case Instruction::UDiv:
case Instruction::SDiv:
if (CI2->isZero())
Expand All @@ -749,9 +749,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
return PoisonValue::get(CI2->getType()); // X % 0 == poison
break;
case Instruction::And:
if (CI2->isZero())
return C2; // X & 0 == 0

assert(!CI2->isZero() && "And zero handled above");
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
// If and'ing the address of a global with a constant, fold it.
if (CE1->getOpcode() == Instruction::PtrToInt &&
Expand Down Expand Up @@ -791,10 +789,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
}
}
break;
case Instruction::Or:
if (CI2->isMinusOne())
return C2; // X | -1 == -1
break;
}
} else if (isa<ConstantInt>(C1)) {
// If C1 is a ConstantInt and C2 is not, swap the operands.
Expand Down Expand Up @@ -854,19 +848,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
}
}

switch (Opcode) {
case Instruction::SDiv:
case Instruction::UDiv:
case Instruction::URem:
case Instruction::SRem:
case Instruction::LShr:
case Instruction::AShr:
case Instruction::Shl:
if (CI1->isZero()) return C1;
break;
default:
break;
}
if (C1 == ConstantExpr::getBinOpAbsorber(Opcode, C1->getType(),
/*AllowLHSConstant*/ true))
return C1;
} else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
const APFloat &C1V = CFP1->getValueAPF();
Expand Down
29 changes: 23 additions & 6 deletions llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2735,17 +2735,34 @@ Constant *ConstantExpr::getIdentity(Instruction *I, Type *Ty,
return nullptr;
}

Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty) {
Constant *ConstantExpr::getBinOpAbsorber(unsigned Opcode, Type *Ty,
bool AllowLHSConstant) {
switch (Opcode) {
default:
// Doesn't have an absorber.
return nullptr;
break;

case Instruction::Or:
case Instruction::Or: // -1 | X = -1
return Constant::getAllOnesValue(Ty);

case Instruction::And:
case Instruction::Mul:
case Instruction::And: // 0 & X = 0
case Instruction::Mul: // 0 * X = 0
return Constant::getNullValue(Ty);
}

// AllowLHSConstant must be set.
if (!AllowLHSConstant)
return nullptr;

switch (Opcode) {
default:
return nullptr;
case Instruction::Shl: // 0 << X = 0
case Instruction::LShr: // 0 >>l X = 0
case Instruction::AShr: // 0 >>a X = 0
case Instruction::SDiv: // 0 /s X = 0
case Instruction::UDiv: // 0 /u X = 0
case Instruction::URem: // 0 %u X = 0
case Instruction::SRem: // 0 %s X = 0
return Constant::getNullValue(Ty);
}
}
Expand Down
Loading