[LLVMbugs] [Bug 7006] New: Missed struct allocation escape analysis

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat May 1 08:53:16 PDT 2010


http://llvm.org/bugs/show_bug.cgi?id=7006

           Summary: Missed struct allocation escape analysis
           Product: new-bugs
           Version: 2.7
          Platform: Other
        OS/Version: other
            Status: NEW
          Keywords: code-quality
          Severity: normal
          Priority: P
         Component: new bugs
        AssignedTo: unassignedbugs at nondot.org
        ReportedBy: bearophile at mailas.com
                CC: llvmbugs at cs.uiuc.edu


This little C program shows something I have found in a larger program:



#include "stdio.h"
#include "stdlib.h"

#define WITH_Y

typedef struct {
    int x;
#ifdef WITH_Y
    int y;
#endif
    double z;
} Foo;

Foo *new_Foo(int n) {
    Foo *f = (Foo *)malloc(sizeof(Foo));
    f->x = n * 1;
#ifdef WITH_Y
    f->y = n * 2;
#endif
    f->z = n * 3;
    return f;
}

int main() {
    int n = atoi("20");
    Foo *f = new_Foo(n);
    printf("%f\n", f->z);
    free(f);
    return 0;
}



With the online llvm 2.7 demo, Optimization level: LTO

If WITH_Y is defined there is a malloc:

define i32 @main() nounwind {
entry:
  %0 = tail call i32 @atoi(i8* getelementptr inbounds ([3 x i8]* @.str, i64 0,
i64 0)) nounwind readonly ; <i32> [#uses=2]
  %1 = tail call noalias i8* @malloc(i64 16) nounwind ; <i8*> [#uses=2]
  %2 = bitcast i8* %1 to i32*                     ; <i32*> [#uses=1]
  store i32 %0, i32* %2, align 8
  %3 = mul nsw i32 %0, 3                          ; <i32> [#uses=1]
  %4 = sitofp i32 %3 to double                    ; <double> [#uses=1]
  %5 = tail call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4
x i8]* @.str1, i64 0, i64 0), double %4) nounwind ; <i32> [#uses=0]
  tail call void @free(i8* %1) nounwind
  ret i32 0
}



If WITH_Y is not defined there isn't a malloc:

define i32 @main() nounwind {
entry:
  %0 = tail call i32 @atoi(i8* getelementptr inbounds ([3 x i8]* @.str, i64 0,
i64 0)) nounwind readonly ; <i32> [#uses=1]
  %1 = mul nsw i32 %0, 3                          ; <i32> [#uses=1]
  %2 = sitofp i32 %1 to double                    ; <double> [#uses=1]
  %3 = tail call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4
x i8]* @.str1, i64 0, i64 0), double %2) nounwind ; <i32> [#uses=0]
  ret i32 0
}

-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list