blob: 473fe7f50fc8baddb9b403a9cb31907289b8fb68 [file] [log] [blame]
Xin Tong9d6f08a2017-06-06 02:34:41 +00001//===- OrderedInstructions.cpp - Unit tests for OrderedInstructions ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Tong9d6f08a2017-06-06 02:34:41 +00006//
7//===----------------------------------------------------------------------===//
8
Max Kazantsevd3a4cbe2018-08-30 04:49:03 +00009#include "llvm/Analysis/OrderedInstructions.h"
Xin Tong9d6f08a2017-06-06 02:34:41 +000010#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
18using namespace llvm;
19
20/// Check intra-basicblock and inter-basicblock dominance using
21/// OrderedInstruction.
22TEST(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 Knight13680222019-02-01 02:28:03 +000028 Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
Xin Tong9d6f08a2017-06-06 02:34:41 +000029
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 Knight14359ef2019-02-01 20:44:24 +000047 LoadInst *LoadInstX = B.CreateLoad(B.getInt8Ty(), PointerArg);
48 LoadInst *LoadInstY = B.CreateLoad(B.getInt8Ty(), PointerArg);
Xin Tong9d6f08a2017-06-06 02:34:41 +000049
50 // Create BBY with 1 load.
51 BasicBlock *BBY = BasicBlock::Create(Ctx, "bby", F);
52 B.SetInsertPoint(BBY);
James Y Knight14359ef2019-02-01 20:44:24 +000053 LoadInst *LoadInstZ = B.CreateLoad(B.getInt8Ty(), PointerArg);
Xin Tong9d6f08a2017-06-06 02:34:41 +000054 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}