Skip to content

Commit a4d6c7d

Browse files
khei4nikic
authored andcommitted
[InstSimplify] Fold LoadInst for uniform constant global variables
Fold LoadInst for uniformly initialized constants, even if there are non-constant GEP indices. Goal proof: https://alive2.llvm.org/ce/z/oZtVby Motivated by rust-lang/rust#107208 Differential Revision: https://reviews.llvm.org/D144184
1 parent 78cd158 commit a4d6c7d

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -6586,6 +6586,12 @@ static Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp,
65866586
if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
65876587
return nullptr;
65886588

6589+
// If GlobalVariable's initializer is uniform, then return the constant
6590+
// regardless of its offset.
6591+
if (Constant *C =
6592+
ConstantFoldLoadFromUniformValue(GV->getInitializer(), LI->getType()))
6593+
return C;
6594+
65896595
// Try to convert operand into a constant by stripping offsets while looking
65906596
// through invariant.group intrinsics.
65916597
APInt Offset(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);

llvm/test/Transforms/InstSimplify/load.ll

+2-7
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ define <3 x float> @load_vec3() {
4848

4949
define i32 @load_gep_const_zero_array(i64 %idx) {
5050
; CHECK-LABEL: @load_gep_const_zero_array(
51-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [4 x i32], ptr @constzeroarray, i64 0, i64 [[IDX:%.*]]
52-
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
53-
; CHECK-NEXT: ret i32 [[LOAD]]
51+
; CHECK-NEXT: ret i32 0
5452
;
5553
%gep = getelementptr inbounds [4 x i32], ptr @constzeroarray, i64 0, i64 %idx
5654
%load = load i32, ptr %gep
@@ -59,10 +57,7 @@ define i32 @load_gep_const_zero_array(i64 %idx) {
5957

6058
define i8 @load_i8_multi_gep_const_zero_array(i64 %idx1, i64 %idx2) {
6159
; CHECK-LABEL: @load_i8_multi_gep_const_zero_array(
62-
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i8, ptr @constzeroarray, i64 [[IDX1:%.*]]
63-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[GEP1]], i64 [[IDX2:%.*]]
64-
; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1
65-
; CHECK-NEXT: ret i8 [[LOAD]]
60+
; CHECK-NEXT: ret i8 0
6661
;
6762
%gep1 = getelementptr inbounds i8, ptr @constzeroarray, i64 %idx1
6863
%gep = getelementptr inbounds i8, ptr %gep1, i64 %idx2

0 commit comments

Comments
 (0)