]> granicus.if.org Git - llvm/commitdiff
Simplify the CFG after loop pass cleanup.
authorFilipe Cabecinhas <me@filcab.net>
Wed, 26 Apr 2017 12:02:41 +0000 (12:02 +0000)
committerFilipe Cabecinhas <me@filcab.net>
Wed, 26 Apr 2017 12:02:41 +0000 (12:02 +0000)
Summary:
Otherwise we might end up with some empty basic blocks or
single-entry-single-exit basic blocks.

This fixes PR32085

Reviewers: chandlerc, danielcdh

Subscribers: mehdi_amini, RKSimon, llvm-commits

Differential Revision: https://reviews.llvm.org/D30468

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301395 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Passes/PassBuilder.cpp
lib/Transforms/IPO/PassManagerBuilder.cpp
test/Other/new-pm-defaults.ll
test/Other/pass-pipelines.ll
test/Other/pr32085.ll [new file with mode: 0644]

index 0421946a32a69527f8b9c15a24bbbc4c75d23a95..55ac2541948eadca693d531e9ae65ab4b16e5e7c 100644 (file)
@@ -624,6 +624,10 @@ PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
   // And finally clean up LCSSA form before generating code.
   OptimizePM.addPass(InstSimplifierPass());
 
+  // LoopSink (and other loop passes since the last simplifyCFG) might have
+  // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
+  OptimizePM.addPass(SimplifyCFGPass());
+
   // Add the core optimizing pipeline.
   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
 
index 50159783334fc3bf2b1021bcebabb8db22ef4883..0d5910ebbfcc9bd123a167496a40080831b87b36 100644 (file)
@@ -680,6 +680,11 @@ void PassManagerBuilder::populateModulePassManager(
   MPM.add(createLoopSinkPass());
   // Get rid of LCSSA nodes.
   MPM.add(createInstructionSimplifierPass());
+
+  // LoopSink (and other loop passes since the last simplifyCFG) might have
+  // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
+  MPM.add(createCFGSimplificationPass());
+
   addExtensionsToPM(EP_OptimizerLast, MPM);
 }
 
index 7657f184b28cdce1e928b3aadc38fd65dc14f3ee..fc1170c666f571b59dedfa1dcf7eb5a4ee027c12 100644 (file)
 ; CHECK-O-NEXT: Running pass: AlignmentFromAssumptionsPass
 ; CHECK-O-NEXT: Running pass: LoopSinkPass
 ; CHECK-O-NEXT: Running pass: InstSimplifierPass
+; CHECK-O-NEXT: Running pass: SimplifyCFGPass
 ; CHECK-O-NEXT: Finished llvm::Function pass manager run.
 ; CHECK-O-NEXT: Running pass: GlobalDCEPass
 ; CHECK-O-NEXT: Running pass: ConstantMergePass
index 196f9bfe9826ddb9c48e8de1c87eb3c02a145cec..971ed2c094730f4f53197985be0ffb9682105d67 100644 (file)
@@ -85,6 +85,7 @@
 ; CHECK-O2: FunctionPass Manager
 ; CHECK-O2: Loop Pass Manager
 ; CHECK-O2-NEXT: Loop Sink
+; CHECK-O2: Simplify the CFG
 ; CHECK-O2-NOT: Manager
 ;
 ; FIXME: There really shouldn't be another pass manager, especially one that
diff --git a/test/Other/pr32085.ll b/test/Other/pr32085.ll
new file mode 100644 (file)
index 0000000..428f91e
--- /dev/null
@@ -0,0 +1,56 @@
+; RUN: opt -S -O1 < %s -o %t1.ll
+;; Show that there's no difference after running another simplify CFG
+; RUN: opt -S -simplifycfg < %t1.ll -o %t2.ll
+; RUN: diff %t1.ll %t2.ll
+
+; Test from LoopSink pass, leaves some single-entry single-exit basic blocks.
+; After LoopSink, we get a basic block .exit.loopexit which has one entry and
+; one exit, the only instruction is a branch. Make sure it doesn't show up.
+; Make sure they disappear at -O1.
+
+@g = global i32 0, align 4
+
+define i32 @t1(i32, i32) {
+  %3 = icmp eq i32 %1, 0
+  br i1 %3, label %.exit, label %.preheader
+
+.preheader:
+  %invariant = load i32, i32* @g
+  br label %.b1
+
+.b1:
+  %iv = phi i32 [ %t7, %.b7 ], [ 0, %.preheader ]
+  %c1 = icmp sgt i32 %iv, %0
+  br i1 %c1, label %.b2, label %.b6
+
+.b2:
+  %c2 = icmp sgt i32 %iv, 1
+  br i1 %c2, label %.b3, label %.b4
+
+.b3:
+  %t3 = sub nsw i32 %invariant, %iv
+  br label %.b5
+
+.b4:
+  %t4 = add nsw i32 %invariant, %iv
+  br label %.b5
+
+.b5:
+  %p5 = phi i32 [ %t3, %.b3 ], [ %t4, %.b4 ]
+  %t5 = mul nsw i32 %p5, 5
+  br label %.b7
+
+.b6:
+  %t6 = add nsw i32 %iv, 100
+  br label %.b7
+
+.b7:
+  %p7 = phi i32 [ %t6, %.b6 ], [ %t5, %.b5 ]
+  %t7 = add nuw nsw i32 %iv, 1
+  %c7 = icmp eq i32 %t7, %p7
+  br i1 %c7, label %.b1, label %.exit
+
+.exit:
+  ret i32 10
+}
+