You've already forked linux-packaging-mono
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
compiler-rt
libcxx
libcxxabi
libunwind
lld
lldb
llvm
bindings
cmake
docs
examples
include
lib
Analysis
AsmParser
BinaryFormat
Bitcode
CodeGen
DebugInfo
Demangle
ExecutionEngine
FuzzMutate
Fuzzer
IR
IRReader
LTO
LineEditor
Linker
MC
Object
ObjectYAML
Option
Passes
ProfileData
Support
TableGen
Target
AArch64
AMDGPU
ARC
ARM
AVR
BPF
Hexagon
Lanai
MSP430
Mips
NVPTX
Nios2
PowerPC
RISCV
Sparc
SystemZ
WebAssembly
X86
AsmParser
Disassembler
InstPrinter
MCTargetDesc
TargetInfo
Utils
CMakeLists.txt
LLVMBuild.txt
README-FPStack.txt
README-MMX.txt
README-SSE.txt
README-UNIMPLEMENTED.txt
README-X86-64.txt
README.txt
X86.h
X86.td
X86AsmPrinter.cpp
X86AsmPrinter.h
X86CallFrameOptimization.cpp
X86CallLowering.cpp
X86CallLowering.h
X86CallingConv.cpp
X86CallingConv.h
X86CallingConv.td
X86CmovConversion.cpp
X86DomainReassignment.cpp
X86EvexToVex.cpp
X86ExpandPseudo.cpp
X86FastISel.cpp.REMOVED.git-id
X86FixupBWInsts.cpp
X86FixupLEAs.cpp
X86FixupSetCC.cpp
X86FlagsCopyLowering.cpp
X86FloatingPoint.cpp
X86FrameLowering.cpp.REMOVED.git-id
X86FrameLowering.h
X86GenRegisterBankInfo.def
X86ISelDAGToDAG.cpp.REMOVED.git-id
X86ISelLowering.cpp.REMOVED.git-id
X86ISelLowering.h
X86Instr3DNow.td
X86InstrAVX512.td.REMOVED.git-id
X86InstrArithmetic.td
X86InstrBuilder.h
X86InstrCMovSetCC.td
X86InstrCompiler.td
X86InstrControl.td
X86InstrExtension.td
X86InstrFMA.td
X86InstrFMA3Info.cpp
X86InstrFMA3Info.h
X86InstrFPStack.td
X86InstrFormats.td
X86InstrFragmentsSIMD.td
X86InstrInfo.cpp.REMOVED.git-id
X86InstrInfo.h
X86InstrInfo.td.REMOVED.git-id
X86InstrMMX.td
X86InstrMPX.td
X86InstrSGX.td
X86InstrSSE.td.REMOVED.git-id
X86InstrSVM.td
X86InstrShiftRotate.td
X86InstrSystem.td
X86InstrTSX.td
X86InstrVMX.td
X86InstrVecCompiler.td
X86InstrXOP.td
X86InstructionSelector.cpp
X86InterleavedAccess.cpp
X86IntrinsicsInfo.h.REMOVED.git-id
X86LegalizerInfo.cpp
X86LegalizerInfo.h
X86MCInstLower.cpp
X86MachineFunctionInfo.cpp
X86MachineFunctionInfo.h
X86MacroFusion.cpp
X86MacroFusion.h
X86OptimizeLEAs.cpp
X86PadShortFunction.cpp
X86RegisterBankInfo.cpp
X86RegisterBankInfo.h
X86RegisterBanks.td
X86RegisterInfo.cpp
X86RegisterInfo.h
X86RegisterInfo.td
X86RetpolineThunks.cpp
X86SchedBroadwell.td.REMOVED.git-id
X86SchedHaswell.td.REMOVED.git-id
X86SchedSandyBridge.td.REMOVED.git-id
X86SchedSkylakeClient.td.REMOVED.git-id
X86SchedSkylakeServer.td.REMOVED.git-id
X86Schedule.td
X86ScheduleAtom.td
X86ScheduleBtVer2.td
X86ScheduleSLM.td
X86ScheduleZnver1.td
X86SelectionDAGInfo.cpp
X86SelectionDAGInfo.h
X86ShuffleDecodeConstantPool.cpp
X86ShuffleDecodeConstantPool.h
X86Subtarget.cpp
X86Subtarget.h
X86TargetMachine.cpp
X86TargetMachine.h
X86TargetObjectFile.cpp
X86TargetObjectFile.h
X86TargetTransformInfo.cpp.REMOVED.git-id
X86TargetTransformInfo.h
X86VZeroUpper.cpp
X86WinAllocaExpander.cpp
X86WinEHState.cpp
XCore
CMakeLists.txt
LLVMBuild.txt
README.txt
Target.cpp
TargetIntrinsicInfo.cpp
TargetLoweringObjectFile.cpp
TargetMachine.cpp
TargetMachineC.cpp
Testing
ToolDrivers
Transforms
WindowsManifest
XRay
CMakeLists.txt
LLVMBuild.txt
projects
resources
runtimes
scripts
test
tools
unittests
utils
.arcconfig
.clang-format
.clang-tidy
.gitattributes
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
CREDITS.TXT
LICENSE.TXT
LLVMBuild.txt
README.txt
RELEASE_TESTERS.TXT
configure
llvm.spec.in
openmp
polly
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mk
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
218 lines
6.6 KiB
C++
218 lines
6.6 KiB
C++
//===-------- X86PadShortFunction.cpp - pad short functions -----------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines the pass which will pad short functions to prevent
|
|
// a stall if a function returns before the return address is ready. This
|
|
// is needed for some Intel Atom processors.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
#include "X86.h"
|
|
#include "X86InstrInfo.h"
|
|
#include "X86Subtarget.h"
|
|
#include "llvm/ADT/Statistic.h"
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
#include "llvm/CodeGen/Passes.h"
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
|
#include "llvm/IR/Function.h"
|
|
#include "llvm/Support/Debug.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
#define DEBUG_TYPE "x86-pad-short-functions"
|
|
|
|
STATISTIC(NumBBsPadded, "Number of basic blocks padded");
|
|
|
|
namespace {
|
|
struct VisitedBBInfo {
|
|
// HasReturn - Whether the BB contains a return instruction
|
|
bool HasReturn;
|
|
|
|
// Cycles - Number of cycles until return if HasReturn is true, otherwise
|
|
// number of cycles until end of the BB
|
|
unsigned int Cycles;
|
|
|
|
VisitedBBInfo() : HasReturn(false), Cycles(0) {}
|
|
VisitedBBInfo(bool HasReturn, unsigned int Cycles)
|
|
: HasReturn(HasReturn), Cycles(Cycles) {}
|
|
};
|
|
|
|
struct PadShortFunc : public MachineFunctionPass {
|
|
static char ID;
|
|
PadShortFunc() : MachineFunctionPass(ID)
|
|
, Threshold(4), STI(nullptr), TII(nullptr) {}
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
return MachineFunctionProperties().set(
|
|
MachineFunctionProperties::Property::NoVRegs);
|
|
}
|
|
|
|
StringRef getPassName() const override {
|
|
return "X86 Atom pad short functions";
|
|
}
|
|
|
|
private:
|
|
void findReturns(MachineBasicBlock *MBB,
|
|
unsigned int Cycles = 0);
|
|
|
|
bool cyclesUntilReturn(MachineBasicBlock *MBB,
|
|
unsigned int &Cycles);
|
|
|
|
void addPadding(MachineBasicBlock *MBB,
|
|
MachineBasicBlock::iterator &MBBI,
|
|
unsigned int NOOPsToAdd);
|
|
|
|
const unsigned int Threshold;
|
|
|
|
// ReturnBBs - Maps basic blocks that return to the minimum number of
|
|
// cycles until the return, starting from the entry block.
|
|
DenseMap<MachineBasicBlock*, unsigned int> ReturnBBs;
|
|
|
|
// VisitedBBs - Cache of previously visited BBs.
|
|
DenseMap<MachineBasicBlock*, VisitedBBInfo> VisitedBBs;
|
|
|
|
const X86Subtarget *STI;
|
|
const TargetInstrInfo *TII;
|
|
};
|
|
|
|
char PadShortFunc::ID = 0;
|
|
}
|
|
|
|
FunctionPass *llvm::createX86PadShortFunctions() {
|
|
return new PadShortFunc();
|
|
}
|
|
|
|
/// runOnMachineFunction - Loop over all of the basic blocks, inserting
|
|
/// NOOP instructions before early exits.
|
|
bool PadShortFunc::runOnMachineFunction(MachineFunction &MF) {
|
|
if (skipFunction(MF.getFunction()))
|
|
return false;
|
|
|
|
if (MF.getFunction().optForSize()) {
|
|
return false;
|
|
}
|
|
|
|
STI = &MF.getSubtarget<X86Subtarget>();
|
|
if (!STI->padShortFunctions())
|
|
return false;
|
|
|
|
TII = STI->getInstrInfo();
|
|
|
|
// Search through basic blocks and mark the ones that have early returns
|
|
ReturnBBs.clear();
|
|
VisitedBBs.clear();
|
|
findReturns(&MF.front());
|
|
|
|
bool MadeChange = false;
|
|
|
|
MachineBasicBlock *MBB;
|
|
unsigned int Cycles = 0;
|
|
|
|
// Pad the identified basic blocks with NOOPs
|
|
for (DenseMap<MachineBasicBlock*, unsigned int>::iterator I = ReturnBBs.begin();
|
|
I != ReturnBBs.end(); ++I) {
|
|
MBB = I->first;
|
|
Cycles = I->second;
|
|
|
|
if (Cycles < Threshold) {
|
|
// BB ends in a return. Skip over any DBG_VALUE instructions
|
|
// trailing the terminator.
|
|
assert(MBB->size() > 0 &&
|
|
"Basic block should contain at least a RET but is empty");
|
|
MachineBasicBlock::iterator ReturnLoc = --MBB->end();
|
|
|
|
while (ReturnLoc->isDebugValue())
|
|
--ReturnLoc;
|
|
assert(ReturnLoc->isReturn() && !ReturnLoc->isCall() &&
|
|
"Basic block does not end with RET");
|
|
|
|
addPadding(MBB, ReturnLoc, Threshold - Cycles);
|
|
NumBBsPadded++;
|
|
MadeChange = true;
|
|
}
|
|
}
|
|
|
|
return MadeChange;
|
|
}
|
|
|
|
/// findReturn - Starting at MBB, follow control flow and add all
|
|
/// basic blocks that contain a return to ReturnBBs.
|
|
void PadShortFunc::findReturns(MachineBasicBlock *MBB, unsigned int Cycles) {
|
|
// If this BB has a return, note how many cycles it takes to get there.
|
|
bool hasReturn = cyclesUntilReturn(MBB, Cycles);
|
|
if (Cycles >= Threshold)
|
|
return;
|
|
|
|
if (hasReturn) {
|
|
ReturnBBs[MBB] = std::max(ReturnBBs[MBB], Cycles);
|
|
return;
|
|
}
|
|
|
|
// Follow branches in BB and look for returns
|
|
for (MachineBasicBlock::succ_iterator I = MBB->succ_begin();
|
|
I != MBB->succ_end(); ++I) {
|
|
if (*I == MBB)
|
|
continue;
|
|
findReturns(*I, Cycles);
|
|
}
|
|
}
|
|
|
|
/// cyclesUntilReturn - return true if the MBB has a return instruction,
|
|
/// and return false otherwise.
|
|
/// Cycles will be incremented by the number of cycles taken to reach the
|
|
/// return or the end of the BB, whichever occurs first.
|
|
bool PadShortFunc::cyclesUntilReturn(MachineBasicBlock *MBB,
|
|
unsigned int &Cycles) {
|
|
// Return cached result if BB was previously visited
|
|
DenseMap<MachineBasicBlock*, VisitedBBInfo>::iterator it
|
|
= VisitedBBs.find(MBB);
|
|
if (it != VisitedBBs.end()) {
|
|
VisitedBBInfo BBInfo = it->second;
|
|
Cycles += BBInfo.Cycles;
|
|
return BBInfo.HasReturn;
|
|
}
|
|
|
|
unsigned int CyclesToEnd = 0;
|
|
|
|
for (MachineInstr &MI : *MBB) {
|
|
// Mark basic blocks with a return instruction. Calls to other
|
|
// functions do not count because the called function will be padded,
|
|
// if necessary.
|
|
if (MI.isReturn() && !MI.isCall()) {
|
|
VisitedBBs[MBB] = VisitedBBInfo(true, CyclesToEnd);
|
|
Cycles += CyclesToEnd;
|
|
return true;
|
|
}
|
|
|
|
CyclesToEnd += TII->getInstrLatency(STI->getInstrItineraryData(), MI);
|
|
}
|
|
|
|
VisitedBBs[MBB] = VisitedBBInfo(false, CyclesToEnd);
|
|
Cycles += CyclesToEnd;
|
|
return false;
|
|
}
|
|
|
|
/// addPadding - Add the given number of NOOP instructions to the function
|
|
/// just prior to the return at MBBI
|
|
void PadShortFunc::addPadding(MachineBasicBlock *MBB,
|
|
MachineBasicBlock::iterator &MBBI,
|
|
unsigned int NOOPsToAdd) {
|
|
DebugLoc DL = MBBI->getDebugLoc();
|
|
|
|
while (NOOPsToAdd-- > 0) {
|
|
BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
|
|
BuildMI(*MBB, MBBI, DL, TII->get(X86::NOOP));
|
|
}
|
|
}
|