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