1//==--- CodeGenUtils.cpp - Shared Classic CodeGen/CIR CodeGen Utils--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#include "clang/CodeGenUtils/CodeGenUtils.h"
10#include "clang/AST/Attr.h"
11#include "clang/AST/Expr.h"
12#include "clang/Basic/Builtins.h"
13#include "clang/Basic/DiagnosticFrontend.h"
14#include "clang/Basic/TargetBuiltins.h"
15#include "clang/Basic/TargetInfo.h"
16#include "llvm/ADT/StringMap.h"
17
18namespace clang::CodeGenUtils {
19static bool
20hasTrivialDestructorBody(ASTContext &Context,
21 const CXXRecordDecl *BaseClassDecl,
22 const CXXRecordDecl *MostDerivedClassDecl) {
23 // If the destructor is trivial we don't have to check anything else.
24 if (BaseClassDecl->hasTrivialDestructor())
25 return true;
26
27 if (!BaseClassDecl->getDestructor()->hasTrivialBody())
28 return false;
29
30 // Check fields.
31 for (const auto *Field : BaseClassDecl->fields())
32 if (!fieldHasTrivialDestructorBody(Context, Field))
33 return false;
34
35 // Check non-virtual bases.
36 for (const auto &I : BaseClassDecl->bases()) {
37 if (I.isVirtual())
38 continue;
39
40 const auto *NonVirtualBase = I.getType()->castAsCXXRecordDecl();
41 if (!hasTrivialDestructorBody(Context, BaseClassDecl: NonVirtualBase,
42 MostDerivedClassDecl))
43 return false;
44 }
45
46 if (BaseClassDecl == MostDerivedClassDecl) {
47 // Check virtual bases.
48 for (const auto &I : BaseClassDecl->vbases()) {
49 const auto *VirtualBase = I.getType()->castAsCXXRecordDecl();
50 if (!hasTrivialDestructorBody(Context, BaseClassDecl: VirtualBase, MostDerivedClassDecl))
51 return false;
52 }
53 }
54
55 return true;
56}
57
58bool fieldHasTrivialDestructorBody(ASTContext &Context,
59 const FieldDecl *Field) {
60 QualType FieldBaseElementType = Context.getBaseElementType(QT: Field->getType());
61
62 auto *FieldClassDecl = FieldBaseElementType->getAsCXXRecordDecl();
63 if (!FieldClassDecl)
64 return true;
65
66 // The destructor for an implicit anonymous union member is never invoked.
67 if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
68 return true;
69
70 return hasTrivialDestructorBody(Context, BaseClassDecl: FieldClassDecl, MostDerivedClassDecl: FieldClassDecl);
71}
72
73/// Check whether we need to initialize any vtable pointers before calling this
74/// destructor.
75bool canSkipVTablePointerInitialization(ASTContext &Ctx,
76 const CXXDestructorDecl *Dtor) {
77 const CXXRecordDecl *ClassDecl = Dtor->getParent();
78 if (!ClassDecl->isDynamicClass())
79 return true;
80
81 // For a final class, the vtable pointer is known to already point to the
82 // class's vtable.
83 if (ClassDecl->isEffectivelyFinal())
84 return true;
85
86 if (!Dtor->hasTrivialBody())
87 return false;
88
89 // Check the fields.
90 for (const auto *Field : ClassDecl->fields())
91 if (!fieldHasTrivialDestructorBody(Context&: Ctx, Field))
92 return false;
93
94 return true;
95}
96bool hasUnwindExceptions(const LangOptions &LangOpts) {
97 // If exceptions are completely disabled, obviously this is false.
98 if (!LangOpts.Exceptions)
99 return false;
100
101 // If C++ exceptions are enabled, this is true.
102 if (LangOpts.CXXExceptions)
103 return true;
104
105 // If ObjC exceptions are enabled, this depends on the ABI.
106 if (LangOpts.ObjCExceptions) {
107 return LangOpts.ObjCRuntime.hasUnwindExceptions();
108 }
109
110 return true;
111}
112
113bool isAAPCS(const TargetInfo &TargetInfo) {
114 return TargetInfo.getABI().starts_with(Prefix: "aapcs");
115}
116bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit) {
117 const Type *BaseType = BaseInit->getBaseClass();
118 return BaseType->castAsCXXRecordDecl()->isDynamicClass();
119}
120
121// Emits an error if we don't have a valid set of target features for the
122// called function.
123void checkTargetFeatures(ASTContext &Ctx, DiagnosticsEngine &Diags,
124 const LangOptions &LangOpts, const CallExpr *E,
125 const FunctionDecl *Caller,
126 const FunctionDecl *TargetDecl) {
127 // SemaChecking cannot handle these x86 builtins because they have different
128 // parameter ranges depending on the caller's TargetAttribute.
129 if (Ctx.getTargetInfo().getTriple().isX86()) {
130 unsigned BuiltinID = TargetDecl->getBuiltinID();
131 if (BuiltinID == X86::BI__builtin_ia32_cmpps ||
132 BuiltinID == X86::BI__builtin_ia32_cmpss ||
133 BuiltinID == X86::BI__builtin_ia32_cmppd ||
134 BuiltinID == X86::BI__builtin_ia32_cmpsd) {
135 llvm::StringMap<bool> TargetFeatureMap;
136 Ctx.getFunctionFeatureMap(FeatureMap&: TargetFeatureMap, Caller);
137 llvm::APSInt Result = *(E->getArg(Arg: 2)->getIntegerConstantExpr(Ctx));
138 if (Result.getSExtValue() > 7 && !TargetFeatureMap.lookup(Key: "avx"))
139 Diags.Report(Loc: E->getBeginLoc(), DiagID: diag::err_builtin_needs_feature)
140 << TargetDecl->getDeclName() << "avx";
141 }
142 }
143 checkTargetFeatures(Ctx, Diags, LangOpts, Loc: E->getBeginLoc(), Caller,
144 TargetDecl);
145}
146
147// Emits an error if we don't have a valid set of target features for the
148// called function.
149void checkTargetFeatures(ASTContext &Ctx, DiagnosticsEngine &Diags,
150 const LangOptions &LangOpts, SourceLocation Loc,
151 const FunctionDecl *Caller,
152 const FunctionDecl *TargetDecl) {
153 if (!TargetDecl || !Caller)
154 return;
155
156 bool IsAlwaysInline = TargetDecl->hasAttr<AlwaysInlineAttr>();
157 bool IsFlatten = Caller->hasAttr<FlattenAttr>();
158
159 unsigned BuiltinID = TargetDecl->getBuiltinID();
160 std::string MissingFeature;
161 llvm::StringMap<bool> CallerFeatureMap;
162 Ctx.getFunctionFeatureMap(FeatureMap&: CallerFeatureMap, Caller);
163 // When compiling in HipStdPar mode we have to be conservative in rejecting
164 // target specific features in the FE, and defer the possible error to the
165 // AcceleratorCodeSelection pass, wherein iff an unsupported target builtin is
166 // referenced by an accelerator executable function, we emit an error.
167 bool IsHipStdPar = LangOpts.HIPStdPar && LangOpts.CUDAIsDevice;
168 if (BuiltinID) {
169 StringRef FeatureList(Ctx.BuiltinInfo.getRequiredFeatures(ID: BuiltinID));
170 if (!Builtin::evaluateRequiredTargetFeatures(RequiredFatures: FeatureList,
171 TargetFetureMap: CallerFeatureMap) &&
172 !IsHipStdPar)
173 Diags.Report(Loc, DiagID: diag::err_builtin_needs_feature)
174 << TargetDecl->getDeclName() << FeatureList;
175 } else if (!TargetDecl->isMultiVersion() &&
176 TargetDecl->hasAttr<TargetAttr>()) {
177 // Get the required features for the callee.
178 const TargetAttr *TD = TargetDecl->getAttr<TargetAttr>();
179 ParsedTargetAttr ParsedAttr = Ctx.filterFunctionTargetAttrs(TD);
180
181 SmallVector<StringRef, 1> ReqFeatures;
182 llvm::StringMap<bool> CalleeFeatureMap;
183 Ctx.getFunctionFeatureMap(FeatureMap&: CalleeFeatureMap, TargetDecl);
184
185 for (const auto &F : ParsedAttr.Features) {
186 if (F[0] == '+' && CalleeFeatureMap.lookup(Key: F.substr(pos: 1)))
187 ReqFeatures.push_back(Elt: StringRef(F).substr(Start: 1));
188 }
189 for (const auto &F : CalleeFeatureMap) {
190 if (F.getValue())
191 ReqFeatures.push_back(Elt: F.getKey());
192 }
193 if (!llvm::all_of(Range&: ReqFeatures,
194 P: [&](StringRef Feature) {
195 if (!CallerFeatureMap.lookup(Key: Feature)) {
196 MissingFeature = Feature.str();
197 return false;
198 }
199 return true;
200 }) &&
201 !IsHipStdPar) {
202 if (IsAlwaysInline)
203 Diags.Report(Loc, DiagID: diag::err_function_needs_feature)
204 << Caller->getDeclName() << TargetDecl->getDeclName()
205 << MissingFeature;
206 else if (IsFlatten)
207 Diags.Report(Loc, DiagID: diag::err_flatten_function_needs_feature)
208 << Caller->getDeclName() << TargetDecl->getDeclName()
209 << MissingFeature;
210 }
211 } else if (!Caller->isMultiVersion() && Caller->hasAttr<TargetAttr>()) {
212 llvm::StringMap<bool> CalleeFeatureMap;
213 Ctx.getFunctionFeatureMap(FeatureMap&: CalleeFeatureMap, TargetDecl);
214
215 for (const auto &F : CalleeFeatureMap) {
216 if (F.getValue() &&
217 (!CallerFeatureMap.lookup(Key: F.getKey()) ||
218 !CallerFeatureMap.find(Key: F.getKey())->getValue()) &&
219 !IsHipStdPar) {
220 if (IsAlwaysInline)
221 Diags.Report(Loc, DiagID: diag::err_function_needs_feature)
222 << Caller->getDeclName() << TargetDecl->getDeclName()
223 << F.getKey();
224 else if (IsFlatten)
225 Diags.Report(Loc, DiagID: diag::err_flatten_function_needs_feature)
226 << Caller->getDeclName() << TargetDecl->getDeclName()
227 << F.getKey();
228 }
229 }
230 }
231}
232
233} // namespace clang::CodeGenUtils
234