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" |
| 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumSimplified, "Number of redundant instructions removed"); |
| 29 | |
| 30 | namespace { |
| 31 | struct InstSimplifier : public FunctionPass { |
| 32 | static char ID; // Pass identification, replacement for typeid |
| 33 | InstSimplifier() : FunctionPass(ID) { |
| 34 | initializeInstSimplifierPass(*PassRegistry::getPassRegistry()); |
| 35 | } |
| 36 | |
| 37 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 38 | AU.setPreservesCFG(); |
| 39 | } |
| 40 | |
| 41 | /// runOnFunction - Remove instructions that simplify. |
| 42 | bool runOnFunction(Function &F) { |
| 43 | bool Changed = false; |
| 44 | const TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
| 45 | const DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>(); |
| 46 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 47 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { |
| 48 | Instruction *I = BI++; |
| 49 | if (Value *V = SimplifyInstruction(I, TD, DT)) { |
| 50 | I->replaceAllUsesWith(V); |
| 51 | I->eraseFromParent(); |
| 52 | Changed = true; |
| 53 | ++NumSimplified; |
| 54 | } |
| 55 | } |
| 56 | return Changed; |
| 57 | } |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | char InstSimplifier::ID = 0; |
| 62 | INITIALIZE_PASS(InstSimplifier, "instsimplify", "Remove redundant instructions", |
| 63 | false, false) |
| 64 | char &llvm::InstructionSimplifierID = InstSimplifier::ID; |
| 65 | |
| 66 | // Public interface to the simplify instructions pass. |
| 67 | FunctionPass *llvm::createInstructionSimplifierPass() { |
| 68 | return new InstSimplifier(); |
| 69 | } |