Benjamin Kramer | c1f5ae2 | 2017-06-02 13:10:31 +0000 | [diff] [blame] | 1 | //===- OrderedBasicBlockTest.cpp - OrderedBasicBlock unit tests -----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Benjamin Kramer | c1f5ae2 | 2017-06-02 13:10:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Analysis/OrderedBasicBlock.h" |
| 10 | #include "llvm/AsmParser/Parser.h" |
| 11 | #include "llvm/IR/BasicBlock.h" |
| 12 | #include "llvm/IR/Function.h" |
| 13 | #include "llvm/IR/LLVMContext.h" |
| 14 | #include "llvm/IR/Module.h" |
| 15 | #include "llvm/Support/DataTypes.h" |
| 16 | #include "llvm/Support/SourceMgr.h" |
| 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | namespace llvm { |
| 20 | namespace { |
| 21 | |
| 22 | class OrderedBasicBlockTest : public testing::Test { |
| 23 | protected: |
| 24 | LLVMContext C; |
| 25 | |
| 26 | std::unique_ptr<Module> makeLLVMModule() { |
| 27 | const char *ModuleString = R"(define i32 @f(i32 %x) { |
| 28 | %add = add i32 %x, 42 |
| 29 | ret i32 %add |
| 30 | })"; |
| 31 | SMDiagnostic Err; |
| 32 | auto foo = parseAssemblyString(ModuleString, Err, C); |
| 33 | return foo; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | TEST_F(OrderedBasicBlockTest, Basic) { |
| 38 | auto M = makeLLVMModule(); |
| 39 | Function *F = M->getFunction("f"); |
| 40 | BasicBlock::iterator I = F->front().begin(); |
| 41 | Instruction *Add = &*I++; |
| 42 | Instruction *Ret = &*I++; |
| 43 | |
| 44 | OrderedBasicBlock OBB(&F->front()); |
| 45 | // Intentionally duplicated to verify cached and uncached are the same. |
| 46 | EXPECT_FALSE(OBB.dominates(Add, Add)); |
| 47 | EXPECT_FALSE(OBB.dominates(Add, Add)); |
| 48 | EXPECT_TRUE(OBB.dominates(Add, Ret)); |
| 49 | EXPECT_TRUE(OBB.dominates(Add, Ret)); |
| 50 | EXPECT_FALSE(OBB.dominates(Ret, Add)); |
| 51 | EXPECT_FALSE(OBB.dominates(Ret, Add)); |
| 52 | EXPECT_FALSE(OBB.dominates(Ret, Ret)); |
| 53 | EXPECT_FALSE(OBB.dominates(Ret, Ret)); |
| 54 | } |
| 55 | |
| 56 | } // end anonymous namespace |
| 57 | } // end namespace llvm |