MixedTBAATest.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //===--- MixedTBAATest.cpp - Mixed TBAA unit tests ------------------------===//
  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. #include "llvm/Analysis/Passes.h"
  10. #include "llvm/IR/Constants.h"
  11. #include "llvm/IR/Instructions.h"
  12. #include "llvm/IR/LLVMContext.h"
  13. #include "llvm/IR/MDBuilder.h"
  14. #include "llvm/IR/Module.h"
  15. #include "llvm/IR/LegacyPassManager.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "gtest/gtest.h"
  18. namespace llvm {
  19. namespace {
  20. class MixedTBAATest : public testing::Test {
  21. protected:
  22. MixedTBAATest() : M("MixedTBAATest", C), MD(C) {}
  23. LLVMContext C;
  24. Module M;
  25. MDBuilder MD;
  26. legacy::PassManager PM;
  27. };
  28. TEST_F(MixedTBAATest, MixedTBAA) {
  29. // Setup function.
  30. FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
  31. std::vector<Type *>(), false);
  32. auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
  33. auto *BB = BasicBlock::Create(C, "entry", F);
  34. auto IntType = Type::getInt32Ty(C);
  35. auto PtrType = Type::getInt32PtrTy(C);
  36. auto *Value = ConstantInt::get(IntType, 42);
  37. auto *Addr = ConstantPointerNull::get(PtrType);
  38. auto *Store1 = new StoreInst(Value, Addr, BB);
  39. auto *Store2 = new StoreInst(Value, Addr, BB);
  40. ReturnInst::Create(C, nullptr, BB);
  41. // New TBAA metadata
  42. {
  43. auto RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
  44. auto MD1 = MD.createTBAAScalarTypeNode("omnipotent char", RootMD);
  45. auto MD2 = MD.createTBAAScalarTypeNode("int", MD1);
  46. auto MD3 = MD.createTBAAStructTagNode(MD2, MD2, 0);
  47. Store2->setMetadata(LLVMContext::MD_tbaa, MD3);
  48. }
  49. // Old TBAA metadata
  50. {
  51. auto RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
  52. auto MD1 = MD.createTBAANode("omnipotent char", RootMD);
  53. auto MD2 = MD.createTBAANode("int", MD1);
  54. Store1->setMetadata(LLVMContext::MD_tbaa, MD2);
  55. }
  56. // Run the TBAA eval pass on a mixture of path-aware and non-path-aware TBAA.
  57. // The order of the metadata (path-aware vs non-path-aware) is important,
  58. // because the AA eval pass only runs one test per store-pair.
  59. const char* args[] = { "MixedTBAATest", "-evaluate-aa-metadata" };
  60. cl::ParseCommandLineOptions(sizeof(args) / sizeof(const char*), args);
  61. PM.add(createTypeBasedAliasAnalysisPass());
  62. PM.add(createAAEvalPass());
  63. PM.run(M);
  64. }
  65. } // end anonymous namspace
  66. } // end llvm namespace