blob: f6070868de44e28f46aa719a087a33e98d425a8e [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
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000038static bool runImpl(Function &F, const DominatorTree *DT,
Sanjay Patel54656ca2017-02-06 18:26:06 +000039 const TargetLibraryInfo *TLI, AssumptionCache *AC,
40 OptimizationRemarkEmitter *ORE) {
Davide Italiano16284df2016-07-07 21:14:36 +000041 const DataLayout &DL = F.getParent()->getDataLayout();
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000042 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Davide Italiano16284df2016-07-07 21:14:36 +000043 bool Changed = false;
44
45 do {
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000046 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
Davide Italiano16284df2016-07-07 21:14:36 +000047 // Here be subtlety: the iterator must be incremented before the loop
48 // body (not sure why), so a range-for loop won't work here.
49 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
50 Instruction *I = &*BI++;
51 // The first time through the loop ToSimplify is empty and we try to
52 // simplify all instructions. On later iterations ToSimplify is not
53 // empty and we only bother simplifying instructions that are in it.
54 if (!ToSimplify->empty() && !ToSimplify->count(I))
55 continue;
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000056
Davide Italiano16284df2016-07-07 21:14:36 +000057 // Don't waste time simplifying unused instructions.
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000058 if (!I->use_empty()) {
Sanjay Patel54656ca2017-02-06 18:26:06 +000059 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC, ORE)) {
Davide Italiano16284df2016-07-07 21:14:36 +000060 // Mark all uses for resimplification next time round the loop.
61 for (User *U : I->users())
62 Next->insert(cast<Instruction>(U));
63 I->replaceAllUsesWith(V);
64 ++NumSimplified;
65 Changed = true;
66 }
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000067 }
68 if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) {
69 // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
70 // instruction, so simply incrementing the iterator does not work.
71 // When instructions get deleted re-iterate instead.
72 BI = BB->begin();
73 BE = BB->end();
74 Changed = true;
Davide Italiano16284df2016-07-07 21:14:36 +000075 }
76 }
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000077 }
Davide Italiano16284df2016-07-07 21:14:36 +000078
79 // Place the list of instructions to simplify on the next loop iteration
80 // into ToSimplify.
81 std::swap(ToSimplify, Next);
82 Next->clear();
83 } while (!ToSimplify->empty());
84
85 return Changed;
86}
87
Duncan Sandseaff5002010-12-20 21:07:42 +000088namespace {
89 struct InstSimplifier : public FunctionPass {
90 static char ID; // Pass identification, replacement for typeid
91 InstSimplifier() : FunctionPass(ID) {
92 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
93 }
94
Craig Topper3e4c6972014-03-05 09:10:37 +000095 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sandseaff5002010-12-20 21:07:42 +000096 AU.setPreservesCFG();
Dehao Chen3857f8f2016-09-06 22:17:16 +000097 AU.addRequired<DominatorTreeWrapperPass>();
Daniel Jasperaec2fa32016-12-19 08:22:17 +000098 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000099 AU.addRequired<TargetLibraryInfoWrapperPass>();
Sanjay Patel54656ca2017-02-06 18:26:06 +0000100 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
Duncan Sandseaff5002010-12-20 21:07:42 +0000101 }
102
103 /// runOnFunction - Remove instructions that simplify.
Craig Topper3e4c6972014-03-05 09:10:37 +0000104 bool runOnFunction(Function &F) override {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000105 if (skipFunction(F))
106 return false;
107
Dehao Chen3857f8f2016-09-06 22:17:16 +0000108 const DominatorTree *DT =
109 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000110 const TargetLibraryInfo *TLI =
111 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000112 AssumptionCache *AC =
113 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Sanjay Patel54656ca2017-02-06 18:26:06 +0000114 OptimizationRemarkEmitter *ORE =
115 &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
116
117 return runImpl(F, DT, TLI, AC, 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);
144 bool Changed = runImpl(F, &DT, &TLI, &AC, &ORE);
Davide Italiano16284df2016-07-07 21:14:36 +0000145 if (!Changed)
146 return PreservedAnalyses::all();
Chandler Carruthca68a3e2017-01-15 06:32:49 +0000147
148 PreservedAnalyses PA;
149 PA.preserveSet<CFGAnalyses>();
150 return PA;
Davide Italiano16284df2016-07-07 21:14:36 +0000151}