blob: 81eb9e0f8ae163238c69b3ee9176021d40de472a [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"
Chad Rosier618c1db2011-12-01 03:08:23 +000027#include "llvm/Target/TargetLibraryInfo.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000028#include "llvm/Transforms/Scalar.h"
Duncan Sandse95cc252010-12-21 16:12:03 +000029#include "llvm/Transforms/Utils/Local.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000030using namespace llvm;
31
32STATISTIC(NumSimplified, "Number of redundant instructions removed");
33
34namespace {
35 struct InstSimplifier : public FunctionPass {
36 static char ID; // Pass identification, replacement for typeid
37 InstSimplifier() : FunctionPass(ID) {
38 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
39 }
40
41 void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.setPreservesCFG();
Chad Rosier618c1db2011-12-01 03:08:23 +000043 AU.addRequired<TargetLibraryInfo>();
Duncan Sands69fdf872010-12-20 21:07:42 +000044 }
45
46 /// runOnFunction - Remove instructions that simplify.
47 bool runOnFunction(Function &F) {
Duncan Sands69fdf872010-12-20 21:07:42 +000048 const DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
Duncan Sands8a3d29c2010-12-31 17:49:05 +000049 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
Chad Rosier618c1db2011-12-01 03:08:23 +000050 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
Duncan Sandsdc615e42011-01-03 10:50:04 +000051 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Duncan Sandse95cc252010-12-21 16:12:03 +000052 bool Changed = false;
53
Duncan Sands8a3d29c2010-12-31 17:49:05 +000054 do {
Duncan Sands8a3d29c2010-12-31 17:49:05 +000055 for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
56 DE = df_end(&F.getEntryBlock()); DI != DE; ++DI)
57 for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) {
58 Instruction *I = BI++;
Duncan Sandsdc615e42011-01-03 10:50:04 +000059 // The first time through the loop ToSimplify is empty and we try to
60 // simplify all instructions. On later iterations ToSimplify is not
61 // empty and we only bother simplifying instructions that are in it.
62 if (!ToSimplify->empty() && !ToSimplify->count(I))
63 continue;
64 // Don't waste time simplifying unused instructions.
Duncan Sands8a3d29c2010-12-31 17:49:05 +000065 if (!I->use_empty())
Chad Rosier618c1db2011-12-01 03:08:23 +000066 if (Value *V = SimplifyInstruction(I, TD, TLI, DT)) {
Duncan Sandsdc615e42011-01-03 10:50:04 +000067 // Mark all uses for resimplification next time round the loop.
68 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
69 UI != UE; ++UI)
70 Next->insert(cast<Instruction>(*UI));
Duncan Sands8a3d29c2010-12-31 17:49:05 +000071 I->replaceAllUsesWith(V);
Duncan Sands8a3d29c2010-12-31 17:49:05 +000072 ++NumSimplified;
Duncan Sandsdc615e42011-01-03 10:50:04 +000073 Changed = true;
Duncan Sands8a3d29c2010-12-31 17:49:05 +000074 }
Duncan Sandsdc615e42011-01-03 10:50:04 +000075 Changed |= RecursivelyDeleteTriviallyDeadInstructions(I);
Duncan Sands69fdf872010-12-20 21:07:42 +000076 }
Duncan Sandse95cc252010-12-21 16:12:03 +000077
Duncan Sandsdc615e42011-01-03 10:50:04 +000078 // Place the list of instructions to simplify on the next loop iteration
79 // into ToSimplify.
80 std::swap(ToSimplify, Next);
81 Next->clear();
82 } while (!ToSimplify->empty());
Duncan Sands1cd0f4632010-12-21 17:08:55 +000083
Duncan Sands69fdf872010-12-20 21:07:42 +000084 return Changed;
85 }
86 };
87}
88
89char InstSimplifier::ID = 0;
Chad Rosier618c1db2011-12-01 03:08:23 +000090INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
91 "Remove redundant instructions", false, false)
92INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
93INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
94 "Remove redundant instructions", false, false)
Duncan Sands69fdf872010-12-20 21:07:42 +000095char &llvm::InstructionSimplifierID = InstSimplifier::ID;
96
97// Public interface to the simplify instructions pass.
98FunctionPass *llvm::createInstructionSimplifierPass() {
99 return new InstSimplifier();
100}