blob: 35d0e00527b9fe5959c6f4506fe03963df0b875a [file] [log] [blame]
Alexander Musman515ad8c2014-05-22 08:54:05 +00001//===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- C++ -*---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Musman515ad8c2014-05-22 08:54:05 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This is the internal state used for llvm translation for loop statement
10// metadata.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
15#define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
Alexander Musman515ad8c2014-05-22 08:54:05 +000016
Tyler Nowicki9d268e12015-06-11 23:23:17 +000017#include "llvm/ADT/ArrayRef.h"
Alexander Musman515ad8c2014-05-22 08:54:05 +000018#include "llvm/ADT/SmallVector.h"
Hal Finkelc07e19b2016-05-25 21:53:24 +000019#include "llvm/IR/DebugLoc.h"
Alexander Musman515ad8c2014-05-22 08:54:05 +000020#include "llvm/IR/Value.h"
21#include "llvm/Support/Compiler.h"
22
23namespace llvm {
24class BasicBlock;
25class Instruction;
26class MDNode;
27} // end namespace llvm
28
29namespace clang {
Tyler Nowicki9d268e12015-06-11 23:23:17 +000030class Attr;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000031class ASTContext;
Alexander Musman515ad8c2014-05-22 08:54:05 +000032namespace CodeGen {
33
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000034/// Attributes that may be specified on loops.
Alexander Musman515ad8c2014-05-22 08:54:05 +000035struct LoopAttributes {
36 explicit LoopAttributes(bool IsParallel = false);
37 void clear();
38
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000039 /// Generate llvm.loop.parallel metadata for loads and stores.
Alexander Musman515ad8c2014-05-22 08:54:05 +000040 bool IsParallel;
41
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000042 /// State of loop vectorization or unrolling.
Mark Heffernan397a98d2015-08-10 17:29:39 +000043 enum LVEnableState { Unspecified, Enable, Disable, Full };
Alexander Musman515ad8c2014-05-22 08:54:05 +000044
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000045 /// Value for llvm.loop.vectorize.enable metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000046 LVEnableState VectorizeEnable;
Alexander Musman515ad8c2014-05-22 08:54:05 +000047
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000048 /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Tyler Nowicki54c020d2015-07-27 20:10:20 +000049 LVEnableState UnrollEnable;
50
David Greenc8e39242018-08-01 14:36:12 +000051 /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
52 LVEnableState UnrollAndJamEnable;
53
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000054 /// Value for llvm.loop.vectorize.width metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000055 unsigned VectorizeWidth;
Alexander Musman515ad8c2014-05-22 08:54:05 +000056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057 /// Value for llvm.loop.interleave.count metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000058 unsigned InterleaveCount;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000059
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000060 /// llvm.unroll.
Tyler Nowicki54c020d2015-07-27 20:10:20 +000061 unsigned UnrollCount;
Adam Nemet2de463e2016-06-14 12:04:26 +000062
David Greenc8e39242018-08-01 14:36:12 +000063 /// llvm.unroll.
64 unsigned UnrollAndJamCount;
65
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000066 /// Value for llvm.loop.distribute.enable metadata.
Adam Nemet2de463e2016-06-14 12:04:26 +000067 LVEnableState DistributeEnable;
Aaron Ballman9bdf5152019-01-04 17:20:00 +000068
69 /// Value for llvm.loop.pipeline.disable metadata.
70 bool PipelineDisabled;
71
72 /// Value for llvm.loop.pipeline.iicount metadata.
73 unsigned PipelineInitiationInterval;
Alexander Musman515ad8c2014-05-22 08:54:05 +000074};
75
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000076/// Information used when generating a structured loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000077class LoopInfo {
78public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000079 /// Construct a new LoopInfo for the loop with entry Header.
Hal Finkelc07e19b2016-05-25 21:53:24 +000080 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
Michael Kruse58e76422019-04-01 17:47:41 +000081 const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc,
82 LoopInfo *Parent);
Alexander Musman515ad8c2014-05-22 08:54:05 +000083
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000084 /// Get the loop id metadata for this loop.
Michael Kruse58e76422019-04-01 17:47:41 +000085 llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
Alexander Musman515ad8c2014-05-22 08:54:05 +000086
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000087 /// Get the header block of this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000088 llvm::BasicBlock *getHeader() const { return Header; }
89
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000090 /// Get the set of attributes active for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000091 const LoopAttributes &getAttributes() const { return Attrs; }
92
Michael Kruse05351372018-12-20 21:24:54 +000093 /// Return this loop's access group or nullptr if it does not have one.
94 llvm::MDNode *getAccessGroup() const { return AccGroup; }
95
Michael Kruse58e76422019-04-01 17:47:41 +000096 /// Create the loop's metadata. Must be called after its nested loops have
97 /// been processed.
98 void finish();
99
Alexander Musman515ad8c2014-05-22 08:54:05 +0000100private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000101 /// Loop ID metadata.
Michael Kruse58e76422019-04-01 17:47:41 +0000102 llvm::TempMDTuple TempLoopID;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000103 /// Header block of this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000104 llvm::BasicBlock *Header;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000105 /// The attributes for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000106 LoopAttributes Attrs;
Michael Kruse05351372018-12-20 21:24:54 +0000107 /// The access group for memory accesses parallel to this loop.
108 llvm::MDNode *AccGroup = nullptr;
Michael Kruse58e76422019-04-01 17:47:41 +0000109 /// Start location of this loop.
110 llvm::DebugLoc StartLoc;
111 /// End location of this loop.
112 llvm::DebugLoc EndLoc;
113 /// The next outer loop, or nullptr if this is the outermost loop.
114 LoopInfo *Parent;
115 /// If this loop has unroll-and-jam metadata, this can be set by the inner
116 /// loop's LoopInfo to set the llvm.loop.unroll_and_jam.followup_inner
117 /// metadata.
118 llvm::MDNode *UnrollAndJamInnerFollowup = nullptr;
119
120 /// Create a LoopID without any transformations.
121 llvm::MDNode *
122 createLoopPropertiesMetadata(llvm::ArrayRef<llvm::Metadata *> LoopProperties);
123
124 /// Create a LoopID for transformations.
125 ///
126 /// The methods call each other in case multiple transformations are applied
127 /// to a loop. The transformation first to be applied will use LoopID of the
128 /// next transformation in its followup attribute.
129 ///
130 /// @param Attrs The loop's transformations.
131 /// @param LoopProperties Non-transformation properties such as debug
132 /// location, parallel accesses and disabled
133 /// transformations. These are added to the returned
134 /// LoopID.
135 /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
136 /// at least one transformation.
137 ///
138 /// @return A LoopID (metadata node) that can be used for the llvm.loop
139 /// annotation or followup-attribute.
140 /// @{
141 llvm::MDNode *
142 createPipeliningMetadata(const LoopAttributes &Attrs,
143 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
144 bool &HasUserTransforms);
145 llvm::MDNode *
146 createPartialUnrollMetadata(const LoopAttributes &Attrs,
147 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
148 bool &HasUserTransforms);
149 llvm::MDNode *
150 createUnrollAndJamMetadata(const LoopAttributes &Attrs,
151 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
152 bool &HasUserTransforms);
153 llvm::MDNode *
154 createLoopVectorizeMetadata(const LoopAttributes &Attrs,
155 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
156 bool &HasUserTransforms);
157 llvm::MDNode *
158 createLoopDistributeMetadata(const LoopAttributes &Attrs,
159 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
160 bool &HasUserTransforms);
161 llvm::MDNode *
162 createFullUnrollMetadata(const LoopAttributes &Attrs,
163 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
164 bool &HasUserTransforms);
165 /// @}
166
167 /// Create a LoopID for this loop, including transformation-unspecific
168 /// metadata such as debug location.
169 ///
170 /// @param Attrs This loop's attributes and transformations.
171 /// @param LoopProperties Additional non-transformation properties to add
172 /// to the LoopID, such as transformation-specific
173 /// metadata that are not covered by @p Attrs.
174 /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
175 /// at least one transformation.
176 ///
177 /// @return A LoopID (metadata node) that can be used for the llvm.loop
178 /// annotation.
179 llvm::MDNode *createMetadata(const LoopAttributes &Attrs,
180 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
181 bool &HasUserTransforms);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000182};
183
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000184/// A stack of loop information corresponding to loop nesting levels.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000185/// This stack can be used to prepare attributes which are applied when a loop
186/// is emitted.
187class LoopInfoStack {
Aaron Ballmanabc18922015-02-15 22:54:08 +0000188 LoopInfoStack(const LoopInfoStack &) = delete;
189 void operator=(const LoopInfoStack &) = delete;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000190
191public:
192 LoopInfoStack() {}
193
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000194 /// Begin a new structured loop. The set of staged attributes will be
Alexander Musman515ad8c2014-05-22 08:54:05 +0000195 /// applied to the loop and then cleared.
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000196 void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
197 const llvm::DebugLoc &EndLoc);
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000198
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000199 /// Begin a new structured loop. Stage attributes from the Attrs list.
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000200 /// The staged attributes are applied to the loop and then cleared.
201 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000202 llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
203 const llvm::DebugLoc &EndLoc);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000204
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000205 /// End the current loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000206 void pop();
207
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000208 /// Return the top loop id metadata.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000209 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
210
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000211 /// Return true if the top loop is parallel.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000212 bool getCurLoopParallel() const {
213 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
214 }
215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000216 /// Function called by the CodeGenFunction when an instruction is
Alexander Musman515ad8c2014-05-22 08:54:05 +0000217 /// created.
218 void InsertHelper(llvm::Instruction *I) const;
219
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000220 /// Set the next pushed loop as parallel.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000221 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
222
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000223 /// Set the next pushed loop 'vectorize.enable'
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000224 void setVectorizeEnable(bool Enable = true) {
225 StagedAttrs.VectorizeEnable =
226 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000227 }
228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000229 /// Set the next pushed loop as a distribution candidate.
Adam Nemet2de463e2016-06-14 12:04:26 +0000230 void setDistributeState(bool Enable = true) {
231 StagedAttrs.DistributeEnable =
232 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
233 }
234
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000235 /// Set the next pushed loop unroll state.
Mark Heffernan397a98d2015-08-10 17:29:39 +0000236 void setUnrollState(const LoopAttributes::LVEnableState &State) {
237 StagedAttrs.UnrollEnable = State;
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000238 }
239
David Greenc8e39242018-08-01 14:36:12 +0000240 /// Set the next pushed loop unroll_and_jam state.
241 void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
242 StagedAttrs.UnrollAndJamEnable = State;
243 }
244
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000245 /// Set the vectorize width for the next loop pushed.
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000246 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000247
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000248 /// Set the interleave count for the next loop pushed.
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000249 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000251 /// Set the unroll count for the next loop pushed.
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000252 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
253
David Greenc8e39242018-08-01 14:36:12 +0000254 /// \brief Set the unroll count for the next loop pushed.
255 void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
256
Aaron Ballman9bdf5152019-01-04 17:20:00 +0000257 /// Set the pipeline disabled state.
258 void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
259
260 /// Set the pipeline initiation interval.
261 void setPipelineInitiationInterval(unsigned C) {
262 StagedAttrs.PipelineInitiationInterval = C;
263 }
264
Alexander Musman515ad8c2014-05-22 08:54:05 +0000265private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000266 /// Returns true if there is LoopInfo on the stack.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000267 bool hasInfo() const { return !Active.empty(); }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000268 /// Return the LoopInfo for the current loop. HasInfo should be called
Alexander Musman515ad8c2014-05-22 08:54:05 +0000269 /// first to ensure LoopInfo is present.
270 const LoopInfo &getInfo() const { return Active.back(); }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000271 /// The set of attributes that will be applied to the next pushed loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000272 LoopAttributes StagedAttrs;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000273 /// Stack of active loops.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000274 llvm::SmallVector<LoopInfo, 4> Active;
275};
276
277} // end namespace CodeGen
278} // end namespace clang
279
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000280#endif