@@ -9070,46 +9070,43 @@ llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {
90709070 return {Intrinsic::not_intrinsic, false };
90719071}
90729072
9073- bool llvm::matchSimpleRecurrence (const PHINode *P, BinaryOperator *&BO,
9074- Value *&Start, Value *&Step) {
9073+ template <typename InstTy>
9074+ static bool matchTwoInputRecurrence (const PHINode *PN, InstTy *&Inst,
9075+ Value *&Init, Value *&OtherOp) {
90759076 // Handle the case of a simple two-predecessor recurrence PHI.
90769077 // There's a lot more that could theoretically be done here, but
90779078 // this is sufficient to catch some interesting cases.
90789079 // TODO: Expand list -- gep, uadd.sat etc.
9079- if (P ->getNumIncomingValues () != 2 )
9080+ if (PN ->getNumIncomingValues () != 2 )
90809081 return false ;
90819082
9082- for (unsigned i = 0 ; i != 2 ; ++i) {
9083- Value *L = P->getIncomingValue (i);
9084- Value *R = P->getIncomingValue (!i);
9085- auto *LU = dyn_cast<BinaryOperator>(L);
9086- if (!LU)
9087- continue ;
9088- Value *LL = LU->getOperand (0 );
9089- Value *LR = LU->getOperand (1 );
9090-
9091- // Find a recurrence.
9092- if (LL == P)
9093- L = LR;
9094- else if (LR == P)
9095- L = LL;
9096- else
9097- continue ; // Check for recurrence with L and R flipped.
9098-
9099- // We have matched a recurrence of the form:
9100- // %iv = [R, %entry], [%iv.next, %backedge]
9101- // %iv.next = binop %iv, L
9102- // OR
9103- // %iv = [R, %entry], [%iv.next, %backedge]
9104- // %iv.next = binop L, %iv
9105- BO = LU;
9106- Start = R;
9107- Step = L;
9108- return true ;
9083+ for (unsigned I = 0 ; I != 2 ; ++I) {
9084+ if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue (I))) {
9085+ Value *LHS = Operation->getOperand (0 );
9086+ Value *RHS = Operation->getOperand (1 );
9087+ if (LHS != PN && RHS != PN)
9088+ continue ;
9089+
9090+ Inst = Operation;
9091+ Init = PN->getIncomingValue (!I);
9092+ OtherOp = (LHS == PN) ? RHS : LHS;
9093+ return true ;
9094+ }
91099095 }
91109096 return false ;
91119097}
91129098
9099+ bool llvm::matchSimpleRecurrence (const PHINode *P, BinaryOperator *&BO,
9100+ Value *&Start, Value *&Step) {
9101+ // We try to match a recurrence of the form:
9102+ // %iv = [Start, %entry], [%iv.next, %backedge]
9103+ // %iv.next = binop %iv, Step
9104+ // Or:
9105+ // %iv = [Start, %entry], [%iv.next, %backedge]
9106+ // %iv.next = binop Step, %iv
9107+ return matchTwoInputRecurrence (P, BO, Start, Step);
9108+ }
9109+
91139110bool llvm::matchSimpleRecurrence (const BinaryOperator *I, PHINode *&P,
91149111 Value *&Start, Value *&Step) {
91159112 BinaryOperator *BO = nullptr ;
@@ -9119,6 +9116,22 @@ bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P,
91199116 return P && matchSimpleRecurrence (P, BO, Start, Step) && BO == I;
91209117}
91219118
9119+ bool llvm::matchSimpleBinaryIntrinsicRecurrence (const IntrinsicInst *I,
9120+ PHINode *&P, Value *&Init,
9121+ Value *&OtherOp) {
9122+ // Binary intrinsics only supported for now.
9123+ if (I->arg_size () != 2 || I->getType () != I->getArgOperand (0 )->getType () ||
9124+ I->getType () != I->getArgOperand (1 )->getType ())
9125+ return false ;
9126+
9127+ IntrinsicInst *II = nullptr ;
9128+ P = dyn_cast<PHINode>(I->getArgOperand (0 ));
9129+ if (!P)
9130+ P = dyn_cast<PHINode>(I->getArgOperand (1 ));
9131+
9132+ return P && matchTwoInputRecurrence (P, II, Init, OtherOp) && II == I;
9133+ }
9134+
91229135// / Return true if "icmp Pred LHS RHS" is always true.
91239136static bool isTruePredicate (CmpInst::Predicate Pred, const Value *LHS,
91249137 const Value *RHS) {
0 commit comments