[llvm] [IR] Remove zext and sext constant expressions (PR #71040)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 2 02:46:09 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

Remove support for zext and sext constant expressions. All places creating them have been removed beforehand, so this just removes the APIs and uses of these constant expressions in tests.

There is some additional cleanup that can be done on top of this, e.g. we can remove the ZExtInst vs ZExtOperator footgun.

This is part of https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.

---

Patch is 95.94 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/71040.diff


61 Files Affected:

- (modified) llvm/bindings/ocaml/llvm/llvm.ml (-8) 
- (modified) llvm/bindings/ocaml/llvm/llvm.mli (-27) 
- (modified) llvm/bindings/ocaml/llvm/llvm_ocaml.c (-30) 
- (modified) llvm/docs/ReleaseNotes.rst (+7) 
- (modified) llvm/include/llvm-c/Core.h (-8) 
- (modified) llvm/include/llvm/IR/Constants.h (+3-26) 
- (modified) llvm/lib/AsmParser/LLParser.cpp (+4-2) 
- (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+3) 
- (modified) llvm/lib/IR/ConstantFold.cpp (-72) 
- (modified) llvm/lib/IR/Constants.cpp (+24-67) 
- (modified) llvm/lib/IR/Core.cpp (-28) 
- (modified) llvm/test/Analysis/Lint/noop-cast-expr-no-pointer.ll (+3-3) 
- (modified) llvm/test/Analysis/ScalarEvolution/ptrtoint-constantexpr-loop.ll (+17-7) 
- (removed) llvm/test/Assembler/2009-03-24-ZextConstantExpr.ll (-12) 
- (modified) llvm/test/Bindings/OCaml/core.ml (-7) 
- (modified) llvm/test/CodeGen/AArch64/arm64-2012-05-09-LOADgot-bug.ll (+2-1) 
- (modified) llvm/test/CodeGen/AArch64/arm64-codegen-prepare-extload.ll (+5-3) 
- (modified) llvm/test/CodeGen/AMDGPU/GlobalISel/irtranslator-constantexpr.ll (+5-7) 
- (modified) llvm/test/CodeGen/ARM/2007-04-03-UndefinedSymbol.ll (+57-33) 
- (modified) llvm/test/CodeGen/ARM/2012-01-23-PostRA-LICM.ll (+31-15) 
- (modified) llvm/test/CodeGen/ARM/2018-02-13-PR36079.ll (+14-5) 
- (modified) llvm/test/CodeGen/Hexagon/opt-addr-mode-subreg-use.ll (+12-6) 
- (modified) llvm/test/CodeGen/Hexagon/packetize-l2fetch.ll (+2-1) 
- (modified) llvm/test/CodeGen/PowerPC/ext-bool-trunc-repl.ll (+21-11) 
- (modified) llvm/test/CodeGen/WebAssembly/fast-isel-noreg.ll (+4-2) 
- (modified) llvm/test/CodeGen/X86/2008-09-19-RegAllocBug.ll (+3-1) 
- (removed) llvm/test/CodeGen/X86/2012-03-20-LargeConstantExpr.ll (-17) 
- (modified) llvm/test/CodeGen/X86/absolute-constant.ll (+2-1) 
- (modified) llvm/test/CodeGen/X86/absolute-rotate.ll (+8-4) 
- (modified) llvm/test/CodeGen/X86/address-type-promotion-constantexpr.ll (+2-1) 
- (modified) llvm/test/CodeGen/X86/codegen-prepare-extload.ll (+6-4) 
- (modified) llvm/test/CodeGen/X86/fast-isel-expect.ll (+2-1) 
- (modified) llvm/test/Feature/constexpr.ll (+1-1) 
- (modified) llvm/test/Feature/newcasts.ll (-9) 
- (modified) llvm/test/Feature/vector-cast-constant-exprs.ll (-6) 
- (modified) llvm/test/Integer/constexpr_bt.ll (+1-1) 
- (modified) llvm/test/Integer/newcasts_bt.ll (-9) 
- (modified) llvm/test/Transforms/GlobalDCE/complex-constantexpr.ll (+3-2) 
- (modified) llvm/test/Transforms/GlobalOpt/dead-constant-user.ll (+1-1) 
- (modified) llvm/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll (+2-1) 
- (modified) llvm/test/Transforms/InstCombine/2012-3-15-or-xor-constant.ll (+2-1) 
- (modified) llvm/test/Transforms/InstCombine/constant-fold-compare.ll (+2-1) 
- (modified) llvm/test/Transforms/InstCombine/constant-fold-iteration.ll (+2-1) 
- (modified) llvm/test/Transforms/InstCombine/fold-bin-operand.ll (+2-1) 
- (modified) llvm/test/Transforms/InstCombine/icmp-mul.ll (+2-1) 
- (modified) llvm/test/Transforms/InstCombine/icmp.ll (+8-2) 
- (modified) llvm/test/Transforms/InstCombine/mul-inseltpoison.ll (+6-3) 
- (modified) llvm/test/Transforms/InstCombine/mul.ll (+6-3) 
- (modified) llvm/test/Transforms/InstCombine/not-add.ll (+2-1) 
- (modified) llvm/test/Transforms/InstCombine/overflow-mul.ll (+2-1) 
- (modified) llvm/test/Transforms/InstCombine/pr32686.ll (+6-2) 
- (modified) llvm/test/Transforms/InstCombine/pr35515.ll (+5-1) 
- (modified) llvm/test/Transforms/InstCombine/rem.ll (+4-2) 
- (modified) llvm/test/Transforms/InstCombine/shift-amount-reassociation-in-bittest.ll (+6-3) 
- (modified) llvm/test/Transforms/InstCombine/udiv-simplify.ll (+4-2) 
- (modified) llvm/test/Transforms/InstCombine/udivrem-change-width.ll (+18-9) 
- (modified) llvm/test/Transforms/InstSimplify/ConstProp/constant-expr.ll (-7) 
- (modified) llvm/test/Transforms/JumpThreading/constant-fold-status.ll (+1-1) 
- (modified) llvm/test/Transforms/SCCP/undef-resolve.ll (+12-10) 
- (modified) llvm/test/Transforms/SLPVectorizer/X86/bad-reduction.ll (+12-4) 
- (modified) llvm/unittests/IR/ConstantsTest.cpp (-2) 


``````````diff
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index a768d10d5d4dc9b..cabf32cae376f22 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -661,8 +661,6 @@ external const_gep : lltype -> llvalue -> llvalue array -> llvalue
 external const_in_bounds_gep : lltype -> llvalue -> llvalue array -> llvalue
                              = "llvm_const_in_bounds_gep"
 external const_trunc : llvalue -> lltype -> llvalue = "llvm_const_trunc"
-external const_sext : llvalue -> lltype -> llvalue = "llvm_const_sext"
-external const_zext : llvalue -> lltype -> llvalue = "llvm_const_zext"
 external const_fptrunc : llvalue -> lltype -> llvalue = "llvm_const_fptrunc"
 external const_fpext : llvalue -> lltype -> llvalue = "llvm_const_fpext"
 external const_uitofp : llvalue -> lltype -> llvalue = "llvm_const_uitofp"
@@ -672,16 +670,10 @@ external const_fptosi : llvalue -> lltype -> llvalue = "llvm_const_fptosi"
 external const_ptrtoint : llvalue -> lltype -> llvalue = "llvm_const_ptrtoint"
 external const_inttoptr : llvalue -> lltype -> llvalue = "llvm_const_inttoptr"
 external const_bitcast : llvalue -> lltype -> llvalue = "llvm_const_bitcast"
-external const_zext_or_bitcast : llvalue -> lltype -> llvalue
-                               = "llvm_const_zext_or_bitcast"
-external const_sext_or_bitcast : llvalue -> lltype -> llvalue
-                               = "llvm_const_sext_or_bitcast"
 external const_trunc_or_bitcast : llvalue -> lltype -> llvalue
                                 = "llvm_const_trunc_or_bitcast"
 external const_pointercast : llvalue -> lltype -> llvalue
                            = "llvm_const_pointercast"
-external const_intcast : llvalue -> lltype -> is_signed:bool -> llvalue
-                       = "llvm_const_intcast"
 external const_fpcast : llvalue -> lltype -> llvalue = "llvm_const_fpcast"
 external const_extractelement : llvalue -> llvalue -> llvalue
                               = "llvm_const_extractelement"
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index 9e85f01df383596..ee691ca83624455 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -1171,16 +1171,6 @@ val const_in_bounds_gep : lltype -> llvalue -> llvalue array -> llvalue
     See the method [llvm::ConstantExpr::getTrunc]. *)
 val const_trunc : llvalue -> lltype -> llvalue
 
