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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Scalar.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"
|
2015-01-04 20:03:27 +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"
|
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");
|
|
|
|
|
|
|
|
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();
|
2015-01-04 20:03:27 +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 {
|
2014-01-13 21:07:17 +08:00
|
|
|
const DominatorTreeWrapperPass *DTWP =
|
|
|
|
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
2014-04-25 13:29:35 +08:00
|
|
|
const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2015-01-15 18:41:28 +08:00
|
|
|
const TargetLibraryInfo *TLI =
|
|
|
|
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
2015-01-04 20:03:27 +08:00
|
|
|
AssumptionCache *AC =
|
|
|
|
&getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
2011-01-03 18:50:04 +08:00
|
|
|
SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
|
2010-12-22 00:12:03 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
2011-01-01 01:49:05 +08:00
|
|
|
do {
|
2014-04-11 09:50:01 +08:00
|
|
|
for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
|
|
|
|
// 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;) {
|
2015-10-13 10:39:05 +08:00
|
|
|
Instruction *I = &*BI++;
|
2011-01-03 18:50:04 +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;
|
|
|
|
// Don't waste time simplifying unused instructions.
|
2011-01-01 01:49:05 +08:00
|
|
|
if (!I->use_empty())
|
2015-03-10 10:37:25 +08:00
|
|
|
if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
|
2011-01-03 18:50:04 +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-01 01:49:05 +08:00
|
|
|
I->replaceAllUsesWith(V);
|
|
|
|
++NumSimplified;
|
2011-01-03 18:50:04 +08:00
|
|
|
Changed = true;
|
2011-01-01 01:49:05 +08:00
|
|
|
}
|
2014-04-26 13:58:11 +08:00
|
|
|
bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
|
|
|
|
if (res) {
|
|
|
|
// 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 |= res;
|
|
|
|
}
|
2011-01-01 01:49:05 +08:00
|
|
|
}
|
2010-12-22 00:12:03 +08:00
|
|
|
|
2011-01-03 18:50:04 +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());
|
2010-12-22 01:08:55 +08:00
|
|
|
|
2010-12-21 05:07:42 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
};
|
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)
|
2015-01-04 20:03:27 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
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();
|
|
|
|
}
|