@@ -2864,14 +2864,7 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
28642864 auto *NewBO = BinaryOperator::Create (
28652865 Opcode, LV, Inv, BO->getName () + " .reass" , BO->getIterator ());
28662866
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) {
28752868 // Intersect FMF flags for FADD and FMUL.
28762869 FastMathFlags Intersect = BO->getFastMathFlags () & BO0->getFastMathFlags ();
28772870 if (auto *I = dyn_cast<Instruction>(Inv))
@@ -2884,6 +2877,16 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
28842877 if (auto *I = dyn_cast<PossiblyDisjointInst>(Inv))
28852878 I->setIsDisjoint (Disjoint);
28862879 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);
28872890 }
28882891
28892892 BO->replaceAllUsesWith (NewBO);
0 commit comments