Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1 | //===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- C++ -*---------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This is the internal state used for llvm translation for loop statement |
| 10 | // metadata. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 16 | |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Hal Finkel | c07e19b | 2016-05-25 21:53:24 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DebugLoc.h" |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Value.h" |
| 21 | #include "llvm/Support/Compiler.h" |
| 22 | |
| 23 | namespace llvm { |
| 24 | class BasicBlock; |
| 25 | class Instruction; |
| 26 | class MDNode; |
| 27 | } // end namespace llvm |
| 28 | |
| 29 | namespace clang { |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 30 | class Attr; |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 31 | class ASTContext; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 32 | namespace CodeGen { |
| 33 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 34 | /// Attributes that may be specified on loops. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 35 | struct LoopAttributes { |
| 36 | explicit LoopAttributes(bool IsParallel = false); |
| 37 | void clear(); |
| 38 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 39 | /// Generate llvm.loop.parallel metadata for loads and stores. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 40 | bool IsParallel; |
| 41 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 42 | /// State of loop vectorization or unrolling. |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 43 | enum LVEnableState { Unspecified, Enable, Disable, Full }; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 44 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 45 | /// Value for llvm.loop.vectorize.enable metadata. |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 46 | LVEnableState VectorizeEnable; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 47 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 48 | /// Value for llvm.loop.unroll.* metadata (enable, disable, or full). |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 49 | LVEnableState UnrollEnable; |
| 50 | |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 51 | /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full). |
| 52 | LVEnableState UnrollAndJamEnable; |
| 53 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 54 | /// Value for llvm.loop.vectorize.width metadata. |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 55 | unsigned VectorizeWidth; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 56 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 57 | /// Value for llvm.loop.interleave.count metadata. |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 58 | unsigned InterleaveCount; |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 59 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 60 | /// llvm.unroll. |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 61 | unsigned UnrollCount; |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 62 | |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 63 | /// llvm.unroll. |
| 64 | unsigned UnrollAndJamCount; |
| 65 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 66 | /// Value for llvm.loop.distribute.enable metadata. |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 67 | LVEnableState DistributeEnable; |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 68 | |
| 69 | /// Value for llvm.loop.pipeline.disable metadata. |
| 70 | bool PipelineDisabled; |
| 71 | |
| 72 | /// Value for llvm.loop.pipeline.iicount metadata. |
| 73 | unsigned PipelineInitiationInterval; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 76 | /// Information used when generating a structured loop. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 77 | class LoopInfo { |
| 78 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 79 | /// Construct a new LoopInfo for the loop with entry Header. |
Hal Finkel | c07e19b | 2016-05-25 21:53:24 +0000 | [diff] [blame] | 80 | LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs, |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 81 | const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 82 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 83 | /// Get the loop id metadata for this loop. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 84 | llvm::MDNode *getLoopID() const { return LoopID; } |
| 85 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 86 | /// Get the header block of this loop. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 87 | llvm::BasicBlock *getHeader() const { return Header; } |
| 88 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 89 | /// Get the set of attributes active for this loop. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 90 | const LoopAttributes &getAttributes() const { return Attrs; } |
| 91 | |
Michael Kruse | 0535137 | 2018-12-20 21:24:54 +0000 | [diff] [blame] | 92 | /// Return this loop's access group or nullptr if it does not have one. |
| 93 | llvm::MDNode *getAccessGroup() const { return AccGroup; } |
| 94 | |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 95 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 96 | /// Loop ID metadata. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 97 | llvm::MDNode *LoopID; |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 98 | /// Header block of this loop. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 99 | llvm::BasicBlock *Header; |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 100 | /// The attributes for this loop. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 101 | LoopAttributes Attrs; |
Michael Kruse | 0535137 | 2018-12-20 21:24:54 +0000 | [diff] [blame] | 102 | /// The access group for memory accesses parallel to this loop. |
| 103 | llvm::MDNode *AccGroup = nullptr; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 106 | /// A stack of loop information corresponding to loop nesting levels. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 107 | /// This stack can be used to prepare attributes which are applied when a loop |
| 108 | /// is emitted. |
| 109 | class LoopInfoStack { |
Aaron Ballman | abc1892 | 2015-02-15 22:54:08 +0000 | [diff] [blame] | 110 | LoopInfoStack(const LoopInfoStack &) = delete; |
| 111 | void operator=(const LoopInfoStack &) = delete; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 112 | |
| 113 | public: |
| 114 | LoopInfoStack() {} |
| 115 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 116 | /// Begin a new structured loop. The set of staged attributes will be |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 117 | /// applied to the loop and then cleared. |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 118 | void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc, |
| 119 | const llvm::DebugLoc &EndLoc); |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 120 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 121 | /// Begin a new structured loop. Stage attributes from the Attrs list. |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 122 | /// The staged attributes are applied to the loop and then cleared. |
| 123 | void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx, |
Benjamin Kramer | 81cb4b7 | 2016-11-24 16:01:20 +0000 | [diff] [blame] | 124 | llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc, |
| 125 | const llvm::DebugLoc &EndLoc); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 126 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 127 | /// End the current loop. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 128 | void pop(); |
| 129 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 130 | /// Return the top loop id metadata. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 131 | llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); } |
| 132 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 133 | /// Return true if the top loop is parallel. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 134 | bool getCurLoopParallel() const { |
| 135 | return hasInfo() ? getInfo().getAttributes().IsParallel : false; |
| 136 | } |
| 137 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 138 | /// Function called by the CodeGenFunction when an instruction is |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 139 | /// created. |
| 140 | void InsertHelper(llvm::Instruction *I) const; |
| 141 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 142 | /// Set the next pushed loop as parallel. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 143 | void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; } |
| 144 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 145 | /// Set the next pushed loop 'vectorize.enable' |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 146 | void setVectorizeEnable(bool Enable = true) { |
| 147 | StagedAttrs.VectorizeEnable = |
| 148 | Enable ? LoopAttributes::Enable : LoopAttributes::Disable; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 151 | /// Set the next pushed loop as a distribution candidate. |
Adam Nemet | 2de463e | 2016-06-14 12:04:26 +0000 | [diff] [blame] | 152 | void setDistributeState(bool Enable = true) { |
| 153 | StagedAttrs.DistributeEnable = |
| 154 | Enable ? LoopAttributes::Enable : LoopAttributes::Disable; |
| 155 | } |
| 156 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 157 | /// Set the next pushed loop unroll state. |
Mark Heffernan | 397a98d | 2015-08-10 17:29:39 +0000 | [diff] [blame] | 158 | void setUnrollState(const LoopAttributes::LVEnableState &State) { |
| 159 | StagedAttrs.UnrollEnable = State; |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 160 | } |
| 161 | |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 162 | /// Set the next pushed loop unroll_and_jam state. |
| 163 | void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) { |
| 164 | StagedAttrs.UnrollAndJamEnable = State; |
| 165 | } |
| 166 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 167 | /// Set the vectorize width for the next loop pushed. |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 168 | void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 169 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 170 | /// Set the interleave count for the next loop pushed. |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 171 | void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 172 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 173 | /// Set the unroll count for the next loop pushed. |
Tyler Nowicki | 54c020d | 2015-07-27 20:10:20 +0000 | [diff] [blame] | 174 | void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; } |
| 175 | |
David Green | c8e3924 | 2018-08-01 14:36:12 +0000 | [diff] [blame] | 176 | /// \brief Set the unroll count for the next loop pushed. |
| 177 | void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; } |
| 178 | |
Aaron Ballman | 9bdf515 | 2019-01-04 17:20:00 +0000 | [diff] [blame] | 179 | /// Set the pipeline disabled state. |
| 180 | void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; } |
| 181 | |
| 182 | /// Set the pipeline initiation interval. |
| 183 | void setPipelineInitiationInterval(unsigned C) { |
| 184 | StagedAttrs.PipelineInitiationInterval = C; |
| 185 | } |
| 186 | |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 187 | private: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 188 | /// Returns true if there is LoopInfo on the stack. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 189 | bool hasInfo() const { return !Active.empty(); } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 190 | /// Return the LoopInfo for the current loop. HasInfo should be called |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 191 | /// first to ensure LoopInfo is present. |
| 192 | const LoopInfo &getInfo() const { return Active.back(); } |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 193 | /// The set of attributes that will be applied to the next pushed loop. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 194 | LoopAttributes StagedAttrs; |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 195 | /// Stack of active loops. |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 196 | llvm::SmallVector<LoopInfo, 4> Active; |
| 197 | }; |
| 198 | |
| 199 | } // end namespace CodeGen |
| 200 | } // end namespace clang |
| 201 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 202 | #endif |