blob: 07ed997f6381a975e2f2917dcf68faadb48f6f41 [file] [log] [blame]
Chandler Carrutha1a0cf02017-05-26 03:10:00 +00001//===- llvm/unittest/IR/BasicBlockTest.cpp - BasicBlock unit tests --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
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"
19#include <memory>
20
21namespace llvm {
22namespace {
23
24TEST(BasicBlockTest, PhiRange) {
25 LLVMContext Context;
26
27 // Create the main block.
28 std::unique_ptr<BasicBlock> BB(BasicBlock::Create(Context));
29
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());
35
Matt Arsenault05983472017-12-29 19:25:53 +000036 // Make sure this doesn't crash if there are no phis.
37 for (auto &PN : BB->phis()) {
38 (void)PN;
39 EXPECT_TRUE(false) << "empty block should have no phis";
40 }
41
Chandler Carrutha1a0cf02017-05-26 03:10:00 +000042 // Make it a cycle.
43 auto *BI = BranchInst::Create(BB.get(), BB.get());
44
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);
50
51 // Some non-PHI nodes.
52 auto *Sum = BinaryOperator::CreateAdd(P1, P2, "sum", BI);
53
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());
58
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());
64 }
65
66 // Test that we can use const iterators and generally that the iterators
67 // behave like iterators.
68 BasicBlock::const_phi_iterator CI;
Daniel Jasper0559b4f2017-05-26 12:07:12 +000069 CI = BB->phis().begin();
Chandler Carrutha1a0cf02017-05-26 03:10:00 +000070 EXPECT_NE(CI, BB->phis().end());
71
Vedant Kumarbf540e52018-04-25 21:50:09 +000072 // 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));
Vedant Kumarca6a4d92018-05-16 23:20:42 +000076 EXPECT_EQ(std::distance(Phis.begin(), Phis.end()), 3);
Vedant Kumarbf540e52018-04-25 21:50:09 +000077 EXPECT_EQ(&*Phis.begin(), P1);
Vedant Kumarca6a4d92018-05-16 23:20:42 +000078 EXPECT_EQ(std::distance(ReversedPhis.begin(), ReversedPhis.end()), 3);
Vedant Kumarbf540e52018-04-25 21:50:09 +000079 EXPECT_EQ(&*ReversedPhis.begin(), P3);
80
Chandler Carrutha1a0cf02017-05-26 03:10:00 +000081 // 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));
86 }
87}
88
Florian Hahn848303c2018-04-19 09:48:07 +000089#define CHECK_ITERATORS(Range1, Range2) \
Vedant Kumarca6a4d92018-05-16 23:20:42 +000090 EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), \
91 std::distance(Range2.begin(), Range2.end())); \
Florian Hahn848303c2018-04-19 09:48:07 +000092 for (auto Pair : zip(Range1, Range2)) \
93 EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair));
94
Florian Hahn6881b022018-04-19 12:06:26 +000095TEST(BasicBlockTest, TestInstructionsWithoutDebug) {
Florian Hahn848303c2018-04-19 09:48:07 +000096 LLVMContext Ctx;
97
Florian Hahn6881b022018-04-19 12:06:26 +000098 Module *M = new Module("MyModule", Ctx);
Florian Hahn848303c2018-04-19 09:48:07 +000099 Type *ArgTy1[] = {Type::getInt32PtrTy(Ctx)};
100 FunctionType *FT = FunctionType::get(Type::getVoidTy(Ctx), ArgTy1, false);
Florian Hahn6881b022018-04-19 12:06:26 +0000101 Argument *V = new Argument(Type::getInt32Ty(Ctx));
102 Function *F = Function::Create(FT, Function::ExternalLinkage, "", M);
Florian Hahn848303c2018-04-19 09:48:07 +0000103
Florian Hahn6881b022018-04-19 12:06:26 +0000104 Value *DbgAddr = Intrinsic::getDeclaration(M, Intrinsic::dbg_addr);
Florian Hahn848303c2018-04-19 09:48:07 +0000105 Value *DbgDeclare =
Florian Hahn6881b022018-04-19 12:06:26 +0000106 Intrinsic::getDeclaration(M, Intrinsic::dbg_declare);
107 Value *DbgValue = Intrinsic::getDeclaration(M, Intrinsic::dbg_value);
Florian Hahn848303c2018-04-19 09:48:07 +0000108 Value *DIV = MetadataAsValue::get(Ctx, (Metadata *)nullptr);
109 SmallVector<Value *, 3> Args = {DIV, DIV, DIV};
110
111 BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F);
112 const BasicBlock *BBConst = BB1;
113 IRBuilder<> Builder1(BB1);
114
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);
122
123 SmallVector<Instruction *, 4> Exp = {Var, AddInst, MulInst, SubInst};
124 CHECK_ITERATORS(BB1->instructionsWithoutDebug(), Exp);
125 CHECK_ITERATORS(BBConst->instructionsWithoutDebug(), Exp);
Florian Hahn6881b022018-04-19 12:06:26 +0000126
127 delete M;
128 delete V;
Florian Hahn848303c2018-04-19 09:48:07 +0000129}
130
Chandler Carrutha1a0cf02017-05-26 03:10:00 +0000131} // End anonymous namespace.
132} // End llvm namespace.