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 .Case("rust_eh_personality", EHPersonality::Rust) 38 .Default(EHPersonality::Unknown); 39 } 40 41 bool llvm::canSimplifyInvokeNoUnwind(const Function *F) { 42 EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn()); 43 // We can't simplify any invokes to nounwind functions if the personality 44 // function wants to catch asynch exceptions. The nounwind attribute only 45 // implies that the function does not throw synchronous exceptions. 46 return !isAsynchronousEHPersonality(Personality); 47 } 48 49 DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) { 50 SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist; 51 BasicBlock *EntryBlock = &F.getEntryBlock(); 52 DenseMap<BasicBlock *, ColorVector> BlockColors; 53 54 // Build up the color map, which maps each block to its set of 'colors'. 55 // For any block B the "colors" of B are the set of funclets F (possibly 56 // including a root "funclet" representing the main function) such that 57 // F will need to directly contain B or a copy of B (where the term "directly 58 // contain" is used to distinguish from being "transitively contained" in 59 // a nested funclet). 60 // 61 // Note: Despite not being a funclet in the truest sense, a catchswitch is 62 // considered to belong to its own funclet for the purposes of coloring. 63 64 DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for " 65 << F.getName() << "\n"); 66 67 Worklist.push_back({EntryBlock, EntryBlock}); 68 69 while (!Worklist.empty()) { 70 BasicBlock *Visiting; 71 BasicBlock *Color; 72 std::tie(Visiting, Color) = Worklist.pop_back_val(); 73 DEBUG_WITH_TYPE("winehprepare-coloring", 74 dbgs() << "Visiting " << Visiting->getName() << ", " 75 << Color->getName() << "\n"); 76 Instruction *VisitingHead = Visiting->getFirstNonPHI(); 77 if (VisitingHead->isEHPad()) { 78 // Mark this funclet head as a member of itself. 79 Color = Visiting; 80 } 81 // Note that this is a member of the given color. 82 ColorVector &Colors = BlockColors[Visiting]; 83 if (std::find(Colors.begin(), Colors.end(), Color) == Colors.end()) 84 Colors.push_back(Color); 85 else 86 continue; 87 88 DEBUG_WITH_TYPE("winehprepare-coloring", 89 dbgs() << " Assigned color \'" << Color->getName() 90 << "\' to block \'" << Visiting->getName() 91 << "\'.\n"); 92 93 BasicBlock *SuccColor = Color; 94 TerminatorInst *Terminator = Visiting->getTerminator(); 95 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) { 96 Value *ParentPad = CatchRet->getCatchSwitchParentPad(); 97 if (isa<ConstantTokenNone>(ParentPad)) 98 SuccColor = EntryBlock; 99 else 100 SuccColor = cast<Instruction>(ParentPad)->getParent(); 101 } 102 103 for (BasicBlock *Succ : successors(Visiting)) 104 Worklist.push_back({Succ, SuccColor}); 105 } 106 return BlockColors; 107 } 108