Reg2MemHLSL.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //===- Reg2MemHLSL.cpp - Convert registers to allocas ---------------------===//
  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. #include "llvm/Transforms/Scalar.h"
  10. #include "llvm/ADT/Statistic.h"
  11. #include "llvm/IR/BasicBlock.h"
  12. #include "llvm/IR/CFG.h"
  13. #include "llvm/IR/Function.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/Pass.h"
  18. #include "llvm/Transforms/Utils/Local.h"
  19. #include <list>
  20. using namespace llvm;
  21. #define DEBUG_TYPE "reg2mem_hlsl"
  22. STATISTIC(NumRegsDemotedHlsl, "Number of registers demoted");
  23. STATISTIC(NumPhisDemotedHlsl, "Number of phi-nodes demoted");
  24. namespace {
  25. struct RegToMemHlsl : public FunctionPass {
  26. static char ID; // Pass identification, replacement for typeid
  27. RegToMemHlsl() : FunctionPass(ID) {
  28. initializeRegToMemHlslPass(*PassRegistry::getPassRegistry());
  29. }
  30. void getAnalysisUsage(AnalysisUsage &AU) const override {
  31. }
  32. bool valueEscapes(const Instruction *Inst) const {
  33. for (const User *U : Inst->users()) {
  34. const Instruction *UI = cast<Instruction>(U);
  35. if (isa<PHINode>(UI))
  36. return true;
  37. }
  38. return false;
  39. }
  40. bool runOnFunction(Function &F) override;
  41. };
  42. /// DemotePHIToStack - This function takes a virtual register computed by a
  43. /// PHI node and replaces it with a slot in the stack frame allocated via
  44. /// alloca.
  45. /// The PHI node is deleted. It returns the pointer to the alloca inserted.
  46. /// The difference of HLSL version is the new Alloca will be loaded for each
  47. /// use, for case a phi inside loop be used outside the loop.
  48. AllocaInst *DemotePHIToStack_HLSL(PHINode *P, Instruction *AllocaPoint) {
  49. if (P->use_empty()) {
  50. P->eraseFromParent();
  51. return nullptr;
  52. }
  53. IRBuilder<> Builder(P);
  54. if (!AllocaPoint) {
  55. Function *F = P->getParent()->getParent();
  56. AllocaPoint = F->getEntryBlock().begin();
  57. }
  58. Builder.SetInsertPoint(AllocaPoint);
  59. // Create a stack slot to hold the value.
  60. AllocaInst *Slot = Builder.CreateAlloca(P->getType(), nullptr, P->getName() + ".reg2mem");
  61. // Insert a load in place of the PHI and replace all uses.
  62. BasicBlock::iterator InsertPt = P;
  63. for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
  64. /* empty */; // Don't insert before PHI nodes or landingpad instrs.
  65. std::vector<Instruction*> WorkList;
  66. for (auto U = P->user_begin(); U != P->user_end();) {
  67. Instruction *I = cast<Instruction>(*(U++));
  68. WorkList.emplace_back(I);
  69. }
  70. for (Instruction *I : WorkList) {
  71. IRBuilder<> Builder(I);
  72. Value *Load = Builder.CreateLoad(Slot);
  73. I->replaceUsesOfWith(P, Load);
  74. }
  75. // Iterate over each operand inserting a store in each predecessor.
  76. // This should be done after load inserting because store for phi must be
  77. // after all other instructions of the incoming block.
  78. for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
  79. if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
  80. assert(II->getParent() != P->getIncomingBlock(i) &&
  81. "Invoke edge not supported yet");
  82. (void)II;
  83. }
  84. Value *V = P->getIncomingValue(i);
  85. // Skip undef
  86. if (isa<UndefValue>(V))
  87. continue;
  88. new StoreInst(P->getIncomingValue(i), Slot,
  89. P->getIncomingBlock(i)->getTerminator());
  90. }
  91. // Delete PHI.
  92. P->eraseFromParent();
  93. return Slot;
  94. }
  95. /// DemoteRegToStack - This function takes a virtual register computed by an
  96. /// Instruction and replaces it with a slot in the stack frame, allocated via
  97. /// alloca. This allows the CFG to be changed around without fear of
  98. /// invalidating the SSA information for the value. It returns the pointer to
  99. /// the alloca inserted to create a stack slot for I.
  100. /// The difference of HLSL version is for I is Alloca, only replace new Alloca with
  101. /// old alloca, and HLSL don't have InvokeInst
  102. AllocaInst *DemoteRegToStack_HLSL(Instruction &I, bool VolatileLoads,
  103. Instruction *AllocaPoint) {
  104. if (I.use_empty()) {
  105. I.eraseFromParent();
  106. return nullptr;
  107. }
  108. IRBuilder<> Builder(&I);
  109. if (!AllocaPoint) {
  110. Function *F = I.getParent()->getParent();
  111. AllocaPoint = F->getEntryBlock().begin();
  112. }
  113. Builder.SetInsertPoint(AllocaPoint);
  114. if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
  115. // Create a stack slot to hold the value.
  116. AllocaInst *Slot = Builder.CreateAlloca(AI->getAllocatedType(), nullptr, I.getName() + ".reg2mem");
  117. I.replaceAllUsesWith(Slot);
  118. I.eraseFromParent();
  119. return Slot;
  120. }
  121. // Create a stack slot to hold the value.
  122. AllocaInst *Slot = Builder.CreateAlloca(I.getType(), nullptr, I.getName() + ".reg2mem");;
  123. // Change all of the users of the instruction to read from the stack slot.
  124. while (!I.use_empty()) {
  125. Instruction *U = cast<Instruction>(I.user_back());
  126. if (PHINode *PN = dyn_cast<PHINode>(U)) {
  127. // If this is a PHI node, we can't insert a load of the value before the
  128. // use. Instead insert the load in the predecessor block corresponding
  129. // to the incoming value.
  130. //
  131. // Note that if there are multiple edges from a basic block to this PHI
  132. // node that we cannot have multiple loads. The problem is that the
  133. // resulting PHI node will have multiple values (from each load) coming
  134. // in
  135. // from the same block, which is illegal SSA form. For this reason, we
  136. // keep track of and reuse loads we insert.
  137. DenseMap<BasicBlock *, Value *> Loads;
  138. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  139. if (PN->getIncomingValue(i) == &I) {
  140. Value *&V = Loads[PN->getIncomingBlock(i)];
  141. if (!V) {
  142. // Insert the load into the predecessor block
  143. V = new LoadInst(Slot, I.getName() + ".reload", VolatileLoads,
  144. PN->getIncomingBlock(i)->getTerminator());
  145. }
  146. PN->setIncomingValue(i, V);
  147. }
  148. } else {
  149. // If this is a normal instruction, just insert a load.
  150. Value *V =
  151. new LoadInst(Slot, I.getName() + ".reload", VolatileLoads, U);
  152. U->replaceUsesOfWith(&I, V);
  153. }
  154. }
  155. // Insert stores of the computed value into the stack slot. We have to be
  156. // careful if I is an invoke instruction, because we can't insert the store
  157. // AFTER the terminator instruction.
  158. BasicBlock::iterator InsertPt;
  159. if (!isa<TerminatorInst>(I)) {
  160. InsertPt = &I;
  161. ++InsertPt;
  162. for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt);
  163. ++InsertPt)
  164. /* empty */; // Don't insert before PHI nodes or landingpad instrs.
  165. } else {
  166. InvokeInst &II = cast<InvokeInst>(I);
  167. InsertPt = II.getNormalDest()->getFirstInsertionPt();
  168. }
  169. new StoreInst(&I, Slot, InsertPt);
  170. return Slot;
  171. }
  172. }
  173. char RegToMemHlsl::ID = 0;
  174. INITIALIZE_PASS_BEGIN(RegToMemHlsl, "reg2mem_hlsl", "Demote values with phi-node usage to stack slots",
  175. false, false)
  176. INITIALIZE_PASS_END(RegToMemHlsl, "reg2mem_hlsl", "Demote values with phi-node usage to stack slots",
  177. false, false)
  178. bool RegToMemHlsl::runOnFunction(Function &F) {
  179. if (F.isDeclaration())
  180. return false;
  181. // Insert all new allocas into entry block.
  182. BasicBlock *BBEntry = &F.getEntryBlock();
  183. assert(pred_empty(BBEntry) &&
  184. "Entry block to function must not have predecessors!");
  185. // Find first non-alloca instruction and create insertion point. This is
  186. // safe if block is well-formed: it always have terminator, otherwise
  187. // we'll get and assertion.
  188. BasicBlock::iterator I = BBEntry->begin();
  189. while (isa<AllocaInst>(I)) ++I;
  190. CastInst *AllocaInsertionPoint =
  191. new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())),
  192. Type::getInt32Ty(F.getContext()),
  193. "reg2mem_hlsl alloca point", I);
  194. // Find the escaped instructions. But don't create stack slots for
  195. // allocas in entry block.
  196. std::list<Instruction*> WorkList;
  197. for (Function::iterator ibb = F.begin(), ibe = F.end();
  198. ibb != ibe; ++ibb)
  199. for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
  200. iib != iie; ++iib) {
  201. if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
  202. valueEscapes(iib)) {
  203. WorkList.push_front(&*iib);
  204. }
  205. }
  206. // Demote escaped instructions
  207. NumRegsDemotedHlsl += WorkList.size();
  208. for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
  209. ile = WorkList.end(); ilb != ile; ++ilb)
  210. DemoteRegToStack_HLSL(**ilb, false, AllocaInsertionPoint);
  211. WorkList.clear();
  212. // Find all phi's
  213. for (Function::iterator ibb = F.begin(), ibe = F.end();
  214. ibb != ibe; ++ibb)
  215. for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
  216. iib != iie; ++iib)
  217. if (isa<PHINode>(iib))
  218. WorkList.push_front(&*iib);
  219. // Demote phi nodes
  220. NumPhisDemotedHlsl += WorkList.size();
  221. for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
  222. ile = WorkList.end(); ilb != ile; ++ilb)
  223. DemotePHIToStack_HLSL(cast<PHINode>(*ilb), AllocaInsertionPoint);
  224. return true;
  225. }
  226. // createDemoteRegisterToMemoryHlsl - Provide an entry point to create this pass.
  227. char &llvm::DemoteRegisterToMemoryHlslID = RegToMemHlsl::ID;
  228. FunctionPass *llvm::createDemoteRegisterToMemoryHlslPass() {
  229. return new RegToMemHlsl();
  230. }