summaryrefslogtreecommitdiff
path: root/tools/llvm-stress/llvm-stress.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/llvm-stress/llvm-stress.cpp')
-rw-r--r--tools/llvm-stress/llvm-stress.cpp87
1 files changed, 47 insertions, 40 deletions
diff --git a/tools/llvm-stress/llvm-stress.cpp b/tools/llvm-stress/llvm-stress.cpp
index 0ed7adb46ddc2..3cf8b37bc2e29 100644
--- a/tools/llvm-stress/llvm-stress.cpp
+++ b/tools/llvm-stress/llvm-stress.cpp
@@ -96,17 +96,13 @@ public:
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);
+ uint64_t Val = Rand() & 0xffff;
+ Val |= uint64_t(Rand() & 0xffff) << 16;
+ Val |= uint64_t(Rand() & 0xffff) << 32;
+ Val |= uint64_t(Rand() & 0xffff) << 48;
+ return Val;
}
/// Rand operator for STL algorithms.
@@ -116,10 +112,14 @@ public:
/// 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; }
-
+ uint32_t operator()() {
+ uint32_t Val = Rand();
+ assert(Val <= max() && "Random value out of range");
+ return Val;
+ }
+
private:
unsigned Seed;
};
@@ -168,19 +168,24 @@ public:
}
protected:
+ /// Return a random integer.
+ uint32_t getRandom() {
+ return Ran->Rand();
+ }
+
/// Return a random value from the list of known values.
Value *getRandomVal() {
assert(PT->size());
- return PT->at(Ran->Rand() % PT->size());
+ return PT->at(getRandom() % PT->size());
}
Constant *getRandomConstant(Type *Tp) {
if (Tp->isIntegerTy()) {
- if (Ran->Rand() & 1)
+ if (getRandom() & 1)
return ConstantInt::getAllOnesValue(Tp);
return ConstantInt::getNullValue(Tp);
} else if (Tp->isFloatingPointTy()) {
- if (Ran->Rand() & 1)
+ if (getRandom() & 1)
return ConstantFP::getAllOnesValue(Tp);
return ConstantFP::getNullValue(Tp);
}
@@ -189,7 +194,7 @@ protected:
/// Return a random value with a known type.
Value *getRandomValue(Type *Tp) {
- unsigned index = Ran->Rand();
+ unsigned index = getRandom();
for (unsigned i=0; i<PT->size(); ++i) {
Value *V = PT->at((index + i) % PT->size());
if (V->getType() == Tp)
@@ -198,11 +203,11 @@ protected:
// If the requested type was not found, generate a constant value.
if (Tp->isIntegerTy()) {
- if (Ran->Rand() & 1)
+ if (getRandom() & 1)
return ConstantInt::getAllOnesValue(Tp);
return ConstantInt::getNullValue(Tp);
} else if (Tp->isFloatingPointTy()) {
- if (Ran->Rand() & 1)
+ if (getRandom() & 1)
return ConstantFP::getAllOnesValue(Tp);
return ConstantFP::getNullValue(Tp);
} else if (Tp->isVectorTy()) {
@@ -222,7 +227,7 @@ protected:
/// Return a random value of any pointer type.
Value *getRandomPointerValue() {
- unsigned index = Ran->Rand();
+ unsigned index = getRandom();
for (unsigned i=0; i<PT->size(); ++i) {
Value *V = PT->at((index + i) % PT->size());
if (V->getType()->isPointerTy())
@@ -233,7 +238,7 @@ protected:
/// Return a random value of any vector type.
Value *getRandomVectorValue() {
- unsigned index = Ran->Rand();
+ unsigned index = getRandom();
for (unsigned i=0; i<PT->size(); ++i) {
Value *V = PT->at((index + i) % PT->size());
if (V->getType()->isVectorTy())
@@ -244,7 +249,7 @@ protected:
/// Pick a random type.
Type *pickType() {
- return (Ran->Rand() & 1 ? pickVectorType() : pickScalarType());
+ return (getRandom() & 1 ? pickVectorType() : pickScalarType());
}
/// Pick a random pointer type.
@@ -258,7 +263,7 @@ protected:
// Pick a random vector width in the range 2**0 to 2**4.
// by adding two randoms we are generating a normal-like distribution
// around 2**3.
- unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3));
+ unsigned width = 1<<((getRandom() % 3) + (getRandom() % 3));
Type *Ty;
// Vectors of x86mmx are illegal; keep trying till we get something else.
@@ -288,7 +293,7 @@ protected:
AdditionalScalarTypes.begin(), AdditionalScalarTypes.end());
}
- return ScalarTypes[Ran->Rand() % ScalarTypes.size()];
+ return ScalarTypes[getRandom() % ScalarTypes.size()];
}
/// Basic block to populate
@@ -348,7 +353,7 @@ struct BinModifier: public Modifier {
bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy();
Instruction* Term = BB->getTerminator();
- unsigned R = Ran->Rand() % (isFloat ? 7 : 13);
+ unsigned R = getRandom() % (isFloat ? 7 : 13);
Instruction::BinaryOps Op;
switch (R) {
@@ -379,7 +384,7 @@ struct ConstModifier: public Modifier {
Type *Ty = pickType();
if (Ty->isVectorTy()) {
- switch (Ran->Rand() % 2) {
+ switch (getRandom() % 2) {
case 0: if (Ty->getScalarType()->isIntegerTy())
return PT->push_back(ConstantVector::getAllOnesValue(Ty));
break;
@@ -398,25 +403,27 @@ struct ConstModifier: public Modifier {
APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
- if (Ran->Rand() & 1)
+ if (getRandom() & 1)
return PT->push_back(ConstantFP::getNullValue(Ty));
return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
}
if (Ty->isIntegerTy()) {
- switch (Ran->Rand() % 7) {
+ switch (getRandom() % 7) {
case 0:
return PT->push_back(ConstantInt::get(
Ty, APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits())));
case 1:
return PT->push_back(ConstantInt::get(
Ty, APInt::getNullValue(Ty->getPrimitiveSizeInBits())));
- case 2: case 3: case 4: case 5:
+ case 2:
+ case 3:
+ case 4:
+ case 5:
case 6:
- PT->push_back(ConstantInt::get(Ty, Ran->Rand()));
+ PT->push_back(ConstantInt::get(Ty, getRandom()));
}
}
-
}
};
@@ -439,7 +446,7 @@ struct ExtractElementModifier: public Modifier {
Value *Val0 = getRandomVectorValue();
Value *V = ExtractElementInst::Create(Val0,
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
- Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
+ getRandom() % cast<VectorType>(Val0->getType())->getNumElements()),
"E", BB->getTerminator());
return PT->push_back(V);
}
@@ -457,9 +464,9 @@ struct ShuffModifier: public Modifier {
Type *I32 = Type::getInt32Ty(BB->getContext());
for (unsigned i=0; i<Width; ++i) {
- Constant *CI = ConstantInt::get(I32, Ran->Rand() % (Width*2));
+ Constant *CI = ConstantInt::get(I32, getRandom() % (Width*2));
// Pick some undef values.
- if (!(Ran->Rand() % 5))
+ if (!(getRandom() % 5))
CI = UndefValue::get(I32);
Idxs.push_back(CI);
}
@@ -482,7 +489,7 @@ struct InsertElementModifier: public Modifier {
Value *V = InsertElementInst::Create(Val0, Val1,
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
- Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
+ getRandom() % cast<VectorType>(Val0->getType())->getNumElements()),
"I", BB->getTerminator());
return PT->push_back(V);
}
@@ -518,7 +525,7 @@ struct CastModifier: public Modifier {
unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits();
// Generate lots of bitcasts.
- if ((Ran->Rand() & 1) && VSize == DestSize) {
+ if ((getRandom() & 1) && VSize == DestSize) {
return PT->push_back(
new BitCastInst(V, DestTy, "BC", BB->getTerminator()));
}
@@ -531,7 +538,7 @@ struct CastModifier: public Modifier {
new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
} else {
assert(VSize < DestSize && "Different int types with the same size?");
- if (Ran->Rand() & 1)
+ if (getRandom() & 1)
return PT->push_back(
new ZExtInst(V, DestTy, "ZE", BB->getTerminator()));
return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator()));
@@ -541,7 +548,7 @@ struct CastModifier: public Modifier {
// Fp to int.
if (VTy->getScalarType()->isFloatingPointTy() &&
DestTy->getScalarType()->isIntegerTy()) {
- if (Ran->Rand() & 1)
+ if (getRandom() & 1)
return PT->push_back(
new FPToSIInst(V, DestTy, "FC", BB->getTerminator()));
return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator()));
@@ -550,7 +557,7 @@ struct CastModifier: public Modifier {
// Int to fp.
if (VTy->getScalarType()->isIntegerTy() &&
DestTy->getScalarType()->isFloatingPointTy()) {
- if (Ran->Rand() & 1)
+ if (getRandom() & 1)
return PT->push_back(
new SIToFPInst(V, DestTy, "FC", BB->getTerminator()));
return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator()));
@@ -587,7 +594,7 @@ struct SelectModifier: public Modifier {
// If the value type is a vector, and we allow vector select, then in 50%
// of the cases generate a vector select.
- if (Val0->getType()->isVectorTy() && (Ran->Rand() % 1)) {
+ if (Val0->getType()->isVectorTy() && (getRandom() % 1)) {
unsigned NumElem = cast<VectorType>(Val0->getType())->getNumElements();
CondTy = VectorType::get(CondTy, NumElem);
}
@@ -611,11 +618,11 @@ struct CmpModifier: public Modifier {
int op;
if (fp) {
- op = Ran->Rand() %
+ op = getRandom() %
(CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) +
CmpInst::FIRST_FCMP_PREDICATE;
} else {
- op = Ran->Rand() %
+ op = getRandom() %
(CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) +
CmpInst::FIRST_ICMP_PREDICATE;
}