39#include "llvm/Config/llvm-config.h"
58#define DEBUG_TYPE "mips-constant-islands"
61STATISTIC(NumSplit,
"Number of uncond branches inserted");
62STATISTIC(NumCBrFixed,
"Number of cond branches fixed");
63STATISTIC(NumUBrFixed,
"Number of uncond branches fixed");
66 switch (
MI->getOpcode()) {
75 case Mips::BeqzRxImm16:
76 case Mips::BeqzRxImmX16:
77 case Mips::BnezRxImm16:
78 case Mips::BnezRxImmX16:
91 return Mips::BteqzX16;
94 return Mips::BtnezX16;
97 case Mips::BeqzRxImm16:
98 case Mips::BeqzRxImmX16:
99 return Mips::BeqzRxImmX16;
100 case Mips::BnezRxImm16:
101 case Mips::BnezRxImmX16:
102 return Mips::BnezRxImmX16;
112 unsigned Bits, Scale;
122 case Mips::BeqzRxImm16:
126 case Mips::BeqzRxImmX16:
130 case Mips::BnezRxImm16:
134 case Mips::BnezRxImmX16:
157 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
182 MipsConstantIslandLegacy() : MachineFunctionPass(ID) {}
184 StringRef getPassName()
const override {
return "Mips Constant Islands"; }
188 MachineFunctionProperties getRequiredProperties()
const override {
189 return MachineFunctionProperties().setNoVRegs();
193 class MipsConstantIslandImpl {
196 struct BasicBlockInfo {
215 BasicBlockInfo() =
default;
217 unsigned postOffset()
const {
return Offset + Size; }
220 std::vector<BasicBlockInfo> BBInfo;
225 std::vector<MachineBasicBlock*> WaterList;
229 SmallPtrSet<MachineBasicBlock *, 4> NewWaterList;
231 using water_iterator = std::vector<MachineBasicBlock *>::iterator;
248 MachineBasicBlock *HighWaterMark;
252 unsigned LongFormMaxDisp;
254 unsigned LongFormOpcode;
259 CPUser(MachineInstr *mi, MachineInstr *cpemi,
unsigned maxdisp,
261 unsigned longformmaxdisp,
unsigned longformopcode)
262 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp),
263 LongFormMaxDisp(longformmaxdisp), LongFormOpcode(longformopcode),
265 HighWaterMark = CPEMI->getParent();
269 unsigned getMaxDisp()
const {
return MaxDisp; }
271 void setMaxDisp(
unsigned val) {
275 unsigned getLongFormMaxDisp()
const {
276 return LongFormMaxDisp;
279 unsigned getLongFormOpcode()
const {
280 return LongFormOpcode;
286 std::vector<CPUser> CPUsers;
296 CPEntry(MachineInstr *cpemi,
unsigned cpi,
unsigned rc = 0)
297 : CPEMI(cpemi), CPI(cpi), RefCount(
rc) {}
305 std::vector<std::vector<CPEntry>> CPEntries;
313 unsigned MaxDisp : 31;
318 ImmBranch(MachineInstr *mi,
unsigned maxdisp,
bool cond,
int ubr)
319 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
324 std::vector<ImmBranch> ImmBranches;
326 const MipsSubtarget *STI =
nullptr;
327 const Mips16InstrInfo *TII;
329 MachineConstantPool *MCP =
nullptr;
331 unsigned PICLabelUId;
332 bool PrescannedForConstants =
false;
334 void initPICLabelUId(
unsigned UId) { PICLabelUId = UId; }
336 unsigned createPICLabelUId() {
return PICLabelUId++; }
338 void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs);
339 CPEntry *findConstPoolEntry(
unsigned CPI,
const MachineInstr *CPEMI);
340 Align getCPEAlign(
const MachineInstr &CPEMI);
341 unsigned getMaxDisp(
const CPUser &U)
const {
342 int SmallOffset = STI->getCLOpts().constant_islands_small_offset;
343 return SmallOffset ? SmallOffset :
U.getMaxDisp();
345 void initializeFunctionInfo(
const std::vector<MachineInstr*> &CPEMIs);
346 unsigned getOffsetOf(MachineInstr *
MI)
const;
347 unsigned getUserOffset(CPUser&)
const;
350 bool isOffsetInRange(
unsigned UserOffset,
unsigned TrialOffset,
351 unsigned Disp,
bool NegativeOK);
352 bool isOffsetInRange(
unsigned UserOffset,
unsigned TrialOffset,
355 void computeBlockSize(MachineBasicBlock *
MBB);
356 MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &
MI);
357 void updateForInsertedWaterBlock(MachineBasicBlock *NewBB);
358 void adjustBBOffsetsAfter(MachineBasicBlock *BB);
359 bool decrementCPEReferenceCount(
unsigned CPI, MachineInstr* CPEMI);
360 int findInRangeCPEntry(CPUser& U,
unsigned UserOffset);
361 int findLongFormInRangeCPEntry(CPUser& U,
unsigned UserOffset);
362 bool findAvailableWater(CPUser&U,
unsigned UserOffset,
363 water_iterator &WaterIter);
364 void createNewWater(
unsigned CPUserIndex,
unsigned UserOffset,
365 MachineBasicBlock *&NewMBB);
366 bool handleConstantPoolUser(
unsigned CPUserIndex);
367 void removeDeadCPEMI(MachineInstr *CPEMI);
368 bool removeUnusedCPEntries();
369 bool isCPEntryInRange(MachineInstr *
MI,
unsigned UserOffset,
370 MachineInstr *CPEMI,
unsigned Disp,
bool NegOk,
371 bool DoDump =
false);
372 bool isWaterInRange(
unsigned UserOffset, MachineBasicBlock *Water,
373 CPUser &U,
unsigned &Growth);
374 bool isBBInRange(MachineInstr *
MI, MachineBasicBlock *BB,
unsigned Disp);
375 bool fixupImmediateBr(ImmBranch &Br);
376 bool fixupConditionalBr(ImmBranch &Br);
377 bool fixupUnconditionalBr(ImmBranch &Br);
379 void prescanForConstants();
387char MipsConstantIslandLegacy::ID = 0;
389bool MipsConstantIslandImpl::isOffsetInRange(
unsigned UserOffset,
390 unsigned TrialOffset,
392 return isOffsetInRange(UserOffset, TrialOffset, getMaxDisp(U),
U.NegOk);
395#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
398 for (
unsigned J = 0,
E = BBInfo.size(); J !=
E; ++J) {
399 const BasicBlockInfo &BBI = BBInfo[J];
406bool MipsConstantIslandImpl::runOnMachineFunction(
MachineFunction &mf) {
424 if (!PrescannedForConstants) prescanForConstants();
433 bool MadeChange =
false;
437 std::vector<MachineInstr*> CPEMIs;
439 doInitialPlacement(CPEMIs);
442 initPICLabelUId(CPEMIs.size());
447 initializeFunctionInfo(CPEMIs);
452 MadeChange |= removeUnusedCPEntries();
456 unsigned NoCPIters = 0, NoBRIters = 0;
459 LLVM_DEBUG(
dbgs() <<
"Beginning CP iteration #" << NoCPIters <<
'\n');
460 bool CPChange =
false;
461 for (
unsigned i = 0, e = CPUsers.size(); i != e; ++i)
462 CPChange |= handleConstantPoolUser(i);
463 if (CPChange && ++NoCPIters > 30)
469 NewWaterList.
clear();
471 LLVM_DEBUG(
dbgs() <<
"Beginning BR iteration #" << NoBRIters <<
'\n');
472 bool BRChange =
false;
473 for (
unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
474 BRChange |= fixupImmediateBr(ImmBranches[i]);
475 if (BRChange && ++NoBRIters > 30)
478 if (!CPChange && !BRChange)
495void MipsConstantIslandImpl::doInitialPlacement(
496 std::vector<MachineInstr *> &CPEMIs) {
523 const std::vector<MachineConstantPoolEntry> &CPs = MCP->
getConstants();
526 for (
unsigned i = 0, e = CPs.size(); i != e; ++i) {
527 unsigned Size = CPs[i].getSizeInBytes(TD);
528 assert(
Size >= 4 &&
"Too small constant pool entry");
535 unsigned LogAlign =
Log2(Alignment);
538 MachineInstr *CPEMI =
542 CPEMIs.push_back(CPEMI);
546 for (
unsigned a = LogAlign + 1; a <=
Log2(MaxAlign); ++a)
547 if (InsPoint[a] == InsAt)
550 CPEntries.emplace_back(1, CPEntry(CPEMI, i));
552 LLVM_DEBUG(
dbgs() <<
"Moved CPI#" << i <<
" to end of function, size = "
564 if (std::next(
MBBI) ==
MBB->getParent()->end())
573MipsConstantIslandImpl::CPEntry *
574MipsConstantIslandImpl::findConstPoolEntry(
unsigned CPI,
575 const MachineInstr *CPEMI) {
576 std::vector<CPEntry> &CPEs = CPEntries[CPI];
579 for (CPEntry &CPE : CPEs) {
580 if (CPE.CPEMI == CPEMI)
588Align MipsConstantIslandImpl::getCPEAlign(
const MachineInstr &CPEMI) {
592 if (!STI->
getCLOpts().align_constant_islands)
596 assert(CPI < MCP->getConstants().
size() &&
"Invalid constant pool index.");
603void MipsConstantIslandImpl::initializeFunctionInfo(
604 const std::vector<MachineInstr *> &CPEMIs) {
612 for (MachineBasicBlock &
MBB : *MF)
613 computeBlockSize(&
MBB);
616 adjustBBOffsetsAfter(&MF->front());
619 for (MachineBasicBlock &
MBB : *MF) {
623 WaterList.push_back(&
MBB);
624 for (MachineInstr &
MI :
MBB) {
625 if (
MI.isDebugInstr())
628 int Opc =
MI.getOpcode();
647 case Mips::BeqzRxImm16:
653 case Mips::BeqzRxImmX16:
659 case Mips::BnezRxImm16:
665 case Mips::BnezRxImmX16:
697 unsigned MaxOffs = ((1 << (
Bits-1))-1) * Scale;
698 ImmBranches.push_back(ImmBranch(&
MI, MaxOffs, isCond, UOpc));
701 if (
Opc == Mips::CONSTPOOL_ENTRY)
705 for (
const MachineOperand &MO :
MI.operands())
714 unsigned LongFormBits = 0;
715 unsigned LongFormScale = 0;
716 unsigned LongFormOpcode = 0;
720 case Mips::LwRxPcTcp16:
723 LongFormOpcode = Mips::LwRxPcTcpX16;
727 case Mips::LwRxPcTcpX16:
734 unsigned CPI = MO.getIndex();
735 MachineInstr *CPEMI = CPEMIs[CPI];
736 unsigned MaxOffs = ((1 <<
Bits)-1) * Scale;
737 unsigned LongFormMaxOffs = ((1 << LongFormBits)-1) * LongFormScale;
738 CPUsers.push_back(CPUser(&
MI, CPEMI, MaxOffs, NegOk, LongFormMaxOffs,
742 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
743 assert(CPE &&
"Cannot find a corresponding CPEntry!");
756void MipsConstantIslandImpl::computeBlockSize(MachineBasicBlock *
MBB) {
760 for (
const MachineInstr &
MI : *
MBB)
767unsigned MipsConstantIslandImpl::getOffsetOf(MachineInstr *
MI)
const {
777 assert(
I !=
MBB->
end() &&
"Didn't find MI in its own basic block?");
787 return LHS->getNumber() <
RHS->getNumber();
793void MipsConstantIslandImpl::updateForInsertedWaterBlock(
794 MachineBasicBlock *NewBB) {
800 BBInfo.insert(BBInfo.begin() + NewBB->
getNumber(), BasicBlockInfo());
805 WaterList.insert(IP, NewBB);
808unsigned MipsConstantIslandImpl::getUserOffset(CPUser &U)
const {
809 return getOffsetOf(
U.MI);
816MipsConstantIslandImpl::splitBlockBeforeInstr(MachineInstr &
MI) {
817 MachineBasicBlock *OrigBB =
MI.getParent();
820 MachineBasicBlock *NewBB =
823 MF->insert(
MBBI, NewBB);
844 MF->RenumberBlocks(NewBB);
848 BBInfo.insert(BBInfo.begin() + NewBB->
getNumber(), BasicBlockInfo());
855 MachineBasicBlock* WaterBB = *IP;
856 if (WaterBB == OrigBB)
857 WaterList.insert(std::next(IP), NewBB);
859 WaterList.insert(IP, OrigBB);
860 NewWaterList.
insert(OrigBB);
867 computeBlockSize(OrigBB);
871 computeBlockSize(NewBB);
874 adjustBBOffsetsAfter(OrigBB);
882bool MipsConstantIslandImpl::isOffsetInRange(
unsigned UserOffset,
883 unsigned TrialOffset,
886 if (UserOffset <= TrialOffset) {
888 if (TrialOffset - UserOffset <= MaxDisp)
890 }
else if (NegativeOK) {
891 if (UserOffset - TrialOffset <= MaxDisp)
901bool MipsConstantIslandImpl::isWaterInRange(
unsigned UserOffset,
902 MachineBasicBlock *Water, CPUser &U,
904 unsigned CPEOffset = BBInfo[Water->
getNumber()].postOffset();
905 unsigned NextBlockOffset;
906 Align NextBlockAlignment;
908 if (NextBlock == MF->end()) {
909 NextBlockOffset = BBInfo[Water->
getNumber()].postOffset();
910 NextBlockAlignment =
Align(1);
912 NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset;
913 NextBlockAlignment = NextBlock->getAlignment();
915 unsigned Size =
U.CPEMI->getOperand(2).getImm();
916 unsigned CPEEnd = CPEOffset +
Size;
921 if (CPEEnd > NextBlockOffset) {
922 Growth = CPEEnd - NextBlockOffset;
930 if (CPEOffset < UserOffset)
931 UserOffset += Growth;
936 return isOffsetInRange(UserOffset, CPEOffset, U);
941bool MipsConstantIslandImpl::isCPEntryInRange(MachineInstr *
MI,
944 unsigned MaxDisp,
bool NegOk,
946 unsigned CPEOffset = getOffsetOf(CPEMI);
950 unsigned Block =
MI->getParent()->getNumber();
951 const BasicBlockInfo &BBI = BBInfo[
Block];
953 <<
" max delta=" << MaxDisp
954 <<
format(
" insn address=%#x", UserOffset) <<
" in "
957 <<
format(
"CPE address=%#x offset=%+d: ", CPEOffset,
958 int(CPEOffset - UserOffset));
962 return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
969 if (
MBB->pred_size() != 1 ||
MBB->succ_size() != 1)
980void MipsConstantIslandImpl::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
982 for(
unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
985 unsigned Offset = BBInfo[i - 1].Offset + BBInfo[i - 1].Size;
986 BBInfo[i].Offset =
Offset;
994bool MipsConstantIslandImpl::decrementCPEReferenceCount(
unsigned CPI,
995 MachineInstr *CPEMI) {
997 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
998 assert(CPE &&
"Unexpected!");
999 if (--CPE->RefCount == 0) {
1000 removeDeadCPEMI(CPEMI);
1001 CPE->CPEMI =
nullptr;
1014int MipsConstantIslandImpl::findInRangeCPEntry(CPUser &U,
unsigned UserOffset) {
1015 MachineInstr *UserMI =
U.MI;
1016 MachineInstr *CPEMI =
U.CPEMI;
1019 if (isCPEntryInRange(UserMI, UserOffset, CPEMI, getMaxDisp(U),
U.NegOk,
1027 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1028 for (CPEntry &CPE : CPEs) {
1030 if (CPE.CPEMI == CPEMI)
1033 if (CPE.CPEMI ==
nullptr)
1035 if (isCPEntryInRange(UserMI, UserOffset, CPE.CPEMI, getMaxDisp(U),
1037 LLVM_DEBUG(
dbgs() <<
"Replacing CPE#" << CPI <<
" with CPE#" << CPE.CPI
1040 U.CPEMI = CPE.CPEMI;
1042 for (MachineOperand &MO : UserMI->
operands())
1044 MO.setIndex(CPE.CPI);
1051 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1065int MipsConstantIslandImpl::findLongFormInRangeCPEntry(CPUser &U,
1066 unsigned UserOffset) {
1067 MachineInstr *UserMI =
U.MI;
1068 MachineInstr *CPEMI =
U.CPEMI;
1071 if (isCPEntryInRange(UserMI, UserOffset, CPEMI,
1072 U.getLongFormMaxDisp(),
U.NegOk,
1076 U.setMaxDisp(
U.getLongFormMaxDisp());
1082 std::vector<CPEntry> &CPEs = CPEntries[CPI];
1083 for (CPEntry &CPE : CPEs) {
1085 if (CPE.CPEMI == CPEMI)
1088 if (CPE.CPEMI ==
nullptr)
1090 if (isCPEntryInRange(UserMI, UserOffset, CPE.CPEMI,
U.getLongFormMaxDisp(),
1092 LLVM_DEBUG(
dbgs() <<
"Replacing CPE#" << CPI <<
" with CPE#" << CPE.CPI
1095 U.CPEMI = CPE.CPEMI;
1097 for (MachineOperand &MO : UserMI->
operands())
1099 MO.setIndex(CPE.CPI);
1106 return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1117 return ((1<<10)-1)*2;
1119 return ((1<<16)-1)*2;
1123 return ((1<<16)-1)*2;
1134bool MipsConstantIslandImpl::findAvailableWater(CPUser &U,
unsigned UserOffset,
1135 water_iterator &WaterIter) {
1136 if (WaterList.empty())
1139 unsigned BestGrowth = ~0
u;
1140 for (water_iterator IP = std::prev(WaterList.end()),
B = WaterList.begin();;
1142 MachineBasicBlock* WaterBB = *IP;
1152 if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
1153 (WaterBB->
getNumber() <
U.HighWaterMark->getNumber() ||
1154 NewWaterList.
count(WaterBB)) && Growth < BestGrowth) {
1156 BestGrowth = Growth;
1159 <<
" Growth=" << Growth <<
'\n');
1162 if (BestGrowth == 0)
1168 return BestGrowth != ~0
u;
1178void MipsConstantIslandImpl::createNewWater(
unsigned CPUserIndex,
1179 unsigned UserOffset,
1180 MachineBasicBlock *&NewMBB) {
1181 CPUser &
U = CPUsers[CPUserIndex];
1182 MachineInstr *UserMI =
U.MI;
1183 MachineInstr *CPEMI =
U.CPEMI;
1184 MachineBasicBlock *UserMBB = UserMI->
getParent();
1185 const BasicBlockInfo &UserBBI = BBInfo[UserMBB->
getNumber()];
1193 unsigned CPEOffset = UserBBI.
postOffset() + Delta;
1195 if (isOffsetInRange(UserOffset, CPEOffset, U)) {
1197 <<
format(
", expected CPE offset %#x\n", CPEOffset));
1204 int UncondBr = Mips::Bimm16;
1207 ImmBranches.push_back(ImmBranch(&UserMBB->
back(),
1208 MaxDisp,
false, UncondBr));
1209 BBInfo[UserMBB->
getNumber()].Size += Delta;
1210 adjustBBOffsetsAfter(UserMBB);
1221 unsigned BaseInsertOffset = UserOffset + getMaxDisp(U);
1228 BaseInsertOffset -= 4;
1231 <<
" la=" <<
Log2(Align) <<
'\n');
1237 if (BaseInsertOffset + 8 >= UserBBI.
postOffset()) {
1241 unsigned EndInsertOffset = BaseInsertOffset + 4 +
1245 unsigned CPUIndex = CPUserIndex+1;
1246 unsigned NumCPUsers = CPUsers.size();
1248 for (
unsigned Offset = UserOffset +
TII->getInstSizeInBytes(*UserMI);
1249 Offset < BaseInsertOffset;
1251 assert(
MI != UserMBB->
end() &&
"Fell off end of block");
1252 if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].
MI ==
MI) {
1253 CPUser &
U = CPUsers[CPUIndex];
1254 if (!isOffsetInRange(
Offset, EndInsertOffset, U)) {
1256 BaseInsertOffset -=
Align.value();
1257 EndInsertOffset -=
Align.value();
1263 EndInsertOffset +=
U.CPEMI->getOperand(2).getImm();
1268 NewMBB = splitBlockBeforeInstr(*--
MI);
1275bool MipsConstantIslandImpl::handleConstantPoolUser(
unsigned CPUserIndex) {
1276 CPUser &
U = CPUsers[CPUserIndex];
1277 MachineInstr *UserMI =
U.MI;
1278 MachineInstr *CPEMI =
U.CPEMI;
1282 unsigned UserOffset = getUserOffset(U);
1286 int result = findInRangeCPEntry(U, UserOffset);
1287 if (result==1)
return false;
1288 else if (result==2)
return true;
1291 MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
1292 MachineBasicBlock *NewMBB;
1294 if (findAvailableWater(U, UserOffset, IP)) {
1296 MachineBasicBlock *WaterBB = *IP;
1301 if (NewWaterList.
erase(WaterBB))
1302 NewWaterList.
insert(NewIsland);
1310 if (!STI->
getCLOpts().constant_islands_no_load_relaxation) {
1311 result = findLongFormInRangeCPEntry(U, UserOffset);
1312 if (result != 0)
return true;
1315 createNewWater(CPUserIndex, UserOffset, NewMBB);
1322 MachineBasicBlock *WaterBB = &*--NewMBB->
getIterator();
1324 if (IP != WaterList.end())
1325 NewWaterList.
erase(WaterBB);
1328 NewWaterList.
insert(NewIsland);
1335 if (IP != WaterList.end())
1336 WaterList.erase(IP);
1342 updateForInsertedWaterBlock(NewIsland);
1345 decrementCPEReferenceCount(CPI, CPEMI);
1349 unsigned ID = createPICLabelUId();
1353 U.HighWaterMark = NewIsland;
1356 CPEntries[CPI].push_back(CPEntry(
U.CPEMI, ID, 1));
1364 adjustBBOffsetsAfter(&*--NewIsland->
getIterator());
1367 for (MachineOperand &MO : UserMI->
operands())
1374 dbgs() <<
" Moved CPE to #" << ID <<
" CPI=" << CPI
1382void MipsConstantIslandImpl::removeDeadCPEMI(MachineInstr *CPEMI) {
1383 MachineBasicBlock *CPEBB = CPEMI->
getParent();
1388 if (CPEBB->
empty()) {
1398 adjustBBOffsetsAfter(CPEBB);
1408bool MipsConstantIslandImpl::removeUnusedCPEntries() {
1409 unsigned MadeChange =
false;
1410 for (std::vector<CPEntry> &CPEs : CPEntries) {
1411 for (CPEntry &CPE : CPEs) {
1412 if (CPE.RefCount == 0 && CPE.CPEMI) {
1413 removeDeadCPEMI(CPE.CPEMI);
1414 CPE.CPEMI =
nullptr;
1424bool MipsConstantIslandImpl::isBBInRange(MachineInstr *
MI,
1425 MachineBasicBlock *DestBB,
1428 unsigned BrOffset = getOffsetOf(
MI) + PCAdj;
1429 unsigned DestOffset = BBInfo[DestBB->
getNumber()].Offset;
1433 <<
" max delta=" << MaxDisp <<
" from " << getOffsetOf(
MI)
1434 <<
" to " << DestOffset <<
" offset "
1435 <<
int(DestOffset - BrOffset) <<
"\t" << *
MI);
1437 if (BrOffset <= DestOffset) {
1439 if (DestOffset-BrOffset <= MaxDisp)
1442 if (BrOffset-DestOffset <= MaxDisp)
1450bool MipsConstantIslandImpl::fixupImmediateBr(ImmBranch &Br) {
1451 MachineInstr *
MI = Br.MI;
1453 MachineBasicBlock *DestBB =
MI->getOperand(TargetOperand).getMBB();
1456 if (isBBInRange(
MI, DestBB, Br.MaxDisp))
1460 return fixupUnconditionalBr(Br);
1461 return fixupConditionalBr(Br);
1468bool MipsConstantIslandImpl::fixupUnconditionalBr(ImmBranch &Br) {
1469 MachineInstr *
MI = Br.MI;
1471 MachineBasicBlock *DestBB =
MI->getOperand(0).getMBB();
1473 unsigned BimmX16MaxDisp = ((1 << 16)-1) * 2;
1474 if (isBBInRange(
MI, DestBB, BimmX16MaxDisp)) {
1475 Br.MaxDisp = BimmX16MaxDisp;
1476 MI->setDesc(
TII->get(Mips::BimmX16));
1491 Br.MaxDisp = ((1<<24)-1) * 2;
1492 MI->setDesc(
TII->get(Mips::JalB16));
1495 adjustBBOffsetsAfter(
MBB);
1506bool MipsConstantIslandImpl::fixupConditionalBr(ImmBranch &Br) {
1507 MachineInstr *
MI = Br.MI;
1509 MachineBasicBlock *DestBB =
MI->getOperand(TargetOperand).getMBB();
1510 unsigned Opcode =
MI->getOpcode();
1515 if (isBBInRange(
MI, DestBB, LongFormMaxOff)) {
1516 Br.MaxDisp = LongFormMaxOff;
1517 MI->setDesc(
TII->get(LongFormOpcode));
1533 MachineInstr *BMI = &
MBB->
back();
1535 unsigned OppositeBranchOpcode =
TII->getOppositeBranchOpc(Opcode);
1549 MachineBasicBlock *NewDest =
1551 if (isBBInRange(
MI, NewDest, Br.MaxDisp)) {
1553 dbgs() <<
" Invert Bcc condition and swap its destination with "
1555 MI->setDesc(
TII->get(OppositeBranchOpcode));
1557 MI->getOperand(TargetOperand).setMBB(NewDest);
1564 splitBlockBeforeInstr(*
MI);
1567 int delta =
TII->getInstSizeInBytes(
MBB->
back());
1575 <<
" also invert condition and change dest. to "
1580 if (
MI->getNumExplicitOperands() == 2) {
1582 .
addReg(
MI->getOperand(0).getReg())
1593 ImmBranches.push_back(ImmBranch(&
MBB->
back(), MaxDisp,
false, Br.UncondBr));
1596 BBInfo[
MI->getParent()->getNumber()].Size -=
TII->getInstSizeInBytes(*
MI);
1597 MI->eraseFromParent();
1598 adjustBBOffsetsAfter(
MBB);
1602void MipsConstantIslandImpl::prescanForConstants() {
1603 for (MachineBasicBlock &
B : *MF) {
1604 for (MachineInstr &
MI :
B) {
1605 switch (
MI.getDesc().getOpcode()) {
1606 case Mips::LwConstant32: {
1607 PrescannedForConstants =
true;
1610 MachineOperand &
Literal =
MI.getOperand(1);
1614 Type *Int32Ty = Type::getInt32Ty(MF->getFunction().getContext());
1617 MI.getOperand(2).ChangeToImmediate(index);
1619 MI.setDesc(
TII->get(Mips::LwRxPcTcp16));
1620 MI.removeOperand(1);
1621 MI.removeOperand(1);
1635 return new MipsConstantIslandLegacy();
1638bool MipsConstantIslandLegacy::runOnMachineFunction(
MachineFunction &MF) {
1639 MipsConstantIslandImpl Impl;
1640 return Impl.runOnMachineFunction(MF);
1646 MipsConstantIslandImpl Impl;
1647 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
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.