Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IR] Remove support for lshr/ashr constant expressions #71955

Merged
merged 1 commit into from Nov 14, 2023

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Nov 10, 2023

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

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

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

This is part of
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 10, 2023

@llvm/pr-subscribers-llvm-transforms
@llvm/pr-subscribers-backend-aarch64
@llvm/pr-subscribers-llvm-ir
@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-backend-x86

Author: Nikita Popov (nikic)

Changes

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

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


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

23 Files Affected:

  • (modified) llvm/bindings/ocaml/llvm/llvm.ml (-2)
  • (modified) llvm/bindings/ocaml/llvm/llvm.mli (-10)
  • (modified) llvm/bindings/ocaml/llvm/llvm_ocaml.c (-12)
  • (modified) llvm/docs/LangRef.rst (-4)
  • (modified) llvm/docs/ReleaseNotes.rst (+4)
  • (modified) llvm/include/llvm-c/Core.h (-2)
  • (modified) llvm/include/llvm/IR/Constants.h (-10)
  • (modified) llvm/lib/AsmParser/LLParser.cpp (+4-8)
  • (modified) llvm/lib/IR/ConstantFold.cpp (-23)
  • (modified) llvm/lib/IR/Constants.cpp (+2-12)
  • (modified) llvm/lib/IR/Core.cpp (-10)
  • (modified) llvm/test/Assembler/2003-05-21-MalformedShiftCrash.ll (+1-1)
  • (removed) llvm/test/Assembler/2003-11-05-ConstantExprShift.ll (-6)
  • (modified) llvm/test/Assembler/flags.ll (-10)
  • (modified) llvm/test/Assembler/vector-shift.ll (-20)
  • (modified) llvm/test/CodeGen/AArch64/fold-global-offsets.ll (+5-1)
  • (modified) llvm/test/CodeGen/X86/2009-01-18-ConstantExprCrash.ll (+4-1)
  • (modified) llvm/test/CodeGen/X86/pre-coalesce-2.ll (+8-2)
  • (modified) llvm/test/DebugInfo/AArch64/asan-stack-vars.mir (+6-3)
  • (modified) llvm/test/Transforms/InstCombine/and-or.ll (+5-2)
  • (modified) llvm/test/Transforms/InstSimplify/2011-10-27-BinOpCrash.ll (+15-1)
  • (removed) llvm/test/Transforms/InstSimplify/ConstProp/2009-06-20-constexpr-zero-lhs.ll (-7)
  • (modified) llvm/unittests/IR/ConstantsTest.cpp (-14)
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml
index 581d832430074d6..057798fc0cea2d0 100644
--- a/llvm/bindings/ocaml/llvm/llvm.ml
+++ b/llvm/bindings/ocaml/llvm/llvm.ml
@@ -654,8 +654,6 @@ external const_icmp : Icmp.t -> llvalue -> llvalue -> llvalue
 external const_fcmp : Fcmp.t -> llvalue -> llvalue -> llvalue
                     = "llvm_const_fcmp"
 external const_shl : llvalue -> llvalue -> llvalue = "llvm_const_shl"
-external const_lshr : llvalue -> llvalue -> llvalue = "llvm_const_lshr"
-external const_ashr : llvalue -> llvalue -> llvalue = "llvm_const_ashr"
 external const_gep : lltype -> llvalue -> llvalue array -> llvalue
                    = "llvm_const_gep"
 external const_in_bounds_gep : lltype -> llvalue -> llvalue array -> llvalue
diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli
index f9aab89e4bddfbd..e0febb79a2b61d8 100644
--- a/llvm/bindings/ocaml/llvm/llvm.mli
+++ b/llvm/bindings/ocaml/llvm/llvm.mli
@@ -1145,16 +1145,6 @@ val const_fcmp : Fcmp.t -> llvalue -> llvalue -> llvalue
     See the method [llvm::ConstantExpr::getShl]. *)
 val const_shl : llvalue -> llvalue -> llvalue
 
