llvm.org GIT mirror llvm / 7c16260
[llvm-stress] Add getRandom() helper that was going to be part of D34157. NFCI. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306294 91177308-0d34-0410-b5e6-96231b3b80d8 Simon Pilgrim 2 years ago
1 changed file(s) with 32 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
167167 }
168168
169169 protected:
170 /// Return a random integer.
171 uint32_t getRandom() {
172 return Ran->Rand();
173 }
174
170175 /// Return a random value from the list of known values.
171176 Value *getRandomVal() {
172177 assert(PT->size());
173 return PT->at(Ran->Rand() % PT->size());
178 return PT->at(getRandom() % PT->size());
174179 }
175180
176181 Constant *getRandomConstant(Type *Tp) {
177182 if (Tp->isIntegerTy()) {
178 if (Ran->Rand() & 1)
183 if (getRandom() & 1)
179184 return ConstantInt::getAllOnesValue(Tp);
180185 return ConstantInt::getNullValue(Tp);
181186 } else if (Tp->isFloatingPointTy()) {
182 if (Ran->Rand() & 1)
187 if (getRandom() & 1)
183188 return ConstantFP::getAllOnesValue(Tp);
184189 return ConstantFP::getNullValue(Tp);
185190 }
188193
189194 /// Return a random value with a known type.
190195 Value *getRandomValue(Type *Tp) {
191 unsigned index = Ran->Rand();
196 unsigned index = getRandom();
192197 for (unsigned i=0; isize(); ++i) {
193198 Value *V = PT->at((index + i) % PT->size());
194199 if (V->getType() == Tp)
197202
198203 // If the requested type was not found, generate a constant value.
199204 if (Tp->isIntegerTy()) {
200 if (Ran->Rand() & 1)
205 if (getRandom() & 1)
201206 return ConstantInt::getAllOnesValue(Tp);
202207 return ConstantInt::getNullValue(Tp);
203208 } else if (Tp->isFloatingPointTy()) {
204 if (Ran->Rand() & 1)
209 if (getRandom() & 1)
205210 return ConstantFP::getAllOnesValue(Tp);
206211 return ConstantFP::getNullValue(Tp);
207212 } else if (Tp->isVectorTy()) {
221226
222227 /// Return a random value of any pointer type.
223228 Value *getRandomPointerValue() {
224 unsigned index = Ran->Rand();
229 unsigned index = getRandom();
225230 for (unsigned i=0; isize(); ++i) {
226231 Value *V = PT->at((index + i) % PT->size());
227232 if (V->getType()->isPointerTy())
232237
233238 /// Return a random value of any vector type.
234239 Value *getRandomVectorValue() {
235 unsigned index = Ran->Rand();
240 unsigned index = getRandom();
236241 for (unsigned i=0; isize(); ++i) {
237242 Value *V = PT->at((index + i) % PT->size());
238243 if (V->getType()->isVectorTy())
243248
244249 /// Pick a random type.
245250 Type *pickType() {
246 return (Ran->Rand() & 1 ? pickVectorType() : pickScalarType());
251 return (getRandom() & 1 ? pickVectorType() : pickScalarType());
247252 }
248253
249254 /// Pick a random pointer type.
257262 // Pick a random vector width in the range 2**0 to 2**4.
258263 // by adding two randoms we are generating a normal-like distribution
259264 // around 2**3.
260 unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3));
265 unsigned width = 1<<((getRandom() % 3) + (getRandom() % 3));
261266 Type *Ty;
262267
263268 // Vectors of x86mmx are illegal; keep trying till we get something else.
287292 AdditionalScalarTypes.begin(), AdditionalScalarTypes.end());
288293 }
289294
290 return ScalarTypes[Ran->Rand() % ScalarTypes.size()];
295 return ScalarTypes[getRandom() % ScalarTypes.size()];
291296 }
292297
293298 /// Basic block to populate
347352
348353 bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy();
349354 Instruction* Term = BB->getTerminator();
350 unsigned R = Ran->Rand() % (isFloat ? 7 : 13);
355 unsigned R = getRandom() % (isFloat ? 7 : 13);
351356 Instruction::BinaryOps Op;
352357
353358 switch (R) {
378383 Type *Ty = pickType();
379384
380385 if (Ty->isVectorTy()) {
381 switch (Ran->Rand() % 2) {
386 switch (getRandom() % 2) {
382387 case 0: if (Ty->getScalarType()->isIntegerTy())
383388 return PT->push_back(ConstantVector::getAllOnesValue(Ty));
384389 break;
397402 APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
398403 APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
399404
400 if (Ran->Rand() & 1)
405 if (getRandom() & 1)
401406 return PT->push_back(ConstantFP::getNullValue(Ty));
402407 return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
403408 }
404409
405410 if (Ty->isIntegerTy()) {
406 switch (Ran->Rand() % 7) {
411 switch (getRandom() % 7) {
407412 case 0:
408413 return PT->push_back(ConstantInt::get(
409414 Ty, APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits())));
415420 case 4:
416421 case 5:
417422 case 6:
418 PT->push_back(ConstantInt::get(Ty, Ran->Rand()));
423 PT->push_back(ConstantInt::get(Ty, getRandom()));
419424 }
420425 }
421426 }
440445 Value *Val0 = getRandomVectorValue();
441446 Value *V = ExtractElementInst::Create(Val0,
442447 ConstantInt::get(Type::getInt32Ty(BB->getContext()),
443 Ran->Rand() % cast(Val0->getType())->getNumElements()),
448 getRandom() % cast(Val0->getType())->getNumElements()),
444449 "E", BB->getTerminator());
445450 return PT->push_back(V);
446451 }
458463
459464 Type *I32 = Type::getInt32Ty(BB->getContext());
460465 for (unsigned i=0; i
461 Constant *CI = ConstantInt::get(I32, Ran->Rand() % (Width*2));
466 Constant *CI = ConstantInt::get(I32, getRandom() % (Width*2));
462467 // Pick some undef values.
463 if (!(Ran->Rand() % 5))
468 if (!(getRandom() % 5))
464469 CI = UndefValue::get(I32);
465470 Idxs.push_back(CI);
466471 }
483488
484489 Value *V = InsertElementInst::Create(Val0, Val1,
485490 ConstantInt::get(Type::getInt32Ty(BB->getContext()),
486 Ran->Rand() % cast(Val0->getType())->getNumElements()),
491 getRandom() % cast(Val0->getType())->getNumElements()),
487492 "I", BB->getTerminator());
488493 return PT->push_back(V);
489494 }
519524 unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits();
520525
521526 // Generate lots of bitcasts.
522 if ((Ran->Rand() & 1) && VSize == DestSize) {
527 if ((getRandom() & 1) && VSize == DestSize) {
523528 return PT->push_back(
524529 new BitCastInst(V, DestTy, "BC", BB->getTerminator()));
525530 }
532537 new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
533538 } else {
534539 assert(VSize < DestSize && "Different int types with the same size?");
535 if (Ran->Rand() & 1)
540 if (getRandom() & 1)
536541 return PT->push_back(
537542 new ZExtInst(V, DestTy, "ZE", BB->getTerminator()));
538543 return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator()));
542547 // Fp to int.
543548 if (VTy->getScalarType()->isFloatingPointTy() &&
544549 DestTy->getScalarType()->isIntegerTy()) {
545 if (Ran->Rand() & 1)
550 if (getRandom() & 1)
546551 return PT->push_back(
547552 new FPToSIInst(V, DestTy, "FC", BB->getTerminator()));
548553 return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator()));
551556 // Int to fp.
552557 if (VTy->getScalarType()->isIntegerTy() &&
553558 DestTy->getScalarType()->isFloatingPointTy()) {
554 if (Ran->Rand() & 1)
559 if (getRandom() & 1)
555560 return PT->push_back(
556561 new SIToFPInst(V, DestTy, "FC", BB->getTerminator()));
557562 return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator()));
588593
589594 // If the value type is a vector, and we allow vector select, then in 50%
590595 // of the cases generate a vector select.
591 if (Val0->getType()->isVectorTy() && (Ran->Rand() % 1)) {
596 if (Val0->getType()->isVectorTy() && (getRandom() % 1)) {
592597 unsigned NumElem = cast(Val0->getType())->getNumElements();
593598 CondTy = VectorType::get(CondTy, NumElem);
594599 }
612617
613618 int op;
614619 if (fp) {
615 op = Ran->Rand() %
620 op = getRandom() %
616621 (CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) +
617622 CmpInst::FIRST_FCMP_PREDICATE;
618623 } else {
619 op = Ran->Rand() %
624 op = getRandom() %
620625 (CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) +
621626 CmpInst::FIRST_ICMP_PREDICATE;
622627 }