diff options
author | Nikita Popov <npopov@redhat.com> | 2022-05-02 17:52:02 +0200 |
---|---|---|
committer | Nikita Popov <npopov@redhat.com> | 2022-05-02 17:56:01 +0200 |
commit | 95fedfab6cfb82a2fe1010d266b1269425f5eb46 (patch) | |
tree | d729ed84f4197ba444e93023a36643e84c816455 /llvm/lib/IR/Constants.cpp | |
parent | aa69cb76958c7cab9f079e3fd0d87515821b2309 (diff) | |
download | llvm-95fedfab6cfb82a2fe1010d266b1269425f5eb46.zip llvm-95fedfab6cfb82a2fe1010d266b1269425f5eb46.tar.gz llvm-95fedfab6cfb82a2fe1010d266b1269425f5eb46.tar.bz2 |
[InstCombine] Handle non-canonical GEP index in indexed compare fold (PR55228)
Normally the index type will already be canonicalized here, but
this is not guaranteed depending on visitation order. The code
was already accounting for a potentially needed sext, but a trunc
may also be needed.
Add a ConstantExpr::getSExtOrTrunc() helper method to make this
simpler. This matches the corresponding IRBuilder method in behavior.
Fixes https://github.com/llvm/llvm-project/issues/55228.
Diffstat (limited to 'llvm/lib/IR/Constants.cpp')
-rw-r--r-- | llvm/lib/IR/Constants.cpp | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index 5dcf1ba..c182513 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -2068,6 +2068,17 @@ Constant *ConstantExpr::getTruncOrBitCast(Constant *C, Type *Ty) { return getTrunc(C, Ty); } +Constant *ConstantExpr::getSExtOrTrunc(Constant *C, Type *Ty) { + assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && + "Can only sign extend/truncate integers!"); + Type *CTy = C->getType(); + if (CTy->getScalarSizeInBits() < Ty->getScalarSizeInBits()) + return getSExt(C, Ty); + if (CTy->getScalarSizeInBits() > Ty->getScalarSizeInBits()) + return getTrunc(C, Ty); + return C; +} + Constant *ConstantExpr::getPointerCast(Constant *S, Type *Ty) { assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |