llvm.org GIT mirror llvm / e98a889
PR1255 related changes (case ranges): LowerSwitch::Clusterify : main functinality was replaced with CRSBuilder::optimize, so big part of Clusterify's code was reduced. test/Transform/LowerSwitch/feature.ll - this test was refactored: grep + count was replaced with FileCheck usage. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157384 91177308-0d34-0410-b5e6-96231b3b80d8 Stepan Dyatkovskiy 7 years ago
2 changed file(s) with 111 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
6565 BasicBlock* OrigBlock, BasicBlock* Default);
6666 unsigned Clusterify(CaseVector& Cases, SwitchInst *SI);
6767 };
68
69 /// The comparison function for sorting the switch case values in the vector.
70 /// WARNING: Case ranges should be disjoint!
71 struct CaseCmp {
72 bool operator () (const LowerSwitch::CaseRange& C1,
73 const LowerSwitch::CaseRange& C2) {
74
75 const ConstantInt* CI1 = cast(C1.Low);
76 const ConstantInt* CI2 = cast(C2.High);
77 return CI1->getValue().slt(CI2->getValue());
78 }
79 };
8068 }
8169
8270 char LowerSwitch::ID = 0;
158146 Function::iterator FI = OrigBlock;
159147 F->getBasicBlockList().insert(++FI, NewNode);
160148
161 ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT,
149 ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_ULT,
162150 Val, Pivot.Low, "Pivot");
163151 NewNode->getInstList().push_back(Comp);
164152 BranchInst::Create(LBranch, RBranch, Comp, NewNode);
233221
234222 // Clusterify - Transform simple list of Cases into list of CaseRange's
235223 unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
236 unsigned numCmps = 0;
224
225 CRSBuilder TheClusterifier;
237226
238227 // Start with "simple" cases
239 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
240 Cases.push_back(CaseRange(i.getCaseValue(), i.getCaseValue(),
241 i.getCaseSuccessor()));
228 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
229 i != e; ++i) {
230 BasicBlock *SuccBB = i.getCaseSuccessor();
231 ConstantRangesSet CRS = i.getCaseValueEx();
232 TheClusterifier.add(CRS, SuccBB);
233 }
242234
243 std::sort(Cases.begin(), Cases.end(), CaseCmp());
244
245 // Merge case into clusters
246 if (Cases.size()>=2)
247 for (CaseItr I=Cases.begin(), J=llvm::next(Cases.begin()); J!=Cases.end(); ) {
248 int64_t nextValue = cast(J->Low)->getSExtValue();
249 int64_t currentValue = cast(I->High)->getSExtValue();
250 BasicBlock* nextBB = J->BB;
251 BasicBlock* currentBB = I->BB;
252
253 // If the two neighboring cases go to the same destination, merge them
254 // into a single case.
255 if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
256 I->High = J->High;
257 J = Cases.erase(J);
258 } else {
259 I = J++;
260 }
261 }
262
263 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
264 if (I->Low != I->High)
235 TheClusterifier.optimize();
236
237 size_t numCmps = 0;
238 for (CRSBuilder::RangeIterator i = TheClusterifier.begin(),
239 e = TheClusterifier.end(); i != e; ++i, ++numCmps) {
240 CRSBuilder::Cluster &C = *i;
241 Cases.push_back(CaseRange(C.first.Low, C.first.High, C.second));
242 if (C.first.Low != C.first.High)
265243 // A range counts double, since it requires two compares.
266244 ++numCmps;
267245 }
268246
269 return numCmps;
247 return numCmps;
270248 }
271249
272250 // processSwitchInst - Replace the specified switch instruction with a sequence
None ; RUN: opt < %s -lowerswitch -S > %t
1 ; RUN: grep slt %t | count 10
2 ; RUN: grep ule %t | count 3
3 ; RUN: grep eq %t | count 9
0 ; RUN: opt < %s -lowerswitch -S | FileCheck %s
1
2 ; We have switch on input.
3 ; On output we should got binary comparison tree. Check that all is fine.
4
5 ;CHECK: entry:
6 ;CHECK-NEXT: br label %NodeBlock37
7
8 ;CHECK: NodeBlock37: ; preds = %entry
9 ;CHECK-NEXT: %Pivot38 = icmp ult i32 %tmp158, 11
10 ;CHECK-NEXT: br i1 %Pivot38, label %NodeBlock13, label %NodeBlock35
11
12 ;CHECK: NodeBlock35: ; preds = %NodeBlock37
13 ;CHECK-NEXT: %Pivot36 = icmp ult i32 %tmp158, 14
14 ;CHECK-NEXT: br i1 %Pivot36, label %NodeBlock23, label %NodeBlock33
15
16 ;CHECK: NodeBlock33: ; preds = %NodeBlock35
17 ;CHECK-NEXT: %Pivot34 = icmp ult i32 %tmp158, 15
18 ;CHECK-NEXT: br i1 %Pivot34, label %LeafBlock25, label %NodeBlock31
19
20 ;CHECK: NodeBlock31: ; preds = %NodeBlock33
21 ;CHECK-NEXT: %Pivot32 = icmp ult i32 %tmp158, -6
22 ;CHECK-NEXT: br i1 %Pivot32, label %LeafBlock27, label %LeafBlock29
23
24 ;CHECK: LeafBlock29: ; preds = %NodeBlock31
25 ;CHECK-NEXT: %tmp158.off = add i32 %tmp158, 6
26 ;CHECK-NEXT: %SwitchLeaf30 = icmp ule i32 %tmp158.off, 4
27 ;CHECK-NEXT: br i1 %SwitchLeaf30, label %bb338, label %NewDefault
28
29 ;CHECK: LeafBlock27: ; preds = %NodeBlock31
30 ;CHECK-NEXT: %SwitchLeaf28 = icmp eq i32 %tmp158, 15
31 ;CHECK-NEXT: br i1 %SwitchLeaf28, label %bb334, label %NewDefault
32
33 ;CHECK: LeafBlock25: ; preds = %NodeBlock33
34 ;CHECK-NEXT: %SwitchLeaf26 = icmp eq i32 %tmp158, 14
35 ;CHECK-NEXT: br i1 %SwitchLeaf26, label %bb332, label %NewDefault
36
37 ;CHECK: NodeBlock23: ; preds = %NodeBlock35
38 ;CHECK-NEXT: %Pivot24 = icmp ult i32 %tmp158, 12
39 ;CHECK-NEXT: br i1 %Pivot24, label %LeafBlock15, label %NodeBlock21
40
41 ;CHECK: NodeBlock21: ; preds = %NodeBlock23
42 ;CHECK-NEXT: %Pivot22 = icmp ult i32 %tmp158, 13
43 ;CHECK-NEXT: br i1 %Pivot22, label %LeafBlock17, label %LeafBlock19
44
45 ;CHECK: LeafBlock19: ; preds = %NodeBlock21
46 ;CHECK-NEXT: %SwitchLeaf20 = icmp eq i32 %tmp158, 13
47 ;CHECK-NEXT: br i1 %SwitchLeaf20, label %bb330, label %NewDefault
48
49 ;CHECK: LeafBlock17: ; preds = %NodeBlock21
50 ;CHECK-NEXT: %SwitchLeaf18 = icmp eq i32 %tmp158, 12
51 ;CHECK-NEXT: br i1 %SwitchLeaf18, label %bb328, label %NewDefault
52
53 ;CHECK: LeafBlock15: ; preds = %NodeBlock23
54 ;CHECK-NEXT: %SwitchLeaf16 = icmp eq i32 %tmp158, 11
55 ;CHECK-NEXT: br i1 %SwitchLeaf16, label %bb326, label %NewDefault
56
57 ;CHECK: NodeBlock13: ; preds = %NodeBlock37
58 ;CHECK-NEXT: %Pivot14 = icmp ult i32 %tmp158, 8
59 ;CHECK-NEXT: br i1 %Pivot14, label %NodeBlock, label %NodeBlock11
60
61 ;CHECK: NodeBlock11: ; preds = %NodeBlock13
62 ;CHECK-NEXT: %Pivot12 = icmp ult i32 %tmp158, 9
63 ;CHECK-NEXT: br i1 %Pivot12, label %LeafBlock3, label %NodeBlock9
64
65 ;CHECK: NodeBlock9: ; preds = %NodeBlock11
66 ;CHECK-NEXT: %Pivot10 = icmp ult i32 %tmp158, 10
67 ;CHECK-NEXT: br i1 %Pivot10, label %LeafBlock5, label %LeafBlock7
68
69 ;CHECK: LeafBlock7: ; preds = %NodeBlock9
70 ;CHECK-NEXT: %SwitchLeaf8 = icmp eq i32 %tmp158, 10
71 ;CHECK-NEXT: br i1 %SwitchLeaf8, label %bb324, label %NewDefault
72
73 ;CHECK: LeafBlock5: ; preds = %NodeBlock9
74 ;CHECK-NEXT: %SwitchLeaf6 = icmp eq i32 %tmp158, 9
75 ;CHECK-NEXT: br i1 %SwitchLeaf6, label %bb322, label %NewDefault
76
77 ;CHECK: LeafBlock3: ; preds = %NodeBlock11
78 ;CHECK-NEXT: %SwitchLeaf4 = icmp eq i32 %tmp158, 8
79 ;CHECK-NEXT: br i1 %SwitchLeaf4, label %bb338, label %NewDefault
80
81 ;CHECK: NodeBlock: ; preds = %NodeBlock13
82 ;CHECK-NEXT: %Pivot = icmp ult i32 %tmp158, 7
83 ;CHECK-NEXT: br i1 %Pivot, label %LeafBlock, label %LeafBlock1
84
85 ;CHECK: LeafBlock1: ; preds = %NodeBlock
86 ;CHECK-NEXT: %SwitchLeaf2 = icmp eq i32 %tmp158, 7
87 ;CHECK-NEXT: br i1 %SwitchLeaf2, label %bb, label %NewDefault
88
89 ;CHECK: LeafBlock: ; preds = %NodeBlock
90 ;CHECK-NEXT: %SwitchLeaf = icmp ule i32 %tmp158, 6
91 ;CHECK-NEXT: br i1 %SwitchLeaf, label %bb338, label %NewDefault
492
593 define i32 @main(i32 %tmp158) {
694 entry:
95
796 switch i32 %tmp158, label %bb336 [
897 i32 -2, label %bb338
998 i32 -3, label %bb338