@@ -168,19 +168,24 @@ struct Modifier {
168
168
}
169
169
170
170
protected:
171
+ // / Return a random integer.
172
+ uint32_t getRandom () {
173
+ return Ran->Rand ();
174
+ }
175
+
171
176
// / Return a random value from the list of known values.
172
177
Value *getRandomVal () {
173
178
assert (PT->size ());
174
- return PT->at (Ran-> Rand () % PT->size ());
179
+ return PT->at (getRandom () % PT->size ());
175
180
}
176
181
177
182
Constant *getRandomConstant (Type *Tp) {
178
183
if (Tp->isIntegerTy ()) {
179
- if (Ran-> Rand () & 1 )
184
+ if (getRandom () & 1 )
180
185
return ConstantInt::getAllOnesValue (Tp);
181
186
return ConstantInt::getNullValue (Tp);
182
187
} else if (Tp->isFloatingPointTy ()) {
183
- if (Ran-> Rand () & 1 )
188
+ if (getRandom () & 1 )
184
189
return ConstantFP::getAllOnesValue (Tp);
185
190
return ConstantFP::getNullValue (Tp);
186
191
}
@@ -189,7 +194,7 @@ struct Modifier {
189
194
190
195
// / Return a random value with a known type.
191
196
Value *getRandomValue (Type *Tp) {
192
- unsigned index = Ran-> Rand ();
197
+ unsigned index = getRandom ();
193
198
for (unsigned i=0 ; i<PT->size (); ++i) {
194
199
Value *V = PT->at ((index + i) % PT->size ());
195
200
if (V->getType () == Tp)
@@ -198,11 +203,11 @@ struct Modifier {
198
203
199
204
// If the requested type was not found, generate a constant value.
200
205
if (Tp->isIntegerTy ()) {
201
- if (Ran-> Rand () & 1 )
206
+ if (getRandom () & 1 )
202
207
return ConstantInt::getAllOnesValue (Tp);
203
208
return ConstantInt::getNullValue (Tp);
204
209
} else if (Tp->isFloatingPointTy ()) {
205
- if (Ran-> Rand () & 1 )
210
+ if (getRandom () & 1 )
206
211
return ConstantFP::getAllOnesValue (Tp);
207
212
return ConstantFP::getNullValue (Tp);
208
213
} else if (Tp->isVectorTy ()) {
@@ -222,7 +227,7 @@ struct Modifier {
222
227
223
228
// / Return a random value of any pointer type.
224
229
Value *getRandomPointerValue () {
225
- unsigned index = Ran-> Rand ();
230
+ unsigned index = getRandom ();
226
231
for (unsigned i=0 ; i<PT->size (); ++i) {
227
232
Value *V = PT->at ((index + i) % PT->size ());
228
233
if (V->getType ()->isPointerTy ())
@@ -233,7 +238,7 @@ struct Modifier {
233
238
234
239
// / Return a random value of any vector type.
235
240
Value *getRandomVectorValue () {
236
- unsigned index = Ran-> Rand ();
241
+ unsigned index = getRandom ();
237
242
for (unsigned i=0 ; i<PT->size (); ++i) {
238
243
Value *V = PT->at ((index + i) % PT->size ());
239
244
if (V->getType ()->isVectorTy ())
@@ -244,7 +249,7 @@ struct Modifier {
244
249
245
250
// / Pick a random type.
246
251
Type *pickType () {
247
- return (Ran-> Rand () & 1 ? pickVectorType () : pickScalarType ());
252
+ return (getRandom () & 1 ? pickVectorType () : pickScalarType ());
248
253
}
249
254
250
255
// / Pick a random pointer type.
@@ -258,7 +263,7 @@ struct Modifier {
258
263
// Pick a random vector width in the range 2**0 to 2**4.
259
264
// by adding two randoms we are generating a normal-like distribution
260
265
// around 2**3.
261
- unsigned width = 1 <<((Ran-> Rand () % 3 ) + (Ran-> Rand () % 3 ));
266
+ unsigned width = 1 <<((getRandom () % 3 ) + (getRandom () % 3 ));
262
267
Type *Ty;
263
268
264
269
// Vectors of x86mmx are illegal; keep trying till we get something else.
@@ -288,7 +293,7 @@ struct Modifier {
288
293
AdditionalScalarTypes.begin (), AdditionalScalarTypes.end ());
289
294
}
290
295
291
- return ScalarTypes[Ran-> Rand () % ScalarTypes.size ()];
296
+ return ScalarTypes[getRandom () % ScalarTypes.size ()];
292
297
}
293
298
294
299
// / Basic block to populate
@@ -348,7 +353,7 @@ struct BinModifier: public Modifier {
348
353
349
354
bool isFloat = Val0->getType ()->getScalarType ()->isFloatingPointTy ();
350
355
Instruction* Term = BB->getTerminator ();
351
- unsigned R = Ran-> Rand () % (isFloat ? 7 : 13 );
356
+ unsigned R = getRandom () % (isFloat ? 7 : 13 );
352
357
Instruction::BinaryOps Op;
353
358
354
359
switch (R) {
@@ -379,7 +384,7 @@ struct ConstModifier: public Modifier {
379
384
Type *Ty = pickType ();
380
385
381
386
if (Ty->isVectorTy ()) {
382
- switch (Ran-> Rand () % 2 ) {
387
+ switch (getRandom () % 2 ) {
383
388
case 0 : if (Ty->getScalarType ()->isIntegerTy ())
384
389
return PT->push_back (ConstantVector::getAllOnesValue (Ty));
385
390
break ;
@@ -398,13 +403,13 @@ struct ConstModifier: public Modifier {
398
403
APInt RandomInt (Ty->getPrimitiveSizeInBits (), makeArrayRef (RandomBits));
399
404
APFloat RandomFloat (Ty->getFltSemantics (), RandomInt);
400
405
401
- if (Ran-> Rand () & 1 )
406
+ if (getRandom () & 1 )
402
407
return PT->push_back (ConstantFP::getNullValue (Ty));
403
408
return PT->push_back (ConstantFP::get (Ty->getContext (), RandomFloat));
404
409
}
405
410
406
411
if (Ty->isIntegerTy ()) {
407
- switch (Ran-> Rand () % 7 ) {
412
+ switch (getRandom () % 7 ) {
408
413
case 0 :
409
414
return PT->push_back (ConstantInt::get (
410
415
Ty, APInt::getAllOnesValue (Ty->getPrimitiveSizeInBits ())));
@@ -416,7 +421,7 @@ struct ConstModifier: public Modifier {
416
421
case 4 :
417
422
case 5 :
418
423
case 6 :
419
- PT->push_back (ConstantInt::get (Ty, Ran-> Rand ()));
424
+ PT->push_back (ConstantInt::get (Ty, getRandom ()));
420
425
}
421
426
}
422
427
}
@@ -441,7 +446,7 @@ struct ExtractElementModifier: public Modifier {
441
446
Value *Val0 = getRandomVectorValue ();
442
447
Value *V = ExtractElementInst::Create (Val0,
443
448
ConstantInt::get (Type::getInt32Ty (BB->getContext ()),
444
- Ran-> Rand () % cast<VectorType>(Val0->getType ())->getNumElements ()),
449
+ getRandom () % cast<VectorType>(Val0->getType ())->getNumElements ()),
445
450
" E" , BB->getTerminator ());
446
451
return PT->push_back (V);
447
452
}
@@ -459,9 +464,9 @@ struct ShuffModifier: public Modifier {
459
464
460
465
Type *I32 = Type::getInt32Ty (BB->getContext ());
461
466
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 ));
463
468
// Pick some undef values.
464
- if (!(Ran-> Rand () % 5 ))
469
+ if (!(getRandom () % 5 ))
465
470
CI = UndefValue::get (I32);
466
471
Idxs.push_back (CI);
467
472
}
@@ -484,7 +489,7 @@ struct InsertElementModifier: public Modifier {
484
489
485
490
Value *V = InsertElementInst::Create (Val0, Val1,
486
491
ConstantInt::get (Type::getInt32Ty (BB->getContext ()),
487
- Ran-> Rand () % cast<VectorType>(Val0->getType ())->getNumElements ()),
492
+ getRandom () % cast<VectorType>(Val0->getType ())->getNumElements ()),
488
493
" I" , BB->getTerminator ());
489
494
return PT->push_back (V);
490
495
}
@@ -520,7 +525,7 @@ struct CastModifier: public Modifier {
520
525
unsigned DestSize = DestTy->getScalarType ()->getPrimitiveSizeInBits ();
521
526
522
527
// Generate lots of bitcasts.
523
- if ((Ran-> Rand () & 1 ) && VSize == DestSize) {
528
+ if ((getRandom () & 1 ) && VSize == DestSize) {
524
529
return PT->push_back (
525
530
new BitCastInst (V, DestTy, " BC" , BB->getTerminator ()));
526
531
}
@@ -533,7 +538,7 @@ struct CastModifier: public Modifier {
533
538
new TruncInst (V, DestTy, " Tr" , BB->getTerminator ()));
534
539
} else {
535
540
assert (VSize < DestSize && " Different int types with the same size?" );
536
- if (Ran-> Rand () & 1 )
541
+ if (getRandom () & 1 )
537
542
return PT->push_back (
538
543
new ZExtInst (V, DestTy, " ZE" , BB->getTerminator ()));
539
544
return PT->push_back (new SExtInst (V, DestTy, " Se" , BB->getTerminator ()));
@@ -543,7 +548,7 @@ struct CastModifier: public Modifier {
543
548
// Fp to int.
544
549
if (VTy->getScalarType ()->isFloatingPointTy () &&
545
550
DestTy->getScalarType ()->isIntegerTy ()) {
546
- if (Ran-> Rand () & 1 )
551
+ if (getRandom () & 1 )
547
552
return PT->push_back (
548
553
new FPToSIInst (V, DestTy, " FC" , BB->getTerminator ()));
549
554
return PT->push_back (new FPToUIInst (V, DestTy, " FC" , BB->getTerminator ()));
@@ -552,7 +557,7 @@ struct CastModifier: public Modifier {
552
557
// Int to fp.
553
558
if (VTy->getScalarType ()->isIntegerTy () &&
554
559
DestTy->getScalarType ()->isFloatingPointTy ()) {
555
- if (Ran-> Rand () & 1 )
560
+ if (getRandom () & 1 )
556
561
return PT->push_back (
557
562
new SIToFPInst (V, DestTy, " FC" , BB->getTerminator ()));
558
563
return PT->push_back (new UIToFPInst (V, DestTy, " FC" , BB->getTerminator ()));
@@ -589,7 +594,7 @@ struct SelectModifier: public Modifier {
589
594
590
595
// If the value type is a vector, and we allow vector select, then in 50%
591
596
// of the cases generate a vector select.
592
- if (Val0->getType ()->isVectorTy () && (Ran-> Rand () % 1 )) {
597
+ if (Val0->getType ()->isVectorTy () && (getRandom () % 1 )) {
593
598
unsigned NumElem = cast<VectorType>(Val0->getType ())->getNumElements ();
594
599
CondTy = VectorType::get (CondTy, NumElem);
595
600
}
@@ -613,11 +618,11 @@ struct CmpModifier: public Modifier {
613
618
614
619
int op;
615
620
if (fp) {
616
- op = Ran-> Rand () %
621
+ op = getRandom () %
617
622
(CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) +
618
623
CmpInst::FIRST_FCMP_PREDICATE;
619
624
} else {
620
- op = Ran-> Rand () %
625
+ op = getRandom () %
621
626
(CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) +
622
627
CmpInst::FIRST_ICMP_PREDICATE;
623
628
}
0 commit comments