Index: llvm/lib/Transforms/IPO/Attributor.cpp =================================================================== --- llvm/lib/Transforms/IPO/Attributor.cpp +++ llvm/lib/Transforms/IPO/Attributor.cpp @@ -237,24 +237,12 @@ if (!GV->hasInitializer()) return UndefValue::get(&Ty); - // Handle constant initializers by extracting the relevant parts for - // aggregates. - Constant *C = GV->getInitializer(); - if (OASPtr && !OASPtr->offsetOrSizeAreUnknown() && - isa(C)) { - Type *CTy = C->getType(); + if (OASPtr && !OASPtr->offsetOrSizeAreUnknown()) { APInt Offset = APInt(64, OASPtr->getOffset()); - Optional Idx = DL.getGEPIndexForOffset(CTy, Offset); - // Check if the indexing worked out properly. - // TODO: Handle partial accesses, e.g., Offset is > 0 or Size < CTy.size(). - if (Idx && Offset.isZero() && - DL.getTypeSizeInBits(CTy) == uint64_t(OASPtr->getSize() * 8)) { - if (auto *Folded = - ConstantFoldExtractValueInstruction(C, Idx->getZExtValue())) - C = Folded; - } + return ConstantFoldLoadFromConst(GV->getInitializer(), &Ty, Offset, DL); } - return dyn_cast_or_null(getWithType(*C, Ty)); + + return nullptr; } bool AA::isValidInScope(const Value &V, const Function *Scope) { Index: llvm/test/Transforms/Attributor/value-simplify-pointer-info-struct.ll =================================================================== --- llvm/test/Transforms/Attributor/value-simplify-pointer-info-struct.ll +++ llvm/test/Transforms/Attributor/value-simplify-pointer-info-struct.ll @@ -99,29 +99,20 @@ ; CHECK-LABEL: define {{[^@]+}}@testOneFieldGlobalS_type_mismatch ; CHECK-SAME: () #[[ATTR2:[0-9]+]] { ; CHECK-NEXT: entry: -; CHECK-NEXT: [[I:%.*]] = load double, ptr @GlobalS, align 8 -; CHECK-NEXT: [[IC:%.*]] = fptosi double [[I]] to i32 -; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[IC]], 42 -; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]] +; CHECK-NEXT: br label [[IF_THEN:%.*]] ; CHECK: if.then: ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 0, 1 -; CHECK-NEXT: br label [[IF_END]] +; CHECK-NEXT: br label [[IF_END:%.*]] ; CHECK: if.end: -; CHECK-NEXT: [[R_0:%.*]] = phi i32 [ [[ADD]], [[IF_THEN]] ], [ 0, [[ENTRY:%.*]] ] -; CHECK-NEXT: [[I1:%.*]] = load i64, ptr getelementptr inbounds ([[STRUCT_S:%.*]], ptr @GlobalS, i32 0, i32 1), align 8 -; CHECK-NEXT: [[I1C:%.*]] = sitofp i64 [[I1]] to double -; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[I1C]], 3.140000e+00 -; CHECK-NEXT: br i1 [[CMP1]], label [[IF_THEN2:%.*]], label [[IF_END4:%.*]] +; CHECK-NEXT: br label [[IF_END4:%.*]] ; CHECK: if.then2: -; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 [[R_0]], 2 -; CHECK-NEXT: br label [[IF_END4]] +; CHECK-NEXT: unreachable ; CHECK: if.end4: -; CHECK-NEXT: [[R_1:%.*]] = phi i32 [ [[ADD3]], [[IF_THEN2]] ], [ [[R_0]], [[IF_END]] ] ; CHECK-NEXT: br label [[IF_END7:%.*]] ; CHECK: if.then5: ; CHECK-NEXT: unreachable ; CHECK: if.end7: -; CHECK-NEXT: ret i32 [[R_1]] +; CHECK-NEXT: ret i32 1 ; entry: %i = load double, ptr @GlobalS, align 8 @@ -165,31 +156,25 @@ ; CHECK-LABEL: define {{[^@]+}}@testOneFieldGlobalS_byte_offset_wrong ; CHECK-SAME: () #[[ATTR2]] { ; CHECK-NEXT: entry: -; CHECK-NEXT: [[I:%.*]] = load i32, ptr getelementptr inbounds (i32, ptr @GlobalS, i32 1), align 8 -; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[I]], 42 -; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]] +; CHECK-NEXT: br label [[IF_THEN:%.*]] ; CHECK: if.then: ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 0, 1 -; CHECK-NEXT: br label [[IF_END]] +; CHECK-NEXT: br label [[IF_END:%.*]] ; CHECK: if.end: -; CHECK-NEXT: [[R_0:%.*]] = phi i32 [ [[ADD]], [[IF_THEN]] ], [ 0, [[ENTRY:%.*]] ] ; CHECK-NEXT: [[I1:%.*]] = load double, ptr getelementptr (double, ptr @GlobalS, i32 3), align 8 ; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[I1]], 3.140000e+00 ; CHECK-NEXT: br i1 [[CMP1]], label [[IF_THEN2:%.*]], label [[IF_END4:%.*]] ; CHECK: if.then2: -; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 [[R_0]], 2 +; CHECK-NEXT: [[ADD3:%.*]] = add nsw i32 1, 2 ; CHECK-NEXT: br label [[IF_END4]] ; CHECK: if.end4: -; CHECK-NEXT: [[R_1:%.*]] = phi i32 [ [[ADD3]], [[IF_THEN2]] ], [ [[R_0]], [[IF_END]] ] -; CHECK-NEXT: [[I2:%.*]] = load ptr, ptr getelementptr (ptr, ptr @GlobalS, i32 11), align 8 -; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne ptr [[I2]], null -; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_THEN5:%.*]], label [[IF_END7:%.*]] +; CHECK-NEXT: [[R_1:%.*]] = phi i32 [ [[ADD3]], [[IF_THEN2]] ], [ 1, [[IF_END]] ] +; CHECK-NEXT: br label [[IF_THEN5:%.*]] ; CHECK: if.then5: ; CHECK-NEXT: [[ADD6:%.*]] = add nsw i32 [[R_1]], 4 -; CHECK-NEXT: br label [[IF_END7]] +; CHECK-NEXT: br label [[IF_END7:%.*]] ; CHECK: if.end7: -; CHECK-NEXT: [[R_2:%.*]] = phi i32 [ [[ADD6]], [[IF_THEN5]] ], [ [[R_1]], [[IF_END4]] ] -; CHECK-NEXT: ret i32 [[R_2]] +; CHECK-NEXT: ret i32 [[ADD6]] ; entry: %i = load i32, ptr getelementptr (i32, ptr @GlobalS, i32 1), align 8 Index: llvm/test/Transforms/Attributor/value-simplify-pointer-info.ll =================================================================== --- llvm/test/Transforms/Attributor/value-simplify-pointer-info.ll +++ llvm/test/Transforms/Attributor/value-simplify-pointer-info.ll @@ -832,9 +832,9 @@ ; } ; define void @static_global_simplifiable_2() { -; TUNIT: Function Attrs: nofree norecurse nosync nounwind willreturn writeonly +; TUNIT: Function Attrs: nofree norecurse nosync nounwind willreturn ; TUNIT-LABEL: define {{[^@]+}}@static_global_simplifiable_2 -; TUNIT-SAME: () #[[ATTR5]] { +; TUNIT-SAME: () #[[ATTR3]] { ; TUNIT-NEXT: entry: ; TUNIT-NEXT: br label [[FOR_COND:%.*]] ; TUNIT: for.cond: @@ -846,6 +846,7 @@ ; TUNIT: for.body: ; TUNIT-NEXT: [[I:%.*]] = mul nuw nsw i64 [[INDVARS_IV]], 10 ; TUNIT-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [1024 x i8], [1024 x i8]* @GBytes, i64 0, i64 [[I]] +; TUNIT-NEXT: store i8 0, i8* [[ARRAYIDX]], align 2, !tbaa [[TBAA19]] ; TUNIT-NEXT: br label [[FOR_INC]] ; TUNIT: for.inc: ; TUNIT-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1 @@ -862,10 +863,11 @@ ; TUNIT-NEXT: [[I15:%.*]] = mul nuw nsw i64 [[INDVARS_IV2]], 10 ; TUNIT-NEXT: [[I16:%.*]] = or i64 [[I15]], 1 ; TUNIT-NEXT: [[ARRAYIDX8:%.*]] = getelementptr inbounds float, float* bitcast ([1024 x i8]* @GBytes to float*), i64 [[I16]] +; TUNIT-NEXT: store float 0.000000e+00, float* [[ARRAYIDX8]], align 4, !tbaa [[TBAA22:![0-9]+]] ; TUNIT-NEXT: br label [[FOR_INC9]] ; TUNIT: for.inc9: ; TUNIT-NEXT: [[INDVARS_IV_NEXT3]] = add nuw nsw i64 [[INDVARS_IV2]], 1 -; TUNIT-NEXT: br label [[FOR_COND2]], !llvm.loop [[LOOP22:![0-9]+]] +; TUNIT-NEXT: br label [[FOR_COND2]], !llvm.loop [[LOOP23:![0-9]+]] ; TUNIT: for.end11: ; TUNIT-NEXT: br label [[FOR_COND13:%.*]] ; TUNIT: for.cond13: @@ -878,11 +880,13 @@ ; TUNIT-NEXT: [[I17:%.*]] = mul nuw nsw i64 [[INDVARS_IV7]], 10 ; TUNIT-NEXT: [[I18:%.*]] = add nuw nsw i64 [[I17]], 2 ; TUNIT-NEXT: [[ARRAYIDX20:%.*]] = getelementptr inbounds i64, i64* bitcast ([1024 x i8]* @GBytes to i64*), i64 [[I18]] +; TUNIT-NEXT: store i64 0, i64* [[ARRAYIDX20]], align 16, !tbaa [[TBAA24:![0-9]+]] ; TUNIT-NEXT: br label [[FOR_INC21]] ; TUNIT: for.inc21: ; TUNIT-NEXT: [[INDVARS_IV_NEXT8]] = add nuw nsw i64 [[INDVARS_IV7]], 1 -; TUNIT-NEXT: br label [[FOR_COND13]], !llvm.loop [[LOOP23:![0-9]+]] +; TUNIT-NEXT: br label [[FOR_COND13]], !llvm.loop [[LOOP26:![0-9]+]] ; TUNIT: for.end23: +; TUNIT-NEXT: store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @GBytes, i64 0, i64 1023), align 1, !tbaa [[TBAA19]] ; TUNIT-NEXT: call void @write_arg(i32* nocapture nofree nonnull writeonly align 4 dereferenceable(524) bitcast (i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @GBytes, i64 0, i64 500) to i32*), i32 noundef 0) #[[ATTR16]] ; TUNIT-NEXT: br label [[FOR_COND25:%.*]] ; TUNIT: for.cond25: @@ -892,12 +896,14 @@ ; TUNIT: for.cond.cleanup27: ; TUNIT-NEXT: br label [[FOR_END35:%.*]] ; TUNIT: for.body28: +; TUNIT-NEXT: [[ARRAYIDX30:%.*]] = getelementptr inbounds [1024 x i8], [1024 x i8]* @GBytes, i64 0, i64 [[INDVARS_IV12]] +; TUNIT-NEXT: [[I19:%.*]] = load i8, i8* [[ARRAYIDX30]], align 1, !tbaa [[TBAA19]] ; TUNIT-NEXT: [[ARRAYIDX32:%.*]] = getelementptr inbounds [1024 x i8], [1024 x i8]* @globalBytes, i64 0, i64 [[INDVARS_IV12]] -; TUNIT-NEXT: store i8 0, i8* [[ARRAYIDX32]], align 1, !tbaa [[TBAA19]] +; TUNIT-NEXT: store i8 [[I19]], i8* [[ARRAYIDX32]], align 1, !tbaa [[TBAA19]] ; TUNIT-NEXT: br label [[FOR_INC33]] ; TUNIT: for.inc33: ; TUNIT-NEXT: [[INDVARS_IV_NEXT13]] = add nuw nsw i64 [[INDVARS_IV12]], 1 -; TUNIT-NEXT: br label [[FOR_COND25]], !llvm.loop [[LOOP24:![0-9]+]] +; TUNIT-NEXT: br label [[FOR_COND25]], !llvm.loop [[LOOP27:![0-9]+]] ; TUNIT: for.end35: ; TUNIT-NEXT: ret void ; @@ -1291,7 +1297,7 @@ ; TUNIT-NEXT: br label [[FOR_INC]] ; TUNIT: for.inc: ; TUNIT-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1 -; TUNIT-NEXT: br label [[FOR_COND]], !llvm.loop [[LOOP25:![0-9]+]] +; TUNIT-NEXT: br label [[FOR_COND]], !llvm.loop [[LOOP28:![0-9]+]] ; TUNIT: for.end: ; TUNIT-NEXT: br label [[FOR_COND2:%.*]] ; TUNIT: for.cond2: @@ -1305,11 +1311,11 @@ ; TUNIT-NEXT: [[I16:%.*]] = mul nuw nsw i64 [[INDVARS_IV2]], 10 ; TUNIT-NEXT: [[I17:%.*]] = or i64 [[I16]], 1 ; TUNIT-NEXT: [[ARRAYIDX8:%.*]] = getelementptr inbounds float, float* [[I15]], i64 [[I17]] -; TUNIT-NEXT: store float 0.000000e+00, float* [[ARRAYIDX8]], align 4, !tbaa [[TBAA26:![0-9]+]] +; TUNIT-NEXT: store float 0.000000e+00, float* [[ARRAYIDX8]], align 4, !tbaa [[TBAA22]] ; TUNIT-NEXT: br label [[FOR_INC9]] ; TUNIT: for.inc9: ; TUNIT-NEXT: [[INDVARS_IV_NEXT3]] = add nuw nsw i64 [[INDVARS_IV2]], 1 -; TUNIT-NEXT: br label [[FOR_COND2]], !llvm.loop [[LOOP27:![0-9]+]] +; TUNIT-NEXT: br label [[FOR_COND2]], !llvm.loop [[LOOP29:![0-9]+]] ; TUNIT: for.end11: ; TUNIT-NEXT: br label [[FOR_COND13:%.*]] ; TUNIT: for.cond13: @@ -1323,7 +1329,7 @@ ; TUNIT-NEXT: [[I19:%.*]] = mul nuw nsw i64 [[INDVARS_IV7]], 10 ; TUNIT-NEXT: [[I20:%.*]] = add nuw nsw i64 [[I19]], 2 ; TUNIT-NEXT: [[ARRAYIDX20:%.*]] = getelementptr inbounds i64, i64* [[I18]], i64 [[I20]] -; TUNIT-NEXT: store i64 0, i64* [[ARRAYIDX20]], align 8, !tbaa [[TBAA28:![0-9]+]] +; TUNIT-NEXT: store i64 0, i64* [[ARRAYIDX20]], align 8, !tbaa [[TBAA24]] ; TUNIT-NEXT: br label [[FOR_INC21]] ; TUNIT: for.inc21: ; TUNIT-NEXT: [[INDVARS_IV_NEXT8]] = add nuw nsw i64 [[INDVARS_IV7]], 1 @@ -3210,14 +3216,14 @@ ; TUNIT: [[TBAA19]] = !{!5, !5, i64 0} ; TUNIT: [[LOOP20]] = distinct !{!20, !16} ; TUNIT: [[LOOP21]] = distinct !{!21, !16} -; TUNIT: [[LOOP22]] = distinct !{!22, !16} +; TUNIT: [[TBAA22]] = !{!9, !9, i64 0} ; TUNIT: [[LOOP23]] = distinct !{!23, !16} -; TUNIT: [[LOOP24]] = distinct !{!24, !16} -; TUNIT: [[LOOP25]] = distinct !{!25, !16} -; TUNIT: [[TBAA26]] = !{!9, !9, i64 0} +; TUNIT: [[TBAA24]] = !{!25, !25, i64 0} +; TUNIT: [[META25:![0-9]+]] = !{!"long long", !5, i64 0} +; TUNIT: [[LOOP26]] = distinct !{!26, !16} ; TUNIT: [[LOOP27]] = distinct !{!27, !16} -; TUNIT: [[TBAA28]] = !{!29, !29, i64 0} -; TUNIT: [[META29:![0-9]+]] = !{!"long long", !5, i64 0} +; TUNIT: [[LOOP28]] = distinct !{!28, !16} +; TUNIT: [[LOOP29]] = distinct !{!29, !16} ; TUNIT: [[LOOP30]] = distinct !{!30, !16} ; TUNIT: [[LOOP31]] = distinct !{!31, !16} ;.