1 //===- 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 21 namespace llvm { 22 namespace { 23 24 TEST(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 36 // 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 42 // 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; 69 CI = BB->phis().begin(); 70 EXPECT_NE(CI, BB->phis().end()); 71 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); 80 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)); 86 } 87 } 88 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)); 94 95 TEST(BasicBlockTest, TestInstructionsWithoutDebug) { 96 LLVMContext Ctx; 97 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); 103 104 Value *DbgAddr = Intrinsic::getDeclaration(M, Intrinsic::dbg_addr); 105 Value *DbgDeclare = 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}; 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); 126 127 delete M; 128 delete V; 129 } 130 131 } // End anonymous namespace. 132 } // End llvm namespace. 133