#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/Analysis/EHPersonalities.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instructions.h"
#include "llvm/InitializePasses.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Utils/CallGraphUpdater.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
using namespace llvm;
#define DEBUG_TYPE "prune-eh"
STATISTIC(NumRemoved, "Number of invokes removed");
STATISTIC(NumUnreach, "Number of noreturn calls optimized");
namespace {
struct PruneEH : public CallGraphSCCPass {
static char ID; PruneEH() : CallGraphSCCPass(ID) {
initializePruneEHPass(*PassRegistry::getPassRegistry());
}
bool runOnSCC(CallGraphSCC &SCC) override;
};
}
static bool SimplifyFunction(Function *F, CallGraphUpdater &CGU);
static void DeleteBasicBlock(BasicBlock *BB, CallGraphUpdater &CGU);
char PruneEH::ID = 0;
INITIALIZE_PASS_BEGIN(PruneEH, "prune-eh",
"Remove unused exception handling info", false, false)
INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
INITIALIZE_PASS_END(PruneEH, "prune-eh",
"Remove unused exception handling info", false, false)
Pass *llvm::createPruneEHPass() { return new PruneEH(); }
static bool runImpl(CallGraphUpdater &CGU, SetVector<Function *> &Functions) {
#ifndef NDEBUG
for (auto *F : Functions)
assert(F && "null Function");
#endif
bool MadeChange = false;
for (Function *F : Functions)
MadeChange |= SimplifyFunction(F, CGU);
bool SCCMightUnwind = false, SCCMightReturn = false;
for (Function *F : Functions) {
if (!F->hasExactDefinition()) {
SCCMightUnwind |= !F->doesNotThrow();
SCCMightReturn |= !F->doesNotReturn();
} else {
bool CheckUnwind = !SCCMightUnwind && !F->doesNotThrow();
bool CheckReturn = !SCCMightReturn && !F->doesNotReturn();
bool CheckReturnViaAsm = CheckReturn &&
F->hasFnAttribute(Attribute::Naked) &&
F->hasFnAttribute(Attribute::NoInline);
if (!CheckUnwind && !CheckReturn)
continue;
for (const BasicBlock &BB : *F) {
const Instruction *TI = BB.getTerminator();
if (CheckUnwind && TI->mayThrow()) {
SCCMightUnwind = true;
} else if (CheckReturn && isa<ReturnInst>(TI)) {
SCCMightReturn = true;
}
for (const Instruction &I : BB) {
if ((!CheckUnwind || SCCMightUnwind) &&
(!CheckReturnViaAsm || SCCMightReturn))
break;
if (CheckUnwind && !SCCMightUnwind && I.mayThrow()) {
bool InstMightUnwind = true;
if (const auto *CI = dyn_cast<CallInst>(&I)) {
if (Function *Callee = CI->getCalledFunction()) {
if (Functions.contains(Callee))
InstMightUnwind = false;
}
}
SCCMightUnwind |= InstMightUnwind;
}
if (CheckReturnViaAsm && !SCCMightReturn)
if (const auto *CB = dyn_cast<CallBase>(&I))
if (const auto *IA = dyn_cast<InlineAsm>(CB->getCalledOperand()))
if (IA->hasSideEffects())
SCCMightReturn = true;
}
}
if (SCCMightUnwind && SCCMightReturn)
break;
}
}
if (!SCCMightUnwind || !SCCMightReturn)
for (Function *F : Functions) {
if (!SCCMightUnwind && !F->hasFnAttribute(Attribute::NoUnwind)) {
F->addFnAttr(Attribute::NoUnwind);
MadeChange = true;
}
if (!SCCMightReturn && !F->hasFnAttribute(Attribute::NoReturn)) {
F->addFnAttr(Attribute::NoReturn);
MadeChange = true;
}
}
for (Function *F : Functions) {
MadeChange |= SimplifyFunction(F, CGU);
}
return MadeChange;
}
bool PruneEH::runOnSCC(CallGraphSCC &SCC) {
if (skipSCC(SCC))
return false;
SetVector<Function *> Functions;
for (auto &N : SCC) {
if (auto *F = N->getFunction())
Functions.insert(F);
}
CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
CallGraphUpdater CGU;
CGU.initialize(CG, SCC);
return runImpl(CGU, Functions);
}
static bool SimplifyFunction(Function *F, CallGraphUpdater &CGU) {
bool MadeChange = false;
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(F)) {
BasicBlock *UnwindBlock = II->getUnwindDest();
removeUnwindEdge(&*BB);
if (pred_empty(UnwindBlock))
DeleteBasicBlock(UnwindBlock, CGU);
++NumRemoved;
MadeChange = true;
}
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
if (CallInst *CI = dyn_cast<CallInst>(I++))
if (CI->doesNotReturn() && !CI->isMustTailCall() &&
!isa<UnreachableInst>(I)) {
BasicBlock *New = BB->splitBasicBlock(I);
BB->getInstList().pop_back();
new UnreachableInst(BB->getContext(), &*BB);
DeleteBasicBlock(New, CGU); MadeChange = true;
++NumUnreach;
break;
}
}
return MadeChange;
}
static void DeleteBasicBlock(BasicBlock *BB, CallGraphUpdater &CGU) {
assert(pred_empty(BB) && "BB is not dead!");
Instruction *TokenInst = nullptr;
for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) {
--I;
if (I->getType()->isTokenTy()) {
TokenInst = &*I;
break;
}
if (auto *Call = dyn_cast<CallBase>(&*I)) {
const Function *Callee = Call->getCalledFunction();
if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
CGU.removeCallSite(*Call);
else if (!Callee->isIntrinsic())
CGU.removeCallSite(*Call);
}
if (!I->use_empty())
I->replaceAllUsesWith(PoisonValue::get(I->getType()));
}
if (TokenInst) {
if (!TokenInst->isTerminator())
changeToUnreachable(TokenInst->getNextNode());
} else {
std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB));
for (unsigned i = 0, e = Succs.size(); i != e; ++i)
Succs[i]->removePredecessor(BB);
BB->eraseFromParent();
}
}