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" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Type.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
| 22 | #include "llvm/Analysis/Dominators.h" |
| 23 | #include "llvm/Analysis/InstructionSimplify.h" |
| 24 | #include "llvm/Target/TargetData.h" |
| 25 | #include "llvm/Transforms/Scalar.h" |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/Local.h" |
Duncan Sands | 1cd0f463 | 2010-12-21 17:08:55 +0000 | [diff] [blame^] | 27 | #include <queue> |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | STATISTIC(NumSimplified, "Number of redundant instructions removed"); |
| 31 | |
| 32 | namespace { |
| 33 | struct InstSimplifier : public FunctionPass { |
| 34 | static char ID; // Pass identification, replacement for typeid |
| 35 | InstSimplifier() : FunctionPass(ID) { |
| 36 | initializeInstSimplifierPass(*PassRegistry::getPassRegistry()); |
| 37 | } |
| 38 | |
| 39 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 40 | AU.setPreservesCFG(); |
| 41 | } |
| 42 | |
| 43 | /// runOnFunction - Remove instructions that simplify. |
| 44 | bool runOnFunction(Function &F) { |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 45 | const TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
| 46 | const DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>(); |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 47 | bool Changed = false; |
| 48 | |
Duncan Sands | 1cd0f463 | 2010-12-21 17:08:55 +0000 | [diff] [blame^] | 49 | // Add all interesting instructions to the worklist. These are processed |
| 50 | // in FIFO order, so instructions are usually visited before their uses. |
| 51 | std::queue<Instruction*> Worklist; |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 52 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 53 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
| 54 | Instruction *I = BI++; |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 55 | // Zap any dead instructions. |
| 56 | if (isInstructionTriviallyDead(I)) { |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 57 | I->eraseFromParent(); |
| 58 | Changed = true; |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 59 | continue; |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 60 | } |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 61 | // Add all others to the worklist. |
Duncan Sands | 1cd0f463 | 2010-12-21 17:08:55 +0000 | [diff] [blame^] | 62 | Worklist.push(I); |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 63 | } |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 64 | |
| 65 | // Simplify everything in the worklist until the cows come home. |
| 66 | while (!Worklist.empty()) { |
Duncan Sands | 1cd0f463 | 2010-12-21 17:08:55 +0000 | [diff] [blame^] | 67 | Instruction *I = Worklist.front(); |
| 68 | Worklist.pop(); |
| 69 | // Don't bother simplifying unused instructions. |
| 70 | if (I->use_empty()) continue; |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 71 | Value *V = SimplifyInstruction(I, TD, DT); |
| 72 | if (!V) continue; |
| 73 | |
| 74 | // This instruction simplifies! Replace it with its simplification and |
| 75 | // add all uses to the worklist, since they may now simplify. |
Duncan Sands | 1cd0f463 | 2010-12-21 17:08:55 +0000 | [diff] [blame^] | 76 | ++NumSimplified; |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 77 | I->replaceAllUsesWith(V); |
| 78 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 79 | UI != UE; ++UI) |
Duncan Sands | 1cd0f463 | 2010-12-21 17:08:55 +0000 | [diff] [blame^] | 80 | Worklist.push(cast<Instruction>(*UI)); |
Duncan Sands | e95cc25 | 2010-12-21 16:12:03 +0000 | [diff] [blame] | 81 | Changed = true; |
| 82 | } |
| 83 | |
Duncan Sands | 1cd0f463 | 2010-12-21 17:08:55 +0000 | [diff] [blame^] | 84 | // Finally, run over the function zapping any dead instructions. |
| 85 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 86 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
| 87 | Instruction *I = BI++; |
| 88 | if (isInstructionTriviallyDead(I)) { |
| 89 | I->eraseFromParent(); |
| 90 | Changed = true; |
| 91 | } |
| 92 | } |
| 93 | |
Duncan Sands | 69fdf87 | 2010-12-20 21:07:42 +0000 | [diff] [blame] | 94 | return Changed; |
| 95 | } |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | char InstSimplifier::ID = 0; |
| 100 | INITIALIZE_PASS(InstSimplifier, "instsimplify", "Remove redundant instructions", |
| 101 | false, false) |
| 102 | char &llvm::InstructionSimplifierID = InstSimplifier::ID; |
| 103 | |
| 104 | // Public interface to the simplify instructions pass. |
| 105 | FunctionPass *llvm::createInstructionSimplifierPass() { |
| 106 | return new InstSimplifier(); |
| 107 | } |