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