| 1 | //===--- UnrollLoopsCheck.h - clang-tidy ------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_UNROLLLOOPSCHECK_H |
| 10 | #define |
| 11 | |
| 12 | #include "../ClangTidyCheck.h" |
| 13 | |
| 14 | namespace clang::tidy::altera { |
| 15 | |
| 16 | /// Finds inner loops that have not been unrolled, as well as fully unrolled |
| 17 | /// loops with unknown loop bounds or a large number of iterations. |
| 18 | /// |
| 19 | /// Unrolling inner loops could improve the performance of OpenCL kernels. |
| 20 | /// However, if they have unknown loop bounds or a large number of iterations, |
| 21 | /// they cannot be fully unrolled, and should be partially unrolled. |
| 22 | /// |
| 23 | /// For the user-facing documentation see: |
| 24 | /// http://clang.llvm.org/extra/clang-tidy/checks/altera/unroll-loops.html |
| 25 | class UnrollLoopsCheck : public ClangTidyCheck { |
| 26 | public: |
| 27 | UnrollLoopsCheck(StringRef Name, ClangTidyContext *Context); |
| 28 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 29 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 30 | |
| 31 | private: |
| 32 | /// Recommend partial unrolling if number of loop iterations is greater than |
| 33 | /// MaxLoopIterations. |
| 34 | const unsigned MaxLoopIterations; |
| 35 | /// The kind of unrolling, if any, applied to a given loop. |
| 36 | enum UnrollType { |
| 37 | // This loop has no #pragma unroll directive associated with it. |
| 38 | NotUnrolled, |
| 39 | // This loop has a #pragma unroll directive associated with it. |
| 40 | FullyUnrolled, |
| 41 | // This loop has a #pragma unroll <num> directive associated with it. |
| 42 | PartiallyUnrolled |
| 43 | }; |
| 44 | /// Attempts to extract an integer value from either side of the |
| 45 | /// BinaryOperator. Returns true and saves the result to &value if successful, |
| 46 | /// returns false otherwise. |
| 47 | bool (int &Value, const BinaryOperator *Op, |
| 48 | const ASTContext *Context); |
| 49 | /// Returns true if the given loop statement has a large number of iterations, |
| 50 | /// as determined by the integer value in the loop's condition expression, |
| 51 | /// if one exists. |
| 52 | bool hasLargeNumIterations(const Stmt *Statement, |
| 53 | const IntegerLiteral *CXXLoopBound, |
| 54 | const ASTContext *Context); |
| 55 | /// Checks one hand side of the binary operator to ascertain if the upper |
| 56 | /// bound on the number of loops is greater than max_loop_iterations or not. |
| 57 | /// If the expression is not evaluatable or not an integer, returns false. |
| 58 | bool exprHasLargeNumIterations(const Expr *Expression, |
| 59 | const ASTContext *Context) const; |
| 60 | /// Returns the type of unrolling, if any, associated with the given |
| 61 | /// statement. |
| 62 | enum UnrollType unrollType(const Stmt *Statement, ASTContext *Context); |
| 63 | /// Returns the condition expression within a given for statement. If there is |
| 64 | /// none, or if the Statement is not a loop, then returns a NULL pointer. |
| 65 | const Expr *getCondExpr(const Stmt *Statement); |
| 66 | /// Returns True if the loop statement has known bounds. |
| 67 | bool hasKnownBounds(const Stmt *Statement, const IntegerLiteral *CXXLoopBound, |
| 68 | const ASTContext *Context); |
| 69 | void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
| 70 | }; |
| 71 | |
| 72 | } // namespace clang::tidy::altera |
| 73 | |
| 74 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALTERA_UNROLLLOOPSCHECK_H |
| 75 | |