Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 1 | //===- OrderedInstructions.cpp - Unit tests for OrderedInstructions ------===// |
| 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 |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Max Kazantsev | d3a4cbe | 2018-08-30 04:49:03 +0000 | [diff] [blame] | 9 | #include "llvm/Analysis/OrderedInstructions.h" |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 10 | #include "llvm/IR/BasicBlock.h" |
| 11 | #include "llvm/IR/Dominators.h" |
| 12 | #include "llvm/IR/IRBuilder.h" |
| 13 | #include "llvm/IR/Instructions.h" |
| 14 | #include "llvm/IR/LLVMContext.h" |
| 15 | #include "llvm/IR/Module.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | /// Check intra-basicblock and inter-basicblock dominance using |
| 21 | /// OrderedInstruction. |
| 22 | TEST(OrderedInstructionsTest, DominanceTest) { |
| 23 | LLVMContext Ctx; |
| 24 | Module M("test", Ctx); |
| 25 | IRBuilder<> B(Ctx); |
| 26 | FunctionType *FTy = |
| 27 | FunctionType::get(Type::getVoidTy(Ctx), {B.getInt8PtrTy()}, false); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 28 | Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M); |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 29 | |
| 30 | // Create the function as follow and check for dominance relation. |
| 31 | // |
| 32 | // test(): |
| 33 | // bbx: |
| 34 | // loadx; |
| 35 | // loady; |
| 36 | // bby: |
| 37 | // loadz; |
| 38 | // return; |
| 39 | // |
| 40 | // More specifically, check for loadx -> (dominates) loady, |
| 41 | // loady -> loadx and loady -> loadz. |
| 42 | // |
| 43 | // Create BBX with 2 loads. |
| 44 | BasicBlock *BBX = BasicBlock::Create(Ctx, "bbx", F); |
| 45 | B.SetInsertPoint(BBX); |
| 46 | Argument *PointerArg = &*F->arg_begin(); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 47 | LoadInst *LoadInstX = B.CreateLoad(B.getInt8Ty(), PointerArg); |
| 48 | LoadInst *LoadInstY = B.CreateLoad(B.getInt8Ty(), PointerArg); |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 49 | |
| 50 | // Create BBY with 1 load. |
| 51 | BasicBlock *BBY = BasicBlock::Create(Ctx, "bby", F); |
| 52 | B.SetInsertPoint(BBY); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 53 | LoadInst *LoadInstZ = B.CreateLoad(B.getInt8Ty(), PointerArg); |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 54 | B.CreateRet(LoadInstZ); |
| 55 | std::unique_ptr<DominatorTree> DT(new DominatorTree(*F)); |
| 56 | OrderedInstructions OI(&*DT); |
| 57 | |
| 58 | // Intra-BB dominance test. |
| 59 | EXPECT_TRUE(OI.dominates(LoadInstX, LoadInstY)); |
| 60 | EXPECT_FALSE(OI.dominates(LoadInstY, LoadInstX)); |
| 61 | |
| 62 | // Inter-BB dominance test. |
| 63 | EXPECT_TRUE(OI.dominates(LoadInstY, LoadInstZ)); |
| 64 | } |