blob: d0cfa59f6459e251118ed521f095c5721ddc666b [file] [log] [blame]
Juergen Ributzkad35c1142014-05-03 22:32:52 +00001//===--- 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
Chandler Carruth1db22822015-08-14 03:33:48 +000010#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Juergen Ributzkad35c1142014-05-03 22:32:52 +000011#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"
Chandler Carruth30d69c22015-02-13 10:01:29 +000017#include "llvm/IR/LegacyPassManager.h"
Juergen Ributzkad35c1142014-05-03 22:32:52 +000018#include "llvm/Support/CommandLine.h"
19#include "gtest/gtest.h"
20
21namespace llvm {
22namespace {
23
24class MixedTBAATest : public testing::Test {
25protected:
26 MixedTBAATest() : M("MixedTBAATest", C), MD(C) {}
27
28 LLVMContext C;
29 Module M;
30 MDBuilder MD;
Chandler Carruth30d69c22015-02-13 10:01:29 +000031 legacy::PassManager PM;
Juergen Ributzkad35c1142014-05-03 22:32:52 +000032};
33
34TEST_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);
Craig Topper66f09ad2014-06-08 22:29:17 +000047 ReturnInst::Create(C, nullptr, BB);
Juergen Ributzkad35c1142014-05-03 22:32:52 +000048
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.
Hal Finkelcc39b672014-07-24 12:16:19 +000069 const char* args[] = { "MixedTBAATest", "-evaluate-aa-metadata" };
Juergen Ributzkad35c1142014-05-03 22:32:52 +000070 cl::ParseCommandLineOptions(sizeof(args) / sizeof(const char*), args);
Chandler Carruth7b560d42015-09-09 17:55:00 +000071 PM.add(createTypeBasedAAWrapperPass());
Juergen Ributzkad35c1142014-05-03 22:32:52 +000072 PM.add(createAAEvalPass());
73 PM.run(M);
74}
75
76} // end anonymous namspace
77} // end llvm namespace
78