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/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/IR/Value.h"
21 #include "llvm/Support/Compiler.h"
27 } // end namespace llvm
32 /// \brief Attributes that may be specified on loops.
33 struct LoopAttributes {
34 explicit LoopAttributes(bool IsParallel = false);
37 /// \brief Generate llvm.loop.parallel metadata for loads and stores.
40 /// \brief Values of llvm.loop.vectorize.enable metadata.
41 enum LVEnableState { VecUnspecified, VecEnable, VecDisable };
43 /// \brief llvm.loop.vectorize.enable
44 LVEnableState VectorizerEnable;
46 /// \brief llvm.loop.vectorize.width
47 unsigned VectorizerWidth;
49 /// \brief llvm.loop.interleave.count
50 unsigned VectorizerUnroll;
53 /// \brief Information used when generating a structured loop.
56 /// \brief Construct a new LoopInfo for the loop with entry Header.
57 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs);
59 /// \brief Get the loop id metadata for this loop.
60 llvm::MDNode *getLoopID() const { return LoopID; }
62 /// \brief Get the header block of this loop.
63 llvm::BasicBlock *getHeader() const { return Header; }
65 /// \brief Get the set of attributes active for this loop.
66 const LoopAttributes &getAttributes() const { return Attrs; }
69 /// \brief Loop ID metadata.
71 /// \brief Header block of this loop.
72 llvm::BasicBlock *Header;
73 /// \brief The attributes for this loop.
77 /// \brief A stack of loop information corresponding to loop nesting levels.
78 /// This stack can be used to prepare attributes which are applied when a loop
81 LoopInfoStack(const LoopInfoStack &) = delete;
82 void operator=(const LoopInfoStack &) = delete;
87 /// \brief Begin a new structured loop. The set of staged attributes will be
88 /// applied to the loop and then cleared.
89 void push(llvm::BasicBlock *Header);
91 /// \brief End the current loop.
94 /// \brief Return the top loop id metadata.
95 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
97 /// \brief Return true if the top loop is parallel.
98 bool getCurLoopParallel() const {
99 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
102 /// \brief Function called by the CodeGenFunction when an instruction is
104 void InsertHelper(llvm::Instruction *I) const;
106 /// \brief Set the next pushed loop as parallel.
107 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
109 /// \brief Set the next pushed loop 'vectorizer.enable'
110 void setVectorizerEnable(bool Enable = true) {
111 StagedAttrs.VectorizerEnable =
112 Enable ? LoopAttributes::VecEnable : LoopAttributes::VecDisable;
115 /// \brief Set the vectorizer width for the next loop pushed.
116 void setVectorizerWidth(unsigned W) { StagedAttrs.VectorizerWidth = W; }
118 /// \brief Set the vectorizer unroll for the next loop pushed.
119 void setVectorizerUnroll(unsigned U) { StagedAttrs.VectorizerUnroll = U; }
122 /// \brief Returns true if there is LoopInfo on the stack.
123 bool hasInfo() const { return !Active.empty(); }
124 /// \brief Return the LoopInfo for the current loop. HasInfo should be called
125 /// first to ensure LoopInfo is present.
126 const LoopInfo &getInfo() const { return Active.back(); }
127 /// \brief The set of attributes that will be applied to the next pushed loop.
128 LoopAttributes StagedAttrs;
129 /// \brief Stack of active loops.
130 llvm::SmallVector<LoopInfo, 4> Active;
133 } // end namespace CodeGen
134 } // end namespace clang