2017-06-07 06:22:41 +08:00
|
|
|
//===- BranchFolding.cpp - Fold machine code branch instructions ----------===//
|
2005-04-22 06:36:52 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-22 06:36:52 +08:00
|
|
|
//
|
2004-07-31 18:01:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass forwards branches to unconditional branches to make them branch
|
|
|
|
// directly to the target block. This pass often results in dead MBB's, which
|
|
|
|
// it then removes.
|
|
|
|
//
|
|
|
|
// Note that this pass must be run after register allocation, it cannot handle
|
2015-07-30 02:59:09 +08:00
|
|
|
// SSA form. It also must handle virtual registers for targets that emit virtual
|
|
|
|
// ISA (e.g. NVPTX).
|
2004-07-31 18:01:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
#include "BranchFolding.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2019-12-06 01:39:37 +08:00
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
2015-10-04 10:22:52 +08:00
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2014-08-08 03:30:13 +08:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2004-07-31 18:01:27 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2017-09-07 04:45:24 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2006-10-18 01:13:52 +08:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2016-06-09 23:24:29 +08:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2012-03-28 01:06:09 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2019-12-06 01:39:37 +08:00
|
|
|
#include "llvm/CodeGen/MachineSizeOpts.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetOpcodes.h"
|
2016-05-10 11:21:59 +08:00
|
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2017-03-15 13:44:59 +08:00
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/MC/LaneBitmask.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
|
|
|
#include "llvm/Support/BranchProbability.h"
|
2006-10-21 08:47:49 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-11-19 05:56:39 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-23 04:03:00 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-07 06:22:41 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
|
|
|
#include <numeric>
|
|
|
|
#include <vector>
|
|
|
|
|
2004-07-31 18:01:27 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
#define DEBUG_TYPE "branch-folder"
|
2014-04-22 10:02:50 +08:00
|
|
|
|
2006-12-20 06:41:21 +08:00
|
|
|
STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
|
|
|
|
STATISTIC(NumBranchOpts, "Number of branches optimized");
|
|
|
|
STATISTIC(NumTailMerge , "Number of block tails merged");
|
2011-05-12 08:56:58 +08:00
|
|
|
STATISTIC(NumHoist , "Number of times common instructions are hoisted");
|
2017-02-16 08:04:05 +08:00
|
|
|
STATISTIC(NumTailCalls, "Number of tail calls optimized");
|
2009-11-19 03:29:37 +08:00
|
|
|
|
2009-11-12 02:38:14 +08:00
|
|
|
static cl::opt<cl::boolOrDefault> FlagEnableTailMerge("enable-tail-merge",
|
2007-05-23 01:14:46 +08:00
|
|
|
cl::init(cl::BOU_UNSET), cl::Hidden);
|
2009-11-19 03:29:37 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
// Throttle for huge numbers of predecessors (compile speed problems)
|
|
|
|
static cl::opt<unsigned>
|
2009-11-12 02:38:14 +08:00
|
|
|
TailMergeThreshold("tail-merge-threshold",
|
2008-05-13 08:00:25 +08:00
|
|
|
cl::desc("Max number of predecessors to consider tail merging"),
|
2008-10-27 10:10:21 +08:00
|
|
|
cl::init(150), cl::Hidden);
|
2007-06-08 09:08:52 +08:00
|
|
|
|
2009-11-12 03:48:59 +08:00
|
|
|
// Heuristic for tail merging (and, inversely, tail duplication).
|
|
|
|
// TODO: This should be replaced with a target query.
|
|
|
|
static cl::opt<unsigned>
|
2009-11-17 01:56:13 +08:00
|
|
|
TailMergeSize("tail-merge-size",
|
2017-10-11 06:33:29 +08:00
|
|
|
cl::desc("Min number of instructions to consider tail merging"),
|
|
|
|
cl::init(3), cl::Hidden);
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2009-11-12 09:59:26 +08:00
|
|
|
namespace {
|
2017-06-07 06:22:41 +08:00
|
|
|
|
2009-11-12 09:59:26 +08:00
|
|
|
/// BranchFolderPass - Wrap branch folder in a machine function pass.
|
2012-02-09 05:22:48 +08:00
|
|
|
class BranchFolderPass : public MachineFunctionPass {
|
2009-11-12 09:59:26 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
2017-06-07 06:22:41 +08:00
|
|
|
|
2012-02-09 05:22:48 +08:00
|
|
|
explicit BranchFolderPass(): MachineFunctionPass(ID) {}
|
2009-11-12 09:59:26 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2012-02-09 05:22:48 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-08-08 03:30:13 +08:00
|
|
|
AU.addRequired<MachineBlockFrequencyInfo>();
|
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
2019-12-06 01:39:37 +08:00
|
|
|
AU.addRequired<ProfileSummaryInfoWrapperPass>();
|
2012-02-09 05:22:48 +08:00
|
|
|
AU.addRequired<TargetPassConfig>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2009-11-12 09:59:26 +08:00
|
|
|
};
|
2017-06-07 06:22:41 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2009-11-12 09:59:26 +08:00
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
char BranchFolderPass::ID = 0;
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2012-02-09 05:22:48 +08:00
|
|
|
char &llvm::BranchFolderPassID = BranchFolderPass::ID;
|
2004-07-31 18:01:27 +08:00
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS(BranchFolderPass, DEBUG_TYPE,
|
2012-02-09 05:22:48 +08:00
|
|
|
"Control Flow Optimizer", false, false)
|
2009-09-04 15:47:40 +08:00
|
|
|
|
|
|
|
bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-16 06:22:58 +08:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2014-04-01 01:43:35 +08:00
|
|
|
return false;
|
|
|
|
|
2012-02-09 05:22:48 +08:00
|
|
|
TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
|
2013-12-07 09:49:19 +08:00
|
|
|
// TailMerge can create jump into if branches that make CFG irreducible for
|
2015-10-04 10:22:52 +08:00
|
|
|
// HW that requires structurized CFG.
|
2013-12-07 09:49:19 +08:00
|
|
|
bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
|
2015-10-02 05:04:13 +08:00
|
|
|
PassConfig->getEnableTailMerge();
|
2020-01-28 02:05:54 +08:00
|
|
|
MBFIWrapper MBBFreqInfo(
|
2016-06-09 23:24:29 +08:00
|
|
|
getAnalysis<MachineBlockFrequencyInfo>());
|
|
|
|
BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo,
|
2019-12-06 01:39:37 +08:00
|
|
|
getAnalysis<MachineBranchProbabilityInfo>(),
|
|
|
|
&getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI());
|
2019-10-01 01:54:50 +08:00
|
|
|
auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
|
|
|
|
return Folder.OptimizeFunction(
|
|
|
|
MF, MF.getSubtarget().getInstrInfo(), MF.getSubtarget().getRegisterInfo(),
|
|
|
|
MMIWP ? &MMIWP->getMMI() : nullptr);
|
2009-09-04 15:47:40 +08:00
|
|
|
}
|
|
|
|
|
2014-08-08 03:30:13 +08:00
|
|
|
BranchFolder::BranchFolder(bool defaultEnableTailMerge, bool CommonHoist,
|
2016-06-09 23:24:29 +08:00
|
|
|
MBFIWrapper &FreqInfo,
|
2016-08-19 02:57:29 +08:00
|
|
|
const MachineBranchProbabilityInfo &ProbInfo,
|
2019-12-06 01:39:37 +08:00
|
|
|
ProfileSummaryInfo *PSI,
|
2016-08-19 02:57:29 +08:00
|
|
|
unsigned MinTailLength)
|
|
|
|
: EnableHoistCommonCode(CommonHoist), MinCommonTailLength(MinTailLength),
|
2019-12-06 01:39:37 +08:00
|
|
|
MBBFreqInfo(FreqInfo), MBPI(ProbInfo), PSI(PSI) {
|
2016-08-19 02:57:29 +08:00
|
|
|
if (MinCommonTailLength == 0)
|
|
|
|
MinCommonTailLength = TailMergeSize;
|
2009-09-04 15:47:40 +08:00
|
|
|
switch (FlagEnableTailMerge) {
|
|
|
|
case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge; break;
|
|
|
|
case cl::BOU_TRUE: EnableTailMerge = true; break;
|
|
|
|
case cl::BOU_FALSE: EnableTailMerge = false; break;
|
|
|
|
}
|
2009-09-04 07:54:22 +08:00
|
|
|
}
|
2004-07-31 18:01:27 +08:00
|
|
|
|
2006-10-18 07:17:27 +08:00
|
|
|
void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) {
|
2007-02-23 00:39:03 +08:00
|
|
|
assert(MBB->pred_empty() && "MBB must be dead!");
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nRemoving MBB: " << *MBB);
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-10-18 01:13:52 +08:00
|
|
|
MachineFunction *MF = MBB->getParent();
|
|
|
|
// drop all successors.
|
|
|
|
while (!MBB->succ_empty())
|
|
|
|
MBB->removeSuccessor(MBB->succ_end()-1);
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2011-06-14 23:31:54 +08:00
|
|
|
// Avoid matching if this pointer gets reused.
|
|
|
|
TriedMerging.erase(MBB);
|
|
|
|
|
2019-10-08 23:43:12 +08:00
|
|
|
// Update call site info.
|
|
|
|
std::for_each(MBB->begin(), MBB->end(), [MF](const MachineInstr &MI) {
|
2020-02-10 16:49:14 +08:00
|
|
|
if (MI.isCandidateForCallSiteEntry())
|
2019-10-08 23:43:12 +08:00
|
|
|
MF->eraseCallSiteInfo(&MI);
|
|
|
|
});
|
2006-10-18 01:13:52 +08:00
|
|
|
// Remove the block.
|
2008-07-08 07:14:23 +08:00
|
|
|
MF->erase(MBB);
|
2018-06-01 08:03:21 +08:00
|
|
|
EHScopeMembership.erase(MBB);
|
2016-06-09 23:24:29 +08:00
|
|
|
if (MLI)
|
|
|
|
MLI->removeBlock(MBB);
|
2006-10-18 01:13:52 +08:00
|
|
|
}
|
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
bool BranchFolder::OptimizeFunction(MachineFunction &MF,
|
|
|
|
const TargetInstrInfo *tii,
|
|
|
|
const TargetRegisterInfo *tri,
|
2016-06-09 23:24:29 +08:00
|
|
|
MachineModuleInfo *mmi,
|
|
|
|
MachineLoopInfo *mli, bool AfterPlacement) {
|
2009-09-04 15:47:40 +08:00
|
|
|
if (!tii) return false;
|
|
|
|
|
2011-06-14 23:31:54 +08:00
|
|
|
TriedMerging.clear();
|
|
|
|
|
2017-05-26 14:32:31 +08:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
2016-06-09 23:24:29 +08:00
|
|
|
AfterBlockPlacement = AfterPlacement;
|
2009-09-04 15:47:40 +08:00
|
|
|
TII = tii;
|
|
|
|
TRI = tri;
|
|
|
|
MMI = mmi;
|
2016-06-09 23:24:29 +08:00
|
|
|
MLI = mli;
|
2017-05-26 14:32:31 +08:00
|
|
|
this->MRI = &MRI;
|
2012-03-28 01:06:09 +08:00
|
|
|
|
2016-07-13 02:44:33 +08:00
|
|
|
UpdateLiveIns = MRI.tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF);
|
|
|
|
if (!UpdateLiveIns)
|
2012-03-28 01:06:09 +08:00
|
|
|
MRI.invalidateLiveness();
|
2008-04-10 10:32:10 +08:00
|
|
|
|
2007-05-16 05:19:17 +08:00
|
|
|
// Fix CFG. The later algorithms expect it to be right.
|
2009-09-04 15:47:40 +08:00
|
|
|
bool MadeChange = false;
|
2015-09-16 11:52:35 +08:00
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2008-08-15 06:49:33 +08:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2016-07-15 22:41:04 +08:00
|
|
|
if (!TII->analyzeBranch(MBB, TBB, FBB, Cond, true))
|
2015-09-16 11:52:35 +08:00
|
|
|
MadeChange |= MBB.CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
|
2007-05-16 05:19:17 +08:00
|
|
|
}
|
|
|
|
|
2018-06-01 08:03:21 +08:00
|
|
|
// Recalculate EH scope membership.
|
|
|
|
EHScopeMembership = getEHScopeMembership(MF);
|
2015-10-04 10:22:52 +08:00
|
|
|
|
2006-10-21 08:47:49 +08:00
|
|
|
bool MadeChangeThisIteration = true;
|
|
|
|
while (MadeChangeThisIteration) {
|
2011-05-12 08:56:58 +08:00
|
|
|
MadeChangeThisIteration = TailMergeBlocks(MF);
|
2016-06-09 23:24:29 +08:00
|
|
|
// No need to clean up if tail merging does not change anything after the
|
|
|
|
// block placement.
|
|
|
|
if (!AfterBlockPlacement || MadeChangeThisIteration)
|
|
|
|
MadeChangeThisIteration |= OptimizeBranches(MF);
|
2011-05-12 08:56:58 +08:00
|
|
|
if (EnableHoistCommonCode)
|
|
|
|
MadeChangeThisIteration |= HoistCommonCode(MF);
|
2009-09-04 15:47:40 +08:00
|
|
|
MadeChange |= MadeChangeThisIteration;
|
2006-10-21 08:47:49 +08:00
|
|
|
}
|
|
|
|
|
2010-03-20 03:05:41 +08:00
|
|
|
// See if any jump tables have become dead as the code generator
|
2006-10-29 02:34:47 +08:00
|
|
|
// did its thing.
|
|
|
|
MachineJumpTableInfo *JTI = MF.getJumpTableInfo();
|
2016-07-13 02:44:33 +08:00
|
|
|
if (!JTI)
|
2010-01-26 07:26:13 +08:00
|
|
|
return MadeChange;
|
2012-02-09 05:22:43 +08:00
|
|
|
|
2010-03-20 03:05:41 +08:00
|
|
|
// Walk the function to find jump tables that are live.
|
|
|
|
BitVector JTIsLive(JTI->getJumpTables().size());
|
2015-09-16 11:52:35 +08:00
|
|
|
for (const MachineBasicBlock &BB : MF) {
|
|
|
|
for (const MachineInstr &I : BB)
|
|
|
|
for (const MachineOperand &Op : I.operands()) {
|
2010-01-26 07:26:13 +08:00
|
|
|
if (!Op.isJTI()) continue;
|
|
|
|
|
|
|
|
// Remember that this JT is live.
|
2010-03-20 03:05:41 +08:00
|
|
|
JTIsLive.set(Op.getIndex());
|
2006-10-29 02:34:47 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2010-03-20 03:05:41 +08:00
|
|
|
// Finally, remove dead jump tables. This happens when the
|
|
|
|
// indirect jump was unreachable (and thus deleted).
|
2010-01-26 07:26:13 +08:00
|
|
|
for (unsigned i = 0, e = JTIsLive.size(); i != e; ++i)
|
|
|
|
if (!JTIsLive.test(i)) {
|
|
|
|
JTI->RemoveJumpTable(i);
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
return MadeChange;
|
2006-10-21 08:47:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Tail Merging of Blocks
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-06-20 14:21:48 +08:00
|
|
|
/// HashMachineInstr - Compute a hash value for MI and its operands.
|
2016-02-28 03:48:01 +08:00
|
|
|
static unsigned HashMachineInstr(const MachineInstr &MI) {
|
|
|
|
unsigned Hash = MI.getOpcode();
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &Op = MI.getOperand(i);
|
2015-06-20 14:21:48 +08:00
|
|
|
|
2015-06-23 22:47:36 +08:00
|
|
|
// Merge in bits from the operand if easy. We can't use MachineOperand's
|
|
|
|
// hash_code here because it's not deterministic and we sort by hash value
|
|
|
|
// later.
|
2015-06-20 14:21:48 +08:00
|
|
|
unsigned OperandHash = 0;
|
|
|
|
switch (Op.getType()) {
|
2015-06-20 14:22:04 +08:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
OperandHash = Op.getReg();
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
OperandHash = Op.getImm();
|
|
|
|
break;
|
2015-06-20 14:21:48 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
OperandHash = Op.getMBB()->getNumber();
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_FrameIndex:
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
|
|
|
OperandHash = Op.getIndex();
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
// Global address / external symbol are too hard, don't bother, but do
|
|
|
|
// pull in the offset.
|
|
|
|
OperandHash = Op.getOffset();
|
|
|
|
break;
|
2015-06-20 14:22:04 +08:00
|
|
|
default:
|
|
|
|
break;
|
2015-06-20 14:21:48 +08:00
|
|
|
}
|
|
|
|
|
2015-06-20 14:22:04 +08:00
|
|
|
Hash += ((OperandHash << 3) | Op.getType()) << (i & 31);
|
2015-06-20 14:21:48 +08:00
|
|
|
}
|
|
|
|
return Hash;
|
|
|
|
}
|
|
|
|
|
2010-05-03 22:35:47 +08:00
|
|
|
/// HashEndOfMBB - Hash the last instruction in the MBB.
|
2016-02-28 03:48:01 +08:00
|
|
|
static unsigned HashEndOfMBB(const MachineBasicBlock &MBB) {
|
|
|
|
MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr();
|
|
|
|
if (I == MBB.end())
|
2015-06-23 22:47:29 +08:00
|
|
|
return 0;
|
2015-06-20 14:21:48 +08:00
|
|
|
|
2016-02-28 03:48:01 +08:00
|
|
|
return HashMachineInstr(*I);
|
2006-10-21 08:47:49 +08:00
|
|
|
}
|
|
|
|
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
/// Whether MI should be counted as an instruction when calculating common tail.
|
2018-04-24 18:32:08 +08:00
|
|
|
static bool countsAsInstruction(const MachineInstr &MI) {
|
2018-11-30 16:07:29 +08:00
|
|
|
return !(MI.isDebugInstr() || MI.isCFIInstruction());
|
2018-04-24 18:32:08 +08:00
|
|
|
}
|
|
|
|
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
/// Iterate backwards from the given iterator \p I, towards the beginning of the
|
|
|
|
/// block. If a MI satisfying 'countsAsInstruction' is found, return an iterator
|
|
|
|
/// pointing to that MI. If no such MI is found, return the end iterator.
|
|
|
|
static MachineBasicBlock::iterator
|
|
|
|
skipBackwardPastNonInstructions(MachineBasicBlock::iterator I,
|
|
|
|
MachineBasicBlock *MBB) {
|
|
|
|
while (I != MBB->begin()) {
|
|
|
|
--I;
|
|
|
|
if (countsAsInstruction(*I))
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
return MBB->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given two machine basic blocks, return the number of instructions they
|
|
|
|
/// actually have in common together at their end. If a common tail is found (at
|
|
|
|
/// least by one instruction), then iterators for the first shared instruction
|
|
|
|
/// in each block are returned as well.
|
|
|
|
///
|
|
|
|
/// Non-instructions according to countsAsInstruction are ignored.
|
2006-10-21 08:47:49 +08:00
|
|
|
static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1,
|
|
|
|
MachineBasicBlock *MBB2,
|
|
|
|
MachineBasicBlock::iterator &I1,
|
|
|
|
MachineBasicBlock::iterator &I2) {
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
MachineBasicBlock::iterator MBBI1 = MBB1->end();
|
|
|
|
MachineBasicBlock::iterator MBBI2 = MBB2->end();
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-10-21 08:47:49 +08:00
|
|
|
unsigned TailLen = 0;
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
while (true) {
|
|
|
|
MBBI1 = skipBackwardPastNonInstructions(MBBI1, MBB1);
|
|
|
|
MBBI2 = skipBackwardPastNonInstructions(MBBI2, MBB2);
|
|
|
|
if (MBBI1 == MBB1->end() || MBBI2 == MBB2->end())
|
|
|
|
break;
|
|
|
|
if (!MBBI1->isIdenticalTo(*MBBI2) ||
|
2007-10-26 03:49:32 +08:00
|
|
|
// FIXME: This check is dubious. It's used to get around a problem where
|
2007-10-26 02:23:45 +08:00
|
|
|
// people incorrectly expect inline asm directives to remain in the same
|
|
|
|
// relative order. This is untenable because normal compiler
|
|
|
|
// optimizations (like this one) may reorder and/or merge these
|
|
|
|
// directives.
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
MBBI1->isInlineAsm()) {
|
2006-10-21 08:47:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++TailLen;
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
I1 = MBBI1;
|
|
|
|
I2 = MBBI2;
|
2018-04-24 18:32:08 +08:00
|
|
|
}
|
|
|
|
|
2006-10-21 08:47:49 +08:00
|
|
|
return TailLen;
|
|
|
|
}
|
|
|
|
|
2017-09-07 04:45:24 +08:00
|
|
|
void BranchFolder::replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
|
|
|
|
MachineBasicBlock &NewDest) {
|
2016-12-17 07:55:37 +08:00
|
|
|
if (UpdateLiveIns) {
|
2017-09-07 04:45:24 +08:00
|
|
|
// OldInst should always point to an instruction.
|
|
|
|
MachineBasicBlock &OldMBB = *OldInst->getParent();
|
|
|
|
LiveRegs.clear();
|
|
|
|
LiveRegs.addLiveOuts(OldMBB);
|
|
|
|
// Move backward to the place where will insert the jump.
|
|
|
|
MachineBasicBlock::iterator I = OldMBB.end();
|
|
|
|
do {
|
|
|
|
--I;
|
|
|
|
LiveRegs.stepBackward(*I);
|
|
|
|
} while (I != OldInst);
|
|
|
|
|
|
|
|
// Merging the tails may have switched some undef operand to non-undef ones.
|
|
|
|
// Add IMPLICIT_DEFS into OldMBB as necessary to have a definition of the
|
|
|
|
// register.
|
|
|
|
for (MachineBasicBlock::RegisterMaskPair P : NewDest.liveins()) {
|
|
|
|
// We computed the liveins with computeLiveIn earlier and should only see
|
|
|
|
// full registers:
|
|
|
|
assert(P.LaneMask == LaneBitmask::getAll() &&
|
|
|
|
"Can only handle full register.");
|
|
|
|
MCPhysReg Reg = P.PhysReg;
|
|
|
|
if (!LiveRegs.available(*MRI, Reg))
|
|
|
|
continue;
|
|
|
|
DebugLoc DL;
|
|
|
|
BuildMI(OldMBB, OldInst, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Reg);
|
|
|
|
}
|
2016-12-17 07:55:37 +08:00
|
|
|
}
|
2011-07-07 07:41:48 +08:00
|
|
|
|
2017-09-07 04:45:24 +08:00
|
|
|
TII->ReplaceTailWithBranchTo(OldInst, &NewDest);
|
2006-10-21 08:47:49 +08:00
|
|
|
++NumTailMerge;
|
|
|
|
}
|
|
|
|
|
2006-11-01 09:16:12 +08:00
|
|
|
MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB,
|
2013-06-24 09:55:01 +08:00
|
|
|
MachineBasicBlock::iterator BBI1,
|
|
|
|
const BasicBlock *BB) {
|
2010-06-22 09:18:16 +08:00
|
|
|
if (!TII->isLegalToSplitMBBAt(CurMBB, BBI1))
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2010-06-22 09:18:16 +08:00
|
|
|
|
2008-07-08 07:14:23 +08:00
|
|
|
MachineFunction &MF = *CurMBB.getParent();
|
|
|
|
|
2006-11-01 09:16:12 +08:00
|
|
|
// Create the fall-through block.
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineFunction::iterator MBBI = CurMBB.getIterator();
|
2017-06-07 06:22:41 +08:00
|
|
|
MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(BB);
|
2008-07-08 07:14:23 +08:00
|
|
|
CurMBB.getParent()->insert(++MBBI, NewMBB);
|
2006-11-01 09:16:12 +08:00
|
|
|
|
|
|
|
// Move all the successors of this block to the specified block.
|
2008-06-20 01:22:29 +08:00
|
|
|
NewMBB->transferSuccessors(&CurMBB);
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-11-01 09:16:12 +08:00
|
|
|
// Add an edge from CurMBB to NewMBB for the fall-through.
|
|
|
|
CurMBB.addSuccessor(NewMBB);
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-11-01 09:16:12 +08:00
|
|
|
// Splice the code over.
|
|
|
|
NewMBB->splice(NewMBB->end(), &CurMBB, BBI1, CurMBB.end());
|
2007-03-21 05:35:06 +08:00
|
|
|
|
2016-06-09 23:24:29 +08:00
|
|
|
// NewMBB belongs to the same loop as CurMBB.
|
2017-02-21 08:12:38 +08:00
|
|
|
if (MLI)
|
2016-06-09 23:24:29 +08:00
|
|
|
if (MachineLoop *ML = MLI->getLoopFor(&CurMBB))
|
|
|
|
ML->addBasicBlockToLoop(NewMBB, MLI->getBase());
|
|
|
|
|
2014-08-08 03:30:13 +08:00
|
|
|
// NewMBB inherits CurMBB's block frequency.
|
|
|
|
MBBFreqInfo.setBlockFreq(NewMBB, MBBFreqInfo.getBlockFreq(&CurMBB));
|
|
|
|
|
2016-12-17 07:55:37 +08:00
|
|
|
if (UpdateLiveIns)
|
2017-09-07 04:45:24 +08:00
|
|
|
computeAndAddLiveIns(LiveRegs, *NewMBB);
|
2007-03-21 05:35:06 +08:00
|
|
|
|
2018-06-01 08:03:21 +08:00
|
|
|
// Add the new block to the EH scope.
|
|
|
|
const auto &EHScopeI = EHScopeMembership.find(&CurMBB);
|
|
|
|
if (EHScopeI != EHScopeMembership.end()) {
|
|
|
|
auto n = EHScopeI->second;
|
|
|
|
EHScopeMembership[NewMBB] = n;
|
2016-03-31 23:42:01 +08:00
|
|
|
}
|
2015-10-04 10:22:52 +08:00
|
|
|
|
2006-11-01 09:16:12 +08:00
|
|
|
return NewMBB;
|
|
|
|
}
|
|
|
|
|
2006-11-02 03:36:29 +08:00
|
|
|
/// EstimateRuntime - Make a rough estimate for how long it will take to run
|
|
|
|
/// the specified code.
|
|
|
|
static unsigned EstimateRuntime(MachineBasicBlock::iterator I,
|
2008-01-07 09:56:04 +08:00
|
|
|
MachineBasicBlock::iterator E) {
|
2006-11-02 03:36:29 +08:00
|
|
|
unsigned Time = 0;
|
|
|
|
for (; I != E; ++I) {
|
2018-04-24 18:32:08 +08:00
|
|
|
if (!countsAsInstruction(*I))
|
2010-03-05 08:02:59 +08:00
|
|
|
continue;
|
2011-12-07 15:15:52 +08:00
|
|
|
if (I->isCall())
|
2006-11-02 03:36:29 +08:00
|
|
|
Time += 10;
|
2019-12-19 19:19:36 +08:00
|
|
|
else if (I->mayLoadOrStore())
|
2006-11-02 03:36:29 +08:00
|
|
|
Time += 2;
|
|
|
|
else
|
|
|
|
++Time;
|
|
|
|
}
|
|
|
|
return Time;
|
|
|
|
}
|
|
|
|
|
2007-05-10 09:01:49 +08:00
|
|
|
// CurMBB needs to add an unconditional branch to SuccMBB (we removed these
|
|
|
|
// branches temporarily for tail merging). In the case where CurMBB ends
|
|
|
|
// with a conditional branch to the next block, optimize by reversing the
|
|
|
|
// test and conditionally branching to SuccMBB instead.
|
2009-11-17 02:08:46 +08:00
|
|
|
static void FixTail(MachineBasicBlock *CurMBB, MachineBasicBlock *SuccBB,
|
2007-05-10 09:01:49 +08:00
|
|
|
const TargetInstrInfo *TII) {
|
|
|
|
MachineFunction *MF = CurMBB->getParent();
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineFunction::iterator I = std::next(MachineFunction::iterator(CurMBB));
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2008-08-15 06:49:33 +08:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2017-02-21 08:12:38 +08:00
|
|
|
DebugLoc dl = CurMBB->findBranchDebugLoc();
|
2016-07-15 22:41:04 +08:00
|
|
|
if (I != MF->end() && !TII->analyzeBranch(*CurMBB, TBB, FBB, Cond, true)) {
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineBasicBlock *NextBB = &*I;
|
2008-05-10 07:28:24 +08:00
|
|
|
if (TBB == NextBB && !Cond.empty() && !FBB) {
|
2016-09-15 04:43:16 +08:00
|
|
|
if (!TII->reverseBranchCondition(Cond)) {
|
|
|
|
TII->removeBranch(*CurMBB);
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(*CurMBB, SuccBB, nullptr, Cond, dl);
|
2007-05-10 09:01:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(*CurMBB, SuccBB, nullptr,
|
2010-06-18 06:43:56 +08:00
|
|
|
SmallVector<MachineOperand, 0>(), dl);
|
2007-05-10 09:01:49 +08:00
|
|
|
}
|
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
bool
|
|
|
|
BranchFolder::MergePotentialsElt::operator<(const MergePotentialsElt &o) const {
|
|
|
|
if (getHash() < o.getHash())
|
|
|
|
return true;
|
2012-10-08 04:31:05 +08:00
|
|
|
if (getHash() > o.getHash())
|
2009-11-12 05:57:02 +08:00
|
|
|
return false;
|
2012-10-08 04:31:05 +08:00
|
|
|
if (getBlock()->getNumber() < o.getBlock()->getNumber())
|
2009-11-12 05:57:02 +08:00
|
|
|
return true;
|
2012-10-08 04:31:05 +08:00
|
|
|
if (getBlock()->getNumber() > o.getBlock()->getNumber())
|
2009-11-12 05:57:02 +08:00
|
|
|
return false;
|
2012-10-08 04:31:05 +08:00
|
|
|
// _GLIBCXX_DEBUG checks strict weak ordering, which involves comparing
|
|
|
|
// an object with itself.
|
2007-07-11 16:47:55 +08:00
|
|
|
#ifndef _GLIBCXX_DEBUG
|
2012-10-08 04:31:05 +08:00
|
|
|
llvm_unreachable("Predecessor appears twice");
|
2012-01-21 05:51:11 +08:00
|
|
|
#else
|
2012-10-08 04:31:05 +08:00
|
|
|
return false;
|
2012-01-21 05:51:11 +08:00
|
|
|
#endif
|
2007-05-30 07:47:50 +08:00
|
|
|
}
|
|
|
|
|
2009-11-12 03:48:59 +08:00
|
|
|
/// CountTerminators - Count the number of terminators in the given
|
|
|
|
/// block and set I to the position of the first non-terminator, if there
|
|
|
|
/// is one, or MBB->end() otherwise.
|
|
|
|
static unsigned CountTerminators(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator &I) {
|
|
|
|
I = MBB->end();
|
|
|
|
unsigned NumTerms = 0;
|
2017-06-07 06:22:41 +08:00
|
|
|
while (true) {
|
2009-11-12 03:48:59 +08:00
|
|
|
if (I == MBB->begin()) {
|
|
|
|
I = MBB->end();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--I;
|
2011-12-07 15:15:52 +08:00
|
|
|
if (!I->isTerminator()) break;
|
2009-11-12 03:48:59 +08:00
|
|
|
++NumTerms;
|
|
|
|
}
|
|
|
|
return NumTerms;
|
|
|
|
}
|
|
|
|
|
2017-02-15 05:02:24 +08:00
|
|
|
/// A no successor, non-return block probably ends in unreachable and is cold.
|
|
|
|
/// Also consider a block that ends in an indirect branch to be a return block,
|
|
|
|
/// since many targets use plain indirect branches to return.
|
|
|
|
static bool blockEndsInUnreachable(const MachineBasicBlock *MBB) {
|
|
|
|
if (!MBB->succ_empty())
|
|
|
|
return false;
|
|
|
|
if (MBB->empty())
|
|
|
|
return true;
|
|
|
|
return !(MBB->back().isReturn() || MBB->back().isIndirectBranch());
|
|
|
|
}
|
|
|
|
|
2009-10-30 02:40:06 +08:00
|
|
|
/// ProfitableToMerge - Check if two machine basic blocks have a common tail
|
|
|
|
/// and decide if it would be profitable to merge those tails. Return the
|
|
|
|
/// length of the common tail and iterators to the first common instruction
|
|
|
|
/// in each block.
|
2016-08-19 02:57:29 +08:00
|
|
|
/// MBB1, MBB2 The blocks to check
|
|
|
|
/// MinCommonTailLength Minimum size of tail block to be merged.
|
|
|
|
/// CommonTailLen Out parameter to record the size of the shared tail between
|
|
|
|
/// MBB1 and MBB2
|
|
|
|
/// I1, I2 Iterator references that will be changed to point to the first
|
|
|
|
/// instruction in the common tail shared by MBB1,MBB2
|
|
|
|
/// SuccBB A common successor of MBB1, MBB2 which are in a canonical form
|
|
|
|
/// relative to SuccBB
|
|
|
|
/// PredBB The layout predecessor of SuccBB, if any.
|
2018-06-01 08:03:21 +08:00
|
|
|
/// EHScopeMembership map from block to EH scope #.
|
2016-08-19 02:57:29 +08:00
|
|
|
/// AfterPlacement True if we are merging blocks after layout. Stricter
|
|
|
|
/// thresholds apply to prevent undoing tail-duplication.
|
2015-10-04 10:22:52 +08:00
|
|
|
static bool
|
|
|
|
ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2,
|
2016-08-19 02:57:29 +08:00
|
|
|
unsigned MinCommonTailLength, unsigned &CommonTailLen,
|
2015-10-04 10:22:52 +08:00
|
|
|
MachineBasicBlock::iterator &I1,
|
|
|
|
MachineBasicBlock::iterator &I2, MachineBasicBlock *SuccBB,
|
|
|
|
MachineBasicBlock *PredBB,
|
2018-06-01 08:03:21 +08:00
|
|
|
DenseMap<const MachineBasicBlock *, int> &EHScopeMembership,
|
2019-12-06 01:39:37 +08:00
|
|
|
bool AfterPlacement,
|
2020-01-28 02:05:54 +08:00
|
|
|
MBFIWrapper &MBBFreqInfo,
|
2019-12-06 01:39:37 +08:00
|
|
|
ProfileSummaryInfo *PSI) {
|
2018-06-01 08:03:21 +08:00
|
|
|
// It is never profitable to tail-merge blocks from two different EH scopes.
|
|
|
|
if (!EHScopeMembership.empty()) {
|
|
|
|
auto EHScope1 = EHScopeMembership.find(MBB1);
|
|
|
|
assert(EHScope1 != EHScopeMembership.end());
|
|
|
|
auto EHScope2 = EHScopeMembership.find(MBB2);
|
|
|
|
assert(EHScope2 != EHScopeMembership.end());
|
|
|
|
if (EHScope1->second != EHScope2->second)
|
2015-10-04 10:22:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-30 02:40:06 +08:00
|
|
|
CommonTailLen = ComputeCommonTailLength(MBB1, MBB2, I1, I2);
|
|
|
|
if (CommonTailLen == 0)
|
|
|
|
return false;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Common tail length of " << printMBBReference(*MBB1)
|
|
|
|
<< " and " << printMBBReference(*MBB2) << " is "
|
|
|
|
<< CommonTailLen << '\n');
|
2009-10-30 02:40:06 +08:00
|
|
|
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
// Move the iterators to the beginning of the MBB if we only got debug
|
|
|
|
// instructions before the tail. This is to avoid splitting a block when we
|
|
|
|
// only got debug instructions before the tail (to be invariant on -g).
|
|
|
|
if (skipDebugInstructionsForward(MBB1->begin(), MBB1->end()) == I1)
|
|
|
|
I1 = MBB1->begin();
|
|
|
|
if (skipDebugInstructionsForward(MBB2->begin(), MBB2->end()) == I2)
|
|
|
|
I2 = MBB2->begin();
|
|
|
|
|
|
|
|
bool FullBlockTail1 = I1 == MBB1->begin();
|
|
|
|
bool FullBlockTail2 = I2 == MBB2->begin();
|
|
|
|
|
2009-11-12 03:48:59 +08:00
|
|
|
// It's almost always profitable to merge any number of non-terminator
|
|
|
|
// instructions with the block that falls through into the common successor.
|
2016-08-11 02:36:18 +08:00
|
|
|
// This is true only for a single successor. For multiple successors, we are
|
|
|
|
// trading a conditional branch for an unconditional one.
|
|
|
|
// TODO: Re-visit successor size for non-layout tail merging.
|
|
|
|
if ((MBB1 == PredBB || MBB2 == PredBB) &&
|
|
|
|
(!AfterPlacement || MBB1->succ_size() == 1)) {
|
2009-11-12 03:48:59 +08:00
|
|
|
MachineBasicBlock::iterator I;
|
|
|
|
unsigned NumTerms = CountTerminators(MBB1 == PredBB ? MBB2 : MBB1, I);
|
|
|
|
if (CommonTailLen > NumTerms)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-15 05:02:24 +08:00
|
|
|
// If these are identical non-return blocks with no successors, merge them.
|
|
|
|
// Such blocks are typically cold calls to noreturn functions like abort, and
|
|
|
|
// are unlikely to become a fallthrough target after machine block placement.
|
|
|
|
// Tail merging these blocks is unlikely to create additional unconditional
|
|
|
|
// branches, and will reduce the size of this cold code.
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
if (FullBlockTail1 && FullBlockTail2 &&
|
2017-02-15 05:02:24 +08:00
|
|
|
blockEndsInUnreachable(MBB1) && blockEndsInUnreachable(MBB2))
|
|
|
|
return true;
|
|
|
|
|
2009-11-12 08:39:10 +08:00
|
|
|
// If one of the blocks can be completely merged and happens to be in
|
|
|
|
// a position where the other could fall through into it, merge any number
|
|
|
|
// of instructions, because it can be done without a branch.
|
|
|
|
// TODO: If the blocks are not adjacent, move one of them so that they are?
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
if (MBB1->isLayoutSuccessor(MBB2) && FullBlockTail2)
|
2009-11-12 08:39:10 +08:00
|
|
|
return true;
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
if (MBB2->isLayoutSuccessor(MBB1) && FullBlockTail1)
|
2009-11-12 08:39:10 +08:00
|
|
|
return true;
|
|
|
|
|
2017-04-11 06:28:12 +08:00
|
|
|
// If both blocks are identical and end in a branch, merge them unless they
|
|
|
|
// both have a fallthrough predecessor and successor.
|
|
|
|
// We can only do this after block placement because it depends on whether
|
|
|
|
// there are fallthroughs, and we don't know until after layout.
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
if (AfterPlacement && FullBlockTail1 && FullBlockTail2) {
|
2017-04-11 06:28:12 +08:00
|
|
|
auto BothFallThrough = [](MachineBasicBlock *MBB) {
|
|
|
|
if (MBB->succ_size() != 0 && !MBB->canFallThrough())
|
|
|
|
return false;
|
|
|
|
MachineFunction::iterator I(MBB);
|
|
|
|
MachineFunction *MF = MBB->getParent();
|
|
|
|
return (MBB != &*MF->begin()) && std::prev(I)->canFallThrough();
|
|
|
|
};
|
|
|
|
if (!BothFallThrough(MBB1) || !BothFallThrough(MBB2))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-12 03:48:59 +08:00
|
|
|
// If both blocks have an unconditional branch temporarily stripped out,
|
2009-11-14 05:02:15 +08:00
|
|
|
// count that as an additional common instruction for the following
|
2016-08-11 02:36:18 +08:00
|
|
|
// heuristics. This heuristic is only accurate for single-succ blocks, so to
|
|
|
|
// make sure that during layout merging and duplicating don't crash, we check
|
|
|
|
// for that when merging during layout.
|
2009-11-14 05:02:15 +08:00
|
|
|
unsigned EffectiveTailLen = CommonTailLen;
|
2009-11-17 01:56:13 +08:00
|
|
|
if (SuccBB && MBB1 != PredBB && MBB2 != PredBB &&
|
2016-08-11 02:36:18 +08:00
|
|
|
(MBB1->succ_size() == 1 || !AfterPlacement) &&
|
2011-12-07 15:15:52 +08:00
|
|
|
!MBB1->back().isBarrier() &&
|
|
|
|
!MBB2->back().isBarrier())
|
2009-11-14 05:02:15 +08:00
|
|
|
++EffectiveTailLen;
|
2009-11-12 03:48:59 +08:00
|
|
|
|
|
|
|
// Check if the common tail is long enough to be worthwhile.
|
2016-08-19 02:57:29 +08:00
|
|
|
if (EffectiveTailLen >= MinCommonTailLength)
|
2009-11-12 03:48:59 +08:00
|
|
|
return true;
|
|
|
|
|
2009-11-14 05:02:15 +08:00
|
|
|
// If we are optimizing for code size, 2 instructions in common is enough if
|
|
|
|
// we don't have to split a block. At worst we will be introducing 1 new
|
|
|
|
// branch instruction, which is likely to be smaller than the 2
|
|
|
|
// instructions that would be deleted in the merge.
|
2011-02-22 07:39:48 +08:00
|
|
|
MachineFunction *MF = MBB1->getParent();
|
2019-12-06 01:39:37 +08:00
|
|
|
bool OptForSize =
|
|
|
|
MF->getFunction().hasOptSize() ||
|
2020-01-30 01:36:31 +08:00
|
|
|
(llvm::shouldOptimizeForSize(MBB1, PSI, &MBBFreqInfo) &&
|
|
|
|
llvm::shouldOptimizeForSize(MBB2, PSI, &MBBFreqInfo));
|
2019-12-06 01:39:37 +08:00
|
|
|
return EffectiveTailLen >= 2 && OptForSize &&
|
[BranchFolding] Fix PR43964 about branch folder not being debug invariant
Summary:
The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.
Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.
This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.
The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
gives consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.
Reviewers: probinson, yechunliang, jmorse
Reviewed By: jmorse
Subscribers: Orlando, mehdi_amini, dexonsmith, aprantl, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70091
2019-11-12 01:20:40 +08:00
|
|
|
(FullBlockTail1 || FullBlockTail2);
|
2009-10-30 02:40:06 +08:00
|
|
|
}
|
|
|
|
|
2009-11-12 02:38:14 +08:00
|
|
|
unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
|
2016-08-19 02:57:29 +08:00
|
|
|
unsigned MinCommonTailLength,
|
2009-11-12 03:48:59 +08:00
|
|
|
MachineBasicBlock *SuccBB,
|
|
|
|
MachineBasicBlock *PredBB) {
|
2008-05-10 07:28:24 +08:00
|
|
|
unsigned maxCommonTailLength = 0U;
|
|
|
|
SameTails.clear();
|
|
|
|
MachineBasicBlock::iterator TrialBBI1, TrialBBI2;
|
2014-03-02 20:27:27 +08:00
|
|
|
MPIterator HighestMPIter = std::prev(MergePotentials.end());
|
|
|
|
for (MPIterator CurMPIter = std::prev(MergePotentials.end()),
|
2009-11-12 02:38:14 +08:00
|
|
|
B = MergePotentials.begin();
|
2014-03-02 20:27:27 +08:00
|
|
|
CurMPIter != B && CurMPIter->getHash() == CurHash; --CurMPIter) {
|
|
|
|
for (MPIterator I = std::prev(CurMPIter); I->getHash() == CurHash; --I) {
|
2009-10-30 02:40:06 +08:00
|
|
|
unsigned CommonTailLen;
|
2009-11-12 05:57:02 +08:00
|
|
|
if (ProfitableToMerge(CurMPIter->getBlock(), I->getBlock(),
|
2016-08-19 02:57:29 +08:00
|
|
|
MinCommonTailLength,
|
2009-11-12 03:48:59 +08:00
|
|
|
CommonTailLen, TrialBBI1, TrialBBI2,
|
2015-10-04 10:22:52 +08:00
|
|
|
SuccBB, PredBB,
|
2018-06-01 08:03:21 +08:00
|
|
|
EHScopeMembership,
|
2019-12-06 01:39:37 +08:00
|
|
|
AfterBlockPlacement, MBBFreqInfo, PSI)) {
|
2008-05-10 07:28:24 +08:00
|
|
|
if (CommonTailLen > maxCommonTailLength) {
|
|
|
|
SameTails.clear();
|
|
|
|
maxCommonTailLength = CommonTailLen;
|
|
|
|
HighestMPIter = CurMPIter;
|
2009-11-12 05:57:02 +08:00
|
|
|
SameTails.push_back(SameTailElt(CurMPIter, TrialBBI1));
|
2008-05-10 07:28:24 +08:00
|
|
|
}
|
|
|
|
if (HighestMPIter == CurMPIter &&
|
|
|
|
CommonTailLen == maxCommonTailLength)
|
2009-11-12 05:57:02 +08:00
|
|
|
SameTails.push_back(SameTailElt(I, TrialBBI2));
|
2008-05-10 07:28:24 +08:00
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
if (I == B)
|
2008-05-10 07:28:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxCommonTailLength;
|
|
|
|
}
|
|
|
|
|
2009-11-12 02:38:14 +08:00
|
|
|
void BranchFolder::RemoveBlocksWithHash(unsigned CurHash,
|
2009-11-17 02:08:46 +08:00
|
|
|
MachineBasicBlock *SuccBB,
|
|
|
|
MachineBasicBlock *PredBB) {
|
2008-05-24 01:19:02 +08:00
|
|
|
MPIterator CurMPIter, B;
|
2014-03-02 20:27:27 +08:00
|
|
|
for (CurMPIter = std::prev(MergePotentials.end()),
|
|
|
|
B = MergePotentials.begin();
|
|
|
|
CurMPIter->getHash() == CurHash; --CurMPIter) {
|
2008-05-10 07:28:24 +08:00
|
|
|
// Put the unconditional branch back, if we need one.
|
2009-11-12 05:57:02 +08:00
|
|
|
MachineBasicBlock *CurMBB = CurMPIter->getBlock();
|
2008-05-10 07:28:24 +08:00
|
|
|
if (SuccBB && CurMBB != PredBB)
|
|
|
|
FixTail(CurMBB, SuccBB, TII);
|
2009-11-12 02:38:14 +08:00
|
|
|
if (CurMPIter == B)
|
2008-05-10 07:28:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-11-12 05:57:02 +08:00
|
|
|
if (CurMPIter->getHash() != CurHash)
|
2008-05-24 01:19:02 +08:00
|
|
|
CurMPIter++;
|
|
|
|
MergePotentials.erase(CurMPIter, MergePotentials.end());
|
2008-05-10 07:28:24 +08:00
|
|
|
}
|
|
|
|
|
2010-06-22 09:18:16 +08:00
|
|
|
bool BranchFolder::CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
|
2013-06-24 09:55:01 +08:00
|
|
|
MachineBasicBlock *SuccBB,
|
2010-06-22 09:18:16 +08:00
|
|
|
unsigned maxCommonTailLength,
|
|
|
|
unsigned &commonTailIndex) {
|
|
|
|
commonTailIndex = 0;
|
2008-05-13 04:33:57 +08:00
|
|
|
unsigned TimeEstimate = ~0U;
|
2009-11-12 09:51:28 +08:00
|
|
|
for (unsigned i = 0, e = SameTails.size(); i != e; ++i) {
|
2008-05-13 04:33:57 +08:00
|
|
|
// Use PredBB if possible; that doesn't require a new branch.
|
2009-11-12 05:57:02 +08:00
|
|
|
if (SameTails[i].getBlock() == PredBB) {
|
2008-05-13 04:33:57 +08:00
|
|
|
commonTailIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Otherwise, make a (fairly bogus) choice based on estimate of
|
|
|
|
// how long it will take the various blocks to execute.
|
2009-11-12 05:57:02 +08:00
|
|
|
unsigned t = EstimateRuntime(SameTails[i].getBlock()->begin(),
|
|
|
|
SameTails[i].getTailStartPos());
|
2009-11-12 02:38:14 +08:00
|
|
|
if (t <= TimeEstimate) {
|
2008-05-13 04:33:57 +08:00
|
|
|
TimeEstimate = t;
|
|
|
|
commonTailIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
MachineBasicBlock::iterator BBI =
|
|
|
|
SameTails[commonTailIndex].getTailStartPos();
|
|
|
|
MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock();
|
2008-05-13 04:33:57 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nSplitting " << printMBBReference(*MBB) << ", size "
|
|
|
|
<< maxCommonTailLength);
|
2008-05-13 04:33:57 +08:00
|
|
|
|
2013-06-24 09:55:01 +08:00
|
|
|
// If the split block unconditionally falls-thru to SuccBB, it will be
|
|
|
|
// merged. In control flow terms it should then take SuccBB's name. e.g. If
|
|
|
|
// SuccBB is an inner loop, the common tail is still part of the inner loop.
|
|
|
|
const BasicBlock *BB = (SuccBB && MBB->succ_size() == 1) ?
|
|
|
|
SuccBB->getBasicBlock() : MBB->getBasicBlock();
|
|
|
|
MachineBasicBlock *newMBB = SplitMBBAt(*MBB, BBI, BB);
|
2010-06-22 09:18:16 +08:00
|
|
|
if (!newMBB) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "... failed!");
|
2010-06-22 09:18:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
SameTails[commonTailIndex].setBlock(newMBB);
|
|
|
|
SameTails[commonTailIndex].setTailStartPos(newMBB->begin());
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2008-05-13 04:33:57 +08:00
|
|
|
// If we split PredBB, newMBB is the new predecessor.
|
2009-11-12 02:38:14 +08:00
|
|
|
if (PredBB == MBB)
|
2008-05-13 04:33:57 +08:00
|
|
|
PredBB = newMBB;
|
|
|
|
|
2010-06-22 09:18:16 +08:00
|
|
|
return true;
|
2008-05-13 04:33:57 +08:00
|
|
|
}
|
|
|
|
|
2015-03-11 00:22:52 +08:00
|
|
|
static void
|
2016-09-20 09:14:42 +08:00
|
|
|
mergeOperations(MachineBasicBlock::iterator MBBIStartPos,
|
|
|
|
MachineBasicBlock &MBBCommon) {
|
2015-03-11 00:22:52 +08:00
|
|
|
MachineBasicBlock *MBB = MBBIStartPos->getParent();
|
|
|
|
// Note CommonTailLen does not necessarily matches the size of
|
|
|
|
// the common BB nor all its instructions because of debug
|
|
|
|
// instructions differences.
|
|
|
|
unsigned CommonTailLen = 0;
|
|
|
|
for (auto E = MBB->end(); MBBIStartPos != E; ++MBBIStartPos)
|
|
|
|
++CommonTailLen;
|
|
|
|
|
|
|
|
MachineBasicBlock::reverse_iterator MBBI = MBB->rbegin();
|
2015-03-11 04:29:59 +08:00
|
|
|
MachineBasicBlock::reverse_iterator MBBIE = MBB->rend();
|
2015-03-11 00:22:52 +08:00
|
|
|
MachineBasicBlock::reverse_iterator MBBICommon = MBBCommon.rbegin();
|
|
|
|
MachineBasicBlock::reverse_iterator MBBIECommon = MBBCommon.rend();
|
|
|
|
|
|
|
|
while (CommonTailLen--) {
|
2015-03-11 04:29:59 +08:00
|
|
|
assert(MBBI != MBBIE && "Reached BB end within common tail length!");
|
|
|
|
(void)MBBIE;
|
2015-03-11 00:22:52 +08:00
|
|
|
|
2018-04-24 18:32:08 +08:00
|
|
|
if (!countsAsInstruction(*MBBI)) {
|
2015-03-11 00:22:52 +08:00
|
|
|
++MBBI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:32:08 +08:00
|
|
|
while ((MBBICommon != MBBIECommon) && !countsAsInstruction(*MBBICommon))
|
2015-03-11 00:22:52 +08:00
|
|
|
++MBBICommon;
|
|
|
|
|
|
|
|
assert(MBBICommon != MBBIECommon &&
|
|
|
|
"Reached BB end within common tail length!");
|
2016-02-28 04:01:33 +08:00
|
|
|
assert(MBBICommon->isIdenticalTo(*MBBI) && "Expected matching MIIs!");
|
2015-03-11 00:22:52 +08:00
|
|
|
|
2016-09-20 09:14:42 +08:00
|
|
|
// Merge MMOs from memory operations in the common block.
|
2019-12-19 19:19:36 +08:00
|
|
|
if (MBBICommon->mayLoadOrStore())
|
2018-08-17 05:30:05 +08:00
|
|
|
MBBICommon->cloneMergedMemRefs(*MBB->getParent(), {&*MBBICommon, &*MBBI});
|
2016-09-20 09:14:42 +08:00
|
|
|
// Drop undef flags if they aren't present in all merged instructions.
|
|
|
|
for (unsigned I = 0, E = MBBICommon->getNumOperands(); I != E; ++I) {
|
|
|
|
MachineOperand &MO = MBBICommon->getOperand(I);
|
|
|
|
if (MO.isReg() && MO.isUndef()) {
|
|
|
|
const MachineOperand &OtherMO = MBBI->getOperand(I);
|
|
|
|
if (!OtherMO.isUndef())
|
|
|
|
MO.setIsUndef(false);
|
|
|
|
}
|
|
|
|
}
|
2015-03-11 00:22:52 +08:00
|
|
|
|
|
|
|
++MBBI;
|
|
|
|
++MBBICommon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 04:45:24 +08:00
|
|
|
void BranchFolder::mergeCommonTails(unsigned commonTailIndex) {
|
|
|
|
MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock();
|
|
|
|
|
|
|
|
std::vector<MachineBasicBlock::iterator> NextCommonInsts(SameTails.size());
|
|
|
|
for (unsigned int i = 0 ; i != SameTails.size() ; ++i) {
|
|
|
|
if (i != commonTailIndex) {
|
|
|
|
NextCommonInsts[i] = SameTails[i].getTailStartPos();
|
|
|
|
mergeOperations(SameTails[i].getTailStartPos(), *MBB);
|
|
|
|
} else {
|
|
|
|
assert(SameTails[i].getTailStartPos() == MBB->begin() &&
|
|
|
|
"MBB is not a common tail only block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &MI : *MBB) {
|
2018-04-24 18:32:08 +08:00
|
|
|
if (!countsAsInstruction(MI))
|
2017-09-07 04:45:24 +08:00
|
|
|
continue;
|
|
|
|
DebugLoc DL = MI.getDebugLoc();
|
|
|
|
for (unsigned int i = 0 ; i < NextCommonInsts.size() ; i++) {
|
|
|
|
if (i == commonTailIndex)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto &Pos = NextCommonInsts[i];
|
|
|
|
assert(Pos != SameTails[i].getBlock()->end() &&
|
|
|
|
"Reached BB end within common tail");
|
2018-04-24 18:32:08 +08:00
|
|
|
while (!countsAsInstruction(*Pos)) {
|
2017-09-07 04:45:24 +08:00
|
|
|
++Pos;
|
|
|
|
assert(Pos != SameTails[i].getBlock()->end() &&
|
|
|
|
"Reached BB end within common tail");
|
|
|
|
}
|
|
|
|
assert(MI.isIdenticalTo(*Pos) && "Expected matching MIIs!");
|
|
|
|
DL = DILocation::getMergedLocation(DL, Pos->getDebugLoc());
|
|
|
|
NextCommonInsts[i] = ++Pos;
|
|
|
|
}
|
|
|
|
MI.setDebugLoc(DL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UpdateLiveIns) {
|
|
|
|
LivePhysRegs NewLiveIns(*TRI);
|
|
|
|
computeLiveIns(NewLiveIns, *MBB);
|
2018-06-23 00:38:38 +08:00
|
|
|
LiveRegs.init(*TRI);
|
2017-09-07 04:45:24 +08:00
|
|
|
|
|
|
|
// The flag merging may lead to some register uses no longer using the
|
|
|
|
// <undef> flag, add IMPLICIT_DEFs in the predecessors as necessary.
|
|
|
|
for (MachineBasicBlock *Pred : MBB->predecessors()) {
|
2018-06-23 00:38:38 +08:00
|
|
|
LiveRegs.clear();
|
2017-09-07 04:45:24 +08:00
|
|
|
LiveRegs.addLiveOuts(*Pred);
|
|
|
|
MachineBasicBlock::iterator InsertBefore = Pred->getFirstTerminator();
|
|
|
|
for (unsigned Reg : NewLiveIns) {
|
|
|
|
if (!LiveRegs.available(*MRI, Reg))
|
|
|
|
continue;
|
|
|
|
DebugLoc DL;
|
|
|
|
BuildMI(*Pred, InsertBefore, DL, TII->get(TargetOpcode::IMPLICIT_DEF),
|
|
|
|
Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MBB->clearLiveIns();
|
|
|
|
addLiveIns(*MBB, NewLiveIns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 05:37:03 +08:00
|
|
|
// See if any of the blocks in MergePotentials (which all have SuccBB as a
|
|
|
|
// successor, or all have no successor if it is null) can be tail-merged.
|
|
|
|
// If there is a successor, any blocks in MergePotentials that are not
|
|
|
|
// tail-merged and are not immediately before Succ must have an unconditional
|
|
|
|
// branch to Succ added (but the predecessor/successor lists need no
|
|
|
|
// adjustment). The lone predecessor of Succ that falls through into Succ,
|
2007-05-08 04:57:21 +08:00
|
|
|
// if any, is given in PredBB.
|
2016-08-19 02:57:29 +08:00
|
|
|
// MinCommonTailLength - Except for the special cases below, tail-merge if
|
|
|
|
// there are at least this many instructions in common.
|
2009-11-12 03:48:59 +08:00
|
|
|
bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
|
2016-08-19 02:57:29 +08:00
|
|
|
MachineBasicBlock *PredBB,
|
|
|
|
unsigned MinCommonTailLength) {
|
2009-09-04 15:47:40 +08:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "\nTryTailMergeBlocks: ";
|
|
|
|
for (unsigned i = 0, e = MergePotentials.size(); i != e; ++i) dbgs()
|
|
|
|
<< printMBBReference(*MergePotentials[i].getBlock())
|
|
|
|
<< (i == e - 1 ? "" : ", ");
|
|
|
|
dbgs() << "\n"; if (SuccBB) {
|
|
|
|
dbgs() << " with successor " << printMBBReference(*SuccBB) << '\n';
|
|
|
|
if (PredBB)
|
|
|
|
dbgs() << " which has fall-through from "
|
|
|
|
<< printMBBReference(*PredBB) << "\n";
|
|
|
|
} dbgs() << "Looking for common tails of at least "
|
|
|
|
<< MinCommonTailLength << " instruction"
|
|
|
|
<< (MinCommonTailLength == 1 ? "" : "s") << '\n';);
|
2008-05-10 07:28:24 +08:00
|
|
|
|
2006-10-21 08:47:49 +08:00
|
|
|
// Sort by hash value so that blocks with identical end sequences sort
|
|
|
|
// together.
|
2015-03-14 05:17:02 +08:00
|
|
|
array_pod_sort(MergePotentials.begin(), MergePotentials.end());
|
2006-10-21 08:47:49 +08:00
|
|
|
|
|
|
|
// Walk through equivalence sets looking for actual exact matches.
|
|
|
|
while (MergePotentials.size() > 1) {
|
2009-11-12 05:57:02 +08:00
|
|
|
unsigned CurHash = MergePotentials.back().getHash();
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2008-05-10 07:28:24 +08:00
|
|
|
// Build SameTails, identifying the set of blocks with this hash code
|
|
|
|
// and with the maximum number of instructions in common.
|
2009-11-12 02:38:14 +08:00
|
|
|
unsigned maxCommonTailLength = ComputeSameTails(CurHash,
|
2016-08-19 02:57:29 +08:00
|
|
|
MinCommonTailLength,
|
2009-11-12 03:48:59 +08:00
|
|
|
SuccBB, PredBB);
|
2007-05-24 05:07:20 +08:00
|
|
|
|
2016-08-19 02:57:29 +08:00
|
|
|
// If we didn't find any pair that has at least MinCommonTailLength
|
2008-05-10 05:24:35 +08:00
|
|
|
// instructions in common, remove all blocks with this hash code and retry.
|
|
|
|
if (SameTails.empty()) {
|
2008-05-10 07:28:24 +08:00
|
|
|
RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
|
2007-05-24 05:07:20 +08:00
|
|
|
continue;
|
|
|
|
}
|
2006-11-01 09:16:12 +08:00
|
|
|
|
2008-05-10 05:24:35 +08:00
|
|
|
// If one of the blocks is the entire common tail (and not the entry
|
2008-05-13 04:33:57 +08:00
|
|
|
// block, which we can't jump to), we can treat all blocks with this same
|
|
|
|
// tail at once. Use PredBB if that is one of the possibilities, as that
|
|
|
|
// will not introduce any extra branches.
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineBasicBlock *EntryBB =
|
|
|
|
&MergePotentials.front().getBlock()->getParent()->front();
|
2009-11-12 05:57:02 +08:00
|
|
|
unsigned commonTailIndex = SameTails.size();
|
2009-11-12 08:39:10 +08:00
|
|
|
// If there are two blocks, check to see if one can be made to fall through
|
|
|
|
// into the other.
|
|
|
|
if (SameTails.size() == 2 &&
|
|
|
|
SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) &&
|
|
|
|
SameTails[1].tailIsWholeBlock())
|
|
|
|
commonTailIndex = 1;
|
|
|
|
else if (SameTails.size() == 2 &&
|
|
|
|
SameTails[1].getBlock()->isLayoutSuccessor(
|
|
|
|
SameTails[0].getBlock()) &&
|
|
|
|
SameTails[0].tailIsWholeBlock())
|
|
|
|
commonTailIndex = 0;
|
|
|
|
else {
|
|
|
|
// Otherwise just pick one, favoring the fall-through predecessor if
|
|
|
|
// there is one.
|
|
|
|
for (unsigned i = 0, e = SameTails.size(); i != e; ++i) {
|
|
|
|
MachineBasicBlock *MBB = SameTails[i].getBlock();
|
|
|
|
if (MBB == EntryBB && SameTails[i].tailIsWholeBlock())
|
|
|
|
continue;
|
|
|
|
if (MBB == PredBB) {
|
|
|
|
commonTailIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (SameTails[i].tailIsWholeBlock())
|
|
|
|
commonTailIndex = i;
|
2008-05-10 05:24:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-12 03:48:59 +08:00
|
|
|
if (commonTailIndex == SameTails.size() ||
|
2009-11-12 05:57:02 +08:00
|
|
|
(SameTails[commonTailIndex].getBlock() == PredBB &&
|
|
|
|
!SameTails[commonTailIndex].tailIsWholeBlock())) {
|
2008-05-13 04:33:57 +08:00
|
|
|
// None of the blocks consist entirely of the common tail.
|
|
|
|
// Split a block so that one does.
|
2013-06-24 09:55:01 +08:00
|
|
|
if (!CreateCommonTailOnlyBlock(PredBB, SuccBB,
|
2010-06-22 09:18:16 +08:00
|
|
|
maxCommonTailLength, commonTailIndex)) {
|
|
|
|
RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
|
|
|
|
continue;
|
|
|
|
}
|
2006-11-01 09:16:12 +08:00
|
|
|
}
|
2008-05-13 04:33:57 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock();
|
2014-08-08 03:30:13 +08:00
|
|
|
|
2016-03-11 05:54:25 +08:00
|
|
|
// Recompute common tail MBB's edge weights and block frequency.
|
2014-08-08 03:30:13 +08:00
|
|
|
setCommonTailEdgeWeights(*MBB);
|
|
|
|
|
2017-09-07 04:45:24 +08:00
|
|
|
// Merge debug locations, MMOs and undef flags across identical instructions
|
|
|
|
// for common tail.
|
|
|
|
mergeCommonTails(commonTailIndex);
|
2016-10-27 01:01:47 +08:00
|
|
|
|
2008-05-13 04:33:57 +08:00
|
|
|
// MBB is common tail. Adjust all other BB's to jump to this one.
|
|
|
|
// Traversal must be forwards so erases work.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nUsing common tail in " << printMBBReference(*MBB)
|
|
|
|
<< " for ");
|
2009-11-12 03:48:59 +08:00
|
|
|
for (unsigned int i=0, e = SameTails.size(); i != e; ++i) {
|
2009-11-12 02:38:14 +08:00
|
|
|
if (commonTailIndex == i)
|
2008-05-13 04:33:57 +08:00
|
|
|
continue;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*SameTails[i].getBlock())
|
|
|
|
<< (i == e - 1 ? "" : ", "));
|
2008-05-13 04:33:57 +08:00
|
|
|
// Hack the end off BB i, making it jump to BB commonTailIndex instead.
|
2017-09-07 04:45:24 +08:00
|
|
|
replaceTailWithBranchTo(SameTails[i].getTailStartPos(), *MBB);
|
2008-05-13 04:33:57 +08:00
|
|
|
// BB i is no longer a predecessor of SuccBB; remove it from the worklist.
|
2009-11-12 05:57:02 +08:00
|
|
|
MergePotentials.erase(SameTails[i].getMPIter());
|
2006-10-21 08:47:49 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n");
|
2008-05-13 04:33:57 +08:00
|
|
|
// We leave commonTailIndex in the worklist in case there are other blocks
|
|
|
|
// that match it with a smaller number of instructions.
|
2006-11-01 09:16:12 +08:00
|
|
|
MadeChange = true;
|
2004-07-31 18:01:27 +08:00
|
|
|
}
|
2006-10-21 08:47:49 +08:00
|
|
|
return MadeChange;
|
|
|
|
}
|
2004-07-31 18:01:27 +08:00
|
|
|
|
2007-05-08 04:57:21 +08:00
|
|
|
bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
|
2009-09-04 15:47:40 +08:00
|
|
|
bool MadeChange = false;
|
[Codegen] Merge tail blocks with no successors after block placement
Summary:
I found the following case having tail blocks with no successors merging opportunities after block placement.
Before block placement:
bb0:
...
bne a0, 0, bb2:
bb1:
mv a0, 1
ret
bb2:
...
bb3:
mv a0, 1
ret
bb4:
mv a0, -1
ret
The conditional branch bne in bb0 is opposite to beq.
After block placement:
bb0:
...
beq a0, 0, bb1
bb2:
...
bb4:
mv a0, -1
ret
bb1:
mv a0, 1
ret
bb3:
mv a0, 1
ret
After block placement, that appears new tail merging opportunity, bb1 and bb3 can be merged as one block. So the conditional constraint for merging tail blocks with no successors should be removed. In my experiment for RISC-V, it decreases code size.
Author of original patch: Jim Lin
Reviewers: haicheng, aheejin, craig.topper, rnk, RKSimon, Jim, dmgreen
Reviewed By: Jim, dmgreen
Subscribers: xbolva00, dschuff, javed.absar, sbc100, jgravelle-google, aheejin, kito-cheng, dmgreen, PkmX, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D54411
llvm-svn: 363284
2019-06-14 02:11:32 +08:00
|
|
|
if (!EnableTailMerge)
|
|
|
|
return MadeChange;
|
2007-05-10 09:01:49 +08:00
|
|
|
|
2007-05-08 04:57:21 +08:00
|
|
|
// First find blocks with no successors.
|
[Codegen] Merge tail blocks with no successors after block placement
Summary:
I found the following case having tail blocks with no successors merging opportunities after block placement.
Before block placement:
bb0:
...
bne a0, 0, bb2:
bb1:
mv a0, 1
ret
bb2:
...
bb3:
mv a0, 1
ret
bb4:
mv a0, -1
ret
The conditional branch bne in bb0 is opposite to beq.
After block placement:
bb0:
...
beq a0, 0, bb1
bb2:
...
bb4:
mv a0, -1
ret
bb1:
mv a0, 1
ret
bb3:
mv a0, 1
ret
After block placement, that appears new tail merging opportunity, bb1 and bb3 can be merged as one block. So the conditional constraint for merging tail blocks with no successors should be removed. In my experiment for RISC-V, it decreases code size.
Author of original patch: Jim Lin
Reviewers: haicheng, aheejin, craig.topper, rnk, RKSimon, Jim, dmgreen
Reviewed By: Jim, dmgreen
Subscribers: xbolva00, dschuff, javed.absar, sbc100, jgravelle-google, aheejin, kito-cheng, dmgreen, PkmX, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D54411
llvm-svn: 363284
2019-06-14 02:11:32 +08:00
|
|
|
// Block placement may create new tail merging opportunities for these blocks.
|
|
|
|
MergePotentials.clear();
|
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
2016-06-09 23:24:29 +08:00
|
|
|
if (MergePotentials.size() == TailMergeThreshold)
|
[Codegen] Merge tail blocks with no successors after block placement
Summary:
I found the following case having tail blocks with no successors merging opportunities after block placement.
Before block placement:
bb0:
...
bne a0, 0, bb2:
bb1:
mv a0, 1
ret
bb2:
...
bb3:
mv a0, 1
ret
bb4:
mv a0, -1
ret
The conditional branch bne in bb0 is opposite to beq.
After block placement:
bb0:
...
beq a0, 0, bb1
bb2:
...
bb4:
mv a0, -1
ret
bb1:
mv a0, 1
ret
bb3:
mv a0, 1
ret
After block placement, that appears new tail merging opportunity, bb1 and bb3 can be merged as one block. So the conditional constraint for merging tail blocks with no successors should be removed. In my experiment for RISC-V, it decreases code size.
Author of original patch: Jim Lin
Reviewers: haicheng, aheejin, craig.topper, rnk, RKSimon, Jim, dmgreen
Reviewed By: Jim, dmgreen
Subscribers: xbolva00, dschuff, javed.absar, sbc100, jgravelle-google, aheejin, kito-cheng, dmgreen, PkmX, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D54411
llvm-svn: 363284
2019-06-14 02:11:32 +08:00
|
|
|
break;
|
|
|
|
if (!TriedMerging.count(&MBB) && MBB.succ_empty())
|
|
|
|
MergePotentials.push_back(MergePotentialsElt(HashEndOfMBB(MBB), &MBB));
|
2016-06-09 23:24:29 +08:00
|
|
|
}
|
2016-06-07 23:17:21 +08:00
|
|
|
|
[Codegen] Merge tail blocks with no successors after block placement
Summary:
I found the following case having tail blocks with no successors merging opportunities after block placement.
Before block placement:
bb0:
...
bne a0, 0, bb2:
bb1:
mv a0, 1
ret
bb2:
...
bb3:
mv a0, 1
ret
bb4:
mv a0, -1
ret
The conditional branch bne in bb0 is opposite to beq.
After block placement:
bb0:
...
beq a0, 0, bb1
bb2:
...
bb4:
mv a0, -1
ret
bb1:
mv a0, 1
ret
bb3:
mv a0, 1
ret
After block placement, that appears new tail merging opportunity, bb1 and bb3 can be merged as one block. So the conditional constraint for merging tail blocks with no successors should be removed. In my experiment for RISC-V, it decreases code size.
Author of original patch: Jim Lin
Reviewers: haicheng, aheejin, craig.topper, rnk, RKSimon, Jim, dmgreen
Reviewed By: Jim, dmgreen
Subscribers: xbolva00, dschuff, javed.absar, sbc100, jgravelle-google, aheejin, kito-cheng, dmgreen, PkmX, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D54411
llvm-svn: 363284
2019-06-14 02:11:32 +08:00
|
|
|
// If this is a large problem, avoid visiting the same basic blocks
|
|
|
|
// multiple times.
|
|
|
|
if (MergePotentials.size() == TailMergeThreshold)
|
|
|
|
for (unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
|
|
|
|
TriedMerging.insert(MergePotentials[i].getBlock());
|
|
|
|
|
|
|
|
// See if we can do any tail merging on those.
|
|
|
|
if (MergePotentials.size() >= 2)
|
|
|
|
MadeChange |= TryTailMergeBlocks(nullptr, nullptr, MinCommonTailLength);
|
|
|
|
|
2007-05-10 09:01:49 +08:00
|
|
|
// Look at blocks (IBB) with multiple predecessors (PBB).
|
|
|
|
// We change each predecessor to a canonical form, by
|
|
|
|
// (1) temporarily removing any unconditional branch from the predecessor
|
|
|
|
// to IBB, and
|
|
|
|
// (2) alter conditional branches so they branch to the other block
|
2009-11-12 02:38:14 +08:00
|
|
|
// not IBB; this may require adding back an unconditional branch to IBB
|
2007-05-10 09:01:49 +08:00
|
|
|
// later, where there wasn't one coming in. E.g.
|
|
|
|
// Bcc IBB
|
|
|
|
// fallthrough to QBB
|
|
|
|
// here becomes
|
|
|
|
// Bncc QBB
|
|
|
|
// with a conceptual B to IBB after that, which never actually exists.
|
|
|
|
// With those changes, we see whether the predecessors' tails match,
|
|
|
|
// and merge them if so. We change things out of canonical form and
|
|
|
|
// back to the way they were later in the process. (OptimizeBranches
|
|
|
|
// would undo some of this, but we can't use it, because we'd get into
|
|
|
|
// a compile-time infinite loop repeatedly doing and undoing the same
|
|
|
|
// transformations.)
|
2007-05-08 04:57:21 +08:00
|
|
|
|
2014-03-02 20:27:27 +08:00
|
|
|
for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
|
2009-11-12 03:48:59 +08:00
|
|
|
I != E; ++I) {
|
2012-05-24 06:12:50 +08:00
|
|
|
if (I->pred_size() < 2) continue;
|
2012-05-24 06:09:50 +08:00
|
|
|
SmallPtrSet<MachineBasicBlock *, 8> UniquePreds;
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineBasicBlock *IBB = &*I;
|
|
|
|
MachineBasicBlock *PredBB = &*std::prev(I);
|
2012-05-24 06:09:50 +08:00
|
|
|
MergePotentials.clear();
|
2016-08-13 07:13:38 +08:00
|
|
|
MachineLoop *ML;
|
|
|
|
|
|
|
|
// Bail if merging after placement and IBB is the loop header because
|
|
|
|
// -- If merging predecessors that belong to the same loop as IBB, the
|
|
|
|
// common tail of merged predecessors may become the loop top if block
|
|
|
|
// placement is called again and the predecessors may branch to this common
|
|
|
|
// tail and require more branches. This can be relaxed if
|
|
|
|
// MachineBlockPlacement::findBestLoopTop is more flexible.
|
|
|
|
// --If merging predecessors that do not belong to the same loop as IBB, the
|
|
|
|
// loop info of IBB's loop and the other loops may be affected. Calling the
|
|
|
|
// block placement again may make big change to the layout and eliminate the
|
|
|
|
// reason to do tail merging here.
|
|
|
|
if (AfterBlockPlacement && MLI) {
|
|
|
|
ML = MLI->getLoopFor(IBB);
|
|
|
|
if (ML && IBB == ML->getHeader())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:52:35 +08:00
|
|
|
for (MachineBasicBlock *PBB : I->predecessors()) {
|
|
|
|
if (MergePotentials.size() == TailMergeThreshold)
|
|
|
|
break;
|
|
|
|
|
2012-05-24 06:09:50 +08:00
|
|
|
if (TriedMerging.count(PBB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Skip blocks that loop to themselves, can't tail merge these.
|
|
|
|
if (PBB == IBB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Visit each predecessor only once.
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!UniquePreds.insert(PBB).second)
|
2012-05-24 06:09:50 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Skip blocks which may jump to a landing pad. Can't tail merge these.
|
2015-09-18 01:19:40 +08:00
|
|
|
if (PBB->hasEHPadSuccessor())
|
2012-05-24 06:09:50 +08:00
|
|
|
continue;
|
|
|
|
|
2016-08-13 07:13:38 +08:00
|
|
|
// After block placement, only consider predecessors that belong to the
|
|
|
|
// same loop as IBB. The reason is the same as above when skipping loop
|
|
|
|
// header.
|
2016-06-09 23:24:29 +08:00
|
|
|
if (AfterBlockPlacement && MLI)
|
2016-08-13 07:13:38 +08:00
|
|
|
if (ML != MLI->getLoopFor(PBB))
|
|
|
|
continue;
|
2016-06-09 23:24:29 +08:00
|
|
|
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2012-05-24 06:09:50 +08:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2016-07-15 22:41:04 +08:00
|
|
|
if (!TII->analyzeBranch(*PBB, TBB, FBB, Cond, true)) {
|
2012-05-24 06:09:50 +08:00
|
|
|
// Failing case: IBB is the target of a cbr, and we cannot reverse the
|
|
|
|
// branch.
|
|
|
|
SmallVector<MachineOperand, 4> NewCond(Cond);
|
|
|
|
if (!Cond.empty() && TBB == IBB) {
|
2016-09-15 04:43:16 +08:00
|
|
|
if (TII->reverseBranchCondition(NewCond))
|
2012-05-24 06:09:50 +08:00
|
|
|
continue;
|
|
|
|
// This is the QBB case described above
|
2015-10-10 02:23:49 +08:00
|
|
|
if (!FBB) {
|
|
|
|
auto Next = ++PBB->getIterator();
|
|
|
|
if (Next != MF.end())
|
|
|
|
FBB = &*Next;
|
|
|
|
}
|
2012-05-24 06:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the unconditional branch at the end, if any.
|
|
|
|
if (TBB && (Cond.empty() || FBB)) {
|
2017-02-21 08:12:38 +08:00
|
|
|
DebugLoc dl = PBB->findBranchDebugLoc();
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(*PBB);
|
2012-05-24 06:09:50 +08:00
|
|
|
if (!Cond.empty())
|
|
|
|
// reinsert conditional branch only, for now
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(*PBB, (TBB == IBB) ? FBB : TBB, nullptr,
|
2014-04-14 08:51:57 +08:00
|
|
|
NewCond, dl);
|
2012-05-24 06:09:50 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 03:48:01 +08:00
|
|
|
MergePotentials.push_back(MergePotentialsElt(HashEndOfMBB(*PBB), PBB));
|
2007-05-08 04:57:21 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-24 06:09:50 +08:00
|
|
|
|
|
|
|
// If this is a large problem, avoid visiting the same basic blocks multiple
|
|
|
|
// times.
|
|
|
|
if (MergePotentials.size() == TailMergeThreshold)
|
|
|
|
for (unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
|
|
|
|
TriedMerging.insert(MergePotentials[i].getBlock());
|
|
|
|
|
|
|
|
if (MergePotentials.size() >= 2)
|
2016-08-19 02:57:29 +08:00
|
|
|
MadeChange |= TryTailMergeBlocks(IBB, PredBB, MinCommonTailLength);
|
2012-05-24 06:09:50 +08:00
|
|
|
|
|
|
|
// Reinsert an unconditional branch if needed. The 1 below can occur as a
|
|
|
|
// result of removing blocks in TryTailMergeBlocks.
|
2015-10-10 02:23:49 +08:00
|
|
|
PredBB = &*std::prev(I); // this may have been changed in TryTailMergeBlocks
|
2012-05-24 06:09:50 +08:00
|
|
|
if (MergePotentials.size() == 1 &&
|
|
|
|
MergePotentials.begin()->getBlock() != PredBB)
|
|
|
|
FixTail(MergePotentials.begin()->getBlock(), IBB, TII);
|
2007-05-08 04:57:21 +08:00
|
|
|
}
|
2012-05-24 06:09:50 +08:00
|
|
|
|
2007-05-08 04:57:21 +08:00
|
|
|
return MadeChange;
|
|
|
|
}
|
2006-10-21 08:47:49 +08:00
|
|
|
|
2014-08-08 03:30:13 +08:00
|
|
|
void BranchFolder::setCommonTailEdgeWeights(MachineBasicBlock &TailMBB) {
|
|
|
|
SmallVector<BlockFrequency, 2> EdgeFreqLs(TailMBB.succ_size());
|
|
|
|
BlockFrequency AccumulatedMBBFreq;
|
|
|
|
|
|
|
|
// Aggregate edge frequency of successor edge j:
|
|
|
|
// edgeFreq(j) = sum (freq(bb) * edgeProb(bb, j)),
|
|
|
|
// where bb is a basic block that is in SameTails.
|
|
|
|
for (const auto &Src : SameTails) {
|
|
|
|
const MachineBasicBlock *SrcMBB = Src.getBlock();
|
|
|
|
BlockFrequency BlockFreq = MBBFreqInfo.getBlockFreq(SrcMBB);
|
|
|
|
AccumulatedMBBFreq += BlockFreq;
|
|
|
|
|
|
|
|
// It is not necessary to recompute edge weights if TailBB has less than two
|
|
|
|
// successors.
|
|
|
|
if (TailMBB.succ_size() <= 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto EdgeFreq = EdgeFreqLs.begin();
|
|
|
|
|
|
|
|
for (auto SuccI = TailMBB.succ_begin(), SuccE = TailMBB.succ_end();
|
|
|
|
SuccI != SuccE; ++SuccI, ++EdgeFreq)
|
|
|
|
*EdgeFreq += BlockFreq * MBPI.getEdgeProbability(SrcMBB, *SuccI);
|
|
|
|
}
|
|
|
|
|
|
|
|
MBBFreqInfo.setBlockFreq(&TailMBB, AccumulatedMBBFreq);
|
|
|
|
|
|
|
|
if (TailMBB.succ_size() <= 1)
|
|
|
|
return;
|
|
|
|
|
2015-12-01 13:29:22 +08:00
|
|
|
auto SumEdgeFreq =
|
|
|
|
std::accumulate(EdgeFreqLs.begin(), EdgeFreqLs.end(), BlockFrequency(0))
|
|
|
|
.getFrequency();
|
2014-08-08 03:30:13 +08:00
|
|
|
auto EdgeFreq = EdgeFreqLs.begin();
|
|
|
|
|
2015-12-01 13:29:22 +08:00
|
|
|
if (SumEdgeFreq > 0) {
|
|
|
|
for (auto SuccI = TailMBB.succ_begin(), SuccE = TailMBB.succ_end();
|
|
|
|
SuccI != SuccE; ++SuccI, ++EdgeFreq) {
|
|
|
|
auto Prob = BranchProbability::getBranchProbability(
|
|
|
|
EdgeFreq->getFrequency(), SumEdgeFreq);
|
|
|
|
TailMBB.setSuccProbability(SuccI, Prob);
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 03:30:13 +08:00
|
|
|
}
|
|
|
|
|
2006-10-21 08:47:49 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Branch Optimization
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
|
2009-09-04 15:47:40 +08:00
|
|
|
bool MadeChange = false;
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2007-02-17 08:44:34 +08:00
|
|
|
// Make sure blocks are numbered in order
|
|
|
|
MF.RenumberBlocks();
|
2018-06-01 08:03:21 +08:00
|
|
|
// Renumbering blocks alters EH scope membership, recalculate it.
|
|
|
|
EHScopeMembership = getEHScopeMembership(MF);
|
2007-02-17 08:44:34 +08:00
|
|
|
|
2014-03-02 20:27:27 +08:00
|
|
|
for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
|
2011-05-12 08:56:58 +08:00
|
|
|
I != E; ) {
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineBasicBlock *MBB = &*I++;
|
2009-09-04 15:47:40 +08:00
|
|
|
MadeChange |= OptimizeBlock(MBB);
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-10-21 08:47:49 +08:00
|
|
|
// If it is dead, remove it.
|
2007-02-23 00:39:03 +08:00
|
|
|
if (MBB->pred_empty()) {
|
2006-10-21 08:47:49 +08:00
|
|
|
RemoveDeadBlock(MBB);
|
|
|
|
MadeChange = true;
|
|
|
|
++NumDeadBlocks;
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 10:22:52 +08:00
|
|
|
|
2006-10-21 08:47:49 +08:00
|
|
|
return MadeChange;
|
2004-07-31 18:01:27 +08:00
|
|
|
}
|
|
|
|
|
2010-03-10 13:45:47 +08:00
|
|
|
// Blocks should be considered empty if they contain only debug info;
|
|
|
|
// else the debug info would affect codegen.
|
|
|
|
static bool IsEmptyBlock(MachineBasicBlock *MBB) {
|
2015-06-23 22:47:29 +08:00
|
|
|
return MBB->getFirstNonDebugInstr() == MBB->end();
|
2010-03-10 13:45:47 +08:00
|
|
|
}
|
2006-10-21 08:47:49 +08:00
|
|
|
|
2010-03-11 03:57:56 +08:00
|
|
|
// Blocks with only debug info and branches should be considered the same
|
|
|
|
// as blocks with only branches.
|
|
|
|
static bool IsBranchOnlyBlock(MachineBasicBlock *MBB) {
|
2015-06-23 22:47:29 +08:00
|
|
|
MachineBasicBlock::iterator I = MBB->getFirstNonDebugInstr();
|
|
|
|
assert(I != MBB->end() && "empty block!");
|
|
|
|
return I->isBranch();
|
2010-03-11 03:57:56 +08:00
|
|
|
}
|
|
|
|
|
2006-11-19 04:47:54 +08:00
|
|
|
/// IsBetterFallthrough - Return true if it would be clearly better to
|
|
|
|
/// fall-through to MBB1 than to fall through into MBB2. This has to return
|
|
|
|
/// a strict ordering, returning true for both (MBB1,MBB2) and (MBB2,MBB1) will
|
|
|
|
/// result in infinite loops.
|
2009-11-12 02:38:14 +08:00
|
|
|
static bool IsBetterFallthrough(MachineBasicBlock *MBB1,
|
2008-01-07 09:56:04 +08:00
|
|
|
MachineBasicBlock *MBB2) {
|
2019-10-05 21:20:30 +08:00
|
|
|
assert(MBB1 && MBB2 && "Unknown MachineBasicBlock");
|
|
|
|
|
2006-11-19 05:30:35 +08:00
|
|
|
// Right now, we use a simple heuristic. If MBB2 ends with a call, and
|
|
|
|
// MBB1 doesn't, we prefer to fall through into MBB1. This allows us to
|
2006-11-19 04:47:54 +08:00
|
|
|
// optimize branches that branch to either a return block or an assert block
|
|
|
|
// into a fallthrough to the return.
|
2015-06-23 22:47:29 +08:00
|
|
|
MachineBasicBlock::iterator MBB1I = MBB1->getLastNonDebugInstr();
|
|
|
|
MachineBasicBlock::iterator MBB2I = MBB2->getLastNonDebugInstr();
|
|
|
|
if (MBB1I == MBB1->end() || MBB2I == MBB2->end())
|
|
|
|
return false;
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2007-12-10 15:24:06 +08:00
|
|
|
// If there is a clear successor ordering we make sure that one block
|
|
|
|
// will fall through to the next
|
|
|
|
if (MBB1->isSuccessor(MBB2)) return true;
|
|
|
|
if (MBB2->isSuccessor(MBB1)) return false;
|
2006-11-19 04:47:54 +08:00
|
|
|
|
2011-12-07 15:15:52 +08:00
|
|
|
return MBB2I->isCall() && !MBB1I->isCall();
|
2006-11-19 04:47:54 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 16:49:42 +08:00
|
|
|
/// getBranchDebugLoc - Find and return, if any, the DebugLoc of the branch
|
2015-06-23 22:47:29 +08:00
|
|
|
/// instructions on the block.
|
2012-03-07 16:49:42 +08:00
|
|
|
static DebugLoc getBranchDebugLoc(MachineBasicBlock &MBB) {
|
2015-06-23 22:47:29 +08:00
|
|
|
MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
|
|
|
|
if (I != MBB.end() && I->isBranch())
|
2012-03-07 16:49:42 +08:00
|
|
|
return I->getDebugLoc();
|
|
|
|
return DebugLoc();
|
|
|
|
}
|
|
|
|
|
[BranchFolding] Salvage DBG_VALUE instructions from empty blocks
Summary:
This patch will introduce copying of DBG_VALUE instructions
from an otherwise empty basic block to predecessor/successor
blocks in case the empty block is eliminated/bypassed. It
is currently only done in one identified situation in the
BranchFolding pass, before optimizing on empty block.
It can be seen as a light variant of the propagation done
by the LiveDebugValues pass, which unfortunately is executed
after the BranchFolding pass.
We only propagate (copy) DBG_VALUE instructions in a limited
number of situations:
a) If the empty BB is the only predecessor of a successor
we can copy the DBG_VALUE instruction to the beginning of
the successor (because the DBG_VALUE instruction is always
part of the flow between the blocks).
b) If the empty BB is the only successor of a predecessor
we can copy the DBG_VALUE instruction to the end of the
predecessor (because the DBG_VALUE instruction is always
part of the flow between the blocks). In this case we add
the DBG_VALUE just before the first terminator (assuming
that the terminators do not impact the DBG_VALUE).
A future solution, to handle more situations, could perhaps
be to run the LiveDebugValues pass before branch folding?
This fix is related to PR37234. It is expected to resolve
the problem seen, when applied together with the fix in
SelectionDAG from here: https://reviews.llvm.org/D46129
Reviewers: #debug-info, aprantl, rnk
Reviewed By: #debug-info, aprantl
Subscribers: ormris, gbedwell, llvm-commits
Differential Revision: https://reviews.llvm.org/D46184
llvm-svn: 331183
2018-04-30 22:37:46 +08:00
|
|
|
static void copyDebugInfoToPredecessor(const TargetInstrInfo *TII,
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock &PredMBB) {
|
|
|
|
auto InsertBefore = PredMBB.getFirstTerminator();
|
|
|
|
for (MachineInstr &MI : MBB.instrs())
|
2018-11-30 16:07:29 +08:00
|
|
|
if (MI.isDebugInstr()) {
|
[BranchFolding] Salvage DBG_VALUE instructions from empty blocks
Summary:
This patch will introduce copying of DBG_VALUE instructions
from an otherwise empty basic block to predecessor/successor
blocks in case the empty block is eliminated/bypassed. It
is currently only done in one identified situation in the
BranchFolding pass, before optimizing on empty block.
It can be seen as a light variant of the propagation done
by the LiveDebugValues pass, which unfortunately is executed
after the BranchFolding pass.
We only propagate (copy) DBG_VALUE instructions in a limited
number of situations:
a) If the empty BB is the only predecessor of a successor
we can copy the DBG_VALUE instruction to the beginning of
the successor (because the DBG_VALUE instruction is always
part of the flow between the blocks).
b) If the empty BB is the only successor of a predecessor
we can copy the DBG_VALUE instruction to the end of the
predecessor (because the DBG_VALUE instruction is always
part of the flow between the blocks). In this case we add
the DBG_VALUE just before the first terminator (assuming
that the terminators do not impact the DBG_VALUE).
A future solution, to handle more situations, could perhaps
be to run the LiveDebugValues pass before branch folding?
This fix is related to PR37234. It is expected to resolve
the problem seen, when applied together with the fix in
SelectionDAG from here: https://reviews.llvm.org/D46129
Reviewers: #debug-info, aprantl, rnk
Reviewed By: #debug-info, aprantl
Subscribers: ormris, gbedwell, llvm-commits
Differential Revision: https://reviews.llvm.org/D46184
llvm-svn: 331183
2018-04-30 22:37:46 +08:00
|
|
|
TII->duplicate(PredMBB, InsertBefore, MI);
|
2018-11-30 16:07:29 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Copied debug entity from empty block to pred: "
|
2018-05-14 20:53:11 +08:00
|
|
|
<< MI);
|
[BranchFolding] Salvage DBG_VALUE instructions from empty blocks
Summary:
This patch will introduce copying of DBG_VALUE instructions
from an otherwise empty basic block to predecessor/successor
blocks in case the empty block is eliminated/bypassed. It
is currently only done in one identified situation in the
BranchFolding pass, before optimizing on empty block.
It can be seen as a light variant of the propagation done
by the LiveDebugValues pass, which unfortunately is executed
after the BranchFolding pass.
We only propagate (copy) DBG_VALUE instructions in a limited
number of situations:
a) If the empty BB is the only predecessor of a successor
we can copy the DBG_VALUE instruction to the beginning of
the successor (because the DBG_VALUE instruction is always
part of the flow between the blocks).
b) If the empty BB is the only successor of a predecessor
we can copy the DBG_VALUE instruction to the end of the
predecessor (because the DBG_VALUE instruction is always
part of the flow between the blocks). In this case we add
the DBG_VALUE just before the first terminator (assuming
that the terminators do not impact the DBG_VALUE).
A future solution, to handle more situations, could perhaps
be to run the LiveDebugValues pass before branch folding?
This fix is related to PR37234. It is expected to resolve
the problem seen, when applied together with the fix in
SelectionDAG from here: https://reviews.llvm.org/D46129
Reviewers: #debug-info, aprantl, rnk
Reviewed By: #debug-info, aprantl
Subscribers: ormris, gbedwell, llvm-commits
Differential Revision: https://reviews.llvm.org/D46184
llvm-svn: 331183
2018-04-30 22:37:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copyDebugInfoToSuccessor(const TargetInstrInfo *TII,
|
|
|
|
MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock &SuccMBB) {
|
|
|
|
auto InsertBefore = SuccMBB.SkipPHIsAndLabels(SuccMBB.begin());
|
|
|
|
for (MachineInstr &MI : MBB.instrs())
|
2018-11-30 16:07:29 +08:00
|
|
|
if (MI.isDebugInstr()) {
|
[BranchFolding] Salvage DBG_VALUE instructions from empty blocks
Summary:
This patch will introduce copying of DBG_VALUE instructions
from an otherwise empty basic block to predecessor/successor
blocks in case the empty block is eliminated/bypassed. It
is currently only done in one identified situation in the
BranchFolding pass, before optimizing on empty block.
It can be seen as a light variant of the propagation done
by the LiveDebugValues pass, which unfortunately is executed
after the BranchFolding pass.
We only propagate (copy) DBG_VALUE instructions in a limited
number of situations:
a) If the empty BB is the only predecessor of a successor
we can copy the DBG_VALUE instruction to the beginning of
the successor (because the DBG_VALUE instruction is always
part of the flow between the blocks).
b) If the empty BB is the only successor of a predecessor
we can copy the DBG_VALUE instruction to the end of the
predecessor (because the DBG_VALUE instruction is always
part of the flow between the blocks). In this case we add
the DBG_VALUE just before the first terminator (assuming
that the terminators do not impact the DBG_VALUE).
A future solution, to handle more situations, could perhaps
be to run the LiveDebugValues pass before branch folding?
This fix is related to PR37234. It is expected to resolve
the problem seen, when applied together with the fix in
SelectionDAG from here: https://reviews.llvm.org/D46129
Reviewers: #debug-info, aprantl, rnk
Reviewed By: #debug-info, aprantl
Subscribers: ormris, gbedwell, llvm-commits
Differential Revision: https://reviews.llvm.org/D46184
llvm-svn: 331183
2018-04-30 22:37:46 +08:00
|
|
|
TII->duplicate(SuccMBB, InsertBefore, MI);
|
2018-11-30 16:07:29 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Copied debug entity from empty block to succ: "
|
2018-05-14 20:53:11 +08:00
|
|
|
<< MI);
|
[BranchFolding] Salvage DBG_VALUE instructions from empty blocks
Summary:
This patch will introduce copying of DBG_VALUE instructions
from an otherwise empty basic block to predecessor/successor
blocks in case the empty block is eliminated/bypassed. It
is currently only done in one identified situation in the
BranchFolding pass, before optimizing on empty block.
It can be seen as a light variant of the propagation done
by the LiveDebugValues pass, which unfortunately is executed
after the BranchFolding pass.
We only propagate (copy) DBG_VALUE instructions in a limited
number of situations:
a) If the empty BB is the only predecessor of a successor
we can copy the DBG_VALUE instruction to the beginning of
the successor (because the DBG_VALUE instruction is always
part of the flow between the blocks).
b) If the empty BB is the only successor of a predecessor
we can copy the DBG_VALUE instruction to the end of the
predecessor (because the DBG_VALUE instruction is always
part of the flow between the blocks). In this case we add
the DBG_VALUE just before the first terminator (assuming
that the terminators do not impact the DBG_VALUE).
A future solution, to handle more situations, could perhaps
be to run the LiveDebugValues pass before branch folding?
This fix is related to PR37234. It is expected to resolve
the problem seen, when applied together with the fix in
SelectionDAG from here: https://reviews.llvm.org/D46129
Reviewers: #debug-info, aprantl, rnk
Reviewed By: #debug-info, aprantl
Subscribers: ormris, gbedwell, llvm-commits
Differential Revision: https://reviews.llvm.org/D46184
llvm-svn: 331183
2018-04-30 22:37:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to salvage DBG_VALUE instructions from an otherwise empty block. If such
|
|
|
|
// a basic block is removed we would lose the debug information unless we have
|
|
|
|
// copied the information to a predecessor/successor.
|
|
|
|
//
|
|
|
|
// TODO: This function only handles some simple cases. An alternative would be
|
|
|
|
// to run a heavier analysis, such as the LiveDebugValues pass, before we do
|
|
|
|
// branch folding.
|
|
|
|
static void salvageDebugInfoFromEmptyBlock(const TargetInstrInfo *TII,
|
|
|
|
MachineBasicBlock &MBB) {
|
|
|
|
assert(IsEmptyBlock(&MBB) && "Expected an empty block (except debug info).");
|
|
|
|
// If this MBB is the only predecessor of a successor it is legal to copy
|
|
|
|
// DBG_VALUE instructions to the beginning of the successor.
|
|
|
|
for (MachineBasicBlock *SuccBB : MBB.successors())
|
|
|
|
if (SuccBB->pred_size() == 1)
|
|
|
|
copyDebugInfoToSuccessor(TII, MBB, *SuccBB);
|
|
|
|
// If this MBB is the only successor of a predecessor it is legal to copy the
|
|
|
|
// DBG_VALUE instructions to the end of the predecessor (just before the
|
|
|
|
// terminators, assuming that the terminator isn't affecting the DBG_VALUE).
|
|
|
|
for (MachineBasicBlock *PredBB : MBB.predecessors())
|
|
|
|
if (PredBB->succ_size() == 1)
|
|
|
|
copyDebugInfoToPredecessor(TII, MBB, *PredBB);
|
|
|
|
}
|
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
|
|
|
|
bool MadeChange = false;
|
2009-11-12 02:18:34 +08:00
|
|
|
MachineFunction &MF = *MBB->getParent();
|
2009-11-12 03:48:59 +08:00
|
|
|
ReoptimizeBlock:
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineFunction::iterator FallThrough = MBB->getIterator();
|
2006-10-24 09:12:32 +08:00
|
|
|
++FallThrough;
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2018-06-01 08:03:21 +08:00
|
|
|
// Make sure MBB and FallThrough belong to the same EH scope.
|
|
|
|
bool SameEHScope = true;
|
|
|
|
if (!EHScopeMembership.empty() && FallThrough != MF.end()) {
|
|
|
|
auto MBBEHScope = EHScopeMembership.find(MBB);
|
|
|
|
assert(MBBEHScope != EHScopeMembership.end());
|
|
|
|
auto FallThroughEHScope = EHScopeMembership.find(&*FallThrough);
|
|
|
|
assert(FallThroughEHScope != EHScopeMembership.end());
|
|
|
|
SameEHScope = MBBEHScope->second == FallThroughEHScope->second;
|
2015-10-04 10:22:52 +08:00
|
|
|
}
|
|
|
|
|
2006-10-14 04:43:10 +08:00
|
|
|
// If this block is empty, make everyone use its fall-through, not the block
|
2007-06-01 05:54:00 +08:00
|
|
|
// explicitly. Landing pads should not do this since the landing-pad table
|
2009-10-30 10:13:27 +08:00
|
|
|
// points to this block. Blocks with their addresses taken shouldn't be
|
|
|
|
// optimized away.
|
2015-10-04 10:22:52 +08:00
|
|
|
if (IsEmptyBlock(MBB) && !MBB->isEHPad() && !MBB->hasAddressTaken() &&
|
2018-06-01 08:03:21 +08:00
|
|
|
SameEHScope) {
|
[BranchFolding] Salvage DBG_VALUE instructions from empty blocks
Summary:
This patch will introduce copying of DBG_VALUE instructions
from an otherwise empty basic block to predecessor/successor
blocks in case the empty block is eliminated/bypassed. It
is currently only done in one identified situation in the
BranchFolding pass, before optimizing on empty block.
It can be seen as a light variant of the propagation done
by the LiveDebugValues pass, which unfortunately is executed
after the BranchFolding pass.
We only propagate (copy) DBG_VALUE instructions in a limited
number of situations:
a) If the empty BB is the only predecessor of a successor
we can copy the DBG_VALUE instruction to the beginning of
the successor (because the DBG_VALUE instruction is always
part of the flow between the blocks).
b) If the empty BB is the only successor of a predecessor
we can copy the DBG_VALUE instruction to the end of the
predecessor (because the DBG_VALUE instruction is always
part of the flow between the blocks). In this case we add
the DBG_VALUE just before the first terminator (assuming
that the terminators do not impact the DBG_VALUE).
A future solution, to handle more situations, could perhaps
be to run the LiveDebugValues pass before branch folding?
This fix is related to PR37234. It is expected to resolve
the problem seen, when applied together with the fix in
SelectionDAG from here: https://reviews.llvm.org/D46129
Reviewers: #debug-info, aprantl, rnk
Reviewed By: #debug-info, aprantl
Subscribers: ormris, gbedwell, llvm-commits
Differential Revision: https://reviews.llvm.org/D46184
llvm-svn: 331183
2018-04-30 22:37:46 +08:00
|
|
|
salvageDebugInfoFromEmptyBlock(TII, *MBB);
|
2006-10-21 13:08:28 +08:00
|
|
|
// Dead block? Leave for cleanup later.
|
2009-09-04 15:47:40 +08:00
|
|
|
if (MBB->pred_empty()) return MadeChange;
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2009-11-12 02:18:34 +08:00
|
|
|
if (FallThrough == MF.end()) {
|
2006-10-18 01:13:52 +08:00
|
|
|
// TODO: Simplify preds to not branch here if possible!
|
2015-08-28 07:27:47 +08:00
|
|
|
} else if (FallThrough->isEHPad()) {
|
2015-05-01 02:58:23 +08:00
|
|
|
// Don't rewrite to a landing pad fallthough. That could lead to the case
|
|
|
|
// where a BB jumps to more than one landing pad.
|
|
|
|
// TODO: Is it ever worth rewriting predecessors which don't already
|
|
|
|
// jump to a landing pad, and so can safely jump to the fallthrough?
|
2016-06-25 02:16:36 +08:00
|
|
|
} else if (MBB->isSuccessor(&*FallThrough)) {
|
2006-10-18 01:13:52 +08:00
|
|
|
// Rewrite all predecessors of the old block to go to the fallthrough
|
|
|
|
// instead.
|
2007-02-23 00:39:03 +08:00
|
|
|
while (!MBB->pred_empty()) {
|
2006-10-14 08:21:48 +08:00
|
|
|
MachineBasicBlock *Pred = *(MBB->pred_end()-1);
|
2015-10-10 02:23:49 +08:00
|
|
|
Pred->ReplaceUsesOfBlockWith(MBB, &*FallThrough);
|
2006-10-14 08:21:48 +08:00
|
|
|
}
|
2006-10-18 01:13:52 +08:00
|
|
|
// If MBB was the target of a jump table, update jump tables to go to the
|
|
|
|
// fallthrough instead.
|
2010-01-26 07:26:13 +08:00
|
|
|
if (MachineJumpTableInfo *MJTI = MF.getJumpTableInfo())
|
2015-10-10 02:23:49 +08:00
|
|
|
MJTI->ReplaceMBBInJumpTables(MBB, &*FallThrough);
|
2006-10-14 08:21:48 +08:00
|
|
|
MadeChange = true;
|
2004-07-31 18:01:27 +08:00
|
|
|
}
|
2009-09-04 15:47:40 +08:00
|
|
|
return MadeChange;
|
2004-07-31 18:01:27 +08:00
|
|
|
}
|
|
|
|
|
2006-10-14 08:21:48 +08:00
|
|
|
// Check to see if we can simplify the terminator of the block before this
|
|
|
|
// one.
|
2014-03-02 20:27:27 +08:00
|
|
|
MachineBasicBlock &PrevBB = *std::prev(MachineFunction::iterator(MBB));
|
Enable deleting branches to successor blocks. With the previous patches,
branch folding can now compile stuff like this:
void foo(int W, int X, int Y, int Z) {
if (W & 1) {
for (; X;--X) bar();
} else if (W & 2) {
for (; Y;--Y) bar();
} else if (W & 4) {
for (; Z;--Z) bar();
} else if (W & 8) {
for (; W;--W) bar();
}
if (W) {
bar();
}
}
contrived testcase where loops exits all end up merging together. To have
the loop merges be:
...
cmplw cr0, r30, r27
bne cr0, LBB1_14 ;bb38
LBB1_16: ;cond_next48.loopexit
mr r27, r29
LBB1_20: ;cond_next48
cmplwi cr0, r27, 0
beq cr0, LBB1_22 ;UnifiedReturnBlock
...
instead of:
...
cmplw cr0, r30, r27
bne cr0, LBB1_14 ;bb38
LBB1_16: ;cond_next48.loopexit
mr r27, r29
b LBB1_20 ;cond_next48
LBB1_17: ;cond_next48.loopexit1
b LBB1_20 ;cond_next48
LBB1_18: ;cond_next48.loopexit2
b LBB1_20 ;cond_next48
LBB1_19: ;cond_next48.loopexit3
LBB1_20: ;cond_next48
cmplwi cr0, r27, 0
beq cr0, LBB1_22 ;UnifiedReturnBlock
...
This is CodeGen/PowerPC/branch-opt.ll
llvm-svn: 31006
2006-10-18 02:16:40 +08:00
|
|
|
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr;
|
2008-08-15 06:49:33 +08:00
|
|
|
SmallVector<MachineOperand, 4> PriorCond;
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
bool PriorUnAnalyzable =
|
2016-07-15 22:41:04 +08:00
|
|
|
TII->analyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true);
|
2006-10-21 13:08:28 +08:00
|
|
|
if (!PriorUnAnalyzable) {
|
|
|
|
// If the CFG for the prior block has extra edges, remove them.
|
2007-06-19 06:43:58 +08:00
|
|
|
MadeChange |= PrevBB.CorrectExtraCFGEdges(PriorTBB, PriorFBB,
|
|
|
|
!PriorCond.empty());
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-10-14 08:21:48 +08:00
|
|
|
// If the previous branch is conditional and both conditions go to the same
|
2006-10-21 13:43:30 +08:00
|
|
|
// destination, remove the branch, replacing it with an unconditional one or
|
|
|
|
// a fall-through.
|
2006-10-14 08:21:48 +08:00
|
|
|
if (PriorTBB && PriorTBB == PriorFBB) {
|
2012-03-07 16:49:42 +08:00
|
|
|
DebugLoc dl = getBranchDebugLoc(PrevBB);
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(PrevBB);
|
2009-11-12 02:38:14 +08:00
|
|
|
PriorCond.clear();
|
2006-10-24 09:12:32 +08:00
|
|
|
if (PriorTBB != MBB)
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(PrevBB, PriorTBB, nullptr, PriorCond, dl);
|
2006-10-14 08:21:48 +08:00
|
|
|
MadeChange = true;
|
2006-10-21 08:47:49 +08:00
|
|
|
++NumBranchOpts;
|
2009-11-12 03:48:59 +08:00
|
|
|
goto ReoptimizeBlock;
|
2006-10-14 08:21:48 +08:00
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2009-11-12 03:48:59 +08:00
|
|
|
// If the previous block unconditionally falls through to this block and
|
|
|
|
// this block has no other predecessors, move the contents of this block
|
|
|
|
// into the prior block. This doesn't usually happen when SimplifyCFG
|
2009-11-18 01:40:31 +08:00
|
|
|
// has been used, but it can happen if tail merging splits a fall-through
|
|
|
|
// predecessor of a block.
|
2009-11-12 03:48:59 +08:00
|
|
|
// This has to check PrevBB->succ_size() because EH edges are ignored by
|
2020-01-21 23:47:35 +08:00
|
|
|
// analyzeBranch.
|
2009-11-12 03:48:59 +08:00
|
|
|
if (PriorCond.empty() && !PriorTBB && MBB->pred_size() == 1 &&
|
|
|
|
PrevBB.succ_size() == 1 &&
|
2015-08-28 07:27:47 +08:00
|
|
|
!MBB->hasAddressTaken() && !MBB->isEHPad()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nMerging into block: " << PrevBB
|
|
|
|
<< "From MBB: " << *MBB);
|
2011-05-27 05:49:28 +08:00
|
|
|
// Remove redundant DBG_VALUEs first.
|
2011-05-27 05:47:59 +08:00
|
|
|
if (PrevBB.begin() != PrevBB.end()) {
|
|
|
|
MachineBasicBlock::iterator PrevBBIter = PrevBB.end();
|
|
|
|
--PrevBBIter;
|
|
|
|
MachineBasicBlock::iterator MBBIter = MBB->begin();
|
2012-02-09 05:22:43 +08:00
|
|
|
// Check if DBG_VALUE at the end of PrevBB is identical to the
|
2011-05-27 05:49:28 +08:00
|
|
|
// DBG_VALUE at the beginning of MBB.
|
2011-05-27 05:47:59 +08:00
|
|
|
while (PrevBBIter != PrevBB.begin() && MBBIter != MBB->end()
|
2018-05-09 10:42:00 +08:00
|
|
|
&& PrevBBIter->isDebugInstr() && MBBIter->isDebugInstr()) {
|
2016-02-28 04:01:33 +08:00
|
|
|
if (!MBBIter->isIdenticalTo(*PrevBBIter))
|
2011-05-27 05:47:59 +08:00
|
|
|
break;
|
2016-02-28 04:27:44 +08:00
|
|
|
MachineInstr &DuplicateDbg = *MBBIter;
|
2011-05-27 05:47:59 +08:00
|
|
|
++MBBIter; -- PrevBBIter;
|
2016-02-28 04:27:44 +08:00
|
|
|
DuplicateDbg.eraseFromParent();
|
2011-05-27 05:47:59 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-12 03:48:59 +08:00
|
|
|
PrevBB.splice(PrevBB.end(), MBB, MBB->begin(), MBB->end());
|
2012-02-23 01:25:00 +08:00
|
|
|
PrevBB.removeSuccessor(PrevBB.succ_begin());
|
2009-11-12 03:48:59 +08:00
|
|
|
assert(PrevBB.succ_empty());
|
|
|
|
PrevBB.transferSuccessors(MBB);
|
|
|
|
MadeChange = true;
|
|
|
|
return MadeChange;
|
|
|
|
}
|
2009-11-17 01:56:13 +08:00
|
|
|
|
2006-10-14 08:21:48 +08:00
|
|
|
// If the previous branch *only* branches to *this* block (conditional or
|
|
|
|
// not) remove the branch.
|
2014-04-14 08:51:57 +08:00
|
|
|
if (PriorTBB == MBB && !PriorFBB) {
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(PrevBB);
|
2006-10-14 08:21:48 +08:00
|
|
|
MadeChange = true;
|
2006-10-21 08:47:49 +08:00
|
|
|
++NumBranchOpts;
|
2009-11-12 03:48:59 +08:00
|
|
|
goto ReoptimizeBlock;
|
2006-10-14 08:21:48 +08:00
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-10-21 13:43:30 +08:00
|
|
|
// If the prior block branches somewhere else on the condition and here if
|
|
|
|
// the condition is false, remove the uncond second branch.
|
2006-10-24 09:12:32 +08:00
|
|
|
if (PriorFBB == MBB) {
|
2012-03-07 16:49:42 +08:00
|
|
|
DebugLoc dl = getBranchDebugLoc(PrevBB);
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(PrevBB);
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(PrevBB, PriorTBB, nullptr, PriorCond, dl);
|
2006-10-21 13:43:30 +08:00
|
|
|
MadeChange = true;
|
|
|
|
++NumBranchOpts;
|
2009-11-12 03:48:59 +08:00
|
|
|
goto ReoptimizeBlock;
|
2006-10-21 13:43:30 +08:00
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-10-21 13:54:00 +08:00
|
|
|
// If the prior block branches here on true and somewhere else on false, and
|
|
|
|
// if the branch condition is reversible, reverse the branch to create a
|
|
|
|
// fall-through.
|
2006-10-24 09:12:32 +08:00
|
|
|
if (PriorTBB == MBB) {
|
2008-08-15 06:49:33 +08:00
|
|
|
SmallVector<MachineOperand, 4> NewPriorCond(PriorCond);
|
2016-09-15 04:43:16 +08:00
|
|
|
if (!TII->reverseBranchCondition(NewPriorCond)) {
|
2012-03-07 16:49:42 +08:00
|
|
|
DebugLoc dl = getBranchDebugLoc(PrevBB);
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(PrevBB);
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(PrevBB, PriorFBB, nullptr, NewPriorCond, dl);
|
2006-10-21 13:54:00 +08:00
|
|
|
MadeChange = true;
|
|
|
|
++NumBranchOpts;
|
2009-11-12 03:48:59 +08:00
|
|
|
goto ReoptimizeBlock;
|
2006-10-21 13:54:00 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2009-10-22 08:03:58 +08:00
|
|
|
// If this block has no successors (e.g. it is a return block or ends with
|
|
|
|
// a call to a no-return function like abort or __cxa_throw) and if the pred
|
|
|
|
// falls through into this block, and if it would otherwise fall through
|
|
|
|
// into the block after this, move this block to the end of the function.
|
2006-11-19 05:30:35 +08:00
|
|
|
//
|
2006-11-19 04:47:54 +08:00
|
|
|
// We consider it more likely that execution will stay in the function (e.g.
|
|
|
|
// due to loops) than it is to exit it. This asserts in loops etc, moving
|
|
|
|
// the assert condition out of the loop body.
|
2014-04-14 08:51:57 +08:00
|
|
|
if (MBB->succ_empty() && !PriorCond.empty() && !PriorFBB &&
|
2006-11-19 05:30:35 +08:00
|
|
|
MachineFunction::iterator(PriorTBB) == FallThrough &&
|
2009-11-26 08:32:21 +08:00
|
|
|
!MBB->canFallThrough()) {
|
2006-11-19 05:56:39 +08:00
|
|
|
bool DoTransform = true;
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-11-19 04:47:54 +08:00
|
|
|
// We have to be careful that the succs of PredBB aren't both no-successor
|
|
|
|
// blocks. If neither have successors and if PredBB is the second from
|
|
|
|
// last block in the function, we'd just keep swapping the two blocks for
|
|
|
|
// last. Only do the swap if one is clearly better to fall through than
|
|
|
|
// the other.
|
2009-11-12 02:18:34 +08:00
|
|
|
if (FallThrough == --MF.end() &&
|
2008-01-07 09:56:04 +08:00
|
|
|
!IsBetterFallthrough(PriorTBB, MBB))
|
2006-11-19 05:56:39 +08:00
|
|
|
DoTransform = false;
|
|
|
|
|
|
|
|
if (DoTransform) {
|
2006-11-19 04:47:54 +08:00
|
|
|
// Reverse the branch so we will fall through on the previous true cond.
|
2008-08-15 06:49:33 +08:00
|
|
|
SmallVector<MachineOperand, 4> NewPriorCond(PriorCond);
|
2016-09-15 04:43:16 +08:00
|
|
|
if (!TII->reverseBranchCondition(NewPriorCond)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\nMoving MBB: " << *MBB
|
|
|
|
<< "To make fallthrough to: " << *PriorTBB << "\n");
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2012-03-07 16:49:42 +08:00
|
|
|
DebugLoc dl = getBranchDebugLoc(PrevBB);
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(PrevBB);
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(PrevBB, MBB, nullptr, NewPriorCond, dl);
|
2006-11-19 04:47:54 +08:00
|
|
|
|
|
|
|
// Move this block to the end of the function.
|
2015-10-10 02:23:49 +08:00
|
|
|
MBB->moveAfter(&MF.back());
|
2006-11-19 04:47:54 +08:00
|
|
|
MadeChange = true;
|
|
|
|
++NumBranchOpts;
|
2009-09-04 15:47:40 +08:00
|
|
|
return MadeChange;
|
2006-11-19 04:47:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-14 08:21:48 +08:00
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2019-12-06 01:39:37 +08:00
|
|
|
bool OptForSize =
|
|
|
|
MF.getFunction().hasOptSize() ||
|
2020-01-30 01:36:31 +08:00
|
|
|
llvm::shouldOptimizeForSize(MBB, PSI, &MBBFreqInfo);
|
2019-12-06 01:39:37 +08:00
|
|
|
if (!IsEmptyBlock(MBB) && MBB->pred_size() == 1 && OptForSize) {
|
2017-02-16 08:04:05 +08:00
|
|
|
// Changing "Jcc foo; foo: jmp bar;" into "Jcc bar;" might change the branch
|
|
|
|
// direction, thereby defeating careful block placement and regressing
|
|
|
|
// performance. Therefore, only consider this for optsize functions.
|
|
|
|
MachineInstr &TailCall = *MBB->getFirstNonDebugInstr();
|
|
|
|
if (TII->isUnconditionalTailCall(TailCall)) {
|
|
|
|
MachineBasicBlock *Pred = *MBB->pred_begin();
|
|
|
|
MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
|
|
|
|
SmallVector<MachineOperand, 4> PredCond;
|
|
|
|
bool PredAnalyzable =
|
|
|
|
!TII->analyzeBranch(*Pred, PredTBB, PredFBB, PredCond, true);
|
|
|
|
|
2017-07-29 03:48:40 +08:00
|
|
|
if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB &&
|
|
|
|
PredTBB != PredFBB) {
|
2017-02-16 08:04:05 +08:00
|
|
|
// The predecessor has a conditional branch to this block which consists
|
|
|
|
// of only a tail call. Try to fold the tail call into the conditional
|
|
|
|
// branch.
|
|
|
|
if (TII->canMakeTailCallConditional(PredCond, TailCall)) {
|
|
|
|
// TODO: It would be nice if analyzeBranch() could provide a pointer
|
2017-07-29 03:48:40 +08:00
|
|
|
// to the branch instruction so replaceBranchWithTailCall() doesn't
|
2017-02-16 08:04:05 +08:00
|
|
|
// have to search for it.
|
|
|
|
TII->replaceBranchWithTailCall(*Pred, PredCond, TailCall);
|
|
|
|
++NumTailCalls;
|
|
|
|
Pred->removeSuccessor(MBB);
|
|
|
|
MadeChange = true;
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the predecessor is falling through to this block, we could reverse
|
|
|
|
// the branch condition and fold the tail call into that. However, after
|
|
|
|
// that we might have to re-arrange the CFG to fall through to the other
|
|
|
|
// block and there is a high risk of regressing code size rather than
|
|
|
|
// improving it.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-21 13:08:28 +08:00
|
|
|
// Analyze the branch in the current block.
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *CurTBB = nullptr, *CurFBB = nullptr;
|
2008-08-15 06:49:33 +08:00
|
|
|
SmallVector<MachineOperand, 4> CurCond;
|
2016-07-15 22:41:04 +08:00
|
|
|
bool CurUnAnalyzable =
|
|
|
|
TII->analyzeBranch(*MBB, CurTBB, CurFBB, CurCond, true);
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
if (!CurUnAnalyzable) {
|
2006-10-21 13:08:28 +08:00
|
|
|
// If the CFG for the prior block has extra edges, remove them.
|
2007-06-19 06:43:58 +08:00
|
|
|
MadeChange |= MBB->CorrectExtraCFGEdges(CurTBB, CurFBB, !CurCond.empty());
|
2006-10-21 13:08:28 +08:00
|
|
|
|
2009-11-12 02:38:14 +08:00
|
|
|
// If this is a two-way branch, and the FBB branches to this block, reverse
|
optimize single MBB loops better. In particular, produce:
LBB1_57: #bb207.i
movl 72(%esp), %ecx
movb (%ecx,%eax), %cl
movl 80(%esp), %edx
movb %cl, 1(%edx,%eax)
incl %eax
cmpl $143, %eax
jne LBB1_57 #bb207.i
jmp LBB1_64 #cond_next255.i
intead of:
LBB1_57: #bb207.i
movl 72(%esp), %ecx
movb (%ecx,%eax), %cl
movl 80(%esp), %edx
movb %cl, 1(%edx,%eax)
incl %eax
cmpl $143, %eax
je LBB1_64 #cond_next255.i
jmp LBB1_57 #bb207.i
This eliminates a branch per iteration of the loop. This hurted PPC
particularly, because the extra branch meant another dispatch group for each
iteration of the loop.
llvm-svn: 31530
2006-11-08 09:03:21 +08:00
|
|
|
// the condition so the single-basic-block loop is faster. Instead of:
|
|
|
|
// Loop: xxx; jcc Out; jmp Loop
|
|
|
|
// we want:
|
|
|
|
// Loop: xxx; jncc Loop; jmp Out
|
|
|
|
if (CurTBB && CurFBB && CurFBB == MBB && CurTBB != MBB) {
|
2008-08-15 06:49:33 +08:00
|
|
|
SmallVector<MachineOperand, 4> NewCond(CurCond);
|
2016-09-15 04:43:16 +08:00
|
|
|
if (!TII->reverseBranchCondition(NewCond)) {
|
2012-03-07 16:49:42 +08:00
|
|
|
DebugLoc dl = getBranchDebugLoc(*MBB);
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(*MBB);
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(*MBB, CurFBB, CurTBB, NewCond, dl);
|
optimize single MBB loops better. In particular, produce:
LBB1_57: #bb207.i
movl 72(%esp), %ecx
movb (%ecx,%eax), %cl
movl 80(%esp), %edx
movb %cl, 1(%edx,%eax)
incl %eax
cmpl $143, %eax
jne LBB1_57 #bb207.i
jmp LBB1_64 #cond_next255.i
intead of:
LBB1_57: #bb207.i
movl 72(%esp), %ecx
movb (%ecx,%eax), %cl
movl 80(%esp), %edx
movb %cl, 1(%edx,%eax)
incl %eax
cmpl $143, %eax
je LBB1_64 #cond_next255.i
jmp LBB1_57 #bb207.i
This eliminates a branch per iteration of the loop. This hurted PPC
particularly, because the extra branch meant another dispatch group for each
iteration of the loop.
llvm-svn: 31530
2006-11-08 09:03:21 +08:00
|
|
|
MadeChange = true;
|
|
|
|
++NumBranchOpts;
|
2009-11-12 03:48:59 +08:00
|
|
|
goto ReoptimizeBlock;
|
optimize single MBB loops better. In particular, produce:
LBB1_57: #bb207.i
movl 72(%esp), %ecx
movb (%ecx,%eax), %cl
movl 80(%esp), %edx
movb %cl, 1(%edx,%eax)
incl %eax
cmpl $143, %eax
jne LBB1_57 #bb207.i
jmp LBB1_64 #cond_next255.i
intead of:
LBB1_57: #bb207.i
movl 72(%esp), %ecx
movb (%ecx,%eax), %cl
movl 80(%esp), %edx
movb %cl, 1(%edx,%eax)
incl %eax
cmpl $143, %eax
je LBB1_64 #cond_next255.i
jmp LBB1_57 #bb207.i
This eliminates a branch per iteration of the loop. This hurted PPC
particularly, because the extra branch meant another dispatch group for each
iteration of the loop.
llvm-svn: 31530
2006-11-08 09:03:21 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-10-21 13:08:28 +08:00
|
|
|
// If this branch is the only thing in its block, see if we can forward
|
|
|
|
// other blocks across it.
|
2014-04-14 08:51:57 +08:00
|
|
|
if (CurTBB && CurCond.empty() && !CurFBB &&
|
2010-03-11 03:57:56 +08:00
|
|
|
IsBranchOnlyBlock(MBB) && CurTBB != MBB &&
|
2015-11-12 07:09:31 +08:00
|
|
|
!MBB->hasAddressTaken() && !MBB->isEHPad()) {
|
2012-03-07 16:49:42 +08:00
|
|
|
DebugLoc dl = getBranchDebugLoc(*MBB);
|
2006-10-21 13:08:28 +08:00
|
|
|
// This block may contain just an unconditional branch. Because there can
|
|
|
|
// be 'non-branch terminators' in the block, try removing the branch and
|
|
|
|
// then seeing if the block is empty.
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(*MBB);
|
2010-03-10 13:45:47 +08:00
|
|
|
// If the only things remaining in the block are debug info, remove these
|
|
|
|
// as well, so this will behave the same as an empty block in non-debug
|
|
|
|
// mode.
|
2015-06-23 22:47:29 +08:00
|
|
|
if (IsEmptyBlock(MBB)) {
|
|
|
|
// Make the block empty, losing the debug info (we could probably
|
|
|
|
// improve this in some cases.)
|
|
|
|
MBB->erase(MBB->begin(), MBB->end());
|
2010-03-10 13:45:47 +08:00
|
|
|
}
|
2006-10-21 13:08:28 +08:00
|
|
|
// If this block is just an unconditional branch to CurTBB, we can
|
|
|
|
// usually completely eliminate the block. The only case we cannot
|
|
|
|
// completely eliminate the block is when the block before this one
|
|
|
|
// falls through into MBB and we can't understand the prior block's branch
|
|
|
|
// condition.
|
2006-10-29 01:32:47 +08:00
|
|
|
if (MBB->empty()) {
|
2009-12-05 08:44:40 +08:00
|
|
|
bool PredHasNoFallThrough = !PrevBB.canFallThrough();
|
2006-10-29 01:32:47 +08:00
|
|
|
if (PredHasNoFallThrough || !PriorUnAnalyzable ||
|
|
|
|
!PrevBB.isSuccessor(MBB)) {
|
|
|
|
// If the prior block falls through into us, turn it into an
|
|
|
|
// explicit branch to us to make updates simpler.
|
2009-11-12 02:38:14 +08:00
|
|
|
if (!PredHasNoFallThrough && PrevBB.isSuccessor(MBB) &&
|
2006-10-29 01:32:47 +08:00
|
|
|
PriorTBB != MBB && PriorFBB != MBB) {
|
2014-04-14 08:51:57 +08:00
|
|
|
if (!PriorTBB) {
|
|
|
|
assert(PriorCond.empty() && !PriorFBB &&
|
2006-10-29 02:34:47 +08:00
|
|
|
"Bad branch analysis");
|
2006-10-29 01:32:47 +08:00
|
|
|
PriorTBB = MBB;
|
|
|
|
} else {
|
2014-04-14 08:51:57 +08:00
|
|
|
assert(!PriorFBB && "Machine CFG out of date!");
|
2006-10-29 01:32:47 +08:00
|
|
|
PriorFBB = MBB;
|
|
|
|
}
|
2012-03-07 16:49:42 +08:00
|
|
|
DebugLoc pdl = getBranchDebugLoc(PrevBB);
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(PrevBB);
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, pdl);
|
2006-10-21 13:08:28 +08:00
|
|
|
}
|
2004-07-31 18:01:27 +08:00
|
|
|
|
2006-10-29 01:32:47 +08:00
|
|
|
// Iterate through all the predecessors, revectoring each in-turn.
|
2007-06-29 10:45:24 +08:00
|
|
|
size_t PI = 0;
|
2006-10-29 01:32:47 +08:00
|
|
|
bool DidChange = false;
|
|
|
|
bool HasBranchToSelf = false;
|
2007-06-29 10:45:24 +08:00
|
|
|
while(PI != MBB->pred_size()) {
|
|
|
|
MachineBasicBlock *PMBB = *(MBB->pred_begin() + PI);
|
|
|
|
if (PMBB == MBB) {
|
2006-10-29 01:32:47 +08:00
|
|
|
// If this block has an uncond branch to itself, leave it.
|
|
|
|
++PI;
|
|
|
|
HasBranchToSelf = true;
|
|
|
|
} else {
|
|
|
|
DidChange = true;
|
2007-06-29 10:45:24 +08:00
|
|
|
PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB);
|
2009-05-12 05:54:13 +08:00
|
|
|
// If this change resulted in PMBB ending in a conditional
|
|
|
|
// branch where both conditions go to the same destination,
|
|
|
|
// change this to an unconditional branch (and fix the CFG).
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *NewCurTBB = nullptr, *NewCurFBB = nullptr;
|
2009-05-12 05:54:13 +08:00
|
|
|
SmallVector<MachineOperand, 4> NewCurCond;
|
2016-07-15 22:41:04 +08:00
|
|
|
bool NewCurUnAnalyzable = TII->analyzeBranch(
|
|
|
|
*PMBB, NewCurTBB, NewCurFBB, NewCurCond, true);
|
2009-05-12 05:54:13 +08:00
|
|
|
if (!NewCurUnAnalyzable && NewCurTBB && NewCurTBB == NewCurFBB) {
|
2012-03-07 16:49:42 +08:00
|
|
|
DebugLoc pdl = getBranchDebugLoc(*PMBB);
|
2016-09-15 04:43:16 +08:00
|
|
|
TII->removeBranch(*PMBB);
|
2009-11-12 02:38:14 +08:00
|
|
|
NewCurCond.clear();
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(*PMBB, NewCurTBB, nullptr, NewCurCond, pdl);
|
2009-05-12 05:54:13 +08:00
|
|
|
MadeChange = true;
|
|
|
|
++NumBranchOpts;
|
2014-04-14 08:51:57 +08:00
|
|
|
PMBB->CorrectExtraCFGEdges(NewCurTBB, nullptr, false);
|
2009-05-12 05:54:13 +08:00
|
|
|
}
|
2006-10-29 01:32:47 +08:00
|
|
|
}
|
2006-10-21 14:11:43 +08:00
|
|
|
}
|
2004-07-31 18:01:27 +08:00
|
|
|
|
2006-10-29 01:32:47 +08:00
|
|
|
// Change any jumptables to go to the new MBB.
|
2010-01-26 07:26:13 +08:00
|
|
|
if (MachineJumpTableInfo *MJTI = MF.getJumpTableInfo())
|
|
|
|
MJTI->ReplaceMBBInJumpTables(MBB, CurTBB);
|
2006-10-29 01:32:47 +08:00
|
|
|
if (DidChange) {
|
|
|
|
++NumBranchOpts;
|
|
|
|
MadeChange = true;
|
2009-09-04 15:47:40 +08:00
|
|
|
if (!HasBranchToSelf) return MadeChange;
|
2006-10-29 01:32:47 +08:00
|
|
|
}
|
2006-10-21 14:11:43 +08:00
|
|
|
}
|
2004-07-31 18:01:27 +08:00
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2006-10-21 13:08:28 +08:00
|
|
|
// Add the branch back if the block is more than just an uncond branch.
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(*MBB, CurTBB, nullptr, CurCond, dl);
|
2004-07-31 18:01:27 +08:00
|
|
|
}
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 08:39:24 +08:00
|
|
|
// If the prior block doesn't fall through into this block, and if this
|
|
|
|
// block doesn't fall through into some other block, see if we can find a
|
|
|
|
// place to move this block where a fall-through will happen.
|
|
|
|
if (!PrevBB.canFallThrough()) {
|
2009-11-18 01:06:18 +08:00
|
|
|
// Now we know that there was no fall-through into this block, check to
|
|
|
|
// see if it has a fall-through into its successor.
|
2009-11-26 08:32:21 +08:00
|
|
|
bool CurFallsThru = MBB->canFallThrough();
|
2009-11-18 01:06:18 +08:00
|
|
|
|
2015-08-28 07:27:47 +08:00
|
|
|
if (!MBB->isEHPad()) {
|
2007-02-22 06:42:20 +08:00
|
|
|
// Check all the predecessors of this block. If one of them has no fall
|
|
|
|
// throughs, move this block right after it.
|
2015-09-16 11:52:35 +08:00
|
|
|
for (MachineBasicBlock *PredBB : MBB->predecessors()) {
|
2007-02-22 06:42:20 +08:00
|
|
|
// Analyze the branch at the end of the pred.
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
|
2009-11-12 03:48:59 +08:00
|
|
|
SmallVector<MachineOperand, 4> PredCond;
|
2009-12-15 08:39:24 +08:00
|
|
|
if (PredBB != MBB && !PredBB->canFallThrough() &&
|
2016-07-15 22:41:04 +08:00
|
|
|
!TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true) &&
|
|
|
|
(!CurFallsThru || !CurTBB || !CurFBB) &&
|
|
|
|
(!CurFallsThru || MBB->getNumber() >= PredBB->getNumber())) {
|
2009-12-15 08:39:24 +08:00
|
|
|
// If the current block doesn't fall through, just move it.
|
|
|
|
// If the current block can fall through and does not end with a
|
|
|
|
// conditional branch, we need to append an unconditional jump to
|
|
|
|
// the (current) next block. To avoid a possible compile-time
|
|
|
|
// infinite loop, move blocks only backward in this case.
|
|
|
|
// Also, if there are already 2 branches here, we cannot add a third;
|
|
|
|
// this means we have the case
|
|
|
|
// Bcc next
|
|
|
|
// B elsewhere
|
|
|
|
// next:
|
2007-02-22 06:42:20 +08:00
|
|
|
if (CurFallsThru) {
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineBasicBlock *NextBB = &*std::next(MBB->getIterator());
|
2007-02-22 06:42:20 +08:00
|
|
|
CurCond.clear();
|
2016-09-15 01:24:15 +08:00
|
|
|
TII->insertBranch(*MBB, NextBB, nullptr, CurCond, DebugLoc());
|
2007-02-22 06:42:20 +08:00
|
|
|
}
|
|
|
|
MBB->moveAfter(PredBB);
|
|
|
|
MadeChange = true;
|
2009-11-12 03:48:59 +08:00
|
|
|
goto ReoptimizeBlock;
|
2006-10-24 09:12:32 +08:00
|
|
|
}
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
}
|
2007-02-17 08:44:34 +08:00
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2007-02-17 08:44:34 +08:00
|
|
|
if (!CurFallsThru) {
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
// Check all successors to see if we can move this block before it.
|
2015-09-16 11:52:35 +08:00
|
|
|
for (MachineBasicBlock *SuccBB : MBB->successors()) {
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
// Analyze the branch at the end of the block before the succ.
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineFunction::iterator SuccPrev = --SuccBB->getIterator();
|
2009-11-12 02:38:14 +08:00
|
|
|
|
2007-05-01 07:35:00 +08:00
|
|
|
// If this block doesn't already fall-through to that successor, and if
|
|
|
|
// the succ doesn't already have a block that can fall through into it,
|
|
|
|
// and if the successor isn't an EH destination, we can arrange for the
|
|
|
|
// fallthrough to happen.
|
2009-11-12 03:48:59 +08:00
|
|
|
if (SuccBB != MBB && &*SuccPrev != MBB &&
|
2009-11-26 08:32:21 +08:00
|
|
|
!SuccPrev->canFallThrough() && !CurUnAnalyzable &&
|
2015-08-28 07:27:47 +08:00
|
|
|
!SuccBB->isEHPad()) {
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
MBB->moveBefore(SuccBB);
|
2006-10-24 09:12:32 +08:00
|
|
|
MadeChange = true;
|
2009-11-12 03:48:59 +08:00
|
|
|
goto ReoptimizeBlock;
|
2006-10-24 09:12:32 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-12 02:38:14 +08:00
|
|
|
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
// Okay, there is no really great place to put this block. If, however,
|
|
|
|
// the block before this one would be a fall-through if this block were
|
2016-12-13 07:05:38 +08:00
|
|
|
// removed, move this block to the end of the function. There is no real
|
|
|
|
// advantage in "falling through" to an EH block, so we don't want to
|
|
|
|
// perform this transformation for that case.
|
|
|
|
//
|
|
|
|
// Also, Windows EH introduced the possibility of an arbitrary number of
|
|
|
|
// successors to a given block. The analyzeBranch call does not consider
|
|
|
|
// exception handling and so we can get in a state where a block
|
|
|
|
// containing a call is followed by multiple EH blocks that would be
|
|
|
|
// rotated infinitely at the end of the function if the transformation
|
|
|
|
// below were performed for EH "FallThrough" blocks. Therefore, even if
|
|
|
|
// that appears not to be happening anymore, we should assume that it is
|
|
|
|
// possible and not remove the "!FallThrough()->isEHPad" condition below.
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *PrevTBB = nullptr, *PrevFBB = nullptr;
|
2009-11-12 03:48:59 +08:00
|
|
|
SmallVector<MachineOperand, 4> PrevCond;
|
2009-11-12 02:18:34 +08:00
|
|
|
if (FallThrough != MF.end() &&
|
2016-12-13 07:05:38 +08:00
|
|
|
!FallThrough->isEHPad() &&
|
2016-07-15 22:41:04 +08:00
|
|
|
!TII->analyzeBranch(PrevBB, PrevTBB, PrevFBB, PrevCond, true) &&
|
2015-10-10 02:23:49 +08:00
|
|
|
PrevBB.isSuccessor(&*FallThrough)) {
|
|
|
|
MBB->moveAfter(&MF.back());
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
MadeChange = true;
|
2009-09-04 15:47:40 +08:00
|
|
|
return MadeChange;
|
Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
successors), and make island block movement more general.
This compiles CodeGen/X86/2006-04-27-ISelFoldingBug.ll to:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
je LBB1_2 #codeRepl5.exitStub
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
instead of:
_loadAndRLEsource_no_exit_2E_1_label_2E_0:
subl $8, %esp
movl %esi, 4(%esp)
movl %ebx, (%esp)
movl 16(%esp), %eax
movl 12(%esp), %ecx
jmp LBB1_3 #label.0
LBB1_1: #label.0.no_exit.1_crit_edge.exitStub
movl $1, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_2: #codeRepl5.exitStub
xorl %eax, %eax
movl (%esp), %ebx
movl 4(%esp), %esi
addl $8, %esp
ret
LBB1_3: #label.0
movl _last, %edx
movl %edx, %esi
incl %esi
movl %esi, _last
movl %ecx, %ebx
# TRUNCATE movb %bl, %bl
movl _block, %esi
movb %bl, 1(%esi,%edx)
cmpl %eax, _last
jge LBB1_2 #codeRepl5.exitStub
LBB1_4: #label.0
cmpl $257, %ecx
jne LBB1_1 #label.0.no_exit.1_crit_edge.exitStub
jmp LBB1_2 #codeRepl5.exitStub
... which is much better layout :)
llvm-svn: 31282
2006-10-30 05:05:41 +08:00
|
|
|
}
|
2006-10-24 09:12:32 +08:00
|
|
|
}
|
2004-07-31 18:01:27 +08:00
|
|
|
}
|
2009-09-04 15:47:40 +08:00
|
|
|
|
|
|
|
return MadeChange;
|
2004-07-31 18:01:27 +08:00
|
|
|
}
|
2011-05-12 08:56:58 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Hoist Common Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool BranchFolder::HoistCommonCode(MachineFunction &MF) {
|
|
|
|
bool MadeChange = false;
|
|
|
|
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ) {
|
2015-10-10 02:23:49 +08:00
|
|
|
MachineBasicBlock *MBB = &*I++;
|
2011-05-12 08:56:58 +08:00
|
|
|
MadeChange |= HoistCommonCodeInSuccs(MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// findFalseBlock - BB has a fallthrough. Find its 'false' successor given
|
|
|
|
/// its 'true' successor.
|
|
|
|
static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
|
|
|
|
MachineBasicBlock *TrueBB) {
|
2015-09-16 11:52:35 +08:00
|
|
|
for (MachineBasicBlock *SuccBB : BB->successors())
|
2011-05-12 08:56:58 +08:00
|
|
|
if (SuccBB != TrueBB)
|
|
|
|
return SuccBB;
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2011-05-12 08:56:58 +08:00
|
|
|
}
|
|
|
|
|
2015-07-30 02:59:09 +08:00
|
|
|
template <class Container>
|
|
|
|
static void addRegAndItsAliases(unsigned Reg, const TargetRegisterInfo *TRI,
|
|
|
|
Container &Set) {
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isPhysicalRegister(Reg)) {
|
2015-07-30 02:59:09 +08:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
Set.insert(*AI);
|
|
|
|
} else {
|
|
|
|
Set.insert(Reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-12 08:56:58 +08:00
|
|
|
/// findHoistingInsertPosAndDeps - Find the location to move common instructions
|
2012-06-02 18:20:22 +08:00
|
|
|
/// in successors to. The location is usually just before the terminator,
|
2011-05-12 08:56:58 +08:00
|
|
|
/// however if the terminator is a conditional branch and its previous
|
|
|
|
/// instruction is the flag setting instruction, the previous instruction is
|
|
|
|
/// the preferred location. This function also gathers uses and defs of the
|
|
|
|
/// instructions from the insertion point to the end of the block. The data is
|
|
|
|
/// used by HoistCommonCodeInSuccs to ensure safety.
|
|
|
|
static
|
|
|
|
MachineBasicBlock::iterator findHoistingInsertPosAndDeps(MachineBasicBlock *MBB,
|
|
|
|
const TargetInstrInfo *TII,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
SmallSet<unsigned,4> &Uses,
|
|
|
|
SmallSet<unsigned,4> &Defs) {
|
|
|
|
MachineBasicBlock::iterator Loc = MBB->getFirstTerminator();
|
2016-02-23 10:46:52 +08:00
|
|
|
if (!TII->isUnpredicatedTerminator(*Loc))
|
2011-05-12 08:56:58 +08:00
|
|
|
return MBB->end();
|
|
|
|
|
2015-09-16 11:52:35 +08:00
|
|
|
for (const MachineOperand &MO : Loc->operands()) {
|
2011-05-12 08:56:58 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2011-05-12 08:56:58 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (MO.isUse()) {
|
2015-07-30 02:59:09 +08:00
|
|
|
addRegAndItsAliases(Reg, TRI, Uses);
|
2014-06-05 21:42:48 +08:00
|
|
|
} else {
|
|
|
|
if (!MO.isDead())
|
|
|
|
// Don't try to hoist code in the rare case the terminator defines a
|
|
|
|
// register that is later used.
|
|
|
|
return MBB->end();
|
|
|
|
|
|
|
|
// If the terminator defines a register, make sure we don't hoist
|
|
|
|
// the instruction whose def might be clobbered by the terminator.
|
2015-07-30 02:59:09 +08:00
|
|
|
addRegAndItsAliases(Reg, TRI, Defs);
|
2014-06-05 21:42:48 +08:00
|
|
|
}
|
2011-05-12 08:56:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Uses.empty())
|
|
|
|
return Loc;
|
2018-05-15 01:31:18 +08:00
|
|
|
// If the terminator is the only instruction in the block and Uses is not
|
|
|
|
// empty (or we would have returned above), we can still safely hoist
|
|
|
|
// instructions just before the terminator as long as the Defs/Uses are not
|
|
|
|
// violated (which is checked in HoistCommonCodeInSuccs).
|
2011-05-12 08:56:58 +08:00
|
|
|
if (Loc == MBB->begin())
|
2018-05-15 01:31:18 +08:00
|
|
|
return Loc;
|
2011-05-12 08:56:58 +08:00
|
|
|
|
|
|
|
// The terminator is probably a conditional branch, try not to separate the
|
|
|
|
// branch from condition setting instruction.
|
2016-12-16 19:10:26 +08:00
|
|
|
MachineBasicBlock::iterator PI =
|
|
|
|
skipDebugInstructionsBackward(std::prev(Loc), MBB->begin());
|
2011-05-12 08:56:58 +08:00
|
|
|
|
|
|
|
bool IsDef = false;
|
2015-09-16 11:52:35 +08:00
|
|
|
for (const MachineOperand &MO : PI->operands()) {
|
2012-02-16 07:42:54 +08:00
|
|
|
// If PI has a regmask operand, it is probably a call. Separate away.
|
|
|
|
if (MO.isRegMask())
|
|
|
|
return Loc;
|
2011-05-12 08:56:58 +08:00
|
|
|
if (!MO.isReg() || MO.isUse())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2011-05-12 08:56:58 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
2015-09-16 11:52:35 +08:00
|
|
|
if (Uses.count(Reg)) {
|
2011-05-12 08:56:58 +08:00
|
|
|
IsDef = true;
|
2015-09-16 11:52:35 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-05-12 08:56:58 +08:00
|
|
|
}
|
|
|
|
if (!IsDef)
|
|
|
|
// The condition setting instruction is not just before the conditional
|
|
|
|
// branch.
|
|
|
|
return Loc;
|
|
|
|
|
|
|
|
// Be conservative, don't insert instruction above something that may have
|
|
|
|
// side-effects. And since it's potentially bad to separate flag setting
|
|
|
|
// instruction from the conditional branch, just abort the optimization
|
|
|
|
// completely.
|
|
|
|
// Also avoid moving code above predicated instruction since it's hard to
|
|
|
|
// reason about register liveness with predicated instruction.
|
|
|
|
bool DontMoveAcrossStore = true;
|
2016-02-23 10:46:52 +08:00
|
|
|
if (!PI->isSafeToMove(nullptr, DontMoveAcrossStore) || TII->isPredicated(*PI))
|
2011-05-12 08:56:58 +08:00
|
|
|
return MBB->end();
|
|
|
|
|
|
|
|
// Find out what registers are live. Note this routine is ignoring other live
|
|
|
|
// registers which are only used by instructions in successor blocks.
|
2015-09-16 11:52:35 +08:00
|
|
|
for (const MachineOperand &MO : PI->operands()) {
|
2011-05-12 08:56:58 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2011-05-12 08:56:58 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (MO.isUse()) {
|
2015-07-30 02:59:09 +08:00
|
|
|
addRegAndItsAliases(Reg, TRI, Uses);
|
2011-05-12 08:56:58 +08:00
|
|
|
} else {
|
2012-08-22 23:37:57 +08:00
|
|
|
if (Uses.erase(Reg)) {
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isPhysicalRegister(Reg)) {
|
2015-07-30 02:59:09 +08:00
|
|
|
for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
|
|
|
|
Uses.erase(*SubRegs); // Use sub-registers to be conservative
|
|
|
|
}
|
2011-05-12 08:56:58 +08:00
|
|
|
}
|
2015-07-30 02:59:09 +08:00
|
|
|
addRegAndItsAliases(Reg, TRI, Defs);
|
2011-05-12 08:56:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PI;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) {
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2011-05-12 08:56:58 +08:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2016-07-15 22:41:04 +08:00
|
|
|
if (TII->analyzeBranch(*MBB, TBB, FBB, Cond, true) || !TBB || Cond.empty())
|
2011-05-12 08:56:58 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!FBB) FBB = findFalseBlock(MBB, TBB);
|
|
|
|
if (!FBB)
|
|
|
|
// Malformed bcc? True and false blocks are the same?
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Restrict the optimization to cases where MBB is the only predecessor,
|
|
|
|
// it is an obvious win.
|
|
|
|
if (TBB->pred_size() > 1 || FBB->pred_size() > 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find a suitable position to hoist the common instructions to. Also figure
|
|
|
|
// out which registers are used or defined by instructions from the insertion
|
|
|
|
// point to the end of the block.
|
|
|
|
SmallSet<unsigned, 4> Uses, Defs;
|
|
|
|
MachineBasicBlock::iterator Loc =
|
|
|
|
findHoistingInsertPosAndDeps(MBB, TII, TRI, Uses, Defs);
|
|
|
|
if (Loc == MBB->end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool HasDups = false;
|
2017-05-05 20:20:07 +08:00
|
|
|
SmallSet<unsigned, 4> ActiveDefsSet, AllDefsSet;
|
2011-05-12 08:56:58 +08:00
|
|
|
MachineBasicBlock::iterator TIB = TBB->begin();
|
|
|
|
MachineBasicBlock::iterator FIB = FBB->begin();
|
|
|
|
MachineBasicBlock::iterator TIE = TBB->end();
|
|
|
|
MachineBasicBlock::iterator FIE = FBB->end();
|
|
|
|
while (TIB != TIE && FIB != FIE) {
|
|
|
|
// Skip dbg_value instructions. These do not count.
|
2016-12-16 19:10:26 +08:00
|
|
|
TIB = skipDebugInstructionsForward(TIB, TIE);
|
|
|
|
FIB = skipDebugInstructionsForward(FIB, FIE);
|
|
|
|
if (TIB == TIE || FIB == FIE)
|
|
|
|
break;
|
|
|
|
|
2016-02-28 04:01:33 +08:00
|
|
|
if (!TIB->isIdenticalTo(*FIB, MachineInstr::CheckKillDead))
|
2011-05-12 08:56:58 +08:00
|
|
|
break;
|
|
|
|
|
2016-02-23 10:46:52 +08:00
|
|
|
if (TII->isPredicated(*TIB))
|
2011-05-12 08:56:58 +08:00
|
|
|
// Hard to reason about register liveness with predicated instruction.
|
|
|
|
break;
|
|
|
|
|
|
|
|
bool IsSafe = true;
|
2015-09-16 11:52:35 +08:00
|
|
|
for (MachineOperand &MO : TIB->operands()) {
|
2012-02-16 07:42:54 +08:00
|
|
|
// Don't attempt to hoist instructions with register masks.
|
|
|
|
if (MO.isRegMask()) {
|
|
|
|
IsSafe = false;
|
|
|
|
break;
|
|
|
|
}
|
2011-05-12 08:56:58 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2011-05-12 08:56:58 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (MO.isDef()) {
|
|
|
|
if (Uses.count(Reg)) {
|
|
|
|
// Avoid clobbering a register that's used by the instruction at
|
|
|
|
// the point of insertion.
|
|
|
|
IsSafe = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Defs.count(Reg) && !MO.isDead()) {
|
|
|
|
// Don't hoist the instruction if the def would be clobber by the
|
|
|
|
// instruction at the point insertion. FIXME: This is overly
|
|
|
|
// conservative. It should be possible to hoist the instructions
|
|
|
|
// in BB2 in the following example:
|
|
|
|
// BB1:
|
|
|
|
// r1, eflag = op1 r2, r3
|
|
|
|
// brcc eflag
|
|
|
|
//
|
|
|
|
// BB2:
|
|
|
|
// r1 = op2, ...
|
2017-12-07 18:40:31 +08:00
|
|
|
// = op3, killed r1
|
2011-05-12 08:56:58 +08:00
|
|
|
IsSafe = false;
|
|
|
|
break;
|
|
|
|
}
|
2017-05-05 20:20:07 +08:00
|
|
|
} else if (!ActiveDefsSet.count(Reg)) {
|
2011-05-12 08:56:58 +08:00
|
|
|
if (Defs.count(Reg)) {
|
|
|
|
// Use is defined by the instruction at the point of insertion.
|
|
|
|
IsSafe = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-13 04:31:24 +08:00
|
|
|
|
|
|
|
if (MO.isKill() && Uses.count(Reg))
|
|
|
|
// Kills a register that's read by the instruction at the point of
|
|
|
|
// insertion. Remove the kill marker.
|
|
|
|
MO.setIsKill(false);
|
2011-05-12 08:56:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!IsSafe)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bool DontMoveAcrossStore = true;
|
2015-05-20 05:22:20 +08:00
|
|
|
if (!TIB->isSafeToMove(nullptr, DontMoveAcrossStore))
|
2011-05-12 08:56:58 +08:00
|
|
|
break;
|
|
|
|
|
2017-05-05 20:20:07 +08:00
|
|
|
// Remove kills from ActiveDefsSet, these registers had short live ranges.
|
2015-09-16 11:52:35 +08:00
|
|
|
for (const MachineOperand &MO : TIB->operands()) {
|
Fix liveness computations in BranchFolding.
The old code would look at kills and defs in one pass over the
instruction operands, causing problems with this code:
%R0<def>, %CPSR<def,dead> = tLSLri %R5<kill>, 2, pred:14, pred:%noreg
%R0<def>, %CPSR<def,dead> = tADDrr %R4<kill>, %R0<kill>, pred:14, %pred:%noreg
The last instruction kills and redefines %R0, so it is still live after
the instruction.
This caused a register scavenger crash when compiling 483.xalancbmk for
armv6. I am not including a test case because it requires too much bad
luck to expose this old bug.
First you need to convince the register allocator to use %R0 twice on
the tADDrr instruction, then you have to convince BranchFolding to do
something that causes it to run the register scavenger on he bad block.
<rdar://problem/9898200>
llvm-svn: 136973
2011-08-06 02:47:07 +08:00
|
|
|
if (!MO.isReg() || !MO.isUse() || !MO.isKill())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2017-05-05 20:20:07 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (!AllDefsSet.count(Reg)) {
|
Fix liveness computations in BranchFolding.
The old code would look at kills and defs in one pass over the
instruction operands, causing problems with this code:
%R0<def>, %CPSR<def,dead> = tLSLri %R5<kill>, 2, pred:14, pred:%noreg
%R0<def>, %CPSR<def,dead> = tADDrr %R4<kill>, %R0<kill>, pred:14, %pred:%noreg
The last instruction kills and redefines %R0, so it is still live after
the instruction.
This caused a register scavenger crash when compiling 483.xalancbmk for
armv6. I am not including a test case because it requires too much bad
luck to expose this old bug.
First you need to convince the register allocator to use %R0 twice on
the tADDrr instruction, then you have to convince BranchFolding to do
something that causes it to run the register scavenger on he bad block.
<rdar://problem/9898200>
llvm-svn: 136973
2011-08-06 02:47:07 +08:00
|
|
|
continue;
|
2017-05-05 20:20:07 +08:00
|
|
|
}
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isPhysicalRegister(Reg)) {
|
2015-07-30 02:59:09 +08:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
2017-05-05 20:20:07 +08:00
|
|
|
ActiveDefsSet.erase(*AI);
|
2015-07-30 02:59:09 +08:00
|
|
|
} else {
|
2017-05-05 20:20:07 +08:00
|
|
|
ActiveDefsSet.erase(Reg);
|
2015-07-30 02:59:09 +08:00
|
|
|
}
|
Fix liveness computations in BranchFolding.
The old code would look at kills and defs in one pass over the
instruction operands, causing problems with this code:
%R0<def>, %CPSR<def,dead> = tLSLri %R5<kill>, 2, pred:14, pred:%noreg
%R0<def>, %CPSR<def,dead> = tADDrr %R4<kill>, %R0<kill>, pred:14, %pred:%noreg
The last instruction kills and redefines %R0, so it is still live after
the instruction.
This caused a register scavenger crash when compiling 483.xalancbmk for
armv6. I am not including a test case because it requires too much bad
luck to expose this old bug.
First you need to convince the register allocator to use %R0 twice on
the tADDrr instruction, then you have to convince BranchFolding to do
something that causes it to run the register scavenger on he bad block.
<rdar://problem/9898200>
llvm-svn: 136973
2011-08-06 02:47:07 +08:00
|
|
|
}
|
|
|
|
|
2011-05-13 04:30:01 +08:00
|
|
|
// Track local defs so we can update liveins.
|
2015-09-16 11:52:35 +08:00
|
|
|
for (const MachineOperand &MO : TIB->operands()) {
|
Fix liveness computations in BranchFolding.
The old code would look at kills and defs in one pass over the
instruction operands, causing problems with this code:
%R0<def>, %CPSR<def,dead> = tLSLri %R5<kill>, 2, pred:14, pred:%noreg
%R0<def>, %CPSR<def,dead> = tADDrr %R4<kill>, %R0<kill>, pred:14, %pred:%noreg
The last instruction kills and redefines %R0, so it is still live after
the instruction.
This caused a register scavenger crash when compiling 483.xalancbmk for
armv6. I am not including a test case because it requires too much bad
luck to expose this old bug.
First you need to convince the register allocator to use %R0 twice on
the tADDrr instruction, then you have to convince BranchFolding to do
something that causes it to run the register scavenger on he bad block.
<rdar://problem/9898200>
llvm-svn: 136973
2011-08-06 02:47:07 +08:00
|
|
|
if (!MO.isReg() || !MO.isDef() || MO.isDead())
|
2011-05-13 04:30:01 +08:00
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Reg || Register::isVirtualRegister(Reg))
|
2011-05-13 04:30:01 +08:00
|
|
|
continue;
|
2017-05-05 20:20:07 +08:00
|
|
|
addRegAndItsAliases(Reg, TRI, ActiveDefsSet);
|
|
|
|
addRegAndItsAliases(Reg, TRI, AllDefsSet);
|
2011-05-13 04:30:01 +08:00
|
|
|
}
|
|
|
|
|
2012-02-23 01:25:00 +08:00
|
|
|
HasDups = true;
|
2011-05-12 08:56:58 +08:00
|
|
|
++TIB;
|
|
|
|
++FIB;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasDups)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MBB->splice(Loc, TBB, TBB->begin(), TIB);
|
|
|
|
FBB->erase(FBB->begin(), FIB);
|
2011-05-13 04:30:01 +08:00
|
|
|
|
2018-06-07 15:20:33 +08:00
|
|
|
if (UpdateLiveIns) {
|
|
|
|
recomputeLiveIns(*TBB);
|
|
|
|
recomputeLiveIns(*FBB);
|
2016-09-13 00:05:31 +08:00
|
|
|
}
|
|
|
|
|
2011-05-12 08:56:58 +08:00
|
|
|
++NumHoist;
|
|
|
|
return true;
|
|
|
|
}
|