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.
getMap(void * Ptr)24 static StringMap<const LibCallFunctionInfo*> *getMap(void *Ptr) {
25 return static_cast<StringMap<const LibCallFunctionInfo*> *>(Ptr);
26 }
27
~LibCallInfo()28 LibCallInfo::~LibCallInfo() {
29 delete getMap(Impl);
30 }
31
getLocationInfo(unsigned LocID) const32 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 *
getFunctionInfo(const Function * F) const45 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.
classifyEHPersonality(const Value * Pers)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
canSimplifyInvokeNoUnwind(const InvokeInst * II)83 bool llvm::canSimplifyInvokeNoUnwind(const InvokeInst *II) {
84 const LandingPadInst *LP = II->getLandingPadInst();
85 EHPersonality Personality = classifyEHPersonality(LP->getPersonalityFn());
86 // We can't simplify any invokes to nounwind functions if the personality
87 // function wants to catch asynch exceptions. The nounwind attribute only
88 // implies that the function does not throw synchronous exceptions.
89 return !isAsynchronousEHPersonality(Personality);
90 }
91