BasicBlockTest.cpp 5.0 KB

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