92 lines · cpp
1//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements Loop Rotation Pass.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/Transforms/Scalar/LoopRotation.h"14#include "llvm/Analysis/AssumptionCache.h"15#include "llvm/Analysis/InstructionSimplify.h"16#include "llvm/Analysis/LazyBlockFrequencyInfo.h"17#include "llvm/Analysis/LoopInfo.h"18#include "llvm/Analysis/LoopPass.h"19#include "llvm/Analysis/MemorySSA.h"20#include "llvm/Analysis/MemorySSAUpdater.h"21#include "llvm/Analysis/ScalarEvolution.h"22#include "llvm/Analysis/TargetTransformInfo.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;29 30#define DEBUG_TYPE "loop-rotate"31 32static cl::opt<unsigned> DefaultRotationThreshold(33 "rotation-max-header-size", cl::init(16), cl::Hidden,34 cl::desc("The default maximum header size for automatic loop rotation"));35 36static cl::opt<bool> PrepareForLTOOption(37 "rotation-prepare-for-lto", cl::init(false), cl::Hidden,38 cl::desc("Run loop-rotation in the prepare-for-lto stage. This option "39 "should be used for testing only."));40 41LoopRotatePass::LoopRotatePass(bool EnableHeaderDuplication, bool PrepareForLTO)42 : EnableHeaderDuplication(EnableHeaderDuplication),43 PrepareForLTO(PrepareForLTO) {}44 45void LoopRotatePass::printPipeline(46 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {47 static_cast<PassInfoMixin<LoopRotatePass> *>(this)->printPipeline(48 OS, MapClassName2PassName);49 OS << "<";50 if (!EnableHeaderDuplication)51 OS << "no-";52 OS << "header-duplication;";53 54 if (!PrepareForLTO)55 OS << "no-";56 OS << "prepare-for-lto";57 OS << ">";58}59 60PreservedAnalyses LoopRotatePass::run(Loop &L, LoopAnalysisManager &AM,61 LoopStandardAnalysisResults &AR,62 LPMUpdater &) {63 // Vectorization requires loop-rotation. Use default threshold for loops the64 // user explicitly marked for vectorization, even when header duplication is65 // disabled.66 int Threshold =67 (EnableHeaderDuplication && !L.getHeader()->getParent()->hasMinSize()) ||68 hasVectorizeTransformation(&L) == TM_ForcedByUser69 ? DefaultRotationThreshold70 : 0;71 const DataLayout &DL = L.getHeader()->getDataLayout();72 const SimplifyQuery SQ = getBestSimplifyQuery(AR, DL);73 74 std::optional<MemorySSAUpdater> MSSAU;75 if (AR.MSSA)76 MSSAU = MemorySSAUpdater(AR.MSSA);77 bool Changed = LoopRotation(&L, &AR.LI, &AR.TTI, &AR.AC, &AR.DT, &AR.SE,78 MSSAU ? &*MSSAU : nullptr, SQ, false, Threshold,79 false, PrepareForLTO || PrepareForLTOOption);80 81 if (!Changed)82 return PreservedAnalyses::all();83 84 if (AR.MSSA && VerifyMemorySSA)85 AR.MSSA->verifyMemorySSA();86 87 auto PA = getLoopPassPreservedAnalyses();88 if (AR.MSSA)89 PA.preserve<MemorySSAAnalysis>();90 return PA;91}92