[llvm-dev] Particular type of loop optimization

Mehdi Amini via llvm-dev llvm-dev at lists.llvm.org
Wed Feb 3 13:17:41 PST 2016


Hi,


> On Feb 3, 2016, at 4:05 AM, Gleison Souza <gleison14051994 at gmail.com> wrote:
> 
> Thanks Mehdi,
> 
> I tried to use this, but some debug information can be lost in these optimizations.
> I need write in the source file to insert information before the loops, and in
> some cases, I'm writing after the loop header.

I’m not sure what you mean.


> 
> Please, take a look:
> 
> int foo1 (int *a, int *b, int n) {                                               
>   int i, s= 0;                                                                   
>   for (i = 0; i < n; i++) {                                                      
>     s = s * a[i];                                                                
>   }                                                                              
>                                                                                
>   for (i = 0; i < n; i++) {                                                      
>     b[i] = a[i] + 3;                                                             
>     s += a[i];                                                                   
>    }                                                                              
>   return s;                                                                      
> } 
> 

What is the problem with the code generated on this example? 
You were asking a specific question related to global variable before, now there is none.



> In this case, using the line obtained by this one in the LLVM's IR:
> 
> Line = l->getStartLoc().getLine(); 
> 
> The source file is cloned, and I'm writing my annotations inside the loop now.
> I can't do several modifications in the struct of code, just the necessary, thats the problem.

This is all getting even more confusing to me…

— 
Mehdi



> 
> Regards,
> 
>     Gleison
> 
> 2016-02-03 1:07 GMT-02:00 Mehdi Amini <mehdi.amini at apple.com <mailto:mehdi.amini at apple.com>>:
> 
>> On Feb 2, 2016, at 8:35 AM, Gleison Souza via llvm-dev <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote:
>> 
>> Dear LLVMers,
>> 
>>     I am trying to implement a particular type of loop optimization, but I am having problems with global variables. To solve this problem, I would like to know if LLVM has some pass that moves loads outside loops. I will illustrate with an example. I want to transform this code below. I am writing in C for readability, but I am analysing LLVM IR:
>> 
>> int *vectorE;
>> 
>> void foo (int n) {       
>>   int i;
>>   for (i = 0; i < n; i++)
>>     vectorE[i] = i;
>> }
>> 
>> into this one:
>> 
>> int *vectorE;
>> 
>> void foo (int n) {       
>>   int i;
>>   int* aux = vectorE;
>>   for (i = 0; i < n; i++)
>>     aux[i] = i;
>> }
> 
> 
> Have you looked at the output of clang with optimization enabled (even O1)? For this C++ code the optimizer moves the access to the global in the loop preheader, and then the loop itself does not access the global at all, which seems to be what you’re looking for.
> 
> Try: clang -O1 -S -o - -mllvm -print-after-all
> 
>> Mehdi
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160203/aceac498/attachment.html>


More information about the llvm-dev mailing list