comparison lib/Target/Mips/MipsConstantIslandPass.cpp @ 100:7d135dc70f03 LLVM 3.9

LLVM 3.9
author Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 22:53:40 +0900
parents afa8332a0e37
children 1172e4bd9c6f
comparison
equal deleted inserted replaced
96:6418606d0ead 100:7d135dc70f03
596 596
597 /// BBHasFallthrough - Return true if the specified basic block can fallthrough 597 /// BBHasFallthrough - Return true if the specified basic block can fallthrough
598 /// into the block immediately after it. 598 /// into the block immediately after it.
599 static bool BBHasFallthrough(MachineBasicBlock *MBB) { 599 static bool BBHasFallthrough(MachineBasicBlock *MBB) {
600 // Get the next machine basic block in the function. 600 // Get the next machine basic block in the function.
601 MachineFunction::iterator MBBI = MBB; 601 MachineFunction::iterator MBBI = MBB->getIterator();
602 // Can't fall off end of function. 602 // Can't fall off end of function.
603 if (std::next(MBBI) == MBB->getParent()->end()) 603 if (std::next(MBBI) == MBB->getParent()->end())
604 return false; 604 return false;
605 605
606 MachineBasicBlock *NextBB = std::next(MBBI); 606 MachineBasicBlock *NextBB = &*std::next(MBBI);
607 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), 607 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
608 E = MBB->succ_end(); I != E; ++I) 608 E = MBB->succ_end(); I != E; ++I)
609 if (*I == NextBB) 609 if (*I == NextBB)
610 return true; 610 return true;
611 611
654 // First thing, compute the size of all basic blocks, and see if the function 654 // First thing, compute the size of all basic blocks, and see if the function
655 // has any inline assembly in it. If so, we have to be conservative about 655 // has any inline assembly in it. If so, we have to be conservative about
656 // alignment assumptions, as we don't know for sure the size of any 656 // alignment assumptions, as we don't know for sure the size of any
657 // instructions in the inline assembly. 657 // instructions in the inline assembly.
658 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) 658 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I)
659 computeBlockSize(I); 659 computeBlockSize(&*I);
660 660
661 661
662 // Compute block offsets. 662 // Compute block offsets.
663 adjustBBOffsetsAfter(MF->begin()); 663 adjustBBOffsetsAfter(&MF->front());
664 664
665 // Now go back through the instructions and build up our data structures. 665 // Now go back through the instructions and build up our data structures.
666 for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); 666 for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
667 MBBI != E; ++MBBI) { 667 MBBI != E; ++MBBI) {
668 MachineBasicBlock &MBB = *MBBI; 668 MachineBasicBlock &MBB = *MBBI;
877 MachineBasicBlock *OrigBB = MI->getParent(); 877 MachineBasicBlock *OrigBB = MI->getParent();
878 878
879 // Create a new MBB for the code after the OrigBB. 879 // Create a new MBB for the code after the OrigBB.
880 MachineBasicBlock *NewBB = 880 MachineBasicBlock *NewBB =
881 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock()); 881 MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
882 MachineFunction::iterator MBBI = OrigBB; ++MBBI; 882 MachineFunction::iterator MBBI = ++OrigBB->getIterator();
883 MF->insert(MBBI, NewBB); 883 MF->insert(MBBI, NewBB);
884 884
885 // Splice the instructions starting with MI over to NewBB. 885 // Splice the instructions starting with MI over to NewBB.
886 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); 886 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
887 887
965 MachineBasicBlock* Water, CPUser &U, 965 MachineBasicBlock* Water, CPUser &U,
966 unsigned &Growth) { 966 unsigned &Growth) {
967 unsigned CPELogAlign = getCPELogAlign(U.CPEMI); 967 unsigned CPELogAlign = getCPELogAlign(U.CPEMI);
968 unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign); 968 unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign);
969 unsigned NextBlockOffset, NextBlockAlignment; 969 unsigned NextBlockOffset, NextBlockAlignment;
970 MachineFunction::const_iterator NextBlock = Water; 970 MachineFunction::const_iterator NextBlock = ++Water->getIterator();
971 if (++NextBlock == MF->end()) { 971 if (NextBlock == MF->end()) {
972 NextBlockOffset = BBInfo[Water->getNumber()].postOffset(); 972 NextBlockOffset = BBInfo[Water->getNumber()].postOffset();
973 NextBlockAlignment = 0; 973 NextBlockAlignment = 0;
974 } else { 974 } else {
975 NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset; 975 NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset;
976 NextBlockAlignment = NextBlock->getAlignment(); 976 NextBlockAlignment = NextBlock->getAlignment();
1259 unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta; 1259 unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta;
1260 1260
1261 if (isOffsetInRange(UserOffset, CPEOffset, U)) { 1261 if (isOffsetInRange(UserOffset, CPEOffset, U)) {
1262 DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber() 1262 DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber()
1263 << format(", expected CPE offset %#x\n", CPEOffset)); 1263 << format(", expected CPE offset %#x\n", CPEOffset));
1264 NewMBB = std::next(MachineFunction::iterator(UserMBB)); 1264 NewMBB = &*++UserMBB->getIterator();
1265 // Add an unconditional branch from UserMBB to fallthrough block. Record 1265 // Add an unconditional branch from UserMBB to fallthrough block. Record
1266 // it for branch lengthening; this new branch will not get out of range, 1266 // it for branch lengthening; this new branch will not get out of range,
1267 // but if the preceding conditional branch is out of range, the targets 1267 // but if the preceding conditional branch is out of range, the targets
1268 // will be exchanged, and the altered branch may be out of range, so the 1268 // will be exchanged, and the altered branch may be out of range, so the
1269 // machinery has to know about it. 1269 // machinery has to know about it.
1369 // updated to match the WaterList, which will be updated below. 1369 // updated to match the WaterList, which will be updated below.
1370 if (NewWaterList.erase(WaterBB)) 1370 if (NewWaterList.erase(WaterBB))
1371 NewWaterList.insert(NewIsland); 1371 NewWaterList.insert(NewIsland);
1372 1372
1373 // The new CPE goes before the following block (NewMBB). 1373 // The new CPE goes before the following block (NewMBB).
1374 NewMBB = std::next(MachineFunction::iterator(WaterBB)); 1374 NewMBB = &*++WaterBB->getIterator();
1375
1376 } else { 1375 } else {
1377 // No water found. 1376 // No water found.
1378 // we first see if a longer form of the instrucion could have reached 1377 // we first see if a longer form of the instrucion could have reached
1379 // the constant. in that case we won't bother to split 1378 // the constant. in that case we won't bother to split
1380 if (!NoLoadRelaxation) { 1379 if (!NoLoadRelaxation) {
1387 // splitBlockBeforeInstr adds to WaterList, which is important when it is 1386 // splitBlockBeforeInstr adds to WaterList, which is important when it is
1388 // called while handling branches so that the water will be seen on the 1387 // called while handling branches so that the water will be seen on the
1389 // next iteration for constant pools, but in this context, we don't want 1388 // next iteration for constant pools, but in this context, we don't want
1390 // it. Check for this so it will be removed from the WaterList. 1389 // it. Check for this so it will be removed from the WaterList.
1391 // Also remove any entry from NewWaterList. 1390 // Also remove any entry from NewWaterList.
1392 MachineBasicBlock *WaterBB = std::prev(MachineFunction::iterator(NewMBB)); 1391 MachineBasicBlock *WaterBB = &*--NewMBB->getIterator();
1393 IP = std::find(WaterList.begin(), WaterList.end(), WaterBB); 1392 IP = std::find(WaterList.begin(), WaterList.end(), WaterBB);
1394 if (IP != WaterList.end()) 1393 if (IP != WaterList.end())
1395 NewWaterList.erase(WaterBB); 1394 NewWaterList.erase(WaterBB);
1396 1395
1397 // We are adding new water. Update NewWaterList. 1396 // We are adding new water. Update NewWaterList.
1404 // more than once and is also important to ensure the algorithm terminates. 1403 // more than once and is also important to ensure the algorithm terminates.
1405 if (IP != WaterList.end()) 1404 if (IP != WaterList.end())
1406 WaterList.erase(IP); 1405 WaterList.erase(IP);
1407 1406
1408 // Okay, we know we can put an island before NewMBB now, do it! 1407 // Okay, we know we can put an island before NewMBB now, do it!
1409 MF->insert(NewMBB, NewIsland); 1408 MF->insert(NewMBB->getIterator(), NewIsland);
1410 1409
1411 // Update internal data structures to account for the newly inserted MBB. 1410 // Update internal data structures to account for the newly inserted MBB.
1412 updateForInsertedWaterBlock(NewIsland); 1411 updateForInsertedWaterBlock(NewIsland);
1413 1412
1414 // Decrement the old entry, and remove it if refcount becomes 0. 1413 // Decrement the old entry, and remove it if refcount becomes 0.
1429 // Mark the basic block as aligned as required by the const-pool entry. 1428 // Mark the basic block as aligned as required by the const-pool entry.
1430 NewIsland->setAlignment(getCPELogAlign(U.CPEMI)); 1429 NewIsland->setAlignment(getCPELogAlign(U.CPEMI));
1431 1430
1432 // Increase the size of the island block to account for the new entry. 1431 // Increase the size of the island block to account for the new entry.
1433 BBInfo[NewIsland->getNumber()].Size += Size; 1432 BBInfo[NewIsland->getNumber()].Size += Size;
1434 adjustBBOffsetsAfter(std::prev(MachineFunction::iterator(NewIsland))); 1433 adjustBBOffsetsAfter(&*--NewIsland->getIterator());
1435
1436
1437 1434
1438 // Finally, change the CPI in the instruction operand to be ID. 1435 // Finally, change the CPI in the instruction operand to be ID.
1439 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) 1436 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
1440 if (UserMI->getOperand(i).isCPI()) { 1437 if (UserMI->getOperand(i).isCPI()) {
1441 UserMI->getOperand(i).setIndex(ID); 1438 UserMI->getOperand(i).setIndex(ID);
1643 int delta = TII->GetInstSizeInBytes(&MBB->back()); 1640 int delta = TII->GetInstSizeInBytes(&MBB->back());
1644 BBInfo[MBB->getNumber()].Size -= delta; 1641 BBInfo[MBB->getNumber()].Size -= delta;
1645 MBB->back().eraseFromParent(); 1642 MBB->back().eraseFromParent();
1646 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below 1643 // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
1647 } 1644 }
1648 MachineBasicBlock *NextBB = std::next(MachineFunction::iterator(MBB)); 1645 MachineBasicBlock *NextBB = &*++MBB->getIterator();
1649 1646
1650 DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber() 1647 DEBUG(dbgs() << " Insert B to BB#" << DestBB->getNumber()
1651 << " also invert condition and change dest. to BB#" 1648 << " also invert condition and change dest. to BB#"
1652 << NextBB->getNumber() << "\n"); 1649 << NextBB->getNumber() << "\n");
1653 1650