Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/DwarfEHPrepare.cpp
35233 views
//===- DwarfEHPrepare - Prepare exception handling for code generation ----===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This pass mulches exception handling code into a form adapted to code9// generation. Required if using dwarf exception handling.10//11//===----------------------------------------------------------------------===//1213#include "llvm/CodeGen/DwarfEHPrepare.h"14#include "llvm/ADT/BitVector.h"15#include "llvm/ADT/SmallVector.h"16#include "llvm/ADT/Statistic.h"17#include "llvm/Analysis/CFG.h"18#include "llvm/Analysis/DomTreeUpdater.h"19#include "llvm/Analysis/TargetTransformInfo.h"20#include "llvm/CodeGen/RuntimeLibcallUtil.h"21#include "llvm/CodeGen/TargetLowering.h"22#include "llvm/CodeGen/TargetPassConfig.h"23#include "llvm/CodeGen/TargetSubtargetInfo.h"24#include "llvm/IR/BasicBlock.h"25#include "llvm/IR/Constants.h"26#include "llvm/IR/DebugInfoMetadata.h"27#include "llvm/IR/DerivedTypes.h"28#include "llvm/IR/Dominators.h"29#include "llvm/IR/EHPersonalities.h"30#include "llvm/IR/Function.h"31#include "llvm/IR/Instructions.h"32#include "llvm/IR/Module.h"33#include "llvm/IR/Type.h"34#include "llvm/InitializePasses.h"35#include "llvm/Pass.h"36#include "llvm/Support/Casting.h"37#include "llvm/Target/TargetMachine.h"38#include "llvm/TargetParser/Triple.h"39#include "llvm/Transforms/Utils/Local.h"40#include <cstddef>4142using namespace llvm;4344#define DEBUG_TYPE "dwarf-eh-prepare"4546STATISTIC(NumResumesLowered, "Number of resume calls lowered");47STATISTIC(NumCleanupLandingPadsUnreachable,48"Number of cleanup landing pads found unreachable");49STATISTIC(NumCleanupLandingPadsRemaining,50"Number of cleanup landing pads remaining");51STATISTIC(NumNoUnwind, "Number of functions with nounwind");52STATISTIC(NumUnwind, "Number of functions with unwind");5354namespace {5556class DwarfEHPrepare {57CodeGenOptLevel OptLevel;5859Function &F;60const TargetLowering &TLI;61DomTreeUpdater *DTU;62const TargetTransformInfo *TTI;63const Triple &TargetTriple;6465/// Return the exception object from the value passed into66/// the 'resume' instruction (typically an aggregate). Clean up any dead67/// instructions, including the 'resume' instruction.68Value *GetExceptionObject(ResumeInst *RI);6970/// Replace resumes that are not reachable from a cleanup landing pad with71/// unreachable and then simplify those blocks.72size_t73pruneUnreachableResumes(SmallVectorImpl<ResumeInst *> &Resumes,74SmallVectorImpl<LandingPadInst *> &CleanupLPads);7576/// Convert the ResumeInsts that are still present77/// into calls to the appropriate _Unwind_Resume function.78bool InsertUnwindResumeCalls();7980public:81DwarfEHPrepare(CodeGenOptLevel OptLevel_, Function &F_,82const TargetLowering &TLI_, DomTreeUpdater *DTU_,83const TargetTransformInfo *TTI_, const Triple &TargetTriple_)84: OptLevel(OptLevel_), F(F_), TLI(TLI_), DTU(DTU_), TTI(TTI_),85TargetTriple(TargetTriple_) {}8687bool run();88};8990} // namespace9192Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {93Value *V = RI->getOperand(0);94Value *ExnObj = nullptr;95InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);96LoadInst *SelLoad = nullptr;97InsertValueInst *ExcIVI = nullptr;98bool EraseIVIs = false;99100if (SelIVI) {101if (SelIVI->getNumIndices() == 1 && *SelIVI->idx_begin() == 1) {102ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));103if (ExcIVI && isa<UndefValue>(ExcIVI->getOperand(0)) &&104ExcIVI->getNumIndices() == 1 && *ExcIVI->idx_begin() == 0) {105ExnObj = ExcIVI->getOperand(1);106SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));107EraseIVIs = true;108}109}110}111112if (!ExnObj)113ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj",114RI->getIterator());115116RI->eraseFromParent();117118if (EraseIVIs) {119if (SelIVI->use_empty())120SelIVI->eraseFromParent();121if (ExcIVI->use_empty())122ExcIVI->eraseFromParent();123if (SelLoad && SelLoad->use_empty())124SelLoad->eraseFromParent();125}126127return ExnObj;128}129130size_t DwarfEHPrepare::pruneUnreachableResumes(131SmallVectorImpl<ResumeInst *> &Resumes,132SmallVectorImpl<LandingPadInst *> &CleanupLPads) {133assert(DTU && "Should have DomTreeUpdater here.");134135BitVector ResumeReachable(Resumes.size());136size_t ResumeIndex = 0;137for (auto *RI : Resumes) {138for (auto *LP : CleanupLPads) {139if (isPotentiallyReachable(LP, RI, nullptr, &DTU->getDomTree())) {140ResumeReachable.set(ResumeIndex);141break;142}143}144++ResumeIndex;145}146147// If everything is reachable, there is no change.148if (ResumeReachable.all())149return Resumes.size();150151LLVMContext &Ctx = F.getContext();152153// Otherwise, insert unreachable instructions and call simplifycfg.154size_t ResumesLeft = 0;155for (size_t I = 0, E = Resumes.size(); I < E; ++I) {156ResumeInst *RI = Resumes[I];157if (ResumeReachable[I]) {158Resumes[ResumesLeft++] = RI;159} else {160BasicBlock *BB = RI->getParent();161new UnreachableInst(Ctx, RI->getIterator());162RI->eraseFromParent();163simplifyCFG(BB, *TTI, DTU);164}165}166Resumes.resize(ResumesLeft);167return ResumesLeft;168}169170bool DwarfEHPrepare::InsertUnwindResumeCalls() {171SmallVector<ResumeInst *, 16> Resumes;172SmallVector<LandingPadInst *, 16> CleanupLPads;173if (F.doesNotThrow())174NumNoUnwind++;175else176NumUnwind++;177for (BasicBlock &BB : F) {178if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))179Resumes.push_back(RI);180if (auto *LP = BB.getLandingPadInst())181if (LP->isCleanup())182CleanupLPads.push_back(LP);183}184185NumCleanupLandingPadsRemaining += CleanupLPads.size();186187if (Resumes.empty())188return false;189190// Check the personality, don't do anything if it's scope-based.191EHPersonality Pers = classifyEHPersonality(F.getPersonalityFn());192if (isScopedEHPersonality(Pers))193return false;194195LLVMContext &Ctx = F.getContext();196197size_t ResumesLeft = Resumes.size();198if (OptLevel != CodeGenOptLevel::None) {199ResumesLeft = pruneUnreachableResumes(Resumes, CleanupLPads);200#if LLVM_ENABLE_STATS201unsigned NumRemainingLPs = 0;202for (BasicBlock &BB : F) {203if (auto *LP = BB.getLandingPadInst())204if (LP->isCleanup())205NumRemainingLPs++;206}207NumCleanupLandingPadsUnreachable += CleanupLPads.size() - NumRemainingLPs;208NumCleanupLandingPadsRemaining -= CleanupLPads.size() - NumRemainingLPs;209#endif210}211212if (ResumesLeft == 0)213return true; // We pruned them all.214215// RewindFunction - _Unwind_Resume or the target equivalent.216FunctionCallee RewindFunction;217CallingConv::ID RewindFunctionCallingConv;218FunctionType *FTy;219const char *RewindName;220bool DoesRewindFunctionNeedExceptionObject;221222if ((Pers == EHPersonality::GNU_CXX || Pers == EHPersonality::GNU_CXX_SjLj) &&223TargetTriple.isTargetEHABICompatible()) {224RewindName = TLI.getLibcallName(RTLIB::CXA_END_CLEANUP);225FTy = FunctionType::get(Type::getVoidTy(Ctx), false);226RewindFunctionCallingConv =227TLI.getLibcallCallingConv(RTLIB::CXA_END_CLEANUP);228DoesRewindFunctionNeedExceptionObject = false;229} else {230RewindName = TLI.getLibcallName(RTLIB::UNWIND_RESUME);231FTy = FunctionType::get(Type::getVoidTy(Ctx), PointerType::getUnqual(Ctx),232false);233RewindFunctionCallingConv = TLI.getLibcallCallingConv(RTLIB::UNWIND_RESUME);234DoesRewindFunctionNeedExceptionObject = true;235}236RewindFunction = F.getParent()->getOrInsertFunction(RewindName, FTy);237238// Create the basic block where the _Unwind_Resume call will live.239if (ResumesLeft == 1) {240// Instead of creating a new BB and PHI node, just append the call to241// _Unwind_Resume to the end of the single resume block.242ResumeInst *RI = Resumes.front();243BasicBlock *UnwindBB = RI->getParent();244Value *ExnObj = GetExceptionObject(RI);245llvm::SmallVector<Value *, 1> RewindFunctionArgs;246if (DoesRewindFunctionNeedExceptionObject)247RewindFunctionArgs.push_back(ExnObj);248249// Call the rewind function.250CallInst *CI =251CallInst::Create(RewindFunction, RewindFunctionArgs, "", UnwindBB);252// The verifier requires that all calls of debug-info-bearing functions253// from debug-info-bearing functions have a debug location (for inlining254// purposes). Assign a dummy location to satisfy the constraint.255Function *RewindFn = dyn_cast<Function>(RewindFunction.getCallee());256if (RewindFn && RewindFn->getSubprogram())257if (DISubprogram *SP = F.getSubprogram())258CI->setDebugLoc(DILocation::get(SP->getContext(), 0, 0, SP));259CI->setCallingConv(RewindFunctionCallingConv);260261// We never expect _Unwind_Resume to return.262CI->setDoesNotReturn();263new UnreachableInst(Ctx, UnwindBB);264return true;265}266267std::vector<DominatorTree::UpdateType> Updates;268Updates.reserve(Resumes.size());269270llvm::SmallVector<Value *, 1> RewindFunctionArgs;271272BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &F);273PHINode *PN = PHINode::Create(PointerType::getUnqual(Ctx), ResumesLeft,274"exn.obj", UnwindBB);275276// Extract the exception object from the ResumeInst and add it to the PHI node277// that feeds the _Unwind_Resume call.278for (ResumeInst *RI : Resumes) {279BasicBlock *Parent = RI->getParent();280BranchInst::Create(UnwindBB, Parent);281Updates.push_back({DominatorTree::Insert, Parent, UnwindBB});282283Value *ExnObj = GetExceptionObject(RI);284PN->addIncoming(ExnObj, Parent);285286++NumResumesLowered;287}288289if (DoesRewindFunctionNeedExceptionObject)290RewindFunctionArgs.push_back(PN);291292// Call the function.293CallInst *CI =294CallInst::Create(RewindFunction, RewindFunctionArgs, "", UnwindBB);295// The verifier requires that all calls of debug-info-bearing functions296// from debug-info-bearing functions have a debug location (for inlining297// purposes). Assign a dummy location to satisfy the constraint.298Function *RewindFn = dyn_cast<Function>(RewindFunction.getCallee());299if (RewindFn && RewindFn->getSubprogram())300if (DISubprogram *SP = F.getSubprogram())301CI->setDebugLoc(DILocation::get(SP->getContext(), 0, 0, SP));302CI->setCallingConv(RewindFunctionCallingConv);303304// We never expect _Unwind_Resume to return.305CI->setDoesNotReturn();306new UnreachableInst(Ctx, UnwindBB);307308if (DTU)309DTU->applyUpdates(Updates);310311return true;312}313314bool DwarfEHPrepare::run() {315bool Changed = InsertUnwindResumeCalls();316317return Changed;318}319320static bool prepareDwarfEH(CodeGenOptLevel OptLevel, Function &F,321const TargetLowering &TLI, DominatorTree *DT,322const TargetTransformInfo *TTI,323const Triple &TargetTriple) {324DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);325326return DwarfEHPrepare(OptLevel, F, TLI, DT ? &DTU : nullptr, TTI,327TargetTriple)328.run();329}330331namespace {332333class DwarfEHPrepareLegacyPass : public FunctionPass {334335CodeGenOptLevel OptLevel;336337public:338static char ID; // Pass identification, replacement for typeid.339340DwarfEHPrepareLegacyPass(CodeGenOptLevel OptLevel = CodeGenOptLevel::Default)341: FunctionPass(ID), OptLevel(OptLevel) {}342343bool runOnFunction(Function &F) override {344const TargetMachine &TM =345getAnalysis<TargetPassConfig>().getTM<TargetMachine>();346const TargetLowering &TLI = *TM.getSubtargetImpl(F)->getTargetLowering();347DominatorTree *DT = nullptr;348const TargetTransformInfo *TTI = nullptr;349if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())350DT = &DTWP->getDomTree();351if (OptLevel != CodeGenOptLevel::None) {352if (!DT)353DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();354TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);355}356return prepareDwarfEH(OptLevel, F, TLI, DT, TTI, TM.getTargetTriple());357}358359void getAnalysisUsage(AnalysisUsage &AU) const override {360AU.addRequired<TargetPassConfig>();361AU.addRequired<TargetTransformInfoWrapperPass>();362if (OptLevel != CodeGenOptLevel::None) {363AU.addRequired<DominatorTreeWrapperPass>();364AU.addRequired<TargetTransformInfoWrapperPass>();365}366AU.addPreserved<DominatorTreeWrapperPass>();367}368369StringRef getPassName() const override {370return "Exception handling preparation";371}372};373374} // end anonymous namespace375376PreservedAnalyses DwarfEHPreparePass::run(Function &F,377FunctionAnalysisManager &FAM) {378const auto &TLI = *TM->getSubtargetImpl(F)->getTargetLowering();379auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);380const TargetTransformInfo *TTI = nullptr;381auto OptLevel = TM->getOptLevel();382if (OptLevel != CodeGenOptLevel::None) {383if (!DT)384DT = &FAM.getResult<DominatorTreeAnalysis>(F);385TTI = &FAM.getResult<TargetIRAnalysis>(F);386}387bool Changed =388prepareDwarfEH(OptLevel, F, TLI, DT, TTI, TM->getTargetTriple());389390if (!Changed)391return PreservedAnalyses::all();392PreservedAnalyses PA;393PA.preserve<DominatorTreeAnalysis>();394return PA;395}396397char DwarfEHPrepareLegacyPass::ID = 0;398399INITIALIZE_PASS_BEGIN(DwarfEHPrepareLegacyPass, DEBUG_TYPE,400"Prepare DWARF exceptions", false, false)401INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)402INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)403INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)404INITIALIZE_PASS_END(DwarfEHPrepareLegacyPass, DEBUG_TYPE,405"Prepare DWARF exceptions", false, false)406407FunctionPass *llvm::createDwarfEHPass(CodeGenOptLevel OptLevel) {408return new DwarfEHPrepareLegacyPass(OptLevel);409}410411412