Index: include/llvm/CodeGen/MachineFunction.h =================================================================== --- include/llvm/CodeGen/MachineFunction.h +++ include/llvm/CodeGen/MachineFunction.h @@ -228,6 +228,10 @@ // Allocation management for pseudo source values. std::unique_ptr PSVManager; + /// Mark a basic block after which the function is cold. + /// This is null if no such fragment exists or has been computed. + const MachineBasicBlock *ColdFragmentStart; + MachineFunction(const MachineFunction &) = delete; void operator=(const MachineFunction&) = delete; public: @@ -346,6 +350,17 @@ const MachineFunctionProperties &getProperties() const { return Properties; } MachineFunctionProperties &getProperties() { return Properties; } + /// Get the start of the cold fragment. null if none exists or was conputed. + const MachineBasicBlock *getColdFragmentStart() const { + return ColdFragmentStart; + } + + /// Set the start of the cold fragment. + void setColdFragmentStart(const MachineBasicBlock *CFS) { + assert(!CFS || CFS->getParent() == this); + ColdFragmentStart = CFS; + } + /// getInfo - Keep track of various per-function pieces of information for /// backends that would like to do so. /// Index: lib/CodeGen/MachineBlockPlacement.cpp =================================================================== --- lib/CodeGen/MachineBlockPlacement.cpp +++ lib/CodeGen/MachineBlockPlacement.cpp @@ -41,6 +41,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/ScaledNumber.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetInstrInfo.h" #include "llvm/Target/TargetLowering.h" @@ -588,6 +589,25 @@ BestFreq = CandidateFreq; } + if (!BestBlock) + return nullptr; + + auto *MF = BestBlock->getParent(); + if (MF->getColdFragmentStart()) + return BestBlock; + + bool IsCold = IsEHPad; + if (!IsCold) { + typedef ScaledNumber Scaled64; + Scaled64 B(BestFreq.getFrequency(), 5); + Scaled64 E(MBFI->getEntryFreq(), 0); + + IsCold = (B / E) < 1; + } + + if (IsCold) + MF->setColdFragmentStart(BestBlock); + return BestBlock; } @@ -1258,6 +1278,10 @@ for (MachineBasicBlock &MBB : F) fillWorkLists(&MBB, UpdatedPreds, BlockWorkList, EHPadWorkList); + // We are reordering blocks, make sure we reset the cold fragment + // start and reset it where apropriate. + F.setColdFragmentStart(nullptr); + BlockChain &FunctionChain = *BlockToChain[&F.front()]; buildChain(&F.front(), FunctionChain, BlockWorkList, EHPadWorkList); Index: lib/CodeGen/MachineFunction.cpp =================================================================== --- lib/CodeGen/MachineFunction.cpp +++ lib/CodeGen/MachineFunction.cpp @@ -133,6 +133,7 @@ FunctionNumber = FunctionNum; JumpTableInfo = nullptr; + ColdFragmentStart = nullptr; if (isFuncletEHPersonality(classifyEHPersonality( F->hasPersonalityFn() ? F->getPersonalityFn() : nullptr))) {