EscapeAnalysis.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //===------------- EscapeAnalysis.h - Pointer escape analysis -------------===//
  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 provides the implementation of the pointer escape analysis.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "escape-analysis"
  14. #include "llvm/Analysis/EscapeAnalysis.h"
  15. #include "llvm/Constants.h"
  16. #include "llvm/Instructions.h"
  17. #include "llvm/Module.h"
  18. #include "llvm/Analysis/AliasAnalysis.h"
  19. #include "llvm/Support/InstIterator.h"
  20. #include "llvm/Target/TargetData.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. #include <vector>
  23. using namespace llvm;
  24. char EscapeAnalysis::ID = 0;
  25. static RegisterPass<EscapeAnalysis> X("escape-analysis",
  26. "Pointer Escape Analysis", true, true);
  27. void EscapeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
  28. AU.addRequiredTransitive<TargetData>();
  29. AU.addRequiredTransitive<AliasAnalysis>();
  30. AU.setPreservesAll();
  31. }
  32. /// runOnFunction - Precomputation for escape analysis. This collects all know
  33. /// "escape points" in the def-use graph of the function. These are
  34. /// instructions which allow their inputs to escape from the current function.
  35. bool EscapeAnalysis::runOnFunction(Function& F) {
  36. EscapePoints.clear();
  37. TargetData& TD = getAnalysis<TargetData>();
  38. AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
  39. Module* M = F.getParent();
  40. // Walk through all instructions in the function, identifying those that
  41. // may allow their inputs to escape.
  42. for(inst_iterator II = inst_begin(F), IE = inst_end(F); II != IE; ++II) {
  43. Instruction* I = &*II;
  44. // The most obvious case is stores. Any store that may write to global
  45. // memory or to a function argument potentially allows its input to escape.
  46. if (StoreInst* S = dyn_cast<StoreInst>(I)) {
  47. const Type* StoreType = S->getOperand(0)->getType();
  48. unsigned StoreSize = TD.getTypeStoreSize(StoreType);
  49. Value* Pointer = S->getPointerOperand();
  50. bool inserted = false;
  51. for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
  52. AI != AE; ++AI) {
  53. if (!isa<PointerType>(AI->getType())) continue;
  54. AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, AI, ~0U);
  55. if (R != AliasAnalysis::NoAlias) {
  56. EscapePoints.insert(S);
  57. inserted = true;
  58. break;
  59. }
  60. }
  61. if (inserted)
  62. continue;
  63. for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
  64. GI != GE; ++GI) {
  65. AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, GI, ~0U);
  66. if (R != AliasAnalysis::NoAlias) {
  67. EscapePoints.insert(S);
  68. break;
  69. }
  70. }
  71. // Calls and invokes potentially allow their parameters to escape.
  72. // FIXME: This can and should be refined. Intrinsics have known escape
  73. // behavior, and alias analysis may be able to tell us more about callees.
  74. } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
  75. EscapePoints.insert(I);
  76. // Returns allow the return value to escape. This is mostly important
  77. // for malloc to alloca promotion.
  78. } else if (isa<ReturnInst>(I)) {
  79. EscapePoints.insert(I);
  80. // Branching on the value of a pointer may allow the value to escape through
  81. // methods not discoverable via def-use chaining.
  82. } else if(isa<BranchInst>(I) || isa<SwitchInst>(I)) {
  83. EscapePoints.insert(I);
  84. }
  85. // FIXME: Are there any other possible escape points?
  86. }
  87. return false;
  88. }
  89. /// escapes - Determines whether the passed allocation can escape from the
  90. /// current function. It does this by using a simple worklist algorithm to
  91. /// search for a path in the def-use graph from the allocation to an
  92. /// escape point.
  93. /// FIXME: Once we've discovered a path, it would be a good idea to memoize it,
  94. /// and all of its subpaths, to amortize the cost of future queries.
  95. bool EscapeAnalysis::escapes(Value* A) {
  96. assert(isa<PointerType>(A->getType()) &&
  97. "Can't do escape analysis on non-pointer types!");
  98. std::vector<Value*> worklist;
  99. worklist.push_back(A);
  100. SmallPtrSet<Value*, 8> visited;
  101. visited.insert(A);
  102. while (!worklist.empty()) {
  103. Value* curr = worklist.back();
  104. worklist.pop_back();
  105. if (Instruction* I = dyn_cast<Instruction>(curr))
  106. if (EscapePoints.count(I)) {
  107. BranchInst* B = dyn_cast<BranchInst>(I);
  108. if (!B) return true;
  109. Value* condition = B->getCondition();
  110. ICmpInst* C = dyn_cast<ICmpInst>(condition);
  111. if (!C) return true;
  112. Value* O1 = C->getOperand(0);
  113. Value* O2 = C->getOperand(1);
  114. if (isa<MallocInst>(O1->stripPointerCasts())) {
  115. if (!isa<ConstantPointerNull>(O2)) return true;
  116. } else if(isa<MallocInst>(O2->stripPointerCasts())) {
  117. if (!isa<ConstantPointerNull>(O1)) return true;
  118. } else
  119. return true;
  120. }
  121. if (StoreInst* S = dyn_cast<StoreInst>(curr)) {
  122. // We know this must be an instruction, because constant gep's would
  123. // have been found to alias a global, so stores to them would have
  124. // been in EscapePoints.
  125. if (visited.insert(cast<Instruction>(S->getPointerOperand())))
  126. worklist.push_back(cast<Instruction>(S->getPointerOperand()));
  127. } else {
  128. for (Instruction::use_iterator UI = curr->use_begin(),
  129. UE = curr->use_end(); UI != UE; ++UI)
  130. if (Instruction* U = dyn_cast<Instruction>(UI))
  131. if (visited.insert(U))
  132. worklist.push_back(U);
  133. }
  134. }
  135. return false;
  136. }