blob: 54d4e64531453f331340f3aad03b89d19b8c974c [file] [log] [blame]
Duncan Sandseaff5002010-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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Transforms/Scalar.h"
Duncan Sands2c440fa2010-12-31 17:49:05 +000019#include "llvm/ADT/DepthFirstIterator.h"
Duncan Sands697de772011-01-03 10:50:04 +000020#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandseaff5002010-12-20 21:07:42 +000021#include "llvm/ADT/Statistic.h"
Duncan Sandseaff5002010-12-20 21:07:42 +000022#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000024#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Function.h"
26#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Pass.h"
Chad Rosierc24b86f2011-12-01 03:08:23 +000028#include "llvm/Target/TargetLibraryInfo.h"
Duncan Sandse7cbb642010-12-21 16:12:03 +000029#include "llvm/Transforms/Utils/Local.h"
Duncan Sandseaff5002010-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
Craig Topper3e4c6972014-03-05 09:10:37 +000041 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sandseaff5002010-12-20 21:07:42 +000042 AU.setPreservesCFG();
Chad Rosierc24b86f2011-12-01 03:08:23 +000043 AU.addRequired<TargetLibraryInfo>();
Duncan Sandseaff5002010-12-20 21:07:42 +000044 }
45
46 /// runOnFunction - Remove instructions that simplify.
Craig Topper3e4c6972014-03-05 09:10:37 +000047 bool runOnFunction(Function &F) override {
Chandler Carruth73523022014-01-13 13:07:17 +000048 const DominatorTreeWrapperPass *DTWP =
49 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
50 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : 0;
Rafael Espindola93512512014-02-25 17:30:31 +000051 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
52 const DataLayout *DL = DLP ? &DLP->getDataLayout() : 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +000053 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
Duncan Sands697de772011-01-03 10:50:04 +000054 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Duncan Sandse7cbb642010-12-21 16:12:03 +000055 bool Changed = false;
56
Duncan Sands2c440fa2010-12-31 17:49:05 +000057 do {
Duncan Sands2c440fa2010-12-31 17:49:05 +000058 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++;
Duncan Sands697de772011-01-03 10:50:04 +000062 // 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))
66 continue;
67 // Don't waste time simplifying unused instructions.
Duncan Sands2c440fa2010-12-31 17:49:05 +000068 if (!I->use_empty())
Rafael Espindola612886f2014-02-21 01:53:35 +000069 if (Value *V = SimplifyInstruction(I, DL, TLI, DT)) {
Duncan Sands697de772011-01-03 10:50:04 +000070 // Mark all uses for resimplification next time round the loop.
71 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
72 UI != UE; ++UI)
73 Next->insert(cast<Instruction>(*UI));
Duncan Sands2c440fa2010-12-31 17:49:05 +000074 I->replaceAllUsesWith(V);
Duncan Sands2c440fa2010-12-31 17:49:05 +000075 ++NumSimplified;
Duncan Sands697de772011-01-03 10:50:04 +000076 Changed = true;
Duncan Sands2c440fa2010-12-31 17:49:05 +000077 }
Benjamin Kramer8bcc9712012-08-29 15:32:21 +000078 Changed |= RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
Duncan Sandseaff5002010-12-20 21:07:42 +000079 }
Duncan Sandse7cbb642010-12-21 16:12:03 +000080
Duncan Sands697de772011-01-03 10:50:04 +000081 // Place the list of instructions to simplify on the next loop iteration
82 // into ToSimplify.
83 std::swap(ToSimplify, Next);
84 Next->clear();
85 } while (!ToSimplify->empty());
Duncan Sands3b8af412010-12-21 17:08:55 +000086
Duncan Sandseaff5002010-12-20 21:07:42 +000087 return Changed;
88 }
89 };
90}
91
92char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +000093INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
94 "Remove redundant instructions", false, false)
95INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
96INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
97 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +000098char &llvm::InstructionSimplifierID = InstSimplifier::ID;
99
100// Public interface to the simplify instructions pass.
101FunctionPass *llvm::createInstructionSimplifierPass() {
102 return new InstSimplifier();
103}