-(** [const_sext c ty] returns the constant sign extension of integer constant
-    [c] to the larger integer type [ty].
-    See the method [llvm::ConstantExpr::getSExt]. *)
-val const_sext : llvalue -> lltype -> llvalue
-
-(** [const_zext c ty] returns the constant zero extension of integer constant
-    [c] to the larger integer type [ty].
-    See the method [llvm::ConstantExpr::getZExt]. *)
-val const_zext : llvalue -> lltype -> llvalue
-
 (** [const_fptrunc c ty] returns the constant truncation of floating point
     constant [c] to the smaller floating point type [ty].
     See the method [llvm::ConstantExpr::getFPTrunc]. *)
@@ -1226,16 +1216,6 @@ val const_inttoptr : llvalue -> lltype -> llvalue
     See the method [llvm::ConstantExpr::getBitCast]. *)
 val const_bitcast : llvalue -> lltype -> llvalue
 
-(** [const_zext_or_bitcast c ty] returns a constant zext or bitwise cast
-    conversion of constant [c] to type [ty].
-    See the method [llvm::ConstantExpr::getZExtOrBitCast]. *)
-val const_zext_or_bitcast : llvalue -> lltype -> llvalue
-
-(** [const_sext_or_bitcast c ty] returns a constant sext or bitwise cast
-    conversion of constant [c] to type [ty].
-    See the method [llvm::ConstantExpr::getSExtOrBitCast]. *)
-val const_sext_or_bitcast : llvalue -> lltype -> llvalue
-
 (** [const_trunc_or_bitcast c ty] returns a constant trunc or bitwise cast
     conversion of constant [c] to type [ty].
     See the method [llvm::ConstantExpr::getTruncOrBitCast]. *)
