blob: fc5cb7b7be207c0eabcca8552225dfb517967cf1 [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"
Duncan Sands8a3d29c2010-12-31 17:49:05 +000021#include "llvm/ADT/DepthFirstIterator.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000022#include "llvm/ADT/Statistic.h"
23#include "llvm/Analysis/Dominators.h"
24#include "llvm/Analysis/InstructionSimplify.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Transforms/Scalar.h"
Duncan Sandse95cc252010-12-21 16:12:03 +000027#include "llvm/Transforms/Utils/Local.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000028using namespace llvm;
29
30STATISTIC(NumSimplified, "Number of redundant instructions removed");
31
32namespace {
33 struct InstSimplifier : public FunctionPass {
34 static char ID; // Pass identification, replacement for typeid
35 InstSimplifier() : FunctionPass(ID) {
36 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesCFG();
41 }
42
43 /// runOnFunction - Remove instructions that simplify.
44 bool runOnFunction(Function &F) {
Duncan Sands69fdf872010-12-20 21:07:42 +000045 const DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
Duncan Sands8a3d29c2010-12-31 17:49:05 +000046 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Duncan Sandse95cc252010-12-21 16:12:03 +000047 bool Changed = false;
Duncan Sands8a3d29c2010-12-31 17:49:05 +000048 bool LocalChanged;
Duncan Sandse95cc252010-12-21 16:12:03 +000049
Duncan Sands8a3d29c2010-12-31 17:49:05 +000050 do {
51 LocalChanged = false;
52
53 for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
54 DE = df_end(&F.getEntryBlock()); DI != DE; ++DI)
55 for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) {
56 Instruction *I = BI++;
57 // Don't bother simplifying unused instructions.
58 if (!I->use_empty())
59 if (Value *V = SimplifyInstruction(I, TD, DT)) {
60 I->replaceAllUsesWith(V);
61 LocalChanged = true;
62 ++NumSimplified;
63 }
64 LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
Duncan Sands69fdf872010-12-20 21:07:42 +000065 }
Duncan Sandse95cc252010-12-21 16:12:03 +000066
Duncan Sands8a3d29c2010-12-31 17:49:05 +000067 Changed |= LocalChanged;
68 } while (LocalChanged);
Duncan Sands1cd0f4632010-12-21 17:08:55 +000069
Duncan Sands69fdf872010-12-20 21:07:42 +000070 return Changed;
71 }
72 };
73}
74
75char InstSimplifier::ID = 0;
76INITIALIZE_PASS(InstSimplifier, "instsimplify", "Remove redundant instructions",
77 false, false)
78char &llvm::InstructionSimplifierID = InstSimplifier::ID;
79
80// Public interface to the simplify instructions pass.
81FunctionPass *llvm::createInstructionSimplifierPass() {
82 return new InstSimplifier();
83}