llvm.org GIT mirror llvm / 38a5bdc
[LVI] Take range metadata into account while calculating icmp condition constraints 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. Reviewed By: sanjoy Differential Revision: https://reviews.llvm.org/D23267 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278496 91177308-0d34-0410-b5e6-96231b3b80d8 Artur Pilipenko 3 years ago
3 changed file(s) with 39 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
12101210 /*isFullSet=*/true);
12111211 if (ConstantInt *CI = dyn_cast(RHS))
12121212 RHSRange = ConstantRange(CI->getValue());
1213 else if (Instruction *I = dyn_cast(RHS))
1214 if (auto *Ranges = I->getMetadata(LLVMContext::MD_range))
1215 RHSRange = getConstantRangeFromMetadata(*Ranges);
12131216
12141217 // If we're interested in the false dest, invert the condition
12151218 CmpInst::Predicate Pred =
192192 exit:
193193 ret void
194194 }
195
196 @limit = external global i32
197 ; CHECK-LABEL: @test11(
198 define i32 @test11(i32* %p, i32 %i) {
199 %limit = load i32, i32* %p, !range !{i32 0, i32 2147483647}
200 %within.1 = icmp ugt i32 %limit, %i
201 %i.plus.7 = add i32 %i, 7
202 %within.2 = icmp ugt i32 %limit, %i.plus.7
203 %within = and i1 %within.1, %within.2
204 br i1 %within, label %then, label %else
205
206 then:
207 ; CHECK: %i.plus.6 = add nuw nsw i32 %i, 6
208 %i.plus.6 = add i32 %i, 6
209 ret i32 %i.plus.6
210
211 else:
212 ret i32 0
213 }
444444 else:
445445 ret i1 false
446446 }
447
448 @limit = external global i32
449 define i1 @test15(i32 %a) {
450 ; CHECK-LABEL: @test15(
451 ; CHECK: then:
452 ; CHECK-NEXT: ret i1 false
453 %limit = load i32, i32* @limit, !range !{i32 0, i32 256}
454 %cmp = icmp ult i32 %a, %limit
455 br i1 %cmp, label %then, label %else
456
457 then:
458 %result = icmp eq i32 %a, 255
459 ret i1 %result
460
461 else:
462 ret i1 false
463 }