28#define DEBUG_TYPE "msp430-branch-select"
32 cl::desc(
"Expand out of range branches"));
34STATISTIC(NumSplit,
"Number of machine basic blocks split");
35STATISTIC(NumExpanded,
"Number of branches expanded to long format");
45 unsigned measureFunction(OffsetVector &BlockOffsets,
47 bool expandBranches(OffsetVector &BlockOffsets);
57 MachineFunctionProperties::Property::NoVRegs);
62char MSP430BSel::ID = 0;
70 const int WordSize = 2;
72 assert((DistanceInBytes % WordSize == 0) &&
73 "Branch offset should be word aligned!");
75 int Words = DistanceInBytes / WordSize;
76 return isInt<10>(Words);
81unsigned MSP430BSel::measureFunction(OffsetVector &BlockOffsets,
84 MF->RenumberBlocks(FromBB);
87 if (FromBB ==
nullptr) {
93 BlockOffsets.resize(MF->getNumBlockIDs());
95 unsigned TotalSize = BlockOffsets[Begin->getNumber()];
99 TotalSize +=
TII->getInstSizeInBytes(
MI);
107bool MSP430BSel::expandBranches(OffsetVector &BlockOffsets) {
117 bool MadeChange =
false;
119 unsigned MBBStartOffset = 0;
121 MBBStartOffset +=
TII->getInstSizeInBytes(*
MI);
124 if (
MI->getOpcode() != MSP430::JCC &&
MI->getOpcode() != MSP430::JMP) {
134 int BranchDistance = BlockDistance - MBBStartOffset;
143 << BranchDistance <<
"\n");
146 if (
MI->getOpcode() == MSP430::JCC && std::next(
MI) != EE) {
148 LLVM_DEBUG(
dbgs() <<
" Found a basic block that needs to be split, "
161 if (Succ == DestBB) {
170 measureFunction(BlockOffsets, &*
MBB);
181 int InstrSizeDiff = -
TII->getInstSizeInBytes(OldBranch);
183 if (
MI->getOpcode() == MSP430::JCC) {
186 "This block must have a layout successor!");
192 Cond.push_back(
MI->getOperand(1));
199 InstrSizeDiff +=
TII->getInstSizeInBytes(*
MI);
205 InstrSizeDiff +=
TII->getInstSizeInBytes(*
MI);
212 for (
int i =
MBB->
getNumber() + 1, e = BlockOffsets.size(); i < e; ++i) {
213 BlockOffsets[i] += InstrSizeDiff;
215 MBBStartOffset += InstrSizeDiff;
232 LLVM_DEBUG(
dbgs() <<
"\n********** " << getPassName() <<
" **********\n");
236 OffsetVector BlockOffsets;
238 unsigned FunctionSize = measureFunction(BlockOffsets);
247 bool MadeChange =
false;
248 while (expandBranches(BlockOffsets))
256 return new MSP430BSel();
const HexagonInstrInfo * TII
static bool isInRage(int DistanceInBytes)
static cl::opt< bool > BranchSelectEnabled("msp430-branch-select", cl::Hidden, cl::init(true), cl::desc("Expand out of range branches"))
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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...
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
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.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
iterator_range< succ_iterator > successors()
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...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
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.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
FunctionPass * createMSP430BranchSelectionPass()
Returns an instance of the Branch Selection Pass.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.