CallPromotionUtils.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CallPromotionUtils.h - Utilities for call promotion ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares utilities useful for promoting indirect call sites to
  15. // direct call sites.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
  19. #define LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
  20. namespace llvm {
  21. class CallBase;
  22. class CastInst;
  23. class Function;
  24. class MDNode;
  25. class Value;
  26. /// Return true if the given indirect call site can be made to call \p Callee.
  27. ///
  28. /// This function ensures that the number and type of the call site's arguments
  29. /// and return value match those of the given function. If the types do not
  30. /// match exactly, they must at least be bitcast compatible. If \p FailureReason
  31. /// is non-null and the indirect call cannot be promoted, the failure reason
  32. /// will be stored in it.
  33. bool isLegalToPromote(const CallBase &CB, Function *Callee,
  34. const char **FailureReason = nullptr);
  35. /// Promote the given indirect call site to unconditionally call \p Callee.
  36. ///
  37. /// This function promotes the given call site, returning the direct call or
  38. /// invoke instruction. If the function type of the call site doesn't match that
  39. /// of the callee, bitcast instructions are inserted where appropriate. If \p
  40. /// RetBitCast is non-null, it will be used to store the return value bitcast,
  41. /// if created.
  42. CallBase &promoteCall(CallBase &CB, Function *Callee,
  43. CastInst **RetBitCast = nullptr);
  44. /// Promote the given indirect call site to conditionally call \p Callee.
  45. ///
  46. /// This function creates an if-then-else structure at the location of the call
  47. /// site. The original call site is moved into the "else" block. A clone of the
  48. /// indirect call site is promoted, placed in the "then" block, and returned. If
  49. /// \p BranchWeights is non-null, it will be used to set !prof metadata on the
  50. /// new conditional branch.
  51. CallBase &promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
  52. MDNode *BranchWeights = nullptr);
  53. /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on
  54. /// success.
  55. ///
  56. /// Look for a pattern like:
  57. ///
  58. /// %o = alloca %class.Impl
  59. /// %1 = getelementptr %class.Impl, %class.Impl* %o, i64 0, i32 0, i32 0
  60. /// store i32 (...)** bitcast (i8** getelementptr inbounds
  61. /// ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV4Impl, i64 0, inrange i32 0, i64 2)
  62. /// to i32 (...)**), i32 (...)*** %1
  63. /// %2 = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 0
  64. /// %3 = bitcast %class.Interface* %2 to void (%class.Interface*)***
  65. /// %vtable.i = load void (%class.Interface*)**, void (%class.Interface*)*** %3
  66. /// %4 = load void (%class.Interface*)*, void (%class.Interface*)** %vtable.i
  67. /// call void %4(%class.Interface* nonnull %2)
  68. ///
  69. /// @_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] }
  70. /// { [3 x i8*]
  71. /// [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI4Impl to i8*),
  72. /// i8* bitcast (void (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] }
  73. ///
  74. bool tryPromoteCall(CallBase &CB);
  75. /// Predicate and clone the given call site.
  76. ///
  77. /// This function creates an if-then-else structure at the location of the call
  78. /// site. The "if" condition compares the call site's called value to the given
  79. /// callee. The original call site is moved into the "else" block, and a clone
  80. /// of the call site is placed in the "then" block. The cloned instruction is
  81. /// returned.
  82. CallBase &versionCallSite(CallBase &CB, Value *Callee, MDNode *BranchWeights);
  83. } // end namespace llvm
  84. #endif // LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
  85. #ifdef __GNUC__
  86. #pragma GCC diagnostic pop
  87. #endif