ReplaceConstant.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //===- ReplaceConstant.cpp - Replace LLVM constant expression--------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements a utility function for replacing LLVM constant
  10. // expressions by instructions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/ReplaceConstant.h"
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/ValueMap.h"
  17. namespace llvm {
  18. void convertConstantExprsToInstructions(Instruction *I, ConstantExpr *CE,
  19. SmallPtrSetImpl<Instruction *> *Insts) {
  20. // Collect all reachable paths to CE from constant exprssion operands of I.
  21. std::map<Use *, std::vector<std::vector<ConstantExpr *>>> CEPaths;
  22. collectConstantExprPaths(I, CE, CEPaths);
  23. // Convert all constant expressions to instructions which are collected at
  24. // CEPaths.
  25. convertConstantExprsToInstructions(I, CEPaths, Insts);
  26. }
  27. void convertConstantExprsToInstructions(
  28. Instruction *I,
  29. std::map<Use *, std::vector<std::vector<ConstantExpr *>>> &CEPaths,
  30. SmallPtrSetImpl<Instruction *> *Insts) {
  31. ValueMap<ConstantExpr *, Instruction *> Visited;
  32. for (Use &U : I->operands()) {
  33. // The operand U is either not a constant expression operand or the
  34. // constant expression paths do not belong to U, ignore U.
  35. if (!CEPaths.count(&U))
  36. continue;
  37. // If the instruction I is a PHI instruction, then fix the instruction
  38. // insertion point to the entry of the incoming basic block for operand U.
  39. auto *BI = I;
  40. if (auto *Phi = dyn_cast<PHINode>(I)) {
  41. BasicBlock *BB = Phi->getIncomingBlock(U);
  42. BI = &(*(BB->getFirstInsertionPt()));
  43. }
  44. // Go through all the paths associated with operand U, and convert all the
  45. // constant expressions along all the paths to corresponding instructions.
  46. auto *II = I;
  47. auto &Paths = CEPaths[&U];
  48. for (auto &Path : Paths) {
  49. for (auto *CE : Path) {
  50. // Instruction which is equivalent to CE.
  51. Instruction *NI = nullptr;
  52. if (!Visited.count(CE)) {
  53. // CE is encountered first time, convert it into a corresponding
  54. // instruction NI, and appropriately insert NI before the parent
  55. // instruction.
  56. NI = CE->getAsInstruction(BI);
  57. // Mark CE as visited by mapping CE to NI.
  58. Visited[CE] = NI;
  59. // If required collect NI.
  60. if (Insts)
  61. Insts->insert(NI);
  62. } else {
  63. // We had already encountered CE, the correponding instruction already
  64. // exist, use it to replace CE.
  65. NI = Visited[CE];
  66. }
  67. assert(NI && "Expected an instruction corresponding to constant "
  68. "expression.");
  69. // Replace all uses of constant expression CE by the corresponding
  70. // instruction NI within the current parent instruction.
  71. II->replaceUsesOfWith(CE, NI);
  72. BI = II = NI;
  73. }
  74. }
  75. }
  76. // Remove all converted constant expressions which are dead by now.
  77. for (auto Item : Visited)
  78. Item.first->removeDeadConstantUsers();
  79. }
  80. void collectConstantExprPaths(
  81. Instruction *I, ConstantExpr *CE,
  82. std::map<Use *, std::vector<std::vector<ConstantExpr *>>> &CEPaths) {
  83. for (Use &U : I->operands()) {
  84. // If the operand U is not a constant expression operand, then ignore it.
  85. auto *CE2 = dyn_cast<ConstantExpr>(U.get());
  86. if (!CE2)
  87. continue;
  88. // Holds all reachable paths from CE2 to CE.
  89. std::vector<std::vector<ConstantExpr *>> Paths;
  90. // Collect all reachable paths from CE2 to CE.
  91. std::vector<ConstantExpr *> Path{CE2};
  92. std::vector<std::vector<ConstantExpr *>> Stack{Path};
  93. while (!Stack.empty()) {
  94. std::vector<ConstantExpr *> TPath = Stack.back();
  95. Stack.pop_back();
  96. auto *CE3 = TPath.back();
  97. if (CE3 == CE) {
  98. Paths.push_back(TPath);
  99. continue;
  100. }
  101. for (auto &UU : CE3->operands()) {
  102. if (auto *CE4 = dyn_cast<ConstantExpr>(UU.get())) {
  103. std::vector<ConstantExpr *> NPath(TPath.begin(), TPath.end());
  104. NPath.push_back(CE4);
  105. Stack.push_back(NPath);
  106. }
  107. }
  108. }
  109. // Associate all the collected paths with U, and save it.
  110. if (!Paths.empty())
  111. CEPaths[&U] = Paths;
  112. }
  113. }
  114. } // namespace llvm