Skip to content

Commit 8691544

Browse files
committed
[SCCP] Use range metadata for loads and calls
When all else fails, use range metadata to constrain the result of loads and calls. It should also be possible to use !nonnull, but that would require some general support for inequalities in SCCP first. Differential Revision: https://reviews.llvm.org/D83179
1 parent 446e3df commit 8691544

2 files changed

Lines changed: 69 additions & 46 deletions

File tree

‎llvm/lib/Transforms/Scalar/SCCP.cpp‎

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,11 +1104,21 @@ void SCCPSolver::visitStoreInst(StoreInst &SI) {
11041104
TrackedGlobals.erase(I); // No need to keep tracking this!
11051105
}
11061106

1107+
static ValueLatticeElement getValueFromMetadata(const Instruction *I) {
1108+
if (MDNode *Ranges = I->getMetadata(LLVMContext::MD_range))
1109+
if (I->getType()->isIntegerTy())
1110+
return ValueLatticeElement::getRange(
1111+
getConstantRangeFromMetadata(*Ranges));
1112+
// TODO: Also handle MD_nonnull.
1113+
return ValueLatticeElement::getOverdefined();
1114+
}
1115+
11071116
// Handle load instructions. If the operand is a constant pointer to a constant
11081117
// global, we can replace the load with the loaded constant value!
11091118
void SCCPSolver::visitLoadInst(LoadInst &I) {
1110-
// If this load is of a struct, just mark the result overdefined.
1111-
if (I.getType()->isStructTy())
1119+
// If this load is of a struct or the load is volatile, just mark the result
1120+
// as overdefined.
1121+
if (I.getType()->isStructTy() || I.isVolatile())
11121122
return (void)markOverdefined(&I);
11131123

11141124
// ResolvedUndefsIn might mark I as overdefined. Bail out, even if we would
@@ -1122,41 +1132,39 @@ void SCCPSolver::visitLoadInst(LoadInst &I) {
11221132

11231133
ValueLatticeElement &IV = ValueState[&I];
11241134

1125-
if (!isConstant(PtrVal) || I.isVolatile())
1126-
return (void)markOverdefined(IV, &I);
1127-
1128-
Constant *Ptr = getConstant(PtrVal);
1129-
1130-
// load null is undefined.
1131-
if (isa<ConstantPointerNull>(Ptr)) {
1132-
if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1133-
return (void)markOverdefined(IV, &I);
1134-
else
1135-
return;
1136-
}
1135+
if (isConstant(PtrVal)) {
1136+
Constant *Ptr = getConstant(PtrVal);
11371137

1138-
// Transform load (constant global) into the value loaded.
1139-
if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1140-
if (!TrackedGlobals.empty()) {
1141-
// If we are tracking this global, merge in the known value for it.
1142-
auto It = TrackedGlobals.find(GV);
1143-
if (It != TrackedGlobals.end()) {
1144-
mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
1138+
// load null is undefined.
1139+
if (isa<ConstantPointerNull>(Ptr)) {
1140+
if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1141+
return (void)markOverdefined(IV, &I);
1142+
else
11451143
return;
1144+
}
1145+
1146+
// Transform load (constant global) into the value loaded.
1147+
if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1148+
if (!TrackedGlobals.empty()) {
1149+
// If we are tracking this global, merge in the known value for it.
1150+
auto It = TrackedGlobals.find(GV);
1151+
if (It != TrackedGlobals.end()) {
1152+
mergeInValue(IV, &I, It->second, getMaxWidenStepsOpts());
1153+
return;
1154+
}
11461155
}
11471156
}
1148-
}
11491157

1150-
// Transform load from a constant into a constant if possible.
1151-
if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) {
1152-
if (isa<UndefValue>(C))
1153-
return;
1154-
return (void)markConstant(IV, &I, C);
1158+
// Transform load from a constant into a constant if possible.
1159+
if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) {
1160+
if (isa<UndefValue>(C))
1161+
return;
1162+
return (void)markConstant(IV, &I, C);
1163+
}
11551164
}
11561165

1157-
// Otherwise we cannot say for certain what value this load will produce.
1158-
// Bail out.
1159-
markOverdefined(IV, &I);
1166+
// Fall back to metadata.
1167+
mergeInValue(&I, getValueFromMetadata(&I));
11601168
}
11611169

