LoopVersioning.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //===- LoopVersioning.h - Utility to version a loop -------------*- C++ -*-===//
  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 defines a utility class to perform loop versioning. The versioned
  11. // loop speculates that otherwise may-aliasing memory accesses don't overlap and
  12. // emits checks to prove this.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
  16. #define LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
  17. #include "llvm/Transforms/Utils/ValueMapper.h"
  18. namespace llvm {
  19. class Loop;
  20. class LoopAccessInfo;
  21. class LoopInfo;
  22. /// \brief This class emits a version of the loop where run-time checks ensure
  23. /// that may-alias pointers can't overlap.
  24. ///
  25. /// It currently only supports single-exit loops and assumes that the loop
  26. /// already has a preheader.
  27. class LoopVersioning {
  28. public:
  29. LoopVersioning(const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI,
  30. DominatorTree *DT,
  31. const SmallVector<int, 8> *PtrToPartition = nullptr);
  32. /// \brief Returns true if we need memchecks to disambiguate may-aliasing
  33. /// accesses.
  34. bool needsRuntimeChecks() const;
  35. /// \brief Performs the CFG manipulation part of versioning the loop including
  36. /// the DominatorTree and LoopInfo updates.
  37. ///
  38. /// The loop that was used to construct the class will be the "versioned" loop
  39. /// i.e. the loop that will receive control if all the memchecks pass.
  40. ///
  41. /// This allows the loop transform pass to operate on the same loop regardless
  42. /// of whether versioning was necessary or not:
  43. ///
  44. /// for each loop L:
  45. /// analyze L
  46. /// if versioning is necessary version L
  47. /// transform L
  48. void versionLoop(Pass *P);
  49. /// \brief Adds the necessary PHI nodes for the versioned loops based on the
  50. /// loop-defined values used outside of the loop.
  51. ///
  52. /// This needs to be called after versionLoop if there are defs in the loop
  53. /// that are used outside the loop. FIXME: this should be invoked internally
  54. /// by versionLoop and made private.
  55. void addPHINodes(const SmallVectorImpl<Instruction *> &DefsUsedOutside);
  56. /// \brief Returns the versioned loop. Control flows here if pointers in the
  57. /// loop don't alias (i.e. all memchecks passed). (This loop is actually the
  58. /// same as the original loop that we got constructed with.)
  59. Loop *getVersionedLoop() { return VersionedLoop; }
  60. /// \brief Returns the fall-back loop. Control flows here if pointers in the
  61. /// loop may alias (i.e. one of the memchecks failed).
  62. Loop *getNonVersionedLoop() { return NonVersionedLoop; }
  63. private:
  64. /// \brief The original loop. This becomes the "versioned" one. I.e.,
  65. /// control flows here if pointers in the loop don't alias.
  66. Loop *VersionedLoop;
  67. /// \brief The fall-back loop. I.e. control flows here if pointers in the
  68. /// loop may alias (memchecks failed).
  69. Loop *NonVersionedLoop;
  70. /// \brief For each memory pointer it contains the partitionId it is used in.
  71. /// If nullptr, no partitioning is used.
  72. ///
  73. /// The I-th entry corresponds to I-th entry in LAI.getRuntimePointerCheck().
  74. /// If the pointer is used in multiple partitions the entry is set to -1.
  75. const SmallVector<int, 8> *PtrToPartition;
  76. /// \brief This maps the instructions from VersionedLoop to their counterpart
  77. /// in NonVersionedLoop.
  78. ValueToValueMapTy VMap;
  79. /// \brief Analyses used.
  80. const LoopAccessInfo &LAI;
  81. LoopInfo *LI;
  82. DominatorTree *DT;
  83. };
  84. }
  85. #endif