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