]> granicus.if.org Git - llvm/commitdiff
Remove uses of deprecated std::random_shuffle in the LLVM code base. Reviewed as...
authorMarshall Clow <mclow.lists@gmail.com>
Thu, 16 Feb 2017 14:37:03 +0000 (14:37 +0000)
committerMarshall Clow <mclow.lists@gmail.com>
Thu, 16 Feb 2017 14:37:03 +0000 (14:37 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295325 91177308-0d34-0410-b5e6-96231b3b80d8

tools/bugpoint/FindBugs.cpp
tools/bugpoint/ListReducer.h
tools/llvm-stress/llvm-stress.cpp
unittests/ADT/TinyPtrVectorTest.cpp

index 156f4d0d78fe1ed0c25cdc657fc5d033a7c2231b..3093169ba8b0010b637cbc39e02e8c6b075130f0 100644 (file)
@@ -21,6 +21,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <ctime>
+#include <random>
 using namespace llvm;
 
 Error
@@ -39,14 +40,13 @@ BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {
       return E;
   }
 
-  srand(time(nullptr));
-
+  std::mt19937 randomness(std::random_device{}());
   unsigned num = 1;
   while (1) {
     //
     // Step 1: Randomize the order of the optimizer passes.
     //
-    std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
+    std::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
 
     //
     // Step 2: Run optimizer passes on the program and check for success.
index dcfa11d06927f42db05aa1f5838c106a5ca93fe8..0f9db022d555e34ae6ba536648015e47efbe7dc8 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <cstdlib>
+#include <random>
 #include <vector>
 
 namespace llvm {
@@ -46,7 +47,7 @@ template <typename ElTy> struct ListReducer {
   /// that bugpoint does.
   Expected<bool> reduceList(std::vector<ElTy> &TheList) {
     std::vector<ElTy> empty;
-    std::srand(0x6e5ea738); // Seed the random number generator
+    std::mt19937 randomness(0x6e5ea738);  // Seed the random number generator
     Expected<TestResult> Result = doTest(TheList, empty);
     if (Error E = Result.takeError())
       return std::move(E);
@@ -92,7 +93,7 @@ template <typename ElTy> struct ListReducer {
       // distribution (improving the speed of convergence).
       if (ShufflingEnabled && NumOfIterationsWithoutProgress > MaxIterations) {
         std::vector<ElTy> ShuffledList(TheList);
-        std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
+        std::shuffle(ShuffledList.begin(), ShuffledList.end(), randomness);
         errs() << "\n\n*** Testing shuffled set...\n\n";
         // Check that random shuffle doesn't lose the bug
         Expected<TestResult> Result = doTest(ShuffledList, empty);
index 731a24d0ac2d2b58e895e30585313d0a314a93e8..fdfa197e60168d68e65097b9dcf3588c84faa821 100644 (file)
@@ -28,6 +28,7 @@
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/ToolOutputFile.h"
 #include <algorithm>
+#include <random>
 #include <vector>
 
 namespace llvm {
@@ -113,6 +114,12 @@ public:
     return  Rand64() % y;
   }
 
+  /// Make this like a C++11 random device
+  typedef uint32_t result_type;
+  uint32_t operator()() { return Rand32(); }
+  static constexpr result_type min() { return 0; }
+  static constexpr result_type max() { return 0x7ffff; }
+  
 private:
   unsigned Seed;
 };
@@ -662,7 +669,7 @@ static void IntroduceControlFlow(Function *F, Random &R) {
       BoolInst.push_back(&Instr);
   }
 
-  std::random_shuffle(BoolInst.begin(), BoolInst.end(), R);
+  std::shuffle(BoolInst.begin(), BoolInst.end(), R);
 
   for (auto *Instr : BoolInst) {
     BasicBlock *Curr = Instr->getParent();
index 26189b76394fc9460023d38d1cc828d761a5755c..8d5fa4060913ba3081b6871b8265a25593b29807 100644 (file)
 #include "llvm/Support/type_traits.h"
 #include "gtest/gtest.h"
 #include <algorithm>
+#include <random>
 #include <vector>
 
 using namespace llvm;
 
 namespace {
 
-// The world's worst RNG, but it is deterministic and makes it easy to get
-// *some* shuffling of elements.
-static ptrdiff_t test_shuffle_rng(ptrdiff_t i) {
-  return (i + i * 33) % i;
-}
-static ptrdiff_t (*test_shuffle_rng_p)(ptrdiff_t) = &test_shuffle_rng;
-
 template <typename VectorT>
 class TinyPtrVectorTest : public testing::Test {
 protected:
@@ -46,7 +40,7 @@ protected:
     for (size_t i = 0, e = array_lengthof(TestValues); i != e; ++i)
       TestPtrs.push_back(&TestValues[i]);
 
-    std::random_shuffle(TestPtrs.begin(), TestPtrs.end(), test_shuffle_rng_p);
+    std::shuffle(TestPtrs.begin(), TestPtrs.end(), std::mt19937{});
   }
 
   ArrayRef<PtrT> testArray(size_t N) {