Safely turn memset_chk etc. to non-chk variant if the known object size is >= memset...
authorEvan Cheng <evan.cheng@apple.com>
Fri, 5 Mar 2010 20:59:47 +0000 (20:59 +0000)
committerEvan Cheng <evan.cheng@apple.com>
Fri, 5 Mar 2010 20:59:47 +0000 (20:59 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97828 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/SimplifyLibCalls.cpp
test/Transforms/SimplifyLibCalls/memset_chk.ll [new file with mode: 0644]

index 5eae7b30cce3032db56f3d5b3ed6dc9b1a29dc8f..1a351f0e06f9e7708969538b9f7f21b520854d7a 100644 (file)
@@ -1006,10 +1006,12 @@ struct MemCpyChkOpt : public LibCallOptimization {
         FT->getParamType(2) != TD->getIntPtrType(*Context))
       return 0;
 
-    ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
-    if (!SizeCI)
+    ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
+    if (!ObjSizeCI)
       return 0;
-    if (SizeCI->isAllOnesValue()) {
+    ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
+    if (ObjSizeCI->isAllOnesValue() ||
+        (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
       EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
       return CI->getOperand(1);
     }
@@ -1034,10 +1036,12 @@ struct MemSetChkOpt : public LibCallOptimization {
         FT->getParamType(2) != TD->getIntPtrType(*Context))
       return 0;
 
-    ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
-    if (!SizeCI)
+    ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
+    if (!ObjSizeCI)
       return 0;
-    if (SizeCI->isAllOnesValue()) {
+    ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
+    if (ObjSizeCI->isAllOnesValue() ||
+        (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
       Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
                                   false);
       EmitMemSet(CI->getOperand(1), Val,  CI->getOperand(3), B);
@@ -1064,10 +1068,12 @@ struct MemMoveChkOpt : public LibCallOptimization {
         FT->getParamType(2) != TD->getIntPtrType(*Context))
       return 0;
 
-    ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
-    if (!SizeCI)
+    ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
+    if (!ObjSizeCI)
       return 0;
-    if (SizeCI->isAllOnesValue()) {
+    ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
+    if (ObjSizeCI->isAllOnesValue() ||
+        (SizeCI && ObjSizeCI->getValue().uge(SizeCI->getValue()))) {
       EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
                  1, B);
       return CI->getOperand(1);
@@ -1085,8 +1091,8 @@ struct StrCpyChkOpt : public LibCallOptimization {
         !FT->getParamType(1)->isPointerTy())
       return 0;
 
-    ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
-    if (!SizeCI)
+    ConstantInt *ObjSizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
+    if (!ObjSizeCI)
       return 0;
     
     // If a) we don't have any length information, or b) we know this will
@@ -1094,8 +1100,8 @@ struct StrCpyChkOpt : public LibCallOptimization {
     // strcpy_chk call which may fail at runtime if the size is too long.
     // TODO: It might be nice to get a maximum length out of the possible
     // string lengths for varying.
-    if (SizeCI->isAllOnesValue() ||
-        SizeCI->getZExtValue() >= GetStringLength(CI->getOperand(2)))
+    if (ObjSizeCI->isAllOnesValue() ||
+        ObjSizeCI->getZExtValue() >= GetStringLength(CI->getOperand(2)))
       return EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B);
 
     return 0;
diff --git a/test/Transforms/SimplifyLibCalls/memset_chk.ll b/test/Transforms/SimplifyLibCalls/memset_chk.ll
new file mode 100644 (file)
index 0000000..c4ef60e
--- /dev/null
@@ -0,0 +1,18 @@
+; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
+; rdar://7719085
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
+
+%struct.data = type { [100 x i32], [100 x i32], [1024 x i8] }
+
+define i32 @t() nounwind ssp {
+; CHECK: @t
+; CHECK: @llvm.memset.i64
+entry:
+  %0 = alloca %struct.data, align 8               ; <%struct.data*> [#uses=1]
+  %1 = bitcast %struct.data* %0 to i8*            ; <i8*> [#uses=1]
+  %2 = call i8* @__memset_chk(i8* %1, i32 0, i64 1824, i64 1824) nounwind ; <i8*> [#uses=0]
+  ret i32 0
+}
+
+declare i8* @__memset_chk(i8*, i32, i64, i64) nounwind