David Majnemer | 6f4583c | 2015-12-02 23:09:05 +0000 | [diff] [blame] | 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" |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 12 | #include "llvm/IR/CFG.h" |
| 13 | #include "llvm/IR/Constants.h" |
David Majnemer | 6f4583c | 2015-12-02 23:09:05 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Function.h" |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Instructions.h" |
| 16 | #include "llvm/Support/Debug.h" |
David Majnemer | f28c52f | 2015-12-12 05:53:20 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
David Majnemer | 6f4583c | 2015-12-02 23:09:05 +0000 | [diff] [blame] | 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) |
Reid Kleckner | fc7ba56 | 2017-05-31 22:35:52 +0000 | [diff] [blame] | 30 | .Case("__gxx_personality_seh0",EHPersonality::GNU_CXX) |
Saleem Abdulrasool | d2f705d | 2016-05-31 01:48:07 +0000 | [diff] [blame] | 31 | .Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj) |
David Majnemer | 6f4583c | 2015-12-02 23:09:05 +0000 | [diff] [blame] | 32 | .Case("__gcc_personality_v0", EHPersonality::GNU_C) |
Reid Kleckner | fc7ba56 | 2017-05-31 22:35:52 +0000 | [diff] [blame] | 33 | .Case("__gcc_personality_seh0",EHPersonality::GNU_C) |
Saleem Abdulrasool | d2f705d | 2016-05-31 01:48:07 +0000 | [diff] [blame] | 34 | .Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj) |
David Majnemer | 6f4583c | 2015-12-02 23:09:05 +0000 | [diff] [blame] | 35 | .Case("__objc_personality_v0", EHPersonality::GNU_ObjC) |
| 36 | .Case("_except_handler3", EHPersonality::MSVC_X86SEH) |
| 37 | .Case("_except_handler4", EHPersonality::MSVC_X86SEH) |
| 38 | .Case("__C_specific_handler", EHPersonality::MSVC_Win64SEH) |
| 39 | .Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX) |
| 40 | .Case("ProcessCLRException", EHPersonality::CoreCLR) |
Bjorn Steinbrink | 59fdec6 | 2016-03-15 20:35:45 +0000 | [diff] [blame] | 41 | .Case("rust_eh_personality", EHPersonality::Rust) |
David Majnemer | 6f4583c | 2015-12-02 23:09:05 +0000 | [diff] [blame] | 42 | .Default(EHPersonality::Unknown); |
| 43 | } |
| 44 | |
Kuba Brecka | ddfdba3 | 2016-11-14 21:41:13 +0000 | [diff] [blame] | 45 | StringRef llvm::getEHPersonalityName(EHPersonality Pers) { |
| 46 | switch (Pers) { |
| 47 | case EHPersonality::GNU_Ada: return "__gnat_eh_personality"; |
| 48 | case EHPersonality::GNU_CXX: return "__gxx_personality_v0"; |
| 49 | case EHPersonality::GNU_CXX_SjLj: return "__gxx_personality_sj0"; |
| 50 | case EHPersonality::GNU_C: return "__gcc_personality_v0"; |
| 51 | case EHPersonality::GNU_C_SjLj: return "__gcc_personality_sj0"; |
| 52 | case EHPersonality::GNU_ObjC: return "__objc_personality_v0"; |
| 53 | case EHPersonality::MSVC_X86SEH: return "_except_handler3"; |
| 54 | case EHPersonality::MSVC_Win64SEH: return "__C_specific_handler"; |
| 55 | case EHPersonality::MSVC_CXX: return "__CxxFrameHandler3"; |
| 56 | case EHPersonality::CoreCLR: return "ProcessCLRException"; |
| 57 | case EHPersonality::Rust: return "rust_eh_personality"; |
| 58 | case EHPersonality::Unknown: llvm_unreachable("Unknown EHPersonality!"); |
| 59 | } |
| 60 | |
| 61 | llvm_unreachable("Invalid EHPersonality!"); |
| 62 | } |
| 63 | |
| 64 | EHPersonality llvm::getDefaultEHPersonality(const Triple &T) { |
| 65 | return EHPersonality::GNU_C; |
| 66 | } |
| 67 | |
David Majnemer | 6f4583c | 2015-12-02 23:09:05 +0000 | [diff] [blame] | 68 | bool llvm::canSimplifyInvokeNoUnwind(const Function *F) { |
| 69 | EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn()); |
| 70 | // We can't simplify any invokes to nounwind functions if the personality |
| 71 | // function wants to catch asynch exceptions. The nounwind attribute only |
| 72 | // implies that the function does not throw synchronous exceptions. |
| 73 | return !isAsynchronousEHPersonality(Personality); |
| 74 | } |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 75 | |
| 76 | DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) { |
| 77 | SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist; |
| 78 | BasicBlock *EntryBlock = &F.getEntryBlock(); |
| 79 | DenseMap<BasicBlock *, ColorVector> BlockColors; |
| 80 | |
| 81 | // Build up the color map, which maps each block to its set of 'colors'. |
| 82 | // For any block B the "colors" of B are the set of funclets F (possibly |
| 83 | // including a root "funclet" representing the main function) such that |
| 84 | // F will need to directly contain B or a copy of B (where the term "directly |
| 85 | // contain" is used to distinguish from being "transitively contained" in |
| 86 | // a nested funclet). |
| 87 | // |
David Majnemer | bbfc721 | 2015-12-14 18:34:23 +0000 | [diff] [blame] | 88 | // Note: Despite not being a funclet in the truest sense, a catchswitch is |
| 89 | // considered to belong to its own funclet for the purposes of coloring. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 90 | |
| 91 | DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for " |
| 92 | << F.getName() << "\n"); |
| 93 | |
| 94 | Worklist.push_back({EntryBlock, EntryBlock}); |
| 95 | |
| 96 | while (!Worklist.empty()) { |
| 97 | BasicBlock *Visiting; |
| 98 | BasicBlock *Color; |
| 99 | std::tie(Visiting, Color) = Worklist.pop_back_val(); |
| 100 | DEBUG_WITH_TYPE("winehprepare-coloring", |
| 101 | dbgs() << "Visiting " << Visiting->getName() << ", " |
| 102 | << Color->getName() << "\n"); |
| 103 | Instruction *VisitingHead = Visiting->getFirstNonPHI(); |
| 104 | if (VisitingHead->isEHPad()) { |
| 105 | // Mark this funclet head as a member of itself. |
| 106 | Color = Visiting; |
| 107 | } |
| 108 | // Note that this is a member of the given color. |
| 109 | ColorVector &Colors = BlockColors[Visiting]; |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 110 | if (!is_contained(Colors, Color)) |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 111 | Colors.push_back(Color); |
| 112 | else |
| 113 | continue; |
| 114 | |
| 115 | DEBUG_WITH_TYPE("winehprepare-coloring", |
| 116 | dbgs() << " Assigned color \'" << Color->getName() |
| 117 | << "\' to block \'" << Visiting->getName() |
| 118 | << "\'.\n"); |
| 119 | |
| 120 | BasicBlock *SuccColor = Color; |
| 121 | TerminatorInst *Terminator = Visiting->getTerminator(); |
| 122 | if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) { |
Joseph Tremoulet | 44b3f96 | 2016-01-15 21:16:19 +0000 | [diff] [blame] | 123 | Value *ParentPad = CatchRet->getCatchSwitchParentPad(); |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 124 | if (isa<ConstantTokenNone>(ParentPad)) |
| 125 | SuccColor = EntryBlock; |
| 126 | else |
| 127 | SuccColor = cast<Instruction>(ParentPad)->getParent(); |
| 128 | } |
| 129 | |
| 130 | for (BasicBlock *Succ : successors(Visiting)) |
| 131 | Worklist.push_back({Succ, SuccColor}); |
| 132 | } |
| 133 | return BlockColors; |
| 134 | } |