1 //===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- C++ -*---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the internal state used for llvm translation for loop statement
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
16 #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
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"
28 } // end namespace llvm
35 /// Attributes that may be specified on loops.
36 struct LoopAttributes {
37 explicit LoopAttributes(bool IsParallel = false);
40 /// Generate llvm.loop.parallel metadata for loads and stores.
43 /// State of loop vectorization or unrolling.
44 enum LVEnableState { Unspecified, Enable, Disable, Full };
46 /// Value for llvm.loop.vectorize.enable metadata.
47 LVEnableState VectorizeEnable;
49 /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
50 LVEnableState UnrollEnable;
52 /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
53 LVEnableState UnrollAndJamEnable;
55 /// Value for llvm.loop.vectorize.width metadata.
56 unsigned VectorizeWidth;
58 /// Value for llvm.loop.interleave.count metadata.
59 unsigned InterleaveCount;
65 unsigned UnrollAndJamCount;
67 /// Value for llvm.loop.distribute.enable metadata.
68 LVEnableState DistributeEnable;
70 /// Value for llvm.loop.pipeline.disable metadata.
71 bool PipelineDisabled;
73 /// Value for llvm.loop.pipeline.iicount metadata.
74 unsigned PipelineInitiationInterval;
77 /// Information used when generating a structured loop.
80 /// Construct a new LoopInfo for the loop with entry Header.
81 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
82 const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc);
84 /// Get the loop id metadata for this loop.
85 llvm::MDNode *getLoopID() const { return LoopID; }
87 /// Get the header block of this loop.
88 llvm::BasicBlock *getHeader() const { return Header; }
90 /// Get the set of attributes active for this loop.
91 const LoopAttributes &getAttributes() const { return Attrs; }
93 /// Return this loop's access group or nullptr if it does not have one.
94 llvm::MDNode *getAccessGroup() const { return AccGroup; }
99 /// Header block of this loop.
100 llvm::BasicBlock *Header;
101 /// The attributes for this loop.
102 LoopAttributes Attrs;
103 /// The access group for memory accesses parallel to this loop.
104 llvm::MDNode *AccGroup = nullptr;
107 /// A stack of loop information corresponding to loop nesting levels.
108 /// This stack can be used to prepare attributes which are applied when a loop
110 class LoopInfoStack {
111 LoopInfoStack(const LoopInfoStack &) = delete;
112 void operator=(const LoopInfoStack &) = delete;
117 /// Begin a new structured loop. The set of staged attributes will be
118 /// applied to the loop and then cleared.
119 void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
120 const llvm::DebugLoc &EndLoc);
122 /// Begin a new structured loop. Stage attributes from the Attrs list.
123 /// The staged attributes are applied to the loop and then cleared.
124 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
125 llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
126 const llvm::DebugLoc &EndLoc);
128 /// End the current loop.
131 /// Return the top loop id metadata.
132 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
134 /// Return true if the top loop is parallel.
135 bool getCurLoopParallel() const {
136 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
139 /// Function called by the CodeGenFunction when an instruction is
141 void InsertHelper(llvm::Instruction *I) const;
143 /// Set the next pushed loop as parallel.
144 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
146 /// Set the next pushed loop 'vectorize.enable'
147 void setVectorizeEnable(bool Enable = true) {
148 StagedAttrs.VectorizeEnable =
149 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
152 /// Set the next pushed loop as a distribution candidate.
153 void setDistributeState(bool Enable = true) {
154 StagedAttrs.DistributeEnable =
155 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
158 /// Set the next pushed loop unroll state.
159 void setUnrollState(const LoopAttributes::LVEnableState &State) {
160 StagedAttrs.UnrollEnable = State;
163 /// Set the next pushed loop unroll_and_jam state.
164 void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
165 StagedAttrs.UnrollAndJamEnable = State;
168 /// Set the vectorize width for the next loop pushed.
169 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
171 /// Set the interleave count for the next loop pushed.
172 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
174 /// Set the unroll count for the next loop pushed.
175 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
177 /// \brief Set the unroll count for the next loop pushed.
178 void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
180 /// Set the pipeline disabled state.
181 void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
183 /// Set the pipeline initiation interval.
184 void setPipelineInitiationInterval(unsigned C) {
185 StagedAttrs.PipelineInitiationInterval = C;
189 /// Returns true if there is LoopInfo on the stack.
190 bool hasInfo() const { return !Active.empty(); }
191 /// Return the LoopInfo for the current loop. HasInfo should be called
192 /// first to ensure LoopInfo is present.
193 const LoopInfo &getInfo() const { return Active.back(); }
194 /// The set of attributes that will be applied to the next pushed loop.
195 LoopAttributes StagedAttrs;
196 /// Stack of active loops.
197 llvm::SmallVector<LoopInfo, 4> Active;
200 } // end namespace CodeGen
201 } // end namespace clang