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/Transforms/Scalar.h"
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/InstructionSimplify.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Target/TargetLibraryInfo.h"
29 #include "llvm/Transforms/Utils/Local.h"
32 STATISTIC(NumSimplified, "Number of redundant instructions removed");
35 struct InstSimplifier : public FunctionPass {
36 static char ID; // Pass identification, replacement for typeid
37 InstSimplifier() : FunctionPass(ID) {
38 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
41 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.addRequired<TargetLibraryInfo>();
46 /// runOnFunction - Remove instructions that simplify.
47 bool runOnFunction(Function &F) override {
48 const DominatorTreeWrapperPass *DTWP =
49 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
50 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : 0;
51 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
52 const DataLayout *DL = DLP ? &DLP->getDataLayout() : 0;
53 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
54 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
58 for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
59 DE = df_end(&F.getEntryBlock()); DI != DE; ++DI)
60 for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) {
61 Instruction *I = BI++;
62 // The first time through the loop ToSimplify is empty and we try to
63 // simplify all instructions. On later iterations ToSimplify is not
64 // empty and we only bother simplifying instructions that are in it.
65 if (!ToSimplify->empty() && !ToSimplify->count(I))
67 // Don't waste time simplifying unused instructions.
69 if (Value *V = SimplifyInstruction(I, DL, TLI, DT)) {
70 // Mark all uses for resimplification next time round the loop.
71 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
73 Next->insert(cast<Instruction>(*UI));
74 I->replaceAllUsesWith(V);
78 Changed |= RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
81 // Place the list of instructions to simplify on the next loop iteration
83 std::swap(ToSimplify, Next);
85 } while (!ToSimplify->empty());
92 char InstSimplifier::ID = 0;
93 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
94 "Remove redundant instructions", false, false)
95 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
96 INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
97 "Remove redundant instructions", false, false)
98 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
100 // Public interface to the simplify instructions pass.
101 FunctionPass *llvm::createInstructionSimplifierPass() {
102 return new InstSimplifier();