Skip to content

Commit d0de2d9

Browse files
committed
[PM] Port InstSimplify to the new pass manager.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274796 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6a6fa34 commit d0de2d9

File tree

5 files changed

+96
-44
lines changed

5 files changed

+96
-44
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- SimplifyInstructions.h - Remove redundant instructions ---*- 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+
//
10+
// This is a utility pass used for testing the InstructionSimplify analysis.
11+
// The analysis is applied to every instruction, and if it simplifies then the
12+
// instruction is replaced by the simplification. If you are looking for a pass
13+
// that performs serious instruction folding, use the instcombine pass instead.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYINSTRUCTIONS_H
18+
#define LLVM_TRANSFORMS_UTILS_SIMPLIFYINSTRUCTIONS_H
19+
20+
#include "llvm/IR/PassManager.h"
21+
22+
namespace llvm {
23+
24+
/// This pass removes redundant instructions.
25+
class InstSimplifierPass : public PassInfoMixin<InstSimplifierPass> {
26+
public:
27+
PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
28+
};
29+
} // end namespace llvm
30+
31+
#endif // LLVM_TRANSFORMS_UTILS_SIMPLIFYINSTRUCTIONS_H

lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
#include "llvm/Transforms/Utils/LCSSA.h"
104104
#include "llvm/Transforms/Utils/Mem2Reg.h"
105105
#include "llvm/Transforms/Utils/MemorySSA.h"
106+
#include "llvm/Transforms/Utils/SimplifyInstructions.h"
106107

107108
#include <type_traits>
108109

lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ FUNCTION_PASS("dce", DCEPass())
132132
FUNCTION_PASS("dse", DSEPass())
133133
FUNCTION_PASS("early-cse", EarlyCSEPass())
134134
FUNCTION_PASS("instcombine", InstCombinePass())
135+
FUNCTION_PASS("instsimplify", InstSimplifierPass())
135136
FUNCTION_PASS("invalidate<all>", InvalidateAllAnalysesPass())
136137
FUNCTION_PASS("float2int", Float2IntPass())
137138
FUNCTION_PASS("no-op-function", NoOpFunctionPass())

lib/Transforms/Utils/SimplifyInstructions.cpp

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "llvm/Transforms/Scalar.h"
17+
#include "llvm/Transforms/Utils/SimplifyInstructions.h"
1818
#include "llvm/ADT/DepthFirstIterator.h"
1919
#include "llvm/ADT/SmallPtrSet.h"
2020
#include "llvm/ADT/Statistic.h"
@@ -27,12 +27,60 @@
2727
#include "llvm/IR/Type.h"
2828
#include "llvm/Pass.h"
2929
#include "llvm/Transforms/Utils/Local.h"
30+
#include "llvm/Transforms/Scalar.h"
3031
using namespace llvm;
3132

3233
#define DEBUG_TYPE "instsimplify"
3334

3435
STATISTIC(NumSimplified, "Number of redundant instructions removed");
3536

