Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp +++ lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -120,6 +120,15 @@ return MI; } + // If we have a store to a location which is known constant, we can conclude + // that the store must be storing the constant value (else the memory + // wouldn't be constant), and this must be a noop. + if (AA->pointsToConstantMemory(MI->getDest())) { + // Set the size of the copy to 0, it will be deleted on the next iteration. + MI->setLength(Constant::getNullValue(MI->getLength()->getType())); + return MI; + } + // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with // load/store. ConstantInt *MemOpLength = dyn_cast(MI->getLength()); @@ -218,6 +227,15 @@ return MI; } + // If we have a store to a location which is known constant, we can conclude + // that the store must be storing the constant value (else the memory + // wouldn't be constant), and this must be a noop. + if (AA->pointsToConstantMemory(MI->getDest())) { + // Set the size of the copy to 0, it will be deleted on the next iteration. + MI->setLength(Constant::getNullValue(MI->getLength()->getType())); + return MI; + } + // Extract the length and alignment and fill if they are constant. ConstantInt *LenC = dyn_cast(MI->getLength()); ConstantInt *FillC = dyn_cast(MI->getValue()); Index: lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -1438,6 +1438,12 @@ } } + // If we have a store to a location which is known constant, we can conclude + // that the store must be storing the constant value (else the memory + // wouldn't be constant), and this must be a noop. + if (AA->pointsToConstantMemory(Ptr)) + return eraseInstFromFunction(SI); + // Do really simple DSE, to catch cases where there are several consecutive // stores to the same location, separated by a few arithmetic operations. This // situation often occurs with bitfield accesses. Index: test/Transforms/InstCombine/memcpy.ll =================================================================== --- test/Transforms/InstCombine/memcpy.ll +++ test/Transforms/InstCombine/memcpy.ll @@ -40,7 +40,6 @@ define void @memcpy_to_constant(i8* %src) { ; CHECK-LABEL: @memcpy_to_constant( -; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 bitcast (i128* @UnknownConstant to i8*), i8* align 1 [[SRC:%.*]], i32 16, i1 false) ; CHECK-NEXT: ret void ; %dest = bitcast i128* @UnknownConstant to i8* Index: test/Transforms/InstCombine/memmove.ll =================================================================== --- test/Transforms/InstCombine/memmove.ll +++ test/Transforms/InstCombine/memmove.ll @@ -59,7 +59,6 @@ define void @memmove_to_constant(i8* %src) { ; CHECK-LABEL: @memmove_to_constant( -; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i32(i8* align 4 bitcast (i128* @UnknownConstant to i8*), i8* align 1 [[SRC:%.*]], i32 16, i1 false) ; CHECK-NEXT: ret void ; %dest = bitcast i128* @UnknownConstant to i8* Index: test/Transforms/InstCombine/memset.ll =================================================================== --- test/Transforms/InstCombine/memset.ll +++ test/Transforms/InstCombine/memset.ll @@ -26,7 +26,6 @@ define void @memset_to_constant() { ; CHECK-LABEL: @memset_to_constant( -; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 4 bitcast (i128* @Unknown to i8*), i8 0, i32 16, i1 false) ; CHECK-NEXT: ret void ; %p = bitcast i128* @Unknown to i8* Index: test/Transforms/InstCombine/store.ll =================================================================== --- test/Transforms/InstCombine/store.ll +++ test/Transforms/InstCombine/store.ll @@ -295,7 +295,6 @@ define void @store_to_constant() { ; CHECK-LABEL: @store_to_constant( -; CHECK-NEXT: store i32 0, i32* @Unknown, align 4 ; CHECK-NEXT: ret void ; store i32 0, i32* @Unknown