Index: llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp =================================================================== --- llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp +++ llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp @@ -825,6 +825,65 @@ return true; } +// calculate GEP Stride and accumulated const ModOffset. +static bool getStrideAndModOffsetOfGEP(Value *PtrOp, APInt &ModOffset, + std::optional &Stride, + const DataLayout &DL) { + + uint64_t BW = ModOffset.getBitWidth(); + if (isa(PtrOp)) { + ModOffset = APInt(BW, 0); + Stride = APInt(BW, 1); + return true; + } + + // If PtrOp is neither GlobalVariable nor GEP, we cannot use GEP stride, + // because it might not arrive back at GlobalVariable. + // Even if so, we can check by alignment. + auto *GEP = dyn_cast(PtrOp); + if (!GEP) { + Stride = std::nullopt; + return true; + } + + Value *PtrOpV = GEP; + // Return a minimum gep stride, greatest common divisor of consective gep + // indices type sizes (c.f. Bézout's identity). + while ((GEP = dyn_cast(PtrOpV))) { + MapVector VarOffsets; + if (!GEP->collectOffset(DL, BW, VarOffsets, ModOffset)) + return false; + + for (auto [V, IndexTypeSize] : VarOffsets) { + APInt Scale = APInt(BW, IndexTypeSize.getZExtValue()); + if (!GEP->isInBounds()) + Scale = APInt::getOneBitSet(Scale.getBitWidth(), Scale.countr_zero()); + + if (!Stride) + Stride = Scale; + else + Stride = APIntOps::GreatestCommonDivisor(*Stride, Scale); + } + + PtrOpV = GEP->getPointerOperand(); + } + + // Check whether pointer arrives back at Global Variable. + // Even if it's not, we can check by alignment. + if (!isa(PtrOpV)) { + Stride = std::nullopt; + return true; + } + + // In consideration of signed GEP indices, non-negligible offset become + // remainder of divission by minimum GEP stride. + ModOffset = ModOffset.srem(*Stride); + if (ModOffset.isNegative()) + ModOffset += *Stride; + + return true; +} + /// If C is a constant patterned array and all valid loaded results for given /// alignment are same to a constant, return that constant. static bool foldPatternedLoads(Instruction &I, const DataLayout &DL) { @@ -847,28 +906,31 @@ if (!GVSize || 4096 < GVSize) return false; - // Check whether pointer arrives back at Global Variable. - // If PtrOp is neither GlobalVariable nor GEP, it might not arrive back at - // GlobalVariable. - // TODO: implement GEP handling unsigned BW = DL.getIndexTypeSizeInBits(PtrOp->getType()); - // TODO: Determine stride based on GEPs. - APInt Stride(BW, 1); + std::optional Stride; APInt ConstOffset(BW, 0); + if (!getStrideAndModOffsetOfGEP(PtrOp, ConstOffset, Stride, DL)) + return false; // Any possible offset could be multiple of GEP stride. And any valid // offset is multiple of load alignment, so checking only multiples of bigger // one is sufficient to say results' equality. if (auto LA = LI->getAlign(); - LA <= GV->getAlign().valueOrOne() && Stride.getZExtValue() < LA.value()) + LA <= GV->getAlign().valueOrOne() && + (!Stride || Stride->getZExtValue() < LA.value())) { + ConstOffset = APInt(BW, 0); Stride = APInt(BW, LA.value()); + } + + if (!Stride) + return false; Constant *Ca = ConstantFoldLoadFromConst(C, LoadTy, ConstOffset, DL); if (!Ca) return false; unsigned E = GVSize - DL.getTypeStoreSize(LoadTy); - for (; ConstOffset.getZExtValue() <= E; ConstOffset += Stride) + for (; ConstOffset.getZExtValue() <= E; ConstOffset += *Stride) if (Ca != ConstantFoldLoadFromConst(C, LoadTy, ConstOffset, DL)) return false; Index: llvm/test/Transforms/AggressiveInstCombine/patterned-load.ll =================================================================== --- llvm/test/Transforms/AggressiveInstCombine/patterned-load.ll +++ llvm/test/Transforms/AggressiveInstCombine/patterned-load.ll @@ -50,45 +50,47 @@ ; can't be folded because ptrmask can change ptr, while preserving provenance define i8 @inbounds_gep_load_i8_align2_ptrmasked(i64 %idx, i64 %mask){ ; CHECK-LABEL: @inbounds_gep_load_i8_align2_ptrmasked( -; CHECK-NEXT: ret i8 1 +; CHECK-NEXT: [[TMP1:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr @constarray1, i64 [[MASK:%.*]]) +; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i16, ptr [[TMP1]], i64 [[IDX:%.*]] +; CHECK-NEXT: [[TMP3:%.*]] = load i8, ptr [[TMP2]], align 1 +; CHECK-NEXT: ret i8 [[TMP3]] ; %1 = call ptr @llvm.ptrmask.p0.i64(ptr @constarray1, i64 %mask) - %2 = getelementptr inbounds i8, ptr %1, i64 %idx - %3 = load i8, ptr %2, align 2 + %2 = getelementptr inbounds i16, ptr %1, i64 %idx + %3 = load i8, ptr %2, align 1 ret i8 %3 } -; TODO: this will be ret i32 65537(LE), 16777472(BE) define i32 @inbounds_gep_i16_load_i32_align1(i64 %idx){ -; CHECK-LABEL: @inbounds_gep_i16_load_i32_align1( -; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i16, ptr @constarray1, i64 [[IDX:%.*]] -; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 1 -; CHECK-NEXT: ret i32 [[TMP2]] +; LE-LABEL: @inbounds_gep_i16_load_i32_align1( +; LE-NEXT: ret i32 65537 +; +; BE-LABEL: @inbounds_gep_i16_load_i32_align1( +; BE-NEXT: ret i32 16777472 ; %1 = getelementptr inbounds i16, ptr @constarray1, i64 %idx %2 = load i32, ptr %1, align 1 ret i32 %2 } -; TODO: this will be ret i32 65537(LE), 16777472(BE) define i32 @inbounds_gep_i32_load_i32_align8(i64 %idx){ -; CHECK-LABEL: @inbounds_gep_i32_load_i32_align8( -; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, ptr @constarray1, i64 [[IDX:%.*]] -; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP1]], align 8 -; CHECK-NEXT: ret i32 [[TMP2]] +; LE-LABEL: @inbounds_gep_i32_load_i32_align8( +; LE-NEXT: ret i32 65537 +; +; BE-LABEL: @inbounds_gep_i32_load_i32_align8( +; BE-NEXT: ret i32 16777472 ; %1 = getelementptr inbounds i32, ptr @constarray1, i64 %idx %2 = load i32, ptr %1, align 8 ret i32 %2 } -; TODO: this will be ret i32 65547(LE), 16777472(BE) define i32 @inbounds_gep_i32_load_i32_const_offset(i64 %idx){ -; CHECK-LABEL: @inbounds_gep_i32_load_i32_const_offset( -; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i16, ptr @constarray2, i64 1 -; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i32, ptr [[TMP1]], i64 [[IDX:%.*]] -; CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4 -; CHECK-NEXT: ret i32 [[TMP3]] +; LE-LABEL: @inbounds_gep_i32_load_i32_const_offset( +; LE-NEXT: ret i32 65537 +; +; BE-LABEL: @inbounds_gep_i32_load_i32_const_offset( +; BE-NEXT: ret i32 16777472 ; %1 = getelementptr inbounds i16, ptr @constarray2, i64 1 %2 = getelementptr inbounds i32, ptr %1, i64 %idx @@ -125,13 +127,9 @@ ret i32 %3 } -; TODO: this will be ret i32 42 define i32 @inbounds_gep_i32_load_i32_const_ptr_array(i64 %idx){ ; CHECK-LABEL: @inbounds_gep_i32_load_i32_const_ptr_array( -; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds ptr, ptr @constptrarray, i64 [[IDX:%.*]] -; CHECK-NEXT: [[TMP2:%.*]] = load ptr, ptr [[TMP1]], align 4 -; CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4 -; CHECK-NEXT: ret i32 [[TMP3]] +; CHECK-NEXT: ret i32 42 ; %1 = getelementptr inbounds ptr, ptr @constptrarray, i64 %idx %2 = load ptr, ptr %1, align 4 @@ -163,16 +161,12 @@ ret i32 %2 } -; TODO: this coould be folded into 65537(LE), 16777472(BE) define i32 @inbounds_gep_i32_load_i32_align4_struct_with_const_offset(i64 %idx){ ; LE-LABEL: @inbounds_gep_i32_load_i32_align4_struct_with_const_offset( ; LE-NEXT: ret i32 65537 ; ; BE-LABEL: @inbounds_gep_i32_load_i32_align4_struct_with_const_offset( -; BE-NEXT: [[TMP1:%.*]] = getelementptr inbounds i16, ptr @conststruct, i64 1 -; BE-NEXT: [[TMP2:%.*]] = getelementptr inbounds i32, ptr [[TMP1]], i64 [[IDX:%.*]] -; BE-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4 -; BE-NEXT: ret i32 [[TMP3]] +; BE-NEXT: ret i32 16777472 ; %1 = getelementptr inbounds i16, ptr @conststruct, i64 1 %2 = getelementptr inbounds i32, ptr %1, i64 %idx