blob: 6074a5f2c21e0a40857b898e71b6b9fc846f9c95 [file] [log] [blame]
Duncan Sands69fdf872010-12-20 21:07:42 +00001//===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
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#define DEBUG_TYPE "instsimplify"
18#include "llvm/Function.h"
19#include "llvm/Pass.h"
20#include "llvm/Type.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/Analysis/Dominators.h"
23#include "llvm/Analysis/InstructionSimplify.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Transforms/Scalar.h"
Duncan Sandse95cc252010-12-21 16:12:03 +000026#include "llvm/Transforms/Utils/Local.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000027using namespace llvm;
28
29STATISTIC(NumSimplified, "Number of redundant instructions removed");
30
31namespace {
32 struct InstSimplifier : public FunctionPass {
33 static char ID; // Pass identification, replacement for typeid
34 InstSimplifier() : FunctionPass(ID) {
35 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
36 }
37
38 void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.setPreservesCFG();
40 }
41
42 /// runOnFunction - Remove instructions that simplify.
43 bool runOnFunction(Function &F) {
Duncan Sands69fdf872010-12-20 21:07:42 +000044 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
45 const DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
Duncan Sandse95cc252010-12-21 16:12:03 +000046 bool Changed = false;
47
48 // Add all interesting instructions to the worklist.
49 std::set<Instruction*> Worklist;
Duncan Sands69fdf872010-12-20 21:07:42 +000050 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
51 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
52 Instruction *I = BI++;
Duncan Sandse95cc252010-12-21 16:12:03 +000053 // Zap any dead instructions.
54 if (isInstructionTriviallyDead(I)) {
Duncan Sands69fdf872010-12-20 21:07:42 +000055 I->eraseFromParent();
56 Changed = true;
Duncan Sandse95cc252010-12-21 16:12:03 +000057 continue;
Duncan Sands69fdf872010-12-20 21:07:42 +000058 }
Duncan Sandse95cc252010-12-21 16:12:03 +000059 // Add all others to the worklist.
60 Worklist.insert(I);
Duncan Sands69fdf872010-12-20 21:07:42 +000061 }
Duncan Sandse95cc252010-12-21 16:12:03 +000062
63 // Simplify everything in the worklist until the cows come home.
64 while (!Worklist.empty()) {
65 Instruction *I = *Worklist.begin();
66 Worklist.erase(Worklist.begin());
67 Value *V = SimplifyInstruction(I, TD, DT);
68 if (!V) continue;
69
70 // This instruction simplifies! Replace it with its simplification and
71 // add all uses to the worklist, since they may now simplify.
72 I->replaceAllUsesWith(V);
73 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
74 UI != UE; ++UI)
75 // In unreachable code an instruction can use itself, in which case
76 // don't add it to the worklist since we are about to erase it.
77 if (*UI != I) Worklist.insert(cast<Instruction>(*UI));
78 if (isInstructionTriviallyDead(I))
79 I->eraseFromParent();
80 ++NumSimplified;
81 Changed = true;
82 }
83
Duncan Sands69fdf872010-12-20 21:07:42 +000084 return Changed;
85 }
86 };
87}
88
89char InstSimplifier::ID = 0;
90INITIALIZE_PASS(InstSimplifier, "instsimplify", "Remove redundant instructions",
91 false, false)
92char &llvm::InstructionSimplifierID = InstSimplifier::ID;
93
94// Public interface to the simplify instructions pass.
95FunctionPass *llvm::createInstructionSimplifierPass() {
96 return new InstSimplifier();
97}