Skip to content

[Local] Move OverflowTracking to Local.h, move logic to helpers (NFC) #140403

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 2 commits into from
May 19, 2025
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
14 changes: 2 additions & 12 deletions llvm/include/llvm/Transforms/Scalar/Reassociate.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Function;
class Instruction;
class IRBuilderBase;
class Value;
struct OverflowTracking;

/// A private "module" namespace for types and utilities used by Reassociate.
/// These are implementation details and should not be used by clients.
Expand All @@ -64,17 +65,6 @@ struct Factor {
Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {}
};

struct OverflowTracking {
bool HasNUW = true;
bool HasNSW = true;
bool AllKnownNonNegative = true;
bool AllKnownNonZero = true;
// Note: AllKnownNonNegative can be true in a case where one of the operands
// is negative, but one the operators is not NSW. AllKnownNonNegative should
// not be used independently of HasNSW
OverflowTracking() = default;
};

class XorOpnd;

} // end namespace reassociate
Expand Down Expand Up @@ -115,7 +105,7 @@ class ReassociatePass : public PassInfoMixin<ReassociatePass> {
void ReassociateExpression(BinaryOperator *I);
void RewriteExprTree(BinaryOperator *I,
SmallVectorImpl<reassociate::ValueEntry> &Ops,
reassociate::OverflowTracking Flags);
OverflowTracking Flags);
Value *OptimizeExpression(BinaryOperator *I,
SmallVectorImpl<reassociate::ValueEntry> &Ops);
Value *OptimizeAdd(Instruction *I,
Expand Down
24 changes: 24 additions & 0 deletions llvm/include/llvm/Transforms/Utils/Local.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,30 @@ Value *invertCondition(Value *Condition);
/// function, explicitly materialize the maximal set in the IR.
bool inferAttributesFromOthers(Function &F);

//===----------------------------------------------------------------------===//
// Helpers to track and update flags on instructions.
//

struct OverflowTracking {
bool HasNUW = true;
bool HasNSW = true;

// Note: At the moment, users are responsible to manage AllKnownNonNegative
// and AllKnownNonZero manually. AllKnownNonNegative can be true in a case
// where one of the operands is negative, but one the operators is not NSW.
// AllKnownNonNegative should not be used independently of HasNSW
bool AllKnownNonNegative = true;
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment indicating that the user is responsible for updating AllKnownNonNegative and AllKnownNonZero.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done thanks

bool AllKnownNonZero = true;

OverflowTracking() = default;

/// Merge in the no-wrap flags from \p I.
void mergeFlags(Instruction &I);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Random thought - maybe we should have two merge routines, one for the interior nodes being reassociated, and one for the leaves? This would let us move all the KnownNonNegative and KnownNonZero logic out, and reuse it from LICM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will share a separate patch for that, thanks


/// Apply the no-wrap flags to \p I if applicable.
void applyFlags(Instruction &I);
};

} // end namespace llvm

#endif // LLVM_TRANSFORMS_UTILS_LOCAL_H
17 changes: 3 additions & 14 deletions llvm/lib/Transforms/Scalar/Reassociate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ using RepeatedValue = std::pair<Value *, uint64_t>;
static bool LinearizeExprTree(Instruction *I,
SmallVectorImpl<RepeatedValue> &Ops,
ReassociatePass::OrderedSet &ToRedo,
reassociate::OverflowTracking &Flags) {
OverflowTracking &Flags) {
assert((isa<UnaryOperator>(I) || isa<BinaryOperator>(I)) &&
"Expected a UnaryOperator or BinaryOperator!");
LLVM_DEBUG(dbgs() << "LINEARIZE: " << *I << '\n');
Expand Down Expand Up @@ -431,10 +431,7 @@ static bool LinearizeExprTree(Instruction *I,
// We examine the operands of this binary operator.
auto [I, Weight] = Worklist.pop_back_val();

if (isa<OverflowingBinaryOperator>(I)) {
Flags.HasNUW &= I->hasNoUnsignedWrap();
Flags.HasNSW &= I->hasNoSignedWrap();
}
Flags.mergeFlags(*I);

for (unsigned OpIdx = 0; OpIdx < I->getNumOperands(); ++OpIdx) { // Visit operands.
Value *Op = I->getOperand(OpIdx);
Expand Down Expand Up @@ -734,15 +731,7 @@ void ReassociatePass::RewriteExprTree(BinaryOperator *I,
ExpressionChangedStart->clearSubclassOptionalData();
ExpressionChangedStart->setFastMathFlags(Flags);
} else {
ExpressionChangedStart->clearSubclassOptionalData();
if (ExpressionChangedStart->getOpcode() == Instruction::Add ||
(ExpressionChangedStart->getOpcode() == Instruction::Mul &&
Flags.AllKnownNonZero)) {
if (Flags.HasNUW)
ExpressionChangedStart->setHasNoUnsignedWrap();
if (Flags.HasNSW && (Flags.AllKnownNonNegative || Flags.HasNUW))
ExpressionChangedStart->setHasNoSignedWrap();
}
Flags.applyFlags(*ExpressionChangedStart);
}
}

Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4362,3 +4362,21 @@ bool llvm::inferAttributesFromOthers(Function &F) {

return Changed;
}

void OverflowTracking::mergeFlags(Instruction &I) {
if (isa<OverflowingBinaryOperator>(&I)) {
HasNUW &= I.hasNoUnsignedWrap();
HasNSW &= I.hasNoSignedWrap();
}
}

void OverflowTracking::applyFlags(Instruction &I) {
I.clearSubclassOptionalData();
if (I.getOpcode() == Instruction::Add ||
(I.getOpcode() == Instruction::Mul && AllKnownNonZero)) {
if (HasNUW)
I.setHasNoUnsignedWrap();
if (HasNSW && (AllKnownNonNegative || HasNUW))
I.setHasNoSignedWrap();
}
}