1616#include " X86TargetMachine.h"
1717#include " llvm/Analysis/ValueTracking.h"
1818#include " llvm/CodeGen/TargetPassConfig.h"
19+ #include " llvm/IR/Analysis.h"
1920#include " llvm/IR/Constants.h"
2021#include " llvm/IR/IRBuilder.h"
2122#include " llvm/IR/Instructions.h"
2223#include " llvm/IR/IntrinsicsX86.h"
24+ #include " llvm/IR/PassManager.h"
2325#include " llvm/IR/PatternMatch.h"
2426#include " llvm/Pass.h"
2527#include " llvm/Support/KnownBits.h"
@@ -30,39 +32,44 @@ using namespace llvm;
3032
3133namespace {
3234
33- class X86PartialReduction : public FunctionPass {
35+ class X86PartialReduction {
36+ const X86TargetMachine *TM;
3437 const DataLayout *DL = nullptr ;
3538 const X86Subtarget *ST = nullptr ;
3639
40+ public:
41+ X86PartialReduction (const X86TargetMachine *TM) : TM(TM) {}
42+ bool run (Function &F);
43+
44+ private:
45+ bool tryMAddReplacement (Instruction *Op, bool ReduceInOneBB);
46+ bool trySADReplacement (Instruction *Op);
47+ };
48+
49+ class X86PartialReductionLegacy : public FunctionPass {
3750public:
3851 static char ID; // Pass identification, replacement for typeid.
3952
40- X86PartialReduction () : FunctionPass(ID) { }
53+ X86PartialReductionLegacy () : FunctionPass(ID) {}
4154
42- bool runOnFunction (Function &Fn ) override ;
55+ bool runOnFunction (Function &F ) override ;
4356
4457 void getAnalysisUsage (AnalysisUsage &AU) const override {
4558 AU.setPreservesCFG ();
4659 }
4760
48- StringRef getPassName () const override {
49- return " X86 Partial Reduction" ;
50- }
51-
52- private:
53- bool tryMAddReplacement (Instruction *Op, bool ReduceInOneBB);
54- bool trySADReplacement (Instruction *Op);
61+ StringRef getPassName () const override { return " X86 Partial Reduction" ; }
5562};
5663}
5764
58- FunctionPass *llvm::createX86PartialReductionPass () {
59- return new X86PartialReduction ();
65+ FunctionPass *llvm::createX86PartialReductionLegacyPass () {
66+ return new X86PartialReductionLegacy ();
6067}
6168
62- char X86PartialReduction ::ID = 0 ;
69+ char X86PartialReductionLegacy ::ID = 0 ;
6370
64- INITIALIZE_PASS (X86PartialReduction , DEBUG_TYPE,
65- " X86 Partial Reduction " , false , false )
71+ INITIALIZE_PASS (X86PartialReductionLegacy , DEBUG_TYPE, " X86 Partial Reduction " ,
72+ false , false )
6673
6774// This function should be aligned with detectExtMul() in X86ISelLowering.cpp.
6875static bool matchVPDPBUSDPattern(const X86Subtarget *ST, BinaryOperator *Mul,
@@ -494,17 +501,8 @@ static void collectLeaves(Value *Root, SmallVectorImpl<Instruction *> &Leaves) {
494501 }
495502}
496503
497- bool X86PartialReduction::runOnFunction (Function &F) {
498- if (skipFunction (F))
499- return false ;
500-
501- auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
502- if (!TPC)
503- return false ;
504-
505- auto &TM = TPC->getTM <X86TargetMachine>();
506- ST = TM.getSubtargetImpl (F);
507-
504+ bool X86PartialReduction::run (Function &F) {
505+ ST = TM->getSubtargetImpl (F);
508506 DL = &F.getDataLayout ();
509507
510508 bool MadeChange = false ;
@@ -540,3 +538,25 @@ bool X86PartialReduction::runOnFunction(Function &F) {
540538
541539 return MadeChange;
542540}
541+
542+ bool X86PartialReductionLegacy::runOnFunction (Function &F) {
543+ if (skipFunction (F))
544+ return false ;
545+
546+ auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
547+ if (!TPC)
548+ return false ;
549+
550+ return X86PartialReduction (&TPC->getTM <X86TargetMachine>()).run (F);
551+ }
552+
553+ PreservedAnalyses X86PartialReductionPass::run (Function &F,
554+ FunctionAnalysisManager &FAM) {
555+ bool Changed = X86PartialReduction (TM).run (F);
556+ if (!Changed)
557+ return PreservedAnalyses::all ();
558+
559+ PreservedAnalyses PA = PreservedAnalyses::none ();
560+ PA.preserveSet <CFGAnalyses>();
561+ return PA;
562+ }
0 commit comments