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/DenseMap.h"
19 #include "llvm/ADT/SmallVector.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 namespace CodeGen {
31 
32 /// \brief Attributes that may be specified on loops.
33 struct LoopAttributes {
34   explicit LoopAttributes(bool IsParallel = false);
35   void clear();
36 
37   /// \brief Generate llvm.loop.parallel metadata for loads and stores.
38   bool IsParallel;
39 
40   /// \brief Values of llvm.loop.vectorize.enable metadata.
41   enum LVEnableState { VecUnspecified, VecEnable, VecDisable };
42 
43   /// \brief llvm.loop.vectorize.enable
44   LVEnableState VectorizerEnable;
45 
46   /// \brief llvm.loop.vectorize.width
47   unsigned VectorizerWidth;
48 
49   /// \brief llvm.loop.interleave.count
50   unsigned VectorizerUnroll;
51 };
52 
53 /// \brief Information used when generating a structured loop.
54 class LoopInfo {
55 public:
56   /// \brief Construct a new LoopInfo for the loop with entry Header.
57   LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs);
58 
59   /// \brief Get the loop id metadata for this loop.
getLoopID()60   llvm::MDNode *getLoopID() const { return LoopID; }
61 
62   /// \brief Get the header block of this loop.
getHeader()63   llvm::BasicBlock *getHeader() const { return Header; }
64 
65   /// \brief Get the set of attributes active for this loop.
getAttributes()66   const LoopAttributes &getAttributes() const { return Attrs; }
67 
68 private:
69   /// \brief Loop ID metadata.
70   llvm::MDNode *LoopID;
71   /// \brief Header block of this loop.
72   llvm::BasicBlock *Header;
73   /// \brief The attributes for this loop.
74   LoopAttributes Attrs;
75 };
76 
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
79 /// is emitted.
80 class LoopInfoStack {
81   LoopInfoStack(const LoopInfoStack &) LLVM_DELETED_FUNCTION;
82   void operator=(const LoopInfoStack &) LLVM_DELETED_FUNCTION;
83 
84 public:
LoopInfoStack()85   LoopInfoStack() {}
86 
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);
90 
91   /// \brief End the current loop.
92   void pop();
93 
94   /// \brief Return the top loop id metadata.
getCurLoopID()95   llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
96 
97   /// \brief Return true if the top loop is parallel.
getCurLoopParallel()98   bool getCurLoopParallel() const {
99     return hasInfo() ? getInfo().getAttributes().IsParallel : false;
100   }
101 
102   /// \brief Function called by the CodeGenFunction when an instruction is
103   /// created.
104   void InsertHelper(llvm::Instruction *I) const;
105 
106   /// \brief Set the next pushed loop as parallel.
107   void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
108 
109   /// \brief Set the next pushed loop 'vectorizer.enable'
110   void setVectorizerEnable(bool Enable = true) {
111     StagedAttrs.VectorizerEnable =
112         Enable ? LoopAttributes::VecEnable : LoopAttributes::VecDisable;
113   }
114 
115   /// \brief Set the vectorizer width for the next loop pushed.
setVectorizerWidth(unsigned W)116   void setVectorizerWidth(unsigned W) { StagedAttrs.VectorizerWidth = W; }
117 
118   /// \brief Set the vectorizer unroll for the next loop pushed.
setVectorizerUnroll(unsigned U)119   void setVectorizerUnroll(unsigned U) { StagedAttrs.VectorizerUnroll = U; }
120 
121 private:
122   /// \brief Returns true if there is LoopInfo on the stack.
hasInfo()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.
getInfo()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;
131 };
132 
133 } // end namespace CodeGen
134 } // end namespace clang
135 
136 #endif
137