[PATCH] D120312: [StructurizeCFG] Fix boolean not bug

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 22 02:07:19 PST 2022


foad created this revision.
foad added reviewers: arsenm, mareko.
Herald added a subscriber: hiraditya.
foad requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

D118623 <https://reviews.llvm.org/D118623> added code to fold not-of-compare into a compare
with the inverted predicate, if the compare had no other
uses. This relies on accurate use lists in the IR but it
was run before setPhiValues, when some phi inputs are still
stored in a data structure on the side, instead of being
real uses in the IR. The effect was that a phi that should
be using the original compare result would now get an
inverted result instead.

Fix this by moving simplifyConditions after setPhiValues.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120312

Files:
  llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
  llvm/test/Transforms/StructurizeCFG/invert-condition.ll


Index: llvm/test/Transforms/StructurizeCFG/invert-condition.ll
===================================================================
--- llvm/test/Transforms/StructurizeCFG/invert-condition.ll
+++ llvm/test/Transforms/StructurizeCFG/invert-condition.ll
@@ -29,13 +29,12 @@
   ret void
 }
 
-; FIXME: StructurizeCFG modifies I5 in-place without updating the use of I5 in
-; the phi instruction.
 define void @invert_condition_phi(i32 %arg) {
 ; CHECK-LABEL: @invert_condition_phi(
 ; CHECK-NEXT:  main_body:
-; CHECK-NEXT:    [[I5:%.*]] = icmp ne i32 [[ARG:%.*]], 0
-; CHECK-NEXT:    br i1 [[I5]], label [[IF1:%.*]], label [[ENDIF1:%.*]]
+; CHECK-NEXT:    [[I5:%.*]] = icmp eq i32 [[ARG:%.*]], 0
+; CHECK-NEXT:    [[I5_INV:%.*]] = xor i1 [[I5]], true
+; CHECK-NEXT:    br i1 [[I5_INV]], label [[IF1:%.*]], label [[ENDIF1:%.*]]
 ; CHECK:       if1:
 ; CHECK-NEXT:    br label [[ENDIF1]]
 ; CHECK:       endif1:
Index: llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
+++ llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
@@ -1089,8 +1089,8 @@
   createFlow();
   insertConditions(false);
   insertConditions(true);
-  simplifyConditions();
   setPhiValues();
+  simplifyConditions();
   simplifyAffectedPhis();
   rebuildSSA();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120312.410484.patch
Type: text/x-patch
Size: 1337 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220222/71719e10/attachment.bin>


More information about the llvm-commits mailing list