llvm.org GIT mirror llvm / d0de2d9
[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 Davide Italiano 4 years ago
5 changed file(s) with 96 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
0 //===- SimplifyInstructions.h - Remove redundant instructions ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is a utility pass used for testing the InstructionSimplify analysis.
10 // The analysis is applied to every instruction, and if it simplifies then the
11 // instruction is replaced by the simplification. If you are looking for a pass
12 // that performs serious instruction folding, use the instcombine pass instead.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYINSTRUCTIONS_H
17 #define LLVM_TRANSFORMS_UTILS_SIMPLIFYINSTRUCTIONS_H
18
19 #include "llvm/IR/PassManager.h"
20
21 namespace llvm {
22
23 /// This pass removes redundant instructions.
24 class InstSimplifierPass : public PassInfoMixin {
25 public:
26 PreservedAnalyses run(Function &F, AnalysisManager &AM);
27 };
28 } // end namespace llvm
29
30 #endif // LLVM_TRANSFORMS_UTILS_SIMPLIFYINSTRUCTIONS_H
102102 #include "llvm/Transforms/Utils/LCSSA.h"
103103 #include "llvm/Transforms/Utils/Mem2Reg.h"
104104 #include "llvm/Transforms/Utils/MemorySSA.h"
105 #include "llvm/Transforms/Utils/SimplifyInstructions.h"
105106
106107 #include
107108
131131 FUNCTION_PASS("dse", DSEPass())
132132 FUNCTION_PASS("early-cse", EarlyCSEPass())
133133 FUNCTION_PASS("instcombine", InstCombinePass())
134 FUNCTION_PASS("instsimplify", InstSimplifierPass())
134135 FUNCTION_PASS("invalidate", InvalidateAllAnalysesPass())
135136 FUNCTION_PASS("float2int", Float2IntPass())
136137 FUNCTION_PASS("no-op-function", NoOpFunctionPass())
1313 //
1414 //===----------------------------------------------------------------------===//
1515
16 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Transforms/Utils/SimplifyInstructions.h"
1717 #include "llvm/ADT/DepthFirstIterator.h"
1818 #include "llvm/ADT/SmallPtrSet.h"
1919 #include "llvm/ADT/Statistic.h"
2626 #include "llvm/IR/Type.h"
2727 #include "llvm/Pass.h"
2828 #include "llvm/Transforms/Utils/Local.h"
29 #include "llvm/Transforms/Scalar.h"
2930 using namespace llvm;
3031
3132 #define DEBUG_TYPE "instsimplify"
3233
3334 STATISTIC(NumSimplified, "Number of redundant instructions removed");
35
36 static bool runImpl(Function &F, const DominatorTree *DT, const TargetLibraryInfo *TLI,
37 AssumptionCache *AC) {
38 const DataLayout &DL = F.getParent()->getDataLayout();
39 SmallPtrSet S1, S2, *ToSimplify = &S1, *Next = &S2;
40 bool Changed = false;
41
42 do {
43 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
44 // Here be subtlety: the iterator must be incremented before the loop
45 // body (not sure why), so a range-for loop won't work here.
46 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
47 Instruction *I = &*BI++;
48 // The first time through the loop ToSimplify is empty and we try to
49 // simplify all instructions. On later iterations ToSimplify is not
50 // empty and we only bother simplifying instructions that are in it.
51 if (!ToSimplify->empty() && !ToSimplify->count(I))
52 continue;
53 // Don't waste time simplifying unused instructions.
54 if (!I->use_empty())
55 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
56 // Mark all uses for resimplification next time round the loop.
57 for (User *U : I->users())
58 Next->insert(cast(U));
59 I->replaceAllUsesWith(V);
60 ++NumSimplified;
61 Changed = true;
62 }
63 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
64 if (res) {
65 // RecursivelyDeleteTriviallyDeadInstruction can remove
66 // more than one instruction, so simply incrementing the
67 // iterator does not work. When instructions get deleted
68 // re-iterate instead.
69 BI = BB->begin(); BE = BB->end();
70 Changed |= res;
71 }
72 }
73
74 // Place the list of instructions to simplify on the next loop iteration
75 // into ToSimplify.
76 std::swap(ToSimplify, Next);
77 Next->clear();
78 } while (!ToSimplify->empty());
79
80 return Changed;
81 }
3482
3583 namespace {
3684 struct InstSimplifier : public FunctionPass {
53101 const DominatorTreeWrapperPass *DTWP =
54102 getAnalysisIfAvailable();
55103 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
56 const DataLayout &DL = F.getParent()->getDataLayout();
57104 const TargetLibraryInfo *TLI =
58105 &getAnalysis().getTLI();
59106 AssumptionCache *AC =
60107 &getAnalysis().getAssumptionCache(F);
61 SmallPtrSet S1, S2, *ToSimplify = &S1, *Next = &S2;
62 bool Changed = false;
63
64 do {
65 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
66 // Here be subtlety: the iterator must be incremented before the loop
67 // body (not sure why), so a range-for loop won't work here.
68 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
69 Instruction *I = &*BI++;
70 // The first time through the loop ToSimplify is empty and we try to
71 // simplify all instructions. On later iterations ToSimplify is not
72 // empty and we only bother simplifying instructions that are in it.
73 if (!ToSimplify->empty() && !ToSimplify->count(I))
74 continue;
75 // Don't waste time simplifying unused instructions.
76 if (!I->use_empty())
77 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
78 // Mark all uses for resimplification next time round the loop.
79 for (User *U : I->users())
80 Next->insert(cast(U));
81 I->replaceAllUsesWith(V);
82 ++NumSimplified;
83 Changed = true;
84 }
85 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
86 if (res) {
87 // RecursivelyDeleteTriviallyDeadInstruction can remove
88 // more than one instruction, so simply incrementing the
89 // iterator does not work. When instructions get deleted
90 // re-iterate instead.
91 BI = BB->begin(); BE = BB->end();
92 Changed |= res;
93 }
94 }
95
96 // Place the list of instructions to simplify on the next loop iteration
97 // into ToSimplify.
98 std::swap(ToSimplify, Next);
99 Next->clear();
100 } while (!ToSimplify->empty());
101
102 return Changed;
108 return runImpl(F, DT, TLI, AC);
103109 }
104110 };
105111 }
117123 FunctionPass *llvm::createInstructionSimplifierPass() {
118124 return new InstSimplifier();
119125 }
126
127 PreservedAnalyses InstSimplifierPass::run(Function &F,
128 AnalysisManager &AM) {
129 auto *DT = AM.getCachedResult(F);
130 auto &TLI = AM.getResult(F);
131 auto &AC = AM.getResult(F);
132 bool Changed = runImpl(F, DT, &TLI, &AC);
133 if (!Changed)
134 return PreservedAnalyses::all();
135 // FIXME: This should also 'preserve the CFG'.
136 return PreservedAnalyses::none();
137 }
0 ; RUN: opt < %s -instsimplify -S | FileCheck %s
1 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
12
23 declare {i8, i1} @llvm.uadd.with.overflow.i8(i8 %a, i8 %b)
34 declare {i8, i1} @llvm.usub.with.overflow.i8(i8 %a, i8 %b)