[PM] Refactor the InstCombiner interface to use an external worklist.
Because in its primary function pass the combiner is run repeatedly over
the same function until doing so produces no changes, it is essentially
to not re-allocate the worklist. However, as a utility, the more common
pattern would be to put a limited set of instructions in the worklist
rather than the entire function body. That is also the more likely
pattern when used by the new pass manager.
The result is a very light weight combiner that does the visiting with
a separable worklist. This can then be wrapped up in a helper function
for users that want a combiner utility, or as I have here it can be
wrapped up in a pass which manages the iterations used when combining an
entire function's instructions.
Hopefully this removes some of the worst of the interface warts that
became apparant with the last patch here. However, there is clearly more
work. I've again left some FIXMEs for the most egregious. The ones that
stick out to me are the exposure of the worklist and IR builder as
public members, and the use of pointers rather than references. However,
fixing these is likely to be much more mechanical and less interesting
so I didn't want to touch them in this patch.
llvm-svn: 226655
diff --git a/llvm/lib/Transforms/InstCombine/InstCombine.h b/llvm/lib/Transforms/InstCombine/InstCombine.h
index 5aaba2c..346dcaa 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombine.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombine.h
@@ -104,32 +104,44 @@
/// of the LLVM pass pipeline.
class LLVM_LIBRARY_VISIBILITY InstCombiner
: public InstVisitor<InstCombiner, Instruction *> {
- AssumptionCache *AC;
- const DataLayout *DL;
- TargetLibraryInfo *TLI;
- DominatorTree *DT;
- LoopInfo *LI;
- bool MadeIRChange;
- bool MinimizeSize;
-
+ // FIXME: These members shouldn't be public.
public:
/// \brief A worklist of the instructions that need to be simplified.
- InstCombineWorklist Worklist;
+ InstCombineWorklist &Worklist;
/// \brief An IRBuilder that automatically inserts new instructions into the
/// worklist.
typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
BuilderTy *Builder;
- InstCombiner() : DL(nullptr), DT(nullptr), LI(nullptr), Builder(nullptr) {
- MinimizeSize = false;
- }
+private:
+ // Mode in which we are running the combiner.
+ const bool MinimizeSize;
+
+ // Required analyses.
+ // FIXME: These can never be null and should be references.
+ AssumptionCache *AC;
+ TargetLibraryInfo *TLI;
+ DominatorTree *DT;
+
+ // Optional analyses. When non-null, these can both be used to do better
+ // combining and will be updated to reflect any changes.
+ const DataLayout *DL;
+ LoopInfo *LI;
+
+ bool MadeIRChange;
public:
- bool run(Function &F, AssumptionCache *AC, const DataLayout *DL,
- TargetLibraryInfo *TLI, DominatorTree *DT, LoopInfo *LI);
+ InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
+ bool MinimizeSize, AssumptionCache *AC, TargetLibraryInfo *TLI,
+ DominatorTree *DT, const DataLayout *DL, LoopInfo *LI)
+ : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
+ AC(AC), TLI(TLI), DT(DT), DL(DL), LI(LI), MadeIRChange(false) {}
- bool DoOneIteration(Function &F, unsigned ItNum);
+ /// \brief Run the combiner over the entire worklist until it is empty.
+ ///
+ /// \returns true if the IR is changed.
+ bool run();
AssumptionCache *getAssumptionCache() const { return AC; }