1353358Sdim//===- RandomIRBuilder.h - Utils for randomly mutation IR -------*- C++ -*-===//
2326938Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6326938Sdim//
7326938Sdim//===----------------------------------------------------------------------===//
8326938Sdim//
9326938Sdim// Provides the Mutator class, which is used to mutate IR for fuzzing.
10326938Sdim//
11326938Sdim//===----------------------------------------------------------------------===//
12326938Sdim
13326938Sdim#ifndef LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
14326938Sdim#define LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
15326938Sdim
16326938Sdim#include "llvm/ADT/SmallPtrSet.h"
17326938Sdim#include "llvm/FuzzMutate/IRMutator.h"
18326938Sdim#include "llvm/FuzzMutate/Random.h"
19326938Sdim#include <random>
20326938Sdim
21326938Sdimnamespace llvm {
22326938Sdim
23326938Sdimusing RandomEngine = std::mt19937;
24326938Sdim
25326938Sdimstruct RandomIRBuilder {
26326938Sdim  RandomEngine Rand;
27326938Sdim  SmallVector<Type *, 16> KnownTypes;
28326938Sdim
29326938Sdim  RandomIRBuilder(int Seed, ArrayRef<Type *> AllowedTypes)
30326938Sdim      : Rand(Seed), KnownTypes(AllowedTypes.begin(), AllowedTypes.end()) {}
31326938Sdim
32326938Sdim  // TODO: Try to make this a bit less of a random mishmash of functions.
33326938Sdim
34326938Sdim  /// Find a "source" for some operation, which will be used in one of the
35326938Sdim  /// operation's operands. This either selects an instruction in \c Insts or
36326938Sdim  /// returns some new arbitrary Value.
37326938Sdim  Value *findOrCreateSource(BasicBlock &BB, ArrayRef<Instruction *> Insts);
38326938Sdim  /// Find a "source" for some operation, which will be used in one of the
39326938Sdim  /// operation's operands. This either selects an instruction in \c Insts that
40326938Sdim  /// matches \c Pred, or returns some new Value that matches \c Pred. The
41326938Sdim  /// values in \c Srcs should be source operands that have already been
42326938Sdim  /// selected.
43326938Sdim  Value *findOrCreateSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
44326938Sdim                            ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred);
45326938Sdim  /// Create some Value suitable as a source for some operation.
46326938Sdim  Value *newSource(BasicBlock &BB, ArrayRef<Instruction *> Insts,
47326938Sdim                   ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred);
48326938Sdim  /// Find a viable user for \c V in \c Insts, which should all be contained in
49326938Sdim  /// \c BB. This may also create some new instruction in \c BB and use that.
50326938Sdim  void connectToSink(BasicBlock &BB, ArrayRef<Instruction *> Insts, Value *V);
51326938Sdim  /// Create a user for \c V in \c BB.
52326938Sdim  void newSink(BasicBlock &BB, ArrayRef<Instruction *> Insts, Value *V);
53326938Sdim  Value *findPointer(BasicBlock &BB, ArrayRef<Instruction *> Insts,
54326938Sdim                     ArrayRef<Value *> Srcs, fuzzerop::SourcePred Pred);
55326938Sdim  Type *chooseType(LLVMContext &Context, ArrayRef<Value *> Srcs,
56326938Sdim                   fuzzerop::SourcePred Pred);
57326938Sdim};
58326938Sdim
59326938Sdim} // end llvm namespace
60326938Sdim
61326938Sdim#endif // LLVM_FUZZMUTATE_RANDOMIRBUILDER_H
62