[llvm] [ValueTracking] Improve `isImpliedCondCommonOperandWithConstants` to handle truncated LHS (PR #69829)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 22 00:49:51 PDT 2023


================
@@ -8231,14 +8231,30 @@ isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
   return std::nullopt;
 }
 
-/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
-/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
-/// Otherwise, return std::nullopt if we can't infer anything.
+/// Return true if "icmp LPred X, LC" implies "icmp RPred cast(X), RC" is true.
+/// Return false if "icmp LPred X, LC" implies "icmp RPred cast(X), RC" is
+/// false. Otherwise, return std::nullopt if we can't infer anything.
 static std::optional<bool> isImpliedCondCommonOperandWithConstants(
-    CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
-    const APInt &RC) {
+    const Value *L0, CmpInst::Predicate LPred, const APInt &LC, const Value *R0,
+    CmpInst::Predicate RPred, const APInt &RC) {
   ConstantRange DomCR = ConstantRange::makeExactICmpRegion(LPred, LC);
   ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, RC);
+
+  if (L0 == R0)
+    ; // noop
+  // Example: icmp eq X, 3 --> icmp sgt trunc(X), 2
+  else if (match(R0, m_Trunc(m_Specific(L0))))
+    DomCR = DomCR.truncate(RC.getBitWidth());
+  // Example: icmp slt trunc(X), 3 --> icmp ne X, 3
+  else if (match(L0, m_Trunc(m_Specific(R0)))) {
----------------
dtcxzyw wrote:

`icmp pred zext/sext(X), C` will be simplified to `icmp pred X, C1`. So we don't need to handle these cases.

https://github.com/llvm/llvm-project/pull/69829


More information about the llvm-commits mailing list