1 //===--- MixedTBAATest.cpp - Mixed TBAA unit tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
11 #include "llvm/Analysis/Passes.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/MDBuilder.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/LegacyPassManager.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "gtest/gtest.h"
24 class MixedTBAATest : public testing::Test {
26 MixedTBAATest() : M("MixedTBAATest", C), MD(C) {}
31 legacy::PassManager PM;
34 TEST_F(MixedTBAATest, MixedTBAA) {
36 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
37 std::vector<Type *>(), false);
38 auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
39 auto *BB = BasicBlock::Create(C, "entry", F);
40 auto IntType = Type::getInt32Ty(C);
41 auto PtrType = Type::getInt32PtrTy(C);
42 auto *Value = ConstantInt::get(IntType, 42);
43 auto *Addr = ConstantPointerNull::get(PtrType);
45 auto *Store1 = new StoreInst(Value, Addr, BB);
46 auto *Store2 = new StoreInst(Value, Addr, BB);
47 ReturnInst::Create(C, nullptr, BB);
51 auto RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
52 auto MD1 = MD.createTBAAScalarTypeNode("omnipotent char", RootMD);
53 auto MD2 = MD.createTBAAScalarTypeNode("int", MD1);
54 auto MD3 = MD.createTBAAStructTagNode(MD2, MD2, 0);
55 Store2->setMetadata(LLVMContext::MD_tbaa, MD3);
60 auto RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
61 auto MD1 = MD.createTBAANode("omnipotent char", RootMD);
62 auto MD2 = MD.createTBAANode("int", MD1);
63 Store1->setMetadata(LLVMContext::MD_tbaa, MD2);
66 // Run the TBAA eval pass on a mixture of path-aware and non-path-aware TBAA.
67 // The order of the metadata (path-aware vs non-path-aware) is important,
68 // because the AA eval pass only runs one test per store-pair.
69 const char* args[] = { "MixedTBAATest", "-evaluate-aa-metadata" };
70 cl::ParseCommandLineOptions(sizeof(args) / sizeof(const char*), args);
71 PM.add(createTypeBasedAAWrapperPass());
72 PM.add(createAAEvalPass());
76 } // end anonymous namspace
77 } // end llvm namespace