clang API Documentation

CGLoopInfo.h
Go to the documentation of this file.
00001 //===---- CGLoopInfo.h - LLVM CodeGen for loop metadata -*- C++ -*---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This is the internal state used for llvm translation for loop statement
00011 // metadata.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
00016 #define LLVM_CLANG_LIB_CODEGEN_CGLOOPINFO_H
00017 
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/IR/Value.h"
00021 #include "llvm/Support/Compiler.h"
00022 
00023 namespace llvm {
00024 class BasicBlock;
00025 class Instruction;
00026 class MDNode;
00027 } // end namespace llvm
00028 
00029 namespace clang {
00030 namespace CodeGen {
00031 
00032 /// \brief Attributes that may be specified on loops.
00033 struct LoopAttributes {
00034   explicit LoopAttributes(bool IsParallel = false);
00035   void clear();
00036 
00037   /// \brief Generate llvm.loop.parallel metadata for loads and stores.
00038   bool IsParallel;
00039 
00040   /// \brief Values of llvm.loop.vectorize.enable metadata.
00041   enum LVEnableState { VecUnspecified, VecEnable, VecDisable };
00042 
00043   /// \brief llvm.loop.vectorize.enable
00044   LVEnableState VectorizerEnable;
00045 
00046   /// \brief llvm.loop.vectorize.width
00047   unsigned VectorizerWidth;
00048 
00049   /// \brief llvm.loop.interleave.count
00050   unsigned VectorizerUnroll;
00051 };
00052 
00053 /// \brief Information used when generating a structured loop.
00054 class LoopInfo {
00055 public:
00056   /// \brief Construct a new LoopInfo for the loop with entry Header.
00057   LoopInfo(llvm::BasicBlock *Header, const LoopAttributes &Attrs);
00058 
00059   /// \brief Get the loop id metadata for this loop.
00060   llvm::MDNode *getLoopID() const { return LoopID; }
00061 
00062   /// \brief Get the header block of this loop.
00063   llvm::BasicBlock *getHeader() const { return Header; }
00064 
00065   /// \brief Get the set of attributes active for this loop.
00066   const LoopAttributes &getAttributes() const { return Attrs; }
00067 
00068 private:
00069   /// \brief Loop ID metadata.
00070   llvm::MDNode *LoopID;
00071   /// \brief Header block of this loop.
00072   llvm::BasicBlock *Header;
00073   /// \brief The attributes for this loop.
00074   LoopAttributes Attrs;
00075 };
00076 
00077 /// \brief A stack of loop information corresponding to loop nesting levels.
00078 /// This stack can be used to prepare attributes which are applied when a loop
00079 /// is emitted.
00080 class LoopInfoStack {
00081   LoopInfoStack(const LoopInfoStack &) LLVM_DELETED_FUNCTION;
00082   void operator=(const LoopInfoStack &) LLVM_DELETED_FUNCTION;
00083 
00084 public:
00085   LoopInfoStack() {}
00086 
00087   /// \brief Begin a new structured loop. The set of staged attributes will be
00088   /// applied to the loop and then cleared.
00089   void push(llvm::BasicBlock *Header);
00090 
00091   /// \brief End the current loop.
00092   void pop();
00093 
00094   /// \brief Return the top loop id metadata.
00095   llvm::MDNode *getCurLoopID() const { return getInfo().getLoopID(); }
00096 
00097   /// \brief Return true if the top loop is parallel.
00098   bool getCurLoopParallel() const {
00099     return hasInfo() ? getInfo().getAttributes().IsParallel : false;
00100   }
00101 
00102   /// \brief Function called by the CodeGenFunction when an instruction is
00103   /// created.
00104   void InsertHelper(llvm::Instruction *I) const;
00105 
00106   /// \brief Set the next pushed loop as parallel.
00107   void setParallel(bool Enable = true) { StagedAttrs.IsParallel = Enable; }
00108 
00109   /// \brief Set the next pushed loop 'vectorizer.enable'
00110   void setVectorizerEnable(bool Enable = true) {
00111     StagedAttrs.VectorizerEnable =
00112         Enable ? LoopAttributes::VecEnable : LoopAttributes::VecDisable;
00113   }
00114 
00115   /// \brief Set the vectorizer width for the next loop pushed.
00116   void setVectorizerWidth(unsigned W) { StagedAttrs.VectorizerWidth = W; }
00117 
00118   /// \brief Set the vectorizer unroll for the next loop pushed.
00119   void setVectorizerUnroll(unsigned U) { StagedAttrs.VectorizerUnroll = U; }
00120 
00121 private:
00122   /// \brief Returns true if there is LoopInfo on the stack.
00123   bool hasInfo() const { return !Active.empty(); }
00124   /// \brief Return the LoopInfo for the current loop. HasInfo should be called
00125   /// first to ensure LoopInfo is present.
00126   const LoopInfo &getInfo() const { return Active.back(); }
00127   /// \brief The set of attributes that will be applied to the next pushed loop.
00128   LoopAttributes StagedAttrs;
00129   /// \brief Stack of active loops.
00130   llvm::SmallVector<LoopInfo, 4> Active;
00131 };
00132 
00133 } // end namespace CodeGen
00134 } // end namespace clang
00135 
00136 #endif