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 "MipsLegalizerInfo.h"
15#include "MipsTargetMachine.h"
16#include "llvm/CodeGen/GlobalISel/Combiner.h"
17#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
18#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
19#include "llvm/CodeGen/GlobalISel/GISelValueTracking.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/TargetPassConfig.h"
22
23#define DEBUG_TYPE "mips-prelegalizer-combiner"
24
25using namespace llvm;
26
27namespace {
28struct MipsPreLegalizerCombinerInfo : public CombinerInfo {
29public:
30 MipsPreLegalizerCombinerInfo()
31 : CombinerInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
32 /*LegalizerInfo*/ nullptr, /*EnableOpt*/ false,
33 /*EnableOptSize*/ false, /*EnableMinSize*/ false) {}
34};
35
36class MipsPreLegalizerCombinerImpl : public Combiner {
37protected:
38 const MipsSubtarget &STI;
39 const CombinerHelper Helper;
40
41public:
42 MipsPreLegalizerCombinerImpl(MachineFunction &MF, CombinerInfo &CInfo,
43 GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
44 const MipsSubtarget &STI,
45 MachineDominatorTree *MDT,
46 const LegalizerInfo *LI)
47 : Combiner(MF, CInfo, &VT, CSEInfo), STI(STI),
48 Helper(Observer, B, /*IsPreLegalize*/ true, &VT, MDT, LI) {}
49
50 static const char *getName() { return "MipsPreLegalizerCombiner"; }
51
52 void setupGeneratedPerFunctionState(MachineFunction &MF) override {
53 // TODO: TableGen-erate this class' impl.
54 }
55
56 bool tryCombineAll(MachineInstr &MI) const override {
57 switch (MI.getOpcode()) {
58 default:
59 return false;
60 case TargetOpcode::G_MEMCPY_INLINE:
61 return Helper.tryEmitMemcpyInline(MI);
62 case TargetOpcode::G_LOAD:
63 case TargetOpcode::G_SEXTLOAD:
64 case TargetOpcode::G_ZEXTLOAD: {
65 // Don't attempt to combine non power of 2 loads or unaligned loads when
66 // subtarget doesn't support them.
67 auto MMO = *MI.memoperands_begin();
68 const MipsSubtarget &STI = MI.getMF()->getSubtarget<MipsSubtarget>();
69 if (!MMO->getSize().hasValue() ||
70 !isPowerOf2_64(Value: MMO->getSize().getValue()))
71 return false;
72 bool isUnaligned = MMO->getAlign() < MMO->getSize().getValue();
73 if (!STI.systemSupportsUnalignedAccess() && isUnaligned)
74 return false;
75
76 return Helper.tryCombineExtendingLoads(MI);
77 }
78 }
79
80 return false;
81 }
82};
83
84// Pass boilerplate
85// ================
86
87class MipsPreLegalizerCombiner : public MachineFunctionPass {
88public:
89 static char ID;
90
91 MipsPreLegalizerCombiner();
92
93 StringRef getPassName() const override { return "MipsPreLegalizerCombiner"; }
94
95 bool runOnMachineFunction(MachineFunction &MF) override;
96
97 void getAnalysisUsage(AnalysisUsage &AU) const override;
98};
99} // end anonymous namespace
100
101void MipsPreLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
102 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
103 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
104 AU.setPreservesCFG();
105 getSelectionDAGFallbackAnalysisUsage(AU);
106 MachineFunctionPass::getAnalysisUsage(AU);
107}
108
109MipsPreLegalizerCombiner::MipsPreLegalizerCombiner()
110 : MachineFunctionPass(ID) {}
111
112bool MipsPreLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {
113 if (MF.getProperties().hasFailedISel())
114 return false;
115
116 const MipsSubtarget &ST = MF.getSubtarget<MipsSubtarget>();
117 const MipsLegalizerInfo *LI =
118 static_cast<const MipsLegalizerInfo *>(ST.getLegalizerInfo());
119
120 GISelValueTracking *VT =
121 &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
122 MipsPreLegalizerCombinerInfo PCInfo;
123 MipsPreLegalizerCombinerImpl Impl(MF, PCInfo, *VT, /*CSEInfo*/ nullptr, ST,
124 /*MDT*/ nullptr, LI);
125 return Impl.combineMachineInstrs();
126}
127
128char MipsPreLegalizerCombiner::ID = 0;
129INITIALIZE_PASS_BEGIN(MipsPreLegalizerCombiner, DEBUG_TYPE,
130 "Combine Mips machine instrs before legalization", false,
131 false)
132INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
133INITIALIZE_PASS_END(MipsPreLegalizerCombiner, DEBUG_TYPE,
134 "Combine Mips machine instrs before legalization", false,
135 false)
136
137FunctionPass *llvm::createMipsPreLegalizeCombiner() {
138 return new MipsPreLegalizerCombiner();
139}
140