blob: d70324f2c6abdd8205a5374fedb5abbeb131c41a [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"
Chandler Carruth4f846a52016-02-20 03:46:03 +000011#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Juergen Ributzkad35c1142014-05-03 22:32:52 +000012#include "llvm/Analysis/Passes.h"
13#include "llvm/IR/Constants.h"
14#include "llvm/IR/Instructions.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/MDBuilder.h"
17#include "llvm/IR/Module.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000018#include "llvm/IR/LegacyPassManager.h"
Juergen Ributzkad35c1142014-05-03 22:32:52 +000019#include "llvm/Support/CommandLine.h"
20#include "gtest/gtest.h"
21
22namespace llvm {
23namespace {
24
25class MixedTBAATest : public testing::Test {
26protected:
27 MixedTBAATest() : M("MixedTBAATest", C), MD(C) {}
28
29 LLVMContext C;
30 Module M;
31 MDBuilder MD;
Chandler Carruth30d69c22015-02-13 10:01:29 +000032 legacy::PassManager PM;
Juergen Ributzkad35c1142014-05-03 22:32:52 +000033};
34
35TEST_F(MixedTBAATest, MixedTBAA) {
36 // Setup function.
37 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
38 std::vector<Type *>(), false);
39 auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
40 auto *BB = BasicBlock::Create(C, "entry", F);
41 auto IntType = Type::getInt32Ty(C);
42 auto PtrType = Type::getInt32PtrTy(C);
43 auto *Value = ConstantInt::get(IntType, 42);
44 auto *Addr = ConstantPointerNull::get(PtrType);
45
46 auto *Store1 = new StoreInst(Value, Addr, BB);
47 auto *Store2 = new StoreInst(Value, Addr, BB);
Craig Topper66f09ad2014-06-08 22:29:17 +000048 ReturnInst::Create(C, nullptr, BB);
Juergen Ributzkad35c1142014-05-03 22:32:52 +000049
50 // New TBAA metadata
51 {
52 auto RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
53 auto MD1 = MD.createTBAAScalarTypeNode("omnipotent char", RootMD);
54 auto MD2 = MD.createTBAAScalarTypeNode("int", MD1);
55 auto MD3 = MD.createTBAAStructTagNode(MD2, MD2, 0);
56 Store2->setMetadata(LLVMContext::MD_tbaa, MD3);
57 }
58
59 // Old TBAA metadata
60 {
61 auto RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
62 auto MD1 = MD.createTBAANode("omnipotent char", RootMD);
63 auto MD2 = MD.createTBAANode("int", MD1);
64 Store1->setMetadata(LLVMContext::MD_tbaa, MD2);
65 }
66
67 // Run the TBAA eval pass on a mixture of path-aware and non-path-aware TBAA.
68 // The order of the metadata (path-aware vs non-path-aware) is important,
69 // because the AA eval pass only runs one test per store-pair.
Hal Finkelcc39b672014-07-24 12:16:19 +000070 const char* args[] = { "MixedTBAATest", "-evaluate-aa-metadata" };
Juergen Ributzkad35c1142014-05-03 22:32:52 +000071 cl::ParseCommandLineOptions(sizeof(args) / sizeof(const char*), args);
Chandler Carruth7b560d42015-09-09 17:55:00 +000072 PM.add(createTypeBasedAAWrapperPass());
Juergen Ributzkad35c1142014-05-03 22:32:52 +000073 PM.add(createAAEvalPass());
74 PM.run(M);
75}
76
77} // end anonymous namspace
78} // end llvm namespace
79