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 | |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Utils/SimplifyInstructions.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" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +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" |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame^] | 23 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Weiming Zhao | 45d4cb9 | 2015-11-24 18:57:06 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Function.h" |
| 28 | #include "llvm/IR/Type.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Duncan Sands | e7cbb64 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/Local.h" |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Scalar.h" |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "instsimplify" |
| 35 | |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 36 | STATISTIC(NumSimplified, "Number of redundant instructions removed"); |
| 37 | |
Sanjay Patel | da9f7bf | 2016-11-27 15:53:48 +0000 | [diff] [blame] | 38 | static bool runImpl(Function &F, const DominatorTree *DT, |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame^] | 39 | const TargetLibraryInfo *TLI, AssumptionCache *AC, |
| 40 | OptimizationRemarkEmitter *ORE) { |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 41 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Sanjay Patel | da9f7bf | 2016-11-27 15:53:48 +0000 | [diff] [blame] | 42 | SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 43 | bool Changed = false; |
| 44 | |
| 45 | do { |
Sanjay Patel | da9f7bf | 2016-11-27 15:53:48 +0000 | [diff] [blame] | 46 | for (BasicBlock *BB : depth_first(&F.getEntryBlock())) { |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 47 | // Here be subtlety: the iterator must be incremented before the loop |
| 48 | // body (not sure why), so a range-for loop won't work here. |
| 49 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
| 50 | Instruction *I = &*BI++; |
| 51 | // The first time through the loop ToSimplify is empty and we try to |
| 52 | // simplify all instructions. On later iterations ToSimplify is not |
| 53 | // empty and we only bother simplifying instructions that are in it. |
| 54 | if (!ToSimplify->empty() && !ToSimplify->count(I)) |
| 55 | continue; |
Sanjay Patel | da9f7bf | 2016-11-27 15:53:48 +0000 | [diff] [blame] | 56 | |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 57 | // Don't waste time simplifying unused instructions. |
Sanjay Patel | da9f7bf | 2016-11-27 15:53:48 +0000 | [diff] [blame] | 58 | if (!I->use_empty()) { |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame^] | 59 | if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC, ORE)) { |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 60 | // Mark all uses for resimplification next time round the loop. |
| 61 | for (User *U : I->users()) |
| 62 | Next->insert(cast<Instruction>(U)); |
| 63 | I->replaceAllUsesWith(V); |
| 64 | ++NumSimplified; |
| 65 | Changed = true; |
| 66 | } |
Sanjay Patel | da9f7bf | 2016-11-27 15:53:48 +0000 | [diff] [blame] | 67 | } |
| 68 | if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) { |
| 69 | // RecursivelyDeleteTriviallyDeadInstruction can remove more than one |
| 70 | // instruction, so simply incrementing the iterator does not work. |
| 71 | // When instructions get deleted re-iterate instead. |
| 72 | BI = BB->begin(); |
| 73 | BE = BB->end(); |
| 74 | Changed = true; |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
Sanjay Patel | da9f7bf | 2016-11-27 15:53:48 +0000 | [diff] [blame] | 77 | } |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 78 | |
| 79 | // Place the list of instructions to simplify on the next loop iteration |
| 80 | // into ToSimplify. |
| 81 | std::swap(ToSimplify, Next); |
| 82 | Next->clear(); |
| 83 | } while (!ToSimplify->empty()); |
| 84 | |
| 85 | return Changed; |
| 86 | } |
| 87 | |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 88 | namespace { |
| 89 | struct InstSimplifier : public FunctionPass { |
| 90 | static char ID; // Pass identification, replacement for typeid |
| 91 | InstSimplifier() : FunctionPass(ID) { |
| 92 | initializeInstSimplifierPass(*PassRegistry::getPassRegistry()); |
| 93 | } |
| 94 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 95 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 96 | AU.setPreservesCFG(); |
Dehao Chen | 3857f8f | 2016-09-06 22:17:16 +0000 | [diff] [blame] | 97 | AU.addRequired<DominatorTreeWrapperPass>(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 98 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 99 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame^] | 100 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /// runOnFunction - Remove instructions that simplify. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 104 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 105 | if (skipFunction(F)) |
| 106 | return false; |
| 107 | |
Dehao Chen | 3857f8f | 2016-09-06 22:17:16 +0000 | [diff] [blame] | 108 | const DominatorTree *DT = |
| 109 | &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 110 | const TargetLibraryInfo *TLI = |
| 111 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 112 | AssumptionCache *AC = |
| 113 | &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame^] | 114 | OptimizationRemarkEmitter *ORE = |
| 115 | &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
| 116 | |
| 117 | return runImpl(F, DT, TLI, AC, ORE); |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 118 | } |
| 119 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 120 | } |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 121 | |
| 122 | char InstSimplifier::ID = 0; |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 123 | INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify", |
| 124 | "Remove redundant instructions", false, false) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 125 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Dehao Chen | 3857f8f | 2016-09-06 22:17:16 +0000 | [diff] [blame] | 126 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 127 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame^] | 128 | INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 129 | INITIALIZE_PASS_END(InstSimplifier, "instsimplify", |
| 130 | "Remove redundant instructions", false, false) |
Duncan Sands | eaff500 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 131 | char &llvm::InstructionSimplifierID = InstSimplifier::ID; |
| 132 | |
| 133 | // Public interface to the simplify instructions pass. |
| 134 | FunctionPass *llvm::createInstructionSimplifierPass() { |
| 135 | return new InstSimplifier(); |
| 136 | } |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 137 | |
| 138 | PreservedAnalyses InstSimplifierPass::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 139 | FunctionAnalysisManager &AM) { |
Dehao Chen | 3857f8f | 2016-09-06 22:17:16 +0000 | [diff] [blame] | 140 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 141 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 142 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame^] | 143 | auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 144 | bool Changed = runImpl(F, &DT, &TLI, &AC, &ORE); |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 145 | if (!Changed) |
| 146 | return PreservedAnalyses::all(); |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 147 | |
| 148 | PreservedAnalyses PA; |
| 149 | PA.preserveSet<CFGAnalyses>(); |
| 150 | return PA; |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 151 | } |