[LLVMdev] Box removal

me22 me22.ca at gmail.com
Wed Jun 29 14:12:09 PDT 2011


On Tue, Jun 28, 2011 at 11:25, Timothy Baldridge <tbaldridge at gmail.com> wrote:
>
> I know other compiler projects, such as PyPy have allocation removal
> where the optimization passes see that we only use the result of an
> allocation a single time. Thinking that LLVM may do this as well, I
> tried this simple test on in-browser LLVM compiler:
>

There's no malloc remover, but there is a malloc/free remover.  If you
fix the C code:

#include <stdio.h>
#include <stdlib.h>

typedef struct Foo
{
int *x;
int x2;
}Foo;

int main(int argc, char **argv) {
Foo *f = (Foo *)malloc(sizeof(Foo));
f->x = (int *)malloc(sizeof(int));
*f->x = 10;
int i = *f->x;
free(f->x);
free(f);
return i;
}

Then it compiles exactly the way you want it to

; ModuleID = '/tmp/webcompile/_26915_0.bc'
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"
target triple = "x86_64-linux-gnu"

define i32 @main(i32 %argc, i8** nocapture %argv) nounwind readnone {
entry:
  ret i32 10
}

~ Scott



More information about the llvm-dev mailing list