<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Sorry, the old title didn't make sense. </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Hi, <div><br>I have doubts on the following transformation in InstCombineAddSub.cpp. Is it always safe to preserve NSW/NUW in this case? </div><div><br></div><div><pre style="margin-top:0px;margin-bottom:0px;color:rgb(0,0,0);padding-top:0.5em;font-size:medium;line-height:16.25px">
<span style="display:block">  <span style="color:rgb(136,0,0)">// If this is a 'B = x-(-A)', change to B = x+A.  This preserves NSW/NUW.</span>
</span><span style="display:block">  <span style="color:rgb(0,0,136)">if</span> (<a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/include/llvm/IR/Value.h&ct=xref_jump_to_def&cl=GROK&l=69&gsn=Value" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">Value</a> <a href="https://cs.corp.google.com/#piper///depot/google3/GENERATED/figments/cpp/PointerTo/start-with-ll/llvm/class-Value.cc&ct=xref_jump_to_def&cl=GROK&l=3&gsn=*" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">*</a><a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_usages&gs=cpp:llvm::class-InstCombiner::visitSub(llvm::BinaryOperator%2520&)::V@google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:48204%257Cdef&l=1470&gsn=V" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">V</a> = <a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp&ct=xref_jump_to_def&cl=GROK&l=611&gsn=dyn_castNegVal" style="text-decoration:none;color:rgb(85,26,139);background-color:rgb(255,255,170)" target="_blank">dyn_castNegVal</a>(<a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_jump_to_def&cl=GROK&l=1456&gsn=Op1" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">Op1</a>)) {
</span><span style="display:block">    <a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/include/llvm/IR/InstrTypes.h&ct=xref_jump_to_def&cl=GROK&l=138&gsn=BinaryOperator" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">BinaryOperator</a> <a href="https://cs.corp.google.com/#piper///depot/google3/GENERATED/figments/cpp/PointerTo/start-with-ll/llvm/class-BinaryOperator.cc&ct=xref_jump_to_def&cl=GROK&l=3&gsn=*" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">*</a><a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_usages&gs=cpp:llvm::class-InstCombiner::visitSub(llvm::BinaryOperator%2520&)::Res@google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:48251%257Cdef&l=1471&gsn=Res" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">Res</a> = <a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/include/llvm/IR/InstrTypes.h&ct=xref_jump_to_def&cl=GROK&l=138&gsn=BinaryOperator" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">BinaryOperator</a>::CreateAdd(<a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_jump_to_def&cl=GROK&l=1456&gsn=Op0" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">Op0</a>, <a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_jump_to_def&cl=GROK&l=1470&gsn=V" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">V</a>);
</span><span style="display:block">    <a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_jump_to_def&cl=GROK&l=1471&gsn=Res" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">Res</a>-><a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/IR/Instructions.cpp&ct=xref_jump_to_def&cl=GROK&l=2013&gsn=setHasNoSignedWrap" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">setHasNoSignedWrap</a>(<a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_jump_to_def&cl=GROK&l=1455&gsn=I" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">I</a>.<a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/IR/Instructions.cpp&ct=xref_jump_to_def&cl=GROK&l=2025&gsn=hasNoSignedWrap" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">hasNoSignedWrap</a>());
</span><span style="display:block">    <a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_jump_to_def&cl=GROK&l=1471&gsn=Res" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">Res</a>-><a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/IR/Instructions.cpp&ct=xref_jump_to_def&cl=GROK&l=2009&gsn=setHasNoUnsignedWrap" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">setHasNoUnsignedWrap</a>(<a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_jump_to_def&cl=GROK&l=1455&gsn=I" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">I</a>.<a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/IR/Instructions.cpp&ct=xref_jump_to_def&cl=GROK&l=2021&gsn=hasNoUnsignedWrap" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">hasNoUnsignedWrap</a>());
</span><span style="display:block">    <span style="color:rgb(0,0,136)">return</span> <a href="https://cs.corp.google.com/#piper///depot/google3/third_party/llvm/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp&ct=xref_jump_to_def&cl=GROK&l=1471&gsn=Res" style="text-decoration:none;color:rgb(85,26,139)" target="_blank">Res</a>;
</span><span style="display:block">  }</span></pre><br>Suppose A = x = (i4 -8) (the minimum signed integer of i4). The first minus in x - (-A) does not sign-overflow, but x + A sign-overflows. </div>
<div><br></div><div>Thanks, </div><span class="HOEnZb"><font color="#888888"><div>Jingyue</div></font></span></div>
</blockquote></div><br></div></div>