LoopVersioning.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LoopVersioning.h - Utility to version a loop -------------*- 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 defines a utility class to perform loop versioning. The versioned
  15. // loop speculates that otherwise may-aliasing memory accesses don't overlap and
  16. // emits checks to prove this.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
  20. #define LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
  21. #include "llvm/IR/PassManager.h"
  22. #include "llvm/Transforms/Utils/LoopUtils.h"
  23. #include "llvm/Transforms/Utils/ValueMapper.h"
  24. namespace llvm {
  25. class Loop;
  26. class SCEVPredicate;
  27. class ScalarEvolution;
  28. class LoopAccessInfo;
  29. class LoopInfo;
  30. struct RuntimeCheckingPtrGroup;
  31. typedef std::pair<const RuntimeCheckingPtrGroup *,
  32. const RuntimeCheckingPtrGroup *>
  33. RuntimePointerCheck;
  34. template <typename T> class ArrayRef;
  35. /// This class emits a version of the loop where run-time checks ensure
  36. /// that may-alias pointers can't overlap.
  37. ///
  38. /// It currently only supports single-exit loops and assumes that the loop
  39. /// already has a preheader.
  40. class LoopVersioning {
  41. public:
  42. /// Expects LoopAccessInfo, Loop, LoopInfo, DominatorTree as input.
  43. /// It uses runtime check provided by the user. If \p UseLAIChecks is true,
  44. /// we will retain the default checks made by LAI. Otherwise, construct an
  45. /// object having no checks and we expect the user to add them.
  46. LoopVersioning(const LoopAccessInfo &LAI,
  47. ArrayRef<RuntimePointerCheck> Checks, Loop *L, LoopInfo *LI,
  48. DominatorTree *DT, ScalarEvolution *SE);
  49. /// Performs the CFG manipulation part of versioning the loop including
  50. /// the DominatorTree and LoopInfo updates.
  51. ///
  52. /// The loop that was used to construct the class will be the "versioned" loop
  53. /// i.e. the loop that will receive control if all the memchecks pass.
  54. ///
  55. /// This allows the loop transform pass to operate on the same loop regardless
  56. /// of whether versioning was necessary or not:
  57. ///
  58. /// for each loop L:
  59. /// analyze L
  60. /// if versioning is necessary version L
  61. /// transform L
  62. void versionLoop() { versionLoop(findDefsUsedOutsideOfLoop(VersionedLoop)); }
  63. /// Same but if the client has already precomputed the set of values
  64. /// used outside the loop, this API will allows passing that.
  65. void versionLoop(const SmallVectorImpl<Instruction *> &DefsUsedOutside);
  66. /// Returns the versioned loop. Control flows here if pointers in the
  67. /// loop don't alias (i.e. all memchecks passed). (This loop is actually the
  68. /// same as the original loop that we got constructed with.)
  69. Loop *getVersionedLoop() { return VersionedLoop; }
  70. /// Returns the fall-back loop. Control flows here if pointers in the
  71. /// loop may alias (i.e. one of the memchecks failed).
  72. Loop *getNonVersionedLoop() { return NonVersionedLoop; }
  73. /// Annotate memory instructions in the versioned loop with no-alias
  74. /// metadata based on the memchecks issued.
  75. ///
  76. /// This is just wrapper that calls prepareNoAliasMetadata and
  77. /// annotateInstWithNoAlias on the instructions of the versioned loop.
  78. void annotateLoopWithNoAlias();
  79. /// Set up the aliasing scopes based on the memchecks. This needs to
  80. /// be called before the first call to annotateInstWithNoAlias.
  81. void prepareNoAliasMetadata();
  82. /// Add the noalias annotations to \p VersionedInst.
  83. ///
  84. /// \p OrigInst is the instruction corresponding to \p VersionedInst in the
  85. /// original loop. Initialize the aliasing scopes with
  86. /// prepareNoAliasMetadata once before this can be called.
  87. void annotateInstWithNoAlias(Instruction *VersionedInst,
  88. const Instruction *OrigInst);
  89. private:
  90. /// Adds the necessary PHI nodes for the versioned loops based on the
  91. /// loop-defined values used outside of the loop.
  92. ///
  93. /// This needs to be called after versionLoop if there are defs in the loop
  94. /// that are used outside the loop.
  95. void addPHINodes(const SmallVectorImpl<Instruction *> &DefsUsedOutside);
  96. /// Add the noalias annotations to \p I. Initialize the aliasing
  97. /// scopes with prepareNoAliasMetadata once before this can be called.
  98. void annotateInstWithNoAlias(Instruction *I) {
  99. annotateInstWithNoAlias(I, I);
  100. }
  101. /// The original loop. This becomes the "versioned" one. I.e.,
  102. /// control flows here if pointers in the loop don't alias.
  103. Loop *VersionedLoop;
  104. /// The fall-back loop. I.e. control flows here if pointers in the
  105. /// loop may alias (memchecks failed).
  106. Loop *NonVersionedLoop = nullptr;
  107. /// This maps the instructions from VersionedLoop to their counterpart
  108. /// in NonVersionedLoop.
  109. ValueToValueMapTy VMap;
  110. /// The set of alias checks that we are versioning for.
  111. SmallVector<RuntimePointerCheck, 4> AliasChecks;
  112. /// The set of SCEV checks that we are versioning for.
  113. const SCEVPredicate &Preds;
  114. /// Maps a pointer to the pointer checking group that the pointer
  115. /// belongs to.
  116. DenseMap<const Value *, const RuntimeCheckingPtrGroup *> PtrToGroup;
  117. /// The alias scope corresponding to a pointer checking group.
  118. DenseMap<const RuntimeCheckingPtrGroup *, MDNode *> GroupToScope;
  119. /// The list of alias scopes that a pointer checking group can't alias.
  120. DenseMap<const RuntimeCheckingPtrGroup *, MDNode *>
  121. GroupToNonAliasingScopeList;
  122. /// Analyses used.
  123. const LoopAccessInfo &LAI;
  124. LoopInfo *LI;
  125. DominatorTree *DT;
  126. ScalarEvolution *SE;
  127. };
  128. /// Expose LoopVersioning as a pass. Currently this is only used for
  129. /// unit-testing. It adds all memchecks necessary to remove all may-aliasing
  130. /// array accesses from the loop.
  131. class LoopVersioningPass : public PassInfoMixin<LoopVersioningPass> {
  132. public:
  133. PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
  134. };
  135. }
  136. #endif
  137. #ifdef __GNUC__
  138. #pragma GCC diagnostic pop
  139. #endif