Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 1 | //===------ 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 | |
| 17 | #define DEBUG_TYPE "instsimplify" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Scalar.h" |
Duncan Sands | 8a3d29c | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DepthFirstIterator.h" |
Duncan Sands | dc615e4 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 24 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Function.h" |
| 26 | #include "llvm/IR/Type.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Pass.h" |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetLibraryInfo.h" |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Local.h" |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | STATISTIC(NumSimplified, "Number of redundant instructions removed"); |
| 33 | |
| 34 | namespace { |
| 35 | struct InstSimplifier : public FunctionPass { |
| 36 | static char ID; // Pass identification, replacement for typeid |
| 37 | InstSimplifier() : FunctionPass(ID) { |
| 38 | initializeInstSimplifierPass(*PassRegistry::getPassRegistry()); |
| 39 | } |
| 40 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 41 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 42 | AU.setPreservesCFG(); |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 43 | AU.addRequired<TargetLibraryInfo>(); |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /// runOnFunction - Remove instructions that simplify. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 47 | bool runOnFunction(Function &F) override { |
| 48 | const DominatorTreeWrapperPass *DTWP = |
| 49 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 50 | const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : 0; |
| 51 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 52 | const DataLayout *DL = DLP ? &DLP->getDataLayout() : 0; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 53 | const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>(); |
Duncan Sands | dc615e4 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 54 | SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 55 | bool Changed = false; |
| 56 | |
Duncan Sands | 8a3d29c | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 57 | do { |
Duncan Sands | 8a3d29c | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 58 | for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()), |
| 59 | DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) |
| 60 | for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) { |
| 61 | Instruction *I = BI++; |
Duncan Sands | dc615e4 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 62 | // The first time through the loop ToSimplify is empty and we try to |
| 63 | // simplify all instructions. On later iterations ToSimplify is not |
| 64 | // empty and we only bother simplifying instructions that are in it. |
| 65 | if (!ToSimplify->empty() && !ToSimplify->count(I)) |
| 66 | continue; |
| 67 | // Don't waste time simplifying unused instructions. |
Duncan Sands | 8a3d29c | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 68 | if (!I->use_empty()) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 69 | if (Value *V = SimplifyInstruction(I, DL, TLI, DT)) { |
Duncan Sands | dc615e4 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 70 | // Mark all uses for resimplification next time round the loop. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 71 | for (User *U : I->users()) |
| 72 | Next->insert(cast<Instruction>(U)); |
Duncan Sands | 8a3d29c | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 73 | I->replaceAllUsesWith(V); |
Duncan Sands | 8a3d29c | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 74 | ++NumSimplified; |
Duncan Sands | dc615e4 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 75 | Changed = true; |
Duncan Sands | 8a3d29c | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 76 | } |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 77 | Changed |= RecursivelyDeleteTriviallyDeadInstructions(I, TLI); |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 78 | } |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 79 | |
Duncan Sands | dc615e4 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 80 | // Place the list of instructions to simplify on the next loop iteration |
| 81 | // into ToSimplify. |
| 82 | std::swap(ToSimplify, Next); |
| 83 | Next->clear(); |
| 84 | } while (!ToSimplify->empty()); |
Duncan Sands | 1cd0f463 | 2010-12-21 17:08:55 +0000 | [diff] [blame] | 85 | |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 86 | return Changed; |
| 87 | } |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | char InstSimplifier::ID = 0; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 92 | INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify", |
| 93 | "Remove redundant instructions", false, false) |
| 94 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
| 95 | INITIALIZE_PASS_END(InstSimplifier, "instsimplify", |
| 96 | "Remove redundant instructions", false, false) |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 97 | char &llvm::InstructionSimplifierID = InstSimplifier::ID; |
| 98 | |
| 99 | // Public interface to the simplify instructions pass. |
| 100 | FunctionPass *llvm::createInstructionSimplifierPass() { |
| 101 | return new InstSimplifier(); |
| 102 | } |