40#include "llvm/Config/llvm-config.h"
59#define DEBUG_TYPE "mips-constant-islands"
62STATISTIC(NumSplit,
"Number of uncond branches inserted");
63STATISTIC(NumCBrFixed,
"Number of cond branches fixed");
64STATISTIC(NumUBrFixed,
"Number of uncond branches fixed");
67 switch (
MI->getOpcode()) {
76 case Mips::BeqzRxImm16:
77 case Mips::BeqzRxImmX16:
78 case Mips::BnezRxImm16:
79 case Mips::BnezRxImmX16:
92 return Mips::BteqzX16;
95 return Mips::BtnezX16;
98 case Mips::BeqzRxImm16:
99 case Mips::BeqzRxImmX16:
100 return Mips::BeqzRxImmX16;
101 case Mips::BnezRxImm16:
102 case Mips::BnezRxImmX16:
103 return Mips::BnezRxImmX16;
113 unsigned Bits, Scale;
123 case Mips::BeqzRxImm16:
127 case Mips::BeqzRxImmX16:
131 case Mips::BnezRxImm16:
135 case Mips::BnezRxImmX16:
158 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
183 MipsConstantIslandLegacy() : MachineFunctionPass(ID) {}
185 StringRef getPassName()
const override {
return "Mips Constant Islands"; }
189 MachineFunctionProperties getRequiredProperties()
const override {
190 return MachineFunctionProperties().setNoVRegs();
194 class MipsConstantIslandImpl {
197 struct BasicBlockInfo {
216 BasicBlockInfo() =
default;
218 unsigned postOffset()
const {
return Offset + Size; }
221 std::vector<BasicBlockInfo> BBInfo;
226 std::vector<MachineBasicBlock*> WaterList;
230 SmallPtrSet<MachineBasicBlock *, 4> NewWaterList;
232 using water_iterator = std::vector<MachineBasicBlock *>::iterator;
249 MachineBasicBlock *HighWaterMark;
253 unsigned LongFormMaxDisp;
255 unsigned LongFormOpcode;
260 CPUser(MachineInstr *mi, MachineInstr *cpemi,
unsigned maxdisp,
262 unsigned longformmaxdisp,
unsigned longformopcode)
263 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp),
264 LongFormMaxDisp(longformmaxdisp), LongFormOpcode(longformopcode),
266 HighWaterMark = CPEMI->getParent();
270 unsigned getMaxDisp()
const {
return MaxDisp; }
272 void setMaxDisp(
unsigned val) {
276 unsigned getLongFormMaxDisp()
const {
277 return LongFormMaxDisp;
280 unsigned getLongFormOpcode()
const {
281 return LongFormOpcode;
287 std::vector<CPUser> CPUsers;
297 CPEntry(MachineInstr *cpemi,
unsigned cpi,
unsigned rc = 0)
298 : CPEMI(cpemi), CPI(cpi), RefCount(
rc) {}
306 std::vector<std::vector<CPEntry>> CPEntries;
314 unsigned MaxDisp : 31;
319 ImmBranch(MachineInstr *mi,
unsigned maxdisp,
bool cond,
int ubr)
320 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
325 std::vector<ImmBranch> ImmBranches;
327 const MipsSubtarget *STI =
nullptr;
328 const Mips16InstrInfo *TII;
329 MipsFunctionInfo *MFI;
331 MachineConstantPool *MCP =
nullptr;
333 unsigned PICLabelUId;
334 bool PrescannedForConstants =
false;
336 void initPICLabelUId(
unsigned UId) { PICLabelUId = UId; }
338 unsigned createPICLabelUId() {
return PICLabelUId++; }
340 void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs);
341 CPEntry *findConstPoolEntry(
unsigned CPI,
const MachineInstr *CPEMI);
342 Align getCPEAlign(
const MachineInstr &CPEMI);
343 unsigned getMaxDisp(
const CPUser &U)
const {
344 int SmallOffset = STI->getCLOpts().constant_islands_small_offset;
345 return SmallOffset ? SmallOffset :
U.getMaxDisp();
347 void initializeFunctionInfo(
const std::vector<MachineInstr*> &CPEMIs);
348 unsigned getOffsetOf(MachineInstr *
MI)
const;
349 unsigned getUserOffset(CPUser&)
const;
352 bool isOffsetInRange(
unsigned UserOffset,
unsigned TrialOffset,
353 unsigned Disp,
bool NegativeOK);
354 bool isOffsetInRange(
unsigned UserOffset,
unsigned TrialOffset,
357 void computeBlockSize(MachineBasicBlock *
MBB);
358 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &
MI);
359 void updateForInsertedWaterBlock(MachineBasicBlock *NewBB);
360 void adjustBBOffsetsAfter(MachineBasicBlock *BB);
361 bool decrementCPEReferenceCount(
unsigned CPI, MachineInstr* CPEMI);
362 int findInRangeCPEntry(CPUser& U,
unsigned UserOffset);
363 int findLongFormInRangeCPEntry(CPUser& U,
unsigned UserOffset);
364 bool findAvailableWater(CPUser&U,
unsigned UserOffset,
365 water_iterator &WaterIter);
366 void createNewWater(
unsigned CPUserIndex,
unsigned UserOffset,
367 MachineBasicBlock *&NewMBB);
368 bool handleConstantPoolUser(
unsigned CPUserIndex);
369 void removeDeadCPEMI(MachineInstr *CPEMI);
370 bool removeUnusedCPEntries();
371 bool isCPEntryInRange(MachineInstr *
MI,
unsigned UserOffset,
372 MachineInstr *CPEMI,
unsigned Disp,
bool NegOk,
373 bool DoDump =
false);
374 bool isWaterInRange(
unsigned UserOffset, MachineBasicBlock *Water,
375 CPUser &U,
unsigned &Growth);
376 bool isBBInRange(MachineInstr *
MI, MachineBasicBlock *BB,
unsigned Disp);
377 bool fixupImmediateBr(ImmBranch &Br);
378 bool fixupConditionalBr(ImmBranch &Br);
379 bool fixupUnconditionalBr(ImmBranch &Br);
381 void prescanForConstants();
389char MipsConstantIslandLegacy::ID = 0;
391bool MipsConstantIslandImpl::isOffsetInRange(
unsigned UserOffset,
392 unsigned TrialOffset,
394 return isOffsetInRange(UserOffset, TrialOffset, getMaxDisp(U),
U.NegOk);
397#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
400 for (
unsigned J = 0,
E = BBInfo.size(); J !=
E; ++J) {
401 const BasicBlockInfo &BBI = BBInfo[J];
408bool MipsConstantIslandImpl::runOnMachineFunction(
MachineFunction &mf) {
420 MFI = MF->
getInfo<MipsFunctionInfo>();
427 if (!PrescannedForConstants) prescanForConstants();
436 bool MadeChange =
false;
440 std::vector<MachineInstr*> CPEMIs;
442 doInitialPlacement(CPEMIs);
445 initPICLabelUId(CPEMIs.size());
450 initializeFunctionInfo(CPEMIs);
455 MadeChange |= removeUnusedCPEntries();
459 unsigned NoCPIters = 0, NoBRIters = 0;
462 LLVM_DEBUG(
dbgs() <<
"Beginning CP iteration #" << NoCPIters <<
'\n');
463 bool CPChange =
false;
464 for (
unsigned i = 0, e = CPUsers.size(); i != e; ++i)
465 CPChange |= handleConstantPoolUser(i);
466 if (CPChange && ++NoCPIters > 30)
472 NewWaterList.
clear();
474 LLVM_DEBUG(
dbgs() <<
"Beginning BR iteration #" << NoBRIters <<
'\n');
475 bool BRChange =
false;
476 for (
unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
477 BRChange |= fixupImmediateBr(ImmBranches[i]);
478 if (BRChange && ++NoBRIters > 30)
481 if (!CPChange && !BRChange)
498void MipsConstantIslandImpl::doInitialPlacement(
499 std::vector<MachineInstr *> &CPEMIs) {
526 const std::vector<MachineConstantPoolEntry> &CPs = MCP->
getConstants();
529 for (
unsigned i = 0, e = CPs.size(); i != e; ++i) {
530 unsigned Size = CPs[i].getSizeInBytes(TD);
531 assert(
Size >= 4 &&
"Too small constant pool entry");
538 unsigned LogAlign =
Log2(Alignment);
541 MachineInstr *CPEMI =
545 CPEMIs.push_back(CPEMI);
549 for (
unsigned a = LogAlign + 1; a <=
Log2(MaxAlign); ++a)
550 if (InsPoint[a] == InsAt)
553 CPEntries.emplace_back(1, CPEntry(CPEMI, i));
555 LLVM_DEBUG(
dbgs() <<
"Moved CPI#" << i <<
" to end of function, size = "
567 if (std::next(
MBBI) ==
MBB->getParent()->end())
576MipsConstantIslandImpl::CPEntry *
577MipsConstantIslandImpl::findConstPoolEntry(
unsigned CPI,
578 const MachineInstr *CPEMI) {
579 std::vector<CPEntry> &CPEs = CPEntries[CPI];
582 for (CPEntry &CPE : CPEs) {
583 if (CPE.CPEMI == CPEMI)
591Align MipsConstantIslandImpl::getCPEAlign(
const MachineInstr &CPEMI) {
595 if (!STI->
getCLOpts().align_constant_islands)
599 assert(CPI < MCP->getConstants().
size() &&
"Invalid constant pool index.");
606void MipsConstantIslandImpl::initializeFunctionInfo(
607 const std::vector<MachineInstr *> &CPEMIs) {
615 for (MachineBasicBlock &
MBB : *MF)
616 computeBlockSize(&
MBB);
619 adjustBBOffsetsAfter(&MF->front());
622 for (MachineBasicBlock &
MBB : *MF) {
626 WaterList.push_back(&
MBB);
627 for (MachineInstr &
MI :
MBB) {
628 if (
MI.isDebugInstr())
631 int Opc =
MI.getOpcode();
650 case Mips::BeqzRxImm16:
656 case Mips::BeqzRxImmX16:
662 case Mips::BnezRxImm16:
668 case Mips::BnezRxImmX16:
700 unsigned MaxOffs = ((1 << (
Bits-1))-1) * Scale;
701 ImmBranches.push_back(ImmBranch(&
MI, MaxOffs, isCond, UOpc));
704 if (
Opc == Mips::CONSTPOOL_ENTRY)
708 for (
const MachineOperand &MO :
MI.operands())
717 unsigned LongFormBits = 0;
718 unsigned LongFormScale = 0;
719 unsigned LongFormOpcode = 0;
723 case Mips::LwRxPcTcp16:
726 LongFormOpcode = Mips::LwRxPcTcpX16;
730 case Mips::LwRxPcTcpX16:
737 unsigned CPI = MO.getIndex();
738 MachineInstr *CPEMI = CPEMIs[CPI];
739 unsigned MaxOffs = ((1 <<
Bits)-1) * Scale;
740 unsigned LongFormMaxOffs = ((1 << LongFormBits)-1) * LongFormScale;
741 CPUsers.push_back(CPUser(&
MI, CPEMI, MaxOffs, NegOk, LongFormMaxOffs,
745 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
746 assert(CPE &&
"Cannot find a corresponding CPEntry!");
759void MipsConstantIslandImpl::computeBlockSize(MachineBasicBlock *
MBB) {
763 for (
const MachineInstr &
MI : *
MBB)
770unsigned MipsConstantIslandImpl::getOffsetOf(MachineInstr *
MI)
const {
780 assert(
I !=
MBB->
end() &&
"Didn't find MI in its own basic block?");
790 return LHS->getNumber() <
RHS->getNumber();
796void MipsConstantIslandImpl::updateForInsertedWaterBlock(
797 MachineBasicBlock *NewBB) {
803 BBInfo.insert(BBInfo.begin() + NewBB->
getNumber(), BasicBlockInfo());
808 WaterList.insert(IP, NewBB);
811unsigned MipsConstantIslandImpl::getUserOffset(CPUser &U)
const {
812 return getOffsetOf(
U.MI);
819MipsConstantIslandImpl::splitBlockBeforeInstr(MachineInstr &
MI) {
820 MachineBasicBlock *OrigBB =
MI.getParent();
823 MachineBasicBlock *NewBB =
826 MF->insert(
MBBI, NewBB);
847 MF->RenumberBlocks(NewBB);
851 BBInfo.insert(BBInfo.begin() + NewBB->
getNumber(), BasicBlockInfo());
858 MachineBasicBlock* WaterBB = *IP;
859 if (WaterBB == OrigBB)
860 WaterList.insert(std::next(IP), NewBB);
862 WaterList.insert(IP, OrigBB);
863 NewWaterList.
insert(OrigBB);
870 computeBlockSize(OrigBB);
874 computeBlockSize(NewBB);
877 adjustBBOffsetsAfter(OrigBB);
885bool MipsConstantIslandImpl::isOffsetInRange(
unsigned UserOffset,
886 unsigned TrialOffset,
889 if (UserOffset <= TrialOffset) {
891 if (TrialOffset - UserOffset <= MaxDisp)
893 }
else if (NegativeOK) {
894 if (UserOffset - TrialOffset <= MaxDisp)
904bool MipsConstantIslandImpl::isWaterInRange(
unsigned UserOffset,
905 MachineBasicBlock *Water, CPUser &U,
907 unsigned CPEOffset = BBInfo[Water->
getNumber()].postOffset();
908 unsigned NextBlockOffset;
909 Align NextBlockAlignment;
911 if (NextBlock == MF->end()) {
912 NextBlockOffset = BBInfo[Water->
getNumber()].postOffset();
913 NextBlockAlignment =
Align(1);
915 NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset;
916 NextBlockAlignment = NextBlock->getAlignment();
918 unsigned Size =
U.CPEMI->getOperand(2).getImm();
919 unsigned CPEEnd = CPEOffset +
Size;
924 if (CPEEnd > NextBlockOffset) {
925 Growth = CPEEnd - NextBlockOffset;
933 if (CPEOffset < UserOffset)
934 UserOffset += Growth;
939 return isOffsetInRange(UserOffset, CPEOffset, U);
944bool MipsConstantIslandImpl::isCPEntryInRange(MachineInstr *
MI,
947 unsigned MaxDisp,
bool NegOk,
949 unsigned CPEOffset = getOffsetOf(CPEMI);
953 unsigned Block =
MI->getParent()->getNumber();
954 const BasicBlockInfo &BBI = BBInfo[
Block];
956 <<
" max delta=" << MaxDisp
957 <<
format(
" insn address=%#x", UserOffset) <<
" in "
960 <<
format(
"CPE address=%#x offset=%+d: ", CPEOffset,
961 int(CPEOffset - UserOffset));
965 return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
972 if (
MBB->pred_size() != 1 ||
MBB->succ_size() != 1)
983void MipsConstantIslandImpl::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
985 for(
unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
988 unsigned Offset = BBInfo[i - 1].Offset + BBInfo[i - 1].Size;
989 BBInfo[i].Offset =
Offset;
997bool MipsConstantIslandImpl::decrementCPEReferenceCount(
unsigned CPI,
998 MachineInstr *CPEMI) {
1000 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
1001 assert(CPE &&
"Unexpected!");
1002 if (--CPE->RefCount == 0) {
1003 removeDeadCPEMI(CPEMI);
1004 CPE->CPEMI =
nullptr;
1017int MipsConstantIslandImpl::findInRangeCPEntry(CPUser &U,
unsigned UserOffset) {
1018 MachineInstr *UserMI =
U.MI;
1019 MachineInstr *CPEMI =
U.CPEMI;
1022 if (isCPEntryInRange(UserMI, UserOffset, CPEMI, getMaxDisp(U),
U.NegOk,
1030 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1031 for (CPEntry &CPE : CPEs) {
1033 if (CPE.CPEMI == CPEMI)
1036 if (CPE.CPEMI ==
nullptr)
1038 if (isCPEntryInRange(UserMI, UserOffset, CPE.CPEMI, getMaxDisp(U),
1040 LLVM_DEBUG(
dbgs() <<
"Replacing CPE#" << CPI <<
" with CPE#" << CPE.CPI
1043 U.CPEMI = CPE.CPEMI;
1045 for (MachineOperand &MO : UserMI->
operands())
1047 MO.setIndex(CPE.CPI);
1054 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1068int MipsConstantIslandImpl::findLongFormInRangeCPEntry(CPUser &U,
1069 unsigned UserOffset) {
1070 MachineInstr *UserMI =
U.MI;
1071 MachineInstr *CPEMI =
U.CPEMI;
1074 if (isCPEntryInRange(UserMI, UserOffset, CPEMI,
1075 U.getLongFormMaxDisp(),
U.NegOk,
1079 U.setMaxDisp(
U.getLongFormMaxDisp());
1085 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1086 for (CPEntry &CPE : CPEs) {
1088 if (CPE.CPEMI == CPEMI)
1091 if (CPE.CPEMI ==
nullptr)
1093 if (isCPEntryInRange(UserMI, UserOffset, CPE.CPEMI,
U.getLongFormMaxDisp(),
1095 LLVM_DEBUG(
dbgs() <<
"Replacing CPE#" << CPI <<
" with CPE#" << CPE.CPI
1098 U.CPEMI = CPE.CPEMI;
1100 for (MachineOperand &MO : UserMI->
operands())
1102 MO.setIndex(CPE.CPI);
1109 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1120 return ((1<<10)-1)*2;
1122 return ((1<<16)-1)*2;
1126 return ((1<<16)-1)*2;
1137bool MipsConstantIslandImpl::findAvailableWater(CPUser &U,
unsigned UserOffset,
1138 water_iterator &WaterIter) {
1139 if (WaterList.empty())
1142 unsigned BestGrowth = ~0
u;
1143 for (water_iterator IP = std::prev(WaterList.end()),
B = WaterList.begin();;
1145 MachineBasicBlock* WaterBB = *IP;
1155 if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
1156 (WaterBB->
getNumber() <
U.HighWaterMark->getNumber() ||
1157 NewWaterList.
count(WaterBB)) && Growth < BestGrowth) {
1159 BestGrowth = Growth;
1162 <<
" Growth=" << Growth <<
'\n');
1165 if (BestGrowth == 0)
1171 return BestGrowth != ~0
u;
1181void MipsConstantIslandImpl::createNewWater(
unsigned CPUserIndex,
1182 unsigned UserOffset,
1183 MachineBasicBlock *&NewMBB) {
1184 CPUser &
U = CPUsers[CPUserIndex];
1185 MachineInstr *UserMI =
U.MI;
1186 MachineInstr *CPEMI =
U.CPEMI;
1187 MachineBasicBlock *UserMBB = UserMI->
getParent();
1188 const BasicBlockInfo &UserBBI = BBInfo[UserMBB->
getNumber()];
1196 unsigned CPEOffset = UserBBI.
postOffset() + Delta;
1198 if (isOffsetInRange(UserOffset, CPEOffset, U)) {
1200 <<
format(
", expected CPE offset %#x\n", CPEOffset));
1207 int UncondBr = Mips::Bimm16;
1210 ImmBranches.push_back(ImmBranch(&UserMBB->
back(),
1211 MaxDisp,
false, UncondBr));
1212 BBInfo[UserMBB->
getNumber()].Size += Delta;
1213 adjustBBOffsetsAfter(UserMBB);
1224 unsigned BaseInsertOffset = UserOffset + getMaxDisp(U);
1231 BaseInsertOffset -= 4;
1234 <<
" la=" <<
Log2(Align) <<
'\n');
1240 if (BaseInsertOffset + 8 >= UserBBI.
postOffset()) {
1244 unsigned EndInsertOffset = BaseInsertOffset + 4 +
1248 unsigned CPUIndex = CPUserIndex+1;
1249 unsigned NumCPUsers = CPUsers.size();
1251 for (
unsigned Offset = UserOffset +
TII->getInstSizeInBytes(*UserMI);
1252 Offset < BaseInsertOffset;
1254 assert(
MI != UserMBB->
end() &&
"Fell off end of block");
1255 if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].
MI ==
MI) {
1256 CPUser &
U = CPUsers[CPUIndex];
1257 if (!isOffsetInRange(
Offset, EndInsertOffset, U)) {
1259 BaseInsertOffset -=
Align.value();
1260 EndInsertOffset -=
Align.value();
1266 EndInsertOffset +=
U.CPEMI->getOperand(2).getImm();
1271 NewMBB = splitBlockBeforeInstr(*--
MI);
1278bool MipsConstantIslandImpl::handleConstantPoolUser(
unsigned CPUserIndex) {
1279 CPUser &
U = CPUsers[CPUserIndex];
1280 MachineInstr *UserMI =
U.MI;
1281 MachineInstr *CPEMI =
U.CPEMI;
1285 unsigned UserOffset = getUserOffset(U);
1289 int result = findInRangeCPEntry(U, UserOffset);
1290 if (result==1)
return false;
1291 else if (result==2)
return true;
1294 MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
1295 MachineBasicBlock *NewMBB;
1297 if (findAvailableWater(U, UserOffset, IP)) {
1299 MachineBasicBlock *WaterBB = *IP;
1304 if (NewWaterList.
erase(WaterBB))
1305 NewWaterList.
insert(NewIsland);
1313 if (!STI->
getCLOpts().constant_islands_no_load_relaxation) {
1314 result = findLongFormInRangeCPEntry(U, UserOffset);
1315 if (result != 0)
return true;
1318 createNewWater(CPUserIndex, UserOffset, NewMBB);
1325 MachineBasicBlock *WaterBB = &*--NewMBB->
getIterator();
1327 if (IP != WaterList.end())
1328 NewWaterList.
erase(WaterBB);
1331 NewWaterList.
insert(NewIsland);
1338 if (IP != WaterList.end())
1339 WaterList.erase(IP);
1345 updateForInsertedWaterBlock(NewIsland);
1348 decrementCPEReferenceCount(CPI, CPEMI);
1352 unsigned ID = createPICLabelUId();
1356 U.HighWaterMark = NewIsland;
1359 CPEntries[CPI].push_back(CPEntry(
U.CPEMI, ID, 1));
1367 adjustBBOffsetsAfter(&*--NewIsland->
getIterator());
1370 for (MachineOperand &MO : UserMI->
operands())
1377 dbgs() <<
" Moved CPE to #" << ID <<
" CPI=" << CPI
1385void MipsConstantIslandImpl::removeDeadCPEMI(MachineInstr *CPEMI) {
1386 MachineBasicBlock *CPEBB = CPEMI->
getParent();
1391 if (CPEBB->
empty()) {
1401 adjustBBOffsetsAfter(CPEBB);
1411bool MipsConstantIslandImpl::removeUnusedCPEntries() {
1412 unsigned MadeChange =
false;
1413 for (std::vector<CPEntry> &CPEs : CPEntries) {
1414 for (CPEntry &CPE : CPEs) {
1415 if (CPE.RefCount == 0 && CPE.CPEMI) {
1416 removeDeadCPEMI(CPE.CPEMI);
1417 CPE.CPEMI =
nullptr;
1427bool MipsConstantIslandImpl::isBBInRange(MachineInstr *
MI,
1428 MachineBasicBlock *DestBB,
1431 unsigned BrOffset = getOffsetOf(
MI) + PCAdj;
1432 unsigned DestOffset = BBInfo[DestBB->
getNumber()].Offset;
1436 <<
" max delta=" << MaxDisp <<
" from " << getOffsetOf(
MI)
1437 <<
" to " << DestOffset <<
" offset "
1438 <<
int(DestOffset - BrOffset) <<
"\t" << *
MI);
1440 if (BrOffset <= DestOffset) {
1442 if (DestOffset-BrOffset <= MaxDisp)
1445 if (BrOffset-DestOffset <= MaxDisp)
1453bool MipsConstantIslandImpl::fixupImmediateBr(ImmBranch &Br) {
1454 MachineInstr *
MI = Br.MI;
1456 MachineBasicBlock *DestBB =
MI->getOperand(TargetOperand).getMBB();
1459 if (isBBInRange(
MI, DestBB, Br.MaxDisp))
1463 return fixupUnconditionalBr(Br);
1464 return fixupConditionalBr(Br);
1471bool MipsConstantIslandImpl::fixupUnconditionalBr(ImmBranch &Br) {
1472 MachineInstr *
MI = Br.MI;
1474 MachineBasicBlock *DestBB =
MI->getOperand(0).getMBB();
1476 unsigned BimmX16MaxDisp = ((1 << 16)-1) * 2;
1477 if (isBBInRange(
MI, DestBB, BimmX16MaxDisp)) {
1478 Br.MaxDisp = BimmX16MaxDisp;
1479 MI->setDesc(
TII->get(Mips::BimmX16));
1494 Br.MaxDisp = ((1<<24)-1) * 2;
1495 MI->setDesc(
TII->get(Mips::JalB16));
1498 adjustBBOffsetsAfter(
MBB);
1509bool MipsConstantIslandImpl::fixupConditionalBr(ImmBranch &Br) {
1510 MachineInstr *
MI = Br.MI;
1512 MachineBasicBlock *DestBB =
MI->getOperand(TargetOperand).getMBB();
1513 unsigned Opcode =
MI->getOpcode();
1518 if (isBBInRange(
MI, DestBB, LongFormMaxOff)) {
1519 Br.MaxDisp = LongFormMaxOff;
1520 MI->setDesc(
TII->get(LongFormOpcode));
1536 MachineInstr *BMI = &
MBB->
back();
1538 unsigned OppositeBranchOpcode =
TII->getOppositeBranchOpc(Opcode);
1552 MachineBasicBlock *NewDest =
1554 if (isBBInRange(
MI, NewDest, Br.MaxDisp)) {
1556 dbgs() <<
" Invert Bcc condition and swap its destination with "
1558 MI->setDesc(
TII->get(OppositeBranchOpcode));
1560 MI->getOperand(TargetOperand).setMBB(NewDest);
1567 splitBlockBeforeInstr(*
MI);
1570 int delta =
TII->getInstSizeInBytes(
MBB->
back());
1578 <<
" also invert condition and change dest. to "
1583 if (
MI->getNumExplicitOperands() == 2) {
1585 .
addReg(
MI->getOperand(0).getReg())
1596 ImmBranches.push_back(ImmBranch(&
MBB->
back(), MaxDisp,
false, Br.UncondBr));
1599 BBInfo[
MI->getParent()->getNumber()].Size -=
TII->getInstSizeInBytes(*
MI);
1600 MI->eraseFromParent();
1601 adjustBBOffsetsAfter(
MBB);
1605void MipsConstantIslandImpl::prescanForConstants() {
1606 for (MachineBasicBlock &
B : *MF) {
1607 for (MachineInstr &
MI :
B) {
1608 switch (
MI.getDesc().getOpcode()) {
1609 case Mips::LwConstant32: {
1610 PrescannedForConstants =
true;
1613 MachineOperand &
Literal =
MI.getOperand(1);
1617 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext());
1620 MI.getOperand(2).ChangeToImmediate(index);
1622 MI.setDesc(
TII->get(Mips::LwRxPcTcp16));
1623 MI.removeOperand(1);
1624 MI.removeOperand(1);
1638 return new MipsConstantIslandLegacy();
1641bool MipsConstantIslandLegacy::runOnMachineFunction(
MachineFunction &MF) {
1642 MipsConstantIslandImpl Impl;
1643 return Impl.runOnMachineFunction(MF);
1649 MipsConstantIslandImpl Impl;
1650 return Impl.runOnMachineFunction(MF)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool CompareMBBNumbers(const MachineBasicBlock *LHS, const MachineBasicBlock *RHS)
CompareMBBNumbers - Little predicate function to sort the WaterList by MBB ID.
static unsigned getUnconditionalBrDisp(int Opc)
getUnconditionalBrDisp - Returns the maximum displacement that can fit in the specific unconditional ...
static bool BBIsJumpedOver(MachineBasicBlock *MBB)
BBIsJumpedOver - Return true of the specified basic block's only predecessor unconditionally branches...
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_PREFERRED_TYPE(T)
\macro LLVM_PREFERRED_TYPE Adjust type of bit-field in debug info.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const HexagonInstrInfo * TII
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
static unsigned int longformBranchOpcode(unsigned int Opcode)
static unsigned int branchMaxOffsets(unsigned int Opcode)
static bool BBHasFallthrough(MachineBasicBlock *MBB)
BBHasFallthrough - Return true if the specified basic block can fallthrough into the block immediatel...
static unsigned int branchTargetOperand(MachineInstr *MI)
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V, bool ImplicitTrunc=false)
Return a ConstantInt with the specified value for the specified type.
FunctionPass class - This class is used to implement most global optimizations.
LLVM_ABI void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
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 setAlignment(Align A)
Set alignment of the basic block.
LLVM_ABI void dump() const
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic 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 '...
Align getAlignment() const
Return alignment of the basic block.
MachineInstrBundleIterator< MachineInstr > iterator
Align getConstantPoolAlign() const
Return the alignment required by the whole constant pool, of which the first element must be aligned.
const std::vector< MachineConstantPoolEntry > & getConstants() const
bool isEmpty() const
isEmpty - Return true if this constant pool contains no constants.
LLVM_ABI unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
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.
void ensureAlignment(Align A)
ensureAlignment - Make sure the function is at least A bytes aligned.
void push_back(MachineBasicBlock *MBB)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
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.
BasicBlockListType::const_iterator const_iterator
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addConstantPoolIndex(unsigned Idx, int Offset=0, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
bool isUnconditionalBranch(QueryType Type=AnyInBundle) const
Return true if this is a branch which always transfers control flow to some other block.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineBasicBlock * getMBB() const
void setMBB(MachineBasicBlock *MBB)
static MachineOperand CreateCPI(unsigned Idx, int Offset, unsigned TargetFlags=0)
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const MipsInstrInfo * getInstrInfo() const override
bool inMips16Mode() const
const MipsOptions & getCLOpts() const
bool useConstantIslands() const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
self_iterator getIterator()
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
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.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
FunctionPass * createMipsConstantIslandLegacyPass()
Returns a pass that converts branches to long branches.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
APFloat neg(APFloat X)
Returns the negated value of the argument.
unsigned Log2(Align A)
Returns the log2 of the alignment.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
unsigned Size
Size - Size of the basic block in bytes.
unsigned postOffset(Align Alignment=Align(1)) const
Compute the offset immediately following this block.
unsigned Offset
Offset - Distance from the beginning of the function to the beginning of this basic block.