1 //===-- llvm-stress.cpp - Generate random LL files to stress-test LLVM ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program is a utility that generates random .ll files to stress-test
11 // different components in LLVM.
13 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/Analysis/CallGraphSCCPass.h"
17 #include "llvm/Analysis/Verifier.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/PassNameParser.h"
26 #include "llvm/Support/PluginLoader.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/ToolOutputFile.h"
35 static cl::opt<unsigned> SeedCL("seed",
36 cl::desc("Seed used for randomness"), cl::init(0));
37 static cl::opt<unsigned> SizeCL("size",
38 cl::desc("The estimated size of the generated function (# of instrs)"),
40 static cl::opt<std::string>
41 OutputFilename("o", cl::desc("Override output filename"),
42 cl::value_desc("filename"));
44 static cl::opt<bool> GenHalfFloat("generate-half-float",
45 cl::desc("Generate half-length floating-point values"), cl::init(false));
46 static cl::opt<bool> GenX86FP80("generate-x86-fp80",
47 cl::desc("Generate 80-bit X86 floating-point values"), cl::init(false));
48 static cl::opt<bool> GenFP128("generate-fp128",
49 cl::desc("Generate 128-bit floating-point values"), cl::init(false));
50 static cl::opt<bool> GenPPCFP128("generate-ppc-fp128",
51 cl::desc("Generate 128-bit PPC floating-point values"), cl::init(false));
52 static cl::opt<bool> GenX86MMX("generate-x86-mmx",
53 cl::desc("Generate X86 MMX floating-point values"), cl::init(false));
55 /// A utility class to provide a pseudo-random number generator which is
56 /// the same across all platforms. This is somewhat close to the libc
57 /// implementation. Note: This is not a cryptographically secure pseudorandom
62 Random(unsigned _seed):Seed(_seed) {}
64 /// Return a random integer, up to a
65 /// maximum of 2**19 - 1.
67 uint32_t Val = Seed + 0x000b07a1;
68 Seed = (Val * 0x3c7c0ac1);
69 // Only lowest 19 bits are random-ish.
70 return Seed & 0x7ffff;
73 /// Return a random 32 bit integer.
75 uint32_t Val = Rand();
77 return Val | (Rand() << 16);
80 /// Return a random 64 bit integer.
82 uint64_t Val = Rand32();
83 return Val | (uint64_t(Rand32()) << 32);
86 /// Rand operator for STL algorithms.
87 ptrdiff_t operator()(ptrdiff_t y) {
95 /// Generate an empty function with a default argument list.
96 Function *GenEmptyFunction(Module *M) {
98 std::vector<Type*> ArgsTy;
99 // Define a few arguments
100 LLVMContext &Context = M->getContext();
101 ArgsTy.push_back(PointerType::get(IntegerType::getInt8Ty(Context), 0));
102 ArgsTy.push_back(PointerType::get(IntegerType::getInt32Ty(Context), 0));
103 ArgsTy.push_back(PointerType::get(IntegerType::getInt64Ty(Context), 0));
104 ArgsTy.push_back(IntegerType::getInt32Ty(Context));
105 ArgsTy.push_back(IntegerType::getInt64Ty(Context));
106 ArgsTy.push_back(IntegerType::getInt8Ty(Context));
108 FunctionType *FuncTy = FunctionType::get(Type::getVoidTy(Context), ArgsTy, 0);
109 // Pick a unique name to describe the input parameters
110 std::stringstream ss;
111 ss<<"autogen_SD"<<SeedCL;
112 Function *Func = Function::Create(FuncTy, GlobalValue::ExternalLinkage,
115 Func->setCallingConv(CallingConv::C);
119 /// A base class, implementing utilities needed for
120 /// modifying and adding new random instructions.
122 /// Used to store the randomly generated values.
123 typedef std::vector<Value*> PieceTable;
127 Modifier(BasicBlock *Block, PieceTable *PT, Random *R):
128 BB(Block),PT(PT),Ran(R),Context(BB->getContext()) {}
130 /// virtual D'tor to silence warnings.
133 /// Add a new instruction.
134 virtual void Act() = 0;
135 /// Add N new instructions,
136 virtual void ActN(unsigned n) {
137 for (unsigned i=0; i<n; ++i)
142 /// Return a random value from the list of known values.
143 Value *getRandomVal() {
145 return PT->at(Ran->Rand() % PT->size());
148 Constant *getRandomConstant(Type *Tp) {
149 if (Tp->isIntegerTy()) {
151 return ConstantInt::getAllOnesValue(Tp);
152 return ConstantInt::getNullValue(Tp);
153 } else if (Tp->isFloatingPointTy()) {
155 return ConstantFP::getAllOnesValue(Tp);
156 return ConstantFP::getNullValue(Tp);
158 return UndefValue::get(Tp);
161 /// Return a random value with a known type.
162 Value *getRandomValue(Type *Tp) {
163 unsigned index = Ran->Rand();
164 for (unsigned i=0; i<PT->size(); ++i) {
165 Value *V = PT->at((index + i) % PT->size());
166 if (V->getType() == Tp)
170 // If the requested type was not found, generate a constant value.
171 if (Tp->isIntegerTy()) {
173 return ConstantInt::getAllOnesValue(Tp);
174 return ConstantInt::getNullValue(Tp);
175 } else if (Tp->isFloatingPointTy()) {
177 return ConstantFP::getAllOnesValue(Tp);
178 return ConstantFP::getNullValue(Tp);
179 } else if (Tp->isVectorTy()) {
180 VectorType *VTp = cast<VectorType>(Tp);
182 std::vector<Constant*> TempValues;
183 TempValues.reserve(VTp->getNumElements());
184 for (unsigned i = 0; i < VTp->getNumElements(); ++i)
185 TempValues.push_back(getRandomConstant(VTp->getScalarType()));
187 ArrayRef<Constant*> VectorValue(TempValues);
188 return ConstantVector::get(VectorValue);
191 return UndefValue::get(Tp);
194 /// Return a random value of any pointer type.
195 Value *getRandomPointerValue() {
196 unsigned index = Ran->Rand();
197 for (unsigned i=0; i<PT->size(); ++i) {
198 Value *V = PT->at((index + i) % PT->size());
199 if (V->getType()->isPointerTy())
202 return UndefValue::get(pickPointerType());
205 /// Return a random value of any vector type.
206 Value *getRandomVectorValue() {
207 unsigned index = Ran->Rand();
208 for (unsigned i=0; i<PT->size(); ++i) {
209 Value *V = PT->at((index + i) % PT->size());
210 if (V->getType()->isVectorTy())
213 return UndefValue::get(pickVectorType());
216 /// Pick a random type.
218 return (Ran->Rand() & 1 ? pickVectorType() : pickScalarType());
221 /// Pick a random pointer type.
222 Type *pickPointerType() {
223 Type *Ty = pickType();
224 return PointerType::get(Ty, 0);
227 /// Pick a random vector type.
228 Type *pickVectorType(unsigned len = (unsigned)-1) {
229 // Pick a random vector width in the range 2**0 to 2**4.
230 // by adding two randoms we are generating a normal-like distribution
232 unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3));
235 // Vectors of x86mmx are illegal; keep trying till we get something else.
237 Ty = pickScalarType();
238 } while (Ty->isX86_MMXTy());
240 if (len != (unsigned)-1)
242 return VectorType::get(Ty, width);
245 /// Pick a random scalar type.
246 Type *pickScalarType() {
249 switch (Ran->Rand() % 30) {
250 case 0: t = Type::getInt1Ty(Context); break;
251 case 1: t = Type::getInt8Ty(Context); break;
252 case 2: t = Type::getInt16Ty(Context); break;
254 case 5: t = Type::getFloatTy(Context); break;
256 case 8: t = Type::getDoubleTy(Context); break;
258 case 11: t = Type::getInt32Ty(Context); break;
260 case 14: t = Type::getInt64Ty(Context); break;
262 case 17: if (GenHalfFloat) t = Type::getHalfTy(Context); break;
264 case 20: if (GenX86FP80) t = Type::getX86_FP80Ty(Context); break;
266 case 23: if (GenFP128) t = Type::getFP128Ty(Context); break;
268 case 26: if (GenPPCFP128) t = Type::getPPC_FP128Ty(Context); break;
270 case 29: if (GenX86MMX) t = Type::getX86_MMXTy(Context); break;
271 default: llvm_unreachable("Invalid scalar value");
278 /// Basic block to populate
282 /// Random number generator
285 LLVMContext &Context;
288 Modifier::~Modifier() {}
290 struct LoadModifier: public Modifier {
291 LoadModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
292 virtual ~LoadModifier();
294 // Try to use predefined pointers. If non exist, use undef pointer value;
295 Value *Ptr = getRandomPointerValue();
296 Value *V = new LoadInst(Ptr, "L", BB->getTerminator());
301 LoadModifier::~LoadModifier() {}
303 struct StoreModifier: public Modifier {
304 StoreModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
305 virtual ~StoreModifier();
307 // Try to use predefined pointers. If non exist, use undef pointer value;
308 Value *Ptr = getRandomPointerValue();
309 Type *Tp = Ptr->getType();
310 Value *Val = getRandomValue(Tp->getContainedType(0));
311 Type *ValTy = Val->getType();
313 // Do not store vectors of i1s because they are unsupported
315 if (ValTy->isVectorTy() && ValTy->getScalarSizeInBits() == 1)
318 new StoreInst(Val, Ptr, BB->getTerminator());
322 StoreModifier::~StoreModifier() {}
324 struct BinModifier: public Modifier {
325 BinModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
326 virtual ~BinModifier();
329 Value *Val0 = getRandomVal();
330 Value *Val1 = getRandomValue(Val0->getType());
332 // Don't handle pointer types.
333 if (Val0->getType()->isPointerTy() ||
334 Val1->getType()->isPointerTy())
337 // Don't handle i1 types.
338 if (Val0->getType()->getScalarSizeInBits() == 1)
342 bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy();
343 Instruction* Term = BB->getTerminator();
344 unsigned R = Ran->Rand() % (isFloat ? 7 : 13);
345 Instruction::BinaryOps Op;
348 default: llvm_unreachable("Invalid BinOp");
349 case 0:{Op = (isFloat?Instruction::FAdd : Instruction::Add); break; }
350 case 1:{Op = (isFloat?Instruction::FSub : Instruction::Sub); break; }
351 case 2:{Op = (isFloat?Instruction::FMul : Instruction::Mul); break; }
352 case 3:{Op = (isFloat?Instruction::FDiv : Instruction::SDiv); break; }
353 case 4:{Op = (isFloat?Instruction::FDiv : Instruction::UDiv); break; }
354 case 5:{Op = (isFloat?Instruction::FRem : Instruction::SRem); break; }
355 case 6:{Op = (isFloat?Instruction::FRem : Instruction::URem); break; }
356 case 7: {Op = Instruction::Shl; break; }
357 case 8: {Op = Instruction::LShr; break; }
358 case 9: {Op = Instruction::AShr; break; }
359 case 10:{Op = Instruction::And; break; }
360 case 11:{Op = Instruction::Or; break; }
361 case 12:{Op = Instruction::Xor; break; }
364 PT->push_back(BinaryOperator::Create(Op, Val0, Val1, "B", Term));
368 BinModifier::~BinModifier() {}
370 /// Generate constant values.
371 struct ConstModifier: public Modifier {
372 ConstModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
373 virtual ~ConstModifier();
376 Type *Ty = pickType();
378 if (Ty->isVectorTy()) {
379 switch (Ran->Rand() % 2) {
380 case 0: if (Ty->getScalarType()->isIntegerTy())
381 return PT->push_back(ConstantVector::getAllOnesValue(Ty));
382 case 1: if (Ty->getScalarType()->isIntegerTy())
383 return PT->push_back(ConstantVector::getNullValue(Ty));
387 if (Ty->isFloatingPointTy()) {
388 // Generate 128 random bits, the size of the (currently)
389 // largest floating-point types.
390 uint64_t RandomBits[2];
391 for (unsigned i = 0; i < 2; ++i)
392 RandomBits[i] = Ran->Rand64();
394 APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
395 APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
398 return PT->push_back(ConstantFP::getNullValue(Ty));
399 return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
402 if (Ty->isIntegerTy()) {
403 switch (Ran->Rand() % 7) {
404 case 0: if (Ty->isIntegerTy())
405 return PT->push_back(ConstantInt::get(Ty,
406 APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits())));
407 case 1: if (Ty->isIntegerTy())
408 return PT->push_back(ConstantInt::get(Ty,
409 APInt::getNullValue(Ty->getPrimitiveSizeInBits())));
410 case 2: case 3: case 4: case 5:
411 case 6: if (Ty->isIntegerTy())
412 PT->push_back(ConstantInt::get(Ty, Ran->Rand()));
419 ConstModifier::~ConstModifier() {}
421 struct AllocaModifier: public Modifier {
422 AllocaModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R){}
423 virtual ~AllocaModifier();
426 Type *Tp = pickType();
427 PT->push_back(new AllocaInst(Tp, "A", BB->getFirstNonPHI()));
431 AllocaModifier::~AllocaModifier() {}
433 struct ExtractElementModifier: public Modifier {
434 ExtractElementModifier(BasicBlock *BB, PieceTable *PT, Random *R):
435 Modifier(BB, PT, R) {}
436 virtual ~ExtractElementModifier();
439 Value *Val0 = getRandomVectorValue();
440 Value *V = ExtractElementInst::Create(Val0,
441 ConstantInt::get(Type::getInt32Ty(BB->getContext()),
442 Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
443 "E", BB->getTerminator());
444 return PT->push_back(V);
448 ExtractElementModifier::~ExtractElementModifier() {}
450 struct ShuffModifier: public Modifier {
451 ShuffModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
452 virtual ~ShuffModifier();
456 Value *Val0 = getRandomVectorValue();
457 Value *Val1 = getRandomValue(Val0->getType());
459 unsigned Width = cast<VectorType>(Val0->getType())->getNumElements();
460 std::vector<Constant*> Idxs;
462 Type *I32 = Type::getInt32Ty(BB->getContext());
463 for (unsigned i=0; i<Width; ++i) {
464 Constant *CI = ConstantInt::get(I32, Ran->Rand() % (Width*2));
465 // Pick some undef values.
466 if (!(Ran->Rand() % 5))
467 CI = UndefValue::get(I32);
471 Constant *Mask = ConstantVector::get(Idxs);
473 Value *V = new ShuffleVectorInst(Val0, Val1, Mask, "Shuff",
474 BB->getTerminator());
479 ShuffModifier::~ShuffModifier() {}
481 struct InsertElementModifier: public Modifier {
482 InsertElementModifier(BasicBlock *BB, PieceTable *PT, Random *R):
483 Modifier(BB, PT, R) {}
484 virtual ~InsertElementModifier();
487 Value *Val0 = getRandomVectorValue();
488 Value *Val1 = getRandomValue(Val0->getType()->getScalarType());
490 Value *V = InsertElementInst::Create(Val0, Val1,
491 ConstantInt::get(Type::getInt32Ty(BB->getContext()),
492 Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
493 "I", BB->getTerminator());
494 return PT->push_back(V);
499 InsertElementModifier::~InsertElementModifier() {}
501 struct CastModifier: public Modifier {
502 CastModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
503 virtual ~CastModifier();
507 Value *V = getRandomVal();
508 Type *VTy = V->getType();
509 Type *DestTy = pickScalarType();
511 // Handle vector casts vectors.
512 if (VTy->isVectorTy()) {
513 VectorType *VecTy = cast<VectorType>(VTy);
514 DestTy = pickVectorType(VecTy->getNumElements());
518 if (VTy == DestTy) return;
521 if (VTy->isPointerTy()) {
522 if (!DestTy->isPointerTy())
523 DestTy = PointerType::get(DestTy, 0);
524 return PT->push_back(
525 new BitCastInst(V, DestTy, "PC", BB->getTerminator()));
528 unsigned VSize = VTy->getScalarType()->getPrimitiveSizeInBits();
529 unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits();
531 // Generate lots of bitcasts.
532 if ((Ran->Rand() & 1) && VSize == DestSize) {
533 return PT->push_back(
534 new BitCastInst(V, DestTy, "BC", BB->getTerminator()));
537 // Both types are integers:
538 if (VTy->getScalarType()->isIntegerTy() &&
539 DestTy->getScalarType()->isIntegerTy()) {
540 if (VSize > DestSize) {
541 return PT->push_back(
542 new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
544 assert(VSize < DestSize && "Different int types with the same size?");
546 return PT->push_back(
547 new ZExtInst(V, DestTy, "ZE", BB->getTerminator()));
548 return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator()));
553 if (VTy->getScalarType()->isFloatingPointTy() &&
554 DestTy->getScalarType()->isIntegerTy()) {
556 return PT->push_back(
557 new FPToSIInst(V, DestTy, "FC", BB->getTerminator()));
558 return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator()));
562 if (VTy->getScalarType()->isIntegerTy() &&
563 DestTy->getScalarType()->isFloatingPointTy()) {
565 return PT->push_back(
566 new SIToFPInst(V, DestTy, "FC", BB->getTerminator()));
567 return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator()));
572 if (VTy->getScalarType()->isFloatingPointTy() &&
573 DestTy->getScalarType()->isFloatingPointTy()) {
574 if (VSize > DestSize) {
575 return PT->push_back(
576 new FPTruncInst(V, DestTy, "Tr", BB->getTerminator()));
577 } else if (VSize < DestSize) {
578 return PT->push_back(
579 new FPExtInst(V, DestTy, "ZE", BB->getTerminator()));
581 // If VSize == DestSize, then the two types must be fp128 and ppc_fp128,
582 // for which there is no defined conversion. So do nothing.
588 CastModifier::~CastModifier() {}
590 struct SelectModifier: public Modifier {
591 SelectModifier(BasicBlock *BB, PieceTable *PT, Random *R):
592 Modifier(BB, PT, R) {}
593 virtual ~SelectModifier();
596 // Try a bunch of different select configuration until a valid one is found.
597 Value *Val0 = getRandomVal();
598 Value *Val1 = getRandomValue(Val0->getType());
600 Type *CondTy = Type::getInt1Ty(Context);
602 // If the value type is a vector, and we allow vector select, then in 50%
603 // of the cases generate a vector select.
604 if (Val0->getType()->isVectorTy() && (Ran->Rand() % 1)) {
605 unsigned NumElem = cast<VectorType>(Val0->getType())->getNumElements();
606 CondTy = VectorType::get(CondTy, NumElem);
609 Value *Cond = getRandomValue(CondTy);
610 Value *V = SelectInst::Create(Cond, Val0, Val1, "Sl", BB->getTerminator());
611 return PT->push_back(V);
615 SelectModifier::~SelectModifier() {}
617 struct CmpModifier: public Modifier {
618 CmpModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
619 virtual ~CmpModifier();
623 Value *Val0 = getRandomVal();
624 Value *Val1 = getRandomValue(Val0->getType());
626 if (Val0->getType()->isPointerTy()) return;
627 bool fp = Val0->getType()->getScalarType()->isFloatingPointTy();
632 (CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) +
633 CmpInst::FIRST_FCMP_PREDICATE;
636 (CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) +
637 CmpInst::FIRST_ICMP_PREDICATE;
640 Value *V = CmpInst::Create(fp ? Instruction::FCmp : Instruction::ICmp,
641 op, Val0, Val1, "Cmp", BB->getTerminator());
642 return PT->push_back(V);
646 CmpModifier::~CmpModifier() {}
648 void FillFunction(Function *F, Random &R) {
649 // Create a legal entry block.
650 BasicBlock *BB = BasicBlock::Create(F->getContext(), "BB", F);
651 ReturnInst::Create(F->getContext(), BB);
653 // Create the value table.
654 Modifier::PieceTable PT;
656 // Consider arguments as legal values.
657 for (Function::arg_iterator it = F->arg_begin(), e = F->arg_end();
661 // List of modifiers which add new random instructions.
662 std::vector<Modifier*> Modifiers;
663 OwningPtr<Modifier> LM(new LoadModifier(BB, &PT, &R));
664 OwningPtr<Modifier> SM(new StoreModifier(BB, &PT, &R));
665 OwningPtr<Modifier> EE(new ExtractElementModifier(BB, &PT, &R));
666 OwningPtr<Modifier> SHM(new ShuffModifier(BB, &PT, &R));
667 OwningPtr<Modifier> IE(new InsertElementModifier(BB, &PT, &R));
668 OwningPtr<Modifier> BM(new BinModifier(BB, &PT, &R));
669 OwningPtr<Modifier> CM(new CastModifier(BB, &PT, &R));
670 OwningPtr<Modifier> SLM(new SelectModifier(BB, &PT, &R));
671 OwningPtr<Modifier> PM(new CmpModifier(BB, &PT, &R));
672 Modifiers.push_back(LM.get());
673 Modifiers.push_back(SM.get());
674 Modifiers.push_back(EE.get());
675 Modifiers.push_back(SHM.get());
676 Modifiers.push_back(IE.get());
677 Modifiers.push_back(BM.get());
678 Modifiers.push_back(CM.get());
679 Modifiers.push_back(SLM.get());
680 Modifiers.push_back(PM.get());
682 // Generate the random instructions
683 AllocaModifier AM(BB, &PT, &R); AM.ActN(5); // Throw in a few allocas
684 ConstModifier COM(BB, &PT, &R); COM.ActN(40); // Throw in a few constants
686 for (unsigned i=0; i< SizeCL / Modifiers.size(); ++i)
687 for (std::vector<Modifier*>::iterator it = Modifiers.begin(),
688 e = Modifiers.end(); it != e; ++it) {
692 SM->ActN(5); // Throw in a few stores.
695 void IntroduceControlFlow(Function *F, Random &R) {
696 std::vector<Instruction*> BoolInst;
697 for (BasicBlock::iterator it = F->begin()->begin(),
698 e = F->begin()->end(); it != e; ++it) {
699 if (it->getType() == IntegerType::getInt1Ty(F->getContext()))
700 BoolInst.push_back(it);
703 std::random_shuffle(BoolInst.begin(), BoolInst.end(), R);
705 for (std::vector<Instruction*>::iterator it = BoolInst.begin(),
706 e = BoolInst.end(); it != e; ++it) {
707 Instruction *Instr = *it;
708 BasicBlock *Curr = Instr->getParent();
709 BasicBlock::iterator Loc= Instr;
710 BasicBlock *Next = Curr->splitBasicBlock(Loc, "CF");
711 Instr->moveBefore(Curr->getTerminator());
712 if (Curr != &F->getEntryBlock()) {
713 BranchInst::Create(Curr, Next, Instr, Curr->getTerminator());
714 Curr->getTerminator()->eraseFromParent();
719 int main(int argc, char **argv) {
720 // Init LLVM, call llvm_shutdown() on exit, parse args, etc.
721 llvm::PrettyStackTraceProgram X(argc, argv);
722 cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n");
725 OwningPtr<Module> M(new Module("/tmp/autogen.bc", getGlobalContext()));
726 Function *F = GenEmptyFunction(M.get());
728 // Pick an initial seed value
730 // Generate lots of random instructions inside a single basic block.
732 // Break the basic block into many loops.
733 IntroduceControlFlow(F, R);
735 // Figure out what stream we are supposed to write to...
736 OwningPtr<tool_output_file> Out;
737 // Default to standard output.
738 if (OutputFilename.empty())
739 OutputFilename = "-";
741 std::string ErrorInfo;
742 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
744 if (!ErrorInfo.empty()) {
745 errs() << ErrorInfo << '\n';
750 Passes.add(createVerifierPass());
751 Passes.add(createPrintModulePass(&Out->os()));
752 Passes.run(*M.get());