blob: 115685ebf2eecdc81d5de52af5f1dde80e4badb0 [file] [log] [blame]
Alexander Musman515ad8c2014-05-22 08:54:05 +00001//===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- C++ -*---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexander Musman515ad8c2014-05-22 08:54:05 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This is the internal state used for llvm translation for loop statement
10// metadata.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
15#define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
Alexander Musman515ad8c2014-05-22 08:54:05 +000016
Tyler Nowicki9d268e12015-06-11 23:23:17 +000017#include "llvm/ADT/ArrayRef.h"
Alexander Musman515ad8c2014-05-22 08:54:05 +000018#include "llvm/ADT/SmallVector.h"
Hal Finkelc07e19b2016-05-25 21:53:24 +000019#include "llvm/IR/DebugLoc.h"
Alexander Musman515ad8c2014-05-22 08:54:05 +000020#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 {
Tyler Nowicki9d268e12015-06-11 23:23:17 +000030class Attr;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000031class ASTContext;
Alexander Musman515ad8c2014-05-22 08:54:05 +000032namespace CodeGen {
33
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000034/// Attributes that may be specified on loops.
Alexander Musman515ad8c2014-05-22 08:54:05 +000035struct LoopAttributes {
36 explicit LoopAttributes(bool IsParallel = false);
37 void clear();
38
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000039 /// Generate llvm.loop.parallel metadata for loads and stores.
Alexander Musman515ad8c2014-05-22 08:54:05 +000040 bool IsParallel;
41
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000042 /// State of loop vectorization or unrolling.
Mark Heffernan397a98d2015-08-10 17:29:39 +000043 enum LVEnableState { Unspecified, Enable, Disable, Full };
Alexander Musman515ad8c2014-05-22 08:54:05 +000044
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000045 /// Value for llvm.loop.vectorize.enable metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000046 LVEnableState VectorizeEnable;
Alexander Musman515ad8c2014-05-22 08:54:05 +000047
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000048 /// Value for llvm.loop.unroll.* metadata (enable, disable, or full).
Tyler Nowicki54c020d2015-07-27 20:10:20 +000049 LVEnableState UnrollEnable;
50
David Greenc8e39242018-08-01 14:36:12 +000051 /// Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
52 LVEnableState UnrollAndJamEnable;
53
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000054 /// Value for llvm.loop.vectorize.width metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000055 unsigned VectorizeWidth;
Alexander Musman515ad8c2014-05-22 08:54:05 +000056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057 /// Value for llvm.loop.interleave.count metadata.
Tyler Nowickida46d0e2015-07-14 23:03:09 +000058 unsigned InterleaveCount;
Tyler Nowicki54c020d2015-07-27 20:10:20 +000059
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000060 /// llvm.unroll.
Tyler Nowicki54c020d2015-07-27 20:10:20 +000061 unsigned UnrollCount;
Adam Nemet2de463e2016-06-14 12:04:26 +000062
David Greenc8e39242018-08-01 14:36:12 +000063 /// llvm.unroll.
64 unsigned UnrollAndJamCount;
65
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000066 /// Value for llvm.loop.distribute.enable metadata.
Adam Nemet2de463e2016-06-14 12:04:26 +000067 LVEnableState DistributeEnable;
Aaron Ballman9bdf5152019-01-04 17:20:00 +000068
69 /// Value for llvm.loop.pipeline.disable metadata.
70 bool PipelineDisabled;
71
72 /// Value for llvm.loop.pipeline.iicount metadata.
73 unsigned PipelineInitiationInterval;
Alexander Musman515ad8c2014-05-22 08:54:05 +000074};
75
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000076/// Information used when generating a structured loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000077class LoopInfo {
78public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000079 /// Construct a new LoopInfo for the loop with entry Header.
Hal Finkelc07e19b2016-05-25 21:53:24 +000080 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +000081 const llvm::DebugLoc &StartLoc, const llvm::DebugLoc &EndLoc);
Alexander Musman515ad8c2014-05-22 08:54:05 +000082
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000083 /// Get the loop id metadata for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000084 llvm::MDNode *getLoopID() const { return LoopID; }
85
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000086 /// Get the header block of this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000087 llvm::BasicBlock *getHeader() const { return Header; }
88
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000089 /// Get the set of attributes active for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000090 const LoopAttributes &getAttributes() const { return Attrs; }
91
Michael Kruse05351372018-12-20 21:24:54 +000092 /// Return this loop's access group or nullptr if it does not have one.
93 llvm::MDNode *getAccessGroup() const { return AccGroup; }
94
Alexander Musman515ad8c2014-05-22 08:54:05 +000095private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000096 /// Loop ID metadata.
Alexander Musman515ad8c2014-05-22 08:54:05 +000097 llvm::MDNode *LoopID;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000098 /// Header block of this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +000099 llvm::BasicBlock *Header;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000100 /// The attributes for this loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000101 LoopAttributes Attrs;
Michael Kruse05351372018-12-20 21:24:54 +0000102 /// The access group for memory accesses parallel to this loop.
103 llvm::MDNode *AccGroup = nullptr;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000104};
105
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000106/// A stack of loop information corresponding to loop nesting levels.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000107/// This stack can be used to prepare attributes which are applied when a loop
108/// is emitted.
109class LoopInfoStack {
Aaron Ballmanabc18922015-02-15 22:54:08 +0000110 LoopInfoStack(const LoopInfoStack &) = delete;
111 void operator=(const LoopInfoStack &) = delete;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000112
113public:
114 LoopInfoStack() {}
115
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000116 /// Begin a new structured loop. The set of staged attributes will be
Alexander Musman515ad8c2014-05-22 08:54:05 +0000117 /// applied to the loop and then cleared.
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000118 void push(llvm::BasicBlock *Header, const llvm::DebugLoc &StartLoc,
119 const llvm::DebugLoc &EndLoc);
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000120
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000121 /// Begin a new structured loop. Stage attributes from the Attrs list.
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000122 /// The staged attributes are applied to the loop and then cleared.
123 void push(llvm::BasicBlock *Header, clang::ASTContext &Ctx,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +0000124 llvm::ArrayRef<const Attr *> Attrs, const llvm::DebugLoc &StartLoc,
125 const llvm::DebugLoc &EndLoc);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000126
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000127 /// End the current loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000128 void pop();
129
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000130 /// Return the top loop id metadata.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000131 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
132
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000133 /// Return true if the top loop is parallel.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000134 bool getCurLoopParallel() const {
135 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
136 }
137
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000138 /// Function called by the CodeGenFunction when an instruction is
Alexander Musman515ad8c2014-05-22 08:54:05 +0000139 /// created.
140 void InsertHelper(llvm::Instruction *I) const;
141
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000142 /// Set the next pushed loop as parallel.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000143 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
144
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000145 /// Set the next pushed loop 'vectorize.enable'
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000146 void setVectorizeEnable(bool Enable = true) {
147 StagedAttrs.VectorizeEnable =
148 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000149 }
150
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000151 /// Set the next pushed loop as a distribution candidate.
Adam Nemet2de463e2016-06-14 12:04:26 +0000152 void setDistributeState(bool Enable = true) {
153 StagedAttrs.DistributeEnable =
154 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
155 }
156
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000157 /// Set the next pushed loop unroll state.
Mark Heffernan397a98d2015-08-10 17:29:39 +0000158 void setUnrollState(const LoopAttributes::LVEnableState &State) {
159 StagedAttrs.UnrollEnable = State;
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000160 }
161
David Greenc8e39242018-08-01 14:36:12 +0000162 /// Set the next pushed loop unroll_and_jam state.
163 void setUnrollAndJamState(const LoopAttributes::LVEnableState &State) {
164 StagedAttrs.UnrollAndJamEnable = State;
165 }
166
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000167 /// Set the vectorize width for the next loop pushed.
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000168 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000169
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000170 /// Set the interleave count for the next loop pushed.
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000171 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000172
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000173 /// Set the unroll count for the next loop pushed.
Tyler Nowicki54c020d2015-07-27 20:10:20 +0000174 void setUnrollCount(unsigned C) { StagedAttrs.UnrollCount = C; }
175
David Greenc8e39242018-08-01 14:36:12 +0000176 /// \brief Set the unroll count for the next loop pushed.
177 void setUnrollAndJamCount(unsigned C) { StagedAttrs.UnrollAndJamCount = C; }
178
Aaron Ballman9bdf5152019-01-04 17:20:00 +0000179 /// Set the pipeline disabled state.
180 void setPipelineDisabled(bool S) { StagedAttrs.PipelineDisabled = S; }
181
182 /// Set the pipeline initiation interval.
183 void setPipelineInitiationInterval(unsigned C) {
184 StagedAttrs.PipelineInitiationInterval = C;
185 }
186
Alexander Musman515ad8c2014-05-22 08:54:05 +0000187private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000188 /// Returns true if there is LoopInfo on the stack.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000189 bool hasInfo() const { return !Active.empty(); }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000190 /// Return the LoopInfo for the current loop. HasInfo should be called
Alexander Musman515ad8c2014-05-22 08:54:05 +0000191 /// first to ensure LoopInfo is present.
192 const LoopInfo &getInfo() const { return Active.back(); }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000193 /// The set of attributes that will be applied to the next pushed loop.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000194 LoopAttributes StagedAttrs;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000195 /// Stack of active loops.
Alexander Musman515ad8c2014-05-22 08:54:05 +0000196 llvm::SmallVector<LoopInfo, 4> Active;
197};
198
199} // end namespace CodeGen
200} // end namespace clang
201
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000202#endif