blob: 686a218c7630cde3d9bc8bbb448c27e6e615a27e [file] [log] [blame]
Alexander Musman515ad8c2014-05-22 08:54:05 +00001//===---- 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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
16#define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
Alexander Musman515ad8c2014-05-22 08:54:05 +000017
Tyler Nowicki9d268e12015-06-11 23:23:17 +000018#include "llvm/ADT/ArrayRef.h"
Alexander Musman515ad8c2014-05-22 08:54:05 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/IR/Value.h"
22#include "llvm/Support/Compiler.h"
23
24namespace llvm {
25class BasicBlock;
26class Instruction;
27class MDNode;
28} // end namespace llvm
29
30namespace clang {
Tyler Nowicki9d268e12015-06-11 23:23:17 +000031class Attr;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000032class ASTContext;
Alexander Musman515ad8c2014-05-22 08:54:05 +000033namespace CodeGen {
34
35/// \brief Attributes that may be specified on loops.
36struct LoopAttributes {
37 explicit LoopAttributes(bool IsParallel = false);
38 void clear();
39
40 /// \brief Generate llvm.loop.parallel metadata for loads and stores.
41 bool IsParallel;
42
Tyler Nowickida46d0e2015-07-14 23:03:09 +000043 /// \brief State of loop vectorization or unrolling.
44 enum LVEnableState { Unspecified, Enable, Disable };
Alexander Musman515ad8c2014-05-22 08:54:05 +000045
Tyler Nowickida46d0e2015-07-14 23:03:09 +000046 /// \brief Value for llvm.loop.vectorize.enable metadata.
47 LVEnableState VectorizeEnable;
Alexander Musman515ad8c2014-05-22 08:54:05 +000048
Tyler Nowicki54c020d2015-07-27 20:10:20 +000049 /// \brief Selects no metadata, llvm.unroll.full, or llvm.unroll.disable.
50 LVEnableState UnrollEnable;
51
Tyler Nowickida46d0e2015-07-14 23:03:09 +000052 /// \brief Value for llvm.loop.vectorize.width metadata.
53 unsigned VectorizeWidth;
Alexander Musman515ad8c2014-05-22 08:54:05 +000054
Tyler Nowickida46d0e2015-07-14 23:03:09 +000055 /// \brief Value for llvm.loop.interleave.count metadata.
56 unsigned InterleaveCount;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000057
58 /// \brief llvm.unroll.
59 unsigned UnrollCount;
Alexander Musman515ad8c2014-05-22 08:54:05 +000060};
61
62/// \brief Information used when generating a structured loop.
63class LoopInfo {
64public:
65 /// \brief Construct a new LoopInfo for the loop with entry Header.
66 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs);
67
68 /// \brief Get the loop id metadata for this loop.
69 llvm::MDNode *getLoopID() const { return LoopID; }
70
71 /// \brief Get the header block of this loop.
72 llvm::BasicBlock *getHeader() const { return Header; }
73
74 /// \brief Get the set of attributes active for this loop.
75 const LoopAttributes &getAttributes() const { return Attrs; }
76
77private:
78 /// \brief Loop ID metadata.
79 llvm::MDNode *LoopID;
80 /// \brief Header block of this loop.
81 llvm::BasicBlock *Header;
82 /// \brief The attributes for this loop.
83 LoopAttributes Attrs;
84};
85
86/// \brief A stack of loop information corresponding to loop nesting levels.
87/// This stack can be used to prepare attributes which are applied when a loop
88/// is emitted.
89class LoopInfoStack {
Aaron Ballmanabc18922015-02-15 22:54:08 +000090 LoopInfoStack(const LoopInfoStack &) = delete;
91 void operator=(const LoopInfoStack &) = delete;
Alexander Musman515ad8c2014-05-22 08:54:05 +000092
93public:
94 LoopInfoStack() {}
95
96 /// \brief Begin a new structured loop. The set of staged attributes will be
97 /// applied to the loop and then cleared.
Tyler Nowicki54c020d2015-07-27 20:10:20 +000098 void push(llvm::BasicBlock *Header);
99
100 /// \brief Begin a new structured loop. Stage attributes from the Attrs list.
101 /// The staged attributes are applied to the loop and then cleared.
102 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
103 llvm::ArrayRef<const Attr *> Attrs);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000104
105 /// \brief End the current loop.
106 void pop();
107
108 /// \brief Return the top loop id metadata.
109 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
110
111 /// \brief Return true if the top loop is parallel.
112 bool getCurLoopParallel() const {
113 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
114 }
115
116 /// \brief Function called by the CodeGenFunction when an instruction is
117 /// created.
118 void InsertHelper(llvm::Instruction *I) const;
119
120 /// \brief Set the next pushed loop as parallel.
121 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
122
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000123 /// \brief Set the next pushed loop 'vectorize.enable'
124 void setVectorizeEnable(bool Enable = true) {
125 StagedAttrs.VectorizeEnable =
126 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000127 }
128
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000129 /// \brief Set the next pushed loop unroll state.
130 void setUnrollEnable(bool Enable = true) {
131 StagedAttrs.UnrollEnable =
132 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
133 }
134
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000135 /// \brief Set the vectorize width for the next loop pushed.
136 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000137
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000138 /// \brief Set the interleave count for the next loop pushed.
139 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000140
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000141 /// \brief Set the unroll count for the next loop pushed.
142 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
143
Alexander Musman515ad8c2014-05-22 08:54:05 +0000144private:
145 /// \brief Returns true if there is LoopInfo on the stack.
146 bool hasInfo() const { return !Active.empty(); }
147 /// \brief Return the LoopInfo for the current loop. HasInfo should be called
148 /// first to ensure LoopInfo is present.
149 const LoopInfo &getInfo() const { return Active.back(); }
150 /// \brief The set of attributes that will be applied to the next pushed loop.
151 LoopAttributes StagedAttrs;
152 /// \brief Stack of active loops.
153 llvm::SmallVector<LoopInfo, 4> Active;
154};
155
156} // end namespace CodeGen
157} // end namespace clang
158
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000159#endif