Nicholas Chapman
2013-Sep-05 20:46 UTC
[LLVMdev] Optimisation pass to move an alloca'd array to a global constant array
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 }
Eli Friedman
2013-Sep-05 20:57 UTC
[LLVMdev] Optimisation pass to move an alloca'd array to a global constant array
On Thu, Sep 5, 2013 at 1:46 PM, Nicholas Chapman <admin at indigorenderer.com>wrote:> 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. >No, there is no such pass at the moment. -Eli> 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 } > > > ______________________________**_________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/**mailman/listinfo/llvmdev<http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130905/60c2fe1c/attachment.html>
Nuno Lopes
2013-Sep-05 21:21 UTC
[LLVMdev] Optimisation pass to move an alloca'd array to a global constant array
There's no such a pass in LLVM. I wrote one 5 years ago but never benchmarked it properly, so I never commited it (and because eventually clang got smarter). The patch is here: http://web.ist.utl.pt/nuno.lopes/llvm_constant_extractor.txt Again, the patch is 5 years old and was written for LLVM 2, so it won't work out of the box for sure, but it should work with a few tweaks. Nuno ----- Original Message ----- From: "Nicholas Chapman" <admin at indigorenderer.com> To: <llvmdev at cs.uiuc.edu> Sent: Thursday, September 05, 2013 9:46 PM Subject: [LLVMdev] Optimisation pass to move an alloca'd array to a global constant array> 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 } > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reasonably Related Threads
- Moving instructions from source Basic Block to dest Basic Block
- [LLVMdev] How to identify LLVM version?
- [LLVMdev] Confusion with a Use of a getelementptr instruction being a Use of a select instruction instead
- simplify CFG Pass in llvm
- [LLVMdev] [PATCH] Lost instcombine opportunity: "or"s of "icmp"s (commutability)