blob: 27373427d4f7d8efd43e61b5eb032d1094035700 [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"
Sanjay Patel54656ca2017-02-06 18:26:06 +000023#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
Weiming Zhao45d4cb92015-11-24 18:57:06 +000024#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000026#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Function.h"
28#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Pass.h"
Duncan Sandse7cbb642010-12-21 16:12:03 +000030#include "llvm/Transforms/Utils/Local.h"
Davide Italiano16284df2016-07-07 21:14:36 +000031#include "llvm/Transforms/Scalar.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 Berlin954006f2017-04-26 13:52:16 +000057 if (Value *V =
58 SimplifyInstruction(I, SQ.getWithInstruction(I), ORE)) {
Davide Italiano16284df2016-07-07 21:14:36 +000059 // Mark all uses for resimplification next time round the loop.
60 for (User *U : I->users())
61 Next->insert(cast<Instruction>(U));
62 I->replaceAllUsesWith(V);
63 ++NumSimplified;
64 Changed = true;
65 }
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000066 }
Daniel Berlin954006f2017-04-26 13:52:16 +000067 if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000068 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
69 // instruction, so simply incrementing the iterator does not work.
70 // When instructions get deleted re-iterate instead.
71 BI = BB->begin();
72 BE = BB->end();
73 Changed = true;
Davide Italiano16284df2016-07-07 21:14:36 +000074 }
75 }
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000076 }
Davide Italiano16284df2016-07-07 21:14:36 +000077
78 // 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());
83
84 return Changed;
85}
86
Duncan Sandseaff5002010-12-20 21:07:42 +000087namespace {
88 struct InstSimplifier : public FunctionPass {
89 static char ID; // Pass identification, replacement for typeid
90 InstSimplifier() : FunctionPass(ID) {
91 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
92 }
93
Craig Topper3e4c6972014-03-05 09:10:37 +000094 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sandseaff5002010-12-20 21:07:42 +000095 AU.setPreservesCFG();
Dehao Chen3857f8f2016-09-06 22:17:16 +000096 AU.addRequired<DominatorTreeWrapperPass>();
Daniel Jasperaec2fa32016-12-19 08:22:17 +000097 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000098 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjay Patel54656ca2017-02-06 18:26:06 +000099 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
Duncan Sandseaff5002010-12-20 21:07:42 +0000100 }
101
102 /// runOnFunction - Remove instructions that simplify.
Craig Topper3e4c6972014-03-05 09:10:37 +0000103 bool runOnFunction(Function &F) override {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000104 if (skipFunction(F))
105 return false;
106
Dehao Chen3857f8f2016-09-06 22:17:16 +0000107 const DominatorTree *DT =
108 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000109 const TargetLibraryInfo *TLI =
110 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000111 AssumptionCache *AC =
112 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Sanjay Patel54656ca2017-02-06 18:26:06 +0000113 OptimizationRemarkEmitter *ORE =
114 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
Daniel Berlin954006f2017-04-26 13:52:16 +0000115 const DataLayout &DL = F.getParent()->getDataLayout();
116 const SimplifyQuery SQ(DL, TLI, DT, AC);
117 return runImpl(F, SQ, ORE);
Duncan Sandseaff5002010-12-20 21:07:42 +0000118 }
119 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000120}
Duncan Sandseaff5002010-12-20 21:07:42 +0000121
122char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000123INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
124 "Remove redundant instructions", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000125INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Dehao Chen3857f8f2016-09-06 22:17:16 +0000126INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000127INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Sanjay Patel54656ca2017-02-06 18:26:06 +0000128INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000129INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
130 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +0000131char &llvm::InstructionSimplifierID = InstSimplifier::ID;
132
133// Public interface to the simplify instructions pass.
134FunctionPass *llvm::createInstructionSimplifierPass() {
135 return new InstSimplifier();
136}
Davide Italiano16284df2016-07-07 21:14:36 +0000137
138PreservedAnalyses InstSimplifierPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000139 FunctionAnalysisManager &AM) {
Dehao Chen3857f8f2016-09-06 22:17:16 +0000140 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Davide Italiano16284df2016-07-07 21:14:36 +0000141 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000142 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Sanjay Patel54656ca2017-02-06 18:26:06 +0000143 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
Daniel Berlin954006f2017-04-26 13:52:16 +0000144 const DataLayout &DL = F.getParent()->getDataLayout();
145 const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
146 bool Changed = runImpl(F, SQ, &ORE);
Davide Italiano16284df2016-07-07 21:14:36 +0000147 if (!Changed)
148 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000149
150 PreservedAnalyses PA;
151 PA.preserveSet<CFGAnalyses>();
152 return PA;
Davide Italiano16284df2016-07-07 21:14:36 +0000153}