<div dir="ltr">Do we have precedence for this kind of change (where sanitizers affect optimizations in arbitrary internal ways - not simply by enabling/disabling certain passes)? If not, does this need some deeper discussion about alternatives (is it important that we be able to produce equivalent code without the sanitizers enabled?)?<br>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Nov 18, 2013 at 7:02 AM, Evgeniy Stepanov <span dir="ltr"><<a href="mailto:eugenis@google.com" target="_blank">eugenis@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Branch folding optimization often leads to confusing MSan reports due to lost debug info.<br>
For example,<br>
1: if (x < 0)<br>
2:   if (y < 0)<br>
3:    do_something();<br>
is transformed into something like<br>
  %0 = and i32 %y, %x<br>
  %1 = icmp slt i32 %0, 0<br>
  br i1 %1, label %if.then2, label %if.end3<br>
where all 3 instructions are associated with line 1.<br>
<br>
This patch disables folding of conditional branches in functions with sanitize_memory attribute.<br>
<br>
<a href="http://llvm-reviews.chandlerc.com/D2214" target="_blank">http://llvm-reviews.chandlerc.com/D2214</a><br>
<br>
Files:<br>
  lib/Transforms/Utils/SimplifyCFG.cpp<br>
  test/Transforms/SimplifyCFG/branch-fold-msan.ll<br>
<br>
Index: lib/Transforms/Utils/SimplifyCFG.cpp<br>
===================================================================<br>
--- lib/Transforms/Utils/SimplifyCFG.cpp<br>
+++ lib/Transforms/Utils/SimplifyCFG.cpp<br>
@@ -1967,6 +1967,13 @@<br>
 bool llvm::FoldBranchToCommonDest(BranchInst *BI) {<br>
   BasicBlock *BB = BI->getParent();<br>
<br>
+  // This optimization results in confusing MemorySanitizer reports. Use of<br>
+  // uninitialized value in this branch instruction is reported with the<br>
+  // predecessor's debug location.<br>
+  if (BB->getParent()->hasFnAttribute(Attribute::SanitizeMemory) &&<br>
+      BI->isConditional())<br>
+    return false;<br>
+<br>
   Instruction *Cond = 0;<br>
   if (BI->isConditional())<br>
     Cond = dyn_cast<Instruction>(BI->getCondition());<br>
Index: test/Transforms/SimplifyCFG/branch-fold-msan.ll<br>
===================================================================<br>
--- test/Transforms/SimplifyCFG/branch-fold-msan.ll<br>
+++ test/Transforms/SimplifyCFG/branch-fold-msan.ll<br>
@@ -0,0 +1,29 @@<br>
+; RUN: opt < %s -simplifycfg -S | FileCheck %s<br>
+<br>
+declare void @callee()<br>
+<br>
+; Test that conditional branches are not folded with sanitize_memory.<br>
+define void @caller(i32 %x, i32 %y) sanitize_memory {<br>
+; CHECK: define void @caller(i32 [[X:%.*]], i32 [[Y:%.*]])<br>
+; CHECK: icmp slt i32 {{.*}}[[X]]<br>
+; CHECK: icmp slt i32 {{.*}}[[Y]]<br>
+; CHECK: ret void<br>
+<br>
+entry:<br>
+  %cmp = icmp slt i32 %x, 0<br>
+  br i1 %cmp, label %if.then, label %if.end3<br>
+<br>
+if.then:                                          ; preds = %entry<br>
+  %cmp1 = icmp slt i32 %y, 0<br>
+  br i1 %cmp1, label %if.then2, label %if.end<br>
+<br>
+if.then2:                                         ; preds = %if.then<br>
+  call void @callee()<br>
+  br label %if.end<br>
+<br>
+if.end:                                           ; preds = %if.then2, %if.then<br>
+  br label %if.end3<br>
+<br>
+if.end3:                                          ; preds = %if.end, %entry<br>
+  ret void<br>
+}<br>
<br>_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div></div>