@@ -435,8 +435,6 @@ class CodeGenPrepare {
435
435
bool optimizeMemoryInst (Instruction *MemoryInst, Value *Addr, Type *AccessTy,
436
436
unsigned AddrSpace);
437
437
bool optimizeGatherScatterInst (Instruction *MemoryInst, Value *Ptr);
438
- bool optimizeUMulWithOverflow (Instruction *I);
439
- bool optimizeSMulWithOverflow (Instruction *I);
440
438
bool optimizeMulWithOverflow (Instruction *I, bool IsSigned,
441
439
ModifyDT &ModifiedDT);
442
440
bool optimizeInlineAsmInst (CallInst *CS);
@@ -6462,19 +6460,21 @@ bool CodeGenPrepare::optimizeMulWithOverflow(Instruction *I, bool IsSigned,
6462
6460
// overflow.res:
6463
6461
6464
6462
// New BBs:
6465
- BasicBlock *OverflowoEntryBB =
6463
+ std::string KeepBBName = I->getParent ()->getName ().str ();
6464
+ BasicBlock *OverflowEntryBB =
6466
6465
I->getParent ()->splitBasicBlock (I, " overflow.entry" , /* Before*/ true );
6467
6466
// Remove the 'br' instruction that is generated as a result of the split:
6468
- OverflowoEntryBB ->getTerminator ()->eraseFromParent ();
6467
+ OverflowEntryBB ->getTerminator ()->eraseFromParent ();
6469
6468
BasicBlock *NoOverflowBB =
6470
6469
BasicBlock::Create (I->getContext (), " overflow.no" , I->getFunction ());
6471
- NoOverflowBB->moveAfter (OverflowoEntryBB);
6472
- I->getParent ()->setName (" overflow" );
6473
- BasicBlock *OverflowBB = I->getParent ();
6470
+ NoOverflowBB->moveAfter (OverflowEntryBB);
6471
+ BasicBlock *OverflowBB =
6472
+ BasicBlock::Create (I->getContext (), " overflow" , I->getFunction ());
6473
+ OverflowBB->moveAfter (NoOverflowBB);
6474
6474
6475
6475
// BB overflow.entry:
6476
+ IRBuilder<> Builder (OverflowEntryBB);
6476
6477
// Get Lo and Hi of LHS & RHS:
6477
- IRBuilder<> Builder (OverflowoEntryBB);
6478
6478
Value *LoLHS = Builder.CreateTrunc (LHS, LegalTy, " lo.lhs" );
6479
6479
Value *HiLHS = Builder.CreateLShr (LHS, VTHalfBitWidth, " lhs.lsr" );
6480
6480
HiLHS = Builder.CreateTrunc (HiLHS, LegalTy, " hi.lhs" );
@@ -6504,16 +6504,7 @@ bool CodeGenPrepare::optimizeMulWithOverflow(Instruction *I, bool IsSigned,
6504
6504
6505
6505
// BB overflow.no:
6506
6506
Builder.SetInsertPoint (NoOverflowBB);
6507
- Value *ExtLoLHS, *ExtLoRHS;
6508
- if (IsSigned) {
6509
- ExtLoLHS = Builder.CreateSExt (LoLHS, Ty, " lo.lhs.ext" );
6510
- ExtLoRHS = Builder.CreateSExt (LoRHS, Ty, " lo.rhs.ext" );
6511
- } else {
6512
- ExtLoLHS = Builder.CreateZExt (LoLHS, Ty, " lo.lhs.ext" );
6513
- ExtLoRHS = Builder.CreateZExt (LoRHS, Ty, " lo.rhs.ext" );
6514
- }
6515
-
6516
- Value *Mul = Builder.CreateMul (ExtLoLHS, ExtLoRHS, " mul.overflow.no" );
6507
+ Value *Mul = Builder.CreateMul (LHS, RHS, " mul.overflow.no" );
6517
6508
6518
6509
// In overflow.no BB: we are sure that the overflow flag is false.
6519
6510
// So, if we found this pattern:
@@ -6547,6 +6538,7 @@ bool CodeGenPrepare::optimizeMulWithOverflow(Instruction *I, bool IsSigned,
6547
6538
if (DetectNoOverflowBrBB) {
6548
6539
// BB overflow.no: jump directly to if.end BB
6549
6540
Builder.CreateBr (DetectNoOverflowBrBB);
6541
+
6550
6542
// BB if.end:
6551
6543
Builder.SetInsertPoint (DetectNoOverflowBrBB,
6552
6544
DetectNoOverflowBrBB->getFirstInsertionPt ());
@@ -6563,26 +6555,30 @@ bool CodeGenPrepare::optimizeMulWithOverflow(Instruction *I, bool IsSigned,
6563
6555
StructValNoOverflow, ConstantInt::getFalse (I->getContext ()), {1 });
6564
6556
// Replace all uses of I, only uses dominated by the if.end BB
6565
6557
I->replaceUsesOutsideBlock (StructValNoOverflow, I->getParent ());
6558
+
6559
+ // Remove the original BB as it's divided into 'overflow.entry' and
6560
+ // 'overflow' BBs.
6561
+ BasicBlock *ToBeRemoveBB = I->getParent ();
6566
6562
// BB overflow:
6567
- Builder.SetInsertPoint (OverflowBB,
6568
- I->getParent ()->getTerminator ()->getIterator ());
6563
+ OverflowBB->splice (OverflowBB->end (), ToBeRemoveBB);
6569
6564
// Extract the multiplication result to add it to the PHI node in the if.end
6570
6565
// BB
6566
+ Builder.SetInsertPoint (OverflowBB, OverflowBB->end ());
6571
6567
Value *IntrinsicMulRes = Builder.CreateExtractValue (I, {0 }, " mul.extract" );
6568
+ cast<Instruction>(IntrinsicMulRes)->moveAfter (I);
6572
6569
NoOverflowPHI->addIncoming (IntrinsicMulRes, OverflowBB);
6570
+
6571
+ ToBeRemoveBB->eraseFromParent ();
6572
+ // Restore the original name of the overflow.entry BB:
6573
+ OverflowEntryBB->setName (KeepBBName);
6573
6574
ModifiedDT = ModifyDT::ModifyBBDT;
6574
6575
return true ;
6575
6576
}
6576
6577
6577
6578
// Otherwise, we need to create the 'overflow.res' BB to merge the results of
6578
- // the two paths.
6579
+ // the two paths:
6579
6580
I->getParent ()->setName (" overflow.res" );
6580
6581
BasicBlock *OverflowResBB = I->getParent ();
6581
- OverflowBB = BasicBlock::Create (I->getContext (), " overflow" , I->getFunction (),
6582
- OverflowResBB);
6583
- // Initially I->getParent() was the overflow BB, now it becomes the
6584
- // overflow.res BB. So we need to keep the old reference to the overflow BB.
6585
- OverflowResBB->replaceAllUsesWith (OverflowBB);
6586
6582
6587
6583
// BB overflow.no: jump to overflow.res BB
6588
6584
Builder.CreateBr (OverflowResBB);
@@ -6617,6 +6613,10 @@ bool CodeGenPrepare::optimizeMulWithOverflow(Instruction *I, bool IsSigned,
6617
6613
// Add The Extracted values to the PHINodes in the overflow.res block.
6618
6614
OverflowResPHI->addIncoming (MulOverflow, OverflowBB);
6619
6615
OverflowFlagPHI->addIncoming (OverflowFlag, OverflowBB);
6616
+
6617
+ // Restore the original name of the overflow.entry BB:
6618
+ OverflowEntryBB->setName (KeepBBName);
6619
+
6620
6620
ModifiedDT = ModifyDT::ModifyBBDT;
6621
6621
return true ;
6622
6622
}
0 commit comments