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);
61char MSP430BSel::ID = 0;
69 const int WordSize = 2;
71 assert((DistanceInBytes % WordSize == 0) &&
72 "Branch offset should be word aligned!");
74 int Words = DistanceInBytes / WordSize;
75 return isInt<10>(Words);
80unsigned MSP430BSel::measureFunction(OffsetVector &BlockOffsets,
83 MF->RenumberBlocks(FromBB);
86 if (FromBB ==
nullptr) {
92 BlockOffsets.resize(MF->getNumBlockIDs());
94 unsigned TotalSize = BlockOffsets[Begin->getNumber()];
98 TotalSize +=
TII->getInstSizeInBytes(
MI);
106bool MSP430BSel::expandBranches(OffsetVector &BlockOffsets) {
116 bool MadeChange =
false;
118 unsigned MBBStartOffset = 0;
120 MBBStartOffset +=
TII->getInstSizeInBytes(*
MI);
123 if (
MI->getOpcode() != MSP430::JCC &&
MI->getOpcode() != MSP430::JMP) {
133 int BranchDistance = BlockDistance - MBBStartOffset;
142 << BranchDistance <<
"\n");
145 if (
MI->getOpcode() == MSP430::JCC && std::next(
MI) != EE) {
147 LLVM_DEBUG(
dbgs() <<
" Found a basic block that needs to be split, "
160 if (Succ == DestBB) {
169 measureFunction(BlockOffsets, &*
MBB);
180 int InstrSizeDiff = -
TII->getInstSizeInBytes(OldBranch);
182 if (
MI->getOpcode() == MSP430::JCC) {
185 "This block must have a layout successor!");
191 Cond.push_back(
MI->getOperand(1));
198 InstrSizeDiff +=
TII->getInstSizeInBytes(*
MI);
204 InstrSizeDiff +=
TII->getInstSizeInBytes(*
MI);
211 for (
int i =
MBB->
getNumber() + 1, e = BlockOffsets.size(); i < e; ++i) {
212 BlockOffsets[i] += InstrSizeDiff;
214 MBBStartOffset += InstrSizeDiff;
231 LLVM_DEBUG(
dbgs() <<
"\n********** " << getPassName() <<
" **********\n");
235 OffsetVector BlockOffsets;
237 unsigned FunctionSize = measureFunction(BlockOffsets);
246 bool MadeChange =
false;
247 while (expandBranches(BlockOffsets))
255 return new MSP430BSel();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
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...
LLVM_ABI void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
LLVM_ABI 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.
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...
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.
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.
LLVM_ABI 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.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.