Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H |
| 16 | #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 17 | |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/IR/Value.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | |
| 24 | namespace llvm { |
| 25 | class BasicBlock; |
| 26 | class Instruction; |
| 27 | class MDNode; |
| 28 | } // end namespace llvm |
| 29 | |
| 30 | namespace clang { |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 31 | class Attr; |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 32 | class ASTContext; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 33 | namespace CodeGen { |
| 34 | |
| 35 | /// \brief Attributes that may be specified on loops. |
| 36 | struct LoopAttributes { |
| 37 | explicit LoopAttributes(bool IsParallel = false); |
| 38 | void clear(); |
| 39 | |
| 40 | /// \brief Generate llvm.loop.parallel metadata for loads and stores. |
| 41 | bool IsParallel; |
| 42 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 43 | /// \brief State of loop vectorization or unrolling. |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame^] | 44 | enum LVEnableState { Unspecified, Enable, Disable, Full }; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 45 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 46 | /// \brief Value for llvm.loop.vectorize.enable metadata. |
| 47 | LVEnableState VectorizeEnable; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 48 | |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame^] | 49 | /// \brief Value for llvm.loop.unroll.* metadata (enable, disable, or full). |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 50 | LVEnableState UnrollEnable; |
| 51 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 52 | /// \brief Value for llvm.loop.vectorize.width metadata. |
| 53 | unsigned VectorizeWidth; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 54 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 55 | /// \brief Value for llvm.loop.interleave.count metadata. |
| 56 | unsigned InterleaveCount; |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 57 | |
| 58 | /// \brief llvm.unroll. |
| 59 | unsigned UnrollCount; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /// \brief Information used when generating a structured loop. |
| 63 | class LoopInfo { |
| 64 | public: |
| 65 | /// \brief Construct a new LoopInfo for the loop with entry Header. |
| 66 | LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs); |
| 67 | |
| 68 | /// \brief Get the loop id metadata for this loop. |
| 69 | llvm::MDNode *getLoopID() const { return LoopID; } |
| 70 | |
| 71 | /// \brief Get the header block of this loop. |
| 72 | llvm::BasicBlock *getHeader() const { return Header; } |
| 73 | |
| 74 | /// \brief Get the set of attributes active for this loop. |
| 75 | const LoopAttributes &getAttributes() const { return Attrs; } |
| 76 | |
| 77 | private: |
| 78 | /// \brief Loop ID metadata. |
| 79 | llvm::MDNode *LoopID; |
| 80 | /// \brief Header block of this loop. |
| 81 | llvm::BasicBlock *Header; |
| 82 | /// \brief The attributes for this loop. |
| 83 | LoopAttributes Attrs; |
| 84 | }; |
| 85 | |
| 86 | /// \brief A stack of loop information corresponding to loop nesting levels. |
| 87 | /// This stack can be used to prepare attributes which are applied when a loop |
| 88 | /// is emitted. |
| 89 | class LoopInfoStack { |
Aaron Ballman | abc1892 | 2015-02-15 22:54:08 +0000 | [diff] [blame] | 90 | LoopInfoStack(const LoopInfoStack &) = delete; |
| 91 | void operator=(const LoopInfoStack &) = delete; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 92 | |
| 93 | public: |
| 94 | LoopInfoStack() {} |
| 95 | |
| 96 | /// \brief Begin a new structured loop. The set of staged attributes will be |
| 97 | /// applied to the loop and then cleared. |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 98 | void push(llvm::BasicBlock *Header); |
| 99 | |
| 100 | /// \brief Begin a new structured loop. Stage attributes from the Attrs list. |
| 101 | /// The staged attributes are applied to the loop and then cleared. |
| 102 | void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx, |
| 103 | llvm::ArrayRef<const Attr *> Attrs); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 104 | |
| 105 | /// \brief End the current loop. |
| 106 | void pop(); |
| 107 | |
| 108 | /// \brief Return the top loop id metadata. |
| 109 | llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); } |
| 110 | |
| 111 | /// \brief Return true if the top loop is parallel. |
| 112 | bool getCurLoopParallel() const { |
| 113 | return hasInfo() ? getInfo().getAttributes().IsParallel : false; |
| 114 | } |
| 115 | |
| 116 | /// \brief Function called by the CodeGenFunction when an instruction is |
| 117 | /// created. |
| 118 | void InsertHelper(llvm::Instruction *I) const; |
| 119 | |
| 120 | /// \brief Set the next pushed loop as parallel. |
| 121 | void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; } |
| 122 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 123 | /// \brief Set the next pushed loop 'vectorize.enable' |
| 124 | void setVectorizeEnable(bool Enable = true) { |
| 125 | StagedAttrs.VectorizeEnable = |
| 126 | Enable ? LoopAttributes::Enable : LoopAttributes::Disable; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 129 | /// \brief Set the next pushed loop unroll state. |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame^] | 130 | void setUnrollState(const LoopAttributes::LVEnableState &State) { |
| 131 | StagedAttrs.UnrollEnable = State; |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 134 | /// \brief Set the vectorize width for the next loop pushed. |
| 135 | void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 136 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 137 | /// \brief Set the interleave count for the next loop pushed. |
| 138 | void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 139 | |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 140 | /// \brief Set the unroll count for the next loop pushed. |
| 141 | void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; } |
| 142 | |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 143 | private: |
| 144 | /// \brief Returns true if there is LoopInfo on the stack. |
| 145 | bool hasInfo() const { return !Active.empty(); } |
| 146 | /// \brief Return the LoopInfo for the current loop. HasInfo should be called |
| 147 | /// first to ensure LoopInfo is present. |
| 148 | const LoopInfo &getInfo() const { return Active.back(); } |
| 149 | /// \brief The set of attributes that will be applied to the next pushed loop. |
| 150 | LoopAttributes StagedAttrs; |
| 151 | /// \brief Stack of active loops. |
| 152 | llvm::SmallVector<LoopInfo, 4> Active; |
| 153 | }; |
| 154 | |
| 155 | } // end namespace CodeGen |
| 156 | } // end namespace clang |
| 157 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 158 | #endif |