Daniel Dunbar | b6f0625 | 2012-02-29 00:20:33 +0000 | [diff] [blame] | 1 | //===-- llvm-stress.cpp - Generate random LL files to stress-test LLVM ----===// |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 2 | // |
| 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 | //===----------------------------------------------------------------------===// |
| 14 | #include "llvm/LLVMContext.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/PassManager.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Instruction.h" |
| 19 | #include "llvm/CallGraphSCCPass.h" |
| 20 | #include "llvm/Assembly/PrintModulePass.h" |
| 21 | #include "llvm/Analysis/Verifier.h" |
| 22 | #include "llvm/Support/PassNameParser.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/ManagedStatic.h" |
| 25 | #include "llvm/Support/PluginLoader.h" |
| 26 | #include "llvm/Support/PrettyStackTrace.h" |
| 27 | #include "llvm/Support/ToolOutputFile.h" |
| 28 | #include <memory> |
| 29 | #include <sstream> |
| 30 | #include <set> |
| 31 | #include <vector> |
| 32 | #include <algorithm> |
| 33 | using namespace llvm; |
| 34 | |
| 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)"), |
| 39 | cl::init(100)); |
| 40 | static cl::opt<std::string> |
| 41 | OutputFilename("o", cl::desc("Override output filename"), |
| 42 | cl::value_desc("filename")); |
| 43 | |
Hal Finkel | 2cff617 | 2012-02-27 23:59:33 +0000 | [diff] [blame] | 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)); |
| 54 | |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 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 |
| 58 | /// number generator. |
| 59 | class Random { |
| 60 | public: |
| 61 | /// C'tor |
| 62 | Random(unsigned _seed):Seed(_seed) {} |
Dylan Noblesmith | 83f17f2 | 2012-04-10 22:44:51 +0000 | [diff] [blame^] | 63 | |
| 64 | /// Return a random integer, up to a |
| 65 | /// maximum of 2**19 - 1. |
| 66 | uint32_t Rand() { |
| 67 | uint32_t Val = Seed + 0x000b07a1; |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 68 | Seed = (Val * 0x3c7c0ac1); |
| 69 | // Only lowest 19 bits are random-ish. |
| 70 | return Seed & 0x7ffff; |
| 71 | } |
| 72 | |
Dylan Noblesmith | 83f17f2 | 2012-04-10 22:44:51 +0000 | [diff] [blame^] | 73 | /// Return a random 32 bit integer. |
| 74 | uint32_t Rand32() { |
| 75 | uint32_t Val = Rand(); |
| 76 | Val &= 0xffff; |
| 77 | return Val | (Rand() << 16); |
| 78 | } |
| 79 | |
| 80 | /// Return a random 64 bit integer. |
| 81 | uint64_t Rand64() { |
| 82 | uint64_t Val = Rand32(); |
| 83 | return Val | (uint64_t(Rand32()) << 32); |
| 84 | } |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 85 | private: |
| 86 | unsigned Seed; |
| 87 | }; |
| 88 | |
| 89 | /// Generate an empty function with a default argument list. |
| 90 | Function *GenEmptyFunction(Module *M) { |
| 91 | // Type Definitions |
| 92 | std::vector<Type*> ArgsTy; |
| 93 | // Define a few arguments |
| 94 | LLVMContext &Context = M->getContext(); |
| 95 | ArgsTy.push_back(PointerType::get(IntegerType::getInt8Ty(Context), 0)); |
| 96 | ArgsTy.push_back(PointerType::get(IntegerType::getInt32Ty(Context), 0)); |
| 97 | ArgsTy.push_back(PointerType::get(IntegerType::getInt64Ty(Context), 0)); |
| 98 | ArgsTy.push_back(IntegerType::getInt32Ty(Context)); |
| 99 | ArgsTy.push_back(IntegerType::getInt64Ty(Context)); |
| 100 | ArgsTy.push_back(IntegerType::getInt8Ty(Context)); |
| 101 | |
| 102 | FunctionType *FuncTy = FunctionType::get(Type::getVoidTy(Context), ArgsTy, 0); |
| 103 | // Pick a unique name to describe the input parameters |
| 104 | std::stringstream ss; |
| 105 | ss<<"autogen_SD"<<SeedCL; |
| 106 | Function *Func = Function::Create(FuncTy, GlobalValue::ExternalLinkage, |
| 107 | ss.str(), M); |
| 108 | |
| 109 | Func->setCallingConv(CallingConv::C); |
| 110 | return Func; |
| 111 | } |
| 112 | |
| 113 | /// A base class, implementing utilities needed for |
| 114 | /// modifying and adding new random instructions. |
| 115 | struct Modifier { |
| 116 | /// Used to store the randomly generated values. |
| 117 | typedef std::vector<Value*> PieceTable; |
| 118 | |
| 119 | public: |
| 120 | /// C'tor |
Nadav Rotem | 08c8339 | 2012-02-26 08:59:25 +0000 | [diff] [blame] | 121 | Modifier(BasicBlock *Block, PieceTable *PT, Random *R): |
Daniel Dunbar | b6f0625 | 2012-02-29 00:20:33 +0000 | [diff] [blame] | 122 | BB(Block),PT(PT),Ran(R),Context(BB->getContext()) {} |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 123 | /// Add a new instruction. |
| 124 | virtual void Act() = 0; |
| 125 | /// Add N new instructions, |
| 126 | virtual void ActN(unsigned n) { |
| 127 | for (unsigned i=0; i<n; ++i) |
| 128 | Act(); |
| 129 | } |
| 130 | |
| 131 | protected: |
| 132 | /// Return a random value from the list of known values. |
| 133 | Value *getRandomVal() { |
| 134 | assert(PT->size()); |
| 135 | return PT->at(Ran->Rand() % PT->size()); |
| 136 | } |
| 137 | |
Nadav Rotem | bfb7dfa | 2012-02-26 13:56:18 +0000 | [diff] [blame] | 138 | Constant *getRandomConstant(Type *Tp) { |
| 139 | if (Tp->isIntegerTy()) { |
| 140 | if (Ran->Rand() & 1) |
| 141 | return ConstantInt::getAllOnesValue(Tp); |
| 142 | return ConstantInt::getNullValue(Tp); |
| 143 | } else if (Tp->isFloatingPointTy()) { |
| 144 | if (Ran->Rand() & 1) |
| 145 | return ConstantFP::getAllOnesValue(Tp); |
| 146 | return ConstantFP::getNullValue(Tp); |
| 147 | } |
| 148 | return UndefValue::get(Tp); |
| 149 | } |
| 150 | |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 151 | /// Return a random value with a known type. |
| 152 | Value *getRandomValue(Type *Tp) { |
| 153 | unsigned index = Ran->Rand(); |
| 154 | for (unsigned i=0; i<PT->size(); ++i) { |
| 155 | Value *V = PT->at((index + i) % PT->size()); |
| 156 | if (V->getType() == Tp) |
| 157 | return V; |
| 158 | } |
| 159 | |
| 160 | // If the requested type was not found, generate a constant value. |
| 161 | if (Tp->isIntegerTy()) { |
| 162 | if (Ran->Rand() & 1) |
| 163 | return ConstantInt::getAllOnesValue(Tp); |
| 164 | return ConstantInt::getNullValue(Tp); |
| 165 | } else if (Tp->isFloatingPointTy()) { |
| 166 | if (Ran->Rand() & 1) |
| 167 | return ConstantFP::getAllOnesValue(Tp); |
| 168 | return ConstantFP::getNullValue(Tp); |
Nadav Rotem | bfb7dfa | 2012-02-26 13:56:18 +0000 | [diff] [blame] | 169 | } else if (Tp->isVectorTy()) { |
| 170 | VectorType *VTp = cast<VectorType>(Tp); |
| 171 | |
| 172 | std::vector<Constant*> TempValues; |
| 173 | TempValues.reserve(VTp->getNumElements()); |
| 174 | for (unsigned i = 0; i < VTp->getNumElements(); ++i) |
| 175 | TempValues.push_back(getRandomConstant(VTp->getScalarType())); |
| 176 | |
| 177 | ArrayRef<Constant*> VectorValue(TempValues); |
| 178 | return ConstantVector::get(VectorValue); |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 181 | return UndefValue::get(Tp); |
| 182 | } |
| 183 | |
| 184 | /// Return a random value of any pointer type. |
| 185 | Value *getRandomPointerValue() { |
| 186 | unsigned index = Ran->Rand(); |
| 187 | for (unsigned i=0; i<PT->size(); ++i) { |
| 188 | Value *V = PT->at((index + i) % PT->size()); |
| 189 | if (V->getType()->isPointerTy()) |
| 190 | return V; |
| 191 | } |
| 192 | return UndefValue::get(pickPointerType()); |
| 193 | } |
| 194 | |
| 195 | /// Return a random value of any vector type. |
| 196 | Value *getRandomVectorValue() { |
| 197 | unsigned index = Ran->Rand(); |
| 198 | for (unsigned i=0; i<PT->size(); ++i) { |
| 199 | Value *V = PT->at((index + i) % PT->size()); |
| 200 | if (V->getType()->isVectorTy()) |
| 201 | return V; |
| 202 | } |
| 203 | return UndefValue::get(pickVectorType()); |
| 204 | } |
| 205 | |
| 206 | /// Pick a random type. |
| 207 | Type *pickType() { |
| 208 | return (Ran->Rand() & 1 ? pickVectorType() : pickScalarType()); |
| 209 | } |
| 210 | |
| 211 | /// Pick a random pointer type. |
| 212 | Type *pickPointerType() { |
| 213 | Type *Ty = pickType(); |
| 214 | return PointerType::get(Ty, 0); |
| 215 | } |
| 216 | |
| 217 | /// Pick a random vector type. |
| 218 | Type *pickVectorType(unsigned len = (unsigned)-1) { |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 219 | // Pick a random vector width in the range 2**0 to 2**4. |
| 220 | // by adding two randoms we are generating a normal-like distribution |
| 221 | // around 2**3. |
| 222 | unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3)); |
Dylan Noblesmith | 701de8f | 2012-04-10 22:44:49 +0000 | [diff] [blame] | 223 | Type *Ty; |
| 224 | |
| 225 | // Vectors of x86mmx are illegal; keep trying till we get something else. |
| 226 | do { |
| 227 | Ty = pickScalarType(); |
| 228 | } while (Ty->isX86_MMXTy()); |
| 229 | |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 230 | if (len != (unsigned)-1) |
| 231 | width = len; |
| 232 | return VectorType::get(Ty, width); |
| 233 | } |
| 234 | |
| 235 | /// Pick a random scalar type. |
| 236 | Type *pickScalarType() { |
Hal Finkel | 2cff617 | 2012-02-27 23:59:33 +0000 | [diff] [blame] | 237 | Type *t = 0; |
| 238 | do { |
| 239 | switch (Ran->Rand() % 30) { |
| 240 | case 0: t = Type::getInt1Ty(Context); break; |
| 241 | case 1: t = Type::getInt8Ty(Context); break; |
| 242 | case 2: t = Type::getInt16Ty(Context); break; |
| 243 | case 3: case 4: |
| 244 | case 5: t = Type::getFloatTy(Context); break; |
| 245 | case 6: case 7: |
| 246 | case 8: t = Type::getDoubleTy(Context); break; |
| 247 | case 9: case 10: |
| 248 | case 11: t = Type::getInt32Ty(Context); break; |
| 249 | case 12: case 13: |
| 250 | case 14: t = Type::getInt64Ty(Context); break; |
| 251 | case 15: case 16: |
| 252 | case 17: if (GenHalfFloat) t = Type::getHalfTy(Context); break; |
| 253 | case 18: case 19: |
| 254 | case 20: if (GenX86FP80) t = Type::getX86_FP80Ty(Context); break; |
| 255 | case 21: case 22: |
| 256 | case 23: if (GenFP128) t = Type::getFP128Ty(Context); break; |
| 257 | case 24: case 25: |
| 258 | case 26: if (GenPPCFP128) t = Type::getPPC_FP128Ty(Context); break; |
| 259 | case 27: case 28: |
| 260 | case 29: if (GenX86MMX) t = Type::getX86_MMXTy(Context); break; |
| 261 | default: llvm_unreachable("Invalid scalar value"); |
| 262 | } |
| 263 | } while (t == 0); |
| 264 | |
| 265 | return t; |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /// Basic block to populate |
| 269 | BasicBlock *BB; |
| 270 | /// Value table |
| 271 | PieceTable *PT; |
| 272 | /// Random number generator |
| 273 | Random *Ran; |
| 274 | /// Context |
| 275 | LLVMContext &Context; |
| 276 | }; |
| 277 | |
| 278 | struct LoadModifier: public Modifier { |
Daniel Dunbar | b6f0625 | 2012-02-29 00:20:33 +0000 | [diff] [blame] | 279 | LoadModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 280 | virtual void Act() { |
| 281 | // Try to use predefined pointers. If non exist, use undef pointer value; |
| 282 | Value *Ptr = getRandomPointerValue(); |
| 283 | Value *V = new LoadInst(Ptr, "L", BB->getTerminator()); |
| 284 | PT->push_back(V); |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | struct StoreModifier: public Modifier { |
| 289 | StoreModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
| 290 | virtual void Act() { |
| 291 | // Try to use predefined pointers. If non exist, use undef pointer value; |
| 292 | Value *Ptr = getRandomPointerValue(); |
| 293 | Type *Tp = Ptr->getType(); |
| 294 | Value *Val = getRandomValue(Tp->getContainedType(0)); |
Nadav Rotem | 2e851a9 | 2012-02-26 12:00:22 +0000 | [diff] [blame] | 295 | Type *ValTy = Val->getType(); |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 296 | |
| 297 | // Do not store vectors of i1s because they are unsupported |
Nadav Rotem | 794c16a | 2012-02-26 12:34:17 +0000 | [diff] [blame] | 298 | // by the codegen. |
| 299 | if (ValTy->isVectorTy() && ValTy->getScalarSizeInBits() == 1) |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 300 | return; |
| 301 | |
| 302 | new StoreInst(Val, Ptr, BB->getTerminator()); |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | struct BinModifier: public Modifier { |
| 307 | BinModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
| 308 | |
| 309 | virtual void Act() { |
| 310 | Value *Val0 = getRandomVal(); |
| 311 | Value *Val1 = getRandomValue(Val0->getType()); |
| 312 | |
| 313 | // Don't handle pointer types. |
| 314 | if (Val0->getType()->isPointerTy() || |
| 315 | Val1->getType()->isPointerTy()) |
| 316 | return; |
| 317 | |
| 318 | // Don't handle i1 types. |
| 319 | if (Val0->getType()->getScalarSizeInBits() == 1) |
| 320 | return; |
| 321 | |
| 322 | |
| 323 | bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy(); |
| 324 | Instruction* Term = BB->getTerminator(); |
| 325 | unsigned R = Ran->Rand() % (isFloat ? 7 : 13); |
| 326 | Instruction::BinaryOps Op; |
| 327 | |
| 328 | switch (R) { |
| 329 | default: llvm_unreachable("Invalid BinOp"); |
| 330 | case 0:{Op = (isFloat?Instruction::FAdd : Instruction::Add); break; } |
| 331 | case 1:{Op = (isFloat?Instruction::FSub : Instruction::Sub); break; } |
| 332 | case 2:{Op = (isFloat?Instruction::FMul : Instruction::Mul); break; } |
| 333 | case 3:{Op = (isFloat?Instruction::FDiv : Instruction::SDiv); break; } |
| 334 | case 4:{Op = (isFloat?Instruction::FDiv : Instruction::UDiv); break; } |
| 335 | case 5:{Op = (isFloat?Instruction::FRem : Instruction::SRem); break; } |
| 336 | case 6:{Op = (isFloat?Instruction::FRem : Instruction::URem); break; } |
| 337 | case 7: {Op = Instruction::Shl; break; } |
| 338 | case 8: {Op = Instruction::LShr; break; } |
| 339 | case 9: {Op = Instruction::AShr; break; } |
| 340 | case 10:{Op = Instruction::And; break; } |
| 341 | case 11:{Op = Instruction::Or; break; } |
| 342 | case 12:{Op = Instruction::Xor; break; } |
| 343 | } |
| 344 | |
| 345 | PT->push_back(BinaryOperator::Create(Op, Val0, Val1, "B", Term)); |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | /// Generate constant values. |
| 350 | struct ConstModifier: public Modifier { |
| 351 | ConstModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
| 352 | virtual void Act() { |
| 353 | Type *Ty = pickType(); |
| 354 | |
| 355 | if (Ty->isVectorTy()) { |
| 356 | switch (Ran->Rand() % 2) { |
| 357 | case 0: if (Ty->getScalarType()->isIntegerTy()) |
| 358 | return PT->push_back(ConstantVector::getAllOnesValue(Ty)); |
| 359 | case 1: if (Ty->getScalarType()->isIntegerTy()) |
| 360 | return PT->push_back(ConstantVector::getNullValue(Ty)); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (Ty->isFloatingPointTy()) { |
Dylan Noblesmith | 83f17f2 | 2012-04-10 22:44:51 +0000 | [diff] [blame^] | 365 | // Generate 128 random bits, the size of the (currently) |
| 366 | // largest floating-point types. |
| 367 | uint64_t RandomBits[2]; |
| 368 | for (unsigned i = 0; i < 2; ++i) |
| 369 | RandomBits[i] = Ran->Rand64(); |
| 370 | |
| 371 | APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits)); |
| 372 | |
| 373 | bool isIEEE = !Ty->isX86_FP80Ty() && !Ty->isPPC_FP128Ty(); |
| 374 | APFloat RandomFloat(RandomInt, isIEEE); |
| 375 | |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 376 | if (Ran->Rand() & 1) |
| 377 | return PT->push_back(ConstantFP::getNullValue(Ty)); |
Dylan Noblesmith | 83f17f2 | 2012-04-10 22:44:51 +0000 | [diff] [blame^] | 378 | return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat)); |
Nadav Rotem | fdc309c | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | if (Ty->isIntegerTy()) { |
| 382 | switch (Ran->Rand() % 7) { |
| 383 | case 0: if (Ty->isIntegerTy()) |
| 384 | return PT->push_back(ConstantInt::get(Ty, |
| 385 | APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits()))); |
| 386 | case 1: if (Ty->isIntegerTy()) |
| 387 | return PT->push_back(ConstantInt::get(Ty, |
| 388 | APInt::getNullValue(Ty->getPrimitiveSizeInBits()))); |
| 389 | case 2: case 3: case 4: case 5: |
| 390 | case 6: if (Ty->isIntegerTy()) |
| 391 | PT->push_back(ConstantInt::get(Ty, Ran->Rand())); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | struct AllocaModifier: public Modifier { |
| 399 | AllocaModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R){} |
| 400 | |
| 401 | virtual void Act() { |
| 402 | Type *Tp = pickType(); |
| 403 | PT->push_back(new AllocaInst(Tp, "A", BB->getFirstNonPHI())); |
| 404 | } |
| 405 | }; |
| 406 | |
| 407 | struct ExtractElementModifier: public Modifier { |
| 408 | ExtractElementModifier(BasicBlock *BB, PieceTable *PT, Random *R): |
| 409 | Modifier(BB, PT, R) {} |
| 410 | |
| 411 | virtual void Act() { |
| 412 | Value *Val0 = getRandomVectorValue(); |
| 413 | Value *V = ExtractElementInst::Create(Val0, |
| 414 | ConstantInt::get(Type::getInt32Ty(BB->getContext()), |
| 415 | Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()), |
| 416 | "E", BB->getTerminator()); |
| 417 | return PT->push_back(V); |
| 418 | } |
| 419 | }; |
| 420 | |
| 421 | struct ShuffModifier: public Modifier { |
| 422 | ShuffModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
| 423 | virtual void Act() { |
| 424 | |
| 425 | Value *Val0 = getRandomVectorValue(); |
| 426 | Value *Val1 = getRandomValue(Val0->getType()); |
| 427 | |
| 428 | unsigned Width = cast<VectorType>(Val0->getType())->getNumElements(); |
| 429 | std::vector<Constant*> Idxs; |
| 430 | |
| 431 | Type *I32 = Type::getInt32Ty(BB->getContext()); |
| 432 | for (unsigned i=0; i<Width; ++i) { |
| 433 | Constant *CI = ConstantInt::get(I32, Ran->Rand() % (Width*2)); |
| 434 | // Pick some undef values. |
| 435 | if (!(Ran->Rand() % 5)) |
| 436 | CI = UndefValue::get(I32); |
| 437 | Idxs.push_back(CI); |
| 438 | } |
| 439 | |
| 440 | Constant *Mask = ConstantVector::get(Idxs); |
| 441 | |
| 442 | Value *V = new ShuffleVectorInst(Val0, Val1, Mask, "Shuff", |
| 443 | BB->getTerminator()); |
| 444 | PT->push_back(V); |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | struct InsertElementModifier: public Modifier { |
| 449 | InsertElementModifier(BasicBlock *BB, PieceTable *PT, Random *R): |
| 450 | Modifier(BB, PT, R) {} |
| 451 | |
| 452 | virtual void Act() { |
| 453 | Value *Val0 = getRandomVectorValue(); |
| 454 | Value *Val1 = getRandomValue(Val0->getType()->getScalarType()); |
| 455 | |
| 456 | Value *V = InsertElementInst::Create(Val0, Val1, |
| 457 | ConstantInt::get(Type::getInt32Ty(BB->getContext()), |
| 458 | Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()), |
| 459 | "I", BB->getTerminator()); |
| 460 | return PT->push_back(V); |
| 461 | } |
| 462 | |
| 463 | }; |
| 464 | |
| 465 | struct CastModifier: public Modifier { |
| 466 | CastModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
| 467 | virtual void Act() { |
| 468 | |
| 469 | Value *V = getRandomVal(); |
| 470 | Type *VTy = V->getType(); |
| 471 | Type *DestTy = pickScalarType(); |
| 472 | |
| 473 | // Handle vector casts vectors. |
| 474 | if (VTy->isVectorTy()) { |
| 475 | VectorType *VecTy = cast<VectorType>(VTy); |
| 476 | DestTy = pickVectorType(VecTy->getNumElements()); |
| 477 | } |
| 478 | |
| 479 | // no need to casr. |
| 480 | if (VTy == DestTy) return; |
| 481 | |
| 482 | // Pointers: |
| 483 | if (VTy->isPointerTy()) { |
| 484 | if (!DestTy->isPointerTy()) |
| 485 | DestTy = PointerType::get(DestTy, 0); |
| 486 | return PT->push_back( |
| 487 | new BitCastInst(V, DestTy, "PC", BB->getTerminator())); |
| 488 | } |
| 489 | |
| 490 | // Generate lots of bitcasts. |
| 491 | if ((Ran->Rand() & 1) && |
| 492 | VTy->getPrimitiveSizeInBits() == DestTy->getPrimitiveSizeInBits()) { |
| 493 | return PT->push_back( |
| 494 | new BitCastInst(V, DestTy, "BC", BB->getTerminator())); |
| 495 | } |
| 496 | |
| 497 | // Both types are integers: |
| 498 | if (VTy->getScalarType()->isIntegerTy() && |
| 499 | DestTy->getScalarType()->isIntegerTy()) { |
| 500 | if (VTy->getScalarType()->getPrimitiveSizeInBits() > |
| 501 | DestTy->getScalarType()->getPrimitiveSizeInBits()) { |
| 502 | return PT->push_back( |
| 503 | new TruncInst(V, DestTy, "Tr", BB->getTerminator())); |
| 504 | } else { |
| 505 | if (Ran->Rand() & 1) |
| 506 | return PT->push_back( |
| 507 | new ZExtInst(V, DestTy, "ZE", BB->getTerminator())); |
| 508 | return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator())); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // Fp to int. |
| 513 | if (VTy->getScalarType()->isFloatingPointTy() && |
| 514 | DestTy->getScalarType()->isIntegerTy()) { |
| 515 | if (Ran->Rand() & 1) |
| 516 | return PT->push_back( |
| 517 | new FPToSIInst(V, DestTy, "FC", BB->getTerminator())); |
| 518 | return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator())); |
| 519 | } |
| 520 | |
| 521 | // Int to fp. |
| 522 | if (VTy->getScalarType()->isIntegerTy() && |
| 523 | DestTy->getScalarType()->isFloatingPointTy()) { |
| 524 | if (Ran->Rand() & 1) |
| 525 | return PT->push_back( |
| 526 | new SIToFPInst(V, DestTy, "FC", BB->getTerminator())); |
| 527 | return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator())); |
| 528 | |
| 529 | } |
| 530 | |
| 531 | // Both floats. |
| 532 | if (VTy->getScalarType()->isFloatingPointTy() && |
| 533 | DestTy->getScalarType()->isFloatingPointTy()) { |
| 534 | if (VTy->getScalarType()->getPrimitiveSizeInBits() > |
| 535 | DestTy->getScalarType()->getPrimitiveSizeInBits()) { |
| 536 | return PT->push_back( |
| 537 | new FPTruncInst(V, DestTy, "Tr", BB->getTerminator())); |
| 538 | } else { |
| 539 | return PT->push_back( |
| 540 | new FPExtInst(V, DestTy, "ZE", BB->getTerminator())); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | }; |
| 546 | |
| 547 | struct SelectModifier: public Modifier { |
| 548 | SelectModifier(BasicBlock *BB, PieceTable *PT, Random *R): |
| 549 | Modifier(BB, PT, R) {} |
| 550 | |
| 551 | virtual void Act() { |
| 552 | // Try a bunch of different select configuration until a valid one is found. |
| 553 | Value *Val0 = getRandomVal(); |
| 554 | Value *Val1 = getRandomValue(Val0->getType()); |
| 555 | |
| 556 | Type *CondTy = Type::getInt1Ty(Context); |
| 557 | |
| 558 | // If the value type is a vector, and we allow vector select, then in 50% |
| 559 | // of the cases generate a vector select. |
| 560 | if (Val0->getType()->isVectorTy() && (Ran->Rand() % 1)) { |
| 561 | unsigned NumElem = cast<VectorType>(Val0->getType())->getNumElements(); |
| 562 | CondTy = VectorType::get(CondTy, NumElem); |
| 563 | } |
| 564 | |
| 565 | Value *Cond = getRandomValue(CondTy); |
| 566 | Value *V = SelectInst::Create(Cond, Val0, Val1, "Sl", BB->getTerminator()); |
| 567 | return PT->push_back(V); |
| 568 | } |
| 569 | }; |
| 570 | |
| 571 | |
| 572 | struct CmpModifier: public Modifier { |
| 573 | CmpModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
| 574 | virtual void Act() { |
| 575 | |
| 576 | Value *Val0 = getRandomVal(); |
| 577 | Value *Val1 = getRandomValue(Val0->getType()); |
| 578 | |
| 579 | if (Val0->getType()->isPointerTy()) return; |
| 580 | bool fp = Val0->getType()->getScalarType()->isFloatingPointTy(); |
| 581 | |
| 582 | int op; |
| 583 | if (fp) { |
| 584 | op = Ran->Rand() % |
| 585 | (CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) + |
| 586 | CmpInst::FIRST_FCMP_PREDICATE; |
| 587 | } else { |
| 588 | op = Ran->Rand() % |
| 589 | (CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) + |
| 590 | CmpInst::FIRST_ICMP_PREDICATE; |
| 591 | } |
| 592 | |
| 593 | Value *V = CmpInst::Create(fp ? Instruction::FCmp : Instruction::ICmp, |
| 594 | op, Val0, Val1, "Cmp", BB->getTerminator()); |
| 595 | return PT->push_back(V); |
| 596 | } |
| 597 | }; |
| 598 | |
| 599 | void FillFunction(Function *F) { |
| 600 | // Create a legal entry block. |
| 601 | BasicBlock *BB = BasicBlock::Create(F->getContext(), "BB", F); |
| 602 | ReturnInst::Create(F->getContext(), BB); |
| 603 | |
| 604 | // Create the value table. |
| 605 | Modifier::PieceTable PT; |
| 606 | // Pick an initial seed value |
| 607 | Random R(SeedCL); |
| 608 | |
| 609 | // Consider arguments as legal values. |
| 610 | for (Function::arg_iterator it = F->arg_begin(), e = F->arg_end(); |
| 611 | it != e; ++it) |
| 612 | PT.push_back(it); |
| 613 | |
| 614 | // List of modifiers which add new random instructions. |
| 615 | std::vector<Modifier*> Modifiers; |
| 616 | std::auto_ptr<Modifier> LM(new LoadModifier(BB, &PT, &R)); |
| 617 | std::auto_ptr<Modifier> SM(new StoreModifier(BB, &PT, &R)); |
| 618 | std::auto_ptr<Modifier> EE(new ExtractElementModifier(BB, &PT, &R)); |
| 619 | std::auto_ptr<Modifier> SHM(new ShuffModifier(BB, &PT, &R)); |
| 620 | std::auto_ptr<Modifier> IE(new InsertElementModifier(BB, &PT, &R)); |
| 621 | std::auto_ptr<Modifier> BM(new BinModifier(BB, &PT, &R)); |
| 622 | std::auto_ptr<Modifier> CM(new CastModifier(BB, &PT, &R)); |
| 623 | std::auto_ptr<Modifier> SLM(new SelectModifier(BB, &PT, &R)); |
| 624 | std::auto_ptr<Modifier> PM(new CmpModifier(BB, &PT, &R)); |
| 625 | Modifiers.push_back(LM.get()); |
| 626 | Modifiers.push_back(SM.get()); |
| 627 | Modifiers.push_back(EE.get()); |
| 628 | Modifiers.push_back(SHM.get()); |
| 629 | Modifiers.push_back(IE.get()); |
| 630 | Modifiers.push_back(BM.get()); |
| 631 | Modifiers.push_back(CM.get()); |
| 632 | Modifiers.push_back(SLM.get()); |
| 633 | Modifiers.push_back(PM.get()); |
| 634 | |
| 635 | // Generate the random instructions |
| 636 | AllocaModifier AM(BB, &PT, &R); AM.ActN(5); // Throw in a few allocas |
| 637 | ConstModifier COM(BB, &PT, &R); COM.ActN(40); // Throw in a few constants |
| 638 | |
| 639 | for (unsigned i=0; i< SizeCL / Modifiers.size(); ++i) |
| 640 | for (std::vector<Modifier*>::iterator it = Modifiers.begin(), |
| 641 | e = Modifiers.end(); it != e; ++it) { |
| 642 | (*it)->Act(); |
| 643 | } |
| 644 | |
| 645 | SM->ActN(5); // Throw in a few stores. |
| 646 | } |
| 647 | |
| 648 | void IntroduceControlFlow(Function *F) { |
| 649 | std::set<Instruction*> BoolInst; |
| 650 | for (BasicBlock::iterator it = F->begin()->begin(), |
| 651 | e = F->begin()->end(); it != e; ++it) { |
| 652 | if (it->getType() == IntegerType::getInt1Ty(F->getContext())) |
| 653 | BoolInst.insert(it); |
| 654 | } |
| 655 | |
| 656 | for (std::set<Instruction*>::iterator it = BoolInst.begin(), |
| 657 | e = BoolInst.end(); it != e; ++it) { |
| 658 | Instruction *Instr = *it; |
| 659 | BasicBlock *Curr = Instr->getParent(); |
| 660 | BasicBlock::iterator Loc= Instr; |
| 661 | BasicBlock *Next = Curr->splitBasicBlock(Loc, "CF"); |
| 662 | Instr->moveBefore(Curr->getTerminator()); |
| 663 | if (Curr != &F->getEntryBlock()) { |
| 664 | BranchInst::Create(Curr, Next, Instr, Curr->getTerminator()); |
| 665 | Curr->getTerminator()->eraseFromParent(); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | int main(int argc, char **argv) { |
| 671 | // Init LLVM, call llvm_shutdown() on exit, parse args, etc. |
| 672 | llvm::PrettyStackTraceProgram X(argc, argv); |
| 673 | cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n"); |
| 674 | llvm_shutdown_obj Y; |
| 675 | |
| 676 | std::auto_ptr<Module> M(new Module("/tmp/autogen.bc", getGlobalContext())); |
| 677 | Function *F = GenEmptyFunction(M.get()); |
| 678 | FillFunction(F); |
| 679 | IntroduceControlFlow(F); |
| 680 | |
| 681 | // Figure out what stream we are supposed to write to... |
| 682 | OwningPtr<tool_output_file> Out; |
| 683 | // Default to standard output. |
| 684 | if (OutputFilename.empty()) |
| 685 | OutputFilename = "-"; |
| 686 | |
| 687 | std::string ErrorInfo; |
| 688 | Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo, |
| 689 | raw_fd_ostream::F_Binary)); |
| 690 | if (!ErrorInfo.empty()) { |
| 691 | errs() << ErrorInfo << '\n'; |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | PassManager Passes; |
| 696 | Passes.add(createVerifierPass()); |
| 697 | Passes.add(createPrintModulePass(&Out->os())); |
| 698 | Passes.run(*M.get()); |
| 699 | Out->keep(); |
| 700 | |
| 701 | return 0; |
| 702 | } |