Index: include/llvm/Transforms/Scalar/IndVarSimplify.h =================================================================== --- /dev/null +++ include/llvm/Transforms/Scalar/IndVarSimplify.h @@ -0,0 +1,29 @@ +//===- IndVarSimplify.h - Induction Variable Simplification -----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file provides the interface for the Induction Variable +// Simplification pass. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_SCALAR_INDVARSIMPLIFY_H +#define LLVM_TRANSFORMS_SCALAR_INDVARSIMPLIFY_H + +#include "llvm/Analysis/LoopInfo.h" +#include "llvm/IR/PassManager.h" + +namespace llvm { + +class IndVarSimplifyPass : public PassInfoMixin { +public: + PreservedAnalyses run(Loop &L, AnalysisManager &AM); +}; +} + +#endif // LLVM_TRANSFORMS_SCALAR_INDVARSIMPLIFY_H Index: lib/Passes/PassBuilder.cpp =================================================================== --- lib/Passes/PassBuilder.cpp +++ lib/Passes/PassBuilder.cpp @@ -70,6 +70,7 @@ #include "llvm/Transforms/Scalar/EarlyCSE.h" #include "llvm/Transforms/Scalar/GVN.h" #include "llvm/Transforms/Scalar/GuardWidening.h" +#include "llvm/Transforms/Scalar/IndVarSimplify.h" #include "llvm/Transforms/Scalar/LoopRotation.h" #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" #include "llvm/Transforms/Scalar/LowerAtomic.h" Index: lib/Passes/PassRegistry.def =================================================================== --- lib/Passes/PassRegistry.def +++ lib/Passes/PassRegistry.def @@ -158,4 +158,5 @@ LOOP_PASS("no-op-loop", NoOpLoopPass()) LOOP_PASS("print", PrintLoopPass(dbgs())) LOOP_PASS("simplify-cfg", LoopSimplifyCFGPass()) +LOOP_PASS("indvars", IndVarSimplifyPass()) #undef LOOP_PASS Index: lib/Transforms/Scalar/IndVarSimplify.cpp =================================================================== --- lib/Transforms/Scalar/IndVarSimplify.cpp +++ lib/Transforms/Scalar/IndVarSimplify.cpp @@ -24,12 +24,14 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Transforms/Scalar/IndVarSimplify.h" #include "llvm/Transforms/Scalar.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/GlobalsModRef.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/LoopPass.h" +#include "llvm/Analysis/LoopPassManager.h" #include "llvm/Analysis/ScalarEvolutionExpander.h" #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" #include "llvm/Analysis/TargetLibraryInfo.h" @@ -2214,6 +2216,29 @@ return Changed; } +PreservedAnalyses IndVarSimplifyPass::run(Loop &L, AnalysisManager &AM) { + auto &FAM = AM.getResult(L).getManager(); + Function *F = L.getHeader()->getParent(); + const DataLayout &DL = F->getParent()->getDataLayout(); + + auto *LI = FAM.getCachedResult(*F); + auto *SE = FAM.getCachedResult(*F); + auto *DT = FAM.getCachedResult(*F); + + assert((LI && SE && DT) && + "Analyses required for indvarsimplify not available!"); + + // Optional analyses. + auto *TTI = FAM.getCachedResult(*F); + auto *TLI = FAM.getCachedResult(*F); + + IndVarSimplify IVS(LI, SE, DT, DL, TLI, TTI); + if (!IVS.run(&L)) + return PreservedAnalyses::all(); + + return getLoopPassPreservedAnalyses(); +} + namespace { struct IndVarSimplifyLegacyPass : public LoopPass { static char ID; // Pass identification, replacement for typeid Index: test/Transforms/IndVarSimplify/backedge-on-min-max.ll =================================================================== --- test/Transforms/IndVarSimplify/backedge-on-min-max.ll +++ test/Transforms/IndVarSimplify/backedge-on-min-max.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -indvars -S | FileCheck %s +; RUN: opt -lcssa -loop-simplify -S < %s | opt -S -passes='require,require,require,loop(indvars)' ;; --- signed --- Index: test/Transforms/IndVarSimplify/iv-widen.ll =================================================================== --- test/Transforms/IndVarSimplify/iv-widen.ll +++ test/Transforms/IndVarSimplify/iv-widen.ll @@ -1,4 +1,5 @@ ; RUN: opt < %s -indvars -S | FileCheck %s +; RUN: opt -lcssa -loop-simplify -S < %s | opt -S -passes='require,require,require,loop(indvars)' ; Provide legal integer types. target datalayout = "n8:16:32:64" Index: test/Transforms/IndVarSimplify/sharpen-range.ll =================================================================== --- test/Transforms/IndVarSimplify/sharpen-range.ll +++ test/Transforms/IndVarSimplify/sharpen-range.ll @@ -1,4 +1,5 @@ ;; RUN: opt -S < %s -indvars | FileCheck %s +; RUN: opt -lcssa -loop-simplify -S < %s | opt -S -passes='require,require,require,loop(indvars)' ;; Check if llvm can narrow !range metadata based on loop entry ;; predicates.