[llvm] r216597 - InstSimplify: Don't simplify gep X, (Y-X) to Y if types differ

David Majnemer david.majnemer at gmail.com
Wed Aug 27 13:08:35 PDT 2014


Author: majnemer
Date: Wed Aug 27 15:08:34 2014
New Revision: 216597

URL: http://llvm.org/viewvc/llvm-project?rev=216597&view=rev
Log:
InstSimplify: Don't simplify gep X, (Y-X) to Y if types differ

It's incorrect to perform this simplification if the types differ.
A bitcast would need to be inserted for this to work.

This fixes PR20771.

Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/test/Transforms/InstSimplify/gep.ll

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=216597&r1=216596&r2=216597&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Wed Aug 27 15:08:34 2014
@@ -2837,7 +2837,8 @@ static Value *SimplifyGEPInst(ArrayRef<V
             return Constant::getNullValue(GEPTy);
           Value *Temp;
           if (match(P, m_PtrToInt(m_Value(Temp))))
-            return Temp;
+            if (Temp->getType() == GEPTy)
+              return Temp;
           return nullptr;
         };
 

Modified: llvm/trunk/test/Transforms/InstSimplify/gep.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/gep.ll?rev=216597&r1=216596&r2=216597&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/gep.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/gep.ll Wed Aug 27 15:08:34 2014
@@ -64,3 +64,17 @@ define i64* @test6(i64* %b) {
 ; CHECK-LABEL: @test6
 ; CHECK-NEXT: ret i64* null
 }
+
+define i8* @test7(i8* %b, i8** %e) {
+  %e_ptr = ptrtoint i8** %e to i64
+  %b_ptr = ptrtoint i8* %b to i64
+  %sub = sub i64 %e_ptr, %b_ptr
+  %gep = getelementptr inbounds i8* %b, i64 %sub
+  ret i8* %gep
+; CHECK-LABEL: @test7
+; CHECK-NEXT: ptrtoint
+; CHECK-NEXT: ptrtoint
+; CHECK-NEXT: sub
+; CHECK-NEXT: getelementptr
+; CHECK-NEXT: ret
+}





More information about the llvm-commits mailing list