General questions about PassManager and the removed SimplifyLibraryCalls pass

Hi,

I've been going the PassManager documentation and reading some of the
code and there a few things that aren't obvious (at least) to me that
I'd like to understand.

1. It seems the opt tool is aware of the dependencies of Transforms
(e.g. Global Value Numbering) and adds its the analyses it depends on
to the PassManager. It looks like the dependencies are specified using
macros

lib/Transforms/Scalar/GVN.cpp

INITIALIZE_PASS_BEGIN(GVN, "gvn", "Global Value Numbering", false, false)
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
INITIALIZE_PASS_END(GVN, "gvn", "Global Value Numbering", false, false)

But also in

void getAnalysisUsage(AnalysisUsage &AU) const override {
      AU.addRequired<DominatorTreeWrapperPass>();
      AU.addRequired<TargetLibraryInfo>();
      if (!NoLoads)
        AU.addRequired<MemoryDependenceAnalysis>();
      AU.addRequired<AliasAnalysis>();

      AU.addPreserved<DominatorTreeWrapperPass>();
      AU.addPreserved<AliasAnalysis>();
    }

Why are these dependencies stated twice, once in getAnalysisUsage()
and the other using macros?

If I am using my a PassManager in my own tool and add a transform like
GVN do I manually need to add the analyses it depends on to the
PassManager before adding the Transform itself or does the PassManager
do something smart and add the Analyses automatically like the opt
tool does?

2. I recently noticed that the SimplifyLibraryCallsPass was removed
and according to the LLVM3.4 release notes its functionality is now in
"instruction combiner and function attribute marking passes". I was
wondering, which function attributes passes were meant?

I took a look at opt's code and it seems to add the TargetLibraryInfo
pass. Is there anything I need to do with this pass (possibly add to
the pass manager depending on the answer to 1) to achieve the same
transform that SimplifyLibraryCalls did?

Thanks,
Dan Liew.

2. I recently noticed that the SimplifyLibraryCallsPass was removed
and according to the LLVM3.4 release notes its functionality is now in
"instruction combiner and function attribute marking passes". I was
wondering, which function attributes passes were meant?

The "functionattrs" pass. See 'lib/Transforms/IPO/FunctionAttrs.cpp'.

I took a look at opt's code and it seems to add the TargetLibraryInfo
pass. Is there anything I need to do with this pass (possibly add to
the pass manager depending on the answer to 1) to achieve the same
transform that SimplifyLibraryCalls did?

Off the top of my head, I am not sure. If I get a moment, then I will look
into it.

Sorry for not replying earlier. I recently just switched my e-mail
account and I noticed there were several e-mails I haven't replied to
during migration.

2. I recently noticed that the SimplifyLibraryCallsPass was removed
and according to the LLVM3.4 release notes its functionality is now in
"instruction combiner and function attribute marking passes". I was
wondering, which function attributes passes were meant?

The "functionattrs" pass. See 'lib/Transforms/IPO/FunctionAttrs.cpp'.

Oh I thought you meant TargetLibraryInfo because it is used by the opt
tool to disable simplification of library calls, like so...

  PassManager Passes;

  // Add an appropriate TargetLibraryInfo pass for the module's triple.
  TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));

  // The -disable-simplify-libcalls flag actually disables all builtin optzns.
  if (DisableSimplifyLibCalls)
    TLI->disableAllFunctions();
  Passes.add(TLI);

However the TargetLibraryInfo pass seems to be immutable so I guess
that means it's not allowed to add attributes to functions. I see that
the FunctionAttrs pass depends on TargetLibraryInfo so I guess that
makes sense.

I recently just sent a patch to the documentation relating to this to
llvm-commits. Looks like I'm going to need to re-write it slightly.

Thanks,

Hmm maybe I'm doing something wrong but I don't observe that using the
FunctionAttrs pass affects an optimization I see that InstCombine
makes.

ret.c:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Hello\n");
    exit(0);
}

$ clang -emit-llvm -S -O0 ret.c

$ opt -instcombine --debug-pass=Structure -S ret.ll

I see that the call to @printf() gets replaced by @puts(). During this
run the FunctionAttrs is not being run as far as I can tell.

$ opt -instcombine -disable-simplify-libcalls --debug-pass=Structure -S ret.ll

Using -disable-simplify-libcalls causes disableAllFunctions() to be
called on the TargetLibraryInfo pass. Now the call to @printf() is not
replaced with @puts().
So far FunctionAttrs hasn't been involved in InstCombine's optimisations.

Now if we use the functionattrs pass
$ opt -instcombine -functionattrs --debug-pass=Structure -S ret.ll

nothing much changes compared to ``opt -instcombine
--debug-pass=Structure -S ret.ll`` except that @printf() has the
``nounwind`` attribute added and @puts() has the ``readonly``
parameter on its first argument. The optimisation hasn't really
changed.

I was also expecting the call to @exit() to be replaced with a return
statement but that doesn't seem to of happened.

Thoughts?

Thanks,
Dan Liew.