Move the personality function from LandingPadInst to Function
[oota-llvm.git] / lib / Analysis / LibCallSemantics.cpp
1 //===- LibCallSemantics.cpp - Describe library semantics ------------------===//
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 // This file implements interfaces that can be used to describe language
11 // specific runtime library interfaces (e.g. libc, libm, etc) to LLVM
12 // optimizers.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/LibCallSemantics.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/IR/Function.h"
20 using namespace llvm;
21
22 /// This impl pointer in ~LibCallInfo is actually a StringMap.  This
23 /// helper does the cast.
24 static StringMap<const LibCallFunctionInfo*> *getMap(void *Ptr) {
25   return static_cast<StringMap<const LibCallFunctionInfo*> *>(Ptr);
26 }
27
28 LibCallInfo::~LibCallInfo() {
29   delete getMap(Impl);
30 }
31
32 const LibCallLocationInfo &LibCallInfo::getLocationInfo(unsigned LocID) const {
33   // Get location info on the first call.
34   if (NumLocations == 0)
35     NumLocations = getLocationInfo(Locations);
36   
37   assert(LocID < NumLocations && "Invalid location ID!");
38   return Locations[LocID];
39 }
40
41
42 /// Return the LibCallFunctionInfo object corresponding to
43 /// the specified function if we have it.  If not, return null.
44 const LibCallFunctionInfo *
45 LibCallInfo::getFunctionInfo(const Function *F) const {
46   StringMap<const LibCallFunctionInfo*> *Map = getMap(Impl);
47   
48   /// If this is the first time we are querying for this info, lazily construct
49   /// the StringMap to index it.
50   if (!Map) {
51     Impl = Map = new StringMap<const LibCallFunctionInfo*>();
52     
53     const LibCallFunctionInfo *Array = getFunctionInfoArray();
54     if (!Array) return nullptr;
55     
56     // We now have the array of entries.  Populate the StringMap.
57     for (unsigned i = 0; Array[i].Name; ++i)
58       (*Map)[Array[i].Name] = Array+i;
59   }
60   
61   // Look up this function in the string map.
62   return Map->lookup(F->getName());
63 }
64
65 /// See if the given exception handling personality function is one that we
66 /// understand.  If so, return a description of it; otherwise return Unknown.
67 EHPersonality llvm::classifyEHPersonality(const Value *Pers) {
68   const Function *F = dyn_cast<Function>(Pers->stripPointerCasts());
69   if (!F)
70     return EHPersonality::Unknown;
71   return StringSwitch<EHPersonality>(F->getName())
72     .Case("__gnat_eh_personality", EHPersonality::GNU_Ada)
73     .Case("__gxx_personality_v0",  EHPersonality::GNU_CXX)
74     .Case("__gcc_personality_v0",  EHPersonality::GNU_C)
75     .Case("__objc_personality_v0", EHPersonality::GNU_ObjC)
76     .Case("_except_handler3",      EHPersonality::MSVC_X86SEH)
77     .Case("_except_handler4",      EHPersonality::MSVC_X86SEH)
78     .Case("__C_specific_handler",  EHPersonality::MSVC_Win64SEH)
79     .Case("__CxxFrameHandler3",    EHPersonality::MSVC_CXX)
80     .Default(EHPersonality::Unknown);
81 }
82
83 bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
84   EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
85   // We can't simplify any invokes to nounwind functions if the personality
86   // function wants to catch asynch exceptions.  The nounwind attribute only
87   // implies that the function does not throw synchronous exceptions.
88   return !isAsynchronousEHPersonality(Personality);
89 }