diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp --- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -456,8 +456,15 @@ // Check to see if this stored value is of the same byte-splattable value. Value *StoredByte = isBytewiseValue(StoredVal, DL); + // If the previously stored value is undef, we can replace it with the + // newly stored value (which may be undef) + // Inversely, if the newly stored value is undef, we can replace it with + // the previously stored one if (isa(ByteVal) && StoredByte) ByteVal = StoredByte; + else if (StoredByte && isa(StoredByte)) + StoredByte = ByteVal; + if (ByteVal != StoredByte) break;