Juergen Ributzka | d35c114 | 2014-05-03 22:32:52 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 1db2282 | 2015-08-14 03:33:48 +0000 | [diff] [blame] | 10 | #include "llvm/Analysis/TypeBasedAliasAnalysis.h" |
Juergen Ributzka | d35c114 | 2014-05-03 22:32:52 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 17 | #include "llvm/IR/LegacyPassManager.h" |
Juergen Ributzka | d35c114 | 2014-05-03 22:32:52 +0000 | [diff] [blame] | 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; |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 31 | legacy::PassManager PM; |
Juergen Ributzka | d35c114 | 2014-05-03 22:32:52 +0000 | [diff] [blame] | 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); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 47 | ReturnInst::Create(C, nullptr, BB); |
Juergen Ributzka | d35c114 | 2014-05-03 22:32:52 +0000 | [diff] [blame] | 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. |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 69 | const char* args[] = { "MixedTBAATest", "-evaluate-aa-metadata" }; |
Juergen Ributzka | d35c114 | 2014-05-03 22:32:52 +0000 | [diff] [blame] | 70 | cl::ParseCommandLineOptions(sizeof(args) / sizeof(const char*), args); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 71 | PM.add(createTypeBasedAAWrapperPass()); |
Juergen Ributzka | d35c114 | 2014-05-03 22:32:52 +0000 | [diff] [blame] | 72 | PM.add(createAAEvalPass()); |
| 73 | PM.run(M); |
| 74 | } |
| 75 | |
| 76 | } // end anonymous namspace |
| 77 | } // end llvm namespace |
| 78 | |