@@ -329,25 +329,29 @@ static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI) {
329
329
// See if we can prove that the given overflow intrinsic will not overflow.
330
330
static bool willNotOverflow (IntrinsicInst *II, LazyValueInfo *LVI) {
331
331
using OBO = OverflowingBinaryOperator;
332
- auto NoWrapOnAddition = [&] (Value *LHS, Value *RHS, unsigned NoWrapKind) {
332
+ auto NoWrap = [&] (Instruction::BinaryOps BinOp, unsigned NoWrapKind) {
333
+ Value *RHS = II->getOperand (1 );
333
334
ConstantRange RRange = LVI->getConstantRange (RHS, II->getParent (), II);
334
335
ConstantRange NWRegion = ConstantRange::makeGuaranteedNoWrapRegion (
335
- BinaryOperator::Add , RRange, NoWrapKind);
336
+ BinOp , RRange, NoWrapKind);
336
337
// As an optimization, do not compute LRange if we do not need it.
337
338
if (NWRegion.isEmptySet ())
338
339
return false ;
340
+ Value *LHS = II->getOperand (0 );
339
341
ConstantRange LRange = LVI->getConstantRange (LHS, II->getParent (), II);
340
342
return NWRegion.contains (LRange);
341
343
};
342
344
switch (II->getIntrinsicID ()) {
343
345
default :
344
346
break ;
345
347
case Intrinsic::uadd_with_overflow:
346
- return NoWrapOnAddition (II->getOperand (0 ), II->getOperand (1 ),
347
- OBO::NoUnsignedWrap);
348
+ return NoWrap (Instruction::Add, OBO::NoUnsignedWrap);
348
349
case Intrinsic::sadd_with_overflow:
349
- return NoWrapOnAddition (II->getOperand (0 ), II->getOperand (1 ),
350
- OBO::NoSignedWrap);
350
+ return NoWrap (Instruction::Add, OBO::NoSignedWrap);
351
+ case Intrinsic::usub_with_overflow:
352
+ return NoWrap (Instruction::Sub, OBO::NoUnsignedWrap);
353
+ case Intrinsic::ssub_with_overflow:
354
+ return NoWrap (Instruction::Sub, OBO::NoSignedWrap);
351
355
}
352
356
return false ;
353
357
}
@@ -356,12 +360,17 @@ static void processOverflowIntrinsic(IntrinsicInst *II) {
356
360
Value *NewOp = nullptr ;
357
361
switch (II->getIntrinsicID ()) {
358
362
default :
359
- llvm_unreachable (" Illegal instruction." );
363
+ llvm_unreachable (" Unexpected instruction." );
360
364
case Intrinsic::uadd_with_overflow:
361
365
case Intrinsic::sadd_with_overflow:
362
366
NewOp = BinaryOperator::CreateAdd (II->getOperand (0 ), II->getOperand (1 ),
363
367
II->getName (), II);
364
368
break ;
369
+ case Intrinsic::usub_with_overflow:
370
+ case Intrinsic::ssub_with_overflow:
371
+ NewOp = BinaryOperator::CreateSub (II->getOperand (0 ), II->getOperand (1 ),
372
+ II->getName (), II);
373
+ break ;
365
374
}
366
375
++NumOverflows;
367
376
IRBuilder<> B (II);
@@ -376,7 +385,7 @@ static bool processCallSite(CallSite CS, LazyValueInfo *LVI) {
376
385
SmallVector<unsigned , 4 > ArgNos;
377
386
unsigned ArgNo = 0 ;
378
387
379
- if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction ())) {
388
+ if (auto *II = dyn_cast<IntrinsicInst>(CS.getInstruction ())) {
380
389
if (willNotOverflow (II, LVI)) {
381
390
processOverflowIntrinsic (II);
382
391
return true ;
0 commit comments