@@ -2864,14 +2864,7 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
2864
2864
auto *NewBO = BinaryOperator::Create (
2865
2865
Opcode, LV, Inv, BO->getName () + " .reass" , BO->getIterator ());
2866
2866
2867
- // Copy NUW for ADDs if both instructions have it.
2868
- if (Opcode == Instruction::Add && BO->hasNoUnsignedWrap () &&
2869
- BO0->hasNoUnsignedWrap ()) {
2870
- // If `Inv` was not constant-folded, a new Instruction has been created.
2871
- if (auto *I = dyn_cast<Instruction>(Inv))
2872
- I->setHasNoUnsignedWrap (true );
2873
- NewBO->setHasNoUnsignedWrap (true );
2874
- } else if (Opcode == Instruction::FAdd || Opcode == Instruction::FMul) {
2867
+ if (Opcode == Instruction::FAdd || Opcode == Instruction::FMul) {
2875
2868
// Intersect FMF flags for FADD and FMUL.
2876
2869
FastMathFlags Intersect = BO->getFastMathFlags () & BO0->getFastMathFlags ();
2877
2870
if (auto *I = dyn_cast<Instruction>(Inv))
@@ -2884,6 +2877,16 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
2884
2877
if (auto *I = dyn_cast<PossiblyDisjointInst>(Inv))
2885
2878
I->setIsDisjoint (Disjoint);
2886
2879
cast<PossiblyDisjointInst>(NewBO)->setIsDisjoint (Disjoint);
2880
+ } else {
2881
+ OverflowTracking Flags;
2882
+ Flags.AllKnownNonNegative = false ;
2883
+ Flags.AllKnownNonZero = false ;
2884
+ Flags.mergeFlags (*BO);
2885
+ Flags.mergeFlags (*BO0);
2886
+ // If `Inv` was not constant-folded, a new Instruction has been created.
2887
+ if (auto *I = dyn_cast<Instruction>(Inv))
2888
+ Flags.applyFlags (*I);
2889
+ Flags.applyFlags (*NewBO);
2887
2890
}
2888
2891
2889
2892
BO->replaceAllUsesWith (NewBO);
0 commit comments