How to require ScalarEvolution analysis in a Module pass?


What I want to do is to hack global opt pass, which needs ScalarEvolution analysis. However opt crashed saying
“opt: /home/xchen/llvm/include/llvm/PassAnalysisSupport.h:242: AnalysisType& llvm::Pass::getAnalysisID(const void*, llvm::Function&) [with AnalysisType = llvm::ScalarEvolution]: Assertion `ResultPass && “Unable to find requested analysis info”’ failed.”.

My question is what should I do to require ScalarEvolution analysis in a Module pass???

Steps to reproduce(bc file attached in case you do not have clang installed):

  1. Makefile looks like this:

clang test.c -disable-llvm-optzns -emit-llvm -S
llvm-as test.s -o test.bc
opt test.bc -loop-unroll -globalopt -loop-unroll -inline -loop-idiom -o out.bc
llvm-dis < out.bc > out.ll

  1. test.c:

#include <stdio.h>

int A[100];
static int toBeInlined(int A[100]) {
int ans = 0;
for (int i=0; i<100; ++i)
return ans;

int main() {
for (int i=0; i<100; ++i)
if (i%3==0)
A[i] = i;
A[i] = i+1;
int ans = toBeInlined(A);
printf("%d\n", ans);
return 0;

  1. globalopt:

— GlobalOpt.cpp (revision 162438)
+++ GlobalOpt.cpp (working copy)
@@ -38,6 +38,7 @@
#include “llvm/ADT/SmallVector.h”
#include “llvm/ADT/Statistic.h”
#include “llvm/ADT/STLExtras.h”
+#include “llvm/Analysis/ScalarEvolutionExpander.h”
using namespace llvm;

@@ -63,6 +64,7 @@
struct GlobalOpt : public ModulePass {
virtual void getAnalysisUsage(AnalysisUsage &AU) const {

  • AU.addRequired();
    static char ID; // Pass identification, replacement for typeid
    GlobalOpt() : ModulePass(ID) {
    @@ -92,6 +94,7 @@
    INITIALIZE_PASS_BEGIN(GlobalOpt, “globalopt”,
    “Global Variable Optimizer”, false, false)
    INITIALIZE_PASS_END(GlobalOpt, “globalopt”,
    “Global Variable Optimizer”, false, false)

@@ -2087,6 +2090,7 @@
if (!F->hasName() && !F->isDeclaration())

  • ScalarEvolution *SE = &getAnalysis(*F); ------------------------------------> CRASH here:
    if (F->isDefTriviallyDead()) {
    Changed = true;
  1. core dump

#0 0x000000386de30265 in raise () from /lib64/
#1 0x000000386de31d10 in abort () from /lib64/
#2 0x000000386de296e6 in __assert_fail () from /lib64/
#3 0x000000000058dbe1 in llvm::Pass::getAnalysisIDllvm::ScalarEvolution (this=0x10e9ef0, PI=0x10d3120, F=
…) at /home/xchen/llvm/include/llvm/PassAnalysisSupport.h:242
#4 0x000000000058dc4c in llvm::Pass::getAnalysisllvm::ScalarEvolution (this=0x10e9ef0, F=…)
at /home/xchen/llvm/include/llvm/PassAnalysisSupport.h:231
#5 0x0000000000577f60 in (anonymous namespace)::GlobalOpt::OptimizeFunctions (this=0x10e9ef0, M=…)
at /home/xchen/llvm/lib/Transforms/IPO/GlobalOpt.cpp:2093
#6 0x00000000005824c5 in (anonymous namespace)::GlobalOpt::runOnModule (this=0x10e9ef0, M=…)
at /home/xchen/llvm/lib/Transforms/IPO/GlobalOpt.cpp:3130
#7 0x0000000000a50c69 in llvm::MPPassManager::runOnModule (this=0x10f4120, M=…)
at /home/xchen/llvm/lib/VMCore/PassManager.cpp:1572
#8 0x0000000000a520d9 in llvm::PassManagerImpl::run (this=0x10f44a0, M=…)
at /home/xchen/llvm/lib/VMCore/PassManager.cpp:1655
#9 0x0000000000a5213b in llvm::PassManager::run (this=0x7fffffffd510, M=…)
at /home/xchen/llvm/lib/VMCore/PassManager.cpp:1684
#10 0x0000000000541e77 in main (argc=9, argv=0x7fffffffd778) at /home/xchen/llvm/tools/opt/opt.cpp:745

test.bc (1.14 KB)