[PATCH] D23267: [LVI] Take range metadata into account while calculating icmp condition constraints

Artur Pilipenko via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 8 08:29:44 PDT 2016


apilipenko created this revision.
apilipenko added a reviewer: sanjoy.
apilipenko added a subscriber: llvm-commits.

Take range metadata into account for conditions like this: 
%length = load i32, i32* %length_ptr, !range !{i32 0, i32 2147483647}
%cmp = icmp ult i32 %a, %length

This is a common pattern for range checks where the length of the array is dynamically loaded.

https://reviews.llvm.org/D23267

Files:
  lib/Analysis/LazyValueInfo.cpp
  test/Transforms/CorrelatedValuePropagation/add.ll
  test/Transforms/CorrelatedValuePropagation/range.ll

Index: test/Transforms/CorrelatedValuePropagation/range.ll
===================================================================
--- test/Transforms/CorrelatedValuePropagation/range.ll
+++ test/Transforms/CorrelatedValuePropagation/range.ll
@@ -445,3 +445,20 @@
 else:
   ret i1 false
 }
+
+ at limit = external global i32
+define i1 @test15(i32 %a) {
+; CHECK-LABEL: @test15(
+; CHECK: then:
+; CHECK-NEXT: ret i1 false
+  %limit = load i32, i32* @limit, !range !{i32 0, i32 256}
+  %cmp = icmp ult i32 %a, %limit
+  br i1 %cmp, label %then, label %else
+
+then:
+  %result = icmp eq i32 %a, 255
+  ret i1 %result
+
+else:
+  ret i1 false
+}
\ No newline at end of file
Index: test/Transforms/CorrelatedValuePropagation/add.ll
===================================================================
--- test/Transforms/CorrelatedValuePropagation/add.ll
+++ test/Transforms/CorrelatedValuePropagation/add.ll
@@ -165,3 +165,22 @@
 exit:
   ret void
 }
+
+ at limit = external global i32
+; CHECK-LABEL: @test9(
+define i32 @test9(i32* %p, i32 %i) {
+  %limit = load i32, i32* %p, !range !{i32 0, i32 2147483647}
+  %within.1 = icmp ugt i32 %limit, %i
+  %i.plus.7 = add i32 %i, 7
+  %within.2 = icmp ugt i32 %limit, %i.plus.7
+  %within = and i1 %within.1, %within.2
+  br i1 %within, label %then, label %else
+
+then:
+; CHECK: %add = add nuw nsw i32 %a, 6
+  %i.plus.6 = add i32 %i, 6
+  ret i32 %i.plus.6
+
+else:
+  ret i32 0
+}
Index: lib/Analysis/LazyValueInfo.cpp
===================================================================
--- lib/Analysis/LazyValueInfo.cpp
+++ lib/Analysis/LazyValueInfo.cpp
@@ -1218,6 +1218,9 @@
                            /*isFullSet=*/true);
     if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
       RHSRange = ConstantRange(CI->getValue());
+    else if (Instruction *I = dyn_cast<Instruction>(RHS))
+      if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
+        RHSRange = getConstantRangeFromMetadata(*Ranges);
 
     // If we're interested in the false dest, invert the condition
     CmpInst::Predicate Pred =


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23267.67174.patch
Type: text/x-patch
Size: 2055 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160808/7a46b332/attachment-0001.bin>


More information about the llvm-commits mailing list