CallPromotionUtils.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /// Return true if the given indirect call site can be made to call \p Callee.
  26. ///
  27. /// This function ensures that the number and type of the call site's arguments
  28. /// and return value match those of the given function. If the types do not
  29. /// match exactly, they must at least be bitcast compatible. If \p FailureReason
  30. /// is non-null and the indirect call cannot be promoted, the failure reason
  31. /// will be stored in it.
  32. bool isLegalToPromote(const CallBase &CB, Function *Callee,
  33. const char **FailureReason = nullptr);
  34. /// Promote the given indirect call site to unconditionally call \p Callee.
  35. ///
  36. /// This function promotes the given call site, returning the direct call or
  37. /// invoke instruction. If the function type of the call site doesn't match that
  38. /// of the callee, bitcast instructions are inserted where appropriate. If \p
  39. /// RetBitCast is non-null, it will be used to store the return value bitcast,
  40. /// if created.
  41. CallBase &promoteCall(CallBase &CB, Function *Callee,
  42. CastInst **RetBitCast = nullptr);
  43. /// Promote the given indirect call site to conditionally call \p Callee.
  44. ///
  45. /// This function creates an if-then-else structure at the location of the call
  46. /// site. The original call site is moved into the "else" block. A clone of the
  47. /// indirect call site is promoted, placed in the "then" block, and returned. If
  48. /// \p BranchWeights is non-null, it will be used to set !prof metadata on the
  49. /// new conditional branch.
  50. CallBase &promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
  51. MDNode *BranchWeights = nullptr);
  52. /// Try to promote (devirtualize) a virtual call on an Alloca. Return true on
  53. /// success.
  54. ///
  55. /// Look for a pattern like:
  56. ///
  57. /// %o = alloca %class.Impl
  58. /// %1 = getelementptr %class.Impl, %class.Impl* %o, i64 0, i32 0, i32 0
  59. /// store i32 (...)** bitcast (i8** getelementptr inbounds
  60. /// ({ [3 x i8*] }, { [3 x i8*] }* @_ZTV4Impl, i64 0, inrange i32 0, i64 2)
  61. /// to i32 (...)**), i32 (...)*** %1
  62. /// %2 = getelementptr inbounds %class.Impl, %class.Impl* %o, i64 0, i32 0
  63. /// %3 = bitcast %class.Interface* %2 to void (%class.Interface*)***
  64. /// %vtable.i = load void (%class.Interface*)**, void (%class.Interface*)*** %3
  65. /// %4 = load void (%class.Interface*)*, void (%class.Interface*)** %vtable.i
  66. /// call void %4(%class.Interface* nonnull %2)
  67. ///
  68. /// @_ZTV4Impl = linkonce_odr dso_local unnamed_addr constant { [3 x i8*] }
  69. /// { [3 x i8*]
  70. /// [i8* null, i8* bitcast ({ i8*, i8*, i8* }* @_ZTI4Impl to i8*),
  71. /// i8* bitcast (void (%class.Impl*)* @_ZN4Impl3RunEv to i8*)] }
  72. ///
  73. bool tryPromoteCall(CallBase &CB);
  74. } // end namespace llvm
  75. #endif // LLVM_TRANSFORMS_UTILS_CALLPROMOTIONUTILS_H
  76. #ifdef __GNUC__
  77. #pragma GCC diagnostic pop
  78. #endif