<div dir="ltr">Hi Philip, <div><br></div><div>The ubsan bootstrap bot crashes in the new code: </div><div><pre style="font-family:"Courier New",courier,monotype,monospace;color:rgb(0,0,0);font-size:medium"><span class="gmail-stdout">FAILED: lib/Support/CMakeFiles/LLVMSupport.dir/Signals.cpp.o 
/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang++   -DGTEST_HAS_RTTI=0 -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Ilib/Support -I/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Support -I/usr/include/libxml2 -Iinclude -I/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/include -fsanitize=undefined -w -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wcovered-switch-default -Wno-class-memaccess -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -fno-omit-frame-pointer -gline-tables-only -fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all -fsanitize-blacklist=/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/utils/sanitizers/ubsan_blacklist.txt -fdiagnostics-color -ffunction-sections -fdata-sections -O3    -UNDEBUG -std=c++14  -fno-exceptions -fno-rtti -MD -MT lib/Support/CMakeFiles/LLVMSupport.dir/Signals.cpp.o -MF lib/Support/CMakeFiles/LLVMSupport.dir/Signals.cpp.o.d -o lib/Support/CMakeFiles/LLVMSupport.dir/Signals.cpp.o -c /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Support/Signals.cpp
clang-10: /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/include/llvm/Support/Casting.h:264: typename llvm::cast_retty<X, Y*>::ret_type llvm::cast(Y*) [with X = llvm::Instruction; Y = llvm::Value; typename llvm::cast_retty<X, Y*>::ret_type = llvm::Instruction*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
Stack dump:
0.      Program arguments: /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -disable-free -main-file-name Signals.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debug-info-kind=line-tables-only -dwarf-version=4 -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/lib/clang/10.0.0 -dependency-file lib/Support/CMakeFiles/LLVMSupport.dir/Signals.cpp.o.d -MT lib/Support/CMakeFiles/LLVMSupport.dir/Signals.cpp.o -sys-header-deps -D GTEST_HAS_RTTI=0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Support -I /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Suppor</span><span class="gmail-stdout">t -I /usr/include/libxml2 -I include -I /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wno-long-long -Wimplicit-fallthrough -Wcovered-switch-default -Wno-class-memaccess -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -pedantic -w -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fsanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,integer-divide-by-zero,nonnull-attribute,null,object-size,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound -fsanitize-blacklist=/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/utils/sanitizers/ubsan_blacklist.txt -fno-rtti -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -faddrsig -o lib/Support/CMakeFiles/LLVMSupport.dir/Signals.cpp.o -x c++ /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Support/Signals.cpp 
</span><span class="gmail-stdout">1.      <eof> parser at end of file
2.      Per-module optimization passes
3.      Running pass 'CallGraph Pass Manager' on module '/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Support/Signals.cpp'.
4.      Running pass 'Simplify the CFG' on function '@_ZL16RegisterHandlersv'
 #0 0x0000555b7ea0366a llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x2cfc66a)
 #1 0x0000555b7ea013a5 llvm::sys::RunSignalHandlers() (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x2cfa3a5)
 #2 0x0000555b7ea014c7 SignalHandler(int) (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x2cfa4c7)
 #3 0x00007f62544fa0e0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x110e0)
 #4 0x00007f62532b9fff raise (/lib/x86_64-linux-gnu/libc.so.6+0x32fff)
 #5 0x00007f62532bb42a abort (/lib/x86_64-linux-gnu/libc.so.6+0x3442a)
 #6 0x00007f62532b2e67 (/lib/x86_64-linux-gnu/libc.so.6+0x2be67)
 #7 0x00007f62532b2f12 (/lib/x86_64-linux-gnu/libc.so.6+0x2bf12)
 #8 0x0000555b7dda0fd0 llvm::parseWidenableBranch(llvm::User*, llvm::Use*&, llvm::Use*&, llvm::BasicBlock*&, llvm::BasicBlock*&) (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x2099fd0)
 #9 0x0000555b7dda12a0 llvm::parseWidenableBranch(llvm::User const*, llvm::Value*&, llvm::Value*&, llvm::BasicBlock*&, llvm::BasicBlock*&) (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x209a2a0)
#10 0x0000555b7eafb640 SimplifyCondBranchToCondBranch(llvm::BranchInst*, llvm::BranchInst*, llvm::DataLayout const&, llvm::TargetTransformInfo const&) (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x2df4640)
#11 0x0000555b7eb09676 llvm::simplifyCFG(llvm::BasicBlock*, llvm::TargetTransformInfo const&, llvm::SimplifyCFGOptions const&, llvm::SmallPtrSetImpl<llvm::BasicBlock*>*) (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x2e02676)
#12 0x0000555b7e946457 iterativelySimplifyCFG(llvm::Function&, llvm::TargetTransformInfo const&, llvm::SimplifyCFGOptions const&) (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x2c3f457)
#13 0x0000555b7e947181 (anonymous namespace)::CFGSimplifyPass::runOnFunction(llvm::Function&) (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build0/bin/clang-10+0x2c40181)</span></pre></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 21, 2019 at 3:07 PM Philip Reames via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Author: Philip Reames<br>
Date: 2019-11-21T15:07:30-08:00<br>
New Revision: 8293f7434577e23a07284686f5b54079e22e6a91<br>
<br>
URL: <a href="https://github.com/llvm/llvm-project/commit/8293f7434577e23a07284686f5b54079e22e6a91" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/8293f7434577e23a07284686f5b54079e22e6a91</a><br>
DIFF: <a href="https://github.com/llvm/llvm-project/commit/8293f7434577e23a07284686f5b54079e22e6a91.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/8293f7434577e23a07284686f5b54079e22e6a91.diff</a><br>
<br>
LOG: Further cleanup manipulation of widenable branches [NFC]<br>
<br>
This is a follow on to aaea24802bf5.  In post commit discussion, Artur and I realized we could cleanup the code using Uses; this patch does so.<br>
<br>
Added: <br>
<br>
<br>
Modified: <br>
    llvm/include/llvm/Analysis/GuardUtils.h<br>
    llvm/lib/Analysis/GuardUtils.cpp<br>
    llvm/lib/Transforms/Utils/GuardUtils.cpp<br>
<br>
Removed: <br>
<br>
<br>
<br>
################################################################################<br>
diff  --git a/llvm/include/llvm/Analysis/GuardUtils.h b/llvm/include/llvm/Analysis/GuardUtils.h<br>
index f4492c72ce63..b83211535ec2 100644<br>
--- a/llvm/include/llvm/Analysis/GuardUtils.h<br>
+++ b/llvm/include/llvm/Analysis/GuardUtils.h<br>
@@ -15,6 +15,7 @@<br>
 namespace llvm {<br>
<br>
 class BasicBlock;<br>
+class Use;<br>
 class User;<br>
 class Value;<br>
<br>
@@ -43,6 +44,11 @@ bool parseWidenableBranch(const User *U, Value *&Condition,<br>
                           Value *&WidenableCondition, BasicBlock *&IfTrueBB,<br>
                           BasicBlock *&IfFalseBB);<br>
<br>
+/// Analgous to the above, but return the Uses so that that they can be<br>
+/// modified. Unlike previous version, Condition is optional and may be null.<br>
+bool parseWidenableBranch(User *U, Use *&Cond, Use *&WC, BasicBlock *&IfTrueBB,<br>
+                          BasicBlock *&IfFalseBB);<br>
+  <br>
 } // llvm<br>
<br>
 #endif // LLVM_ANALYSIS_GUARDUTILS_H<br>
<br>
diff  --git a/llvm/lib/Analysis/GuardUtils.cpp b/llvm/lib/Analysis/GuardUtils.cpp<br>
index d4a86e18997e..18141069f6a4 100644<br>
--- a/llvm/lib/Analysis/GuardUtils.cpp<br>
+++ b/llvm/lib/Analysis/GuardUtils.cpp<br>
@@ -44,11 +44,35 @@ bool llvm::isGuardAsWidenableBranch(const User *U) {<br>
 bool llvm::parseWidenableBranch(const User *U, Value *&Condition,<br>
                                 Value *&WidenableCondition,<br>
                                 BasicBlock *&IfTrueBB, BasicBlock *&IfFalseBB) {<br>
-  if (match(U, m_Br(m_Intrinsic<Intrinsic::experimental_widenable_condition>(),<br>
-                    IfTrueBB, IfFalseBB)) &&<br>
-      cast<BranchInst>(U)->getCondition()->hasOneUse()) {<br>
-    WidenableCondition = cast<BranchInst>(U)->getCondition();<br>
-    Condition = ConstantInt::getTrue(IfTrueBB->getContext());<br>
+<br>
+  Use *C, *WC;<br>
+  if (parseWidenableBranch(const_cast<User*>(U), C, WC, IfTrueBB, IfFalseBB)) {<br>
+    if (C) <br>
+      Condition = C->get();<br>
+    else<br>
+      Condition = ConstantInt::getTrue(IfTrueBB->getContext());<br>
+    WidenableCondition = WC->get();<br>
+    return true;<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+bool llvm::parseWidenableBranch(User *U, Use *&C,Use *&WC,<br>
+                                BasicBlock *&IfTrueBB, BasicBlock *&IfFalseBB) {<br>
+<br>
+  auto *BI = dyn_cast<BranchInst>(U);<br>
+  if (!BI || !BI->isConditional())<br>
+    return false;<br>
+  auto *Cond = BI->getCondition();<br>
+  if (!Cond->hasOneUse())<br>
+    return false;<br>
+  <br>
+  IfTrueBB = BI->getSuccessor(0);<br>
+  IfFalseBB = BI->getSuccessor(1);<br>
+  <br>
+  if (match(Cond, m_Intrinsic<Intrinsic::experimental_widenable_condition>())) {<br>
+    WC = &BI->getOperandUse(0);<br>
+    C = nullptr;<br>
     return true;<br>
   }<br>
<br>
@@ -57,19 +81,23 @@ bool llvm::parseWidenableBranch(const User *U, Value *&Condition,<br>
   // 2) br (i1 (and WC(), B)), label %IfTrue, label %IfFalse<br>
   // We do not check for more generalized and trees as we should canonicalize<br>
   // to the form above in instcombine. (TODO)<br>
-  if (!match(U, m_Br(m_And(m_Value(Condition), m_Value(WidenableCondition)),<br>
-                     IfTrueBB, IfFalseBB)))<br>
+  Value *A, *B;<br>
+  if (!match(Cond, m_And(m_Value(A), m_Value(B))))<br>
     return false;<br>
-  if (!match(WidenableCondition,<br>
-             m_Intrinsic<Intrinsic::experimental_widenable_condition>())) {<br>
-    if (!match(Condition,<br>
-               m_Intrinsic<Intrinsic::experimental_widenable_condition>()))<br>
-      return false;<br>
-    std::swap(Condition, WidenableCondition);<br>
+  auto *And = cast<Instruction>(Cond);<br>
+  <br>
+  if (match(A, m_Intrinsic<Intrinsic::experimental_widenable_condition>()) &&<br>
+      A->hasOneUse()) {<br>
+    WC = &And->getOperandUse(0);<br>
+    C = &And->getOperandUse(1);<br>
+    return true;<br>
   }<br>
-    <br>
-  // For the branch to be (easily) widenable, it must not correlate with other<br>
-  // branches.  Thus, the widenable condition must have a single use.<br>
-  return (WidenableCondition->hasOneUse() &&<br>
-          cast<BranchInst>(U)->getCondition()->hasOneUse());<br>
+<br>
+  if (match(B, m_Intrinsic<Intrinsic::experimental_widenable_condition>()) &&<br>
+      B->hasOneUse()) {<br>
+    WC = &And->getOperandUse(1);<br>
+    C = &And->getOperandUse(0);<br>
+    return true;<br>
+  }<br>
+  return false;<br>
 }<br>
<br>
diff  --git a/llvm/lib/Transforms/Utils/GuardUtils.cpp b/llvm/lib/Transforms/Utils/GuardUtils.cpp<br>
index 241bfbf80bdf..4cfc9358499a 100644<br>
--- a/llvm/lib/Transforms/Utils/GuardUtils.cpp<br>
+++ b/llvm/lib/Transforms/Utils/GuardUtils.cpp<br>
@@ -87,23 +87,20 @@ void llvm::widenWidenableBranch(BranchInst *WidenableBR, Value *NewCond) {<br>
   // condition, but that doesn't match the pattern parseWidenableBranch expects<br>
   // so we have to be more sophisticated.<br>
<br>
-  if (match(WidenableBR->getCondition(),<br>
-            m_Intrinsic<Intrinsic::experimental_widenable_condition>())) {<br>
+  Use *C, *WC;<br>
+  BasicBlock *IfTrueBB, *IfFalseBB;<br>
+  parseWidenableBranch(WidenableBR, C, WC, IfTrueBB, IfFalseBB);<br>
+  if (!C) {<br>
+    // br (wc()), ... form<br>
     IRBuilder<> B(WidenableBR);<br>
-    WidenableBR->setCondition(B.CreateAnd(NewCond,<br>
-                                          WidenableBR->getCondition()));<br>
+    WidenableBR->setCondition(B.CreateAnd(NewCond, WC->get()));<br>
   } else {<br>
+    // br (wc & C), ... form<br>
+    IRBuilder<> B(WidenableBR);<br>
+    C->set(B.CreateAnd(NewCond, C->get()));<br>
     Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());<br>
     // Condition is only guaranteed to dominate branch<br>
-    WCAnd->moveBefore(WidenableBR);<br>
-    IRBuilder<> B(WCAnd);<br>
-    const bool Op0IsWC =<br>
-      match(WCAnd->getOperand(0),<br>
-            m_Intrinsic<Intrinsic::experimental_widenable_condition>());<br>
-    const unsigned CondOpIdx = Op0IsWC ? 1 : 0;<br>
-    Value *OldCond = WCAnd->getOperand(CondOpIdx);<br>
-    NewCond = B.CreateAnd(NewCond, OldCond);<br>
-    WCAnd->setOperand(CondOpIdx, NewCond);<br>
+    WCAnd->moveBefore(WidenableBR);    <br>
   }<br>
   assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");<br>
 }<br>
@@ -111,20 +108,19 @@ void llvm::widenWidenableBranch(BranchInst *WidenableBR, Value *NewCond) {<br>
 void llvm::setWidenableBranchCond(BranchInst *WidenableBR, Value *NewCond) {<br>
   assert(isWidenableBranch(WidenableBR) && "precondition");<br>
<br>
-  if (match(WidenableBR->getCondition(),<br>
-            m_Intrinsic<Intrinsic::experimental_widenable_condition>())) {<br>
+  Use *C, *WC;<br>
+  BasicBlock *IfTrueBB, *IfFalseBB;<br>
+  parseWidenableBranch(WidenableBR, C, WC, IfTrueBB, IfFalseBB);<br>
+  if (!C) {<br>
+    // br (wc()), ... form<br>
     IRBuilder<> B(WidenableBR);<br>
-    WidenableBR->setCondition(B.CreateAnd(NewCond,<br>
-                                          WidenableBR->getCondition()));<br>
+    WidenableBR->setCondition(B.CreateAnd(NewCond, WC->get()));<br>
   } else {<br>
+    // br (wc & C), ... form<br>
     Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());<br>
     // Condition is only guaranteed to dominate branch<br>
     WCAnd->moveBefore(WidenableBR);<br>
-    const bool Op0IsWC =<br>
-      match(WCAnd->getOperand(0),<br>
-            m_Intrinsic<Intrinsic::experimental_widenable_condition>());<br>
-    const unsigned CondOpIdx = Op0IsWC ? 1 : 0;<br>
-    WCAnd->setOperand(CondOpIdx, NewCond);<br>
+    C->set(NewCond);<br>
   }<br>
   assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");<br>
 }<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>