blob: 727d03f9d6ea4325d256beb03305a71f25a87c93 [file] [log] [blame]
Daniel Dunbarbeb34252012-02-29 00:20:33 +00001//===-- llvm-stress.cpp - Generate random LL files to stress-test LLVM ----===//
Nadav Rotem78bda892012-02-26 08:35:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program is a utility that generates random .ll files to stress-test
11// different components in LLVM.
12//
13//===----------------------------------------------------------------------===//
Ahmed Charles56440fd2014-03-06 05:51:42 +000014
Chandler Carruth839a98e2013-01-07 15:26:48 +000015#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000017#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Instruction.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000019#include "llvm/IR/LLVMContext.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000020#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000022#include "llvm/IR/Verifier.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000023#include "llvm/IR/LegacyPassManager.h"
Nadav Rotem78bda892012-02-26 08:35:53 +000024#include "llvm/Support/Debug.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000025#include "llvm/Support/FileSystem.h"
Nadav Rotem78bda892012-02-26 08:35:53 +000026#include "llvm/Support/ManagedStatic.h"
27#include "llvm/Support/PluginLoader.h"
28#include "llvm/Support/PrettyStackTrace.h"
29#include "llvm/Support/ToolOutputFile.h"
Nadav Rotem78bda892012-02-26 08:35:53 +000030#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include <set>
32#include <sstream>
33#include <vector>
Nadav Rotem78bda892012-02-26 08:35:53 +000034using namespace llvm;
35
36static cl::opt<unsigned> SeedCL("seed",
37 cl::desc("Seed used for randomness"), cl::init(0));
38static cl::opt<unsigned> SizeCL("size",
39 cl::desc("The estimated size of the generated function (# of instrs)"),
40 cl::init(100));
41static cl::opt<std::string>
42OutputFilename("o", cl::desc("Override output filename"),
43 cl::value_desc("filename"));
44
Hal Finkelc9474122012-02-27 23:59:33 +000045static cl::opt<bool> GenHalfFloat("generate-half-float",
46 cl::desc("Generate half-length floating-point values"), cl::init(false));
47static cl::opt<bool> GenX86FP80("generate-x86-fp80",
48 cl::desc("Generate 80-bit X86 floating-point values"), cl::init(false));
49static cl::opt<bool> GenFP128("generate-fp128",
50 cl::desc("Generate 128-bit floating-point values"), cl::init(false));
51static cl::opt<bool> GenPPCFP128("generate-ppc-fp128",
52 cl::desc("Generate 128-bit PPC floating-point values"), cl::init(false));
53static cl::opt<bool> GenX86MMX("generate-x86-mmx",
54 cl::desc("Generate X86 MMX floating-point values"), cl::init(false));
55
Juergen Ributzka05c5a932013-11-19 03:08:35 +000056namespace {
Nadav Rotem78bda892012-02-26 08:35:53 +000057/// A utility class to provide a pseudo-random number generator which is
58/// the same across all platforms. This is somewhat close to the libc
59/// implementation. Note: This is not a cryptographically secure pseudorandom
60/// number generator.
61class Random {
62public:
63 /// C'tor
64 Random(unsigned _seed):Seed(_seed) {}
Dylan Noblesmith68f310d2012-04-10 22:44:51 +000065
66 /// Return a random integer, up to a
67 /// maximum of 2**19 - 1.
68 uint32_t Rand() {
69 uint32_t Val = Seed + 0x000b07a1;
Nadav Rotem78bda892012-02-26 08:35:53 +000070 Seed = (Val * 0x3c7c0ac1);
71 // Only lowest 19 bits are random-ish.
72 return Seed & 0x7ffff;
73 }
74
Dylan Noblesmith68f310d2012-04-10 22:44:51 +000075 /// Return a random 32 bit integer.
76 uint32_t Rand32() {
77 uint32_t Val = Rand();
78 Val &= 0xffff;
79 return Val | (Rand() << 16);
80 }
81
82 /// Return a random 64 bit integer.
83 uint64_t Rand64() {
84 uint64_t Val = Rand32();
85 return Val | (uint64_t(Rand32()) << 32);
86 }
Nadav Rotem0fb74082012-06-21 08:58:15 +000087
88 /// Rand operator for STL algorithms.
89 ptrdiff_t operator()(ptrdiff_t y) {
90 return Rand64() % y;
91 }
92
Nadav Rotem78bda892012-02-26 08:35:53 +000093private:
94 unsigned Seed;
95};
96
97/// Generate an empty function with a default argument list.
98Function *GenEmptyFunction(Module *M) {
Nadav Rotem78bda892012-02-26 08:35:53 +000099 // Define a few arguments
100 LLVMContext &Context = M->getContext();
Pawel Bylica23663472015-06-24 11:49:44 +0000101 Type* ArgsTy[] = {
102 Type::getInt8PtrTy(Context),
103 Type::getInt32PtrTy(Context),
104 Type::getInt64PtrTy(Context),
105 Type::getInt32Ty(Context),
106 Type::getInt64Ty(Context),
107 Type::getInt8Ty(Context)
108 };
Nadav Rotem78bda892012-02-26 08:35:53 +0000109
Pawel Bylica23663472015-06-24 11:49:44 +0000110 auto *FuncTy = FunctionType::get(Type::getVoidTy(Context), ArgsTy, false);
Nadav Rotem78bda892012-02-26 08:35:53 +0000111 // Pick a unique name to describe the input parameters
Pawel Bylica23663472015-06-24 11:49:44 +0000112 Twine Name = "autogen_SD" + Twine{SeedCL};
113 auto *Func = Function::Create(FuncTy, GlobalValue::ExternalLinkage, Name, M);
Nadav Rotem78bda892012-02-26 08:35:53 +0000114 Func->setCallingConv(CallingConv::C);
115 return Func;
116}
117
118/// A base class, implementing utilities needed for
119/// modifying and adding new random instructions.
120struct Modifier {
121 /// Used to store the randomly generated values.
122 typedef std::vector<Value*> PieceTable;
123
124public:
125 /// C'tor
Nadav Rotemdc497b62012-02-26 08:59:25 +0000126 Modifier(BasicBlock *Block, PieceTable *PT, Random *R):
Daniel Dunbarbeb34252012-02-29 00:20:33 +0000127 BB(Block),PT(PT),Ran(R),Context(BB->getContext()) {}
Andrew Trickbecbbbe2012-09-19 05:08:30 +0000128
129 /// virtual D'tor to silence warnings.
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000130 virtual ~Modifier() {}
Andrew Trickbecbbbe2012-09-19 05:08:30 +0000131
Nadav Rotem78bda892012-02-26 08:35:53 +0000132 /// Add a new instruction.
133 virtual void Act() = 0;
134 /// Add N new instructions,
135 virtual void ActN(unsigned n) {
136 for (unsigned i=0; i<n; ++i)
137 Act();
138 }
139
140protected:
141 /// Return a random value from the list of known values.
142 Value *getRandomVal() {
143 assert(PT->size());
144 return PT->at(Ran->Rand() % PT->size());
145 }
146
Nadav Roteme4972dd2012-02-26 13:56:18 +0000147 Constant *getRandomConstant(Type *Tp) {
148 if (Tp->isIntegerTy()) {
149 if (Ran->Rand() & 1)
150 return ConstantInt::getAllOnesValue(Tp);
151 return ConstantInt::getNullValue(Tp);
152 } else if (Tp->isFloatingPointTy()) {
153 if (Ran->Rand() & 1)
154 return ConstantFP::getAllOnesValue(Tp);
155 return ConstantFP::getNullValue(Tp);
156 }
157 return UndefValue::get(Tp);
158 }
159
Nadav Rotem78bda892012-02-26 08:35:53 +0000160 /// Return a random value with a known type.
161 Value *getRandomValue(Type *Tp) {
162 unsigned index = Ran->Rand();
163 for (unsigned i=0; i<PT->size(); ++i) {
164 Value *V = PT->at((index + i) % PT->size());
165 if (V->getType() == Tp)
166 return V;
167 }
168
169 // If the requested type was not found, generate a constant value.
170 if (Tp->isIntegerTy()) {
171 if (Ran->Rand() & 1)
172 return ConstantInt::getAllOnesValue(Tp);
173 return ConstantInt::getNullValue(Tp);
174 } else if (Tp->isFloatingPointTy()) {
175 if (Ran->Rand() & 1)
176 return ConstantFP::getAllOnesValue(Tp);
177 return ConstantFP::getNullValue(Tp);
Nadav Roteme4972dd2012-02-26 13:56:18 +0000178 } else if (Tp->isVectorTy()) {
179 VectorType *VTp = cast<VectorType>(Tp);
180
181 std::vector<Constant*> TempValues;
182 TempValues.reserve(VTp->getNumElements());
183 for (unsigned i = 0; i < VTp->getNumElements(); ++i)
184 TempValues.push_back(getRandomConstant(VTp->getScalarType()));
185
186 ArrayRef<Constant*> VectorValue(TempValues);
187 return ConstantVector::get(VectorValue);
Nadav Rotem78bda892012-02-26 08:35:53 +0000188 }
189
Nadav Rotem78bda892012-02-26 08:35:53 +0000190 return UndefValue::get(Tp);
191 }
192
193 /// Return a random value of any pointer type.
194 Value *getRandomPointerValue() {
195 unsigned index = Ran->Rand();
196 for (unsigned i=0; i<PT->size(); ++i) {
197 Value *V = PT->at((index + i) % PT->size());
198 if (V->getType()->isPointerTy())
199 return V;
200 }
201 return UndefValue::get(pickPointerType());
202 }
203
204 /// Return a random value of any vector type.
205 Value *getRandomVectorValue() {
206 unsigned index = Ran->Rand();
207 for (unsigned i=0; i<PT->size(); ++i) {
208 Value *V = PT->at((index + i) % PT->size());
209 if (V->getType()->isVectorTy())
210 return V;
211 }
212 return UndefValue::get(pickVectorType());
213 }
214
215 /// Pick a random type.
216 Type *pickType() {
217 return (Ran->Rand() & 1 ? pickVectorType() : pickScalarType());
218 }
219
220 /// Pick a random pointer type.
221 Type *pickPointerType() {
222 Type *Ty = pickType();
223 return PointerType::get(Ty, 0);
224 }
225
226 /// Pick a random vector type.
227 Type *pickVectorType(unsigned len = (unsigned)-1) {
Nadav Rotem78bda892012-02-26 08:35:53 +0000228 // Pick a random vector width in the range 2**0 to 2**4.
229 // by adding two randoms we are generating a normal-like distribution
230 // around 2**3.
231 unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3));
Dylan Noblesmith2a592dc2012-04-10 22:44:49 +0000232 Type *Ty;
233
234 // Vectors of x86mmx are illegal; keep trying till we get something else.
235 do {
236 Ty = pickScalarType();
237 } while (Ty->isX86_MMXTy());
238
Nadav Rotem78bda892012-02-26 08:35:53 +0000239 if (len != (unsigned)-1)
240 width = len;
241 return VectorType::get(Ty, width);
242 }
243
244 /// Pick a random scalar type.
245 Type *pickScalarType() {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000246 Type *t = nullptr;
Hal Finkelc9474122012-02-27 23:59:33 +0000247 do {
248 switch (Ran->Rand() % 30) {
249 case 0: t = Type::getInt1Ty(Context); break;
250 case 1: t = Type::getInt8Ty(Context); break;
251 case 2: t = Type::getInt16Ty(Context); break;
252 case 3: case 4:
253 case 5: t = Type::getFloatTy(Context); break;
254 case 6: case 7:
255 case 8: t = Type::getDoubleTy(Context); break;
256 case 9: case 10:
257 case 11: t = Type::getInt32Ty(Context); break;
258 case 12: case 13:
259 case 14: t = Type::getInt64Ty(Context); break;
260 case 15: case 16:
261 case 17: if (GenHalfFloat) t = Type::getHalfTy(Context); break;
262 case 18: case 19:
263 case 20: if (GenX86FP80) t = Type::getX86_FP80Ty(Context); break;
264 case 21: case 22:
265 case 23: if (GenFP128) t = Type::getFP128Ty(Context); break;
266 case 24: case 25:
267 case 26: if (GenPPCFP128) t = Type::getPPC_FP128Ty(Context); break;
268 case 27: case 28:
269 case 29: if (GenX86MMX) t = Type::getX86_MMXTy(Context); break;
270 default: llvm_unreachable("Invalid scalar value");
271 }
Craig Toppere6cb63e2014-04-25 04:24:47 +0000272 } while (t == nullptr);
Hal Finkelc9474122012-02-27 23:59:33 +0000273
274 return t;
Nadav Rotem78bda892012-02-26 08:35:53 +0000275 }
276
277 /// Basic block to populate
278 BasicBlock *BB;
279 /// Value table
280 PieceTable *PT;
281 /// Random number generator
282 Random *Ran;
283 /// Context
284 LLVMContext &Context;
285};
286
287struct LoadModifier: public Modifier {
Daniel Dunbarbeb34252012-02-29 00:20:33 +0000288 LoadModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
Craig Toppere56917c2014-03-08 08:27:28 +0000289 void Act() override {
Alp Tokerf907b892013-12-05 05:44:44 +0000290 // Try to use predefined pointers. If non-exist, use undef pointer value;
Nadav Rotem78bda892012-02-26 08:35:53 +0000291 Value *Ptr = getRandomPointerValue();
292 Value *V = new LoadInst(Ptr, "L", BB->getTerminator());
293 PT->push_back(V);
294 }
295};
296
297struct StoreModifier: public Modifier {
298 StoreModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
Craig Toppere56917c2014-03-08 08:27:28 +0000299 void Act() override {
Alp Tokerf907b892013-12-05 05:44:44 +0000300 // Try to use predefined pointers. If non-exist, use undef pointer value;
Nadav Rotem78bda892012-02-26 08:35:53 +0000301 Value *Ptr = getRandomPointerValue();
302 Type *Tp = Ptr->getType();
303 Value *Val = getRandomValue(Tp->getContainedType(0));
Nadav Rotem63ff91d2012-02-26 12:00:22 +0000304 Type *ValTy = Val->getType();
Nadav Rotem78bda892012-02-26 08:35:53 +0000305
306 // Do not store vectors of i1s because they are unsupported
Nadav Rotem115ec822012-02-26 12:34:17 +0000307 // by the codegen.
308 if (ValTy->isVectorTy() && ValTy->getScalarSizeInBits() == 1)
Nadav Rotem78bda892012-02-26 08:35:53 +0000309 return;
310
311 new StoreInst(Val, Ptr, BB->getTerminator());
312 }
313};
314
315struct BinModifier: public Modifier {
316 BinModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
317
Craig Toppere56917c2014-03-08 08:27:28 +0000318 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000319 Value *Val0 = getRandomVal();
320 Value *Val1 = getRandomValue(Val0->getType());
321
322 // Don't handle pointer types.
323 if (Val0->getType()->isPointerTy() ||
324 Val1->getType()->isPointerTy())
325 return;
326
327 // Don't handle i1 types.
328 if (Val0->getType()->getScalarSizeInBits() == 1)
329 return;
330
331
332 bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy();
333 Instruction* Term = BB->getTerminator();
334 unsigned R = Ran->Rand() % (isFloat ? 7 : 13);
335 Instruction::BinaryOps Op;
336
337 switch (R) {
338 default: llvm_unreachable("Invalid BinOp");
339 case 0:{Op = (isFloat?Instruction::FAdd : Instruction::Add); break; }
340 case 1:{Op = (isFloat?Instruction::FSub : Instruction::Sub); break; }
341 case 2:{Op = (isFloat?Instruction::FMul : Instruction::Mul); break; }
342 case 3:{Op = (isFloat?Instruction::FDiv : Instruction::SDiv); break; }
343 case 4:{Op = (isFloat?Instruction::FDiv : Instruction::UDiv); break; }
344 case 5:{Op = (isFloat?Instruction::FRem : Instruction::SRem); break; }
345 case 6:{Op = (isFloat?Instruction::FRem : Instruction::URem); break; }
346 case 7: {Op = Instruction::Shl; break; }
347 case 8: {Op = Instruction::LShr; break; }
348 case 9: {Op = Instruction::AShr; break; }
349 case 10:{Op = Instruction::And; break; }
350 case 11:{Op = Instruction::Or; break; }
351 case 12:{Op = Instruction::Xor; break; }
352 }
353
354 PT->push_back(BinaryOperator::Create(Op, Val0, Val1, "B", Term));
355 }
356};
357
358/// Generate constant values.
359struct ConstModifier: public Modifier {
360 ConstModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
Craig Toppere56917c2014-03-08 08:27:28 +0000361 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000362 Type *Ty = pickType();
363
364 if (Ty->isVectorTy()) {
365 switch (Ran->Rand() % 2) {
366 case 0: if (Ty->getScalarType()->isIntegerTy())
367 return PT->push_back(ConstantVector::getAllOnesValue(Ty));
368 case 1: if (Ty->getScalarType()->isIntegerTy())
369 return PT->push_back(ConstantVector::getNullValue(Ty));
370 }
371 }
372
373 if (Ty->isFloatingPointTy()) {
Dylan Noblesmith68f310d2012-04-10 22:44:51 +0000374 // Generate 128 random bits, the size of the (currently)
375 // largest floating-point types.
376 uint64_t RandomBits[2];
377 for (unsigned i = 0; i < 2; ++i)
378 RandomBits[i] = Ran->Rand64();
379
380 APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits));
Tim Northover98d9b7e2013-01-22 10:18:26 +0000381 APFloat RandomFloat(Ty->getFltSemantics(), RandomInt);
Dylan Noblesmith68f310d2012-04-10 22:44:51 +0000382
Nadav Rotem78bda892012-02-26 08:35:53 +0000383 if (Ran->Rand() & 1)
384 return PT->push_back(ConstantFP::getNullValue(Ty));
Dylan Noblesmith68f310d2012-04-10 22:44:51 +0000385 return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat));
Nadav Rotem78bda892012-02-26 08:35:53 +0000386 }
387
388 if (Ty->isIntegerTy()) {
389 switch (Ran->Rand() % 7) {
390 case 0: if (Ty->isIntegerTy())
391 return PT->push_back(ConstantInt::get(Ty,
392 APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits())));
393 case 1: if (Ty->isIntegerTy())
394 return PT->push_back(ConstantInt::get(Ty,
395 APInt::getNullValue(Ty->getPrimitiveSizeInBits())));
396 case 2: case 3: case 4: case 5:
397 case 6: if (Ty->isIntegerTy())
398 PT->push_back(ConstantInt::get(Ty, Ran->Rand()));
399 }
400 }
401
402 }
403};
404
405struct AllocaModifier: public Modifier {
406 AllocaModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R){}
407
Craig Toppere56917c2014-03-08 08:27:28 +0000408 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000409 Type *Tp = pickType();
410 PT->push_back(new AllocaInst(Tp, "A", BB->getFirstNonPHI()));
411 }
412};
413
414struct ExtractElementModifier: public Modifier {
415 ExtractElementModifier(BasicBlock *BB, PieceTable *PT, Random *R):
416 Modifier(BB, PT, R) {}
417
Craig Toppere56917c2014-03-08 08:27:28 +0000418 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000419 Value *Val0 = getRandomVectorValue();
420 Value *V = ExtractElementInst::Create(Val0,
421 ConstantInt::get(Type::getInt32Ty(BB->getContext()),
Nadav Rotemaeacc172012-04-15 20:17:14 +0000422 Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
Nadav Rotem78bda892012-02-26 08:35:53 +0000423 "E", BB->getTerminator());
424 return PT->push_back(V);
425 }
426};
427
428struct ShuffModifier: public Modifier {
429 ShuffModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
Craig Toppere56917c2014-03-08 08:27:28 +0000430 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000431
432 Value *Val0 = getRandomVectorValue();
433 Value *Val1 = getRandomValue(Val0->getType());
434
435 unsigned Width = cast<VectorType>(Val0->getType())->getNumElements();
436 std::vector<Constant*> Idxs;
437
438 Type *I32 = Type::getInt32Ty(BB->getContext());
439 for (unsigned i=0; i<Width; ++i) {
440 Constant *CI = ConstantInt::get(I32, Ran->Rand() % (Width*2));
441 // Pick some undef values.
442 if (!(Ran->Rand() % 5))
443 CI = UndefValue::get(I32);
444 Idxs.push_back(CI);
445 }
446
447 Constant *Mask = ConstantVector::get(Idxs);
448
449 Value *V = new ShuffleVectorInst(Val0, Val1, Mask, "Shuff",
450 BB->getTerminator());
451 PT->push_back(V);
452 }
453};
454
455struct InsertElementModifier: public Modifier {
456 InsertElementModifier(BasicBlock *BB, PieceTable *PT, Random *R):
457 Modifier(BB, PT, R) {}
458
Craig Toppere56917c2014-03-08 08:27:28 +0000459 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000460 Value *Val0 = getRandomVectorValue();
461 Value *Val1 = getRandomValue(Val0->getType()->getScalarType());
462
463 Value *V = InsertElementInst::Create(Val0, Val1,
464 ConstantInt::get(Type::getInt32Ty(BB->getContext()),
465 Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()),
466 "I", BB->getTerminator());
467 return PT->push_back(V);
468 }
469
470};
471
472struct CastModifier: public Modifier {
473 CastModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
Craig Toppere56917c2014-03-08 08:27:28 +0000474 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000475
476 Value *V = getRandomVal();
477 Type *VTy = V->getType();
478 Type *DestTy = pickScalarType();
479
480 // Handle vector casts vectors.
481 if (VTy->isVectorTy()) {
482 VectorType *VecTy = cast<VectorType>(VTy);
483 DestTy = pickVectorType(VecTy->getNumElements());
484 }
485
Nadav Rotemaeacc172012-04-15 20:17:14 +0000486 // no need to cast.
Nadav Rotem78bda892012-02-26 08:35:53 +0000487 if (VTy == DestTy) return;
488
489 // Pointers:
490 if (VTy->isPointerTy()) {
491 if (!DestTy->isPointerTy())
492 DestTy = PointerType::get(DestTy, 0);
493 return PT->push_back(
494 new BitCastInst(V, DestTy, "PC", BB->getTerminator()));
495 }
496
Nadav Rotemaeacc172012-04-15 20:17:14 +0000497 unsigned VSize = VTy->getScalarType()->getPrimitiveSizeInBits();
498 unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits();
499
Nadav Rotem78bda892012-02-26 08:35:53 +0000500 // Generate lots of bitcasts.
Nadav Rotemaeacc172012-04-15 20:17:14 +0000501 if ((Ran->Rand() & 1) && VSize == DestSize) {
Nadav Rotem78bda892012-02-26 08:35:53 +0000502 return PT->push_back(
503 new BitCastInst(V, DestTy, "BC", BB->getTerminator()));
504 }
505
506 // Both types are integers:
507 if (VTy->getScalarType()->isIntegerTy() &&
508 DestTy->getScalarType()->isIntegerTy()) {
Nadav Rotemaeacc172012-04-15 20:17:14 +0000509 if (VSize > DestSize) {
Nadav Rotem78bda892012-02-26 08:35:53 +0000510 return PT->push_back(
511 new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
512 } else {
Nadav Rotemaeacc172012-04-15 20:17:14 +0000513 assert(VSize < DestSize && "Different int types with the same size?");
Nadav Rotem78bda892012-02-26 08:35:53 +0000514 if (Ran->Rand() & 1)
515 return PT->push_back(
516 new ZExtInst(V, DestTy, "ZE", BB->getTerminator()));
517 return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator()));
518 }
519 }
520
521 // Fp to int.
522 if (VTy->getScalarType()->isFloatingPointTy() &&
523 DestTy->getScalarType()->isIntegerTy()) {
524 if (Ran->Rand() & 1)
525 return PT->push_back(
526 new FPToSIInst(V, DestTy, "FC", BB->getTerminator()));
527 return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator()));
528 }
529
530 // Int to fp.
531 if (VTy->getScalarType()->isIntegerTy() &&
532 DestTy->getScalarType()->isFloatingPointTy()) {
533 if (Ran->Rand() & 1)
534 return PT->push_back(
535 new SIToFPInst(V, DestTy, "FC", BB->getTerminator()));
536 return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator()));
537
538 }
539
540 // Both floats.
541 if (VTy->getScalarType()->isFloatingPointTy() &&
542 DestTy->getScalarType()->isFloatingPointTy()) {
Nadav Rotemaeacc172012-04-15 20:17:14 +0000543 if (VSize > DestSize) {
Nadav Rotem78bda892012-02-26 08:35:53 +0000544 return PT->push_back(
545 new FPTruncInst(V, DestTy, "Tr", BB->getTerminator()));
Nadav Rotemaeacc172012-04-15 20:17:14 +0000546 } else if (VSize < DestSize) {
Nadav Rotem78bda892012-02-26 08:35:53 +0000547 return PT->push_back(
548 new FPExtInst(V, DestTy, "ZE", BB->getTerminator()));
549 }
Nadav Rotemaeacc172012-04-15 20:17:14 +0000550 // If VSize == DestSize, then the two types must be fp128 and ppc_fp128,
551 // for which there is no defined conversion. So do nothing.
Nadav Rotem78bda892012-02-26 08:35:53 +0000552 }
553 }
554
555};
556
557struct SelectModifier: public Modifier {
558 SelectModifier(BasicBlock *BB, PieceTable *PT, Random *R):
559 Modifier(BB, PT, R) {}
560
Craig Toppere56917c2014-03-08 08:27:28 +0000561 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000562 // Try a bunch of different select configuration until a valid one is found.
563 Value *Val0 = getRandomVal();
564 Value *Val1 = getRandomValue(Val0->getType());
565
566 Type *CondTy = Type::getInt1Ty(Context);
567
568 // If the value type is a vector, and we allow vector select, then in 50%
569 // of the cases generate a vector select.
570 if (Val0->getType()->isVectorTy() && (Ran->Rand() % 1)) {
571 unsigned NumElem = cast<VectorType>(Val0->getType())->getNumElements();
572 CondTy = VectorType::get(CondTy, NumElem);
573 }
574
575 Value *Cond = getRandomValue(CondTy);
576 Value *V = SelectInst::Create(Cond, Val0, Val1, "Sl", BB->getTerminator());
577 return PT->push_back(V);
578 }
579};
580
581
582struct CmpModifier: public Modifier {
583 CmpModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {}
Craig Toppere56917c2014-03-08 08:27:28 +0000584 void Act() override {
Nadav Rotem78bda892012-02-26 08:35:53 +0000585
586 Value *Val0 = getRandomVal();
587 Value *Val1 = getRandomValue(Val0->getType());
588
589 if (Val0->getType()->isPointerTy()) return;
590 bool fp = Val0->getType()->getScalarType()->isFloatingPointTy();
591
592 int op;
593 if (fp) {
594 op = Ran->Rand() %
595 (CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) +
596 CmpInst::FIRST_FCMP_PREDICATE;
597 } else {
598 op = Ran->Rand() %
599 (CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) +
600 CmpInst::FIRST_ICMP_PREDICATE;
601 }
602
603 Value *V = CmpInst::Create(fp ? Instruction::FCmp : Instruction::ICmp,
604 op, Val0, Val1, "Cmp", BB->getTerminator());
605 return PT->push_back(V);
606 }
607};
608
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000609} // end anonymous namespace
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000610
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000611static void FillFunction(Function *F, Random &R) {
Nadav Rotem78bda892012-02-26 08:35:53 +0000612 // Create a legal entry block.
613 BasicBlock *BB = BasicBlock::Create(F->getContext(), "BB", F);
614 ReturnInst::Create(F->getContext(), BB);
615
616 // Create the value table.
617 Modifier::PieceTable PT;
Nadav Rotem78bda892012-02-26 08:35:53 +0000618
619 // Consider arguments as legal values.
Pawel Bylica23663472015-06-24 11:49:44 +0000620 for (auto &arg : F->args())
621 PT.push_back(&arg);
Nadav Rotem78bda892012-02-26 08:35:53 +0000622
623 // List of modifiers which add new random instructions.
Pawel Bylica23663472015-06-24 11:49:44 +0000624 std::vector<std::unique_ptr<Modifier>> Modifiers;
625 Modifiers.emplace_back(new LoadModifier(BB, &PT, &R));
626 Modifiers.emplace_back(new StoreModifier(BB, &PT, &R));
627 auto SM = Modifiers.back().get();
628 Modifiers.emplace_back(new ExtractElementModifier(BB, &PT, &R));
629 Modifiers.emplace_back(new ShuffModifier(BB, &PT, &R));
630 Modifiers.emplace_back(new InsertElementModifier(BB, &PT, &R));
631 Modifiers.emplace_back(new BinModifier(BB, &PT, &R));
632 Modifiers.emplace_back(new CastModifier(BB, &PT, &R));
633 Modifiers.emplace_back(new SelectModifier(BB, &PT, &R));
634 Modifiers.emplace_back(new CmpModifier(BB, &PT, &R));
Nadav Rotem78bda892012-02-26 08:35:53 +0000635
636 // Generate the random instructions
Pawel Bylica23663472015-06-24 11:49:44 +0000637 AllocaModifier{BB, &PT, &R}.ActN(5); // Throw in a few allocas
638 ConstModifier{BB, &PT, &R}.ActN(40); // Throw in a few constants
Nadav Rotem78bda892012-02-26 08:35:53 +0000639
Pawel Bylica23663472015-06-24 11:49:44 +0000640 for (unsigned i = 0; i < SizeCL / Modifiers.size(); ++i)
641 for (auto &Mod : Modifiers)
642 Mod->Act();
Nadav Rotem78bda892012-02-26 08:35:53 +0000643
644 SM->ActN(5); // Throw in a few stores.
645}
646
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000647static void IntroduceControlFlow(Function *F, Random &R) {
Nadav Rotem0fb74082012-06-21 08:58:15 +0000648 std::vector<Instruction*> BoolInst;
Pawel Bylica23663472015-06-24 11:49:44 +0000649 for (auto &Instr : F->front()) {
650 if (Instr.getType() == IntegerType::getInt1Ty(F->getContext()))
651 BoolInst.push_back(&Instr);
Nadav Rotem78bda892012-02-26 08:35:53 +0000652 }
653
Nadav Rotem0fb74082012-06-21 08:58:15 +0000654 std::random_shuffle(BoolInst.begin(), BoolInst.end(), R);
655
Pawel Bylica23663472015-06-24 11:49:44 +0000656 for (auto *Instr : BoolInst) {
Nadav Rotem78bda892012-02-26 08:35:53 +0000657 BasicBlock *Curr = Instr->getParent();
Pawel Bylica23663472015-06-24 11:49:44 +0000658 BasicBlock::iterator Loc = Instr;
Nadav Rotem78bda892012-02-26 08:35:53 +0000659 BasicBlock *Next = Curr->splitBasicBlock(Loc, "CF");
660 Instr->moveBefore(Curr->getTerminator());
661 if (Curr != &F->getEntryBlock()) {
662 BranchInst::Create(Curr, Next, Instr, Curr->getTerminator());
663 Curr->getTerminator()->eraseFromParent();
664 }
665 }
666}
667
668int main(int argc, char **argv) {
669 // Init LLVM, call llvm_shutdown() on exit, parse args, etc.
670 llvm::PrettyStackTraceProgram X(argc, argv);
671 cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n");
672 llvm_shutdown_obj Y;
673
Pawel Bylica23663472015-06-24 11:49:44 +0000674 auto M = make_unique<Module>("/tmp/autogen.bc", getGlobalContext());
Nadav Rotem78bda892012-02-26 08:35:53 +0000675 Function *F = GenEmptyFunction(M.get());
Nadav Rotem0fb74082012-06-21 08:58:15 +0000676
677 // Pick an initial seed value
678 Random R(SeedCL);
679 // Generate lots of random instructions inside a single basic block.
680 FillFunction(F, R);
681 // Break the basic block into many loops.
682 IntroduceControlFlow(F, R);
Nadav Rotem78bda892012-02-26 08:35:53 +0000683
684 // Figure out what stream we are supposed to write to...
Ahmed Charles56440fd2014-03-06 05:51:42 +0000685 std::unique_ptr<tool_output_file> Out;
Nadav Rotem78bda892012-02-26 08:35:53 +0000686 // Default to standard output.
687 if (OutputFilename.empty())
688 OutputFilename = "-";
689
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000690 std::error_code EC;
691 Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
692 if (EC) {
693 errs() << EC.message() << '\n';
Nadav Rotem78bda892012-02-26 08:35:53 +0000694 return 1;
695 }
696
Chandler Carruth30d69c22015-02-13 10:01:29 +0000697 legacy::PassManager Passes;
Nadav Rotem78bda892012-02-26 08:35:53 +0000698 Passes.add(createVerifierPass());
Chandler Carruth3bdf0432014-01-12 11:39:04 +0000699 Passes.add(createPrintModulePass(Out->os()));
Nadav Rotem78bda892012-02-26 08:35:53 +0000700 Passes.run(*M.get());
Nadav Rotem78bda892012-02-26 08:35:53 +0000701 Out->keep();
702
703 return 0;
704}