Index: include/llvm/Analysis/BasicAliasAnalysis.h =================================================================== --- include/llvm/Analysis/BasicAliasAnalysis.h +++ include/llvm/Analysis/BasicAliasAnalysis.h @@ -183,6 +183,12 @@ uint64_t V2Size, const AAMDNodes &V2AAInfo, const Value *UnderlyingV1, const Value *UnderlyingV2); + AliasResult aliasSameBasePointerGEPs(const GEPOperator *GEP1, + uint64_t V1Size, + const GEPOperator *GEP2, + uint64_t V2Size, + const DataLayout &DL); + AliasResult aliasPHI(const PHINode *PN, uint64_t PNSize, const AAMDNodes &PNAAInfo, const Value *V2, uint64_t V2Size, const AAMDNodes &V2AAInfo, Index: lib/Analysis/BasicAliasAnalysis.cpp =================================================================== --- lib/Analysis/BasicAliasAnalysis.cpp +++ lib/Analysis/BasicAliasAnalysis.cpp @@ -922,11 +922,11 @@ /// Provide ad-hoc rules to disambiguate accesses through two GEP operators, /// both having the exact same pointer operand. -static AliasResult aliasSameBasePointerGEPs(const GEPOperator *GEP1, - uint64_t V1Size, - const GEPOperator *GEP2, - uint64_t V2Size, - const DataLayout &DL) { +AliasResult BasicAAResult::aliasSameBasePointerGEPs(const GEPOperator *GEP1, + uint64_t V1Size, + const GEPOperator *GEP2, + uint64_t V2Size, + const DataLayout &DL) { assert(GEP1->getPointerOperand()->stripPointerCastsAndBarriers() == GEP2->getPointerOperand()->stripPointerCastsAndBarriers() && @@ -1006,7 +1006,7 @@ // Because they cannot partially overlap and because fields in an array // cannot overlap, if we can prove the final indices are different between // GEP1 and GEP2, we can conclude GEP1 and GEP2 don't alias. - + // If the last indices are constants, we've already checked they don't // equal each other so we can exit early. if (C1 && C2) @@ -1014,11 +1014,15 @@ { Value *GEP1LastIdx = GEP1->getOperand(GEP1->getNumOperands() - 1); Value *GEP2LastIdx = GEP2->getOperand(GEP2->getNumOperands() - 1); - if (isa(GEP1LastIdx) || isa(GEP2LastIdx)) { + if ((isa(GEP1LastIdx) || isa(GEP2LastIdx)) && + VisitedPhiBBs.size() != 0) { // If one of the indices is a PHI node, be safe and only use // computeKnownBits so we don't make any assumptions about the // relationships between the two indices. This is important if we're // asking about values from different loop iterations. See PR32314. + // But, with zero visitedPhiBBs we can guarantee that the values are + // from the same iteration. Therefore, we can avoid doing this + // conservative check. // TODO: We may be able to change the check so we only do this when // we definitely looked through a PHINode. KnownBits Known1 = computeKnownBits(GEP1LastIdx, DL); Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -1878,7 +1878,7 @@ if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal) return true; // Are all the bits to be shifted out known zero? - if (Known.countMinTrailingZeros() >= ShiftVal) + if (Known.isUnknown() || Known.countMinTrailingZeros() >= ShiftVal) return isKnownNonZero(X, Depth, Q); } } Index: test/Transforms/GVN/pr33549.ll =================================================================== --- test/Transforms/GVN/pr33549.ll +++ test/Transforms/GVN/pr33549.ll @@ -0,0 +1,98 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -S -gvn < %s | FileCheck %s + +; The purpose of this test is to make sure that the load(%2) is pre'd. + +; ModuleID = 'test.c' +source_filename = "test.c" +target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" +target triple = "thumbv7em-arm-none-eabi" + +@Data = common local_unnamed_addr global [32 x i32] zeroinitializer, align 4 + +; Function Attrs: norecurse nounwind +define void @testshl() local_unnamed_addr #0 { +; CHECK-LABEL: @testshl( +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[FOR_BODY:%.*]] +; CHECK: for.body: +; CHECK-NEXT: [[K_031:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ [[INC9:%.*]], [[FOR_INC8:%.*]] ] +; CHECK-NEXT: [[SHL:%.*]] = shl i32 1, [[K_031]] +; CHECK-NEXT: [[SHR:%.*]] = ashr exact i32 [[SHL]], 1 +; CHECK-NEXT: [[CMP229:%.*]] = icmp slt i32 [[SHL]], 64 +; CHECK-NEXT: br i1 [[CMP229]], label [[FOR_BODY3_PREHEADER:%.*]], label [[FOR_INC8]] +; CHECK: for.body3.preheader: +; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[SHR]], 2 +; CHECK-NEXT: br label [[FOR_BODY3:%.*]] +; CHECK: for.body3: +; CHECK-NEXT: [[I_030:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_BODY3]] ], [ [[DIV]], [[FOR_BODY3_PREHEADER]] ] +; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[I_030]], [[SHR]] +; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [32 x i32], [32 x i32]* @Data, i32 0, i32 [[ADD]] +; CHECK-NEXT: [[ARRAYIDX4:%.*]] = getelementptr inbounds [32 x i32], [32 x i32]* @Data, i32 0, i32 [[I_030]] +; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* [[ARRAYIDX]], align 4, !tbaa !3 +; CHECK-NEXT: [[TMP1:%.*]] = load i32, i32* [[ARRAYIDX4]], align 4, !tbaa !3 +; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[TMP1]], [[TMP0]] +; CHECK-NEXT: store i32 [[SUB]], i32* [[ARRAYIDX]], align 4, !tbaa !3 +; CHECK-NEXT: [[ADD7:%.*]] = add nsw i32 [[TMP1]], [[TMP0]] +; CHECK-NEXT: store i32 [[ADD7]], i32* [[ARRAYIDX4]], align 4, !tbaa !3 +; CHECK-NEXT: [[INC]] = add nsw i32 [[I_030]], 1 +; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[I_030]], 15 +; CHECK-NEXT: br i1 [[CMP2]], label [[FOR_BODY3]], label [[FOR_INC8]] +; CHECK: for.inc8: +; CHECK-NEXT: [[INC9]] = add nuw nsw i32 [[K_031]], 1 +; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC9]], 8 +; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_END10:%.*]], label [[FOR_BODY]] +; CHECK: for.end10: +; CHECK-NEXT: ret void +; +entry: + br label %for.body + +for.body: ; preds = %for.inc8, %entry + %k.031 = phi i32 [ 1, %entry ], [ %inc9, %for.inc8 ] + %shl = shl i32 1, %k.031 + %shr = ashr exact i32 %shl, 1 + %cmp229 = icmp slt i32 %shl, 64 + br i1 %cmp229, label %for.body3.preheader, label %for.inc8 + +for.body3.preheader: ; preds = %for.body + %div = sdiv i32 %shr, 2 + br label %for.body3 + +for.body3: ; preds = %for.body3.preheader, %for.body3 + %i.030 = phi i32 [ %inc, %for.body3 ], [ %div, %for.body3.preheader ] + %add = add nsw i32 %i.030, %shr + %arrayidx = getelementptr inbounds [32 x i32], [32 x i32]* @Data, i32 0, i32 %add + %arrayidx4 = getelementptr inbounds [32 x i32], [32 x i32]* @Data, i32 0, i32 %i.030 + %0 = load i32, i32* %arrayidx, align 4, !tbaa !3 + %1 = load i32, i32* %arrayidx4, align 4, !tbaa !3 + %sub = sub nsw i32 %1, %0 + store i32 %sub, i32* %arrayidx, align 4, !tbaa !3 + %2 = load i32, i32* %arrayidx4, align 4, !tbaa !3 + %add7 = add nsw i32 %2, %0 + store i32 %add7, i32* %arrayidx4, align 4, !tbaa !3 + %inc = add nsw i32 %i.030, 1 + %cmp2 = icmp slt i32 %i.030, 15 + br i1 %cmp2, label %for.body3, label %for.inc8 + +for.inc8: ; preds = %for.body3, %for.body + %inc9 = add nuw nsw i32 %k.031, 1 + %exitcond = icmp eq i32 %inc9, 8 + br i1 %exitcond, label %for.end10, label %for.body + +for.end10: ; preds = %for.inc8 + ret void +} + +attributes #0 = { norecurse nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="cortex-m7" "target-features"="+d16,+dsp,+fp-armv8,+hwdiv,+strict-align,+thumb-mode" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.module.flags = !{!0, !1} +!llvm.ident = !{!2} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 1, !"min_enum_size", i32 4} +!2 = !{!"clang version 5.0.0 (http://llvm.org/git/clang.git 100ecde90ef26ee81763214d5fb335415ffc2b53) (http://llvm.org/git/llvm.git f2fe26d60eb81a699f2d565e8baabcaf32f9c5ea)"} +!3 = !{!4, !4, i64 0} +!4 = !{!"int", !5, i64 0} +!5 = !{!"omnipotent char", !6, i64 0} +!6 = !{!"Simple C/C++ TBAA"}