Index: lib/CodeGen/ManagedMemoryRewrite.cpp =================================================================== --- lib/CodeGen/ManagedMemoryRewrite.cpp +++ lib/CodeGen/ManagedMemoryRewrite.cpp @@ -102,6 +102,7 @@ Function *PollyMallocManaged = GetOrCreatePollyMallocManaged(M); assert(PollyMallocManaged && "unable to create polly_mallocManaged"); Malloc->replaceAllUsesWith(PollyMallocManaged); + Malloc->eraseFromParent(); } Function *Free = M.getFunction("free"); @@ -110,6 +111,7 @@ Function *PollyFreeManaged = GetOrCreatePollyFreeManaged(M); assert(PollyFreeManaged && "unable to create polly_freeManaged"); Free->replaceAllUsesWith(PollyFreeManaged); + Free->eraseFromParent(); } return true; Index: test/GPGPU/managed-memory-rewrite-malloc-free.ll =================================================================== --- test/GPGPU/managed-memory-rewrite-malloc-free.ll +++ test/GPGPU/managed-memory-rewrite-malloc-free.ll @@ -41,6 +41,10 @@ ; HOST-IR call void @polly_freeManaged(i8* %toFreeBitcast) ; HOST-IR: declare void @polly_freeManaged(i8*) +; // Check that we remove the original malloc,free +; HOST-IR-NOT: declare i8* @malloc(i64) +; HOST-IR-NOT: declare void @free(i8*) + target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.12.0"