xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Analysis/EHPersonalities.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===- EHPersonalities.cpp - Compute EH-related information ---------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "llvm/Analysis/EHPersonalities.h"
107330f729Sjoerg #include "llvm/ADT/StringSwitch.h"
117330f729Sjoerg #include "llvm/IR/CFG.h"
127330f729Sjoerg #include "llvm/IR/Constants.h"
137330f729Sjoerg #include "llvm/IR/Function.h"
147330f729Sjoerg #include "llvm/IR/Instructions.h"
157330f729Sjoerg #include "llvm/Support/Debug.h"
167330f729Sjoerg #include "llvm/Support/raw_ostream.h"
177330f729Sjoerg using namespace llvm;
187330f729Sjoerg 
197330f729Sjoerg /// See if the given exception handling personality function is one that we
207330f729Sjoerg /// understand.  If so, return a description of it; otherwise return Unknown.
classifyEHPersonality(const Value * Pers)217330f729Sjoerg EHPersonality llvm::classifyEHPersonality(const Value *Pers) {
22*82d56013Sjoerg   const GlobalValue *F =
23*82d56013Sjoerg       Pers ? dyn_cast<GlobalValue>(Pers->stripPointerCasts()) : nullptr;
24*82d56013Sjoerg   if (!F || !F->getValueType() || !F->getValueType()->isFunctionTy())
257330f729Sjoerg     return EHPersonality::Unknown;
267330f729Sjoerg   return StringSwitch<EHPersonality>(F->getName())
277330f729Sjoerg       .Case("__gnat_eh_personality", EHPersonality::GNU_Ada)
287330f729Sjoerg       .Case("__gxx_personality_v0", EHPersonality::GNU_CXX)
297330f729Sjoerg       .Case("__gxx_personality_seh0", EHPersonality::GNU_CXX)
307330f729Sjoerg       .Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj)
317330f729Sjoerg       .Case("__gcc_personality_v0", EHPersonality::GNU_C)
327330f729Sjoerg       .Case("__gcc_personality_seh0", EHPersonality::GNU_C)
337330f729Sjoerg       .Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj)
347330f729Sjoerg       .Case("__objc_personality_v0", EHPersonality::GNU_ObjC)
357330f729Sjoerg       .Case("_except_handler3", EHPersonality::MSVC_X86SEH)
367330f729Sjoerg       .Case("_except_handler4", EHPersonality::MSVC_X86SEH)
37*82d56013Sjoerg       .Case("__C_specific_handler", EHPersonality::MSVC_TableSEH)
387330f729Sjoerg       .Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX)
397330f729Sjoerg       .Case("ProcessCLRException", EHPersonality::CoreCLR)
407330f729Sjoerg       .Case("rust_eh_personality", EHPersonality::Rust)
417330f729Sjoerg       .Case("__gxx_wasm_personality_v0", EHPersonality::Wasm_CXX)
42*82d56013Sjoerg       .Case("__xlcxx_personality_v1", EHPersonality::XL_CXX)
437330f729Sjoerg       .Default(EHPersonality::Unknown);
447330f729Sjoerg }
457330f729Sjoerg 
getEHPersonalityName(EHPersonality Pers)467330f729Sjoerg StringRef llvm::getEHPersonalityName(EHPersonality Pers) {
477330f729Sjoerg   switch (Pers) {
487330f729Sjoerg   case EHPersonality::GNU_Ada:       return "__gnat_eh_personality";
497330f729Sjoerg   case EHPersonality::GNU_CXX:       return "__gxx_personality_v0";
507330f729Sjoerg   case EHPersonality::GNU_CXX_SjLj:  return "__gxx_personality_sj0";
517330f729Sjoerg   case EHPersonality::GNU_C:         return "__gcc_personality_v0";
527330f729Sjoerg   case EHPersonality::GNU_C_SjLj:    return "__gcc_personality_sj0";
537330f729Sjoerg   case EHPersonality::GNU_ObjC:      return "__objc_personality_v0";
547330f729Sjoerg   case EHPersonality::MSVC_X86SEH:   return "_except_handler3";
55*82d56013Sjoerg   case EHPersonality::MSVC_TableSEH:
56*82d56013Sjoerg     return "__C_specific_handler";
577330f729Sjoerg   case EHPersonality::MSVC_CXX:      return "__CxxFrameHandler3";
587330f729Sjoerg   case EHPersonality::CoreCLR:       return "ProcessCLRException";
597330f729Sjoerg   case EHPersonality::Rust:          return "rust_eh_personality";
607330f729Sjoerg   case EHPersonality::Wasm_CXX:      return "__gxx_wasm_personality_v0";
61*82d56013Sjoerg   case EHPersonality::XL_CXX:
62*82d56013Sjoerg     return "__xlcxx_personality_v1";
637330f729Sjoerg   case EHPersonality::Unknown:       llvm_unreachable("Unknown EHPersonality!");
647330f729Sjoerg   }
657330f729Sjoerg 
667330f729Sjoerg   llvm_unreachable("Invalid EHPersonality!");
677330f729Sjoerg }
687330f729Sjoerg 
getDefaultEHPersonality(const Triple & T)697330f729Sjoerg EHPersonality llvm::getDefaultEHPersonality(const Triple &T) {
707330f729Sjoerg   return EHPersonality::GNU_C;
717330f729Sjoerg }
727330f729Sjoerg 
canSimplifyInvokeNoUnwind(const Function * F)737330f729Sjoerg bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
747330f729Sjoerg   EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
757330f729Sjoerg   // We can't simplify any invokes to nounwind functions if the personality
767330f729Sjoerg   // function wants to catch asynch exceptions.  The nounwind attribute only
777330f729Sjoerg   // implies that the function does not throw synchronous exceptions.
787330f729Sjoerg   return !isAsynchronousEHPersonality(Personality);
797330f729Sjoerg }
807330f729Sjoerg 
colorEHFunclets(Function & F)817330f729Sjoerg DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) {
827330f729Sjoerg   SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
837330f729Sjoerg   BasicBlock *EntryBlock = &F.getEntryBlock();
847330f729Sjoerg   DenseMap<BasicBlock *, ColorVector> BlockColors;
857330f729Sjoerg 
867330f729Sjoerg   // Build up the color map, which maps each block to its set of 'colors'.
877330f729Sjoerg   // For any block B the "colors" of B are the set of funclets F (possibly
887330f729Sjoerg   // including a root "funclet" representing the main function) such that
897330f729Sjoerg   // F will need to directly contain B or a copy of B (where the term "directly
907330f729Sjoerg   // contain" is used to distinguish from being "transitively contained" in
917330f729Sjoerg   // a nested funclet).
927330f729Sjoerg   //
937330f729Sjoerg   // Note: Despite not being a funclet in the truest sense, a catchswitch is
947330f729Sjoerg   // considered to belong to its own funclet for the purposes of coloring.
957330f729Sjoerg 
967330f729Sjoerg   DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
977330f729Sjoerg                                                   << F.getName() << "\n");
987330f729Sjoerg 
997330f729Sjoerg   Worklist.push_back({EntryBlock, EntryBlock});
1007330f729Sjoerg 
1017330f729Sjoerg   while (!Worklist.empty()) {
1027330f729Sjoerg     BasicBlock *Visiting;
1037330f729Sjoerg     BasicBlock *Color;
1047330f729Sjoerg     std::tie(Visiting, Color) = Worklist.pop_back_val();
1057330f729Sjoerg     DEBUG_WITH_TYPE("winehprepare-coloring",
1067330f729Sjoerg                     dbgs() << "Visiting " << Visiting->getName() << ", "
1077330f729Sjoerg                            << Color->getName() << "\n");
1087330f729Sjoerg     Instruction *VisitingHead = Visiting->getFirstNonPHI();
1097330f729Sjoerg     if (VisitingHead->isEHPad()) {
1107330f729Sjoerg       // Mark this funclet head as a member of itself.
1117330f729Sjoerg       Color = Visiting;
1127330f729Sjoerg     }
1137330f729Sjoerg     // Note that this is a member of the given color.
1147330f729Sjoerg     ColorVector &Colors = BlockColors[Visiting];
1157330f729Sjoerg     if (!is_contained(Colors, Color))
1167330f729Sjoerg       Colors.push_back(Color);
1177330f729Sjoerg     else
1187330f729Sjoerg       continue;
1197330f729Sjoerg 
1207330f729Sjoerg     DEBUG_WITH_TYPE("winehprepare-coloring",
1217330f729Sjoerg                     dbgs() << "  Assigned color \'" << Color->getName()
1227330f729Sjoerg                            << "\' to block \'" << Visiting->getName()
1237330f729Sjoerg                            << "\'.\n");
1247330f729Sjoerg 
1257330f729Sjoerg     BasicBlock *SuccColor = Color;
1267330f729Sjoerg     Instruction *Terminator = Visiting->getTerminator();
1277330f729Sjoerg     if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
1287330f729Sjoerg       Value *ParentPad = CatchRet->getCatchSwitchParentPad();
1297330f729Sjoerg       if (isa<ConstantTokenNone>(ParentPad))
1307330f729Sjoerg         SuccColor = EntryBlock;
1317330f729Sjoerg       else
1327330f729Sjoerg         SuccColor = cast<Instruction>(ParentPad)->getParent();
1337330f729Sjoerg     }
1347330f729Sjoerg 
1357330f729Sjoerg     for (BasicBlock *Succ : successors(Visiting))
1367330f729Sjoerg       Worklist.push_back({Succ, SuccColor});
1377330f729Sjoerg   }
1387330f729Sjoerg   return BlockColors;
1397330f729Sjoerg }
140