blob: 15608c105dc7e9c1baa105f990c11d2f900d48aa [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/SmallVector.h"
Hal Finkelc07e19b2016-05-25 21:53:24 +000020#include "llvm/IR/DebugLoc.h"
Alexander Musman515ad8c2014-05-22 08:54:05 +000021#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.
Mark Heffernan397a98d2015-08-10 17:29:39 +000044 enum LVEnableState { Unspecified, Enable, Disable, Full };
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
Mark Heffernan397a98d2015-08-10 17:29:39 +000049 /// \brief Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Tyler Nowicki54c020d2015-07-27 20:10:20 +000050 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;
Adam Nemet2de463e2016-06-14 12:04:26 +000060
61 /// \brief Value for llvm.loop.distribute.enable metadata.
62 LVEnableState DistributeEnable;
Alexander Musman515ad8c2014-05-22 08:54:05 +000063};
64
65/// \brief Information used when generating a structured loop.
66class LoopInfo {
67public:
68 /// \brief Construct a new LoopInfo for the loop with entry Header.
Hal Finkelc07e19b2016-05-25 21:53:24 +000069 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +000070 const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc);
Alexander Musman515ad8c2014-05-22 08:54:05 +000071
72 /// \brief Get the loop id metadata for this loop.
73 llvm::MDNode *getLoopID() const { return LoopID; }
74
75 /// \brief Get the header block of this loop.
76 llvm::BasicBlock *getHeader() const { return Header; }
77
78 /// \brief Get the set of attributes active for this loop.
79 const LoopAttributes &getAttributes() const { return Attrs; }
80
81private:
82 /// \brief Loop ID metadata.
83 llvm::MDNode *LoopID;
84 /// \brief Header block of this loop.
85 llvm::BasicBlock *Header;
86 /// \brief The attributes for this loop.
87 LoopAttributes Attrs;
88};
89
90/// \brief A stack of loop information corresponding to loop nesting levels.
91/// This stack can be used to prepare attributes which are applied when a loop
92/// is emitted.
93class LoopInfoStack {
Aaron Ballmanabc18922015-02-15 22:54:08 +000094 LoopInfoStack(const LoopInfoStack &) = delete;
95 void operator=(const LoopInfoStack &) = delete;
Alexander Musman515ad8c2014-05-22 08:54:05 +000096
97public:
98 LoopInfoStack() {}
99
100 /// \brief Begin a new structured loop. The set of staged attributes will be
101 /// applied to the loop and then cleared.
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000102 void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
103 const llvm::DebugLoc &EndLoc);
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000104
105 /// \brief Begin a new structured loop. Stage attributes from the Attrs list.
106 /// The staged attributes are applied to the loop and then cleared.
107 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000108 llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
109 const llvm::DebugLoc &EndLoc);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000110
111 /// \brief End the current loop.
112 void pop();
113
114 /// \brief Return the top loop id metadata.
115 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
116
117 /// \brief Return true if the top loop is parallel.
118 bool getCurLoopParallel() const {
119 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
120 }
121
122 /// \brief Function called by the CodeGenFunction when an instruction is
123 /// created.
124 void InsertHelper(llvm::Instruction *I) const;
125
126 /// \brief Set the next pushed loop as parallel.
127 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
128
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000129 /// \brief Set the next pushed loop 'vectorize.enable'
130 void setVectorizeEnable(bool Enable = true) {
131 StagedAttrs.VectorizeEnable =
132 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000133 }
134
Adam Nemet2de463e2016-06-14 12:04:26 +0000135 /// \brief Set the next pushed loop as a distribution candidate.
136 void setDistributeState(bool Enable = true) {
137 StagedAttrs.DistributeEnable =
138 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
139 }
140
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000141 /// \brief Set the next pushed loop unroll state.
Mark Heffernan397a98d2015-08-10 17:29:39 +0000142 void setUnrollState(const LoopAttributes::LVEnableState &State) {
143 StagedAttrs.UnrollEnable = State;
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000144 }
145
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000146 /// \brief Set the vectorize width for the next loop pushed.
147 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000148
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000149 /// \brief Set the interleave count for the next loop pushed.
150 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000151
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000152 /// \brief Set the unroll count for the next loop pushed.
153 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
154
Alexander Musman515ad8c2014-05-22 08:54:05 +0000155private:
156 /// \brief Returns true if there is LoopInfo on the stack.
157 bool hasInfo() const { return !Active.empty(); }
158 /// \brief Return the LoopInfo for the current loop. HasInfo should be called
159 /// first to ensure LoopInfo is present.
160 const LoopInfo &getInfo() const { return Active.back(); }
161 /// \brief The set of attributes that will be applied to the next pushed loop.
162 LoopAttributes StagedAttrs;
163 /// \brief Stack of active loops.
164 llvm::SmallVector<LoopInfo, 4> Active;
165};
166
167} // end namespace CodeGen
168} // end namespace clang
169
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000170#endif