1 //=== lib/CodeGen/GlobalISel/MipsPreLegalizerCombiner.cpp --------------===// 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 // This pass does combining of machine instructions at the generic MI level, 10 // before the legalizer. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MipsTargetMachine.h" 15 #include "llvm/CodeGen/GlobalISel/Combiner.h" 16 #include "llvm/CodeGen/GlobalISel/CombinerHelper.h" 17 #include "llvm/CodeGen/GlobalISel/CombinerInfo.h" 18 #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h" 19 #include "llvm/CodeGen/TargetPassConfig.h" 20 #include "llvm/InitializePasses.h" 21 22 #define DEBUG_TYPE "mips-prelegalizer-combiner" 23 24 using namespace llvm; 25 26 namespace { 27 class MipsPreLegalizerCombinerInfo : public CombinerInfo { 28 public: 29 MipsPreLegalizerCombinerInfo() 30 : CombinerInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false, 31 /*LegalizerInfo*/ nullptr, /*EnableOpt*/ false, 32 /*EnableOptSize*/ false, /*EnableMinSize*/ false) {} 33 virtual bool combine(GISelChangeObserver &Observer, MachineInstr &MI, 34 MachineIRBuilder &B) const override; 35 }; 36 37 bool MipsPreLegalizerCombinerInfo::combine(GISelChangeObserver &Observer, 38 MachineInstr &MI, 39 MachineIRBuilder &B) const { 40 CombinerHelper Helper(Observer, B); 41 42 switch (MI.getOpcode()) { 43 default: 44 return false; 45 case TargetOpcode::G_LOAD: 46 case TargetOpcode::G_SEXTLOAD: 47 case TargetOpcode::G_ZEXTLOAD: 48 return Helper.tryCombineExtendingLoads(MI); 49 } 50 return false; 51 } 52 53 // Pass boilerplate 54 // ================ 55 56 class MipsPreLegalizerCombiner : public MachineFunctionPass { 57 public: 58 static char ID; 59 60 MipsPreLegalizerCombiner(); 61 62 StringRef getPassName() const override { return "MipsPreLegalizerCombiner"; } 63 64 bool runOnMachineFunction(MachineFunction &MF) override; 65 66 void getAnalysisUsage(AnalysisUsage &AU) const override; 67 }; 68 } // end anonymous namespace 69 70 void MipsPreLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const { 71 AU.addRequired<TargetPassConfig>(); 72 AU.setPreservesCFG(); 73 getSelectionDAGFallbackAnalysisUsage(AU); 74 MachineFunctionPass::getAnalysisUsage(AU); 75 } 76 77 MipsPreLegalizerCombiner::MipsPreLegalizerCombiner() : MachineFunctionPass(ID) { 78 initializeMipsPreLegalizerCombinerPass(*PassRegistry::getPassRegistry()); 79 } 80 81 bool MipsPreLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) { 82 if (MF.getProperties().hasProperty( 83 MachineFunctionProperties::Property::FailedISel)) 84 return false; 85 auto *TPC = &getAnalysis<TargetPassConfig>(); 86 MipsPreLegalizerCombinerInfo PCInfo; 87 Combiner C(PCInfo, TPC); 88 return C.combineMachineInstrs(MF, nullptr); 89 } 90 91 char MipsPreLegalizerCombiner::ID = 0; 92 INITIALIZE_PASS_BEGIN(MipsPreLegalizerCombiner, DEBUG_TYPE, 93 "Combine Mips machine instrs before legalization", false, 94 false) 95 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) 96 INITIALIZE_PASS_END(MipsPreLegalizerCombiner, DEBUG_TYPE, 97 "Combine Mips machine instrs before legalization", false, 98 false) 99 100 namespace llvm { 101 FunctionPass *createMipsPreLegalizeCombiner() { 102 return new MipsPreLegalizerCombiner(); 103 } 104 } // end namespace llvm 105