LoopVersioning.h 6.2 KB

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