clang  9.0.0svn
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 namespace CodeGen {
33 
34 /// Attributes that may be specified on loops.
36  explicit LoopAttributes(bool IsParallel = false);
37  void clear();
38 
39  /// Generate llvm.loop.parallel metadata for loads and stores.
40  bool IsParallel;
41 
42  /// State of loop vectorization or unrolling.
43  enum LVEnableState { Unspecified, Enable, Disable, Full };
44 
45  /// Value for llvm.loop.vectorize.enable metadata.
47 
48  /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
50 
51  /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
53 
54  /// Value for llvm.loop.vectorize.width metadata.
55  unsigned VectorizeWidth;
56 
57  /// Value for llvm.loop.interleave.count metadata.
58  unsigned InterleaveCount;
59 
60  /// llvm.unroll.
61  unsigned UnrollCount;
62 
63  /// llvm.unroll.
65 
66  /// Value for llvm.loop.distribute.enable metadata.
68 
69  /// Value for llvm.loop.pipeline.disable metadata.
71 
72  /// Value for llvm.loop.pipeline.iicount metadata.
74 };
75 
76 /// Information used when generating a structured loop.
77 class LoopInfo {
78 public:
79  /// Construct a new LoopInfo for the loop with entry Header.
80  LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
81  const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc,
82  LoopInfo *Parent);
83 
84  /// Get the loop id metadata for this loop.
85  llvm::MDNode *getLoopID() const { return TempLoopID.get(); }
86 
87  /// Get the header block of this loop.
88  llvm::BasicBlock *getHeader() const { return Header; }
89 
90  /// Get the set of attributes active for this loop.
91  const LoopAttributes &getAttributes() const { return Attrs; }
92 
93  /// Return this loop's access group or nullptr if it does not have one.
94  llvm::MDNode *getAccessGroup() const { return AccGroup; }
95 
96  /// Create the loop's metadata. Must be called after its nested loops have
97  /// been processed.
98  void finish();
99 
100 private:
101  /// Loop ID metadata.
102  llvm::TempMDTuple TempLoopID;
103  /// Header block of this loop.
104  llvm::BasicBlock *Header;
105  /// The attributes for this loop.
106  LoopAttributes Attrs;
107  /// The access group for memory accesses parallel to this loop.
108  llvm::MDNode *AccGroup = nullptr;
109  /// 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);
182 };
183 
184 /// A stack of loop information corresponding to loop nesting levels.
185 /// This stack can be used to prepare attributes which are applied when a loop
186 /// is emitted.
188  LoopInfoStack(const LoopInfoStack &) = delete;
189  void operator=(const LoopInfoStack &) = delete;
190 
191 public:
193 
194  /// Begin a new structured loop. The set of staged attributes will be
195  /// applied to the loop and then cleared.
196  void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
197  const llvm::DebugLoc &EndLoc);
198 
199  /// Begin a new structured loop. Stage attributes from the Attrs list.
200  /// The staged attributes are applied to the loop and then cleared.
201  void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
202  llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
203  const llvm::DebugLoc &EndLoc);
204 
205  /// End the current loop.
206  void pop();
207 
208  /// Return the top loop id metadata.
209  llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
210 
211  /// Return true if the top loop is parallel.
212  bool getCurLoopParallel() const {
213  return hasInfo() ? getInfo().getAttributes().IsParallel : false;
214  }
215 
216  /// Function called by the CodeGenFunction when an instruction is
217  /// created.
218  void InsertHelper(llvm::Instruction *I) const;
219 
220  /// Set the next pushed loop as parallel.
221  void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
222 
223  /// Set the next pushed loop 'vectorize.enable'
224  void setVectorizeEnable(bool Enable = true) {
225  StagedAttrs.VectorizeEnable =
226  Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
227  }
228 
229  /// Set the next pushed loop as a distribution candidate.
230  void setDistributeState(bool Enable = true) {
231  StagedAttrs.DistributeEnable =
232  Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
233  }
234 
235  /// Set the next pushed loop unroll state.
237  StagedAttrs.UnrollEnable = State;
238  }
239 
240  /// Set the next pushed loop unroll_and_jam state.
242  StagedAttrs.UnrollAndJamEnable = State;
243  }
244 
245  /// Set the vectorize width for the next loop pushed.
246  void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
247 
248  /// Set the interleave count for the next loop pushed.
249  void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
250 
251  /// Set the unroll count for the next loop pushed.
252  void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
253 
254  /// \brief Set the unroll count for the next loop pushed.
255  void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
256 
257  /// 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 
265 private:
266  /// Returns true if there is LoopInfo on the stack.
267  bool hasInfo() const { return !Active.empty(); }
268  /// Return the LoopInfo for the current loop. HasInfo should be called
269  /// first to ensure LoopInfo is present.
270  const LoopInfo &getInfo() const { return Active.back(); }
271  /// The set of attributes that will be applied to the next pushed loop.
272  LoopAttributes StagedAttrs;
273  /// Stack of active loops.
275 };
276 
277 } // end namespace CodeGen
278 } // end namespace clang
279 
280 #endif
void setUnrollCount(unsigned C)
Set the unroll count for the next loop pushed.
Definition: CGLoopInfo.h:252
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:29
Attributes that may be specified on loops.
Definition: CGLoopInfo.h:35
unsigned UnrollAndJamCount
llvm.unroll.
Definition: CGLoopInfo.h:64
Information used when generating a structured loop.
Definition: CGLoopInfo.h:77
virtual void clear()
LVEnableState UnrollEnable
Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Definition: CGLoopInfo.h:49
unsigned PipelineInitiationInterval
Value for llvm.loop.pipeline.iicount metadata.
Definition: CGLoopInfo.h:73
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
LineState State
unsigned InterleaveCount
Value for llvm.loop.interleave.count metadata.
Definition: CGLoopInfo.h:58
void setUnrollAndJamCount(unsigned C)
Set the unroll count for the next loop pushed.
Definition: CGLoopInfo.h:255
LVEnableState VectorizeEnable
Value for llvm.loop.vectorize.enable metadata.
Definition: CGLoopInfo.h:46
void setVectorizeWidth(unsigned W)
Set the vectorize width for the next loop pushed.
Definition: CGLoopInfo.h:246
LVEnableState UnrollAndJamEnable
Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
Definition: CGLoopInfo.h:52
NodeId Parent
Definition: ASTDiff.cpp:191
bool IsParallel
Generate llvm.loop.parallel metadata for loads and stores.
Definition: CGLoopInfo.h:40
void setInterleaveCount(unsigned C)
Set the interleave count for the next loop pushed.
Definition: CGLoopInfo.h:249
unsigned UnrollCount
llvm.unroll.
Definition: CGLoopInfo.h:61
void setParallel(bool Enable=true)
Set the next pushed loop as parallel.
Definition: CGLoopInfo.h:221
LVEnableState DistributeEnable
Value for llvm.loop.distribute.enable metadata.
Definition: CGLoopInfo.h:67
llvm::MDNode * getLoopID() const
Get the loop id metadata for this loop.
Definition: CGLoopInfo.h:85
llvm::MDNode * getCurLoopID() const
Return the top loop id metadata.
Definition: CGLoopInfo.h:209
void setDistributeState(bool Enable=true)
Set the next pushed loop as a distribution candidate.
Definition: CGLoopInfo.h:230
llvm::MDNode * getAccessGroup() const
Return this loop&#39;s access group or nullptr if it does not have one.
Definition: CGLoopInfo.h:94
Dataflow Directional Tag Classes.
bool PipelineDisabled
Value for llvm.loop.pipeline.disable metadata.
Definition: CGLoopInfo.h:70
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:33
A stack of loop information corresponding to loop nesting levels.
Definition: CGLoopInfo.h:187
bool getCurLoopParallel() const
Return true if the top loop is parallel.
Definition: CGLoopInfo.h:212
LVEnableState
State of loop vectorization or unrolling.
Definition: CGLoopInfo.h:43
void setUnrollState(const LoopAttributes::LVEnableState &State)
Set the next pushed loop unroll state.
Definition: CGLoopInfo.h:236
void setUnrollAndJamState(const LoopAttributes::LVEnableState &State)
Set the next pushed loop unroll_and_jam state.
Definition: CGLoopInfo.h:241
void setVectorizeEnable(bool Enable=true)
Set the next pushed loop &#39;vectorize.enable&#39;.
Definition: CGLoopInfo.h:224
void setPipelineDisabled(bool S)
Set the pipeline disabled state.
Definition: CGLoopInfo.h:258
void setPipelineInitiationInterval(unsigned C)
Set the pipeline initiation interval.
Definition: CGLoopInfo.h:261
unsigned VectorizeWidth
Value for llvm.loop.vectorize.width metadata.
Definition: CGLoopInfo.h:55
llvm::BasicBlock * getHeader() const
Get the header block of this loop.
Definition: CGLoopInfo.h:88
const LoopAttributes & getAttributes() const
Get the set of attributes active for this loop.
Definition: CGLoopInfo.h:91