Duncan Sands | eaff500 | 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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar.h" |
Duncan Sands | 2c440fa | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DepthFirstIterator.h" |
Duncan Sands | 697de77 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AssumptionCache.h" |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Function.h" |
| 26 | #include "llvm/IR/Type.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Pass.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Duncan Sands | e7cbb64 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Local.h" |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "instsimplify" |
| 33 | |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 34 | STATISTIC(NumSimplified, "Number of redundant instructions removed"); |
| 35 | |
| 36 | namespace { |
| 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 Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 43 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 44 | AU.setPreservesCFG(); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 45 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame^] | 46 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /// runOnFunction - Remove instructions that simplify. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 50 | bool runOnFunction(Function &F) override { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 51 | const DominatorTreeWrapperPass *DTWP = |
| 52 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 53 | const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 54 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 55 | const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr; |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame^] | 56 | const TargetLibraryInfo *TLI = |
| 57 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 58 | AssumptionCache *AC = |
| 59 | &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Duncan Sands | 697de77 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 60 | SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; |
Duncan Sands | e7cbb64 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 61 | bool Changed = false; |
| 62 | |
Duncan Sands | 2c440fa | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 63 | do { |
David Blaikie | ceec2bd | 2014-04-11 01:50:01 +0000 | [diff] [blame] | 64 | for (BasicBlock *BB : depth_first(&F.getEntryBlock())) |
| 65 | // Here be subtlety: the iterator must be incremented before the loop |
| 66 | // body (not sure why), so a range-for loop won't work here. |
| 67 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
Duncan Sands | 2c440fa | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 68 | Instruction *I = BI++; |
Duncan Sands | 697de77 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 69 | // The first time through the loop ToSimplify is empty and we try to |
| 70 | // simplify all instructions. On later iterations ToSimplify is not |
| 71 | // empty and we only bother simplifying instructions that are in it. |
| 72 | if (!ToSimplify->empty() && !ToSimplify->count(I)) |
| 73 | continue; |
| 74 | // Don't waste time simplifying unused instructions. |
Duncan Sands | 2c440fa | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 75 | if (!I->use_empty()) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 76 | if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) { |
Duncan Sands | 697de77 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 77 | // Mark all uses for resimplification next time round the loop. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 78 | for (User *U : I->users()) |
| 79 | Next->insert(cast<Instruction>(U)); |
Duncan Sands | 2c440fa | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 80 | I->replaceAllUsesWith(V); |
Duncan Sands | 2c440fa | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 81 | ++NumSimplified; |
Duncan Sands | 697de77 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 82 | Changed = true; |
Duncan Sands | 2c440fa | 2010-12-31 17:49:05 +0000 | [diff] [blame] | 83 | } |
Gerolf Hoflehner | af7a87d | 2014-04-26 05:58:11 +0000 | [diff] [blame] | 84 | bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI); |
| 85 | if (res) { |
| 86 | // RecursivelyDeleteTriviallyDeadInstruction can remove |
| 87 | // more than one instruction, so simply incrementing the |
| 88 | // iterator does not work. When instructions get deleted |
| 89 | // re-iterate instead. |
| 90 | BI = BB->begin(); BE = BB->end(); |
| 91 | Changed |= res; |
| 92 | } |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 93 | } |
Duncan Sands | e7cbb64 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 94 | |
Duncan Sands | 697de77 | 2011-01-03 10:50:04 +0000 | [diff] [blame] | 95 | // Place the list of instructions to simplify on the next loop iteration |
| 96 | // into ToSimplify. |
| 97 | std::swap(ToSimplify, Next); |
| 98 | Next->clear(); |
| 99 | } while (!ToSimplify->empty()); |
Duncan Sands | 3b8af41 | 2010-12-21 17:08:55 +0000 | [diff] [blame] | 100 | |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 101 | return Changed; |
| 102 | } |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | char InstSimplifier::ID = 0; |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 107 | INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify", |
| 108 | "Remove redundant instructions", false, false) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 109 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame^] | 110 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 111 | INITIALIZE_PASS_END(InstSimplifier, "instsimplify", |
| 112 | "Remove redundant instructions", false, false) |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 113 | char &llvm::InstructionSimplifierID = InstSimplifier::ID; |
| 114 | |
| 115 | // Public interface to the simplify instructions pass. |
| 116 | FunctionPass *llvm::createInstructionSimplifierPass() { |
| 117 | return new InstSimplifier(); |
| 118 | } |