blob: ac005f95b33a0073857d1b81e49462288e456c5e [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 Sandsdc615e42011-01-03 10:50:04 +000022#include "llvm/ADT/SmallPtrSet.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000023#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"
Duncan Sandse95cc252010-12-21 16:12:03 +000028#include "llvm/Transforms/Utils/Local.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000029using namespace llvm;
30
31STATISTIC(NumSimplified, "Number of redundant instructions removed");
32
33namespace {
34 struct InstSimplifier : public FunctionPass {
35 static char ID; // Pass identification, replacement for typeid
36 InstSimplifier() : FunctionPass(ID) {
37 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
38 }
39
40 void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.setPreservesCFG();
42 }
43
44 /// runOnFunction - Remove instructions that simplify.
45 bool runOnFunction(Function &F) {
Duncan Sands69fdf872010-12-20 21:07:42 +000046 const DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
Duncan Sands8a3d29c2010-12-31 17:49:05 +000047 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Duncan Sandsdc615e42011-01-03 10:50:04 +000048 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Duncan Sandse95cc252010-12-21 16:12:03 +000049 bool Changed = false;
50
Duncan Sands8a3d29c2010-12-31 17:49:05 +000051 do {
Duncan Sands8a3d29c2010-12-31 17:49:05 +000052 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++;
Duncan Sandsdc615e42011-01-03 10:50:04 +000056 // 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))
60 continue;
61 // Don't waste time simplifying unused instructions.
Duncan Sands8a3d29c2010-12-31 17:49:05 +000062 if (!I->use_empty())
63 if (Value *V = SimplifyInstruction(I, TD, DT)) {
Duncan Sandsdc615e42011-01-03 10:50:04 +000064 // Mark all uses for resimplification next time round the loop.
65 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
66 UI != UE; ++UI)
67 Next->insert(cast<Instruction>(*UI));
Duncan Sands8a3d29c2010-12-31 17:49:05 +000068 I->replaceAllUsesWith(V);
Duncan Sands8a3d29c2010-12-31 17:49:05 +000069 ++NumSimplified;
Duncan Sandsdc615e42011-01-03 10:50:04 +000070 Changed = true;
Duncan Sands8a3d29c2010-12-31 17:49:05 +000071 }
Duncan Sandsdc615e42011-01-03 10:50:04 +000072 Changed |= RecursivelyDeleteTriviallyDeadInstructions(I);
Duncan Sands69fdf872010-12-20 21:07:42 +000073 }
Duncan Sandse95cc252010-12-21 16:12:03 +000074
Duncan Sandsdc615e42011-01-03 10:50:04 +000075 // Place the list of instructions to simplify on the next loop iteration
76 // into ToSimplify.
77 std::swap(ToSimplify, Next);
78 Next->clear();
79 } while (!ToSimplify->empty());
Duncan Sands1cd0f4632010-12-21 17:08:55 +000080
Duncan Sands69fdf872010-12-20 21:07:42 +000081 return Changed;
82 }
83 };
84}
85
86char InstSimplifier::ID = 0;
87INITIALIZE_PASS(InstSimplifier, "instsimplify", "Remove redundant instructions",
88 false, false)
89char &llvm::InstructionSimplifierID = InstSimplifier::ID;
90
91// Public interface to the simplify instructions pass.
92FunctionPass *llvm::createInstructionSimplifierPass() {
93 return new InstSimplifier();
94}