37+
static bool runImpl(Function &F, const DominatorTree *DT, const TargetLibraryInfo *TLI,
38+
AssumptionCache *AC) {
39+
const DataLayout &DL = F.getParent()->getDataLayout();
40+
SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
41+
bool Changed = false;
42+
43+
do {
44+
for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
45+
// Here be subtlety: the iterator must be incremented before the loop
46+
// body (not sure why), so a range-for loop won't work here.
47+
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
48+
Instruction *I = &*BI++;
49+
// The first time through the loop ToSimplify is empty and we try to
50+
// simplify all instructions. On later iterations ToSimplify is not
51+
// empty and we only bother simplifying instructions that are in it.
52+
if (!ToSimplify->empty() && !ToSimplify->count(I))
53+
continue;
54+
// Don't waste time simplifying unused instructions.
55+
if (!I->use_empty())
56+
if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
57+
// Mark all uses for resimplification next time round the loop.
58+
for (User *U : I->users())
59+
Next->insert(cast<Instruction>(U));
60+
I->replaceAllUsesWith(V);
61+
++NumSimplified;
62+
Changed = true;
63+
}
64+
bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
65+
if (res) {
66+
// RecursivelyDeleteTriviallyDeadInstruction can remove
67+
// more than one instruction, so simply incrementing the
68+
// iterator does not work. When instructions get deleted
69+
// re-iterate instead.
70+
BI = BB->begin(); BE = BB->end();
71+
Changed |= res;
72+
}
73+
}
74+
75+
// Place the list of instructions to simplify on the next loop iteration
76+
// into ToSimplify.
77+
std::swap(ToSimplify, Next);
78+
Next->clear();
79+
} while (!ToSimplify->empty());
80+
81+
return Changed;
82+
}
83+
3684
namespace {
3785
struct InstSimplifier : public FunctionPass {
3886
static char ID; // Pass identification, replacement for typeid
@@ -54,53 +102,11 @@ namespace {
54102
const DominatorTreeWrapperPass *DTWP =
55103
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
56104
const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
57-
const DataLayout &DL = F.getParent()->getDataLayout();
58105
const TargetLibraryInfo *TLI =
59106
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
60107
AssumptionCache *AC =
61108
&getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
62-
SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
63-
bool Changed = false;
64-
65-
do {
66-
for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
67-
// Here be subtlety: the iterator must be incremented before the loop
68-
// body (not sure why), so a range-for loop won't work here.
69-
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
70-
Instruction *I = &*BI++;
71-
// The first time through the loop ToSimplify is empty and we try to
72-
// simplify all instructions. On later iterations ToSimplify is not
73-
// empty and we only bother simplifying instructions that are in it.
74-
if (!ToSimplify->empty() && !ToSimplify->count(I))
75-
continue;
76-
// Don't waste time simplifying unused instructions.
77-
if (!I->use_empty())
78-
if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
79-
// Mark all uses for resimplification next time round the loop.
80-
for (User *U : I->users())
81-
Next->insert(cast<Instruction>(U));
82-
I->replaceAllUsesWith(V);
83-
++NumSimplified;
84-
Changed = true;
85-
}
86-
bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
87-
if (res) {
88-
// RecursivelyDeleteTriviallyDeadInstruction can remove
89-
// more than one instruction, so simply incrementing the
90-
// iterator does not work. When instructions get deleted
91-
// re-iterate instead.
92-
BI = BB->begin(); BE = BB->end();
93-
Changed |= res;
94-
}
95-
}
96-
97-
// Place the list of instructions to simplify on the next loop iteration
98-
// into ToSimplify.
99-
std::swap(ToSimplify, Next);
100-
Next->clear();
101-
} while (!ToSimplify->empty());
102-
103-
return Changed;
109+
return runImpl(F, DT, TLI, AC);
104110
}
105111
};
106112
}
@@ -118,3 +124,15 @@ char &llvm::InstructionSimplifierID = InstSimplifier::ID;
118124
FunctionPass *llvm::createInstructionSimplifierPass() {
119125
return new InstSimplifier();
120126
}
127+
128+
PreservedAnalyses InstSimplifierPass::run(Function &F,
129+
AnalysisManager<Function> &AM) {
130+
auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
131+
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
132+
auto &AC = AM.getResult<AssumptionAnalysis>(F);
133+
bool Changed = runImpl(F, DT, &TLI, &AC);
134+
if (!Changed)
135+
return PreservedAnalyses::all();
136+
// FIXME: This should also 'preserve the CFG'.
137+
return PreservedAnalyses::none();
138+
}

test/Transforms/InstSimplify/call.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt < %s -instsimplify -S | FileCheck %s
2+
; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
23

34
declare {i8, i1} @llvm.uadd.with.overflow.i8(i8 %a, i8 %b)
45
declare {i8, i1} @llvm.usub.with.overflow.i8(i8 %a, i8 %b)

0 commit comments

Comments
 (0)