Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Scalar/LoopRotation.cpp
35266 views
//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements Loop Rotation Pass.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Transforms/Scalar/LoopRotation.h"13#include "llvm/Analysis/AssumptionCache.h"14#include "llvm/Analysis/InstructionSimplify.h"15#include "llvm/Analysis/LazyBlockFrequencyInfo.h"16#include "llvm/Analysis/LoopInfo.h"17#include "llvm/Analysis/LoopPass.h"18#include "llvm/Analysis/MemorySSA.h"19#include "llvm/Analysis/MemorySSAUpdater.h"20#include "llvm/Analysis/ScalarEvolution.h"21#include "llvm/Analysis/TargetTransformInfo.h"22#include "llvm/InitializePasses.h"23#include "llvm/Support/CommandLine.h"24#include "llvm/Transforms/Scalar.h"25#include "llvm/Transforms/Utils/LoopRotationUtils.h"26#include "llvm/Transforms/Utils/LoopUtils.h"27#include <optional>28using namespace llvm;2930#define DEBUG_TYPE "loop-rotate"3132static cl::opt<unsigned> DefaultRotationThreshold(33"rotation-max-header-size", cl::init(16), cl::Hidden,34cl::desc("The default maximum header size for automatic loop rotation"));3536static cl::opt<bool> PrepareForLTOOption(37"rotation-prepare-for-lto", cl::init(false), cl::Hidden,38cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "39"should be used for testing only."));4041LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO)42: EnableHeaderDuplication(EnableHeaderDuplication),43PrepareForLTO(PrepareForLTO) {}4445void LoopRotatePass::printPipeline(46raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {47static_cast<PassInfoMixin<LoopRotatePass> *>(this)->printPipeline(48OS, MapClassName2PassName);49OS << "<";50if (!EnableHeaderDuplication)51OS << "no-";52OS << "header-duplication;";5354if (!PrepareForLTO)55OS << "no-";56OS << "prepare-for-lto";57OS << ">";58}5960PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,61LoopStandardAnalysisResults &AR,62LPMUpdater &) {63// Vectorization requires loop-rotation. Use default threshold for loops the64// user explicitly marked for vectorization, even when header duplication is65// disabled.66int Threshold =67(EnableHeaderDuplication && !L.getHeader()->getParent()->hasMinSize()) ||68hasVectorizeTransformation(&L) == TM_ForcedByUser69? DefaultRotationThreshold70: 0;71const DataLayout &DL = L.getHeader()->getDataLayout();72const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);7374std::optional<MemorySSAUpdater> MSSAU;75if (AR.MSSA)76MSSAU = MemorySSAUpdater(AR.MSSA);77bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,78MSSAU ? &*MSSAU : nullptr, SQ, false, Threshold,79false, PrepareForLTO || PrepareForLTOOption);8081if (!Changed)82return PreservedAnalyses::all();8384if (AR.MSSA && VerifyMemorySSA)85AR.MSSA->verifyMemorySSA();8687auto PA = getLoopPassPreservedAnalyses();88if (AR.MSSA)89PA.preserve<MemorySSAAnalysis>();90return PA;91}929394