@@ -1606,15 +1606,58 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
1606
1606
// alignment assumption. Note that this doesn't use salavageKnowledge,
1607
1607
// as we need to create the assumption for the value we replaced the
1608
1608
// load with.
1609
- if (Inst.hasMetadata (LLVMContext::MD_noundef)) {
1610
- if (auto *AlignMD = Inst.getMetadata (LLVMContext::MD_align)) {
1609
+ if (auto *AlignMD = Inst.getMetadata (LLVMContext::MD_align)) {
1610
+ if (Inst.hasMetadata (LLVMContext::MD_noundef) ||
1611
+ programUndefinedIfPoison (&Inst)) {
1611
1612
Inst.setMetadata (LLVMContext::MD_align, nullptr );
1612
- auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand (0 ));
1613
+ auto *B = mdconst::extract<ConstantInt>(AlignMD->getOperand (0 ));
1613
1614
auto KB = computeKnownBits (Op, SQ.DL );
1614
1615
unsigned AlignFromKB = 1 << KB.countMinTrailingZeros ();
1615
- if (AlignFromKB < A->getZExtValue ()) {
1616
- IRBuilder B (&Inst);
1617
- B.CreateAlignmentAssumption (SQ.DL , Op, A);
1616
+ if (AlignFromKB < B->getZExtValue ()) {
1617
+ SetVector<const Value *> WorkList;
1618
+ bool AlignNeeded = false ;
1619
+ for (const User *U : Inst.users ())
1620
+ if (auto *I = dyn_cast<Instruction>(U))
1621
+ WorkList.insert (I);
1622
+
1623
+ for (unsigned I = 0 ; I != WorkList.size (); ++I) {
1624
+ auto *Curr = WorkList[I];
1625
+ if (auto *LI = dyn_cast<LoadInst>(Curr)) {
1626
+ if (LI->getAlign ().value () < B->getZExtValue ()) {
1627
+ AlignNeeded = true ;
1628
+ break ;
1629
+ }
1630
+ continue ;
1631
+ }
1632
+ if (auto *SI = dyn_cast<StoreInst>(Curr)) {
1633
+ if (SI->getAlign ().value () < B->getZExtValue ()) {
1634
+ AlignNeeded = true ;
1635
+ break ;
1636
+ }
1637
+ continue ;
1638
+ }
1639
+ if (isa<ReturnInst>(Curr)) {
1640
+ AlignNeeded = true ;
1641
+ break ;
1642
+ }
1643
+ if (isa<ICmpInst>(Curr) &&
1644
+ !isa<Constant>(cast<Instruction>(Curr)->getOperand (0 )) &&
1645
+ !isa<Constant>(cast<Instruction>(Curr)->getOperand (1 ))) {
1646
+ AlignNeeded = true ;
1647
+ break ;
1648
+ }
1649
+ if (WorkList.size () > 16 ) {
1650
+ AlignNeeded = true ;
1651
+ break ;
1652
+ }
1653
+
1654
+ for (const User *U : Curr->users ())
1655
+ WorkList.insert (cast<Instruction>(U));
1656
+ }
1657
+ if (AlignNeeded) {
1658
+ IRBuilder Builder (&Inst);
1659
+ Builder.CreateAlignmentAssumption (SQ.DL , Op, B);
1660
+ }
1618
1661
}
1619
1662
}
1620
1663
}
0 commit comments