[LLVMdev] help with pointer-to-array conversion

Naftali Schwartz nschwart at cs.nyu.edu
Fri Jul 29 02:02:42 PDT 2005


OK, thanks Chris, I've found that running

 	opt -loopsimplify -instcombine -indvars -stats

gives me the setup I need for this transformation, and a small patch 
makes it happen in the simple case we discussed.  However, I'm having 
some trouble when things get a bit more complicated with 3 nesting levels:

int A[3000000], B[20000], C[100], Z;
int main()
{
         int i, j, k, *a, *b, *c;
         for ( a = &A[0], i = 0; i != 300; i++ )
                 for ( b = &B[0], j = 0; j != 200; j++ )
                         for ( c = &C[0], k = 0; k != 100; k++ )
                                 Z += *a++ * *b++ * *c++;
}

My main problem is maintaining the loop nesting structure, which some pass 
in llvm seems to want to collapse, and then scalar evolution can't find 
all the predictable loop counts.  However, when I insert a volatile memory 
operation inside of each loop, this preserves the loop structure and 
scalar evolution finds all the loop counts.  What to do?

Thanks,
Naftali

On Thu, 28 Jul 2005, Chris Lattner wrote:

> On Thu, 28 Jul 2005, Naftali Schwartz wrote:
>> I now understand that IndVarSimplify.cpp is capable of reproducing array 
>> references when the pointer initialization from the array address is found 
>> inside the immediately enclosing loop, such that in the following code:
>
> Ok.
>
>> int A[20000], B[100], Z;
>> int main()
>> {
>>        int i, j, *a, *b;
>>        for ( a = &A[0], i = 0; i != 200; i++ )
>>                for ( b = &B[0], j = 0; j != 100; j++ )
>>                        Z += *a++ * *b++;
>> }
>> 
>> the pointer b can be transformed into an offset from B[], but a cannot be 
>> transformed into an offset from A[] because the initialization is two 
>> levels up.
>
> Hrm, that should work.
>
>> I started poking around in IndVarSimplify.cpp and I have a patch which 
>> should (at least partially) enable the transformation for a as well, but I 
>> find that although the code looks OK in debug printouts, the gccas output 
>> is unchanged.
>
> Ok
>




More information about the llvm-dev mailing list