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 15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H 16 #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H 17 18 #include "llvm/ADT/ArrayRef.h" 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 { 31 class Attr; 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 42 /// \brief State of loop vectorization or unrolling. 43 enum LVEnableState { Unspecified, Enable, Disable }; 44 45 /// \brief Value for llvm.loop.vectorize.enable metadata. 46 LVEnableState VectorizeEnable; 47 48 /// \brief Value for llvm.loop.vectorize.width metadata. 49 unsigned VectorizeWidth; 50 51 /// \brief Value for llvm.loop.interleave.count metadata. 52 unsigned InterleaveCount; 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 { 83 LoopInfoStack(const LoopInfoStack &) = delete; 84 void operator=(const LoopInfoStack &) = delete; 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. 91 void push(llvm::BasicBlock *Header, 92 llvm::ArrayRef<const Attr *> Attrs = llvm::None); 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 112 /// \brief Set the next pushed loop 'vectorize.enable' 113 void setVectorizeEnable(bool Enable = true) { 114 StagedAttrs.VectorizeEnable = 115 Enable ? LoopAttributes::Enable : LoopAttributes::Disable; 116 } 117 118 /// \brief Set the vectorize width for the next loop pushed. 119 void setVectorizeWidth(unsigned W) { StagedAttrs.VectorizeWidth = W; } 120 121 /// \brief Set the interleave count for the next loop pushed. 122 void setInterleaveCount(unsigned C) { StagedAttrs.InterleaveCount = C; } 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 139 #endif 140