llvm.org GIT mirror llvm / 8b560b8
Only check for PHI uses inside the current loop. Hoisting a value that is used by a PHI in the loop will introduce a copy because the live range is extended to cross the PHI. The same applies to PHIs in exit blocks. Also use this opportunity to make HasLoopPHIUse() non-recursive. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154454 91177308-0d34-0410-b5e6-96231b3b80d8 Jakob Stoklund Olesen 8 years ago
1 changed file(s) with 52 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
7979 MachineLoop *CurLoop; // The current loop we are working on.
8080 MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
8181
82 // Exit blocks for CurLoop.
83 SmallVector ExitBlocks;
84
85 bool isExitBlock(const MachineBasicBlock *MBB) const {
86 return std::find(ExitBlocks.begin(), ExitBlocks.end(), MBB) !=
87 ExitBlocks.end();
88 }
89
8290 // Track 'estimated' register pressure.
8391 SmallSet RegSeen;
8492 SmallVector RegPressure;
181189 ///
182190 bool IsLoopInvariantInst(MachineInstr &I);
183191
184 /// HasAnyPHIUse - Return true if the specified register is used by any
185 /// phi node.
186 bool HasAnyPHIUse(unsigned Reg) const;
192 /// HasLoopPHIUse - Return true if the specified instruction is used by any
193 /// phi node in the current loop.
194 bool HasLoopPHIUse(const MachineInstr *MI) const;
187195
188196 /// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
189197 /// and an use in the current loop, return true if the target considered
347355 while (!Worklist.empty()) {
348356 CurLoop = Worklist.pop_back_val();
349357 CurPreheader = 0;
358 ExitBlocks.clear();
350359
351360 // If this is done before regalloc, only visit outer-most preheader-sporting
352361 // loops.
354363 Worklist.append(CurLoop->begin(), CurLoop->end());
355364 continue;
356365 }
366
367 CurLoop->getExitBlocks(ExitBlocks);
357368
358369 if (!PreRegAlloc)
359370 HoistRegionPostRA();
954965 }
955966
956967
957 /// HasAnyPHIUse - Return true if the specified register is used by any
958 /// phi node.
959 bool MachineLICM::HasAnyPHIUse(unsigned Reg) const {
960 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
961 UE = MRI->use_end(); UI != UE; ++UI) {
962 MachineInstr *UseMI = &*UI;
963 if (UseMI->isPHI())
964 return true;
965 // Look pass copies as well.
966 if (UseMI->isCopy()) {
967 unsigned Def = UseMI->getOperand(0).getReg();
968 if (TargetRegisterInfo::isVirtualRegister(Def) &&
969 HasAnyPHIUse(Def))
970 return true;
971 }
972 }
968 /// HasLoopPHIUse - Return true if the specified instruction is used by a
969 /// phi node and hoisting it could cause a copy to be inserted.
970 bool MachineLICM::HasLoopPHIUse(const MachineInstr *MI) const {
971 SmallVector Work(1, MI);
972 do {
973 MI = Work.pop_back_val();
974 for (ConstMIOperands MO(MI); MO.isValid(); ++MO) {
975 if (!MO->isReg() || !MO->isDef())
976 continue;
977 unsigned Reg = MO->getReg();
978 if (!TargetRegisterInfo::isVirtualRegister(Reg))
979 continue;
980 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
981 UE = MRI->use_end(); UI != UE; ++UI) {
982 MachineInstr *UseMI = &*UI;
983 // A PHI may cause a copy to be inserted.
984 if (UseMI->isPHI()) {
985 // A PHI inside the loop causes a copy because the live range of Reg is
986 // extended across the PHI.
987 if (CurLoop->contains(UseMI))
988 return true;
989 // A PHI in an exit block can cause a copy to be inserted if the PHI
990 // has multiple predecessors in the loop with different values.
991 // For now, approximate by rejecting all exit blocks.
992 if (isExitBlock(UseMI->getParent()))
993 return true;
994 continue;
995 }
996 // Look past copies as well.
997 if (UseMI->isCopy() && CurLoop->contains(UseMI))
998 Work.push_back(UseMI);
999 }
1000 }
1001 } while (!Work.empty());
9731002 return false;
9741003 }
9751004
11811210 return false;
11821211 }
11831212
1184 // If result(s) of this instruction is used by PHIs outside of the loop, then
1185 // don't hoist it if the instruction because it will introduce an extra copy.
1186 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
1187 const MachineOperand &MO = MI.getOperand(i);
1188 if (!MO.isReg() || !MO.isDef())
1189 continue;
1190 if (HasAnyPHIUse(MO.getReg()))
1191 return false;
1192 }
1213 // If result(s) of this instruction is used by PHIs inside the loop, then
1214 // don't hoist it because it will introduce an extra copy.
1215 if (HasLoopPHIUse(&MI))
1216 return false;
11931217
11941218 return true;
11951219 }