31#define DEBUG_TYPE "msp430-branch-select"
33STATISTIC(NumSplit,
"Number of machine basic blocks split");
34STATISTIC(NumExpanded,
"Number of branches expanded to long format");
44 unsigned measureFunction(OffsetVector &BlockOffsets,
46 bool expandBranches(OffsetVector &BlockOffsets);
55 MSP430BranchSelectLegacyPass() : MachineFunctionPass(ID) {}
59 MachineFunctionProperties getRequiredProperties()
const override {
60 return MachineFunctionProperties().setNoVRegs();
63 StringRef getPassName()
const override {
return "MSP430 Branch Selector"; }
66char MSP430BranchSelectLegacyPass::ID = 0;
74 const int WordSize = 2;
76 assert((DistanceInBytes % WordSize == 0) &&
77 "Branch offset should be word aligned!");
79 int Words = DistanceInBytes / WordSize;
85unsigned MSP430BSelImpl::measureFunction(OffsetVector &BlockOffsets,
91 if (FromBB ==
nullptr) {
99 unsigned TotalSize = BlockOffsets[Begin->getNumber()];
102 for (MachineInstr &
MI :
MBB) {
103 TotalSize +=
TII->getInstSizeInBytes(
MI);
111bool MSP430BSelImpl::expandBranches(OffsetVector &BlockOffsets) {
121 bool MadeChange =
false;
123 unsigned MBBStartOffset = 0;
125 MBBStartOffset +=
TII->getInstSizeInBytes(*
MI);
128 if (
MI->getOpcode() != MSP430::JCC &&
MI->getOpcode() != MSP430::JMP) {
132 MachineBasicBlock *DestBB =
MI->getOperand(0).getMBB();
138 int BranchDistance = BlockDistance - MBBStartOffset;
147 << BranchDistance <<
"\n");
150 if (
MI->getOpcode() == MSP430::JCC && std::next(
MI) != EE) {
152 LLVM_DEBUG(
dbgs() <<
" Found a basic block that needs to be split, "
156 MachineBasicBlock *NewBB =
165 if (Succ == DestBB) {
174 measureFunction(BlockOffsets, &*
MBB);
183 MachineInstr &OldBranch = *
MI;
185 int InstrSizeDiff = -
TII->getInstSizeInBytes(OldBranch);
187 if (
MI->getOpcode() == MSP430::JCC) {
188 MachineBasicBlock *NextMBB = &*std::next(
MBB);
190 "This block must have a layout successor!");
203 InstrSizeDiff +=
TII->getInstSizeInBytes(*
MI);
209 InstrSizeDiff +=
TII->getInstSizeInBytes(*
MI);
216 for (
int i =
MBB->
getNumber() + 1, e = BlockOffsets.size(); i < e; ++i) {
217 BlockOffsets[i] += InstrSizeDiff;
219 MBBStartOffset += InstrSizeDiff;
230 TII =
static_cast<const MSP430InstrInfo *
>(MF->
getSubtarget().getInstrInfo());
233 if (!MF->
getSubtarget<MSP430Subtarget>().getCLOpts().branch_select)
240 OffsetVector BlockOffsets;
242 unsigned FunctionSize = measureFunction(BlockOffsets);
251 bool MadeChange =
false;
252 while (expandBranches(BlockOffsets))
258bool MSP430BranchSelectLegacyPass::runOnMachineFunction(
MachineFunction &MF) {
259 return MSP430BSelImpl().runOnMachineFunction(MF);
265 return MSP430BSelImpl().runOnMachineFunction(MF)
272 return new MSP430BranchSelectLegacyPass();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
static bool isInRage(int DistanceInBytes)
const SmallVectorImpl< MachineOperand > & Cond
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
FunctionPass class - This class is used to implement most global optimizations.
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
Reverses the branch condition of the specified condition list, returning false on success and true if...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
iterator_range< succ_iterator > successors()
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
BasicBlockListType::iterator iterator
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
self_iterator getIterator()
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FunctionPass * createMSP430BranchSelectLegacyPass()
Returns an instance of the Branch Selection Pass.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.