1 //===- OrderedInstructions.cpp - Unit tests for OrderedInstructions ------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/OrderedInstructions.h"
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"
20 /// Check intra-basicblock and inter-basicblock dominance using
21 /// OrderedInstruction.
22 TEST(OrderedInstructionsTest
, DominanceTest
) {
24 Module
M("test", Ctx
);
27 FunctionType::get(Type::getVoidTy(Ctx
), {B
.getInt8PtrTy()}, false);
28 Function
*F
= Function::Create(FTy
, Function::ExternalLinkage
, "f", M
);
30 // Create the function as follow and check for dominance relation.
40 // More specifically, check for loadx -> (dominates) loady,
41 // loady -> loadx and loady -> loadz.
43 // Create BBX with 2 loads.
44 BasicBlock
*BBX
= BasicBlock::Create(Ctx
, "bbx", F
);
45 B
.SetInsertPoint(BBX
);
46 Argument
*PointerArg
= &*F
->arg_begin();
47 LoadInst
*LoadInstX
= B
.CreateLoad(B
.getInt8Ty(), PointerArg
);
48 LoadInst
*LoadInstY
= B
.CreateLoad(B
.getInt8Ty(), PointerArg
);
50 // Create BBY with 1 load.
51 BasicBlock
*BBY
= BasicBlock::Create(Ctx
, "bby", F
);
52 B
.SetInsertPoint(BBY
);
53 LoadInst
*LoadInstZ
= B
.CreateLoad(B
.getInt8Ty(), PointerArg
);
54 B
.CreateRet(LoadInstZ
);
55 std::unique_ptr
<DominatorTree
> DT(new DominatorTree(*F
));
56 OrderedInstructions
OI(&*DT
);
58 // Intra-BB dominance test.
59 EXPECT_TRUE(OI
.dominates(LoadInstX
, LoadInstY
));
60 EXPECT_FALSE(OI
.dominates(LoadInstY
, LoadInstX
));
62 // Inter-BB dominance test.
63 EXPECT_TRUE(OI
.dominates(LoadInstY
, LoadInstZ
));