1 //===- llvm/unittest/IR/BasicBlockTest.cpp - BasicBlock unit tests --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/BasicBlock.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/IRBuilder.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/NoFolder.h"
17 #include "gmock/gmock-matchers.h"
18 #include "gtest/gtest.h"
24 TEST(BasicBlockTest
, PhiRange
) {
27 // Create the main block.
28 std::unique_ptr
<BasicBlock
> BB(BasicBlock::Create(Context
));
30 // Create some predecessors of it.
31 std::unique_ptr
<BasicBlock
> BB1(BasicBlock::Create(Context
));
32 BranchInst::Create(BB
.get(), BB1
.get());
33 std::unique_ptr
<BasicBlock
> BB2(BasicBlock::Create(Context
));
34 BranchInst::Create(BB
.get(), BB2
.get());
36 // Make sure this doesn't crash if there are no phis.
37 for (auto &PN
: BB
->phis()) {
39 EXPECT_TRUE(false) << "empty block should have no phis";
43 auto *BI
= BranchInst::Create(BB
.get(), BB
.get());
45 // Now insert some PHI nodes.
46 auto *Int32Ty
= Type::getInt32Ty(Context
);
47 auto *P1
= PHINode::Create(Int32Ty
, /*NumReservedValues*/ 3, "phi.1", BI
);
48 auto *P2
= PHINode::Create(Int32Ty
, /*NumReservedValues*/ 3, "phi.2", BI
);
49 auto *P3
= PHINode::Create(Int32Ty
, /*NumReservedValues*/ 3, "phi.3", BI
);
51 // Some non-PHI nodes.
52 auto *Sum
= BinaryOperator::CreateAdd(P1
, P2
, "sum", BI
);
54 // Now wire up the incoming values that are interesting.
55 P1
->addIncoming(P2
, BB
.get());
56 P2
->addIncoming(P1
, BB
.get());
57 P3
->addIncoming(Sum
, BB
.get());
59 // Finally, let's iterate them, which is the thing we're trying to test.
60 // We'll use this to wire up the rest of the incoming values.
61 for (auto &PN
: BB
->phis()) {
62 PN
.addIncoming(UndefValue::get(Int32Ty
), BB1
.get());
63 PN
.addIncoming(UndefValue::get(Int32Ty
), BB2
.get());
66 // Test that we can use const iterators and generally that the iterators
67 // behave like iterators.
68 BasicBlock::const_phi_iterator CI
;
69 CI
= BB
->phis().begin();
70 EXPECT_NE(CI
, BB
->phis().end());
72 // Test that filtering iterators work with basic blocks.
73 auto isPhi
= [](Instruction
&I
) { return isa
<PHINode
>(&I
); };
74 auto Phis
= make_filter_range(*BB
, isPhi
);
75 auto ReversedPhis
= reverse(make_filter_range(*BB
, isPhi
));
76 EXPECT_EQ(std::distance(Phis
.begin(), Phis
.end()), 3);
77 EXPECT_EQ(&*Phis
.begin(), P1
);
78 EXPECT_EQ(std::distance(ReversedPhis
.begin(), ReversedPhis
.end()), 3);
79 EXPECT_EQ(&*ReversedPhis
.begin(), P3
);
81 // And iterate a const range.
82 for (const auto &PN
: const_cast<const BasicBlock
*>(BB
.get())->phis()) {
83 EXPECT_EQ(BB
.get(), PN
.getIncomingBlock(0));
84 EXPECT_EQ(BB1
.get(), PN
.getIncomingBlock(1));
85 EXPECT_EQ(BB2
.get(), PN
.getIncomingBlock(2));
89 #define CHECK_ITERATORS(Range1, Range2) \
90 EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), \
91 std::distance(Range2.begin(), Range2.end())); \
92 for (auto Pair : zip(Range1, Range2)) \
93 EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair));
95 TEST(BasicBlockTest
, TestInstructionsWithoutDebug
) {
98 Module
*M
= new Module("MyModule", Ctx
);
99 Type
*ArgTy1
[] = {Type::getInt32PtrTy(Ctx
)};
100 FunctionType
*FT
= FunctionType::get(Type::getVoidTy(Ctx
), ArgTy1
, false);
101 Argument
*V
= new Argument(Type::getInt32Ty(Ctx
));
102 Function
*F
= Function::Create(FT
, Function::ExternalLinkage
, "", M
);
104 Value
*DbgAddr
= Intrinsic::getDeclaration(M
, Intrinsic::dbg_addr
);
106 Intrinsic::getDeclaration(M
, Intrinsic::dbg_declare
);
107 Value
*DbgValue
= Intrinsic::getDeclaration(M
, Intrinsic::dbg_value
);
108 Value
*DIV
= MetadataAsValue::get(Ctx
, (Metadata
*)nullptr);
109 SmallVector
<Value
*, 3> Args
= {DIV
, DIV
, DIV
};
111 BasicBlock
*BB1
= BasicBlock::Create(Ctx
, "", F
);
112 const BasicBlock
*BBConst
= BB1
;
113 IRBuilder
<> Builder1(BB1
);
115 AllocaInst
*Var
= Builder1
.CreateAlloca(Builder1
.getInt8Ty());
116 Builder1
.CreateCall(DbgValue
, Args
);
117 Instruction
*AddInst
= cast
<Instruction
>(Builder1
.CreateAdd(V
, V
));
118 Instruction
*MulInst
= cast
<Instruction
>(Builder1
.CreateMul(AddInst
, V
));
119 Builder1
.CreateCall(DbgDeclare
, Args
);
120 Instruction
*SubInst
= cast
<Instruction
>(Builder1
.CreateSub(MulInst
, V
));
121 Builder1
.CreateCall(DbgAddr
, Args
);
123 SmallVector
<Instruction
*, 4> Exp
= {Var
, AddInst
, MulInst
, SubInst
};
124 CHECK_ITERATORS(BB1
->instructionsWithoutDebug(), Exp
);
125 CHECK_ITERATORS(BBConst
->instructionsWithoutDebug(), Exp
);
131 } // End anonymous namespace.
132 } // End llvm namespace.