clang  12.0.0git
CGLoopInfo.h
Go to the documentation of this file.
1 //===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- C++ -*---------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is the internal state used for llvm translation for loop statement
10 // metadata.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/IR/DebugLoc.h"
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 {
30 class Attr;
31 class ASTContext;
32 class CodeGenOptions;
33 namespace CodeGen {
34 
35 /// Attributes that may be specified on loops.
37  explicit LoopAttributes(bool IsParallel = false);
38  void clear();
39 
40  /// Generate llvm.loop.parallel metadata for loads and stores.
41  bool IsParallel;
42 
43  /// State of loop vectorization or unrolling.
44  enum LVEnableState { Unspecified, Enable, Disable, Full };
45 
46  /// Value for llvm.loop.vectorize.enable metadata.
48 
49  /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
51 
52  /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
54 
55  /// Value for llvm.loop.vectorize.predicate metadata
57 
58  /// Value for llvm.loop.vectorize.width metadata.
59  unsigned VectorizeWidth;
60 
61  /// Value for llvm.loop.interleave.count metadata.
62  unsigned InterleaveCount;
63 
64  /// llvm.unroll.
65  unsigned UnrollCount;
66 
67  /// llvm.unroll.
69 
70  /// Value for llvm.loop.distribute.enable metadata.
72 
73  /// Value for llvm.loop.pipeline.disable metadata.
75 
76  /// Value for llvm.loop.pipeline.iicount metadata.
78 };
79 
80 /// Information used when generating a structured loop.
81 class LoopInfo {
82 public:
83  /// Construct a new LoopInfo for the loop with entry Header.
84  LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
85  const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc,
86  LoopInfo *Parent);
87 
88  /// Get the loop id metadata for this loop.
89  llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
90 
91  /// Get the header block of this loop.
92  llvm::BasicBlock *getHeader() const { return Header; }
93 
94  /// Get the set of attributes active for this loop.
95  const LoopAttributes &getAttributes() const { return Attrs; }
96 
97  /// Return this loop's access group or nullptr if it does not have one.
98  llvm::MDNode *getAccessGroup() const { return AccGroup; }
99 
100  /// Create the loop's metadata. Must be called after its nested loops have
101  /// been processed.
102  void finish();
103 
104 private:
105  /// Loop ID metadata.
106  llvm::TempMDTuple TempLoopID;
107  /// Header block of this loop.
108  llvm::BasicBlock *Header;
109  /// The attributes for this loop.
110  LoopAttributes Attrs;
111  /// The access group for memory accesses parallel to this loop.
112  llvm::MDNode *AccGroup = nullptr;
113  /// Start location of this loop.
114  llvm::DebugLoc StartLoc;
115  /// End location of this loop.
116  llvm::DebugLoc EndLoc;
117  /// The next outer loop, or nullptr if this is the outermost loop.
118  LoopInfo *Parent;
119  /// If this loop has unroll-and-jam metadata, this can be set by the inner
120  /// loop's LoopInfo to set the llvm.loop.unroll_and_jam.followup_inner
121  /// metadata.
122  llvm::MDNode *UnrollAndJamInnerFollowup = nullptr;
123 
124  /// Create a LoopID without any transformations.
125  llvm::MDNode *
126  createLoopPropertiesMetadata(llvm::ArrayRef<llvm::Metadata *> LoopProperties);
127 
128  /// Create a LoopID for transformations.
129  ///
130  /// The methods call each other in case multiple transformations are applied
131  /// to a loop. The transformation first to be applied will use LoopID of the
132  /// next transformation in its followup attribute.
133  ///
134  /// @param Attrs The loop's transformations.
135  /// @param LoopProperties Non-transformation properties such as debug
136  /// location, parallel accesses and disabled
137  /// transformations. These are added to the returned
138  /// LoopID.
139  /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
140  /// at least one transformation.
141  ///
142  /// @return A LoopID (metadata node) that can be used for the llvm.loop
143  /// annotation or followup-attribute.
144  /// @{
145  llvm::MDNode *
146  createPipeliningMetadata(const LoopAttributes &Attrs,
147  llvm::ArrayRef<llvm::Metadata *> LoopProperties,
148  bool &HasUserTransforms);
149  llvm::MDNode *
150  createPartialUnrollMetadata(const LoopAttributes &Attrs,
151  llvm::ArrayRef<llvm::Metadata *> LoopProperties,
152  bool &HasUserTransforms);
153  llvm::MDNode *
154  createUnrollAndJamMetadata(const LoopAttributes &Attrs,
155  llvm::ArrayRef<llvm::Metadata *> LoopProperties,
156  bool &HasUserTransforms);
157  llvm::MDNode *
158  createLoopVectorizeMetadata(const LoopAttributes &Attrs,
159  llvm::ArrayRef<llvm::Metadata *> LoopProperties,
160  bool &HasUserTransforms);
161  llvm::MDNode *
162  createLoopDistributeMetadata(const LoopAttributes &Attrs,
163  llvm::ArrayRef<llvm::Metadata *> LoopProperties,
164  bool &HasUserTransforms);
165  llvm::MDNode *
166  createFullUnrollMetadata(const LoopAttributes &Attrs,
167  llvm::ArrayRef<llvm::Metadata *> LoopProperties,
168  bool &HasUserTransforms);
169  /// @}
170 
171  /// Create a LoopID for this loop, including transformation-unspecific
172  /// metadata such as debug location.
173  ///
174  /// @param Attrs This loop's attributes and transformations.
175  /// @param LoopProperties Additional non-transformation properties to add
176  /// to the LoopID, such as transformation-specific
177  /// metadata that are not covered by @p Attrs.
178  /// @param HasUserTransforms [out] Set to true if the returned MDNode encodes
179  /// at least one transformation.
180  ///
181  /// @return A LoopID (metadata node) that can be used for the llvm.loop
182  /// annotation.
183  llvm::MDNode *createMetadata(const LoopAttributes &Attrs,
184  llvm::ArrayRef<llvm::Metadata *> LoopProperties,
185  bool &HasUserTransforms);
186 };
187 
188 /// A stack of loop information corresponding to loop nesting levels.
189 /// This stack can be used to prepare attributes which are applied when a loop
190 /// is emitted.
192  LoopInfoStack(const LoopInfoStack &) = delete;
193  void operator=(const LoopInfoStack &) = delete;
194 
195 public:
197 
198  /// Begin a new structured loop. The set of staged attributes will be
199  /// applied to the loop and then cleared.
200  void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
201  const llvm::DebugLoc &EndLoc);
202 
203  /// Begin a new structured loop. Stage attributes from the Attrs list.
204  /// The staged attributes are applied to the loop and then cleared.
205  void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
206  const clang::CodeGenOptions &CGOpts,
207  llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
208  const llvm::DebugLoc &EndLoc);
209 
210  /// End the current loop.
211  void pop();
212 
213  /// Return the top loop id metadata.
214  llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
215 
216  /// Return true if the top loop is parallel.
217  bool getCurLoopParallel() const {
218  return hasInfo() ? getInfo().getAttributes().IsParallel : false;
219  }
220 
221  /// Function called by the CodeGenFunction when an instruction is
222  /// created.
223  void InsertHelper(llvm::Instruction *I) const;
224 
225  /// Set the next pushed loop as parallel.
226  void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
227 
228  /// Set the next pushed loop 'vectorize.enable'
229  void setVectorizeEnable(bool Enable = true) {
230  StagedAttrs.VectorizeEnable =
231  Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
232  }
233 
234  /// Set the next pushed loop as a distribution candidate.
235  void setDistributeState(bool Enable = true) {
236  StagedAttrs.DistributeEnable =
237  Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
238  }
239 
240  /// Set the next pushed loop unroll state.
242  StagedAttrs.UnrollEnable = State;
243  }
244 
245  /// Set the next pushed vectorize predicate state.
247  StagedAttrs.VectorizePredicateEnable = State;
248  }
249 
250  /// Set the next pushed loop unroll_and_jam state.
252  StagedAttrs.UnrollAndJamEnable = State;
253  }
254 
255  /// Set the vectorize width for the next loop pushed.
256  void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
257 
258  /// Set the interleave count for the next loop pushed.
259  void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
260 
261  /// Set the unroll count for the next loop pushed.
262  void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
263 
264  /// \brief Set the unroll count for the next loop pushed.
265  void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
266 
267  /// Set the pipeline disabled state.
268  void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
269 
270  /// Set the pipeline initiation interval.
271  void setPipelineInitiationInterval(unsigned C) {
272  StagedAttrs.PipelineInitiationInterval = C;
273  }
274 
275 private:
276  /// Returns true if there is LoopInfo on the stack.
277  bool hasInfo() const { return !Active.empty(); }
278  /// Return the LoopInfo for the current loop. HasInfo should be called
279  /// first to ensure LoopInfo is present.
280  const LoopInfo &getInfo() const { return *Active.back(); }
281  /// The set of attributes that will be applied to the next pushed loop.
282  LoopAttributes StagedAttrs;
283  /// Stack of active loops.
285 };
286 
287 } // end namespace CodeGen
288 } // end namespace clang
289 
290 #endif
void setVectorizePredicateState(const LoopAttributes::LVEnableState &State)
Set the next pushed vectorize predicate state.
Definition: CGLoopInfo.h:246
void setUnrollCount(unsigned C)
Set the unroll count for the next loop pushed.
Definition: CGLoopInfo.h:262
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
Attributes that may be specified on loops.
Definition: CGLoopInfo.h:36
unsigned UnrollAndJamCount
llvm.unroll.
Definition: CGLoopInfo.h:68
Information used when generating a structured loop.
Definition: CGLoopInfo.h:81
LVEnableState VectorizePredicateEnable
Value for llvm.loop.vectorize.predicate metadata.
Definition: CGLoopInfo.h:56
virtual void clear()
LVEnableState UnrollEnable
Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Definition: CGLoopInfo.h:50
unsigned PipelineInitiationInterval
Value for llvm.loop.pipeline.iicount metadata.
Definition: CGLoopInfo.h:77
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:174
LineState State
unsigned InterleaveCount
Value for llvm.loop.interleave.count metadata.
Definition: CGLoopInfo.h:62
void setUnrollAndJamCount(unsigned C)
Set the unroll count for the next loop pushed.
Definition: CGLoopInfo.h:265
LVEnableState VectorizeEnable
Value for llvm.loop.vectorize.enable metadata.
Definition: CGLoopInfo.h:47
void setVectorizeWidth(unsigned W)
Set the vectorize width for the next loop pushed.
Definition: CGLoopInfo.h:256
LVEnableState UnrollAndJamEnable
Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
Definition: CGLoopInfo.h:53
NodeId Parent
Definition: ASTDiff.cpp:192
bool IsParallel
Generate llvm.loop.parallel metadata for loads and stores.
Definition: CGLoopInfo.h:41
void setInterleaveCount(unsigned C)
Set the interleave count for the next loop pushed.
Definition: CGLoopInfo.h:259
unsigned UnrollCount
llvm.unroll.
Definition: CGLoopInfo.h:65
void setParallel(bool Enable=true)
Set the next pushed loop as parallel.
Definition: CGLoopInfo.h:226
LVEnableState DistributeEnable
Value for llvm.loop.distribute.enable metadata.
Definition: CGLoopInfo.h:71
llvm::MDNode * getLoopID() const
Get the loop id metadata for this loop.
Definition: CGLoopInfo.h:89
llvm::MDNode * getCurLoopID() const
Return the top loop id metadata.
Definition: CGLoopInfo.h:214
void setDistributeState(bool Enable=true)
Set the next pushed loop as a distribution candidate.
Definition: CGLoopInfo.h:235
llvm::MDNode * getAccessGroup() const
Return this loop&#39;s access group or nullptr if it does not have one.
Definition: CGLoopInfo.h:98
Dataflow Directional Tag Classes.
bool PipelineDisabled
Value for llvm.loop.pipeline.disable metadata.
Definition: CGLoopInfo.h:74
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:47
A stack of loop information corresponding to loop nesting levels.
Definition: CGLoopInfo.h:191
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
bool getCurLoopParallel() const
Return true if the top loop is parallel.
Definition: CGLoopInfo.h:217
LVEnableState
State of loop vectorization or unrolling.
Definition: CGLoopInfo.h:44
void setUnrollState(const LoopAttributes::LVEnableState &State)
Set the next pushed loop unroll state.
Definition: CGLoopInfo.h:241
void setUnrollAndJamState(const LoopAttributes::LVEnableState &State)
Set the next pushed loop unroll_and_jam state.
Definition: CGLoopInfo.h:251
void setVectorizeEnable(bool Enable=true)
Set the next pushed loop &#39;vectorize.enable&#39;.
Definition: CGLoopInfo.h:229
void setPipelineDisabled(bool S)
Set the pipeline disabled state.
Definition: CGLoopInfo.h:268
void setPipelineInitiationInterval(unsigned C)
Set the pipeline initiation interval.
Definition: CGLoopInfo.h:271
unsigned VectorizeWidth
Value for llvm.loop.vectorize.width metadata.
Definition: CGLoopInfo.h:59
llvm::BasicBlock * getHeader() const
Get the header block of this loop.
Definition: CGLoopInfo.h:92
const LoopAttributes & getAttributes() const
Get the set of attributes active for this loop.
Definition: CGLoopInfo.h:95