aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
diff options
context:
space:
mode:
authorkper <kevin.per@protonmail.com>2025-09-10 17:54:18 +0200
committerGitHub <noreply@github.com>2025-09-10 15:54:18 +0000
commit265b032bdae0550de69ff52b4ef9deaf78bae522 (patch)
tree43b0d0f3ed102a84a84d10b4d33fade5a2007c47 /llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
parent61e4d2312d59d059f775734a3a7c96a6914c07b7 (diff)
downloadllvm-265b032bdae0550de69ff52b4ef9deaf78bae522.zip
llvm-265b032bdae0550de69ff52b4ef9deaf78bae522.tar.gz
llvm-265b032bdae0550de69ff52b4ef9deaf78bae522.tar.bz2
[InstCombine] Added optimisation for trunc (Pow2 >> x) to i1 (#157030)
Closes #156898 I have added two cases. The first one matches when the constant is exactly power of 2. The second case was to address the general case mentioned in the linked issue. I, however, did not really solve the general case. We can only emit a `icmp ult` if all the bits are one and that's only the case when the constant + 1 is a power of 2. Otherwise, we need to create `icmp eq` for every bit that is one. Here are a few examples which won't be working with the two cases: - constant is `9`: https://alive2.llvm.org/ce/z/S5FLJZ - subrange in `56`: https://alive2.llvm.org/ce/z/yn_ZNG - and finally an example as worst case (because it alternates the bits): https://alive2.llvm.org/ce/z/nDitNA
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index fdef49e..ccf918f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -11,11 +11,13 @@
//===----------------------------------------------------------------------===//
#include "InstCombineInternal.h"
+#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/Value.h"
#include "llvm/Support/KnownBits.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"
#include <optional>
@@ -969,6 +971,25 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
Changed = true;
}
+ const APInt *C1;
+ Value *V1;
+ // OP = { lshr, ashr }
+ // trunc ( OP i8 C1, V1) to i1 -> icmp eq V1, log_2(C1) iff C1 is power of 2
+ if (DestWidth == 1 && match(Src, m_Shr(m_Power2(C1), m_Value(V1)))) {
+ Value *Right = ConstantInt::get(V1->getType(), C1->countr_zero());
+ Value *Icmp = Builder.CreateICmpEQ(V1, Right);
+ return replaceInstUsesWith(Trunc, Icmp);
+ }
+
+ // OP = { lshr, ashr }
+ // trunc ( OP i8 C1, V1) to i1 -> icmp ult V1, log_2(C1 + 1) iff (C1 + 1) is
+ // power of 2
+ if (DestWidth == 1 && match(Src, m_Shr(m_LowBitMask(C1), m_Value(V1)))) {
+ Value *Right = ConstantInt::get(V1->getType(), C1->countr_one());
+ Value *Icmp = Builder.CreateICmpULT(V1, Right);
+ return replaceInstUsesWith(Trunc, Icmp);
+ }
+
return Changed ? &Trunc : nullptr;
}