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