DxilEliminateVector.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilEliminateVector.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // A pass to remove vector instructions, especially in situations where //
  9. // optimizations are turned off. //
  10. // //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "llvm/Pass.h"
  13. #include "llvm/IR/Instructions.h"
  14. #include "llvm/IR/Dominators.h"
  15. #include "llvm/Transforms/Scalar.h"
  16. #include "llvm/Transforms/Utils/PromoteMemToReg.h"
  17. #include "llvm/IR/DebugInfo.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/IR/IntrinsicInst.h"
  20. #include "llvm/IR/DIBuilder.h"
  21. #include "llvm/Analysis/DxilValueCache.h"
  22. #include <vector>
  23. using namespace llvm;
  24. namespace {
  25. class DxilEliminateVector : public FunctionPass {
  26. public:
  27. static char ID;
  28. DxilEliminateVector() : FunctionPass(ID) {
  29. initializeDxilEliminateVectorPass(*PassRegistry::getPassRegistry());
  30. }
  31. void getAnalysisUsage(AnalysisUsage &AU) const override {
  32. AU.addRequired<DxilValueCache>();
  33. AU.addRequired<DominatorTreeWrapperPass>();
  34. AU.setPreservesAll(); // DxilValueCache is safe. CFG is not changed, so DT is okay.
  35. }
  36. bool TryRewriteDebugInfoForVector(InsertElementInst *IE);
  37. bool runOnFunction(Function &F) override;
  38. const char *getPassName() const override { return "Dxil Eliminate Vector"; }
  39. };
  40. char DxilEliminateVector::ID;
  41. }
  42. static
  43. MetadataAsValue *GetAsMetadata(Instruction *I) {
  44. if (auto *L = LocalAsMetadata::getIfExists(I)) {
  45. if (auto *DINode = MetadataAsValue::getIfExists(I->getContext(), L)) {
  46. return DINode;
  47. }
  48. }
  49. return nullptr;
  50. }
  51. static
  52. bool CollectVectorElements(Value *V, SmallVector<Value *, 4> &Elements) {
  53. if (InsertElementInst *IE = dyn_cast<InsertElementInst>(V)) {
  54. Value *Vec = IE->getOperand(0);
  55. Value *Element = IE->getOperand(1);
  56. Value *Index = IE->getOperand(2);
  57. if (!isa<UndefValue>(Vec)) {
  58. if (!CollectVectorElements(Vec, Elements))
  59. return false;
  60. }
  61. ConstantInt *ConstIndex = dyn_cast<ConstantInt>(Index);
  62. if (!ConstIndex)
  63. return false;
  64. uint64_t IdxValue = ConstIndex->getLimitedValue();
  65. if (IdxValue < 4) {
  66. if (Elements.size() <= IdxValue)
  67. Elements.resize(IdxValue+1);
  68. Elements[IdxValue] = Element;
  69. }
  70. return true;
  71. }
  72. return false;
  73. }
  74. static bool HasDebugValue(Value *V) {
  75. Instruction *I = dyn_cast<Instruction>(V);
  76. if (!I) return false;
  77. MetadataAsValue *DebugI = GetAsMetadata(I);
  78. if (!DebugI) return false;
  79. for (User *U : DebugI->users()) {
  80. if (isa<DbgValueInst>(U))
  81. return true;
  82. }
  83. return false;
  84. }
  85. bool DxilEliminateVector::TryRewriteDebugInfoForVector(InsertElementInst *IE) {
  86. // If this is not ever used as meta-data, there's no debug
  87. MetadataAsValue *DebugI = GetAsMetadata(IE);
  88. if (!DebugI)
  89. return false;
  90. // Collect @dbg.value instructions
  91. SmallVector<DbgValueInst *, 4> DbgValueInsts;
  92. for (User *U : DebugI->users()) {
  93. if (DbgValueInst *DbgValueI = dyn_cast<DbgValueInst>(U)) {
  94. DbgValueInsts.push_back(DbgValueI);
  95. }
  96. }
  97. if (!DbgValueInsts.size())
  98. return false;
  99. SmallVector<Value *, 4> Elements;
  100. if (!CollectVectorElements(IE, Elements))
  101. return false;
  102. DIBuilder DIB(*IE->getModule());
  103. const DataLayout &DL = IE->getModule()->getDataLayout();
  104. // Go through the elements and create @dbg.value with bit-piece
  105. // expressions for them.
  106. bool Changed = false;
  107. for (DbgValueInst *DVI : DbgValueInsts) {
  108. DIExpression *ParentExpr = DVI->getExpression();
  109. unsigned BitpieceOffset = 0;
  110. if (ParentExpr->isBitPiece())
  111. BitpieceOffset = ParentExpr->getBitPieceOffset();
  112. for (unsigned i = 0; i < Elements.size(); i++) {
  113. if (!Elements[i])
  114. continue;
  115. if (HasDebugValue(Elements[i]))
  116. continue;
  117. unsigned ElementSize = DL.getTypeAllocSizeInBits(Elements[i]->getType());
  118. DIExpression *Expr = DIB.createBitPieceExpression(BitpieceOffset + i * ElementSize, ElementSize);
  119. DIB.insertDbgValueIntrinsic(Elements[i], 0, DVI->getVariable(), Expr, DVI->getDebugLoc(), DVI);
  120. Changed = true;
  121. }
  122. DVI->eraseFromParent();
  123. }
  124. return Changed;
  125. }
  126. bool DxilEliminateVector::runOnFunction(Function &F) {
  127. auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  128. DxilValueCache *DVC = &getAnalysis<DxilValueCache>();
  129. std::vector<Instruction *> VectorInsts;
  130. std::vector<AllocaInst *> VectorAllocas;
  131. // Collect the vector insts and allocas.
  132. for (auto &BB : F) {
  133. for (auto &I : BB)
  134. if (isa<InsertElementInst>(&I) || isa<ExtractElementInst>(&I))
  135. VectorInsts.push_back(&I);
  136. else if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
  137. if (AI->getAllocatedType()->isVectorTy() && llvm::isAllocaPromotable(AI))
  138. VectorAllocas.push_back(AI);
  139. }
  140. }
  141. if (!VectorInsts.size())
  142. return false;
  143. bool Changed = false;
  144. // Promote the allocas if they exist. They could very well exist
  145. // because of precise.
  146. if (VectorAllocas.size()) {
  147. PromoteMemToReg(VectorAllocas, *DT);
  148. Changed = true;
  149. }
  150. // Iteratively try to remove them, untill all gone or unable to
  151. // do it anymore.
  152. unsigned Attempts = VectorInsts.size();
  153. for (unsigned i = 0; i < Attempts; i++) {
  154. bool LocalChange = false;
  155. for (unsigned j = 0; j < VectorInsts.size();) {
  156. auto *I = VectorInsts[j];
  157. bool Remove = false;
  158. if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
  159. TryRewriteDebugInfoForVector(IE);
  160. }
  161. if (Value *V = DVC->GetValue(I, DT)) {
  162. I->replaceAllUsesWith(V);
  163. Remove = true;
  164. }
  165. else if (I->user_empty()) {
  166. Remove = true;
  167. }
  168. // Do the remove
  169. if (Remove) {
  170. LocalChange = true;
  171. I->eraseFromParent();
  172. VectorInsts.erase(VectorInsts.begin() + j);
  173. }
  174. else {
  175. j++;
  176. }
  177. }
  178. Changed |= LocalChange;
  179. if (!LocalChange)
  180. break;
  181. }
  182. return Changed;
  183. }
  184. Pass *llvm::createDxilEliminateVectorPass() {
  185. return new DxilEliminateVector();
  186. }
  187. INITIALIZE_PASS(DxilEliminateVector, "dxil-elim-vector", "Dxil Eliminate Vectors", false, false)