1f197b1f7SDuncan P. N. Exon Smith //===- MachineInstrBundleIteratorTest.cpp ---------------------------------===//
2f197b1f7SDuncan P. N. Exon Smith //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f197b1f7SDuncan P. N. Exon Smith //
7f197b1f7SDuncan P. N. Exon Smith //===----------------------------------------------------------------------===//
8f197b1f7SDuncan P. N. Exon Smith
9f197b1f7SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineInstrBundleIterator.h"
109a67b073SChandler Carruth #include "llvm/ADT/ilist_node.h"
11f197b1f7SDuncan P. N. Exon Smith #include "gtest/gtest.h"
12f197b1f7SDuncan P. N. Exon Smith
13f197b1f7SDuncan P. N. Exon Smith using namespace llvm;
14f197b1f7SDuncan P. N. Exon Smith
15f197b1f7SDuncan P. N. Exon Smith namespace {
16f197b1f7SDuncan P. N. Exon Smith
17cc9edaceSDuncan P. N. Exon Smith struct MyBundledInstr
18cc9edaceSDuncan P. N. Exon Smith : public ilist_node<MyBundledInstr, ilist_sentinel_tracking<true>> {
isBundledWithPred__anon9c45e14c0111::MyBundledInstr19f197b1f7SDuncan P. N. Exon Smith bool isBundledWithPred() const { return true; }
isBundledWithSucc__anon9c45e14c0111::MyBundledInstr20f197b1f7SDuncan P. N. Exon Smith bool isBundledWithSucc() const { return true; }
21f197b1f7SDuncan P. N. Exon Smith };
22f197b1f7SDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyBundledInstr> bundled_iterator;
23f197b1f7SDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyBundledInstr> const_bundled_iterator;
241872096fSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyBundledInstr, true>
251872096fSDuncan P. N. Exon Smith reverse_bundled_iterator;
261872096fSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyBundledInstr, true>
271872096fSDuncan P. N. Exon Smith const_reverse_bundled_iterator;
28f197b1f7SDuncan P. N. Exon Smith
29f197b1f7SDuncan P. N. Exon Smith #ifdef GTEST_HAS_DEATH_TEST
30f197b1f7SDuncan P. N. Exon Smith #ifndef NDEBUG
TEST(MachineInstrBundleIteratorTest,CheckForBundles)31f197b1f7SDuncan P. N. Exon Smith TEST(MachineInstrBundleIteratorTest, CheckForBundles) {
32f197b1f7SDuncan P. N. Exon Smith MyBundledInstr MBI;
331872096fSDuncan P. N. Exon Smith auto I = MBI.getIterator();
341872096fSDuncan P. N. Exon Smith auto RI = I.getReverse();
35f197b1f7SDuncan P. N. Exon Smith
36f197b1f7SDuncan P. N. Exon Smith // Confirm that MBI is always considered bundled.
37f197b1f7SDuncan P. N. Exon Smith EXPECT_TRUE(MBI.isBundledWithPred());
38f197b1f7SDuncan P. N. Exon Smith EXPECT_TRUE(MBI.isBundledWithSucc());
39f197b1f7SDuncan P. N. Exon Smith
40f197b1f7SDuncan P. N. Exon Smith // Confirm that iterators check in their constructor for bundled iterators.
411872096fSDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<bundled_iterator>(I),
421872096fSDuncan P. N. Exon Smith "not legal to initialize");
43f197b1f7SDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<bundled_iterator>(MBI),
44f197b1f7SDuncan P. N. Exon Smith "not legal to initialize");
45f197b1f7SDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<bundled_iterator>(&MBI),
46f197b1f7SDuncan P. N. Exon Smith "not legal to initialize");
471872096fSDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<const_bundled_iterator>(I),
481872096fSDuncan P. N. Exon Smith "not legal to initialize");
49f197b1f7SDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<const_bundled_iterator>(MBI),
50f197b1f7SDuncan P. N. Exon Smith "not legal to initialize");
51f197b1f7SDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<const_bundled_iterator>(&MBI),
52f197b1f7SDuncan P. N. Exon Smith "not legal to initialize");
531872096fSDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(RI),
541872096fSDuncan P. N. Exon Smith "not legal to initialize");
551872096fSDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(MBI),
561872096fSDuncan P. N. Exon Smith "not legal to initialize");
571872096fSDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(&MBI),
581872096fSDuncan P. N. Exon Smith "not legal to initialize");
591872096fSDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(RI),
601872096fSDuncan P. N. Exon Smith "not legal to initialize");
611872096fSDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(MBI),
621872096fSDuncan P. N. Exon Smith "not legal to initialize");
631872096fSDuncan P. N. Exon Smith EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(&MBI),
641872096fSDuncan P. N. Exon Smith "not legal to initialize");
65f197b1f7SDuncan P. N. Exon Smith }
66f197b1f7SDuncan P. N. Exon Smith #endif
67f197b1f7SDuncan P. N. Exon Smith #endif
68f197b1f7SDuncan P. N. Exon Smith
TEST(MachineInstrBundleIteratorTest,CompareToBundledMI)69f197b1f7SDuncan P. N. Exon Smith TEST(MachineInstrBundleIteratorTest, CompareToBundledMI) {
70f197b1f7SDuncan P. N. Exon Smith MyBundledInstr MBI;
71f197b1f7SDuncan P. N. Exon Smith const MyBundledInstr &CMBI = MBI;
72f197b1f7SDuncan P. N. Exon Smith bundled_iterator I;
73f197b1f7SDuncan P. N. Exon Smith const_bundled_iterator CI;
74f197b1f7SDuncan P. N. Exon Smith
75f197b1f7SDuncan P. N. Exon Smith // Confirm that MBI is always considered bundled.
76f197b1f7SDuncan P. N. Exon Smith EXPECT_TRUE(MBI.isBundledWithPred());
77f197b1f7SDuncan P. N. Exon Smith EXPECT_TRUE(MBI.isBundledWithSucc());
78f197b1f7SDuncan P. N. Exon Smith
79f197b1f7SDuncan P. N. Exon Smith // These invocations will crash when !NDEBUG if a conversion is taking place.
80f197b1f7SDuncan P. N. Exon Smith // These checks confirm that comparison operators don't use any conversion
81f197b1f7SDuncan P. N. Exon Smith // operators.
82f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(MBI == I);
83f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(&MBI == I);
84f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(CMBI == I);
85f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(&CMBI == I);
86f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(I == MBI);
87f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(I == &MBI);
88f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(I == CMBI);
89f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(I == &CMBI);
90f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(MBI == CI);
91f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(&MBI == CI);
92f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(CMBI == CI);
93f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(&CMBI == CI);
94f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(CI == MBI);
95f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(CI == &MBI);
96f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(CI == CMBI);
97f197b1f7SDuncan P. N. Exon Smith ASSERT_FALSE(CI == &CMBI);
983b22b181SDuncan P. N. Exon Smith ASSERT_FALSE(MBI.getIterator() == I);
993b22b181SDuncan P. N. Exon Smith ASSERT_FALSE(CMBI.getIterator() == I);
1003b22b181SDuncan P. N. Exon Smith ASSERT_FALSE(I == MBI.getIterator());
1013b22b181SDuncan P. N. Exon Smith ASSERT_FALSE(I == CMBI.getIterator());
1023b22b181SDuncan P. N. Exon Smith ASSERT_FALSE(MBI.getIterator() == CI);
1033b22b181SDuncan P. N. Exon Smith ASSERT_FALSE(CMBI.getIterator() == CI);
1043b22b181SDuncan P. N. Exon Smith ASSERT_FALSE(CI == MBI.getIterator());
1053b22b181SDuncan P. N. Exon Smith ASSERT_FALSE(CI == CMBI.getIterator());
106f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(MBI != I);
107f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(&MBI != I);
108f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(CMBI != I);
109f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(&CMBI != I);
110f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(I != MBI);
111f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(I != &MBI);
112f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(I != CMBI);
113f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(I != &CMBI);
114f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(MBI != CI);
115f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(&MBI != CI);
116f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(CMBI != CI);
117f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(&CMBI != CI);
118f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(CI != MBI);
119f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(CI != &MBI);
120f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(CI != CMBI);
121f197b1f7SDuncan P. N. Exon Smith ASSERT_TRUE(CI != &CMBI);
1223b22b181SDuncan P. N. Exon Smith ASSERT_TRUE(MBI.getIterator() != I);
1233b22b181SDuncan P. N. Exon Smith ASSERT_TRUE(CMBI.getIterator() != I);
1243b22b181SDuncan P. N. Exon Smith ASSERT_TRUE(I != MBI.getIterator());
1253b22b181SDuncan P. N. Exon Smith ASSERT_TRUE(I != CMBI.getIterator());
1263b22b181SDuncan P. N. Exon Smith ASSERT_TRUE(MBI.getIterator() != CI);
1273b22b181SDuncan P. N. Exon Smith ASSERT_TRUE(CMBI.getIterator() != CI);
1283b22b181SDuncan P. N. Exon Smith ASSERT_TRUE(CI != MBI.getIterator());
1293b22b181SDuncan P. N. Exon Smith ASSERT_TRUE(CI != CMBI.getIterator());
130f197b1f7SDuncan P. N. Exon Smith }
131f197b1f7SDuncan P. N. Exon Smith
132fcd69daaSDuncan P. N. Exon Smith struct MyUnbundledInstr
133fcd69daaSDuncan P. N. Exon Smith : ilist_node<MyUnbundledInstr, ilist_sentinel_tracking<true>> {
isBundledWithPred__anon9c45e14c0111::MyUnbundledInstr134fcd69daaSDuncan P. N. Exon Smith bool isBundledWithPred() const { return false; }
isBundledWithSucc__anon9c45e14c0111::MyUnbundledInstr135fcd69daaSDuncan P. N. Exon Smith bool isBundledWithSucc() const { return false; }
136fcd69daaSDuncan P. N. Exon Smith };
137fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyUnbundledInstr> unbundled_iterator;
138fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyUnbundledInstr>
139fcd69daaSDuncan P. N. Exon Smith const_unbundled_iterator;
140fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyUnbundledInstr, true>
141fcd69daaSDuncan P. N. Exon Smith reverse_unbundled_iterator;
142fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyUnbundledInstr, true>
143fcd69daaSDuncan P. N. Exon Smith const_reverse_unbundled_iterator;
144fcd69daaSDuncan P. N. Exon Smith
TEST(MachineInstrBundleIteratorTest,ReverseConstructor)145fcd69daaSDuncan P. N. Exon Smith TEST(MachineInstrBundleIteratorTest, ReverseConstructor) {
146fcd69daaSDuncan P. N. Exon Smith simple_ilist<MyUnbundledInstr, ilist_sentinel_tracking<true>> L;
147fcd69daaSDuncan P. N. Exon Smith const auto &CL = L;
148fcd69daaSDuncan P. N. Exon Smith MyUnbundledInstr A, B;
149fcd69daaSDuncan P. N. Exon Smith L.insert(L.end(), A);
150fcd69daaSDuncan P. N. Exon Smith L.insert(L.end(), B);
151fcd69daaSDuncan P. N. Exon Smith
152fcd69daaSDuncan P. N. Exon Smith // Save typing.
153fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyUnbundledInstr> iterator;
154fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyUnbundledInstr, true> reverse_iterator;
155fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyUnbundledInstr> const_iterator;
156fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyUnbundledInstr, true>
157fcd69daaSDuncan P. N. Exon Smith const_reverse_iterator;
158fcd69daaSDuncan P. N. Exon Smith
159fcd69daaSDuncan P. N. Exon Smith // Convert to bundle iterators.
160fcd69daaSDuncan P. N. Exon Smith auto begin = [&]() -> iterator { return L.begin(); };
161fcd69daaSDuncan P. N. Exon Smith auto end = [&]() -> iterator { return L.end(); };
162fcd69daaSDuncan P. N. Exon Smith auto rbegin = [&]() -> reverse_iterator { return L.rbegin(); };
163fcd69daaSDuncan P. N. Exon Smith auto rend = [&]() -> reverse_iterator { return L.rend(); };
164fcd69daaSDuncan P. N. Exon Smith auto cbegin = [&]() -> const_iterator { return CL.begin(); };
165fcd69daaSDuncan P. N. Exon Smith auto cend = [&]() -> const_iterator { return CL.end(); };
166fcd69daaSDuncan P. N. Exon Smith auto crbegin = [&]() -> const_reverse_iterator { return CL.rbegin(); };
167fcd69daaSDuncan P. N. Exon Smith auto crend = [&]() -> const_reverse_iterator { return CL.rend(); };
168fcd69daaSDuncan P. N. Exon Smith
169fcd69daaSDuncan P. N. Exon Smith // Check conversion values.
170fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(begin(), iterator(rend()));
171fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(++begin(), iterator(++rbegin()));
172fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(end(), iterator(rbegin()));
173fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(rbegin(), reverse_iterator(end()));
174fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(++rbegin(), reverse_iterator(++begin()));
175fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(rend(), reverse_iterator(begin()));
176fcd69daaSDuncan P. N. Exon Smith
177fcd69daaSDuncan P. N. Exon Smith // Check const iterator constructors.
178fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(cbegin(), const_iterator(rend()));
179fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(cbegin(), const_iterator(crend()));
180fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(crbegin(), const_reverse_iterator(end()));
181fcd69daaSDuncan P. N. Exon Smith EXPECT_EQ(crbegin(), const_reverse_iterator(cend()));
182fcd69daaSDuncan P. N. Exon Smith
183fcd69daaSDuncan P. N. Exon Smith // Confirm lack of implicit conversions.
184*6aa050a6SNathan James static_assert(!std::is_convertible_v<iterator, reverse_iterator>,
185fcd69daaSDuncan P. N. Exon Smith "unexpected implicit conversion");
186*6aa050a6SNathan James static_assert(!std::is_convertible_v<reverse_iterator, iterator>,
187fcd69daaSDuncan P. N. Exon Smith "unexpected implicit conversion");
188*6aa050a6SNathan James static_assert(!std::is_convertible_v<const_iterator, const_reverse_iterator>,
189fcd69daaSDuncan P. N. Exon Smith "unexpected implicit conversion");
190*6aa050a6SNathan James static_assert(!std::is_convertible_v<const_reverse_iterator, const_iterator>,
191fcd69daaSDuncan P. N. Exon Smith "unexpected implicit conversion");
192fcd69daaSDuncan P. N. Exon Smith }
193fcd69daaSDuncan P. N. Exon Smith
194f197b1f7SDuncan P. N. Exon Smith } // end namespace
195