xref: /aosp_15_r20/external/llvm/lib/Target/Lanai/LanaiMemAluCombiner.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- LanaiMemAluCombiner.cpp - Pass to combine memory & ALU operations -===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker // Simple pass to combine memory and ALU operations
10*9880d681SAndroid Build Coastguard Worker //
11*9880d681SAndroid Build Coastguard Worker // The Lanai ISA supports instructions where a load/store modifies the base
12*9880d681SAndroid Build Coastguard Worker // register used in the load/store operation. This pass finds suitable
13*9880d681SAndroid Build Coastguard Worker // load/store and ALU instructions and combines them into one instruction.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker // For example,
16*9880d681SAndroid Build Coastguard Worker //   ld [ %r6 -- ], %r12
17*9880d681SAndroid Build Coastguard Worker // is a supported instruction that is not currently generated by the instruction
18*9880d681SAndroid Build Coastguard Worker // selection pass of this backend. This pass generates these instructions by
19*9880d681SAndroid Build Coastguard Worker // merging
20*9880d681SAndroid Build Coastguard Worker //   add %r6, -4, %r6
21*9880d681SAndroid Build Coastguard Worker // followed by
22*9880d681SAndroid Build Coastguard Worker //   ld [ %r6 ], %r12
23*9880d681SAndroid Build Coastguard Worker // in the same machine basic block into one machine instruction.
24*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker #include "Lanai.h"
27*9880d681SAndroid Build Coastguard Worker #include "LanaiTargetMachine.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegisterScavenging.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
35*9880d681SAndroid Build Coastguard Worker using namespace llvm;
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker #define GET_INSTRMAP_INFO
38*9880d681SAndroid Build Coastguard Worker #include "LanaiGenInstrInfo.inc"
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "lanai-mem-alu-combiner"
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker STATISTIC(NumLdStAluCombined, "Number of memory and ALU instructions combined");
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker static llvm::cl::opt<bool> DisableMemAluCombiner(
45*9880d681SAndroid Build Coastguard Worker     "disable-lanai-mem-alu-combiner", llvm::cl::init(false),
46*9880d681SAndroid Build Coastguard Worker     llvm::cl::desc("Do not combine ALU and memory operators"),
47*9880d681SAndroid Build Coastguard Worker     llvm::cl::Hidden);
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker namespace llvm {
50*9880d681SAndroid Build Coastguard Worker void initializeLanaiMemAluCombinerPass(PassRegistry &);
51*9880d681SAndroid Build Coastguard Worker } // namespace llvm
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker namespace {
54*9880d681SAndroid Build Coastguard Worker typedef MachineBasicBlock::iterator MbbIterator;
55*9880d681SAndroid Build Coastguard Worker typedef MachineFunction::iterator MfIterator;
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker class LanaiMemAluCombiner : public MachineFunctionPass {
58*9880d681SAndroid Build Coastguard Worker public:
59*9880d681SAndroid Build Coastguard Worker   static char ID;
LanaiMemAluCombiner()60*9880d681SAndroid Build Coastguard Worker   explicit LanaiMemAluCombiner() : MachineFunctionPass(ID) {
61*9880d681SAndroid Build Coastguard Worker     initializeLanaiMemAluCombinerPass(*PassRegistry::getPassRegistry());
62*9880d681SAndroid Build Coastguard Worker   }
63*9880d681SAndroid Build Coastguard Worker 
getPassName() const64*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override {
65*9880d681SAndroid Build Coastguard Worker     return "Lanai load / store optimization pass";
66*9880d681SAndroid Build Coastguard Worker   }
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &F) override;
69*9880d681SAndroid Build Coastguard Worker 
getRequiredProperties() const70*9880d681SAndroid Build Coastguard Worker   MachineFunctionProperties getRequiredProperties() const override {
71*9880d681SAndroid Build Coastguard Worker     return MachineFunctionProperties().set(
72*9880d681SAndroid Build Coastguard Worker         MachineFunctionProperties::Property::AllVRegsAllocated);
73*9880d681SAndroid Build Coastguard Worker   }
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker private:
76*9880d681SAndroid Build Coastguard Worker   MbbIterator findClosestSuitableAluInstr(MachineBasicBlock *BB,
77*9880d681SAndroid Build Coastguard Worker                                           const MbbIterator &MemInstr,
78*9880d681SAndroid Build Coastguard Worker                                           bool Decrement);
79*9880d681SAndroid Build Coastguard Worker   void insertMergedInstruction(MachineBasicBlock *BB,
80*9880d681SAndroid Build Coastguard Worker                                const MbbIterator &MemInstr,
81*9880d681SAndroid Build Coastguard Worker                                const MbbIterator &AluInstr, bool Before);
82*9880d681SAndroid Build Coastguard Worker   bool combineMemAluInBasicBlock(MachineBasicBlock *BB);
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker   // Target machine description which we query for register names, data
85*9880d681SAndroid Build Coastguard Worker   // layout, etc.
86*9880d681SAndroid Build Coastguard Worker   const TargetInstrInfo *TII;
87*9880d681SAndroid Build Coastguard Worker };
88*9880d681SAndroid Build Coastguard Worker } // namespace
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker char LanaiMemAluCombiner::ID = 0;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(LanaiMemAluCombiner, DEBUG_TYPE,
93*9880d681SAndroid Build Coastguard Worker                 "Lanai memory ALU combiner pass", false, false)
94*9880d681SAndroid Build Coastguard Worker 
95*9880d681SAndroid Build Coastguard Worker namespace {
isSpls(uint16_t Opcode)96*9880d681SAndroid Build Coastguard Worker bool isSpls(uint16_t Opcode) { return Lanai::splsIdempotent(Opcode) == Opcode; }
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker // Determine the opcode for the merged instruction created by considering the
99*9880d681SAndroid Build Coastguard Worker // old memory operation's opcode and whether the merged opcode will have an
100*9880d681SAndroid Build Coastguard Worker // immediate offset.
mergedOpcode(unsigned OldOpcode,bool ImmediateOffset)101*9880d681SAndroid Build Coastguard Worker unsigned mergedOpcode(unsigned OldOpcode, bool ImmediateOffset) {
102*9880d681SAndroid Build Coastguard Worker   switch (OldOpcode) {
103*9880d681SAndroid Build Coastguard Worker   case Lanai::LDW_RI:
104*9880d681SAndroid Build Coastguard Worker   case Lanai::LDW_RR:
105*9880d681SAndroid Build Coastguard Worker     if (ImmediateOffset)
106*9880d681SAndroid Build Coastguard Worker       return Lanai::LDW_RI;
107*9880d681SAndroid Build Coastguard Worker     return Lanai::LDW_RR;
108*9880d681SAndroid Build Coastguard Worker   case Lanai::LDHs_RI:
109*9880d681SAndroid Build Coastguard Worker   case Lanai::LDHs_RR:
110*9880d681SAndroid Build Coastguard Worker     if (ImmediateOffset)
111*9880d681SAndroid Build Coastguard Worker       return Lanai::LDHs_RI;
112*9880d681SAndroid Build Coastguard Worker     return Lanai::LDHs_RR;
113*9880d681SAndroid Build Coastguard Worker   case Lanai::LDHz_RI:
114*9880d681SAndroid Build Coastguard Worker   case Lanai::LDHz_RR:
115*9880d681SAndroid Build Coastguard Worker     if (ImmediateOffset)
116*9880d681SAndroid Build Coastguard Worker       return Lanai::LDHz_RI;
117*9880d681SAndroid Build Coastguard Worker     return Lanai::LDHz_RR;
118*9880d681SAndroid Build Coastguard Worker   case Lanai::LDBs_RI:
119*9880d681SAndroid Build Coastguard Worker   case Lanai::LDBs_RR:
120*9880d681SAndroid Build Coastguard Worker     if (ImmediateOffset)
121*9880d681SAndroid Build Coastguard Worker       return Lanai::LDBs_RI;
122*9880d681SAndroid Build Coastguard Worker     return Lanai::LDBs_RR;
123*9880d681SAndroid Build Coastguard Worker   case Lanai::LDBz_RI:
124*9880d681SAndroid Build Coastguard Worker   case Lanai::LDBz_RR:
125*9880d681SAndroid Build Coastguard Worker     if (ImmediateOffset)
126*9880d681SAndroid Build Coastguard Worker       return Lanai::LDBz_RI;
127*9880d681SAndroid Build Coastguard Worker     return Lanai::LDBz_RR;
128*9880d681SAndroid Build Coastguard Worker   case Lanai::SW_RI:
129*9880d681SAndroid Build Coastguard Worker   case Lanai::SW_RR:
130*9880d681SAndroid Build Coastguard Worker     if (ImmediateOffset)
131*9880d681SAndroid Build Coastguard Worker       return Lanai::SW_RI;
132*9880d681SAndroid Build Coastguard Worker     return Lanai::SW_RR;
133*9880d681SAndroid Build Coastguard Worker   case Lanai::STB_RI:
134*9880d681SAndroid Build Coastguard Worker   case Lanai::STB_RR:
135*9880d681SAndroid Build Coastguard Worker     if (ImmediateOffset)
136*9880d681SAndroid Build Coastguard Worker       return Lanai::STB_RI;
137*9880d681SAndroid Build Coastguard Worker     return Lanai::STB_RR;
138*9880d681SAndroid Build Coastguard Worker   case Lanai::STH_RI:
139*9880d681SAndroid Build Coastguard Worker   case Lanai::STH_RR:
140*9880d681SAndroid Build Coastguard Worker     if (ImmediateOffset)
141*9880d681SAndroid Build Coastguard Worker       return Lanai::STH_RI;
142*9880d681SAndroid Build Coastguard Worker     return Lanai::STH_RR;
143*9880d681SAndroid Build Coastguard Worker   default:
144*9880d681SAndroid Build Coastguard Worker     return 0;
145*9880d681SAndroid Build Coastguard Worker   }
146*9880d681SAndroid Build Coastguard Worker }
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker // Check if the machine instruction has non-volatile memory operands of the type
149*9880d681SAndroid Build Coastguard Worker // supported for combining with ALU instructions.
isNonVolatileMemoryOp(const MachineInstr & MI)150*9880d681SAndroid Build Coastguard Worker bool isNonVolatileMemoryOp(const MachineInstr &MI) {
151*9880d681SAndroid Build Coastguard Worker   if (!MI.hasOneMemOperand())
152*9880d681SAndroid Build Coastguard Worker     return false;
153*9880d681SAndroid Build Coastguard Worker 
154*9880d681SAndroid Build Coastguard Worker   // Determine if the machine instruction is a supported memory operation by
155*9880d681SAndroid Build Coastguard Worker   // testing if the computed merge opcode is a valid memory operation opcode.
156*9880d681SAndroid Build Coastguard Worker   if (mergedOpcode(MI.getOpcode(), false) == 0)
157*9880d681SAndroid Build Coastguard Worker     return false;
158*9880d681SAndroid Build Coastguard Worker 
159*9880d681SAndroid Build Coastguard Worker   const MachineMemOperand *MemOperand = *MI.memoperands_begin();
160*9880d681SAndroid Build Coastguard Worker 
161*9880d681SAndroid Build Coastguard Worker   // Don't move volatile memory accesses
162*9880d681SAndroid Build Coastguard Worker   if (MemOperand->isVolatile())
163*9880d681SAndroid Build Coastguard Worker     return false;
164*9880d681SAndroid Build Coastguard Worker 
165*9880d681SAndroid Build Coastguard Worker   return true;
166*9880d681SAndroid Build Coastguard Worker }
167*9880d681SAndroid Build Coastguard Worker 
168*9880d681SAndroid Build Coastguard Worker // Test to see if two machine operands are of the same type. This test is less
169*9880d681SAndroid Build Coastguard Worker // strict than the MachineOperand::isIdenticalTo function.
isSameOperand(const MachineOperand & Op1,const MachineOperand & Op2)170*9880d681SAndroid Build Coastguard Worker bool isSameOperand(const MachineOperand &Op1, const MachineOperand &Op2) {
171*9880d681SAndroid Build Coastguard Worker   if (Op1.getType() != Op2.getType())
172*9880d681SAndroid Build Coastguard Worker     return false;
173*9880d681SAndroid Build Coastguard Worker 
174*9880d681SAndroid Build Coastguard Worker   switch (Op1.getType()) {
175*9880d681SAndroid Build Coastguard Worker   case MachineOperand::MO_Register:
176*9880d681SAndroid Build Coastguard Worker     return Op1.getReg() == Op2.getReg();
177*9880d681SAndroid Build Coastguard Worker   case MachineOperand::MO_Immediate:
178*9880d681SAndroid Build Coastguard Worker     return Op1.getImm() == Op2.getImm();
179*9880d681SAndroid Build Coastguard Worker   default:
180*9880d681SAndroid Build Coastguard Worker     return false;
181*9880d681SAndroid Build Coastguard Worker   }
182*9880d681SAndroid Build Coastguard Worker }
183*9880d681SAndroid Build Coastguard Worker 
isZeroOperand(const MachineOperand & Op)184*9880d681SAndroid Build Coastguard Worker bool isZeroOperand(const MachineOperand &Op) {
185*9880d681SAndroid Build Coastguard Worker   return ((Op.isReg() && Op.getReg() == Lanai::R0) ||
186*9880d681SAndroid Build Coastguard Worker           (Op.isImm() && Op.getImm() == 0));
187*9880d681SAndroid Build Coastguard Worker }
188*9880d681SAndroid Build Coastguard Worker 
189*9880d681SAndroid Build Coastguard Worker // Determines whether a register is used by an instruction.
InstrUsesReg(const MbbIterator & Instr,const MachineOperand * Reg)190*9880d681SAndroid Build Coastguard Worker bool InstrUsesReg(const MbbIterator &Instr, const MachineOperand *Reg) {
191*9880d681SAndroid Build Coastguard Worker   for (MachineInstr::const_mop_iterator Mop = Instr->operands_begin();
192*9880d681SAndroid Build Coastguard Worker        Mop != Instr->operands_end(); ++Mop) {
193*9880d681SAndroid Build Coastguard Worker     if (isSameOperand(*Mop, *Reg))
194*9880d681SAndroid Build Coastguard Worker       return true;
195*9880d681SAndroid Build Coastguard Worker   }
196*9880d681SAndroid Build Coastguard Worker   return false;
197*9880d681SAndroid Build Coastguard Worker }
198*9880d681SAndroid Build Coastguard Worker 
199*9880d681SAndroid Build Coastguard Worker // Converts between machine opcode and AluCode.
200*9880d681SAndroid Build Coastguard Worker // Flag using/modifying ALU operations should not be considered for merging and
201*9880d681SAndroid Build Coastguard Worker // are omitted from this list.
mergedAluCode(unsigned AluOpcode)202*9880d681SAndroid Build Coastguard Worker LPAC::AluCode mergedAluCode(unsigned AluOpcode) {
203*9880d681SAndroid Build Coastguard Worker   switch (AluOpcode) {
204*9880d681SAndroid Build Coastguard Worker   case Lanai::ADD_I_LO:
205*9880d681SAndroid Build Coastguard Worker   case Lanai::ADD_R:
206*9880d681SAndroid Build Coastguard Worker     return LPAC::ADD;
207*9880d681SAndroid Build Coastguard Worker   case Lanai::SUB_I_LO:
208*9880d681SAndroid Build Coastguard Worker   case Lanai::SUB_R:
209*9880d681SAndroid Build Coastguard Worker     return LPAC::SUB;
210*9880d681SAndroid Build Coastguard Worker   case Lanai::AND_I_LO:
211*9880d681SAndroid Build Coastguard Worker   case Lanai::AND_R:
212*9880d681SAndroid Build Coastguard Worker     return LPAC::AND;
213*9880d681SAndroid Build Coastguard Worker   case Lanai::OR_I_LO:
214*9880d681SAndroid Build Coastguard Worker   case Lanai::OR_R:
215*9880d681SAndroid Build Coastguard Worker     return LPAC::OR;
216*9880d681SAndroid Build Coastguard Worker   case Lanai::XOR_I_LO:
217*9880d681SAndroid Build Coastguard Worker   case Lanai::XOR_R:
218*9880d681SAndroid Build Coastguard Worker     return LPAC::XOR;
219*9880d681SAndroid Build Coastguard Worker   case Lanai::SHL_R:
220*9880d681SAndroid Build Coastguard Worker     return LPAC::SHL;
221*9880d681SAndroid Build Coastguard Worker   case Lanai::SRL_R:
222*9880d681SAndroid Build Coastguard Worker     return LPAC::SRL;
223*9880d681SAndroid Build Coastguard Worker   case Lanai::SRA_R:
224*9880d681SAndroid Build Coastguard Worker     return LPAC::SRA;
225*9880d681SAndroid Build Coastguard Worker   case Lanai::SA_I:
226*9880d681SAndroid Build Coastguard Worker   case Lanai::SL_I:
227*9880d681SAndroid Build Coastguard Worker   default:
228*9880d681SAndroid Build Coastguard Worker     return LPAC::UNKNOWN;
229*9880d681SAndroid Build Coastguard Worker   }
230*9880d681SAndroid Build Coastguard Worker }
231*9880d681SAndroid Build Coastguard Worker 
232*9880d681SAndroid Build Coastguard Worker // Insert a new combined memory and ALU operation instruction.
233*9880d681SAndroid Build Coastguard Worker //
234*9880d681SAndroid Build Coastguard Worker // This function builds a new machine instruction using the MachineInstrBuilder
235*9880d681SAndroid Build Coastguard Worker // class and inserts it before the memory instruction.
insertMergedInstruction(MachineBasicBlock * BB,const MbbIterator & MemInstr,const MbbIterator & AluInstr,bool Before)236*9880d681SAndroid Build Coastguard Worker void LanaiMemAluCombiner::insertMergedInstruction(MachineBasicBlock *BB,
237*9880d681SAndroid Build Coastguard Worker                                                   const MbbIterator &MemInstr,
238*9880d681SAndroid Build Coastguard Worker                                                   const MbbIterator &AluInstr,
239*9880d681SAndroid Build Coastguard Worker                                                   bool Before) {
240*9880d681SAndroid Build Coastguard Worker   // Insert new combined load/store + alu operation
241*9880d681SAndroid Build Coastguard Worker   MachineOperand Dest = MemInstr->getOperand(0);
242*9880d681SAndroid Build Coastguard Worker   MachineOperand Base = MemInstr->getOperand(1);
243*9880d681SAndroid Build Coastguard Worker   MachineOperand MemOffset = MemInstr->getOperand(2);
244*9880d681SAndroid Build Coastguard Worker   MachineOperand AluOffset = AluInstr->getOperand(2);
245*9880d681SAndroid Build Coastguard Worker 
246*9880d681SAndroid Build Coastguard Worker   // Abort if ALU offset is not a register or immediate
247*9880d681SAndroid Build Coastguard Worker   assert((AluOffset.isReg() || AluOffset.isImm()) &&
248*9880d681SAndroid Build Coastguard Worker          "Unsupported operand type in merge");
249*9880d681SAndroid Build Coastguard Worker 
250*9880d681SAndroid Build Coastguard Worker   // Determined merged instructions opcode and ALU code
251*9880d681SAndroid Build Coastguard Worker   LPAC::AluCode AluOpcode = mergedAluCode(AluInstr->getOpcode());
252*9880d681SAndroid Build Coastguard Worker   unsigned NewOpc = mergedOpcode(MemInstr->getOpcode(), AluOffset.isImm());
253*9880d681SAndroid Build Coastguard Worker 
254*9880d681SAndroid Build Coastguard Worker   assert(AluOpcode != LPAC::UNKNOWN && "Unknown ALU code in merging");
255*9880d681SAndroid Build Coastguard Worker   assert(NewOpc != 0 && "Unknown merged node opcode");
256*9880d681SAndroid Build Coastguard Worker 
257*9880d681SAndroid Build Coastguard Worker   // Build and insert new machine instruction
258*9880d681SAndroid Build Coastguard Worker   MachineInstrBuilder InstrBuilder =
259*9880d681SAndroid Build Coastguard Worker       BuildMI(*BB, MemInstr, MemInstr->getDebugLoc(), TII->get(NewOpc));
260*9880d681SAndroid Build Coastguard Worker   InstrBuilder.addReg(Dest.getReg(), getDefRegState(true));
261*9880d681SAndroid Build Coastguard Worker   InstrBuilder.addReg(Base.getReg(), getKillRegState(true));
262*9880d681SAndroid Build Coastguard Worker 
263*9880d681SAndroid Build Coastguard Worker   // Add offset to machine instruction
264*9880d681SAndroid Build Coastguard Worker   if (AluOffset.isReg())
265*9880d681SAndroid Build Coastguard Worker     InstrBuilder.addReg(AluOffset.getReg());
266*9880d681SAndroid Build Coastguard Worker   else if (AluOffset.isImm())
267*9880d681SAndroid Build Coastguard Worker     InstrBuilder.addImm(AluOffset.getImm());
268*9880d681SAndroid Build Coastguard Worker   else
269*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Unsupported ld/st ALU merge.");
270*9880d681SAndroid Build Coastguard Worker 
271*9880d681SAndroid Build Coastguard Worker   // Create a pre-op if the ALU operation preceded the memory operation or the
272*9880d681SAndroid Build Coastguard Worker   // MemOffset is non-zero (i.e. the memory value should be adjusted before
273*9880d681SAndroid Build Coastguard Worker   // accessing it), else create a post-op.
274*9880d681SAndroid Build Coastguard Worker   if (Before || !isZeroOperand(MemOffset))
275*9880d681SAndroid Build Coastguard Worker     InstrBuilder.addImm(LPAC::makePreOp(AluOpcode));
276*9880d681SAndroid Build Coastguard Worker   else
277*9880d681SAndroid Build Coastguard Worker     InstrBuilder.addImm(LPAC::makePostOp(AluOpcode));
278*9880d681SAndroid Build Coastguard Worker 
279*9880d681SAndroid Build Coastguard Worker   // Transfer memory operands.
280*9880d681SAndroid Build Coastguard Worker   InstrBuilder->setMemRefs(MemInstr->memoperands_begin(),
281*9880d681SAndroid Build Coastguard Worker                            MemInstr->memoperands_end());
282*9880d681SAndroid Build Coastguard Worker }
283*9880d681SAndroid Build Coastguard Worker 
284*9880d681SAndroid Build Coastguard Worker // Function determines if ALU operation (in alu_iter) can be combined with
285*9880d681SAndroid Build Coastguard Worker // a load/store with base and offset.
isSuitableAluInstr(bool IsSpls,const MbbIterator & AluIter,const MachineOperand & Base,const MachineOperand & Offset)286*9880d681SAndroid Build Coastguard Worker bool isSuitableAluInstr(bool IsSpls, const MbbIterator &AluIter,
287*9880d681SAndroid Build Coastguard Worker                         const MachineOperand &Base,
288*9880d681SAndroid Build Coastguard Worker                         const MachineOperand &Offset) {
289*9880d681SAndroid Build Coastguard Worker   // ALU operations have 3 operands
290*9880d681SAndroid Build Coastguard Worker   if (AluIter->getNumOperands() != 3)
291*9880d681SAndroid Build Coastguard Worker     return false;
292*9880d681SAndroid Build Coastguard Worker 
293*9880d681SAndroid Build Coastguard Worker   MachineOperand &Dest = AluIter->getOperand(0);
294*9880d681SAndroid Build Coastguard Worker   MachineOperand &Op1 = AluIter->getOperand(1);
295*9880d681SAndroid Build Coastguard Worker   MachineOperand &Op2 = AluIter->getOperand(2);
296*9880d681SAndroid Build Coastguard Worker 
297*9880d681SAndroid Build Coastguard Worker   // Only match instructions using the base register as destination and with the
298*9880d681SAndroid Build Coastguard Worker   // base and first operand equal
299*9880d681SAndroid Build Coastguard Worker   if (!isSameOperand(Dest, Base) || !isSameOperand(Dest, Op1))
300*9880d681SAndroid Build Coastguard Worker     return false;
301*9880d681SAndroid Build Coastguard Worker 
302*9880d681SAndroid Build Coastguard Worker   if (Op2.isImm()) {
303*9880d681SAndroid Build Coastguard Worker     // It is not a match if the 2nd operand in the ALU operation is an
304*9880d681SAndroid Build Coastguard Worker     // immediate but the ALU operation is not an addition.
305*9880d681SAndroid Build Coastguard Worker     if (AluIter->getOpcode() != Lanai::ADD_I_LO)
306*9880d681SAndroid Build Coastguard Worker       return false;
307*9880d681SAndroid Build Coastguard Worker 
308*9880d681SAndroid Build Coastguard Worker     if (Offset.isReg() && Offset.getReg() == Lanai::R0)
309*9880d681SAndroid Build Coastguard Worker       return true;
310*9880d681SAndroid Build Coastguard Worker 
311*9880d681SAndroid Build Coastguard Worker     if (Offset.isImm() &&
312*9880d681SAndroid Build Coastguard Worker         ((Offset.getImm() == 0 &&
313*9880d681SAndroid Build Coastguard Worker           // Check that the Op2 would fit in the immediate field of the
314*9880d681SAndroid Build Coastguard Worker           // memory operation.
315*9880d681SAndroid Build Coastguard Worker           ((IsSpls && isInt<10>(Op2.getImm())) ||
316*9880d681SAndroid Build Coastguard Worker            (!IsSpls && isInt<16>(Op2.getImm())))) ||
317*9880d681SAndroid Build Coastguard Worker          Offset.getImm() == Op2.getImm()))
318*9880d681SAndroid Build Coastguard Worker       return true;
319*9880d681SAndroid Build Coastguard Worker   } else if (Op2.isReg()) {
320*9880d681SAndroid Build Coastguard Worker     // The Offset and 2nd operand are both registers and equal
321*9880d681SAndroid Build Coastguard Worker     if (Offset.isReg() && Op2.getReg() == Offset.getReg())
322*9880d681SAndroid Build Coastguard Worker       return true;
323*9880d681SAndroid Build Coastguard Worker   } else
324*9880d681SAndroid Build Coastguard Worker     // Only consider operations with register or immediate values
325*9880d681SAndroid Build Coastguard Worker     return false;
326*9880d681SAndroid Build Coastguard Worker 
327*9880d681SAndroid Build Coastguard Worker   return false;
328*9880d681SAndroid Build Coastguard Worker }
329*9880d681SAndroid Build Coastguard Worker 
findClosestSuitableAluInstr(MachineBasicBlock * BB,const MbbIterator & MemInstr,const bool Decrement)330*9880d681SAndroid Build Coastguard Worker MbbIterator LanaiMemAluCombiner::findClosestSuitableAluInstr(
331*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *BB, const MbbIterator &MemInstr, const bool Decrement) {
332*9880d681SAndroid Build Coastguard Worker   MachineOperand *Base = &MemInstr->getOperand(1);
333*9880d681SAndroid Build Coastguard Worker   MachineOperand *Offset = &MemInstr->getOperand(2);
334*9880d681SAndroid Build Coastguard Worker   bool IsSpls = isSpls(MemInstr->getOpcode());
335*9880d681SAndroid Build Coastguard Worker 
336*9880d681SAndroid Build Coastguard Worker   MbbIterator First = MemInstr;
337*9880d681SAndroid Build Coastguard Worker   MbbIterator Last = Decrement ? BB->begin() : BB->end();
338*9880d681SAndroid Build Coastguard Worker 
339*9880d681SAndroid Build Coastguard Worker   while (First != Last) {
340*9880d681SAndroid Build Coastguard Worker     Decrement ? --First : ++First;
341*9880d681SAndroid Build Coastguard Worker 
342*9880d681SAndroid Build Coastguard Worker     // Skip over debug instructions
343*9880d681SAndroid Build Coastguard Worker     if (First->isDebugValue())
344*9880d681SAndroid Build Coastguard Worker       continue;
345*9880d681SAndroid Build Coastguard Worker 
346*9880d681SAndroid Build Coastguard Worker     if (isSuitableAluInstr(IsSpls, First, *Base, *Offset)) {
347*9880d681SAndroid Build Coastguard Worker       return First;
348*9880d681SAndroid Build Coastguard Worker     }
349*9880d681SAndroid Build Coastguard Worker 
350*9880d681SAndroid Build Coastguard Worker     // Usage of the base or offset register is not a form suitable for merging.
351*9880d681SAndroid Build Coastguard Worker     if (First != Last) {
352*9880d681SAndroid Build Coastguard Worker       if (InstrUsesReg(First, Base))
353*9880d681SAndroid Build Coastguard Worker         break;
354*9880d681SAndroid Build Coastguard Worker       if (Offset->isReg() && InstrUsesReg(First, Offset))
355*9880d681SAndroid Build Coastguard Worker         break;
356*9880d681SAndroid Build Coastguard Worker     }
357*9880d681SAndroid Build Coastguard Worker   }
358*9880d681SAndroid Build Coastguard Worker 
359*9880d681SAndroid Build Coastguard Worker   return MemInstr;
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker 
combineMemAluInBasicBlock(MachineBasicBlock * BB)362*9880d681SAndroid Build Coastguard Worker bool LanaiMemAluCombiner::combineMemAluInBasicBlock(MachineBasicBlock *BB) {
363*9880d681SAndroid Build Coastguard Worker   bool Modified = false;
364*9880d681SAndroid Build Coastguard Worker 
365*9880d681SAndroid Build Coastguard Worker   MbbIterator MBBIter = BB->begin(), End = BB->end();
366*9880d681SAndroid Build Coastguard Worker   while (MBBIter != End) {
367*9880d681SAndroid Build Coastguard Worker     bool IsMemOp = isNonVolatileMemoryOp(*MBBIter);
368*9880d681SAndroid Build Coastguard Worker 
369*9880d681SAndroid Build Coastguard Worker     if (IsMemOp) {
370*9880d681SAndroid Build Coastguard Worker       MachineOperand AluOperand = MBBIter->getOperand(3);
371*9880d681SAndroid Build Coastguard Worker       unsigned int DestReg = MBBIter->getOperand(0).getReg(),
372*9880d681SAndroid Build Coastguard Worker                    BaseReg = MBBIter->getOperand(1).getReg();
373*9880d681SAndroid Build Coastguard Worker       assert(AluOperand.isImm() && "Unexpected memory operator type");
374*9880d681SAndroid Build Coastguard Worker       LPAC::AluCode AluOpcode = static_cast<LPAC::AluCode>(AluOperand.getImm());
375*9880d681SAndroid Build Coastguard Worker 
376*9880d681SAndroid Build Coastguard Worker       // Skip memory operations that already modify the base register or if
377*9880d681SAndroid Build Coastguard Worker       // the destination and base register are the same
378*9880d681SAndroid Build Coastguard Worker       if (!LPAC::modifiesOp(AluOpcode) && DestReg != BaseReg) {
379*9880d681SAndroid Build Coastguard Worker         for (int Inc = 0; Inc <= 1; ++Inc) {
380*9880d681SAndroid Build Coastguard Worker           MbbIterator AluIter =
381*9880d681SAndroid Build Coastguard Worker               findClosestSuitableAluInstr(BB, MBBIter, Inc == 0);
382*9880d681SAndroid Build Coastguard Worker           if (AluIter != MBBIter) {
383*9880d681SAndroid Build Coastguard Worker             insertMergedInstruction(BB, MBBIter, AluIter, Inc == 0);
384*9880d681SAndroid Build Coastguard Worker 
385*9880d681SAndroid Build Coastguard Worker             ++NumLdStAluCombined;
386*9880d681SAndroid Build Coastguard Worker             Modified = true;
387*9880d681SAndroid Build Coastguard Worker 
388*9880d681SAndroid Build Coastguard Worker             // Erase the matching ALU instruction
389*9880d681SAndroid Build Coastguard Worker             BB->erase(AluIter);
390*9880d681SAndroid Build Coastguard Worker             // Erase old load/store instruction
391*9880d681SAndroid Build Coastguard Worker             BB->erase(MBBIter++);
392*9880d681SAndroid Build Coastguard Worker             break;
393*9880d681SAndroid Build Coastguard Worker           }
394*9880d681SAndroid Build Coastguard Worker         }
395*9880d681SAndroid Build Coastguard Worker       }
396*9880d681SAndroid Build Coastguard Worker     }
397*9880d681SAndroid Build Coastguard Worker     if (MBBIter == End)
398*9880d681SAndroid Build Coastguard Worker       break;
399*9880d681SAndroid Build Coastguard Worker     ++MBBIter;
400*9880d681SAndroid Build Coastguard Worker   }
401*9880d681SAndroid Build Coastguard Worker 
402*9880d681SAndroid Build Coastguard Worker   return Modified;
403*9880d681SAndroid Build Coastguard Worker }
404*9880d681SAndroid Build Coastguard Worker 
405*9880d681SAndroid Build Coastguard Worker // Driver function that iterates over the machine basic building blocks of a
406*9880d681SAndroid Build Coastguard Worker // machine function
runOnMachineFunction(MachineFunction & MF)407*9880d681SAndroid Build Coastguard Worker bool LanaiMemAluCombiner::runOnMachineFunction(MachineFunction &MF) {
408*9880d681SAndroid Build Coastguard Worker   if (DisableMemAluCombiner)
409*9880d681SAndroid Build Coastguard Worker     return false;
410*9880d681SAndroid Build Coastguard Worker 
411*9880d681SAndroid Build Coastguard Worker   TII = MF.getSubtarget<LanaiSubtarget>().getInstrInfo();
412*9880d681SAndroid Build Coastguard Worker   bool Modified = false;
413*9880d681SAndroid Build Coastguard Worker   for (MfIterator MFI = MF.begin(); MFI != MF.end(); ++MFI) {
414*9880d681SAndroid Build Coastguard Worker     Modified |= combineMemAluInBasicBlock(&*MFI);
415*9880d681SAndroid Build Coastguard Worker   }
416*9880d681SAndroid Build Coastguard Worker   return Modified;
417*9880d681SAndroid Build Coastguard Worker }
418*9880d681SAndroid Build Coastguard Worker } // namespace
419*9880d681SAndroid Build Coastguard Worker 
createLanaiMemAluCombinerPass()420*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createLanaiMemAluCombinerPass() {
421*9880d681SAndroid Build Coastguard Worker   return new LanaiMemAluCombiner();
422*9880d681SAndroid Build Coastguard Worker }
423