Index: llvm/include/llvm/Analysis/Utils/Local.h =================================================================== --- llvm/include/llvm/Analysis/Utils/Local.h +++ llvm/include/llvm/Analysis/Utils/Local.h @@ -30,7 +30,7 @@ bool NoAssumptions = false) { GEPOperator *GEPOp = cast(GEP); Type *IntIdxTy = DL.getIndexType(GEP->getType()); - Value *Result = Constant::getNullValue(IntIdxTy); + Value *Result = nullptr; // nullptr stands for zero. // If the GEP is inbounds, we know that none of the addressing operations will // overflow in a signed sense. @@ -56,8 +56,11 @@ Size = DL.getStructLayout(STy)->getElementOffset(OpValue); if (Size) - Result = Builder->CreateAdd(Result, ConstantInt::get(IntIdxTy, Size), - GEP->getName().str()+".offs"); + Result = + !Result + ? ConstantInt::get(IntIdxTy, Size) + : Builder->CreateAdd(Result, ConstantInt::get(IntIdxTy, Size), + GEP->getName().str() + ".offs"); continue; } @@ -71,7 +74,9 @@ Scale = ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/); // Emit an add instruction. - Result = Builder->CreateAdd(Result, Scale, GEP->getName().str()+".offs"); + Result = !Result ? Scale + : Builder->CreateAdd(Result, Scale, + GEP->getName().str() + ".offs"); continue; } @@ -91,9 +96,12 @@ } // Emit an add instruction. - Result = Builder->CreateAdd(Op, Result, GEP->getName().str()+".offs"); + Result = !Result ? Op + : Builder->CreateAdd(Op, Result, + GEP->getName().str() + ".offs"); } - return Result; + + return Result ? Result : Constant::getNullValue(IntIdxTy); } } Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1672,12 +1672,10 @@ Value *Result = EmitGEPOffset(GEP1); // If this is a single inbounds GEP and the original sub was nuw, - // then the final multiplication is also nuw. We match an extra add zero - // here, because that's what EmitGEPOffset() generates. + // then the final multiplication is also nuw. Instruction *I; if (IsNUW && !GEP2 && !Swapped && GEP1->isInBounds() && - match(Result, m_Add(m_Instruction(I), m_Zero())) && - I->getOpcode() == Instruction::Mul) + match(Result, m_Instruction(I)) && I->getOpcode() == Instruction::Mul) I->setHasNoUnsignedWrap(); // If we had a constant expression GEP on the other side offsetting the