diff --git a/llvm/lib/Target/AArch64/SVEIntrinsicOpts.cpp b/llvm/lib/Target/AArch64/SVEIntrinsicOpts.cpp index 8e8b12c07bbfda..9911f33371c65e 100644 --- a/llvm/lib/Target/AArch64/SVEIntrinsicOpts.cpp +++ b/llvm/lib/Target/AArch64/SVEIntrinsicOpts.cpp @@ -177,22 +177,50 @@ bool SVEIntrinsicOpts::optimizeConvertFromSVBool(IntrinsicInst *I) { if (isa(I->getArgOperand(0))) return processPhiNode(I); - // If we have a reinterpret intrinsic I of type A which is converting from - // another reinterpret Y of type B, and the source type of Y is A, then we can - // elide away both reinterprets if there are no other users of Y. - auto *Y = isReinterpretToSVBool(I->getArgOperand(0)); - if (!Y) - return false; + SmallVector CandidatesForRemoval; + Value *Cursor = I->getOperand(0), *EarliestReplacement = nullptr; + + const auto *IVTy = cast(I->getType()); + + // Walk the chain of conversions. + while (Cursor) { + // If the type of the cursor has fewer lanes than the final result, zeroing + // must take place, which breaks the equivalence chain. + const auto *CursorVTy = cast(Cursor->getType()); + if (CursorVTy->getElementCount().getKnownMinValue() < + IVTy->getElementCount().getKnownMinValue()) + break; + + // If the cursor has the same type as I, it is a viable replacement. + if (Cursor->getType() == IVTy) + EarliestReplacement = Cursor; - Value *SourceVal = Y->getArgOperand(0); - if (I->getType() != SourceVal->getType()) + auto *IntrinsicCursor = dyn_cast(Cursor); + + // If this is not an SVE conversion intrinsic, this is the end of the chain. + if (!IntrinsicCursor || !(IntrinsicCursor->getIntrinsicID() == + Intrinsic::aarch64_sve_convert_to_svbool || + IntrinsicCursor->getIntrinsicID() == + Intrinsic::aarch64_sve_convert_from_svbool)) + break; + + CandidatesForRemoval.insert(CandidatesForRemoval.begin(), IntrinsicCursor); + Cursor = IntrinsicCursor->getOperand(0); + } + + // If no viable replacement in the conversion chain was found, there is + // nothing to do. + if (!EarliestReplacement) return false; - I->replaceAllUsesWith(SourceVal); + I->replaceAllUsesWith(EarliestReplacement); I->eraseFromParent(); - if (Y->use_empty()) - Y->eraseFromParent(); + while (!CandidatesForRemoval.empty()) { + Instruction *Candidate = CandidatesForRemoval.pop_back_val(); + if (Candidate->use_empty()) + Candidate->eraseFromParent(); + } return true; } diff --git a/llvm/test/CodeGen/AArch64/sve-intrinsic-opts-reinterpret.ll b/llvm/test/CodeGen/AArch64/sve-intrinsic-opts-reinterpret.ll index 47e0ff8f19c7f1..22c61d0565af2f 100644 --- a/llvm/test/CodeGen/AArch64/sve-intrinsic-opts-reinterpret.ll +++ b/llvm/test/CodeGen/AArch64/sve-intrinsic-opts-reinterpret.ll @@ -67,6 +67,62 @@ define @reinterpret_test_d_rev( %a) { ret %2 } +define @reinterpret_test_full_chain( %a) { +; OPT-LABEL: @reinterpret_test_full_chain( +; OPT: ret %a + %1 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv2i1( %a) + %2 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %1) + %3 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv4i1( %2) + %4 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %3) + %5 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv4i1( %4) + %6 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv2i1( %5) + ret %6 +} + +; The last two reinterprets are not necessary, since they are doing the same +; work as the first two. +define @reinterpret_test_partial_chain( %a) { +; OPT-LABEL: @reinterpret_test_partial_chain( +; OPT: %1 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv2i1( %a) +; OPT-NEXT: %2 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %1) +; OPT-NEXT: ret %2 + %1 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv2i1( %a) + %2 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %1) + %3 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv4i1( %2) + %4 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %3) + ret %4 +} + +; The chain cannot be reduced because of the second reinterpret, which causes +; zeroing. +define @reinterpret_test_irreducible_chain( %a) { +; OPT-LABEL: @reinterpret_test_irreducible_chain( +; OPT: %1 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv8i1( %a) +; OPT-NEXT: %2 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %1) +; OPT-NEXT: %3 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv4i1( %2) +; OPT-NEXT: %4 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv8i1( %3) +; OPT-NEXT: ret %4 + %1 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv8i1( %a) + %2 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %1) + %3 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv4i1( %2) + %4 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv8i1( %3) + ret %4 +} + +; Here, the candidate list is larger than the number of instructions that we +; end up removing. +define @reinterpret_test_keep_some_candidates( %a) { +; OPT-LABEL: @reinterpret_test_keep_some_candidates( +; OPT: %1 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv8i1( %a) +; OPT-NEXT: %2 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %1) +; OPT-NEXT: ret %2 + %1 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv8i1( %a) + %2 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %1) + %3 = tail call @llvm.aarch64.sve.convert.to.svbool.nxv4i1( %2) + %4 = tail call @llvm.aarch64.sve.convert.from.svbool.nxv4i1( %3) + ret %4 +} + define @reinterpret_reductions(i32 %cond, %a, %b, %c) { ; OPT-LABEL: reinterpret_reductions ; OPT-NOT: convert