llvm.org GIT mirror llvm / d01f50f
ConstantInt has some getters which return ConstantInt's or ConstantVector's of the value splatted into every element. Extend this to getTrue and getFalse which by providing new overloads that take Types that are either i1 or <N x i1>. Use it in InstCombine to add vector support to some code, fixing PR8469! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@127116 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 8 years ago
4 changed file(s) with 65 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
5656 public:
5757 static ConstantInt *getTrue(LLVMContext &Context);
5858 static ConstantInt *getFalse(LLVMContext &Context);
59 static Constant *getTrue(const Type *Ty);
60 static Constant *getFalse(const Type *Ty);
5961
6062 /// If Ty is a vector type, return a Constant with a splat of the given
6163 /// value. Otherwise return a ConstantInt for the given value.
19141914 // that code below can assume that Min != Max.
19151915 if (!isa(Op0) && Op0Min == Op0Max)
19161916 return new ICmpInst(I.getPredicate(),
1917 ConstantInt::get(I.getContext(), Op0Min), Op1);
1917 ConstantInt::get(Op0->getType(), Op0Min), Op1);
19181918 if (!isa(Op1) && Op1Min == Op1Max)
19191919 return new ICmpInst(I.getPredicate(), Op0,
1920 ConstantInt::get(I.getContext(), Op1Min));
1920 ConstantInt::get(Op1->getType(), Op1Min));
19211921
19221922 // Based on the range information we know about the LHS, see if we can
19231923 // simplify this comparison. For example, (x&4) < 8 is always true.
19251925 default: llvm_unreachable("Unknown icmp opcode!");
19261926 case ICmpInst::ICMP_EQ: {
19271927 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
1928 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
1928 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
19291929
19301930 // If all bits are known zero except for one, then we know at most one
19311931 // bit is set. If the comparison is against zero, then this is a check
19621962 }
19631963 case ICmpInst::ICMP_NE: {
19641964 if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
1965 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
1965 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
19661966
19671967 // If all bits are known zero except for one, then we know at most one
19681968 // bit is set. If the comparison is against zero, then this is a check
19991999 }
20002000 case ICmpInst::ICMP_ULT:
20012001 if (Op0Max.ult(Op1Min)) // A true if max(A) < min(B)
2002 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2002 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
20032003 if (Op0Min.uge(Op1Max)) // A false if min(A) >= max(B)
2004 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2004 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
20052005 if (Op1Min == Op0Max) // A A != B if max(A) == min(B)
20062006 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
20072007 if (ConstantInt *CI = dyn_cast(Op1)) {
20172017 break;
20182018 case ICmpInst::ICMP_UGT:
20192019 if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
2020 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2020 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
20212021 if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
2022 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2022 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
20232023
20242024 if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
20252025 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
20362036 break;
20372037 case ICmpInst::ICMP_SLT:
20382038 if (Op0Max.slt(Op1Min)) // A true if max(A) < min(C)
2039 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2039 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
20402040 if (Op0Min.sge(Op1Max)) // A false if min(A) >= max(C)
2041 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2041 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
20422042 if (Op1Min == Op0Max) // A A != B if max(A) == min(B)
20432043 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
20442044 if (ConstantInt *CI = dyn_cast(Op1)) {
20492049 break;
20502050 case ICmpInst::ICMP_SGT:
20512051 if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
2052 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2052 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
20532053 if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
2054 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2054 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
20552055
20562056 if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
20572057 return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
20642064 case ICmpInst::ICMP_SGE:
20652065 assert(!isa(Op1) && "ICMP_SGE with ConstantInt not folded!");
20662066 if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
2067 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2067 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
20682068 if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
2069 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2069 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
20702070 break;
20712071 case ICmpInst::ICMP_SLE:
20722072 assert(!isa(Op1) && "ICMP_SLE with ConstantInt not folded!");
20732073 if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
2074 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2074 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
20752075 if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
2076 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2076 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
20772077 break;
20782078 case ICmpInst::ICMP_UGE:
20792079 assert(!isa(Op1) && "ICMP_UGE with ConstantInt not folded!");
20802080 if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
2081 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2081 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
20822082 if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
2083 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2083 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
20842084 break;
20852085 case ICmpInst::ICMP_ULE:
20862086 assert(!isa(Op1) && "ICMP_ULE with ConstantInt not folded!");
20872087 if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
2088 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2088 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
20892089 if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
2090 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2090 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
20912091 break;
20922092 }
20932093
23282328 switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
23292329 default: break;
23302330 case ICmpInst::ICMP_EQ:
2331 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext()));
2331 return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
23322332 case ICmpInst::ICMP_NE:
2333 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext()));
2333 return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
23342334 case ICmpInst::ICMP_SGT:
23352335 case ICmpInst::ICMP_SGE:
23362336 return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
325325 assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type");
326326 }
327327
328 ConstantInt* ConstantInt::getTrue(LLVMContext &Context) {
328 ConstantInt *ConstantInt::getTrue(LLVMContext &Context) {
329329 LLVMContextImpl *pImpl = Context.pImpl;
330330 if (!pImpl->TheTrueVal)
331331 pImpl->TheTrueVal = ConstantInt::get(Type::getInt1Ty(Context), 1);
332332 return pImpl->TheTrueVal;
333333 }
334334
335 ConstantInt* ConstantInt::getFalse(LLVMContext &Context) {
335 ConstantInt *ConstantInt::getFalse(LLVMContext &Context) {
336336 LLVMContextImpl *pImpl = Context.pImpl;
337337 if (!pImpl->TheFalseVal)
338338 pImpl->TheFalseVal = ConstantInt::get(Type::getInt1Ty(Context), 0);
339339 return pImpl->TheFalseVal;
340 }
341
342 Constant *ConstantInt::getTrue(const Type *Ty) {
343 const VectorType *VTy = dyn_cast(Ty);
344 if (!VTy) {
345 assert(Ty->isIntegerTy(1) && "True must be i1 or vector of i1.");
346 return ConstantInt::getTrue(Ty->getContext());
347 }
348 assert(VTy->getElementType()->isIntegerTy(1) &&
349 "True must be vector of i1 or i1.");
350 SmallVector Splat(VTy->getNumElements(),
351 ConstantInt::getTrue(Ty->getContext()));
352 return ConstantVector::get(Splat);
353 }
354
355 Constant *ConstantInt::getFalse(const Type *Ty) {
356 const VectorType *VTy = dyn_cast(Ty);
357 if (!VTy) {
358 assert(Ty->isIntegerTy(1) && "False must be i1 or vector of i1.");
359 return ConstantInt::getFalse(Ty->getContext());
360 }
361 assert(VTy->getElementType()->isIntegerTy(1) &&
362 "False must be vector of i1 or i1.");
363 SmallVector Splat(VTy->getNumElements(),
364 ConstantInt::getFalse(Ty->getContext()));
365 return ConstantVector::get(Splat);
340366 }
341367
342368
345371 // operator== and operator!= to ensure that the DenseMap doesn't attempt to
346372 // compare APInt's of different widths, which would violate an APInt class
347373 // invariant which generates an assertion.
348 ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt& V) {
374 ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) {
349375 // Get the corresponding integer type for the bit width of the value.
350376 const IntegerType *ITy = IntegerType::get(Context, V.getBitWidth());
351377 // get an existing value or the insertion position
355381 return Slot;
356382 }
357383
358 Constant *ConstantInt::get(const Type* Ty, uint64_t V, bool isSigned) {
359 Constant *C = get(cast(Ty->getScalarType()),
360 V, isSigned);
384 Constant *ConstantInt::get(const Type *Ty, uint64_t V, bool isSigned) {
385 Constant *C = get(cast(Ty->getScalarType()), V, isSigned);
361386
362387 // For vectors, broadcast the value.
363388 if (const VectorType *VTy = dyn_cast(Ty))
464464 %C = icmp eq i32 %A, %B
465465 ret i1 %C
466466 }
467
468 ; PR8469
469 ; CHECK: @test49
470 ; CHECK: ret <2 x i1>
471 define <2 x i1> @test49(<2 x i32> %tmp3) {
472 entry:
473 %tmp11 = and <2 x i32> %tmp3,
474 %cmp = icmp ult <2 x i32> %tmp11,
475 ret <2 x i1> %cmp
476 }