[Support] Change SaturatingAdd()/SaturatingMultiply() to use pointer for returning...
[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/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"
20
21 namespace llvm {
22 namespace {
23
24 class MixedTBAATest : public testing::Test {
25 protected:
26   MixedTBAATest() : M("MixedTBAATest", C), MD(C) {}
27
28   LLVMContext C;
29   Module M;
30   MDBuilder MD;
31   legacy::PassManager PM;
32 };
33
34 TEST_F(MixedTBAATest, MixedTBAA) {
35   // Setup function.
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);
44
45   auto *Store1 = new StoreInst(Value, Addr, BB);
46   auto *Store2 = new StoreInst(Value, Addr, BB);
47   ReturnInst::Create(C, nullptr, BB);
48
49   // New TBAA metadata
50   {
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);
56   }
57
58   // Old TBAA metadata
59   {
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);
64   }
65
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());
73   PM.run(M);
74 }
75
76 } // end anonymous namspace
77 } // end llvm namespace
78