llvm.org GIT mirror llvm / c8cb8ef
have instcombine preserve nsw/nuw/exact when sinking common operations through a phi. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125790 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 8 years ago
2 changed file(s) with 71 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
3030 const Type *LHSType = LHSVal->getType();
3131 const Type *RHSType = RHSVal->getType();
3232
33 bool isNUW = false, isNSW = false, isExact = false;
34 if (OverflowingBinaryOperator *BO =
35 dyn_cast(FirstInst)) {
36 isNUW = BO->hasNoUnsignedWrap();
37 isNSW = BO->hasNoSignedWrap();
38 } else if (PossiblyExactOperator *PEO =
39 dyn_cast(FirstInst))
40 isExact = PEO->isExact();
41
3342 // Scan to see if all operands are the same opcode, and all have one use.
3443 for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
3544 Instruction *I = dyn_cast(PN.getIncomingValue(i));
3645 if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
3746 // Verify type of the LHS matches so we don't fold cmp's of different
38 // types or GEP's with different index types.
47 // types.
3948 I->getOperand(0)->getType() != LHSType ||
4049 I->getOperand(1)->getType() != RHSType)
4150 return 0;
4251
4352 // If they are CmpInst instructions, check their predicates
44 if (Opc == Instruction::ICmp || Opc == Instruction::FCmp)
45 if (cast(I)->getPredicate() !=
46 cast(FirstInst)->getPredicate())
53 if (CmpInst *CI = dyn_cast(I))
54 if (CI->getPredicate() != cast(FirstInst)->getPredicate())
4755 return 0;
56
57 if (isNUW)
58 isNUW = cast(I)->hasNoUnsignedWrap();
59 if (isNSW)
60 isNSW = cast(I)->hasNoSignedWrap();
61 if (isExact)
62 isExact = cast(I)->isExact();
4863
4964 // Keep track of which operand needs a phi node.
5065 if (I->getOperand(0) != LHSVal) LHSVal = 0;
96111 }
97112 }
98113
99 if (BinaryOperator *BinOp = dyn_cast(FirstInst))
100 return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
101 CmpInst *CIOp = cast(FirstInst);
102 return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
103 LHSVal, RHSVal);
114 if (CmpInst *CIOp = dyn_cast(FirstInst))
115 return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
116 LHSVal, RHSVal);
117
118 BinaryOperator *BinOp = cast(FirstInst);
119 BinaryOperator *NewBinOp =
120 BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
121 if (isNUW) NewBinOp->setHasNoUnsignedWrap();
122 if (isNSW) NewBinOp->setHasNoSignedWrap();
123 if (isExact) NewBinOp->setIsExact();
124 return NewBinOp;
104125 }
105126
106127 Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) {
372393 // code size and simplifying code.
373394 Constant *ConstantOp = 0;
374395 const Type *CastSrcTy = 0;
396 bool isNUW = false, isNSW = false, isExact = false;
375397
376398 if (isa(FirstInst)) {
377399 CastSrcTy = FirstInst->getOperand(0)->getType();
388410 ConstantOp = dyn_cast(FirstInst->getOperand(1));
389411 if (ConstantOp == 0)
390412 return FoldPHIArgBinOpIntoPHI(PN);
413
414 if (OverflowingBinaryOperator *BO =
415 dyn_cast(FirstInst)) {
416 isNUW = BO->hasNoUnsignedWrap();
417 isNSW = BO->hasNoSignedWrap();
418 } else if (PossiblyExactOperator *PEO =
419 dyn_cast(FirstInst))
420 isExact = PEO->isExact();
391421 } else {
392422 return 0; // Cannot fold this operation.
393423 }
403433 } else if (I->getOperand(1) != ConstantOp) {
404434 return 0;
405435 }
436
437 if (isNUW)
438 isNUW = cast(I)->hasNoUnsignedWrap();
439 if (isNSW)
440 isNSW = cast(I)->hasNoSignedWrap();
441 if (isExact)
442 isExact = cast(I)->isExact();
406443 }
407444
408445 // Okay, they are all the same operation. Create a new PHI node of the
437474 if (CastInst *FirstCI = dyn_cast(FirstInst))
438475 return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType());
439476
440 if (BinaryOperator *BinOp = dyn_cast(FirstInst))
441 return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
477 if (BinaryOperator *BinOp = dyn_cast(FirstInst)) {
478 BinOp = BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
479 if (isNUW) BinOp->setHasNoUnsignedWrap();
480 if (isNSW) BinOp->setHasNoSignedWrap();
481 if (isExact) BinOp->setIsExact();
482 return BinOp;
483 }
442484
443485 CmpInst *CIOp = cast(FirstInst);
444486 return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
525525 ; CHECK-NEXT: ret i32 %B
526526 }
527527
528 define i32 @test24(i32 %A, i1 %cond) {
529 BB0:
530 %X = add nuw nsw i32 %A, 1
531 br i1 %cond, label %BB1, label %BB2
532
533 BB1:
534 %Y = add nuw i32 %A, 1
535 br label %BB2
536
537 BB2:
538 %C = phi i32 [ %X, %BB0 ], [ %Y, %BB1 ]
539 ret i32 %C
540 ; CHECK: @test24
541 ; CHECK-NOT: phi
542 ; CHECK: BB2:
543 ; CHECK-NEXT: %C = add nuw i32 %A, 1
544 ; CHECK-NEXT: ret i32 %C
545 }