@@ -810,11 +810,15 @@ Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
810
810
if (!SI->getParent ()->hasNPredecessorsOrMore (128 / SI->getNumSuccessors ()))
811
811
CV = SI->getCondition ();
812
812
} else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
813
- if (BI->isConditional () && BI->getCondition ()->hasOneUse ())
813
+ if (BI->isConditional () && BI->getCondition ()->hasOneUse ()) {
814
814
if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition ())) {
815
815
if (ICI->isEquality () && getConstantInt (ICI->getOperand (1 ), DL))
816
816
CV = ICI->getOperand (0 );
817
+ } else if (auto *Trunc = dyn_cast<TruncInst>(BI->getCondition ())) {
818
+ if (Trunc->hasNoUnsignedWrap ())
819
+ CV = Trunc->getOperand (0 );
817
820
}
821
+ }
818
822
819
823
// Unwrap any lossless ptrtoint cast.
820
824
if (CV) {
@@ -840,11 +844,20 @@ BasicBlock *SimplifyCFGOpt::getValueEqualityComparisonCases(
840
844
}
841
845
842
846
BranchInst *BI = cast<BranchInst>(TI);
843
- ICmpInst *ICI = cast<ICmpInst>(BI->getCondition ());
844
- BasicBlock *Succ = BI->getSuccessor (ICI->getPredicate () == ICmpInst::ICMP_NE);
845
- Cases.push_back (ValueEqualityComparisonCase (
846
- getConstantInt (ICI->getOperand (1 ), DL), Succ));
847
- return BI->getSuccessor (ICI->getPredicate () == ICmpInst::ICMP_EQ);
847
+ Value *Cond = BI->getCondition ();
848
+ ICmpInst::Predicate Pred;
849
+ ConstantInt *C;
850
+ if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
851
+ Pred = ICI->getPredicate ();
852
+ C = getConstantInt (ICI->getOperand (1 ), DL);
853
+ } else {
854
+ Pred = ICmpInst::ICMP_NE;
855
+ auto *Trunc = cast<TruncInst>(Cond);
856
+ C = ConstantInt::get (cast<IntegerType>(Trunc->getOperand (0 )->getType ()), 0 );
857
+ }
858
+ BasicBlock *Succ = BI->getSuccessor (Pred == ICmpInst::ICMP_NE);
859
+ Cases.push_back (ValueEqualityComparisonCase (C, Succ));
860
+ return BI->getSuccessor (Pred == ICmpInst::ICMP_EQ);
848
861
}
849
862
850
863
// / Given a vector of bb/value pairs, remove any entries
@@ -1106,7 +1119,10 @@ static void getBranchWeights(Instruction *TI,
1106
1119
// default weight to be the first entry.
1107
1120
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1108
1121
assert (Weights.size () == 2 );
1109
- ICmpInst *ICI = cast<ICmpInst>(BI->getCondition ());
1122
+ auto *ICI = dyn_cast<ICmpInst>(BI->getCondition ());
1123
+ if (!ICI)
1124
+ return ;
1125
+
1110
1126
if (ICI->getPredicate () == ICmpInst::ICMP_EQ)
1111
1127
std::swap (Weights.front (), Weights.back ());
1112
1128
}
0 commit comments