Daniel Dunbar | beb3425 | 2012-02-29 00:20:33 +0000 | [diff] [blame] | 1 | //===-- llvm-stress.cpp - Generate random LL files to stress-test LLVM ----===// |
Nadav Rotem | 78bda89 | 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 | //===----------------------------------------------------------------------===// |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 14 | |
Chandler Carruth | 839a98e | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
Chandler Carruth | b8ddc70 | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | b8ddc70 | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LLVMContext.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 1b69ed8 | 2014-03-04 12:32:42 +0000 | [diff] [blame] | 21 | #include "llvm/IR/LegacyPassNameParser.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Module.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Verifier.h" |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FileSystem.h" |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ManagedStatic.h" |
| 27 | #include "llvm/Support/PluginLoader.h" |
| 28 | #include "llvm/Support/PrettyStackTrace.h" |
| 29 | #include "llvm/Support/ToolOutputFile.h" |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 31 | #include <random> |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 32 | #include <vector> |
Pawel Bylica | edb0210 | 2015-07-10 10:01:47 +0000 | [diff] [blame] | 33 | |
| 34 | namespace llvm { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 35 | |
| 36 | static cl::opt<unsigned> SeedCL("seed", |
| 37 | cl::desc("Seed used for randomness"), cl::init(0)); |
| 38 | static cl::opt<unsigned> SizeCL("size", |
| 39 | cl::desc("The estimated size of the generated function (# of instrs)"), |
| 40 | cl::init(100)); |
| 41 | static cl::opt<std::string> |
| 42 | OutputFilename("o", cl::desc("Override output filename"), |
| 43 | cl::value_desc("filename")); |
| 44 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 45 | static LLVMContext Context; |
| 46 | |
Pawel Bylica | edb0210 | 2015-07-10 10:01:47 +0000 | [diff] [blame] | 47 | namespace cl { |
| 48 | template <> class parser<Type*> final : public basic_parser<Type*> { |
| 49 | public: |
| 50 | parser(Option &O) : basic_parser(O) {} |
| 51 | |
| 52 | // Parse options as IR types. Return true on error. |
| 53 | bool parse(Option &O, StringRef, StringRef Arg, Type *&Value) { |
Pawel Bylica | edb0210 | 2015-07-10 10:01:47 +0000 | [diff] [blame] | 54 | if (Arg == "half") Value = Type::getHalfTy(Context); |
| 55 | else if (Arg == "fp128") Value = Type::getFP128Ty(Context); |
| 56 | else if (Arg == "x86_fp80") Value = Type::getX86_FP80Ty(Context); |
| 57 | else if (Arg == "ppc_fp128") Value = Type::getPPC_FP128Ty(Context); |
| 58 | else if (Arg == "x86_mmx") Value = Type::getX86_MMXTy(Context); |
| 59 | else if (Arg.startswith("i")) { |
| 60 | unsigned N = 0; |
| 61 | Arg.drop_front().getAsInteger(10, N); |
| 62 | if (N > 0) |
| 63 | Value = Type::getIntNTy(Context, N); |
| 64 | } |
| 65 | |
| 66 | if (!Value) |
| 67 | return O.error("Invalid IR scalar type: '" + Arg + "'!"); |
| 68 | return false; |
| 69 | } |
| 70 | |
Mehdi Amini | e11b745 | 2016-10-01 03:43:20 +0000 | [diff] [blame] | 71 | StringRef getValueName() const override { return "IR scalar type"; } |
Pawel Bylica | edb0210 | 2015-07-10 10:01:47 +0000 | [diff] [blame] | 72 | }; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | static cl::list<Type*> AdditionalScalarTypes("types", cl::CommaSeparated, |
| 77 | cl::desc("Additional IR scalar types " |
| 78 | "(always includes i1, i8, i16, i32, i64, float and double)")); |
Hal Finkel | c947412 | 2012-02-27 23:59:33 +0000 | [diff] [blame] | 79 | |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 80 | namespace { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 81 | /// A utility class to provide a pseudo-random number generator which is |
| 82 | /// the same across all platforms. This is somewhat close to the libc |
| 83 | /// implementation. Note: This is not a cryptographically secure pseudorandom |
| 84 | /// number generator. |
| 85 | class Random { |
| 86 | public: |
| 87 | /// C'tor |
| 88 | Random(unsigned _seed):Seed(_seed) {} |
Dylan Noblesmith | 68f310d | 2012-04-10 22:44:51 +0000 | [diff] [blame] | 89 | |
| 90 | /// Return a random integer, up to a |
| 91 | /// maximum of 2**19 - 1. |
| 92 | uint32_t Rand() { |
| 93 | uint32_t Val = Seed + 0x000b07a1; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 94 | Seed = (Val * 0x3c7c0ac1); |
| 95 | // Only lowest 19 bits are random-ish. |
| 96 | return Seed & 0x7ffff; |
| 97 | } |
| 98 | |
Dylan Noblesmith | 68f310d | 2012-04-10 22:44:51 +0000 | [diff] [blame] | 99 | /// Return a random 32 bit integer. |
| 100 | uint32_t Rand32() { |
| 101 | uint32_t Val = Rand(); |
| 102 | Val &= 0xffff; |
| 103 | return Val | (Rand() << 16); |
| 104 | } |
| 105 | |
| 106 | /// Return a random 64 bit integer. |
| 107 | uint64_t Rand64() { |
| 108 | uint64_t Val = Rand32(); |
| 109 | return Val | (uint64_t(Rand32()) << 32); |
| 110 | } |
Nadav Rotem | 0fb7408 | 2012-06-21 08:58:15 +0000 | [diff] [blame] | 111 | |
| 112 | /// Rand operator for STL algorithms. |
| 113 | ptrdiff_t operator()(ptrdiff_t y) { |
| 114 | return Rand64() % y; |
| 115 | } |
| 116 | |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 117 | /// Make this like a C++11 random device |
| 118 | typedef uint32_t result_type; |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 119 | static constexpr result_type min() { return 0; } |
| 120 | static constexpr result_type max() { return 0x7ffff; } |
Simon Pilgrim | 1158fe9 | 2017-06-26 10:16:34 +0000 | [diff] [blame^] | 121 | uint32_t operator()() { |
| 122 | uint32_t Val = Rand(); |
| 123 | assert(Val <= max() && "Random value out of range"); |
| 124 | return Val; |
| 125 | } |
| 126 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 127 | private: |
| 128 | unsigned Seed; |
| 129 | }; |
| 130 | |
| 131 | /// Generate an empty function with a default argument list. |
| 132 | Function *GenEmptyFunction(Module *M) { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 133 | // Define a few arguments |
| 134 | LLVMContext &Context = M->getContext(); |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 135 | Type* ArgsTy[] = { |
| 136 | Type::getInt8PtrTy(Context), |
| 137 | Type::getInt32PtrTy(Context), |
| 138 | Type::getInt64PtrTy(Context), |
| 139 | Type::getInt32Ty(Context), |
| 140 | Type::getInt64Ty(Context), |
| 141 | Type::getInt8Ty(Context) |
| 142 | }; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 143 | |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 144 | auto *FuncTy = FunctionType::get(Type::getVoidTy(Context), ArgsTy, false); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 145 | // Pick a unique name to describe the input parameters |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 146 | Twine Name = "autogen_SD" + Twine{SeedCL}; |
| 147 | auto *Func = Function::Create(FuncTy, GlobalValue::ExternalLinkage, Name, M); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 148 | Func->setCallingConv(CallingConv::C); |
| 149 | return Func; |
| 150 | } |
| 151 | |
| 152 | /// A base class, implementing utilities needed for |
| 153 | /// modifying and adding new random instructions. |
| 154 | struct Modifier { |
| 155 | /// Used to store the randomly generated values. |
| 156 | typedef std::vector<Value*> PieceTable; |
| 157 | |
| 158 | public: |
| 159 | /// C'tor |
Nadav Rotem | dc497b6 | 2012-02-26 08:59:25 +0000 | [diff] [blame] | 160 | Modifier(BasicBlock *Block, PieceTable *PT, Random *R): |
Daniel Dunbar | beb3425 | 2012-02-29 00:20:33 +0000 | [diff] [blame] | 161 | BB(Block),PT(PT),Ran(R),Context(BB->getContext()) {} |
Andrew Trick | becbbbe | 2012-09-19 05:08:30 +0000 | [diff] [blame] | 162 | |
| 163 | /// virtual D'tor to silence warnings. |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 164 | virtual ~Modifier() {} |
Andrew Trick | becbbbe | 2012-09-19 05:08:30 +0000 | [diff] [blame] | 165 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 166 | /// Add a new instruction. |
| 167 | virtual void Act() = 0; |
| 168 | /// Add N new instructions, |
| 169 | virtual void ActN(unsigned n) { |
| 170 | for (unsigned i=0; i<n; ++i) |
| 171 | Act(); |
| 172 | } |
| 173 | |
| 174 | protected: |
| 175 | /// Return a random value from the list of known values. |
| 176 | Value *getRandomVal() { |
| 177 | assert(PT->size()); |
| 178 | return PT->at(Ran->Rand() % PT->size()); |
| 179 | } |
| 180 | |
Nadav Rotem | e4972dd | 2012-02-26 13:56:18 +0000 | [diff] [blame] | 181 | Constant *getRandomConstant(Type *Tp) { |
| 182 | if (Tp->isIntegerTy()) { |
| 183 | if (Ran->Rand() & 1) |
| 184 | return ConstantInt::getAllOnesValue(Tp); |
| 185 | return ConstantInt::getNullValue(Tp); |
| 186 | } else if (Tp->isFloatingPointTy()) { |
| 187 | if (Ran->Rand() & 1) |
| 188 | return ConstantFP::getAllOnesValue(Tp); |
| 189 | return ConstantFP::getNullValue(Tp); |
| 190 | } |
| 191 | return UndefValue::get(Tp); |
| 192 | } |
| 193 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 194 | /// Return a random value with a known type. |
| 195 | Value *getRandomValue(Type *Tp) { |
| 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() == Tp) |
| 200 | return V; |
| 201 | } |
| 202 | |
| 203 | // If the requested type was not found, generate a constant value. |
| 204 | if (Tp->isIntegerTy()) { |
| 205 | if (Ran->Rand() & 1) |
| 206 | return ConstantInt::getAllOnesValue(Tp); |
| 207 | return ConstantInt::getNullValue(Tp); |
| 208 | } else if (Tp->isFloatingPointTy()) { |
| 209 | if (Ran->Rand() & 1) |
| 210 | return ConstantFP::getAllOnesValue(Tp); |
| 211 | return ConstantFP::getNullValue(Tp); |
Nadav Rotem | e4972dd | 2012-02-26 13:56:18 +0000 | [diff] [blame] | 212 | } else if (Tp->isVectorTy()) { |
| 213 | VectorType *VTp = cast<VectorType>(Tp); |
| 214 | |
| 215 | std::vector<Constant*> TempValues; |
| 216 | TempValues.reserve(VTp->getNumElements()); |
| 217 | for (unsigned i = 0; i < VTp->getNumElements(); ++i) |
| 218 | TempValues.push_back(getRandomConstant(VTp->getScalarType())); |
| 219 | |
| 220 | ArrayRef<Constant*> VectorValue(TempValues); |
| 221 | return ConstantVector::get(VectorValue); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 224 | return UndefValue::get(Tp); |
| 225 | } |
| 226 | |
| 227 | /// Return a random value of any pointer type. |
| 228 | Value *getRandomPointerValue() { |
| 229 | unsigned index = Ran->Rand(); |
| 230 | for (unsigned i=0; i<PT->size(); ++i) { |
| 231 | Value *V = PT->at((index + i) % PT->size()); |
| 232 | if (V->getType()->isPointerTy()) |
| 233 | return V; |
| 234 | } |
| 235 | return UndefValue::get(pickPointerType()); |
| 236 | } |
| 237 | |
| 238 | /// Return a random value of any vector type. |
| 239 | Value *getRandomVectorValue() { |
| 240 | unsigned index = Ran->Rand(); |
| 241 | for (unsigned i=0; i<PT->size(); ++i) { |
| 242 | Value *V = PT->at((index + i) % PT->size()); |
| 243 | if (V->getType()->isVectorTy()) |
| 244 | return V; |
| 245 | } |
| 246 | return UndefValue::get(pickVectorType()); |
| 247 | } |
| 248 | |
| 249 | /// Pick a random type. |
| 250 | Type *pickType() { |
| 251 | return (Ran->Rand() & 1 ? pickVectorType() : pickScalarType()); |
| 252 | } |
| 253 | |
| 254 | /// Pick a random pointer type. |
| 255 | Type *pickPointerType() { |
| 256 | Type *Ty = pickType(); |
| 257 | return PointerType::get(Ty, 0); |
| 258 | } |
| 259 | |
| 260 | /// Pick a random vector type. |
| 261 | Type *pickVectorType(unsigned len = (unsigned)-1) { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 262 | // Pick a random vector width in the range 2**0 to 2**4. |
| 263 | // by adding two randoms we are generating a normal-like distribution |
| 264 | // around 2**3. |
| 265 | unsigned width = 1<<((Ran->Rand() % 3) + (Ran->Rand() % 3)); |
Dylan Noblesmith | 2a592dc | 2012-04-10 22:44:49 +0000 | [diff] [blame] | 266 | Type *Ty; |
| 267 | |
| 268 | // Vectors of x86mmx are illegal; keep trying till we get something else. |
| 269 | do { |
| 270 | Ty = pickScalarType(); |
| 271 | } while (Ty->isX86_MMXTy()); |
| 272 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 273 | if (len != (unsigned)-1) |
| 274 | width = len; |
| 275 | return VectorType::get(Ty, width); |
| 276 | } |
| 277 | |
| 278 | /// Pick a random scalar type. |
| 279 | Type *pickScalarType() { |
Pawel Bylica | edb0210 | 2015-07-10 10:01:47 +0000 | [diff] [blame] | 280 | static std::vector<Type*> ScalarTypes; |
| 281 | if (ScalarTypes.empty()) { |
| 282 | ScalarTypes.assign({ |
| 283 | Type::getInt1Ty(Context), |
| 284 | Type::getInt8Ty(Context), |
| 285 | Type::getInt16Ty(Context), |
| 286 | Type::getInt32Ty(Context), |
| 287 | Type::getInt64Ty(Context), |
| 288 | Type::getFloatTy(Context), |
| 289 | Type::getDoubleTy(Context) |
| 290 | }); |
| 291 | ScalarTypes.insert(ScalarTypes.end(), |
| 292 | AdditionalScalarTypes.begin(), AdditionalScalarTypes.end()); |
| 293 | } |
Hal Finkel | c947412 | 2012-02-27 23:59:33 +0000 | [diff] [blame] | 294 | |
Pawel Bylica | edb0210 | 2015-07-10 10:01:47 +0000 | [diff] [blame] | 295 | return ScalarTypes[Ran->Rand() % ScalarTypes.size()]; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /// Basic block to populate |
| 299 | BasicBlock *BB; |
| 300 | /// Value table |
| 301 | PieceTable *PT; |
| 302 | /// Random number generator |
| 303 | Random *Ran; |
| 304 | /// Context |
| 305 | LLVMContext &Context; |
| 306 | }; |
| 307 | |
| 308 | struct LoadModifier: public Modifier { |
Daniel Dunbar | beb3425 | 2012-02-29 00:20:33 +0000 | [diff] [blame] | 309 | LoadModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 310 | void Act() override { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 311 | // Try to use predefined pointers. If non-exist, use undef pointer value; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 312 | Value *Ptr = getRandomPointerValue(); |
| 313 | Value *V = new LoadInst(Ptr, "L", BB->getTerminator()); |
| 314 | PT->push_back(V); |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | struct StoreModifier: public Modifier { |
| 319 | StoreModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 320 | void Act() override { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 321 | // Try to use predefined pointers. If non-exist, use undef pointer value; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 322 | Value *Ptr = getRandomPointerValue(); |
| 323 | Type *Tp = Ptr->getType(); |
| 324 | Value *Val = getRandomValue(Tp->getContainedType(0)); |
Nadav Rotem | 63ff91d | 2012-02-26 12:00:22 +0000 | [diff] [blame] | 325 | Type *ValTy = Val->getType(); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 326 | |
| 327 | // Do not store vectors of i1s because they are unsupported |
Nadav Rotem | 115ec82 | 2012-02-26 12:34:17 +0000 | [diff] [blame] | 328 | // by the codegen. |
| 329 | if (ValTy->isVectorTy() && ValTy->getScalarSizeInBits() == 1) |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 330 | return; |
| 331 | |
| 332 | new StoreInst(Val, Ptr, BB->getTerminator()); |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | struct BinModifier: public Modifier { |
| 337 | BinModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
| 338 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 339 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 340 | Value *Val0 = getRandomVal(); |
| 341 | Value *Val1 = getRandomValue(Val0->getType()); |
| 342 | |
| 343 | // Don't handle pointer types. |
| 344 | if (Val0->getType()->isPointerTy() || |
| 345 | Val1->getType()->isPointerTy()) |
| 346 | return; |
| 347 | |
| 348 | // Don't handle i1 types. |
| 349 | if (Val0->getType()->getScalarSizeInBits() == 1) |
| 350 | return; |
| 351 | |
| 352 | |
| 353 | bool isFloat = Val0->getType()->getScalarType()->isFloatingPointTy(); |
| 354 | Instruction* Term = BB->getTerminator(); |
| 355 | unsigned R = Ran->Rand() % (isFloat ? 7 : 13); |
| 356 | Instruction::BinaryOps Op; |
| 357 | |
| 358 | switch (R) { |
| 359 | default: llvm_unreachable("Invalid BinOp"); |
| 360 | case 0:{Op = (isFloat?Instruction::FAdd : Instruction::Add); break; } |
| 361 | case 1:{Op = (isFloat?Instruction::FSub : Instruction::Sub); break; } |
| 362 | case 2:{Op = (isFloat?Instruction::FMul : Instruction::Mul); break; } |
| 363 | case 3:{Op = (isFloat?Instruction::FDiv : Instruction::SDiv); break; } |
| 364 | case 4:{Op = (isFloat?Instruction::FDiv : Instruction::UDiv); break; } |
| 365 | case 5:{Op = (isFloat?Instruction::FRem : Instruction::SRem); break; } |
| 366 | case 6:{Op = (isFloat?Instruction::FRem : Instruction::URem); break; } |
| 367 | case 7: {Op = Instruction::Shl; break; } |
| 368 | case 8: {Op = Instruction::LShr; break; } |
| 369 | case 9: {Op = Instruction::AShr; break; } |
| 370 | case 10:{Op = Instruction::And; break; } |
| 371 | case 11:{Op = Instruction::Or; break; } |
| 372 | case 12:{Op = Instruction::Xor; break; } |
| 373 | } |
| 374 | |
| 375 | PT->push_back(BinaryOperator::Create(Op, Val0, Val1, "B", Term)); |
| 376 | } |
| 377 | }; |
| 378 | |
| 379 | /// Generate constant values. |
| 380 | struct ConstModifier: public Modifier { |
| 381 | ConstModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 382 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 383 | Type *Ty = pickType(); |
| 384 | |
| 385 | if (Ty->isVectorTy()) { |
| 386 | switch (Ran->Rand() % 2) { |
| 387 | case 0: if (Ty->getScalarType()->isIntegerTy()) |
| 388 | return PT->push_back(ConstantVector::getAllOnesValue(Ty)); |
Galina Kistanova | b644814 | 2017-06-10 18:26:19 +0000 | [diff] [blame] | 389 | break; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 390 | case 1: if (Ty->getScalarType()->isIntegerTy()) |
| 391 | return PT->push_back(ConstantVector::getNullValue(Ty)); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | if (Ty->isFloatingPointTy()) { |
Dylan Noblesmith | 68f310d | 2012-04-10 22:44:51 +0000 | [diff] [blame] | 396 | // Generate 128 random bits, the size of the (currently) |
| 397 | // largest floating-point types. |
| 398 | uint64_t RandomBits[2]; |
| 399 | for (unsigned i = 0; i < 2; ++i) |
| 400 | RandomBits[i] = Ran->Rand64(); |
| 401 | |
| 402 | APInt RandomInt(Ty->getPrimitiveSizeInBits(), makeArrayRef(RandomBits)); |
Tim Northover | 98d9b7e | 2013-01-22 10:18:26 +0000 | [diff] [blame] | 403 | APFloat RandomFloat(Ty->getFltSemantics(), RandomInt); |
Dylan Noblesmith | 68f310d | 2012-04-10 22:44:51 +0000 | [diff] [blame] | 404 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 405 | if (Ran->Rand() & 1) |
| 406 | return PT->push_back(ConstantFP::getNullValue(Ty)); |
Dylan Noblesmith | 68f310d | 2012-04-10 22:44:51 +0000 | [diff] [blame] | 407 | return PT->push_back(ConstantFP::get(Ty->getContext(), RandomFloat)); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if (Ty->isIntegerTy()) { |
| 411 | switch (Ran->Rand() % 7) { |
David Blaikie | 30b2c6b | 2017-06-12 20:09:53 +0000 | [diff] [blame] | 412 | case 0: |
| 413 | return PT->push_back(ConstantInt::get( |
| 414 | Ty, APInt::getAllOnesValue(Ty->getPrimitiveSizeInBits()))); |
| 415 | case 1: |
| 416 | return PT->push_back(ConstantInt::get( |
| 417 | Ty, APInt::getNullValue(Ty->getPrimitiveSizeInBits()))); |
David Blaikie | 8f9621a | 2017-06-21 15:20:46 +0000 | [diff] [blame] | 418 | case 2: |
| 419 | case 3: |
| 420 | case 4: |
| 421 | case 5: |
David Blaikie | 30b2c6b | 2017-06-12 20:09:53 +0000 | [diff] [blame] | 422 | case 6: |
| 423 | PT->push_back(ConstantInt::get(Ty, Ran->Rand())); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 426 | } |
| 427 | }; |
| 428 | |
| 429 | struct AllocaModifier: public Modifier { |
| 430 | AllocaModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R){} |
| 431 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 432 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 433 | Type *Tp = pickType(); |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 434 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
| 435 | PT->push_back(new AllocaInst(Tp, DL.getAllocaAddrSpace(), |
| 436 | "A", BB->getFirstNonPHI())); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 437 | } |
| 438 | }; |
| 439 | |
| 440 | struct ExtractElementModifier: public Modifier { |
| 441 | ExtractElementModifier(BasicBlock *BB, PieceTable *PT, Random *R): |
| 442 | Modifier(BB, PT, R) {} |
| 443 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 444 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 445 | Value *Val0 = getRandomVectorValue(); |
| 446 | Value *V = ExtractElementInst::Create(Val0, |
| 447 | ConstantInt::get(Type::getInt32Ty(BB->getContext()), |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 448 | Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()), |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 449 | "E", BB->getTerminator()); |
| 450 | return PT->push_back(V); |
| 451 | } |
| 452 | }; |
| 453 | |
| 454 | struct ShuffModifier: public Modifier { |
| 455 | ShuffModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 456 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 457 | |
| 458 | Value *Val0 = getRandomVectorValue(); |
| 459 | Value *Val1 = getRandomValue(Val0->getType()); |
| 460 | |
| 461 | unsigned Width = cast<VectorType>(Val0->getType())->getNumElements(); |
| 462 | std::vector<Constant*> Idxs; |
| 463 | |
| 464 | Type *I32 = Type::getInt32Ty(BB->getContext()); |
| 465 | for (unsigned i=0; i<Width; ++i) { |
| 466 | Constant *CI = ConstantInt::get(I32, Ran->Rand() % (Width*2)); |
| 467 | // Pick some undef values. |
| 468 | if (!(Ran->Rand() % 5)) |
| 469 | CI = UndefValue::get(I32); |
| 470 | Idxs.push_back(CI); |
| 471 | } |
| 472 | |
| 473 | Constant *Mask = ConstantVector::get(Idxs); |
| 474 | |
| 475 | Value *V = new ShuffleVectorInst(Val0, Val1, Mask, "Shuff", |
| 476 | BB->getTerminator()); |
| 477 | PT->push_back(V); |
| 478 | } |
| 479 | }; |
| 480 | |
| 481 | struct InsertElementModifier: public Modifier { |
| 482 | InsertElementModifier(BasicBlock *BB, PieceTable *PT, Random *R): |
| 483 | Modifier(BB, PT, R) {} |
| 484 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 485 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 486 | Value *Val0 = getRandomVectorValue(); |
| 487 | Value *Val1 = getRandomValue(Val0->getType()->getScalarType()); |
| 488 | |
| 489 | Value *V = InsertElementInst::Create(Val0, Val1, |
| 490 | ConstantInt::get(Type::getInt32Ty(BB->getContext()), |
| 491 | Ran->Rand() % cast<VectorType>(Val0->getType())->getNumElements()), |
| 492 | "I", BB->getTerminator()); |
| 493 | return PT->push_back(V); |
| 494 | } |
| 495 | |
| 496 | }; |
| 497 | |
| 498 | struct CastModifier: public Modifier { |
| 499 | CastModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 500 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 501 | |
| 502 | Value *V = getRandomVal(); |
| 503 | Type *VTy = V->getType(); |
| 504 | Type *DestTy = pickScalarType(); |
| 505 | |
| 506 | // Handle vector casts vectors. |
| 507 | if (VTy->isVectorTy()) { |
| 508 | VectorType *VecTy = cast<VectorType>(VTy); |
| 509 | DestTy = pickVectorType(VecTy->getNumElements()); |
| 510 | } |
| 511 | |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 512 | // no need to cast. |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 513 | if (VTy == DestTy) return; |
| 514 | |
| 515 | // Pointers: |
| 516 | if (VTy->isPointerTy()) { |
| 517 | if (!DestTy->isPointerTy()) |
| 518 | DestTy = PointerType::get(DestTy, 0); |
| 519 | return PT->push_back( |
| 520 | new BitCastInst(V, DestTy, "PC", BB->getTerminator())); |
| 521 | } |
| 522 | |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 523 | unsigned VSize = VTy->getScalarType()->getPrimitiveSizeInBits(); |
| 524 | unsigned DestSize = DestTy->getScalarType()->getPrimitiveSizeInBits(); |
| 525 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 526 | // Generate lots of bitcasts. |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 527 | if ((Ran->Rand() & 1) && VSize == DestSize) { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 528 | return PT->push_back( |
| 529 | new BitCastInst(V, DestTy, "BC", BB->getTerminator())); |
| 530 | } |
| 531 | |
| 532 | // Both types are integers: |
| 533 | if (VTy->getScalarType()->isIntegerTy() && |
| 534 | DestTy->getScalarType()->isIntegerTy()) { |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 535 | if (VSize > DestSize) { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 536 | return PT->push_back( |
| 537 | new TruncInst(V, DestTy, "Tr", BB->getTerminator())); |
| 538 | } else { |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 539 | assert(VSize < DestSize && "Different int types with the same size?"); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 540 | if (Ran->Rand() & 1) |
| 541 | return PT->push_back( |
| 542 | new ZExtInst(V, DestTy, "ZE", BB->getTerminator())); |
| 543 | return PT->push_back(new SExtInst(V, DestTy, "Se", BB->getTerminator())); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // Fp to int. |
| 548 | if (VTy->getScalarType()->isFloatingPointTy() && |
| 549 | DestTy->getScalarType()->isIntegerTy()) { |
| 550 | if (Ran->Rand() & 1) |
| 551 | return PT->push_back( |
| 552 | new FPToSIInst(V, DestTy, "FC", BB->getTerminator())); |
| 553 | return PT->push_back(new FPToUIInst(V, DestTy, "FC", BB->getTerminator())); |
| 554 | } |
| 555 | |
| 556 | // Int to fp. |
| 557 | if (VTy->getScalarType()->isIntegerTy() && |
| 558 | DestTy->getScalarType()->isFloatingPointTy()) { |
| 559 | if (Ran->Rand() & 1) |
| 560 | return PT->push_back( |
| 561 | new SIToFPInst(V, DestTy, "FC", BB->getTerminator())); |
| 562 | return PT->push_back(new UIToFPInst(V, DestTy, "FC", BB->getTerminator())); |
| 563 | |
| 564 | } |
| 565 | |
| 566 | // Both floats. |
| 567 | if (VTy->getScalarType()->isFloatingPointTy() && |
| 568 | DestTy->getScalarType()->isFloatingPointTy()) { |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 569 | if (VSize > DestSize) { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 570 | return PT->push_back( |
| 571 | new FPTruncInst(V, DestTy, "Tr", BB->getTerminator())); |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 572 | } else if (VSize < DestSize) { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 573 | return PT->push_back( |
| 574 | new FPExtInst(V, DestTy, "ZE", BB->getTerminator())); |
| 575 | } |
Nadav Rotem | aeacc17 | 2012-04-15 20:17:14 +0000 | [diff] [blame] | 576 | // If VSize == DestSize, then the two types must be fp128 and ppc_fp128, |
| 577 | // for which there is no defined conversion. So do nothing. |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | }; |
| 582 | |
| 583 | struct SelectModifier: public Modifier { |
| 584 | SelectModifier(BasicBlock *BB, PieceTable *PT, Random *R): |
| 585 | Modifier(BB, PT, R) {} |
| 586 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 587 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 588 | // Try a bunch of different select configuration until a valid one is found. |
| 589 | Value *Val0 = getRandomVal(); |
| 590 | Value *Val1 = getRandomValue(Val0->getType()); |
| 591 | |
| 592 | Type *CondTy = Type::getInt1Ty(Context); |
| 593 | |
| 594 | // If the value type is a vector, and we allow vector select, then in 50% |
| 595 | // of the cases generate a vector select. |
| 596 | if (Val0->getType()->isVectorTy() && (Ran->Rand() % 1)) { |
| 597 | unsigned NumElem = cast<VectorType>(Val0->getType())->getNumElements(); |
| 598 | CondTy = VectorType::get(CondTy, NumElem); |
| 599 | } |
| 600 | |
| 601 | Value *Cond = getRandomValue(CondTy); |
| 602 | Value *V = SelectInst::Create(Cond, Val0, Val1, "Sl", BB->getTerminator()); |
| 603 | return PT->push_back(V); |
| 604 | } |
| 605 | }; |
| 606 | |
| 607 | |
| 608 | struct CmpModifier: public Modifier { |
| 609 | CmpModifier(BasicBlock *BB, PieceTable *PT, Random *R):Modifier(BB, PT, R) {} |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 610 | void Act() override { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 611 | |
| 612 | Value *Val0 = getRandomVal(); |
| 613 | Value *Val1 = getRandomValue(Val0->getType()); |
| 614 | |
| 615 | if (Val0->getType()->isPointerTy()) return; |
| 616 | bool fp = Val0->getType()->getScalarType()->isFloatingPointTy(); |
| 617 | |
| 618 | int op; |
| 619 | if (fp) { |
| 620 | op = Ran->Rand() % |
| 621 | (CmpInst::LAST_FCMP_PREDICATE - CmpInst::FIRST_FCMP_PREDICATE) + |
| 622 | CmpInst::FIRST_FCMP_PREDICATE; |
| 623 | } else { |
| 624 | op = Ran->Rand() % |
| 625 | (CmpInst::LAST_ICMP_PREDICATE - CmpInst::FIRST_ICMP_PREDICATE) + |
| 626 | CmpInst::FIRST_ICMP_PREDICATE; |
| 627 | } |
| 628 | |
| 629 | Value *V = CmpInst::Create(fp ? Instruction::FCmp : Instruction::ICmp, |
Craig Topper | 1c3f283 | 2015-12-15 06:11:33 +0000 | [diff] [blame] | 630 | (CmpInst::Predicate)op, Val0, Val1, "Cmp", |
| 631 | BB->getTerminator()); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 632 | return PT->push_back(V); |
| 633 | } |
| 634 | }; |
| 635 | |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 636 | } // end anonymous namespace |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 637 | |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 638 | static void FillFunction(Function *F, Random &R) { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 639 | // Create a legal entry block. |
| 640 | BasicBlock *BB = BasicBlock::Create(F->getContext(), "BB", F); |
| 641 | ReturnInst::Create(F->getContext(), BB); |
| 642 | |
| 643 | // Create the value table. |
| 644 | Modifier::PieceTable PT; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 645 | |
| 646 | // Consider arguments as legal values. |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 647 | for (auto &arg : F->args()) |
| 648 | PT.push_back(&arg); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 649 | |
| 650 | // List of modifiers which add new random instructions. |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 651 | std::vector<std::unique_ptr<Modifier>> Modifiers; |
| 652 | Modifiers.emplace_back(new LoadModifier(BB, &PT, &R)); |
| 653 | Modifiers.emplace_back(new StoreModifier(BB, &PT, &R)); |
| 654 | auto SM = Modifiers.back().get(); |
| 655 | Modifiers.emplace_back(new ExtractElementModifier(BB, &PT, &R)); |
| 656 | Modifiers.emplace_back(new ShuffModifier(BB, &PT, &R)); |
| 657 | Modifiers.emplace_back(new InsertElementModifier(BB, &PT, &R)); |
| 658 | Modifiers.emplace_back(new BinModifier(BB, &PT, &R)); |
| 659 | Modifiers.emplace_back(new CastModifier(BB, &PT, &R)); |
| 660 | Modifiers.emplace_back(new SelectModifier(BB, &PT, &R)); |
| 661 | Modifiers.emplace_back(new CmpModifier(BB, &PT, &R)); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 662 | |
| 663 | // Generate the random instructions |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 664 | AllocaModifier{BB, &PT, &R}.ActN(5); // Throw in a few allocas |
| 665 | ConstModifier{BB, &PT, &R}.ActN(40); // Throw in a few constants |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 666 | |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 667 | for (unsigned i = 0; i < SizeCL / Modifiers.size(); ++i) |
| 668 | for (auto &Mod : Modifiers) |
| 669 | Mod->Act(); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 670 | |
| 671 | SM->ActN(5); // Throw in a few stores. |
| 672 | } |
| 673 | |
Juergen Ributzka | 05c5a93 | 2013-11-19 03:08:35 +0000 | [diff] [blame] | 674 | static void IntroduceControlFlow(Function *F, Random &R) { |
Nadav Rotem | 0fb7408 | 2012-06-21 08:58:15 +0000 | [diff] [blame] | 675 | std::vector<Instruction*> BoolInst; |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 676 | for (auto &Instr : F->front()) { |
| 677 | if (Instr.getType() == IntegerType::getInt1Ty(F->getContext())) |
| 678 | BoolInst.push_back(&Instr); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 681 | std::shuffle(BoolInst.begin(), BoolInst.end(), R); |
Nadav Rotem | 0fb7408 | 2012-06-21 08:58:15 +0000 | [diff] [blame] | 682 | |
Pawel Bylica | 2366347 | 2015-06-24 11:49:44 +0000 | [diff] [blame] | 683 | for (auto *Instr : BoolInst) { |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 684 | BasicBlock *Curr = Instr->getParent(); |
Duncan P. N. Exon Smith | 83c4b68 | 2015-11-07 00:01:16 +0000 | [diff] [blame] | 685 | BasicBlock::iterator Loc = Instr->getIterator(); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 686 | BasicBlock *Next = Curr->splitBasicBlock(Loc, "CF"); |
| 687 | Instr->moveBefore(Curr->getTerminator()); |
| 688 | if (Curr != &F->getEntryBlock()) { |
| 689 | BranchInst::Create(Curr, Next, Instr, Curr->getTerminator()); |
| 690 | Curr->getTerminator()->eraseFromParent(); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
Pawel Bylica | edb0210 | 2015-07-10 10:01:47 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 697 | int main(int argc, char **argv) { |
Pawel Bylica | edb0210 | 2015-07-10 10:01:47 +0000 | [diff] [blame] | 698 | using namespace llvm; |
| 699 | |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 700 | // Init LLVM, call llvm_shutdown() on exit, parse args, etc. |
Pawel Bylica | 4dd430f | 2015-07-13 11:25:56 +0000 | [diff] [blame] | 701 | PrettyStackTraceProgram X(argc, argv); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 702 | cl::ParseCommandLineOptions(argc, argv, "llvm codegen stress-tester\n"); |
| 703 | llvm_shutdown_obj Y; |
| 704 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 705 | auto M = make_unique<Module>("/tmp/autogen.bc", Context); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 706 | Function *F = GenEmptyFunction(M.get()); |
Nadav Rotem | 0fb7408 | 2012-06-21 08:58:15 +0000 | [diff] [blame] | 707 | |
| 708 | // Pick an initial seed value |
| 709 | Random R(SeedCL); |
| 710 | // Generate lots of random instructions inside a single basic block. |
| 711 | FillFunction(F, R); |
| 712 | // Break the basic block into many loops. |
| 713 | IntroduceControlFlow(F, R); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 714 | |
| 715 | // Figure out what stream we are supposed to write to... |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 716 | std::unique_ptr<tool_output_file> Out; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 717 | // Default to standard output. |
| 718 | if (OutputFilename.empty()) |
| 719 | OutputFilename = "-"; |
| 720 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 721 | std::error_code EC; |
| 722 | Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None)); |
| 723 | if (EC) { |
| 724 | errs() << EC.message() << '\n'; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 725 | return 1; |
| 726 | } |
| 727 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 728 | legacy::PassManager Passes; |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 729 | Passes.add(createVerifierPass()); |
Chandler Carruth | 3bdf043 | 2014-01-12 11:39:04 +0000 | [diff] [blame] | 730 | Passes.add(createPrintModulePass(Out->os())); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 731 | Passes.run(*M.get()); |
Nadav Rotem | 78bda89 | 2012-02-26 08:35:53 +0000 | [diff] [blame] | 732 | Out->keep(); |
| 733 | |
| 734 | return 0; |
| 735 | } |