[llvm-stress] Add getRandom() helper that was going to be part of D34157. NFCI.

llvm-svn: 306294
This commit is contained in:
Simon Pilgrim 2017-06-26 15:41:36 +00:00
parent 502d4ce2e4
commit e77df9bc6c
1 changed files with 32 additions and 27 deletions

View File

@ -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,13 +403,13 @@ 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())));
@ -416,7 +421,7 @@ struct ConstModifier: public Modifier {
case 4:
case 5:
case 6:
PT->push_back(ConstantInt::get(Ty, Ran->Rand()));
PT->push_back(ConstantInt::get(Ty, getRandom()));
}
}
}
@ -441,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);
}
@ -459,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);
}
@ -484,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);
}
@ -520,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()));
}
@ -533,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()));
@ -543,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()));
@ -552,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()));
@ -589,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);
}
@ -613,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;
}