BasicBlockTest.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include "llvm/IR/BasicBlock.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/IR/Function.h"
  12. #include "llvm/IR/IRBuilder.h"
  13. #include "llvm/IR/LLVMContext.h"
  14. #include "llvm/IR/Module.h"
  15. #include "llvm/IR/NoFolder.h"
  16. #include "gmock/gmock-matchers.h"
  17. #include "gtest/gtest.h"
  18. #include <memory>
  19. namespace llvm {
  20. namespace {
  21. TEST(BasicBlockTest, PhiRange) {
  22. LLVMContext Context;
  23. // Create the main block.
  24. std::unique_ptr<BasicBlock> BB(BasicBlock::Create(Context));
  25. // Create some predecessors of it.
  26. std::unique_ptr<BasicBlock> BB1(BasicBlock::Create(Context));
  27. BranchInst::Create(BB.get(), BB1.get());
  28. std::unique_ptr<BasicBlock> BB2(BasicBlock::Create(Context));
  29. BranchInst::Create(BB.get(), BB2.get());
  30. // Make it a cycle.
  31. auto *BI = BranchInst::Create(BB.get(), BB.get());
  32. // Now insert some PHI nodes.
  33. auto *Int32Ty = Type::getInt32Ty(Context);
  34. auto *P1 = PHINode::Create(Int32Ty, /*NumReservedValues*/ 3, "phi.1", BI);
  35. auto *P2 = PHINode::Create(Int32Ty, /*NumReservedValues*/ 3, "phi.2", BI);
  36. auto *P3 = PHINode::Create(Int32Ty, /*NumReservedValues*/ 3, "phi.3", BI);
  37. // Some non-PHI nodes.
  38. auto *Sum = BinaryOperator::CreateAdd(P1, P2, "sum", BI);
  39. // Now wire up the incoming values that are interesting.
  40. P1->addIncoming(P2, BB.get());
  41. P2->addIncoming(P1, BB.get());
  42. P3->addIncoming(Sum, BB.get());
  43. // Finally, let's iterate them, which is the thing we're trying to test.
  44. // We'll use this to wire up the rest of the incoming values.
  45. for (auto &PN : BB->phis()) {
  46. PN.addIncoming(UndefValue::get(Int32Ty), BB1.get());
  47. PN.addIncoming(UndefValue::get(Int32Ty), BB2.get());
  48. }
  49. // Test that we can use const iterators and generally that the iterators
  50. // behave like iterators.
  51. BasicBlock::const_phi_iterator CI;
  52. CI = BB->phis().begin();
  53. EXPECT_NE(CI, BB->phis().end());
  54. // And iterate a const range.
  55. for (const auto &PN : const_cast<const BasicBlock *>(BB.get())->phis()) {
  56. EXPECT_EQ(BB.get(), PN.getIncomingBlock(0));
  57. EXPECT_EQ(BB1.get(), PN.getIncomingBlock(1));
  58. EXPECT_EQ(BB2.get(), PN.getIncomingBlock(2));
  59. }
  60. }
  61. } // End anonymous namespace.
  62. } // End llvm namespace.