@@ -1246,13 +1226,6 @@ val const_trunc_or_bitcast : llvalue -> lltype -> llvalue
     See the method [llvm::ConstantExpr::getPointerCast]. *)
 val const_pointercast : llvalue -> lltype -> llvalue
 
-(** [const_intcast c ty ~is_signed] returns a constant sext/zext, bitcast,
-    or trunc for integer -> integer casts of constant [c] to type [ty].
-    When converting a narrower value to a wider one, whether sext or zext
-    will be used is controlled by [is_signed].
-    See the method [llvm::ConstantExpr::getIntegerCast]. *)
-val const_intcast : llvalue -> lltype -> is_signed:bool -> llvalue
-
 (** [const_fpcast c ty] returns a constant fpext, bitcast, or fptrunc for fp ->
     fp casts of constant [c] to type [ty].
     See the method [llvm::ConstantExpr::getFPCast]. *)
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index f0e47a31af03d7b..7c5df6643d21a8a 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -1271,18 +1271,6 @@ value llvm_const_trunc(value CV, value T) {
   return to_val(Value);
 }
 
-/* llvalue -> lltype -> llvalue */
-value llvm_const_sext(value CV, value T) {
-  LLVMValueRef Value = LLVMConstSExt(Value_val(CV), Type_val(T));
-  return to_val(Value);
-}
-
-/* llvalue -> lltype -> llvalue */
-value llvm_const_zext(value CV, value T) {
-  LLVMValueRef Value = LLVMConstZExt(Value_val(CV), Type_val(T));
-  return to_val(Value);
-}
-
 /* llvalue -> lltype -> llvalue */
 value llvm_const_fptrunc(value CV, value T) {
   LLVMValueRef Value = LLVMConstFPTrunc(Value_val(CV), Type_val(T));
@@ -1337,18 +1325,6 @@ value llvm_const_bitcast(value CV, value T) {
   return to_val(Value);
 }
 
-/* llvalue -> lltype -> llvalue */
-value llvm_const_zext_or_bitcast(value CV, value T) {
-  LLVMValueRef Value = LLVMConstZExtOrBitCast(Value_val(CV), Type_val(T));
-  return to_val(Value);
-}
-
-/* llvalue -> lltype -> llvalue */
-value llvm_const_sext_or_bitcast(value CV, value T) {
-  LLVMValueRef Value = LLVMConstSExtOrBitCast(Value_val(CV), Type_val(T));
-  return to_val(Value);
-}
-
 /* llvalue -> lltype -> llvalue */
 value llvm_const_trunc_or_bitcast(value CV, value T) {
   LLVMValueRef Value = LLVMConstTruncOrBitCast(Value_val(CV), Type_val(T));
@@ -1361,12 +1337,6 @@ value llvm_const_pointercast(value CV, value T) {
   return to_val(Value);
 }
 
-/* llvalue -> lltype -> is_signed:bool -> llvalue */
-value llvm_const_intcast(value CV, value T, value IsSigned) {
-  return to_val(
-      LLVMConstIntCast(Value_val(CV), Type_val(T), Bool_val(IsSigned)));
-}
-
 /* llvalue -> lltype -> llvalue */
 value llvm_const_fpcast(value CV, value T) {
   LLVMValueRef Value = LLVMConstFPCast(Value_val(CV), Type_val(T));
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 25817e6e1d7f413..23b54e6a37a7451 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -57,6 +57,8 @@ Changes to the LLVM IR
 
   * ``and``
   * ``or``
+  * ``zext``
+  * ``sext``
 
 * Added `llvm.exp10` intrinsic.
 
@@ -166,6 +168,11 @@ Changes to the C API
 
   * ``LLVMConstAnd``
   * ``LLVMConstOr``
+  * ``LLVMConstZExt``
+  * ``LLVMConstSExt``
+  * ``LLVMConstZExtOrBitCast``
+  * ``LLVMConstSExtOrBitCast``
+  * ``LLVMConstIntCast``
 
 * Added ``LLVMCreateTargetMachineWithOptions``, along with helper functions for
   an opaque option structure, as an alternative to ``LLVMCreateTargetMachine``.
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 7ee46cac43042a1..4fc88b2b64eacea 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2290,8 +2290,6 @@ LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
                                    LLVMValueRef *ConstantIndices,
                                    unsigned NumIndices);
 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
-LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
-LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
@@ -2302,16 +2300,10 @@ LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
-LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
-                                    LLVMTypeRef ToType);
-LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
-                                    LLVMTypeRef ToType);
 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
                                      LLVMTypeRef ToType);
 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
                                   LLVMTypeRef ToType);
-LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
-                              LLVMBool isSigned);
 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
                                      LLVMValueRef IndexConstant);
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index afa1aefb44aa1aa..43744ba8a617812 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -1041,8 +1041,6 @@ class ConstantExpr : public Constant {
   static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
   static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
   static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
-  static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
-  static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
   static Constant *getFPTrunc(Constant *C, Type *Ty,
                               bool OnlyIfReduced = false);
   static Constant *getFPExtend(Constant *C, Type *Ty,
@@ -1140,29 +1138,12 @@ class ConstantExpr : public Constant {
   static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
                            bool OnlyIfReduced = false);
 
-  // Create a ZExt or BitCast cast constant expression
-  static Constant *
-  getZExtOrBitCast(Constant *C, ///< The constant to zext or bitcast
-                   Type *Ty     ///< The type to zext or bitcast C to
-  );
-
-  // Create a SExt or BitCast cast constant expression
-  static Constant *
-  getSExtOrBitCast(Constant *C, ///< The constant to sext or bitcast
-                   Type *Ty     ///< The type to sext or bitcast C to
-  );
-
   // Create a Trunc or BitCast cast constant expression
   static Constant *
   getTruncOrBitCast(Constant *C, ///< The constant to trunc or bitcast
                     Type *Ty     ///< The type to trunc or bitcast C to
   );
 
-  /// Create either an sext, trunc or nothing, depending on whether Ty is
-  /// wider, narrower or the same as C->getType(). This only works with
-  /// integer or vector of integer types.
-  static Constant *getSExtOrTrunc(Constant *C, Type *Ty);
-
   /// Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
   /// expression.
   static Constant *
@@ -1177,13 +1158,6 @@ class ConstantExpr : public Constant {
       Type *Ty     ///< The type to bitcast or addrspacecast C to
   );
 
-  /// Create a ZExt, Bitcast or Trunc for integer -> integer casts
-  static Constant *
-  getIntegerCast(Constant *C,  ///< The integer constant to be casted
-                 Type *Ty,     ///< The integer type to cast to
-                 bool IsSigned ///< Whether C should be treated as signed or not
-  );
-
   /// Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
   static Constant *getFPCast(Constant *C, ///< The integer constant to be casted
                              Type *Ty     ///< The integer type to cast to
@@ -1333,6 +1307,9 @@ class ConstantExpr : public Constant {
   /// Whether creating a constant expression for this cast is desirable.
   static bool isDesirableCastOp(unsigned Opcode);
 
+  /// Whether creating a constant expression for this cast is supported.
+  static bool isSupportedCastOp(unsigned Opcode);
+
   /// Whether creating a constant expression for this getelementptr type is
   /// supported.
   static bool isSupportedGetElementPtr(const Type *SrcElemTy) {
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 42f306a99d5eefa..d3abd16204a4534 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -3805,8 +3805,6 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
   }
 
   case lltok::kw_trunc:
-  case lltok::kw_zext:
-  case lltok::kw_sext:
   case lltok::kw_fptrunc:
   case lltok::kw_fpext:
   case lltok::kw_bitcast:
@@ -3866,6 +3864,10 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     return error(ID.Loc, "fneg constexprs are no longer supported");
   case lltok::kw_select:
     return error(ID.Loc, "select constexprs are no longer supported");
+  case lltok::kw_zext:
+    return error(ID.Loc, "zext constexprs are no longer supported");
+  case lltok::kw_sext:
+    return error(ID.Loc, "sext constexprs are no longer supported");
   case lltok::kw_icmp:
   case lltok::kw_fcmp: {
     unsigned PredVal, Opc = Lex.getUIntVal();
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 747968b51407cd9..1c92da5a49a7872 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1394,6 +1394,9 @@ static bool isConstExprSupported(const BitcodeConstant *BC) {
   if (Instruction::isBinaryOp(Opcode))
     return ConstantExpr::isSupportedBinOp(Opcode);
 
+  if (Instruction::isCast(Opcode))
+    return ConstantExpr::isSupportedCastOp(Opcode);
+
   if (Opcode == Instruction::GetElementPtr)
     return ConstantExpr::isSupportedGetElementPtr(BC->SrcElemTy);
 
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 15a74859045183b..81691a5f69aa0a6 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -258,40 +258,6 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
     // TODO: Handle the 'partially zero' case.
     return nullptr;
   }
-
-  case Instruction::ZExt: {
-    unsigned SrcBitSize =
-      cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
-
-    // If extracting something that is completely zero, return 0.
-    if (ByteStart*8 >= SrcBitSize)
-      return Constant::getNullValue(IntegerType::get(CE->getContext(),
-                                                     ByteSize*8));
-
-    // If exactly extracting the input, return it.
-    if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
-      return CE->getOperand(0);
-
-    // If extracting something completely in the input, if the input is a
-    // multiple of 8 bits, recurse.
-    if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
-      return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
-
-    // Otherwise, if extracting a subset of the input, which is not multiple of
-    // 8 bits, do a shift and trunc to get the bits.
-    if ((ByteStart+ByteSize)*8 < SrcBitSize) {
-      assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
-      Constant *Res = CE->getOperand(0);
-      if (ByteStart)
-        Res = ConstantExpr::getLShr(Res,
-                                 ConstantInt::get(Res->getType(), ByteStart*8));
-      return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
-                                                          ByteSize*8));
-    }
-
-    // TODO: Handle the 'partially zero' case.
-    return nullptr;
-  }
   }
 }
 
@@ -986,16 +952,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
         return C1;                                            // X & -1 == X
 
       if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
-        // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
-        if (CE1->getOpcode() == Instruction::ZExt) {
-          unsigned DstWidth = CI2->getType()->getBitWidth();
-          unsigned SrcWidth =
-            CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
-          APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
-          if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
-            return C1;
-        }
-
         // If and'ing the address of a global with a constant, fold it.
         if (CE1->getOpcode() == Instruction::PtrToInt &&
             isa<GlobalValue>(CE1->getOperand(0))) {
@@ -1056,12 +1012,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
         }
       }
       break;
