NVPTXSplitBBatBar.cpp revision 252723
1101704Smjacob//===- NVPTXSplitBBatBar.cpp - Split BB at Barrier  --*- C++ -*--===//
2139749Simp//
3101704Smjacob//                     The LLVM Compiler Infrastructure
4101704Smjacob//
5101704Smjacob// This file is distributed under the University of Illinois Open Source
6101704Smjacob// License. See LICENSE.TXT for details.
7101704Smjacob//
8101704Smjacob//===----------------------------------------------------------------------===//
9101704Smjacob// Split basic blocks so that a basic block that contains a barrier instruction
10101704Smjacob// only contains the barrier instruction.
11101704Smjacob//
12101704Smjacob//===----------------------------------------------------------------------===//
13101704Smjacob
14101704Smjacob#include "NVPTXSplitBBatBar.h"
15101704Smjacob#include "NVPTXUtilities.h"
16101704Smjacob#include "llvm/IR/Function.h"
17101704Smjacob#include "llvm/IR/Instructions.h"
18101704Smjacob#include "llvm/IR/IntrinsicInst.h"
19101704Smjacob#include "llvm/IR/Intrinsics.h"
20101704Smjacob#include "llvm/Support/InstIterator.h"
21101704Smjacob
22101704Smjacobusing namespace llvm;
23101704Smjacob
24101704Smjacobnamespace llvm { FunctionPass *createSplitBBatBarPass(); }
25101704Smjacob
26101704Smjacobchar NVPTXSplitBBatBar::ID = 0;
27101704Smjacob
28156000Smjacobbool NVPTXSplitBBatBar::runOnFunction(Function &F) {
29156000Smjacob
30156000Smjacob  SmallVector<Instruction *, 4> SplitPoints;
31156000Smjacob  bool changed = false;
32156000Smjacob
33156000Smjacob  // Collect all the split points in SplitPoints
34156000Smjacob  for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
35156000Smjacob    BasicBlock::iterator IB = BI->begin();
36156000Smjacob    BasicBlock::iterator II = IB;
37156000Smjacob    BasicBlock::iterator IE = BI->end();
38156000Smjacob
39156000Smjacob    // Skit the first intruction. No splitting is needed at this
40156000Smjacob    // point even if this is a bar.
41156000Smjacob    while (II != IE) {
42156000Smjacob      if (IntrinsicInst *inst = dyn_cast<IntrinsicInst>(II)) {
43156000Smjacob        Intrinsic::ID id = inst->getIntrinsicID();
44156000Smjacob        // If this is a barrier, split at this instruction
45156000Smjacob        // and the next instruction.
46156000Smjacob        if (llvm::isBarrierIntrinsic(id)) {
47156000Smjacob          if (II != IB)
48156000Smjacob            SplitPoints.push_back(II);
49156000Smjacob          II++;
50156000Smjacob          if ((II != IE) && (!II->isTerminator())) {
51156000Smjacob            SplitPoints.push_back(II);
52156000Smjacob            II++;
53156000Smjacob          }
54156000Smjacob          continue;
55156000Smjacob        }
56156000Smjacob      }
57156000Smjacob      II++;
58147883Sscottl    }
59156000Smjacob  }
60156000Smjacob
61159052Smjacob  for (unsigned i = 0; i != SplitPoints.size(); i++) {
62159052Smjacob    changed = true;
63159052Smjacob    Instruction *inst = SplitPoints[i];
64159052Smjacob    inst->getParent()->splitBasicBlock(inst, "bar_split");
65101704Smjacob  }
66101704Smjacob
67147883Sscottl  return changed;
68147883Sscottl}
69147883Sscottl
70147883Sscottl// This interface will most likely not be necessary, because this pass will
71147883Sscottl// not be invoked by the driver, but will be used as a prerequisite to
72147883Sscottl// another pass.
73147883SscottlFunctionPass *llvm::createSplitBBatBarPass() { return new NVPTXSplitBBatBar(); }
74147883Sscottl