TBAAMetadataTest.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. //=== unittests/CodeGen/TBAAMetadataTest.cpp - Checks metadata generation -===//
  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. #include "IRMatchers.h"
  9. #include "clang/AST/ASTConsumer.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/CodeGen/ModuleBuilder.h"
  12. #include "clang/Frontend/CompilerInstance.h"
  13. #include "clang/Parse/ParseAST.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include "gtest/gtest.h"
  20. #include <memory>
  21. using namespace llvm;
  22. namespace {
  23. struct TestCompiler {
  24. LLVMContext Context;
  25. clang::CompilerInstance compiler;
  26. clang::CodeGenerator *CG = nullptr;
  27. llvm::Module *M = nullptr;
  28. unsigned PtrSize = 0;
  29. void init(const char *TestProgram) {
  30. compiler.createDiagnostics();
  31. compiler.getCodeGenOpts().StructPathTBAA = 1;
  32. compiler.getCodeGenOpts().OptimizationLevel = 1;
  33. std::string TrStr = llvm::Triple::normalize(llvm::sys::getProcessTriple());
  34. llvm::Triple Tr(TrStr);
  35. Tr.setOS(Triple::Linux);
  36. Tr.setVendor(Triple::VendorType::UnknownVendor);
  37. Tr.setEnvironment(Triple::EnvironmentType::UnknownEnvironment);
  38. compiler.getTargetOpts().Triple = Tr.getTriple();
  39. compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
  40. compiler.getDiagnostics(),
  41. std::make_shared<clang::TargetOptions>(compiler.getTargetOpts())));
  42. const clang::TargetInfo &TInfo = compiler.getTarget();
  43. PtrSize = TInfo.getPointerWidth(0) / 8;
  44. compiler.createFileManager();
  45. compiler.createSourceManager(compiler.getFileManager());
  46. compiler.createPreprocessor(clang::TU_Prefix);
  47. compiler.createASTContext();
  48. CG = CreateLLVMCodeGen(
  49. compiler.getDiagnostics(),
  50. "main-module",
  51. compiler.getHeaderSearchOpts(),
  52. compiler.getPreprocessorOpts(),
  53. compiler.getCodeGenOpts(),
  54. Context);
  55. compiler.setASTConsumer(std::unique_ptr<clang::ASTConsumer>(CG));
  56. compiler.createSema(clang::TU_Prefix, nullptr);
  57. clang::SourceManager &sm = compiler.getSourceManager();
  58. sm.setMainFileID(sm.createFileID(
  59. llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
  60. }
  61. const BasicBlock *compile() {
  62. clang::ParseAST(compiler.getSema(), false, false);
  63. M = CG->GetModule();
  64. // Do not expect more than one function definition.
  65. auto FuncPtr = M->begin();
  66. for (; FuncPtr != M->end(); ++FuncPtr)
  67. if (!FuncPtr->isDeclaration())
  68. break;
  69. assert(FuncPtr != M->end());
  70. const llvm::Function &Func = *FuncPtr;
  71. ++FuncPtr;
  72. for (; FuncPtr != M->end(); ++FuncPtr)
  73. if (!FuncPtr->isDeclaration())
  74. break;
  75. assert(FuncPtr == M->end());
  76. // The function must consist of single basic block.
  77. auto BBPtr = Func.begin();
  78. assert(Func.begin() != Func.end());
  79. const BasicBlock &BB = *BBPtr;
  80. ++BBPtr;
  81. assert(BBPtr == Func.end());
  82. return &BB;
  83. }
  84. };
  85. auto OmnipotentCharC = MMTuple(
  86. MMString("omnipotent char"),
  87. MMTuple(
  88. MMString("Simple C/C++ TBAA")),
  89. MConstInt(0, 64)
  90. );
  91. auto OmnipotentCharCXX = MMTuple(
  92. MMString("omnipotent char"),
  93. MMTuple(
  94. MMString("Simple C++ TBAA")),
  95. MConstInt(0, 64)
  96. );
  97. TEST(TBAAMetadataTest, BasicTypes) {
  98. const char TestProgram[] = R"**(
  99. void func(char *CP, short *SP, int *IP, long long *LP, void **VPP,
  100. int **IPP) {
  101. *CP = 4;
  102. *SP = 11;
  103. *IP = 601;
  104. *LP = 604;
  105. *VPP = CP;
  106. *IPP = IP;
  107. }
  108. )**";
  109. TestCompiler Compiler;
  110. Compiler.compiler.getLangOpts().C11 = 1;
  111. Compiler.init(TestProgram);
  112. const BasicBlock *BB = Compiler.compile();
  113. const Instruction *I = match(BB,
  114. MInstruction(Instruction::Store,
  115. MConstInt(4, 8),
  116. MMTuple(
  117. OmnipotentCharC,
  118. MSameAs(0),
  119. MConstInt(0))));
  120. ASSERT_TRUE(I);
  121. I = matchNext(I,
  122. MInstruction(Instruction::Store,
  123. MConstInt(11, 16),
  124. MMTuple(
  125. MMTuple(
  126. MMString("short"),
  127. OmnipotentCharC,
  128. MConstInt(0)),
  129. MSameAs(0),
  130. MConstInt(0))));
  131. ASSERT_TRUE(I);
  132. I = matchNext(I,
  133. MInstruction(Instruction::Store,
  134. MConstInt(601, 32),
  135. MMTuple(
  136. MMTuple(
  137. MMString("int"),
  138. OmnipotentCharC,
  139. MConstInt(0)),
  140. MSameAs(0),
  141. MConstInt(0))));
  142. ASSERT_TRUE(I);
  143. I = matchNext(I,
  144. MInstruction(Instruction::Store,
  145. MConstInt(604, 64),
  146. MMTuple(
  147. MMTuple(
  148. MMString("long long"),
  149. OmnipotentCharC,
  150. MConstInt(0)),
  151. MSameAs(0),
  152. MConstInt(0))));
  153. ASSERT_TRUE(I);
  154. I = matchNext(I,
  155. MInstruction(Instruction::Store,
  156. MValType(Type::getInt8PtrTy(Compiler.Context)),
  157. MMTuple(
  158. MMTuple(
  159. MMString("any pointer"),
  160. OmnipotentCharC,
  161. MConstInt(0)),
  162. MSameAs(0),
  163. MConstInt(0))));
  164. ASSERT_TRUE(I);
  165. I = matchNext(I,
  166. MInstruction(Instruction::Store,
  167. MValType(Type::getInt32PtrTy(Compiler.Context)),
  168. MMTuple(
  169. MMTuple(
  170. MMString("any pointer"),
  171. OmnipotentCharC,
  172. MConstInt(0)),
  173. MSameAs(0),
  174. MConstInt(0))));
  175. ASSERT_TRUE(I);
  176. }
  177. TEST(TBAAMetadataTest, CFields) {
  178. const char TestProgram[] = R"**(
  179. struct ABC {
  180. short f16;
  181. int f32;
  182. long long f64;
  183. unsigned short f16_2;
  184. unsigned f32_2;
  185. unsigned long long f64_2;
  186. };
  187. void func(struct ABC *A) {
  188. A->f32 = 4;
  189. A->f16 = 11;
  190. A->f64 = 601;
  191. A->f16_2 = 22;
  192. A->f32_2 = 77;
  193. A->f64_2 = 604;
  194. }
  195. )**";
  196. TestCompiler Compiler;
  197. Compiler.compiler.getLangOpts().C11 = 1;
  198. Compiler.init(TestProgram);
  199. const BasicBlock *BB = Compiler.compile();
  200. auto StructABC = MMTuple(
  201. MMString("ABC"),
  202. MMTuple(
  203. MMString("short"),
  204. OmnipotentCharC,
  205. MConstInt(0)),
  206. MConstInt(0),
  207. MMTuple(
  208. MMString("int"),
  209. OmnipotentCharC,
  210. MConstInt(0)),
  211. MConstInt(4),
  212. MMTuple(
  213. MMString("long long"),
  214. OmnipotentCharC,
  215. MConstInt(0)),
  216. MConstInt(8),
  217. MSameAs(1),
  218. MConstInt(16),
  219. MSameAs(3),
  220. MConstInt(20),
  221. MSameAs(5),
  222. MConstInt(24));
  223. const Instruction *I = match(BB,
  224. MInstruction(Instruction::Store,
  225. MConstInt(4, 32),
  226. MMTuple(
  227. StructABC,
  228. MMTuple(
  229. MMString("int"),
  230. OmnipotentCharC,
  231. MConstInt(0)),
  232. MConstInt(4))));
  233. ASSERT_TRUE(I);
  234. I = matchNext(I,
  235. MInstruction(Instruction::Store,
  236. MConstInt(11, 16),
  237. MMTuple(
  238. StructABC,
  239. MMTuple(
  240. MMString("short"),
  241. OmnipotentCharC,
  242. MConstInt(0)),
  243. MConstInt(0))));
  244. ASSERT_TRUE(I);
  245. I = matchNext(I,
  246. MInstruction(Instruction::Store,
  247. MConstInt(601, 64),
  248. MMTuple(
  249. StructABC,
  250. MMTuple(
  251. MMString("long long"),
  252. OmnipotentCharC,
  253. MConstInt(0)),
  254. MConstInt(8))));
  255. ASSERT_TRUE(I);
  256. I = matchNext(I,
  257. MInstruction(Instruction::Store,
  258. MConstInt(22, 16),
  259. MMTuple(
  260. StructABC,
  261. MMTuple(
  262. MMString("short"),
  263. OmnipotentCharC,
  264. MConstInt(0)),
  265. MConstInt(16))));
  266. ASSERT_TRUE(I);
  267. I = matchNext(I,
  268. MInstruction(Instruction::Store,
  269. MConstInt(77, 32),
  270. MMTuple(
  271. StructABC,
  272. MMTuple(
  273. MMString("int"),
  274. OmnipotentCharC,
  275. MConstInt(0)),
  276. MConstInt(20))));
  277. ASSERT_TRUE(I);
  278. I = matchNext(I,
  279. MInstruction(Instruction::Store,
  280. MConstInt(604, 64),
  281. MMTuple(
  282. StructABC,
  283. MMTuple(
  284. MMString("long long"),
  285. OmnipotentCharC,
  286. MConstInt(0)),
  287. MConstInt(24))));
  288. ASSERT_TRUE(I);
  289. }
  290. TEST(TBAAMetadataTest, CTypedefFields) {
  291. const char TestProgram[] = R"**(
  292. typedef struct {
  293. short f16;
  294. int f32;
  295. } ABC;
  296. typedef struct {
  297. short value_f16;
  298. int value_f32;
  299. } CDE;
  300. void func(ABC *A, CDE *B) {
  301. A->f32 = 4;
  302. A->f16 = 11;
  303. B->value_f32 = 44;
  304. B->value_f16 = 111;
  305. }
  306. )**";
  307. TestCompiler Compiler;
  308. Compiler.compiler.getLangOpts().C11 = 1;
  309. Compiler.init(TestProgram);
  310. const BasicBlock *BB = Compiler.compile();
  311. auto NamelessStruct = MMTuple(
  312. MMString(""),
  313. MMTuple(
  314. MMString("short"),
  315. OmnipotentCharC,
  316. MConstInt(0)),
  317. MConstInt(0),
  318. MMTuple(
  319. MMString("int"),
  320. OmnipotentCharC,
  321. MConstInt(0)),
  322. MConstInt(4));
  323. const Metadata *MetaABC = nullptr;
  324. const Instruction *I = match(BB,
  325. MInstruction(Instruction::Store,
  326. MConstInt(4, 32),
  327. MMTuple(
  328. MMSave(MetaABC, NamelessStruct),
  329. MMTuple(
  330. MMString("int"),
  331. OmnipotentCharC,
  332. MConstInt(0)),
  333. MConstInt(4))));
  334. ASSERT_TRUE(I);
  335. I = matchNext(I,
  336. MInstruction(Instruction::Store,
  337. MConstInt(11, 16),
  338. MMTuple(
  339. NamelessStruct,
  340. MMTuple(
  341. MMString("short"),
  342. OmnipotentCharC,
  343. MConstInt(0)),
  344. MConstInt(0))));
  345. ASSERT_TRUE(I);
  346. const Metadata *MetaCDE = nullptr;
  347. I = matchNext(I,
  348. MInstruction(Instruction::Store,
  349. MConstInt(44, 32),
  350. MMTuple(
  351. MMSave(MetaCDE, NamelessStruct),
  352. MMTuple(
  353. MMString("int"),
  354. OmnipotentCharC,
  355. MConstInt(0)),
  356. MConstInt(4))));
  357. ASSERT_TRUE(I);
  358. I = matchNext(I,
  359. MInstruction(Instruction::Store,
  360. MConstInt(111, 16),
  361. MMTuple(
  362. NamelessStruct,
  363. MMTuple(
  364. MMString("short"),
  365. OmnipotentCharC,
  366. MConstInt(0)),
  367. MConstInt(0))));
  368. ASSERT_TRUE(I);
  369. // FIXME: Nameless structures used in definitions of 'ABC' and 'CDE' are
  370. // different structures and must be described by different descriptors.
  371. //ASSERT_TRUE(MetaABC != MetaCDE);
  372. }
  373. TEST(TBAAMetadataTest, CTypedefFields2) {
  374. const char TestProgram[] = R"**(
  375. typedef struct {
  376. short f16;
  377. int f32;
  378. } ABC;
  379. typedef struct {
  380. short f16;
  381. int f32;
  382. } CDE;
  383. void func(ABC *A, CDE *B) {
  384. A->f32 = 4;
  385. A->f16 = 11;
  386. B->f32 = 44;
  387. B->f16 = 111;
  388. }
  389. )**";
  390. TestCompiler Compiler;
  391. Compiler.compiler.getLangOpts().C11 = 1;
  392. Compiler.init(TestProgram);
  393. const BasicBlock *BB = Compiler.compile();
  394. auto NamelessStruct = MMTuple(
  395. MMString(""),
  396. MMTuple(
  397. MMString("short"),
  398. OmnipotentCharC,
  399. MConstInt(0)),
  400. MConstInt(0),
  401. MMTuple(
  402. MMString("int"),
  403. OmnipotentCharC,
  404. MConstInt(0)),
  405. MConstInt(4));
  406. const Metadata *MetaABC = nullptr;
  407. const Instruction *I = match(BB,
  408. MInstruction(Instruction::Store,
  409. MConstInt(4, 32),
  410. MMTuple(
  411. MMSave(MetaABC, NamelessStruct),
  412. MMTuple(
  413. MMString("int"),
  414. OmnipotentCharC,
  415. MConstInt(0)),
  416. MConstInt(4))));
  417. ASSERT_TRUE(I);
  418. I = matchNext(I,
  419. MInstruction(Instruction::Store,
  420. MConstInt(11, 16),
  421. MMTuple(
  422. NamelessStruct,
  423. MMTuple(
  424. MMString("short"),
  425. OmnipotentCharC,
  426. MConstInt(0)),
  427. MConstInt(0))));
  428. ASSERT_TRUE(I);
  429. const Metadata *MetaCDE = nullptr;
  430. I = matchNext(I,
  431. MInstruction(Instruction::Store,
  432. MConstInt(44, 32),
  433. MMTuple(
  434. MMSave(MetaCDE, NamelessStruct),
  435. MMTuple(
  436. MMString("int"),
  437. OmnipotentCharC,
  438. MConstInt(0)),
  439. MConstInt(4))));
  440. ASSERT_TRUE(I);
  441. I = matchNext(I,
  442. MInstruction(Instruction::Store,
  443. MConstInt(111, 16),
  444. MMTuple(
  445. NamelessStruct,
  446. MMTuple(
  447. MMString("short"),
  448. OmnipotentCharC,
  449. MConstInt(0)),
  450. MConstInt(0))));
  451. ASSERT_TRUE(I);
  452. // FIXME: Nameless structures used in definitions of 'ABC' and 'CDE' are
  453. // different structures, although they have the same field sequence. They must
  454. // be described by different descriptors.
  455. //ASSERT_TRUE(MetaABC != MetaCDE);
  456. }
  457. TEST(TBAAMetadataTest, CTypedefFields3) {
  458. const char TestProgram[] = R"**(
  459. typedef struct {
  460. short f16;
  461. int f32;
  462. } ABC;
  463. typedef struct {
  464. int f32;
  465. short f16;
  466. } CDE;
  467. void func(ABC *A, CDE *B) {
  468. A->f32 = 4;
  469. A->f16 = 11;
  470. B->f32 = 44;
  471. B->f16 = 111;
  472. }
  473. )**";
  474. TestCompiler Compiler;
  475. Compiler.compiler.getLangOpts().C11 = 1;
  476. Compiler.init(TestProgram);
  477. const BasicBlock *BB = Compiler.compile();
  478. auto NamelessStruct1 = MMTuple(
  479. MMString(""),
  480. MMTuple(
  481. MMString("short"),
  482. OmnipotentCharC,
  483. MConstInt(0)),
  484. MConstInt(0),
  485. MMTuple(
  486. MMString("int"),
  487. OmnipotentCharC,
  488. MConstInt(0)),
  489. MConstInt(4));
  490. auto NamelessStruct2 = MMTuple(
  491. MMString(""),
  492. MMTuple(
  493. MMString("int"),
  494. OmnipotentCharC,
  495. MConstInt(0)),
  496. MConstInt(0),
  497. MMTuple(
  498. MMString("short"),
  499. OmnipotentCharC,
  500. MConstInt(0)),
  501. MConstInt(4));
  502. const Instruction *I = match(BB,
  503. MInstruction(Instruction::Store,
  504. MConstInt(4, 32),
  505. MMTuple(
  506. NamelessStruct1,
  507. MMTuple(
  508. MMString("int"),
  509. OmnipotentCharC,
  510. MConstInt(0)),
  511. MConstInt(4))));
  512. ASSERT_TRUE(I);
  513. I = matchNext(I,
  514. MInstruction(Instruction::Store,
  515. MConstInt(11, 16),
  516. MMTuple(
  517. NamelessStruct1,
  518. MMTuple(
  519. MMString("short"),
  520. OmnipotentCharC,
  521. MConstInt(0)),
  522. MConstInt(0))));
  523. ASSERT_TRUE(I);
  524. I = matchNext(I,
  525. MInstruction(Instruction::Store,
  526. MConstInt(44, 32),
  527. MMTuple(
  528. NamelessStruct2,
  529. MMTuple(
  530. MMString("int"),
  531. OmnipotentCharC,
  532. MConstInt(0)),
  533. MConstInt(0))));
  534. ASSERT_TRUE(I);
  535. I = matchNext(I,
  536. MInstruction(Instruction::Store,
  537. MConstInt(111, 16),
  538. MMTuple(
  539. NamelessStruct2,
  540. MMTuple(
  541. MMString("short"),
  542. OmnipotentCharC,
  543. MConstInt(0)),
  544. MConstInt(4))));
  545. ASSERT_TRUE(I);
  546. }
  547. TEST(TBAAMetadataTest, CXXFields) {
  548. const char TestProgram[] = R"**(
  549. struct ABC {
  550. short f16;
  551. int f32;
  552. long long f64;
  553. unsigned short f16_2;
  554. unsigned f32_2;
  555. unsigned long long f64_2;
  556. };
  557. void func(struct ABC *A) {
  558. A->f32 = 4;
  559. A->f16 = 11;
  560. A->f64 = 601;
  561. A->f16_2 = 22;
  562. A->f32_2 = 77;
  563. A->f64_2 = 604;
  564. }
  565. )**";
  566. TestCompiler Compiler;
  567. Compiler.compiler.getLangOpts().CPlusPlus = 1;
  568. Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
  569. Compiler.init(TestProgram);
  570. const BasicBlock *BB = Compiler.compile();
  571. auto StructABC = MMTuple(
  572. MMString("_ZTS3ABC"),
  573. MMTuple(
  574. MMString("short"),
  575. OmnipotentCharCXX,
  576. MConstInt(0)),
  577. MConstInt(0),
  578. MMTuple(
  579. MMString("int"),
  580. OmnipotentCharCXX,
  581. MConstInt(0)),
  582. MConstInt(4),
  583. MMTuple(
  584. MMString("long long"),
  585. OmnipotentCharCXX,
  586. MConstInt(0)),
  587. MConstInt(8),
  588. MSameAs(1),
  589. MConstInt(16),
  590. MSameAs(3),
  591. MConstInt(20),
  592. MSameAs(5),
  593. MConstInt(24));
  594. const Instruction *I = match(BB,
  595. MInstruction(Instruction::Store,
  596. MConstInt(4, 32),
  597. MMTuple(
  598. StructABC,
  599. MMTuple(
  600. MMString("int"),
  601. OmnipotentCharCXX,
  602. MConstInt(0)),
  603. MConstInt(4))));
  604. ASSERT_TRUE(I);
  605. I = matchNext(I,
  606. MInstruction(Instruction::Store,
  607. MConstInt(11, 16),
  608. MMTuple(
  609. StructABC,
  610. MMTuple(
  611. MMString("short"),
  612. OmnipotentCharCXX,
  613. MConstInt(0)),
  614. MConstInt(0))));
  615. ASSERT_TRUE(I);
  616. I = matchNext(I,
  617. MInstruction(Instruction::Store,
  618. MConstInt(601, 64),
  619. MMTuple(
  620. StructABC,
  621. MMTuple(
  622. MMString("long long"),
  623. OmnipotentCharCXX,
  624. MConstInt(0)),
  625. MConstInt(8))));
  626. ASSERT_TRUE(I);
  627. I = matchNext(I,
  628. MInstruction(Instruction::Store,
  629. MConstInt(22, 16),
  630. MMTuple(
  631. StructABC,
  632. MMTuple(
  633. MMString("short"),
  634. OmnipotentCharCXX,
  635. MConstInt(0)),
  636. MConstInt(16))));
  637. ASSERT_TRUE(I);
  638. I = matchNext(I,
  639. MInstruction(Instruction::Store,
  640. MConstInt(77, 32),
  641. MMTuple(
  642. StructABC,
  643. MMTuple(
  644. MMString("int"),
  645. OmnipotentCharCXX,
  646. MConstInt(0)),
  647. MConstInt(20))));
  648. ASSERT_TRUE(I);
  649. I = matchNext(I,
  650. MInstruction(Instruction::Store,
  651. MConstInt(604, 64),
  652. MMTuple(
  653. StructABC,
  654. MMTuple(
  655. MMString("long long"),
  656. OmnipotentCharCXX,
  657. MConstInt(0)),
  658. MConstInt(24))));
  659. ASSERT_TRUE(I);
  660. }
  661. TEST(TBAAMetadataTest, CXXTypedefFields) {
  662. const char TestProgram[] = R"**(
  663. typedef struct {
  664. short f16;
  665. int f32;
  666. } ABC;
  667. typedef struct {
  668. short value_f16;
  669. int value_f32;
  670. } CDE;
  671. void func(ABC *A, CDE *B) {
  672. A->f32 = 4;
  673. A->f16 = 11;
  674. B->value_f32 = 44;
  675. B->value_f16 = 111;
  676. }
  677. )**";
  678. TestCompiler Compiler;
  679. Compiler.compiler.getLangOpts().CPlusPlus = 1;
  680. Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
  681. Compiler.init(TestProgram);
  682. const BasicBlock *BB = Compiler.compile();
  683. auto StructABC = MMTuple(
  684. MMString("_ZTS3ABC"),
  685. MMTuple(
  686. MMString("short"),
  687. OmnipotentCharCXX,
  688. MConstInt(0)),
  689. MConstInt(0),
  690. MMTuple(
  691. MMString("int"),
  692. OmnipotentCharCXX,
  693. MConstInt(0)),
  694. MConstInt(4));
  695. auto StructCDE = MMTuple(
  696. MMString("_ZTS3CDE"),
  697. MMTuple(
  698. MMString("short"),
  699. OmnipotentCharCXX,
  700. MConstInt(0)),
  701. MConstInt(0),
  702. MMTuple(
  703. MMString("int"),
  704. OmnipotentCharCXX,
  705. MConstInt(0)),
  706. MConstInt(4));
  707. const Instruction *I = match(BB,
  708. MInstruction(Instruction::Store,
  709. MConstInt(4, 32),
  710. MMTuple(
  711. StructABC,
  712. MMTuple(
  713. MMString("int"),
  714. OmnipotentCharCXX,
  715. MConstInt(0)),
  716. MConstInt(4))));
  717. ASSERT_TRUE(I);
  718. I = matchNext(I,
  719. MInstruction(Instruction::Store,
  720. MConstInt(11, 16),
  721. MMTuple(
  722. StructABC,
  723. MMTuple(
  724. MMString("short"),
  725. OmnipotentCharCXX,
  726. MConstInt(0)),
  727. MConstInt(0))));
  728. ASSERT_TRUE(I);
  729. I = matchNext(I,
  730. MInstruction(Instruction::Store,
  731. MConstInt(44, 32),
  732. MMTuple(
  733. StructCDE,
  734. MMTuple(
  735. MMString("int"),
  736. OmnipotentCharCXX,
  737. MConstInt(0)),
  738. MConstInt(4))));
  739. ASSERT_TRUE(I);
  740. I = matchNext(I,
  741. MInstruction(Instruction::Store,
  742. MConstInt(111, 16),
  743. MMTuple(
  744. StructCDE,
  745. MMTuple(
  746. MMString("short"),
  747. OmnipotentCharCXX,
  748. MConstInt(0)),
  749. MConstInt(0))));
  750. ASSERT_TRUE(I);
  751. }
  752. TEST(TBAAMetadataTest, StructureFields) {
  753. const char TestProgram[] = R"**(
  754. struct Inner {
  755. int f32;
  756. };
  757. struct Outer {
  758. short f16;
  759. Inner b1;
  760. Inner b2;
  761. };
  762. void func(Outer *S) {
  763. S->f16 = 14;
  764. S->b1.f32 = 35;
  765. S->b2.f32 = 77;
  766. }
  767. )**";
  768. TestCompiler Compiler;
  769. Compiler.compiler.getLangOpts().CPlusPlus = 1;
  770. Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
  771. Compiler.init(TestProgram);
  772. const BasicBlock *BB = Compiler.compile();
  773. auto StructInner = MMTuple(
  774. MMString("_ZTS5Inner"),
  775. MMTuple(
  776. MMString("int"),
  777. OmnipotentCharCXX,
  778. MConstInt(0)),
  779. MConstInt(0));
  780. auto StructOuter = MMTuple(
  781. MMString("_ZTS5Outer"),
  782. MMTuple(
  783. MMString("short"),
  784. OmnipotentCharCXX,
  785. MConstInt(0)),
  786. MConstInt(0),
  787. StructInner,
  788. MConstInt(4),
  789. MSameAs(3),
  790. MConstInt(8));
  791. const Instruction *I = match(BB,
  792. MInstruction(Instruction::Store,
  793. MConstInt(14, 16),
  794. MMTuple(
  795. StructOuter,
  796. MMTuple(
  797. MMString("short"),
  798. OmnipotentCharCXX,
  799. MConstInt(0)),
  800. MConstInt(0))));
  801. ASSERT_TRUE(I);
  802. I = matchNext(I,
  803. MInstruction(Instruction::Store,
  804. MConstInt(35, 32),
  805. MMTuple(
  806. StructOuter,
  807. MMTuple(
  808. MMString("int"),
  809. OmnipotentCharCXX,
  810. MConstInt(0)),
  811. MConstInt(4))));
  812. ASSERT_TRUE(I);
  813. I = matchNext(I,
  814. MInstruction(Instruction::Store,
  815. MConstInt(77, 32),
  816. MMTuple(
  817. StructOuter,
  818. MMTuple(
  819. MMString("int"),
  820. OmnipotentCharCXX,
  821. MConstInt(0)),
  822. MConstInt(8))));
  823. ASSERT_TRUE(I);
  824. }
  825. TEST(TBAAMetadataTest, ArrayFields) {
  826. const char TestProgram[] = R"**(
  827. struct Inner {
  828. int f32;
  829. };
  830. struct Outer {
  831. short f16;
  832. Inner b1[2];
  833. };
  834. void func(Outer *S) {
  835. S->f16 = 14;
  836. S->b1[0].f32 = 35;
  837. S->b1[1].f32 = 77;
  838. }
  839. )**";
  840. TestCompiler Compiler;
  841. Compiler.compiler.getLangOpts().CPlusPlus = 1;
  842. Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
  843. Compiler.init(TestProgram);
  844. const BasicBlock *BB = Compiler.compile();
  845. auto StructInner = MMTuple(
  846. MMString("_ZTS5Inner"),
  847. MMTuple(
  848. MMString("int"),
  849. OmnipotentCharCXX,
  850. MConstInt(0)),
  851. MConstInt(0));
  852. auto StructOuter = MMTuple(
  853. MMString("_ZTS5Outer"),
  854. MMTuple(
  855. MMString("short"),
  856. OmnipotentCharCXX,
  857. MConstInt(0)),
  858. MConstInt(0),
  859. OmnipotentCharCXX, // FIXME: Info about array field is lost.
  860. MConstInt(4));
  861. const Instruction *I = match(BB,
  862. MInstruction(Instruction::Store,
  863. MConstInt(14, 16),
  864. MMTuple(
  865. StructOuter,
  866. MMTuple(
  867. MMString("short"),
  868. OmnipotentCharCXX,
  869. MConstInt(0)),
  870. MConstInt(0))));
  871. ASSERT_TRUE(I);
  872. I = matchNext(I,
  873. MInstruction(Instruction::Store,
  874. MConstInt(35, 32),
  875. MMTuple(
  876. StructInner,
  877. MMTuple(
  878. MMString("int"),
  879. OmnipotentCharCXX,
  880. MConstInt(0)),
  881. MConstInt(0))));
  882. ASSERT_TRUE(I);
  883. I = matchNext(I,
  884. MInstruction(Instruction::Store,
  885. MConstInt(77, 32),
  886. MMTuple(
  887. StructInner,
  888. MMTuple(
  889. MMString("int"),
  890. OmnipotentCharCXX,
  891. MConstInt(0)),
  892. MConstInt(0))));
  893. ASSERT_TRUE(I);
  894. }
  895. TEST(TBAAMetadataTest, BaseClass) {
  896. const char TestProgram[] = R"**(
  897. struct Base {
  898. int f32;
  899. };
  900. struct Derived : public Base {
  901. short f16;
  902. };
  903. void func(Base *B, Derived *D) {
  904. B->f32 = 14;
  905. D->f16 = 35;
  906. D->f32 = 77;
  907. }
  908. )**";
  909. TestCompiler Compiler;
  910. Compiler.compiler.getLangOpts().CPlusPlus = 1;
  911. Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
  912. Compiler.init(TestProgram);
  913. const BasicBlock *BB = Compiler.compile();
  914. auto ClassBase = MMTuple(
  915. MMString("_ZTS4Base"),
  916. MMTuple(
  917. MMString("int"),
  918. OmnipotentCharCXX,
  919. MConstInt(0)),
  920. MConstInt(0));
  921. auto ClassDerived = MMTuple(
  922. MMString("_ZTS7Derived"),
  923. MMTuple(
  924. MMString("short"),
  925. OmnipotentCharCXX,
  926. MConstInt(0)),
  927. MConstInt(4));
  928. const Instruction *I = match(BB,
  929. MInstruction(Instruction::Store,
  930. MConstInt(14, 32),
  931. MMTuple(
  932. ClassBase,
  933. MMTuple(
  934. MMString("int"),
  935. OmnipotentCharCXX,
  936. MConstInt(0)),
  937. MConstInt(0))));
  938. ASSERT_TRUE(I);
  939. I = matchNext(I,
  940. MInstruction(Instruction::Store,
  941. MConstInt(35, 16),
  942. MMTuple(
  943. ClassDerived,
  944. MMTuple(
  945. MMString("short"),
  946. OmnipotentCharCXX,
  947. MConstInt(0)),
  948. MConstInt(4))));
  949. ASSERT_TRUE(I);
  950. I = matchNext(I,
  951. MInstruction(Instruction::Store,
  952. MConstInt(77, 32),
  953. MMTuple(
  954. ClassBase,
  955. MMTuple(
  956. MMString("int"),
  957. OmnipotentCharCXX,
  958. MConstInt(0)),
  959. MConstInt(0))));
  960. ASSERT_TRUE(I);
  961. }
  962. TEST(TBAAMetadataTest, PolymorphicClass) {
  963. const char TestProgram[] = R"**(
  964. struct Base {
  965. virtual void m1(int *) = 0;
  966. int f32;
  967. };
  968. struct Derived : public Base {
  969. virtual void m1(int *) override;
  970. short f16;
  971. };
  972. void func(Base *B, Derived *D) {
  973. B->f32 = 14;
  974. D->f16 = 35;
  975. D->f32 = 77;
  976. }
  977. )**";
  978. TestCompiler Compiler;
  979. Compiler.compiler.getLangOpts().CPlusPlus = 1;
  980. Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
  981. Compiler.init(TestProgram);
  982. const BasicBlock *BB = Compiler.compile();
  983. auto ClassBase = MMTuple(
  984. MMString("_ZTS4Base"),
  985. MMTuple(
  986. MMString("int"),
  987. OmnipotentCharCXX,
  988. MConstInt(0)),
  989. MConstInt(Compiler.PtrSize));
  990. auto ClassDerived = MMTuple(
  991. MMString("_ZTS7Derived"),
  992. MMTuple(
  993. MMString("short"),
  994. OmnipotentCharCXX,
  995. MConstInt(0)),
  996. MConstInt(Compiler.PtrSize + 4));
  997. const Instruction *I = match(BB,
  998. MInstruction(Instruction::Store,
  999. MConstInt(14, 32),
  1000. MMTuple(
  1001. ClassBase,
  1002. MMTuple(
  1003. MMString("int"),
  1004. OmnipotentCharCXX,
  1005. MConstInt(0)),
  1006. MConstInt(Compiler.PtrSize))));
  1007. ASSERT_TRUE(I);
  1008. I = matchNext(I,
  1009. MInstruction(Instruction::Store,
  1010. MConstInt(35, 16),
  1011. MMTuple(
  1012. ClassDerived,
  1013. MMTuple(
  1014. MMString("short"),
  1015. OmnipotentCharCXX,
  1016. MConstInt(0)),
  1017. MConstInt(Compiler.PtrSize + 4))));
  1018. ASSERT_TRUE(I);
  1019. I = matchNext(I,
  1020. MInstruction(Instruction::Store,
  1021. MConstInt(77, 32),
  1022. MMTuple(
  1023. ClassBase,
  1024. MMTuple(
  1025. MMString("int"),
  1026. OmnipotentCharCXX,
  1027. MConstInt(0)),
  1028. MConstInt(Compiler.PtrSize))));
  1029. ASSERT_TRUE(I);
  1030. }
  1031. TEST(TBAAMetadataTest, VirtualBase) {
  1032. const char TestProgram[] = R"**(
  1033. struct Base {
  1034. int f32;
  1035. };
  1036. struct Derived : public virtual Base {
  1037. short f16;
  1038. };
  1039. void func(Base *B, Derived *D) {
  1040. B->f32 = 14;
  1041. D->f16 = 35;
  1042. D->f32 = 77;
  1043. }
  1044. )**";
  1045. TestCompiler Compiler;
  1046. Compiler.compiler.getLangOpts().CPlusPlus = 1;
  1047. Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
  1048. Compiler.init(TestProgram);
  1049. const BasicBlock *BB = Compiler.compile();
  1050. auto ClassBase = MMTuple(
  1051. MMString("_ZTS4Base"),
  1052. MMTuple(
  1053. MMString("int"),
  1054. OmnipotentCharCXX,
  1055. MConstInt(0)),
  1056. MConstInt(0));
  1057. auto ClassDerived = MMTuple(
  1058. MMString("_ZTS7Derived"),
  1059. MMTuple(
  1060. MMString("short"),
  1061. OmnipotentCharCXX,
  1062. MConstInt(0)),
  1063. MConstInt(Compiler.PtrSize));
  1064. const Instruction *I = match(BB,
  1065. MInstruction(Instruction::Store,
  1066. MConstInt(14, 32),
  1067. MMTuple(
  1068. ClassBase,
  1069. MMTuple(
  1070. MMString("int"),
  1071. OmnipotentCharCXX,
  1072. MConstInt(0)),
  1073. MConstInt(0))));
  1074. ASSERT_TRUE(I);
  1075. I = matchNext(I,
  1076. MInstruction(Instruction::Store,
  1077. MConstInt(35, 16),
  1078. MMTuple(
  1079. ClassDerived,
  1080. MMTuple(
  1081. MMString("short"),
  1082. OmnipotentCharCXX,
  1083. MConstInt(0)),
  1084. MConstInt(Compiler.PtrSize))));
  1085. ASSERT_TRUE(I);
  1086. I = matchNext(I,
  1087. MInstruction(Instruction::Load,
  1088. MMTuple(
  1089. MMTuple(
  1090. MMString("vtable pointer"),
  1091. MMTuple(
  1092. MMString("Simple C++ TBAA")),
  1093. MConstInt(0)),
  1094. MSameAs(0),
  1095. MConstInt(0))));
  1096. ASSERT_TRUE(I);
  1097. I = matchNext(I,
  1098. MInstruction(Instruction::Store,
  1099. MConstInt(77, 32),
  1100. MMTuple(
  1101. ClassBase,
  1102. MMTuple(
  1103. MMString("int"),
  1104. OmnipotentCharCXX,
  1105. MConstInt(0)),
  1106. MConstInt(0))));
  1107. ASSERT_TRUE(I);
  1108. }
  1109. TEST(TBAAMetadataTest, TemplSpec) {
  1110. const char TestProgram[] = R"**(
  1111. template<typename T1, typename T2>
  1112. struct ABC {
  1113. T1 f1;
  1114. T2 f2;
  1115. };
  1116. void func(ABC<double, int> *p) {
  1117. p->f1 = 12.1;
  1118. p->f2 = 44;
  1119. }
  1120. )**";
  1121. TestCompiler Compiler;
  1122. Compiler.compiler.getLangOpts().CPlusPlus = 1;
  1123. Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
  1124. Compiler.init(TestProgram);
  1125. const BasicBlock *BB = Compiler.compile();
  1126. auto SpecABC = MMTuple(
  1127. MMString("_ZTS3ABCIdiE"),
  1128. MMTuple(
  1129. MMString("double"),
  1130. OmnipotentCharCXX,
  1131. MConstInt(0)),
  1132. MConstInt(0),
  1133. MMTuple(
  1134. MMString("int"),
  1135. OmnipotentCharCXX,
  1136. MConstInt(0)),
  1137. MConstInt(8));
  1138. const Instruction *I = match(BB,
  1139. MInstruction(Instruction::Store,
  1140. MValType(MType([](const Type &T)->bool { return T.isDoubleTy(); })),
  1141. MMTuple(
  1142. SpecABC,
  1143. MMTuple(
  1144. MMString("double"),
  1145. OmnipotentCharCXX,
  1146. MConstInt(0)),
  1147. MConstInt(0))));
  1148. ASSERT_TRUE(I);
  1149. I = matchNext(I,
  1150. MInstruction(Instruction::Store,
  1151. MConstInt(44, 32),
  1152. MMTuple(
  1153. SpecABC,
  1154. MMTuple(
  1155. MMString("int"),
  1156. OmnipotentCharCXX,
  1157. MConstInt(0)),
  1158. MConstInt(8))));
  1159. ASSERT_TRUE(I);
  1160. }
  1161. }