diff options
Diffstat (limited to 'lib/Transforms/Scalar/NaryReassociate.cpp')
-rw-r--r-- | lib/Transforms/Scalar/NaryReassociate.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/Transforms/Scalar/NaryReassociate.cpp b/lib/Transforms/Scalar/NaryReassociate.cpp index b026c8d692c3..7106ea216ad6 100644 --- a/lib/Transforms/Scalar/NaryReassociate.cpp +++ b/lib/Transforms/Scalar/NaryReassociate.cpp @@ -83,6 +83,7 @@ #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" +#include "llvm/Transforms/Utils/Local.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constants.h" @@ -105,7 +106,6 @@ #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Transforms/Scalar.h" -#include "llvm/Transforms/Utils/Local.h" #include <cassert> #include <cstdint> @@ -240,10 +240,17 @@ bool NaryReassociatePass::doOneIteration(Function &F) { Changed = true; SE->forgetValue(&*I); I->replaceAllUsesWith(NewI); - // If SeenExprs constains I's WeakTrackingVH, that entry will be - // replaced with - // nullptr. + WeakVH NewIExist = NewI; + // If SeenExprs/NewIExist contains I's WeakTrackingVH/WeakVH, that + // entry will be replaced with nullptr if deleted. RecursivelyDeleteTriviallyDeadInstructions(&*I, TLI); + if (!NewIExist) { + // Rare occation where the new instruction (NewI) have been removed, + // probably due to parts of the input code was dead from the + // beginning, reset the iterator and start over from the beginning + I = BB->begin(); + continue; + } I = NewI->getIterator(); } // Add the rewritten instruction to SeenExprs; the original instruction @@ -429,6 +436,9 @@ NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP, Instruction *NaryReassociatePass::tryReassociateBinaryOp(BinaryOperator *I) { Value *LHS = I->getOperand(0), *RHS = I->getOperand(1); + // There is no need to reassociate 0. + if (SE->getSCEV(I)->isZero()) + return nullptr; if (auto *NewI = tryReassociateBinaryOp(LHS, RHS, I)) return NewI; if (auto *NewI = tryReassociateBinaryOp(RHS, LHS, I)) |