Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/CanonicalizeFreezeInLoops.cpp
35271 views
//==- CanonicalizeFreezeInLoops - Canonicalize freezes in a loop-*- C++ -*-===//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 pass canonicalizes freeze instructions in a loop by pushing them out to9// the preheader.10//11// loop:12// i = phi init, i.next13// i.next = add nsw i, 114// i.next.fr = freeze i.next // push this out of this loop15// use(i.next.fr)16// br i1 (i.next <= N), loop, exit17// =>18// init.fr = freeze init19// loop:20// i = phi init.fr, i.next21// i.next = add i, 1 // nsw is dropped here22// use(i.next)23// br i1 (i.next <= N), loop, exit24//25// Removing freezes from these chains help scalar evolution successfully analyze26// expressions.27//28//===----------------------------------------------------------------------===//2930#include "llvm/Transforms/Utils/CanonicalizeFreezeInLoops.h"31#include "llvm/ADT/DenseMapInfo.h"32#include "llvm/ADT/STLExtras.h"33#include "llvm/ADT/SetVector.h"34#include "llvm/ADT/SmallSet.h"35#include "llvm/Analysis/IVDescriptors.h"36#include "llvm/Analysis/LoopAnalysisManager.h"37#include "llvm/Analysis/LoopInfo.h"38#include "llvm/Analysis/LoopPass.h"39#include "llvm/Analysis/ScalarEvolution.h"40#include "llvm/Analysis/ValueTracking.h"41#include "llvm/IR/Dominators.h"42#include "llvm/InitializePasses.h"43#include "llvm/Pass.h"44#include "llvm/Support/Debug.h"45#include "llvm/Transforms/Utils.h"4647using namespace llvm;4849#define DEBUG_TYPE "canon-freeze"5051namespace {5253class CanonicalizeFreezeInLoops : public LoopPass {54public:55static char ID;5657CanonicalizeFreezeInLoops();5859private:60bool runOnLoop(Loop *L, LPPassManager &LPM) override;61void getAnalysisUsage(AnalysisUsage &AU) const override;62};6364class CanonicalizeFreezeInLoopsImpl {65Loop *L;66ScalarEvolution &SE;67DominatorTree &DT;6869// Can freeze instruction be pushed into operands of I?70// In order to do this, I should not create a poison after I's flags are71// stripped.72bool canHandleInst(const Instruction *I) {73auto Opc = I->getOpcode();74// If add/sub/mul, drop nsw/nuw flags.75return Opc == Instruction::Add || Opc == Instruction::Sub ||76Opc == Instruction::Mul;77}7879void InsertFreezeAndForgetFromSCEV(Use &U);8081public:82CanonicalizeFreezeInLoopsImpl(Loop *L, ScalarEvolution &SE, DominatorTree &DT)83: L(L), SE(SE), DT(DT) {}84bool run();85};8687} // anonymous namespace8889namespace llvm {9091struct FrozenIndPHIInfo {92// A freeze instruction that uses an induction phi93FreezeInst *FI = nullptr;94// The induction phi, step instruction, the operand idx of StepInst which is95// a step value96PHINode *PHI;97BinaryOperator *StepInst;98unsigned StepValIdx = 0;99100FrozenIndPHIInfo(PHINode *PHI, BinaryOperator *StepInst)101: PHI(PHI), StepInst(StepInst) {}102103bool operator==(const FrozenIndPHIInfo &Other) { return FI == Other.FI; }104};105106template <> struct DenseMapInfo<FrozenIndPHIInfo> {107static inline FrozenIndPHIInfo getEmptyKey() {108return FrozenIndPHIInfo(DenseMapInfo<PHINode *>::getEmptyKey(),109DenseMapInfo<BinaryOperator *>::getEmptyKey());110}111112static inline FrozenIndPHIInfo getTombstoneKey() {113return FrozenIndPHIInfo(DenseMapInfo<PHINode *>::getTombstoneKey(),114DenseMapInfo<BinaryOperator *>::getTombstoneKey());115}116117static unsigned getHashValue(const FrozenIndPHIInfo &Val) {118return DenseMapInfo<FreezeInst *>::getHashValue(Val.FI);119};120121static bool isEqual(const FrozenIndPHIInfo &LHS,122const FrozenIndPHIInfo &RHS) {123return LHS.FI == RHS.FI;124};125};126127} // end namespace llvm128129// Given U = (value, user), replace value with freeze(value), and let130// SCEV forget user. The inserted freeze is placed in the preheader.131void CanonicalizeFreezeInLoopsImpl::InsertFreezeAndForgetFromSCEV(Use &U) {132auto *PH = L->getLoopPreheader();133134auto *UserI = cast<Instruction>(U.getUser());135auto *ValueToFr = U.get();136assert(L->contains(UserI->getParent()) &&137"Should not process an instruction that isn't inside the loop");138if (isGuaranteedNotToBeUndefOrPoison(ValueToFr, nullptr, UserI, &DT))139return;140141LLVM_DEBUG(dbgs() << "canonfr: inserting freeze:\n");142LLVM_DEBUG(dbgs() << "\tUser: " << *U.getUser() << "\n");143LLVM_DEBUG(dbgs() << "\tOperand: " << *U.get() << "\n");144145U.set(new FreezeInst(ValueToFr, ValueToFr->getName() + ".frozen",146PH->getTerminator()->getIterator()));147148SE.forgetValue(UserI);149}150151bool CanonicalizeFreezeInLoopsImpl::run() {152// The loop should be in LoopSimplify form.153if (!L->isLoopSimplifyForm())154return false;155156SmallSetVector<FrozenIndPHIInfo, 4> Candidates;157158for (auto &PHI : L->getHeader()->phis()) {159InductionDescriptor ID;160if (!InductionDescriptor::isInductionPHI(&PHI, L, &SE, ID))161continue;162163LLVM_DEBUG(dbgs() << "canonfr: PHI: " << PHI << "\n");164FrozenIndPHIInfo Info(&PHI, ID.getInductionBinOp());165if (!Info.StepInst || !canHandleInst(Info.StepInst)) {166// The stepping instruction has unknown form.167// Ignore this PHI.168continue;169}170171Info.StepValIdx = Info.StepInst->getOperand(0) == &PHI;172Value *StepV = Info.StepInst->getOperand(Info.StepValIdx);173if (auto *StepI = dyn_cast<Instruction>(StepV)) {174if (L->contains(StepI->getParent())) {175// The step value is inside the loop. Freezing step value will introduce176// another freeze into the loop, so skip this PHI.177continue;178}179}180181auto Visit = [&](User *U) {182if (auto *FI = dyn_cast<FreezeInst>(U)) {183LLVM_DEBUG(dbgs() << "canonfr: found: " << *FI << "\n");184Info.FI = FI;185Candidates.insert(Info);186}187};188for_each(PHI.users(), Visit);189for_each(Info.StepInst->users(), Visit);190}191192if (Candidates.empty())193return false;194195SmallSet<PHINode *, 8> ProcessedPHIs;196for (const auto &Info : Candidates) {197PHINode *PHI = Info.PHI;198if (!ProcessedPHIs.insert(Info.PHI).second)199continue;200201BinaryOperator *StepI = Info.StepInst;202assert(StepI && "Step instruction should have been found");203204// Drop flags from the step instruction.205if (!isGuaranteedNotToBeUndefOrPoison(StepI, nullptr, StepI, &DT)) {206LLVM_DEBUG(dbgs() << "canonfr: drop flags: " << *StepI << "\n");207StepI->dropPoisonGeneratingFlags();208SE.forgetValue(StepI);209}210211InsertFreezeAndForgetFromSCEV(StepI->getOperandUse(Info.StepValIdx));212213unsigned OperandIdx =214PHI->getOperandNumForIncomingValue(PHI->getIncomingValue(0) == StepI);215InsertFreezeAndForgetFromSCEV(PHI->getOperandUse(OperandIdx));216}217218// Finally, remove the old freeze instructions.219for (const auto &Item : Candidates) {220auto *FI = Item.FI;221LLVM_DEBUG(dbgs() << "canonfr: removing " << *FI << "\n");222SE.forgetValue(FI);223FI->replaceAllUsesWith(FI->getOperand(0));224FI->eraseFromParent();225}226227return true;228}229230CanonicalizeFreezeInLoops::CanonicalizeFreezeInLoops() : LoopPass(ID) {231initializeCanonicalizeFreezeInLoopsPass(*PassRegistry::getPassRegistry());232}233234void CanonicalizeFreezeInLoops::getAnalysisUsage(AnalysisUsage &AU) const {235AU.addPreservedID(LoopSimplifyID);236AU.addRequired<LoopInfoWrapperPass>();237AU.addPreserved<LoopInfoWrapperPass>();238AU.addRequiredID(LoopSimplifyID);239AU.addRequired<ScalarEvolutionWrapperPass>();240AU.addPreserved<ScalarEvolutionWrapperPass>();241AU.addRequired<DominatorTreeWrapperPass>();242AU.addPreserved<DominatorTreeWrapperPass>();243}244245bool CanonicalizeFreezeInLoops::runOnLoop(Loop *L, LPPassManager &) {246if (skipLoop(L))247return false;248249auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();250auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();251return CanonicalizeFreezeInLoopsImpl(L, SE, DT).run();252}253254PreservedAnalyses255CanonicalizeFreezeInLoopsPass::run(Loop &L, LoopAnalysisManager &AM,256LoopStandardAnalysisResults &AR,257LPMUpdater &U) {258if (!CanonicalizeFreezeInLoopsImpl(&L, AR.SE, AR.DT).run())259return PreservedAnalyses::all();260261return getLoopPassPreservedAnalyses();262}263264INITIALIZE_PASS_BEGIN(CanonicalizeFreezeInLoops, "canon-freeze",265"Canonicalize Freeze Instructions in Loops", false, false)266INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)267INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)268INITIALIZE_PASS_DEPENDENCY(LoopSimplify)269INITIALIZE_PASS_END(CanonicalizeFreezeInLoops, "canon-freeze",270"Canonicalize Freeze Instructions in Loops", false, false)271272Pass *llvm::createCanonicalizeFreezeInLoopsPass() {273return new CanonicalizeFreezeInLoops();274}275276char CanonicalizeFreezeInLoops::ID = 0;277278279