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