blob: 8822c22c91c8a2f3edc4b03986a632d36a712777 [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;
Alexander Musman515ad8c2014-05-22 08:54:05 +000032namespace CodeGen {
33
34/// \brief Attributes that may be specified on loops.
35struct LoopAttributes {
36 explicit LoopAttributes(bool IsParallel = false);
37 void clear();
38
39 /// \brief Generate llvm.loop.parallel metadata for loads and stores.
40 bool IsParallel;
41
Tyler Nowickida46d0e2015-07-14 23:03:09 +000042 /// \brief State of loop vectorization or unrolling.
43 enum LVEnableState { Unspecified, Enable, Disable };
Alexander Musman515ad8c2014-05-22 08:54:05 +000044
Tyler Nowickida46d0e2015-07-14 23:03:09 +000045 /// \brief Value for llvm.loop.vectorize.enable metadata.
46 LVEnableState VectorizeEnable;
Alexander Musman515ad8c2014-05-22 08:54:05 +000047
Tyler Nowickida46d0e2015-07-14 23:03:09 +000048 /// \brief Value for llvm.loop.vectorize.width metadata.
49 unsigned VectorizeWidth;
Alexander Musman515ad8c2014-05-22 08:54:05 +000050
Tyler Nowickida46d0e2015-07-14 23:03:09 +000051 /// \brief Value for llvm.loop.interleave.count metadata.
52 unsigned InterleaveCount;
Alexander Musman515ad8c2014-05-22 08:54:05 +000053};
54
55/// \brief Information used when generating a structured loop.
56class LoopInfo {
57public:
58 /// \brief Construct a new LoopInfo for the loop with entry Header.
59 LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs);
60
61 /// \brief Get the loop id metadata for this loop.
62 llvm::MDNode *getLoopID() const { return LoopID; }
63
64 /// \brief Get the header block of this loop.
65 llvm::BasicBlock *getHeader() const { return Header; }
66
67 /// \brief Get the set of attributes active for this loop.
68 const LoopAttributes &getAttributes() const { return Attrs; }
69
70private:
71 /// \brief Loop ID metadata.
72 llvm::MDNode *LoopID;
73 /// \brief Header block of this loop.
74 llvm::BasicBlock *Header;
75 /// \brief The attributes for this loop.
76 LoopAttributes Attrs;
77};
78
79/// \brief A stack of loop information corresponding to loop nesting levels.
80/// This stack can be used to prepare attributes which are applied when a loop
81/// is emitted.
82class LoopInfoStack {
Aaron Ballmanabc18922015-02-15 22:54:08 +000083 LoopInfoStack(const LoopInfoStack &) = delete;
84 void operator=(const LoopInfoStack &) = delete;
Alexander Musman515ad8c2014-05-22 08:54:05 +000085
86public:
87 LoopInfoStack() {}
88
89 /// \brief Begin a new structured loop. The set of staged attributes will be
90 /// applied to the loop and then cleared.
Tyler Nowicki9d268e12015-06-11 23:23:17 +000091 void push(llvm::BasicBlock *Header,
92 llvm::ArrayRef<const Attr *> Attrs = llvm::None);
Alexander Musman515ad8c2014-05-22 08:54:05 +000093
94 /// \brief End the current loop.
95 void pop();
96
97 /// \brief Return the top loop id metadata.
98 llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
99
100 /// \brief Return true if the top loop is parallel.
101 bool getCurLoopParallel() const {
102 return hasInfo() ? getInfo().getAttributes().IsParallel : false;
103 }
104
105 /// \brief Function called by the CodeGenFunction when an instruction is
106 /// created.
107 void InsertHelper(llvm::Instruction *I) const;
108
109 /// \brief Set the next pushed loop as parallel.
110 void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
111
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000112 /// \brief Set the next pushed loop 'vectorize.enable'
113 void setVectorizeEnable(bool Enable = true) {
114 StagedAttrs.VectorizeEnable =
115 Enable ? LoopAttributes::Enable : LoopAttributes::Disable;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000116 }
117
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000118 /// \brief Set the vectorize width for the next loop pushed.
119 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000120
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000121 /// \brief Set the interleave count for the next loop pushed.
122 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000123
124private:
125 /// \brief Returns true if there is LoopInfo on the stack.
126 bool hasInfo() const { return !Active.empty(); }
127 /// \brief Return the LoopInfo for the current loop. HasInfo should be called
128 /// first to ensure LoopInfo is present.
129 const LoopInfo &getInfo() const { return Active.back(); }
130 /// \brief The set of attributes that will be applied to the next pushed loop.
131 LoopAttributes StagedAttrs;
132 /// \brief Stack of active loops.
133 llvm::SmallVector<LoopInfo, 4> Active;
134};
135
136} // end namespace CodeGen
137} // end namespace clang
138
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000139#endif