xref: /llvm-project/bolt/lib/Passes/AllocCombiner.cpp (revision f92ab6af35343df1f82c3a85feb75d41b6be382e)
12f09f445SMaksim Panchenko //===- bolt/Passes/AllocCombiner.cpp --------------------------------------===//
2a34c753fSRafael Auler //
3a34c753fSRafael Auler // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a34c753fSRafael Auler // See https://llvm.org/LICENSE.txt for license information.
5a34c753fSRafael Auler // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a34c753fSRafael Auler //
7a34c753fSRafael Auler //===----------------------------------------------------------------------===//
8a34c753fSRafael Auler //
92f09f445SMaksim Panchenko // This file implements the AllocCombinerPass class.
102f09f445SMaksim Panchenko //
11a34c753fSRafael Auler //===----------------------------------------------------------------------===//
12a34c753fSRafael Auler 
13a34c753fSRafael Auler #include "bolt/Passes/AllocCombiner.h"
14a34c753fSRafael Auler 
15a34c753fSRafael Auler #define DEBUG_TYPE "alloccombiner"
16a34c753fSRafael Auler 
17a34c753fSRafael Auler using namespace llvm;
18a34c753fSRafael Auler 
19a34c753fSRafael Auler namespace opts {
20a34c753fSRafael Auler 
21a34c753fSRafael Auler extern cl::opt<bolt::FrameOptimizationType> FrameOptimization;
22a34c753fSRafael Auler 
23a34c753fSRafael Auler } // end namespace opts
24a34c753fSRafael Auler 
25a34c753fSRafael Auler namespace llvm {
26a34c753fSRafael Auler namespace bolt {
27a34c753fSRafael Auler 
28a34c753fSRafael Auler namespace {
29a34c753fSRafael Auler 
30a34c753fSRafael Auler bool getStackAdjustmentSize(const BinaryContext &BC, const MCInst &Inst,
31a34c753fSRafael Auler                             int64_t &Adjustment) {
32a34c753fSRafael Auler   return BC.MIB->evaluateSimple(Inst, Adjustment,
33a34c753fSRafael Auler                                 std::make_pair(BC.MIB->getStackPointer(), 0LL),
34a34c753fSRafael Auler                                 std::make_pair(0, 0LL));
35a34c753fSRafael Auler }
36a34c753fSRafael Auler 
37a34c753fSRafael Auler bool isIndifferentToSP(const MCInst &Inst, const BinaryContext &BC) {
38a34c753fSRafael Auler   if (BC.MIB->isCFI(Inst))
39a34c753fSRafael Auler     return true;
40a34c753fSRafael Auler 
41a34c753fSRafael Auler   const MCInstrDesc II = BC.MII->get(Inst.getOpcode());
42a34c753fSRafael Auler   if (BC.MIB->isTerminator(Inst) ||
43a34c753fSRafael Auler       II.hasImplicitDefOfPhysReg(BC.MIB->getStackPointer(), BC.MRI.get()) ||
44a34c753fSRafael Auler       II.hasImplicitUseOfPhysReg(BC.MIB->getStackPointer()))
45a34c753fSRafael Auler     return false;
46a34c753fSRafael Auler 
47a34c753fSRafael Auler   for (int I = 0, E = MCPlus::getNumPrimeOperands(Inst); I != E; ++I) {
48a34c753fSRafael Auler     const MCOperand &Operand = Inst.getOperand(I);
49*f92ab6afSAmir Ayupov     if (Operand.isReg() && Operand.getReg() == BC.MIB->getStackPointer())
50a34c753fSRafael Auler       return false;
51a34c753fSRafael Auler   }
52a34c753fSRafael Auler   return true;
53a34c753fSRafael Auler }
54a34c753fSRafael Auler 
55a34c753fSRafael Auler bool shouldProcess(const BinaryFunction &Function) {
56a34c753fSRafael Auler   return Function.isSimple() && Function.hasCFG() && !Function.isIgnored();
57a34c753fSRafael Auler }
58a34c753fSRafael Auler 
59a34c753fSRafael Auler void runForAllWeCare(std::map<uint64_t, BinaryFunction> &BFs,
60a34c753fSRafael Auler                      std::function<void(BinaryFunction &)> Task) {
61a34c753fSRafael Auler   for (auto &It : BFs) {
62a34c753fSRafael Auler     BinaryFunction &Function = It.second;
63a34c753fSRafael Auler     if (shouldProcess(Function))
64a34c753fSRafael Auler       Task(Function);
65a34c753fSRafael Auler   }
66a34c753fSRafael Auler }
67a34c753fSRafael Auler 
68a34c753fSRafael Auler } // end anonymous namespace
69a34c753fSRafael Auler 
7060b09997SMaksim Panchenko void AllocCombinerPass::combineAdjustments(BinaryFunction &BF) {
7160b09997SMaksim Panchenko   BinaryContext &BC = BF.getBinaryContext();
72a34c753fSRafael Auler   for (BinaryBasicBlock &BB : BF) {
73a34c753fSRafael Auler     MCInst *Prev = nullptr;
74a34c753fSRafael Auler     for (auto I = BB.rbegin(), E = BB.rend(); I != E; ++I) {
75a34c753fSRafael Auler       MCInst &Inst = *I;
76a34c753fSRafael Auler       if (isIndifferentToSP(Inst, BC))
77a34c753fSRafael Auler         continue; // Skip updating Prev
78a34c753fSRafael Auler 
79a34c753fSRafael Auler       int64_t Adjustment = 0LL;
80a34c753fSRafael Auler       if (!Prev || !BC.MIB->isStackAdjustment(Inst) ||
81a34c753fSRafael Auler           !BC.MIB->isStackAdjustment(*Prev) ||
82a34c753fSRafael Auler           !getStackAdjustmentSize(BC, *Prev, Adjustment)) {
83a34c753fSRafael Auler         Prev = &Inst;
84a34c753fSRafael Auler         continue;
85a34c753fSRafael Auler       }
86a34c753fSRafael Auler 
87a34c753fSRafael Auler       LLVM_DEBUG({
88a34c753fSRafael Auler         dbgs() << "At \"" << BF.getPrintName() << "\", combining: \n";
89a34c753fSRafael Auler         Inst.dump();
90a34c753fSRafael Auler         Prev->dump();
91a34c753fSRafael Auler         dbgs() << "Adjustment: " << Adjustment << "\n";
92a34c753fSRafael Auler       });
93a34c753fSRafael Auler 
94a34c753fSRafael Auler       if (BC.MIB->isSUB(Inst))
95a34c753fSRafael Auler         Adjustment = -Adjustment;
96a34c753fSRafael Auler 
97a34c753fSRafael Auler       BC.MIB->addToImm(Inst, Adjustment, BC.Ctx.get());
98a34c753fSRafael Auler 
99a34c753fSRafael Auler       LLVM_DEBUG({
100a34c753fSRafael Auler         dbgs() << "After adjustment:\n";
101a34c753fSRafael Auler         Inst.dump();
102a34c753fSRafael Auler       });
103a34c753fSRafael Auler 
104a34c753fSRafael Auler       BB.eraseInstruction(BB.findInstruction(Prev));
105a34c753fSRafael Auler       ++NumCombined;
106a34c753fSRafael Auler       FuncsChanged.insert(&BF);
107a34c753fSRafael Auler       Prev = &Inst;
108a34c753fSRafael Auler     }
109a34c753fSRafael Auler   }
110a34c753fSRafael Auler }
111a34c753fSRafael Auler 
112a34c753fSRafael Auler void AllocCombinerPass::runOnFunctions(BinaryContext &BC) {
113a34c753fSRafael Auler   if (opts::FrameOptimization == FOP_NONE)
114a34c753fSRafael Auler     return;
115a34c753fSRafael Auler 
11660b09997SMaksim Panchenko   runForAllWeCare(BC.getBinaryFunctions(), [&](BinaryFunction &Function) {
11760b09997SMaksim Panchenko     combineAdjustments(Function);
11860b09997SMaksim Panchenko   });
119a34c753fSRafael Auler 
120a34c753fSRafael Auler   outs() << "BOLT-INFO: Allocation combiner: " << NumCombined
121a34c753fSRafael Auler          << " empty spaces coalesced.\n";
122a34c753fSRafael Auler }
123a34c753fSRafael Auler 
124a34c753fSRafael Auler } // end namespace bolt
125a34c753fSRafael Auler } // end namespace llvm
126