blob: 55a4455b98524fcb6e16c17b8de251c9c285e726 [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Transforms/Scalar.h"
Duncan Sands2c440fa2010-12-31 17:49:05 +000018#include "llvm/ADT/DepthFirstIterator.h"
Duncan Sands697de772011-01-03 10:50:04 +000019#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandseaff5002010-12-20 21:07:42 +000020#include "llvm/ADT/Statistic.h"
Chandler Carruth66b31302015-01-04 12:03:27 +000021#include "llvm/Analysis/AssumptionCache.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"
Chandler Carruth62d42152015-01-15 02:16:27 +000028#include "llvm/Analysis/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
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "instsimplify"
33
Duncan Sandseaff5002010-12-20 21:07:42 +000034STATISTIC(NumSimplified, "Number of redundant instructions removed");
35
36namespace {
37 struct InstSimplifier : public FunctionPass {
38 static char ID; // Pass identification, replacement for typeid
39 InstSimplifier() : FunctionPass(ID) {
40 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
41 }
42
Craig Topper3e4c6972014-03-05 09:10:37 +000043 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sandseaff5002010-12-20 21:07:42 +000044 AU.setPreservesCFG();
Chandler Carruth66b31302015-01-04 12:03:27 +000045 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000046 AU.addRequired<TargetLibraryInfoWrapperPass>();
Duncan Sandseaff5002010-12-20 21:07:42 +000047 }
48
49 /// runOnFunction - Remove instructions that simplify.
Craig Topper3e4c6972014-03-05 09:10:37 +000050 bool runOnFunction(Function &F) override {
Chandler Carruth73523022014-01-13 13:07:17 +000051 const DominatorTreeWrapperPass *DTWP =
52 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000053 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Rafael Espindola93512512014-02-25 17:30:31 +000054 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000055 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
Chandler Carruthb98f63d2015-01-15 10:41:28 +000056 const TargetLibraryInfo *TLI =
57 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruth66b31302015-01-04 12:03:27 +000058 AssumptionCache *AC =
59 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Duncan Sands697de772011-01-03 10:50:04 +000060 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Duncan Sandse7cbb642010-12-21 16:12:03 +000061 bool Changed = false;
62
Duncan Sands2c440fa2010-12-31 17:49:05 +000063 do {
David Blaikieceec2bd2014-04-11 01:50:01 +000064 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
65 // Here be subtlety: the iterator must be incremented before the loop
66 // body (not sure why), so a range-for loop won't work here.
67 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan Sands2c440fa2010-12-31 17:49:05 +000068 Instruction *I = BI++;
Duncan Sands697de772011-01-03 10:50:04 +000069 // The first time through the loop ToSimplify is empty and we try to
70 // simplify all instructions. On later iterations ToSimplify is not
71 // empty and we only bother simplifying instructions that are in it.
72 if (!ToSimplify->empty() && !ToSimplify->count(I))
73 continue;
74 // Don't waste time simplifying unused instructions.
Duncan Sands2c440fa2010-12-31 17:49:05 +000075 if (!I->use_empty())
Chandler Carruth66b31302015-01-04 12:03:27 +000076 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
Duncan Sands697de772011-01-03 10:50:04 +000077 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +000078 for (User *U : I->users())
79 Next->insert(cast<Instruction>(U));
Duncan Sands2c440fa2010-12-31 17:49:05 +000080 I->replaceAllUsesWith(V);
Duncan Sands2c440fa2010-12-31 17:49:05 +000081 ++NumSimplified;
Duncan Sands697de772011-01-03 10:50:04 +000082 Changed = true;
Duncan Sands2c440fa2010-12-31 17:49:05 +000083 }
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +000084 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
85 if (res) {
86 // RecursivelyDeleteTriviallyDeadInstruction can remove
87 // more than one instruction, so simply incrementing the
88 // iterator does not work. When instructions get deleted
89 // re-iterate instead.
90 BI = BB->begin(); BE = BB->end();
91 Changed |= res;
92 }
Duncan Sandseaff5002010-12-20 21:07:42 +000093 }
Duncan Sandse7cbb642010-12-21 16:12:03 +000094
Duncan Sands697de772011-01-03 10:50:04 +000095 // Place the list of instructions to simplify on the next loop iteration
96 // into ToSimplify.
97 std::swap(ToSimplify, Next);
98 Next->clear();
99 } while (!ToSimplify->empty());
Duncan Sands3b8af412010-12-21 17:08:55 +0000100
Duncan Sandseaff5002010-12-20 21:07:42 +0000101 return Changed;
102 }
103 };
104}
105
106char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000107INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
108 "Remove redundant instructions", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +0000109INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000110INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000111INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
112 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +0000113char &llvm::InstructionSimplifierID = InstSimplifier::ID;
114
115// Public interface to the simplify instructions pass.
116FunctionPass *llvm::createInstructionSimplifierPass() {
117 return new InstSimplifier();
118}