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

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