reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
//===- MachineInstrBundleIteratorTest.cpp ---------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/MachineInstrBundleIterator.h"
#include "llvm/ADT/ilist_node.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

struct MyBundledInstr
    : public ilist_node<MyBundledInstr, ilist_sentinel_tracking<true>> {
  bool isBundledWithPred() const { return true; }
  bool isBundledWithSucc() const { return true; }
};
typedef MachineInstrBundleIterator<MyBundledInstr> bundled_iterator;
typedef MachineInstrBundleIterator<const MyBundledInstr> const_bundled_iterator;
typedef MachineInstrBundleIterator<MyBundledInstr, true>
    reverse_bundled_iterator;
typedef MachineInstrBundleIterator<const MyBundledInstr, true>
    const_reverse_bundled_iterator;

#ifdef GTEST_HAS_DEATH_TEST
#ifndef NDEBUG
TEST(MachineInstrBundleIteratorTest, CheckForBundles) {
  MyBundledInstr MBI;
  auto I = MBI.getIterator();
  auto RI = I.getReverse();

  // Confirm that MBI is always considered bundled.
  EXPECT_TRUE(MBI.isBundledWithPred());
  EXPECT_TRUE(MBI.isBundledWithSucc());

  // Confirm that iterators check in their constructor for bundled iterators.
  EXPECT_DEATH((void)static_cast<bundled_iterator>(I),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<bundled_iterator>(MBI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<bundled_iterator>(&MBI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<const_bundled_iterator>(I),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<const_bundled_iterator>(MBI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<const_bundled_iterator>(&MBI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(RI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(MBI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(&MBI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(RI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(MBI),
               "not legal to initialize");
  EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(&MBI),
               "not legal to initialize");
}
#endif
#endif

TEST(MachineInstrBundleIteratorTest, CompareToBundledMI) {
  MyBundledInstr MBI;
  const MyBundledInstr &CMBI = MBI;
  bundled_iterator I;
  const_bundled_iterator CI;

  // Confirm that MBI is always considered bundled.
  EXPECT_TRUE(MBI.isBundledWithPred());
  EXPECT_TRUE(MBI.isBundledWithSucc());

  // These invocations will crash when !NDEBUG if a conversion is taking place.
  // These checks confirm that comparison operators don't use any conversion
  // operators.
  ASSERT_FALSE(MBI == I);
  ASSERT_FALSE(&MBI == I);
  ASSERT_FALSE(CMBI == I);
  ASSERT_FALSE(&CMBI == I);
  ASSERT_FALSE(I == MBI);
  ASSERT_FALSE(I == &MBI);
  ASSERT_FALSE(I == CMBI);
  ASSERT_FALSE(I == &CMBI);
  ASSERT_FALSE(MBI == CI);
  ASSERT_FALSE(&MBI == CI);
  ASSERT_FALSE(CMBI == CI);
  ASSERT_FALSE(&CMBI == CI);
  ASSERT_FALSE(CI == MBI);
  ASSERT_FALSE(CI == &MBI);
  ASSERT_FALSE(CI == CMBI);
  ASSERT_FALSE(CI == &CMBI);
  ASSERT_FALSE(MBI.getIterator() == I);
  ASSERT_FALSE(CMBI.getIterator() == I);
  ASSERT_FALSE(I == MBI.getIterator());
  ASSERT_FALSE(I == CMBI.getIterator());
  ASSERT_FALSE(MBI.getIterator() == CI);
  ASSERT_FALSE(CMBI.getIterator() == CI);
  ASSERT_FALSE(CI == MBI.getIterator());
  ASSERT_FALSE(CI == CMBI.getIterator());
  ASSERT_TRUE(MBI != I);
  ASSERT_TRUE(&MBI != I);
  ASSERT_TRUE(CMBI != I);
  ASSERT_TRUE(&CMBI != I);
  ASSERT_TRUE(I != MBI);
  ASSERT_TRUE(I != &MBI);
  ASSERT_TRUE(I != CMBI);
  ASSERT_TRUE(I != &CMBI);
  ASSERT_TRUE(MBI != CI);
  ASSERT_TRUE(&MBI != CI);
  ASSERT_TRUE(CMBI != CI);
  ASSERT_TRUE(&CMBI != CI);
  ASSERT_TRUE(CI != MBI);
  ASSERT_TRUE(CI != &MBI);
  ASSERT_TRUE(CI != CMBI);
  ASSERT_TRUE(CI != &CMBI);
  ASSERT_TRUE(MBI.getIterator() != I);
  ASSERT_TRUE(CMBI.getIterator() != I);
  ASSERT_TRUE(I != MBI.getIterator());
  ASSERT_TRUE(I != CMBI.getIterator());
  ASSERT_TRUE(MBI.getIterator() != CI);
  ASSERT_TRUE(CMBI.getIterator() != CI);
  ASSERT_TRUE(CI != MBI.getIterator());
  ASSERT_TRUE(CI != CMBI.getIterator());
}

struct MyUnbundledInstr
    : ilist_node<MyUnbundledInstr, ilist_sentinel_tracking<true>> {
  bool isBundledWithPred() const { return false; }
  bool isBundledWithSucc() const { return false; }
};
typedef MachineInstrBundleIterator<MyUnbundledInstr> unbundled_iterator;
typedef MachineInstrBundleIterator<const MyUnbundledInstr>
    const_unbundled_iterator;
typedef MachineInstrBundleIterator<MyUnbundledInstr, true>
    reverse_unbundled_iterator;
typedef MachineInstrBundleIterator<const MyUnbundledInstr, true>
    const_reverse_unbundled_iterator;

TEST(MachineInstrBundleIteratorTest, ReverseConstructor) {
  simple_ilist<MyUnbundledInstr, ilist_sentinel_tracking<true>> L;
  const auto &CL = L;
  MyUnbundledInstr A, B;
  L.insert(L.end(), A);
  L.insert(L.end(), B);

  // Save typing.
  typedef MachineInstrBundleIterator<MyUnbundledInstr> iterator;
  typedef MachineInstrBundleIterator<MyUnbundledInstr, true> reverse_iterator;
  typedef MachineInstrBundleIterator<const MyUnbundledInstr> const_iterator;
  typedef MachineInstrBundleIterator<const MyUnbundledInstr, true>
      const_reverse_iterator;

  // Convert to bundle iterators.
  auto begin = [&]() -> iterator { return L.begin(); };
  auto end = [&]() -> iterator { return L.end(); };
  auto rbegin = [&]() -> reverse_iterator { return L.rbegin(); };
  auto rend = [&]() -> reverse_iterator { return L.rend(); };
  auto cbegin = [&]() -> const_iterator { return CL.begin(); };
  auto cend = [&]() -> const_iterator { return CL.end(); };
  auto crbegin = [&]() -> const_reverse_iterator { return CL.rbegin(); };
  auto crend = [&]() -> const_reverse_iterator { return CL.rend(); };

  // Check conversion values.
  EXPECT_EQ(begin(), iterator(rend()));
  EXPECT_EQ(++begin(), iterator(++rbegin()));
  EXPECT_EQ(end(), iterator(rbegin()));
  EXPECT_EQ(rbegin(), reverse_iterator(end()));
  EXPECT_EQ(++rbegin(), reverse_iterator(++begin()));
  EXPECT_EQ(rend(), reverse_iterator(begin()));

  // Check const iterator constructors.
  EXPECT_EQ(cbegin(), const_iterator(rend()));
  EXPECT_EQ(cbegin(), const_iterator(crend()));
  EXPECT_EQ(crbegin(), const_reverse_iterator(end()));
  EXPECT_EQ(crbegin(), const_reverse_iterator(cend()));

  // Confirm lack of implicit conversions.
  static_assert(!std::is_convertible<iterator, reverse_iterator>::value,
                "unexpected implicit conversion");
  static_assert(!std::is_convertible<reverse_iterator, iterator>::value,
                "unexpected implicit conversion");
  static_assert(
      !std::is_convertible<const_iterator, const_reverse_iterator>::value,
      "unexpected implicit conversion");
  static_assert(
      !std::is_convertible<const_reverse_iterator, const_iterator>::value,
      "unexpected implicit conversion");
}

} // end namespace