[llvm] r185259 - InstCombine: FoldGEPICmp shouldn't change sign of base pointer comparison

David Majnemer david.majnemer at gmail.com
Sat Jun 29 03:28:04 PDT 2013


Author: majnemer
Date: Sat Jun 29 05:28:04 2013
New Revision: 185259

URL: http://llvm.org/viewvc/llvm-project?rev=185259&view=rev
Log:
InstCombine: FoldGEPICmp shouldn't change sign of base pointer comparison

Changing the sign when comparing the base pointer would introduce all
sorts of unexpected things like:
  %gep.i = getelementptr inbounds [1 x i8]* %a, i32 0, i32 0
  %gep2.i = getelementptr inbounds [1 x i8]* %b, i32 0, i32 0
  %cmp.i = icmp ult i8* %gep.i, %gep2.i
  %cmp.i1 = icmp ult [1 x i8]* %a, %b
  %cmp = icmp ne i1 %cmp.i, %cmp.i1
  ret i1 %cmp

into:
  %cmp.i = icmp slt [1 x i8]* %a, %b
  %cmp.i1 = icmp ult [1 x i8]* %a, %b
  %cmp = xor i1 %cmp.i, %cmp.i1
  ret i1 %cmp

By preserving the original sign, we now get:
  ret i1 false

This fixes PR16483.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/getelementptr.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=185259&r1=185258&r2=185259&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Sat Jun 29 05:28:04 2013
@@ -647,8 +647,7 @@ Instruction *InstCombiner::FoldGEPICmp(G
 
       // If all indices are the same, just compare the base pointers.
       if (IndicesTheSame)
-        return new ICmpInst(ICmpInst::getSignedPredicate(Cond),
-                            GEPLHS->getOperand(0), GEPRHS->getOperand(0));
+        return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
 
       // If we're comparing GEPs with two base pointers that only differ in type
       // and both GEPs have only constant indices or just one use, then fold

Modified: llvm/trunk/test/Transforms/InstCombine/getelementptr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/getelementptr.ll?rev=185259&r1=185258&r2=185259&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/getelementptr.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/getelementptr.ll Sat Jun 29 05:28:04 2013
@@ -509,4 +509,14 @@ define void @test39(%struct.ham* %arg, i
 ; CHECK: getelementptr inbounds i8* %tmp3, i64 -8
 }
 
+define i1 @pr16483([1 x i8]* %a, [1 x i8]* %b) {
+  %c = getelementptr [1 x i8]* %a, i32 0, i32 0
+  %d = getelementptr [1 x i8]* %b, i32 0, i32 0
+  %cmp = icmp ult i8* %c, %d
+  ret i1 %cmp
+
+; CHECK: @pr16483
+; CHECK-NEXT: icmp ult  [1 x i8]* %a, %b
+}
+
 ; CHECK: attributes [[NUW]] = { nounwind }





More information about the llvm-commits mailing list