<div dir="ltr">Hi Hal,<div><br></div><div>I just want to check I understand correctly the subtlety, in C++ an overflowing signed addition is an undefined behavior (so Clang adds the nsw keyword to the sign add), while a signed cast that overflow is "only" implementation defined (so Clang cant risk to produce poison on trunc). Is that right?</div><div><br></div><div>If trunc produced undef instead of poison, clang could use it, but then it is less useful and/or unwanted?</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 5, 2017 at 10:30 PM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000"><span>
    <p><br>
    </p>
    <div class="m_5162489970081525388m_-3311001276389849401moz-cite-prefix">On 07/05/2017 03:10 PM, Alexandre
      Isoard wrote:<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">Ah, ok. I read it wrong. In *neither* case it is
        UB.
        <div><br>
        </div>
        <div>Hum, can an implementation define it as UB? :-)</div>
      </div>
    </blockquote>
    <br></span>
    Nope :-)<br>
    <br>
    The only case I've thought of where we could add these for C++ would
    be on conversions to (most) enums (because they used signed
    underlying types and the out-of-bounds mapping won't generally be
    one of the allowed values (or maybe we can define this to be the
    case?)).<span class="m_5162489970081525388HOEnZb"><font color="#888888"><br>
    <br>
     -Hal</font></span><div><div class="m_5162489970081525388h5"><br>
    <br>
    <blockquote type="cite">
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On Wed, Jul 5, 2017 at 9:09 PM,
          Alexandre Isoard <span dir="ltr"><<a href="mailto:alexandre.isoard@gmail.com" target="_blank">alexandre.isoard@gmail.com</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
            <div dir="ltr"><br>
              <div class="gmail_extra"><br>
                <div class="gmail_quote"><span>On Wed, Jul 5,
                    2017 at 3:59 PM, Hal Finkel 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>
                    <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><br>
                        On 07/04/2017 01:41 AM, Dr.-Ing. Christoph
                        Cullmann via llvm-dev wrote:<br>
                        <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
                          Hi,<br>
                          <br>
                          <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
                            Hi Alexandre,<br>
                            <br>
                            LLVM currently doesn't have trunc nsw/nuw,
                            no.<br>
                            Which frontend would emit such instructions?
                            Any application in mind?<br>
                            Just asking because if no frontend could
                            emit those, then the motivation to<br>
                            add nsw/nuw support to trunc would be very
                            low I guess.<br>
                          </blockquote>
                          I think the clang frontend could use that to
                          allow better static analysis of integer
                          overflows<br>
                          on the LLVM IR.<br>
                          <br>
                          It might be interesting for static analysis
                          tools that want to know if you truncate
                          something<br>
                          that is too large for the target range but you
                          need the sign to determine, e.g. the C/C++
                          frontend<br>
                          could use that flag for trunc of
                          signed/unsigned integers, then you could e.g.
                          check if<br>
                          <br>
                          (uint8_t)x<br>
                          <br>
                          changes the values if x has stuff out of the
                          0..255 range.<br>
                          <br>
                          e.g. x as int with -100 => trunc nuw to 8
                          => bad<br>
                          <br>
                          (int8_t)x<br>
                          <br>
                             => trunc nsw to 8 => ok<br>
                        </blockquote>
                        <br>
                      </span>
                      I'm not sure that a C/C++ frontend could add this
                      flag. In C++, the conversion for unsigned types is
                      specified and for signed types, it's
                      implementation defined, but in neither case is it
                      UB.<br>
                    </blockquote>
                    <div><br>
                    </div>
                  </span>
                  <div>Hmm, I don't get it.</div>
                  <div><br>
                  </div>
                  <div>Two questions:</div>
                  <div>- if the conversion for unsigned types is
                    specified, how is it undefined behavior?</div>
                  <div>- if the conversion for signed types is UB, then
                    this is a direct application of those flags, isn't
                    it?</div>
                  <div>
                    <div class="m_5162489970081525388m_-3311001276389849401h5">
                      <div><br>
                      </div>
                      <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
                         -Hal
                        <div>
                          <div class="m_5162489970081525388m_-3311001276389849401m_-5682808666156860653h5"><br>
                            <br>
                            <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
                              <br>
                              Greetings<br>
                              Christoph<br>
                              <br>
                              <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
                                Thanks,<br>
                                Nuno<br>
                                <br>
                                -----Original Message-----<br>
                                From: Alexandre Isoard via llvm-dev<br>
                                Sent: Monday, July 3, 2017 8:38 PM<br>
                                To: llvm-dev<br>
                                Subject: [llvm-dev] trunc nsw/nuw?<br>
                                <br>
                                <br>
                                Hello,<br>
                                <br>
                                 From [1], trunc does not seems to have
                                a nsw/nuw attribute.<br>
                                Is it possible to have that? Or do we
                                have that and it is not up-to-date?<br>
                                <br>
                                The definition would be:<br>
                                <br>
                                If the nuw keyword is present, the
                                result value of the trunc is a poison<br>
                                value if the truncated high order bits
                                are non-zero. If the nsw keyword is<br>
                                present, the result value of the trunc
                                is a poison value if the truncated<br>
                                high order bits are not all equal to the
                                non-truncated bit of the highest<br>
                                order.<br>
                                <br>
                                This allow to cancel out:<br>
                                - sext with trunc nsw<br>
                                - zext with trunc nuw<br>
                                <br>
                                And probably to commute with
                                add/sub/mul/lshr/ashr/shl/urem<wbr>/udiv/udiv
                                (with<br>
                                the correct flags).<br>
                                <br>
                                [1]: <a href="http://llvm.org/docs/LangRef.html#trunc-to-instruction" rel="noreferrer" target="_blank">http://llvm.org/docs/LangRef.h<wbr>tml#trunc-to-instruction</a><br>
                                <br>
                                <br>
                                --<br>
                                <br>
                                Alexandre Isoard<br>
                                <br>
                                ______________________________<wbr>_________________<br>
                                LLVM Developers mailing list<br>
                                <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">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>
                              </blockquote>
                            </blockquote>
                            <br>
                            -- <br>
                          </div>
                        </div>
                        Hal Finkel<br>
                        Lead, Compiler Technology and Programming
                        Languages<br>
                        Leadership Computing Facility<br>
                        Argonne National Laboratory
                        <div class="m_5162489970081525388m_-3311001276389849401m_-5682808666156860653HOEnZb">
                          <div class="m_5162489970081525388m_-3311001276389849401m_-5682808666156860653h5"><br>
                            <br>
                            ______________________________<wbr>_________________<br>
                            LLVM Developers mailing list<br>
                            <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">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>
                          </div>
                        </div>
                      </blockquote>
                    </div>
                  </div>
                </div>
                <span class="m_5162489970081525388m_-3311001276389849401HOEnZb"><font color="#888888"><br>
                    <br clear="all">
                    <div><br>
                    </div>
                    -- <br>
                    <div class="m_5162489970081525388m_-3311001276389849401m_-5682808666156860653gmail_signature" data-smartmail="gmail_signature">
                      <div dir="ltr"><b>Alexandre Isoard</b><br>
                      </div>
                    </div>
                  </font></span></div>
            </div>
          </blockquote>
        </div>
        <br>
        <br clear="all">
        <div><br>
        </div>
        -- <br>
        <div class="m_5162489970081525388m_-3311001276389849401gmail_signature" data-smartmail="gmail_signature">
          <div dir="ltr"><b>Alexandre Isoard</b><br>
          </div>
        </div>
      </div>
    </blockquote>
    <br>
    <pre class="m_5162489970081525388m_-3311001276389849401moz-signature" cols="72">-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
  </div></div></div>

</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="m_5162489970081525388gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><b>Alexandre Isoard</b><br></div></div>
</div></div>