blob: 1220490123ce5384be1bc8dd008ad69d140eac48 [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"
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"
Weiming Zhao45d4cb92015-11-24 18:57:06 +000023#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000025#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Function.h"
27#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Pass.h"
Duncan Sandse7cbb642010-12-21 16:12:03 +000029#include "llvm/Transforms/Utils/Local.h"
Davide Italiano16284df2016-07-07 21:14:36 +000030#include "llvm/Transforms/Scalar.h"
Duncan Sandseaff5002010-12-20 21:07:42 +000031using namespace llvm;
32
Chandler Carruth964daaa2014-04-22 02:55:47 +000033#define DEBUG_TYPE "instsimplify"
34
Duncan Sandseaff5002010-12-20 21:07:42 +000035STATISTIC(NumSimplified, "Number of redundant instructions removed");
36
Sanjay Patelda9f7bf2016-11-27 15:53:48 +000037static bool runImpl(Function &F, const DominatorTree *DT,
38 const TargetLibraryInfo *TLI, AssumptionCache *AC) {
Davide Italiano16284df2016-07-07 21:14:36 +000039 const DataLayout &DL = F.getParent()->getDataLayout();
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()) {
Davide Italiano16284df2016-07-07 21:14:36 +000057 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
58 // 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 }
66 if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) {
67 // 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>();
Chandler Carruth66b31302015-01-04 12:03:27 +000096 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000097 AU.addRequired<TargetLibraryInfoWrapperPass>();
Duncan Sandseaff5002010-12-20 21:07:42 +000098 }
99
100 /// runOnFunction - Remove instructions that simplify.
Craig Topper3e4c6972014-03-05 09:10:37 +0000101 bool runOnFunction(Function &F) override {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000102 if (skipFunction(F))
103 return false;
104
Dehao Chen3857f8f2016-09-06 22:17:16 +0000105 const DominatorTree *DT =
106 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000107 const TargetLibraryInfo *TLI =
108 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruth66b31302015-01-04 12:03:27 +0000109 AssumptionCache *AC =
110 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Davide Italiano16284df2016-07-07 21:14:36 +0000111 return runImpl(F, DT, TLI, AC);
Duncan Sandseaff5002010-12-20 21:07:42 +0000112 }
113 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000114}
Duncan Sandseaff5002010-12-20 21:07:42 +0000115
116char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000117INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
118 "Remove redundant instructions", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +0000119INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Dehao Chen3857f8f2016-09-06 22:17:16 +0000120INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000121INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000122INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
123 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +0000124char &llvm::InstructionSimplifierID = InstSimplifier::ID;
125
126// Public interface to the simplify instructions pass.
127FunctionPass *llvm::createInstructionSimplifierPass() {
128 return new InstSimplifier();
129}
Davide Italiano16284df2016-07-07 21:14:36 +0000130
131PreservedAnalyses InstSimplifierPass::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000132 FunctionAnalysisManager &AM) {
Dehao Chen3857f8f2016-09-06 22:17:16 +0000133 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
Davide Italiano16284df2016-07-07 21:14:36 +0000134 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
135 auto &AC = AM.getResult<AssumptionAnalysis>(F);
Dehao Chen3857f8f2016-09-06 22:17:16 +0000136 bool Changed = runImpl(F, &DT, &TLI, &AC);
Davide Italiano16284df2016-07-07 21:14:36 +0000137 if (!Changed)
138 return PreservedAnalyses::all();
139 // FIXME: This should also 'preserve the CFG'.
140 return PreservedAnalyses::none();
141}