diff options
Diffstat (limited to 'llvm/lib/Transforms')
3 files changed, 19 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp index b7364d2ecdf..18432c03108 100644 --- a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp +++ b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp @@ -20,6 +20,7 @@ #include "llvm/Analysis/GlobalsModRef.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/Dominators.h" #include "llvm/Pass.h" #include "llvm/Transforms/Scalar.h" using namespace llvm; @@ -55,20 +56,23 @@ public: void AggressiveInstCombinerLegacyPass::getAnalysisUsage( AnalysisUsage &AU) const { AU.setPreservesCFG(); + AU.addRequired<DominatorTreeWrapperPass>(); AU.addRequired<TargetLibraryInfoWrapperPass>(); AU.addPreserved<AAResultsWrapperPass>(); AU.addPreserved<BasicAAWrapperPass>(); + AU.addPreserved<DominatorTreeWrapperPass>(); AU.addPreserved<GlobalsAAWrapperPass>(); } bool AggressiveInstCombinerLegacyPass::runOnFunction(Function &F) { + auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); auto &DL = F.getParent()->getDataLayout(); bool MadeIRChange = false; // Handle TruncInst patterns - TruncInstCombine TIC(TLI, DL); + TruncInstCombine TIC(TLI, DL, DT); MadeIRChange |= TIC.run(F); // TODO: add more patterns to handle... @@ -78,12 +82,13 @@ bool AggressiveInstCombinerLegacyPass::runOnFunction(Function &F) { PreservedAnalyses AggressiveInstCombinePass::run(Function &F, FunctionAnalysisManager &AM) { + auto &DT = AM.getResult<DominatorTreeAnalysis>(F); auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); auto &DL = F.getParent()->getDataLayout(); bool MadeIRChange = false; // Handle TruncInst patterns - TruncInstCombine TIC(TLI, DL); + TruncInstCombine TIC(TLI, DL, DT); MadeIRChange |= TIC.run(F); if (!MadeIRChange) // No changes, all analyses are preserved. @@ -101,6 +106,7 @@ char AggressiveInstCombinerLegacyPass::ID = 0; INITIALIZE_PASS_BEGIN(AggressiveInstCombinerLegacyPass, "aggressive-instcombine", "Combine pattern based expressions", false, false) +INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) INITIALIZE_PASS_END(AggressiveInstCombinerLegacyPass, "aggressive-instcombine", "Combine pattern based expressions", false, false) diff --git a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h index 63255dd28f8..d4655cff1b1 100644 --- a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h +++ b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h @@ -45,11 +45,13 @@ using namespace llvm; namespace llvm { class DataLayout; + class DominatorTree; class TargetLibraryInfo; class TruncInstCombine { TargetLibraryInfo &TLI; const DataLayout &DL; + const DominatorTree &DT; /// List of all TruncInst instructions to be processed. SmallVector<TruncInst *, 4> Worklist; @@ -73,8 +75,9 @@ class TruncInstCombine { MapVector<Instruction *, Info> InstInfoMap; public: - TruncInstCombine(TargetLibraryInfo &TLI, const DataLayout &DL) - : TLI(TLI), DL(DL), CurrentTruncInst(nullptr) {} + TruncInstCombine(TargetLibraryInfo &TLI, const DataLayout &DL, + const DominatorTree &DT) + : TLI(TLI), DL(DL), DT(DT), CurrentTruncInst(nullptr) {} /// Perform TruncInst pattern optimization on given function. bool run(Function &F); diff --git a/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp index deb0979265c..0378ea79ef7 100644 --- a/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp +++ b/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp @@ -31,6 +31,7 @@ #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/IR/DataLayout.h" +#include "llvm/IR/Dominators.h" #include "llvm/IR/IRBuilder.h" using namespace llvm; @@ -380,10 +381,14 @@ bool TruncInstCombine::run(Function &F) { bool MadeIRChange = false; // Collect all TruncInst in the function into the Worklist for evaluating. - for (auto &BB : F) + for (auto &BB : F) { + // Ignore unreachable basic block. + if (!DT.isReachableFromEntry(&BB)) + continue; for (auto &I : BB) if (auto *CI = dyn_cast<TruncInst>(&I)) Worklist.push_back(CI); + } // Process all TruncInst in the Worklist, for each instruction: // 1. Check if it dominates an eligible expression dag to be reduced. |