2011-01-03 08:25:16 +08:00
|
|
|
//===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass performs lightweight instruction simplification on loop bodies.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-16 00:42:11 +08:00
|
|
|
#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
|
2013-02-09 09:04:28 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-12-19 16:22:17 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2011-01-03 08:25:16 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2011-01-04 08:12:46 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2011-01-05 13:15:53 +08:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2015-01-28 12:57:56 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2016-07-16 00:42:11 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2016-07-16 00:42:11 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2017-01-11 17:43:56 +08:00
|
|
|
#include "llvm/Transforms/Scalar/LoopPassManager.h"
|
2011-01-03 08:25:16 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
[LPM] Factor all of the loop analysis usage updates into a common helper
routine.
We were getting this wrong in small ways and generally being very
inconsistent about it across loop passes. Instead, let's have a common
place where we do this. One minor downside is that this will require
some analyses like SCEV in more places than they are strictly needed.
However, this seems benign as these analyses are complete no-ops, and
without this consistency we can in many cases end up with the legacy
pass manager scheduling deciding to split up a loop pass pipeline in
order to run the function analysis half-way through. It is very, very
annoying to fix these without just being very pedantic across the board.
The only loop passes I've not updated here are ones that use
AU.setPreservesAll() such as IVUsers (an analysis) and the pass printer.
They seemed less relevant.
With this patch, almost all of the problems in PR24804 around loop pass
pipelines are fixed. The one remaining issue is that we run simplify-cfg
and instcombine in the middle of the loop pass pipeline. We've recently
added some loop variants of these passes that would seem substantially
cleaner to use, but this at least gets us much closer to the previous
state. Notably, the seven loop pass managers is down to three.
I've not updated the loop passes using LoopAccessAnalysis because that
analysis hasn't been fully wired into LoopSimplify/LCSSA, and it isn't
clear that those transforms want to support those forms anyways. They
all run late anyways, so this is harmless. Similarly, LSR is left alone
because it already carefully manages its forms and doesn't need to get
fused into a single loop pass manager with a bunch of other loop passes.
LoopReroll didn't use loop simplified form previously, and I've updated
the test case to match the trivially different output.
Finally, I've also factored all the pass initialization for the passes
that use this technique as well, so that should be done regularly and
reliably.
Thanks to James for the help reviewing and thinking about this stuff,
and Ben for help thinking about it as well!
Differential Revision: http://reviews.llvm.org/D17435
llvm-svn: 261316
2016-02-19 18:45:18 +08:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
2011-01-03 08:25:16 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "loop-instsimplify"
|
|
|
|
|
2011-01-03 08:25:16 +08:00
|
|
|
STATISTIC(NumSimplified, "Number of redundant instructions simplified");
|
|
|
|
|
2016-07-16 00:42:11 +08:00
|
|
|
static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI,
|
2016-12-19 16:22:17 +08:00
|
|
|
AssumptionCache *AC,
|
2016-07-16 00:42:11 +08:00
|
|
|
const TargetLibraryInfo *TLI) {
|
|
|
|
SmallVector<BasicBlock *, 8> ExitBlocks;
|
2011-01-05 13:15:53 +08:00
|
|
|
L->getUniqueExitBlocks(ExitBlocks);
|
|
|
|
array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
|
|
|
|
|
2016-07-16 00:42:11 +08:00
|
|
|
SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
|
2011-01-05 13:47:47 +08:00
|
|
|
|
2011-01-08 23:52:22 +08:00
|
|
|
// The bit we are stealing from the pointer represents whether this basic
|
|
|
|
// block is the header of a subloop, in which case we only process its phis.
|
2016-07-16 00:42:11 +08:00
|
|
|
typedef PointerIntPair<BasicBlock *, 1> WorklistItem;
|
2011-01-08 23:52:22 +08:00
|
|
|
SmallVector<WorklistItem, 16> VisitStack;
|
2016-07-16 00:42:11 +08:00
|
|
|
SmallPtrSet<BasicBlock *, 32> Visited;
|
2011-01-05 13:15:53 +08:00
|
|
|
|
2011-01-03 08:25:16 +08:00
|
|
|
bool Changed = false;
|
|
|
|
bool LocalChanged;
|
|
|
|
do {
|
|
|
|
LocalChanged = false;
|
|
|
|
|
2011-01-05 13:15:53 +08:00
|
|
|
VisitStack.clear();
|
|
|
|
Visited.clear();
|
|
|
|
|
2011-01-08 23:52:22 +08:00
|
|
|
VisitStack.push_back(WorklistItem(L->getHeader(), false));
|
2011-01-05 13:15:53 +08:00
|
|
|
|
|
|
|
while (!VisitStack.empty()) {
|
2011-01-08 23:52:22 +08:00
|
|
|
WorklistItem Item = VisitStack.pop_back_val();
|
2011-01-09 01:07:11 +08:00
|
|
|
BasicBlock *BB = Item.getPointer();
|
2011-01-08 23:52:22 +08:00
|
|
|
bool IsSubloopHeader = Item.getInt();
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
|
2011-01-05 13:15:53 +08:00
|
|
|
|
|
|
|
// Simplify instructions in the current basic block.
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
|
2015-10-14 03:26:58 +08:00
|
|
|
Instruction *I = &*BI++;
|
2011-01-05 13:47:47 +08:00
|
|
|
|
|
|
|
// The first time through the loop ToSimplify is empty and we try to
|
|
|
|
// simplify all instructions. On later iterations ToSimplify is not
|
|
|
|
// empty and we only bother simplifying instructions that are in it.
|
|
|
|
if (!ToSimplify->empty() && !ToSimplify->count(I))
|
|
|
|
continue;
|
|
|
|
|
2011-01-03 08:25:16 +08:00
|
|
|
// Don't bother simplifying unused instructions.
|
|
|
|
if (!I->use_empty()) {
|
2017-04-29 03:55:38 +08:00
|
|
|
Value *V = SimplifyInstruction(I, {DL, TLI, DT, AC});
|
2011-01-04 08:12:46 +08:00
|
|
|
if (V && LI->replacementPreservesLCSSAForm(I, V)) {
|
2011-01-05 13:47:47 +08:00
|
|
|
// Mark all uses for resimplification next time round the loop.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : I->users())
|
|
|
|
Next->insert(cast<Instruction>(U));
|
2011-01-05 13:47:47 +08:00
|
|
|
|
2011-01-03 08:25:16 +08:00
|
|
|
I->replaceAllUsesWith(V);
|
|
|
|
LocalChanged = true;
|
|
|
|
++NumSimplified;
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 21:27:13 +08:00
|
|
|
if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) {
|
|
|
|
// RecursivelyDeleteTriviallyDeadInstruction can remove more than one
|
|
|
|
// instruction, so simply incrementing the iterator does not work.
|
|
|
|
// When instructions get deleted re-iterate instead.
|
2016-07-16 00:42:11 +08:00
|
|
|
BI = BB->begin();
|
|
|
|
BE = BB->end();
|
2016-04-06 21:27:13 +08:00
|
|
|
LocalChanged = true;
|
2014-04-26 13:58:11 +08:00
|
|
|
}
|
2011-01-08 23:52:22 +08:00
|
|
|
|
|
|
|
if (IsSubloopHeader && !isa<PHINode>(I))
|
|
|
|
break;
|
2011-01-03 08:25:16 +08:00
|
|
|
}
|
|
|
|
|
2011-01-08 23:52:22 +08:00
|
|
|
// Add all successors to the worklist, except for loop exit blocks and the
|
2016-07-16 00:42:11 +08:00
|
|
|
// bodies of subloops. We visit the headers of loops so that we can
|
|
|
|
// process
|
|
|
|
// their phis, but we contract the rest of the subloop body and only
|
|
|
|
// follow
|
2011-01-08 23:52:22 +08:00
|
|
|
// edges leading back to the original loop.
|
2014-07-22 01:06:51 +08:00
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
|
|
|
|
++SI) {
|
|
|
|
BasicBlock *SuccBB = *SI;
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!Visited.insert(SuccBB).second)
|
2011-01-08 23:52:22 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const Loop *SuccLoop = LI->getLoopFor(SuccBB);
|
2016-07-16 00:42:11 +08:00
|
|
|
if (SuccLoop && SuccLoop->getHeader() == SuccBB &&
|
|
|
|
L->contains(SuccLoop)) {
|
2011-01-08 23:52:22 +08:00
|
|
|
VisitStack.push_back(WorklistItem(SuccBB, true));
|
|
|
|
|
2016-07-16 00:42:11 +08:00
|
|
|
SmallVector<BasicBlock *, 8> SubLoopExitBlocks;
|
2011-01-08 23:52:22 +08:00
|
|
|
SuccLoop->getExitBlocks(SubLoopExitBlocks);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
|
|
|
|
BasicBlock *ExitBB = SubLoopExitBlocks[i];
|
2014-11-19 15:49:26 +08:00
|
|
|
if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
|
2011-01-08 23:52:22 +08:00
|
|
|
VisitStack.push_back(WorklistItem(ExitBB, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-16 00:42:11 +08:00
|
|
|
bool IsExitBlock =
|
|
|
|
std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB);
|
2011-01-08 23:52:22 +08:00
|
|
|
if (IsExitBlock)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
VisitStack.push_back(WorklistItem(SuccBB, false));
|
2011-01-05 13:15:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-05 13:47:47 +08:00
|
|
|
// Place the list of instructions to simplify on the next loop iteration
|
|
|
|
// into ToSimplify.
|
|
|
|
std::swap(ToSimplify, Next);
|
|
|
|
Next->clear();
|
|
|
|
|
2011-01-04 08:12:46 +08:00
|
|
|
Changed |= LocalChanged;
|
2011-01-03 08:25:16 +08:00
|
|
|
} while (LocalChanged);
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2016-07-16 00:42:11 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LoopInstSimplifyLegacyPass : public LoopPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass ID, replacement for typeid
|
|
|
|
LoopInstSimplifyLegacyPass() : LoopPass(ID) {
|
|
|
|
initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnLoop(Loop *L, LPPassManager &LPM) override {
|
|
|
|
if (skipLoop(L))
|
|
|
|
return false;
|
|
|
|
DominatorTreeWrapperPass *DTWP =
|
|
|
|
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
|
|
|
DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
|
|
|
|
LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2016-12-19 16:22:17 +08:00
|
|
|
AssumptionCache *AC =
|
|
|
|
&getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
|
|
|
|
*L->getHeader()->getParent());
|
2016-07-16 00:42:11 +08:00
|
|
|
const TargetLibraryInfo *TLI =
|
|
|
|
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
return SimplifyLoopInst(L, DT, LI, AC, TLI);
|
2016-07-16 00:42:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2016-12-19 16:22:17 +08:00
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
2016-07-16 00:42:11 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
getLoopAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:23:21 +08:00
|
|
|
PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
|
|
|
|
LoopStandardAnalysisResults &AR,
|
|
|
|
LPMUpdater &) {
|
|
|
|
if (!SimplifyLoopInst(&L, &AR.DT, &AR.LI, &AR.AC, &AR.TLI))
|
2016-07-16 00:42:11 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
2017-01-15 14:32:49 +08:00
|
|
|
auto PA = getLoopPassPreservedAnalyses();
|
|
|
|
PA.preserveSet<CFGAnalyses>();
|
|
|
|
return PA;
|
2016-07-16 00:42:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char LoopInstSimplifyLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
|
|
|
|
"Simplify instructions in loops", false, false)
|
2016-12-19 16:22:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
2016-07-16 00:42:11 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
|
|
|
|
"Simplify instructions in loops", false, false)
|
|
|
|
|
|
|
|
Pass *llvm::createLoopInstSimplifyPass() {
|
|
|
|
return new LoopInstSimplifyLegacyPass();
|
|
|
|
}
|