7b8a25c4438928eea8956a8ca7fb21a7d0d98a6b
[oota-llvm.git] / unittests / Analysis / MixedTBAATest.cpp
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/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"
19
20 namespace llvm {
21 namespace {
22
23 class MixedTBAATest : public testing::Test {
24 protected:
25   MixedTBAATest() : M("MixedTBAATest", C), MD(C) {}
26
27   LLVMContext C;
28   Module M;
29   MDBuilder MD;
30   legacy::PassManager PM;
31 };
32
33 TEST_F(MixedTBAATest, MixedTBAA) {
34   // Setup function.
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);
43
44   auto *Store1 = new StoreInst(Value, Addr, BB);
45   auto *Store2 = new StoreInst(Value, Addr, BB);
46   ReturnInst::Create(C, nullptr, BB);
47
48   // New TBAA metadata
49   {
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);
55   }
56
57   // Old TBAA metadata
58   {
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);
63   }
64
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());
72   PM.run(M);
73 }
74
75 } // end anonymous namspace
76 } // end llvm namespace
77