[LLVMdev] Optimisation pass to move an alloca'd array to a global constant array

Nicholas Chapman admin at indigorenderer.com
Thu Sep 5 13:46:29 PDT 2013


Hi All,
I was wondering if there is an optimisation pass that moves a stack 
allocated array, initialised with constant values, to a global constant 
array.
And if there is such a pass, what requirements are there for it to operate?
My optimised IR is below.  As you can see an array of 5 integers is 
created with alloca, then each element is stored to in turn.  It would 
be nice if this array was transformed into a global constant array.

Thanks,
     Nick



; ModuleID = 'WinterModule'
target datalayout = 
"e-p:64:64:64-S128-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-f128:128:128-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"

; Function Attrs: nounwind readnone
define i32 @"main(int)"(i32 %i, i32* nocapture %hidden) #0 {
entry:
   %"Array literal space" = alloca [5 x i32], align 4
   %0 = getelementptr [5 x i32]* %"Array literal space", i64 0, i64 0
   store i32 1, i32* %0, align 4
   %1 = getelementptr [5 x i32]* %"Array literal space", i64 0, i64 1
   store i32 2, i32* %1, align 4
   %2 = getelementptr [5 x i32]* %"Array literal space", i64 0, i64 2
   store i32 3, i32* %2, align 4
   %3 = getelementptr [5 x i32]* %"Array literal space", i64 0, i64 3
   store i32 4, i32* %3, align 4
   %4 = getelementptr [5 x i32]* %"Array literal space", i64 0, i64 4
   store i32 5, i32* %4, align 4
   %5 = icmp ult i32 %i, 5
   br i1 %5, label %in-bounds.i, label %"elem(array<int, 5>, int).exit"

in-bounds.i:                                      ; preds = %entry
   %6 = sext i32 %i to i64
   %7 = getelementptr [5 x i32]* %"Array literal space", i64 0, i64 %6
   %8 = load i32* %7, align 4
   br label %"elem(array<int, 5>, int).exit"

"elem(array<int, 5>, int).exit":                  ; preds = %entry, 
%in-bounds.i
   %iftmp.i = phi i32 [ %8, %in-bounds.i ], [ 0, %entry ]
   ret i32 %iftmp.i
}

attributes #0 = { nounwind readnone }





More information about the llvm-dev mailing list