Skip to content

Commit f38b005

Browse files
committedFeb 26, 2019
[TableGen] Make OpcodeMappings sort comparator deterministic NFCI
The previous sort comparator was not deterministic, i.e. in some situations it would be possible for lhs < rhs && rhs < lhs. This was discovered by an STL assertion in a Windows debug build of llvm-tblgen. Differential Revision: https://reviews.llvm.org/D58687 llvm-svn: 354910
1 parent 7557afa commit f38b005

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed
 

‎llvm/utils/TableGen/CodeGenSchedule.cpp

+16-18
Original file line numberDiff line numberDiff line change
@@ -368,24 +368,22 @@ processSTIPredicate(STIPredicateFunction &Fn,
368368
[&](const OpcodeMapPair &Lhs, const OpcodeMapPair &Rhs) {
369369
unsigned LhsIdx = Opcode2Index[Lhs.first];
370370
unsigned RhsIdx = Opcode2Index[Rhs.first];
371-
std::pair<APInt, APInt> &LhsMasks = OpcodeMasks[LhsIdx];
372-
std::pair<APInt, APInt> &RhsMasks = OpcodeMasks[RhsIdx];
373-
374-
if (LhsMasks.first != RhsMasks.first) {
375-
if (LhsMasks.first.countPopulation() <
376-
RhsMasks.first.countPopulation())
377-
return true;
378-
return LhsMasks.first.countLeadingZeros() >
379-
RhsMasks.first.countLeadingZeros();
380-
}
381-
382-
if (LhsMasks.second != RhsMasks.second) {
383-
if (LhsMasks.second.countPopulation() <
384-
RhsMasks.second.countPopulation())
385-
return true;
386-
return LhsMasks.second.countLeadingZeros() >
387-
RhsMasks.second.countLeadingZeros();
388-
}
371+
const std::pair<APInt, APInt> &LhsMasks = OpcodeMasks[LhsIdx];
372+
const std::pair<APInt, APInt> &RhsMasks = OpcodeMasks[RhsIdx];
373+
374+
auto LessThan = [](const APInt &Lhs, const APInt &Rhs) {
375+
unsigned LhsCountPopulation = Lhs.countPopulation();
376+
unsigned RhsCountPopulation = Rhs.countPopulation();
377+
return ((LhsCountPopulation < RhsCountPopulation) ||
378+
((LhsCountPopulation == RhsCountPopulation) &&
379+
(Lhs.countLeadingZeros() > Rhs.countLeadingZeros())));
380+
};
381+
382+
if (LhsMasks.first != RhsMasks.first)
383+
return LessThan(LhsMasks.first, RhsMasks.first);
384+
385+
if (LhsMasks.second != RhsMasks.second)
386+
return LessThan(LhsMasks.second, RhsMasks.second);
389387

390388
return LhsIdx < RhsIdx;
391389
});

0 commit comments

Comments
 (0)
Please sign in to comment.