llvm.org GIT mirror llvm / 87650a2
Revert "[ValueTracking] Improve isKnowNonZero for Ints" This reverts commit 3b137a495686bd6018d115ea82fb8bb7718349fd. As reported in https://reviews.llvm.org/D60846, this is causing miscompiles. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360260 91177308-0d34-0410-b5e6-96231b3b80d8 Nikita Popov 4 months ago
4 changed file(s) with 25 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
18921892 return false;
18931893 }
18941894
1895 static bool isKnownNonZeroFromDominatingCondition(const Value *V,
1895 static bool isKnownNonNullFromDominatingCondition(const Value *V,
18961896 const Instruction *CtxI,
18971897 const DominatorTree *DT) {
1898 assert(V->getType()->isPointerTy() && "V must be pointer type");
18981899 assert(!isa(V) && "Did not expect ConstantPointerNull");
18991900
19001901 if (!CtxI || !DT)
19071908 break;
19081909 NumUsesExplored++;
19091910
1910 // If the value is a pointer and used as an argument to a call or invoke,
1911 // then argument attributes may provide an answer about null-ness.
1912 if (V->getType()->isPointerTy())
1913 if (auto CS = ImmutableCallSite(U))
1914 if (auto *CalledFunc = CS.getCalledFunction())
1915 for (const Argument &Arg : CalledFunc->args())
1916 if (CS.getArgOperand(Arg.getArgNo()) == V &&
1917 Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI))
1918 return true;
1911 // If the value is used as an argument to a call or invoke, then argument
1912 // attributes may provide an answer about null-ness.
1913 if (auto CS = ImmutableCallSite(U))
1914 if (auto *CalledFunc = CS.getCalledFunction())
1915 for (const Argument &Arg : CalledFunc->args())
1916 if (CS.getArgOperand(Arg.getArgNo()) == V &&
1917 Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI))
1918 return true;
19191919
19201920 // Consider only compare instructions uniquely controlling a branch
19211921 CmpInst::Predicate Pred;
20632063 }
20642064
20652065
2066 if (isKnownNonZeroFromDominatingCondition(V, Q.CxtI, Q.DT))
2067 return true;
2068
20692066 // Check for recursive pointer simplifications.
20702067 if (V->getType()->isPointerTy()) {
2068 if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))
2069 return true;
2070
20712071 // Look through bitcast operations, GEPs, and int2ptr instructions as they
20722072 // do not alter the value, or at least not the nullness property of the
20732073 // value, e.g., int2ptr is allowed to zero/sign extend the value.
1212 ; CHECK-NEXT: [[C:%.*]] = icmp eq i64 [[X:%.*]], 0
1313 ; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
1414 ; CHECK: non_zero:
15 ; CHECK-NEXT: [[CTZ:%.*]] = call i64 @llvm.cttz.i64(i64 [[X]], i1 true), !range !0
15 ; CHECK-NEXT: [[CTZ:%.*]] = call i64 @llvm.cttz.i64(i64 [[X]], i1 false), !range !0
1616 ; CHECK-NEXT: [[CTZ32:%.*]] = trunc i64 [[CTZ]] to i32
1717 ; CHECK-NEXT: br label [[EXIT]]
1818 ; CHECK: exit:
3939 ; CHECK-NEXT: [[C:%.*]] = icmp eq i64 [[X:%.*]], 0
4040 ; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
4141 ; CHECK: non_zero:
42 ; CHECK-NEXT: [[CTZ:%.*]] = call i64 @llvm.ctlz.i64(i64 [[X]], i1 true), !range !0
42 ; CHECK-NEXT: [[CTZ:%.*]] = call i64 @llvm.ctlz.i64(i64 [[X]], i1 false), !range !0
4343 ; CHECK-NEXT: [[CTZ32:%.*]] = trunc i64 [[CTZ]] to i32
4444 ; CHECK-NEXT: br label [[EXIT]]
4545 ; CHECK: exit:
66 ; CHECK-NEXT: [[A:%.*]] = icmp eq i64 [[X:%.*]], 0
77 ; CHECK-NEXT: br i1 [[A]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
88 ; CHECK: non_zero:
9 ; CHECK-NEXT: br i1 false, label [[UNREACHABLE:%.*]], label [[EXIT]]
9 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X]], 0
10 ; CHECK-NEXT: br i1 [[B]], label [[UNREACHABLE:%.*]], label [[EXIT]]
1011 ; CHECK: unreachable:
1112 ; CHECK-NEXT: br label [[EXIT]]
1213 ; CHECK: exit:
3536 ; CHECK-NEXT: [[A:%.*]] = icmp eq i64 [[X:%.*]], 0
3637 ; CHECK-NEXT: br i1 [[A]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
3738 ; CHECK: non_zero:
38 ; CHECK-NEXT: br i1 true, label [[EXIT]], label [[UNREACHABLE:%.*]]
39 ; CHECK-NEXT: [[B:%.*]] = icmp ugt i64 [[X]], 0
40 ; CHECK-NEXT: br i1 [[B]], label [[EXIT]], label [[UNREACHABLE:%.*]]
3941 ; CHECK: unreachable:
4042 ; CHECK-NEXT: br label [[EXIT]]
4143 ; CHECK: exit:
7072 ; CHECK: two:
7173 ; CHECK-NEXT: br label [[MAINBLOCK]]
7274 ; CHECK: mainblock:
75 ; CHECK-NEXT: [[P:%.*]] = phi i64 [ [[X]], [[ONE]] ], [ 42, [[TWO]] ]
76 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[P]], 0
7377 ; CHECK-NEXT: br label [[EXIT]]
7478 ; CHECK: exit:
75 ; CHECK-NEXT: [[RES:%.*]] = phi i1 [ false, [[MAINBLOCK]] ], [ true, [[START:%.*]] ]
79 ; CHECK-NEXT: [[RES:%.*]] = phi i1 [ [[CMP]], [[MAINBLOCK]] ], [ true, [[START:%.*]] ]
7680 ; CHECK-NEXT: ret i1 [[RES]]
7781 ;
7882 start:
128128 }
129129
130130 ; requires fact length is non-zero
131 ; TODO: IsKnownNonNullFromDominatingConditions is currently only be done for
132 ; pointers; should handle integers too
131133 define i32 @test4(i32* noalias nocapture readonly %a) nounwind uwtable {
132134 ; CHECK-LABEL: @test4(
133135 ; CHECK-NEXT: entry:
135137 ; CHECK-NEXT: [[IS_ZERO:%.*]] = icmp eq i32 [[LEN]], 0
136138 ; CHECK-NEXT: br i1 [[IS_ZERO]], label [[FAIL:%.*]], label [[PREHEADER:%.*]]
137139 ; CHECK: preheader:
138 ; CHECK-NEXT: [[I1:%.*]] = load i32, i32* [[A]], align 4
139140 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
140141 ; CHECK: for.body:
141142 ; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[PREHEADER]] ], [ [[INC:%.*]], [[CONTINUE:%.*]] ]
143144 ; CHECK-NEXT: [[R_CHK:%.*]] = icmp ult i32 [[IV]], [[LEN]]
144145 ; CHECK-NEXT: br i1 [[R_CHK]], label [[CONTINUE]], label [[FAIL_LOOPEXIT:%.*]]
145146 ; CHECK: continue:
147 ; CHECK-NEXT: [[I1:%.*]] = load i32, i32* [[A]], align 4
146148 ; CHECK-NEXT: [[ADD]] = add nsw i32 [[I1]], [[ACC]]
147149 ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[IV]], 1
148150 ; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], 1000