Home | History | Annotate | Download | only in Analysis
      1 //===- EHPersonalities.cpp - Compute EH-related information ---------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "llvm/Analysis/EHPersonalities.h"
     11 #include "llvm/ADT/StringSwitch.h"
     12 #include "llvm/IR/CFG.h"
     13 #include "llvm/IR/Constants.h"
     14 #include "llvm/IR/Function.h"
     15 #include "llvm/IR/Instructions.h"
     16 #include "llvm/Support/Debug.h"
     17 #include "llvm/Support/raw_ostream.h"
     18 using namespace llvm;
     19 
     20 /// See if the given exception handling personality function is one that we
     21 /// understand.  If so, return a description of it; otherwise return Unknown.
     22 EHPersonality llvm::classifyEHPersonality(const Value *Pers) {
     23   const Function *F =
     24       Pers ? dyn_cast<Function>(Pers->stripPointerCasts()) : nullptr;
     25   if (!F)
     26     return EHPersonality::Unknown;
     27   return StringSwitch<EHPersonality>(F->getName())
     28     .Case("__gnat_eh_personality", EHPersonality::GNU_Ada)
     29     .Case("__gxx_personality_v0",  EHPersonality::GNU_CXX)
     30     .Case("__gcc_personality_v0",  EHPersonality::GNU_C)
     31     .Case("__objc_personality_v0", EHPersonality::GNU_ObjC)
     32     .Case("_except_handler3",      EHPersonality::MSVC_X86SEH)
     33     .Case("_except_handler4",      EHPersonality::MSVC_X86SEH)
     34     .Case("__C_specific_handler",  EHPersonality::MSVC_Win64SEH)
     35     .Case("__CxxFrameHandler3",    EHPersonality::MSVC_CXX)
     36     .Case("ProcessCLRException",   EHPersonality::CoreCLR)
     37     .Default(EHPersonality::Unknown);
     38 }
     39 
     40 bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
     41   EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
     42   // We can't simplify any invokes to nounwind functions if the personality
     43   // function wants to catch asynch exceptions.  The nounwind attribute only
     44   // implies that the function does not throw synchronous exceptions.
     45   return !isAsynchronousEHPersonality(Personality);
     46 }
     47 
     48 DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) {
     49   SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
     50   BasicBlock *EntryBlock = &F.getEntryBlock();
     51   DenseMap<BasicBlock *, ColorVector> BlockColors;
     52 
     53   // Build up the color map, which maps each block to its set of 'colors'.
     54   // For any block B the "colors" of B are the set of funclets F (possibly
     55   // including a root "funclet" representing the main function) such that
     56   // F will need to directly contain B or a copy of B (where the term "directly
     57   // contain" is used to distinguish from being "transitively contained" in
     58   // a nested funclet).
     59   //
     60   // Note: Despite not being a funclet in the truest sense, a catchswitch is
     61   // considered to belong to its own funclet for the purposes of coloring.
     62 
     63   DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
     64                                                   << F.getName() << "\n");
     65 
     66   Worklist.push_back({EntryBlock, EntryBlock});
     67 
     68   while (!Worklist.empty()) {
     69     BasicBlock *Visiting;
     70     BasicBlock *Color;
     71     std::tie(Visiting, Color) = Worklist.pop_back_val();
     72     DEBUG_WITH_TYPE("winehprepare-coloring",
     73                     dbgs() << "Visiting " << Visiting->getName() << ", "
     74                            << Color->getName() << "\n");
     75     Instruction *VisitingHead = Visiting->getFirstNonPHI();
     76     if (VisitingHead->isEHPad()) {
     77       // Mark this funclet head as a member of itself.
     78       Color = Visiting;
     79     }
     80     // Note that this is a member of the given color.
     81     ColorVector &Colors = BlockColors[Visiting];
     82     if (std::find(Colors.begin(), Colors.end(), Color) == Colors.end())
     83       Colors.push_back(Color);
     84     else
     85       continue;
     86 
     87     DEBUG_WITH_TYPE("winehprepare-coloring",
     88                     dbgs() << "  Assigned color \'" << Color->getName()
     89                            << "\' to block \'" << Visiting->getName()
     90                            << "\'.\n");
     91 
     92     BasicBlock *SuccColor = Color;
     93     TerminatorInst *Terminator = Visiting->getTerminator();
     94     if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
     95       Value *ParentPad = CatchRet->getParentPad();
     96       if (isa<ConstantTokenNone>(ParentPad))
     97         SuccColor = EntryBlock;
     98       else
     99         SuccColor = cast<Instruction>(ParentPad)->getParent();
    100     }
    101 
    102     for (BasicBlock *Succ : successors(Visiting))
    103       Worklist.push_back({Succ, SuccColor});
    104   }
    105   return BlockColors;
    106 }
    107