[PoisonChecking] Add validation rules for "exact" on sdiv/udiv

As directly stated in the LangRef, no ambiguity here...

llvm-svn: 365538
This commit is contained in:
Philip Reames 2019-07-09 18:56:41 +00:00
parent f4fcc00e4d
commit b7e737435c
2 changed files with 61 additions and 0 deletions

View File

@ -151,6 +151,24 @@ static void generatePoisonChecksForBinOp(Instruction &I,
}
break;
}
case Instruction::UDiv: {
if (I.isExact()) {
auto *Check =
B.CreateICmp(ICmpInst::ICMP_NE, B.CreateURem(LHS, RHS),
ConstantInt::get(LHS->getType(), 0));
Checks.push_back(Check);
}
break;
}
case Instruction::SDiv: {
if (I.isExact()) {
auto *Check =
B.CreateICmp(ICmpInst::ICMP_NE, B.CreateSRem(LHS, RHS),
ConstantInt::get(LHS->getType(), 0));
Checks.push_back(Check);
}
break;
}
};
}

View File

@ -156,3 +156,46 @@ define i32 @mul_nsw_nuw(i32 %a, i32 %b) {
ret i32 %res
}
define i32 @sdiv_noflags(i32 %a, i32 %b) {
; CHECK-LABEL: @sdiv_noflags(
; CHECK-NEXT: [[RES:%.*]] = sdiv i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: ret i32 [[RES]]
;
%res = sdiv i32 %a, %b
ret i32 %res
}
define i32 @sdiv_exact(i32 %a, i32 %b) {
; CHECK-LABEL: @sdiv_exact(
; CHECK-NEXT: [[TMP1:%.*]] = srem i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 0
; CHECK-NEXT: [[RES:%.*]] = sdiv exact i32 [[A]], [[B]]
; CHECK-NEXT: [[TMP3:%.*]] = xor i1 [[TMP2]], true
; CHECK-NEXT: call void @__poison_checker_assert(i1 [[TMP3]])
; CHECK-NEXT: ret i32 [[RES]]
;
%res = sdiv exact i32 %a, %b
ret i32 %res
}
define i32 @udiv_noflags(i32 %a, i32 %b) {
; CHECK-LABEL: @udiv_noflags(
; CHECK-NEXT: [[RES:%.*]] = udiv i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: ret i32 [[RES]]
;
%res = udiv i32 %a, %b
ret i32 %res
}
define i32 @udiv_exact(i32 %a, i32 %b) {
; CHECK-LABEL: @udiv_exact(
; CHECK-NEXT: [[TMP1:%.*]] = urem i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 0
; CHECK-NEXT: [[RES:%.*]] = udiv exact i32 [[A]], [[B]]
; CHECK-NEXT: [[TMP3:%.*]] = xor i1 [[TMP2]], true
; CHECK-NEXT: call void @__poison_checker_assert(i1 [[TMP3]])
; CHECK-NEXT: ret i32 [[RES]]
;
%res = udiv exact i32 %a, %b
ret i32 %res
}