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"
24 namespace llvm { FunctionPass *createSplitBBatBarPass(); }
26 char NVPTXSplitBBatBar::ID = 0;
28 bool NVPTXSplitBBatBar::runOnFunction(Function &F) {
30 SmallVector<Instruction *, 4> SplitPoints;
33 // Collect all the split points in SplitPoints
34 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
35 BasicBlock::iterator IB = BI->begin();
36 BasicBlock::iterator II = IB;
37 BasicBlock::iterator IE = BI->end();
39 // Skit the first intruction. No splitting is needed at this
40 // point even if this is a bar.
42 if (IntrinsicInst *inst = dyn_cast<IntrinsicInst>(II)) {
43 Intrinsic::ID id = inst->getIntrinsicID();
44 // If this is a barrier, split at this instruction
45 // and the next instruction.
46 if (llvm::isBarrierIntrinsic(id)) {
48 SplitPoints.push_back(II);
50 if ((II != IE) && (!II->isTerminator())) {
51 SplitPoints.push_back(II);
61 for (unsigned i = 0; i != SplitPoints.size(); i++) {
63 Instruction *inst = SplitPoints[i];
64 inst->getParent()->splitBasicBlock(inst, "bar_split");
70 // This interface will most likely not be necessary, because this pass will
71 // not be invoked by the driver, but will be used as a prerequisite to
73 FunctionPass *llvm::createSplitBBatBarPass() { return new NVPTXSplitBBatBar(); }