[llvm-commits] [llvm] r55604 - in /llvm/trunk: include/llvm/LinkAllPasses.h include/llvm/Transforms/Scalar.h lib/Transforms/Scalar/MarkModRef.cpp

Duncan Sands baldrick at free.fr
Mon Sep 1 04:40:11 PDT 2008


Author: baldrick
Date: Mon Sep  1 06:40:11 2008
New Revision: 55604

URL: http://llvm.org/viewvc/llvm-project?rev=55604&view=rev
Log:
Add a small pass that sets the readnone/readonly
attributes on functions, based on the result of
alias analysis.  It's not hardwired to use
GlobalsModRef even though this is the only (AFAIK)
alias analysis that results in this pass actually
doing something.  Enable as follows:
  opt ... -globalsmodref-aa -markmodref ...
Advantages of this pass: (1) records the result
of globalsmodref in the bitcode, meaning it is
available for use by later passes (currently
the pass manager isn't smart enough to magically
make an advanced alias analysis available to all
later passes), which may expose more optimization
opportunities; (2) hopefully speeds up compilation
when code is optimized twice, for example when a
file is compiled to bitcode, then later LTO is done
on it: marking functions readonly/readnone when
producing the initial bitcode should speed up alias
analysis during LTO; (3) good for discovering that
globalsmodref doesn't work very well :)
Not currently turned on by default.

Added:
    llvm/trunk/lib/Transforms/Scalar/MarkModRef.cpp
Modified:
    llvm/trunk/include/llvm/LinkAllPasses.h
    llvm/trunk/include/llvm/Transforms/Scalar.h

Modified: llvm/trunk/include/llvm/LinkAllPasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LinkAllPasses.h?rev=55604&r1=55603&r2=55604&view=diff

==============================================================================
--- llvm/trunk/include/llvm/LinkAllPasses.h (original)
+++ llvm/trunk/include/llvm/LinkAllPasses.h Mon Sep  1 06:40:11 2008
@@ -85,6 +85,7 @@
       (void) llvm::createLowerInvokePass();
       (void) llvm::createLowerSetJmpPass();
       (void) llvm::createLowerSwitchPass();
+      (void) llvm::createMarkModRefPass();
       (void) llvm::createNoAAPass();
       (void) llvm::createNoProfileInfoPass();
       (void) llvm::createProfileLoaderPass();

Modified: llvm/trunk/include/llvm/Transforms/Scalar.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=55604&r1=55603&r2=55604&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Transforms/Scalar.h (original)
+++ llvm/trunk/include/llvm/Transforms/Scalar.h Mon Sep  1 06:40:11 2008
@@ -137,6 +137,11 @@
 //
 LoopPass *createLoopIndexSplitPass();
 
+//===----------------------------------------------------------------------===//
+//
+// MarkModRef - This pass marks functions readnone/readonly.
+//
+FunctionPass *createMarkModRefPass();
 
 //===----------------------------------------------------------------------===//
 //

Added: llvm/trunk/lib/Transforms/Scalar/MarkModRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MarkModRef.cpp?rev=55604&view=auto

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/MarkModRef.cpp (added)
+++ llvm/trunk/lib/Transforms/Scalar/MarkModRef.cpp Mon Sep  1 06:40:11 2008
@@ -0,0 +1,69 @@
+//===--------- MarkModRef.cpp - Mark functions readnone/readonly ----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass marks functions readnone/readonly based on the results of alias
+// analysis.  This requires a sufficiently powerful alias analysis, such as
+// GlobalsModRef (invoke as "opt ... -globalsmodref-aa -markmodref ...").
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "markmodref"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/Function.h"
+#include "llvm/Pass.h"
+using namespace llvm;
+
+STATISTIC(NumReadNone, "Number of functions marked readnone");
+STATISTIC(NumReadOnly, "Number of functions marked readonly");
+
+namespace {
+  struct VISIBILITY_HIDDEN MarkModRef : public FunctionPass {
+    static char ID; // Pass identification, replacement for typeid
+    MarkModRef() : FunctionPass((intptr_t)&ID) {}
+
+    bool runOnFunction(Function &F);
+
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.setPreservesCFG();
+      AU.addRequired<AliasAnalysis>();
+      AU.addPreserved<AliasAnalysis>();
+    }
+  };
+}
+
+char MarkModRef::ID = 0;
+static RegisterPass<MarkModRef>
+X("markmodref", "Mark functions readnone/readonly");
+
+bool MarkModRef::runOnFunction(Function &F) {
+  // FIXME: Wrong for functions with weak linkage.
+  if (F.doesNotAccessMemory())
+    // Cannot do better.
+    return false;
+
+  AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
+  AliasAnalysis::ModRefBehavior ModRef = AA.getModRefBehavior(&F);
+  if (ModRef == AliasAnalysis::DoesNotAccessMemory) {
+    F.setDoesNotAccessMemory();
+    NumReadNone++;
+    return true;
+  } else if (ModRef == AliasAnalysis::OnlyReadsMemory && !F.onlyReadsMemory()) {
+    F.setOnlyReadsMemory();
+    NumReadOnly++;
+    return true;
+  }
+  return false;
+}
+
+FunctionPass *llvm::createMarkModRefPass() {
+  return new MarkModRef();
+}





More information about the llvm-commits mailing list