<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 08/10/2014 02:47 PM, Jan Vesely
      wrote:<br>
    </div>
    <blockquote cite="mid:1407707255.17340.6.camel@adriatix" type="cite">
      <div class="moz-text-plain" wrap="true" graphical-quote="true"
        style="font-family: -moz-fixed; font-size: 12px;"
        lang="x-unicode">
        <pre wrap="">On Sat, 2014-08-09 at 16:26 -0400, Tom Stellard wrote:
</pre>
        <blockquote type="cite" style="color: #000000;">
          <pre wrap=""><span class="moz-txt-citetags">> </span>On 08/09/2014 03:52 PM, Jan Vesely wrote:
</pre>
          <blockquote type="cite" style="color: #000000;">
            <pre wrap=""><span class="moz-txt-citetags">> > </span>On Fri, 2014-08-08 at 14:18 -0700, Matt Arsenault wrote:
</pre>
            <blockquote type="cite" style="color: #000000;">
              <pre wrap=""><span class="moz-txt-citetags">> >> </span>On Aug 8, 2014, at 1:41 PM, Jan Vesely <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:jan.vesely@rutgers.edu"><jan.vesely@rutgers.edu></a> wrote:
<span class="moz-txt-citetags">> >></span>
</pre>
              <blockquote type="cite" style="color: #000000;">
                <pre wrap=""><span class="moz-txt-citetags">> >>> </span>v2: continue iterating through the rest of the bb
<span class="moz-txt-citetags">> >>> </span>    use for loop
<span class="moz-txt-citetags">> >>></span>
<span class="moz-txt-citetags">> >>> </span>Signed-off-by: Jan Vesely <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:jan.vesely@rutgers.edu"><jan.vesely@rutgers.edu></a>
<span class="moz-txt-citetags">> >>> </span>---
<span class="moz-txt-citetags">> >>> </span>lib/Transforms/Utils/FlattenCFG.cpp | 9 +++++----
<span class="moz-txt-citetags">> >>> </span>1 file changed, 5 insertions(+), 4 deletions(-)
<span class="moz-txt-citetags">> >>></span>
<span class="moz-txt-citetags">> >>> </span>diff --git a/lib/Transforms/Utils/FlattenCFG.cpp b/lib/Transforms/Utils/FlattenCFG.cpp
<span class="moz-txt-citetags">> >>> </span>index 51ead40..4eb3e3d 100644
<span class="moz-txt-citetags">> >>> </span>--- a/lib/Transforms/Utils/FlattenCFG.cpp
<span class="moz-txt-citetags">> >>> </span>+++ b/lib/Transforms/Utils/FlattenCFG.cpp
<span class="moz-txt-citetags">> >>> </span>@@ -238,9 +238,13 @@ bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
<span class="moz-txt-citetags">> >>> </span>     // Do branch inversion.
<span class="moz-txt-citetags">> >>> </span>     BasicBlock *CurrBlock = LastCondBlock;
<span class="moz-txt-citetags">> >>> </span>     bool EverChanged = false;
<span class="moz-txt-citetags">> >>> </span>-    while (1) {
<span class="moz-txt-citetags">> >>> </span>+    for (;CurrBlock != FirstCondBlock;
<span class="moz-txt-citetags">> >>> </span>+          CurrBlock = CurrBlock->getSinglePredecessor()) {
<span class="moz-txt-citetags">> >>> </span>       BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator());
<span class="moz-txt-citetags">> >>> </span>       CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
<span class="moz-txt-citetags">> >>> </span>+      if (!CI)
<span class="moz-txt-citetags">> >>> </span>+        continue;
<span class="moz-txt-citetags">> >>> </span>+
<span class="moz-txt-citetags">> >>> </span>       CmpInst::Predicate Predicate = CI->getPredicate();
<span class="moz-txt-citetags">> >>> </span>       // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
<span class="moz-txt-citetags">> >>> </span>       if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
<span class="moz-txt-citetags">> >>> </span>@@ -248,9 +252,6 @@ bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
<span class="moz-txt-citetags">> >>> </span>         BI->swapSuccessors();
<span class="moz-txt-citetags">> >>> </span>         EverChanged = true;
<span class="moz-txt-citetags">> >>> </span>       }
<span class="moz-txt-citetags">> >>> </span>-      if (CurrBlock == FirstCondBlock)
<span class="moz-txt-citetags">> >>> </span>-        break;
<span class="moz-txt-citetags">> >>> </span>-      CurrBlock = CurrBlock->getSinglePredecessor();
<span class="moz-txt-citetags">> >>> </span>     }
<span class="moz-txt-citetags">> >>> </span>     return EverChanged;
<span class="moz-txt-citetags">> >>> </span>   }
</pre>
              </blockquote>
              <pre wrap=""><span class="moz-txt-citetags">> >></span>
