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