33
33
//
34
34
// ===----------------------------------------------------------------------===//
35
35
36
- #include " llvm/Transforms/Scalar .h"
36
+ #include " llvm/Transforms/InstCombine/InstCombine .h"
37
37
#include " InstCombineInternal.h"
38
38
#include " llvm-c/Initialization.h"
39
39
#include " llvm/ADT/SmallPtrSet.h"
45
45
#include " llvm/Analysis/InstructionSimplify.h"
46
46
#include " llvm/Analysis/LoopInfo.h"
47
47
#include " llvm/Analysis/MemoryBuiltins.h"
48
+ #include " llvm/Analysis/TargetLibraryInfo.h"
48
49
#include " llvm/Analysis/ValueTracking.h"
49
50
#include " llvm/IR/CFG.h"
50
51
#include " llvm/IR/DataLayout.h"
55
56
#include " llvm/IR/ValueHandle.h"
56
57
#include " llvm/Support/CommandLine.h"
57
58
#include " llvm/Support/Debug.h"
58
- #include " llvm/Analysis/TargetLibraryInfo .h"
59
+ #include " llvm/Transforms/Scalar .h"
59
60
#include " llvm/Transforms/Utils/Local.h"
60
61
#include < algorithm>
61
62
#include < climits>
@@ -2922,6 +2923,66 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout *DL,
2922
2923
return MadeIRChange;
2923
2924
}
2924
2925
2926
+ static bool combineInstructionsOverFunction (
2927
+ Function &F, InstCombineWorklist &Worklist, AssumptionCache &AC,
2928
+ TargetLibraryInfo &TLI, DominatorTree &DT, const DataLayout *DL = nullptr ,
2929
+ LoopInfo *LI = nullptr ) {
2930
+ // Minimizing size?
2931
+ bool MinimizeSize = F.getAttributes ().hasAttribute (
2932
+ AttributeSet::FunctionIndex, Attribute::MinSize);
2933
+
2934
+ // / Builder - This is an IRBuilder that automatically inserts new
2935
+ // / instructions into the worklist when they are created.
2936
+ IRBuilder<true , TargetFolder, InstCombineIRInserter> Builder (
2937
+ F.getContext (), TargetFolder (DL), InstCombineIRInserter (Worklist, &AC));
2938
+
2939
+ // Lower dbg.declare intrinsics otherwise their value may be clobbered
2940
+ // by instcombiner.
2941
+ bool DbgDeclaresChanged = LowerDbgDeclare (F);
2942
+
2943
+ // Iterate while there is work to do.
2944
+ int Iteration = 0 ;
2945
+ for (;;) {
2946
+ ++Iteration;
2947
+ DEBUG (dbgs () << " \n\n INSTCOMBINE ITERATION #" << Iteration << " on "
2948
+ << F.getName () << " \n " );
2949
+
2950
+ bool Changed = false ;
2951
+ if (prepareICWorklistFromFunction (F, DL, &TLI, Worklist))
2952
+ Changed = true ;
2953
+
2954
+ InstCombiner IC (Worklist, &Builder, MinimizeSize, &AC, &TLI, &DT, DL, LI);
2955
+ if (IC.run ())
2956
+ Changed = true ;
2957
+
2958
+ if (!Changed)
2959
+ break ;
2960
+ }
2961
+
2962
+ return DbgDeclaresChanged || Iteration > 1 ;
2963
+ }
2964
+
2965
+ PreservedAnalyses InstCombinePass::run (Function &F,
2966
+ AnalysisManager<Function> *AM) {
2967
+ auto *DL = F.getParent ()->getDataLayout ();
2968
+
2969
+ auto &AC = AM->getResult <AssumptionAnalysis>(F);
2970
+ auto &DT = AM->getResult <DominatorTreeAnalysis>(F);
2971
+ auto &TLI = AM->getResult <TargetLibraryAnalysis>(F);
2972
+
2973
+ auto *LI = AM->getCachedResult <LoopAnalysis>(F);
2974
+
2975
+ if (!combineInstructionsOverFunction (F, Worklist, AC, TLI, DT, DL, LI))
2976
+ // No changes, all analyses are preserved.
2977
+ return PreservedAnalyses::all ();
2978
+
2979
+ // Mark all the analyses that instcombine updates as preserved.
2980
+ // FIXME: Need a way to preserve CFG analyses here!
2981
+ PreservedAnalyses PA;
2982
+ PA.preserve <DominatorTreeAnalysis>();
2983
+ return PA;
2984
+ }
2985
+
2925
2986
namespace {
2926
2987
// / \brief The legacy pass manager's instcombine pass.
2927
2988
// /
@@ -2954,10 +3015,6 @@ bool InstructionCombiningPass::runOnFunction(Function &F) {
2954
3015
if (skipOptnoneFunction (F))
2955
3016
return false ;
2956
3017
2957
- // Lower dbg.declare intrinsics otherwise their value may be clobbered
2958
- // by instcombiner.
2959
- bool DbgDeclaresChanged = LowerDbgDeclare (F);
2960
-
2961
3018
// Required analyses.
2962
3019
auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache (F);
2963
3020
auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI ();
@@ -2969,35 +3026,7 @@ bool InstructionCombiningPass::runOnFunction(Function &F) {
2969
3026
auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
2970
3027
auto *LI = LIWP ? &LIWP->getLoopInfo () : nullptr ;
2971
3028
2972
- // Minimizing size?
2973
- bool MinimizeSize = F.getAttributes ().hasAttribute (
2974
- AttributeSet::FunctionIndex, Attribute::MinSize);
2975
-
2976
- // / Builder - This is an IRBuilder that automatically inserts new
2977
- // / instructions into the worklist when they are created.
2978
- IRBuilder<true , TargetFolder, InstCombineIRInserter> Builder (
2979
- F.getContext (), TargetFolder (DL), InstCombineIRInserter (Worklist, &AC));
2980
-
2981
- // Iterate while there is work to do.
2982
- int Iteration = 0 ;
2983
- for (;;) {
2984
- ++Iteration;
2985
- DEBUG (dbgs () << " \n\n INSTCOMBINE ITERATION #" << Iteration << " on "
2986
- << F.getName () << " \n " );
2987
-
2988
- bool Changed = false ;
2989
- if (prepareICWorklistFromFunction (F, DL, &TLI, Worklist))
2990
- Changed = true ;
2991
-
2992
- InstCombiner IC (Worklist, &Builder, MinimizeSize, &AC, &TLI, &DT, DL, LI);
2993
- if (IC.run ())
2994
- Changed = true ;
2995
-
2996
- if (!Changed)
2997
- break ;
2998
- }
2999
-
3000
- return DbgDeclaresChanged || Iteration > 1 ;
3029
+ return combineInstructionsOverFunction (F, Worklist, AC, TLI, DT, DL, LI);
3001
3030
}
3002
3031
3003
3032
char InstructionCombiningPass::ID = 0 ;
0 commit comments