Hi Chris,<br><br>Thanks for your reply.<br><br>You said that scalarRepl gets shy about loads and stores of the entire aggregate. Then I use a test case:<br><br>; ModuleID = 'test1.ll'<br>define i32 @fun(i32* nocapture %X, i32 %i) nounwind uwtable readonly {<br>
  %stackArray = alloca <4 x i32><br>  %XC = bitcast i32* %X to <4 x i32>*<br>  %arrayVal = load <4 x i32>* %XC<br>  store <4 x i32> %arrayVal, <4 x i32>* %stackArray<br>  %arrayVal1 = load <4 x i32>* %stackArray<br>
  %1 = extractelement <4 x i32> %arrayVal1, i32 1<br>  ret i32 %1<br>}<br><br>$ opt -S -stats -scalarrepl test1.ll <br>; ModuleID = 'test1.ll'<br><br>define i32 @fun(i32* nocapture %X, i32 %i) nounwind uwtable readonly {<br>
  %XC = bitcast i32* %X to <4 x i32>*<br>  %arrayVal = load <4 x i32>* %XC<br>  %1 = extractelement <4 x i32> %arrayVal, i32 1<br>  ret i32 %1<br>}<br>===-------------------------------------------------------------------------===<br>
                          ... Statistics Collected ...<br>===-------------------------------------------------------------------------===<br><br>1 mem2reg    - Number of alloca's promoted with a single store<br>1 scalarrepl - Number of allocas promoted<br>
<br>You can see that the stackArray is eliminated, although there is loads and stores of the entire aggregate. <br><br>However, the optimised code is still not optimal. I want the code just load one element from X instead of the whole array.<br>
<br>Thanks,<br>David<br><br><br><br><br><br><div class="gmail_quote">On Sun, Mar 11, 2012 at 5:22 AM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@apple.com">clattner@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
On Mar 10, 2012, at 9:34 AM, Fan Dawei wrote:<br>
<br>
> Hi all,<br>
><br>
> I want to use scalarrepl pass to eliminate the allocation of mat_alloc which is of type [4 x <4 x float>] in the following program.<br>
><br>
> $cat test.ll<br>
><br>
> ; ModuleID = 'test.ll'<br>
><br>
> define void @main(<4 x float>* %inArg, <4 x float>* %outArg, [4 x <4 x float>]* %constants) nounwind {<br>
> entry:<br>
>   %inArg1 = load <4 x float>* %inArg<br>
>   %mat_alloc = alloca [4 x <4 x float>]<br>
>   %matVal = load [4 x <4 x float>]* %constants<br>
>   store [4 x <4 x float>] %matVal, [4 x <4 x float>]* %mat_alloc<br>
>   %0 = getelementptr inbounds [4 x <4 x float>]* %mat_alloc, i32 0, i32 0<br>
>   %1 = load <4 x float>* %0<br>
>   %2 = fmul <4 x float> %1, %inArg1<br>
>   %3 = getelementptr inbounds [4 x <4 x float>]* %mat_alloc, i32 0, i32 1<br>
>   %4 = load <4 x float>* %3<br>
>   %5 = fmul <4 x float> %4, %inArg1<br>
>   %6 = fadd <4 x float> %2, %5<br>
>   %7 = getelementptr inbounds [4 x <4 x float>]* %mat_alloc, i32 0, i32 2<br>
>   %8 = load <4 x float>* %7<br>
>   %9 = fmul <4 x float> %8, %inArg1<br>
>   %10 = fadd <4 x float> %6, %9<br>
>   %11 = getelementptr inbounds [4 x <4 x float>]* %mat_alloc, i32 0, i32 3<br>
>   %12 = load <4 x float>* %11<br>
>   %13 = fadd <4 x float> %10, %12<br>
>   %14 = getelementptr <4 x float>* %outArg, i32 1<br>
>   store <4 x float> %13, <4 x float>* %14<br>
>   ret void<br>
> }<br>
><br>
> $ opt -S -stats -scalarrepl test.ll<br>
><br>
> No transformation is performed. I've examined the source code of scalarrepl. It seems this pass does not handle array allocations. Is there other transformation pass I can use to eliminate this allocation?<br>
<br>
</div></div>Hi David,<br>
<br>
ScalarRepl gets shy about loads and stores of the entire aggregate:<br>
<div class="im"><br>
>   %matVal = load [4 x <4 x float>]* %constants<br>
>   store [4 x <4 x float>] %matVal, [4 x <4 x float>]* %mat_alloc<br>
<br>
</div>It is possible to generalize scalarrepl to handle these similar to the way it handles memcpy, but noone has done that yet.  Also, it's not generally recommended to do stuff like this, because you'll get inefficient code from many parts of the optimizer and code generator.<br>

<span class="HOEnZb"><font color="#888888"><br>
-Chris<br>
<br>
</font></span></blockquote></div><br>