<span class="moz-txt-citetags">> >> </span>Needs tests
</pre>
            </blockquote>
            <pre wrap=""><span class="moz-txt-citetags">> ></span>
<span class="moz-txt-citetags">> > </span>sorry, I misunderstood what you meant by test the first time.
<span class="moz-txt-citetags">> ></span>
<span class="moz-txt-citetags">> > </span>I wanted to add a flattencfg pass specific test but ran into problems
<span class="moz-txt-citetags">> ></span>
<span class="moz-txt-citetags">> > </span>is there an easy way to run only the flattencfg pass?
<span class="moz-txt-citetags">> > </span>opt -pass_name works only for analytical passes,
<span class="moz-txt-citetags">> > </span>and llc ... -stop-after=flattencfg reports pass not registered.
<span class="moz-txt-citetags">> ></span>
</pre>
          </blockquote>
          <pre wrap=""><span class="moz-txt-citetags">> </span>
<span class="moz-txt-citetags">> </span>You should be able to run any pass using opt.  Did you look at the 
<span class="moz-txt-citetags">> </span>output of opt -help-hidden ?
<span class="moz-txt-citetags">> </span>
<span class="moz-txt-citetags">> </span>Actually, flattencfg doens't show up for me in the help options.
<span class="moz-txt-citetags">> </span>This may be because it doesn't get initialized with the other passes in
<span class="moz-txt-citetags">> </span>lib/Transforms/Scalar/Scalar.cpp.  Try adding the initialize call there 
<span class="moz-txt-citetags">> </span>and see if that makes it work with opt.
</pre>
        </blockquote>
        <pre wrap="">this worked, thank you. v3 is attached.

the test is a simplified situation from the mad_sat implementation.
the crash occurs at the end of b0.

jan

</pre>
        <blockquote type="cite" style="color: #000000;">
          <pre wrap=""><span class="moz-txt-citetags">> </span>
<span class="moz-txt-citetags">> </span>-Tom
<span class="moz-txt-citetags">> </span>
<span class="moz-txt-citetags">> </span>
</pre>
          <blockquote type="cite" style="color: #000000;">
            <pre wrap=""><span class="moz-txt-citetags">> > </span>I guess I'll have to add a codegen test.
<span class="moz-txt-citetags">> ></span>
<span class="moz-txt-citetags">> > </span>I also found the parallel{or,and}ifcollapse tests, that are marked XFAIL
<span class="moz-txt-citetags">> > </span>for unrelated reason. Should I try to fix those or add a new test?
<span class="moz-txt-citetags">> ></span>
<span class="moz-txt-citetags">> > </span>jan
<span class="moz-txt-citetags">> ></span>
</pre>
          </blockquote>
          <pre wrap=""><span class="moz-txt-citetags">> </span>
</pre>
        </blockquote>
        <pre wrap=""><div class="moz-txt-sig">-- 
Jan Vesely <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:jan.vesely@rutgers.edu"><jan.vesely@rutgers.edu></a>
</div></pre>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"><legend
          class="mimeAttachmentHeaderName">0001-utils-Fix-segfault-in-flattencfg.patch</legend></fieldset>
      <br>
      <div class="moz-text-plain" wrap="true" graphical-quote="true"
        style="font-family: -moz-fixed; font-size: 12px;"
        lang="x-unicode">
        <pre wrap="">From 509d65e62475b7d34966c38b0177648482a31ce8 Mon Sep 17 00:00:00 2001
