xref: /llvm-project/llvm/tools/llvm-reduce/ReducerWorkItem.h (revision 592536a9ec9318852c133b3392f2e271a08b0c68)
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 
15 namespace llvm {
16 class LLVMContext;
17 class MachineModuleInfo;
18 class MemoryBufferRef;
19 class raw_ostream;
20 class TargetMachine;
21 class TestRunner;
22 struct BitcodeLTOInfo;
23 
24 class ReducerWorkItem {
25 public:
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 
isMIR()35   bool isMIR() const { return MMI != nullptr; }
36 
getContext()37   LLVMContext &getContext() {
38     return M->getContext();
39   }
40 
getModule()41   Module &getModule() { return *M; }
getModule()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.
getComplexityScore()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 
60 private:
61   uint64_t computeIRComplexityScore() const;
62   uint64_t computeMIRComplexityScore() const;
63 };
64 
65 std::pair<std::unique_ptr<ReducerWorkItem>, bool>
66 parseReducerWorkItem(StringRef ToolName, StringRef Filename, LLVMContext &Ctxt,
67                      std::unique_ptr<TargetMachine> &TM, bool IsMIR);
68 } // namespace llvm
69 
70 #endif
71