-(** [const_lshr c1 c2] returns the constant integer [c1] right-shifted by the
-    constant integer [c2] with zero extension.
-    See the method [llvm::ConstantExpr::getLShr]. *)
-val const_lshr : llvalue -> llvalue -> llvalue
-
-(** [const_ashr c1 c2] returns the constant integer [c1] right-shifted by the
-    constant integer [c2] with sign extension.
-    See the method [llvm::ConstantExpr::getAShr]. *)
-val const_ashr : llvalue -> llvalue -> llvalue
-
 (** [const_gep srcty pc indices] returns the constant [getElementPtr] of [pc]
     with source element type [srcty] and the constant integers indices from the
     array [indices].
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index 4c4c49027206c05..d74d8030cea0dea 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -1233,18 +1233,6 @@ value llvm_const_shl(value LHS, value RHS) {
   return to_val(Value);
 }
 
-/* llvalue -> llvalue -> llvalue */
-value llvm_const_lshr(value LHS, value RHS) {
-  LLVMValueRef Value = LLVMConstLShr(Value_val(LHS), Value_val(RHS));
-  return to_val(Value);
-}
-
-/* llvalue -> llvalue -> llvalue */
-value llvm_const_ashr(value LHS, value RHS) {
-  LLVMValueRef Value = LLVMConstAShr(Value_val(LHS), Value_val(RHS));
-  return to_val(Value);
-}
-
 /* lltype -> llvalue -> llvalue array -> llvalue */
 value llvm_const_gep(value Ty, value ConstantVal, value Indices) {
   mlsize_t Length = Wosize_val(Indices);
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index ff4f769dcc0dbac..bc1eab1e0b7a07f 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -4693,10 +4693,6 @@ The following is the syntax for constant expressions:
     Perform a multiplication on constants.
 ``shl (LHS, RHS)``
     Perform a left shift on constants.
-``lshr (LHS, RHS)``
-    Perform a logical right shift on constants.
-``ashr (LHS, RHS)``
-    Perform an arithmetic right shift on constants.
 ``xor (LHS, RHS)``
     Perform a bitwise xor on constants.
 
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index d39bd75a9bd47d0..e27a81316c2ce1d 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -57,6 +57,8 @@ Changes to the LLVM IR
 
   * ``and``
   * ``or``
+  * ``lshr``
+  * ``ashr``
   * ``zext``
   * ``sext``
   * ``fptrunc``
@@ -174,6 +176,8 @@ Changes to the C API
 
   * ``LLVMConstAnd``
   * ``LLVMConstOr``
+  * ``LLVMConstLShr``
+  * ``LLVMConstAShr``
   * ``LLVMConstZExt``
   * ``LLVMConstSExt``
   * ``LLVMConstZExtOrBitCast``
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 41e148e89fbd2c9..b752fd42a7a12cf 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2281,8 +2281,6 @@ LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
-LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
-LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
 LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
                            LLVMValueRef *ConstantIndices, unsigned NumIndices);
 LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index cf3812a79f028fb..c443ec58822b007 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -1038,8 +1038,6 @@ class ConstantExpr : public Constant {
   static Constant *getXor(Constant *C1, Constant *C2);
   static Constant *getShl(Constant *C1, Constant *C2, bool HasNUW = false,
                           bool HasNSW = false);
-  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 *getPtrToInt(Constant *C, Type *Ty,
                                bool OnlyIfReduced = false);
@@ -1085,14 +1083,6 @@ class ConstantExpr : public Constant {
     return getShl(C1, C2, true, false);
   }
 
-  static Constant *getExactAShr(Constant *C1, Constant *C2) {
-    return getAShr(C1, C2, true);
-  }
-
-  static Constant *getExactLShr(Constant *C1, Constant *C2) {
-    return getLShr(C1, C2, true);
-  }
-
   /// If C is a scalar/fixed width vector of known powers of 2, then this
   /// function returns a new scalar/fixed width vector obtained from logBase2
   /// of C. Undef vector elements are set to zero.
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 9940bfb15d1979e..f9df70fb6fc0996 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -3852,6 +3852,10 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     return error(ID.Loc, "and constexprs are no longer supported");
   case lltok::kw_or:
     return error(ID.Loc, "or constexprs are no longer supported");
+  case lltok::kw_lshr:
+    return error(ID.Loc, "lshr constexprs are no longer supported");
+  case lltok::kw_ashr:
+    return error(ID.Loc, "ashr constexprs are no longer supported");
   case lltok::kw_fneg:
     return error(ID.Loc, "fneg constexprs are no longer supported");
   case lltok::kw_select:
@@ -3910,12 +3914,9 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
   case lltok::kw_sub:
   case lltok::kw_mul:
   case lltok::kw_shl:
-  case lltok::kw_lshr:
-  case lltok::kw_ashr:
   case lltok::kw_xor: {
     bool NUW = false;
     bool NSW = false;
-    bool Exact = false;
     unsigned Opc = Lex.getUIntVal();
     Constant *Val0, *Val1;
     Lex.Lex();
@@ -3928,10 +3929,6 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
         if (EatIfPresent(lltok::kw_nuw))
           NUW = true;
       }
-    } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
-               Opc == Instruction::LShr || Opc == Instruction::AShr) {
-      if (EatIfPresent(lltok::kw_exact))
-        Exact = true;
     }
     if (parseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
         parseGlobalTypeAndValue(Val0) ||
@@ -3948,7 +3945,6 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     unsigned Flags = 0;
     if (NUW)   Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
     if (NSW)   Flags |= OverflowingBinaryOperator::NoSignedWrap;
-    if (Exact) Flags |= PossiblyExactOperator::IsExact;
     ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1, Flags);
     ID.Kind = ValID::t_Constant;
     return false;
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 0358f996f285165..25f7ac22e3ab687 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -155,29 +155,6 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
 
   switch (CE->getOpcode()) {
   default: return nullptr;
-  case Instruction::LShr: {
-    ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
-    if (!Amt)
-      return nullptr;
-    APInt ShAmt = Amt->getValue();
-    // Cannot analyze non-byte shifts.
-    if ((ShAmt & 7) != 0)
-      return nullptr;
-    ShAmt.lshrInPlace(3);
-
-    // If the extract is known to be all zeros, return zero.
-    if (ShAmt.uge(CSize - ByteStart))
-      return Constant::getNullValue(
-          IntegerType::get(CE->getContext(), ByteSize * 8));
-    // If the extract is known to be fully in the input, extract it.
-    if (ShAmt.ule(CSize - (ByteStart + ByteSize)))
-      return ExtractConstantBytes(CE->getOperand(0),
-                                  ByteStart + ShAmt.getZExtValue(), ByteSize);
-
-    // TODO: Handle the 'partially zero' case.
-    return nullptr;
-  }
-
   case Instruction::Shl: {
     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
     if (!Amt)
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index aab624bf8dec5f3..bc55d5b48527124 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -2160,13 +2160,13 @@ bool ConstantExpr::isSupportedBinOp(unsigned Opcode) {
   case Instruction::FRem:
   case Instruction::And:
   case Instruction::Or:
+  case Instruction::LShr:
+  case Instruction::AShr:
     return false;
   case Instruction::Add:
   case Instruction::Sub:
   case Instruction::Mul:
   case Instruction::Shl:
-  case Instruction::LShr:
-  case Instruction::AShr:
   case Instruction::Xor:
     return true;
   default:
@@ -2482,16 +2482,6 @@ Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
   return get(Instruction::Shl, C1, C2, Flags);
 }
 
-Constant *ConstantExpr::getLShr(Constant *C1, Constant *C2, bool isExact) {
-  return get(Instruction::LShr, C1, C2,
-             isExact ? PossiblyExactOperator::IsExact : 0);
-}
-
-Constant *ConstantExpr::getAShr(Constant *C1, Constant *C2, bool isExact) {
-  return get(Instruction::AShr, C1, C2,
-             isExact ? PossiblyExactOperator::IsExact : 0);
-}
-
 Constant *ConstantExpr::getExactLogBase2(Constant *C) {
   Type *Ty = C->getType();
   const APInt *IVal;
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 004b0e8577a7e52..f838b0501a88978 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -1712,16 +1712,6 @@ LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
                                    unwrap<Constant>(RHSConstant)));
 }
 
-LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
-  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
-                                    unwrap<Constant>(RHSConstant)));
-}
-
-LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
-  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
-                                    unwrap<Constant>(RHSConstant)));
-}
-
 LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
                            LLVMValueRef *ConstantIndices, unsigned NumIndices) {
   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
diff --git a/llvm/test/Assembler/2003-05-21-MalformedShiftCrash.ll b/llvm/test/Assembler/2003-05-21-MalformedShiftCrash.ll
index 0ec3c8b1045cbcc..a0dda4553c9aa05 100644
--- a/llvm/test/Assembler/2003-05-21-MalformedShiftCrash.ll
+++ b/llvm/test/Assembler/2003-05-21-MalformedShiftCrash.ll
@@ -2,4 +2,4 @@
 ; RUN: not llvm-as < %s > /dev/null 2> %t
 ; RUN: grep "constexpr requires integer or integer vector operands" %t
 
-@0 = global i32 ashr (float 1.0, float 2.0)
+@0 = global i32 shl (float 1.0, float 2.0)
diff --git a/llvm/test/Assembler/2003-11-05-ConstantExprShift.ll b/llvm/test/Assembler/2003-11-05-ConstantExprShift.ll
deleted file mode 100644
index 6aa58232b314f2f..000000000000000
--- a/llvm/test/Assembler/2003-11-05-ConstantExprShift.ll
+++ /dev/null
@@ -1,6 +0,0 @@
-; RUN: llvm-as < %s | llvm-dis
-; RUN: verify-uselistorder %s
-
-define i32 @test() {
-        ret i32 ashr (i32 ptrtoint (ptr @test to i32), i32 2)
-}
diff --git a/llvm/test/Assembler/flags.ll b/llvm/test/Assembler/flags.ll
index 8331edf52a1699d..6ab5e1bfb9c4f46 100644
--- a/llvm/test/Assembler/flags.ll
+++ b/llvm/test/Assembler/flags.ll
@@ -174,16 +174,6 @@ define i64 @mul_both_ce() {
 	ret i64 mul nuw nsw (i64 ptrtoint (ptr @addr to i64), i64 91)
 }
 
-define i64 @ashr_exact_ce() {
-; CHECK: ret i64 ashr exact (i64 ptrtoint (ptr @addr to i64), i64 9)
-	ret i64 ashr exact (i64 ptrtoint (ptr @addr to i64), i64 9)
-}
-
-define i64 @lshr_exact_ce() {
-; CHECK: ret i64 lshr exact (i64 ptrtoint (ptr @addr to i64), i64 9)
-	ret i64 lshr exact (i64 ptrtoint (ptr @addr to i64), i64 9)
-}
-
 define ptr @gep_nw_ce() {
 ; CHECK: ret ptr getelementptr inbounds (i64, ptr @addr, i64 171)
         ret ptr getelementptr inbounds (i64, ptr @addr, i64 171)
diff --git a/llvm/test/Assembler/vector-shift.ll b/llvm/test/Assembler/vector-shift.ll
index d4351a87ce9433a..ba8708bac8cf0d3 100644
--- a/llvm/test/Assembler/vector-shift.ll
+++ b/llvm/test/Assembler/vector-shift.ll
@@ -24,23 +24,3 @@ entry:
   %cmp = ashr <4 x i32> %a, %b		; <4 x i32> [#uses=1]
   ret <4 x i32> %cmp
 }
-
-; Constant expressions: these should be folded.
-
-; CHECK: @foo_ce
-; CHECK: ret <2 x i64> <i64 40, i64 192>
-define <2 x i64> @foo_ce() nounwind {
-  ret <2 x i64> shl (<2 x i64> <i64 5, i64 6>, <2 x i64> <i64 3, i64 5>)
-}
-
-; CHECK: @bar_ce
-; CHECK: ret <2 x i64> <i64 42, i64 11>
-define <2 x i64> @bar_ce() nounwind {
-  ret <2 x i64> lshr (<2 x i64> <i64 340, i64 380>, <2 x i64> <i64 3, i64 5>)
-}
-
-; CHECK: baz_ce
-; CHECK: ret <2 x i64> <i64 71, i64 12>
-define <2 x i64> @baz_ce() nounwind {
-  ret <2 x i64> ashr (<2 x i64> <i64 573, i64 411>, <2 x i64> <i64 3, i64 5>)
-}
diff --git a/llvm/test/CodeGen/AArch64/fold-global-offsets.ll b/llvm/test/CodeGen/AArch64/fold-global-offsets.ll
index 34b13faad69b74c..897d35a78495b11 100644
--- a/llvm/test/CodeGen/AArch64/fold-global-offsets.ll
+++ b/llvm/test/CodeGen/AArch64/fold-global-offsets.ll
@@ -131,6 +131,10 @@ define i32 @f7() {
 ; GISEL-NEXT:    ret
 
 entry:
-  %l = load i32, ptr getelementptr (i32, ptr inttoptr (i64 trunc (i128 lshr (i128 bitcast (<2 x i64> <i64 undef, i64 ptrtoint (ptr getelementptr inbounds ({ [9 x ptr], [8 x ptr] }, ptr @x3, i64 0, inrange i32 1, i64 2) to i64)> to i128), i128 64) to i64) to ptr), i64 5)
+  %lshr = lshr i128 bitcast (<2 x i64> <i64 undef, i64 ptrtoint (ptr getelementptr inbounds ({ [9 x ptr], [8 x ptr] }, ptr @x3, i64 0, inrange i32 1, i64 2) to i64)> to i128), 64
+  %trunc = trunc i128 %lshr to i64
+  %inttoptr = inttoptr i64 %trunc to ptr
+  %gep = getelementptr i32, ptr %inttoptr, i64 5
+  %l = load i32, ptr %gep
   ret i32 %l
 }
diff --git a/llvm/test/CodeGen/X86/2009-01-18-ConstantExprCrash.ll b/llvm/test/CodeGen/X86/2009-01-18-ConstantExprCrash.ll
index ad4458c0621c7af..c5541d65c6c3a06 100644
--- a/llvm/test/CodeGen/X86/2009-01-18-ConstantExprCrash.ll
+++ b/llvm/test/CodeGen/X86/2009-01-18-ConstantExprCrash.ll
@@ -25,7 +25,10 @@ bb4.i.i70:		; preds = %bb4.i.i70, %bb.i51
 	br i1 false, label %_ZN11xercesc_2_59XMLString9stringLenEPKt.exit.i73, label %bb4.i.i70
 
 _ZN11xercesc_2_59XMLString9stringLenEPKt.exit.i73:		; preds = %bb4.i.i70
-	%0 = load i16, ptr getelementptr ([7 x i16], ptr @_ZN11xercesc_2_5L17gIdeographicCharsE, i32 0, i32 add (i32 ashr (i32 sub (i32 ptrtoint (ptr getelementptr ([7 x i16], ptr @_ZN11xercesc_2_5L17gIdeographicCharsE, i32 0, i32 4) to i32), i32 ptrtoint (ptr @_ZN11xercesc_2_5L17gIdeographicCharsE to i32)), i32 1), i32 1)), align 4		; <i16> [#uses=0]
+	%ashr = ashr i32 sub (i32 ptrtoint (ptr getelementptr ([7 x i16], ptr @_ZN11xercesc_2_5L17gIdeographicCharsE, i32 0, i32 4) to i32), i32 ptrtoint (ptr @_ZN11xercesc_2_5L17gIdeographicCharsE to i32)), 1
+	%add = add i32 %ashr, 1
+	%gep = getelementptr [7 x i16], ptr @_ZN11xercesc_2_5L17gIdeographicCharsE, i32 0, i32 %add
+	%0 = load i16, ptr %gep, align 4
 	br label %bb4.i5.i141
 
 bb4.i5.i141:		; preds = %bb4.i5.i141, %_ZN11xercesc_2_59XMLString9stringLenEPKt.exit.i73
diff --git a/llvm/test/CodeGen/X86/pre-coalesce-2.ll b/llvm/test/CodeGen/X86/pre-coalesce-2.ll
index 67d57408c9c2d58..55e9fb6ee184b32 100644
--- a/llvm/test/CodeGen/X86/pre-coalesce-2.ll
+++ b/llvm/test/CodeGen/X86/pre-coalesce-2.ll
@@ -86,7 +86,10 @@ if.end5:                                          ; preds = %while.body
   br i1 %cmp6, label %if.end14, label %cleanup.thread40
 
 if.end14:                                         ; preds = %if.end5
-  %22 = load i8, ptr inttoptr (i64 add (i64 lshr (i64 ptrtoint (ptr @a to i64), i64 3), i64 2147450880) to ptr)
+  %lshr1 = lshr i64 ptrtoint (ptr @a to i64), 3
+  %add1 = add i64 %lshr1, 2147450880
+  %inttoptr1 = inttoptr i64 %add1 to ptr
+  %22 = load i8, ptr %inttoptr1
   %23 = icmp ne i8 %22, 0
   br i1 %23, label %24, label %25
 
@@ -177,7 +180,10 @@ cleanup.thread40:                                 ; preds = %if.end5
   br label %enoent
 
 cleanup:                                          ; preds = %while.body
-  %61 = load i8, ptr inttoptr (i64 add (i64 lshr (i64 ptrtoint (ptr @b to i64), i64 3), i64 2147450880) to ptr)
+  %lshr2 = lshr i64 ptrtoint (ptr @b to i64), 3
+  %add2 = add i64 %lshr2, 2147450880
+  %inttoptr2 = inttoptr i64 %add2 to ptr
+  %61 = load i8, ptr %inttoptr2
   %62 = icmp ne i8 %61, 0
   br i1 %62, label %63, label %66
 
diff --git a/llvm/test/DebugInfo/AArch64/asan-stack-vars.mir b/llvm/test/DebugInfo/AArch64/asan-stack-vars.mir
index 233ec00679aae13..1b7159d5f075259 100644
--- a/llvm/test/DebugInfo/AArch64/asan-stack-vars.mir
+++ b/llvm/test/DebugInfo/AArch64/asan-stack-vars.mir
@@ -133,7 +133,8 @@
     store i8* %_cmd, i8** %_cmd.addr, align 8
     call void @llvm.dbg.declare(metadata i8** %_cmd.addr, metadata !48, metadata !DIExpression()), !dbg !47
     %29 = load %struct._class_t*, %struct._class_t** @"OBJC_CLASSLIST_REFERENCES_$_", align 8, !dbg !50
-    %30 = add i64 lshr (i64 ptrtoint (i8** @OBJC_SELECTOR_REFERENCES_ to i64), i64 3), %1, !dbg !50
+    %lshr1 = lshr i64 ptrtoint (i8** @OBJC_SELECTOR_REFERENCES_ to i64), 3, !dbg !50
+    %30 = add i64 %lshr1, %1, !dbg !50
     %31 = inttoptr i64 %30 to i8*, !dbg !50
     %32 = load i8, i8* %31, !dbg !50
     %33 = icmp ne i8 %32, 0, !dbg !50
@@ -149,7 +150,8 @@
     %37 = bitcast %struct._class_t* %29 to i8*, !dbg !50
     %call = call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8* (i8*, i8*)*)(i8* %37, i8* %36), !dbg !50
     %38 = bitcast i8* %call to %0*, !dbg !50
-    %39 = add i64 lshr (i64 ptrtoint (i8** @OBJC_SELECTOR_REFERENCES_.2 to i64), i64 3), %1, !dbg !51
+    %lshr2 = lshr i64 ptrtoint (i8** @OBJC_SELECTOR_REFERENCES_.2 to i64), 3, !dbg !51
+    %39 = add i64 %lshr2, %1, !dbg !51
     %40 = inttoptr i64 %39 to i8*, !dbg !51
     %41 = load i8, i8* %40, !dbg !51
     %42 = icmp ne i8 %41, 0, !dbg !51
@@ -197,7 +199,8 @@
   ; <label>:65:                                     ; preds = %55
     store %0* %57, %0** %6, align 8, !dbg !44
     %66 = load %0*, %0** %6, align 8, !dbg !52
-    %67 = add i64 lshr (i64 ptrtoint (i8** @OBJC_SELECTOR_REFERENCES_.4 to i64), i64 3), %1, !dbg !53
+    %lshr3 = lshr i64 ptrtoint (i8** @OBJC_SELECTOR_REFERENCES_.4 to i64), 3, !dbg !53
+    %67 = add i64 %lshr3, %1, !dbg !53
     %68 = inttoptr i64 %67 to i8*, !dbg !53
     %69 = load i8, i8* %68, !dbg !53
     %70 = icmp ne i8 %69, 0, !dbg !53
diff --git a/llvm/test/Transforms/InstCombine/and-or.ll b/llvm/test/Transforms/InstCombine/and-or.ll
index e919d6b6475866f..631da498e6644ef 100644
--- a/llvm/test/Transforms/InstCombine/and-or.ll
+++ b/llvm/test/Transforms/InstCombine/and-or.ll
@@ -322,10 +322,13 @@ define <2 x i8> @and_or_hoist_mask_commute_vec_splat(<2 x i8> %a, <2 x i8> %b) {
 
 define i32 @pr64114_and_xor_hoist_mask_constexpr() {
 ; CHECK-LABEL: @pr64114_and_xor_hoist_mask_constexpr(
-; CHECK-NEXT:    [[AND:%.*]] = and i32 xor (i32 lshr (i32 ptrtoint (ptr @g to i32), i32 8), i32 ptrtoint (ptr @g to i32)), 1
+; CH...
[truncated]

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff cf1e3420b0141d84770869205cfdb546609014dd 6e900400baaf34bd8984c0598a6ae72876dc1194 -- llvm/bindings/ocaml/llvm/llvm_ocaml.c llvm/include/llvm-c/Core.h llvm/include/llvm/IR/Constants.h llvm/lib/AsmParser/LLParser.cpp llvm/lib/IR/ConstantFold.cpp llvm/lib/IR/Constants.cpp llvm/lib/IR/Core.cpp llvm/unittests/IR/ConstantsTest.cpp
View the diff from clang-format here.
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index f9df70fb6fc0..d565918f24ea 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -3944,7 +3944,8 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
                    "constexpr requires integer or integer vector operands");
     unsigned Flags = 0;
     if (NUW)   Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
-    if (NSW)   Flags |= OverflowingBinaryOperator::NoSignedWrap;
+    if (NSW)
+      Flags |= OverflowingBinaryOperator::NoSignedWrap;
     ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1, Flags);
     ID.Kind = ValID::t_Constant;
     return false;
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 25f7ac22e3ab..4e28e9af24af 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -154,7 +154,8 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
   if (!CE) return nullptr;
 
   switch (CE->getOpcode()) {
-  default: return nullptr;
+  default:
+    return nullptr;
   case Instruction::Shl: {
     ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
     if (!Amt)

Copy link
Collaborator

@nhaehnle nhaehnle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM

@@ -3948,7 +3945,6 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
unsigned Flags = 0;
if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
if (Exact) Flags |= PossiblyExactOperator::IsExact;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated musing: I've been wondering for a long time if we couldn't turn or and and into possibly exact operators with the meaning of exact being "the set / unset bits of the operands are disjoint". The idea being that this might help sometimes with issues caused by LLVM eagerly replacing add with or in address expressions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 I've wondered something similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good addition to me. At least for or, the utility is pretty clear. For and, I'm not sure where we could make use of this property.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't have a concrete use for and. The motivation is simply to close over deMorgan's laws.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to work on a patch for this. Do we want to use 'exact' or have a new keyword like 'disjoint'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would lean towards a new flag. The usage will be quite different from the existing exact flag.

@nikic nikic merged commit 56c1d30 into llvm:main Nov 14, 2023
8 of 9 checks passed
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
Remove support for the lshr and ashr constant expressions. All places
creating them have been removed beforehand, so this just removes the
APIs and uses of these constant expressions in tests.

This is part of
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
topperc added a commit that referenced this pull request Nov 24, 2023
This flag indicates that every bit is known to be zero in at least one
of the inputs. This allows the Or to be treated as an Add since there is
no possibility of a carry from any bit.

If the flag is present and this property does not hold, the result is
poison.

This makes it easier to reverse the InstCombine transform that turns Add
into Or.

This is inspired by a comment here
#71955 (comment)

Discourse thread
https://discourse.llvm.org/t/rfc-add-or-disjoint-flag/75036
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants