2010-12-21 05:07:42 +08:00
|
|
|
//===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a utility pass used for testing the InstructionSimplify analysis.
|
|
|
|
// The analysis is applied to every instruction, and if it simplifies then the
|
|
|
|
// instruction is replaced by the simplification. If you are looking for a pass
|
|
|
|
// that performs serious instruction folding, use the instcombine pass instead.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-08 05:14:36 +08:00
|
|
|
#include "llvm/Transforms/Utils/SimplifyInstructions.h"
|
2011-01-01 01:49:05 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2011-01-03 18:50:04 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2010-12-21 05:07:42 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-12-19 16:22:17 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2010-12-21 05:07:42 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2015-11-25 02:57:06 +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/Function.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2010-12-22 00:12:03 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2016-07-08 05:14:36 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2010-12-21 05:07:42 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "instsimplify"
|
|
|
|
|
2010-12-21 05:07:42 +08:00
|
|
|
STATISTIC(NumSimplified, "Number of redundant instructions removed");
|
|
|
|
|
2016-11-27 23:53:48 +08:00
|
|
|
static bool runImpl(Function &F, const DominatorTree *DT,
|
2016-12-19 16:22:17 +08:00
|
|
|
const TargetLibraryInfo *TLI, AssumptionCache *AC) {
|
2016-07-08 05:14:36 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2016-11-27 23:53:48 +08:00
|
|
|
SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
|
2016-07-08 05:14:36 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
do {
|
2016-11-27 23:53:48 +08:00
|
|
|
for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
|
2016-07-08 05:14:36 +08:00
|
|
|
// Here be subtlety: the iterator must be incremented before the loop
|
|
|
|
// body (not sure why), so a range-for loop won't work here.
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
|
|
|
|
Instruction *I = &*BI++;
|
|
|
|
// 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;
|
2016-11-27 23:53:48 +08:00
|
|
|
|
2016-07-08 05:14:36 +08:00
|
|
|
// Don't waste time simplifying unused instructions.
|
2016-11-27 23:53:48 +08:00
|
|
|
if (!I->use_empty()) {
|
2016-12-19 16:22:17 +08:00
|
|
|
if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
|
2016-07-08 05:14:36 +08:00
|
|
|
// Mark all uses for resimplification next time round the loop.
|
|
|
|
for (User *U : I->users())
|
|
|
|
Next->insert(cast<Instruction>(U));
|
|
|
|
I->replaceAllUsesWith(V);
|
|
|
|
++NumSimplified;
|
|
|
|
Changed = true;
|
|
|
|
}
|
2016-11-27 23:53:48 +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.
|
|
|
|
BI = BB->begin();
|
|
|
|
BE = BB->end();
|
|
|
|
Changed = true;
|
2016-07-08 05:14:36 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-27 23:53:48 +08:00
|
|
|
}
|
2016-07-08 05:14:36 +08:00
|
|
|
|
|
|
|
// Place the list of instructions to simplify on the next loop iteration
|
|
|
|
// into ToSimplify.
|
|
|
|
std::swap(ToSimplify, Next);
|
|
|
|
Next->clear();
|
|
|
|
} while (!ToSimplify->empty());
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2010-12-21 05:07:42 +08:00
|
|
|
namespace {
|
|
|
|
struct InstSimplifier : public FunctionPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
InstSimplifier() : FunctionPass(ID) {
|
|
|
|
initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-12-21 05:07:42 +08:00
|
|
|
AU.setPreservesCFG();
|
2016-09-07 06:17:16 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2016-12-19 16:22:17 +08:00
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
2015-01-15 18:41:28 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2010-12-21 05:07:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnFunction - Remove instructions that simplify.
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
2016-05-04 06:32:30 +08:00
|
|
|
if (skipFunction(F))
|
|
|
|
return false;
|
|
|
|
|
2016-09-07 06:17:16 +08:00
|
|
|
const DominatorTree *DT =
|
|
|
|
&getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2015-01-15 18:41:28 +08:00
|
|
|
const TargetLibraryInfo *TLI =
|
|
|
|
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
2016-12-19 16:22:17 +08:00
|
|
|
AssumptionCache *AC =
|
|
|
|
&getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
|
|
|
return runImpl(F, DT, TLI, AC);
|
2010-12-21 05:07:42 +08:00
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2010-12-21 05:07:42 +08:00
|
|
|
|
|
|
|
char InstSimplifier::ID = 0;
|
2011-12-01 11:08:23 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
|
|
|
|
"Remove redundant instructions", false, false)
|
2016-12-19 16:22:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
2016-09-07 06:17:16 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2015-01-15 18:41:28 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
2011-12-01 11:08:23 +08:00
|
|
|
INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
|
|
|
|
"Remove redundant instructions", false, false)
|
2010-12-21 05:07:42 +08:00
|
|
|
char &llvm::InstructionSimplifierID = InstSimplifier::ID;
|
|
|
|
|
|
|
|
// Public interface to the simplify instructions pass.
|
|
|
|
FunctionPass *llvm::createInstructionSimplifierPass() {
|
|
|
|
return new InstSimplifier();
|
|
|
|
}
|
2016-07-08 05:14:36 +08:00
|
|
|
|
|
|
|
PreservedAnalyses InstSimplifierPass::run(Function &F,
|
2016-08-09 08:28:15 +08:00
|
|
|
FunctionAnalysisManager &AM) {
|
2016-09-07 06:17:16 +08:00
|
|
|
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
|
2016-07-08 05:14:36 +08:00
|
|
|
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
|
2016-12-19 16:22:17 +08:00
|
|
|
auto &AC = AM.getResult<AssumptionAnalysis>(F);
|
|
|
|
bool Changed = runImpl(F, &DT, &TLI, &AC);
|
2016-07-08 05:14:36 +08:00
|
|
|
if (!Changed)
|
|
|
|
return PreservedAnalyses::all();
|
2017-01-15 14:32:49 +08:00
|
|
|
|
|
|
|
PreservedAnalyses PA;
|
|
|
|
PA.preserveSet<CFGAnalyses>();
|
|
|
|
return PA;
|
2016-07-08 05:14:36 +08:00
|
|
|
}
|