blob: 40ab33d7bd8a7a4dae0784410bdbb008fc71462e [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000024#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Function.h"
26#include "llvm/IR/Type.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Pass.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000028#include "llvm/Analysis/TargetLibraryInfo.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 {
Chandler Carruth73523022014-01-13 13:07:17 +000051 const DominatorTreeWrapperPass *DTWP =
52 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000053 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Mehdi Amini46a43552015-03-04 18:43:29 +000054 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000055 const TargetLibraryInfo *TLI =
56 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruth66b31302015-01-04 12:03:27 +000057 AssumptionCache *AC =
58 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
Duncan Sands697de772011-01-03 10:50:04 +000059 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Duncan Sandse7cbb642010-12-21 16:12:03 +000060 bool Changed = false;
61
Duncan Sands2c440fa2010-12-31 17:49:05 +000062 do {
David Blaikieceec2bd2014-04-11 01:50:01 +000063 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
64 // Here be subtlety: the iterator must be incremented before the loop
65 // body (not sure why), so a range-for loop won't work here.
66 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000067 Instruction *I = &*BI++;
Duncan Sands697de772011-01-03 10:50:04 +000068 // The first time through the loop ToSimplify is empty and we try to
69 // simplify all instructions. On later iterations ToSimplify is not
70 // empty and we only bother simplifying instructions that are in it.
71 if (!ToSimplify->empty() && !ToSimplify->count(I))
72 continue;
73 // Don't waste time simplifying unused instructions.
Duncan Sands2c440fa2010-12-31 17:49:05 +000074 if (!I->use_empty())
Mehdi Aminia28d91d2015-03-10 02:37:25 +000075 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
Duncan Sands697de772011-01-03 10:50:04 +000076 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +000077 for (User *U : I->users())
78 Next->insert(cast<Instruction>(U));
Duncan Sands2c440fa2010-12-31 17:49:05 +000079 I->replaceAllUsesWith(V);
Duncan Sands2c440fa2010-12-31 17:49:05 +000080 ++NumSimplified;
Duncan Sands697de772011-01-03 10:50:04 +000081 Changed = true;
Duncan Sands2c440fa2010-12-31 17:49:05 +000082 }
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +000083 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
84 if (res) {
85 // RecursivelyDeleteTriviallyDeadInstruction can remove
86 // more than one instruction, so simply incrementing the
87 // iterator does not work. When instructions get deleted
88 // re-iterate instead.
89 BI = BB->begin(); BE = BB->end();
90 Changed |= res;
91 }
Duncan Sandseaff5002010-12-20 21:07:42 +000092 }
Duncan Sandse7cbb642010-12-21 16:12:03 +000093
Duncan Sands697de772011-01-03 10:50:04 +000094 // Place the list of instructions to simplify on the next loop iteration
95 // into ToSimplify.
96 std::swap(ToSimplify, Next);
97 Next->clear();
98 } while (!ToSimplify->empty());
Duncan Sands3b8af412010-12-21 17:08:55 +000099
Duncan Sandseaff5002010-12-20 21:07:42 +0000100 return Changed;
101 }
102 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000103}
Duncan Sandseaff5002010-12-20 21:07:42 +0000104
105char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000106INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
107 "Remove redundant instructions", false, false)
Chandler Carruth66b31302015-01-04 12:03:27 +0000108INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000109INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000110INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
111 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +0000112char &llvm::InstructionSimplifierID = InstSimplifier::ID;
113
114// Public interface to the simplify instructions pass.
115FunctionPass *llvm::createInstructionSimplifierPass() {
116 return new InstSimplifier();
117}