Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1 | //===---- 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 Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H |
| 16 | #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 17 | |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/IR/Value.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | |
| 24 | namespace llvm { |
| 25 | class BasicBlock; |
| 26 | class Instruction; |
| 27 | class MDNode; |
| 28 | } // end namespace llvm |
| 29 | |
| 30 | namespace clang { |
Tyler Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 31 | class Attr; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 32 | namespace CodeGen { |
| 33 | |
| 34 | /// \brief Attributes that may be specified on loops. |
| 35 | struct 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 Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 42 | /// \brief State of loop vectorization or unrolling. |
| 43 | enum LVEnableState { Unspecified, Enable, Disable }; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 44 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 45 | /// \brief Value for llvm.loop.vectorize.enable metadata. |
| 46 | LVEnableState VectorizeEnable; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 47 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 48 | /// \brief Value for llvm.loop.vectorize.width metadata. |
| 49 | unsigned VectorizeWidth; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 50 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 51 | /// \brief Value for llvm.loop.interleave.count metadata. |
| 52 | unsigned InterleaveCount; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /// \brief Information used when generating a structured loop. |
| 56 | class LoopInfo { |
| 57 | public: |
| 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 | |
| 70 | private: |
| 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. |
| 82 | class LoopInfoStack { |
Aaron Ballman | abc1892 | 2015-02-15 22:54:08 +0000 | [diff] [blame] | 83 | LoopInfoStack(const LoopInfoStack &) = delete; |
| 84 | void operator=(const LoopInfoStack &) = delete; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 85 | |
| 86 | public: |
| 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 Nowicki | 9d268e1 | 2015-06-11 23:23:17 +0000 | [diff] [blame] | 91 | void push(llvm::BasicBlock *Header, |
| 92 | llvm::ArrayRef<const Attr *> Attrs = llvm::None); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 93 | |
| 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 Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 112 | /// \brief Set the next pushed loop 'vectorize.enable' |
| 113 | void setVectorizeEnable(bool Enable = true) { |
| 114 | StagedAttrs.VectorizeEnable = |
| 115 | Enable ? LoopAttributes::Enable : LoopAttributes::Disable; |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 118 | /// \brief Set the vectorize width for the next loop pushed. |
| 119 | void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 120 | |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 121 | /// \brief Set the interleave count for the next loop pushed. |
| 122 | void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 123 | |
| 124 | private: |
| 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 Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 139 | #endif |