Skip to content

Commit 1f67e19

Browse files
committed
Correctly intersect
1 parent 7a2702f commit 1f67e19

File tree

6 files changed

+55
-13
lines changed

6 files changed

+55
-13
lines changed

llvm/include/llvm/IR/ConstantRangeList.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class [[nodiscard]] ConstantRangeList {
3535
ConstantRangeList(ArrayRef<ConstantRange> RangesRef) {
3636
assert(isOrderedRanges(RangesRef));
3737
for (const ConstantRange &R : RangesRef) {
38-
assert(R.getBitWidth() == getBitWidth());
38+
assert(empty() || R.getBitWidth() == getBitWidth());
3939
Ranges.push_back(R);
4040
}
4141
}
@@ -60,7 +60,7 @@ class [[nodiscard]] ConstantRangeList {
6060
bool empty() const { return Ranges.empty(); }
6161

6262
/// Get the bit width of this ConstantRangeList.
63-
uint32_t getBitWidth() const { return 64; }
63+
uint32_t getBitWidth() const { return Ranges.front().getBitWidth(); }
6464

6565
/// Return the number of ranges in this ConstantRangeList.
6666
size_t size() const { return Ranges.size(); }

llvm/include/llvm/IR/Metadata.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@ class MDNode : public Metadata {
14561456
static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
14571457
static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
14581458
static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
1459+
static MDNode *getMostGenericNoaliasAddrspace(MDNode *A, MDNode *B);
14591460
static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B);
14601461
static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B);
14611462
/// Merge !prof metadata from two instructions.

llvm/lib/IR/ConstantRangeList.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ void ConstantRangeList::insert(const ConstantRange &NewRange) {
3939
return;
4040
assert(!NewRange.isFullSet() && "Do not support full set");
4141
assert(NewRange.getLower().slt(NewRange.getUpper()));
42-
assert(getBitWidth() == NewRange.getBitWidth());
4342
// Handle common cases.
4443
if (empty() || Ranges.back().getUpper().slt(NewRange.getLower())) {
4544
Ranges.push_back(NewRange);
4645
return;
4746
}
47+
48+
assert(getBitWidth() == NewRange.getBitWidth());
49+
4850
if (NewRange.getUpper().slt(Ranges.front().getLower())) {
4951
Ranges.insert(Ranges.begin(), NewRange);
5052
return;
@@ -142,14 +144,15 @@ void ConstantRangeList::subtract(const ConstantRange &SubRange) {
142144

143145
ConstantRangeList
144146
ConstantRangeList::unionWith(const ConstantRangeList &CRL) const {
145-
assert(getBitWidth() == CRL.getBitWidth() &&
146-
"ConstantRangeList bitwidths don't agree!");
147147
// Handle common cases.
148148
if (empty())
149149
return CRL;
150150
if (CRL.empty())
151151
return *this;
152152

153+
assert(getBitWidth() == CRL.getBitWidth() &&
154+
"ConstantRangeList bitwidths don't agree!");
155+
153156
ConstantRangeList Result;
154157
size_t i = 0, j = 0;
155158
// "PreviousRange" tracks the lowest unioned range that is being processed.
@@ -192,15 +195,15 @@ ConstantRangeList::unionWith(const ConstantRangeList &CRL) const {
192195

193196
ConstantRangeList
194197
ConstantRangeList::intersectWith(const ConstantRangeList &CRL) const {
195-
assert(getBitWidth() == CRL.getBitWidth() &&
196-
"ConstantRangeList bitwidths don't agree!");
197-
198198
// Handle common cases.
199199
if (empty())
200200
return *this;
201201
if (CRL.empty())
202202
return CRL;
203203

204+
assert(getBitWidth() == CRL.getBitWidth() &&
205+
"ConstantRangeList bitwidths don't agree!");
206+
204207
ConstantRangeList Result;
205208
size_t i = 0, j = 0;
206209
while (i < size() && j < CRL.size()) {

llvm/lib/IR/Metadata.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/IR/BasicBlock.h"
3030
#include "llvm/IR/Constant.h"
3131
#include "llvm/IR/ConstantRange.h"
32+
#include "llvm/IR/ConstantRangeList.h"
3233
#include "llvm/IR/Constants.h"
3334
#include "llvm/IR/DebugInfoMetadata.h"
3435
#include "llvm/IR/DebugLoc.h"
@@ -1354,6 +1355,43 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
13541355
return MDNode::get(A->getContext(), MDs);
13551356
}
13561357

1358+
MDNode *MDNode::getMostGenericNoaliasAddrspace(MDNode *A, MDNode *B) {
1359+
if (!A || !B)
1360+
return nullptr;
1361+
1362+
if (A == B)
1363+
return A;
1364+
1365+
SmallVector<ConstantRange> RangeListA, RangeListB;
1366+
for (unsigned I = 0, E = A->getNumOperands() / 2; I != E; ++I) {
1367+
auto *LowA = mdconst::extract<ConstantInt>(A->getOperand(2 * I + 0));
1368+
auto *HighA = mdconst::extract<ConstantInt>(A->getOperand(2 * I + 1));
1369+
RangeListA.push_back(ConstantRange(LowA->getValue(), HighA->getValue()));
1370+
}
1371+
1372+
for (unsigned I = 0, E = B->getNumOperands() / 2; I != E; ++I) {
1373+
auto *LowB = mdconst::extract<ConstantInt>(B->getOperand(2 * I + 0));
1374+
auto *HighB = mdconst::extract<ConstantInt>(B->getOperand(2 * I + 1));
1375+
RangeListB.push_back(ConstantRange(LowB->getValue(), HighB->getValue()));
1376+
}
1377+
1378+
ConstantRangeList CRLA(RangeListA);
1379+
ConstantRangeList CRLB(RangeListB);
1380+
ConstantRangeList Result = CRLA.intersectWith(CRLB);
1381+
if (Result.empty())
1382+
return nullptr;
1383+
1384+
SmallVector<Metadata *> MDs;
1385+
for (const ConstantRange &CR : Result) {
1386+
MDs.push_back(ConstantAsMetadata::get(
1387+
ConstantInt::get(A->getContext(), CR.getLower())));
1388+
MDs.push_back(ConstantAsMetadata::get(
1389+
ConstantInt::get(A->getContext(), CR.getUpper())));
1390+
}
1391+
1392+
return MDNode::get(A->getContext(), MDs);
1393+
}
1394+
13571395
MDNode *MDNode::getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B) {
13581396
if (!A || !B)
13591397
return nullptr;

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3393,7 +3393,8 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J,
33933393
break;
33943394
case LLVMContext::MD_noalias_addrspace:
33953395
if (DoesKMove)
3396-
K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD));
3396+
K->setMetadata(Kind,
3397+
MDNode::getMostGenericNoaliasAddrspace(JMD, KMD));
33973398
break;
33983399
}
33993400
}

llvm/test/Transforms/SimplifyCFG/hoist-with-metadata.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ out:
361361
define void @hoist_noalias_addrspace_switch(i64 %i, ptr %p, i64 %val) {
362362
; CHECK-LABEL: @hoist_noalias_addrspace_switch(
363363
; CHECK-NEXT: out:
364-
; CHECK-NEXT: [[T:%.*]] = atomicrmw add ptr [[P:%.*]], i64 [[VAL:%.*]] seq_cst, align 8, !noalias.addrspace [[META8:![0-9]+]]
364+
; CHECK-NEXT: [[T:%.*]] = atomicrmw add ptr [[P:%.*]], i64 [[VAL:%.*]] seq_cst, align 8, !noalias.addrspace [[META7]]
365365
; CHECK-NEXT: ret void
366366
;
367367
switch i64 %i, label %bb0 [
@@ -384,7 +384,7 @@ out:
384384
define void @hoist_noalias_addrspace_switch_multiple(i64 %i, ptr %p, i64 %val) {
385385
; CHECK-LABEL: @hoist_noalias_addrspace_switch_multiple(
386386
; CHECK-NEXT: out:
387-
; CHECK-NEXT: [[T:%.*]] = atomicrmw add ptr [[P:%.*]], i64 [[VAL:%.*]] seq_cst, align 8, !noalias.addrspace [[META9:![0-9]+]]
387+
; CHECK-NEXT: [[T:%.*]] = atomicrmw add ptr [[P:%.*]], i64 [[VAL:%.*]] seq_cst, align 8, !noalias.addrspace [[META8:![0-9]+]]
388388
; CHECK-NEXT: ret void
389389
;
390390
switch i64 %i, label %bb0 [
@@ -424,6 +424,5 @@ out:
424424
; CHECK: [[META5]] = !{i64 4}
425425
; CHECK: [[META6]] = !{float 2.500000e+00}
426426
; CHECK: [[META7]] = !{i32 5, i32 6}
427-
; CHECK: [[META8]] = !{i32 4, i32 8}
428-
; CHECK: [[META9]] = !{i32 2, i32 8, i32 20, i32 42, i32 45, i32 50}
427+
; CHECK: [[META8]] = !{i32 4, i32 5}
429428
;.

0 commit comments

Comments
 (0)