<div dir="ltr">On Thu, Apr 12, 2018 at 2:09 PM, Bill Wendling via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello again!<div><br></div><div>I took a stab at PR4898[1]. The attached patch improves Clang's <font face="monospace">__builtin_constant_p</font> support so that the Linux kernel is happy. With this improvement, Clang can determine if <font face="monospace">__builtin_constant_p</font> is true or false after inlining.</div><div><br></div><div>As an example:</div><div><br></div><div><div><font face="monospace">static __attribute__((always_inline)) int foo(int x) {</font></div><div><font face="monospace"><span style="white-space:pre-wrap">       </span>if (__builtin_constant_p(x))</font></div><div><font face="monospace"><span style="white-space:pre-wrap">         </span>return 1;</font></div><div><font face="monospace"><span style="white-space:pre-wrap">    </span>return 0;</font></div><div><font face="monospace">}</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">static __attribute__((always_inline)) int mux() {</font></div><div><font face="monospace"><span style="white-space:pre-wrap">       </span>if (__builtin_constant_p(37))</font></div><div><font face="monospace"><span style="white-space:pre-wrap">                </span>return 927;</font></div><div><font face="monospace"><span style="white-space:pre-wrap">  </span>return 0;</font></div><div><font face="monospace">}</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">int bar(int a) {</font></div><div><font face="monospace"><span style="white-space:pre-wrap">        </span>if (a)</font></div><div><font face="monospace"><span style="white-space:pre-wrap">               </span>return foo(42);</font></div><div><font face="monospace"><span style="white-space:pre-wrap">      </span>else</font></div><div><font face="monospace"><span style="white-space:pre-wrap">         </span>return mux();</font></div><div><font face="monospace">}</font></div></div><div><br></div><div>Now outputs this code at -O1:</div><div><br></div><div><font face="monospace">bar:</font></div><div><div><font face="monospace"><span style="white-space:pre-wrap">        </span>.cfi_startproc</font></div><div><font face="monospace"># %bb.0:                                # %entry</font></div><div><font face="monospace"><span style="white-space:pre-wrap">    </span>testl<span style="white-space:pre-wrap">   </span>%edi, %edi</font></div><div><font face="monospace"><span style="white-space:pre-wrap">   </span>movl<span style="white-space:pre-wrap">    </span>$927, %ecx              # imm = 0x39F</font></div><div><font face="monospace"><span style="white-space:pre-wrap"> </span>movl<span style="white-space:pre-wrap">    </span>$1, %eax</font></div><div><font face="monospace"><span style="white-space:pre-wrap">     </span>cmovel<span style="white-space:pre-wrap">  </span>%ecx, %eax</font></div><div><font face="monospace"><span style="white-space:pre-wrap">   </span>retq</font></div></div><div><br></div><div>And this code at -O0:</div><div><br></div><div><div><font face="monospace">bar:                                    # @bar</font></div><div><font face="monospace"><span style="white-space:pre-wrap"> </span>.cfi_startproc</font></div><div><font face="monospace"># %bb.0:                                # %entry</font></div><div><font face="monospace"><span style="white-space:pre-wrap">    </span>pushq<span style="white-space:pre-wrap">   </span>%rbp</font></div><div><font face="monospace"><span style="white-space:pre-wrap"> </span>.cfi_def_cfa_offset 16</font></div><div><font face="monospace"><span style="white-space:pre-wrap">       </span>.cfi_offset %rbp, -16</font></div><div><font face="monospace"><span style="white-space:pre-wrap">        </span>movq<span style="white-space:pre-wrap">    </span>%rsp, %rbp</font></div><div><font face="monospace"><span style="white-space:pre-wrap">   </span>.cfi_def_cfa_register %rbp</font></div><div><font face="monospace"><span style="white-space:pre-wrap">   </span>movl<span style="white-space:pre-wrap">    </span>%edi, -16(%rbp)</font></div><div><font face="monospace"><span style="white-space:pre-wrap">      </span>cmpl<span style="white-space:pre-wrap">    </span>$0, -16(%rbp)</font></div><div><font face="monospace"><span style="white-space:pre-wrap">        </span>je<span style="white-space:pre-wrap">      </span>.LBB0_2</font></div><div><font face="monospace"># %bb.1:                                # %if.then</font></div><div><font face="monospace"><span style="white-space:pre-wrap"> </span>movl<span style="white-space:pre-wrap">    </span>$42, -8(%rbp)</font></div><div><font face="monospace"><span style="white-space:pre-wrap">        </span>movl<span style="white-space:pre-wrap">    </span>$0, -4(%rbp)</font></div><div><font face="monospace"><span style="white-space:pre-wrap"> </span>movl<span style="white-space:pre-wrap">    </span>-4(%rbp), %eax</font></div><div><font face="monospace"><span style="white-space:pre-wrap">       </span>movl<span style="white-space:pre-wrap">    </span>%eax, -12(%rbp)</font></div><div><font face="monospace"><span style="white-space:pre-wrap">      </span>jmp<span style="white-space:pre-wrap">     </span>.LBB0_3</font></div><div><font face="monospace">.LBB0_2:                                # %if.else</font></div><div><font face="monospace"><span style="white-space:pre-wrap"> </span>movl<span style="white-space:pre-wrap">    </span>$927, -12(%rbp)         # imm = 0x39F</font></div><div><font face="monospace">.LBB0_3:                                # %return</font></div><div><font face="monospace"><span style="white-space:pre-wrap">       </span>movl<span style="white-space:pre-wrap">    </span>-12(%rbp), %eax</font></div><div><font face="monospace"><span style="white-space:pre-wrap">      </span>popq<span style="white-space:pre-wrap">    </span>%rbp</font></div><div><font face="monospace"><span style="white-space:pre-wrap"> </span>retq</font></div></div><div><br></div><div>If the patch looks okay to people, I can shove it onto Phabricator for a review. (My phab-fu is bad.)</div><div><br></div><div>Thoughts?</div></div></blockquote><div><br></div><div>The patch seems reasonable.  Could you please also add docs on the intrinsic itself?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>-bw</div><div><br></div><div>[1] <a href="https://bugs.llvm.org/show_bug.cgi?id=4898" target="_blank">https://bugs.llvm.org/<wbr>show_bug.cgi?id=4898</a></div></div>
<br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Saleem Abdulrasool<br>compnerd (at) compnerd (dot) org</div>
</div></div>