Skip to content

Commit 545b4cd

Browse files
committed
[Local] Move OverflowTracking to Local.h, move logic to helpers (NFC)
Move parts of the logic used by Reassociate to OverflowTracking (mergeFlags & applyFlags) and move the definition to Local.h (not sure if there's a better place?). For now it just moves the NUW/NSW handling, as this matches the uses in LICM. I'll look into the FP math handling separately, as it looks like there's a difference between Reassociate (takes all flags from I, while LICM takes the intersection of the flags on both instructions).
1 parent 55531e3 commit 545b4cd

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

llvm/include/llvm/Transforms/Scalar/Reassociate.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Function;
3939
class Instruction;
4040
class IRBuilderBase;
4141
class Value;
42+
struct OverflowTracking;
4243

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

67-
struct OverflowTracking {
68-
bool HasNUW = true;
69-
bool HasNSW = true;
70-
bool AllKnownNonNegative = true;
71-
bool AllKnownNonZero = true;
72-
// Note: AllKnownNonNegative can be true in a case where one of the operands
73-
// is negative, but one the operators is not NSW. AllKnownNonNegative should
74-
// not be used independently of HasNSW
75-
OverflowTracking() = default;
76-
};
77-
7868
class XorOpnd;
7969

8070
} // end namespace reassociate
@@ -115,7 +105,7 @@ class ReassociatePass : public PassInfoMixin<ReassociatePass> {
115105
void ReassociateExpression(BinaryOperator *I);
116106
void RewriteExprTree(BinaryOperator *I,
117107
SmallVectorImpl<reassociate::ValueEntry> &Ops,
118-
reassociate::OverflowTracking Flags);
108+
OverflowTracking Flags);
119109
Value *OptimizeExpression(BinaryOperator *I,
120110
SmallVectorImpl<reassociate::ValueEntry> &Ops);
121111
Value *OptimizeAdd(Instruction *I,

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,27 @@ Value *invertCondition(Value *Condition);
556556
/// function, explicitly materialize the maximal set in the IR.
557557
bool inferAttributesFromOthers(Function &F);
558558

559+
//===----------------------------------------------------------------------===//
560+
// Helpers to track and update flags on instructions.
561+
//
562+
563+
struct OverflowTracking {
564+
bool HasNUW = true;
565+
bool HasNSW = true;
566+
bool AllKnownNonNegative = true;
567+
bool AllKnownNonZero = true;
568+
// Note: AllKnownNonNegative can be true in a case where one of the operands
569+
// is negative, but one the operators is not NSW. AllKnownNonNegative should
570+
// not be used independently of HasNSW
571+
OverflowTracking() = default;
572+
573+
/// Merge in the no-wrap flags from \p I.
574+
void mergeFlags(Instruction &I);
575+
576+
/// Apply the no-wrap flags to \p I if applicable.
577+
void applyFlags(Instruction &I);
578+
};
579+
559580
} // end namespace llvm
560581

561582
#endif // LLVM_TRANSFORMS_UTILS_LOCAL_H

llvm/lib/Transforms/Scalar/Reassociate.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ using RepeatedValue = std::pair<Value *, uint64_t>;
382382
static bool LinearizeExprTree(Instruction *I,
383383
SmallVectorImpl<RepeatedValue> &Ops,
384384
ReassociatePass::OrderedSet &ToRedo,
385-
reassociate::OverflowTracking &Flags) {
385+
OverflowTracking &Flags) {
386386
assert((isa<UnaryOperator>(I) || isa<BinaryOperator>(I)) &&
387387
"Expected a UnaryOperator or BinaryOperator!");
388388
LLVM_DEBUG(dbgs() << "LINEARIZE: " << *I << '\n');
@@ -431,10 +431,7 @@ static bool LinearizeExprTree(Instruction *I,
431431
// We examine the operands of this binary operator.
432432
auto [I, Weight] = Worklist.pop_back_val();
433433

434-
if (isa<OverflowingBinaryOperator>(I)) {
435-
Flags.HasNUW &= I->hasNoUnsignedWrap();
436-
Flags.HasNSW &= I->hasNoSignedWrap();
437-
}
434+
Flags.mergeFlags(*I);
438435

439436
for (unsigned OpIdx = 0; OpIdx < I->getNumOperands(); ++OpIdx) { // Visit operands.
440437
Value *Op = I->getOperand(OpIdx);
@@ -734,15 +731,7 @@ void ReassociatePass::RewriteExprTree(BinaryOperator *I,
734731
ExpressionChangedStart->clearSubclassOptionalData();
735732
ExpressionChangedStart->setFastMathFlags(Flags);
736733
} else {
737-
ExpressionChangedStart->clearSubclassOptionalData();
738-
if (ExpressionChangedStart->getOpcode() == Instruction::Add ||
739-
(ExpressionChangedStart->getOpcode() == Instruction::Mul &&
740-
Flags.AllKnownNonZero)) {
741-
if (Flags.HasNUW)
742-
ExpressionChangedStart->setHasNoUnsignedWrap();
743-
if (Flags.HasNSW && (Flags.AllKnownNonNegative || Flags.HasNUW))
744-
ExpressionChangedStart->setHasNoSignedWrap();
745-
}
734+
Flags.applyFlags(*ExpressionChangedStart);
746735
}
747736
}
748737

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4362,3 +4362,21 @@ bool llvm::inferAttributesFromOthers(Function &F) {
43624362

43634363
return Changed;
43644364
}
4365+
4366+
void OverflowTracking::mergeFlags(Instruction &I) {
4367+
if (isa<OverflowingBinaryOperator>(&I)) {
4368+
HasNUW &= I.hasNoUnsignedWrap();
4369+
HasNSW &= I.hasNoSignedWrap();
4370+
}
4371+
}
4372+
4373+
void OverflowTracking::applyFlags(Instruction &I) {
4374+
I.clearSubclassOptionalData();
4375+
if (I.getOpcode() == Instruction::Add ||
4376+
(I.getOpcode() == Instruction::Mul && AllKnownNonZero)) {
4377+
if (HasNUW)
4378+
I.setHasNoUnsignedWrap();
4379+
if (HasNSW && (AllKnownNonNegative || HasNUW))
4380+
I.setHasNoSignedWrap();
4381+
}
4382+
}

0 commit comments

Comments
 (0)