[llvm-commits] [llvm] r89978 - in /llvm/trunk: lib/Analysis/MemoryDependenceAnalysis.cpp lib/Target/README.txt test/Transforms/GVN/rle-phi-translate.ll

Chris Lattner sabre at nondot.org
Thu Nov 26 15:41:07 PST 2009


Author: lattner
Date: Thu Nov 26 17:41:07 2009
New Revision: 89978

URL: http://llvm.org/viewvc/llvm-project?rev=89978&view=rev
Log:
Teach memdep to phi translate bitcasts.  This allows us to compile
the example in GCC PR16799 to:

LBB1_2:                                                     ## %bb1
	movl	%eax, %eax
	subq	%rax, %rdi
	movq	%rdi, (%rcx)
	movl	(%rdi), %eax
	testl	%eax, %eax
	je	LBB1_2

instead of:

LBB1_2:                                                     ## %bb1
	movl	(%rdi), %ecx
	subq	%rcx, %rdi
	movq	%rdi, (%rax)
	cmpl	$0, (%rdi)
	je	LBB1_2


Modified:
    llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
    llvm/trunk/lib/Target/README.txt
    llvm/trunk/test/Transforms/GVN/rle-phi-translate.ll

Modified: llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp?rev=89978&r1=89977&r2=89978&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/MemoryDependenceAnalysis.cpp Thu Nov 26 17:41:07 2009
@@ -690,10 +690,15 @@
   if (isa<PHINode>(Inst))
     return true;
   
-  // TODO: BITCAST, GEP.
-
-  // ...
+  // We can handle bitcast of a PHI, but the PHI needs to be in the same block
+  // as the bitcast.
+  if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst))
+    if (PHINode *PN = dyn_cast<PHINode>(BC->getOperand(0)))
+      if (PN->getParent() == BC->getParent())
+        return true;
   
+  // TODO: GEP, ...
+
   //   cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
   //   if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
   //     cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
@@ -708,6 +713,25 @@
   if (PHINode *PN = dyn_cast<PHINode>(Inst))
     return PN->getIncomingValueForBlock(Pred);
   
+  if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) {
+    PHINode *BCPN = cast<PHINode>(BC->getOperand(0));
+    Value *PHIIn = BCPN->getIncomingValueForBlock(Pred);
+    
+    // Constants are trivial to phi translate.
+    if (Constant *C = dyn_cast<Constant>(PHIIn))
+      return ConstantExpr::getBitCast(C, BC->getType());
+    
+    // Otherwise we have to see if a bitcasted version of the incoming pointer
+    // is available.  If so, we can use it, otherwise we have to fail.
+    for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end();
+         UI != E; ++UI) {
+      if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI))
+        if (BCI->getType() == BC->getType())
+          return BCI;
+    }
+    return 0;
+  }
+
   return 0;
 }
 

Modified: llvm/trunk/lib/Target/README.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/README.txt?rev=89978&r1=89977&r2=89978&view=diff

==============================================================================
--- llvm/trunk/lib/Target/README.txt (original)
+++ llvm/trunk/lib/Target/README.txt Thu Nov 26 17:41:07 2009
@@ -1284,8 +1284,6 @@
 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34677 (licm does this, LPRE crit edge)
   llvm-gcc t2.c -S -o - -O0 -emit-llvm | llvm-as | opt -mem2reg -simplifycfg -gvn | llvm-dis
 
-http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16799 [BITCAST PHI TRANS]
-
 //===---------------------------------------------------------------------===//
 
 Type based alias analysis:

Modified: llvm/trunk/test/Transforms/GVN/rle-phi-translate.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GVN/rle-phi-translate.ll?rev=89978&r1=89977&r2=89978&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/GVN/rle-phi-translate.ll (original)
+++ llvm/trunk/test/Transforms/GVN/rle-phi-translate.ll Thu Nov 26 17:41:07 2009
@@ -4,6 +4,7 @@
 target triple = "i386-apple-darwin7"
 
 define i32 @test1(i32* %b, i32* %c) nounwind {
+; CHECK: @test1
 entry:
 	%g = alloca i32
 	%t1 = icmp eq i32* %b, null
@@ -34,3 +35,28 @@
 	ret i32 %ret
 }
 
+define i8 @test2(i1 %cond, i32* %b, i32* %c) nounwind {
+; CHECK: @test2
+entry:
+	br i1 %cond, label %bb, label %bb1
+
+bb:
+  %b1 = bitcast i32* %b to i8*
+  store i8 4, i8* %b1
+	br label %bb2
+
+bb1:
+  %c1 = bitcast i32* %c to i8*
+  store i8 92, i8* %c1
+	br label %bb2
+
+bb2:
+	%d = phi i32* [ %c, %bb1 ], [ %b, %bb ]
+  %d1 = bitcast i32* %d to i8*
+	%dv = load i8* %d1
+; CHECK: %dv = phi i8
+; CHECK-NOT: load
+; CHECK: ret i8 %dv
+	ret i8 %dv
+}
+





More information about the llvm-commits mailing list