<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 3/9/17 8:28 AM, Zeson Wu via
      llvm-dev wrote:<br>
    </div>
    <blockquote
cite="mid:CALaRbYHzbNJEPD6J-Zdnt8cJgnCvpTdO5JBfR9NbZhx4fqLrgg@mail.gmail.com"
      type="cite">
      <meta http-equiv="Context-Type" content="text/html; charset=UTF-8">
      <div dir="ltr">Hi, All.
        <div><br>
        </div>
        <div>Does anybody know about ConstantExpr in llvm? What's it?</div>
        <div>Since it always appears after llvm optimization such as -O2
          level, what is it supposed to be to codegen? I am wondering it
          represents constant value which can be determined or computed
          at compile-time(actually is link-time) to improve performance.
          Although we do not know the actual constant value util the
          object file is linked.<br>
        </div>
      </div>
    </blockquote>
    <br>
    You're pretty much got it.  A Constant Expression (ConstantExpr) is
    simply a constant value.  Since some constant values depend upon
    architecture-dependent features (e.g., structure layout, pointer
    size, etc.), LLVM provides the ConstantExpr to represent them in a
    (more or less) architecture-independent way.<br>
    <br>
    For example, a GEP with constant indices on an internal global
    variable will always compute the same value; it is a constant. 
    However, we use a GEP ConstantExpr to represent it; the backend code
    generator converts it to the appropriate numerical constant when
    generating native code.<br>
    <br>
    For more information on the ConstantExpr, please see the LLVM
    Language Reference Manual
    (<a class="moz-txt-link-freetext" href="http://llvm.org/docs/LangRef.html#constant-expressions">http://llvm.org/docs/LangRef.html#constant-expressions</a>).<br>
    <br>
    Regards,<br>
    <br>
    John Criswell<br>
    <br>
    <blockquote
cite="mid:CALaRbYHzbNJEPD6J-Zdnt8cJgnCvpTdO5JBfR9NbZhx4fqLrgg@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div><br>
          Here is a my example, but there is still existing code to
          compute value in run-time.<br>
          <br>
          <div>
            <div>cat a.C</div>
            <div>int n=5;</div>
            <div>int main(){</div>
            <div>  long a = (long)&n+7;</div>
            <div>  int b = a;</div>
            <div>  return b;</div>
            <div>}<br>
              <br>
              <div>clang++ a.C  -c -O2 -emit-llvm -S;cat a.ll</div>
              <div>; ModuleID = 'a.C'</div>
              <div>target datalayout =
                "e-m:o-i64:64-f80:128-n8:16:32:64-S128"</div>
              <div>target triple = "x86_64-apple-macosx10.12.0"</div>
              <div><br>
              </div>
              <div>@n = global i32 5, align 4</div>
              <div><br>
              </div>
              <div>; Function Attrs: norecurse nounwind readnone ssp
                uwtable</div>
              <div>define i32 @main() #0 {</div>
              <div>  ret i32 trunc (i64 add (i64 ptrtoint (i32* @n to
                i64), i64 7) to i32)</div>
              <div>}</div>
              <br>
              <div>clang++ a.C  -c -O2;objdump -d a.O</div>
              <div><br>
              </div>
              <div>a.O:<span class="gmail-Apple-tab-span"> </span>file
                format Mach-O 64-bit x86-64</div>
              <div><br>
              </div>
              <div>Disassembly of section __TEXT,__text:</div>
              <div>_main:</div>
              <div>       0:<span class="gmail-Apple-tab-span"> </span>55
                <span class="gmail-Apple-tab-span"> </span>pushq<span
                  class="gmail-Apple-tab-span"> </span>%rbp</div>
              <div>       1:<span class="gmail-Apple-tab-span"> </span>48
                89 e5 <span class="gmail-Apple-tab-span"> </span>movq<span
                  class="gmail-Apple-tab-span"> </span>%rsp, %rbp</div>
              <div>       4:<span class="gmail-Apple-tab-span"> </span>48
                8d 05 00 00 00 00 <span class="gmail-Apple-tab-span"> </span>leaq<span
                  class="gmail-Apple-tab-span"> </span>(%rip), %rax</div>
              <div>       b:<span class="gmail-Apple-tab-span"> </span>83
                c0 07 <span class="gmail-Apple-tab-span"> </span><b>addl<span
                    class="gmail-Apple-tab-span"> </span>$7, %eax</b></div>
              <div>       e:<span class="gmail-Apple-tab-span"> </span>5d
                <span class="gmail-Apple-tab-span"> </span>popq<span
                  class="gmail-Apple-tab-span"> </span>%rbp</div>
              <div>       f:<span class="gmail-Apple-tab-span"> </span>c3
                <span class="gmail-Apple-tab-span"> </span>retq</div>
              <br>
            </div>
          </div>
          <div>I am confused about what is its functionality in llvm?<br>
          </div>
          <div><br>
          </div>
          Thanks.<br>
          ---------<br>
        </div>
        <div><br>
          <div class="gmail_signature">
            <div dir="ltr">Zeson<br>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
    </blockquote>
    <br>
    <p><br>
    </p>
    <pre class="moz-signature" cols="72">-- 
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
<a class="moz-txt-link-freetext" href="http://www.cs.rochester.edu/u/criswell">http://www.cs.rochester.edu/u/criswell</a></pre>
  </body>
</html>