CodeMoverUtilsTest.cpp
32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
//===- CodeMoverUtils.cpp - Unit tests for CodeMoverUtils ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/CodeMoverUtils.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/DependenceAnalysis.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/PostDominators.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
#include "gtest/gtest.h"
using namespace llvm;
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
SMDiagnostic Err;
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
if (!Mod)
Err.print("CodeMoverUtilsTests", errs());
return Mod;
}
static void run(Module &M, StringRef FuncName,
function_ref<void(Function &F, DominatorTree &DT,
PostDominatorTree &PDT, DependenceInfo &DI)>
Test) {
auto *F = M.getFunction(FuncName);
DominatorTree DT(*F);
PostDominatorTree PDT(*F);
TargetLibraryInfoImpl TLII;
TargetLibraryInfo TLI(TLII);
AssumptionCache AC(*F);
AliasAnalysis AA(TLI);
LoopInfo LI(DT);
ScalarEvolution SE(*F, TLI, AC, DT, LI);
DependenceInfo DI(F, &AA, &SE, &LI);
Test(*F, DT, PDT, DI);
}
static BasicBlock *getBasicBlockByName(Function &F, StringRef Name) {
for (BasicBlock &BB : F)
if (BB.getName() == Name)
return &BB;
llvm_unreachable("Expected to find basic block!");
}
static Instruction *getInstructionByName(Function &F, StringRef Name) {
for (BasicBlock &BB : F)
for (Instruction &I : BB)
if (I.getName() == Name)
return &I;
llvm_unreachable("Expected to find instruction!");
}
TEST(CodeMoverUtils, IsControlFlowEquivalentSimpleTest) {
LLVMContext C;
// void foo(int &i, bool cond1, bool cond2) {
// if (cond1)
// i = 1;
// if (cond1)
// i = 2;
// if (cond2)
// i = 3;
// }
std::unique_ptr<Module> M =
parseIR(C, R"(define void @foo(i32* %i, i1 %cond1, i1 %cond2) {
entry:
br i1 %cond1, label %if.first, label %if.first.end
if.first:
store i32 1, i32* %i, align 4
br label %if.first.end
if.first.end:
br i1 %cond1, label %if.second, label %if.second.end
if.second:
store i32 2, i32* %i, align 4
br label %if.second.end
if.second.end:
br i1 %cond2, label %if.third, label %if.third.end
if.third:
store i32 3, i32* %i, align 4
br label %if.third.end
if.third.end:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
BasicBlock *FirstIfBody = getBasicBlockByName(F, "if.first");
EXPECT_TRUE(
isControlFlowEquivalent(*FirstIfBody, *FirstIfBody, DT, PDT));
BasicBlock *SecondIfBody = getBasicBlockByName(F, "if.second");
EXPECT_TRUE(
isControlFlowEquivalent(*FirstIfBody, *SecondIfBody, DT, PDT));
BasicBlock *ThirdIfBody = getBasicBlockByName(F, "if.third");
EXPECT_FALSE(
isControlFlowEquivalent(*FirstIfBody, *ThirdIfBody, DT, PDT));
EXPECT_FALSE(
isControlFlowEquivalent(*SecondIfBody, *ThirdIfBody, DT, PDT));
});
}
TEST(CodeMoverUtils, IsControlFlowEquivalentOppositeCondTest) {
LLVMContext C;
// void foo(int &i, unsigned X, unsigned Y) {
// if (X < Y)
// i = 1;
// if (Y > X)
// i = 2;
// if (X >= Y)
// i = 3;
// else
// i = 4;
// if (X == Y)
// i = 5;
// if (Y == X)
// i = 6;
// else
// i = 7;
// if (X != Y)
// i = 8;
// else
// i = 9;
// }
std::unique_ptr<Module> M =
parseIR(C, R"(define void @foo(i32* %i, i32 %X, i32 %Y) {
entry:
%cmp1 = icmp ult i32 %X, %Y
br i1 %cmp1, label %if.first, label %if.first.end
if.first:
store i32 1, i32* %i, align 4
br label %if.first.end
if.first.end:
%cmp2 = icmp ugt i32 %Y, %X
br i1 %cmp2, label %if.second, label %if.second.end
if.second:
store i32 2, i32* %i, align 4
br label %if.second.end
if.second.end:
%cmp3 = icmp uge i32 %X, %Y
br i1 %cmp3, label %if.third, label %if.third.else
if.third:
store i32 3, i32* %i, align 4
br label %if.third.end
if.third.else:
store i32 4, i32* %i, align 4
br label %if.third.end
if.third.end:
%cmp4 = icmp eq i32 %X, %Y
br i1 %cmp4, label %if.fourth, label %if.fourth.end
if.fourth:
store i32 5, i32* %i, align 4
br label %if.fourth.end
if.fourth.end:
%cmp5 = icmp eq i32 %Y, %X
br i1 %cmp5, label %if.fifth, label %if.fifth.else
if.fifth:
store i32 6, i32* %i, align 4
br label %if.fifth.end
if.fifth.else:
store i32 7, i32* %i, align 4
br label %if.fifth.end
if.fifth.end:
%cmp6 = icmp ne i32 %X, %Y
br i1 %cmp6, label %if.sixth, label %if.sixth.else
if.sixth:
store i32 8, i32* %i, align 4
br label %if.sixth.end
if.sixth.else:
store i32 9, i32* %i, align 4
br label %if.sixth.end
if.sixth.end:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
BasicBlock *FirstIfBody = getBasicBlockByName(F, "if.first");
BasicBlock *SecondIfBody = getBasicBlockByName(F, "if.second");
BasicBlock *ThirdIfBody = getBasicBlockByName(F, "if.third");
BasicBlock *ThirdElseBody = getBasicBlockByName(F, "if.third.else");
EXPECT_TRUE(
isControlFlowEquivalent(*FirstIfBody, *ThirdElseBody, DT, PDT));
EXPECT_TRUE(
isControlFlowEquivalent(*SecondIfBody, *ThirdElseBody, DT, PDT));
EXPECT_FALSE(
isControlFlowEquivalent(*ThirdIfBody, *ThirdElseBody, DT, PDT));
BasicBlock *FourthIfBody = getBasicBlockByName(F, "if.fourth");
BasicBlock *FifthIfBody = getBasicBlockByName(F, "if.fifth");
BasicBlock *FifthElseBody = getBasicBlockByName(F, "if.fifth.else");
EXPECT_FALSE(
isControlFlowEquivalent(*FifthIfBody, *FifthElseBody, DT, PDT));
BasicBlock *SixthIfBody = getBasicBlockByName(F, "if.sixth");
EXPECT_TRUE(
isControlFlowEquivalent(*FifthElseBody, *SixthIfBody, DT, PDT));
BasicBlock *SixthElseBody = getBasicBlockByName(F, "if.sixth.else");
EXPECT_TRUE(
isControlFlowEquivalent(*FourthIfBody, *SixthElseBody, DT, PDT));
EXPECT_TRUE(
isControlFlowEquivalent(*FifthIfBody, *SixthElseBody, DT, PDT));
});
}
TEST(CodeMoverUtils, IsControlFlowEquivalentCondNestTest) {
LLVMContext C;
// void foo(int &i, bool cond1, bool cond2) {
// if (cond1)
// if (cond2)
// i = 1;
// if (cond2)
// if (cond1)
// i = 2;
// }
std::unique_ptr<Module> M =
parseIR(C, R"(define void @foo(i32* %i, i1 %cond1, i1 %cond2) {
entry:
br i1 %cond1, label %if.outer.first, label %if.first.end
if.outer.first:
br i1 %cond2, label %if.inner.first, label %if.first.end
if.inner.first:
store i32 1, i32* %i, align 4
br label %if.first.end
if.first.end:
br i1 %cond2, label %if.outer.second, label %if.second.end
if.outer.second:
br i1 %cond1, label %if.inner.second, label %if.second.end
if.inner.second:
store i32 2, i32* %i, align 4
br label %if.second.end
if.second.end:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
BasicBlock *FirstOuterIfBody = getBasicBlockByName(F, "if.outer.first");
BasicBlock *FirstInnerIfBody = getBasicBlockByName(F, "if.inner.first");
BasicBlock *SecondOuterIfBody =
getBasicBlockByName(F, "if.outer.second");
BasicBlock *SecondInnerIfBody =
getBasicBlockByName(F, "if.inner.second");
EXPECT_TRUE(isControlFlowEquivalent(*FirstInnerIfBody,
*SecondInnerIfBody, DT, PDT));
EXPECT_FALSE(isControlFlowEquivalent(*FirstOuterIfBody,
*SecondOuterIfBody, DT, PDT));
EXPECT_FALSE(isControlFlowEquivalent(*FirstOuterIfBody,
*SecondInnerIfBody, DT, PDT));
EXPECT_FALSE(isControlFlowEquivalent(*FirstInnerIfBody,
*SecondOuterIfBody, DT, PDT));
});
}
TEST(CodeMoverUtils, IsControlFlowEquivalentImbalanceTest) {
LLVMContext C;
// void foo(int &i, bool cond1, bool cond2) {
// if (cond1)
// if (cond2)
// if (cond3)
// i = 1;
// if (cond2)
// if (cond3)
// i = 2;
// if (cond1)
// if (cond1)
// i = 3;
// if (cond1)
// i = 4;
// }
std::unique_ptr<Module> M = parseIR(
C, R"(define void @foo(i32* %i, i1 %cond1, i1 %cond2, i1 %cond3) {
entry:
br i1 %cond1, label %if.outer.first, label %if.first.end
if.outer.first:
br i1 %cond2, label %if.middle.first, label %if.first.end
if.middle.first:
br i1 %cond3, label %if.inner.first, label %if.first.end
if.inner.first:
store i32 1, i32* %i, align 4
br label %if.first.end
if.first.end:
br i1 %cond2, label %if.outer.second, label %if.second.end
if.outer.second:
br i1 %cond3, label %if.inner.second, label %if.second.end
if.inner.second:
store i32 2, i32* %i, align 4
br label %if.second.end
if.second.end:
br i1 %cond1, label %if.outer.third, label %if.third.end
if.outer.third:
br i1 %cond1, label %if.inner.third, label %if.third.end
if.inner.third:
store i32 3, i32* %i, align 4
br label %if.third.end
if.third.end:
br i1 %cond1, label %if.fourth, label %if.fourth.end
if.fourth:
store i32 4, i32* %i, align 4
br label %if.fourth.end
if.fourth.end:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
BasicBlock *FirstIfBody = getBasicBlockByName(F, "if.inner.first");
BasicBlock *SecondIfBody = getBasicBlockByName(F, "if.inner.second");
EXPECT_FALSE(
isControlFlowEquivalent(*FirstIfBody, *SecondIfBody, DT, PDT));
BasicBlock *ThirdIfBody = getBasicBlockByName(F, "if.inner.third");
BasicBlock *FourthIfBody = getBasicBlockByName(F, "if.fourth");
EXPECT_TRUE(
isControlFlowEquivalent(*ThirdIfBody, *FourthIfBody, DT, PDT));
});
}
TEST(CodeMoverUtils, IsControlFlowEquivalentPointerTest) {
LLVMContext C;
// void foo(int &i, int *cond) {
// if (*cond)
// i = 1;
// if (*cond)
// i = 2;
// *cond = 1;
// if (*cond)
// i = 3;
// }
std::unique_ptr<Module> M =
parseIR(C, R"(define void @foo(i32* %i, i32* %cond) {
entry:
%0 = load i32, i32* %cond, align 4
%tobool1 = icmp ne i32 %0, 0
br i1 %tobool1, label %if.first, label %if.first.end
if.first:
store i32 1, i32* %i, align 4
br label %if.first.end
if.first.end:
%1 = load i32, i32* %cond, align 4
%tobool2 = icmp ne i32 %1, 0
br i1 %tobool2, label %if.second, label %if.second.end
if.second:
store i32 2, i32* %i, align 4
br label %if.second.end
if.second.end:
store i32 1, i32* %cond, align 4
%2 = load i32, i32* %cond, align 4
%tobool3 = icmp ne i32 %2, 0
br i1 %tobool3, label %if.third, label %if.third.end
if.third:
store i32 3, i32* %i, align 4
br label %if.third.end
if.third.end:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
BasicBlock *FirstIfBody = getBasicBlockByName(F, "if.first");
BasicBlock *SecondIfBody = getBasicBlockByName(F, "if.second");
// Limitation: if we can prove cond haven't been modify between %0 and
// %1, then we can prove FirstIfBody and SecondIfBody are control flow
// equivalent.
EXPECT_FALSE(
isControlFlowEquivalent(*FirstIfBody, *SecondIfBody, DT, PDT));
BasicBlock *ThirdIfBody = getBasicBlockByName(F, "if.third");
EXPECT_FALSE(
isControlFlowEquivalent(*FirstIfBody, *ThirdIfBody, DT, PDT));
EXPECT_FALSE(
isControlFlowEquivalent(*SecondIfBody, *ThirdIfBody, DT, PDT));
});
}
TEST(CodeMoverUtils, IsControlFlowEquivalentNotPostdomTest) {
LLVMContext C;
// void foo(bool cond1, bool cond2) {
// if (cond1) {
// if (cond2)
// return;
// } else
// if (cond2)
// return;
// return;
// }
std::unique_ptr<Module> M =
parseIR(C, R"(define void @foo(i1 %cond1, i1 %cond2) {
idom:
br i1 %cond1, label %succ0, label %succ1
succ0:
br i1 %cond2, label %succ0ret, label %succ0succ1
succ0ret:
ret void
succ0succ1:
br label %bb
succ1:
br i1 %cond2, label %succ1ret, label %succ1succ1
succ1ret:
ret void
succ1succ1:
br label %bb
bb:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
BasicBlock &Idom = F.front();
assert(Idom.getName() == "idom" && "Expecting BasicBlock idom");
BasicBlock &BB = F.back();
assert(BB.getName() == "bb" && "Expecting BasicBlock bb");
EXPECT_FALSE(isControlFlowEquivalent(Idom, BB, DT, PDT));
});
}
TEST(CodeMoverUtils, IsSafeToMoveTest1) {
LLVMContext C;
// void safecall() noexcept willreturn nosync;
// void unsafecall();
// void foo(int * __restrict__ A, int * __restrict__ B, int * __restrict__ C,
// long N) {
// X = N / 1;
// safecall();
// unsafecall1();
// unsafecall2();
// for (long i = 0; i < N; ++i) {
// A[5] = 5;
// A[i] = 0;
// B[i] = A[i];
// C[i] = A[i];
// A[6] = 6;
// }
// }
std::unique_ptr<Module> M = parseIR(
C, R"(define void @foo(i32* noalias %A, i32* noalias %B, i32* noalias %C
, i64 %N) {
entry:
%X = sdiv i64 1, %N
call void @safecall()
%cmp1 = icmp slt i64 0, %N
call void @unsafecall1()
call void @unsafecall2()
br i1 %cmp1, label %for.body, label %for.end
for.body:
%i = phi i64 [ 0, %entry ], [ %inc, %for.body ]
%arrayidx_A5 = getelementptr inbounds i32, i32* %A, i64 5
store i32 5, i32* %arrayidx_A5, align 4
%arrayidx_A = getelementptr inbounds i32, i32* %A, i64 %i
store i32 0, i32* %arrayidx_A, align 4
%load1 = load i32, i32* %arrayidx_A, align 4
%arrayidx_B = getelementptr inbounds i32, i32* %B, i64 %i
store i32 %load1, i32* %arrayidx_B, align 4
%load2 = load i32, i32* %arrayidx_A, align 4
%arrayidx_C = getelementptr inbounds i32, i32* %C, i64 %i
store i32 %load2, i32* %arrayidx_C, align 4
%arrayidx_A6 = getelementptr inbounds i32, i32* %A, i64 6
store i32 6, i32* %arrayidx_A6, align 4
%inc = add nsw i64 %i, 1
%cmp = icmp slt i64 %inc, %N
br i1 %cmp, label %for.body, label %for.end
for.end:
ret void
}
declare void @safecall() nounwind nosync willreturn
declare void @unsafecall1()
declare void @unsafecall2())");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
BasicBlock *Entry = getBasicBlockByName(F, "entry");
Instruction *CI_safecall = Entry->front().getNextNode();
assert(isa<CallInst>(CI_safecall) &&
"Expecting CI_safecall to be a CallInst");
Instruction *CI_unsafecall = CI_safecall->getNextNode()->getNextNode();
assert(isa<CallInst>(CI_unsafecall) &&
"Expecting CI_unsafecall to be a CallInst");
BasicBlock *ForBody = getBasicBlockByName(F, "for.body");
Instruction &PN = ForBody->front();
assert(isa<PHINode>(PN) && "Expecting PN to be a PHINode");
Instruction *SI_A5 =
getInstructionByName(F, "arrayidx_A5")->getNextNode();
Instruction *SI = getInstructionByName(F, "arrayidx_A")->getNextNode();
Instruction *LI1 = getInstructionByName(F, "load1");
Instruction *LI2 = getInstructionByName(F, "load2");
Instruction *SI_A6 =
getInstructionByName(F, "arrayidx_A6")->getNextNode();
// Can move after CI_safecall, as it does not throw, not synchronize, or
// must return.
EXPECT_TRUE(isSafeToMoveBefore(*CI_safecall->getPrevNode(),
*CI_safecall->getNextNode(), DT, &PDT,
&DI));
// Cannot move CI_unsafecall, as it may throw.
EXPECT_FALSE(isSafeToMoveBefore(*CI_unsafecall->getNextNode(),
*CI_unsafecall, DT, &PDT, &DI));
// Moving instruction to non control flow equivalent places are not
// supported.
EXPECT_FALSE(
isSafeToMoveBefore(*SI_A5, *Entry->getTerminator(), DT, &PDT, &DI));
// Moving PHINode is not supported.
EXPECT_FALSE(isSafeToMoveBefore(PN, *PN.getNextNode()->getNextNode(),
DT, &PDT, &DI));
// Cannot move non-PHINode before PHINode.
EXPECT_FALSE(isSafeToMoveBefore(*PN.getNextNode(), PN, DT, &PDT, &DI));
// Moving Terminator is not supported.
EXPECT_FALSE(isSafeToMoveBefore(*Entry->getTerminator(),
*PN.getNextNode(), DT, &PDT, &DI));
// Cannot move %arrayidx_A after SI, as SI is its user.
EXPECT_FALSE(isSafeToMoveBefore(*SI->getPrevNode(), *SI->getNextNode(),
DT, &PDT, &DI));
// Cannot move SI before %arrayidx_A, as %arrayidx_A is its operand.
EXPECT_FALSE(
isSafeToMoveBefore(*SI, *SI->getPrevNode(), DT, &PDT, &DI));
// Cannot move LI2 after SI_A6, as there is a flow dependence.
EXPECT_FALSE(
isSafeToMoveBefore(*LI2, *SI_A6->getNextNode(), DT, &PDT, &DI));
// Cannot move SI after LI1, as there is a anti dependence.
EXPECT_FALSE(
isSafeToMoveBefore(*SI, *LI1->getNextNode(), DT, &PDT, &DI));
// Cannot move SI_A5 after SI, as there is a output dependence.
EXPECT_FALSE(isSafeToMoveBefore(*SI_A5, *LI1, DT, &PDT, &DI));
// Can move LI2 before LI1, as there is only an input dependence.
EXPECT_TRUE(isSafeToMoveBefore(*LI2, *LI1, DT, &PDT, &DI));
});
}
TEST(CodeMoverUtils, IsSafeToMoveTest2) {
LLVMContext C;
std::unique_ptr<Module> M =
parseIR(C, R"(define void @foo(i1 %cond, i32 %op0, i32 %op1) {
entry:
br i1 %cond, label %if.then.first, label %if.end.first
if.then.first:
%add = add i32 %op0, %op1
%user = add i32 %add, 1
br label %if.end.first
if.end.first:
br i1 %cond, label %if.then.second, label %if.end.second
if.then.second:
%sub_op0 = add i32 %op0, 1
%sub = sub i32 %sub_op0, %op1
br label %if.end.second
if.end.second:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
Instruction *AddInst = getInstructionByName(F, "add");
Instruction *SubInst = getInstructionByName(F, "sub");
// Cannot move as %user uses %add and %sub doesn't dominates %user.
EXPECT_FALSE(isSafeToMoveBefore(*AddInst, *SubInst, DT, &PDT, &DI));
// Cannot move as %sub_op0 is an operand of %sub and %add doesn't
// dominates %sub_op0.
EXPECT_FALSE(isSafeToMoveBefore(*SubInst, *AddInst, DT, &PDT, &DI));
});
}
TEST(CodeMoverUtils, IsSafeToMoveTest3) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(C, R"(define void @foo(i64 %N) {
entry:
br label %for.body
for.body:
%i = phi i64 [ 0, %entry ], [ %inc, %for.latch ]
%inc = add nsw i64 %i, 1
br label %for.latch
for.latch:
%cmp = icmp slt i64 %inc, %N
br i1 %cmp, label %for.body, label %for.end
for.end:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
Instruction *IncInst = getInstructionByName(F, "inc");
Instruction *CmpInst = getInstructionByName(F, "cmp");
// Can move as the incoming block of %inc for %i (%for.latch) dominated
// by %cmp.
EXPECT_TRUE(isSafeToMoveBefore(*IncInst, *CmpInst, DT, &PDT, &DI));
});
}
TEST(CodeMoverUtils, IsSafeToMoveTest4) {
LLVMContext C;
std::unique_ptr<Module> M =
parseIR(C, R"(define void @foo(i1 %cond, i32 %op0, i32 %op1) {
entry:
br i1 %cond, label %if.end.first, label %if.then.first
if.then.first:
%add = add i32 %op0, %op1
%user = add i32 %add, 1
br label %if.end.first
if.end.first:
br i1 %cond, label %if.end.second, label %if.then.second
if.then.second:
%sub_op0 = add i32 %op0, 1
%sub = sub i32 %sub_op0, %op1
br label %if.end.second
if.end.second:
ret void
})");
run(*M, "foo",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
Instruction *AddInst = getInstructionByName(F, "add");
Instruction *SubInst = getInstructionByName(F, "sub");
// Cannot move as %user uses %add and %sub doesn't dominates %user.
EXPECT_FALSE(isSafeToMoveBefore(*AddInst, *SubInst, DT, &PDT, &DI));
// Cannot move as %sub_op0 is an operand of %sub and %add doesn't
// dominates %sub_op0.
EXPECT_FALSE(isSafeToMoveBefore(*SubInst, *AddInst, DT, &PDT, &DI));
});
}
TEST(CodeMoverUtils, IsSafeToMoveTest5) {
LLVMContext C;
std::unique_ptr<Module> M =
parseIR(C, R"(define void @dependence(i32* noalias %A, i32* noalias %B){
entry:
store i32 0, i32* %A, align 4 ; storeA0
store i32 2, i32* %A, align 4 ; storeA1
%tmp0 = load i32, i32* %A, align 4 ; loadA0
store i32 1, i32* %B, align 4 ; storeB0
%tmp1 = load i32, i32* %A, align 4 ; loadA1
store i32 2, i32* %A, align 4 ; storeA2
store i32 4, i32* %B, align 4 ; StoreB1
%tmp2 = load i32, i32* %A, align 4 ; loadA2
%tmp3 = load i32, i32* %A, align 4 ; loadA3
%tmp4 = load i32, i32* %B, align 4 ; loadB2
%tmp5 = load i32, i32* %B, align 4 ; loadB3
ret void
})");
run(*M, "dependence",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
Instruction *LoadA0 = getInstructionByName(F, "tmp0");
Instruction *LoadA1 = getInstructionByName(F, "tmp1");
Instruction *LoadA2 = getInstructionByName(F, "tmp2");
Instruction *LoadA3 = getInstructionByName(F, "tmp3");
Instruction *LoadB2 = getInstructionByName(F, "tmp4");
Instruction *LoadB3 = getInstructionByName(F, "tmp5");
Instruction *StoreA1 = LoadA0->getPrevNode();
Instruction *StoreA0 = StoreA1->getPrevNode();
Instruction *StoreB0 = LoadA0->getNextNode();
Instruction *StoreB1 = LoadA2->getPrevNode();
Instruction *StoreA2 = StoreB1->getPrevNode();
// Input forward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA2, *LoadB2, DT, &PDT, &DI));
// Input backward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA3, *LoadA2, DT, &PDT, &DI));
// Output forward dependency
EXPECT_FALSE(isSafeToMoveBefore(*StoreA0, *LoadA0, DT, &PDT, &DI));
// Output backward dependency
EXPECT_FALSE(isSafeToMoveBefore(*StoreA1, *StoreA0, DT, &PDT, &DI));
// Flow forward dependency
EXPECT_FALSE(isSafeToMoveBefore(*StoreA1, *StoreB0, DT, &PDT, &DI));
// Flow backward dependency
EXPECT_FALSE(isSafeToMoveBefore(*LoadA0, *StoreA1, DT, &PDT, &DI));
// Anti forward dependency
EXPECT_FALSE(isSafeToMoveBefore(*LoadA1, *StoreB1, DT, &PDT, &DI));
// Anti backward dependency
EXPECT_FALSE(isSafeToMoveBefore(*StoreA2, *LoadA1, DT, &PDT, &DI));
// No input backward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadB2, *LoadA3, DT, &PDT, &DI));
// No input forward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA3, *LoadB3, DT, &PDT, &DI));
// No Output forward dependency
EXPECT_TRUE(isSafeToMoveBefore(*StoreA2, *LoadA2, DT, &PDT, &DI));
// No Output backward dependency
EXPECT_TRUE(isSafeToMoveBefore(*StoreB1, *StoreA2, DT, &PDT, &DI));
// No flow forward dependency
EXPECT_TRUE(isSafeToMoveBefore(*StoreB0, *StoreA2, DT, &PDT, &DI));
// No flow backward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA1, *StoreB0, DT, &PDT, &DI));
// No anti backward dependency
EXPECT_TRUE(isSafeToMoveBefore(*StoreB0, *LoadA0, DT, &PDT, &DI));
// No anti forward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA0, *LoadA1, DT, &PDT, &DI));
});
}
TEST(CodeMoverUtils, IsSafeToMoveTest6) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(
C, R"(define void @dependence(i1 %cond, i32* noalias %A, i32* noalias %B){
entry:
br i1 %cond, label %bb0, label %bb1
bb0:
br label %bb1
bb1:
store i32 0, i32* %A, align 4 ; storeA0
br i1 %cond, label %bb2, label %bb3
bb2:
br label %bb3
bb3:
store i32 2, i32* %A, align 4 ; storeA1
br i1 %cond, label %bb4, label %bb5
bb4:
br label %bb5
bb5:
%tmp0 = load i32, i32* %A, align 4 ; loadA0
br i1 %cond, label %bb6, label %bb7
bb6:
br label %bb7
bb7:
store i32 1, i32* %B, align 4 ; storeB0
br i1 %cond, label %bb8, label %bb9
bb8:
br label %bb9
bb9:
%tmp1 = load i32, i32* %A, align 4 ; loadA1
br i1 %cond, label %bb10, label %bb11
bb10:
br label %bb11
bb11:
store i32 2, i32* %A, align 4 ; storeA2
br i1 %cond, label %bb12, label %bb13
bb12:
br label %bb13
bb13:
store i32 4, i32* %B, align 4 ; StoreB1
br i1 %cond, label %bb14, label %bb15
bb14:
br label %bb15
bb15:
%tmp2 = load i32, i32* %A, align 4 ; loadA2
br i1 %cond, label %bb16, label %bb17
bb16:
br label %bb17
bb17:
%tmp3 = load i32, i32* %A, align 4 ; loadA3
br i1 %cond, label %bb18, label %bb19
bb18:
br label %bb19
bb19:
%tmp4 = load i32, i32* %B, align 4 ; loadB2
br i1 %cond, label %bb20, label %bb21
bb20:
br label %bb21
bb21:
%tmp5 = load i32, i32* %B, align 4 ; loadB3
ret void
})");
run(*M, "dependence",
[&](Function &F, DominatorTree &DT, PostDominatorTree &PDT,
DependenceInfo &DI) {
BasicBlock *BB1 = getBasicBlockByName(F, "bb1");
BasicBlock *BB3 = getBasicBlockByName(F, "bb3");
BasicBlock *BB7 = getBasicBlockByName(F, "bb7");
BasicBlock *BB11 = getBasicBlockByName(F, "bb11");
BasicBlock *BB13 = getBasicBlockByName(F, "bb13");
Instruction *LoadA0 = getInstructionByName(F, "tmp0");
Instruction *LoadA1 = getInstructionByName(F, "tmp1");
Instruction *LoadA2 = getInstructionByName(F, "tmp2");
Instruction *LoadA3 = getInstructionByName(F, "tmp3");
Instruction *LoadB2 = getInstructionByName(F, "tmp4");
Instruction *LoadB3 = getInstructionByName(F, "tmp5");
Instruction &StoreA1 = BB3->front();
Instruction &StoreA0 = BB1->front();
Instruction &StoreB0 = BB7->front();
Instruction &StoreB1 = BB13->front();
Instruction &StoreA2 = BB11->front();
// Input forward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA2, *LoadB2, DT, &PDT, &DI));
// Input backward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA3, *LoadA2, DT, &PDT, &DI));
// Output forward dependency
EXPECT_FALSE(isSafeToMoveBefore(StoreA0, *LoadA0, DT, &PDT, &DI));
// Output backward dependency
EXPECT_FALSE(isSafeToMoveBefore(StoreA1, StoreA0, DT, &PDT, &DI));
// Flow forward dependency
EXPECT_FALSE(isSafeToMoveBefore(StoreA1, StoreB0, DT, &PDT, &DI));
// Flow backward dependency
EXPECT_FALSE(isSafeToMoveBefore(*LoadA0, StoreA1, DT, &PDT, &DI));
// Anti forward dependency
EXPECT_FALSE(isSafeToMoveBefore(*LoadA1, StoreB1, DT, &PDT, &DI));
// Anti backward dependency
EXPECT_FALSE(isSafeToMoveBefore(StoreA2, *LoadA1, DT, &PDT, &DI));
// No input backward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadB2, *LoadA3, DT, &PDT, &DI));
// No input forward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA3, *LoadB3, DT, &PDT, &DI));
// No Output forward dependency
EXPECT_TRUE(isSafeToMoveBefore(StoreA2, *LoadA2, DT, &PDT, &DI));
// No Output backward dependency
EXPECT_TRUE(isSafeToMoveBefore(StoreB1, StoreA2, DT, &PDT, &DI));
// No flow forward dependency
EXPECT_TRUE(isSafeToMoveBefore(StoreB0, StoreA2, DT, &PDT, &DI));
// No flow backward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA1, StoreB0, DT, &PDT, &DI));
// No anti backward dependency
EXPECT_TRUE(isSafeToMoveBefore(StoreB0, *LoadA0, DT, &PDT, &DI));
// No anti forward dependency
EXPECT_TRUE(isSafeToMoveBefore(*LoadA0, *LoadA1, DT, &PDT, &DI));
});
}