blob: df299067094f6788e7a3b589a2fd0376ae3d95a8 [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
Davide Italiano16284df2016-07-07 21:14:36 +000037static bool runImpl(Function &F, const DominatorTree *DT, const TargetLibraryInfo *TLI,
38 AssumptionCache *AC) {
39 const DataLayout &DL = F.getParent()->getDataLayout();
40 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
41 bool Changed = false;
42
43 do {
44 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
45 // 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;
54 // Don't waste time simplifying unused instructions.
55 if (!I->use_empty())
56 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
57 // Mark all uses for resimplification next time round the loop.
58 for (User *U : I->users())
59 Next->insert(cast<Instruction>(U));
60 I->replaceAllUsesWith(V);
61 ++NumSimplified;
62 Changed = true;
63 }
64 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
65 if (res) {
66 // RecursivelyDeleteTriviallyDeadInstruction can remove
67 // more than one instruction, so simply incrementing the
68 // iterator does not work. When instructions get deleted
69 // re-iterate instead.
70 BI = BB->begin(); BE = BB->end();
71 Changed |= res;
72 }
73 }
74
75 // Place the list of instructions to simplify on the next loop iteration
76 // into ToSimplify.
77 std::swap(ToSimplify, Next);
78 Next->clear();
79 } while (!ToSimplify->empty());
80
81 return Changed;
82}
83
Duncan Sandseaff5002010-12-20 21:07:42 +000084namespace {
85 struct InstSimplifier : public FunctionPass {
86 static char ID; // Pass identification, replacement for typeid
87 InstSimplifier() : FunctionPass(ID) {
88 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
89 }
90
Craig Topper3e4c6972014-03-05 09:10:37 +000091 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sandseaff5002010-12-20 21:07:42 +000092 AU.setPreservesCFG();
Chandler Carruth66b31302015-01-04 12:03:27 +000093 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000094 AU.addRequired<TargetLibraryInfoWrapperPass>();
Duncan Sandseaff5002010-12-20 21:07:42 +000095 }
96
97 /// runOnFunction - Remove instructions that simplify.
Craig Topper3e4c6972014-03-05 09:10:37 +000098 bool runOnFunction(Function &F) override {
Andrew Kaylor50271f72016-05-03 22:32:30 +000099 if (skipFunction(F))
100 return false;
101
Chandler Carruth73523022014-01-13 13:07:17 +0000102 const DominatorTreeWrapperPass *DTWP =
103 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +0000104 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000105 const TargetLibraryInfo *TLI =
106 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruth66b31302015-01-04 12:03:27 +0000107 AssumptionCache *AC =
108 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Davide Italiano16284df2016-07-07 21:14:36 +0000109 return runImpl(F, DT, TLI, AC);
Duncan Sandseaff5002010-12-20 21:07:42 +0000110 }
111 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000112}
Duncan Sandseaff5002010-12-20 21:07:42 +0000113
114char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000115INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
116 "Remove redundant instructions", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +0000117INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000118INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000119INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
120 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +0000121char &llvm::InstructionSimplifierID = InstSimplifier::ID;
122
123// Public interface to the simplify instructions pass.
124FunctionPass *llvm::createInstructionSimplifierPass() {
125 return new InstSimplifier();
126}
Davide Italiano16284df2016-07-07 21:14:36 +0000127
128PreservedAnalyses InstSimplifierPass::run(Function &F,
129 AnalysisManager<Function> &AM) {
130 auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
131 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
132 auto &AC = AM.getResult<AssumptionAnalysis>(F);
133 bool Changed = runImpl(F, DT, &TLI, &AC);
134 if (!Changed)
135 return PreservedAnalyses::all();
136 // FIXME: This should also 'preserve the CFG'.
137 return PreservedAnalyses::none();
138}