llvm.org GIT mirror llvm / b82d90f
[PoisonChecking] Add validation rules for "exact" on sdiv/udiv As directly stated in the LangRef, no ambiguity here... git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365538 91177308-0d34-0410-b5e6-96231b3b80d8 Philip Reames 3 months ago
2 changed file(s) with 61 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
147147 auto *OverflowOp =
148148 B.CreateBinaryIntrinsic(Intrinsic::umul_with_overflow, LHS, RHS);
149149 Checks.push_back(B.CreateExtractValue(OverflowOp, 1));
150 }
151 break;
152 }
153 case Instruction::UDiv: {
154 if (I.isExact()) {
155 auto *Check =
156 B.CreateICmp(ICmpInst::ICMP_NE, B.CreateURem(LHS, RHS),
157 ConstantInt::get(LHS->getType(), 0));
158 Checks.push_back(Check);
159 }
160 break;
161 }
162 case Instruction::SDiv: {
163 if (I.isExact()) {
164 auto *Check =
165 B.CreateICmp(ICmpInst::ICMP_NE, B.CreateSRem(LHS, RHS),
166 ConstantInt::get(LHS->getType(), 0));
167 Checks.push_back(Check);
150168 }
151169 break;
152170 }
155155 ret i32 %res
156156 }
157157
158 define i32 @sdiv_noflags(i32 %a, i32 %b) {
159 ; CHECK-LABEL: @sdiv_noflags(
160 ; CHECK-NEXT: [[RES:%.*]] = sdiv i32 [[A:%.*]], [[B:%.*]]
161 ; CHECK-NEXT: ret i32 [[RES]]
162 ;
163 %res = sdiv i32 %a, %b
164 ret i32 %res
165 }
166
167 define i32 @sdiv_exact(i32 %a, i32 %b) {
168 ; CHECK-LABEL: @sdiv_exact(
169 ; CHECK-NEXT: [[TMP1:%.*]] = srem i32 [[A:%.*]], [[B:%.*]]
170 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 0
171 ; CHECK-NEXT: [[RES:%.*]] = sdiv exact i32 [[A]], [[B]]
172 ; CHECK-NEXT: [[TMP3:%.*]] = xor i1 [[TMP2]], true
173 ; CHECK-NEXT: call void @__poison_checker_assert(i1 [[TMP3]])
174 ; CHECK-NEXT: ret i32 [[RES]]
175 ;
176 %res = sdiv exact i32 %a, %b
177 ret i32 %res
178 }
179
180 define i32 @udiv_noflags(i32 %a, i32 %b) {
181 ; CHECK-LABEL: @udiv_noflags(
182 ; CHECK-NEXT: [[RES:%.*]] = udiv i32 [[A:%.*]], [[B:%.*]]
183 ; CHECK-NEXT: ret i32 [[RES]]
184 ;
185 %res = udiv i32 %a, %b
186 ret i32 %res
187 }
188
189 define i32 @udiv_exact(i32 %a, i32 %b) {
190 ; CHECK-LABEL: @udiv_exact(
191 ; CHECK-NEXT: [[TMP1:%.*]] = urem i32 [[A:%.*]], [[B:%.*]]
192 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 0
193 ; CHECK-NEXT: [[RES:%.*]] = udiv exact i32 [[A]], [[B]]
194 ; CHECK-NEXT: [[TMP3:%.*]] = xor i1 [[TMP2]], true
195 ; CHECK-NEXT: call void @__poison_checker_assert(i1 [[TMP3]])
196 ; CHECK-NEXT: ret i32 [[RES]]
197 ;
198 %res = udiv exact i32 %a, %b
199 ret i32 %res
200 }