[PATCH] StructurizeCFG: Fix inverting a branch on an argument

Matt Arsenault Matthew.Arsenault at amd.com
Sat Nov 16 21:48:53 PST 2013


http://llvm-reviews.chandlerc.com/D2196

Files:
  lib/Transforms/Scalar/StructurizeCFG.cpp
  test/Transforms/StructurizeCFG/branch-on-argument.ll

Index: lib/Transforms/Scalar/StructurizeCFG.cpp
===================================================================
--- lib/Transforms/Scalar/StructurizeCFG.cpp
+++ lib/Transforms/Scalar/StructurizeCFG.cpp
@@ -323,21 +323,32 @@
   if (match(Condition, m_Not(m_Value(Condition))))
     return Condition;
 
-  // Third: Check all the users for an invert
-  BasicBlock *Parent = cast<Instruction>(Condition)->getParent();
-  for (Value::use_iterator I = Condition->use_begin(),
-       E = Condition->use_end(); I != E; ++I) {
+  if (Instruction *Inst = dyn_cast<Instruction>(Condition)) {
+    // Third: Check all the users for an invert
+    BasicBlock *Parent = Inst->getParent();
+    for (Value::use_iterator I = Condition->use_begin(),
+           E = Condition->use_end(); I != E; ++I) {
+
+      Instruction *User = dyn_cast<Instruction>(*I);
+      if (!User || User->getParent() != Parent)
+        continue;
 
-    Instruction *User = dyn_cast<Instruction>(*I);
-    if (!User || User->getParent() != Parent)
-      continue;
+      if (match(*I, m_Not(m_Specific(Condition))))
+        return *I;
+    }
+
+    // Last option: Create a new instruction
+    return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
+  }
 
-    if (match(*I, m_Not(m_Specific(Condition))))
-      return *I;
+  if (Argument *Arg = dyn_cast<Argument>(Condition)) {
+    BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock();
+    return BinaryOperator::CreateNot(Condition,
+                                     Arg->getName() + ".inv",
+                                     EntryBlock.getTerminator());
   }
 
-  // Last option: Create a new instruction
-  return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
+  llvm_unreachable("Unhandled condition to invert");
 }
 
 /// \brief Build the condition for one edge
Index: test/Transforms/StructurizeCFG/branch-on-argument.ll
===================================================================
--- /dev/null
+++ test/Transforms/StructurizeCFG/branch-on-argument.ll
@@ -0,0 +1,18 @@
+; RUN: opt -S -o - -structurizecfg < %s | FileCheck %s
+
+; CHECK-LABEL: @invert_branch_on_arg(
+; CHECK: entry:
+; CHECK: %cmp.inv = xor i1 %cmp, true
+; CHECK: phi i1 [ false, %Flow1 ], [ %cmp.inv, %entry ]
+
+define void @invert_branch_on_arg(i32 addrspace(1)* %out, i1 %cmp) {
+entry:
+  br i1 %cmp, label %for.end, label %for.body
+
+for.body:                                         ; preds = %entry, %for.body
+  store i32 999, i32 addrspace(1)* %out, align 4
+  br label %for.body
+
+for.end:                                          ; preds = %Flow
+  ret void
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2196.1.patch
Type: text/x-patch
Size: 2637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131116/7b1d9686/attachment.bin>


More information about the llvm-commits mailing list