blob: 123e76d7ad1cdb15d616b37d355171826834b051 [file] [log] [blame]
Fangrui Song258a9592018-02-15 17:56:43 +00001//===--- SIMDIntrinsicsCheck.cpp - clang-tidy------------------------------===//
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#include "SIMDIntrinsicsCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/Triple.h"
15#include "llvm/Support/Regex.h"
16
17using namespace clang::ast_matchers;
18
19namespace clang {
20namespace tidy {
21namespace readability {
22
23namespace {
24
25// If the callee has parameter of VectorType or pointer to VectorType,
26// or the return type is VectorType, we consider it a vector function
27// and a candidate for checking.
28AST_MATCHER(FunctionDecl, isVectorFunction) {
29 bool IsVector = Node.getReturnType()->isVectorType();
30 for (const ParmVarDecl *Parm : Node.parameters()) {
31 QualType Type = Parm->getType();
32 if (Type->isPointerType())
33 Type = Type->getPointeeType();
34 if (Type->isVectorType())
35 IsVector = true;
36 }
37 return IsVector;
38}
39
40} // namespace
41
42static StringRef TrySuggestPPC(StringRef Name) {
43 if (!Name.consume_front("vec_"))
44 return {};
45
46 static const llvm::StringMap<StringRef> Mapping{
47 // [simd.alg]
48 {"max", "$std::max"},
49 {"min", "$std::min"},
50
51 // [simd.binary]
52 {"add", "operator+ on $simd objects"},
53 {"sub", "operator- on $simd objects"},
54 {"mul", "operator* on $simd objects"},
55 };
56
57 auto It = Mapping.find(Name);
58 if (It != Mapping.end())
59 return It->second;
60 return {};
61}
62
63static StringRef TrySuggestX86(StringRef Name) {
64 if (!(Name.consume_front("_mm_") || Name.consume_front("_mm256_") ||
65 Name.consume_front("_mm512_")))
66 return {};
67
68 // [simd.alg]
69 if (Name.startswith("max_"))
70 return "$simd::max";
71 if (Name.startswith("min_"))
72 return "$simd::min";
73
74 // [simd.binary]
75 if (Name.startswith("add_"))
76 return "operator+ on $simd objects";
77 if (Name.startswith("sub_"))
78 return "operator- on $simd objects";
79 if (Name.startswith("mul_"))
80 return "operator* on $simd objects";
81
82 return {};
83}
84
85SIMDIntrinsicsCheck::SIMDIntrinsicsCheck(StringRef Name,
86 ClangTidyContext *Context)
87 : ClangTidyCheck(Name, Context), Suggest(Options.get("Suggest", 0) != 0) {}
88
89void SIMDIntrinsicsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
90 Options.store(Opts, "Suggest", 0);
91}
92
93void SIMDIntrinsicsCheck::registerMatchers(MatchFinder *Finder) {
94 if (!getLangOpts().CPlusPlus11)
95 return;
96 // libcxx implementation backports it to C++11 std::experimental::simd.
97 Std = getLangOpts().CPlusPlus2a ? "std" : "std::experimental";
98
99 Finder->addMatcher(callExpr(callee(functionDecl(allOf(
100 matchesName("^::(_mm_|_mm256_|_mm512_|vec_)"),
101 isVectorFunction()))),
102 unless(isExpansionInSystemHeader()))
103 .bind("call"),
104 this);
105}
106
107void SIMDIntrinsicsCheck::check(const MatchFinder::MatchResult &Result) {
108 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
109 assert(Call != nullptr);
110 const FunctionDecl *Callee = Call->getDirectCallee();
111 if (!Callee)
112 return;
113
114 StringRef Old = Callee->getName();
115 StringRef New;
116 llvm::Triple::ArchType Arch =
117 Result.Context->getTargetInfo().getTriple().getArch();
118
119 switch (Arch) {
120 default:
121 break;
122 case llvm::Triple::ppc:
123 case llvm::Triple::ppc64:
124 case llvm::Triple::ppc64le:
125 New = TrySuggestPPC(Old);
126 break;
127 case llvm::Triple::x86:
128 case llvm::Triple::x86_64:
129 New = TrySuggestX86(Old);
130 break;
131 }
132
133 if (!New.empty()) {
134 std::string Message;
135 // If Suggest is true, give a P0214 alternative, otherwise point it out it
136 // is non-portable.
137 if (Suggest) {
138 Message = (Twine("'") + Old + "' can be replaced by " + New).str();
139 Message = llvm::Regex("\\$std").sub(Std, Message);
140 Message = llvm::Regex("\\$simd").sub(Std.str() + "::simd", Message);
141 } else {
142 Message = (Twine("'") + Old + "' is a non-portable " +
143 llvm::Triple::getArchTypeName(Arch) + " intrinsic function")
144 .str();
145 }
146 diag(Call->getExprLoc(), Message);
147 }
148}
149
150} // namespace readability
151} // namespace tidy
152} // namespace clang