Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / ADT / IListIteratorBitsTest.cpp
blob167b30a5e308512b585b2cd966b97c6099b152a1
1 //==- unittests/ADT/IListIteratorBitsTest.cpp - ilist_iterator_w_bits tests -=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/simple_ilist.h"
10 #include "gtest/gtest.h"
12 // Test that ilist_iterator_w_bits can be used to store extra information about
13 // what we're iterating over, that it's only enabled when given the relevant
14 // option, and it can be fed into various iteration utilities.
16 using namespace llvm;
18 namespace {
20 class dummy;
22 struct Node : ilist_node<Node, ilist_iterator_bits<true>> {
23 friend class dummy;
26 struct PlainNode : ilist_node<PlainNode> {
27 friend class dummy;
30 TEST(IListIteratorBitsTest, DefaultConstructor) {
31 simple_ilist<Node, ilist_iterator_bits<true>>::iterator I;
32 simple_ilist<Node, ilist_iterator_bits<true>>::reverse_iterator RI;
33 simple_ilist<Node, ilist_iterator_bits<true>>::const_iterator CI;
34 simple_ilist<Node, ilist_iterator_bits<true>>::const_reverse_iterator CRI;
35 EXPECT_EQ(nullptr, I.getNodePtr());
36 EXPECT_EQ(nullptr, CI.getNodePtr());
37 EXPECT_EQ(nullptr, RI.getNodePtr());
38 EXPECT_EQ(nullptr, CRI.getNodePtr());
39 EXPECT_EQ(I, I);
40 EXPECT_EQ(I, CI);
41 EXPECT_EQ(CI, I);
42 EXPECT_EQ(CI, CI);
43 EXPECT_EQ(RI, RI);
44 EXPECT_EQ(RI, CRI);
45 EXPECT_EQ(CRI, RI);
46 EXPECT_EQ(CRI, CRI);
47 EXPECT_EQ(I, RI.getReverse());
48 EXPECT_EQ(RI, I.getReverse());
51 TEST(IListIteratorBitsTest, ConsAndAssignment) {
52 simple_ilist<Node, ilist_iterator_bits<true>> L;
53 Node A;
54 L.insert(L.end(), A);
56 simple_ilist<Node, ilist_iterator_bits<true>>::iterator I, I2;
58 // Two sets of tests: if we've compiled in the iterator bits, then check that
59 // HeadInclusiveBit and TailInclusiveBit are preserved on assignment and copy
60 // construction, but not on other operations.
61 #ifdef EXPERIMENTAL_DEBUGINFO_ITERATORS
62 I = L.begin();
63 EXPECT_FALSE(I.getHeadBit());
64 EXPECT_FALSE(I.getTailBit());
65 I.setHeadBit(true);
66 I.setTailBit(true);
67 EXPECT_TRUE(I.getHeadBit());
68 EXPECT_TRUE(I.getTailBit());
70 ++I;
72 EXPECT_FALSE(I.getHeadBit());
73 EXPECT_FALSE(I.getTailBit());
75 I = L.begin();
76 I.setHeadBit(true);
77 I.setTailBit(true);
78 I2 = I;
79 EXPECT_TRUE(I2.getHeadBit());
80 EXPECT_TRUE(I2.getTailBit());
82 I = L.begin();
83 I.setHeadBit(true);
84 I.setTailBit(true);
85 simple_ilist<Node, ilist_iterator_bits<true>>::iterator I3(I);
86 EXPECT_TRUE(I3.getHeadBit());
87 EXPECT_TRUE(I3.getTailBit());
88 #else
89 // The calls should be available, but shouldn't actually store information.
90 I = L.begin();
91 EXPECT_FALSE(I.getHeadBit());
92 EXPECT_FALSE(I.getTailBit());
93 I.setHeadBit(true);
94 I.setTailBit(true);
95 EXPECT_FALSE(I.getHeadBit());
96 EXPECT_FALSE(I.getTailBit());
97 // Suppress warnings as we don't test with this variable.
98 (void)I2;
99 #endif
102 class dummy {
103 // Test that we get an ilist_iterator_w_bits out of the node given that the
104 // options are enabled.
105 using node_options = typename ilist_detail::compute_node_options<
106 Node, ilist_iterator_bits<true>>::type;
107 static_assert(std::is_same<Node::self_iterator,
108 llvm::ilist_iterator_w_bits<node_options, false,
109 false>>::value);
111 // Now test that a plain node, without the option, gets a plain
112 // ilist_iterator.
113 using plain_node_options =
114 typename ilist_detail::compute_node_options<PlainNode>::type;
115 static_assert(std::is_same<
116 PlainNode::self_iterator,
117 llvm::ilist_iterator<plain_node_options, false, false>>::value);
120 TEST(IListIteratorBitsTest, RangeIteration) {
121 // Check that we can feed ilist_iterator_w_bits into make_range and similar.
122 // Plus, we should be able to convert it to a reverse iterator and use that.
123 simple_ilist<Node, ilist_iterator_bits<true>> L;
124 Node A;
125 L.insert(L.end(), A);
127 for (Node &N : make_range(L.begin(), L.end()))
128 (void)N;
130 simple_ilist<Node, ilist_iterator_bits<true>>::iterator It =
131 L.begin()->getIterator();
132 auto RevIt = It.getReverse();
134 for (Node &N : make_range(RevIt, L.rend()))
135 (void)N;
138 } // end namespace