Skip to content

Commit 438ce41

Browse files
committed
[CHERIoT] Fix AllNonPositive condition in the Cheriot non-reassociation path in LICM.
The previous implementation was incorrectly too permissive, allowing hoisting that should have been disallowed.
1 parent 3f866ed commit 438ce41

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,9 @@ static bool hoistGEP(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo,
25462546
(ABI == "cheriot" || ABI == "cheriot-baremetal");
25472547
if (MustStayInBounds) {
25482548
auto NonPositive = [&](Value *V) {
2549-
return !isKnownPositive(V, SimplifyQuery(DL, DT, AC, GEP));
2549+
if (const auto *Cst = dyn_cast<Constant>(V))
2550+
return Cst->isNullValue();
2551+
return isKnownNegative(V, SimplifyQuery(DL, DT, AC, GEP));
25502552
};
25512553
bool AllNonNeg = all_of(Src->indices(), NonNegative) &&
25522554
all_of(GEP->indices(), NonNegative);

llvm/test/Transforms/LICM/cheriot-gep-reorder.ll

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,48 @@ do.end:
5656
ret void
5757
}
5858

59+
; CHECK-LABEL: @zot
60+
; CHECK: bb:
61+
; CHECK-NOT: getelementptr
62+
; CHECK: bb12:
63+
; CHECK: getelementptr
64+
; CHECK: getelementptr
65+
; CHECK: call void @bar
66+
67+
define void @zot(ptr addrspace(200) %arg) {
68+
bb:
69+
%alloca = alloca [25 x i64], align 8, addrspace(200)
70+
br label %bb8
71+
72+
bb8:
73+
%phi = phi i32 [ 0, %bb ], [ %add16, %bb14 ]
74+
%phi9 = phi i32 [ 0, %bb ], [ %select, %bb14 ]
75+
%icmp = icmp ult i32 %phi9, 256
76+
br i1 %icmp, label %bb10, label %bb17
77+
78+
bb10:
79+
%icmp11 = icmp eq i32 %phi, 4
80+
br i1 %icmp11, label %bb12, label %bb14
81+
82+
bb12:
83+
%getelementptr = getelementptr inbounds nuw i16, ptr addrspace(200) %arg, i32 %phi9
84+
%getelementptr13 = getelementptr inbounds i8, ptr addrspace(200) %getelementptr, i32 -6
85+
call void @bar(ptr addrspace(200) %getelementptr13)
86+
br label %bb14
87+
88+
bb14:
89+
%icmp15 = icmp ne i32 %phi9, 255
90+
%add = add nuw nsw i32 %phi9, 2
91+
%zext = zext i1 %icmp15 to i32
92+
%add16 = add i32 %zext, %phi
93+
%select = select i1 %icmp15, i32 %add, i32 256
94+
br label %bb8
95+
96+
bb17:
97+
ret void
98+
}
5999

60-
declare void @bar(ptr addrspace(200) noundef)
100+
declare void @bar(ptr addrspace(200))
61101

62102
!llvm.module.flags = !{!0}
63103
!0 = !{i32 1, !"target-abi", !"cheriot"}

0 commit comments

Comments
 (0)