<div dir="ltr">I'm getting a compile eror after this change. It also doesn't seem portable to MSVC<div><br></div><div><div>lib/Transforms/Scalar/LoopStrengthReduce.cpp:3944:86: error: cannot initialize a parameter of type 'long long *' with an rvalue of type 'int64_t *' (aka 'long *')</div><div>    if (__builtin_saddll_overflow(Imms.begin()->first, std::prev(Imms.end())->first, &Res))</div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div>
<br><div class="gmail_quote">On Mon, Feb 12, 2018 at 1:49 PM, Gerolf Hoflehner via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@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">Author: ghoflehner<br>
Date: Mon Feb 12 13:49:32 2018<br>
New Revision: 324943<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=324943&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=324943&view=rev</a><br>
Log:<br>
[LSR] Avoid UB overflow when examining reuse opportunities<br>
<br>
Added:<br>
    llvm/trunk/test/CodeGen/X86/<wbr>loop-strength-reduce-overflow-<wbr>check.ll<br>
Modified:<br>
    llvm/trunk/lib/Transforms/<wbr>Scalar/LoopStrengthReduce.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/<wbr>Scalar/LoopStrengthReduce.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp?rev=324943&r1=324942&r2=324943&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/lib/<wbr>Transforms/Scalar/<wbr>LoopStrengthReduce.cpp?rev=<wbr>324943&r1=324942&r2=324943&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/Transforms/<wbr>Scalar/LoopStrengthReduce.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/<wbr>Scalar/LoopStrengthReduce.cpp Mon Feb 12 13:49:32 2018<br>
@@ -3939,6 +3939,11 @@ void LSRInstance::<wbr>GenerateCrossUseConsta<br>
     if (Imms.size() == 1)<br>
       continue;<br>
<br>
+    // UB: Check for overflow<br>
+    int64_t Res;<br>
+    if (__builtin_saddll_overflow(<wbr>Imms.begin()->first, std::prev(Imms.end())->first, &Res))<br>
+      continue;<br>
+<br>
     DEBUG(dbgs() << "Generating cross-use offsets for " << *Reg << ':';<br>
           for (const auto &Entry : Imms)<br>
             dbgs() << ' ' << Entry.first;<br>
<br>
Added: llvm/trunk/test/CodeGen/X86/<wbr>loop-strength-reduce-overflow-<wbr>check.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/loop-strength-reduce-overflow-check.ll?rev=324943&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/test/<wbr>CodeGen/X86/loop-strength-<wbr>reduce-overflow-check.ll?rev=<wbr>324943&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/test/CodeGen/X86/<wbr>loop-strength-reduce-overflow-<wbr>check.ll (added)<br>
+++ llvm/trunk/test/CodeGen/X86/<wbr>loop-strength-reduce-overflow-<wbr>check.ll Mon Feb 12 13:49:32 2018<br>
@@ -0,0 +1,26 @@<br>
+; RUN: llc < %s -O1 -mtriple=x86_64-unknown-<wbr>unknown<br>
+<br>
+; LSR has to check for overflow to avoid UB when it examines reuse opportunities<br>
+; Clang built with UBSan would expose the issue in this test case<br>
+<br>
+define void @main() {<br>
+bb:<br>
+  br label %bb1<br>
+<br>
+bb1:                                              ; preds = %bb1, %bb<br>
+  %tmp = phi i64 [ 248268322795906120, %bb ], [ %tmp10, %bb1 ]<br>
+  %tmp2 = sub i64 %tmp, 248268322795906120<br>
+  %tmp3 = getelementptr i8, i8* undef, i64 %tmp2<br>
+  %tmp4 = sub i64 %tmp, 248268322795906120<br>
+  %tmp5 = getelementptr i8, i8* undef, i64 %tmp4<br>
+  %tmp6 = getelementptr i8, i8* %tmp5, i64 -9086989864993762928<br>
+  %tmp7 = load i8, i8* %tmp6, align 1<br>
+  %tmp8 = getelementptr i8, i8* %tmp3, i64 -1931736422337600660<br>
+  store i8 undef, i8* %tmp8<br>
+  %tmp9 = add i64 %tmp, 248268322795906121<br>
+  %tmp10 = add i64 %tmp9, -248268322795906120<br>
+  br i1 undef, label %bb11, label %bb1<br>
+<br>
+bb11:                                             ; preds = %bb1<br>
+  ret void<br>
+}<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>