[PATCH] D16337: [LibCallSimplifier] fold memset(malloc(x), 0, x) --> calloc(1, x)

Chad Rosier via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 20 06:46:45 PST 2016


mcrosier added a comment.

What happens if there's an intervening store to the malloc'ed memory?  (dumb I know.. but..)

Slightly modified version of the test case in memset_chk-1.ll:

define float* @pr25892(i64 %size) #0 {
entry:

  %call = tail call i8* @malloc(i64 %size) #1
  %0 = bitcast i8* %call to i32*        ;; 
  store i32 1, i32* %0, align 4           ;; fdata[0] = 1;
  %cmp = icmp eq i8* %call, null
  br i1 %cmp, label %cleanup, label %if.end

if.end:

  %bc = bitcast i8* %call to float*
  %call2 = tail call i64 @llvm.objectsize.i64.p0i8(i8* nonnull %call, i1 false)
  %call3 = tail call i8* @__memset_chk(i8* nonnull %call, i32 0, i64 %size, i64 %call2) #1
  br label %cleanup

cleanup:

  %retval.0 = phi float* [ %bc, %if.end ], [ null, %entry ]
  ret float* %retval.0

}

This does get transformed incorrectly with this patch.

Chad


================
Comment at: lib/Transforms/Utils/SimplifyLibCalls.cpp:978
@@ +977,3 @@
+                             B, DL, TLI);
+  Malloc->replaceAllUsesWith(Calloc);
+  Malloc->eraseFromParent();
----------------
Shouldn't you check that Calloc is non-null here?


http://reviews.llvm.org/D16337





More information about the llvm-commits mailing list