comparison unittests/IR/BasicBlockTest.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //===- llvm/unittest/IR/BasicBlockTest.cpp - BasicBlock unit tests --------===// 1 //===- llvm/unittest/IR/BasicBlockTest.cpp - BasicBlock unit tests --------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 8
10 #include "llvm/IR/BasicBlock.h" 9 #include "llvm/IR/BasicBlock.h"
11 #include "llvm/ADT/STLExtras.h" 10 #include "llvm/ADT/STLExtras.h"
67 // behave like iterators. 66 // behave like iterators.
68 BasicBlock::const_phi_iterator CI; 67 BasicBlock::const_phi_iterator CI;
69 CI = BB->phis().begin(); 68 CI = BB->phis().begin();
70 EXPECT_NE(CI, BB->phis().end()); 69 EXPECT_NE(CI, BB->phis().end());
71 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
72 // And iterate a const range. 80 // And iterate a const range.
73 for (const auto &PN : const_cast<const BasicBlock *>(BB.get())->phis()) { 81 for (const auto &PN : const_cast<const BasicBlock *>(BB.get())->phis()) {
74 EXPECT_EQ(BB.get(), PN.getIncomingBlock(0)); 82 EXPECT_EQ(BB.get(), PN.getIncomingBlock(0));
75 EXPECT_EQ(BB1.get(), PN.getIncomingBlock(1)); 83 EXPECT_EQ(BB1.get(), PN.getIncomingBlock(1));
76 EXPECT_EQ(BB2.get(), PN.getIncomingBlock(2)); 84 EXPECT_EQ(BB2.get(), PN.getIncomingBlock(2));
77 } 85 }
78 } 86 }
79 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
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 delete M;
126 delete V;
127 }
128
80 } // End anonymous namespace. 129 } // End anonymous namespace.
81 } // End llvm namespace. 130 } // End llvm namespace.