Skip to content

Commit 135e521

Browse files
authored
[NewGVN] Set parent to the temporal instructions that are generated during phi-of-ops optimization (#66314)
- Test for future commit in NewGVN - [NewGVN] Set parent to the temporal instructions that are generated during phi-of-ops optimization
1 parent 04ba475 commit 135e521

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

llvm/lib/Transforms/Scalar/NewGVN.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,7 @@ NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
19041904
LastPredInfo = PI;
19051905
// In phi of ops cases, we may have predicate info that we are evaluating
19061906
// in a different context.
1907-
if (!DT->dominates(PBranch->To, getBlockForValue(I)))
1907+
if (!DT->dominates(PBranch->To, I->getParent()))
19081908
continue;
19091909
// TODO: Along the false edge, we may know more things too, like
19101910
// icmp of
@@ -2765,6 +2765,9 @@ NewGVN::makePossiblePHIOfOps(Instruction *I,
27652765
// Clone the instruction, create an expression from it that is
27662766
// translated back into the predecessor, and see if we have a leader.
27672767
Instruction *ValueOp = I->clone();
2768+
// Emit the temporal instruction in the predecessor basic block where the
2769+
// corresponding value is defined.
2770+
ValueOp->insertBefore(PredBB->getTerminator());
27682771
if (MemAccess)
27692772
TempToMemory.insert({ValueOp, MemAccess});
27702773
bool SafeForPHIOfOps = true;
@@ -2794,7 +2797,7 @@ NewGVN::makePossiblePHIOfOps(Instruction *I,
27942797
FoundVal = !SafeForPHIOfOps ? nullptr
27952798
: findLeaderForInst(ValueOp, Visited,
27962799
MemAccess, I, PredBB);
2797-
ValueOp->deleteValue();
2800+
ValueOp->eraseFromParent();
27982801
if (!FoundVal) {
27992802
// We failed to find a leader for the current ValueOp, but this might
28002803
// change in case of the translated operands change.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2+
; RUN: opt -passes=newgvn -S < %s | FileCheck %s
3+
4+
@c = global i32 0, align 4
5+
6+
; Function Attrs: nounwind optsize uwtable
7+
define dso_local i32 @main(i1 %cond, i32 %0, i32 %1) {
8+
; CHECK-LABEL: define dso_local i32 @main(
9+
; CHECK-SAME: i1 [[COND:%.*]], i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
10+
; CHECK-NEXT: entry:
11+
; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[IF_END6:%.*]]
12+
; CHECK: if.then:
13+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[TMP0]], 1
14+
; CHECK-NEXT: [[TOBOOL1_NOT:%.*]] = icmp eq i32 [[TMP1]], 0
15+
; CHECK-NEXT: br i1 [[TOBOOL1_NOT]], label [[IF_END6]], label [[IF_THEN2:%.*]]
16+
; CHECK: if.then2:
17+
; CHECK-NEXT: [[TOBOOL3_NOT:%.*]] = icmp ne i32 [[XOR]], 0
18+
; CHECK-NEXT: tail call void @llvm.assume(i1 [[TOBOOL3_NOT]])
19+
; CHECK-NEXT: br label [[IF_END6]]
20+
; CHECK: if.end6:
21+
; CHECK-NEXT: [[F_0:%.*]] = phi i32 [ undef, [[ENTRY:%.*]] ], [ [[XOR]], [[IF_THEN]] ], [ [[XOR]], [[IF_THEN2]] ]
22+
; CHECK-NEXT: [[NOT:%.*]] = xor i32 [[F_0]], -1
23+
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr @c, align 4
24+
; CHECK-NEXT: [[OR:%.*]] = or i32 [[TMP2]], [[NOT]]
25+
; CHECK-NEXT: [[TOBOOL7_NOT:%.*]] = icmp eq i32 [[OR]], 0
26+
; CHECK-NEXT: [[TOBOOL9_NOT:%.*]] = icmp eq i32 [[F_0]], 0
27+
; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[TOBOOL7_NOT]], [[TOBOOL9_NOT]]
28+
; CHECK-NEXT: br i1 [[OR_COND]], label [[IF_END10:%.*]], label [[WHILE_COND_PREHEADER:%.*]]
29+
; CHECK: while.cond.preheader:
30+
; CHECK-NEXT: ret i32 1
31+
; CHECK: if.end10:
32+
; CHECK-NEXT: ret i32 0
33+
;
34+
entry:
35+
br i1 %cond, label %if.then, label %if.end6
36+
37+
if.then: ; preds = %entry
38+
%xor = xor i32 %0, 1
39+
%tobool1.not = icmp eq i32 %1, 0
40+
br i1 %tobool1.not, label %if.end6, label %if.then2
41+
42+
if.then2: ; preds = %if.then
43+
%tobool3.not = icmp ne i32 %xor, 0
44+
tail call void @llvm.assume(i1 %tobool3.not)
45+
br label %if.end6
46+
47+
if.end6: ; preds = %if.then2, %if.then, %entry
48+
%f.0 = phi i32 [ undef, %entry ], [ %xor, %if.then ], [ %xor, %if.then2 ]
49+
%not = xor i32 %f.0, -1
50+
%2 = load i32, ptr @c, align 4
51+
%or = or i32 %2, %not
52+
%tobool7.not = icmp eq i32 %or, 0
53+
%tobool9.not = icmp eq i32 %f.0, 0
54+
%or.cond = or i1 %tobool7.not, %tobool9.not
55+
br i1 %or.cond, label %if.end10, label %while.cond.preheader
56+
57+
while.cond.preheader: ; preds = %if.end6
58+
ret i32 1
59+
60+
if.end10: ; preds = %if.end6
61+
ret i32 0
62+
}
63+
64+
declare void @llvm.assume(i1 noundef)

0 commit comments

Comments
 (0)