LowerAllocations.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===- LowerAllocations.cpp - Reduce free insts to calls ------------------===//
  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. // The LowerAllocations transformation is a target-dependent tranformation
  11. // because it depends on the size of data types and alignment constraints.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "lowerallocs"
  15. #include "llvm/Transforms/Scalar.h"
  16. #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
  17. #include "llvm/Module.h"
  18. #include "llvm/DerivedTypes.h"
  19. #include "llvm/Instructions.h"
  20. #include "llvm/Constants.h"
  21. #include "llvm/LLVMContext.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/ADT/Statistic.h"
  24. #include "llvm/Target/TargetData.h"
  25. using namespace llvm;
  26. STATISTIC(NumLowered, "Number of allocations lowered");
  27. namespace {
  28. /// LowerAllocations - Turn free instructions into @free calls.
  29. ///
  30. class LowerAllocations : public BasicBlockPass {
  31. Constant *FreeFunc; // Functions in the module we are processing
  32. // Initialized by doInitialization
  33. public:
  34. static char ID; // Pass ID, replacement for typeid
  35. explicit LowerAllocations()
  36. : BasicBlockPass(&ID), FreeFunc(0) {}
  37. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  38. AU.addRequired<TargetData>();
  39. AU.setPreservesCFG();
  40. // This is a cluster of orthogonal Transforms:
  41. AU.addPreserved<UnifyFunctionExitNodes>();
  42. AU.addPreservedID(PromoteMemoryToRegisterID);
  43. AU.addPreservedID(LowerSwitchID);
  44. AU.addPreservedID(LowerInvokePassID);
  45. }
  46. /// doPassInitialization - For the lower allocations pass, this ensures that
  47. /// a module contains a declaration for a free function.
  48. ///
  49. bool doInitialization(Module &M);
  50. virtual bool doInitialization(Function &F) {
  51. return doInitialization(*F.getParent());
  52. }
  53. /// runOnBasicBlock - This method does the actual work of converting
  54. /// instructions over, assuming that the pass has already been initialized.
  55. ///
  56. bool runOnBasicBlock(BasicBlock &BB);
  57. };
  58. }
  59. char LowerAllocations::ID = 0;
  60. static RegisterPass<LowerAllocations>
  61. X("lowerallocs", "Lower allocations from instructions to calls");
  62. // Publically exposed interface to pass...
  63. const PassInfo *const llvm::LowerAllocationsID = &X;
  64. // createLowerAllocationsPass - Interface to this file...
  65. Pass *llvm::createLowerAllocationsPass() {
  66. return new LowerAllocations();
  67. }
  68. // doInitialization - For the lower allocations pass, this ensures that a
  69. // module contains a declaration for a free function.
  70. //
  71. // This function is always successful.
  72. //
  73. bool LowerAllocations::doInitialization(Module &M) {
  74. const Type *BPTy = Type::getInt8PtrTy(M.getContext());
  75. FreeFunc = M.getOrInsertFunction("free" , Type::getVoidTy(M.getContext()),
  76. BPTy, (Type *)0);
  77. return true;
  78. }
  79. // runOnBasicBlock - This method does the actual work of converting
  80. // instructions over, assuming that the pass has already been initialized.
  81. //
  82. bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
  83. bool Changed = false;
  84. assert(FreeFunc && "Pass not initialized!");
  85. BasicBlock::InstListType &BBIL = BB.getInstList();
  86. // Loop over all of the instructions, looking for free instructions
  87. for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
  88. if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
  89. // Insert a call to the free function...
  90. CallInst::CreateFree(FI->getOperand(0), I);
  91. // Delete the old free instruction
  92. I = --BBIL.erase(I);
  93. Changed = true;
  94. ++NumLowered;
  95. }
  96. }
  97. return Changed;
  98. }