blob: edba5d2656ed7bdb28bf8110ba948ce6b7c9db7e [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Transforms/Scalar.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"
Duncan Sandseaff5002010-12-20 21:07:42 +000030using namespace llvm;
31
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "instsimplify"
33
Duncan Sandseaff5002010-12-20 21:07:42 +000034STATISTIC(NumSimplified, "Number of redundant instructions removed");
35
36namespace {
37 struct InstSimplifier : public FunctionPass {
38 static char ID; // Pass identification, replacement for typeid
39 InstSimplifier() : FunctionPass(ID) {
40 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
41 }
42
Craig Topper3e4c6972014-03-05 09:10:37 +000043 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sandseaff5002010-12-20 21:07:42 +000044 AU.setPreservesCFG();
Chandler Carruth66b31302015-01-04 12:03:27 +000045 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000046 AU.addRequired<TargetLibraryInfoWrapperPass>();
Duncan Sandseaff5002010-12-20 21:07:42 +000047 }
48
49 /// runOnFunction - Remove instructions that simplify.
Craig Topper3e4c6972014-03-05 09:10:37 +000050 bool runOnFunction(Function &F) override {
Andrew Kaylor50271f72016-05-03 22:32:30 +000051 if (skipFunction(F))
52 return false;
53
Chandler Carruth73523022014-01-13 13:07:17 +000054 const DominatorTreeWrapperPass *DTWP =
55 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000056 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Mehdi Amini46a43552015-03-04 18:43:29 +000057 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000058 const TargetLibraryInfo *TLI =
59 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruth66b31302015-01-04 12:03:27 +000060 AssumptionCache *AC =
61 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Duncan Sands697de772011-01-03 10:50:04 +000062 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Duncan Sandse7cbb642010-12-21 16:12:03 +000063 bool Changed = false;
64
Duncan Sands2c440fa2010-12-31 17:49:05 +000065 do {
David Blaikieceec2bd2014-04-11 01:50:01 +000066 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
67 // Here be subtlety: the iterator must be incremented before the loop
68 // body (not sure why), so a range-for loop won't work here.
69 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000070 Instruction *I = &*BI++;
Duncan Sands697de772011-01-03 10:50:04 +000071 // The first time through the loop ToSimplify is empty and we try to
72 // simplify all instructions. On later iterations ToSimplify is not
73 // empty and we only bother simplifying instructions that are in it.
74 if (!ToSimplify->empty() && !ToSimplify->count(I))
75 continue;
76 // Don't waste time simplifying unused instructions.
Duncan Sands2c440fa2010-12-31 17:49:05 +000077 if (!I->use_empty())
Mehdi Aminia28d91d2015-03-10 02:37:25 +000078 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
Duncan Sands697de772011-01-03 10:50:04 +000079 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +000080 for (User *U : I->users())
81 Next->insert(cast<Instruction>(U));
Duncan Sands2c440fa2010-12-31 17:49:05 +000082 I->replaceAllUsesWith(V);
Duncan Sands2c440fa2010-12-31 17:49:05 +000083 ++NumSimplified;
Duncan Sands697de772011-01-03 10:50:04 +000084 Changed = true;
Duncan Sands2c440fa2010-12-31 17:49:05 +000085 }
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +000086 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
87 if (res) {
88 // RecursivelyDeleteTriviallyDeadInstruction can remove
89 // more than one instruction, so simply incrementing the
90 // iterator does not work. When instructions get deleted
91 // re-iterate instead.
92 BI = BB->begin(); BE = BB->end();
93 Changed |= res;
94 }
Duncan Sandseaff5002010-12-20 21:07:42 +000095 }
Duncan Sandse7cbb642010-12-21 16:12:03 +000096
Duncan Sands697de772011-01-03 10:50:04 +000097 // Place the list of instructions to simplify on the next loop iteration
98 // into ToSimplify.
99 std::swap(ToSimplify, Next);
100 Next->clear();
101 } while (!ToSimplify->empty());
Duncan Sands3b8af412010-12-21 17:08:55 +0000102
Duncan Sandseaff5002010-12-20 21:07:42 +0000103 return Changed;
104 }
105 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000106}
Duncan Sandseaff5002010-12-20 21:07:42 +0000107
108char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000109INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
110 "Remove redundant instructions", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +0000111INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000112INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000113INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
114 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +0000115char &llvm::InstructionSimplifierID = InstSimplifier::ID;
116
117// Public interface to the simplify instructions pass.
118FunctionPass *llvm::createInstructionSimplifierPass() {
119 return new InstSimplifier();
120}