1 //=== unittests/CodeGen/TBAAMetadataTest.cpp - Checks metadata generation -===//
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
7 //===----------------------------------------------------------------------===//
9 #include "IRMatchers.h"
10 #include "TestCompiler.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Basic/TargetInfo.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "gtest/gtest.h"
24 struct TBAATestCompiler
: public TestCompiler
{
25 TBAATestCompiler(clang::LangOptions LO
, clang::CodeGenOptions CGO
)
26 : TestCompiler(LO
, CGO
) {}
27 static clang::CodeGenOptions
getCommonCodeGenOpts() {
28 clang::CodeGenOptions CGOpts
;
29 CGOpts
.StructPathTBAA
= 1;
30 CGOpts
.OptimizationLevel
= 1;
35 auto OmnipotentCharC
= MMTuple(
36 MMString("omnipotent char"),
38 MMString("Simple C/C++ TBAA")),
43 auto OmnipotentCharCXX
= MMTuple(
44 MMString("omnipotent char"),
46 MMString("Simple C++ TBAA")),
51 TEST(TBAAMetadataTest
, BasicTypes
) {
52 const char TestProgram
[] = R
"**(
53 void func(char *CP, short *SP, int *IP, long long *LP, void **VPP,
64 clang::LangOptions LO
;
65 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
66 Compiler
.init(TestProgram
);
67 const BasicBlock
*BB
= Compiler
.compile();
69 const Instruction
*I
= match(BB
,
70 MInstruction(Instruction::Store
,
79 MInstruction(Instruction::Store
,
91 MInstruction(Instruction::Store
,
103 MInstruction(Instruction::Store
,
107 MMString("long long"),
115 MInstruction(Instruction::Store
,
116 MValType(PointerType::getUnqual(Compiler
.Context
)),
119 MMString("any pointer"),
127 MInstruction(Instruction::Store
,
128 MValType(PointerType::getUnqual(Compiler
.Context
)),
131 MMString("any pointer"),
139 TEST(TBAAMetadataTest
, CFields
) {
140 const char TestProgram
[] = R
"**(
145 unsigned short f16_2;
147 unsigned long long f64_2;
150 void func(struct ABC *A) {
160 clang::LangOptions LO
;
162 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
163 Compiler
.init(TestProgram
);
164 const BasicBlock
*BB
= Compiler
.compile();
166 auto StructABC
= MMTuple(
179 MMString("long long"),
190 const Instruction
*I
= match(BB
,
191 MInstruction(Instruction::Store
,
203 MInstruction(Instruction::Store
,
215 MInstruction(Instruction::Store
,
220 MMString("long long"),
227 MInstruction(Instruction::Store
,
239 MInstruction(Instruction::Store
,
251 MInstruction(Instruction::Store
,
256 MMString("long long"),
263 TEST(TBAAMetadataTest
, CTypedefFields
) {
264 const char TestProgram
[] = R
"**(
274 void func(ABC *A, CDE *B) {
282 clang::LangOptions LO
;
284 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
285 Compiler
.init(TestProgram
);
286 const BasicBlock
*BB
= Compiler
.compile();
288 auto NamelessStruct
= MMTuple(
301 const Metadata
*MetaABC
= nullptr;
302 const Instruction
*I
= match(BB
,
303 MInstruction(Instruction::Store
,
306 MMSave(MetaABC
, NamelessStruct
),
315 MInstruction(Instruction::Store
,
326 const Metadata
*MetaCDE
= nullptr;
328 MInstruction(Instruction::Store
,
331 MMSave(MetaCDE
, NamelessStruct
),
340 MInstruction(Instruction::Store
,
351 // FIXME: Nameless structures used in definitions of 'ABC' and 'CDE' are
352 // different structures and must be described by different descriptors.
353 //ASSERT_TRUE(MetaABC != MetaCDE);
356 TEST(TBAAMetadataTest
, CTypedefFields2
) {
357 const char TestProgram
[] = R
"**(
367 void func(ABC *A, CDE *B) {
375 clang::LangOptions LO
;
377 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
378 Compiler
.init(TestProgram
);
379 const BasicBlock
*BB
= Compiler
.compile();
381 auto NamelessStruct
= MMTuple(
394 const Metadata
*MetaABC
= nullptr;
395 const Instruction
*I
= match(BB
,
396 MInstruction(Instruction::Store
,
399 MMSave(MetaABC
, NamelessStruct
),
408 MInstruction(Instruction::Store
,
419 const Metadata
*MetaCDE
= nullptr;
421 MInstruction(Instruction::Store
,
424 MMSave(MetaCDE
, NamelessStruct
),
433 MInstruction(Instruction::Store
,
444 // FIXME: Nameless structures used in definitions of 'ABC' and 'CDE' are
445 // different structures, although they have the same field sequence. They must
446 // be described by different descriptors.
447 //ASSERT_TRUE(MetaABC != MetaCDE);
450 TEST(TBAAMetadataTest
, CTypedefFields3
) {
451 const char TestProgram
[] = R
"**(
461 void func(ABC *A, CDE *B) {
469 clang::LangOptions LO
;
471 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
472 Compiler
.init(TestProgram
);
473 const BasicBlock
*BB
= Compiler
.compile();
475 auto NamelessStruct1
= MMTuple(
488 auto NamelessStruct2
= MMTuple(
501 const Instruction
*I
= match(BB
,
502 MInstruction(Instruction::Store
,
514 MInstruction(Instruction::Store
,
526 MInstruction(Instruction::Store
,
538 MInstruction(Instruction::Store
,
550 TEST(TBAAMetadataTest
, CXXFields
) {
551 const char TestProgram
[] = R
"**(
556 unsigned short f16_2;
558 unsigned long long f64_2;
561 void func(struct ABC *A) {
571 clang::LangOptions LO
;
574 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
575 Compiler
.init(TestProgram
);
576 const BasicBlock
*BB
= Compiler
.compile();
578 auto StructABC
= MMTuple(
579 MMString("_ZTS3ABC"),
591 MMString("long long"),
602 const Instruction
*I
= match(BB
,
603 MInstruction(Instruction::Store
,
615 MInstruction(Instruction::Store
,
627 MInstruction(Instruction::Store
,
632 MMString("long long"),
639 MInstruction(Instruction::Store
,
651 MInstruction(Instruction::Store
,
663 MInstruction(Instruction::Store
,
668 MMString("long long"),
675 TEST(TBAAMetadataTest
, CXXTypedefFields
) {
676 const char TestProgram
[] = R
"**(
686 void func(ABC *A, CDE *B) {
694 clang::LangOptions LO
;
697 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
698 Compiler
.init(TestProgram
);
699 const BasicBlock
*BB
= Compiler
.compile();
701 auto StructABC
= MMTuple(
702 MMString("_ZTS3ABC"),
714 auto StructCDE
= MMTuple(
715 MMString("_ZTS3CDE"),
727 const Instruction
*I
= match(BB
,
728 MInstruction(Instruction::Store
,
740 MInstruction(Instruction::Store
,
752 MInstruction(Instruction::Store
,
764 MInstruction(Instruction::Store
,
776 TEST(TBAAMetadataTest
, StructureFields
) {
777 const char TestProgram
[] = R
"**(
788 void func(Outer *S) {
795 clang::LangOptions LO
;
798 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
799 Compiler
.init(TestProgram
);
800 const BasicBlock
*BB
= Compiler
.compile();
802 auto StructInner
= MMTuple(
803 MMString("_ZTS5Inner"),
810 auto StructOuter
= MMTuple(
811 MMString("_ZTS5Outer"),
822 const Instruction
*I
= match(BB
,
823 MInstruction(Instruction::Store
,
835 MInstruction(Instruction::Store
,
847 MInstruction(Instruction::Store
,
859 TEST(TBAAMetadataTest
, ArrayFields
) {
860 const char TestProgram
[] = R
"**(
870 void func(Outer *S) {
877 clang::LangOptions LO
;
880 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
881 Compiler
.init(TestProgram
);
882 const BasicBlock
*BB
= Compiler
.compile();
884 auto StructInner
= MMTuple(
885 MMString("_ZTS5Inner"),
892 auto StructOuter
= MMTuple(
893 MMString("_ZTS5Outer"),
899 OmnipotentCharCXX
, // FIXME: Info about array field is lost.
902 const Instruction
*I
= match(BB
,
903 MInstruction(Instruction::Store
,
915 MInstruction(Instruction::Store
,
927 MInstruction(Instruction::Store
,
939 TEST(TBAAMetadataTest
, BaseClass
) {
940 const char TestProgram
[] = R
"**(
945 struct Derived : public Base {
949 void func(Base *B, Derived *D) {
956 clang::LangOptions LO
;
959 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
960 Compiler
.init(TestProgram
);
961 const BasicBlock
*BB
= Compiler
.compile();
963 auto ClassBase
= MMTuple(
964 MMString("_ZTS4Base"),
972 MMTuple(MMString("_ZTS7Derived"), ClassBase
, MConstInt(0),
973 MMTuple(MMString("short"), OmnipotentCharCXX
, MConstInt(0)),
976 const Instruction
*I
= match(BB
,
977 MInstruction(Instruction::Store
,
989 MInstruction(Instruction::Store
,
1001 MInstruction(Instruction::Store
,
1013 TEST(TBAAMetadataTest
, PolymorphicClass
) {
1014 const char TestProgram
[] = R
"**(
1016 virtual void m1(int *) = 0;
1020 struct Derived : public Base {
1021 virtual void m1(int *) override;
1025 void func(Base *B, Derived *D) {
1032 clang::LangOptions LO
;
1035 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
1036 Compiler
.init(TestProgram
);
1037 const BasicBlock
*BB
= Compiler
.compile();
1039 auto ClassBase
= MMTuple(
1040 MMString("_ZTS4Base"),
1045 MConstInt(Compiler
.PtrSize
));
1048 MMTuple(MMString("_ZTS7Derived"), ClassBase
, MConstInt(0),
1049 MMTuple(MMString("short"), OmnipotentCharCXX
, MConstInt(0)),
1050 MConstInt(Compiler
.PtrSize
+ 4));
1052 const Instruction
*I
= match(BB
,
1053 MInstruction(Instruction::Store
,
1061 MConstInt(Compiler
.PtrSize
))));
1065 MInstruction(Instruction::Store
,
1073 MConstInt(Compiler
.PtrSize
+ 4))));
1077 MInstruction(Instruction::Store
,
1085 MConstInt(Compiler
.PtrSize
))));
1089 TEST(TBAAMetadataTest
, VirtualBase
) {
1090 const char TestProgram
[] = R
"**(
1095 struct Derived : public virtual Base {
1099 void func(Base *B, Derived *D) {
1106 clang::LangOptions LO
;
1109 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
1110 Compiler
.init(TestProgram
);
1111 const BasicBlock
*BB
= Compiler
.compile();
1113 auto ClassBase
= MMTuple(
1114 MMString("_ZTS4Base"),
1121 auto ClassDerived
= MMTuple(
1122 MMString("_ZTS7Derived"),
1127 MConstInt(Compiler
.PtrSize
));
1129 const Instruction
*I
= match(BB
,
1130 MInstruction(Instruction::Store
,
1142 MInstruction(Instruction::Store
,
1150 MConstInt(Compiler
.PtrSize
))));
1154 MInstruction(Instruction::Load
,
1157 MMString("vtable pointer"),
1159 MMString("Simple C++ TBAA")),
1166 MInstruction(Instruction::Store
,
1178 TEST(TBAAMetadataTest
, TemplSpec
) {
1179 const char TestProgram
[] = R
"**(
1180 template<typename T1, typename T2>
1186 void func(ABC<double, int> *p) {
1192 clang::LangOptions LO
;
1195 TBAATestCompiler
Compiler(LO
, TBAATestCompiler::getCommonCodeGenOpts());
1196 Compiler
.init(TestProgram
);
1197 const BasicBlock
*BB
= Compiler
.compile();
1199 auto SpecABC
= MMTuple(
1200 MMString("_ZTS3ABCIdiE"),
1212 const Instruction
*I
= match(BB
,
1213 MInstruction(Instruction::Store
,
1214 MValType(MType([](const Type
&T
)->bool { return T
.isDoubleTy(); })),
1225 MInstruction(Instruction::Store
,