<div dir="ltr">










<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US">Hi, </p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"><span style="mso-spacerun:yes">  </span>I'm have found that LLVM can infer variable
value, and it may lead to bad situations.</p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"> </p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"><span style="mso-spacerun:yes">  </span>here is a simple function(t.c) :</p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"> </p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  1 </span><span lang="en-US">extern unsigned
NestCount1;</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  2 </span><span lang="en-US">extern unsigned
NestCount2;</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  3 </span><span lang="en-US">unsigned
foo(unsigned a)</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  4 </span><span lang="en-US">{</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  5 </span><span lang="en-US">    if
(NestCount1 > 0) {</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  6 </span><span lang="en-US">   
    NestCount1--;</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  7 </span><span lang="en-US">    }</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  8 </span><span lang="en-US">    if
(NestCount1 == 0 && NestCount2 == 0) {</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  9 </span><span lang="en-US">   
    return a;</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 10 </span><span lang="en-US">    }</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 11 </span><span lang="en-US">   
return 0;</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 12 </span><span lang="en-US">}   </span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:#ebebeb" lang="en-US"> </p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"><span style="mso-spacerun:yes"> </span>and when I run : `clang -O3 -S -emit-llvm t.c
-o t.ll ', I get the following code:</p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"> </p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN">  9 </span><span lang="en-US">define i32
@foo(i32 %a) #0 {</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 10 </span><span lang="en-US">  %1 = load
i32* @NestCount1, align 4, !tbaa !1</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 11 </span><span lang="en-US">  %2 = icmp
eq i32 %1, 0</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 12 </span><span lang="en-US">  br i1 %2,
label %5, label %3</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 13</span><span lang="en-US"> </span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 14 </span><span lang="en-US">;
<label>:3                  
                    ; preds =
%0</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 15 </span><span lang="en-US">  %4 = add
i32 %1, -1</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 16 </span><span lang="en-US">  store i32
%4, i32* @NestCount1, align 4, !tbaa !1</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 17 </span><span lang="en-US">  br label
%5</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 18</span><span lang="en-US"> </span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 19 </span><span lang="en-US">;
<label>:5                  
                    ; preds =
%0, %3</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 20 </span><span lang="en-US">  %6 = phi
i32 [ 0, %0 ], [ %4, %3 ]</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 21 </span><span lang="en-US">  %7 = load
i32* @NestCount2, align 4</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 22 </span><span lang="en-US">  %8 = or
i32 %7, %6</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 23 </span><span lang="en-US">  %9 = icmp
eq i32 %8, 0</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 24 </span><span lang="en-US">  %a. =
select i1 %9, i32 %a, i32 0</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 25 </span><span lang="en-US">  ret i32
%a.</span></p>

<p style="margin:0in;font-family:"Menlo for Powerline";font-size:8.25pt;color:black"><span lang="zh-CN"> 26 </span><span lang="en-US">}</span></p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"> </p>

<p style="margin:0in"><span style="font-family:Calibri;font-size:11.0pt" lang="en-US">what confused me is </span><span style="font-family:"Menlo for Powerline";font-size:8.25pt" lang="en-US">LINE</span><span style="font-family:"Menlo for Powerline";font-size:8.25pt" lang="zh-CN">20</span><span style="font-family:Calibri;font-size:11.0pt" lang="en-US">:<span style="mso-spacerun:yes">   </span></span><span style="font-family:"Menlo for Powerline";font-size:8.25pt" lang="en-US">%6 = phi
i32 [ <font size="2"><b>0</b></font>, %0 ], [ %4, %3 ]</span></p>

<p style="margin:0in" lang="en-US"><span style="font-family:Calibri;font-size:11.0pt">If I'm understanding it right, a </span><span style="font-family:Calibri;font-size:11.0pt"><span style="font-family:"Menlo for Powerline";font-size:8.25pt" lang="en-US">phi</span> will finally replaced by serveral
move instructions. So, there will be an imm move instruction (like </span><span style="font-family:"Menlo for Powerline";font-size:8.25pt">movl %0, %esi </span><span style="font-family:Calibri;font-size:11.0pt">) . However, if we change LINE20
to </span><span style="font-family:"Menlo for Powerline";font-size:8.25pt">%6 =
phi i32 [ <font size="2"><b>%1</b></font>, %0 ], [ %4, %3 ] </span><span style="font-family:Calibri;font-size:11.0pt"><span style="mso-spacerun:yes"> </span>and when the reg
allocator map </span><span style="font-family:"Menlo for Powerline";font-size:8.25pt">%1</span><span style="font-family:Calibri;font-size:11.0pt">, </span><span style="font-family:"Menlo for Powerline";font-size:8.25pt">%4 </span><span style="font-family:Calibri;font-size:11.0pt">to the same register, we can save
an imm move.</span></p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"> </p>

<p style="margin:0in" lang="en-US"><span style="font-family:"Menlo for Powerline";font-size:8.25pt;color:black">%6 = phi i32 [ <font size="2"><b>0</b></font>, %0 ], [ %4, %3 ]</span><span style="font-family:Calibri;font-size:11.0pt"> seems harder to generate, so I
think this should be an optimization taking by LLVM. </span></p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US">And here
are my questions:</p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"><span style="mso-spacerun:yes">  </span>1. which optimization pass infer variable
values ?</p>

<p style="margin:0in" lang="en-US"><span style="font-family:Calibri;font-size:11.0pt"><span style="mso-spacerun:yes">  </span>2. is it any bad to change to </span><span style="font-family:"Menlo for Powerline";font-size:8.25pt">%6 = phi i32 [ <font size="2"><b>%1</b></font>,
%0 ], [ %4, %3 ]</span><span style="font-family:Calibri;font-size:11.0pt">,<span style="mso-spacerun:yes">  </span>(incresing register pressure ?)</span></p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"> </p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US">Any
ideas? Thanks.</p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US"> </p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US">Cheers.</p>

<p style="margin:0in;font-family:Calibri;font-size:11.0pt" lang="en-US">Huang</p>





</div>