[PATCH] D46752: [Analysis] Validate the return type of s(n)printf libcalls

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 11 07:00:43 PDT 2018


mstorsjo updated this revision to Diff 146320.
mstorsjo retitled this revision from "[InstCombine] Validate the assumption about return type in optimizeSnPrintFString" to "[Analysis] Validate the return type of s(n)printf libcalls".
mstorsjo edited the summary of this revision.

https://reviews.llvm.org/D46752

Files:
  lib/Analysis/TargetLibraryInfo.cpp
  test/Transforms/InstCombine/sprintf-void.ll


Index: test/Transforms/InstCombine/sprintf-void.ll
===================================================================
--- /dev/null
+++ test/Transforms/InstCombine/sprintf-void.ll
@@ -0,0 +1,19 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
+
+ at hello_world = constant [13 x i8] c"hello world\0A\00"
+
+declare void @sprintf(i8*, i8*, ...)
+
+; Check that a sprintf call, that would otherwise be optimized, but with
+; optimized out return type, doesn't crash the optimizer.
+
+define void @test_simplify1(i8* %dst) {
+; CHECK-LABEL: @test_simplify1(
+  %fmt = getelementptr [13 x i8], [13 x i8]* @hello_world, i32 0, i32 0
+  call void (i8*, i8*, ...) @sprintf(i8* %dst, i8* %fmt)
+; CHECK-NEXT: call void (i8*, i8*, ...) @sprintf(i8* %dst, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @hello_world, i32 0, i32 0))
+  ret void
+; CHECK-NEXT: ret void
+}
Index: lib/Analysis/TargetLibraryInfo.cpp
===================================================================
--- lib/Analysis/TargetLibraryInfo.cpp
+++ lib/Analysis/TargetLibraryInfo.cpp
@@ -692,10 +692,12 @@
   case LibFunc_siprintf:
   case LibFunc_sprintf:
     return (NumParams >= 2 && FTy.getParamType(0)->isPointerTy() &&
-            FTy.getParamType(1)->isPointerTy());
+            FTy.getParamType(1)->isPointerTy() &&
+            FTy.getReturnType()->isIntegerTy());
   case LibFunc_snprintf:
     return (NumParams == 3 && FTy.getParamType(0)->isPointerTy() &&
-            FTy.getParamType(2)->isPointerTy());
+            FTy.getParamType(2)->isPointerTy() &&
+            FTy.getReturnType()->isIntegerTy());
   case LibFunc_setitimer:
     return (NumParams == 3 && FTy.getParamType(1)->isPointerTy() &&
             FTy.getParamType(2)->isPointerTy());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46752.146320.patch
Type: text/x-patch
Size: 1882 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180511/61219f9b/attachment.bin>


More information about the llvm-commits mailing list