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