blob: 33b36378027d01715456f9b9382e62225e142eac [file] [log] [blame]
Duncan Sands69fdf872010-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 Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/Transforms/Scalar.h"
Duncan Sands8a3d29c2010-12-31 17:49:05 +000018#include "llvm/ADT/DepthFirstIterator.h"
Duncan Sandsdc615e42011-01-03 10:50:04 +000019#include "llvm/ADT/SmallPtrSet.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000020#include "llvm/ADT/Statistic.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000021#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070023#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
25#include "llvm/IR/Type.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Pass.h"
Chad Rosier618c1db2011-12-01 03:08:23 +000027#include "llvm/Target/TargetLibraryInfo.h"
Duncan Sandse95cc252010-12-21 16:12:03 +000028#include "llvm/Transforms/Utils/Local.h"
Duncan Sands69fdf872010-12-20 21:07:42 +000029using namespace llvm;
30
Stephen Hinesdce4a402014-05-29 02:49:00 -070031#define DEBUG_TYPE "instsimplify"
32
Duncan Sands69fdf872010-12-20 21:07:42 +000033STATISTIC(NumSimplified, "Number of redundant instructions removed");
34
35namespace {
36 struct InstSimplifier : public FunctionPass {
37 static char ID; // Pass identification, replacement for typeid
38 InstSimplifier() : FunctionPass(ID) {
39 initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
40 }
41
Stephen Hines36b56882014-04-23 16:57:46 -070042 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sands69fdf872010-12-20 21:07:42 +000043 AU.setPreservesCFG();
Chad Rosier618c1db2011-12-01 03:08:23 +000044 AU.addRequired<TargetLibraryInfo>();
Duncan Sands69fdf872010-12-20 21:07:42 +000045 }
46
47 /// runOnFunction - Remove instructions that simplify.
Stephen Hines36b56882014-04-23 16:57:46 -070048 bool runOnFunction(Function &F) override {
49 const DominatorTreeWrapperPass *DTWP =
50 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Stephen Hinesdce4a402014-05-29 02:49:00 -070051 const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
Stephen Hines36b56882014-04-23 16:57:46 -070052 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Stephen Hinesdce4a402014-05-29 02:49:00 -070053 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
Chad Rosier618c1db2011-12-01 03:08:23 +000054 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
Duncan Sandsdc615e42011-01-03 10:50:04 +000055 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
Duncan Sandse95cc252010-12-21 16:12:03 +000056 bool Changed = false;
57
Duncan Sands8a3d29c2010-12-31 17:49:05 +000058 do {
Stephen Hinesdce4a402014-05-29 02:49:00 -070059 for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
60 // Here be subtlety: the iterator must be incremented before the loop
61 // body (not sure why), so a range-for loop won't work here.
62 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Duncan Sands8a3d29c2010-12-31 17:49:05 +000063 Instruction *I = BI++;
Duncan Sandsdc615e42011-01-03 10:50:04 +000064 // The first time through the loop ToSimplify is empty and we try to
65 // simplify all instructions. On later iterations ToSimplify is not
66 // empty and we only bother simplifying instructions that are in it.
67 if (!ToSimplify->empty() && !ToSimplify->count(I))
68 continue;
69 // Don't waste time simplifying unused instructions.
Duncan Sands8a3d29c2010-12-31 17:49:05 +000070 if (!I->use_empty())
Stephen Hines36b56882014-04-23 16:57:46 -070071 if (Value *V = SimplifyInstruction(I, DL, TLI, DT)) {
Duncan Sandsdc615e42011-01-03 10:50:04 +000072 // Mark all uses for resimplification next time round the loop.
Stephen Hines36b56882014-04-23 16:57:46 -070073 for (User *U : I->users())
74 Next->insert(cast<Instruction>(U));
Duncan Sands8a3d29c2010-12-31 17:49:05 +000075 I->replaceAllUsesWith(V);
Duncan Sands8a3d29c2010-12-31 17:49:05 +000076 ++NumSimplified;
Duncan Sandsdc615e42011-01-03 10:50:04 +000077 Changed = true;
Duncan Sands8a3d29c2010-12-31 17:49:05 +000078 }
Stephen Hinesdce4a402014-05-29 02:49:00 -070079 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
80 if (res) {
81 // RecursivelyDeleteTriviallyDeadInstruction can remove
82 // more than one instruction, so simply incrementing the
83 // iterator does not work. When instructions get deleted
84 // re-iterate instead.
85 BI = BB->begin(); BE = BB->end();
86 Changed |= res;
87 }
Duncan Sands69fdf872010-12-20 21:07:42 +000088 }
Duncan Sandse95cc252010-12-21 16:12:03 +000089
Duncan Sandsdc615e42011-01-03 10:50:04 +000090 // Place the list of instructions to simplify on the next loop iteration
91 // into ToSimplify.
92 std::swap(ToSimplify, Next);
93 Next->clear();
94 } while (!ToSimplify->empty());
Duncan Sands1cd0f4632010-12-21 17:08:55 +000095
Duncan Sands69fdf872010-12-20 21:07:42 +000096 return Changed;
97 }
98 };
99}
100
101char InstSimplifier::ID = 0;
Chad Rosier618c1db2011-12-01 03:08:23 +0000102INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
103 "Remove redundant instructions", false, false)
104INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
105INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
106 "Remove redundant instructions", false, false)
Duncan Sands69fdf872010-12-20 21:07:42 +0000107char &llvm::InstructionSimplifierID = InstSimplifier::ID;
108
109// Public interface to the simplify instructions pass.
110FunctionPass *llvm::createInstructionSimplifierPass() {
111 return new InstSimplifier();
112}