1 //===- NVPTXSplitBBatBar.cpp - Split BB at Barrier --*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Split basic blocks so that a basic block that contains a barrier instruction
10 // only contains the barrier instruction.
12 //===----------------------------------------------------------------------===//
14 #include "NVPTXSplitBBatBar.h"
15 #include "NVPTXUtilities.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/Support/InstIterator.h"
25 FunctionPass *createSplitBBatBarPass();
28 char NVPTXSplitBBatBar::ID = 0;
30 bool NVPTXSplitBBatBar::runOnFunction(Function &F) {
32 SmallVector<Instruction *, 4> SplitPoints;
35 // Collect all the split points in SplitPoints
36 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
37 BasicBlock::iterator IB = BI->begin();
38 BasicBlock::iterator II = IB;
39 BasicBlock::iterator IE = BI->end();
41 // Skit the first intruction. No splitting is needed at this
42 // point even if this is a bar.
44 if (IntrinsicInst *inst = dyn_cast<IntrinsicInst>(II)) {
45 Intrinsic::ID id = inst->getIntrinsicID();
46 // If this is a barrier, split at this instruction
47 // and the next instruction.
48 if (llvm::isBarrierIntrinsic(id)) {
50 SplitPoints.push_back(II);
52 if ((II != IE) && (!II->isTerminator())) {
53 SplitPoints.push_back(II);
63 for (unsigned i = 0; i != SplitPoints.size(); i++) {
65 Instruction *inst = SplitPoints[i];
66 inst->getParent()->splitBasicBlock(inst, "bar_split");
72 // This interface will most likely not be necessary, because this pass will
73 // not be invoked by the driver, but will be used as a prerequisite to
75 FunctionPass *llvm::createSplitBBatBarPass() {
76 return new NVPTXSplitBBatBar();