From e1287f556f4f9befaa55c205e3b5a9429d897083 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Thu, 17 Jul 2014 09:10:37 +0000 Subject: [PATCH] [msan] Avoid redundant origin stores. Origin is meaningless for fully initialized values. Avoid storing origin for function arguments that are known to be always initialized (i.e. shadow is a compile-time null constant). This is not about correctness, but purely an optimization. Seems to affect compilation time of blacklisted functions significantly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213239 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Instrumentation/MemorySanitizer.cpp | 5 ++++- .../MemorySanitizer/msan_basic.ll | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/lib/Transforms/Instrumentation/MemorySanitizer.cpp index 496ab4877e0..57e308c20db 100644 --- a/lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ b/lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -2313,6 +2313,7 @@ struct MemorySanitizerVisitor : public InstVisitor { Value *ArgShadowBase = getShadowPtrForArgument(A, IRB, ArgOffset); DEBUG(dbgs() << " Arg#" << i << ": " << *A << " Shadow: " << *ArgShadow << "\n"); + bool ArgIsInitialized = false; if (CS.paramHasAttr(i + 1, Attribute::ByVal)) { assert(A->getType()->isPointerTy() && "ByVal argument is not a pointer!"); @@ -2325,8 +2326,10 @@ struct MemorySanitizerVisitor : public InstVisitor { Size = MS.DL->getTypeAllocSize(A->getType()); Store = IRB.CreateAlignedStore(ArgShadow, ArgShadowBase, kShadowTLSAlignment); + Constant *Cst = dyn_cast(ArgShadow); + if (Cst && Cst->isNullValue()) ArgIsInitialized = true; } - if (MS.TrackOrigins) + if (MS.TrackOrigins && !ArgIsInitialized) IRB.CreateStore(getOrigin(A), getOriginPtrForArgument(A, IRB, ArgOffset)); (void)Store; diff --git a/test/Instrumentation/MemorySanitizer/msan_basic.ll b/test/Instrumentation/MemorySanitizer/msan_basic.ll index 51693cdd567..0faf45d70c5 100644 --- a/test/Instrumentation/MemorySanitizer/msan_basic.ll +++ b/test/Instrumentation/MemorySanitizer/msan_basic.ll @@ -766,6 +766,24 @@ cond.end: ; preds = %cond.false, %cond.t ; CHECK: ret i32 [[A]] +; Test that there are no __msan_param_origin_tls stores when +; argument shadow is a compile-time zero constant (which is always the case +; in functions missing sanitize_memory attribute). + +define i32 @NoSanitizeMemoryParamTLS(i32* nocapture readonly %x) { +entry: + %0 = load i32* %x, align 4 + %call = tail call i32 @NoSanitizeMemoryParamTLSHelper(i32 %0) + ret i32 %call +} + +declare i32 @NoSanitizeMemoryParamTLSHelper(i32 %x) + +; CHECK-LABEL: define i32 @NoSanitizeMemoryParamTLS( +; CHECK-NOT: __msan_param_origin_tls +; CHECK: ret i32 + + ; Test argument shadow alignment define <2 x i64> @ArgumentShadowAlignment(i64 %a, <2 x i64> %b) sanitize_memory {