10eae32dcSDimitry Andric //===- CycleAnalysis.cpp - Compute CycleInfo for LLVM IR ------------------===// 20eae32dcSDimitry Andric // 30eae32dcSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40eae32dcSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50eae32dcSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60eae32dcSDimitry Andric // 70eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 80eae32dcSDimitry Andric 90eae32dcSDimitry Andric #include "llvm/Analysis/CycleAnalysis.h" 100eae32dcSDimitry Andric #include "llvm/ADT/GenericCycleImpl.h" 11*81ad6265SDimitry Andric #include "llvm/IR/CFG.h" // for successors found by ADL in GenericCycleImpl.h 120eae32dcSDimitry Andric #include "llvm/InitializePasses.h" 130eae32dcSDimitry Andric 140eae32dcSDimitry Andric using namespace llvm; 150eae32dcSDimitry Andric 16*81ad6265SDimitry Andric namespace llvm { 17*81ad6265SDimitry Andric class Module; 18*81ad6265SDimitry Andric } 19*81ad6265SDimitry Andric 200eae32dcSDimitry Andric template class llvm::GenericCycleInfo<SSAContext>; 210eae32dcSDimitry Andric template class llvm::GenericCycle<SSAContext>; 220eae32dcSDimitry Andric 230eae32dcSDimitry Andric CycleInfo CycleAnalysis::run(Function &F, FunctionAnalysisManager &) { 240eae32dcSDimitry Andric CycleInfo CI; 250eae32dcSDimitry Andric CI.compute(F); 260eae32dcSDimitry Andric return CI; 270eae32dcSDimitry Andric } 280eae32dcSDimitry Andric 290eae32dcSDimitry Andric AnalysisKey CycleAnalysis::Key; 300eae32dcSDimitry Andric 310eae32dcSDimitry Andric CycleInfoPrinterPass::CycleInfoPrinterPass(raw_ostream &OS) : OS(OS) {} 320eae32dcSDimitry Andric 330eae32dcSDimitry Andric PreservedAnalyses CycleInfoPrinterPass::run(Function &F, 340eae32dcSDimitry Andric FunctionAnalysisManager &AM) { 350eae32dcSDimitry Andric OS << "CycleInfo for function: " << F.getName() << "\n"; 360eae32dcSDimitry Andric AM.getResult<CycleAnalysis>(F).print(OS); 370eae32dcSDimitry Andric 380eae32dcSDimitry Andric return PreservedAnalyses::all(); 390eae32dcSDimitry Andric } 400eae32dcSDimitry Andric 410eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 420eae32dcSDimitry Andric // CycleInfoWrapperPass Implementation 430eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 440eae32dcSDimitry Andric // 450eae32dcSDimitry Andric // The implementation details of the wrapper pass that holds a CycleInfo 460eae32dcSDimitry Andric // suitable for use with the legacy pass manager. 470eae32dcSDimitry Andric // 480eae32dcSDimitry Andric //===----------------------------------------------------------------------===// 490eae32dcSDimitry Andric 500eae32dcSDimitry Andric char CycleInfoWrapperPass::ID = 0; 510eae32dcSDimitry Andric 520eae32dcSDimitry Andric CycleInfoWrapperPass::CycleInfoWrapperPass() : FunctionPass(ID) { 530eae32dcSDimitry Andric initializeCycleInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 540eae32dcSDimitry Andric } 550eae32dcSDimitry Andric 560eae32dcSDimitry Andric INITIALIZE_PASS_BEGIN(CycleInfoWrapperPass, "cycles", "Cycle Info Analysis", 570eae32dcSDimitry Andric true, true) 580eae32dcSDimitry Andric INITIALIZE_PASS_END(CycleInfoWrapperPass, "cycles", "Cycle Info Analysis", true, 590eae32dcSDimitry Andric true) 600eae32dcSDimitry Andric 610eae32dcSDimitry Andric void CycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 620eae32dcSDimitry Andric AU.setPreservesAll(); 630eae32dcSDimitry Andric } 640eae32dcSDimitry Andric 650eae32dcSDimitry Andric bool CycleInfoWrapperPass::runOnFunction(Function &Func) { 660eae32dcSDimitry Andric CI.clear(); 670eae32dcSDimitry Andric 680eae32dcSDimitry Andric F = &Func; 690eae32dcSDimitry Andric CI.compute(Func); 700eae32dcSDimitry Andric return false; 710eae32dcSDimitry Andric } 720eae32dcSDimitry Andric 730eae32dcSDimitry Andric void CycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const { 740eae32dcSDimitry Andric OS << "CycleInfo for function: " << F->getName() << "\n"; 750eae32dcSDimitry Andric CI.print(OS); 760eae32dcSDimitry Andric } 770eae32dcSDimitry Andric 780eae32dcSDimitry Andric void CycleInfoWrapperPass::releaseMemory() { 790eae32dcSDimitry Andric CI.clear(); 800eae32dcSDimitry Andric F = nullptr; 810eae32dcSDimitry Andric } 82