blob: aee162118a2f5ccac4970a3e11021e15a3c9af73 [file] [log] [blame]
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001//===---- 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
Stephen Hines176edba2014-12-01 14:53:08 -080015#ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
16#define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
Stephen Hines6bcf27b2014-05-29 04:14:42 -070017
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
23namespace llvm {
24class BasicBlock;
25class Instruction;
26class MDNode;
27} // end namespace llvm
28
29namespace clang {
30namespace CodeGen {
31
32/// \brief Attributes that may be specified on loops.
33struct 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
Stephen Hinesc568f1e2014-07-21 00:47:37 -070040 /// \brief Values of llvm.loop.vectorize.enable metadata.
Stephen Hines6bcf27b2014-05-29 04:14:42 -070041 enum LVEnableState { VecUnspecified, VecEnable, VecDisable };
42
Stephen Hinesc568f1e2014-07-21 00:47:37 -070043 /// \brief llvm.loop.vectorize.enable
Stephen Hines6bcf27b2014-05-29 04:14:42 -070044 LVEnableState VectorizerEnable;
45
Stephen Hinesc568f1e2014-07-21 00:47:37 -070046 /// \brief llvm.loop.vectorize.width
Stephen Hines6bcf27b2014-05-29 04:14:42 -070047 unsigned VectorizerWidth;
48
Stephen Hines176edba2014-12-01 14:53:08 -080049 /// \brief llvm.loop.interleave.count
Stephen Hines6bcf27b2014-05-29 04:14:42 -070050 unsigned VectorizerUnroll;
51};
52
53/// \brief Information used when generating a structured loop.
54class LoopInfo {
55public:
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.
60 llvm::MDNode *getLoopID() const { return LoopID; }
61
62 /// \brief Get the header block of this loop.
63 llvm::BasicBlock *getHeader() const { return Header; }
64
65 /// \brief Get the set of attributes active for this loop.
66 const LoopAttributes &getAttributes() const { return Attrs; }
67
68private:
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.
80class LoopInfoStack {
Stephen Hines0e2c34f2015-03-23 12:09:02 -070081 LoopInfoStack(const LoopInfoStack &) = delete;
82 void operator=(const LoopInfoStack &) = delete;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070083
84public:
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.
95 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
96
97 /// \brief Return true if the top loop is parallel.
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.
116 void setVectorizerWidth(unsigned W) { StagedAttrs.VectorizerWidth = W; }
117
118 /// \brief Set the vectorizer unroll for the next loop pushed.
119 void setVectorizerUnroll(unsigned U) { StagedAttrs.VectorizerUnroll = U; }
120
121private:
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;
131};
132
133} // end namespace CodeGen
134} // end namespace clang
135
Stephen Hines176edba2014-12-01 14:53:08 -0800136#endif