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