ConstantProp.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
  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 file implements constant propagation and merging:
  11. //
  12. // Specifically, this:
  13. // * Converts instructions like "add int 1, 2" into 3
  14. //
  15. // Notice that:
  16. // * This pass has a habit of making definitions be dead. It is a good idea
  17. // to run a DIE pass sometime after running this pass.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "llvm/Transforms/Scalar.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/Analysis/ConstantFolding.h"
  23. #include "llvm/IR/Constant.h"
  24. #include "llvm/IR/InstIterator.h"
  25. #include "llvm/IR/Instruction.h"
  26. #include "llvm/Pass.h"
  27. #include "llvm/Analysis/TargetLibraryInfo.h"
  28. #include <set>
  29. using namespace llvm;
  30. #define DEBUG_TYPE "constprop"
  31. STATISTIC(NumInstKilled, "Number of instructions killed");
  32. namespace {
  33. struct ConstantPropagation : public FunctionPass {
  34. static char ID; // Pass identification, replacement for typeid
  35. ConstantPropagation() : FunctionPass(ID) {
  36. initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
  37. }
  38. bool runOnFunction(Function &F) override;
  39. void getAnalysisUsage(AnalysisUsage &AU) const override {
  40. AU.setPreservesCFG();
  41. AU.addRequired<TargetLibraryInfoWrapperPass>();
  42. }
  43. };
  44. }
  45. char ConstantPropagation::ID = 0;
  46. INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
  47. "Simple constant propagation", false, false)
  48. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  49. INITIALIZE_PASS_END(ConstantPropagation, "constprop",
  50. "Simple constant propagation", false, false)
  51. FunctionPass *llvm::createConstantPropagationPass() {
  52. return new ConstantPropagation();
  53. }
  54. bool ConstantPropagation::runOnFunction(Function &F) {
  55. // Initialize the worklist to all of the instructions ready to process...
  56. std::set<Instruction*> WorkList;
  57. for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
  58. WorkList.insert(&*i);
  59. }
  60. bool Changed = false;
  61. const DataLayout &DL = F.getParent()->getDataLayout();
  62. TargetLibraryInfo *TLI =
  63. &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  64. while (!WorkList.empty()) {
  65. Instruction *I = *WorkList.begin();
  66. WorkList.erase(WorkList.begin()); // Get an element from the worklist...
  67. if (!I->use_empty()) // Don't muck with dead instructions...
  68. if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
  69. // Add all of the users of this instruction to the worklist, they might
  70. // be constant propagatable now...
  71. for (User *U : I->users())
  72. WorkList.insert(cast<Instruction>(U));
  73. // Replace all of the uses of a variable with uses of the constant.
  74. I->replaceAllUsesWith(C);
  75. // Remove the dead instruction.
  76. WorkList.erase(I);
  77. I->eraseFromParent();
  78. // We made a change to the function...
  79. Changed = true;
  80. ++NumInstKilled;
  81. }
  82. }
  83. return Changed;
  84. }