Index: include/llvm/IR/Operator.h =================================================================== --- include/llvm/IR/Operator.h +++ include/llvm/IR/Operator.h @@ -425,6 +425,16 @@ } return true; } + + /// hasAConstantIndex - Returns true if any of the indices of this GEP are + /// constant integers. + bool hasAConstantIndex() const { + for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) { + if (isa(I)) + return true; + } + return false; + } /// hasAllConstantIndices - Return true if all of the indices of this GEP are /// constant integers. If so, the result pointer and the first operand have Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -842,6 +842,7 @@ if (GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() && !Src.hasOneUse()) return false; + return true; } @@ -1348,6 +1349,11 @@ // normalized. if (SO1->getType() != GO1->getType()) return nullptr; + + // Is it worth summing the offsets? + if (!isa(SO1)) + return nullptr; + Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); }