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