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