This is an archive of the discontinued LLVM Phabricator instance.

[LibCallSimplifier] fold memset(malloc(x), 0, x) to calloc(1, x) if memset is intrinsic
Needs ReviewPublic

Authored by JudyZhu on Dec 15 2020, 7:11 PM.

Details

Summary

this patch want to fix the bug: https://llvm.org/bugs/show_bug.cgi?id=25892

When there is memset in code, llvm might transform memset to llvm.memset.*.
But llvm might not handle this well, so I use this patch to do it.

test:
check-llvm

Diff Detail

Event Timeline

JudyZhu created this revision.Dec 15 2020, 7:11 PM
JudyZhu requested review of this revision.Dec 15 2020, 7:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 15 2020, 7:11 PM

Please review the comments in D16337 - especially:
https://reviews.llvm.org/D16337#331117

How does this patch guarantee that memory is not touched between the malloc and the memset? Is the transform safe on this example?

define float* @store_between_malloc_and_memset(i64 %size) {
entry:
  %call = tail call i8* @malloc(i64 %size) #1
  %bc1 = bitcast i8* %call to i32*
  store i32 1, i32* %bc1, align 4
  %cmp = icmp eq i8* %call, null
  br i1 %cmp, label %cleanup, label %if.end

if.end:
  %bc = bitcast i8* %call to float*
  %call3 = tail call i8* @memset(i8* %call, i32 0, i64 %size)
  br label %cleanup

cleanup:
  %r = phi float* [ %bc, %if.end ], [ null, %entry ]
  ret float* %r
}

yes, that might get an error.
let me change it.

Please review the comments in D16337 - especially:
https://reviews.llvm.org/D16337#331117

How does this patch guarantee that memory is not touched between the malloc and the memset? Is the transform safe on this example?

define float* @store_between_malloc_and_memset(i64 %size) {
entry:
  %call = tail call i8* @malloc(i64 %size) #1
  %bc1 = bitcast i8* %call to i32*
  store i32 1, i32* %bc1, align 4
  %cmp = icmp eq i8* %call, null
  br i1 %cmp, label %cleanup, label %if.end

if.end:
  %bc = bitcast i8* %call to float*
  %call3 = tail call i8* @memset(i8* %call, i32 0, i64 %size)
  br label %cleanup

cleanup:
  %r = phi float* [ %bc, %if.end ], [ null, %entry ]
  ret float* %r
}

Fixed by @yurai007

llvm/test/Transforms/InstCombine/memset-1.ll
60

Remove fixme

lebedev.ri resigned from this revision.Jan 12 2023, 5:23 PM

This review seems to be stuck/dead, consider abandoning if no longer relevant.

Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2023, 5:23 PM
Herald added a subscriber: StephenFan. · View Herald Transcript