clang  8.0.0svn
CGLoopInfo.h
Go to the documentation of this file.
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 
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
16 #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/IR/DebugLoc.h"
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 {
31 class Attr;
32 class ASTContext;
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.width metadata.
56  unsigned VectorizeWidth;
57 
58  /// Value for llvm.loop.interleave.count metadata.
59  unsigned InterleaveCount;
60 
61  /// llvm.unroll.
62  unsigned UnrollCount;
63 
64  /// llvm.unroll.
66 
67  /// Value for llvm.loop.distribute.enable metadata.
69 };
70 
71 /// Information used when generating a structured loop.
72 class LoopInfo {
73 public:
74  /// Construct a new LoopInfo for the loop with entry Header.
75  LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
76  const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc);
77 
78  /// Get the loop id metadata for this loop.
79  llvm::MDNode *getLoopID() const { return LoopID; }
80 
81  /// Get the header block of this loop.
82  llvm::BasicBlock *getHeader() const { return Header; }
83 
84  /// Get the set of attributes active for this loop.
85  const LoopAttributes &getAttributes() const { return Attrs; }
86 
87 private:
88  /// Loop ID metadata.
89  llvm::MDNode *LoopID;
90  /// Header block of this loop.
91  llvm::BasicBlock *Header;
92  /// The attributes for this loop.
93  LoopAttributes Attrs;
94 };
95 
96 /// A stack of loop information corresponding to loop nesting levels.
97 /// This stack can be used to prepare attributes which are applied when a loop
98 /// is emitted.
100  LoopInfoStack(const LoopInfoStack &) = delete;
101  void operator=(const LoopInfoStack &) = delete;
102 
103 public:
105 
106  /// Begin a new structured loop. The set of staged attributes will be
107  /// applied to the loop and then cleared.
108  void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
109  const llvm::DebugLoc &EndLoc);
110 
111  /// Begin a new structured loop. Stage attributes from the Attrs list.
112  /// The staged attributes are applied to the loop and then cleared.
113  void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
114  llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
115  const llvm::DebugLoc &EndLoc);
116 
117  /// End the current loop.
118  void pop();
119 
120  /// Return the top loop id metadata.
121  llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
122 
123  /// Return true if the top loop is parallel.
124  bool getCurLoopParallel() const {
125  return hasInfo() ? getInfo().getAttributes().IsParallel : false;
126  }
127 
128  /// Function called by the CodeGenFunction when an instruction is
129  /// created.
130  void InsertHelper(llvm::Instruction *I) const;
131 
132  /// Set the next pushed loop as parallel.
133  void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
134 
135  /// Set the next pushed loop 'vectorize.enable'
136  void setVectorizeEnable(bool Enable = true) {
137  StagedAttrs.VectorizeEnable =
138  Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
139  }
140 
141  /// Set the next pushed loop as a distribution candidate.
142  void setDistributeState(bool Enable = true) {
143  StagedAttrs.DistributeEnable =
144  Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
145  }
146 
147  /// Set the next pushed loop unroll state.
149  StagedAttrs.UnrollEnable = State;
150  }
151 
152  /// Set the next pushed loop unroll_and_jam state.
154  StagedAttrs.UnrollAndJamEnable = State;
155  }
156 
157  /// Set the vectorize width for the next loop pushed.
158  void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
159 
160  /// Set the interleave count for the next loop pushed.
161  void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
162 
163  /// Set the unroll count for the next loop pushed.
164  void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
165 
166  /// \brief Set the unroll count for the next loop pushed.
167  void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
168 
169 private:
170  /// Returns true if there is LoopInfo on the stack.
171  bool hasInfo() const { return !Active.empty(); }
172  /// Return the LoopInfo for the current loop. HasInfo should be called
173  /// first to ensure LoopInfo is present.
174  const LoopInfo &getInfo() const { return Active.back(); }
175  /// The set of attributes that will be applied to the next pushed loop.
176  LoopAttributes StagedAttrs;
177  /// Stack of active loops.
179 };
180 
181 } // end namespace CodeGen
182 } // end namespace clang
183 
184 #endif
void setUnrollCount(unsigned C)
Set the unroll count for the next loop pushed.
Definition: CGLoopInfo.h:164
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
Attributes that may be specified on loops.
Definition: CGLoopInfo.h:36
unsigned UnrollAndJamCount
llvm.unroll.
Definition: CGLoopInfo.h:65
Information used when generating a structured loop.
Definition: CGLoopInfo.h:72
virtual void clear()
LVEnableState UnrollEnable
Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Definition: CGLoopInfo.h:50
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:59
void setUnrollAndJamCount(unsigned C)
Set the unroll count for the next loop pushed.
Definition: CGLoopInfo.h:167
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:158
LVEnableState UnrollAndJamEnable
Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
Definition: CGLoopInfo.h:53
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:161
unsigned UnrollCount
llvm.unroll.
Definition: CGLoopInfo.h:62
void setParallel(bool Enable=true)
Set the next pushed loop as parallel.
Definition: CGLoopInfo.h:133
LVEnableState DistributeEnable
Value for llvm.loop.distribute.enable metadata.
Definition: CGLoopInfo.h:68
llvm::MDNode * getLoopID() const
Get the loop id metadata for this loop.
Definition: CGLoopInfo.h:79
llvm::MDNode * getCurLoopID() const
Return the top loop id metadata.
Definition: CGLoopInfo.h:121
void setDistributeState(bool Enable=true)
Set the next pushed loop as a distribution candidate.
Definition: CGLoopInfo.h:142
Dataflow Directional Tag Classes.
static const TypeInfo & getInfo(unsigned id)
Definition: Types.cpp:34
A stack of loop information corresponding to loop nesting levels.
Definition: CGLoopInfo.h:99
bool getCurLoopParallel() const
Return true if the top loop is parallel.
Definition: CGLoopInfo.h:124
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:148
void setUnrollAndJamState(const LoopAttributes::LVEnableState &State)
Set the next pushed loop unroll_and_jam state.
Definition: CGLoopInfo.h:153
void setVectorizeEnable(bool Enable=true)
Set the next pushed loop &#39;vectorize.enable&#39;.
Definition: CGLoopInfo.h:136
unsigned VectorizeWidth
Value for llvm.loop.vectorize.width metadata.
Definition: CGLoopInfo.h:56
llvm::BasicBlock * getHeader() const
Get the header block of this loop.
Definition: CGLoopInfo.h:82
const LoopAttributes & getAttributes() const
Get the set of attributes active for this loop.
Definition: CGLoopInfo.h:85