Index: llvm/include/llvm/Analysis/ConstantFolding.h =================================================================== --- llvm/include/llvm/Analysis/ConstantFolding.h +++ llvm/include/llvm/Analysis/ConstantFolding.h @@ -170,6 +170,12 @@ /// represented, return null. Constant *ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty); +/// If C is a constant patterned array and all valid loaded results for given +/// alignment are same to a constant, return that constant. +Constant *ConstantFoldLoadFromPatternedAggregate(Constant *C, Type *LoadTy, + uint64_t LoadAlign, + const DataLayout &DL); + /// canConstantFoldCallTo - Return true if its even possible to fold a call to /// the specified function. bool canConstantFoldCallTo(const CallBase *Call, const Function *F); Index: llvm/lib/Analysis/ConstantFolding.cpp =================================================================== --- llvm/lib/Analysis/ConstantFolding.cpp +++ llvm/lib/Analysis/ConstantFolding.cpp @@ -767,6 +767,70 @@ return nullptr; } +Constant *llvm::ConstantFoldLoadFromPatternedAggregate(Constant *C, + Type *LoadTy, + uint64_t LoadAlign, + const DataLayout &DL) { + + auto *CTy = C->getType(); + auto CSTy = dyn_cast(CTy); + auto CATy = dyn_cast(CTy); + // TODO: Current implementation can't handle unpacked struct and pointer + // elements + if ((CSTy && CSTy->isPacked()) || CATy) { + // Bail if some ptr type element exists. + if (CSTy) { + for (unsigned I = 0, NumElm = CSTy->getNumElements(); I < NumElm; I++) + if (isa(CSTy->getElementType(I))) + return nullptr; + } + if (CATy) { + if (isa(CATy->getElementType())) + return nullptr; + } + + uint64_t GVSize = DL.getTypeStoreSize(CTy); + + // Bail for large initializers in excess of 64K to avoid allocating + // too much memory. + if (UINT16_MAX < GVSize) + return nullptr; + + if (GVSize) { + uint64_t LoadSize = LoadTy->getScalarSizeInBits() / 8; + + // Read Global Variable bytes and check the results' equivalence from + // possible offsets for given load alignment + SmallVector RawBytes(static_cast(GVSize)); + unsigned char *GVBytes = RawBytes.data(); + ReadDataFromGlobal(C, 0, GVBytes, GVSize, DL); + for (uint64_t ByteOffset = LoadAlign; ByteOffset <= GVSize - LoadSize; + ByteOffset += LoadAlign) + for (uint64_t I = 0; I < LoadSize; I++) + if (GVBytes[I] != GVBytes[I + ByteOffset]) + return nullptr; + + // swap bytes if on big endian + if (!DL.isLittleEndian()) + for (unsigned I = 0; I < LoadSize / 2; I += 1) { + unsigned char T = GVBytes[I]; + GVBytes[I] = GVBytes[LoadSize - 1 - I]; + GVBytes[LoadSize - 1 - I] = T; + } + + // convert bytes to a Load type Constant + StringRef s = + StringRef(reinterpret_cast(GVBytes), LoadSize); + Constant *CDA = ConstantDataArray::getRaw(s, 1, LoadTy); + Constant *Res = CDA->getAggregateElement(0U); + + return Res; + } + } + + return nullptr; +} + namespace { /// One of Op0/Op1 is a constant expression. Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -6634,16 +6634,21 @@ return ConstantFoldLoadFromConstPtr(PtrOpC, LI->getType(), Q.DL); // We can only fold the load if it is from a constant global with definitive - // initializer. Skip expensive logic if this is not the case. + // or unique initializer. Skip expensive logic if this is not the case. auto *GV = dyn_cast(getUnderlyingObject(PtrOp)); - if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) + if (!GV || !GV->isConstant() || + (!GV->hasDefinitiveInitializer() && !GV->hasUniqueInitializer())) return nullptr; - // If GlobalVariable's initializer is uniform, then return the constant - // regardless of its offset. + // If GlobalVariable's initializer is uniform or any valid load results of + // arrays/structs are equal to some constant , then return the constant regardless of its + // offset. if (Constant *C = ConstantFoldLoadFromUniformValue(GV->getInitializer(), LI->getType())) return C; + if (Constant *C = ConstantFoldLoadFromPatternedAggregate( + GV->getInitializer(), LI->getType(), LI->getAlign().value(), Q.DL)) + return C; // Try to convert operand into a constant by stripping offsets while looking // through invariant.group intrinsics. Index: llvm/test/Transforms/InstSimplify/load-patterned-aggregates.ll =================================================================== --- llvm/test/Transforms/InstSimplify/load-patterned-aggregates.ll +++ llvm/test/Transforms/InstSimplify/load-patterned-aggregates.ll @@ -4,7 +4,7 @@ @constzeroarray = internal constant [4 x i32] zeroinitializer @constarray = internal constant [8 x i8] c"\01\00\01\00\01\00\01\00", align 4 -@conststruct = internal constant <{[8 x i8]}> <{[8 x i8] c"\01\00\01\00\01\00\01\00"}>, align 4 +@constpackedstruct = internal constant <{[8 x i8]}> <{[8 x i8] c"\01\00\01\00\01\00\01\00"}>, align 4 define i32 @load_gep_const_zero_array(i64 %idx) { ; CHECK-LABEL: @load_gep_const_zero_array( @@ -25,37 +25,9 @@ ret i8 %load } - -define i32 @load_gep_const_patterned_array(i64 %idx) { -; CHECK-LABEL: @load_gep_const_patterned_array( -; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [4 x i32], ptr @constarray, i64 0, i64 [[IDX:%.*]] -; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4 -; CHECK-NEXT: ret i32 [[LOAD]] -; - %gep = getelementptr inbounds [4 x i32], ptr @constarray, i64 0, i64 %idx - %load = load i32, ptr %gep - ret i32 %load -} - -define i8 @load_i8_multi_gep_const_array(i64 %idx1, i64 %idx2) { -; CHECK-LABEL: @load_i8_multi_gep_const_array( -; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i8, ptr @constarray, i64 [[IDX1:%.*]] -; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[GEP1]], i64 [[IDX2:%.*]] -; CHECK-NEXT: [[LOAD:%.*]] = load i8, ptr [[GEP]], align 1 -; CHECK-NEXT: ret i8 [[LOAD]] -; - %gep1 = getelementptr inbounds i8, ptr @constarray, i64 %idx1 - %gep = getelementptr inbounds i8, ptr %gep1, i64 %idx2 - %load = load i8, ptr %gep - ret i8 %load -} - -; TODO: this should be ret i8 1 define i8 @gep_load_i8_align2(i64 %idx){ ; CHECK-LABEL: @gep_load_i8_align2( -; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr @constarray, i64 [[IDX:%.*]] -; CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr [[TMP1]], align 2 -; CHECK-NEXT: ret i8 [[TMP2]] +; CHECK-NEXT: ret i8 1 ; %1 = getelementptr inbounds i8, ptr @constarray, i64 %idx %2 = load i8, ptr %1, align 2 @@ -74,26 +46,20 @@ ret i8 %2 } -; TODO: this should be ret i8 65537 on the case for little endian define i32 @gep_i32_load_i32_align4(i64 %idx){ ; CHECK-LABEL: @gep_i32_load_i32_align4( -; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, ptr @constarray, i64 [[IDX:%.*]] -; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 4 -; CHECK-NEXT: ret i32 [[TMP2]] +; CHECK-NEXT: ret i32 65537 ; %1 = getelementptr inbounds i32, ptr @constarray, i64 %idx %2 = load i32, ptr %1, align 4 ret i32 %2 } -; TODO: this should be ret i8 65537 on the case for little endian -define i32 @gep_i32_load_i32_align4_struct(i64 %idx){ -; CHECK-LABEL: @gep_i32_load_i32_align4_struct( -; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, ptr @conststruct, i64 [[IDX:%.*]] -; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 4 -; CHECK-NEXT: ret i32 [[TMP2]] +define i32 @gep_i32_load_i32_align4_packedstruct(i64 %idx){ +; CHECK-LABEL: @gep_i32_load_i32_align4_packedstruct( +; CHECK-NEXT: ret i32 65537 ; - %1 = getelementptr inbounds i32, ptr @conststruct, i64 %idx + %1 = getelementptr inbounds i32, ptr @constpackedstruct, i64 %idx %2 = load i32, ptr %1, align 4 ret i32 %2 } @@ -111,16 +77,17 @@ } ; can't be folded -define i32 @gep_i8_load_i32_align1_struct(i64 %idx){ -; CHECK-LABEL: @gep_i8_load_i32_align1_struct( -; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr @conststruct, i64 [[IDX:%.*]] +define i32 @gep_i8_load_i32_align1_packedstruct(i64 %idx){ +; CHECK-LABEL: @gep_i8_load_i32_align1_packedstruct( +; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr @constpackedstruct, i64 [[IDX:%.*]] ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 1 ; CHECK-NEXT: ret i32 [[TMP2]] ; - %1 = getelementptr inbounds i8, ptr @conststruct, i64 %idx + %1 = getelementptr inbounds i8, ptr @constpackedstruct, i64 %idx %2 = load i32, ptr %1, align 1 ret i32 %2 } + ; TODO: This could be folded but need to see GEP source types define i32 @gep_i16_load_i32_align1(i64 %idx){ ; CHECK-LABEL: @gep_i16_load_i32_align1(