Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:18

0001 //===--- UnrollLoopsCheck.h - clang-tidy ------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_UNROLLLOOPSCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_UNROLLLOOPSCHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 
0014 namespace clang::tidy::altera {
0015 
0016 /// Finds inner loops that have not been unrolled, as well as fully unrolled
0017 /// loops with unknown loop bounds or a large number of iterations.
0018 ///
0019 /// Unrolling inner loops could improve the performance of OpenCL kernels.
0020 /// However, if they have unknown loop bounds or a large number of iterations,
0021 /// they cannot be fully unrolled, and should be partially unrolled.
0022 ///
0023 /// For the user-facing documentation see:
0024 /// http://clang.llvm.org/extra/clang-tidy/checks/altera/unroll-loops.html
0025 class UnrollLoopsCheck : public ClangTidyCheck {
0026 public:
0027   UnrollLoopsCheck(StringRef Name, ClangTidyContext *Context);
0028   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0029   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0030 
0031 private:
0032   /// Recommend partial unrolling if number of loop iterations is greater than
0033   /// MaxLoopIterations.
0034   const unsigned MaxLoopIterations;
0035   /// The kind of unrolling, if any, applied to a given loop.
0036   enum UnrollType {
0037     // This loop has no #pragma unroll directive associated with it.
0038     NotUnrolled,
0039     // This loop has a #pragma unroll directive associated with it.
0040     FullyUnrolled,
0041     // This loop has a #pragma unroll <num> directive associated with it.
0042     PartiallyUnrolled
0043   };
0044   /// Attempts to extract an integer value from either side of the
0045   /// BinaryOperator. Returns true and saves the result to &value if successful,
0046   /// returns false otherwise.
0047   bool extractValue(int &Value, const BinaryOperator *Op,
0048                     const ASTContext *Context);
0049   /// Returns true if the given loop statement has a large number of iterations,
0050   /// as determined by the integer value in the loop's condition expression,
0051   /// if one exists.
0052   bool hasLargeNumIterations(const Stmt *Statement,
0053                              const IntegerLiteral *CXXLoopBound,
0054                              const ASTContext *Context);
0055   /// Checks one hand side of the binary operator to ascertain if the upper
0056   /// bound on the number of loops is greater than max_loop_iterations or not.
0057   /// If the expression is not evaluatable or not an integer, returns false.
0058   bool exprHasLargeNumIterations(const Expr *Expression,
0059                                  const ASTContext *Context) const;
0060   /// Returns the type of unrolling, if any, associated with the given
0061   /// statement.
0062   enum UnrollType unrollType(const Stmt *Statement, ASTContext *Context);
0063   /// Returns the condition expression within a given for statement. If there is
0064   /// none, or if the Statement is not a loop, then returns a NULL pointer.
0065   const Expr *getCondExpr(const Stmt *Statement);
0066   /// Returns True if the loop statement has known bounds.
0067   bool hasKnownBounds(const Stmt *Statement, const IntegerLiteral *CXXLoopBound,
0068                       const ASTContext *Context);
0069   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0070 };
0071 
0072 } // namespace clang::tidy::altera
0073 
0074 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_UNROLLLOOPSCHECK_H