blob: 201cbb7894bbad99177fc79fdca667736fc80a23 [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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000035/// Attributes that may be specified on loops.
Alexander Musman515ad8c2014-05-22 08:54:05 +000036struct LoopAttributes {
37 explicit LoopAttributes(bool IsParallel = false);
38 void clear();
39
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000040 /// Generate llvm.loop.parallel metadata for loads and stores.
Alexander Musman515ad8c2014-05-22 08:54:05 +000041 bool IsParallel;
42
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000043 /// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046 /// Value for llvm.loop.vectorize.enable metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000047 LVEnableState VectorizeEnable;
Alexander Musman515ad8c2014-05-22 08:54:05 +000048
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000049 /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Tyler Nowicki54c020d2015-07-27 20:10:20 +000050 LVEnableState UnrollEnable;
51
David Greenc8e39242018-08-01 14:36:12 +000052 /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
53 LVEnableState UnrollAndJamEnable;
54
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000055 /// Value for llvm.loop.vectorize.width metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000056 unsigned VectorizeWidth;
Alexander Musman515ad8c2014-05-22 08:54:05 +000057
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000058 /// Value for llvm.loop.interleave.count metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000059 unsigned InterleaveCount;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000060
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000061 /// llvm.unroll.
Tyler Nowicki54c020d2015-07-27 20:10:20 +000062 unsigned UnrollCount;
Adam Nemet2de463e2016-06-14 12:04:26 +000063
David Greenc8e39242018-08-01 14:36:12 +000064 /// llvm.unroll.
65 unsigned UnrollAndJamCount;
66
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000067 /// Value for llvm.loop.distribute.enable metadata.
Adam Nemet2de463e2016-06-14 12:04:26 +000068 LVEnableState DistributeEnable;
Alexander Musman515ad8c2014-05-22 08:54:05 +000069};
70
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000071/// Information used when generating a structured loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000072class LoopInfo {
73public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000074 /// Construct a new LoopInfo for the loop with entry Header.
Hal Finkelc07e19b2016-05-25 21:53:24 +000075 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +000076 const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc);
Alexander Musman515ad8c2014-05-22 08:54:05 +000077
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000078 /// Get the loop id metadata for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000079 llvm::MDNode *getLoopID() const { return LoopID; }
80
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000081 /// Get the header block of this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000082 llvm::BasicBlock *getHeader() const { return Header; }
83
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000084 /// Get the set of attributes active for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000085 const LoopAttributes &getAttributes() const { return Attrs; }
86
Michael Kruse05351372018-12-20 21:24:54 +000087 /// Return this loop's access group or nullptr if it does not have one.
88 llvm::MDNode *getAccessGroup() const { return AccGroup; }
89
Alexander Musman515ad8c2014-05-22 08:54:05 +000090private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000091 /// Loop ID metadata.
Alexander Musman515ad8c2014-05-22 08:54:05 +000092 llvm::MDNode *LoopID;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000093 /// Header block of this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000094 llvm::BasicBlock *Header;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000095 /// The attributes for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000096 LoopAttributes Attrs;
Michael Kruse05351372018-12-20 21:24:54 +000097 /// The access group for memory accesses parallel to this loop.
98 llvm::MDNode *AccGroup = nullptr;
Alexander Musman515ad8c2014-05-22 08:54:05 +000099};
100
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000101/// A stack of loop information corresponding to loop nesting levels.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000102/// This stack can be used to prepare attributes which are applied when a loop
103/// is emitted.
104class LoopInfoStack {
Aaron Ballmanabc18922015-02-15 22:54:08 +0000105 LoopInfoStack(const LoopInfoStack &) = delete;
106 void operator=(const LoopInfoStack &) = delete;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000107
108public:
109 LoopInfoStack() {}
110
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000111 /// Begin a new structured loop. The set of staged attributes will be
Alexander Musman515ad8c2014-05-22 08:54:05 +0000112 /// applied to the loop and then cleared.
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000113 void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
114 const llvm::DebugLoc &EndLoc);
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000115
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000116 /// Begin a new structured loop. Stage attributes from the Attrs list.
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000117 /// The staged attributes are applied to the loop and then cleared.
118 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000119 llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
120 const llvm::DebugLoc &EndLoc);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000121
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000122 /// End the current loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000123 void pop();
124
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000125 /// Return the top loop id metadata.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000126 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
127
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000128 /// Return true if the top loop is parallel.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000129 bool getCurLoopParallel() const {
130 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
131 }
132
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000133 /// Function called by the CodeGenFunction when an instruction is
Alexander Musman515ad8c2014-05-22 08:54:05 +0000134 /// created.
135 void InsertHelper(llvm::Instruction *I) const;
136
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000137 /// Set the next pushed loop as parallel.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000138 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
139
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000140 /// Set the next pushed loop 'vectorize.enable'
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000141 void setVectorizeEnable(bool Enable = true) {
142 StagedAttrs.VectorizeEnable =
143 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000144 }
145
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000146 /// Set the next pushed loop as a distribution candidate.
Adam Nemet2de463e2016-06-14 12:04:26 +0000147 void setDistributeState(bool Enable = true) {
148 StagedAttrs.DistributeEnable =
149 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
150 }
151
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000152 /// Set the next pushed loop unroll state.
Mark Heffernan397a98d2015-08-10 17:29:39 +0000153 void setUnrollState(const LoopAttributes::LVEnableState &State) {
154 StagedAttrs.UnrollEnable = State;
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000155 }
156
David Greenc8e39242018-08-01 14:36:12 +0000157 /// Set the next pushed loop unroll_and_jam state.
158 void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
159 StagedAttrs.UnrollAndJamEnable = State;
160 }
161
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000162 /// Set the vectorize width for the next loop pushed.
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000163 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000164
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000165 /// Set the interleave count for the next loop pushed.
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000166 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000167
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000168 /// Set the unroll count for the next loop pushed.
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000169 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
170
David Greenc8e39242018-08-01 14:36:12 +0000171 /// \brief Set the unroll count for the next loop pushed.
172 void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
173
Alexander Musman515ad8c2014-05-22 08:54:05 +0000174private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000175 /// Returns true if there is LoopInfo on the stack.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000176 bool hasInfo() const { return !Active.empty(); }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000177 /// Return the LoopInfo for the current loop. HasInfo should be called
Alexander Musman515ad8c2014-05-22 08:54:05 +0000178 /// first to ensure LoopInfo is present.
179 const LoopInfo &getInfo() const { return Active.back(); }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000180 /// The set of attributes that will be applied to the next pushed loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000181 LoopAttributes StagedAttrs;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000182 /// Stack of active loops.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000183 llvm::SmallVector<LoopInfo, 4> Active;
184};
185
186} // end namespace CodeGen
187} // end namespace clang
188
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000189#endif