ARM tidy up ARMConstantIsland.cpp.

No functional change, just tidy up the code and nomenclature a bit.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153347 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jim Grosbach
2012-03-23 23:07:03 +00:00
parent 7c0bc008f1
commit 7a46525056

View File

@@ -283,51 +283,52 @@ namespace {
} }
private: private:
void DoInitialPlacement(std::vector<MachineInstr*> &CPEMIs); void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs);
CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
unsigned getCPELogAlign(const MachineInstr *CPEMI); unsigned getCPELogAlign(const MachineInstr *CPEMI);
void JumpTableFunctionScan(); void scanFunctionJumpTables();
void InitialFunctionScan(const std::vector<MachineInstr*> &CPEMIs); void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs);
MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI); MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI);
void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB); void updateForInsertedWaterBlock(MachineBasicBlock *NewBB);
void AdjustBBOffsetsAfter(MachineBasicBlock *BB); void adjustBBOffsetsAfter(MachineBasicBlock *BB);
bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI); bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI);
int LookForExistingCPEntry(CPUser& U, unsigned UserOffset); int findInRangeCPEntry(CPUser& U, unsigned UserOffset);
bool LookForWater(CPUser&U, unsigned UserOffset, water_iterator &WaterIter); bool findAvailableWater(CPUser&U, unsigned UserOffset,
void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset, water_iterator &WaterIter);
void createNewWater(unsigned CPUserIndex, unsigned UserOffset,
MachineBasicBlock *&NewMBB); MachineBasicBlock *&NewMBB);
bool HandleConstantPoolUser(unsigned CPUserIndex); bool handleConstantPoolUser(unsigned CPUserIndex);
void RemoveDeadCPEMI(MachineInstr *CPEMI); void removeDeadCPEMI(MachineInstr *CPEMI);
bool RemoveUnusedCPEntries(); bool removeUnusedCPEntries();
bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset, bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
MachineInstr *CPEMI, unsigned Disp, bool NegOk, MachineInstr *CPEMI, unsigned Disp, bool NegOk,
bool DoDump = false); bool DoDump = false);
bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water, bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water,
CPUser &U, unsigned &Growth); CPUser &U, unsigned &Growth);
bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
bool FixUpImmediateBr(ImmBranch &Br); bool fixupImmediateBr(ImmBranch &Br);
bool FixUpConditionalBr(ImmBranch &Br); bool fixupConditionalBr(ImmBranch &Br);
bool FixUpUnconditionalBr(ImmBranch &Br); bool fixupUnconditionalBr(ImmBranch &Br);
bool UndoLRSpillRestore(); bool undoLRSpillRestore();
bool mayOptimizeThumb2Instruction(const MachineInstr *MI) const; bool mayOptimizeThumb2Instruction(const MachineInstr *MI) const;
bool OptimizeThumb2Instructions(); bool optimizeThumb2Instructions();
bool OptimizeThumb2Branches(); bool optimizeThumb2Branches();
bool ReorderThumb2JumpTables(); bool reorderThumb2JumpTables();
bool OptimizeThumb2JumpTables(); bool optimizeThumb2JumpTables();
MachineBasicBlock *AdjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *adjustJTTargetBlockForward(MachineBasicBlock *BB,
MachineBasicBlock *JTBB); MachineBasicBlock *JTBB);
void ComputeBlockSize(MachineBasicBlock *MBB); void computeBlockSize(MachineBasicBlock *MBB);
unsigned GetOffsetOf(MachineInstr *MI) const; unsigned getOffsetOf(MachineInstr *MI) const;
unsigned GetUserOffset(CPUser&) const; unsigned getUserOffset(CPUser&) const;
void dumpBBs(); void dumpBBs();
void verify(); void verify();
bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset, bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
unsigned Disp, bool NegativeOK, bool IsSoImm = false); unsigned Disp, bool NegativeOK, bool IsSoImm = false);
bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset, bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
const CPUser &U) { const CPUser &U) {
return OffsetIsInRange(UserOffset, TrialOffset, return isOffsetInRange(UserOffset, TrialOffset,
U.getMaxDisp(), U.NegOk, U.IsSoImm); U.getMaxDisp(), U.NegOk, U.IsSoImm);
} }
}; };
@@ -347,9 +348,9 @@ void ARMConstantIslands::verify() {
} }
for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) {
CPUser &U = CPUsers[i]; CPUser &U = CPUsers[i];
unsigned UserOffset = GetUserOffset(U); unsigned UserOffset = getUserOffset(U);
assert(CPEIsInRange(U.MI, UserOffset, U.CPEMI, U.getMaxDisp(), U.NegOk) && assert(isCPEntryInRange(U.MI, UserOffset, U.CPEMI, U.getMaxDisp(),
"Constant pool entry out of range!"); U.NegOk) && "Constant pool entry out of range!");
} }
#endif #endif
} }
@@ -400,8 +401,8 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
// of the TB[BH] instructions. // of the TB[BH] instructions.
bool MadeChange = false; bool MadeChange = false;
if (isThumb2 && AdjustJumpTableBlocks) { if (isThumb2 && AdjustJumpTableBlocks) {
JumpTableFunctionScan(); scanFunctionJumpTables();
MadeChange |= ReorderThumb2JumpTables(); MadeChange |= reorderThumb2JumpTables();
// Data is out of date, so clear it. It'll be re-computed later. // Data is out of date, so clear it. It'll be re-computed later.
T2JumpTables.clear(); T2JumpTables.clear();
// Blocks may have shifted around. Keep the numbering up to date. // Blocks may have shifted around. Keep the numbering up to date.
@@ -419,7 +420,7 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
// we put them all at the end of the function. // we put them all at the end of the function.
std::vector<MachineInstr*> CPEMIs; std::vector<MachineInstr*> CPEMIs;
if (!MCP->isEmpty()) if (!MCP->isEmpty())
DoInitialPlacement(CPEMIs); doInitialPlacement(CPEMIs);
/// The next UID to take is the first unused one. /// The next UID to take is the first unused one.
AFI->initPICLabelUId(CPEMIs.size()); AFI->initPICLabelUId(CPEMIs.size());
@@ -427,13 +428,13 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
// Do the initial scan of the function, building up information about the // Do the initial scan of the function, building up information about the
// sizes of each block, the location of all the water, and finding all of the // sizes of each block, the location of all the water, and finding all of the
// constant pool users. // constant pool users.
InitialFunctionScan(CPEMIs); initializeFunctionInfo(CPEMIs);
CPEMIs.clear(); CPEMIs.clear();
DEBUG(dumpBBs()); DEBUG(dumpBBs());
/// Remove dead constant pool entries. /// Remove dead constant pool entries.
MadeChange |= RemoveUnusedCPEntries(); MadeChange |= removeUnusedCPEntries();
// Iteratively place constant pool entries and fix up branches until there // Iteratively place constant pool entries and fix up branches until there
// is no change. // is no change.
@@ -442,7 +443,7 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n'); DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n');
bool CPChange = false; bool CPChange = false;
for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
CPChange |= HandleConstantPoolUser(i); CPChange |= handleConstantPoolUser(i);
if (CPChange && ++NoCPIters > 30) if (CPChange && ++NoCPIters > 30)
report_fatal_error("Constant Island pass failed to converge!"); report_fatal_error("Constant Island pass failed to converge!");
DEBUG(dumpBBs()); DEBUG(dumpBBs());
@@ -454,7 +455,7 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n'); DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n');
bool BRChange = false; bool BRChange = false;
for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
BRChange |= FixUpImmediateBr(ImmBranches[i]); BRChange |= fixupImmediateBr(ImmBranches[i]);
if (BRChange && ++NoBRIters > 30) if (BRChange && ++NoBRIters > 30)
report_fatal_error("Branch Fix Up pass failed to converge!"); report_fatal_error("Branch Fix Up pass failed to converge!");
DEBUG(dumpBBs()); DEBUG(dumpBBs());
@@ -466,7 +467,7 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
// Shrink 32-bit Thumb2 branch, load, and store instructions. // Shrink 32-bit Thumb2 branch, load, and store instructions.
if (isThumb2 && !STI->prefers32BitThumb()) if (isThumb2 && !STI->prefers32BitThumb())
MadeChange |= OptimizeThumb2Instructions(); MadeChange |= optimizeThumb2Instructions();
// After a while, this might be made debug-only, but it is not expensive. // After a while, this might be made debug-only, but it is not expensive.
verify(); verify();
@@ -474,7 +475,7 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
// If LR has been forced spilled and no far jump (i.e. BL) has been issued, // If LR has been forced spilled and no far jump (i.e. BL) has been issued,
// undo the spill / restore of LR if possible. // undo the spill / restore of LR if possible.
if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump()) if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump())
MadeChange |= UndoLRSpillRestore(); MadeChange |= undoLRSpillRestore();
// Save the mapping between original and cloned constpool entries. // Save the mapping between original and cloned constpool entries.
for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
@@ -497,10 +498,10 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
return MadeChange; return MadeChange;
} }
/// DoInitialPlacement - Perform the initial placement of the constant pool /// doInitialPlacement - Perform the initial placement of the constant pool
/// entries. To start with, we put them all at the end of the function. /// entries. To start with, we put them all at the end of the function.
void void
ARMConstantIslands::DoInitialPlacement(std::vector<MachineInstr*> &CPEMIs) { ARMConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) {
// Create the basic block to hold the CPE's. // Create the basic block to hold the CPE's.
MachineBasicBlock *BB = MF->CreateMachineBasicBlock(); MachineBasicBlock *BB = MF->CreateMachineBasicBlock();
MF->push_back(BB); MF->push_back(BB);
@@ -610,10 +611,10 @@ unsigned ARMConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) {
return Log2_32(Align); return Log2_32(Align);
} }
/// JumpTableFunctionScan - Do a scan of the function, building up /// scanFunctionJumpTables - Do a scan of the function, building up
/// information about the sizes of each block and the locations of all /// information about the sizes of each block and the locations of all
/// the jump tables. /// the jump tables.
void ARMConstantIslands::JumpTableFunctionScan() { void ARMConstantIslands::scanFunctionJumpTables() {
for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
MBBI != E; ++MBBI) { MBBI != E; ++MBBI) {
MachineBasicBlock &MBB = *MBBI; MachineBasicBlock &MBB = *MBBI;
@@ -625,11 +626,11 @@ void ARMConstantIslands::JumpTableFunctionScan() {
} }
} }
/// InitialFunctionScan - Do the initial scan of the function, building up /// initializeFunctionInfo - Do the initial scan of the function, building up
/// information about the sizes of each block, the location of all the water, /// information about the sizes of each block, the location of all the water,
/// and finding all of the constant pool users. /// and finding all of the constant pool users.
void ARMConstantIslands:: void ARMConstantIslands::
InitialFunctionScan(const std::vector<MachineInstr*> &CPEMIs) { initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) {
BBInfo.clear(); BBInfo.clear();
BBInfo.resize(MF->getNumBlockIDs()); BBInfo.resize(MF->getNumBlockIDs());
@@ -638,14 +639,14 @@ InitialFunctionScan(const std::vector<MachineInstr*> &CPEMIs) {
// alignment assumptions, as we don't know for sure the size of any // alignment assumptions, as we don't know for sure the size of any
// instructions in the inline assembly. // instructions in the inline assembly.
for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I)
ComputeBlockSize(I); computeBlockSize(I);
// The known bits of the entry block offset are determined by the function // The known bits of the entry block offset are determined by the function
// alignment. // alignment.
BBInfo.front().KnownBits = MF->getAlignment(); BBInfo.front().KnownBits = MF->getAlignment();
// Compute block offsets and known bits. // Compute block offsets and known bits.
AdjustBBOffsetsAfter(MF->begin()); adjustBBOffsetsAfter(MF->begin());
// Now go back through the instructions and build up our data structures. // Now go back through the instructions and build up our data structures.
for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
@@ -790,9 +791,9 @@ InitialFunctionScan(const std::vector<MachineInstr*> &CPEMIs) {
} }
} }
/// ComputeBlockSize - Compute the size and some alignment information for MBB. /// computeBlockSize - Compute the size and some alignment information for MBB.
/// This function updates BBInfo directly. /// This function updates BBInfo directly.
void ARMConstantIslands::ComputeBlockSize(MachineBasicBlock *MBB) { void ARMConstantIslands::computeBlockSize(MachineBasicBlock *MBB) {
BasicBlockInfo &BBI = BBInfo[MBB->getNumber()]; BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
BBI.Size = 0; BBI.Size = 0;
BBI.Unalign = 0; BBI.Unalign = 0;
@@ -817,10 +818,10 @@ void ARMConstantIslands::ComputeBlockSize(MachineBasicBlock *MBB) {
} }
} }
/// GetOffsetOf - Return the current offset of the specified machine instruction /// getOffsetOf - Return the current offset of the specified machine instruction
/// from the start of the function. This offset changes as stuff is moved /// from the start of the function. This offset changes as stuff is moved
/// around inside the function. /// around inside the function.
unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const { unsigned ARMConstantIslands::getOffsetOf(MachineInstr *MI) const {
MachineBasicBlock *MBB = MI->getParent(); MachineBasicBlock *MBB = MI->getParent();
// The offset is composed of two things: the sum of the sizes of all MBB's // The offset is composed of two things: the sum of the sizes of all MBB's
@@ -843,10 +844,10 @@ static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
return LHS->getNumber() < RHS->getNumber(); return LHS->getNumber() < RHS->getNumber();
} }
/// UpdateForInsertedWaterBlock - When a block is newly inserted into the /// updateForInsertedWaterBlock - When a block is newly inserted into the
/// machine function, it upsets all of the block numbers. Renumber the blocks /// machine function, it upsets all of the block numbers. Renumber the blocks
/// and update the arrays that parallel this numbering. /// and update the arrays that parallel this numbering.
void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) { void ARMConstantIslands::updateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
// Renumber the MBB's to keep them consecutive. // Renumber the MBB's to keep them consecutive.
NewBB->getParent()->RenumberBlocks(NewBB); NewBB->getParent()->RenumberBlocks(NewBB);
@@ -866,7 +867,7 @@ void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
/// Split the basic block containing MI into two blocks, which are joined by /// Split the basic block containing MI into two blocks, which are joined by
/// an unconditional branch. Update data structures and renumber blocks to /// an unconditional branch. Update data structures and renumber blocks to
/// account for this change and returns the newly created block. /// account for this change and returns the newly created block.
MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) { MachineBasicBlock *ARMConstantIslands::splitBlockBeforeInstr(MachineInstr *MI) {
MachineBasicBlock *OrigBB = MI->getParent(); MachineBasicBlock *OrigBB = MI->getParent();
// Create a new MBB for the code after the OrigBB. // Create a new MBB for the code after the OrigBB.
@@ -897,7 +898,7 @@ MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
OrigBB->addSuccessor(NewBB); OrigBB->addSuccessor(NewBB);
// Update internal data structures to account for the newly inserted MBB. // Update internal data structures to account for the newly inserted MBB.
// This is almost the same as UpdateForInsertedWaterBlock, except that // This is almost the same as updateForInsertedWaterBlock, except that
// the Water goes after OrigBB, not NewBB. // the Water goes after OrigBB, not NewBB.
MF->RenumberBlocks(NewBB); MF->RenumberBlocks(NewBB);
@@ -924,23 +925,23 @@ MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
// the new jump we added. (It should be possible to do this without // the new jump we added. (It should be possible to do this without
// recounting everything, but it's very confusing, and this is rarely // recounting everything, but it's very confusing, and this is rarely
// executed.) // executed.)
ComputeBlockSize(OrigBB); computeBlockSize(OrigBB);
// Figure out how large the NewMBB is. As the second half of the original // Figure out how large the NewMBB is. As the second half of the original
// block, it may contain a tablejump. // block, it may contain a tablejump.
ComputeBlockSize(NewBB); computeBlockSize(NewBB);
// All BBOffsets following these blocks must be modified. // All BBOffsets following these blocks must be modified.
AdjustBBOffsetsAfter(OrigBB); adjustBBOffsetsAfter(OrigBB);
return NewBB; return NewBB;
} }
/// GetUserOffset - Compute the offset of U.MI as seen by the hardware /// getUserOffset - Compute the offset of U.MI as seen by the hardware
/// displacement computation. Update U.KnownAlignment to match its current /// displacement computation. Update U.KnownAlignment to match its current
/// basic block location. /// basic block location.
unsigned ARMConstantIslands::GetUserOffset(CPUser &U) const { unsigned ARMConstantIslands::getUserOffset(CPUser &U) const {
unsigned UserOffset = GetOffsetOf(U.MI); unsigned UserOffset = getOffsetOf(U.MI);
const BasicBlockInfo &BBI = BBInfo[U.MI->getParent()->getNumber()]; const BasicBlockInfo &BBI = BBInfo[U.MI->getParent()->getNumber()];
unsigned KnownBits = BBI.internalKnownBits(); unsigned KnownBits = BBI.internalKnownBits();
@@ -960,13 +961,13 @@ unsigned ARMConstantIslands::GetUserOffset(CPUser &U) const {
return UserOffset; return UserOffset;
} }
/// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool /// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
/// reference) is within MaxDisp of TrialOffset (a proposed location of a /// reference) is within MaxDisp of TrialOffset (a proposed location of a
/// constant pool entry). /// constant pool entry).
/// UserOffset is computed by GetUserOffset above to include PC adjustments. If /// UserOffset is computed by getUserOffset above to include PC adjustments. If
/// the mod 4 alignment of UserOffset is not known, the uncertainty must be /// the mod 4 alignment of UserOffset is not known, the uncertainty must be
/// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that. /// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that.
bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset, bool ARMConstantIslands::isOffsetInRange(unsigned UserOffset,
unsigned TrialOffset, unsigned MaxDisp, unsigned TrialOffset, unsigned MaxDisp,
bool NegativeOK, bool IsSoImm) { bool NegativeOK, bool IsSoImm) {
if (UserOffset <= TrialOffset) { if (UserOffset <= TrialOffset) {
@@ -982,11 +983,11 @@ bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset,
return false; return false;
} }
/// WaterIsInRange - Returns true if a CPE placed after the specified /// isWaterInRange - Returns true if a CPE placed after the specified
/// Water (a basic block) will be in range for the specific MI. /// Water (a basic block) will be in range for the specific MI.
/// ///
/// Compute how much the function will grow by inserting a CPE after Water. /// Compute how much the function will grow by inserting a CPE after Water.
bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset, bool ARMConstantIslands::isWaterInRange(unsigned UserOffset,
MachineBasicBlock* Water, CPUser &U, MachineBasicBlock* Water, CPUser &U,
unsigned &Growth) { unsigned &Growth) {
unsigned CPELogAlign = getCPELogAlign(U.CPEMI); unsigned CPELogAlign = getCPELogAlign(U.CPEMI);
@@ -1013,7 +1014,7 @@ bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
Growth += OffsetToAlignment(CPEEnd, 1u << NextBlockAlignment); Growth += OffsetToAlignment(CPEEnd, 1u << NextBlockAlignment);
// If the CPE is to be inserted before the instruction, that will raise // If the CPE is to be inserted before the instruction, that will raise
// the offset of the instruction. Also account for unknown alignment padding // the offset of the instruction. Also account for unknown alignment padding
// in blocks between CPE and the user. // in blocks between CPE and the user.
if (CPEOffset < UserOffset) if (CPEOffset < UserOffset)
UserOffset += Growth + UnknownPadding(MF->getAlignment(), CPELogAlign); UserOffset += Growth + UnknownPadding(MF->getAlignment(), CPELogAlign);
@@ -1021,15 +1022,15 @@ bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
// CPE fits in existing padding. // CPE fits in existing padding.
Growth = 0; Growth = 0;
return OffsetIsInRange(UserOffset, CPEOffset, U); return isOffsetInRange(UserOffset, CPEOffset, U);
} }
/// CPEIsInRange - Returns true if the distance between specific MI and /// isCPEntryInRange - Returns true if the distance between specific MI and
/// specific ConstPool entry instruction can fit in MI's displacement field. /// specific ConstPool entry instruction can fit in MI's displacement field.
bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset, bool ARMConstantIslands::isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
MachineInstr *CPEMI, unsigned MaxDisp, MachineInstr *CPEMI, unsigned MaxDisp,
bool NegOk, bool DoDump) { bool NegOk, bool DoDump) {
unsigned CPEOffset = GetOffsetOf(CPEMI); unsigned CPEOffset = getOffsetOf(CPEMI);
assert(CPEOffset % 4 == 0 && "Misaligned CPE"); assert(CPEOffset % 4 == 0 && "Misaligned CPE");
if (DoDump) { if (DoDump) {
@@ -1046,7 +1047,7 @@ bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
}); });
} }
return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk); return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
} }
#ifndef NDEBUG #ifndef NDEBUG
@@ -1066,7 +1067,7 @@ static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
} }
#endif // NDEBUG #endif // NDEBUG
void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB) { void ARMConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
unsigned BBNum = BB->getNumber(); unsigned BBNum = BB->getNumber();
for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) { for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
// Get the offset and known bits at the end of the layout predecessor. // Get the offset and known bits at the end of the layout predecessor.
@@ -1088,17 +1089,18 @@ void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB) {
} }
} }
/// DecrementOldEntry - find the constant pool entry with index CPI /// decrementCPEReferenceCount - find the constant pool entry with index CPI
/// and instruction CPEMI, and decrement its refcount. If the refcount /// and instruction CPEMI, and decrement its refcount. If the refcount
/// becomes 0 remove the entry and instruction. Returns true if we removed /// becomes 0 remove the entry and instruction. Returns true if we removed
/// the entry, false if we didn't. /// the entry, false if we didn't.
bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) { bool ARMConstantIslands::decrementCPEReferenceCount(unsigned CPI,
MachineInstr *CPEMI) {
// Find the old entry. Eliminate it if it is no longer used. // Find the old entry. Eliminate it if it is no longer used.
CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
assert(CPE && "Unexpected!"); assert(CPE && "Unexpected!");
if (--CPE->RefCount == 0) { if (--CPE->RefCount == 0) {
RemoveDeadCPEMI(CPEMI); removeDeadCPEMI(CPEMI);
CPE->CPEMI = NULL; CPE->CPEMI = NULL;
--NumCPEs; --NumCPEs;
return true; return true;
@@ -1112,13 +1114,14 @@ bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) {
/// 0 = no existing entry found /// 0 = no existing entry found
/// 1 = entry found, and there were no code insertions or deletions /// 1 = entry found, and there were no code insertions or deletions
/// 2 = entry found, and there were code insertions or deletions /// 2 = entry found, and there were code insertions or deletions
int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset) int ARMConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset)
{ {
MachineInstr *UserMI = U.MI; MachineInstr *UserMI = U.MI;
MachineInstr *CPEMI = U.CPEMI; MachineInstr *CPEMI = U.CPEMI;
// Check to see if the CPE is already in-range. // Check to see if the CPE is already in-range.
if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk, true)) { if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk,
true)) {
DEBUG(dbgs() << "In range\n"); DEBUG(dbgs() << "In range\n");
return 1; return 1;
} }
@@ -1133,7 +1136,7 @@ int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
// Removing CPEs can leave empty entries, skip // Removing CPEs can leave empty entries, skip
if (CPEs[i].CPEMI == NULL) if (CPEs[i].CPEMI == NULL)
continue; continue;
if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(), if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(),
U.NegOk)) { U.NegOk)) {
DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#" DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
<< CPEs[i].CPI << "\n"); << CPEs[i].CPI << "\n");
@@ -1149,7 +1152,7 @@ int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
CPEs[i].RefCount++; CPEs[i].RefCount++;
// ...and the original. If we didn't remove the old entry, none of the // ...and the original. If we didn't remove the old entry, none of the
// addresses changed, so we don't need another pass. // addresses changed, so we don't need another pass.
return DecrementOldEntry(CPI, CPEMI) ? 2 : 1; return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
} }
} }
return 0; return 0;
@@ -1170,7 +1173,7 @@ static inline unsigned getUnconditionalBrDisp(int Opc) {
return ((1<<23)-1)*4; return ((1<<23)-1)*4;
} }
/// LookForWater - Look for an existing entry in the WaterList in which /// findAvailableWater - Look for an existing entry in the WaterList in which
/// we can place the CPE referenced from U so it's within range of U's MI. /// we can place the CPE referenced from U so it's within range of U's MI.
/// Returns true if found, false if not. If it returns true, WaterIter /// Returns true if found, false if not. If it returns true, WaterIter
/// is set to the WaterList entry. For Thumb, prefer water that will not /// is set to the WaterList entry. For Thumb, prefer water that will not
@@ -1178,7 +1181,7 @@ static inline unsigned getUnconditionalBrDisp(int Opc) {
/// terminates, the CPE location for a particular CPUser is only allowed to /// terminates, the CPE location for a particular CPUser is only allowed to
/// move to a lower address, so search backward from the end of the list and /// move to a lower address, so search backward from the end of the list and
/// prefer the first water that is in range. /// prefer the first water that is in range.
bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset, bool ARMConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset,
water_iterator &WaterIter) { water_iterator &WaterIter) {
if (WaterList.empty()) if (WaterList.empty())
return false; return false;
@@ -1196,7 +1199,7 @@ bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset,
// sure to take advantage of it for all the CPEs near that block, so that // sure to take advantage of it for all the CPEs near that block, so that
// we don't insert more branches than necessary. // we don't insert more branches than necessary.
unsigned Growth; unsigned Growth;
if (WaterIsInRange(UserOffset, WaterBB, U, Growth) && if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
(WaterBB->getNumber() < U.HighWaterMark->getNumber() || (WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
NewWaterList.count(WaterBB)) && Growth < BestGrowth) { NewWaterList.count(WaterBB)) && Growth < BestGrowth) {
// This is the least amount of required padding seen so far. // This is the least amount of required padding seen so far.
@@ -1215,14 +1218,14 @@ bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset,
return BestGrowth != ~0u; return BestGrowth != ~0u;
} }
/// CreateNewWater - No existing WaterList entry will work for /// createNewWater - No existing WaterList entry will work for
/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
/// block is used if in range, and the conditional branch munged so control /// block is used if in range, and the conditional branch munged so control
/// flow is correct. Otherwise the block is split to create a hole with an /// flow is correct. Otherwise the block is split to create a hole with an
/// unconditional branch around it. In either case NewMBB is set to a /// unconditional branch around it. In either case NewMBB is set to a
/// block following which the new island can be inserted (the WaterList /// block following which the new island can be inserted (the WaterList
/// is not adjusted). /// is not adjusted).
void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex, void ARMConstantIslands::createNewWater(unsigned CPUserIndex,
unsigned UserOffset, unsigned UserOffset,
MachineBasicBlock *&NewMBB) { MachineBasicBlock *&NewMBB) {
CPUser &U = CPUsers[CPUserIndex]; CPUser &U = CPUsers[CPUserIndex];
@@ -1245,7 +1248,7 @@ void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
unsigned CPEOffset = WorstCaseAlign(UserBlockEnd, CPELogAlign, unsigned CPEOffset = WorstCaseAlign(UserBlockEnd, CPELogAlign,
UserBBI.postKnownBits()); UserBBI.postKnownBits());
if (OffsetIsInRange(UserOffset, CPEOffset, U)) { if (isOffsetInRange(UserOffset, CPEOffset, U)) {
DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber() DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber()
<< format(", expected CPE offset %#x\n", CPEOffset)); << format(", expected CPE offset %#x\n", CPEOffset));
NewMBB = llvm::next(MachineFunction::iterator(UserMBB)); NewMBB = llvm::next(MachineFunction::iterator(UserMBB));
@@ -1264,7 +1267,7 @@ void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
ImmBranches.push_back(ImmBranch(&UserMBB->back(), ImmBranches.push_back(ImmBranch(&UserMBB->back(),
MaxDisp, false, UncondBr)); MaxDisp, false, UncondBr));
BBInfo[UserMBB->getNumber()].Size += Delta; BBInfo[UserMBB->getNumber()].Size += Delta;
AdjustBBOffsetsAfter(UserMBB); adjustBBOffsetsAfter(UserMBB);
return; return;
} }
} }
@@ -1298,7 +1301,7 @@ void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
// The 4 in the following is for the unconditional branch we'll be inserting // The 4 in the following is for the unconditional branch we'll be inserting
// (allows for long branch on Thumb1). Alignment of the island is handled // (allows for long branch on Thumb1). Alignment of the island is handled
// inside OffsetIsInRange. // inside isOffsetInRange.
BaseInsertOffset -= 4; BaseInsertOffset -= 4;
DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset) DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset)
@@ -1327,7 +1330,7 @@ void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
MI = llvm::next(MI)) { MI = llvm::next(MI)) {
if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) { if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
CPUser &U = CPUsers[CPUIndex]; CPUser &U = CPUsers[CPUIndex];
if (!OffsetIsInRange(Offset, EndInsertOffset, U)) { if (!isOffsetInRange(Offset, EndInsertOffset, U)) {
// Shift intertion point by one unit of alignment so it is within reach. // Shift intertion point by one unit of alignment so it is within reach.
BaseInsertOffset -= 1u << LogAlign; BaseInsertOffset -= 1u << LogAlign;
EndInsertOffset -= 1u << LogAlign; EndInsertOffset -= 1u << LogAlign;
@@ -1356,25 +1359,25 @@ void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
if (CC != ARMCC::AL) if (CC != ARMCC::AL)
MI = LastIT; MI = LastIT;
} }
NewMBB = SplitBlockBeforeInstr(MI); NewMBB = splitBlockBeforeInstr(MI);
} }
/// HandleConstantPoolUser - Analyze the specified user, checking to see if it /// handleConstantPoolUser - Analyze the specified user, checking to see if it
/// is out-of-range. If so, pick up the constant pool value and move it some /// is out-of-range. If so, pick up the constant pool value and move it some
/// place in-range. Return true if we changed any addresses (thus must run /// place in-range. Return true if we changed any addresses (thus must run
/// another pass of branch lengthening), false otherwise. /// another pass of branch lengthening), false otherwise.
bool ARMConstantIslands::HandleConstantPoolUser(unsigned CPUserIndex) { bool ARMConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) {
CPUser &U = CPUsers[CPUserIndex]; CPUser &U = CPUsers[CPUserIndex];
MachineInstr *UserMI = U.MI; MachineInstr *UserMI = U.MI;
MachineInstr *CPEMI = U.CPEMI; MachineInstr *CPEMI = U.CPEMI;
unsigned CPI = CPEMI->getOperand(1).getIndex(); unsigned CPI = CPEMI->getOperand(1).getIndex();
unsigned Size = CPEMI->getOperand(2).getImm(); unsigned Size = CPEMI->getOperand(2).getImm();
// Compute this only once, it's expensive. // Compute this only once, it's expensive.
unsigned UserOffset = GetUserOffset(U); unsigned UserOffset = getUserOffset(U);
// See if the current entry is within range, or there is a clone of it // See if the current entry is within range, or there is a clone of it
// in range. // in range.
int result = LookForExistingCPEntry(U, UserOffset); int result = findInRangeCPEntry(U, UserOffset);
if (result==1) return false; if (result==1) return false;
else if (result==2) return true; else if (result==2) return true;
@@ -1386,7 +1389,7 @@ bool ARMConstantIslands::HandleConstantPoolUser(unsigned CPUserIndex) {
MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock(); MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
MachineBasicBlock *NewMBB; MachineBasicBlock *NewMBB;
water_iterator IP; water_iterator IP;
if (LookForWater(U, UserOffset, IP)) { if (findAvailableWater(U, UserOffset, IP)) {
DEBUG(dbgs() << "Found water in range\n"); DEBUG(dbgs() << "Found water in range\n");
MachineBasicBlock *WaterBB = *IP; MachineBasicBlock *WaterBB = *IP;
@@ -1403,9 +1406,9 @@ bool ARMConstantIslands::HandleConstantPoolUser(unsigned CPUserIndex) {
} else { } else {
// No water found. // No water found.
DEBUG(dbgs() << "No water found\n"); DEBUG(dbgs() << "No water found\n");
CreateNewWater(CPUserIndex, UserOffset, NewMBB); createNewWater(CPUserIndex, UserOffset, NewMBB);
// SplitBlockBeforeInstr adds to WaterList, which is important when it is // splitBlockBeforeInstr adds to WaterList, which is important when it is
// called while handling branches so that the water will be seen on the // called while handling branches so that the water will be seen on the
// next iteration for constant pools, but in this context, we don't want // next iteration for constant pools, but in this context, we don't want
// it. Check for this so it will be removed from the WaterList. // it. Check for this so it will be removed from the WaterList.
@@ -1430,10 +1433,10 @@ bool ARMConstantIslands::HandleConstantPoolUser(unsigned CPUserIndex) {
MF->insert(NewMBB, NewIsland); MF->insert(NewMBB, NewIsland);
// Update internal data structures to account for the newly inserted MBB. // Update internal data structures to account for the newly inserted MBB.
UpdateForInsertedWaterBlock(NewIsland); updateForInsertedWaterBlock(NewIsland);
// Decrement the old entry, and remove it if refcount becomes 0. // Decrement the old entry, and remove it if refcount becomes 0.
DecrementOldEntry(CPI, CPEMI); decrementCPEReferenceCount(CPI, CPEMI);
// Now that we have an island to add the CPE to, clone the original CPE and // Now that we have an island to add the CPE to, clone the original CPE and
// add it to the island. // add it to the island.
@@ -1448,7 +1451,7 @@ bool ARMConstantIslands::HandleConstantPoolUser(unsigned CPUserIndex) {
// Increase the size of the island block to account for the new entry. // Increase the size of the island block to account for the new entry.
BBInfo[NewIsland->getNumber()].Size += Size; BBInfo[NewIsland->getNumber()].Size += Size;
AdjustBBOffsetsAfter(llvm::prior(MachineFunction::iterator(NewIsland))); adjustBBOffsetsAfter(llvm::prior(MachineFunction::iterator(NewIsland)));
// Finally, change the CPI in the instruction operand to be ID. // Finally, change the CPI in the instruction operand to be ID.
for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
@@ -1463,9 +1466,9 @@ bool ARMConstantIslands::HandleConstantPoolUser(unsigned CPUserIndex) {
return true; return true;
} }
/// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update /// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
/// sizes and offsets of impacted basic blocks. /// sizes and offsets of impacted basic blocks.
void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) { void ARMConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) {
MachineBasicBlock *CPEBB = CPEMI->getParent(); MachineBasicBlock *CPEBB = CPEMI->getParent();
unsigned Size = CPEMI->getOperand(2).getImm(); unsigned Size = CPEMI->getOperand(2).getImm();
CPEMI->eraseFromParent(); CPEMI->eraseFromParent();
@@ -1480,7 +1483,7 @@ void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) {
// Entries are sorted by descending alignment, so realign from the front. // Entries are sorted by descending alignment, so realign from the front.
CPEBB->setAlignment(getCPELogAlign(CPEBB->begin())); CPEBB->setAlignment(getCPELogAlign(CPEBB->begin()));
AdjustBBOffsetsAfter(CPEBB); adjustBBOffsetsAfter(CPEBB);
// An island has only one predecessor BB and one successor BB. Check if // An island has only one predecessor BB and one successor BB. Check if
// this BB's predecessor jumps directly to this BB's successor. This // this BB's predecessor jumps directly to this BB's successor. This
// shouldn't happen currently. // shouldn't happen currently.
@@ -1488,15 +1491,15 @@ void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) {
// FIXME: remove the empty blocks after all the work is done? // FIXME: remove the empty blocks after all the work is done?
} }
/// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts /// removeUnusedCPEntries - Remove constant pool entries whose refcounts
/// are zero. /// are zero.
bool ARMConstantIslands::RemoveUnusedCPEntries() { bool ARMConstantIslands::removeUnusedCPEntries() {
unsigned MadeChange = false; unsigned MadeChange = false;
for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
std::vector<CPEntry> &CPEs = CPEntries[i]; std::vector<CPEntry> &CPEs = CPEntries[i];
for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) { for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) { if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
RemoveDeadCPEMI(CPEs[j].CPEMI); removeDeadCPEMI(CPEs[j].CPEMI);
CPEs[j].CPEMI = NULL; CPEs[j].CPEMI = NULL;
MadeChange = true; MadeChange = true;
} }
@@ -1505,18 +1508,18 @@ bool ARMConstantIslands::RemoveUnusedCPEntries() {
return MadeChange; return MadeChange;
} }
/// BBIsInRange - Returns true if the distance between specific MI and /// isBBInRange - Returns true if the distance between specific MI and
/// specific BB can fit in MI's displacement field. /// specific BB can fit in MI's displacement field.
bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB, bool ARMConstantIslands::isBBInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
unsigned MaxDisp) { unsigned MaxDisp) {
unsigned PCAdj = isThumb ? 4 : 8; unsigned PCAdj = isThumb ? 4 : 8;
unsigned BrOffset = GetOffsetOf(MI) + PCAdj; unsigned BrOffset = getOffsetOf(MI) + PCAdj;
unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber() DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber()
<< " from BB#" << MI->getParent()->getNumber() << " from BB#" << MI->getParent()->getNumber()
<< " max delta=" << MaxDisp << " max delta=" << MaxDisp
<< " from " << GetOffsetOf(MI) << " to " << DestOffset << " from " << getOffsetOf(MI) << " to " << DestOffset
<< " offset " << int(DestOffset-BrOffset) << "\t" << *MI); << " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
if (BrOffset <= DestOffset) { if (BrOffset <= DestOffset) {
@@ -1530,37 +1533,37 @@ bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
return false; return false;
} }
/// FixUpImmediateBr - Fix up an immediate branch whose destination is too far /// fixupImmediateBr - Fix up an immediate branch whose destination is too far
/// away to fit in its displacement field. /// away to fit in its displacement field.
bool ARMConstantIslands::FixUpImmediateBr(ImmBranch &Br) { bool ARMConstantIslands::fixupImmediateBr(ImmBranch &Br) {
MachineInstr *MI = Br.MI; MachineInstr *MI = Br.MI;
MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
// Check to see if the DestBB is already in-range. // Check to see if the DestBB is already in-range.
if (BBIsInRange(MI, DestBB, Br.MaxDisp)) if (isBBInRange(MI, DestBB, Br.MaxDisp))
return false; return false;
if (!Br.isCond) if (!Br.isCond)
return FixUpUnconditionalBr(Br); return fixupUnconditionalBr(Br);
return FixUpConditionalBr(Br); return fixupConditionalBr(Br);
} }
/// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is /// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
/// too far away to fit in its displacement field. If the LR register has been /// too far away to fit in its displacement field. If the LR register has been
/// spilled in the epilogue, then we can use BL to implement a far jump. /// spilled in the epilogue, then we can use BL to implement a far jump.
/// Otherwise, add an intermediate branch instruction to a branch. /// Otherwise, add an intermediate branch instruction to a branch.
bool bool
ARMConstantIslands::FixUpUnconditionalBr(ImmBranch &Br) { ARMConstantIslands::fixupUnconditionalBr(ImmBranch &Br) {
MachineInstr *MI = Br.MI; MachineInstr *MI = Br.MI;
MachineBasicBlock *MBB = MI->getParent(); MachineBasicBlock *MBB = MI->getParent();
if (!isThumb1) if (!isThumb1)
llvm_unreachable("FixUpUnconditionalBr is Thumb1 only!"); llvm_unreachable("fixupUnconditionalBr is Thumb1 only!");
// Use BL to implement far jump. // Use BL to implement far jump.
Br.MaxDisp = (1 << 21) * 2; Br.MaxDisp = (1 << 21) * 2;
MI->setDesc(TII->get(ARM::tBfar)); MI->setDesc(TII->get(ARM::tBfar));
BBInfo[MBB->getNumber()].Size += 2; BBInfo[MBB->getNumber()].Size += 2;
AdjustBBOffsetsAfter(MBB); adjustBBOffsetsAfter(MBB);
HasFarJump = true; HasFarJump = true;
++NumUBrFixed; ++NumUBrFixed;
@@ -1569,11 +1572,11 @@ ARMConstantIslands::FixUpUnconditionalBr(ImmBranch &Br) {
return true; return true;
} }
/// FixUpConditionalBr - Fix up a conditional branch whose destination is too /// fixupConditionalBr - Fix up a conditional branch whose destination is too
/// far away to fit in its displacement field. It is converted to an inverse /// far away to fit in its displacement field. It is converted to an inverse
/// conditional branch + an unconditional branch to the destination. /// conditional branch + an unconditional branch to the destination.
bool bool
ARMConstantIslands::FixUpConditionalBr(ImmBranch &Br) { ARMConstantIslands::fixupConditionalBr(ImmBranch &Br) {
MachineInstr *MI = Br.MI; MachineInstr *MI = Br.MI;
MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
@@ -1607,7 +1610,7 @@ ARMConstantIslands::FixUpConditionalBr(ImmBranch &Br) {
// bne L2 // bne L2
// b L1 // b L1
MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB(); MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
if (BBIsInRange(MI, NewDest, Br.MaxDisp)) { if (isBBInRange(MI, NewDest, Br.MaxDisp)) {
DEBUG(dbgs() << " Invert Bcc condition and swap its destination with " DEBUG(dbgs() << " Invert Bcc condition and swap its destination with "
<< *BMI); << *BMI);
BMI->getOperand(0).setMBB(DestBB); BMI->getOperand(0).setMBB(DestBB);
@@ -1619,7 +1622,7 @@ ARMConstantIslands::FixUpConditionalBr(ImmBranch &Br) {
} }
if (NeedSplit) { if (NeedSplit) {
SplitBlockBeforeInstr(MI); splitBlockBeforeInstr(MI);
// No need for the branch to the next block. We're adding an unconditional // No need for the branch to the next block. We're adding an unconditional
// branch to the destination. // branch to the destination.
int delta = TII->GetInstSizeInBytes(&MBB->back()); int delta = TII->GetInstSizeInBytes(&MBB->back());
@@ -1651,14 +1654,14 @@ ARMConstantIslands::FixUpConditionalBr(ImmBranch &Br) {
// Remove the old conditional branch. It may or may not still be in MBB. // Remove the old conditional branch. It may or may not still be in MBB.
BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI); BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI);
MI->eraseFromParent(); MI->eraseFromParent();
AdjustBBOffsetsAfter(MBB); adjustBBOffsetsAfter(MBB);
return true; return true;
} }
/// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills /// undoLRSpillRestore - Remove Thumb push / pop instructions that only spills
/// LR / restores LR to pc. FIXME: This is done here because it's only possible /// LR / restores LR to pc. FIXME: This is done here because it's only possible
/// to do this if tBfar is not used. /// to do this if tBfar is not used.
bool ARMConstantIslands::UndoLRSpillRestore() { bool ARMConstantIslands::undoLRSpillRestore() {
bool MadeChange = false; bool MadeChange = false;
for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) { for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
MachineInstr *MI = PushPopMIs[i]; MachineInstr *MI = PushPopMIs[i];
@@ -1677,26 +1680,26 @@ bool ARMConstantIslands::UndoLRSpillRestore() {
return MadeChange; return MadeChange;
} }
// mayOptimizeThumb2Instruction - Returns true if OptimizeThumb2Instructions // mayOptimizeThumb2Instruction - Returns true if optimizeThumb2Instructions
// below may shrink MI. // below may shrink MI.
bool bool
ARMConstantIslands::mayOptimizeThumb2Instruction(const MachineInstr *MI) const { ARMConstantIslands::mayOptimizeThumb2Instruction(const MachineInstr *MI) const {
switch(MI->getOpcode()) { switch(MI->getOpcode()) {
// OptimizeThumb2Instructions. // optimizeThumb2Instructions.
case ARM::t2LEApcrel: case ARM::t2LEApcrel:
case ARM::t2LDRpci: case ARM::t2LDRpci:
// OptimizeThumb2Branches. // optimizeThumb2Branches.
case ARM::t2B: case ARM::t2B:
case ARM::t2Bcc: case ARM::t2Bcc:
case ARM::tBcc: case ARM::tBcc:
// OptimizeThumb2JumpTables. // optimizeThumb2JumpTables.
case ARM::t2BR_JT: case ARM::t2BR_JT:
return true; return true;
} }
return false; return false;
} }
bool ARMConstantIslands::OptimizeThumb2Instructions() { bool ARMConstantIslands::optimizeThumb2Instructions() {
bool MadeChange = false; bool MadeChange = false;
// Shrink ADR and LDR from constantpool. // Shrink ADR and LDR from constantpool.
@@ -1727,7 +1730,7 @@ bool ARMConstantIslands::OptimizeThumb2Instructions() {
if (!NewOpc) if (!NewOpc)
continue; continue;
unsigned UserOffset = GetUserOffset(U); unsigned UserOffset = getUserOffset(U);
unsigned MaxOffs = ((1 << Bits) - 1) * Scale; unsigned MaxOffs = ((1 << Bits) - 1) * Scale;
// Be conservative with inline asm. // Be conservative with inline asm.
@@ -1735,22 +1738,22 @@ bool ARMConstantIslands::OptimizeThumb2Instructions() {
MaxOffs -= 2; MaxOffs -= 2;
// FIXME: Check if offset is multiple of scale if scale is not 4. // FIXME: Check if offset is multiple of scale if scale is not 4.
if (CPEIsInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) { if (isCPEntryInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) {
U.MI->setDesc(TII->get(NewOpc)); U.MI->setDesc(TII->get(NewOpc));
MachineBasicBlock *MBB = U.MI->getParent(); MachineBasicBlock *MBB = U.MI->getParent();
BBInfo[MBB->getNumber()].Size -= 2; BBInfo[MBB->getNumber()].Size -= 2;
AdjustBBOffsetsAfter(MBB); adjustBBOffsetsAfter(MBB);
++NumT2CPShrunk; ++NumT2CPShrunk;
MadeChange = true; MadeChange = true;
} }
} }
MadeChange |= OptimizeThumb2Branches(); MadeChange |= optimizeThumb2Branches();
MadeChange |= OptimizeThumb2JumpTables(); MadeChange |= optimizeThumb2JumpTables();
return MadeChange; return MadeChange;
} }
bool ARMConstantIslands::OptimizeThumb2Branches() { bool ARMConstantIslands::optimizeThumb2Branches() {
bool MadeChange = false; bool MadeChange = false;
for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) { for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) {
@@ -1776,11 +1779,11 @@ bool ARMConstantIslands::OptimizeThumb2Branches() {
if (NewOpc) { if (NewOpc) {
unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
if (BBIsInRange(Br.MI, DestBB, MaxOffs)) { if (isBBInRange(Br.MI, DestBB, MaxOffs)) {
Br.MI->setDesc(TII->get(NewOpc)); Br.MI->setDesc(TII->get(NewOpc));
MachineBasicBlock *MBB = Br.MI->getParent(); MachineBasicBlock *MBB = Br.MI->getParent();
BBInfo[MBB->getNumber()].Size -= 2; BBInfo[MBB->getNumber()].Size -= 2;
AdjustBBOffsetsAfter(MBB); adjustBBOffsetsAfter(MBB);
++NumT2BrShrunk; ++NumT2BrShrunk;
MadeChange = true; MadeChange = true;
} }
@@ -1807,7 +1810,7 @@ bool ARMConstantIslands::OptimizeThumb2Branches() {
MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB();
// Check if the distance is within 126. Subtract starting offset by 2 // Check if the distance is within 126. Subtract starting offset by 2
// because the cmp will be eliminated. // because the cmp will be eliminated.
unsigned BrOffset = GetOffsetOf(Br.MI) + 4 - 2; unsigned BrOffset = getOffsetOf(Br.MI) + 4 - 2;
unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) { if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) {
MachineBasicBlock::iterator CmpMI = Br.MI; MachineBasicBlock::iterator CmpMI = Br.MI;
@@ -1827,7 +1830,7 @@ bool ARMConstantIslands::OptimizeThumb2Branches() {
Br.MI->eraseFromParent(); Br.MI->eraseFromParent();
Br.MI = NewBR; Br.MI = NewBR;
BBInfo[MBB->getNumber()].Size -= 2; BBInfo[MBB->getNumber()].Size -= 2;
AdjustBBOffsetsAfter(MBB); adjustBBOffsetsAfter(MBB);
++NumCBZ; ++NumCBZ;
MadeChange = true; MadeChange = true;
} }
@@ -1839,9 +1842,9 @@ bool ARMConstantIslands::OptimizeThumb2Branches() {
return MadeChange; return MadeChange;
} }
/// OptimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller /// optimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
/// jumptables when it's possible. /// jumptables when it's possible.
bool ARMConstantIslands::OptimizeThumb2JumpTables() { bool ARMConstantIslands::optimizeThumb2JumpTables() {
bool MadeChange = false; bool MadeChange = false;
// FIXME: After the tables are shrunk, can we get rid some of the // FIXME: After the tables are shrunk, can we get rid some of the
@@ -1861,7 +1864,7 @@ bool ARMConstantIslands::OptimizeThumb2JumpTables() {
bool ByteOk = true; bool ByteOk = true;
bool HalfWordOk = true; bool HalfWordOk = true;
unsigned JTOffset = GetOffsetOf(MI) + 4; unsigned JTOffset = getOffsetOf(MI) + 4;
const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
MachineBasicBlock *MBB = JTBBs[j]; MachineBasicBlock *MBB = JTBBs[j];
@@ -1954,7 +1957,7 @@ bool ARMConstantIslands::OptimizeThumb2JumpTables() {
int delta = OrigSize - NewSize; int delta = OrigSize - NewSize;
BBInfo[MBB->getNumber()].Size -= delta; BBInfo[MBB->getNumber()].Size -= delta;
AdjustBBOffsetsAfter(MBB); adjustBBOffsetsAfter(MBB);
++NumTBs; ++NumTBs;
MadeChange = true; MadeChange = true;
@@ -1964,9 +1967,9 @@ bool ARMConstantIslands::OptimizeThumb2JumpTables() {
return MadeChange; return MadeChange;
} }
/// ReorderThumb2JumpTables - Adjust the function's block layout to ensure that /// reorderThumb2JumpTables - Adjust the function's block layout to ensure that
/// jump tables always branch forwards, since that's what tbb and tbh need. /// jump tables always branch forwards, since that's what tbb and tbh need.
bool ARMConstantIslands::ReorderThumb2JumpTables() { bool ARMConstantIslands::reorderThumb2JumpTables() {
bool MadeChange = false; bool MadeChange = false;
MachineJumpTableInfo *MJTI = MF->getJumpTableInfo(); MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
@@ -1995,7 +1998,7 @@ bool ARMConstantIslands::ReorderThumb2JumpTables() {
// The destination precedes the switch. Try to move the block forward // The destination precedes the switch. Try to move the block forward
// so we have a positive offset. // so we have a positive offset.
MachineBasicBlock *NewBB = MachineBasicBlock *NewBB =
AdjustJTTargetBlockForward(MBB, MI->getParent()); adjustJTTargetBlockForward(MBB, MI->getParent());
if (NewBB) if (NewBB)
MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB); MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB);
MadeChange = true; MadeChange = true;
@@ -2007,8 +2010,7 @@ bool ARMConstantIslands::ReorderThumb2JumpTables() {
} }
MachineBasicBlock *ARMConstantIslands:: MachineBasicBlock *ARMConstantIslands::
AdjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) adjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) {
{
// If the destination block is terminated by an unconditional branch, // If the destination block is terminated by an unconditional branch,
// try to move it; otherwise, create a new block following the jump // try to move it; otherwise, create a new block following the jump
// table that branches back to the actual target. This is a very simple // table that branches back to the actual target. This is a very simple