1790a779fSJames Molloy //===- ModuloSchedule.cpp - Software pipeline schedule expansion ----------===// 2790a779fSJames Molloy // 3790a779fSJames Molloy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4790a779fSJames Molloy // See https://llvm.org/LICENSE.txt for license information. 5790a779fSJames Molloy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6790a779fSJames Molloy // 7790a779fSJames Molloy //===----------------------------------------------------------------------===// 8790a779fSJames Molloy 9790a779fSJames Molloy #include "llvm/CodeGen/ModuloSchedule.h" 1093549957SJames Molloy #include "llvm/ADT/StringExtras.h" 11790a779fSJames Molloy #include "llvm/CodeGen/LiveIntervals.h" 12790a779fSJames Molloy #include "llvm/CodeGen/MachineInstrBuilder.h" 13fef9f590SJames Molloy #include "llvm/CodeGen/MachineRegisterInfo.h" 14790a779fSJames Molloy #include "llvm/CodeGen/TargetInstrInfo.h" 1593549957SJames Molloy #include "llvm/MC/MCContext.h" 16790a779fSJames Molloy #include "llvm/Support/Debug.h" 17fef9f590SJames Molloy #include "llvm/Support/ErrorHandling.h" 18fef9f590SJames Molloy #include "llvm/Support/raw_ostream.h" 19790a779fSJames Molloy 20790a779fSJames Molloy #define DEBUG_TYPE "pipeliner" 21790a779fSJames Molloy using namespace llvm; 22790a779fSJames Molloy 23fef9f590SJames Molloy void ModuloSchedule::print(raw_ostream &OS) { 24fef9f590SJames Molloy for (MachineInstr *MI : ScheduledInstrs) 25fef9f590SJames Molloy OS << "[stage " << getStage(MI) << " @" << getCycle(MI) << "c] " << *MI; 26fef9f590SJames Molloy } 27fef9f590SJames Molloy 2893549957SJames Molloy //===----------------------------------------------------------------------===// 2993549957SJames Molloy // ModuloScheduleExpander implementation 3093549957SJames Molloy //===----------------------------------------------------------------------===// 3193549957SJames Molloy 32790a779fSJames Molloy /// Return the register values for the operands of a Phi instruction. 33790a779fSJames Molloy /// This function assume the instruction is a Phi. 34790a779fSJames Molloy static void getPhiRegs(MachineInstr &Phi, MachineBasicBlock *Loop, 35790a779fSJames Molloy unsigned &InitVal, unsigned &LoopVal) { 36790a779fSJames Molloy assert(Phi.isPHI() && "Expecting a Phi."); 37790a779fSJames Molloy 38790a779fSJames Molloy InitVal = 0; 39790a779fSJames Molloy LoopVal = 0; 40790a779fSJames Molloy for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2) 41790a779fSJames Molloy if (Phi.getOperand(i + 1).getMBB() != Loop) 42790a779fSJames Molloy InitVal = Phi.getOperand(i).getReg(); 43790a779fSJames Molloy else 44790a779fSJames Molloy LoopVal = Phi.getOperand(i).getReg(); 45790a779fSJames Molloy 46790a779fSJames Molloy assert(InitVal != 0 && LoopVal != 0 && "Unexpected Phi structure."); 47790a779fSJames Molloy } 48790a779fSJames Molloy 49790a779fSJames Molloy /// Return the Phi register value that comes from the incoming block. 50790a779fSJames Molloy static unsigned getInitPhiReg(MachineInstr &Phi, MachineBasicBlock *LoopBB) { 51790a779fSJames Molloy for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2) 52790a779fSJames Molloy if (Phi.getOperand(i + 1).getMBB() != LoopBB) 53790a779fSJames Molloy return Phi.getOperand(i).getReg(); 54790a779fSJames Molloy return 0; 55790a779fSJames Molloy } 56790a779fSJames Molloy 57790a779fSJames Molloy /// Return the Phi register value that comes the loop block. 58790a779fSJames Molloy static unsigned getLoopPhiReg(MachineInstr &Phi, MachineBasicBlock *LoopBB) { 59790a779fSJames Molloy for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2) 60790a779fSJames Molloy if (Phi.getOperand(i + 1).getMBB() == LoopBB) 61790a779fSJames Molloy return Phi.getOperand(i).getReg(); 62790a779fSJames Molloy return 0; 63790a779fSJames Molloy } 64790a779fSJames Molloy 65790a779fSJames Molloy void ModuloScheduleExpander::expand() { 66790a779fSJames Molloy BB = Schedule.getLoop()->getTopBlock(); 67790a779fSJames Molloy Preheader = *BB->pred_begin(); 68790a779fSJames Molloy if (Preheader == BB) 69790a779fSJames Molloy Preheader = *std::next(BB->pred_begin()); 70790a779fSJames Molloy 71790a779fSJames Molloy // Iterate over the definitions in each instruction, and compute the 72790a779fSJames Molloy // stage difference for each use. Keep the maximum value. 73790a779fSJames Molloy for (MachineInstr *MI : Schedule.getInstructions()) { 74790a779fSJames Molloy int DefStage = Schedule.getStage(MI); 75790a779fSJames Molloy for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) { 76790a779fSJames Molloy MachineOperand &Op = MI->getOperand(i); 77790a779fSJames Molloy if (!Op.isReg() || !Op.isDef()) 78790a779fSJames Molloy continue; 79790a779fSJames Molloy 80790a779fSJames Molloy Register Reg = Op.getReg(); 81790a779fSJames Molloy unsigned MaxDiff = 0; 82790a779fSJames Molloy bool PhiIsSwapped = false; 83790a779fSJames Molloy for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(Reg), 84790a779fSJames Molloy EI = MRI.use_end(); 85790a779fSJames Molloy UI != EI; ++UI) { 86790a779fSJames Molloy MachineOperand &UseOp = *UI; 87790a779fSJames Molloy MachineInstr *UseMI = UseOp.getParent(); 88790a779fSJames Molloy int UseStage = Schedule.getStage(UseMI); 89790a779fSJames Molloy unsigned Diff = 0; 90790a779fSJames Molloy if (UseStage != -1 && UseStage >= DefStage) 91790a779fSJames Molloy Diff = UseStage - DefStage; 92790a779fSJames Molloy if (MI->isPHI()) { 93790a779fSJames Molloy if (isLoopCarried(*MI)) 94790a779fSJames Molloy ++Diff; 95790a779fSJames Molloy else 96790a779fSJames Molloy PhiIsSwapped = true; 97790a779fSJames Molloy } 98790a779fSJames Molloy MaxDiff = std::max(Diff, MaxDiff); 99790a779fSJames Molloy } 100790a779fSJames Molloy RegToStageDiff[Reg] = std::make_pair(MaxDiff, PhiIsSwapped); 101790a779fSJames Molloy } 102790a779fSJames Molloy } 103790a779fSJames Molloy 104790a779fSJames Molloy generatePipelinedLoop(); 105790a779fSJames Molloy } 106790a779fSJames Molloy 107790a779fSJames Molloy void ModuloScheduleExpander::generatePipelinedLoop() { 108790a779fSJames Molloy // Create a new basic block for the kernel and add it to the CFG. 109790a779fSJames Molloy MachineBasicBlock *KernelBB = MF.CreateMachineBasicBlock(BB->getBasicBlock()); 110790a779fSJames Molloy 111790a779fSJames Molloy unsigned MaxStageCount = Schedule.getNumStages() - 1; 112790a779fSJames Molloy 113790a779fSJames Molloy // Remember the registers that are used in different stages. The index is 114790a779fSJames Molloy // the iteration, or stage, that the instruction is scheduled in. This is 115790a779fSJames Molloy // a map between register names in the original block and the names created 116790a779fSJames Molloy // in each stage of the pipelined loop. 117790a779fSJames Molloy ValueMapTy *VRMap = new ValueMapTy[(MaxStageCount + 1) * 2]; 118790a779fSJames Molloy InstrMapTy InstrMap; 119790a779fSJames Molloy 120790a779fSJames Molloy SmallVector<MachineBasicBlock *, 4> PrologBBs; 121790a779fSJames Molloy 122790a779fSJames Molloy // Generate the prolog instructions that set up the pipeline. 123790a779fSJames Molloy generateProlog(MaxStageCount, KernelBB, VRMap, PrologBBs); 124790a779fSJames Molloy MF.insert(BB->getIterator(), KernelBB); 125790a779fSJames Molloy 126790a779fSJames Molloy // Rearrange the instructions to generate the new, pipelined loop, 127790a779fSJames Molloy // and update register names as needed. 128790a779fSJames Molloy for (MachineInstr *CI : Schedule.getInstructions()) { 129790a779fSJames Molloy if (CI->isPHI()) 130790a779fSJames Molloy continue; 131790a779fSJames Molloy unsigned StageNum = Schedule.getStage(CI); 132790a779fSJames Molloy MachineInstr *NewMI = cloneInstr(CI, MaxStageCount, StageNum); 133790a779fSJames Molloy updateInstruction(NewMI, false, MaxStageCount, StageNum, VRMap); 134790a779fSJames Molloy KernelBB->push_back(NewMI); 135790a779fSJames Molloy InstrMap[NewMI] = CI; 136790a779fSJames Molloy } 137790a779fSJames Molloy 138790a779fSJames Molloy // Copy any terminator instructions to the new kernel, and update 139790a779fSJames Molloy // names as needed. 140790a779fSJames Molloy for (MachineBasicBlock::iterator I = BB->getFirstTerminator(), 141790a779fSJames Molloy E = BB->instr_end(); 142790a779fSJames Molloy I != E; ++I) { 143790a779fSJames Molloy MachineInstr *NewMI = MF.CloneMachineInstr(&*I); 144790a779fSJames Molloy updateInstruction(NewMI, false, MaxStageCount, 0, VRMap); 145790a779fSJames Molloy KernelBB->push_back(NewMI); 146790a779fSJames Molloy InstrMap[NewMI] = &*I; 147790a779fSJames Molloy } 148790a779fSJames Molloy 149fef9f590SJames Molloy NewKernel = KernelBB; 150790a779fSJames Molloy KernelBB->transferSuccessors(BB); 151790a779fSJames Molloy KernelBB->replaceSuccessor(BB, KernelBB); 152790a779fSJames Molloy 153790a779fSJames Molloy generateExistingPhis(KernelBB, PrologBBs.back(), KernelBB, KernelBB, VRMap, 154790a779fSJames Molloy InstrMap, MaxStageCount, MaxStageCount, false); 155790a779fSJames Molloy generatePhis(KernelBB, PrologBBs.back(), KernelBB, KernelBB, VRMap, InstrMap, 156790a779fSJames Molloy MaxStageCount, MaxStageCount, false); 157790a779fSJames Molloy 158790a779fSJames Molloy LLVM_DEBUG(dbgs() << "New block\n"; KernelBB->dump();); 159790a779fSJames Molloy 160790a779fSJames Molloy SmallVector<MachineBasicBlock *, 4> EpilogBBs; 161790a779fSJames Molloy // Generate the epilog instructions to complete the pipeline. 162790a779fSJames Molloy generateEpilog(MaxStageCount, KernelBB, VRMap, EpilogBBs, PrologBBs); 163790a779fSJames Molloy 164790a779fSJames Molloy // We need this step because the register allocation doesn't handle some 165790a779fSJames Molloy // situations well, so we insert copies to help out. 166790a779fSJames Molloy splitLifetimes(KernelBB, EpilogBBs); 167790a779fSJames Molloy 168790a779fSJames Molloy // Remove dead instructions due to loop induction variables. 169790a779fSJames Molloy removeDeadInstructions(KernelBB, EpilogBBs); 170790a779fSJames Molloy 171790a779fSJames Molloy // Add branches between prolog and epilog blocks. 172790a779fSJames Molloy addBranches(*Preheader, PrologBBs, KernelBB, EpilogBBs, VRMap); 173790a779fSJames Molloy 174fef9f590SJames Molloy delete[] VRMap; 175fef9f590SJames Molloy } 176fef9f590SJames Molloy 177fef9f590SJames Molloy void ModuloScheduleExpander::cleanup() { 178790a779fSJames Molloy // Remove the original loop since it's no longer referenced. 179790a779fSJames Molloy for (auto &I : *BB) 180790a779fSJames Molloy LIS.RemoveMachineInstrFromMaps(I); 181790a779fSJames Molloy BB->clear(); 182790a779fSJames Molloy BB->eraseFromParent(); 183790a779fSJames Molloy } 184790a779fSJames Molloy 185790a779fSJames Molloy /// Generate the pipeline prolog code. 186790a779fSJames Molloy void ModuloScheduleExpander::generateProlog(unsigned LastStage, 187790a779fSJames Molloy MachineBasicBlock *KernelBB, 188790a779fSJames Molloy ValueMapTy *VRMap, 189790a779fSJames Molloy MBBVectorTy &PrologBBs) { 190790a779fSJames Molloy MachineBasicBlock *PredBB = Preheader; 191790a779fSJames Molloy InstrMapTy InstrMap; 192790a779fSJames Molloy 193790a779fSJames Molloy // Generate a basic block for each stage, not including the last stage, 194790a779fSJames Molloy // which will be generated in the kernel. Each basic block may contain 195790a779fSJames Molloy // instructions from multiple stages/iterations. 196790a779fSJames Molloy for (unsigned i = 0; i < LastStage; ++i) { 197790a779fSJames Molloy // Create and insert the prolog basic block prior to the original loop 198790a779fSJames Molloy // basic block. The original loop is removed later. 199790a779fSJames Molloy MachineBasicBlock *NewBB = MF.CreateMachineBasicBlock(BB->getBasicBlock()); 200790a779fSJames Molloy PrologBBs.push_back(NewBB); 201790a779fSJames Molloy MF.insert(BB->getIterator(), NewBB); 202790a779fSJames Molloy NewBB->transferSuccessors(PredBB); 203790a779fSJames Molloy PredBB->addSuccessor(NewBB); 204790a779fSJames Molloy PredBB = NewBB; 205790a779fSJames Molloy 206790a779fSJames Molloy // Generate instructions for each appropriate stage. Process instructions 207790a779fSJames Molloy // in original program order. 208790a779fSJames Molloy for (int StageNum = i; StageNum >= 0; --StageNum) { 209790a779fSJames Molloy for (MachineBasicBlock::iterator BBI = BB->instr_begin(), 210790a779fSJames Molloy BBE = BB->getFirstTerminator(); 211790a779fSJames Molloy BBI != BBE; ++BBI) { 212790a779fSJames Molloy if (Schedule.getStage(&*BBI) == StageNum) { 213790a779fSJames Molloy if (BBI->isPHI()) 214790a779fSJames Molloy continue; 215790a779fSJames Molloy MachineInstr *NewMI = 216790a779fSJames Molloy cloneAndChangeInstr(&*BBI, i, (unsigned)StageNum); 217790a779fSJames Molloy updateInstruction(NewMI, false, i, (unsigned)StageNum, VRMap); 218790a779fSJames Molloy NewBB->push_back(NewMI); 219790a779fSJames Molloy InstrMap[NewMI] = &*BBI; 220790a779fSJames Molloy } 221790a779fSJames Molloy } 222790a779fSJames Molloy } 223790a779fSJames Molloy rewritePhiValues(NewBB, i, VRMap, InstrMap); 224790a779fSJames Molloy LLVM_DEBUG({ 225790a779fSJames Molloy dbgs() << "prolog:\n"; 226790a779fSJames Molloy NewBB->dump(); 227790a779fSJames Molloy }); 228790a779fSJames Molloy } 229790a779fSJames Molloy 230790a779fSJames Molloy PredBB->replaceSuccessor(BB, KernelBB); 231790a779fSJames Molloy 232790a779fSJames Molloy // Check if we need to remove the branch from the preheader to the original 233790a779fSJames Molloy // loop, and replace it with a branch to the new loop. 234790a779fSJames Molloy unsigned numBranches = TII->removeBranch(*Preheader); 235790a779fSJames Molloy if (numBranches) { 236790a779fSJames Molloy SmallVector<MachineOperand, 0> Cond; 237790a779fSJames Molloy TII->insertBranch(*Preheader, PrologBBs[0], nullptr, Cond, DebugLoc()); 238790a779fSJames Molloy } 239790a779fSJames Molloy } 240790a779fSJames Molloy 241790a779fSJames Molloy /// Generate the pipeline epilog code. The epilog code finishes the iterations 242790a779fSJames Molloy /// that were started in either the prolog or the kernel. We create a basic 243790a779fSJames Molloy /// block for each stage that needs to complete. 244790a779fSJames Molloy void ModuloScheduleExpander::generateEpilog(unsigned LastStage, 245790a779fSJames Molloy MachineBasicBlock *KernelBB, 246790a779fSJames Molloy ValueMapTy *VRMap, 247790a779fSJames Molloy MBBVectorTy &EpilogBBs, 248790a779fSJames Molloy MBBVectorTy &PrologBBs) { 249790a779fSJames Molloy // We need to change the branch from the kernel to the first epilog block, so 250790a779fSJames Molloy // this call to analyze branch uses the kernel rather than the original BB. 251790a779fSJames Molloy MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 252790a779fSJames Molloy SmallVector<MachineOperand, 4> Cond; 253790a779fSJames Molloy bool checkBranch = TII->analyzeBranch(*KernelBB, TBB, FBB, Cond); 254790a779fSJames Molloy assert(!checkBranch && "generateEpilog must be able to analyze the branch"); 255790a779fSJames Molloy if (checkBranch) 256790a779fSJames Molloy return; 257790a779fSJames Molloy 258790a779fSJames Molloy MachineBasicBlock::succ_iterator LoopExitI = KernelBB->succ_begin(); 259790a779fSJames Molloy if (*LoopExitI == KernelBB) 260790a779fSJames Molloy ++LoopExitI; 261790a779fSJames Molloy assert(LoopExitI != KernelBB->succ_end() && "Expecting a successor"); 262790a779fSJames Molloy MachineBasicBlock *LoopExitBB = *LoopExitI; 263790a779fSJames Molloy 264790a779fSJames Molloy MachineBasicBlock *PredBB = KernelBB; 265790a779fSJames Molloy MachineBasicBlock *EpilogStart = LoopExitBB; 266790a779fSJames Molloy InstrMapTy InstrMap; 267790a779fSJames Molloy 268790a779fSJames Molloy // Generate a basic block for each stage, not including the last stage, 269790a779fSJames Molloy // which was generated for the kernel. Each basic block may contain 270790a779fSJames Molloy // instructions from multiple stages/iterations. 271790a779fSJames Molloy int EpilogStage = LastStage + 1; 272790a779fSJames Molloy for (unsigned i = LastStage; i >= 1; --i, ++EpilogStage) { 273790a779fSJames Molloy MachineBasicBlock *NewBB = MF.CreateMachineBasicBlock(); 274790a779fSJames Molloy EpilogBBs.push_back(NewBB); 275790a779fSJames Molloy MF.insert(BB->getIterator(), NewBB); 276790a779fSJames Molloy 277790a779fSJames Molloy PredBB->replaceSuccessor(LoopExitBB, NewBB); 278790a779fSJames Molloy NewBB->addSuccessor(LoopExitBB); 279790a779fSJames Molloy 280790a779fSJames Molloy if (EpilogStart == LoopExitBB) 281790a779fSJames Molloy EpilogStart = NewBB; 282790a779fSJames Molloy 283790a779fSJames Molloy // Add instructions to the epilog depending on the current block. 284790a779fSJames Molloy // Process instructions in original program order. 285790a779fSJames Molloy for (unsigned StageNum = i; StageNum <= LastStage; ++StageNum) { 286790a779fSJames Molloy for (auto &BBI : *BB) { 287790a779fSJames Molloy if (BBI.isPHI()) 288790a779fSJames Molloy continue; 289790a779fSJames Molloy MachineInstr *In = &BBI; 290790a779fSJames Molloy if ((unsigned)Schedule.getStage(In) == StageNum) { 291790a779fSJames Molloy // Instructions with memoperands in the epilog are updated with 292790a779fSJames Molloy // conservative values. 293790a779fSJames Molloy MachineInstr *NewMI = cloneInstr(In, UINT_MAX, 0); 294790a779fSJames Molloy updateInstruction(NewMI, i == 1, EpilogStage, 0, VRMap); 295790a779fSJames Molloy NewBB->push_back(NewMI); 296790a779fSJames Molloy InstrMap[NewMI] = In; 297790a779fSJames Molloy } 298790a779fSJames Molloy } 299790a779fSJames Molloy } 300790a779fSJames Molloy generateExistingPhis(NewBB, PrologBBs[i - 1], PredBB, KernelBB, VRMap, 301790a779fSJames Molloy InstrMap, LastStage, EpilogStage, i == 1); 302790a779fSJames Molloy generatePhis(NewBB, PrologBBs[i - 1], PredBB, KernelBB, VRMap, InstrMap, 303790a779fSJames Molloy LastStage, EpilogStage, i == 1); 304790a779fSJames Molloy PredBB = NewBB; 305790a779fSJames Molloy 306790a779fSJames Molloy LLVM_DEBUG({ 307790a779fSJames Molloy dbgs() << "epilog:\n"; 308790a779fSJames Molloy NewBB->dump(); 309790a779fSJames Molloy }); 310790a779fSJames Molloy } 311790a779fSJames Molloy 312790a779fSJames Molloy // Fix any Phi nodes in the loop exit block. 313790a779fSJames Molloy LoopExitBB->replacePhiUsesWith(BB, PredBB); 314790a779fSJames Molloy 315790a779fSJames Molloy // Create a branch to the new epilog from the kernel. 316790a779fSJames Molloy // Remove the original branch and add a new branch to the epilog. 317790a779fSJames Molloy TII->removeBranch(*KernelBB); 318790a779fSJames Molloy TII->insertBranch(*KernelBB, KernelBB, EpilogStart, Cond, DebugLoc()); 319790a779fSJames Molloy // Add a branch to the loop exit. 320790a779fSJames Molloy if (EpilogBBs.size() > 0) { 321790a779fSJames Molloy MachineBasicBlock *LastEpilogBB = EpilogBBs.back(); 322790a779fSJames Molloy SmallVector<MachineOperand, 4> Cond1; 323790a779fSJames Molloy TII->insertBranch(*LastEpilogBB, LoopExitBB, nullptr, Cond1, DebugLoc()); 324790a779fSJames Molloy } 325790a779fSJames Molloy } 326790a779fSJames Molloy 327790a779fSJames Molloy /// Replace all uses of FromReg that appear outside the specified 328790a779fSJames Molloy /// basic block with ToReg. 329790a779fSJames Molloy static void replaceRegUsesAfterLoop(unsigned FromReg, unsigned ToReg, 330790a779fSJames Molloy MachineBasicBlock *MBB, 331790a779fSJames Molloy MachineRegisterInfo &MRI, 332790a779fSJames Molloy LiveIntervals &LIS) { 333790a779fSJames Molloy for (MachineRegisterInfo::use_iterator I = MRI.use_begin(FromReg), 334790a779fSJames Molloy E = MRI.use_end(); 335790a779fSJames Molloy I != E;) { 336790a779fSJames Molloy MachineOperand &O = *I; 337790a779fSJames Molloy ++I; 338790a779fSJames Molloy if (O.getParent()->getParent() != MBB) 339790a779fSJames Molloy O.setReg(ToReg); 340790a779fSJames Molloy } 341790a779fSJames Molloy if (!LIS.hasInterval(ToReg)) 342790a779fSJames Molloy LIS.createEmptyInterval(ToReg); 343790a779fSJames Molloy } 344790a779fSJames Molloy 345790a779fSJames Molloy /// Return true if the register has a use that occurs outside the 346790a779fSJames Molloy /// specified loop. 347790a779fSJames Molloy static bool hasUseAfterLoop(unsigned Reg, MachineBasicBlock *BB, 348790a779fSJames Molloy MachineRegisterInfo &MRI) { 349790a779fSJames Molloy for (MachineRegisterInfo::use_iterator I = MRI.use_begin(Reg), 350790a779fSJames Molloy E = MRI.use_end(); 351790a779fSJames Molloy I != E; ++I) 352790a779fSJames Molloy if (I->getParent()->getParent() != BB) 353790a779fSJames Molloy return true; 354790a779fSJames Molloy return false; 355790a779fSJames Molloy } 356790a779fSJames Molloy 357790a779fSJames Molloy /// Generate Phis for the specific block in the generated pipelined code. 358790a779fSJames Molloy /// This function looks at the Phis from the original code to guide the 359790a779fSJames Molloy /// creation of new Phis. 360790a779fSJames Molloy void ModuloScheduleExpander::generateExistingPhis( 361790a779fSJames Molloy MachineBasicBlock *NewBB, MachineBasicBlock *BB1, MachineBasicBlock *BB2, 362790a779fSJames Molloy MachineBasicBlock *KernelBB, ValueMapTy *VRMap, InstrMapTy &InstrMap, 363790a779fSJames Molloy unsigned LastStageNum, unsigned CurStageNum, bool IsLast) { 364790a779fSJames Molloy // Compute the stage number for the initial value of the Phi, which 365790a779fSJames Molloy // comes from the prolog. The prolog to use depends on to which kernel/ 366790a779fSJames Molloy // epilog that we're adding the Phi. 367790a779fSJames Molloy unsigned PrologStage = 0; 368790a779fSJames Molloy unsigned PrevStage = 0; 369790a779fSJames Molloy bool InKernel = (LastStageNum == CurStageNum); 370790a779fSJames Molloy if (InKernel) { 371790a779fSJames Molloy PrologStage = LastStageNum - 1; 372790a779fSJames Molloy PrevStage = CurStageNum; 373790a779fSJames Molloy } else { 374790a779fSJames Molloy PrologStage = LastStageNum - (CurStageNum - LastStageNum); 375790a779fSJames Molloy PrevStage = LastStageNum + (CurStageNum - LastStageNum) - 1; 376790a779fSJames Molloy } 377790a779fSJames Molloy 378790a779fSJames Molloy for (MachineBasicBlock::iterator BBI = BB->instr_begin(), 379790a779fSJames Molloy BBE = BB->getFirstNonPHI(); 380790a779fSJames Molloy BBI != BBE; ++BBI) { 381790a779fSJames Molloy Register Def = BBI->getOperand(0).getReg(); 382790a779fSJames Molloy 383790a779fSJames Molloy unsigned InitVal = 0; 384790a779fSJames Molloy unsigned LoopVal = 0; 385790a779fSJames Molloy getPhiRegs(*BBI, BB, InitVal, LoopVal); 386790a779fSJames Molloy 387790a779fSJames Molloy unsigned PhiOp1 = 0; 388790a779fSJames Molloy // The Phi value from the loop body typically is defined in the loop, but 389790a779fSJames Molloy // not always. So, we need to check if the value is defined in the loop. 390790a779fSJames Molloy unsigned PhiOp2 = LoopVal; 391790a779fSJames Molloy if (VRMap[LastStageNum].count(LoopVal)) 392790a779fSJames Molloy PhiOp2 = VRMap[LastStageNum][LoopVal]; 393790a779fSJames Molloy 394790a779fSJames Molloy int StageScheduled = Schedule.getStage(&*BBI); 395790a779fSJames Molloy int LoopValStage = Schedule.getStage(MRI.getVRegDef(LoopVal)); 396790a779fSJames Molloy unsigned NumStages = getStagesForReg(Def, CurStageNum); 397790a779fSJames Molloy if (NumStages == 0) { 398790a779fSJames Molloy // We don't need to generate a Phi anymore, but we need to rename any uses 399790a779fSJames Molloy // of the Phi value. 400790a779fSJames Molloy unsigned NewReg = VRMap[PrevStage][LoopVal]; 401790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, 0, &*BBI, Def, 402790a779fSJames Molloy InitVal, NewReg); 403790a779fSJames Molloy if (VRMap[CurStageNum].count(LoopVal)) 404790a779fSJames Molloy VRMap[CurStageNum][Def] = VRMap[CurStageNum][LoopVal]; 405790a779fSJames Molloy } 406790a779fSJames Molloy // Adjust the number of Phis needed depending on the number of prologs left, 407790a779fSJames Molloy // and the distance from where the Phi is first scheduled. The number of 408790a779fSJames Molloy // Phis cannot exceed the number of prolog stages. Each stage can 409790a779fSJames Molloy // potentially define two values. 410790a779fSJames Molloy unsigned MaxPhis = PrologStage + 2; 411790a779fSJames Molloy if (!InKernel && (int)PrologStage <= LoopValStage) 412790a779fSJames Molloy MaxPhis = std::max((int)MaxPhis - (int)LoopValStage, 1); 413790a779fSJames Molloy unsigned NumPhis = std::min(NumStages, MaxPhis); 414790a779fSJames Molloy 415790a779fSJames Molloy unsigned NewReg = 0; 416790a779fSJames Molloy unsigned AccessStage = (LoopValStage != -1) ? LoopValStage : StageScheduled; 417790a779fSJames Molloy // In the epilog, we may need to look back one stage to get the correct 418790a779fSJames Molloy // Phi name because the epilog and prolog blocks execute the same stage. 419790a779fSJames Molloy // The correct name is from the previous block only when the Phi has 420790a779fSJames Molloy // been completely scheduled prior to the epilog, and Phi value is not 421790a779fSJames Molloy // needed in multiple stages. 422790a779fSJames Molloy int StageDiff = 0; 423790a779fSJames Molloy if (!InKernel && StageScheduled >= LoopValStage && AccessStage == 0 && 424790a779fSJames Molloy NumPhis == 1) 425790a779fSJames Molloy StageDiff = 1; 426790a779fSJames Molloy // Adjust the computations below when the phi and the loop definition 427790a779fSJames Molloy // are scheduled in different stages. 428790a779fSJames Molloy if (InKernel && LoopValStage != -1 && StageScheduled > LoopValStage) 429790a779fSJames Molloy StageDiff = StageScheduled - LoopValStage; 430790a779fSJames Molloy for (unsigned np = 0; np < NumPhis; ++np) { 431790a779fSJames Molloy // If the Phi hasn't been scheduled, then use the initial Phi operand 432790a779fSJames Molloy // value. Otherwise, use the scheduled version of the instruction. This 433790a779fSJames Molloy // is a little complicated when a Phi references another Phi. 434790a779fSJames Molloy if (np > PrologStage || StageScheduled >= (int)LastStageNum) 435790a779fSJames Molloy PhiOp1 = InitVal; 436790a779fSJames Molloy // Check if the Phi has already been scheduled in a prolog stage. 437790a779fSJames Molloy else if (PrologStage >= AccessStage + StageDiff + np && 438790a779fSJames Molloy VRMap[PrologStage - StageDiff - np].count(LoopVal) != 0) 439790a779fSJames Molloy PhiOp1 = VRMap[PrologStage - StageDiff - np][LoopVal]; 440790a779fSJames Molloy // Check if the Phi has already been scheduled, but the loop instruction 441790a779fSJames Molloy // is either another Phi, or doesn't occur in the loop. 442790a779fSJames Molloy else if (PrologStage >= AccessStage + StageDiff + np) { 443790a779fSJames Molloy // If the Phi references another Phi, we need to examine the other 444790a779fSJames Molloy // Phi to get the correct value. 445790a779fSJames Molloy PhiOp1 = LoopVal; 446790a779fSJames Molloy MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1); 447790a779fSJames Molloy int Indirects = 1; 448790a779fSJames Molloy while (InstOp1 && InstOp1->isPHI() && InstOp1->getParent() == BB) { 449790a779fSJames Molloy int PhiStage = Schedule.getStage(InstOp1); 450790a779fSJames Molloy if ((int)(PrologStage - StageDiff - np) < PhiStage + Indirects) 451790a779fSJames Molloy PhiOp1 = getInitPhiReg(*InstOp1, BB); 452790a779fSJames Molloy else 453790a779fSJames Molloy PhiOp1 = getLoopPhiReg(*InstOp1, BB); 454790a779fSJames Molloy InstOp1 = MRI.getVRegDef(PhiOp1); 455790a779fSJames Molloy int PhiOpStage = Schedule.getStage(InstOp1); 456790a779fSJames Molloy int StageAdj = (PhiOpStage != -1 ? PhiStage - PhiOpStage : 0); 457790a779fSJames Molloy if (PhiOpStage != -1 && PrologStage - StageAdj >= Indirects + np && 458790a779fSJames Molloy VRMap[PrologStage - StageAdj - Indirects - np].count(PhiOp1)) { 459790a779fSJames Molloy PhiOp1 = VRMap[PrologStage - StageAdj - Indirects - np][PhiOp1]; 460790a779fSJames Molloy break; 461790a779fSJames Molloy } 462790a779fSJames Molloy ++Indirects; 463790a779fSJames Molloy } 464790a779fSJames Molloy } else 465790a779fSJames Molloy PhiOp1 = InitVal; 466790a779fSJames Molloy // If this references a generated Phi in the kernel, get the Phi operand 467790a779fSJames Molloy // from the incoming block. 468790a779fSJames Molloy if (MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1)) 469790a779fSJames Molloy if (InstOp1->isPHI() && InstOp1->getParent() == KernelBB) 470790a779fSJames Molloy PhiOp1 = getInitPhiReg(*InstOp1, KernelBB); 471790a779fSJames Molloy 472790a779fSJames Molloy MachineInstr *PhiInst = MRI.getVRegDef(LoopVal); 473790a779fSJames Molloy bool LoopDefIsPhi = PhiInst && PhiInst->isPHI(); 474790a779fSJames Molloy // In the epilog, a map lookup is needed to get the value from the kernel, 475790a779fSJames Molloy // or previous epilog block. How is does this depends on if the 476790a779fSJames Molloy // instruction is scheduled in the previous block. 477790a779fSJames Molloy if (!InKernel) { 478790a779fSJames Molloy int StageDiffAdj = 0; 479790a779fSJames Molloy if (LoopValStage != -1 && StageScheduled > LoopValStage) 480790a779fSJames Molloy StageDiffAdj = StageScheduled - LoopValStage; 481790a779fSJames Molloy // Use the loop value defined in the kernel, unless the kernel 482790a779fSJames Molloy // contains the last definition of the Phi. 483790a779fSJames Molloy if (np == 0 && PrevStage == LastStageNum && 484790a779fSJames Molloy (StageScheduled != 0 || LoopValStage != 0) && 485790a779fSJames Molloy VRMap[PrevStage - StageDiffAdj].count(LoopVal)) 486790a779fSJames Molloy PhiOp2 = VRMap[PrevStage - StageDiffAdj][LoopVal]; 487790a779fSJames Molloy // Use the value defined by the Phi. We add one because we switch 488790a779fSJames Molloy // from looking at the loop value to the Phi definition. 489790a779fSJames Molloy else if (np > 0 && PrevStage == LastStageNum && 490790a779fSJames Molloy VRMap[PrevStage - np + 1].count(Def)) 491790a779fSJames Molloy PhiOp2 = VRMap[PrevStage - np + 1][Def]; 492790a779fSJames Molloy // Use the loop value defined in the kernel. 493790a779fSJames Molloy else if (static_cast<unsigned>(LoopValStage) > PrologStage + 1 && 494790a779fSJames Molloy VRMap[PrevStage - StageDiffAdj - np].count(LoopVal)) 495790a779fSJames Molloy PhiOp2 = VRMap[PrevStage - StageDiffAdj - np][LoopVal]; 496790a779fSJames Molloy // Use the value defined by the Phi, unless we're generating the first 497790a779fSJames Molloy // epilog and the Phi refers to a Phi in a different stage. 498790a779fSJames Molloy else if (VRMap[PrevStage - np].count(Def) && 499790a779fSJames Molloy (!LoopDefIsPhi || (PrevStage != LastStageNum) || 500790a779fSJames Molloy (LoopValStage == StageScheduled))) 501790a779fSJames Molloy PhiOp2 = VRMap[PrevStage - np][Def]; 502790a779fSJames Molloy } 503790a779fSJames Molloy 504790a779fSJames Molloy // Check if we can reuse an existing Phi. This occurs when a Phi 505790a779fSJames Molloy // references another Phi, and the other Phi is scheduled in an 506790a779fSJames Molloy // earlier stage. We can try to reuse an existing Phi up until the last 507790a779fSJames Molloy // stage of the current Phi. 508790a779fSJames Molloy if (LoopDefIsPhi) { 509790a779fSJames Molloy if (static_cast<int>(PrologStage - np) >= StageScheduled) { 510790a779fSJames Molloy int LVNumStages = getStagesForPhi(LoopVal); 511790a779fSJames Molloy int StageDiff = (StageScheduled - LoopValStage); 512790a779fSJames Molloy LVNumStages -= StageDiff; 513790a779fSJames Molloy // Make sure the loop value Phi has been processed already. 514790a779fSJames Molloy if (LVNumStages > (int)np && VRMap[CurStageNum].count(LoopVal)) { 515790a779fSJames Molloy NewReg = PhiOp2; 516790a779fSJames Molloy unsigned ReuseStage = CurStageNum; 517790a779fSJames Molloy if (isLoopCarried(*PhiInst)) 518790a779fSJames Molloy ReuseStage -= LVNumStages; 519790a779fSJames Molloy // Check if the Phi to reuse has been generated yet. If not, then 520790a779fSJames Molloy // there is nothing to reuse. 521790a779fSJames Molloy if (VRMap[ReuseStage - np].count(LoopVal)) { 522790a779fSJames Molloy NewReg = VRMap[ReuseStage - np][LoopVal]; 523790a779fSJames Molloy 524790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, np, &*BBI, 525790a779fSJames Molloy Def, NewReg); 526790a779fSJames Molloy // Update the map with the new Phi name. 527790a779fSJames Molloy VRMap[CurStageNum - np][Def] = NewReg; 528790a779fSJames Molloy PhiOp2 = NewReg; 529790a779fSJames Molloy if (VRMap[LastStageNum - np - 1].count(LoopVal)) 530790a779fSJames Molloy PhiOp2 = VRMap[LastStageNum - np - 1][LoopVal]; 531790a779fSJames Molloy 532790a779fSJames Molloy if (IsLast && np == NumPhis - 1) 533790a779fSJames Molloy replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS); 534790a779fSJames Molloy continue; 535790a779fSJames Molloy } 536790a779fSJames Molloy } 537790a779fSJames Molloy } 538790a779fSJames Molloy if (InKernel && StageDiff > 0 && 539790a779fSJames Molloy VRMap[CurStageNum - StageDiff - np].count(LoopVal)) 540790a779fSJames Molloy PhiOp2 = VRMap[CurStageNum - StageDiff - np][LoopVal]; 541790a779fSJames Molloy } 542790a779fSJames Molloy 543790a779fSJames Molloy const TargetRegisterClass *RC = MRI.getRegClass(Def); 544790a779fSJames Molloy NewReg = MRI.createVirtualRegister(RC); 545790a779fSJames Molloy 546790a779fSJames Molloy MachineInstrBuilder NewPhi = 547790a779fSJames Molloy BuildMI(*NewBB, NewBB->getFirstNonPHI(), DebugLoc(), 548790a779fSJames Molloy TII->get(TargetOpcode::PHI), NewReg); 549790a779fSJames Molloy NewPhi.addReg(PhiOp1).addMBB(BB1); 550790a779fSJames Molloy NewPhi.addReg(PhiOp2).addMBB(BB2); 551790a779fSJames Molloy if (np == 0) 552790a779fSJames Molloy InstrMap[NewPhi] = &*BBI; 553790a779fSJames Molloy 554790a779fSJames Molloy // We define the Phis after creating the new pipelined code, so 555790a779fSJames Molloy // we need to rename the Phi values in scheduled instructions. 556790a779fSJames Molloy 557790a779fSJames Molloy unsigned PrevReg = 0; 558790a779fSJames Molloy if (InKernel && VRMap[PrevStage - np].count(LoopVal)) 559790a779fSJames Molloy PrevReg = VRMap[PrevStage - np][LoopVal]; 560790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, np, &*BBI, Def, 561790a779fSJames Molloy NewReg, PrevReg); 562790a779fSJames Molloy // If the Phi has been scheduled, use the new name for rewriting. 563790a779fSJames Molloy if (VRMap[CurStageNum - np].count(Def)) { 564790a779fSJames Molloy unsigned R = VRMap[CurStageNum - np][Def]; 565790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, np, &*BBI, R, 566790a779fSJames Molloy NewReg); 567790a779fSJames Molloy } 568790a779fSJames Molloy 569790a779fSJames Molloy // Check if we need to rename any uses that occurs after the loop. The 570790a779fSJames Molloy // register to replace depends on whether the Phi is scheduled in the 571790a779fSJames Molloy // epilog. 572790a779fSJames Molloy if (IsLast && np == NumPhis - 1) 573790a779fSJames Molloy replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS); 574790a779fSJames Molloy 575790a779fSJames Molloy // In the kernel, a dependent Phi uses the value from this Phi. 576790a779fSJames Molloy if (InKernel) 577790a779fSJames Molloy PhiOp2 = NewReg; 578790a779fSJames Molloy 579790a779fSJames Molloy // Update the map with the new Phi name. 580790a779fSJames Molloy VRMap[CurStageNum - np][Def] = NewReg; 581790a779fSJames Molloy } 582790a779fSJames Molloy 583790a779fSJames Molloy while (NumPhis++ < NumStages) { 584790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, NumPhis, &*BBI, Def, 585790a779fSJames Molloy NewReg, 0); 586790a779fSJames Molloy } 587790a779fSJames Molloy 588790a779fSJames Molloy // Check if we need to rename a Phi that has been eliminated due to 589790a779fSJames Molloy // scheduling. 590790a779fSJames Molloy if (NumStages == 0 && IsLast && VRMap[CurStageNum].count(LoopVal)) 591790a779fSJames Molloy replaceRegUsesAfterLoop(Def, VRMap[CurStageNum][LoopVal], BB, MRI, LIS); 592790a779fSJames Molloy } 593790a779fSJames Molloy } 594790a779fSJames Molloy 595790a779fSJames Molloy /// Generate Phis for the specified block in the generated pipelined code. 596790a779fSJames Molloy /// These are new Phis needed because the definition is scheduled after the 597790a779fSJames Molloy /// use in the pipelined sequence. 598790a779fSJames Molloy void ModuloScheduleExpander::generatePhis( 599790a779fSJames Molloy MachineBasicBlock *NewBB, MachineBasicBlock *BB1, MachineBasicBlock *BB2, 600790a779fSJames Molloy MachineBasicBlock *KernelBB, ValueMapTy *VRMap, InstrMapTy &InstrMap, 601790a779fSJames Molloy unsigned LastStageNum, unsigned CurStageNum, bool IsLast) { 602790a779fSJames Molloy // Compute the stage number that contains the initial Phi value, and 603790a779fSJames Molloy // the Phi from the previous stage. 604790a779fSJames Molloy unsigned PrologStage = 0; 605790a779fSJames Molloy unsigned PrevStage = 0; 606790a779fSJames Molloy unsigned StageDiff = CurStageNum - LastStageNum; 607790a779fSJames Molloy bool InKernel = (StageDiff == 0); 608790a779fSJames Molloy if (InKernel) { 609790a779fSJames Molloy PrologStage = LastStageNum - 1; 610790a779fSJames Molloy PrevStage = CurStageNum; 611790a779fSJames Molloy } else { 612790a779fSJames Molloy PrologStage = LastStageNum - StageDiff; 613790a779fSJames Molloy PrevStage = LastStageNum + StageDiff - 1; 614790a779fSJames Molloy } 615790a779fSJames Molloy 616790a779fSJames Molloy for (MachineBasicBlock::iterator BBI = BB->getFirstNonPHI(), 617790a779fSJames Molloy BBE = BB->instr_end(); 618790a779fSJames Molloy BBI != BBE; ++BBI) { 619790a779fSJames Molloy for (unsigned i = 0, e = BBI->getNumOperands(); i != e; ++i) { 620790a779fSJames Molloy MachineOperand &MO = BBI->getOperand(i); 621790a779fSJames Molloy if (!MO.isReg() || !MO.isDef() || 622790a779fSJames Molloy !Register::isVirtualRegister(MO.getReg())) 623790a779fSJames Molloy continue; 624790a779fSJames Molloy 625790a779fSJames Molloy int StageScheduled = Schedule.getStage(&*BBI); 626790a779fSJames Molloy assert(StageScheduled != -1 && "Expecting scheduled instruction."); 627790a779fSJames Molloy Register Def = MO.getReg(); 628790a779fSJames Molloy unsigned NumPhis = getStagesForReg(Def, CurStageNum); 629790a779fSJames Molloy // An instruction scheduled in stage 0 and is used after the loop 630790a779fSJames Molloy // requires a phi in the epilog for the last definition from either 631790a779fSJames Molloy // the kernel or prolog. 632790a779fSJames Molloy if (!InKernel && NumPhis == 0 && StageScheduled == 0 && 633790a779fSJames Molloy hasUseAfterLoop(Def, BB, MRI)) 634790a779fSJames Molloy NumPhis = 1; 635790a779fSJames Molloy if (!InKernel && (unsigned)StageScheduled > PrologStage) 636790a779fSJames Molloy continue; 637790a779fSJames Molloy 638790a779fSJames Molloy unsigned PhiOp2 = VRMap[PrevStage][Def]; 639790a779fSJames Molloy if (MachineInstr *InstOp2 = MRI.getVRegDef(PhiOp2)) 640790a779fSJames Molloy if (InstOp2->isPHI() && InstOp2->getParent() == NewBB) 641790a779fSJames Molloy PhiOp2 = getLoopPhiReg(*InstOp2, BB2); 642790a779fSJames Molloy // The number of Phis can't exceed the number of prolog stages. The 643790a779fSJames Molloy // prolog stage number is zero based. 644790a779fSJames Molloy if (NumPhis > PrologStage + 1 - StageScheduled) 645790a779fSJames Molloy NumPhis = PrologStage + 1 - StageScheduled; 646790a779fSJames Molloy for (unsigned np = 0; np < NumPhis; ++np) { 647790a779fSJames Molloy unsigned PhiOp1 = VRMap[PrologStage][Def]; 648790a779fSJames Molloy if (np <= PrologStage) 649790a779fSJames Molloy PhiOp1 = VRMap[PrologStage - np][Def]; 650790a779fSJames Molloy if (MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1)) { 651790a779fSJames Molloy if (InstOp1->isPHI() && InstOp1->getParent() == KernelBB) 652790a779fSJames Molloy PhiOp1 = getInitPhiReg(*InstOp1, KernelBB); 653790a779fSJames Molloy if (InstOp1->isPHI() && InstOp1->getParent() == NewBB) 654790a779fSJames Molloy PhiOp1 = getInitPhiReg(*InstOp1, NewBB); 655790a779fSJames Molloy } 656790a779fSJames Molloy if (!InKernel) 657790a779fSJames Molloy PhiOp2 = VRMap[PrevStage - np][Def]; 658790a779fSJames Molloy 659790a779fSJames Molloy const TargetRegisterClass *RC = MRI.getRegClass(Def); 660790a779fSJames Molloy Register NewReg = MRI.createVirtualRegister(RC); 661790a779fSJames Molloy 662790a779fSJames Molloy MachineInstrBuilder NewPhi = 663790a779fSJames Molloy BuildMI(*NewBB, NewBB->getFirstNonPHI(), DebugLoc(), 664790a779fSJames Molloy TII->get(TargetOpcode::PHI), NewReg); 665790a779fSJames Molloy NewPhi.addReg(PhiOp1).addMBB(BB1); 666790a779fSJames Molloy NewPhi.addReg(PhiOp2).addMBB(BB2); 667790a779fSJames Molloy if (np == 0) 668790a779fSJames Molloy InstrMap[NewPhi] = &*BBI; 669790a779fSJames Molloy 670790a779fSJames Molloy // Rewrite uses and update the map. The actions depend upon whether 671790a779fSJames Molloy // we generating code for the kernel or epilog blocks. 672790a779fSJames Molloy if (InKernel) { 673790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, np, &*BBI, PhiOp1, 674790a779fSJames Molloy NewReg); 675790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, np, &*BBI, PhiOp2, 676790a779fSJames Molloy NewReg); 677790a779fSJames Molloy 678790a779fSJames Molloy PhiOp2 = NewReg; 679790a779fSJames Molloy VRMap[PrevStage - np - 1][Def] = NewReg; 680790a779fSJames Molloy } else { 681790a779fSJames Molloy VRMap[CurStageNum - np][Def] = NewReg; 682790a779fSJames Molloy if (np == NumPhis - 1) 683790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, CurStageNum, np, &*BBI, Def, 684790a779fSJames Molloy NewReg); 685790a779fSJames Molloy } 686790a779fSJames Molloy if (IsLast && np == NumPhis - 1) 687790a779fSJames Molloy replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS); 688790a779fSJames Molloy } 689790a779fSJames Molloy } 690790a779fSJames Molloy } 691790a779fSJames Molloy } 692790a779fSJames Molloy 693790a779fSJames Molloy /// Remove instructions that generate values with no uses. 694790a779fSJames Molloy /// Typically, these are induction variable operations that generate values 695790a779fSJames Molloy /// used in the loop itself. A dead instruction has a definition with 696790a779fSJames Molloy /// no uses, or uses that occur in the original loop only. 697790a779fSJames Molloy void ModuloScheduleExpander::removeDeadInstructions(MachineBasicBlock *KernelBB, 698790a779fSJames Molloy MBBVectorTy &EpilogBBs) { 699790a779fSJames Molloy // For each epilog block, check that the value defined by each instruction 700790a779fSJames Molloy // is used. If not, delete it. 701790a779fSJames Molloy for (MBBVectorTy::reverse_iterator MBB = EpilogBBs.rbegin(), 702790a779fSJames Molloy MBE = EpilogBBs.rend(); 703790a779fSJames Molloy MBB != MBE; ++MBB) 704790a779fSJames Molloy for (MachineBasicBlock::reverse_instr_iterator MI = (*MBB)->instr_rbegin(), 705790a779fSJames Molloy ME = (*MBB)->instr_rend(); 706790a779fSJames Molloy MI != ME;) { 707790a779fSJames Molloy // From DeadMachineInstructionElem. Don't delete inline assembly. 708790a779fSJames Molloy if (MI->isInlineAsm()) { 709790a779fSJames Molloy ++MI; 710790a779fSJames Molloy continue; 711790a779fSJames Molloy } 712790a779fSJames Molloy bool SawStore = false; 713790a779fSJames Molloy // Check if it's safe to remove the instruction due to side effects. 714790a779fSJames Molloy // We can, and want to, remove Phis here. 715790a779fSJames Molloy if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI()) { 716790a779fSJames Molloy ++MI; 717790a779fSJames Molloy continue; 718790a779fSJames Molloy } 719790a779fSJames Molloy bool used = true; 720790a779fSJames Molloy for (MachineInstr::mop_iterator MOI = MI->operands_begin(), 721790a779fSJames Molloy MOE = MI->operands_end(); 722790a779fSJames Molloy MOI != MOE; ++MOI) { 723790a779fSJames Molloy if (!MOI->isReg() || !MOI->isDef()) 724790a779fSJames Molloy continue; 725790a779fSJames Molloy Register reg = MOI->getReg(); 726790a779fSJames Molloy // Assume physical registers are used, unless they are marked dead. 727790a779fSJames Molloy if (Register::isPhysicalRegister(reg)) { 728790a779fSJames Molloy used = !MOI->isDead(); 729790a779fSJames Molloy if (used) 730790a779fSJames Molloy break; 731790a779fSJames Molloy continue; 732790a779fSJames Molloy } 733790a779fSJames Molloy unsigned realUses = 0; 734790a779fSJames Molloy for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(reg), 735790a779fSJames Molloy EI = MRI.use_end(); 736790a779fSJames Molloy UI != EI; ++UI) { 737790a779fSJames Molloy // Check if there are any uses that occur only in the original 738790a779fSJames Molloy // loop. If so, that's not a real use. 739790a779fSJames Molloy if (UI->getParent()->getParent() != BB) { 740790a779fSJames Molloy realUses++; 741790a779fSJames Molloy used = true; 742790a779fSJames Molloy break; 743790a779fSJames Molloy } 744790a779fSJames Molloy } 745790a779fSJames Molloy if (realUses > 0) 746790a779fSJames Molloy break; 747790a779fSJames Molloy used = false; 748790a779fSJames Molloy } 749790a779fSJames Molloy if (!used) { 750790a779fSJames Molloy LIS.RemoveMachineInstrFromMaps(*MI); 751790a779fSJames Molloy MI++->eraseFromParent(); 752790a779fSJames Molloy continue; 753790a779fSJames Molloy } 754790a779fSJames Molloy ++MI; 755790a779fSJames Molloy } 756790a779fSJames Molloy // In the kernel block, check if we can remove a Phi that generates a value 757790a779fSJames Molloy // used in an instruction removed in the epilog block. 758790a779fSJames Molloy for (MachineBasicBlock::iterator BBI = KernelBB->instr_begin(), 759790a779fSJames Molloy BBE = KernelBB->getFirstNonPHI(); 760790a779fSJames Molloy BBI != BBE;) { 761790a779fSJames Molloy MachineInstr *MI = &*BBI; 762790a779fSJames Molloy ++BBI; 763790a779fSJames Molloy Register reg = MI->getOperand(0).getReg(); 764790a779fSJames Molloy if (MRI.use_begin(reg) == MRI.use_end()) { 765790a779fSJames Molloy LIS.RemoveMachineInstrFromMaps(*MI); 766790a779fSJames Molloy MI->eraseFromParent(); 767790a779fSJames Molloy } 768790a779fSJames Molloy } 769790a779fSJames Molloy } 770790a779fSJames Molloy 771790a779fSJames Molloy /// For loop carried definitions, we split the lifetime of a virtual register 772790a779fSJames Molloy /// that has uses past the definition in the next iteration. A copy with a new 773790a779fSJames Molloy /// virtual register is inserted before the definition, which helps with 774790a779fSJames Molloy /// generating a better register assignment. 775790a779fSJames Molloy /// 776790a779fSJames Molloy /// v1 = phi(a, v2) v1 = phi(a, v2) 777790a779fSJames Molloy /// v2 = phi(b, v3) v2 = phi(b, v3) 778790a779fSJames Molloy /// v3 = .. v4 = copy v1 779790a779fSJames Molloy /// .. = V1 v3 = .. 780790a779fSJames Molloy /// .. = v4 781790a779fSJames Molloy void ModuloScheduleExpander::splitLifetimes(MachineBasicBlock *KernelBB, 782790a779fSJames Molloy MBBVectorTy &EpilogBBs) { 783790a779fSJames Molloy const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 784790a779fSJames Molloy for (auto &PHI : KernelBB->phis()) { 785790a779fSJames Molloy Register Def = PHI.getOperand(0).getReg(); 786790a779fSJames Molloy // Check for any Phi definition that used as an operand of another Phi 787790a779fSJames Molloy // in the same block. 788790a779fSJames Molloy for (MachineRegisterInfo::use_instr_iterator I = MRI.use_instr_begin(Def), 789790a779fSJames Molloy E = MRI.use_instr_end(); 790790a779fSJames Molloy I != E; ++I) { 791790a779fSJames Molloy if (I->isPHI() && I->getParent() == KernelBB) { 792790a779fSJames Molloy // Get the loop carried definition. 793790a779fSJames Molloy unsigned LCDef = getLoopPhiReg(PHI, KernelBB); 794790a779fSJames Molloy if (!LCDef) 795790a779fSJames Molloy continue; 796790a779fSJames Molloy MachineInstr *MI = MRI.getVRegDef(LCDef); 797790a779fSJames Molloy if (!MI || MI->getParent() != KernelBB || MI->isPHI()) 798790a779fSJames Molloy continue; 799790a779fSJames Molloy // Search through the rest of the block looking for uses of the Phi 800790a779fSJames Molloy // definition. If one occurs, then split the lifetime. 801790a779fSJames Molloy unsigned SplitReg = 0; 802790a779fSJames Molloy for (auto &BBJ : make_range(MachineBasicBlock::instr_iterator(MI), 803790a779fSJames Molloy KernelBB->instr_end())) 804790a779fSJames Molloy if (BBJ.readsRegister(Def)) { 805790a779fSJames Molloy // We split the lifetime when we find the first use. 806790a779fSJames Molloy if (SplitReg == 0) { 807790a779fSJames Molloy SplitReg = MRI.createVirtualRegister(MRI.getRegClass(Def)); 808790a779fSJames Molloy BuildMI(*KernelBB, MI, MI->getDebugLoc(), 809790a779fSJames Molloy TII->get(TargetOpcode::COPY), SplitReg) 810790a779fSJames Molloy .addReg(Def); 811790a779fSJames Molloy } 812790a779fSJames Molloy BBJ.substituteRegister(Def, SplitReg, 0, *TRI); 813790a779fSJames Molloy } 814790a779fSJames Molloy if (!SplitReg) 815790a779fSJames Molloy continue; 816790a779fSJames Molloy // Search through each of the epilog blocks for any uses to be renamed. 817790a779fSJames Molloy for (auto &Epilog : EpilogBBs) 818790a779fSJames Molloy for (auto &I : *Epilog) 819790a779fSJames Molloy if (I.readsRegister(Def)) 820790a779fSJames Molloy I.substituteRegister(Def, SplitReg, 0, *TRI); 821790a779fSJames Molloy break; 822790a779fSJames Molloy } 823790a779fSJames Molloy } 824790a779fSJames Molloy } 825790a779fSJames Molloy } 826790a779fSJames Molloy 827790a779fSJames Molloy /// Remove the incoming block from the Phis in a basic block. 828790a779fSJames Molloy static void removePhis(MachineBasicBlock *BB, MachineBasicBlock *Incoming) { 829790a779fSJames Molloy for (MachineInstr &MI : *BB) { 830790a779fSJames Molloy if (!MI.isPHI()) 831790a779fSJames Molloy break; 832790a779fSJames Molloy for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) 833790a779fSJames Molloy if (MI.getOperand(i + 1).getMBB() == Incoming) { 834790a779fSJames Molloy MI.RemoveOperand(i + 1); 835790a779fSJames Molloy MI.RemoveOperand(i); 836790a779fSJames Molloy break; 837790a779fSJames Molloy } 838790a779fSJames Molloy } 839790a779fSJames Molloy } 840790a779fSJames Molloy 841790a779fSJames Molloy /// Create branches from each prolog basic block to the appropriate epilog 842790a779fSJames Molloy /// block. These edges are needed if the loop ends before reaching the 843790a779fSJames Molloy /// kernel. 844790a779fSJames Molloy void ModuloScheduleExpander::addBranches(MachineBasicBlock &PreheaderBB, 845790a779fSJames Molloy MBBVectorTy &PrologBBs, 846790a779fSJames Molloy MachineBasicBlock *KernelBB, 847790a779fSJames Molloy MBBVectorTy &EpilogBBs, 848790a779fSJames Molloy ValueMapTy *VRMap) { 849790a779fSJames Molloy assert(PrologBBs.size() == EpilogBBs.size() && "Prolog/Epilog mismatch"); 850790a779fSJames Molloy MachineInstr *IndVar; 851790a779fSJames Molloy MachineInstr *Cmp; 852790a779fSJames Molloy if (TII->analyzeLoop(*Schedule.getLoop(), IndVar, Cmp)) 853790a779fSJames Molloy llvm_unreachable("Must be able to analyze loop!"); 854790a779fSJames Molloy MachineBasicBlock *LastPro = KernelBB; 855790a779fSJames Molloy MachineBasicBlock *LastEpi = KernelBB; 856790a779fSJames Molloy 857790a779fSJames Molloy // Start from the blocks connected to the kernel and work "out" 858790a779fSJames Molloy // to the first prolog and the last epilog blocks. 859790a779fSJames Molloy SmallVector<MachineInstr *, 4> PrevInsts; 860790a779fSJames Molloy unsigned MaxIter = PrologBBs.size() - 1; 861790a779fSJames Molloy unsigned LC = UINT_MAX; 862790a779fSJames Molloy unsigned LCMin = UINT_MAX; 863790a779fSJames Molloy for (unsigned i = 0, j = MaxIter; i <= MaxIter; ++i, --j) { 864790a779fSJames Molloy // Add branches to the prolog that go to the corresponding 865790a779fSJames Molloy // epilog, and the fall-thru prolog/kernel block. 866790a779fSJames Molloy MachineBasicBlock *Prolog = PrologBBs[j]; 867790a779fSJames Molloy MachineBasicBlock *Epilog = EpilogBBs[i]; 868790a779fSJames Molloy // We've executed one iteration, so decrement the loop count and check for 869790a779fSJames Molloy // the loop end. 870790a779fSJames Molloy SmallVector<MachineOperand, 4> Cond; 871790a779fSJames Molloy // Check if the LOOP0 has already been removed. If so, then there is no need 872790a779fSJames Molloy // to reduce the trip count. 873790a779fSJames Molloy if (LC != 0) 874790a779fSJames Molloy LC = TII->reduceLoopCount(*Prolog, PreheaderBB, IndVar, *Cmp, Cond, 875790a779fSJames Molloy PrevInsts, j, MaxIter); 876790a779fSJames Molloy 877790a779fSJames Molloy // Record the value of the first trip count, which is used to determine if 878790a779fSJames Molloy // branches and blocks can be removed for constant trip counts. 879790a779fSJames Molloy if (LCMin == UINT_MAX) 880790a779fSJames Molloy LCMin = LC; 881790a779fSJames Molloy 882790a779fSJames Molloy unsigned numAdded = 0; 883790a779fSJames Molloy if (Register::isVirtualRegister(LC)) { 884790a779fSJames Molloy Prolog->addSuccessor(Epilog); 885790a779fSJames Molloy numAdded = TII->insertBranch(*Prolog, Epilog, LastPro, Cond, DebugLoc()); 886790a779fSJames Molloy } else if (j >= LCMin) { 887790a779fSJames Molloy Prolog->addSuccessor(Epilog); 888790a779fSJames Molloy Prolog->removeSuccessor(LastPro); 889790a779fSJames Molloy LastEpi->removeSuccessor(Epilog); 890790a779fSJames Molloy numAdded = TII->insertBranch(*Prolog, Epilog, nullptr, Cond, DebugLoc()); 891790a779fSJames Molloy removePhis(Epilog, LastEpi); 892790a779fSJames Molloy // Remove the blocks that are no longer referenced. 893790a779fSJames Molloy if (LastPro != LastEpi) { 894790a779fSJames Molloy LastEpi->clear(); 895790a779fSJames Molloy LastEpi->eraseFromParent(); 896790a779fSJames Molloy } 897790a779fSJames Molloy LastPro->clear(); 898790a779fSJames Molloy LastPro->eraseFromParent(); 899fef9f590SJames Molloy if (LastPro == KernelBB) 900fef9f590SJames Molloy NewKernel = nullptr; 901790a779fSJames Molloy } else { 902790a779fSJames Molloy numAdded = TII->insertBranch(*Prolog, LastPro, nullptr, Cond, DebugLoc()); 903790a779fSJames Molloy removePhis(Epilog, Prolog); 904790a779fSJames Molloy } 905790a779fSJames Molloy LastPro = Prolog; 906790a779fSJames Molloy LastEpi = Epilog; 907790a779fSJames Molloy for (MachineBasicBlock::reverse_instr_iterator I = Prolog->instr_rbegin(), 908790a779fSJames Molloy E = Prolog->instr_rend(); 909790a779fSJames Molloy I != E && numAdded > 0; ++I, --numAdded) 910790a779fSJames Molloy updateInstruction(&*I, false, j, 0, VRMap); 911790a779fSJames Molloy } 912790a779fSJames Molloy } 913790a779fSJames Molloy 914790a779fSJames Molloy /// Return true if we can compute the amount the instruction changes 915790a779fSJames Molloy /// during each iteration. Set Delta to the amount of the change. 916790a779fSJames Molloy bool ModuloScheduleExpander::computeDelta(MachineInstr &MI, unsigned &Delta) { 917790a779fSJames Molloy const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 918790a779fSJames Molloy const MachineOperand *BaseOp; 919790a779fSJames Molloy int64_t Offset; 920790a779fSJames Molloy if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, TRI)) 921790a779fSJames Molloy return false; 922790a779fSJames Molloy 923790a779fSJames Molloy if (!BaseOp->isReg()) 924790a779fSJames Molloy return false; 925790a779fSJames Molloy 926790a779fSJames Molloy Register BaseReg = BaseOp->getReg(); 927790a779fSJames Molloy 928790a779fSJames Molloy MachineRegisterInfo &MRI = MF.getRegInfo(); 929790a779fSJames Molloy // Check if there is a Phi. If so, get the definition in the loop. 930790a779fSJames Molloy MachineInstr *BaseDef = MRI.getVRegDef(BaseReg); 931790a779fSJames Molloy if (BaseDef && BaseDef->isPHI()) { 932790a779fSJames Molloy BaseReg = getLoopPhiReg(*BaseDef, MI.getParent()); 933790a779fSJames Molloy BaseDef = MRI.getVRegDef(BaseReg); 934790a779fSJames Molloy } 935790a779fSJames Molloy if (!BaseDef) 936790a779fSJames Molloy return false; 937790a779fSJames Molloy 938790a779fSJames Molloy int D = 0; 939790a779fSJames Molloy if (!TII->getIncrementValue(*BaseDef, D) && D >= 0) 940790a779fSJames Molloy return false; 941790a779fSJames Molloy 942790a779fSJames Molloy Delta = D; 943790a779fSJames Molloy return true; 944790a779fSJames Molloy } 945790a779fSJames Molloy 946790a779fSJames Molloy /// Update the memory operand with a new offset when the pipeliner 947790a779fSJames Molloy /// generates a new copy of the instruction that refers to a 948790a779fSJames Molloy /// different memory location. 949790a779fSJames Molloy void ModuloScheduleExpander::updateMemOperands(MachineInstr &NewMI, 950790a779fSJames Molloy MachineInstr &OldMI, 951790a779fSJames Molloy unsigned Num) { 952790a779fSJames Molloy if (Num == 0) 953790a779fSJames Molloy return; 954790a779fSJames Molloy // If the instruction has memory operands, then adjust the offset 955790a779fSJames Molloy // when the instruction appears in different stages. 956790a779fSJames Molloy if (NewMI.memoperands_empty()) 957790a779fSJames Molloy return; 958790a779fSJames Molloy SmallVector<MachineMemOperand *, 2> NewMMOs; 959790a779fSJames Molloy for (MachineMemOperand *MMO : NewMI.memoperands()) { 960790a779fSJames Molloy // TODO: Figure out whether isAtomic is really necessary (see D57601). 961790a779fSJames Molloy if (MMO->isVolatile() || MMO->isAtomic() || 962790a779fSJames Molloy (MMO->isInvariant() && MMO->isDereferenceable()) || 963790a779fSJames Molloy (!MMO->getValue())) { 964790a779fSJames Molloy NewMMOs.push_back(MMO); 965790a779fSJames Molloy continue; 966790a779fSJames Molloy } 967790a779fSJames Molloy unsigned Delta; 968790a779fSJames Molloy if (Num != UINT_MAX && computeDelta(OldMI, Delta)) { 969790a779fSJames Molloy int64_t AdjOffset = Delta * Num; 970790a779fSJames Molloy NewMMOs.push_back( 971790a779fSJames Molloy MF.getMachineMemOperand(MMO, AdjOffset, MMO->getSize())); 972790a779fSJames Molloy } else { 973790a779fSJames Molloy NewMMOs.push_back( 974790a779fSJames Molloy MF.getMachineMemOperand(MMO, 0, MemoryLocation::UnknownSize)); 975790a779fSJames Molloy } 976790a779fSJames Molloy } 977790a779fSJames Molloy NewMI.setMemRefs(MF, NewMMOs); 978790a779fSJames Molloy } 979790a779fSJames Molloy 980790a779fSJames Molloy /// Clone the instruction for the new pipelined loop and update the 981790a779fSJames Molloy /// memory operands, if needed. 982790a779fSJames Molloy MachineInstr *ModuloScheduleExpander::cloneInstr(MachineInstr *OldMI, 983790a779fSJames Molloy unsigned CurStageNum, 984790a779fSJames Molloy unsigned InstStageNum) { 985790a779fSJames Molloy MachineInstr *NewMI = MF.CloneMachineInstr(OldMI); 986790a779fSJames Molloy // Check for tied operands in inline asm instructions. This should be handled 987790a779fSJames Molloy // elsewhere, but I'm not sure of the best solution. 988790a779fSJames Molloy if (OldMI->isInlineAsm()) 989790a779fSJames Molloy for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) { 990790a779fSJames Molloy const auto &MO = OldMI->getOperand(i); 991790a779fSJames Molloy if (MO.isReg() && MO.isUse()) 992790a779fSJames Molloy break; 993790a779fSJames Molloy unsigned UseIdx; 994790a779fSJames Molloy if (OldMI->isRegTiedToUseOperand(i, &UseIdx)) 995790a779fSJames Molloy NewMI->tieOperands(i, UseIdx); 996790a779fSJames Molloy } 997790a779fSJames Molloy updateMemOperands(*NewMI, *OldMI, CurStageNum - InstStageNum); 998790a779fSJames Molloy return NewMI; 999790a779fSJames Molloy } 1000790a779fSJames Molloy 1001790a779fSJames Molloy /// Clone the instruction for the new pipelined loop. If needed, this 1002790a779fSJames Molloy /// function updates the instruction using the values saved in the 1003790a779fSJames Molloy /// InstrChanges structure. 1004790a779fSJames Molloy MachineInstr *ModuloScheduleExpander::cloneAndChangeInstr( 1005790a779fSJames Molloy MachineInstr *OldMI, unsigned CurStageNum, unsigned InstStageNum) { 1006790a779fSJames Molloy MachineInstr *NewMI = MF.CloneMachineInstr(OldMI); 1007790a779fSJames Molloy auto It = InstrChanges.find(OldMI); 1008790a779fSJames Molloy if (It != InstrChanges.end()) { 1009790a779fSJames Molloy std::pair<unsigned, int64_t> RegAndOffset = It->second; 1010790a779fSJames Molloy unsigned BasePos, OffsetPos; 1011790a779fSJames Molloy if (!TII->getBaseAndOffsetPosition(*OldMI, BasePos, OffsetPos)) 1012790a779fSJames Molloy return nullptr; 1013790a779fSJames Molloy int64_t NewOffset = OldMI->getOperand(OffsetPos).getImm(); 1014790a779fSJames Molloy MachineInstr *LoopDef = findDefInLoop(RegAndOffset.first); 1015790a779fSJames Molloy if (Schedule.getStage(LoopDef) > (signed)InstStageNum) 1016790a779fSJames Molloy NewOffset += RegAndOffset.second * (CurStageNum - InstStageNum); 1017790a779fSJames Molloy NewMI->getOperand(OffsetPos).setImm(NewOffset); 1018790a779fSJames Molloy } 1019790a779fSJames Molloy updateMemOperands(*NewMI, *OldMI, CurStageNum - InstStageNum); 1020790a779fSJames Molloy return NewMI; 1021790a779fSJames Molloy } 1022790a779fSJames Molloy 1023790a779fSJames Molloy /// Update the machine instruction with new virtual registers. This 1024790a779fSJames Molloy /// function may change the defintions and/or uses. 1025790a779fSJames Molloy void ModuloScheduleExpander::updateInstruction(MachineInstr *NewMI, 1026790a779fSJames Molloy bool LastDef, 1027790a779fSJames Molloy unsigned CurStageNum, 1028790a779fSJames Molloy unsigned InstrStageNum, 1029790a779fSJames Molloy ValueMapTy *VRMap) { 1030790a779fSJames Molloy for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) { 1031790a779fSJames Molloy MachineOperand &MO = NewMI->getOperand(i); 1032790a779fSJames Molloy if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg())) 1033790a779fSJames Molloy continue; 1034790a779fSJames Molloy Register reg = MO.getReg(); 1035790a779fSJames Molloy if (MO.isDef()) { 1036790a779fSJames Molloy // Create a new virtual register for the definition. 1037790a779fSJames Molloy const TargetRegisterClass *RC = MRI.getRegClass(reg); 1038790a779fSJames Molloy Register NewReg = MRI.createVirtualRegister(RC); 1039790a779fSJames Molloy MO.setReg(NewReg); 1040790a779fSJames Molloy VRMap[CurStageNum][reg] = NewReg; 1041790a779fSJames Molloy if (LastDef) 1042790a779fSJames Molloy replaceRegUsesAfterLoop(reg, NewReg, BB, MRI, LIS); 1043790a779fSJames Molloy } else if (MO.isUse()) { 1044790a779fSJames Molloy MachineInstr *Def = MRI.getVRegDef(reg); 1045790a779fSJames Molloy // Compute the stage that contains the last definition for instruction. 1046790a779fSJames Molloy int DefStageNum = Schedule.getStage(Def); 1047790a779fSJames Molloy unsigned StageNum = CurStageNum; 1048790a779fSJames Molloy if (DefStageNum != -1 && (int)InstrStageNum > DefStageNum) { 1049790a779fSJames Molloy // Compute the difference in stages between the defintion and the use. 1050790a779fSJames Molloy unsigned StageDiff = (InstrStageNum - DefStageNum); 1051790a779fSJames Molloy // Make an adjustment to get the last definition. 1052790a779fSJames Molloy StageNum -= StageDiff; 1053790a779fSJames Molloy } 1054790a779fSJames Molloy if (VRMap[StageNum].count(reg)) 1055790a779fSJames Molloy MO.setReg(VRMap[StageNum][reg]); 1056790a779fSJames Molloy } 1057790a779fSJames Molloy } 1058790a779fSJames Molloy } 1059790a779fSJames Molloy 1060790a779fSJames Molloy /// Return the instruction in the loop that defines the register. 1061790a779fSJames Molloy /// If the definition is a Phi, then follow the Phi operand to 1062790a779fSJames Molloy /// the instruction in the loop. 1063790a779fSJames Molloy MachineInstr *ModuloScheduleExpander::findDefInLoop(unsigned Reg) { 1064790a779fSJames Molloy SmallPtrSet<MachineInstr *, 8> Visited; 1065790a779fSJames Molloy MachineInstr *Def = MRI.getVRegDef(Reg); 1066790a779fSJames Molloy while (Def->isPHI()) { 1067790a779fSJames Molloy if (!Visited.insert(Def).second) 1068790a779fSJames Molloy break; 1069790a779fSJames Molloy for (unsigned i = 1, e = Def->getNumOperands(); i < e; i += 2) 1070790a779fSJames Molloy if (Def->getOperand(i + 1).getMBB() == BB) { 1071790a779fSJames Molloy Def = MRI.getVRegDef(Def->getOperand(i).getReg()); 1072790a779fSJames Molloy break; 1073790a779fSJames Molloy } 1074790a779fSJames Molloy } 1075790a779fSJames Molloy return Def; 1076790a779fSJames Molloy } 1077790a779fSJames Molloy 1078790a779fSJames Molloy /// Return the new name for the value from the previous stage. 1079790a779fSJames Molloy unsigned ModuloScheduleExpander::getPrevMapVal( 1080790a779fSJames Molloy unsigned StageNum, unsigned PhiStage, unsigned LoopVal, unsigned LoopStage, 1081790a779fSJames Molloy ValueMapTy *VRMap, MachineBasicBlock *BB) { 1082790a779fSJames Molloy unsigned PrevVal = 0; 1083790a779fSJames Molloy if (StageNum > PhiStage) { 1084790a779fSJames Molloy MachineInstr *LoopInst = MRI.getVRegDef(LoopVal); 1085790a779fSJames Molloy if (PhiStage == LoopStage && VRMap[StageNum - 1].count(LoopVal)) 1086790a779fSJames Molloy // The name is defined in the previous stage. 1087790a779fSJames Molloy PrevVal = VRMap[StageNum - 1][LoopVal]; 1088790a779fSJames Molloy else if (VRMap[StageNum].count(LoopVal)) 1089790a779fSJames Molloy // The previous name is defined in the current stage when the instruction 1090790a779fSJames Molloy // order is swapped. 1091790a779fSJames Molloy PrevVal = VRMap[StageNum][LoopVal]; 1092790a779fSJames Molloy else if (!LoopInst->isPHI() || LoopInst->getParent() != BB) 1093790a779fSJames Molloy // The loop value hasn't yet been scheduled. 1094790a779fSJames Molloy PrevVal = LoopVal; 1095790a779fSJames Molloy else if (StageNum == PhiStage + 1) 1096790a779fSJames Molloy // The loop value is another phi, which has not been scheduled. 1097790a779fSJames Molloy PrevVal = getInitPhiReg(*LoopInst, BB); 1098790a779fSJames Molloy else if (StageNum > PhiStage + 1 && LoopInst->getParent() == BB) 1099790a779fSJames Molloy // The loop value is another phi, which has been scheduled. 1100790a779fSJames Molloy PrevVal = 1101790a779fSJames Molloy getPrevMapVal(StageNum - 1, PhiStage, getLoopPhiReg(*LoopInst, BB), 1102790a779fSJames Molloy LoopStage, VRMap, BB); 1103790a779fSJames Molloy } 1104790a779fSJames Molloy return PrevVal; 1105790a779fSJames Molloy } 1106790a779fSJames Molloy 1107790a779fSJames Molloy /// Rewrite the Phi values in the specified block to use the mappings 1108790a779fSJames Molloy /// from the initial operand. Once the Phi is scheduled, we switch 1109790a779fSJames Molloy /// to using the loop value instead of the Phi value, so those names 1110790a779fSJames Molloy /// do not need to be rewritten. 1111790a779fSJames Molloy void ModuloScheduleExpander::rewritePhiValues(MachineBasicBlock *NewBB, 1112790a779fSJames Molloy unsigned StageNum, 1113790a779fSJames Molloy ValueMapTy *VRMap, 1114790a779fSJames Molloy InstrMapTy &InstrMap) { 1115790a779fSJames Molloy for (auto &PHI : BB->phis()) { 1116790a779fSJames Molloy unsigned InitVal = 0; 1117790a779fSJames Molloy unsigned LoopVal = 0; 1118790a779fSJames Molloy getPhiRegs(PHI, BB, InitVal, LoopVal); 1119790a779fSJames Molloy Register PhiDef = PHI.getOperand(0).getReg(); 1120790a779fSJames Molloy 1121790a779fSJames Molloy unsigned PhiStage = (unsigned)Schedule.getStage(MRI.getVRegDef(PhiDef)); 1122790a779fSJames Molloy unsigned LoopStage = (unsigned)Schedule.getStage(MRI.getVRegDef(LoopVal)); 1123790a779fSJames Molloy unsigned NumPhis = getStagesForPhi(PhiDef); 1124790a779fSJames Molloy if (NumPhis > StageNum) 1125790a779fSJames Molloy NumPhis = StageNum; 1126790a779fSJames Molloy for (unsigned np = 0; np <= NumPhis; ++np) { 1127790a779fSJames Molloy unsigned NewVal = 1128790a779fSJames Molloy getPrevMapVal(StageNum - np, PhiStage, LoopVal, LoopStage, VRMap, BB); 1129790a779fSJames Molloy if (!NewVal) 1130790a779fSJames Molloy NewVal = InitVal; 1131790a779fSJames Molloy rewriteScheduledInstr(NewBB, InstrMap, StageNum - np, np, &PHI, PhiDef, 1132790a779fSJames Molloy NewVal); 1133790a779fSJames Molloy } 1134790a779fSJames Molloy } 1135790a779fSJames Molloy } 1136790a779fSJames Molloy 1137790a779fSJames Molloy /// Rewrite a previously scheduled instruction to use the register value 1138790a779fSJames Molloy /// from the new instruction. Make sure the instruction occurs in the 1139790a779fSJames Molloy /// basic block, and we don't change the uses in the new instruction. 1140790a779fSJames Molloy void ModuloScheduleExpander::rewriteScheduledInstr( 1141790a779fSJames Molloy MachineBasicBlock *BB, InstrMapTy &InstrMap, unsigned CurStageNum, 1142790a779fSJames Molloy unsigned PhiNum, MachineInstr *Phi, unsigned OldReg, unsigned NewReg, 1143790a779fSJames Molloy unsigned PrevReg) { 1144790a779fSJames Molloy bool InProlog = (CurStageNum < (unsigned)Schedule.getNumStages() - 1); 1145790a779fSJames Molloy int StagePhi = Schedule.getStage(Phi) + PhiNum; 1146790a779fSJames Molloy // Rewrite uses that have been scheduled already to use the new 1147790a779fSJames Molloy // Phi register. 1148790a779fSJames Molloy for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(OldReg), 1149790a779fSJames Molloy EI = MRI.use_end(); 1150790a779fSJames Molloy UI != EI;) { 1151790a779fSJames Molloy MachineOperand &UseOp = *UI; 1152790a779fSJames Molloy MachineInstr *UseMI = UseOp.getParent(); 1153790a779fSJames Molloy ++UI; 1154790a779fSJames Molloy if (UseMI->getParent() != BB) 1155790a779fSJames Molloy continue; 1156790a779fSJames Molloy if (UseMI->isPHI()) { 1157790a779fSJames Molloy if (!Phi->isPHI() && UseMI->getOperand(0).getReg() == NewReg) 1158790a779fSJames Molloy continue; 1159790a779fSJames Molloy if (getLoopPhiReg(*UseMI, BB) != OldReg) 1160790a779fSJames Molloy continue; 1161790a779fSJames Molloy } 1162790a779fSJames Molloy InstrMapTy::iterator OrigInstr = InstrMap.find(UseMI); 1163790a779fSJames Molloy assert(OrigInstr != InstrMap.end() && "Instruction not scheduled."); 1164790a779fSJames Molloy MachineInstr *OrigMI = OrigInstr->second; 1165790a779fSJames Molloy int StageSched = Schedule.getStage(OrigMI); 1166790a779fSJames Molloy int CycleSched = Schedule.getCycle(OrigMI); 1167790a779fSJames Molloy unsigned ReplaceReg = 0; 1168790a779fSJames Molloy // This is the stage for the scheduled instruction. 1169790a779fSJames Molloy if (StagePhi == StageSched && Phi->isPHI()) { 1170790a779fSJames Molloy int CyclePhi = Schedule.getCycle(Phi); 1171790a779fSJames Molloy if (PrevReg && InProlog) 1172790a779fSJames Molloy ReplaceReg = PrevReg; 1173790a779fSJames Molloy else if (PrevReg && !isLoopCarried(*Phi) && 1174790a779fSJames Molloy (CyclePhi <= CycleSched || OrigMI->isPHI())) 1175790a779fSJames Molloy ReplaceReg = PrevReg; 1176790a779fSJames Molloy else 1177790a779fSJames Molloy ReplaceReg = NewReg; 1178790a779fSJames Molloy } 1179790a779fSJames Molloy // The scheduled instruction occurs before the scheduled Phi, and the 1180790a779fSJames Molloy // Phi is not loop carried. 1181790a779fSJames Molloy if (!InProlog && StagePhi + 1 == StageSched && !isLoopCarried(*Phi)) 1182790a779fSJames Molloy ReplaceReg = NewReg; 1183790a779fSJames Molloy if (StagePhi > StageSched && Phi->isPHI()) 1184790a779fSJames Molloy ReplaceReg = NewReg; 1185790a779fSJames Molloy if (!InProlog && !Phi->isPHI() && StagePhi < StageSched) 1186790a779fSJames Molloy ReplaceReg = NewReg; 1187790a779fSJames Molloy if (ReplaceReg) { 1188790a779fSJames Molloy MRI.constrainRegClass(ReplaceReg, MRI.getRegClass(OldReg)); 1189790a779fSJames Molloy UseOp.setReg(ReplaceReg); 1190790a779fSJames Molloy } 1191790a779fSJames Molloy } 1192790a779fSJames Molloy } 1193790a779fSJames Molloy 1194790a779fSJames Molloy bool ModuloScheduleExpander::isLoopCarried(MachineInstr &Phi) { 1195790a779fSJames Molloy if (!Phi.isPHI()) 1196790a779fSJames Molloy return false; 1197790a779fSJames Molloy unsigned DefCycle = Schedule.getCycle(&Phi); 1198790a779fSJames Molloy int DefStage = Schedule.getStage(&Phi); 1199790a779fSJames Molloy 1200790a779fSJames Molloy unsigned InitVal = 0; 1201790a779fSJames Molloy unsigned LoopVal = 0; 1202790a779fSJames Molloy getPhiRegs(Phi, Phi.getParent(), InitVal, LoopVal); 1203790a779fSJames Molloy MachineInstr *Use = MRI.getVRegDef(LoopVal); 1204790a779fSJames Molloy if (!Use || Use->isPHI()) 1205790a779fSJames Molloy return true; 1206790a779fSJames Molloy unsigned LoopCycle = Schedule.getCycle(Use); 1207790a779fSJames Molloy int LoopStage = Schedule.getStage(Use); 1208790a779fSJames Molloy return (LoopCycle > DefCycle) || (LoopStage <= DefStage); 1209790a779fSJames Molloy } 121093549957SJames Molloy 121193549957SJames Molloy //===----------------------------------------------------------------------===// 1212fef9f590SJames Molloy // PeelingModuloScheduleExpander implementation 1213fef9f590SJames Molloy //===----------------------------------------------------------------------===// 1214fef9f590SJames Molloy // This is a reimplementation of ModuloScheduleExpander that works by creating 1215fef9f590SJames Molloy // a fully correct steady-state kernel and peeling off the prolog and epilogs. 1216fef9f590SJames Molloy //===----------------------------------------------------------------------===// 1217fef9f590SJames Molloy 1218fef9f590SJames Molloy namespace { 1219fef9f590SJames Molloy // Remove any dead phis in MBB. Dead phis either have only one block as input 1220fef9f590SJames Molloy // (in which case they are the identity) or have no uses. 1221fef9f590SJames Molloy void EliminateDeadPhis(MachineBasicBlock *MBB, MachineRegisterInfo &MRI, 1222fef9f590SJames Molloy LiveIntervals *LIS) { 1223fef9f590SJames Molloy bool Changed = true; 1224fef9f590SJames Molloy while (Changed) { 1225fef9f590SJames Molloy Changed = false; 1226fef9f590SJames Molloy for (auto I = MBB->begin(); I != MBB->getFirstNonPHI();) { 1227fef9f590SJames Molloy MachineInstr &MI = *I++; 1228fef9f590SJames Molloy assert(MI.isPHI()); 1229fef9f590SJames Molloy if (MRI.use_empty(MI.getOperand(0).getReg())) { 1230fef9f590SJames Molloy if (LIS) 1231fef9f590SJames Molloy LIS->RemoveMachineInstrFromMaps(MI); 1232fef9f590SJames Molloy MI.eraseFromParent(); 1233fef9f590SJames Molloy Changed = true; 1234fef9f590SJames Molloy } else if (MI.getNumExplicitOperands() == 3) { 1235fef9f590SJames Molloy MRI.constrainRegClass(MI.getOperand(1).getReg(), 1236fef9f590SJames Molloy MRI.getRegClass(MI.getOperand(0).getReg())); 1237fef9f590SJames Molloy MRI.replaceRegWith(MI.getOperand(0).getReg(), 1238fef9f590SJames Molloy MI.getOperand(1).getReg()); 1239fef9f590SJames Molloy if (LIS) 1240fef9f590SJames Molloy LIS->RemoveMachineInstrFromMaps(MI); 1241fef9f590SJames Molloy MI.eraseFromParent(); 1242fef9f590SJames Molloy Changed = true; 1243fef9f590SJames Molloy } 1244fef9f590SJames Molloy } 1245fef9f590SJames Molloy } 1246fef9f590SJames Molloy } 1247fef9f590SJames Molloy 1248fef9f590SJames Molloy /// Rewrites the kernel block in-place to adhere to the given schedule. 1249fef9f590SJames Molloy /// KernelRewriter holds all of the state required to perform the rewriting. 1250fef9f590SJames Molloy class KernelRewriter { 1251fef9f590SJames Molloy ModuloSchedule &S; 1252fef9f590SJames Molloy MachineBasicBlock *BB; 1253fef9f590SJames Molloy MachineBasicBlock *PreheaderBB, *ExitBB; 1254fef9f590SJames Molloy MachineRegisterInfo &MRI; 1255fef9f590SJames Molloy const TargetInstrInfo *TII; 1256fef9f590SJames Molloy LiveIntervals *LIS; 1257fef9f590SJames Molloy 1258fef9f590SJames Molloy // Map from register class to canonical undef register for that class. 1259fef9f590SJames Molloy DenseMap<const TargetRegisterClass *, Register> Undefs; 1260fef9f590SJames Molloy // Map from <LoopReg, InitReg> to phi register for all created phis. Note that 1261fef9f590SJames Molloy // this map is only used when InitReg is non-undef. 1262fef9f590SJames Molloy DenseMap<std::pair<unsigned, unsigned>, Register> Phis; 1263fef9f590SJames Molloy // Map from LoopReg to phi register where the InitReg is undef. 1264fef9f590SJames Molloy DenseMap<Register, Register> UndefPhis; 1265fef9f590SJames Molloy 1266fef9f590SJames Molloy // Reg is used by MI. Return the new register MI should use to adhere to the 1267fef9f590SJames Molloy // schedule. Insert phis as necessary. 1268fef9f590SJames Molloy Register remapUse(Register Reg, MachineInstr &MI); 1269fef9f590SJames Molloy // Insert a phi that carries LoopReg from the loop body and InitReg otherwise. 1270fef9f590SJames Molloy // If InitReg is not given it is chosen arbitrarily. It will either be undef 1271fef9f590SJames Molloy // or will be chosen so as to share another phi. 1272fef9f590SJames Molloy Register phi(Register LoopReg, Optional<Register> InitReg = {}, 1273fef9f590SJames Molloy const TargetRegisterClass *RC = nullptr); 1274fef9f590SJames Molloy // Create an undef register of the given register class. 1275fef9f590SJames Molloy Register undef(const TargetRegisterClass *RC); 1276fef9f590SJames Molloy 1277fef9f590SJames Molloy public: 1278fef9f590SJames Molloy KernelRewriter(MachineLoop &L, ModuloSchedule &S, 1279fef9f590SJames Molloy LiveIntervals *LIS = nullptr); 1280fef9f590SJames Molloy void rewrite(); 1281fef9f590SJames Molloy }; 1282fef9f590SJames Molloy } // namespace 1283fef9f590SJames Molloy 1284fef9f590SJames Molloy KernelRewriter::KernelRewriter(MachineLoop &L, ModuloSchedule &S, 1285fef9f590SJames Molloy LiveIntervals *LIS) 1286fef9f590SJames Molloy : S(S), BB(L.getTopBlock()), PreheaderBB(L.getLoopPreheader()), 1287fef9f590SJames Molloy ExitBB(L.getExitBlock()), MRI(BB->getParent()->getRegInfo()), 1288fef9f590SJames Molloy TII(BB->getParent()->getSubtarget().getInstrInfo()), LIS(LIS) { 1289fef9f590SJames Molloy PreheaderBB = *BB->pred_begin(); 1290fef9f590SJames Molloy if (PreheaderBB == BB) 1291fef9f590SJames Molloy PreheaderBB = *std::next(BB->pred_begin()); 1292fef9f590SJames Molloy } 1293fef9f590SJames Molloy 1294fef9f590SJames Molloy void KernelRewriter::rewrite() { 1295fef9f590SJames Molloy // Rearrange the loop to be in schedule order. Note that the schedule may 1296fef9f590SJames Molloy // contain instructions that are not owned by the loop block (InstrChanges and 1297fef9f590SJames Molloy // friends), so we gracefully handle unowned instructions and delete any 1298fef9f590SJames Molloy // instructions that weren't in the schedule. 1299fef9f590SJames Molloy auto InsertPt = BB->getFirstTerminator(); 1300fef9f590SJames Molloy MachineInstr *FirstMI = nullptr; 1301fef9f590SJames Molloy for (MachineInstr *MI : S.getInstructions()) { 1302fef9f590SJames Molloy if (MI->isPHI()) 1303fef9f590SJames Molloy continue; 1304fef9f590SJames Molloy if (MI->getParent()) 1305fef9f590SJames Molloy MI->removeFromParent(); 1306fef9f590SJames Molloy BB->insert(InsertPt, MI); 1307fef9f590SJames Molloy if (!FirstMI) 1308fef9f590SJames Molloy FirstMI = MI; 1309fef9f590SJames Molloy } 1310fef9f590SJames Molloy 1311fef9f590SJames Molloy // At this point all of the scheduled instructions are between FirstMI 1312fef9f590SJames Molloy // and the end of the block. Kill from the first non-phi to FirstMI. 1313fef9f590SJames Molloy for (auto I = BB->getFirstNonPHI(); I != FirstMI->getIterator();) { 1314fef9f590SJames Molloy if (LIS) 1315fef9f590SJames Molloy LIS->RemoveMachineInstrFromMaps(*I); 1316fef9f590SJames Molloy (I++)->eraseFromParent(); 1317fef9f590SJames Molloy } 1318fef9f590SJames Molloy 1319fef9f590SJames Molloy // Now remap every instruction in the loop. 1320fef9f590SJames Molloy for (MachineInstr &MI : *BB) { 1321fef9f590SJames Molloy if (MI.isPHI()) 1322fef9f590SJames Molloy continue; 1323fef9f590SJames Molloy for (MachineOperand &MO : MI.uses()) { 1324fef9f590SJames Molloy if (!MO.isReg() || MO.getReg().isPhysical() || MO.isImplicit()) 1325fef9f590SJames Molloy continue; 1326fef9f590SJames Molloy Register Reg = remapUse(MO.getReg(), MI); 1327fef9f590SJames Molloy MO.setReg(Reg); 1328fef9f590SJames Molloy } 1329fef9f590SJames Molloy } 1330fef9f590SJames Molloy EliminateDeadPhis(BB, MRI, LIS); 1331fef9f590SJames Molloy 1332fef9f590SJames Molloy // Ensure a phi exists for all instructions that are either referenced by 1333fef9f590SJames Molloy // an illegal phi or by an instruction outside the loop. This allows us to 1334fef9f590SJames Molloy // treat remaps of these values the same as "normal" values that come from 1335fef9f590SJames Molloy // loop-carried phis. 1336fef9f590SJames Molloy for (auto MI = BB->getFirstNonPHI(); MI != BB->end(); ++MI) { 1337fef9f590SJames Molloy if (MI->isPHI()) { 1338fef9f590SJames Molloy Register R = MI->getOperand(0).getReg(); 1339fef9f590SJames Molloy phi(R); 1340fef9f590SJames Molloy continue; 1341fef9f590SJames Molloy } 1342fef9f590SJames Molloy 1343fef9f590SJames Molloy for (MachineOperand &Def : MI->defs()) { 1344fef9f590SJames Molloy for (MachineInstr &MI : MRI.use_instructions(Def.getReg())) { 1345fef9f590SJames Molloy if (MI.getParent() != BB) { 1346fef9f590SJames Molloy phi(Def.getReg()); 1347fef9f590SJames Molloy break; 1348fef9f590SJames Molloy } 1349fef9f590SJames Molloy } 1350fef9f590SJames Molloy } 1351fef9f590SJames Molloy } 1352fef9f590SJames Molloy } 1353fef9f590SJames Molloy 1354fef9f590SJames Molloy Register KernelRewriter::remapUse(Register Reg, MachineInstr &MI) { 1355fef9f590SJames Molloy MachineInstr *Producer = MRI.getUniqueVRegDef(Reg); 1356fef9f590SJames Molloy if (!Producer) 1357fef9f590SJames Molloy return Reg; 1358fef9f590SJames Molloy 1359fef9f590SJames Molloy int ConsumerStage = S.getStage(&MI); 1360fef9f590SJames Molloy if (!Producer->isPHI()) { 1361fef9f590SJames Molloy // Non-phi producers are simple to remap. Insert as many phis as the 1362fef9f590SJames Molloy // difference between the consumer and producer stages. 1363fef9f590SJames Molloy if (Producer->getParent() != BB) 1364fef9f590SJames Molloy // Producer was not inside the loop. Use the register as-is. 1365fef9f590SJames Molloy return Reg; 1366fef9f590SJames Molloy int ProducerStage = S.getStage(Producer); 1367fef9f590SJames Molloy assert(ConsumerStage != -1 && 1368fef9f590SJames Molloy "In-loop consumer should always be scheduled!"); 1369fef9f590SJames Molloy assert(ConsumerStage >= ProducerStage); 1370fef9f590SJames Molloy unsigned StageDiff = ConsumerStage - ProducerStage; 1371fef9f590SJames Molloy 1372fef9f590SJames Molloy for (unsigned I = 0; I < StageDiff; ++I) 1373fef9f590SJames Molloy Reg = phi(Reg); 1374fef9f590SJames Molloy return Reg; 1375fef9f590SJames Molloy } 1376fef9f590SJames Molloy 1377fef9f590SJames Molloy // First, dive through the phi chain to find the defaults for the generated 1378fef9f590SJames Molloy // phis. 1379fef9f590SJames Molloy SmallVector<Optional<Register>, 4> Defaults; 1380fef9f590SJames Molloy Register LoopReg = Reg; 1381fef9f590SJames Molloy auto LoopProducer = Producer; 1382fef9f590SJames Molloy while (LoopProducer->isPHI() && LoopProducer->getParent() == BB) { 1383fef9f590SJames Molloy LoopReg = getLoopPhiReg(*LoopProducer, BB); 1384fef9f590SJames Molloy Defaults.emplace_back(getInitPhiReg(*LoopProducer, BB)); 1385fef9f590SJames Molloy LoopProducer = MRI.getUniqueVRegDef(LoopReg); 1386fef9f590SJames Molloy assert(LoopProducer); 1387fef9f590SJames Molloy } 1388fef9f590SJames Molloy int LoopProducerStage = S.getStage(LoopProducer); 1389fef9f590SJames Molloy 1390fef9f590SJames Molloy Optional<Register> IllegalPhiDefault; 1391fef9f590SJames Molloy 1392fef9f590SJames Molloy if (LoopProducerStage == -1) { 1393fef9f590SJames Molloy // Do nothing. 1394fef9f590SJames Molloy } else if (LoopProducerStage > ConsumerStage) { 1395fef9f590SJames Molloy // This schedule is only representable if ProducerStage == ConsumerStage+1. 1396fef9f590SJames Molloy // In addition, Consumer's cycle must be scheduled after Producer in the 139711f0f7f5SJames Molloy // rescheduled loop. This is enforced by the pipeliner's ASAP and ALAP 139811f0f7f5SJames Molloy // functions. 139911f0f7f5SJames Molloy #ifndef NDEBUG // Silence unused variables in non-asserts mode. 140011f0f7f5SJames Molloy int LoopProducerCycle = S.getCycle(LoopProducer); 140111f0f7f5SJames Molloy int ConsumerCycle = S.getCycle(&MI); 140211f0f7f5SJames Molloy #endif 1403fef9f590SJames Molloy assert(LoopProducerCycle <= ConsumerCycle); 1404fef9f590SJames Molloy assert(LoopProducerStage == ConsumerStage + 1); 1405fef9f590SJames Molloy // Peel off the first phi from Defaults and insert a phi between producer 1406fef9f590SJames Molloy // and consumer. This phi will not be at the front of the block so we 1407fef9f590SJames Molloy // consider it illegal. It will only exist during the rewrite process; it 1408fef9f590SJames Molloy // needs to exist while we peel off prologs because these could take the 1409fef9f590SJames Molloy // default value. After that we can replace all uses with the loop producer 1410fef9f590SJames Molloy // value. 1411fef9f590SJames Molloy IllegalPhiDefault = Defaults.front(); 1412fef9f590SJames Molloy Defaults.erase(Defaults.begin()); 1413fef9f590SJames Molloy } else { 1414fef9f590SJames Molloy assert(ConsumerStage >= LoopProducerStage); 1415fef9f590SJames Molloy int StageDiff = ConsumerStage - LoopProducerStage; 1416fef9f590SJames Molloy if (StageDiff > 0) { 1417fef9f590SJames Molloy LLVM_DEBUG(dbgs() << " -- padding defaults array from " << Defaults.size() 1418fef9f590SJames Molloy << " to " << (Defaults.size() + StageDiff) << "\n"); 1419fef9f590SJames Molloy // If we need more phis than we have defaults for, pad out with undefs for 1420fef9f590SJames Molloy // the earliest phis, which are at the end of the defaults chain (the 1421fef9f590SJames Molloy // chain is in reverse order). 1422fef9f590SJames Molloy Defaults.resize(Defaults.size() + StageDiff, Defaults.empty() 1423fef9f590SJames Molloy ? Optional<Register>() 1424fef9f590SJames Molloy : Defaults.back()); 1425fef9f590SJames Molloy } 1426fef9f590SJames Molloy } 1427fef9f590SJames Molloy 1428fef9f590SJames Molloy // Now we know the number of stages to jump back, insert the phi chain. 1429fef9f590SJames Molloy auto DefaultI = Defaults.rbegin(); 1430fef9f590SJames Molloy while (DefaultI != Defaults.rend()) 1431fef9f590SJames Molloy LoopReg = phi(LoopReg, *DefaultI++, MRI.getRegClass(Reg)); 1432fef9f590SJames Molloy 1433fef9f590SJames Molloy if (IllegalPhiDefault.hasValue()) { 1434fef9f590SJames Molloy // The consumer optionally consumes LoopProducer in the same iteration 1435fef9f590SJames Molloy // (because the producer is scheduled at an earlier cycle than the consumer) 1436fef9f590SJames Molloy // or the initial value. To facilitate this we create an illegal block here 1437fef9f590SJames Molloy // by embedding a phi in the middle of the block. We will fix this up 1438fef9f590SJames Molloy // immediately prior to pruning. 1439fef9f590SJames Molloy auto RC = MRI.getRegClass(Reg); 1440fef9f590SJames Molloy Register R = MRI.createVirtualRegister(RC); 1441fef9f590SJames Molloy BuildMI(*BB, MI, DebugLoc(), TII->get(TargetOpcode::PHI), R) 1442fef9f590SJames Molloy .addReg(IllegalPhiDefault.getValue()) 1443fef9f590SJames Molloy .addMBB(PreheaderBB) // Block choice is arbitrary and has no effect. 1444fef9f590SJames Molloy .addReg(LoopReg) 1445fef9f590SJames Molloy .addMBB(BB); // Block choice is arbitrary and has no effect. 1446fef9f590SJames Molloy return R; 1447fef9f590SJames Molloy } 1448fef9f590SJames Molloy 1449fef9f590SJames Molloy return LoopReg; 1450fef9f590SJames Molloy } 1451fef9f590SJames Molloy 1452fef9f590SJames Molloy Register KernelRewriter::phi(Register LoopReg, Optional<Register> InitReg, 1453fef9f590SJames Molloy const TargetRegisterClass *RC) { 1454fef9f590SJames Molloy // If the init register is not undef, try and find an existing phi. 1455fef9f590SJames Molloy if (InitReg.hasValue()) { 1456fef9f590SJames Molloy auto I = Phis.find({LoopReg, InitReg.getValue()}); 1457fef9f590SJames Molloy if (I != Phis.end()) 1458fef9f590SJames Molloy return I->second; 1459fef9f590SJames Molloy } else { 1460fef9f590SJames Molloy for (auto &KV : Phis) { 1461fef9f590SJames Molloy if (KV.first.first == LoopReg) 1462fef9f590SJames Molloy return KV.second; 1463fef9f590SJames Molloy } 1464fef9f590SJames Molloy } 1465fef9f590SJames Molloy 1466fef9f590SJames Molloy // InitReg is either undef or no existing phi takes InitReg as input. Try and 1467fef9f590SJames Molloy // find a phi that takes undef as input. 1468fef9f590SJames Molloy auto I = UndefPhis.find(LoopReg); 1469fef9f590SJames Molloy if (I != UndefPhis.end()) { 1470fef9f590SJames Molloy Register R = I->second; 1471fef9f590SJames Molloy if (!InitReg.hasValue()) 1472fef9f590SJames Molloy // Found a phi taking undef as input, and this input is undef so return 1473fef9f590SJames Molloy // without any more changes. 1474fef9f590SJames Molloy return R; 1475fef9f590SJames Molloy // Found a phi taking undef as input, so rewrite it to take InitReg. 1476fef9f590SJames Molloy MachineInstr *MI = MRI.getVRegDef(R); 1477fef9f590SJames Molloy MI->getOperand(1).setReg(InitReg.getValue()); 1478fef9f590SJames Molloy Phis.insert({{LoopReg, InitReg.getValue()}, R}); 1479fef9f590SJames Molloy MRI.constrainRegClass(R, MRI.getRegClass(InitReg.getValue())); 1480fef9f590SJames Molloy UndefPhis.erase(I); 1481fef9f590SJames Molloy return R; 1482fef9f590SJames Molloy } 1483fef9f590SJames Molloy 1484fef9f590SJames Molloy // Failed to find any existing phi to reuse, so create a new one. 1485fef9f590SJames Molloy if (!RC) 1486fef9f590SJames Molloy RC = MRI.getRegClass(LoopReg); 1487fef9f590SJames Molloy Register R = MRI.createVirtualRegister(RC); 1488fef9f590SJames Molloy if (InitReg.hasValue()) 1489fef9f590SJames Molloy MRI.constrainRegClass(R, MRI.getRegClass(*InitReg)); 1490fef9f590SJames Molloy BuildMI(*BB, BB->getFirstNonPHI(), DebugLoc(), TII->get(TargetOpcode::PHI), R) 1491fef9f590SJames Molloy .addReg(InitReg.hasValue() ? *InitReg : undef(RC)) 1492fef9f590SJames Molloy .addMBB(PreheaderBB) 1493fef9f590SJames Molloy .addReg(LoopReg) 1494fef9f590SJames Molloy .addMBB(BB); 1495fef9f590SJames Molloy if (!InitReg.hasValue()) 1496fef9f590SJames Molloy UndefPhis[LoopReg] = R; 1497fef9f590SJames Molloy else 1498fef9f590SJames Molloy Phis[{LoopReg, *InitReg}] = R; 1499fef9f590SJames Molloy return R; 1500fef9f590SJames Molloy } 1501fef9f590SJames Molloy 1502fef9f590SJames Molloy Register KernelRewriter::undef(const TargetRegisterClass *RC) { 1503fef9f590SJames Molloy Register &R = Undefs[RC]; 1504fef9f590SJames Molloy if (R == 0) { 1505fef9f590SJames Molloy // Create an IMPLICIT_DEF that defines this register if we need it. 1506fef9f590SJames Molloy // All uses of this should be removed by the time we have finished unrolling 1507fef9f590SJames Molloy // prologs and epilogs. 1508fef9f590SJames Molloy R = MRI.createVirtualRegister(RC); 1509fef9f590SJames Molloy auto *InsertBB = &PreheaderBB->getParent()->front(); 1510fef9f590SJames Molloy BuildMI(*InsertBB, InsertBB->getFirstTerminator(), DebugLoc(), 1511fef9f590SJames Molloy TII->get(TargetOpcode::IMPLICIT_DEF), R); 1512fef9f590SJames Molloy } 1513fef9f590SJames Molloy return R; 1514fef9f590SJames Molloy } 1515fef9f590SJames Molloy 1516fef9f590SJames Molloy namespace { 1517fef9f590SJames Molloy /// Describes an operand in the kernel of a pipelined loop. Characteristics of 1518fef9f590SJames Molloy /// the operand are discovered, such as how many in-loop PHIs it has to jump 1519fef9f590SJames Molloy /// through and defaults for these phis. 1520fef9f590SJames Molloy class KernelOperandInfo { 1521fef9f590SJames Molloy MachineBasicBlock *BB; 1522fef9f590SJames Molloy MachineRegisterInfo &MRI; 1523fef9f590SJames Molloy SmallVector<Register, 4> PhiDefaults; 1524fef9f590SJames Molloy MachineOperand *Source; 1525fef9f590SJames Molloy MachineOperand *Target; 1526fef9f590SJames Molloy 1527fef9f590SJames Molloy public: 1528fef9f590SJames Molloy KernelOperandInfo(MachineOperand *MO, MachineRegisterInfo &MRI, 1529fef9f590SJames Molloy const SmallPtrSetImpl<MachineInstr *> &IllegalPhis) 1530fef9f590SJames Molloy : MRI(MRI) { 1531fef9f590SJames Molloy Source = MO; 1532fef9f590SJames Molloy BB = MO->getParent()->getParent(); 1533fef9f590SJames Molloy while (isRegInLoop(MO)) { 1534fef9f590SJames Molloy MachineInstr *MI = MRI.getVRegDef(MO->getReg()); 1535fef9f590SJames Molloy if (MI->isFullCopy()) { 1536fef9f590SJames Molloy MO = &MI->getOperand(1); 1537fef9f590SJames Molloy continue; 1538fef9f590SJames Molloy } 1539fef9f590SJames Molloy if (!MI->isPHI()) 1540fef9f590SJames Molloy break; 1541fef9f590SJames Molloy // If this is an illegal phi, don't count it in distance. 1542fef9f590SJames Molloy if (IllegalPhis.count(MI)) { 1543fef9f590SJames Molloy MO = &MI->getOperand(3); 1544fef9f590SJames Molloy continue; 1545fef9f590SJames Molloy } 1546fef9f590SJames Molloy 1547fef9f590SJames Molloy Register Default = getInitPhiReg(*MI, BB); 1548fef9f590SJames Molloy MO = MI->getOperand(2).getMBB() == BB ? &MI->getOperand(1) 1549fef9f590SJames Molloy : &MI->getOperand(3); 1550fef9f590SJames Molloy PhiDefaults.push_back(Default); 1551fef9f590SJames Molloy } 1552fef9f590SJames Molloy Target = MO; 1553fef9f590SJames Molloy } 1554fef9f590SJames Molloy 1555fef9f590SJames Molloy bool operator==(const KernelOperandInfo &Other) const { 1556fef9f590SJames Molloy return PhiDefaults.size() == Other.PhiDefaults.size(); 1557fef9f590SJames Molloy } 1558fef9f590SJames Molloy 1559fef9f590SJames Molloy void print(raw_ostream &OS) const { 1560fef9f590SJames Molloy OS << "use of " << *Source << ": distance(" << PhiDefaults.size() << ") in " 1561fef9f590SJames Molloy << *Source->getParent(); 1562fef9f590SJames Molloy } 1563fef9f590SJames Molloy 1564fef9f590SJames Molloy private: 1565fef9f590SJames Molloy bool isRegInLoop(MachineOperand *MO) { 1566fef9f590SJames Molloy return MO->isReg() && MO->getReg().isVirtual() && 1567fef9f590SJames Molloy MRI.getVRegDef(MO->getReg())->getParent() == BB; 1568fef9f590SJames Molloy } 1569fef9f590SJames Molloy }; 1570fef9f590SJames Molloy } // namespace 1571fef9f590SJames Molloy 1572fef9f590SJames Molloy void PeelingModuloScheduleExpander::validateAgainstModuloScheduleExpander() { 1573fef9f590SJames Molloy BB = Schedule.getLoop()->getTopBlock(); 1574fef9f590SJames Molloy Preheader = Schedule.getLoop()->getLoopPreheader(); 1575fef9f590SJames Molloy 1576fef9f590SJames Molloy // Dump the schedule before we invalidate and remap all its instructions. 1577fef9f590SJames Molloy // Stash it in a string so we can print it if we found an error. 1578fef9f590SJames Molloy std::string ScheduleDump; 1579fef9f590SJames Molloy raw_string_ostream OS(ScheduleDump); 1580fef9f590SJames Molloy Schedule.print(OS); 1581fef9f590SJames Molloy OS.flush(); 1582fef9f590SJames Molloy 1583fef9f590SJames Molloy // First, run the normal ModuleScheduleExpander. We don't support any 1584fef9f590SJames Molloy // InstrChanges. 1585fef9f590SJames Molloy assert(LIS && "Requires LiveIntervals!"); 1586fef9f590SJames Molloy ModuloScheduleExpander MSE(MF, Schedule, *LIS, 1587fef9f590SJames Molloy ModuloScheduleExpander::InstrChangesTy()); 1588fef9f590SJames Molloy MSE.expand(); 1589fef9f590SJames Molloy MachineBasicBlock *ExpandedKernel = MSE.getRewrittenKernel(); 1590fef9f590SJames Molloy if (!ExpandedKernel) { 1591fef9f590SJames Molloy // The expander optimized away the kernel. We can't do any useful checking. 1592fef9f590SJames Molloy MSE.cleanup(); 1593fef9f590SJames Molloy return; 1594fef9f590SJames Molloy } 1595fef9f590SJames Molloy // Before running the KernelRewriter, re-add BB into the CFG. 1596fef9f590SJames Molloy Preheader->addSuccessor(BB); 1597fef9f590SJames Molloy 1598fef9f590SJames Molloy // Now run the new expansion algorithm. 1599fef9f590SJames Molloy KernelRewriter KR(*Schedule.getLoop(), Schedule); 1600fef9f590SJames Molloy KR.rewrite(); 1601fef9f590SJames Molloy 1602fef9f590SJames Molloy // Collect all illegal phis that the new algorithm created. We'll give these 1603fef9f590SJames Molloy // to KernelOperandInfo. 1604fef9f590SJames Molloy SmallPtrSet<MachineInstr *, 4> IllegalPhis; 1605fef9f590SJames Molloy for (auto NI = BB->getFirstNonPHI(); NI != BB->end(); ++NI) { 1606fef9f590SJames Molloy if (NI->isPHI()) 1607fef9f590SJames Molloy IllegalPhis.insert(&*NI); 1608fef9f590SJames Molloy } 1609fef9f590SJames Molloy 1610fef9f590SJames Molloy // Co-iterate across both kernels. We expect them to be identical apart from 1611fef9f590SJames Molloy // phis and full COPYs (we look through both). 1612fef9f590SJames Molloy SmallVector<std::pair<KernelOperandInfo, KernelOperandInfo>, 8> KOIs; 1613fef9f590SJames Molloy auto OI = ExpandedKernel->begin(); 1614fef9f590SJames Molloy auto NI = BB->begin(); 1615fef9f590SJames Molloy for (; !OI->isTerminator() && !NI->isTerminator(); ++OI, ++NI) { 1616fef9f590SJames Molloy while (OI->isPHI() || OI->isFullCopy()) 1617fef9f590SJames Molloy ++OI; 1618fef9f590SJames Molloy while (NI->isPHI() || NI->isFullCopy()) 1619fef9f590SJames Molloy ++NI; 1620fef9f590SJames Molloy assert(OI->getOpcode() == NI->getOpcode() && "Opcodes don't match?!"); 1621fef9f590SJames Molloy // Analyze every operand separately. 1622fef9f590SJames Molloy for (auto OOpI = OI->operands_begin(), NOpI = NI->operands_begin(); 1623fef9f590SJames Molloy OOpI != OI->operands_end(); ++OOpI, ++NOpI) 1624fef9f590SJames Molloy KOIs.emplace_back(KernelOperandInfo(&*OOpI, MRI, IllegalPhis), 1625fef9f590SJames Molloy KernelOperandInfo(&*NOpI, MRI, IllegalPhis)); 1626fef9f590SJames Molloy } 1627fef9f590SJames Molloy 1628fef9f590SJames Molloy bool Failed = false; 1629fef9f590SJames Molloy for (auto &OldAndNew : KOIs) { 1630fef9f590SJames Molloy if (OldAndNew.first == OldAndNew.second) 1631fef9f590SJames Molloy continue; 1632fef9f590SJames Molloy Failed = true; 1633fef9f590SJames Molloy errs() << "Modulo kernel validation error: [\n"; 1634fef9f590SJames Molloy errs() << " [golden] "; 1635fef9f590SJames Molloy OldAndNew.first.print(errs()); 1636fef9f590SJames Molloy errs() << " "; 1637fef9f590SJames Molloy OldAndNew.second.print(errs()); 1638fef9f590SJames Molloy errs() << "]\n"; 1639fef9f590SJames Molloy } 1640fef9f590SJames Molloy 1641fef9f590SJames Molloy if (Failed) { 1642fef9f590SJames Molloy errs() << "Golden reference kernel:\n"; 164311f0f7f5SJames Molloy ExpandedKernel->print(errs()); 1644fef9f590SJames Molloy errs() << "New kernel:\n"; 164511f0f7f5SJames Molloy BB->print(errs()); 1646fef9f590SJames Molloy errs() << ScheduleDump; 1647fef9f590SJames Molloy report_fatal_error( 1648fef9f590SJames Molloy "Modulo kernel validation (-pipeliner-experimental-cg) failed"); 1649fef9f590SJames Molloy } 1650fef9f590SJames Molloy 1651fef9f590SJames Molloy // Cleanup by removing BB from the CFG again as the original 1652fef9f590SJames Molloy // ModuloScheduleExpander intended. 1653fef9f590SJames Molloy Preheader->removeSuccessor(BB); 1654fef9f590SJames Molloy MSE.cleanup(); 1655fef9f590SJames Molloy } 1656fef9f590SJames Molloy 1657fef9f590SJames Molloy //===----------------------------------------------------------------------===// 165893549957SJames Molloy // ModuloScheduleTestPass implementation 165993549957SJames Molloy //===----------------------------------------------------------------------===// 166093549957SJames Molloy // This pass constructs a ModuloSchedule from its module and runs 166193549957SJames Molloy // ModuloScheduleExpander. 166293549957SJames Molloy // 166393549957SJames Molloy // The module is expected to contain a single-block analyzable loop. 166493549957SJames Molloy // The total order of instructions is taken from the loop as-is. 166593549957SJames Molloy // Instructions are expected to be annotated with a PostInstrSymbol. 166693549957SJames Molloy // This PostInstrSymbol must have the following format: 166793549957SJames Molloy // "Stage=%d Cycle=%d". 166893549957SJames Molloy //===----------------------------------------------------------------------===// 166993549957SJames Molloy 1670*df4b9a3fSBenjamin Kramer namespace { 167193549957SJames Molloy class ModuloScheduleTest : public MachineFunctionPass { 167293549957SJames Molloy public: 167393549957SJames Molloy static char ID; 167493549957SJames Molloy 167593549957SJames Molloy ModuloScheduleTest() : MachineFunctionPass(ID) { 167693549957SJames Molloy initializeModuloScheduleTestPass(*PassRegistry::getPassRegistry()); 167793549957SJames Molloy } 167893549957SJames Molloy 167993549957SJames Molloy bool runOnMachineFunction(MachineFunction &MF) override; 168093549957SJames Molloy void runOnLoop(MachineFunction &MF, MachineLoop &L); 168193549957SJames Molloy 168293549957SJames Molloy void getAnalysisUsage(AnalysisUsage &AU) const override { 168393549957SJames Molloy AU.addRequired<MachineLoopInfo>(); 168493549957SJames Molloy AU.addRequired<LiveIntervals>(); 168593549957SJames Molloy MachineFunctionPass::getAnalysisUsage(AU); 168693549957SJames Molloy } 168793549957SJames Molloy }; 1688*df4b9a3fSBenjamin Kramer } // namespace 168993549957SJames Molloy 169093549957SJames Molloy char ModuloScheduleTest::ID = 0; 169193549957SJames Molloy 169293549957SJames Molloy INITIALIZE_PASS_BEGIN(ModuloScheduleTest, "modulo-schedule-test", 169393549957SJames Molloy "Modulo Schedule test pass", false, false) 169493549957SJames Molloy INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 169593549957SJames Molloy INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 169693549957SJames Molloy INITIALIZE_PASS_END(ModuloScheduleTest, "modulo-schedule-test", 169793549957SJames Molloy "Modulo Schedule test pass", false, false) 169893549957SJames Molloy 169993549957SJames Molloy bool ModuloScheduleTest::runOnMachineFunction(MachineFunction &MF) { 170093549957SJames Molloy MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 170193549957SJames Molloy for (auto *L : MLI) { 170293549957SJames Molloy if (L->getTopBlock() != L->getBottomBlock()) 170393549957SJames Molloy continue; 170493549957SJames Molloy runOnLoop(MF, *L); 170593549957SJames Molloy return false; 170693549957SJames Molloy } 170793549957SJames Molloy return false; 170893549957SJames Molloy } 170993549957SJames Molloy 171093549957SJames Molloy static void parseSymbolString(StringRef S, int &Cycle, int &Stage) { 171193549957SJames Molloy std::pair<StringRef, StringRef> StageAndCycle = getToken(S, "_"); 171293549957SJames Molloy std::pair<StringRef, StringRef> StageTokenAndValue = 171393549957SJames Molloy getToken(StageAndCycle.first, "-"); 171493549957SJames Molloy std::pair<StringRef, StringRef> CycleTokenAndValue = 171593549957SJames Molloy getToken(StageAndCycle.second, "-"); 171693549957SJames Molloy if (StageTokenAndValue.first != "Stage" || 171793549957SJames Molloy CycleTokenAndValue.first != "_Cycle") { 171893549957SJames Molloy llvm_unreachable( 171993549957SJames Molloy "Bad post-instr symbol syntax: see comment in ModuloScheduleTest"); 172093549957SJames Molloy return; 172193549957SJames Molloy } 172293549957SJames Molloy 172393549957SJames Molloy StageTokenAndValue.second.drop_front().getAsInteger(10, Stage); 172493549957SJames Molloy CycleTokenAndValue.second.drop_front().getAsInteger(10, Cycle); 172593549957SJames Molloy 172693549957SJames Molloy dbgs() << " Stage=" << Stage << ", Cycle=" << Cycle << "\n"; 172793549957SJames Molloy } 172893549957SJames Molloy 172993549957SJames Molloy void ModuloScheduleTest::runOnLoop(MachineFunction &MF, MachineLoop &L) { 173093549957SJames Molloy LiveIntervals &LIS = getAnalysis<LiveIntervals>(); 173193549957SJames Molloy MachineBasicBlock *BB = L.getTopBlock(); 173293549957SJames Molloy dbgs() << "--- ModuloScheduleTest running on BB#" << BB->getNumber() << "\n"; 173393549957SJames Molloy 173493549957SJames Molloy DenseMap<MachineInstr *, int> Cycle, Stage; 173593549957SJames Molloy std::vector<MachineInstr *> Instrs; 173693549957SJames Molloy for (MachineInstr &MI : *BB) { 173793549957SJames Molloy if (MI.isTerminator()) 173893549957SJames Molloy continue; 173993549957SJames Molloy Instrs.push_back(&MI); 174093549957SJames Molloy if (MCSymbol *Sym = MI.getPostInstrSymbol()) { 174193549957SJames Molloy dbgs() << "Parsing post-instr symbol for " << MI; 174293549957SJames Molloy parseSymbolString(Sym->getName(), Cycle[&MI], Stage[&MI]); 174393549957SJames Molloy } 174493549957SJames Molloy } 174593549957SJames Molloy 1746fef9f590SJames Molloy ModuloSchedule MS(MF, &L, std::move(Instrs), std::move(Cycle), 1747fef9f590SJames Molloy std::move(Stage)); 174893549957SJames Molloy ModuloScheduleExpander MSE( 174993549957SJames Molloy MF, MS, LIS, /*InstrChanges=*/ModuloScheduleExpander::InstrChangesTy()); 175093549957SJames Molloy MSE.expand(); 1751fef9f590SJames Molloy MSE.cleanup(); 175293549957SJames Molloy } 175393549957SJames Molloy 175493549957SJames Molloy //===----------------------------------------------------------------------===// 175593549957SJames Molloy // ModuloScheduleTestAnnotater implementation 175693549957SJames Molloy //===----------------------------------------------------------------------===// 175793549957SJames Molloy 175893549957SJames Molloy void ModuloScheduleTestAnnotater::annotate() { 175993549957SJames Molloy for (MachineInstr *MI : S.getInstructions()) { 176093549957SJames Molloy SmallVector<char, 16> SV; 176193549957SJames Molloy raw_svector_ostream OS(SV); 176293549957SJames Molloy OS << "Stage-" << S.getStage(MI) << "_Cycle-" << S.getCycle(MI); 176393549957SJames Molloy MCSymbol *Sym = MF.getContext().getOrCreateSymbol(OS.str()); 176493549957SJames Molloy MI->setPostInstrSymbol(MF, Sym); 176593549957SJames Molloy } 176693549957SJames Molloy } 1767