Skip to content

Commit

Permalink
!fixup check noundef
Browse files Browse the repository at this point in the history
  • Loading branch information
fhahn committed Sep 19, 2024
1 parent 3ddc96f commit 31a095c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
20 changes: 12 additions & 8 deletions llvm/lib/Transforms/Scalar/EarlyCSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,14 +1590,18 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
if (auto *I = dyn_cast<Instruction>(Op))
combineMetadataForCSE(I, &Inst, false);

// If the load has align metadata, preserve it via an alignment
// assumption. Note that this doesn't use salavageKnowledge, as we need
// to create the assumption for the value we replaced the load with.
if (auto *AlignMD = Inst.getMetadata(LLVMContext::MD_align)) {
auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
if (Op->getPointerAlignment(SQ.DL).value() % A->getZExtValue() != 0) {
IRBuilder B(&Inst);
B.CreateAlignmentAssumption(SQ.DL, Op, A);
// If the load has align and noundef metadata, preserve it via an
// alignment assumption. Note that this doesn't use salavageKnowledge,
// as we need to create the assumption for the value we replaced the
// load with.
if (Inst.hasMetadata(LLVMContext::MD_noundef)) {
if (auto *AlignMD = Inst.getMetadata(LLVMContext::MD_align)) {
auto *A = mdconst::extract<ConstantInt>(AlignMD->getOperand(0));
if (Op->getPointerAlignment(SQ.DL).value() % A->getZExtValue() !=
0) {
IRBuilder B(&Inst);
B.CreateAlignmentAssumption(SQ.DL, Op, A);
}
}
}

Expand Down
35 changes: 27 additions & 8 deletions llvm/test/Transforms/EarlyCSE/materialize-align-assumptions.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@

declare void @foo(ptr)

define ptr @align_replacement_does_not_have_align_metadata_missing_noundef(ptr noalias %p) {
; CHECK-LABEL: define ptr @align_replacement_does_not_have_align_metadata_missing_noundef(
; CHECK-SAME: ptr noalias [[P:%.*]]) {
; CHECK-NEXT: [[L_1:%.*]] = load ptr, ptr [[P]], align 8
; CHECK-NEXT: call void @foo(ptr [[L_1]])
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[L_1]], i64 4
; CHECK-NEXT: store ptr [[GEP]], ptr [[P]], align 8
; CHECK-NEXT: ret ptr [[GEP]]
;
%l.1 = load ptr, ptr %p, align 8
call void @foo(ptr %l.1)
%l.2 = load ptr, ptr %p, align 8
%gep = getelementptr i8, ptr %l.2, i64 4
store ptr %gep, ptr %p, align 8
%l.3 = load ptr, ptr %p, align 8, !align !0
ret ptr %l.3
}

define ptr @align_replacement_does_not_have_align_metadata(ptr noalias %p) {
; CHECK-LABEL: define ptr @align_replacement_does_not_have_align_metadata(
; CHECK-SAME: ptr noalias [[P:%.*]]) {
Expand All @@ -18,7 +36,7 @@ define ptr @align_replacement_does_not_have_align_metadata(ptr noalias %p) {
%l.2 = load ptr, ptr %p, align 8
%gep = getelementptr i8, ptr %l.2, i64 4
store ptr %gep, ptr %p, align 8
%l.3 = load ptr, ptr %p, align 8, !align !0
%l.3 = load ptr, ptr %p, align 8, !align !0, !noundef !{}
ret ptr %l.3
}

Expand All @@ -34,7 +52,7 @@ define ptr @align_replacement_does_not_have_align_metadata2(ptr noalias %p) {
%l.1 = load ptr, ptr %p, align 8
%gep = getelementptr i8, ptr %l.1, i64 4
store ptr %gep, ptr %p, align 8
%l.2 = load ptr, ptr %p, align 8, !align !0
%l.2 = load ptr, ptr %p, align 8, !align !0, !noundef !{}
ret ptr %l.2
}

Expand All @@ -61,7 +79,7 @@ define ptr @align_replacement_has_smaller_alignment(ptr noalias %p) {
;
%l.1 = load ptr, ptr %p, align 8, !align !0
call void @foo(ptr %l.1)
%l.2 = load ptr, ptr %p, align 8, !align !1
%l.2 = load ptr, ptr %p, align 8, !align !1, !noundef !{}
ret ptr %l.2
}

Expand All @@ -70,12 +88,12 @@ define ptr @align_replacement_has_larger_alignment(ptr %p) {
; CHECK-SAME: ptr [[P:%.*]]) {
; CHECK-NEXT: [[L_1:%.*]] = load ptr, ptr [[P]], align 8, !align [[META1:![0-9]+]]
; CHECK-NEXT: call void @foo(ptr [[L_1]])
; CHECK-NEXT: [[L_2:%.*]] = load ptr, ptr [[P]], align 8, !align [[META0]]
; CHECK-NEXT: [[L_2:%.*]] = load ptr, ptr [[P]], align 8, !align [[META0]], !noundef [[META2:![0-9]+]]
; CHECK-NEXT: ret ptr [[L_2]]
;
%l.1 = load ptr, ptr %p, align 8, !align !1
call void @foo(ptr %l.1)
%l.2 = load ptr, ptr %p, align 8, !align !0
%l.2 = load ptr, ptr %p, align 8, !align !0, !noundef !{}
ret ptr %l.2
}

Expand All @@ -84,12 +102,12 @@ define ptr @align_1(ptr %p) {
; CHECK-SAME: ptr [[P:%.*]]) {
; CHECK-NEXT: [[L_1:%.*]] = load ptr, ptr [[P]], align 8
; CHECK-NEXT: call void @foo(ptr [[L_1]])
; CHECK-NEXT: [[L_2:%.*]] = load ptr, ptr [[P]], align 8, !align [[META2:![0-9]+]]
; CHECK-NEXT: [[L_2:%.*]] = load ptr, ptr [[P]], align 8, !align [[META3:![0-9]+]], !noundef [[META2]]
; CHECK-NEXT: ret ptr [[L_2]]
;
%l.1 = load ptr, ptr %p, align 8
call void @foo(ptr %l.1)
%l.2 = load ptr, ptr %p, align 8, !align !2
%l.2 = load ptr, ptr %p, align 8, !align !2, !noundef !{}
ret ptr %l.2
}

Expand All @@ -99,5 +117,6 @@ define ptr @align_1(ptr %p) {
;.
; CHECK: [[META0]] = !{i64 4}
; CHECK: [[META1]] = !{i64 8}
; CHECK: [[META2]] = !{i64 1}
; CHECK: [[META2]] = !{}
; CHECK: [[META3]] = !{i64 1}
;.

0 comments on commit 31a095c

Please sign in to comment.