Skip to content

Commit e77df9b

Browse files
committedJun 26, 2017
[llvm-stress] Add getRandom() helper that was going to be part of D34157. NFCI.
llvm-svn: 306294
1 parent 502d4ce commit e77df9b

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed
 

‎llvm/tools/llvm-stress/llvm-stress.cpp

+32-27
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,24 @@ struct Modifier {
168168
}
169169

170170
protected:
171+
/// Return a random integer.
172+
uint32_t getRandom() {
173+
return Ran->Rand();
174+
}
175+
171176
/// Return a random value from the list of known values.
172177
Value *getRandomVal() {
173178
assert(PT->size());
174-
return PT->at(Ran->Rand() % PT->size());
179+
return PT->at(getRandom() % PT->size());
175180
}
176181

177182
Constant *getRandomConstant(Type *Tp) {
178183
if (Tp->isIntegerTy()) {
179-
if (Ran->Rand() & 1)
184+
if (getRandom() & 1)
180185
return ConstantInt::getAllOnesValue(Tp);
181186
return ConstantInt::getNullValue(Tp);
182187
} else if (Tp->isFloatingPointTy()) {
183-
if (Ran->Rand() & 1)
188+
if (getRandom() & 1)
184189
return ConstantFP::getAllOnesValue(Tp);
185190
return ConstantFP::getNullValue(Tp);
186191
}
@@ -189,7 +194,7 @@ struct Modifier {
189194

190195
/// Return a random value with a known type.
191196
Value *getRandomValue(Type *Tp) {
192-
unsigned index = Ran->Rand();
197+
unsigned index = getRandom();
193198
for (unsigned i=0; i<PT->size(); ++i) {
194199
Value *V = PT->at((index + i) % PT->size());
195200
if (V->getType() == Tp)
@@ -198,11 +203,11 @@ struct Modifier {
198203

199204
// If the requested type was not found, generate a constant value.
200205
if (Tp->isIntegerTy()) {
201-
if (Ran->Rand() & 1)
206+
if (getRandom() & 1)
202207
return ConstantInt::getAllOnesValue(Tp);
203208
return ConstantInt::getNullValue(Tp);
204209
} else if (Tp->isFloatingPointTy()) {
205-
if (Ran->Rand() & 1)
210+
if (getRandom() & 1)
206211
return ConstantFP::getAllOnesValue(Tp);
207212
return ConstantFP::getNullValue(Tp);
208213
} else if (Tp->isVectorTy()) {
@@ -222,7 +227,7 @@ struct Modifier {
222227

223228
/// Return a random value of any pointer type.
224229
Value *getRandomPointerValue() {
225-
unsigned index = Ran->Rand();
230+
unsigned index = getRandom();
226231
for (unsigned i=0; i<PT->size(); ++i) {
227232
Value *V = PT->at((index + i) % PT->size());
228233
if (V->getType()->isPointerTy())
@@ -233,7 +238,7 @@ struct Modifier {
233238

234239
/// Return a random value of any vector type.
235240
Value *getRandomVectorValue() {
236-
unsigned index = Ran->Rand();
241+
unsigned index = getRandom();
237242
for (unsigned i=0; i<PT->size(); ++i) {
238243
Value *V = PT->at((index + i) % PT->size());
239244
if (V->getType()->isVectorTy())
@@ -244,7 +249,7 @@ struct Modifier {
244249

245250
/// Pick a random type.
246251
Type *pickType() {
247-
return (Ran->Rand() & 1 ? pickVectorType() : pickScalarType());
252+
return (getRandom() & 1 ? pickVectorType() : pickScalarType());
248253
}
249254

250255
/// Pick a random pointer type.
@@ -258,7 +263,7 @@ struct Modifier {
258263
// Pick a random vector width in the range 2**0 to 2**4.
259264
// by adding two randoms we are generating a normal-like distribution
260265
// around 2**3.
261-
unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3));
266+
unsigned width = 1<<((getRandom() % 3) + (getRandom() % 3));
262267
Type *Ty;
263268

264269
// Vectors of x86mmx are illegal; keep trying till we get something else.
@@ -288,7 +293,7 @@ struct Modifier {
288293
AdditionalScalarTypes.begin(), AdditionalScalarTypes.end());
289294
}
290295

291-
return ScalarTypes[Ran->Rand() % ScalarTypes.size()];
296+
return ScalarTypes[getRandom() % ScalarTypes.size()];
292297
}
293298

294299
/// Basic block to populate
@@ -348,7 +353,7 @@ struct BinModifier: public Modifier {
348353

349354
bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy();
350355
Instruction* Term = BB->getTerminator();
351-
unsigned R = Ran->Rand() % (isFloat ? 7 : 13);
356+
unsigned R = getRandom() % (isFloat ? 7 : 13);
352357
Instruction::BinaryOps Op;
353358

354359
switch (R) {
@@ -379,7 +384,7 @@ struct ConstModifier: public Modifier {
379384
Type *Ty = pickType();
380385

381386
if (Ty->isVectorTy()) {
382-
switch (Ran->Rand() % 2) {
387+
switch (getRandom() % 2) {
383388
case 0: if (Ty->getScalarType()->isIntegerTy())
384389
return PT->push_back(ConstantVector::getAllOnesValue(Ty));
385390
break;
@@ -398,13 +403,13 @@ struct ConstModifier: public Modifier {
398403
APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
399404
APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
400405

401-
if (Ran->Rand() & 1)
406+
if (getRandom() & 1)
402407
return PT->push_back(ConstantFP::getNullValue(Ty));
403408
return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
404409
}
405410

406411
if (Ty->isIntegerTy()) {
407-
switch (Ran->Rand() % 7) {
412+
switch (getRandom() % 7) {
408413
case 0:
409414
return PT->push_back(ConstantInt::get(
410415
Ty, APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits())));
@@ -416,7 +421,7 @@ struct ConstModifier: public Modifier {
416421
case 4:
417422
case 5:
418423
case 6:
419-
PT->push_back(ConstantInt::get(Ty, Ran->Rand()));
424+
PT->push_back(ConstantInt::get(Ty, getRandom()));
420425
}
421426
}
422427
}
@@ -441,7 +446,7 @@ struct ExtractElementModifier: public Modifier {
441446
Value *Val0 = getRandomVectorValue();
442447
Value *V = ExtractElementInst::Create(Val0,
443448
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
444-
Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
449+
getRandom() % cast<VectorType>(Val0->getType())->getNumElements()),
445450
"E", BB->getTerminator());
446451
return PT->push_back(V);
447452
}
@@ -459,9 +464,9 @@ struct ShuffModifier: public Modifier {
459464

460465
Type *I32 = Type::getInt32Ty(BB->getContext());
461466
for (unsigned i=0; i<Width; ++i) {
462-
Constant *CI = ConstantInt::get(I32, Ran->Rand() % (Width*2));
467+
Constant *CI = ConstantInt::get(I32, getRandom() % (Width*2));
463468
// Pick some undef values.
464-
if (!(Ran->Rand() % 5))
469+
if (!(getRandom() % 5))
465470
CI = UndefValue::get(I32);
466471
Idxs.push_back(CI);
467472
}
@@ -484,7 +489,7 @@ struct InsertElementModifier: public Modifier {
484489

485490
Value *V = InsertElementInst::Create(Val0, Val1,
486491
ConstantInt::get(Type::getInt32Ty(BB->getContext()),
487-
Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
492+
getRandom() % cast<VectorType>(Val0->getType())->getNumElements()),
488493
"I", BB->getTerminator());
489494
return PT->push_back(V);
490495
}
@@ -520,7 +525,7 @@ struct CastModifier: public Modifier {
520525
unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits();
521526

522527
// Generate lots of bitcasts.
523-
if ((Ran->Rand() & 1) && VSize == DestSize) {
528+
if ((getRandom() & 1) && VSize == DestSize) {
524529
return PT->push_back(
525530
new BitCastInst(V, DestTy, "BC", BB->getTerminator()));
526531
}
@@ -533,7 +538,7 @@ struct CastModifier: public Modifier {
533538
new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
534539
} else {
535540
assert(VSize < DestSize && "Different int types with the same size?");
536-
if (Ran->Rand() & 1)
541+
if (getRandom() & 1)
537542
return PT->push_back(
538543
new ZExtInst(V, DestTy, "ZE", BB->getTerminator()));
539544
return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator()));
@@ -543,7 +548,7 @@ struct CastModifier: public Modifier {
543548
// Fp to int.
544549
if (VTy->getScalarType()->isFloatingPointTy() &&
545550
DestTy->getScalarType()->isIntegerTy()) {
546-
if (Ran->Rand() & 1)
551+
if (getRandom() & 1)
547552
return PT->push_back(
548553
new FPToSIInst(V, DestTy, "FC", BB->getTerminator()));
549554
return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator()));
@@ -552,7 +557,7 @@ struct CastModifier: public Modifier {
552557
// Int to fp.
553558
if (VTy->getScalarType()->isIntegerTy() &&
554559
DestTy->getScalarType()->isFloatingPointTy()) {
555-
if (Ran->Rand() & 1)
560+
if (getRandom() & 1)
556561
return PT->push_back(
557562
new SIToFPInst(V, DestTy, "FC", BB->getTerminator()));
558563
return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator()));
@@ -589,7 +594,7 @@ struct SelectModifier: public Modifier {
589594

590595
// If the value type is a vector, and we allow vector select, then in 50%
591596
// of the cases generate a vector select.
592-
if (Val0->getType()->isVectorTy() && (Ran->Rand() % 1)) {
597+
if (Val0->getType()->isVectorTy() && (getRandom() % 1)) {
593598
unsigned NumElem = cast<VectorType>(Val0->getType())->getNumElements();
594599
CondTy = VectorType::get(CondTy, NumElem);
595600
}
@@ -613,11 +618,11 @@ struct CmpModifier: public Modifier {
613618

614619
int op;
615620
if (fp) {
616-
op = Ran->Rand() %
621+
op = getRandom() %
617622
(CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) +
618623
CmpInst::FIRST_FCMP_PREDICATE;
619624
} else {
620-
op = Ran->Rand() %
625+
op = getRandom() %
621626
(CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) +
622627
CmpInst::FIRST_ICMP_PREDICATE;
623628
}

0 commit comments

Comments
 (0)
Please sign in to comment.