Blame SOURCES/0001-PowerPC-PPCBoolRetToInt-Don-t-translate-Constant-s-o.patch

08f1e7
From cbea17568f4301582c1d5d43990f089ca6cff522 Mon Sep 17 00:00:00 2001
08f1e7
From: Kai Luo <lkail@cn.ibm.com>
08f1e7
Date: Fri, 28 Aug 2020 01:56:12 +0000
08f1e7
Subject: [PATCH] [PowerPC] PPCBoolRetToInt: Don't translate Constant's
08f1e7
 operands
1e30ac
08f1e7
When collecting `i1` values via `findAllDefs`, ignore Constant's
08f1e7
operands, since Constant's operands might not be `i1`.
1e30ac
1e30ac
Fixes https://bugs.llvm.org/show_bug.cgi?id=46923 which causes ICE
1e30ac
```
1e30ac
llvm-project/llvm/lib/IR/Constants.cpp:1924: static llvm::Constant *llvm::ConstantExpr::getZExt(llvm::Constant *, llvm::Type *, bool): Assertion `C->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits()&& "SrcTy must be smaller than DestTy for ZExt!"' failed.
1e30ac
```
1e30ac
1e30ac
Differential Revision: https://reviews.llvm.org/D85007
1e30ac
---
08f1e7
 llvm/lib/Target/PowerPC/PPCBoolRetToInt.cpp | 15 ++++++-----
08f1e7
 llvm/test/CodeGen/PowerPC/pr46923.ll        | 29 +++++++++++++++++++++
08f1e7
 2 files changed, 38 insertions(+), 6 deletions(-)
1e30ac
 create mode 100644 llvm/test/CodeGen/PowerPC/pr46923.ll
1e30ac
1e30ac
diff --git a/llvm/lib/Target/PowerPC/PPCBoolRetToInt.cpp b/llvm/lib/Target/PowerPC/PPCBoolRetToInt.cpp
08f1e7
index acc8b317a22..172f1346c50 100644
1e30ac
--- a/llvm/lib/Target/PowerPC/PPCBoolRetToInt.cpp
1e30ac
+++ b/llvm/lib/Target/PowerPC/PPCBoolRetToInt.cpp
08f1e7
@@ -78,9 +78,9 @@ class PPCBoolRetToInt : public FunctionPass {
08f1e7
       Value *Curr = WorkList.back();
08f1e7
       WorkList.pop_back();
08f1e7
       auto *CurrUser = dyn_cast<User>(Curr);
08f1e7
-      // Operands of CallInst are skipped because they may not be Bool type,
08f1e7
-      // and their positions are defined by ABI.
08f1e7
-      if (CurrUser && !isa<CallInst>(Curr))
08f1e7
+      // Operands of CallInst/Constant are skipped because they may not be Bool
08f1e7
+      // type. For CallInst, their positions are defined by ABI.
08f1e7
+      if (CurrUser && !isa<CallInst>(Curr) && !isa<Constant>(Curr))
08f1e7
         for (auto &Op : CurrUser->operands())
08f1e7
           if (Defs.insert(Op).second)
08f1e7
             WorkList.push_back(Op);
1e30ac
@@ -90,6 +90,9 @@ class PPCBoolRetToInt : public FunctionPass {
1e30ac
 
1e30ac
   // Translate a i1 value to an equivalent i32/i64 value:
1e30ac
   Value *translate(Value *V) {
1e30ac
+    assert(V->getType() == Type::getInt1Ty(V->getContext()) &&
1e30ac
+           "Expect an i1 value");
1e30ac
+
1e30ac
     Type *IntTy = ST->isPPC64() ? Type::getInt64Ty(V->getContext())
1e30ac
                                 : Type::getInt32Ty(V->getContext());
1e30ac
 
08f1e7
@@ -252,9 +255,9 @@ class PPCBoolRetToInt : public FunctionPass {
08f1e7
       auto *First = dyn_cast<User>(Pair.first);
08f1e7
       auto *Second = dyn_cast<User>(Pair.second);
08f1e7
       assert((!First || Second) && "translated from user to non-user!?");
08f1e7
-      // Operands of CallInst are skipped because they may not be Bool type,
08f1e7
-      // and their positions are defined by ABI.
08f1e7
-      if (First && !isa<CallInst>(First))
08f1e7
+      // Operands of CallInst/Constant are skipped because they may not be Bool
08f1e7
+      // type. For CallInst, their positions are defined by ABI.
08f1e7
+      if (First && !isa<CallInst>(First) && !isa<Constant>(First))
08f1e7
         for (unsigned i = 0; i < First->getNumOperands(); ++i)
08f1e7
           Second->setOperand(i, BoolToIntMap[First->getOperand(i)]);
08f1e7
     }
1e30ac
diff --git a/llvm/test/CodeGen/PowerPC/pr46923.ll b/llvm/test/CodeGen/PowerPC/pr46923.ll
1e30ac
new file mode 100644
08f1e7
index 00000000000..3e9faa60422
1e30ac
--- /dev/null
1e30ac
+++ b/llvm/test/CodeGen/PowerPC/pr46923.ll
08f1e7
@@ -0,0 +1,29 @@
1e30ac
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
1e30ac
+; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-unknown \
1e30ac
+; RUN:   -ppc-asm-full-reg-names < %s | FileCheck %s
1e30ac
+
1e30ac
+@bar = external constant i64, align 8
1e30ac
+
1e30ac
+define i1 @foo() {
1e30ac
+; CHECK-LABEL: foo:
1e30ac
+; CHECK:       # %bb.0: # %entry
1e30ac
+; CHECK-NEXT:    li r3, 0
08f1e7
+; CHECK-NEXT:    isel r3, 0, r3, 4*cr5+lt
1e30ac
+; CHECK-NEXT:    blr
1e30ac
+entry:
1e30ac
+  br label %next
1e30ac
+
1e30ac
+next:
1e30ac
+  br i1 undef, label %true, label %false
1e30ac
+
1e30ac
+true:
1e30ac
+  br label %end
1e30ac
+
1e30ac
+false:
1e30ac
+  br label %end
1e30ac
+
1e30ac
+end:
1e30ac
+  %a = phi i1 [ icmp ugt (i64 0, i64 ptrtoint (i64* @bar to i64)), %true ],
1e30ac
+              [ icmp ugt (i64 0, i64 2), %false ]
1e30ac
+  ret i1 %a
1e30ac
+}
1e30ac
-- 
08f1e7
2.25.2
1e30ac