[llvm] r259256 - Add LoopSimplifyCFG pass

Fiona Glaser via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 29 14:35:36 PST 2016


Author: escha
Date: Fri Jan 29 16:35:36 2016
New Revision: 259256

URL: http://llvm.org/viewvc/llvm-project?rev=259256&view=rev
Log:
Add LoopSimplifyCFG pass

Loop transformations can sometimes fail because the loop, while in
valid rotated LCSSA form, is not in a canonical CFG form. This is
an extremely simple pass that just merges obviously redundant
blocks, which can be used to fix some known failure cases. In the
future, it may be enhanced with more cases (and have code shared with
SimplifyCFG).

This allows us to run LoopSimplifyCFG -> LoopRotate -> LoopUnroll,
so that SimplifyCFG cleans up the loop before Rotate tries to run.

Not currently used in the pass manager, since this pass doesn't do
anything unless you can hook it up in an LPM with other loop passes.
It'll be added once Chandler cleans up things to allow this.

Tested in a custom pipeline out of tree to confirm it works in
practice (in addition to the included trivial test).

Added:
    llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
    llvm/trunk/test/Transforms/LoopSimplifyCFG/
    llvm/trunk/test/Transforms/LoopSimplifyCFG/merge-header.ll
Modified:
    llvm/trunk/include/llvm/InitializePasses.h
    llvm/trunk/include/llvm/LinkAllPasses.h
    llvm/trunk/include/llvm/Transforms/Scalar.h
    llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt
    llvm/trunk/lib/Transforms/Scalar/Scalar.cpp

