llvm-stress: stop abusing ConstantFP::get()
authorDylan Noblesmith <nobled@dreamwidth.org>
Tue, 10 Apr 2012 22:44:51 +0000 (22:44 +0000)
committerDylan Noblesmith <nobled@dreamwidth.org>
Tue, 10 Apr 2012 22:44:51 +0000 (22:44 +0000)
ConstantFP::get(Type*, double) is unreliably host-specific:
it can't handle a type like PPC128 on an x86 host. It even
has a comment to that effect: "This should only be used for
simple constant values like 2.0/1.0 etc, that are
known-valid both as host double and as the target format."

Instead, use APFloat. While we're at it, randomize the floating
point value more thoroughly; it was previously limited
to the range 0 to 2**19 - 1.

PR12451.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154446 91177308-0d34-0410-b5e6-96231b3b80d8

tools/llvm-stress/llvm-stress.cpp

index 7da80bcff92cbdd6ee9c3366af13be71acab8ae6..d284ea5e42c9e74222b8a1bb25aee32df358b5fa 100644 (file)
@@ -60,14 +60,28 @@ class Random {
 public:
   /// C'tor
   Random(unsigned _seed):Seed(_seed) {}
-  /// Return the next random value.
-  unsigned Rand() {
-    unsigned Val = Seed + 0x000b07a1;
+
+  /// Return a random integer, up to a
+  /// maximum of 2**19 - 1.
+  uint32_t Rand() {
+    uint32_t Val = Seed + 0x000b07a1;
     Seed = (Val * 0x3c7c0ac1);
     // Only lowest 19 bits are random-ish.
     return Seed & 0x7ffff;
   }
 
+  /// Return a random 32 bit integer.
+  uint32_t Rand32() {
+    uint32_t Val = Rand();
+    Val &= 0xffff;
+    return Val | (Rand() << 16);
+  }
+
+  /// Return a random 64 bit integer.
+  uint64_t Rand64() {
+    uint64_t Val = Rand32();
+    return Val | (uint64_t(Rand32()) << 32);
+  }
 private:
   unsigned Seed;
 };
@@ -348,10 +362,20 @@ struct ConstModifier: public Modifier {
     }
 
     if (Ty->isFloatingPointTy()) {
+      // Generate 128 random bits, the size of the (currently)
+      // largest floating-point types.
+      uint64_t RandomBits[2];
+      for (unsigned i = 0; i < 2; ++i)
+        RandomBits[i] = Ran->Rand64();
+
+      APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
+
+      bool isIEEE = !Ty->isX86_FP80Ty() && !Ty->isPPC_FP128Ty();
+      APFloat RandomFloat(RandomInt, isIEEE);
+
       if (Ran->Rand() & 1)
         return PT->push_back(ConstantFP::getNullValue(Ty));
-      return PT->push_back(ConstantFP::get(Ty,
-                                           static_cast<double>(1)/Ran->Rand()));
+      return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
     }
 
     if (Ty->isIntegerTy()) {