Skip to content

Commit e80799e

Browse files
committed
[ADT] Notify ilist traits about in-list transfers
Summary: Previously no client of ilist traits has needed to know about transfers of nodes within the same list, so as an optimization, ilist doesn't call transferNodesFromList in that case. However, now there are clients that want to use ilist traits to cache instruction ordering information to optimize dominance queries of instructions in the same basic block. This change updates the existing ilist traits users to detect in-list transfers and do nothing in that case. After this change, we can start caching instruction ordering information in LLVM IR data structures. There are two main ways to do that: - by putting an order integer into the Instruction class - by maintaining order integers in a hash table on BasicBlock I plan to implement and measure both, but I wanted to commit this change first to enable other out of tree ilist clients to implement this optimization as well. Reviewers: lattner, hfinkel, chandlerc Subscribers: hiraditya, dexonsmith, llvm-commits Differential Revision: https://reviews.llvm.org/D57120 llvm-svn: 351992
1 parent 1ecf6e5 commit e80799e

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

llvm/include/llvm/ADT/ilist.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ template <typename NodeTy> struct ilist_callback_traits {
6565
void addNodeToList(NodeTy *) {}
6666
void removeNodeFromList(NodeTy *) {}
6767

68-
/// Callback before transferring nodes to this list.
69-
///
70-
/// \pre \c this!=&OldList
68+
/// Callback before transferring nodes to this list. The nodes may already be
69+
/// in this same list.
7170
template <class Iterator>
7271
void transferNodesFromList(ilist_callback_traits &OldList, Iterator /*first*/,
7372
Iterator /*last*/) {
@@ -286,8 +285,8 @@ class iplist_impl : public TraitsT, IntrusiveListT {
286285
if (position == last)
287286
return;
288287

289-
if (this != &L2) // Notify traits we moved the nodes...
290-
this->transferNodesFromList(L2, first, last);
288+
// Notify traits we moved the nodes...
289+
this->transferNodesFromList(L2, first, last);
291290

292291
base_list_type::splice(position, L2, first, last);
293292
}

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ template <> struct ilist_callback_traits<MachineBasicBlock> {
8585

8686
template <class Iterator>
8787
void transferNodesFromList(ilist_callback_traits &OldList, Iterator, Iterator) {
88-
llvm_unreachable("Never transfer between lists");
88+
assert(this == &OldList && "never transfer MBBs between functions");
8989
}
9090
};
9191

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ void ilist_traits<MachineInstr>::transferNodesFromList(ilist_traits &FromList,
132132
instr_iterator First,
133133
instr_iterator Last) {
134134
assert(Parent->getParent() == FromList.Parent->getParent() &&
135-
"MachineInstr parent mismatch!");
136-
assert(this != &FromList && "Called without a real transfer...");
135+
"cannot transfer MachineInstrs between MachineFunctions");
136+
137+
// If it's within the same BB, there's nothing to do.
138+
if (this == &FromList)
139+
return;
140+
137141
assert(Parent != FromList.Parent && "Two lists have the same parent?");
138142

139143
// If splicing between two blocks within the same function, just update the

llvm/lib/IR/SymbolTableListTraitsImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ void SymbolTableListTraits<ValueSubClass>::transferNodesFromList(
8383
SymbolTableListTraits &L2, iterator first, iterator last) {
8484
// We only have to do work here if transferring instructions between BBs
8585
ItemParentClass *NewIP = getListOwner(), *OldIP = L2.getListOwner();
86-
assert(NewIP != OldIP && "Expected different list owners");
86+
if (NewIP == OldIP)
87+
return;
8788

8889
// We only have to update symbol table entries if we are transferring the
8990
// instructions to a different symtab object...

llvm/unittests/ADT/IListTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ struct NodeWithCallback : ilist_node<NodeWithCallback> {
207207
} // end namespace
208208

209209
namespace llvm {
210+
// These nodes are stack-allocated for testing purposes, so don't let the ilist
211+
// own or delete them.
212+
template <> struct ilist_alloc_traits<NodeWithCallback> {
213+
static void deleteNode(NodeWithCallback *) {}
214+
};
215+
210216
template <> struct ilist_callback_traits<NodeWithCallback> {
211217
void addNodeToList(NodeWithCallback *N) { N->IsInList = true; }
212218
void removeNodeFromList(NodeWithCallback *N) { N->IsInList = false; }
@@ -247,6 +253,30 @@ TEST(IListTest, addNodeToList) {
247253
ASSERT_TRUE(N.WasTransferred);
248254
}
249255

256+
TEST(IListTest, sameListSplice) {
257+
NodeWithCallback N1(1);
258+
NodeWithCallback N2(2);
259+
ASSERT_FALSE(N1.WasTransferred);
260+
ASSERT_FALSE(N2.WasTransferred);
261+
262+
ilist<NodeWithCallback> L1;
263+
L1.insert(L1.end(), &N1);
264+
L1.insert(L1.end(), &N2);
265+
ASSERT_EQ(2u, L1.size());
266+
ASSERT_EQ(&N1, &L1.front());
267+
ASSERT_FALSE(N1.WasTransferred);
268+
ASSERT_FALSE(N2.WasTransferred);
269+
270+
// Swap the nodes with splice inside the same list. Check that we get the
271+
// transfer callback.
272+
L1.splice(L1.begin(), L1, std::next(L1.begin()), L1.end());
273+
ASSERT_EQ(2u, L1.size());
274+
ASSERT_EQ(&N1, &L1.back());
275+
ASSERT_EQ(&N2, &L1.front());
276+
ASSERT_FALSE(N1.WasTransferred);
277+
ASSERT_TRUE(N2.WasTransferred);
278+
}
279+
250280
struct PrivateNode : private ilist_node<PrivateNode> {
251281
friend struct llvm::ilist_detail::NodeAccess;
252282

0 commit comments

Comments
 (0)