llvm.org GIT mirror llvm / ce3ee9d
support phi ranges for machine-level IR Add iterator ranges for machine instruction phis, similar to the IR-level phi ranges added in r303964. I updated a few places to use this. Besides general code simplification, this change will allow removing a non-upstream change from Swift's copy of LLVM (in a better way than my previous attempt in http://reviews.llvm.org/D19080). https://reviews.llvm.org/D41672 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321783 91177308-0d34-0410-b5e6-96231b3b80d8 Bob Wilson 2 years ago
4 changed file(s) with 88 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
222222 }
223223 inline iterator_range terminators() const {
224224 return make_range(getFirstTerminator(), end());
225 }
226
227 /// Returns a range that iterates over the phis in the basic block.
228 inline iterator_range phis() {
229 return make_range(begin(), getFirstNonPHI());
230 }
231 inline iterator_range phis() const {
232 return const_cast(this)->phis();
225233 }
226234
227235 // Machine-CFG iterators
388388 /// FindExistingPHI - Look through the PHI nodes in a block to see if any of
389389 /// them match what is needed.
390390 void FindExistingPHI(BlkT *BB, BlockListTy *BlockList) {
391 for (typename BlkT::iterator BBI = BB->begin(), BBE = BB->end();
392 BBI != BBE; ++BBI) {
393 PhiT *SomePHI = Traits::InstrIsPHI(&*BBI);
394 if (!SomePHI)
395 break;
396 if (CheckIfPHIMatches(SomePHI)) {
391 for (auto &SomePHI : BB->phis()) {
392 if (CheckIfPHIMatches(&SomePHI)) {
397393 RecordMatchingPHIs(BlockList);
398394 break;
399395 }
807807 // because we don't know how to maintain subreg information in the
808808 // VMap structure.
809809 MachineBasicBlock *MBB = L.getHeader();
810 for (MachineBasicBlock::iterator BBI = MBB->instr_begin(),
811 BBE = MBB->getFirstNonPHI();
812 BBI != BBE; ++BBI)
813 for (unsigned i = 1; i != BBI->getNumOperands(); i += 2)
814 if (BBI->getOperand(i).getSubReg() != 0)
810 for (auto &PHI : MBB->phis())
811 for (unsigned i = 1; i != PHI.getNumOperands(); i += 2)
812 if (PHI.getOperand(i).getSubReg() != 0)
815813 return false;
816814
817815 return true;
29232921 MBBVectorTy &EpilogBBs,
29242922 SMSchedule &Schedule) {
29252923 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2926 for (MachineBasicBlock::iterator BBI = KernelBB->instr_begin(),
2927 BBF = KernelBB->getFirstNonPHI();
2928 BBI != BBF; ++BBI) {
2929 unsigned Def = BBI->getOperand(0).getReg();
2924 for (auto &PHI : KernelBB->phis()) {
2925 unsigned Def = PHI.getOperand(0).getReg();
29302926 // Check for any Phi definition that used as an operand of another Phi
29312927 // in the same block.
29322928 for (MachineRegisterInfo::use_instr_iterator I = MRI.use_instr_begin(Def),
29342930 I != E; ++I) {
29352931 if (I->isPHI() && I->getParent() == KernelBB) {
29362932 // Get the loop carried definition.
2937 unsigned LCDef = getLoopPhiReg(*BBI, KernelBB);
2933 unsigned LCDef = getLoopPhiReg(PHI, KernelBB);
29382934 if (!LCDef)
29392935 continue;
29402936 MachineInstr *MI = MRI.getVRegDef(LCDef);
32483244 SMSchedule &Schedule,
32493245 ValueMapTy *VRMap,
32503246 InstrMapTy &InstrMap) {
3251 for (MachineBasicBlock::iterator BBI = BB->instr_begin(),
3252 BBE = BB->getFirstNonPHI();
3253 BBI != BBE; ++BBI) {
3247 for (auto &PHI : BB->phis()) {
32543248 unsigned InitVal = 0;
32553249 unsigned LoopVal = 0;
3256 getPhiRegs(*BBI, BB, InitVal, LoopVal);
3257 unsigned PhiDef = BBI->getOperand(0).getReg();
3250 getPhiRegs(PHI, BB, InitVal, LoopVal);
3251 unsigned PhiDef = PHI.getOperand(0).getReg();
32583252
32593253 unsigned PhiStage =
32603254 (unsigned)Schedule.stageScheduled(getSUnit(MRI.getVRegDef(PhiDef)));
32683262 getPrevMapVal(StageNum - np, PhiStage, LoopVal, LoopStage, VRMap, BB);
32693263 if (!NewVal)
32703264 NewVal = InitVal;
3271 rewriteScheduledInstr(NewBB, Schedule, InstrMap, StageNum - np, np, &*BBI,
3265 rewriteScheduledInstr(NewBB, Schedule, InstrMap, StageNum - np, np, &PHI,
32723266 PhiDef, NewVal);
32733267 }
32743268 }
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/CodeGen/MachineInstr.h"
10 #include "llvm/CodeGen/MachineInstrBuilder.h"
1011 #include "llvm/CodeGen/MachineFunction.h"
1112 #include "llvm/CodeGen/MachineModuleInfo.h"
1213 #include "llvm/CodeGen/TargetFrameLowering.h"
243244
244245 checkHashAndIsEqualMatch(VD2PU, VD2PD);
245246 }
247
248 TEST(MachineBasicBlockTest, PhiRange) {
249 auto MF = createMachineFunction();
250
251 // Create the main block.
252 auto BB = MF->CreateMachineBasicBlock();
253
254 // Create some predecessors of it.
255 auto BB1 = MF->CreateMachineBasicBlock();
256 BB1->addSuccessor(BB);
257 auto BB2 = MF->CreateMachineBasicBlock();
258 BB2->addSuccessor(BB);
259
260 // Make sure this doesn't crash if there are no phis.
261 for (auto &PN : BB->phis()) {
262 (void)PN;
263 ASSERT_TRUE(false) << "empty block should have no phis";
264 }
265
266 // Make it a cycle.
267 BB->addSuccessor(BB);
268
269 // Now insert some PHI nodes.
270 MCOperandInfo OpInfo[] = { { -1, 0, MCOI::OPERAND_UNKNOWN, 0} };
271 MCInstrDesc PHIMCID = {
272 TargetOpcode::PHI, 1, 1, 0, 0,
273 (1ULL << MCID::Pseudo) | (1ULL << MCID::Variadic), 0,
274 nullptr, nullptr, OpInfo, -1, nullptr};
275 auto P1 = BuildMI(*BB, BB->end(), DebugLoc(), PHIMCID, -101);
276 auto P2 = BuildMI(*BB, BB->end(), DebugLoc(), PHIMCID, -102);
277 auto P3 = BuildMI(*BB, BB->end(), DebugLoc(), PHIMCID, -103);
278
279 // A non-PHI node.
280 MCInstrDesc ImpDefMCID = {
281 TargetOpcode::IMPLICIT_DEF, 1, 1, 0, 0,
282 (1ULL << MCID::Pseudo), 0,
283 nullptr, nullptr, OpInfo, -1, nullptr};
284 BuildMI(*BB, BB->end(), DebugLoc(), ImpDefMCID, -104);
285
286 // Now wire up the incoming values that are interesting.
287 P1.addReg(-102).addMBB(BB);
288 P2.addReg(-101).addMBB(BB);
289 P3.addReg(-104).addMBB(BB);
290
291 // Finally, let's iterate them, which is the thing we're trying to test.
292 // We'll use this to wire up the rest of the incoming values.
293 for (auto &PN : BB->phis()) {
294 EXPECT_TRUE(PN.isPHI());
295 PN.addOperand(*MF, MachineOperand::CreateReg(-100, /*isDef*/ false));
296 PN.addOperand(*MF, MachineOperand::CreateMBB(BB1));
297 PN.addOperand(*MF, MachineOperand::CreateReg(-100, /*isDef*/ false));
298 PN.addOperand(*MF, MachineOperand::CreateMBB(BB2));
299 }
300
301 // Test that we can use const iterators and generally that the iterators
302 // behave like iterators.
303 MachineBasicBlock::const_iterator CI;
304 CI = BB->phis().begin();
305 EXPECT_NE(CI, BB->phis().end());
306
307 // And iterate a const range.
308 for (const auto &PN : const_cast(BB)->phis()) {
309 EXPECT_EQ(BB, PN.getOperand(2).getMBB());
310 EXPECT_EQ(BB1, PN.getOperand(4).getMBB());
311 EXPECT_EQ(BB2, PN.getOperand(6).getMBB());
312 }
313 }
246314 } // end namespace