Mercurial > hg > CbC > CbC_llvm
diff 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 |
line wrap: on
line diff
--- a/unittests/IR/BasicBlockTest.cpp Sun Dec 23 19:23:36 2018 +0900 +++ b/unittests/IR/BasicBlockTest.cpp Wed Aug 14 19:46:37 2019 +0900 @@ -1,9 +1,8 @@ //===- llvm/unittest/IR/BasicBlockTest.cpp - BasicBlock unit tests --------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -69,6 +68,15 @@ CI = BB->phis().begin(); EXPECT_NE(CI, BB->phis().end()); + // Test that filtering iterators work with basic blocks. + auto isPhi = [](Instruction &I) { return isa<PHINode>(&I); }; + auto Phis = make_filter_range(*BB, isPhi); + auto ReversedPhis = reverse(make_filter_range(*BB, isPhi)); + EXPECT_EQ(std::distance(Phis.begin(), Phis.end()), 3); + EXPECT_EQ(&*Phis.begin(), P1); + EXPECT_EQ(std::distance(ReversedPhis.begin(), ReversedPhis.end()), 3); + EXPECT_EQ(&*ReversedPhis.begin(), P3); + // And iterate a const range. for (const auto &PN : const_cast<const BasicBlock *>(BB.get())->phis()) { EXPECT_EQ(BB.get(), PN.getIncomingBlock(0)); @@ -77,5 +85,46 @@ } } +#define CHECK_ITERATORS(Range1, Range2) \ + EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), \ + std::distance(Range2.begin(), Range2.end())); \ + for (auto Pair : zip(Range1, Range2)) \ + EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair)); + +TEST(BasicBlockTest, TestInstructionsWithoutDebug) { + LLVMContext Ctx; + + Module *M = new Module("MyModule", Ctx); + Type *ArgTy1[] = {Type::getInt32PtrTy(Ctx)}; + FunctionType *FT = FunctionType::get(Type::getVoidTy(Ctx), ArgTy1, false); + Argument *V = new Argument(Type::getInt32Ty(Ctx)); + Function *F = Function::Create(FT, Function::ExternalLinkage, "", M); + + Function *DbgAddr = Intrinsic::getDeclaration(M, Intrinsic::dbg_addr); + Function *DbgDeclare = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare); + Function *DbgValue = Intrinsic::getDeclaration(M, Intrinsic::dbg_value); + Value *DIV = MetadataAsValue::get(Ctx, (Metadata *)nullptr); + SmallVector<Value *, 3> Args = {DIV, DIV, DIV}; + + BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F); + const BasicBlock *BBConst = BB1; + IRBuilder<> Builder1(BB1); + + AllocaInst *Var = Builder1.CreateAlloca(Builder1.getInt8Ty()); + Builder1.CreateCall(DbgValue, Args); + Instruction *AddInst = cast<Instruction>(Builder1.CreateAdd(V, V)); + Instruction *MulInst = cast<Instruction>(Builder1.CreateMul(AddInst, V)); + Builder1.CreateCall(DbgDeclare, Args); + Instruction *SubInst = cast<Instruction>(Builder1.CreateSub(MulInst, V)); + Builder1.CreateCall(DbgAddr, Args); + + SmallVector<Instruction *, 4> Exp = {Var, AddInst, MulInst, SubInst}; + CHECK_ITERATORS(BB1->instructionsWithoutDebug(), Exp); + CHECK_ITERATORS(BBConst->instructionsWithoutDebug(), Exp); + + delete M; + delete V; +} + } // End anonymous namespace. } // End llvm namespace.