LibCallSemantics.cpp 3.4 KB

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