From: Jan Vesely <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:jan.vesely@rutgers.edu"><jan.vesely@rutgers.edu></a>
Date: Sat, 9 Aug 2014 16:41:29 -0400
Subject: [PATCH v3 1/1] utils: Fix segfault in flattencfg

v2: continue iterating through the rest of the bb
    use for loop

v3: initialize FlattenCFG pass in ScalarOps
    add test

Signed-off-by: Jan Vesely <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:jan.vesely@rutgers.edu"><jan.vesely@rutgers.edu></a>
---
 lib/Transforms/Scalar/Scalar.cpp    |  1 +
 lib/Transforms/Utils/FlattenCFG.cpp |  9 +++++----
 test/Transforms/Util/flattencfg.ll  | 23 +++++++++++++++++++++++
 3 files changed, 29 insertions(+), 4 deletions(-)
 create mode 100644 test/Transforms/Util/flattencfg.ll

diff --git a/lib/Transforms/Scalar/Scalar.cpp b/lib/Transforms/Scalar/Scalar.cpp
index 12df676..4bebb42 100644
--- a/lib/Transforms/Scalar/Scalar.cpp
+++ b/lib/Transforms/Scalar/Scalar.cpp
@@ -38,6 +38,7 @@ void llvm::initializeScalarOpts(PassRegistry &Registry) {
   initializeDSEPass(Registry);
   initializeGVNPass(Registry);
   initializeEarlyCSEPass(Registry);
+  initializeFlattenCFGPassPass(Registry);
   initializeIndVarSimplifyPass(Registry);
   initializeJumpThreadingPass(Registry);
   initializeLICMPass(Registry);
diff --git a/lib/Transforms/Utils/FlattenCFG.cpp b/lib/Transforms/Utils/FlattenCFG.cpp
index 51ead40..4eb3e3d 100644
--- a/lib/Transforms/Utils/FlattenCFG.cpp
+++ b/lib/Transforms/Utils/FlattenCFG.cpp
@@ -238,9 +238,13 @@ bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
     // Do branch inversion.
     BasicBlock *CurrBlock = LastCondBlock;
     bool EverChanged = false;
-    while (1) {
+    for (;CurrBlock != FirstCondBlock;
+          CurrBlock = CurrBlock->getSinglePredecessor()) {
       BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator());
       CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
+      if (!CI)
+        continue;
+
       CmpInst::Predicate Predicate = CI->getPredicate();
       // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
       if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
@@ -248,9 +252,6 @@ bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
         BI->swapSuccessors();
         EverChanged = true;
       }
-      if (CurrBlock == FirstCondBlock)
-        break;
-      CurrBlock = CurrBlock->getSinglePredecessor();
     }
     return EverChanged;
   }
diff --git a/test/Transforms/Util/flattencfg.ll b/test/Transforms/Util/flattencfg.ll
new file mode 100644
index 0000000..949d7a2
--- /dev/null
+++ b/test/Transforms/Util/flattencfg.ll
@@ -0,0 +1,23 @@
+; RUN: opt -flattencfg -S < %s | FileCheck %s
+
+; Function Attrs: nounwind
+; CHECK-LABEL: test_1
+define void @test_1(i32 %in_a) #0 {
+entry:
+  %cmp0 = icmp eq i32 %in_a, -1
+  %cmp1 = icmp ne i32 %in_a, 0
+  %cond0 = and i1 %cmp0, %cmp1
+  br i1 %cond0, label %b0, label %b1
+
+b0:                                ; preds = %entry
+  %cmp2 = icmp eq i32 %in_a, 0
+  %cmp3 = icmp ne i32 %in_a, 1
+  %cond1 = or i1 %cmp2, %cmp3
+  br i1 %cond1, label %exit, label %b1
+
+b1:                                       ; preds = %entry, %b0
+  br label %exit
+
+exit:                               ; preds = %entry, %b0, %b1
+  ret void
+}
<div class="moz-txt-sig">-- 
1.9.3

</div></pre>
      </div>
    </blockquote>
    <br>
    LGTM, although the test isn't actually checking anything. The test
    should either check something, or not use filecheck if this is
    supposed to be a no-op on this. CHECK-LABELs should also include the
    @ on the function name.
  </body>
</html>