Skip to content

Commit cc3b893

Browse files
committed
Support check alias between two instructions using getModRefInfo
1 parent 5cd91b8 commit cc3b893

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

llvm/include/llvm/Analysis/AliasAnalysis.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ class AAResults {
381381
MemoryLocation::getBeforeOrAfter(V2));
382382
}
383383

384+
/// A convenience wrapper around the \c isNoAlias for two instructions.
385+
bool isNoAlias(const Instruction *I1, const Instruction *I2) {
386+
return getModRefInfo(&*I1, MemoryLocation::getOrNone(&*I2)) ==
387+
ModRefInfo::NoModRef &&
388+
getModRefInfo(&*I2, MemoryLocation::getOrNone(&*I1)) ==
389+
ModRefInfo::NoModRef;
390+
}
391+
384392
/// A trivial helper function to check to see if the specified pointers are
385393
/// must-alias.
386394
bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {

llvm/lib/Transforms/Utils/FlattenCFG.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,8 @@ bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Block1, BasicBlock *Block2,
357357
for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
358358
if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
359359
// Check alias with Head2.
360-
if (AA) {
361-
MemoryLocation Loc1 = MemoryLocation::get(&*iter1);
362-
MemoryLocation Loc2 = MemoryLocation::get(&*BI);
363-
if (!AA->isNoAlias(Loc1, Loc2))
364-
return false;
365-
}
360+
if (!AA || !AA->isNoAlias(&*iter1, &*BI))
361+
return false;
366362
}
367363
}
368364
}

llvm/test/Transforms/Util/flatten-cfg.ll

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2-
; RUN: opt -passes='require<aa>,flatten-cfg' -S < %s | FileCheck %s
2+
; RUN: opt -passes=flatten-cfg -S < %s | FileCheck %s
33

44

55
; This test checks whether the pass completes without a crash.
@@ -310,6 +310,9 @@ exit:
310310
ret i1 %cmp.y
311311
}
312312

313+
declare void @foo()
314+
declare void @bar() #1
315+
313316
; Test that two if-regions are not merged when there's potential aliasing
314317
; between a store in the first if-region and a load in the second if-region's header
315318
define i32 @test_alias(i32 %a, i32 %b, ptr %p1, ptr %p2) {
@@ -354,6 +357,52 @@ if.end2:
354357
ret i32 0
355358
}
356359

360+
; Test that two if-regions are not merged when there's potential aliasing
361+
; between a fuction call in the first if-region and a load in the second if-region's header
362+
define i32 @test_alias_2(i32 %a, i32 %b) {
363+
; CHECK-LABEL: define i32 @test_alias_2
364+
; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
365+
; CHECK-NEXT: entry:
366+
; CHECK-NEXT: [[P:%.*]] = alloca i32, align 4
367+
; CHECK-NEXT: store i32 42, ptr [[P]], align 4
368+
; CHECK-NEXT: [[COND1:%.*]] = icmp eq i32 [[A]], 0
369+
; CHECK-NEXT: br i1 [[COND1]], label [[IF_THEN1:%.*]], label [[IF_END1:%.*]]
370+
; CHECK: if.then1:
371+
; CHECK-NEXT: call void @foo()
372+
; CHECK-NEXT: br label [[IF_END1]]
373+
; CHECK: if.end1:
374+
; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr @g, align 4
375+
; CHECK-NEXT: [[COND2:%.*]] = icmp eq i32 [[B]], 0
376+
; CHECK-NEXT: br i1 [[COND2]], label [[IF_THEN2:%.*]], label [[IF_END2:%.*]]
377+
; CHECK: if.then2:
378+
; CHECK-NEXT: call void @foo()
379+
; CHECK-NEXT: br label [[IF_END2]]
380+
; CHECK: if.end2:
381+
; CHECK-NEXT: ret i32 0
382+
;
383+
entry:
384+
%p = alloca i32
385+
store i32 42, ptr %p
386+
%cond1 = icmp eq i32 %a, 0
387+
br i1 %cond1, label %if.then1, label %if.end1
388+
389+
if.then1:
390+
call void @foo()
391+
br label %if.end1
392+
393+
if.end1:
394+
%val = load i32, ptr @g
395+
%cond2 = icmp eq i32 %b, 0
396+
br i1 %cond2, label %if.then2, label %if.end2
397+
398+
if.then2:
399+
call void @foo()
400+
br label %if.end2
401+
402+
if.end2:
403+
ret i32 0
404+
}
405+
357406
; Test that two if-regions are merged when there's no potential aliasing
358407
; between a store in the first if-region and a load in the second if-region's header
359408
define i32 @test_no_alias(i32 %a, i32 %b) {
@@ -369,18 +418,20 @@ define i32 @test_no_alias(i32 %a, i32 %b) {
369418
; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN2:%.*]], label [[IF_END2:%.*]]
370419
; CHECK: if.then2:
371420
; CHECK-NEXT: store i32 100, ptr [[P]], align 4
421+
; CHECK-NEXT: call void @bar()
372422
; CHECK-NEXT: br label [[IF_END2]]
373423
; CHECK: if.end2:
374424
; CHECK-NEXT: ret i32 0
375425
;
376-
entry:
426+
entry:
377427
%p = alloca i32
378428
store i32 42, ptr %p
379429
%cond1 = icmp eq i32 %a, 0
380430
br i1 %cond1, label %if.then1, label %if.end1
381431

382432
if.then1:
383433
store i32 100, ptr %p ; No alias with the load below
434+
call void @bar() ; No alias with the load below since it's a pure function
384435
br label %if.end1
385436

386437
if.end1:
@@ -390,8 +441,11 @@ if.end1:
390441

391442
if.then2:
392443
store i32 100, ptr %p
444+
call void @bar()
393445
br label %if.end2
394446

395447
if.end2:
396448
ret i32 0
397-
}
449+
}
450+
451+
attributes #1 = { readnone willreturn nounwind }

0 commit comments

Comments
 (0)