Index: include/llvm/ADT/STLExtras.h =================================================================== --- include/llvm/ADT/STLExtras.h +++ include/llvm/ADT/STLExtras.h @@ -32,9 +32,14 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/iterator_range.h" +#include "llvm/Config/abi-breaking.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" +#if LLVM_ENABLE_ABI_BREAKING_CHECKS +#include // for std::mt19937 +#endif + namespace llvm { // Only used by compiler if both template types are the same. Useful when @@ -771,6 +776,10 @@ // behavior with an empty sequence. auto NElts = End - Start; if (NElts <= 1) return; +#if LLVM_ENABLE_ABI_BREAKING_CHECKS + std::mt19937 Generator(std::random_device{}()); + std::shuffle(Start, End, Generator); +#endif qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start)); } @@ -784,10 +793,33 @@ // behavior with an empty sequence. auto NElts = End - Start; if (NElts <= 1) return; +#if LLVM_ENABLE_ABI_BREAKING_CHECKS + std::mt19937 Generator(std::random_device{}()); + std::shuffle(Start, End, Generator); +#endif qsort(&*Start, NElts, sizeof(*Start), reinterpret_cast(Compare)); } +// Provide wrappers to std::sort which shuffle the elements before sorting. +template +inline void sort(IteratorTy Start, IteratorTy End) { +#if LLVM_ENABLE_ABI_BREAKING_CHECKS + std::mt19937 Generator(std::random_device{}()); + std::shuffle(Start, End, Generator); +#endif + std::sort(Start, End); +} + +template +inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) { +#if LLVM_ENABLE_ABI_BREAKING_CHECKS + std::mt19937 Generator(std::random_device{}()); + std::shuffle(Start, End, Generator); +#endif + std::sort(Start, End, Comp); +} + //===----------------------------------------------------------------------===// // Extra additions to //===----------------------------------------------------------------------===//