//===-- X86PartialReduction.cpp -------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This pass looks for add instructions used by a horizontal reduction to see // if we might be able to use pmaddwd or psadbw. Some cases of this require // cross basic block knowledge and can't be done in SelectionDAG. // //===----------------------------------------------------------------------===// #include "X86.h" #include "X86TargetMachine.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/IR/Constants.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/IntrinsicsX86.h" #include "llvm/IR/Operator.h" #include "llvm/IR/PatternMatch.h" #include "llvm/Pass.h" #include "llvm/Support/KnownBits.h" usingnamespacellvm; #define DEBUG_TYPE … namespace { class X86PartialReduction : public FunctionPass { … }; } FunctionPass *llvm::createX86PartialReductionPass() { … } char X86PartialReduction::ID = …; INITIALIZE_PASS(…) // This function should be aligned with detectExtMul() in X86ISelLowering.cpp. static bool matchVPDPBUSDPattern(const X86Subtarget *ST, BinaryOperator *Mul, const DataLayout *DL) { … } bool X86PartialReduction::tryMAddReplacement(Instruction *Op, bool ReduceInOneBB) { … } bool X86PartialReduction::trySADReplacement(Instruction *Op) { … } // Walk backwards from the ExtractElementInst and determine if it is the end of // a horizontal reduction. Return the input to the reduction if we find one. static Value *matchAddReduction(const ExtractElementInst &EE, bool &ReduceInOneBB) { … } // See if this BO is reachable from this Phi by walking forward through single // use BinaryOperators with the same opcode. If we get back then we know we've // found a loop and it is safe to step through this Add to find more leaves. static bool isReachableFromPHI(PHINode *Phi, BinaryOperator *BO) { … } // Collect all the leaves of the tree of adds that feeds into the horizontal // reduction. Root is the Value that is used by the horizontal reduction. // We look through single use phis, single use adds, or adds that are used by // a phi that forms a loop with the add. static void collectLeaves(Value *Root, SmallVectorImpl<Instruction *> &Leaves) { … } bool X86PartialReduction::runOnFunction(Function &F) { … }