[LLVMdev] simple optimization question

Krzysztof Parzyszek kparzysz at codeaurora.org
Fri Jan 18 06:27:06 PST 2013


On 1/18/2013 6:19 AM, Duncan Sands wrote:
> Hi Bjorn,
>
>> currently the following two lines of code with integer variables get
>> compiled completely differently:
>>
>> if (d0<tmp) d0=tmp;
>
> there must be something else going on because this
>
> void bar(int);
> void foo(int d0, int tmp) {
>    if (d0 < tmp) d0=tmp;
>    bar(d0);
> }
>
> when compiled like this
>
> clang -S -O4 -o - sel.c
>
> gives this
>
> define void @foo(i32 %d0, i32 %tmp) nounwind uwtable {
> entry:
>    %cmp = icmp slt i32 %d0, %tmp
>    %tmp.d0 = select i1 %cmp, i32 %tmp, i32 %d0
>    tail call void @bar(i32 %tmp.d0) nounwind
>    ret void
> }


Last time I looked at this code, it only handled blocks with a single 
value in it, e.g.

if.cond:
   ... %oldvalue ...
   br i1 %cond, %if.then, %skip
if.then:
   %value = something       <-- single instruction in this block
   br %skip
skip:
   %newvalue = phi [ %oldvalue, %if.cond ], [ %newvalue, %if.then ]

The simplification was happening in SimplifyCFG, and was driven by the 
existence of a phi node in the join block (in "skip" in this case).  If 
there were more instructions in "if.then", the code would not handle it. 
  I have actually extended it to handle multiple phis a while back.  I 
guess I should upstream it if nobody else has done it yet...

-Krzysztof


-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, 
hosted by The Linux Foundation



More information about the llvm-dev mailing list