Modified: llvm/trunk/include/llvm/InitializePasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=259256&r1=259255&r2=259256&view=diff
==============================================================================
--- llvm/trunk/include/llvm/InitializePasses.h (original)
+++ llvm/trunk/include/llvm/InitializePasses.h Fri Jan 29 16:35:36 2016
@@ -170,6 +170,7 @@ void initializeLoopInterchangePass(PassR
 void initializeLoopInstSimplifyPass(PassRegistry&);
 void initializeLoopRotatePass(PassRegistry&);
 void initializeLoopSimplifyPass(PassRegistry&);
+void initializeLoopSimplifyCFGPass(PassRegistry&);
 void initializeLoopStrengthReducePass(PassRegistry&);
 void initializeGlobalMergePass(PassRegistry&);
 void initializeLoopRerollPass(PassRegistry&);

Modified: llvm/trunk/include/llvm/LinkAllPasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=259256&r1=259255&r2=259256&view=diff
==============================================================================
--- llvm/trunk/include/llvm/LinkAllPasses.h (original)
+++ llvm/trunk/include/llvm/LinkAllPasses.h Fri Jan 29 16:35:36 2016
@@ -106,6 +106,7 @@ namespace {
       (void) llvm::createLoopExtractorPass();
       (void) llvm::createLoopInterchangePass();
       (void) llvm::createLoopSimplifyPass();
+      (void) llvm::createLoopSimplifyCFGPass();
       (void) llvm::createLoopStrengthReducePass();
       (void) llvm::createLoopRerollPass();
       (void) llvm::createLoopUnrollPass();

Modified: llvm/trunk/include/llvm/Transforms/Scalar.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=259256&r1=259255&r2=259256&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Scalar.h (original)
+++ llvm/trunk/include/llvm/Transforms/Scalar.h Fri Jan 29 16:35:36 2016
@@ -476,6 +476,13 @@ FunctionPass *createLoopDistributePass()
 //
 FunctionPass *createLoopLoadEliminationPass();
 
+//===----------------------------------------------------------------------===//
+//
+// LoopSimplifyCFG - This pass performs basic CFG simplification on loops,
+// primarily to help other loop passes.
+//
+Pass *createLoopSimplifyCFGPass();
+
 } // End llvm namespace
 
 #endif

Modified: llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt?rev=259256&r1=259255&r2=259256&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt (original)
+++ llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt Fri Jan 29 16:35:36 2016
@@ -24,6 +24,7 @@ add_llvm_library(LLVMScalarOpts
   LoopLoadElimination.cpp
   LoopRerollPass.cpp
   LoopRotation.cpp
+  LoopSimplifyCFG.cpp
   LoopStrengthReduce.cpp
   LoopUnrollPass.cpp
   LoopUnswitch.cpp

Added: llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp?rev=259256&view=auto
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp (added)
+++ llvm/trunk/lib/Transforms/Scalar/LoopSimplifyCFG.cpp Fri Jan 29 16:35:36 2016
@@ -0,0 +1,111 @@
+//===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the Loop SimplifyCFG Pass. This pass is responsible for
+// basic loop CFG cleanup, primarily to assist other loop passes. If you
+// encounter a noncanonical CFG construct that causes another loop pass to
+// perform suboptimally, this is the place to fix it up.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Analysis/BasicAliasAnalysis.h"
+#include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/Analysis/DependenceAnalysis.h"
+#include "llvm/Analysis/GlobalsModRef.h"
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/LoopPass.h"
+#include "llvm/Analysis/ScalarEvolution.h"
+#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/IR/Dominators.h"
+#include "llvm/Transforms/Utils/Local.h"
+using namespace llvm;
+
+#define DEBUG_TYPE "loop-simplifycfg"
+
+namespace {
+class LoopSimplifyCFG : public LoopPass {
+public:
+  static char ID; // Pass ID, replacement for typeid
+  LoopSimplifyCFG() : LoopPass(ID) {
+    initializeLoopSimplifyCFGPass(*PassRegistry::getPassRegistry());
+  }
+
+  bool runOnLoop(Loop *L, LPPassManager &) override;
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.addRequired<DominatorTreeWrapperPass>();
+    AU.addRequired<LoopInfoWrapperPass>();
+
+    AU.addPreserved<DominatorTreeWrapperPass>();
+    AU.addPreserved<LoopInfoWrapperPass>();
+    AU.addPreserved<GlobalsAAWrapperPass>();
+    AU.addPreserved<BasicAAWrapperPass>();
+    AU.addPreserved<AAResultsWrapperPass>();
+    AU.addPreserved<ScalarEvolutionWrapperPass>();
+    AU.addPreserved<SCEVAAWrapperPass>();
+    AU.addPreserved<DependenceAnalysis>();
+    AU.addPreservedID(LoopSimplifyID);
+    AU.addPreservedID(LCSSAID);
+  }
+};
+}
+
+char LoopSimplifyCFG::ID = 0;
+INITIALIZE_PASS_BEGIN(LoopSimplifyCFG, "loop-simplifycfg", "Simplify loop CFG",
+                      false, false)
+INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
+INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
+INITIALIZE_PASS_END(LoopSimplifyCFG, "loop-simplifycfg", "Simplify loop CFG",
+                    false, false)
+
+Pass *llvm::createLoopSimplifyCFGPass() { return new LoopSimplifyCFG(); }
+
+static bool simplifyLoopCFG(Loop *L, DominatorTree *DT, LoopInfo *LI) {
+  bool Changed = false;
+  // Copy blocks into a temporary array to avoid iterator invalidation issues
+  // as we remove them.
+  SmallVector<WeakVH, 16> Blocks(L->blocks());
+
+  for (auto &Block : Blocks) {
+    // Attempt to merge blocks in the trivial case. Don't modify blocks which
+    // belong to other loops.
+    BasicBlock *Succ = cast<BasicBlock>(Block);
+    if (!Succ)
+      continue;
+
+    BasicBlock *Pred = Succ->getSinglePredecessor();
+    if (!Pred || !Pred->getSingleSuccessor() || LI->getLoopFor(Pred) != L)
+      continue;
+
+    // Pred is going to disappear, so we need to update the loop info.
+    if (L->getHeader() == Pred)
+      L->moveToHeader(Succ);
+    LI->removeBlock(Pred);
+    MergeBasicBlockIntoOnlyPred(Succ, DT);
+    Changed = true;
+  }
+
+  return Changed;
+}
+
+/// runOnLoop - Perform basic CFG simplifications to assist other loop passes.
+/// For now, this only attempts to merge blocks in the trivial case.
+bool LoopSimplifyCFG::runOnLoop(Loop *L, LPPassManager &) {
+  if (skipOptnoneFunction(L))
+    return false;
+
+  DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
+  LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
+  return simplifyLoopCFG(L, DT, LI);
+}

Modified: llvm/trunk/lib/Transforms/Scalar/Scalar.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Scalar.cpp?rev=259256&r1=259255&r2=259256&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/Scalar.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/Scalar.cpp Fri Jan 29 16:35:36 2016
@@ -84,6 +84,7 @@ void llvm::initializeScalarOpts(PassRegi
   initializeFloat2IntPass(Registry);
   initializeLoopDistributePass(Registry);
   initializeLoopLoadEliminationPass(Registry);
+  initializeLoopSimplifyCFGPass(Registry);
 }
 
 void LLVMInitializeScalarOpts(LLVMPassRegistryRef R) {
@@ -154,6 +155,10 @@ void LLVMAddLoopRerollPass(LLVMPassManag
   unwrap(PM)->add(createLoopRerollPass());
 }
 
+void LLVMAddLoopSimplifyCFGPass(LLVMPassManagerRef PM) {
+  unwrap(PM)->add(createLoopSimplifyCFGPass());
+}
+
 void LLVMAddLoopUnrollPass(LLVMPassManagerRef PM) {
   unwrap(PM)->add(createLoopUnrollPass());
 }

Added: llvm/trunk/test/Transforms/LoopSimplifyCFG/merge-header.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopSimplifyCFG/merge-header.ll?rev=259256&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/LoopSimplifyCFG/merge-header.ll (added)
+++ llvm/trunk/test/Transforms/LoopSimplifyCFG/merge-header.ll Fri Jan 29 16:35:36 2016
@@ -0,0 +1,34 @@
+; RUN: opt -S -loop-simplifycfg < %s | FileCheck %s
+
+; CHECK-LABEL: foo
+; CHECK:      entry:
+; CHECK-NEXT:   br label %[[LOOP:[a-z]+]]
+; CHECK:      [[LOOP]]:
+; CHECK-NEXT:   phi
+; CHECK-NOT:    br label
+; CHECK:        br i1
+define i32 @foo(i32* %P, i64* %Q) {
+entry:
+  br label %outer
+
+outer:                                            ; preds = %outer.latch2, %entry
+  %y.2 = phi i32 [ 0, %entry ], [ %y.inc2, %outer.latch2 ]
+  br label %inner
+
+inner:                                            ; preds = %outer
+  store i32 0, i32* %P
+  store i32 1, i32* %P
+  store i32 2, i32* %P
+  %y.inc2 = add nsw i32 %y.2, 1
+  %exitcond.outer = icmp eq i32 %y.inc2, 3
+  store i32 %y.2, i32* %P
+  br i1 %exitcond.outer, label %exit, label %outer.latch2
+
+outer.latch2:                                     ; preds = %inner
+  %t = sext i32 %y.inc2 to i64
+  store i64 %t, i64* %Q
+  br label %outer
+
+exit:                                             ; preds = %inner
+  ret i32 0
+}




More information about the llvm-commits mailing list