1 //===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "instsimplify"
18 #include "llvm/Function.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Type.h"
21 #include "llvm/ADT/DepthFirstIterator.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/Dominators.h"
25 #include "llvm/Analysis/InstructionSimplify.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "llvm/Transforms/Utils/Local.h"
31 STATISTIC(NumSimplified
, "Number of redundant instructions removed");
34 struct InstSimplifier
: public FunctionPass
{
35 static char ID
; // Pass identification, replacement for typeid
36 InstSimplifier() : FunctionPass(ID
) {
37 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
40 void getAnalysisUsage(AnalysisUsage
&AU
) const {
44 /// runOnFunction - Remove instructions that simplify.
45 bool runOnFunction(Function
&F
) {
46 const DominatorTree
*DT
= getAnalysisIfAvailable
<DominatorTree
>();
47 const TargetData
*TD
= getAnalysisIfAvailable
<TargetData
>();
48 SmallPtrSet
<const Instruction
*, 8> S1
, S2
, *ToSimplify
= &S1
, *Next
= &S2
;
52 for (df_iterator
<BasicBlock
*> DI
= df_begin(&F
.getEntryBlock()),
53 DE
= df_end(&F
.getEntryBlock()); DI
!= DE
; ++DI
)
54 for (BasicBlock::iterator BI
= DI
->begin(), BE
= DI
->end(); BI
!= BE
;) {
55 Instruction
*I
= BI
++;
56 // The first time through the loop ToSimplify is empty and we try to
57 // simplify all instructions. On later iterations ToSimplify is not
58 // empty and we only bother simplifying instructions that are in it.
59 if (!ToSimplify
->empty() && !ToSimplify
->count(I
))
61 // Don't waste time simplifying unused instructions.
63 if (Value
*V
= SimplifyInstruction(I
, TD
, DT
)) {
64 // Mark all uses for resimplification next time round the loop.
65 for (Value::use_iterator UI
= I
->use_begin(), UE
= I
->use_end();
67 Next
->insert(cast
<Instruction
>(*UI
));
68 I
->replaceAllUsesWith(V
);
72 Changed
|= RecursivelyDeleteTriviallyDeadInstructions(I
);
75 // Place the list of instructions to simplify on the next loop iteration
77 std::swap(ToSimplify
, Next
);
79 } while (!ToSimplify
->empty());
86 char InstSimplifier::ID
= 0;
87 INITIALIZE_PASS(InstSimplifier
, "instsimplify", "Remove redundant instructions",
89 char &llvm::InstructionSimplifierID
= InstSimplifier::ID
;
91 // Public interface to the simplify instructions pass.
92 FunctionPass
*llvm::createInstructionSimplifierPass() {
93 return new InstSimplifier();