-    case Instruction::AShr:
-      // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
-      if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
-        if (CE1->getOpcode() == Instruction::ZExt)  // Top bits known zero.
-          return ConstantExpr::getLShr(C1, C2);
-      break;
     }
   } else if (isa<ConstantInt>(C1)) {
     // If C1 is a ConstantInt and C2 is not, swap the operands.
@@ -1461,8 +1411,6 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
       [[fallthrough]];
     case Instruction::UIToFP:
     case Instruction::SIToFP:
-    case Instruction::ZExt:
-    case Instruction::SExt:
       // We can't evaluate floating point casts or truncations.
       if (CE1Op0->getType()->isFPOrFPVectorTy())
         break;
@@ -1470,8 +1418,6 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
       // If the cast is not actually changing bits, and the second operand is a
       // null pointer, do the comparison with the pre-casted value.
       if (V2->isNullValue() && CE1->getType()->isIntOrPtrTy()) {
-        if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
-        if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
         return evaluateICmpRelation(CE1Op0,
                                     Constant::getNullValue(CE1Op0->getType()),
                                     isSigned);
@@ -1828,24 +1774,6 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
       }
     }
 
-    // If the left hand side is an extension, try eliminating it.
-    if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
-      if ((CE1->getOpcode() == Instruction::SExt &&
-           ICmpInst::isSigned(Predicate)) ||
-          (CE1->getOpcode() == Instruction::ZExt &&
-           !ICmpInst::isSigned(Predicate))) {
-        Constant *CE1Op0 = CE1->getOperand(0);
-        Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
-        if (CE1Inverse == CE1Op0) {
-          // Check whether we can safely truncate the right hand side.
-          Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
-          if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse,
-                                    C2->getType()) == C2)
-            return ConstantExpr::getICmp(Predicate, CE1Inverse, C2Inverse);
-        }
-      }
-    }
-
     if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
         (C1->isNullValue() && !C2->isNullValue())) {
       // If C2 is a constant expr and C1 isn't, flip them around and fold the
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 58cbde1bfb530df..cca481181068d7e 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -1958,6 +1958,8 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
                                 bool OnlyIfReduced) {
   Instruction::CastOps opc = Instruction::CastOps(oc);
   assert(Instruction::isCast(opc) && "opcode out of range");
+  assert(isSupportedCastOp(opc) &&
+         "Cast opcode not supported as constant expression");
   assert(C && Ty && "Null arguments to getCast");
   assert(CastInst::castIsValid(opc, C, Ty) && "Invalid constantexpr cast!");
 
@@ -1966,10 +1968,6 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, Type *Ty,
     llvm_unreachable("Invalid cast opcode");
   ca...
[truncated]

``````````

</details>


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


More information about the llvm-commits mailing list