1//===- ReducerWorkItem.h - Wrapper for Module -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_TOOLS_LLVM_REDUCE_REDUCERWORKITEM_H
10#define LLVM_TOOLS_LLVM_REDUCE_REDUCERWORKITEM_H
11
12#include "llvm/IR/Module.h"
13#include <memory>
14
15namespace llvm {
16class LLVMContext;
17class MachineModuleInfo;
18class MemoryBufferRef;
19class raw_ostream;
20class TargetMachine;
21class TestRunner;
22struct BitcodeLTOInfo;
23
24class ReducerWorkItem {
25public:
26 std::shared_ptr<Module> M;
27 std::unique_ptr<BitcodeLTOInfo> LTOInfo;
28 std::unique_ptr<MachineModuleInfo> MMI;
29
30 ReducerWorkItem();
31 ~ReducerWorkItem();
32 ReducerWorkItem(ReducerWorkItem &) = delete;
33 ReducerWorkItem(ReducerWorkItem &&) = default;
34
35 bool isMIR() const { return MMI != nullptr; }
36
37 LLVMContext &getContext() {
38 return M->getContext();
39 }
40
41 Module &getModule() { return *M; }
42 const Module &getModule() const { return *M; }
43 operator Module &() const { return *M; }
44
45 void print(raw_ostream &ROS, void *p = nullptr) const;
46 bool verify(raw_fd_ostream *OS) const;
47 std::unique_ptr<ReducerWorkItem> clone(const TargetMachine *TM) const;
48
49 /// Return a number to indicate whether there was any reduction progress.
50 uint64_t getComplexityScore() const {
51 return isMIR() ? computeMIRComplexityScore() : computeIRComplexityScore();
52 }
53
54 void writeOutput(raw_ostream &OS, bool EmitBitcode) const;
55 void readBitcode(MemoryBufferRef Data, LLVMContext &Ctx, StringRef ToolName);
56 void writeBitcode(raw_ostream &OutStream) const;
57
58 bool isReduced(const TestRunner &Test) const;
59
60private:
61 uint64_t computeIRComplexityScore() const;
62 uint64_t computeMIRComplexityScore() const;
63};
64
65std::pair<std::unique_ptr<ReducerWorkItem>, bool>
66parseReducerWorkItem(StringRef ToolName, StringRef Filename, LLVMContext &Ctxt,
67 std::unique_ptr<TargetMachine> &TM, bool IsMIR);
68} // namespace llvm
69
70#endif
71