1 //===- MachineInstrBundleIteratorTest.cpp ---------------------------------===//
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
10 #include "llvm/CodeGen/MachineInstrBundleIterator.h"
11 #include "llvm/ADT/ilist_node.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15
16 namespace {
17
18 struct MyBundledInstr
19 : public ilist_node<MyBundledInstr, ilist_sentinel_tracking<true>> {
isBundledWithPred__anon5811a35c0111::MyBundledInstr20 bool isBundledWithPred() const { return true; }
isBundledWithSucc__anon5811a35c0111::MyBundledInstr21 bool isBundledWithSucc() const { return true; }
22 };
23 typedef MachineInstrBundleIterator<MyBundledInstr> bundled_iterator;
24 typedef MachineInstrBundleIterator<const MyBundledInstr> const_bundled_iterator;
25 typedef MachineInstrBundleIterator<MyBundledInstr, true>
26 reverse_bundled_iterator;
27 typedef MachineInstrBundleIterator<const MyBundledInstr, true>
28 const_reverse_bundled_iterator;
29
30 #ifdef GTEST_HAS_DEATH_TEST
31 #ifndef NDEBUG
TEST(MachineInstrBundleIteratorTest,CheckForBundles)32 TEST(MachineInstrBundleIteratorTest, CheckForBundles) {
33 MyBundledInstr MBI;
34 auto I = MBI.getIterator();
35 auto RI = I.getReverse();
36
37 // Confirm that MBI is always considered bundled.
38 EXPECT_TRUE(MBI.isBundledWithPred());
39 EXPECT_TRUE(MBI.isBundledWithSucc());
40
41 // Confirm that iterators check in their constructor for bundled iterators.
42 EXPECT_DEATH((void)static_cast<bundled_iterator>(I),
43 "not legal to initialize");
44 EXPECT_DEATH((void)static_cast<bundled_iterator>(MBI),
45 "not legal to initialize");
46 EXPECT_DEATH((void)static_cast<bundled_iterator>(&MBI),
47 "not legal to initialize");
48 EXPECT_DEATH((void)static_cast<const_bundled_iterator>(I),
49 "not legal to initialize");
50 EXPECT_DEATH((void)static_cast<const_bundled_iterator>(MBI),
51 "not legal to initialize");
52 EXPECT_DEATH((void)static_cast<const_bundled_iterator>(&MBI),
53 "not legal to initialize");
54 EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(RI),
55 "not legal to initialize");
56 EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(MBI),
57 "not legal to initialize");
58 EXPECT_DEATH((void)static_cast<reverse_bundled_iterator>(&MBI),
59 "not legal to initialize");
60 EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(RI),
61 "not legal to initialize");
62 EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(MBI),
63 "not legal to initialize");
64 EXPECT_DEATH((void)static_cast<const_reverse_bundled_iterator>(&MBI),
65 "not legal to initialize");
66 }
67 #endif
68 #endif
69
TEST(MachineInstrBundleIteratorTest,CompareToBundledMI)70 TEST(MachineInstrBundleIteratorTest, CompareToBundledMI) {
71 MyBundledInstr MBI;
72 const MyBundledInstr &CMBI = MBI;
73 bundled_iterator I;
74 const_bundled_iterator CI;
75
76 // Confirm that MBI is always considered bundled.
77 EXPECT_TRUE(MBI.isBundledWithPred());
78 EXPECT_TRUE(MBI.isBundledWithSucc());
79
80 // These invocations will crash when !NDEBUG if a conversion is taking place.
81 // These checks confirm that comparison operators don't use any conversion
82 // operators.
83 ASSERT_FALSE(MBI == I);
84 ASSERT_FALSE(&MBI == I);
85 ASSERT_FALSE(CMBI == I);
86 ASSERT_FALSE(&CMBI == I);
87 ASSERT_FALSE(I == MBI);
88 ASSERT_FALSE(I == &MBI);
89 ASSERT_FALSE(I == CMBI);
90 ASSERT_FALSE(I == &CMBI);
91 ASSERT_FALSE(MBI == CI);
92 ASSERT_FALSE(&MBI == CI);
93 ASSERT_FALSE(CMBI == CI);
94 ASSERT_FALSE(&CMBI == CI);
95 ASSERT_FALSE(CI == MBI);
96 ASSERT_FALSE(CI == &MBI);
97 ASSERT_FALSE(CI == CMBI);
98 ASSERT_FALSE(CI == &CMBI);
99 ASSERT_FALSE(MBI.getIterator() == I);
100 ASSERT_FALSE(CMBI.getIterator() == I);
101 ASSERT_FALSE(I == MBI.getIterator());
102 ASSERT_FALSE(I == CMBI.getIterator());
103 ASSERT_FALSE(MBI.getIterator() == CI);
104 ASSERT_FALSE(CMBI.getIterator() == CI);
105 ASSERT_FALSE(CI == MBI.getIterator());
106 ASSERT_FALSE(CI == CMBI.getIterator());
107 ASSERT_TRUE(MBI != I);
108 ASSERT_TRUE(&MBI != I);
109 ASSERT_TRUE(CMBI != I);
110 ASSERT_TRUE(&CMBI != I);
111 ASSERT_TRUE(I != MBI);
112 ASSERT_TRUE(I != &MBI);
113 ASSERT_TRUE(I != CMBI);
114 ASSERT_TRUE(I != &CMBI);
115 ASSERT_TRUE(MBI != CI);
116 ASSERT_TRUE(&MBI != CI);
117 ASSERT_TRUE(CMBI != CI);
118 ASSERT_TRUE(&CMBI != CI);
119 ASSERT_TRUE(CI != MBI);
120 ASSERT_TRUE(CI != &MBI);
121 ASSERT_TRUE(CI != CMBI);
122 ASSERT_TRUE(CI != &CMBI);
123 ASSERT_TRUE(MBI.getIterator() != I);
124 ASSERT_TRUE(CMBI.getIterator() != I);
125 ASSERT_TRUE(I != MBI.getIterator());
126 ASSERT_TRUE(I != CMBI.getIterator());
127 ASSERT_TRUE(MBI.getIterator() != CI);
128 ASSERT_TRUE(CMBI.getIterator() != CI);
129 ASSERT_TRUE(CI != MBI.getIterator());
130 ASSERT_TRUE(CI != CMBI.getIterator());
131 }
132
133 struct MyUnbundledInstr
134 : ilist_node<MyUnbundledInstr, ilist_sentinel_tracking<true>> {
isBundledWithPred__anon5811a35c0111::MyUnbundledInstr135 bool isBundledWithPred() const { return false; }
isBundledWithSucc__anon5811a35c0111::MyUnbundledInstr136 bool isBundledWithSucc() const { return false; }
137 };
138 typedef MachineInstrBundleIterator<MyUnbundledInstr> unbundled_iterator;
139 typedef MachineInstrBundleIterator<const MyUnbundledInstr>
140 const_unbundled_iterator;
141 typedef MachineInstrBundleIterator<MyUnbundledInstr, true>
142 reverse_unbundled_iterator;
143 typedef MachineInstrBundleIterator<const MyUnbundledInstr, true>
144 const_reverse_unbundled_iterator;
145
TEST(MachineInstrBundleIteratorTest,ReverseConstructor)146 TEST(MachineInstrBundleIteratorTest, ReverseConstructor) {
147 simple_ilist<MyUnbundledInstr, ilist_sentinel_tracking<true>> L;
148 const auto &CL = L;
149 MyUnbundledInstr A, B;
150 L.insert(L.end(), A);
151 L.insert(L.end(), B);
152
153 // Save typing.
154 typedef MachineInstrBundleIterator<MyUnbundledInstr> iterator;
155 typedef MachineInstrBundleIterator<MyUnbundledInstr, true> reverse_iterator;
156 typedef MachineInstrBundleIterator<const MyUnbundledInstr> const_iterator;
157 typedef MachineInstrBundleIterator<const MyUnbundledInstr, true>
158 const_reverse_iterator;
159
160 // Convert to bundle iterators.
161 auto begin = [&]() -> iterator { return L.begin(); };
162 auto end = [&]() -> iterator { return L.end(); };
163 auto rbegin = [&]() -> reverse_iterator { return L.rbegin(); };
164 auto rend = [&]() -> reverse_iterator { return L.rend(); };
165 auto cbegin = [&]() -> const_iterator { return CL.begin(); };
166 auto cend = [&]() -> const_iterator { return CL.end(); };
167 auto crbegin = [&]() -> const_reverse_iterator { return CL.rbegin(); };
168 auto crend = [&]() -> const_reverse_iterator { return CL.rend(); };
169
170 // Check conversion values.
171 EXPECT_EQ(begin(), iterator(rend()));
172 EXPECT_EQ(++begin(), iterator(++rbegin()));
173 EXPECT_EQ(end(), iterator(rbegin()));
174 EXPECT_EQ(rbegin(), reverse_iterator(end()));
175 EXPECT_EQ(++rbegin(), reverse_iterator(++begin()));
176 EXPECT_EQ(rend(), reverse_iterator(begin()));
177
178 // Check const iterator constructors.
179 EXPECT_EQ(cbegin(), const_iterator(rend()));
180 EXPECT_EQ(cbegin(), const_iterator(crend()));
181 EXPECT_EQ(crbegin(), const_reverse_iterator(end()));
182 EXPECT_EQ(crbegin(), const_reverse_iterator(cend()));
183
184 // Confirm lack of implicit conversions.
185 static_assert(!std::is_convertible<iterator, reverse_iterator>::value,
186 "unexpected implicit conversion");
187 static_assert(!std::is_convertible<reverse_iterator, iterator>::value,
188 "unexpected implicit conversion");
189 static_assert(
190 !std::is_convertible<const_iterator, const_reverse_iterator>::value,
191 "unexpected implicit conversion");
192 static_assert(
193 !std::is_convertible<const_reverse_iterator, const_iterator>::value,
194 "unexpected implicit conversion");
195 }
196
197 } // end namespace
198