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/Transforms/InstCombine/InstCombineWorklist.h"
22 
23 namespace llvm {
24 
25 class InstCombinePass : public PassInfoMixin<InstCombinePass> {
26   InstCombineWorklist Worklist;
27   const bool ExpensiveCombines;
28   const unsigned MaxIterations;
29 
30 public:
31   static StringRef name() { return "InstCombinePass"; }
32 
33   explicit InstCombinePass(bool ExpensiveCombines = true);
34   explicit InstCombinePass(bool ExpensiveCombines, unsigned MaxIterations);
35 
36   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
37 };
38 
39 /// The legacy pass manager's instcombine pass.
40 ///
41 /// This is a basic whole-function wrapper around the instcombine utility. It
42 /// will try to combine all instructions in the function.
43 class InstructionCombiningPass : public FunctionPass {
44   InstCombineWorklist Worklist;
45   const bool ExpensiveCombines;
46   const unsigned MaxIterations;
47 
48 public:
49   static char ID; // Pass identification, replacement for typeid
50 
51   explicit InstructionCombiningPass(bool ExpensiveCombines = true);
52   explicit InstructionCombiningPass(bool ExpensiveCombines,
53                                     unsigned MaxIterations);
54 
55   void getAnalysisUsage(AnalysisUsage &AU) const override;
56   bool runOnFunction(Function &F) override;
57 };
58 
59 //===----------------------------------------------------------------------===//
60 //
61 // InstructionCombining - Combine instructions to form fewer, simple
62 // instructions. This pass does not modify the CFG, and has a tendency to make
63 // instructions dead, so a subsequent DCE pass is useful.
64 //
65 // This pass combines things like:
66 //    %Y = add int 1, %X
67 //    %Z = add int 1, %Y
68 // into:
69 //    %Z = add int 2, %X
70 //
71 FunctionPass *createInstructionCombiningPass(bool ExpensiveCombines = true);
72 FunctionPass *createInstructionCombiningPass(bool ExpensiveCombines,
73                                              unsigned MaxIterations);
74 }
75 
76 #endif
77