DxilEliminateVector.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 bool IsZeroInitializer(Value *V) {
  52. Constant *C = dyn_cast<Constant>(V);
  53. return C && C->isZeroValue();
  54. }
  55. static
  56. bool CollectVectorElements(Value *V, SmallVector<Value *, 4> &Elements) {
  57. if (InsertElementInst *IE = dyn_cast<InsertElementInst>(V)) {
  58. Value *Vec = IE->getOperand(0);
  59. Value *Element = IE->getOperand(1);
  60. Value *Index = IE->getOperand(2);
  61. if (!isa<UndefValue>(Vec) && !IsZeroInitializer(Vec)) {
  62. if (!CollectVectorElements(Vec, Elements))
  63. return false;
  64. }
  65. ConstantInt *ConstIndex = dyn_cast<ConstantInt>(Index);
  66. if (!ConstIndex)
  67. return false;
  68. uint64_t IdxValue = ConstIndex->getLimitedValue();
  69. if (IdxValue < 4) {
  70. if (Elements.size() <= IdxValue)
  71. Elements.resize(IdxValue+1);
  72. Elements[IdxValue] = Element;
  73. }
  74. return true;
  75. }
  76. return false;
  77. }
  78. bool DxilEliminateVector::TryRewriteDebugInfoForVector(InsertElementInst *IE) {
  79. // If this is not ever used as meta-data, there's no debug
  80. MetadataAsValue *DebugI = GetAsMetadata(IE);
  81. if (!DebugI)
  82. return false;
  83. // Collect @dbg.value instructions
  84. SmallVector<DbgValueInst *, 4> DbgValueInsts;
  85. for (User *U : DebugI->users()) {
  86. if (DbgValueInst *DbgValueI = dyn_cast<DbgValueInst>(U)) {
  87. DbgValueInsts.push_back(DbgValueI);
  88. }
  89. }
  90. if (!DbgValueInsts.size())
  91. return false;
  92. SmallVector<Value *, 4> Elements;
  93. if (!CollectVectorElements(IE, Elements))
  94. return false;
  95. DIBuilder DIB(*IE->getModule());
  96. const DataLayout &DL = IE->getModule()->getDataLayout();
  97. // Go through the elements and create @dbg.value with bit-piece
  98. // expressions for them.
  99. bool Changed = false;
  100. for (DbgValueInst *DVI : DbgValueInsts) {
  101. DIExpression *ParentExpr = DVI->getExpression();
  102. unsigned BitpieceOffset = 0;
  103. if (ParentExpr->isBitPiece())
  104. BitpieceOffset = ParentExpr->getBitPieceOffset();
  105. for (unsigned i = 0; i < Elements.size(); i++) {
  106. if (!Elements[i])
  107. continue;
  108. unsigned ElementSize = DL.getTypeSizeInBits(Elements[i]->getType());
  109. unsigned ElementAlign = DL.getTypeAllocSizeInBits(Elements[i]->getType());
  110. DIExpression *Expr = DIB.createBitPieceExpression(BitpieceOffset + i * ElementAlign, ElementSize);
  111. DIB.insertDbgValueIntrinsic(Elements[i], 0, DVI->getVariable(), Expr, DVI->getDebugLoc(), DVI);
  112. Changed = true;
  113. }
  114. DVI->eraseFromParent();
  115. }
  116. return Changed;
  117. }
  118. bool DxilEliminateVector::runOnFunction(Function &F) {
  119. auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  120. DxilValueCache *DVC = &getAnalysis<DxilValueCache>();
  121. std::vector<Instruction *> VectorInsts;
  122. std::vector<AllocaInst *> VectorAllocas;
  123. // Collect the vector insts and allocas.
  124. for (auto &BB : F) {
  125. for (auto &I : BB)
  126. if (isa<InsertElementInst>(&I) || isa<ExtractElementInst>(&I))
  127. VectorInsts.push_back(&I);
  128. else if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
  129. if (AI->getAllocatedType()->isVectorTy() && llvm::isAllocaPromotable(AI))
  130. VectorAllocas.push_back(AI);
  131. }
  132. }
  133. if (!VectorInsts.size())
  134. return false;
  135. bool Changed = false;
  136. // Promote the allocas if they exist. They could very well exist
  137. // because of precise.
  138. if (VectorAllocas.size()) {
  139. PromoteMemToReg(VectorAllocas, *DT);
  140. Changed = true;
  141. }
  142. // Iteratively try to remove them, untill all gone or unable to
  143. // do it anymore.
  144. unsigned Attempts = VectorInsts.size();
  145. for (unsigned i = 0; i < Attempts; i++) {
  146. bool LocalChange = false;
  147. for (unsigned j = 0; j < VectorInsts.size();) {
  148. auto *I = VectorInsts[j];
  149. bool Remove = false;
  150. if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
  151. TryRewriteDebugInfoForVector(IE);
  152. }
  153. if (Value *V = DVC->GetValue(I, DT)) {
  154. I->replaceAllUsesWith(V);
  155. Remove = true;
  156. }
  157. else if (I->user_empty()) {
  158. Remove = true;
  159. }
  160. // Do the remove
  161. if (Remove) {
  162. LocalChange = true;
  163. I->eraseFromParent();
  164. VectorInsts.erase(VectorInsts.begin() + j);
  165. }
  166. else {
  167. j++;
  168. }
  169. }
  170. Changed |= LocalChange;
  171. if (!LocalChange)
  172. break;
  173. }
  174. return Changed;
  175. }
  176. Pass *llvm::createDxilEliminateVectorPass() {
  177. return new DxilEliminateVector();
  178. }
  179. INITIALIZE_PASS(DxilEliminateVector, "dxil-elim-vector", "Dxil Eliminate Vectors", false, false)