[BasicAA] Account for cycles when checking for same select condition
authorNikita Popov <npopov@redhat.com>
Fri, 14 Oct 2022 08:35:36 +0000 (10:35 +0200)
committerNikita Popov <npopov@redhat.com>
Fri, 14 Oct 2022 08:37:40 +0000 (10:37 +0200)
If we have translated across a cycle backedge, the same SSA value
for the condition might be referring to two different loop iterations.
Use the isValueEqualInPotentialCycles() helper to avoid assuming
equality in that case.

llvm/lib/Analysis/BasicAliasAnalysis.cpp
llvm/test/Analysis/BasicAA/phi-and-select.ll

index 16d5f39..9ec34fa 100644 (file)
@@ -1303,7 +1303,8 @@ BasicAAResult::aliasSelect(const SelectInst *SI, LocationSize SISize,
   // If the values are Selects with the same condition, we can do a more precise
   // check: just check for aliases between the values on corresponding arms.
   if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
-    if (SI->getCondition() == SI2->getCondition()) {
+    if (isValueEqualInPotentialCycles(SI->getCondition(),
+                                      SI2->getCondition())) {
       AliasResult Alias =
           AAQI.AAR.alias(MemoryLocation(SI->getTrueValue(), SISize),
                          MemoryLocation(SI2->getTrueValue(), V2Size), AAQI);
index e7614bb..8b5352b 100644 (file)
@@ -82,10 +82,9 @@ entry:
 ; On the first iteration, sel1 = a1, sel2 = a2, phi = a3
 ; On the second iteration, sel1 = a2, sel1 = a1, phi = a2
 ; As such, sel1 and phi may alias.
-; FIXME: Miscompile.
 ; CHECK-LABEL: Function: select_backedge
 ; CHECK: NoAlias:      i32* %sel1, i32* %sel2
-; CHECK: NoAlias:      i32* %phi, i32* %sel1
+; CHECK: MayAlias:     i32* %phi, i32* %sel1
 ; CHECK: MayAlias:     i32* %phi, i32* %sel2
 define void @select_backedge() {
 entry: