blob: 5abcf37c5433ed02820453f98629c9800b7b4865 [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
Sjoerd Meijera48f58c2019-07-25 07:33:13 +000054 /// Value for llvm.loop.vectorize.predicate metadata
55 LVEnableState VectorizePredicateEnable;
56
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057 /// Value for llvm.loop.vectorize.width metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000058 unsigned VectorizeWidth;
Alexander Musman515ad8c2014-05-22 08:54:05 +000059
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000060 /// Value for llvm.loop.interleave.count metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000061 unsigned InterleaveCount;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000062
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000063 /// llvm.unroll.
Tyler Nowicki54c020d2015-07-27 20:10:20 +000064 unsigned UnrollCount;
Adam Nemet2de463e2016-06-14 12:04:26 +000065
David Greenc8e39242018-08-01 14:36:12 +000066 /// llvm.unroll.
67 unsigned UnrollAndJamCount;
68
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000069 /// Value for llvm.loop.distribute.enable metadata.
Adam Nemet2de463e2016-06-14 12:04:26 +000070 LVEnableState DistributeEnable;
Aaron Ballman9bdf5152019-01-04 17:20:00 +000071
72 /// Value for llvm.loop.pipeline.disable metadata.
73 bool PipelineDisabled;
74
75 /// Value for llvm.loop.pipeline.iicount metadata.
76 unsigned PipelineInitiationInterval;
Alexander Musman515ad8c2014-05-22 08:54:05 +000077};
78
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000079/// Information used when generating a structured loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000080class LoopInfo {
81public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000082 /// Construct a new LoopInfo for the loop with entry Header.
Hal Finkelc07e19b2016-05-25 21:53:24 +000083 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
Michael Kruse58e76422019-04-01 17:47:41 +000084 const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc,
85 LoopInfo *Parent);
Alexander Musman515ad8c2014-05-22 08:54:05 +000086
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000087 /// Get the loop id metadata for this loop.
Michael Kruse58e76422019-04-01 17:47:41 +000088 llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
Alexander Musman515ad8c2014-05-22 08:54:05 +000089
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000090 /// Get the header block of this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000091 llvm::BasicBlock *getHeader() const { return Header; }
92
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000093 /// Get the set of attributes active for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000094 const LoopAttributes &getAttributes() const { return Attrs; }
95
Michael Kruse05351372018-12-20 21:24:54 +000096 /// Return this loop's access group or nullptr if it does not have one.
97 llvm::MDNode *getAccessGroup() const { return AccGroup; }
98
Michael Kruse58e76422019-04-01 17:47:41 +000099 /// Create the loop's metadata. Must be called after its nested loops have
100 /// been processed.
101 void finish();
102
Alexander Musman515ad8c2014-05-22 08:54:05 +0000103private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000104 /// Loop ID metadata.
Michael Kruse58e76422019-04-01 17:47:41 +0000105 llvm::TempMDTuple TempLoopID;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000106 /// Header block of this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000107 llvm::BasicBlock *Header;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000108 /// The attributes for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000109 LoopAttributes Attrs;
Michael Kruse05351372018-12-20 21:24:54 +0000110 /// The access group for memory accesses parallel to this loop.
111 llvm::MDNode *AccGroup = nullptr;
Michael Kruse58e76422019-04-01 17:47:41 +0000112 /// Start location of this loop.
113 llvm::DebugLoc StartLoc;
114 /// End location of this loop.
115 llvm::DebugLoc EndLoc;
116 /// The next outer loop, or nullptr if this is the outermost loop.
117 LoopInfo *Parent;
118 /// If this loop has unroll-and-jam metadata, this can be set by the inner
119 /// loop's LoopInfo to set the llvm.loop.unroll_and_jam.followup_inner
120 /// metadata.
121 llvm::MDNode *UnrollAndJamInnerFollowup = nullptr;
122
123 /// Create a LoopID without any transformations.
124 llvm::MDNode *
125 createLoopPropertiesMetadata(llvm::ArrayRef<llvm::Metadata *> LoopProperties);
126
127 /// Create a LoopID for transformations.
128 ///
129 /// The methods call each other in case multiple transformations are applied
130 /// to a loop. The transformation first to be applied will use LoopID of the
131 /// next transformation in its followup attribute.
132 ///
133 /// @param Attrs The loop's transformations.
134 /// @param LoopProperties Non-transformation properties such as debug
135 /// location, parallel accesses and disabled
136 /// transformations. These are added to the returned
137 /// LoopID.
138 /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
139 /// at least one transformation.
140 ///
141 /// @return A LoopID (metadata node) that can be used for the llvm.loop
142 /// annotation or followup-attribute.
143 /// @{
144 llvm::MDNode *
145 createPipeliningMetadata(const LoopAttributes &Attrs,
146 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
147 bool &HasUserTransforms);
148 llvm::MDNode *
149 createPartialUnrollMetadata(const LoopAttributes &Attrs,
150 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
151 bool &HasUserTransforms);
152 llvm::MDNode *
153 createUnrollAndJamMetadata(const LoopAttributes &Attrs,
154 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
155 bool &HasUserTransforms);
156 llvm::MDNode *
157 createLoopVectorizeMetadata(const LoopAttributes &Attrs,
158 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
159 bool &HasUserTransforms);
160 llvm::MDNode *
161 createLoopDistributeMetadata(const LoopAttributes &Attrs,
162 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
163 bool &HasUserTransforms);
164 llvm::MDNode *
165 createFullUnrollMetadata(const LoopAttributes &Attrs,
166 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
167 bool &HasUserTransforms);
168 /// @}
169
170 /// Create a LoopID for this loop, including transformation-unspecific
171 /// metadata such as debug location.
172 ///
173 /// @param Attrs This loop's attributes and transformations.
174 /// @param LoopProperties Additional non-transformation properties to add
175 /// to the LoopID, such as transformation-specific
176 /// metadata that are not covered by @p Attrs.
177 /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
178 /// at least one transformation.
179 ///
180 /// @return A LoopID (metadata node) that can be used for the llvm.loop
181 /// annotation.
182 llvm::MDNode *createMetadata(const LoopAttributes &Attrs,
183 llvm::ArrayRef<llvm::Metadata *> LoopProperties,
184 bool &HasUserTransforms);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000185};
186
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000187/// A stack of loop information corresponding to loop nesting levels.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000188/// This stack can be used to prepare attributes which are applied when a loop
189/// is emitted.
190class LoopInfoStack {
Aaron Ballmanabc18922015-02-15 22:54:08 +0000191 LoopInfoStack(const LoopInfoStack &) = delete;
192 void operator=(const LoopInfoStack &) = delete;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000193
194public:
195 LoopInfoStack() {}
196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000197 /// Begin a new structured loop. The set of staged attributes will be
Alexander Musman515ad8c2014-05-22 08:54:05 +0000198 /// applied to the loop and then cleared.
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000199 void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
200 const llvm::DebugLoc &EndLoc);
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000201
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000202 /// Begin a new structured loop. Stage attributes from the Attrs list.
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000203 /// The staged attributes are applied to the loop and then cleared.
204 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000205 llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
206 const llvm::DebugLoc &EndLoc);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000207
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000208 /// End the current loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000209 void pop();
210
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000211 /// Return the top loop id metadata.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000212 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
213
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000214 /// Return true if the top loop is parallel.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000215 bool getCurLoopParallel() const {
216 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
217 }
218
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000219 /// Function called by the CodeGenFunction when an instruction is
Alexander Musman515ad8c2014-05-22 08:54:05 +0000220 /// created.
221 void InsertHelper(llvm::Instruction *I) const;
222
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000223 /// Set the next pushed loop as parallel.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000224 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
225
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000226 /// Set the next pushed loop 'vectorize.enable'
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000227 void setVectorizeEnable(bool Enable = true) {
228 StagedAttrs.VectorizeEnable =
229 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000230 }
231
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000232 /// Set the next pushed loop as a distribution candidate.
Adam Nemet2de463e2016-06-14 12:04:26 +0000233 void setDistributeState(bool Enable = true) {
234 StagedAttrs.DistributeEnable =
235 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
236 }
237
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000238 /// Set the next pushed loop unroll state.
Mark Heffernan397a98d2015-08-10 17:29:39 +0000239 void setUnrollState(const LoopAttributes::LVEnableState &State) {
240 StagedAttrs.UnrollEnable = State;
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000241 }
242
Sjoerd Meijera48f58c2019-07-25 07:33:13 +0000243 /// Set the next pushed vectorize predicate state.
244 void setVectorizePredicateState(const LoopAttributes::LVEnableState &State) {
245 StagedAttrs.VectorizePredicateEnable = State;
246 }
247
David Greenc8e39242018-08-01 14:36:12 +0000248 /// Set the next pushed loop unroll_and_jam state.
249 void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
250 StagedAttrs.UnrollAndJamEnable = State;
251 }
252
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000253 /// Set the vectorize width for the next loop pushed.
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000254 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000255
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000256 /// Set the interleave count for the next loop pushed.
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000257 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000258
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000259 /// Set the unroll count for the next loop pushed.
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000260 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
261
David Greenc8e39242018-08-01 14:36:12 +0000262 /// \brief Set the unroll count for the next loop pushed.
263 void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
264
Aaron Ballman9bdf5152019-01-04 17:20:00 +0000265 /// Set the pipeline disabled state.
266 void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
267
268 /// Set the pipeline initiation interval.
269 void setPipelineInitiationInterval(unsigned C) {
270 StagedAttrs.PipelineInitiationInterval = C;
271 }
272
Alexander Musman515ad8c2014-05-22 08:54:05 +0000273private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000274 /// Returns true if there is LoopInfo on the stack.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000275 bool hasInfo() const { return !Active.empty(); }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000276 /// Return the LoopInfo for the current loop. HasInfo should be called
Alexander Musman515ad8c2014-05-22 08:54:05 +0000277 /// first to ensure LoopInfo is present.
Aaron Ballmanb6ab5332019-08-19 13:37:41 +0000278 const LoopInfo &getInfo() const { return *Active.back(); }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000279 /// The set of attributes that will be applied to the next pushed loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000280 LoopAttributes StagedAttrs;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000281 /// Stack of active loops.
Aaron Ballmanb6ab5332019-08-19 13:37:41 +0000282 llvm::SmallVector<std::unique_ptr<LoopInfo>, 4> Active;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000283};
284
285} // end namespace CodeGen
286} // end namespace clang
287
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000288#endif