Russell Wallace via llvm-dev
2022-Jan-17 08:07 UTC
[llvm-dev] Which optimization pass deals with heap values?
All the optimization passes I can find in the documentation, deal with
register, stack or global values; I cannot find any mention of anything
trying to optimize values on the heap. But this:
#include <stdlib.h>
int main(int argc, char **argv) {
int *a = malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++)
a[i] = i;
return a[5];
}
compiles to 'ret 5' (as it should). Which pass does that?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20220117/1ff82f5c/attachment.html>
chuanqi.xcq via llvm-dev
2022-Jan-17 09:49 UTC
[llvm-dev] Which optimization pass deals with heap values?
Hi,
The optimization should be done at InstructionCombine pass. See:
https://godbolt.org/z/zGvrMerKv. Previous cleaning passes are necessary.
But I feel this optimization is a little bit aggresive. Since it changes the
behavior of the program. The program above might hit a memory leak while the
optimized one would not.
As a reference, GCC wouldn't do this under O2:
https://godbolt.org/z/5z1GWMWPj. It would only do this under O3.
Thanks,
Chuanqi
------------------------------------------------------------------
From:llvm-dev <llvm-dev at lists.llvm.org>
Send Time:2022年1月17日(星期一) 16:08
To:llvm-dev <llvm-dev at lists.llvm.org>
Subject:[llvm-dev] Which optimization pass deals with heap values?
All the optimization passes I can find in the documentation, deal with register,
stack or global values; I cannot find any mention of anything trying to optimize
values on the heap. But this:
#include <stdlib.h>
int main(int argc, char **argv) {
int *a = malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++)
a[i] = i;
return a[5];
}
compiles to 'ret 5' (as it should). Which pass does that?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20220117/16c62777/attachment.html>
Florian Hahn via llvm-dev
2022-Jan-17 09:50 UTC
[llvm-dev] Which optimization pass deals with heap values?
> On Jan 17, 2022, at 08:07, Russell Wallace via llvm-dev <llvm-dev at lists.llvm.org> wrote: > > All the optimization passes I can find in the documentation, deal with register, stack or global values; I cannot find any mention of anything trying to optimize values on the heap. But this: > > #include <stdlib.h> > > int main(int argc, char **argv) { > int *a = malloc(10 * sizeof(int)); > for (int i = 0; i < 10; i++) > a[i] = i; > return a[5]; > } > > compiles to 'ret 5' (as it should). Which pass does that? > _____________________________________________You can use `-mllvm -print-changed` with clang when built with assertions to find this out, e.g. see https://clang.godbolt.org/z/aM18Tr7Mr <https://clang.godbolt.org/z/aM18Tr7Mr> It will be multiple optimizations working together. There are several passes that optimize memory operation, like DSE, GVN, EarlyCSE +MemorySSA. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20220117/4d02d947/attachment.html>