17
17
#include " SPIRVSubtarget.h"
18
18
#include " SPIRVTargetMachine.h"
19
19
#include " SPIRVUtils.h"
20
+ #include " llvm/ADT/DenseMap.h"
21
+ #include " llvm/ADT/SmallPtrSet.h"
20
22
#include " llvm/Analysis/LoopInfo.h"
21
23
#include " llvm/CodeGen/IntrinsicLowering.h"
22
24
#include " llvm/IR/CFG.h"
@@ -71,7 +73,7 @@ class SPIRVMergeRegionExitTargets : public FunctionPass {
71
73
// / terminator will take.
72
74
llvm::Value *createExitVariable (
73
75
BasicBlock *BB,
74
- const std::unordered_map <BasicBlock *, ConstantInt *> &TargetToValue) {
76
+ const DenseMap <BasicBlock *, ConstantInt *> &TargetToValue) {
75
77
auto *T = BB->getTerminator ();
76
78
if (isa<ReturnInst>(T))
77
79
return nullptr ;
@@ -103,7 +105,7 @@ class SPIRVMergeRegionExitTargets : public FunctionPass {
103
105
104
106
// / Replaces |BB|'s branch targets present in |ToReplace| with |NewTarget|.
105
107
void replaceBranchTargets (BasicBlock *BB,
106
- const std::unordered_set <BasicBlock *> ToReplace,
108
+ const SmallPtrSet <BasicBlock *, 4 > & ToReplace,
107
109
BasicBlock *NewTarget) {
108
110
auto *T = BB->getTerminator ();
109
111
if (isa<ReturnInst>(T))
@@ -133,7 +135,7 @@ class SPIRVMergeRegionExitTargets : public FunctionPass {
133
135
bool runOnConvergenceRegionNoRecurse (LoopInfo &LI,
134
136
const SPIRV::ConvergenceRegion *CR) {
135
137
// Gather all the exit targets for this region.
136
- std::unordered_set <BasicBlock *> ExitTargets;
138
+ SmallPtrSet <BasicBlock *, 4 > ExitTargets;
137
139
for (BasicBlock *Exit : CR->Exits ) {
138
140
for (BasicBlock *Target : gatherSuccessors (Exit)) {
139
141
if (CR->Blocks .count (Target) == 0 )
@@ -164,9 +166,10 @@ class SPIRVMergeRegionExitTargets : public FunctionPass {
164
166
165
167
// Creating one constant per distinct exit target. This will be route to the
166
168
// correct target.
167
- std::unordered_map <BasicBlock *, ConstantInt *> TargetToValue;
169
+ DenseMap <BasicBlock *, ConstantInt *> TargetToValue;
168
170
for (BasicBlock *Target : SortedExitTargets)
169
- TargetToValue.emplace (Target, Builder.getInt32 (TargetToValue.size ()));
171
+ TargetToValue.insert (
172
+ std::make_pair (Target, Builder.getInt32 (TargetToValue.size ())));
170
173
171
174
// Creating one variable per exit node, set to the constant matching the
172
175
// targeted external block.
@@ -184,12 +187,12 @@ class SPIRVMergeRegionExitTargets : public FunctionPass {
184
187
}
185
188
186
189
// Creating the switch to jump to the correct exit target.
187
- std::vector<std::pair<BasicBlock *, ConstantInt *>> CasesList (
188
- TargetToValue. begin (), TargetToValue. end () );
189
- llvm::SwitchInst *Sw =
190
- Builder. CreateSwitch (node, CasesList[ 0 ]. first , CasesList. size () - 1 ) ;
191
- for ( size_t i = 1 ; i < CasesList. size (); i++)
192
- Sw-> addCase (CasesList[i]. second , CasesList[i]. first );
190
+ llvm::SwitchInst *Sw = Builder. CreateSwitch (node, SortedExitTargets[ 0 ],
191
+ SortedExitTargets. size () - 1 );
192
+ for ( size_t i = 1 ; i < SortedExitTargets. size (); i++) {
193
+ BasicBlock *BB = SortedExitTargets[i] ;
194
+ Sw-> addCase (TargetToValue[BB], BB);
195
+ }
193
196
194
197
// Fix exit branches to redirect to the new exit.
195
198
for (auto Exit : CR->Exits )
0 commit comments