blob: 8c6596a7c63fd2ecef7e5133fe886083901a6c5d [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
Davide Italiano16284df2016-07-07 21:14:36 +000017#include "llvm/Transforms/Utils/SimplifyInstructions.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"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000021#include "llvm/Analysis/AssumptionCache.h"
Duncan Sandseaff5002010-12-20 21:07:42 +000022#include "llvm/Analysis/InstructionSimplify.h"
Adam Nemet0965da22017-10-09 23:19:02 +000023#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Weiming Zhao45d4cb92015-11-24 18:57:06 +000024#include "llvm/Analysis/TargetLibraryInfo.h"
David Blaikie2be39222018-03-21 22:34:23 +000025#include "llvm/Analysis/Utils/Local.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000027#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Function.h"
29#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Pass.h"
David Blaikiea373d182018-03-28 17:44:36 +000031#include "llvm/Transforms/Utils.h"
Duncan Sandseaff5002010-12-20 21:07:42 +000032using namespace llvm;
33
Chandler Carruth964daaa2014-04-22 02:55:47 +000034#define DEBUG_TYPE "instsimplify"
35
Duncan Sandseaff5002010-12-20 21:07:42 +000036STATISTIC(NumSimplified, "Number of redundant instructions removed");
37
Daniel Berlin954006f2017-04-26 13:52:16 +000038static bool runImpl(Function &F, const SimplifyQuery &SQ,
Sanjay Patel54656ca2017-02-06 18:26:06 +000039 OptimizationRemarkEmitter *ORE) {
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000040 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Davide Italiano16284df2016-07-07 21:14:36 +000041 bool Changed = false;
42
43 do {
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000044 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
Davide Italiano16284df2016-07-07 21:14:36 +000045 // Here be subtlety: the iterator must be incremented before the loop
46 // body (not sure why), so a range-for loop won't work here.
47 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
48 Instruction *I = &*BI++;
49 // The first time through the loop ToSimplify is empty and we try to
50 // simplify all instructions. On later iterations ToSimplify is not
51 // empty and we only bother simplifying instructions that are in it.
52 if (!ToSimplify->empty() && !ToSimplify->count(I))
53 continue;
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000054
Davide Italiano16284df2016-07-07 21:14:36 +000055 // Don't waste time simplifying unused instructions.
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000056 if (!I->use_empty()) {
Daniel Berlin4d0fe642017-04-28 19:55:38 +000057 if (Value *V = SimplifyInstruction(I, SQ, ORE)) {
Davide Italiano16284df2016-07-07 21:14:36 +000058 // Mark all uses for resimplification next time round the loop.
59 for (User *U : I->users())
60 Next->insert(cast<Instruction>(U));
61 I->replaceAllUsesWith(V);
62 ++NumSimplified;
63 Changed = true;
64 }
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000065 }
Daniel Berlin954006f2017-04-26 13:52:16 +000066 if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000067 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
68 // instruction, so simply incrementing the iterator does not work.
69 // When instructions get deleted re-iterate instead.
70 BI = BB->begin();
71 BE = BB->end();
72 Changed = true;
Davide Italiano16284df2016-07-07 21:14:36 +000073 }
74 }
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000075 }
Davide Italiano16284df2016-07-07 21:14:36 +000076
77 // Place the list of instructions to simplify on the next loop iteration
78 // into ToSimplify.
79 std::swap(ToSimplify, Next);
80 Next->clear();
81 } while (!ToSimplify->empty());
82
83 return Changed;
84}
85
Duncan Sandseaff5002010-12-20 21:07:42 +000086namespace {
87 struct InstSimplifier : public FunctionPass {
88 static char ID; // Pass identification, replacement for typeid
89 InstSimplifier() : FunctionPass(ID) {
90 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
91 }
92
Craig Topper3e4c6972014-03-05 09:10:37 +000093 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sandseaff5002010-12-20 21:07:42 +000094 AU.setPreservesCFG();
Dehao Chen3857f8f2016-09-06 22:17:16 +000095 AU.addRequired<DominatorTreeWrapperPass>();
Daniel Jasperaec2fa32016-12-19 08:22:17 +000096 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000097 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjay Patel54656ca2017-02-06 18:26:06 +000098 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
Duncan Sandseaff5002010-12-20 21:07:42 +000099 }
100
101 /// runOnFunction - Remove instructions that simplify.
Craig Topper3e4c6972014-03-05 09:10:37 +0000102 bool runOnFunction(Function &F) override {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000103 if (skipFunction(F))
104 return false;
105
Dehao Chen3857f8f2016-09-06 22:17:16 +0000106 const DominatorTree *DT =
107 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000108 const TargetLibraryInfo *TLI =
109 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000110 AssumptionCache *AC =
111 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Sanjay Patel54656ca2017-02-06 18:26:06 +0000112 OptimizationRemarkEmitter *ORE =
113 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
Daniel Berlin954006f2017-04-26 13:52:16 +0000114 const DataLayout &DL = F.getParent()->getDataLayout();
115 const SimplifyQuery SQ(DL, TLI, DT, AC);
116 return runImpl(F, SQ, ORE);
Duncan Sandseaff5002010-12-20 21:07:42 +0000117 }
118 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000119}
Duncan Sandseaff5002010-12-20 21:07:42 +0000120
121char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000122INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
123 "Remove redundant instructions", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000124INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Dehao Chen3857f8f2016-09-06 22:17:16 +0000125INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000126INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Sanjay Patel54656ca2017-02-06 18:26:06 +0000127INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000128INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
129 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +0000130char &llvm::InstructionSimplifierID = InstSimplifier::ID;
131
132// Public interface to the simplify instructions pass.
133FunctionPass *llvm::createInstructionSimplifierPass() {
134 return new InstSimplifier();
135}
Davide Italiano16284df2016-07-07 21:14:36 +0000136
137PreservedAnalyses InstSimplifierPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000138 FunctionAnalysisManager &AM) {
Dehao Chen3857f8f2016-09-06 22:17:16 +0000139 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Davide Italiano16284df2016-07-07 21:14:36 +0000140 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000141 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Sanjay Patel54656ca2017-02-06 18:26:06 +0000142 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
Daniel Berlin954006f2017-04-26 13:52:16 +0000143 const DataLayout &DL = F.getParent()->getDataLayout();
144 const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
145 bool Changed = runImpl(F, SQ, &ORE);
Davide Italiano16284df2016-07-07 21:14:36 +0000146 if (!Changed)
147 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000148
149 PreservedAnalyses PA;
150 PA.preserveSet<CFGAnalyses>();
151 return PA;
Davide Italiano16284df2016-07-07 21:14:36 +0000152}