LCOV - code coverage report
Current view: top level - lib/Target/AMDGPU - AMDGPUUnifyDivergentExitNodes.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 78 78 100.0 %
Date: 2017-09-14 15:23:50 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- AMDGPUUnifyDivergentExitNodes.cpp ----------------------------------===//
       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 is a variant of the UnifyDivergentExitNodes pass. Rather than ensuring
      11             : // there is at most one ret and one unreachable instruction, it ensures there is
      12             : // at most one divergent exiting block.
      13             : //
      14             : // StructurizeCFG can't deal with multi-exit regions formed by branches to
      15             : // multiple return nodes. It is not desirable to structurize regions with
      16             : // uniform branches, so unifying those to the same return block as divergent
      17             : // branches inhibits use of scalar branching. It still can't deal with the case
      18             : // where one branch goes to return, and one unreachable. Replace unreachable in
      19             : // this case with a return.
      20             : //
      21             : //===----------------------------------------------------------------------===//
      22             : 
      23             : #include "AMDGPU.h"
      24             : #include "llvm/ADT/DepthFirstIterator.h"
      25             : #include "llvm/ADT/StringExtras.h"
      26             : #include "llvm/Analysis/DivergenceAnalysis.h"
      27             : #include "llvm/Analysis/PostDominators.h"
      28             : #include "llvm/Analysis/TargetTransformInfo.h"
      29             : #include "llvm/IR/BasicBlock.h"
      30             : #include "llvm/IR/CFG.h"
      31             : #include "llvm/IR/Function.h"
      32             : #include "llvm/IR/Instructions.h"
      33             : #include "llvm/IR/Type.h"
      34             : #include "llvm/Transforms/Scalar.h"
      35             : #include "llvm/Transforms/Utils/Local.h"
      36             : using namespace llvm;
      37             : 
      38             : #define DEBUG_TYPE "amdgpu-unify-divergent-exit-nodes"
      39             : 
      40             : namespace {
      41             : 
      42        2922 : class AMDGPUUnifyDivergentExitNodes : public FunctionPass {
      43             : public:
      44             :   static char ID; // Pass identification, replacement for typeid
      45        2938 :   AMDGPUUnifyDivergentExitNodes() : FunctionPass(ID) {
      46        1469 :     initializeAMDGPUUnifyDivergentExitNodesPass(*PassRegistry::getPassRegistry());
      47             :   }
      48             : 
      49             :   // We can preserve non-critical-edgeness when we unify function exit nodes
      50             :   void getAnalysisUsage(AnalysisUsage &AU) const override;
      51             :   bool runOnFunction(Function &F) override;
      52             : };
      53             : 
      54             : }
      55             : 
      56             : char AMDGPUUnifyDivergentExitNodes::ID = 0;
      57       53042 : INITIALIZE_PASS_BEGIN(AMDGPUUnifyDivergentExitNodes, DEBUG_TYPE,
      58             :                      "Unify divergent function exit nodes", false, false)
      59       53042 : INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
      60       53042 : INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
      61      316945 : INITIALIZE_PASS_END(AMDGPUUnifyDivergentExitNodes, DEBUG_TYPE,
      62             :                     "Unify divergent function exit nodes", false, false)
      63             : 
      64             : char &llvm::AMDGPUUnifyDivergentExitNodesID = AMDGPUUnifyDivergentExitNodes::ID;
      65             : 
      66        1461 : void AMDGPUUnifyDivergentExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
      67             :   // TODO: Preserve dominator tree.
      68        1461 :   AU.addRequired<PostDominatorTreeWrapperPass>();
      69             : 
      70        1461 :   AU.addRequired<DivergenceAnalysis>();
      71             : 
      72             :   // No divergent values are changed, only blocks and branch edges.
      73        1461 :   AU.addPreserved<DivergenceAnalysis>();
      74             : 
      75             :   // We preserve the non-critical-edgeness property
      76        2922 :   AU.addPreservedID(BreakCriticalEdgesID);
      77             : 
      78             :   // This is a cluster of orthogonal Transforms
      79        2922 :   AU.addPreservedID(LowerSwitchID);
      80        1461 :   FunctionPass::getAnalysisUsage(AU);
      81             : 
      82        1461 :   AU.addRequired<TargetTransformInfoWrapperPass>();
      83        1461 : }
      84             : 
      85             : /// \returns true if \p BB is reachable through only uniform branches.
      86             : /// XXX - Is there a more efficient way to find this?
      87         174 : static bool isUniformlyReached(const DivergenceAnalysis &DA,
      88             :                                BasicBlock &BB) {
      89         348 :   SmallVector<BasicBlock *, 8> Stack;
      90         348 :   SmallPtrSet<BasicBlock *, 8> Visited;
      91             : 
      92         802 :   for (BasicBlock *Pred : predecessors(&BB))
      93         227 :     Stack.push_back(Pred);
      94             : 
      95         464 :   while (!Stack.empty()) {
      96         233 :     BasicBlock *Top = Stack.pop_back_val();
      97         466 :     if (!DA.isUniform(Top->getTerminator()))
      98             :       return false;
      99             : 
     100         418 :     for (BasicBlock *Pred : predecessors(Top)) {
     101          64 :       if (Visited.insert(Pred).second)
     102          51 :         Stack.push_back(Pred);
     103             :     }
     104             :   }
     105             : 
     106             :   return true;
     107             : }
     108             : 
     109          36 : static BasicBlock *unifyReturnBlockSet(Function &F,
     110             :                                        ArrayRef<BasicBlock *> ReturningBlocks,
     111             :                                        const TargetTransformInfo &TTI,
     112             :                                        StringRef Name) {
     113             :   // Otherwise, we need to insert a new basic block into the function, add a PHI
     114             :   // nodes (if the function returns values), and convert all of the return
     115             :   // instructions into unconditional branches.
     116             :   //
     117          72 :   BasicBlock *NewRetBlock = BasicBlock::Create(F.getContext(), Name, &F);
     118             : 
     119          36 :   PHINode *PN = nullptr;
     120          72 :   if (F.getReturnType()->isVoidTy()) {
     121          28 :     ReturnInst::Create(F.getContext(), nullptr, NewRetBlock);
     122             :   } else {
     123             :     // If the function doesn't return void... add a PHI node to the block...
     124          24 :     PN = PHINode::Create(F.getReturnType(), ReturningBlocks.size(),
     125             :                          "UnifiedRetVal");
     126          16 :     NewRetBlock->getInstList().push_back(PN);
     127           8 :     ReturnInst::Create(F.getContext(), PN, NewRetBlock);
     128             :   }
     129             : 
     130             :   // Loop over all of the blocks, replacing the return instruction with an
     131             :   // unconditional branch.
     132             :   //
     133         146 :   for (BasicBlock *BB : ReturningBlocks) {
     134             :     // Add an incoming element to the PHI node for every return instruction that
     135             :     // is merging into this new block...
     136          74 :     if (PN)
     137          32 :       PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
     138             : 
     139          74 :     BB->getInstList().pop_back();  // Remove the return insn
     140          74 :     BranchInst::Create(NewRetBlock, BB);
     141             :   }
     142             : 
     143         184 :   for (BasicBlock *BB : ReturningBlocks) {
     144             :     // Cleanup possible branch to unconditional branch to the return.
     145          74 :     SimplifyCFG(BB, TTI, 2);
     146             :   }
     147             : 
     148          36 :   return NewRetBlock;
     149             : }
     150             : 
     151       14671 : bool AMDGPUUnifyDivergentExitNodes::runOnFunction(Function &F) {
     152       14671 :   auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
     153       29342 :   if (PDT.getRoots().size() <= 1)
     154             :     return false;
     155             : 
     156          87 :   DivergenceAnalysis &DA = getAnalysis<DivergenceAnalysis>();
     157             : 
     158             :   // Loop over all of the blocks in a function, tracking all of the blocks that
     159             :   // return.
     160             :   //
     161          87 :   SmallVector<BasicBlock *, 4> ReturningBlocks;
     162         174 :   SmallVector<BasicBlock *, 4> UnreachableBlocks;
     163             : 
     164         444 :   for (BasicBlock *BB : PDT.getRoots()) {
     165         549 :     if (isa<ReturnInst>(BB->getTerminator())) {
     166         121 :       if (!isUniformlyReached(DA, *BB))
     167          61 :         ReturningBlocks.push_back(BB);
     168         124 :     } else if (isa<UnreachableInst>(BB->getTerminator())) {
     169          53 :       if (!isUniformlyReached(DA, *BB))
     170          27 :         UnreachableBlocks.push_back(BB);
     171             :     }
     172             :   }
     173             : 
     174          87 :   if (!UnreachableBlocks.empty()) {
     175          20 :     BasicBlock *UnreachableBlock = nullptr;
     176             : 
     177          20 :     if (UnreachableBlocks.size() == 1) {
     178          28 :       UnreachableBlock = UnreachableBlocks.front();
     179             :     } else {
     180          12 :       UnreachableBlock = BasicBlock::Create(F.getContext(),
     181             :                                             "UnifiedUnreachableBlock", &F);
     182          12 :       new UnreachableInst(F.getContext(), UnreachableBlock);
     183             : 
     184          31 :       for (BasicBlock *BB : UnreachableBlocks) {
     185          13 :         BB->getInstList().pop_back();  // Remove the unreachable inst.
     186          13 :         BranchInst::Create(UnreachableBlock, BB);
     187             :       }
     188             :     }
     189             : 
     190          20 :     if (!ReturningBlocks.empty()) {
     191             :       // Don't create a new unreachable inst if we have a return. The
     192             :       // structurizer/annotator can't handle the multiple exits
     193             : 
     194          16 :       Type *RetTy = F.getReturnType();
     195          16 :       Value *RetVal = RetTy->isVoidTy() ? nullptr : UndefValue::get(RetTy);
     196          16 :       UnreachableBlock->getInstList().pop_back();  // Remove the unreachable inst.
     197             : 
     198             :       Function *UnreachableIntrin =
     199          16 :         Intrinsic::getDeclaration(F.getParent(), Intrinsic::amdgcn_unreachable);
     200             : 
     201             :       // Insert a call to an intrinsic tracking that this is an unreachable
     202             :       // point, in case we want to kill the active lanes or something later.
     203          32 :       CallInst::Create(UnreachableIntrin, {}, "", UnreachableBlock);
     204             : 
     205             :       // Don't create a scalar trap. We would only want to trap if this code was
     206             :       // really reached, but a scalar trap would happen even if no lanes
     207             :       // actually reached here.
     208          16 :       ReturnInst::Create(F.getContext(), RetVal, UnreachableBlock);
     209          16 :       ReturningBlocks.push_back(UnreachableBlock);
     210             :     }
     211             :   }
     212             : 
     213             :   // Now handle return blocks.
     214          87 :   if (ReturningBlocks.empty())
     215             :     return false; // No blocks return
     216             : 
     217          39 :   if (ReturningBlocks.size() == 1)
     218             :     return false; // Already has a single return block
     219             : 
     220             :   const TargetTransformInfo &TTI
     221          36 :     = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
     222             : 
     223          72 :   unifyReturnBlockSet(F, ReturningBlocks, TTI, "UnifiedReturnBlock");
     224          36 :   return true;
     225             : }

Generated by: LCOV version 1.13