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