Issue 75601
Summary Missing optimization: @malloc call not conditionally placed inside if branch
Labels new issue
Assignees
Reporter 0xdeafbeef
    [Alive2 proof](https://alive2.llvm.org/ce/z/c3yvcs)

## Description
The LLVM optimizer does not move the `malloc` call into the conditional branch for a function that conditionally allocates memory based on an input parameter. This optimization is expected to reduce unnecessary allocations when the condition is not met.

## Steps to Reproduce
Consider the following C function `alloc_escape`:

```c
char* alloc_escape(int alloc) {
 char* alloced = malloc(10);
    if (alloc) {
        return alloced;
    } else {
        free(alloced);
        return NULL;
    }
}
```
or more opaque rust version:
```rust
pub fn alloc_escape(alloc: bool) -> Option<Vec<u8>> {
    let alloced = vec![0; 10];
    if alloc {
        Some(alloced)
    } else {
 None
    }
}
```

## Expected Behavior

The memory allocation should be moved inside the conditional branch to ensure that it only occurs when necessary, based on the condition.
## Actual Behavior

The memory allocation is not moved into the conditional branch, leading to a pair of unnecessary allocations and deallocations.

Llvm version -  17.0.6



_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to