xref: /llvm-project/llvm/include/llvm/Transforms/InstCombine/InstCombine.h (revision cacbe71af7b1075f8ad1f84e002d1fcc83e85713)
1 //===- InstCombine.h - InstCombine pass -------------------------*- 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 /// \file
9 ///
10 /// This file provides the primary interface to the instcombine pass. This pass
11 /// is suitable for use in the new pass manager. For a pass that works with the
12 /// legacy pass manager, use \c createInstructionCombiningPass().
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINE_H
17 #define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINE_H
18 
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/Pass.h"
22 
23 #define DEBUG_TYPE "instcombine"
24 #include "llvm/Transforms/Utils/InstructionWorklist.h"
25 
26 namespace llvm {
27 
28 static constexpr unsigned InstCombineDefaultMaxIterations = 1;
29 
30 struct InstCombineOptions {
31   // Verify that a fix point has been reached after MaxIterations.
32   bool VerifyFixpoint = false;
33   unsigned MaxIterations = InstCombineDefaultMaxIterations;
34 
35   InstCombineOptions() = default;
36 
37   InstCombineOptions &setVerifyFixpoint(bool Value) {
38     VerifyFixpoint = Value;
39     return *this;
40   }
41 
42   InstCombineOptions &setMaxIterations(unsigned Value) {
43     MaxIterations = Value;
44     return *this;
45   }
46 };
47 
48 class InstCombinePass : public PassInfoMixin<InstCombinePass> {
49 private:
50   InstructionWorklist Worklist;
51   InstCombineOptions Options;
52   static char ID;
53 
54 public:
55   explicit InstCombinePass(InstCombineOptions Opts = {});
56   void printPipeline(raw_ostream &OS,
57                      function_ref<StringRef(StringRef)> MapClassName2PassName);
58 
59   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
60 };
61 
62 /// The legacy pass manager's instcombine pass.
63 ///
64 /// This is a basic whole-function wrapper around the instcombine utility. It
65 /// will try to combine all instructions in the function.
66 class InstructionCombiningPass : public FunctionPass {
67   InstructionWorklist Worklist;
68 
69 public:
70   static char ID; // Pass identification, replacement for typeid
71 
72   explicit InstructionCombiningPass();
73 
74   void getAnalysisUsage(AnalysisUsage &AU) const override;
75   bool runOnFunction(Function &F) override;
76 };
77 
78 //===----------------------------------------------------------------------===//
79 //
80 // InstructionCombining - Combine instructions to form fewer, simple
81 // instructions. This pass does not modify the CFG, and has a tendency to make
82 // instructions dead, so a subsequent DCE pass is useful.
83 //
84 // This pass combines things like:
85 //    %Y = add int 1, %X
86 //    %Z = add int 1, %Y
87 // into:
88 //    %Z = add int 2, %X
89 //
90 FunctionPass *createInstructionCombiningPass();
91 }
92 
93 #undef DEBUG_TYPE
94 
95 #endif
96