LLVM has no effect when using getAnalysisUsage() to register ScalarEvolutionWrapperPass

#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/Analysis/ScalarEvolution.h"

class MyPass : public llvm::FunctionPass {
  static char ID;
  MyPass() : llvm::FunctionPass(ID) {}

  void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {

  bool runOnFunction(llvm::Function &F) override {
    ScalarEvolution* SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
    for (BasicBlock &BB : F) {
        for (Instruction &I : BB) {
          if (auto *GEP= dyn_cast<GetElementPtrInst>(&I)){
            const SCEV *arrayIndexSCEV = SE->getSCEV(GEP);
            // The scevtype obtained here is 0x0
            SCEVTypes scevtype = static_cast<SCEVTypes>(arrayIndexSCEV->getSCEVType()); 

    return true;

char MyPass::ID = 0;

static llvm::RegisterPass<MyPass> X("my-pass", "My Example Pass");

Is there any problem with the registration above? Why did I use this method to obtain a scevtype of 0x0? After investigation, it was confirmed that there was a problem at the registration location, but I couldn’t find the reason. I hope it can provide me with some help again.

  1. GEP in your case might not be “translatable” to its corresponding SCEV expression
  2. SCEVTypes is an enum with an unsigned short base, so it’s totally normal to yield a 0x0, which likely represents the first element in such enum.
  3. This is a soon-to-be-deprecated legacy PassManager Pass, it is encouraged to migrate to the new PassManager Pass.
  4. I think this post should be put in IR & Optimizations - LLVM Discussion Forums rather than Clang Frontend

Thank you very much for your answer

  1. I found a case of indirect use of SCEV to get this data, I can get some GEP data with simple offset, but in the face of some complicated ones, such as when the offset is operated by loop traversal and constant, or multiple loop traversal for operation, etc., the conversion to SCEV to get is scUnkonw type
  2. Can you tell me how the new version of PassManager Pass works? I only use the old PassManage Pass, or is there any way to learn to use it?
  3. Thank you for your correction, this is my first time posting to the forum, I am not very good at using it, I will correct this error later - IR & Optimizations - LLVM
  4. I also encountered a problem is how to use LLVM to determine the array is sequential access and read (or understand the array subscript from 0 to n-1 in increasing order by 1, where n-1 is the maximum subscript of the array), the biggest problem is to encounter the subscript of the array has arithmetic case, as I mentioned in the first case.
    Finally, thank you again for your reply.

yeah…unfortunately a SCEV is constructed on a best-effort basis.


You might want to turn the loop into a canonical form that is more favorable to most loop analyses / transformations, including ScalarEvolution.