11621170
void SCCPSolver::visitCallBase(CallBase &CB) {
@@ -1171,10 +1179,13 @@ void SCCPSolver::handleCallOverdefined(CallBase &CB) {
11711179
if (CB.getType()->isVoidTy())
11721180
return;
11731181

1182+
// Always mark struct return as overdefined.
1183+
if (CB.getType()->isStructTy())
1184+
return (void)markOverdefined(&CB);
1185+
11741186
// Otherwise, if we have a single return value case, and if the function is
11751187
// a declaration, maybe we can constant fold it.
1176-
if (F && F->isDeclaration() && !CB.getType()->isStructTy() &&
1177-
canConstantFoldCallTo(&CB, F)) {
1188+
if (F && F->isDeclaration() && canConstantFoldCallTo(&CB, F)) {
11781189
SmallVector<Constant *, 8> Operands;
11791190
for (auto AI = CB.arg_begin(), E = CB.arg_end(); AI != E; ++AI) {
11801191
if (AI->get()->getType()->isStructTy())
@@ -1202,8 +1213,8 @@ void SCCPSolver::handleCallOverdefined(CallBase &CB) {
12021213
}
12031214
}
12041215

1205-
// Otherwise, we don't know anything about this call, mark it overdefined.
1206-
return (void)markOverdefined(&CB);
1216+
// Fall back to metadata.
1217+
mergeInValue(&CB, getValueFromMetadata(&CB));
12071218
}
12081219

12091220
void SCCPSolver::handleCallArguments(CallBase &CB) {

‎llvm/test/Transforms/SCCP/metadata.ll‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ declare i32 @get_i32()
77
define void @load_range(i32* %p) {
88
; CHECK-LABEL: @load_range(
99
; CHECK-NEXT: [[V:%.*]] = load i32, i32* [[P:%.*]], align 4, !range !0
10-
; CHECK-NEXT: [[C1:%.*]] = icmp ult i32 [[V]], 10
11-
; CHECK-NEXT: call void @use(i1 [[C1]])
10+
; CHECK-NEXT: call void @use(i1 true)
1211
; CHECK-NEXT: [[C2:%.*]] = icmp ult i32 [[V]], 9
1312
; CHECK-NEXT: call void @use(i1 [[C2]])
14-
; CHECK-NEXT: [[C3:%.*]] = icmp ugt i32 [[V]], 9
15-
; CHECK-NEXT: call void @use(i1 [[C3]])
13+
; CHECK-NEXT: call void @use(i1 false)
1614
; CHECK-NEXT: [[C4:%.*]] = icmp ugt i32 [[V]], 8
1715
; CHECK-NEXT: call void @use(i1 [[C4]])
1816
; CHECK-NEXT: ret void
@@ -29,9 +27,26 @@ define void @load_range(i32* %p) {
2927
ret void
3028
}
3129

30+
define i32 @load_range_single(i32* %p) {
31+
; CHECK-LABEL: @load_range_single(
32+
; CHECK-NEXT: ret i32 0
33+
;
34+
%v = load i32, i32* %p, !range !{i32 0, i32 1}
35+
ret i32 %v
36+
}
37+
38+
define i32 @load_range_single_volatile(i32* %p) {
39+
; CHECK-LABEL: @load_range_single_volatile(
40+
; CHECK-NEXT: [[V:%.*]] = load volatile i32, i32* [[P:%.*]], align 4, !range !1
41+
; CHECK-NEXT: ret i32 [[V]]
42+
;
43+
%v = load volatile i32, i32* %p, !range !{i32 0, i32 1}
44+
ret i32 %v
45+
}
46+
3247
define void @load_nonnull(i32** %p) {
3348
; CHECK-LABEL: @load_nonnull(
34-
; CHECK-NEXT: [[V:%.*]] = load i32*, i32** [[P:%.*]], align 8, !nonnull !1
49+
; CHECK-NEXT: [[V:%.*]] = load i32*, i32** [[P:%.*]], align 8, !nonnull !2
3550
; CHECK-NEXT: [[C1:%.*]] = icmp ne i32* [[V]], null
3651
; CHECK-NEXT: call void @use(i1 [[C1]])
3752
; CHECK-NEXT: ret void
@@ -45,12 +60,10 @@ define void @load_nonnull(i32** %p) {
4560
define void @call_range(i32* %p) {
4661
; CHECK-LABEL: @call_range(
4762
; CHECK-NEXT: [[V:%.*]] = call i32 @get_i32(), !range !0
48-
; CHECK-NEXT: [[C1:%.*]] = icmp ult i32 [[V]], 10
49-
; CHECK-NEXT: call void @use(i1 [[C1]])
63+
; CHECK-NEXT: call void @use(i1 true)
5064
; CHECK-NEXT: [[C2:%.*]] = icmp ult i32 [[V]], 9
5165
; CHECK-NEXT: call void @use(i1 [[C2]])
52-
; CHECK-NEXT: [[C3:%.*]] = icmp ugt i32 [[V]], 9
53-
; CHECK-NEXT: call void @use(i1 [[C3]])
66+
; CHECK-NEXT: call void @use(i1 false)
5467
; CHECK-NEXT: [[C4:%.*]] = icmp ugt i32 [[V]], 8
5568
; CHECK-NEXT: call void @use(i1 [[C4]])
5669
; CHECK-NEXT: ret void
@@ -69,8 +82,7 @@ define void @call_range(i32* %p) {
6982

7083
define internal i1 @ip_cmp_range(i32 %v) {
7184
; CHECK-LABEL: @ip_cmp_range(
72-
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[V:%.*]], 10
73-
; CHECK-NEXT: ret i1 [[C]]
85+
; CHECK-NEXT: ret i1 undef
7486
;
7587
%c = icmp ult i32 %v, 10
7688
ret i1 %c
@@ -80,7 +92,7 @@ define i1 @ip_load_range(i32* %p) {
8092
; CHECK-LABEL: @ip_load_range(
8193
; CHECK-NEXT: [[V:%.*]] = load i32, i32* [[P:%.*]], align 4, !range !0
8294
; CHECK-NEXT: [[C:%.*]] = call i1 @ip_cmp_range(i32 [[V]])
83-
; CHECK-NEXT: ret i1 [[C]]
95+
; CHECK-NEXT: ret i1 true
8496
;
8597
%v = load i32, i32* %p, !range !{i32 0, i32 10}
8698
%c = call i1 @ip_cmp_range(i32 %v)

0 commit comments

Comments
 (0)