CGLoopInfo.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- 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 is the internal state used for llvm translation for loop statement
  11. // metadata.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
  15. #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/IR/Value.h"
  20. #include "llvm/Support/Compiler.h"
  21. namespace llvm {
  22. class BasicBlock;
  23. class Instruction;
  24. class MDNode;
  25. } // end namespace llvm
  26. namespace clang {
  27. class Attr;
  28. namespace CodeGen {
  29. /// \brief Attributes that may be specified on loops.
  30. struct LoopAttributes {
  31. explicit LoopAttributes(bool IsParallel = false);
  32. void clear();
  33. /// \brief Generate llvm.loop.parallel metadata for loads and stores.
  34. bool IsParallel;
  35. /// \brief Values of llvm.loop.vectorize.enable metadata.
  36. enum LVEnableState { VecUnspecified, VecEnable, VecDisable };
  37. /// \brief llvm.loop.vectorize.enable
  38. LVEnableState VectorizerEnable;
  39. /// \brief llvm.loop.vectorize.width
  40. unsigned VectorizerWidth;
  41. /// \brief llvm.loop.interleave.count
  42. unsigned VectorizerUnroll;
  43. // HLSL Change Begins.
  44. /// \brief hlsl [loop] attribute
  45. bool HlslLoop;
  46. /// \brief hlsl [unroll] attribute
  47. unsigned HlslUnrollCount;
  48. // HLSL Change Ends.
  49. };
  50. /// \brief Information used when generating a structured loop.
  51. class LoopInfo {
  52. public:
  53. /// \brief Construct a new LoopInfo for the loop with entry Header.
  54. LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs);
  55. /// \brief Get the loop id metadata for this loop.
  56. llvm::MDNode *getLoopID() const { return LoopID; }
  57. /// \brief Get the header block of this loop.
  58. llvm::BasicBlock *getHeader() const { return Header; }
  59. /// \brief Get the set of attributes active for this loop.
  60. const LoopAttributes &getAttributes() const { return Attrs; }
  61. private:
  62. /// \brief Loop ID metadata.
  63. llvm::MDNode *LoopID;
  64. /// \brief Header block of this loop.
  65. llvm::BasicBlock *Header;
  66. /// \brief The attributes for this loop.
  67. LoopAttributes Attrs;
  68. };
  69. /// \brief A stack of loop information corresponding to loop nesting levels.
  70. /// This stack can be used to prepare attributes which are applied when a loop
  71. /// is emitted.
  72. class LoopInfoStack {
  73. LoopInfoStack(const LoopInfoStack &) = delete;
  74. void operator=(const LoopInfoStack &) = delete;
  75. public:
  76. LoopInfoStack() {}
  77. /// \brief Begin a new structured loop. The set of staged attributes will be
  78. /// applied to the loop and then cleared.
  79. void push(llvm::BasicBlock *Header,
  80. llvm::ArrayRef<const Attr *> Attrs = llvm::None);
  81. /// \brief End the current loop.
  82. void pop();
  83. /// \brief Return the top loop id metadata.
  84. llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
  85. /// \brief Return true if the top loop is parallel.
  86. bool getCurLoopParallel() const {
  87. return hasInfo() ? getInfo().getAttributes().IsParallel : false;
  88. }
  89. /// \brief Function called by the CodeGenFunction when an instruction is
  90. /// created.
  91. void InsertHelper(llvm::Instruction *I) const;
  92. /// \brief Set the next pushed loop as parallel.
  93. void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
  94. /// \brief Set the next pushed loop 'vectorizer.enable'
  95. void setVectorizerEnable(bool Enable = true) {
  96. StagedAttrs.VectorizerEnable =
  97. Enable ? LoopAttributes::VecEnable : LoopAttributes::VecDisable;
  98. }
  99. /// \brief Set the vectorizer width for the next loop pushed.
  100. void setVectorizerWidth(unsigned W) { StagedAttrs.VectorizerWidth = W; }
  101. /// \brief Set the vectorizer unroll for the next loop pushed.
  102. void setVectorizerUnroll(unsigned U) { StagedAttrs.VectorizerUnroll = U; }
  103. // HLSL Change Begins
  104. /// \brief Set the hlsl unroll count for the next loop pushed.
  105. void setHlslUnrollCount(unsigned U) { StagedAttrs.HlslUnrollCount = U; }
  106. /// \brief Set the hlsl loop for the next loop pushed.
  107. void setHlslLoop(bool Enable = true) { StagedAttrs.HlslLoop = Enable; }
  108. // HLSL Chagne Ends
  109. private:
  110. /// \brief Returns true if there is LoopInfo on the stack.
  111. bool hasInfo() const { return !Active.empty(); }
  112. /// \brief Return the LoopInfo for the current loop. HasInfo should be called
  113. /// first to ensure LoopInfo is present.
  114. const LoopInfo &getInfo() const { return Active.back(); }
  115. /// \brief The set of attributes that will be applied to the next pushed loop.
  116. LoopAttributes StagedAttrs;
  117. /// \brief Stack of active loops.
  118. llvm::SmallVector<LoopInfo, 4> Active;
  119. };
  120. } // end namespace CodeGen
  121. } // end namespace clang
  122. #endif