blob: 7b9980c61837d2dd330ff494e4646601a7ab4fe4 [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"
Hal Finkelc07e19b2016-05-25 21:53:24 +000021#include "llvm/IR/DebugLoc.h"
Alexander Musman515ad8c2014-05-22 08:54:05 +000022#include "llvm/IR/Value.h"
23#include "llvm/Support/Compiler.h"
24
25namespace llvm {
26class BasicBlock;
27class Instruction;
28class MDNode;
29} // end namespace llvm
30
31namespace clang {
Tyler Nowicki9d268e12015-06-11 23:23:17 +000032class Attr;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000033class ASTContext;
Alexander Musman515ad8c2014-05-22 08:54:05 +000034namespace CodeGen {
35
36/// \brief Attributes that may be specified on loops.
37struct LoopAttributes {
38 explicit LoopAttributes(bool IsParallel = false);
39 void clear();
40
41 /// \brief Generate llvm.loop.parallel metadata for loads and stores.
42 bool IsParallel;
43
Tyler Nowickida46d0e2015-07-14 23:03:09 +000044 /// \brief State of loop vectorization or unrolling.
Mark Heffernan397a98d2015-08-10 17:29:39 +000045 enum LVEnableState { Unspecified, Enable, Disable, Full };
Alexander Musman515ad8c2014-05-22 08:54:05 +000046
Tyler Nowickida46d0e2015-07-14 23:03:09 +000047 /// \brief Value for llvm.loop.vectorize.enable metadata.
48 LVEnableState VectorizeEnable;
Alexander Musman515ad8c2014-05-22 08:54:05 +000049
Mark Heffernan397a98d2015-08-10 17:29:39 +000050 /// \brief Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Tyler Nowicki54c020d2015-07-27 20:10:20 +000051 LVEnableState UnrollEnable;
52
Tyler Nowickida46d0e2015-07-14 23:03:09 +000053 /// \brief Value for llvm.loop.vectorize.width metadata.
54 unsigned VectorizeWidth;
Alexander Musman515ad8c2014-05-22 08:54:05 +000055
Tyler Nowickida46d0e2015-07-14 23:03:09 +000056 /// \brief Value for llvm.loop.interleave.count metadata.
57 unsigned InterleaveCount;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000058
59 /// \brief llvm.unroll.
60 unsigned UnrollCount;
Alexander Musman515ad8c2014-05-22 08:54:05 +000061};
62
63/// \brief Information used when generating a structured loop.
64class LoopInfo {
65public:
66 /// \brief Construct a new LoopInfo for the loop with entry Header.
Hal Finkelc07e19b2016-05-25 21:53:24 +000067 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
68 llvm::DebugLoc Location);
Alexander Musman515ad8c2014-05-22 08:54:05 +000069
70 /// \brief Get the loop id metadata for this loop.
71 llvm::MDNode *getLoopID() const { return LoopID; }
72
73 /// \brief Get the header block of this loop.
74 llvm::BasicBlock *getHeader() const { return Header; }
75
76 /// \brief Get the set of attributes active for this loop.
77 const LoopAttributes &getAttributes() const { return Attrs; }
78
79private:
80 /// \brief Loop ID metadata.
81 llvm::MDNode *LoopID;
82 /// \brief Header block of this loop.
83 llvm::BasicBlock *Header;
84 /// \brief The attributes for this loop.
85 LoopAttributes Attrs;
86};
87
88/// \brief A stack of loop information corresponding to loop nesting levels.
89/// This stack can be used to prepare attributes which are applied when a loop
90/// is emitted.
91class LoopInfoStack {
Aaron Ballmanabc18922015-02-15 22:54:08 +000092 LoopInfoStack(const LoopInfoStack &) = delete;
93 void operator=(const LoopInfoStack &) = delete;
Alexander Musman515ad8c2014-05-22 08:54:05 +000094
95public:
96 LoopInfoStack() {}
97
98 /// \brief Begin a new structured loop. The set of staged attributes will be
99 /// applied to the loop and then cleared.
Hal Finkelc07e19b2016-05-25 21:53:24 +0000100 void push(llvm::BasicBlock *Header,
101 llvm::DebugLoc Location = llvm::DebugLoc());
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000102
103 /// \brief Begin a new structured loop. Stage attributes from the Attrs list.
104 /// The staged attributes are applied to the loop and then cleared.
105 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
Hal Finkelc07e19b2016-05-25 21:53:24 +0000106 llvm::ArrayRef<const Attr *> Attrs,
107 llvm::DebugLoc Location = llvm::DebugLoc());
Alexander Musman515ad8c2014-05-22 08:54:05 +0000108
109 /// \brief End the current loop.
110 void pop();
111
112 /// \brief Return the top loop id metadata.
113 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
114
115 /// \brief Return true if the top loop is parallel.
116 bool getCurLoopParallel() const {
117 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
118 }
119
120 /// \brief Function called by the CodeGenFunction when an instruction is
121 /// created.
122 void InsertHelper(llvm::Instruction *I) const;
123
124 /// \brief Set the next pushed loop as parallel.
125 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
126
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000127 /// \brief Set the next pushed loop 'vectorize.enable'
128 void setVectorizeEnable(bool Enable = true) {
129 StagedAttrs.VectorizeEnable =
130 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000131 }
132
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000133 /// \brief Set the next pushed loop unroll state.
Mark Heffernan397a98d2015-08-10 17:29:39 +0000134 void setUnrollState(const LoopAttributes::LVEnableState &State) {
135 StagedAttrs.UnrollEnable = State;
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000136 }
137
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000138 /// \brief Set the vectorize width for the next loop pushed.
139 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000140
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000141 /// \brief Set the interleave count for the next loop pushed.
142 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000143
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000144 /// \brief Set the unroll count for the next loop pushed.
145 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
146
Alexander Musman515ad8c2014-05-22 08:54:05 +0000147private:
148 /// \brief Returns true if there is LoopInfo on the stack.
149 bool hasInfo() const { return !Active.empty(); }
150 /// \brief Return the LoopInfo for the current loop. HasInfo should be called
151 /// first to ensure LoopInfo is present.
152 const LoopInfo &getInfo() const { return Active.back(); }
153 /// \brief The set of attributes that will be applied to the next pushed loop.
154 LoopAttributes StagedAttrs;
155 /// \brief Stack of active loops.
156 llvm::SmallVector<LoopInfo, 4> Active;
157};
158
159} // end namespace CodeGen
160} // end namespace clang
161
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000162#endif