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/DenseMap.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/IR/Value.h"
22 #include "llvm/Support/Compiler.h"
28 } // end namespace llvm
34 /// \brief Attributes that may be specified on loops.
35 struct LoopAttributes {
36 explicit LoopAttributes(bool IsParallel = false);
39 /// \brief Generate llvm.loop.parallel metadata for loads and stores.
42 /// \brief Values of llvm.loop.vectorize.enable metadata.
43 enum LVEnableState { VecUnspecified, VecEnable, VecDisable };
45 /// \brief llvm.loop.vectorize.enable
46 LVEnableState VectorizerEnable;
48 /// \brief llvm.loop.vectorize.width
49 unsigned VectorizerWidth;
51 /// \brief llvm.loop.interleave.count
52 unsigned VectorizerUnroll;
55 /// \brief Information used when generating a structured loop.
58 /// \brief Construct a new LoopInfo for the loop with entry Header.
59 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs);
61 /// \brief Get the loop id metadata for this loop.
62 llvm::MDNode *getLoopID() const { return LoopID; }
64 /// \brief Get the header block of this loop.
65 llvm::BasicBlock *getHeader() const { return Header; }
67 /// \brief Get the set of attributes active for this loop.
68 const LoopAttributes &getAttributes() const { return Attrs; }
71 /// \brief Loop ID metadata.
73 /// \brief Header block of this loop.
74 llvm::BasicBlock *Header;
75 /// \brief The attributes for this loop.
79 /// \brief A stack of loop information corresponding to loop nesting levels.
80 /// This stack can be used to prepare attributes which are applied when a loop
83 LoopInfoStack(const LoopInfoStack &) = delete;
84 void operator=(const LoopInfoStack &) = delete;
89 /// \brief Begin a new structured loop. The set of staged attributes will be
90 /// applied to the loop and then cleared.
91 void push(llvm::BasicBlock *Header,
92 llvm::ArrayRef<const Attr *> Attrs = llvm::None);
94 /// \brief End the current loop.
97 /// \brief Return the top loop id metadata.
98 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
100 /// \brief Return true if the top loop is parallel.
101 bool getCurLoopParallel() const {
102 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
105 /// \brief Function called by the CodeGenFunction when an instruction is
107 void InsertHelper(llvm::Instruction *I) const;
109 /// \brief Set the next pushed loop as parallel.
110 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
112 /// \brief Set the next pushed loop 'vectorizer.enable'
113 void setVectorizerEnable(bool Enable = true) {
114 StagedAttrs.VectorizerEnable =
115 Enable ? LoopAttributes::VecEnable : LoopAttributes::VecDisable;
118 /// \brief Set the vectorizer width for the next loop pushed.
119 void setVectorizerWidth(unsigned W) { StagedAttrs.VectorizerWidth = W; }
121 /// \brief Set the vectorizer unroll for the next loop pushed.
122 void setVectorizerUnroll(unsigned U) { StagedAttrs.VectorizerUnroll = U; }
125 /// \brief Returns true if there is LoopInfo on the stack.
126 bool hasInfo() const { return !Active.empty(); }
127 /// \brief Return the LoopInfo for the current loop. HasInfo should be called
128 /// first to ensure LoopInfo is present.
129 const LoopInfo &getInfo() const { return Active.back(); }
130 /// \brief The set of attributes that will be applied to the next pushed loop.
131 LoopAttributes StagedAttrs;
132 /// \brief Stack of active loops.
133 llvm::SmallVector<LoopInfo, 4> Active;
136 } // end namespace CodeGen
137 } // end namespace clang