blob: 5632095b1249ff26f5522f509f087fdec64a2fca [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"
Hal Finkel60db0582014-09-07 18:57:58 +000021#include "llvm/Analysis/AssumptionTracker.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"
Chad Rosierc24b86f2011-12-01 03:08:23 +000028#include "llvm/Target/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();
Hal Finkel60db0582014-09-07 18:57:58 +000045 AU.addRequired<AssumptionTracker>();
Chad Rosierc24b86f2011-12-01 03:08:23 +000046 AU.addRequired<TargetLibraryInfo>();
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;
Rafael Espindola93512512014-02-25 17:30:31 +000054 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topperf40110f2014-04-25 05:29:35 +000055 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
Chad Rosierc24b86f2011-12-01 03:08:23 +000056 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
Hal Finkel60db0582014-09-07 18:57:58 +000057 AssumptionTracker *AT = &getAnalysis<AssumptionTracker>();
Duncan Sands697de772011-01-03 10:50:04 +000058 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Duncan Sandse7cbb642010-12-21 16:12:03 +000059 bool Changed = false;
60
Duncan Sands2c440fa2010-12-31 17:49:05 +000061 do {
David Blaikieceec2bd2014-04-11 01:50:01 +000062 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
63 // Here be subtlety: the iterator must be incremented before the loop
64 // body (not sure why), so a range-for loop won't work here.
65 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan Sands2c440fa2010-12-31 17:49:05 +000066 Instruction *I = BI++;
Duncan Sands697de772011-01-03 10:50:04 +000067 // The first time through the loop ToSimplify is empty and we try to
68 // simplify all instructions. On later iterations ToSimplify is not
69 // empty and we only bother simplifying instructions that are in it.
70 if (!ToSimplify->empty() && !ToSimplify->count(I))
71 continue;
72 // Don't waste time simplifying unused instructions.
Duncan Sands2c440fa2010-12-31 17:49:05 +000073 if (!I->use_empty())
Hal Finkel60db0582014-09-07 18:57:58 +000074 if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AT)) {
Duncan Sands697de772011-01-03 10:50:04 +000075 // Mark all uses for resimplification next time round the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +000076 for (User *U : I->users())
77 Next->insert(cast<Instruction>(U));
Duncan Sands2c440fa2010-12-31 17:49:05 +000078 I->replaceAllUsesWith(V);
Duncan Sands2c440fa2010-12-31 17:49:05 +000079 ++NumSimplified;
Duncan Sands697de772011-01-03 10:50:04 +000080 Changed = true;
Duncan Sands2c440fa2010-12-31 17:49:05 +000081 }
Gerolf Hoflehneraf7a87d2014-04-26 05:58:11 +000082 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
83 if (res) {
84 // RecursivelyDeleteTriviallyDeadInstruction can remove
85 // more than one instruction, so simply incrementing the
86 // iterator does not work. When instructions get deleted
87 // re-iterate instead.
88 BI = BB->begin(); BE = BB->end();
89 Changed |= res;
90 }
Duncan Sandseaff5002010-12-20 21:07:42 +000091 }
Duncan Sandse7cbb642010-12-21 16:12:03 +000092
Duncan Sands697de772011-01-03 10:50:04 +000093 // Place the list of instructions to simplify on the next loop iteration
94 // into ToSimplify.
95 std::swap(ToSimplify, Next);
96 Next->clear();
97 } while (!ToSimplify->empty());
Duncan Sands3b8af412010-12-21 17:08:55 +000098
Duncan Sandseaff5002010-12-20 21:07:42 +000099 return Changed;
100 }
101 };
102}
103
104char InstSimplifier::ID = 0;
Chad Rosierc24b86f2011-12-01 03:08:23 +0000105INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
106 "Remove redundant instructions", false, false)
Hal Finkel60db0582014-09-07 18:57:58 +0000107INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
Chad Rosierc24b86f2011-12-01 03:08:23 +0000108INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
109INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
110 "Remove redundant instructions", false, false)
Duncan Sandseaff5002010-12-20 21:07:42 +0000111char &llvm::InstructionSimplifierID = InstSimplifier::ID;
112
113// Public interface to the simplify instructions pass.
114FunctionPass *llvm::createInstructionSimplifierPass() {
115 return new InstSimplifier();
116}