14
14
//
15
15
// ===----------------------------------------------------------------------===//
16
16
17
- #include " llvm/Transforms/Scalar .h"
17
+ #include " llvm/Transforms/Utils/SimplifyInstructions .h"
18
18
#include " llvm/ADT/DepthFirstIterator.h"
19
19
#include " llvm/ADT/SmallPtrSet.h"
20
20
#include " llvm/ADT/Statistic.h"
27
27
#include " llvm/IR/Type.h"
28
28
#include " llvm/Pass.h"
29
29
#include " llvm/Transforms/Utils/Local.h"
30
+ #include " llvm/Transforms/Scalar.h"
30
31
using namespace llvm ;
31
32
32
33
#define DEBUG_TYPE " instsimplify"
33
34
34
35
STATISTIC (NumSimplified, " Number of redundant instructions removed" );
35
36
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
+
36
84
namespace {
37
85
struct InstSimplifier : public FunctionPass {
38
86
static char ID; // Pass identification, replacement for typeid
@@ -54,53 +102,11 @@ namespace {
54
102
const DominatorTreeWrapperPass *DTWP =
55
103
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
56
104
const DominatorTree *DT = DTWP ? &DTWP->getDomTree () : nullptr ;
57
- const DataLayout &DL = F.getParent ()->getDataLayout ();
58
105
const TargetLibraryInfo *TLI =
59
106
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI ();
60
107
AssumptionCache *AC =
61
108
&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);
104
110
}
105
111
};
106
112
}
@@ -118,3 +124,15 @@ char &llvm::InstructionSimplifierID = InstSimplifier::ID;
118
124
FunctionPass *llvm::createInstructionSimplifierPass () {
119
125
return new InstSimplifier ();
120
126
}
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
+ }
0 commit comments