1 //===-------- X86PadShortFunction.cpp - pad short functions -----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the pass which will pad short functions to prevent
10 // a stall if a function returns before the return address is ready. This
11 // is needed for some Intel Atom processors.
13 //===----------------------------------------------------------------------===//
17 #include "X86InstrInfo.h"
18 #include "X86Subtarget.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/ProfileSummaryInfo.h"
21 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineSizeOpts.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/CodeGen/TargetSchedule.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
33 #define DEBUG_TYPE "x86-pad-short-functions"
35 STATISTIC(NumBBsPadded, "Number of basic blocks padded");
38 struct VisitedBBInfo {
39 // HasReturn - Whether the BB contains a return instruction
42 // Cycles - Number of cycles until return if HasReturn is true, otherwise
43 // number of cycles until end of the BB
46 VisitedBBInfo() : HasReturn(false), Cycles(0) {}
47 VisitedBBInfo(bool HasReturn, unsigned int Cycles)
48 : HasReturn(HasReturn), Cycles(Cycles) {}
51 struct PadShortFunc : public MachineFunctionPass {
53 PadShortFunc() : MachineFunctionPass(ID)
56 bool runOnMachineFunction(MachineFunction &MF) override;
58 void getAnalysisUsage(AnalysisUsage &AU) const override {
59 AU.addRequired<ProfileSummaryInfoWrapperPass>();
60 AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
61 MachineFunctionPass::getAnalysisUsage(AU);
64 MachineFunctionProperties getRequiredProperties() const override {
65 return MachineFunctionProperties().set(
66 MachineFunctionProperties::Property::NoVRegs);
69 StringRef getPassName() const override {
70 return "X86 Atom pad short functions";
74 void findReturns(MachineBasicBlock *MBB,
75 unsigned int Cycles = 0);
77 bool cyclesUntilReturn(MachineBasicBlock *MBB,
78 unsigned int &Cycles);
80 void addPadding(MachineBasicBlock *MBB,
81 MachineBasicBlock::iterator &MBBI,
82 unsigned int NOOPsToAdd);
84 const unsigned int Threshold;
86 // ReturnBBs - Maps basic blocks that return to the minimum number of
87 // cycles until the return, starting from the entry block.
88 DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
90 // VisitedBBs - Cache of previously visited BBs.
91 DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
96 char PadShortFunc::ID = 0;
99 FunctionPass *llvm::createX86PadShortFunctions() {
100 return new PadShortFunc();
103 /// runOnMachineFunction - Loop over all of the basic blocks, inserting
104 /// NOOP instructions before early exits.
105 bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
106 if (skipFunction(MF.getFunction()))
109 if (MF.getFunction().hasOptSize())
112 if (!MF.getSubtarget<X86Subtarget>().padShortFunctions())
115 TSM.init(&MF.getSubtarget());
118 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
119 auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
120 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
123 // Search through basic blocks and mark the ones that have early returns
126 findReturns(&MF.front());
128 bool MadeChange = false;
130 // Pad the identified basic blocks with NOOPs
131 for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
132 I != ReturnBBs.end(); ++I) {
133 MachineBasicBlock *MBB = I->first;
134 unsigned Cycles = I->second;
136 // Function::hasOptSize is already checked above.
137 bool OptForSize = llvm::shouldOptimizeForSize(MBB, PSI, MBFI);
141 if (Cycles < Threshold) {
142 // BB ends in a return. Skip over any DBG_VALUE instructions
143 // trailing the terminator.
144 assert(MBB->size() > 0 &&
145 "Basic block should contain at least a RET but is empty");
146 MachineBasicBlock::iterator ReturnLoc = --MBB->end();
148 while (ReturnLoc->isDebugInstr())
150 assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
151 "Basic block does not end with RET");
153 addPadding(MBB, ReturnLoc, Threshold - Cycles);
162 /// findReturn - Starting at MBB, follow control flow and add all
163 /// basic blocks that contain a return to ReturnBBs.
164 void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
165 // If this BB has a return, note how many cycles it takes to get there.
166 bool hasReturn = cyclesUntilReturn(MBB, Cycles);
167 if (Cycles >= Threshold)
171 ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
175 // Follow branches in BB and look for returns
176 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
177 I != MBB->succ_end(); ++I) {
180 findReturns(*I, Cycles);
184 /// cyclesUntilReturn - return true if the MBB has a return instruction,
185 /// and return false otherwise.
186 /// Cycles will be incremented by the number of cycles taken to reach the
187 /// return or the end of the BB, whichever occurs first.
188 bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
189 unsigned int &Cycles) {
190 // Return cached result if BB was previously visited
191 DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
192 = VisitedBBs.find(MBB);
193 if (it != VisitedBBs.end()) {
194 VisitedBBInfo BBInfo = it->second;
195 Cycles += BBInfo.Cycles;
196 return BBInfo.HasReturn;
199 unsigned int CyclesToEnd = 0;
201 for (MachineInstr &MI : *MBB) {
202 // Mark basic blocks with a return instruction. Calls to other
203 // functions do not count because the called function will be padded,
205 if (MI.isReturn() && !MI.isCall()) {
206 VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
207 Cycles += CyclesToEnd;
211 CyclesToEnd += TSM.computeInstrLatency(&MI);
214 VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
215 Cycles += CyclesToEnd;
219 /// addPadding - Add the given number of NOOP instructions to the function
220 /// just prior to the return at MBBI
221 void PadShortFunc::addPadding(MachineBasicBlock *MBB,
222 MachineBasicBlock::iterator &MBBI,
223 unsigned int NOOPsToAdd) {
224 DebugLoc DL = MBBI->getDebugLoc();
225 unsigned IssueWidth = TSM.getIssueWidth();
227 for (unsigned i = 0, e = IssueWidth * NOOPsToAdd; i != e; ++i)
228 BuildMI(*MBB, MBBI, DL, TSM.getInstrInfo()->get(X86::NOOP));