[llvm-commits] [llvm] r57050 - in /llvm/trunk: lib/Transforms/IPO/AddReadAttrs.cpp test/Transforms/AddReadAttrs/2008-10-04-LocalMemory.ll

Duncan Sands baldrick at free.fr
Sat Oct 4 06:24:27 PDT 2008


Author: baldrick
Date: Sat Oct  4 08:24:24 2008
New Revision: 57050

URL: http://llvm.org/viewvc/llvm-project?rev=57050&view=rev
Log:
Ignore loads from and stores to local memory (i.e. allocas)
when deciding whether to mark a function readnone/readonly.
Since the pass is currently run before SROA, this may be
quite helpful.  Requested by Chris on IRC.

Added:
    llvm/trunk/test/Transforms/AddReadAttrs/2008-10-04-LocalMemory.ll
Modified:
    llvm/trunk/lib/Transforms/IPO/AddReadAttrs.cpp

Modified: llvm/trunk/lib/Transforms/IPO/AddReadAttrs.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/AddReadAttrs.cpp?rev=57050&r1=57049&r2=57050&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/AddReadAttrs.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/AddReadAttrs.cpp Sat Oct  4 08:24:24 2008
@@ -86,17 +86,34 @@
 
     // Scan the function body for instructions that may read or write memory.
     for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
-      CallSite CS = CallSite::get(&*II);
+      Instruction *I = &*II;
 
-      // Ignore calls to functions in the same SCC.
-      if (CS.getInstruction() && SCCNodes.count(CG[CS.getCalledFunction()]))
-        continue;
-
-      if (II->mayWriteToMemory())
+      // Some instructions can be ignored even if they read or write memory.
+      // Detect these now, skipping to the next instruction if one is found.
+      CallSite CS = CallSite::get(I);
+      if (CS.getInstruction()) {
+        // Ignore calls to functions in the same SCC.
+        if (SCCNodes.count(CG[CS.getCalledFunction()]))
+          continue;
+      } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
+        Value *Target = LI->getPointerOperand()->getUnderlyingObject();
+        // Ignore loads from local memory.
+        if (isa<AllocaInst>(Target))
+          continue;
+      } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
+        Value *Target = SI->getPointerOperand()->getUnderlyingObject();
+        // Ignore stores to local memory.
+        if (isa<AllocaInst>(Target))
+          continue;
+      }
+
+      // Any remaining instructions need to be taken seriously!  Check if they
+      // read or write memory.
+      if (I->mayWriteToMemory())
         // Writes memory.  Just give up.
         return false;
-
-      ReadsMemory |= II->mayReadFromMemory();
+      // If this instruction may read memory, remember that.
+      ReadsMemory |= I->mayReadFromMemory();
     }
   }
 

Added: llvm/trunk/test/Transforms/AddReadAttrs/2008-10-04-LocalMemory.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/AddReadAttrs/2008-10-04-LocalMemory.ll?rev=57050&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/AddReadAttrs/2008-10-04-LocalMemory.ll (added)
+++ llvm/trunk/test/Transforms/AddReadAttrs/2008-10-04-LocalMemory.ll Sat Oct  4 08:24:24 2008
@@ -0,0 +1,10 @@
+; RUN: llvm-as < %s | opt -addreadattrs | llvm-dis | grep readnone | count 2
+
+declare i32 @g(i32*) readnone
+
+define i32 @f() {
+	%x = alloca i32		; <i32*> [#uses=2]
+	store i32 0, i32* %x
+	%y = call i32 @g(i32* %x)		; <i32> [#uses=1]
+	ret i32 %y
+}





More information about the llvm-commits mailing list