Skip to content

Commit 83ba269

Browse files
committedJan 24, 2015
[PM] Port instcombine to the new pass manager!
This is exciting as this is a much more involved port. This is a complex, existing transformation pass. All of the core logic is shared between both old and new pass managers. Only the access to the analyses is separate because the actual techniques are separate. This also uses a bunch of different and interesting analyses and is the first time where we need to use an analysis across an IR layer. This also paves the way to expose instcombine utility functions. I've got a static function that implements the core pass logic over a function which might be mildly interesting, but more interesting is likely exposing a routine which just uses instructions *already in* the worklist and combines until empty. I've switched one of my favorite instcombine tests to run with both as well to make sure this keeps working. llvm-svn: 226987
1 parent de968ec commit 83ba269

File tree

7 files changed

+125
-38
lines changed

7 files changed

+125
-38
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===- InstCombine.h - InstCombine pass -------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
/// \file
10+
///
11+
/// This file provides the primary interface to the instcombine pass. This pass
12+
/// is suitable for use in the new pass manager. For a pass that works with the
13+
/// legacy pass manager, please look for \c createInstructionCombiningPass() in
14+
/// Scalar.h.
15+
///
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINE_H
19+
#define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINE_H
20+
21+
#include "llvm/IR/Function.h"
22+
#include "llvm/IR/PassManager.h"
23+
#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
24+
25+
namespace llvm {
26+
27+
class InstCombinePass {
28+
InstCombineWorklist Worklist;
29+
30+
public:
31+
static StringRef name() { return "InstCombinePass"; }
32+
33+
// Explicitly define constructors for MSVC.
34+
InstCombinePass() {}
35+
InstCombinePass(InstCombinePass &&Arg) : Worklist(std::move(Arg.Worklist)) {}
36+
InstCombinePass &operator=(InstCombinePass &&RHS) {
37+
Worklist = std::move(RHS.Worklist);
38+
return *this;
39+
}
40+
41+
PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
42+
};
43+
44+
}
45+
46+
#endif

‎llvm/lib/Transforms/InstCombine/InstCombineWorklist.h ‎llvm/include/llvm/Transforms/InstCombine/InstCombineWorklist.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
#ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
11-
#define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
10+
#ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
11+
#define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINEWORKLIST_H
1212

1313
#include "llvm/ADT/DenseMap.h"
1414
#include "llvm/ADT/SmallVector.h"
@@ -32,6 +32,15 @@ class LLVM_LIBRARY_VISIBILITY InstCombineWorklist {
3232
public:
3333
InstCombineWorklist() {}
3434

35+
InstCombineWorklist(InstCombineWorklist &&Arg)
36+
: Worklist(std::move(Arg.Worklist)),
37+
WorklistMap(std::move(Arg.WorklistMap)) {}
38+
InstCombineWorklist &operator=(InstCombineWorklist &&RHS) {
39+
Worklist = std::move(RHS.Worklist);
40+
WorklistMap = std::move(RHS.WorklistMap);
41+
return *this;
42+
}
43+
3544
bool isEmpty() const { return Worklist.empty(); }
3645

3746
/// Add - Add the specified instruction to the worklist if it isn't already

‎llvm/lib/Transforms/InstCombine/InstCombineInternal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
1616
#define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
1717

18-
#include "InstCombineWorklist.h"
1918
#include "llvm/Analysis/AssumptionCache.h"
2019
#include "llvm/Analysis/LoopInfo.h"
2120
#include "llvm/Analysis/TargetFolder.h"
@@ -27,6 +26,7 @@
2726
#include "llvm/IR/Operator.h"
2827
#include "llvm/IR/PatternMatch.h"
2928
#include "llvm/Pass.h"
29+
#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
3030

3131
#define DEBUG_TYPE "instcombine"
3232

‎llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+64-35
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//
3434
//===----------------------------------------------------------------------===//
3535

36-
#include "llvm/Transforms/Scalar.h"
36+
#include "llvm/Transforms/InstCombine/InstCombine.h"
3737
#include "InstCombineInternal.h"
3838
#include "llvm-c/Initialization.h"
3939
#include "llvm/ADT/SmallPtrSet.h"
@@ -45,6 +45,7 @@
4545
#include "llvm/Analysis/InstructionSimplify.h"
4646
#include "llvm/Analysis/LoopInfo.h"
4747
#include "llvm/Analysis/MemoryBuiltins.h"
48+
#include "llvm/Analysis/TargetLibraryInfo.h"
4849
#include "llvm/Analysis/ValueTracking.h"
4950
#include "llvm/IR/CFG.h"
5051
#include "llvm/IR/DataLayout.h"
@@ -55,7 +56,7 @@
5556
#include "llvm/IR/ValueHandle.h"
5657
#include "llvm/Support/CommandLine.h"
5758
#include "llvm/Support/Debug.h"
58-
#include "llvm/Analysis/TargetLibraryInfo.h"
59+
#include "llvm/Transforms/Scalar.h"
5960
#include "llvm/Transforms/Utils/Local.h"
6061
#include <algorithm>
6162
#include <climits>
@@ -2922,6 +2923,66 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout *DL,
29222923
return MadeIRChange;
29232924
}
29242925

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\nINSTCOMBINE 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+
29252986
namespace {
29262987
/// \brief The legacy pass manager's instcombine pass.
29272988
///
@@ -2954,10 +3015,6 @@ bool InstructionCombiningPass::runOnFunction(Function &F) {
29543015
if (skipOptnoneFunction(F))
29553016
return false;
29563017

2957-
// Lower dbg.declare intrinsics otherwise their value may be clobbered
2958-
// by instcombiner.
2959-
bool DbgDeclaresChanged = LowerDbgDeclare(F);
2960-
29613018
// Required analyses.
29623019
auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
29633020
auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
@@ -2969,35 +3026,7 @@ bool InstructionCombiningPass::runOnFunction(Function &F) {
29693026
auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
29703027
auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
29713028

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\nINSTCOMBINE 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);
30013030
}
30023031

30033032
char InstructionCombiningPass::ID = 0;

‎llvm/test/Transforms/InstCombine/load.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -instcombine -S < %s | FileCheck %s
2+
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
23

34
; This test makes sure that these instructions are properly eliminated.
45

‎llvm/tools/opt/PassRegistry.def

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ FUNCTION_ANALYSIS("targetlibinfo", TargetLibraryAnalysis())
6060
#ifndef FUNCTION_PASS
6161
#define FUNCTION_PASS(NAME, CREATE_PASS)
6262
#endif
63+
FUNCTION_PASS("instcombine", InstCombinePass())
6364
FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())
6465
FUNCTION_PASS("no-op-function", NoOpFunctionPass())
6566
FUNCTION_PASS("print", PrintFunctionPass(dbgs()))

‎llvm/tools/opt/Passes.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/IR/PassManager.h"
2626
#include "llvm/IR/Verifier.h"
2727
#include "llvm/Support/Debug.h"
28+
#include "llvm/Transforms/InstCombine/InstCombine.h"
2829

2930
using namespace llvm;
3031

0 commit comments

Comments
 (0)
Please sign in to comment.