1f197b1f7SDuncan P. N. Exon Smith //===- MachineInstrBundleIteratorTest.cpp ---------------------------------===//
2f197b1f7SDuncan P. N. Exon Smith //
3f197b1f7SDuncan P. N. Exon Smith //                     The LLVM Compiler Infrastructure
4f197b1f7SDuncan P. N. Exon Smith //
5f197b1f7SDuncan P. N. Exon Smith // This file is distributed under the University of Illinois Open Source
6f197b1f7SDuncan P. N. Exon Smith // License. See LICENSE.TXT for details.
7f197b1f7SDuncan P. N. Exon Smith //
8f197b1f7SDuncan P. N. Exon Smith //===----------------------------------------------------------------------===//
9f197b1f7SDuncan P. N. Exon Smith 
10f197b1f7SDuncan P. N. Exon Smith #include "llvm/ADT/ilist_node.h"
11f197b1f7SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineInstrBundleIterator.h"
12f197b1f7SDuncan P. N. Exon Smith #include "gtest/gtest.h"
13f197b1f7SDuncan P. N. Exon Smith 
14f197b1f7SDuncan P. N. Exon Smith using namespace llvm;
15f197b1f7SDuncan P. N. Exon Smith 
16f197b1f7SDuncan P. N. Exon Smith namespace {
17f197b1f7SDuncan P. N. Exon Smith 
18cc9edaceSDuncan P. N. Exon Smith struct MyBundledInstr
19cc9edaceSDuncan P. N. Exon Smith     : public ilist_node<MyBundledInstr, ilist_sentinel_tracking<true>> {
20f197b1f7SDuncan P. N. Exon Smith   bool isBundledWithPred() const { return true; }
21f197b1f7SDuncan P. N. Exon Smith   bool isBundledWithSucc() const { return true; }
22f197b1f7SDuncan P. N. Exon Smith };
23f197b1f7SDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyBundledInstr> bundled_iterator;
24f197b1f7SDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyBundledInstr> const_bundled_iterator;
251872096fSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyBundledInstr, true>
261872096fSDuncan P. N. Exon Smith     reverse_bundled_iterator;
271872096fSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyBundledInstr, true>
281872096fSDuncan P. N. Exon Smith     const_reverse_bundled_iterator;
29f197b1f7SDuncan P. N. Exon Smith 
30f197b1f7SDuncan P. N. Exon Smith #ifdef GTEST_HAS_DEATH_TEST
31f197b1f7SDuncan P. N. Exon Smith #ifndef NDEBUG
32f197b1f7SDuncan P. N. Exon Smith TEST(MachineInstrBundleIteratorTest, CheckForBundles) {
33f197b1f7SDuncan P. N. Exon Smith   MyBundledInstr MBI;
341872096fSDuncan P. N. Exon Smith   auto I = MBI.getIterator();
351872096fSDuncan P. N. Exon Smith   auto RI = I.getReverse();
36f197b1f7SDuncan P. N. Exon Smith 
37f197b1f7SDuncan P. N. Exon Smith   // Confirm that MBI is always considered bundled.
38f197b1f7SDuncan P. N. Exon Smith   EXPECT_TRUE(MBI.isBundledWithPred());
39f197b1f7SDuncan P. N. Exon Smith   EXPECT_TRUE(MBI.isBundledWithSucc());
40f197b1f7SDuncan P. N. Exon Smith 
41f197b1f7SDuncan P. N. Exon Smith   // Confirm that iterators check in their constructor for bundled iterators.
421872096fSDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<bundled_iterator>(I),
431872096fSDuncan P. N. Exon Smith                "not legal to initialize");
44f197b1f7SDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<bundled_iterator>(MBI),
45f197b1f7SDuncan P. N. Exon Smith                "not legal to initialize");
46f197b1f7SDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<bundled_iterator>(&MBI),
47f197b1f7SDuncan P. N. Exon Smith                "not legal to initialize");
481872096fSDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<const_bundled_iterator>(I),
491872096fSDuncan P. N. Exon Smith                "not legal to initialize");
50f197b1f7SDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<const_bundled_iterator>(MBI),
51f197b1f7SDuncan P. N. Exon Smith                "not legal to initialize");
52f197b1f7SDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<const_bundled_iterator>(&MBI),
53f197b1f7SDuncan P. N. Exon Smith                "not legal to initialize");
541872096fSDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(RI),
551872096fSDuncan P. N. Exon Smith                "not legal to initialize");
561872096fSDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(MBI),
571872096fSDuncan P. N. Exon Smith                "not legal to initialize");
581872096fSDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(&MBI),
591872096fSDuncan P. N. Exon Smith                "not legal to initialize");
601872096fSDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(RI),
611872096fSDuncan P. N. Exon Smith                "not legal to initialize");
621872096fSDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(MBI),
631872096fSDuncan P. N. Exon Smith                "not legal to initialize");
641872096fSDuncan P. N. Exon Smith   EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(&MBI),
651872096fSDuncan P. N. Exon Smith                "not legal to initialize");
66f197b1f7SDuncan P. N. Exon Smith }
67f197b1f7SDuncan P. N. Exon Smith #endif
68f197b1f7SDuncan P. N. Exon Smith #endif
69f197b1f7SDuncan P. N. Exon Smith 
70f197b1f7SDuncan P. N. Exon Smith TEST(MachineInstrBundleIteratorTest, CompareToBundledMI) {
71f197b1f7SDuncan P. N. Exon Smith   MyBundledInstr MBI;
72f197b1f7SDuncan P. N. Exon Smith   const MyBundledInstr &CMBI = MBI;
73f197b1f7SDuncan P. N. Exon Smith   bundled_iterator I;
74f197b1f7SDuncan P. N. Exon Smith   const_bundled_iterator CI;
75f197b1f7SDuncan P. N. Exon Smith 
76f197b1f7SDuncan P. N. Exon Smith   // Confirm that MBI is always considered bundled.
77f197b1f7SDuncan P. N. Exon Smith   EXPECT_TRUE(MBI.isBundledWithPred());
78f197b1f7SDuncan P. N. Exon Smith   EXPECT_TRUE(MBI.isBundledWithSucc());
79f197b1f7SDuncan P. N. Exon Smith 
80f197b1f7SDuncan P. N. Exon Smith   // These invocations will crash when !NDEBUG if a conversion is taking place.
81f197b1f7SDuncan P. N. Exon Smith   // These checks confirm that comparison operators don't use any conversion
82f197b1f7SDuncan P. N. Exon Smith   // operators.
83f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(MBI == I);
84f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(&MBI == I);
85f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(CMBI == I);
86f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(&CMBI == I);
87f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(I == MBI);
88f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(I == &MBI);
89f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(I == CMBI);
90f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(I == &CMBI);
91f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(MBI == CI);
92f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(&MBI == CI);
93f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(CMBI == CI);
94f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(&CMBI == CI);
95f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(CI == MBI);
96f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(CI == &MBI);
97f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(CI == CMBI);
98f197b1f7SDuncan P. N. Exon Smith   ASSERT_FALSE(CI == &CMBI);
993b22b181SDuncan P. N. Exon Smith   ASSERT_FALSE(MBI.getIterator() == I);
1003b22b181SDuncan P. N. Exon Smith   ASSERT_FALSE(CMBI.getIterator() == I);
1013b22b181SDuncan P. N. Exon Smith   ASSERT_FALSE(I == MBI.getIterator());
1023b22b181SDuncan P. N. Exon Smith   ASSERT_FALSE(I == CMBI.getIterator());
1033b22b181SDuncan P. N. Exon Smith   ASSERT_FALSE(MBI.getIterator() == CI);
1043b22b181SDuncan P. N. Exon Smith   ASSERT_FALSE(CMBI.getIterator() == CI);
1053b22b181SDuncan P. N. Exon Smith   ASSERT_FALSE(CI == MBI.getIterator());
1063b22b181SDuncan P. N. Exon Smith   ASSERT_FALSE(CI == CMBI.getIterator());
107f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(MBI != I);
108f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(&MBI != I);
109f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(CMBI != I);
110f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(&CMBI != I);
111f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(I != MBI);
112f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(I != &MBI);
113f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(I != CMBI);
114f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(I != &CMBI);
115f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(MBI != CI);
116f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(&MBI != CI);
117f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(CMBI != CI);
118f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(&CMBI != CI);
119f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(CI != MBI);
120f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(CI != &MBI);
121f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(CI != CMBI);
122f197b1f7SDuncan P. N. Exon Smith   ASSERT_TRUE(CI != &CMBI);
1233b22b181SDuncan P. N. Exon Smith   ASSERT_TRUE(MBI.getIterator() != I);
1243b22b181SDuncan P. N. Exon Smith   ASSERT_TRUE(CMBI.getIterator() != I);
1253b22b181SDuncan P. N. Exon Smith   ASSERT_TRUE(I != MBI.getIterator());
1263b22b181SDuncan P. N. Exon Smith   ASSERT_TRUE(I != CMBI.getIterator());
1273b22b181SDuncan P. N. Exon Smith   ASSERT_TRUE(MBI.getIterator() != CI);
1283b22b181SDuncan P. N. Exon Smith   ASSERT_TRUE(CMBI.getIterator() != CI);
1293b22b181SDuncan P. N. Exon Smith   ASSERT_TRUE(CI != MBI.getIterator());
1303b22b181SDuncan P. N. Exon Smith   ASSERT_TRUE(CI != CMBI.getIterator());
131f197b1f7SDuncan P. N. Exon Smith }
132f197b1f7SDuncan P. N. Exon Smith 
133*fcd69daaSDuncan P. N. Exon Smith struct MyUnbundledInstr
134*fcd69daaSDuncan P. N. Exon Smith     : ilist_node<MyUnbundledInstr, ilist_sentinel_tracking<true>> {
135*fcd69daaSDuncan P. N. Exon Smith   bool isBundledWithPred() const { return false; }
136*fcd69daaSDuncan P. N. Exon Smith   bool isBundledWithSucc() const { return false; }
137*fcd69daaSDuncan P. N. Exon Smith };
138*fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyUnbundledInstr> unbundled_iterator;
139*fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyUnbundledInstr>
140*fcd69daaSDuncan P. N. Exon Smith     const_unbundled_iterator;
141*fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<MyUnbundledInstr, true>
142*fcd69daaSDuncan P. N. Exon Smith     reverse_unbundled_iterator;
143*fcd69daaSDuncan P. N. Exon Smith typedef MachineInstrBundleIterator<const MyUnbundledInstr, true>
144*fcd69daaSDuncan P. N. Exon Smith     const_reverse_unbundled_iterator;
145*fcd69daaSDuncan P. N. Exon Smith 
146*fcd69daaSDuncan P. N. Exon Smith TEST(MachineInstrBundleIteratorTest, ReverseConstructor) {
147*fcd69daaSDuncan P. N. Exon Smith   simple_ilist<MyUnbundledInstr, ilist_sentinel_tracking<true>> L;
148*fcd69daaSDuncan P. N. Exon Smith   const auto &CL = L;
149*fcd69daaSDuncan P. N. Exon Smith   MyUnbundledInstr A, B;
150*fcd69daaSDuncan P. N. Exon Smith   L.insert(L.end(), A);
151*fcd69daaSDuncan P. N. Exon Smith   L.insert(L.end(), B);
152*fcd69daaSDuncan P. N. Exon Smith 
153*fcd69daaSDuncan P. N. Exon Smith   // Save typing.
154*fcd69daaSDuncan P. N. Exon Smith   typedef MachineInstrBundleIterator<MyUnbundledInstr> iterator;
155*fcd69daaSDuncan P. N. Exon Smith   typedef MachineInstrBundleIterator<MyUnbundledInstr, true> reverse_iterator;
156*fcd69daaSDuncan P. N. Exon Smith   typedef MachineInstrBundleIterator<const MyUnbundledInstr> const_iterator;
157*fcd69daaSDuncan P. N. Exon Smith   typedef MachineInstrBundleIterator<const MyUnbundledInstr, true>
158*fcd69daaSDuncan P. N. Exon Smith       const_reverse_iterator;
159*fcd69daaSDuncan P. N. Exon Smith 
160*fcd69daaSDuncan P. N. Exon Smith   // Convert to bundle iterators.
161*fcd69daaSDuncan P. N. Exon Smith   auto begin = [&]() -> iterator { return L.begin(); };
162*fcd69daaSDuncan P. N. Exon Smith   auto end = [&]() -> iterator { return L.end(); };
163*fcd69daaSDuncan P. N. Exon Smith   auto rbegin = [&]() -> reverse_iterator { return L.rbegin(); };
164*fcd69daaSDuncan P. N. Exon Smith   auto rend = [&]() -> reverse_iterator { return L.rend(); };
165*fcd69daaSDuncan P. N. Exon Smith   auto cbegin = [&]() -> const_iterator { return CL.begin(); };
166*fcd69daaSDuncan P. N. Exon Smith   auto cend = [&]() -> const_iterator { return CL.end(); };
167*fcd69daaSDuncan P. N. Exon Smith   auto crbegin = [&]() -> const_reverse_iterator { return CL.rbegin(); };
168*fcd69daaSDuncan P. N. Exon Smith   auto crend = [&]() -> const_reverse_iterator { return CL.rend(); };
169*fcd69daaSDuncan P. N. Exon Smith 
170*fcd69daaSDuncan P. N. Exon Smith   // Check conversion values.
171*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(begin(), iterator(rend()));
172*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(++begin(), iterator(++rbegin()));
173*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(end(), iterator(rbegin()));
174*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(rbegin(), reverse_iterator(end()));
175*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(++rbegin(), reverse_iterator(++begin()));
176*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(rend(), reverse_iterator(begin()));
177*fcd69daaSDuncan P. N. Exon Smith 
178*fcd69daaSDuncan P. N. Exon Smith   // Check const iterator constructors.
179*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(cbegin(), const_iterator(rend()));
180*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(cbegin(), const_iterator(crend()));
181*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(crbegin(), const_reverse_iterator(end()));
182*fcd69daaSDuncan P. N. Exon Smith   EXPECT_EQ(crbegin(), const_reverse_iterator(cend()));
183*fcd69daaSDuncan P. N. Exon Smith 
184*fcd69daaSDuncan P. N. Exon Smith   // Confirm lack of implicit conversions.
185*fcd69daaSDuncan P. N. Exon Smith   static_assert(!std::is_convertible<iterator, reverse_iterator>::value,
186*fcd69daaSDuncan P. N. Exon Smith                 "unexpected implicit conversion");
187*fcd69daaSDuncan P. N. Exon Smith   static_assert(!std::is_convertible<reverse_iterator, iterator>::value,
188*fcd69daaSDuncan P. N. Exon Smith                 "unexpected implicit conversion");
189*fcd69daaSDuncan P. N. Exon Smith   static_assert(
190*fcd69daaSDuncan P. N. Exon Smith       !std::is_convertible<const_iterator, const_reverse_iterator>::value,
191*fcd69daaSDuncan P. N. Exon Smith       "unexpected implicit conversion");
192*fcd69daaSDuncan P. N. Exon Smith   static_assert(
193*fcd69daaSDuncan P. N. Exon Smith       !std::is_convertible<const_reverse_iterator, const_iterator>::value,
194*fcd69daaSDuncan P. N. Exon Smith       "unexpected implicit conversion");
195*fcd69daaSDuncan P. N. Exon Smith }
196*fcd69daaSDuncan P. N. Exon Smith 
197f197b1f7SDuncan P. N. Exon Smith } // end namespace
198