38 SPIRVMergeRegionExitTargets() : FunctionPass(ID) {}
44 const DenseMap<BasicBlock *, ConstantInt *> &TargetToValue) {
49 return TargetToValue.
lookup(BI->getSuccessor());
52 Builder.SetInsertPoint(
T);
58 if (
LHS ==
nullptr ||
RHS ==
nullptr)
60 return Builder.CreateSelect(BI->getCondition(),
LHS,
RHS);
67 AllocaInst *CreateVariable(Function &
F,
Type *
Type,
69 const DataLayout &
DL =
F.getDataLayout();
70 return new AllocaInst(
Type,
DL.getAllocaAddrSpace(),
nullptr,
"reg",
76 bool runOnConvergenceRegionNoRecurse(LoopInfo &LI,
77 SPIRV::ConvergenceRegion *CR) {
79 SmallPtrSet<BasicBlock *, 4> ExitTargets;
80 for (BasicBlock *Exit : CR->
Exits) {
83 ExitTargets.
insert(Target);
88 if (ExitTargets.
size() <= 1)
96 AllocaInst *
Variable = CreateVariable(*
F, Builder.getInt32Ty(),
97 F->begin()->getFirstInsertionPt());
102 std::vector<BasicBlock *> SortedExitTargets;
103 std::vector<BasicBlock *> SortedExits;
104 for (BasicBlock &BB : *
F) {
105 if (ExitTargets.
count(&BB) != 0)
106 SortedExitTargets.push_back(&BB);
107 if (CR->
Exits.count(&BB) != 0)
108 SortedExits.push_back(&BB);
113 DenseMap<BasicBlock *, ConstantInt *> TargetToValue;
114 for (BasicBlock *Target : SortedExitTargets)
116 std::make_pair(Target, Builder.getInt32(TargetToValue.
size())));
120 std::vector<std::pair<BasicBlock *, Value *>> ExitToVariable;
121 for (
auto Exit : SortedExits) {
124 B2.SetInsertPoint(
Exit->getFirstInsertionPt());
125 B2.CreateStore(
Value, Variable);
126 ExitToVariable.emplace_back(std::make_pair(Exit,
Value));
129 llvm::Value *
Load = Builder.CreateLoad(Builder.getInt32Ty(), Variable);
132 llvm::SwitchInst *Sw = Builder.CreateSwitch(Load, SortedExitTargets[0],
133 SortedExitTargets.size() - 1);
134 for (
size_t i = 1; i < SortedExitTargets.size(); i++) {
136 Sw->
addCase(TargetToValue[BB], BB);
140 for (
auto Exit : CR->
Exits) {
144 I.getUse()->set(NewExitTarget);
159 bool runOnConvergenceRegion(LoopInfo &LI, SPIRV::ConvergenceRegion *CR) {
161 if (runOnConvergenceRegion(LI, Child))
164 return runOnConvergenceRegionNoRecurse(LI, CR);
170 void validateRegionExits(
const SPIRV::ConvergenceRegion *CR) {
172 validateRegionExits(Child);
174 std::unordered_set<BasicBlock *> ExitTargets;
175 for (
auto *Exit : CR->
Exits) {
178 ExitTargets.insert(BB);
182 assert(ExitTargets.size() <= 1);
187 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
188 auto *TopLevelRegion =
189 getAnalysis<SPIRVConvergenceRegionAnalysisWrapperPass>()
191 .getWritableTopLevelRegion();
197 bool modified =
false;
198 while (runOnConvergenceRegion(LI, TopLevelRegion)) {
202#if !defined(NDEBUG) || defined(EXPENSIVE_CHECKS)
203 validateRegionExits(TopLevelRegion);
208 void getAnalysisUsage(AnalysisUsage &AU)
const override {
211 AU.
addRequired<SPIRVConvergenceRegionAnalysisWrapperPass>();
213 AU.
addPreserved<SPIRVConvergenceRegionAnalysisWrapperPass>();
214 FunctionPass::getAnalysisUsage(AU);
219char SPIRVMergeRegionExitTargets::ID = 0;
222 "SPIRV split region exit blocks",
false,
false)
232 return new SPIRVMergeRegionExitTargets();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file defines the SmallPtrSet class.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
const Function * getParent() const
Return the enclosing method, or null if none.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Legacy analysis pass which computes a DominatorTree.
FunctionPass class - This class is used to implement most global optimizations.
The legacy pass manager's analysis pass to compute loop information.
SmallVector< ConvergenceRegion * > Children
SmallPtrSet< BasicBlock *, 2 > Exits
ConvergenceRegion * Parent
SmallPtrSet< BasicBlock *, 8 > Blocks
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
LLVM_ABI void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto successors(const MachineBasicBlock *BB)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
FunctionPass * createSPIRVMergeRegionExitTargetsPass()