Skip to content

Commit 76d3ab2

Browse files
authored
[IR] Remove support for shl constant expressions (#96037)
Remove support for shl constant expressions, as part of: https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179
1 parent 930dd3f commit 76d3ab2

File tree

18 files changed

+25
-141
lines changed

18 files changed

+25
-141
lines changed

llvm/bindings/ocaml/llvm/llvm.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@ external const_mul : llvalue -> llvalue -> llvalue = "llvm_const_mul"
651651
external const_nsw_mul : llvalue -> llvalue -> llvalue = "llvm_const_nsw_mul"
652652
external const_nuw_mul : llvalue -> llvalue -> llvalue = "llvm_const_nuw_mul"
653653
external const_xor : llvalue -> llvalue -> llvalue = "llvm_const_xor"
654-
external const_shl : llvalue -> llvalue -> llvalue = "llvm_const_shl"
655654
external const_gep : lltype -> llvalue -> llvalue array -> llvalue
656655
= "llvm_const_gep"
657656
external const_in_bounds_gep : lltype -> llvalue -> llvalue array -> llvalue

llvm/bindings/ocaml/llvm/llvm.mli

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,11 +1136,6 @@ val const_nuw_mul : llvalue -> llvalue -> llvalue
11361136
See the method [llvm::ConstantExpr::getXor]. *)
11371137
val const_xor : llvalue -> llvalue -> llvalue
11381138

1139-
(** [const_shl c1 c2] returns the constant integer [c1] left-shifted by the
1140-
constant integer [c2].
1141-
See the method [llvm::ConstantExpr::getShl]. *)
1142-
val const_shl : llvalue -> llvalue -> llvalue
1143-
11441139
(** [const_gep srcty pc indices] returns the constant [getElementPtr] of [pc]
11451140
with source element type [srcty] and the constant integers indices from the
11461141
array [indices].

llvm/bindings/ocaml/llvm/llvm_ocaml.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,12 +1224,6 @@ value llvm_const_xor(value LHS, value RHS) {
12241224
return to_val(Value);
12251225
}
12261226

1227-
/* llvalue -> llvalue -> llvalue */
1228-
value llvm_const_shl(value LHS, value RHS) {
1229-
LLVMValueRef Value = LLVMConstShl(Value_val(LHS), Value_val(RHS));
1230-
return to_val(Value);
1231-
}
1232-
12331227
/* lltype -> llvalue -> llvalue array -> llvalue */
12341228
value llvm_const_gep(value Ty, value ConstantVal, value Indices) {
12351229
mlsize_t Length = Wosize_val(Indices);

llvm/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Changes to the LLVM IR
6666

6767
* ``icmp``
6868
* ``fcmp``
69+
* ``shl``
6970
* LLVM has switched from using debug intrinsics in textual IR to using debug
7071
records by default. Details of the change and instructions on how to update
7172
any downstream tools and tests can be found in the `migration docs
@@ -227,6 +228,7 @@ Changes to the C API
227228

228229
* ``LLVMConstICmp``
229230
* ``LLVMConstFCmp``
231+
* ``LLVMConstShl``
230232

231233
**Note:** The following changes are due to the removal of the debug info
232234
intrinsics from LLVM and to the introduction of debug records into LLVM.

llvm/include/llvm-c/Core.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,6 @@ LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
23542354
LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
23552355
LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
23562356
LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
2357-
LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
23582357
LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
23592358
LLVMValueRef *ConstantIndices, unsigned NumIndices);
23602359
LLVMValueRef LLVMConstInBoundsGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,

llvm/include/llvm/IR/Constants.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,6 @@ class ConstantExpr : public Constant {
11221122
static Constant *getMul(Constant *C1, Constant *C2, bool HasNUW = false,
11231123
bool HasNSW = false);
11241124
static Constant *getXor(Constant *C1, Constant *C2);
1125-
static Constant *getShl(Constant *C1, Constant *C2, bool HasNUW = false,
1126-
bool HasNSW = false);
11271125
static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
11281126
static Constant *getPtrToInt(Constant *C, Type *Ty,
11291127
bool OnlyIfReduced = false);
@@ -1160,14 +1158,6 @@ class ConstantExpr : public Constant {
11601158
return getMul(C1, C2, true, false);
11611159
}
11621160

1163-
static Constant *getNSWShl(Constant *C1, Constant *C2) {
1164-
return getShl(C1, C2, false, true);
1165-
}
1166-
1167-
static Constant *getNUWShl(Constant *C1, Constant *C2) {
1168-
return getShl(C1, C2, true, false);
1169-
}
1170-
11711161
/// If C is a scalar/fixed width vector of known powers of 2, then this
11721162
/// function returns a new scalar/fixed width vector obtained from logBase2
11731163
/// of C. Undef vector elements are set to zero.

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,6 +4157,8 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
41574157
return error(ID.Loc, "lshr constexprs are no longer supported");
41584158
case lltok::kw_ashr:
41594159
return error(ID.Loc, "ashr constexprs are no longer supported");
4160+
case lltok::kw_shl:
4161+
return error(ID.Loc, "shl constexprs are no longer supported");
41604162
case lltok::kw_fneg:
41614163
return error(ID.Loc, "fneg constexprs are no longer supported");
41624164
case lltok::kw_select:
@@ -4186,15 +4188,14 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
41864188
case lltok::kw_add:
41874189
case lltok::kw_sub:
41884190
case lltok::kw_mul:
4189-
case lltok::kw_shl:
41904191
case lltok::kw_xor: {
41914192
bool NUW = false;
41924193
bool NSW = false;
41934194
unsigned Opc = Lex.getUIntVal();
41944195
Constant *Val0, *Val1;
41954196
Lex.Lex();
41964197
if (Opc == Instruction::Add || Opc == Instruction::Sub ||
4197-
Opc == Instruction::Mul || Opc == Instruction::Shl) {
4198+
Opc == Instruction::Mul) {
41984199
if (EatIfPresent(lltok::kw_nuw))
41994200
NUW = true;
42004201
if (EatIfPresent(lltok::kw_nsw)) {

llvm/lib/IR/ConstantFold.cpp

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -120,66 +120,6 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
120120
return nullptr;
121121
}
122122

123-
124-
/// V is an integer constant which only has a subset of its bytes used.
125-
/// The bytes used are indicated by ByteStart (which is the first byte used,
126-
/// counting from the least significant byte) and ByteSize, which is the number
127-
/// of bytes used.
128-
///
129-
/// This function analyzes the specified constant to see if the specified byte
130-
/// range can be returned as a simplified constant. If so, the constant is
131-
/// returned, otherwise null is returned.
132-
static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
133-
unsigned ByteSize) {
134-
assert(C->getType()->isIntegerTy() &&
135-
(cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
136-
"Non-byte sized integer input");
137-
[[maybe_unused]] unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
138-
assert(ByteSize && "Must be accessing some piece");
139-
assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
140-
assert(ByteSize != CSize && "Should not extract everything");
141-
142-
// Constant Integers are simple.
143-
if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
144-
APInt V = CI->getValue();
145-
if (ByteStart)
146-
V.lshrInPlace(ByteStart*8);
147-
V = V.trunc(ByteSize*8);
148-
return ConstantInt::get(CI->getContext(), V);
149-
}
150-
151-
// In the input is a constant expr, we might be able to recursively simplify.
152-
// If not, we definitely can't do anything.
153-
ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
154-
if (!CE) return nullptr;
155-
156-
switch (CE->getOpcode()) {
157-
default: return nullptr;
158-
case Instruction::Shl: {
159-
ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
160-
if (!Amt)
161-
return nullptr;
162-
APInt ShAmt = Amt->getValue();
163-
// Cannot analyze non-byte shifts.
164-
if ((ShAmt & 7) != 0)
165-
return nullptr;
166-
ShAmt.lshrInPlace(3);
167-
168-
// If the extract is known to be all zeros, return zero.
169-
if (ShAmt.uge(ByteStart + ByteSize))
170-
return Constant::getNullValue(
171-
IntegerType::get(CE->getContext(), ByteSize * 8));
172-
// If the extract is known to be fully in the input, extract it.
173-
if (ShAmt.ule(ByteStart))
174-
return ExtractConstantBytes(CE->getOperand(0),
175-
ByteStart - ShAmt.getZExtValue(), ByteSize);
176-
177-
// TODO: Handle the 'partially zero' case.
178-
return nullptr;
179-
}
180-
}
181-
}
182-
183123
static Constant *foldMaybeUndesirableCast(unsigned opc, Constant *V,
184124
Type *DestTy) {
185125
return ConstantExpr::isDesirableCastOp(opc)
@@ -313,14 +253,6 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
313253
CI->getValue().trunc(DestBitWidth));
314254
}
315255

316-
// The input must be a constantexpr. See if we can simplify this based on
317-
// the bytes we are demanding. Only do this if the source and dest are an
318-
// even multiple of a byte.
319-
if ((DestBitWidth & 7) == 0 &&
320-
(cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
321-
if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
322-
return Res;
323-
324256
return nullptr;
325257
}
326258
case Instruction::BitCast:

llvm/lib/IR/Constants.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,12 +2285,6 @@ Constant *ConstantExpr::get(unsigned Opcode, Constant *C1, Constant *C2,
22852285
assert(C1->getType()->isIntOrIntVectorTy() &&
22862286
"Tried to create a logical operation on a non-integral type!");
22872287
break;
2288-
case Instruction::Shl:
2289-
case Instruction::LShr:
2290-
case Instruction::AShr:
2291-
assert(C1->getType()->isIntOrIntVectorTy() &&
2292-
"Tried to create a shift operation on a non-integer type!");
2293-
break;
22942288
default:
22952289
break;
22962290
}
@@ -2351,11 +2345,11 @@ bool ConstantExpr::isSupportedBinOp(unsigned Opcode) {
23512345
case Instruction::Or:
23522346
case Instruction::LShr:
23532347
case Instruction::AShr:
2348+
case Instruction::Shl:
23542349
return false;
23552350
case Instruction::Add:
23562351
case Instruction::Sub:
23572352
case Instruction::Mul:
2358-
case Instruction::Shl:
23592353
case Instruction::Xor:
23602354
return true;
23612355
default:
@@ -2589,13 +2583,6 @@ Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
25892583
return get(Instruction::Xor, C1, C2);
25902584
}
25912585

2592-
Constant *ConstantExpr::getShl(Constant *C1, Constant *C2,
2593-
bool HasNUW, bool HasNSW) {
2594-
unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2595-
(HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2596-
return get(Instruction::Shl, C1, C2, Flags);
2597-
}
2598-
25992586
Constant *ConstantExpr::getExactLogBase2(Constant *C) {
26002587
Type *Ty = C->getType();
26012588
const APInt *IVal;

llvm/lib/IR/Core.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,11 +1746,6 @@ LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
17461746
unwrap<Constant>(RHSConstant)));
17471747
}
17481748

1749-
LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1750-
return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1751-
unwrap<Constant>(RHSConstant)));
1752-
}
1753-
17541749
LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
17551750
LLVMValueRef *ConstantIndices, unsigned NumIndices) {
17561751
ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),

llvm/test/Assembler/2003-05-21-MalformedShiftCrash.ll

Lines changed: 0 additions & 5 deletions
This file was deleted.

llvm/test/Assembler/flags.ll

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,6 @@ define i64 @mul_signed_ce() {
230230
ret i64 mul nsw (i64 ptrtoint (ptr @addr to i64), i64 91)
231231
}
232232

233-
define i64 @shl_signed_ce() {
234-
; CHECK: ret i64 shl nsw (i64 ptrtoint (ptr @addr to i64), i64 17)
235-
ret i64 shl nsw (i64 ptrtoint (ptr @addr to i64), i64 17)
236-
}
237-
238-
239233
define i64 @add_unsigned_ce() {
240234
; CHECK: ret i64 add nuw (i64 ptrtoint (ptr @addr to i64), i64 91)
241235
ret i64 add nuw (i64 ptrtoint (ptr @addr to i64), i64 91)

llvm/test/Transforms/InstCombine/rotate.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,11 +893,13 @@ define i64 @rotr_select_zext_shamt(i64 %x, i32 %y) {
893893
define i32 @rotl_constant_expr(i32 %shamt) {
894894
; CHECK-LABEL: @rotl_constant_expr(
895895
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 ptrtoint (ptr @external_global to i32), [[SHAMT:%.*]]
896-
; CHECK-NEXT: [[R:%.*]] = or i32 [[SHR]], shl (i32 ptrtoint (ptr @external_global to i32), i32 11)
896+
; CHECK-NEXT: [[SHL:%.*]] = shl i32 ptrtoint (ptr @external_global to i32), 11
897+
; CHECK-NEXT: [[R:%.*]] = or i32 [[SHR]], [[SHL]]
897898
; CHECK-NEXT: ret i32 [[R]]
898899
;
899900
%shr = lshr i32 ptrtoint (ptr @external_global to i32), %shamt
900-
%r = or i32 %shr, shl (i32 ptrtoint (ptr @external_global to i32), i32 11)
901+
%shl = shl i32 ptrtoint (ptr @external_global to i32), 11
902+
%r = or i32 %shr, %shl
901903
ret i32 %r
902904
}
903905

llvm/test/Transforms/InstCombine/shift-logic.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,14 @@ define i32 @lshr_or_extra_use(i32 %x, i32 %y, ptr %p) {
247247
define i32 @PR44028(i32 %x) {
248248
; CHECK-LABEL: @PR44028(
249249
; CHECK-NEXT: [[SH1:%.*]] = ashr exact i32 [[X:%.*]], 16
250-
; CHECK-NEXT: [[T0:%.*]] = xor i32 [[SH1]], shl (i32 ptrtoint (ptr @g to i32), i32 16)
250+
; CHECK-NEXT: [[SH2:%.*]] = shl i32 ptrtoint (ptr @g to i32), 16
251+
; CHECK-NEXT: [[T0:%.*]] = xor i32 [[SH1]], [[SH2]]
251252
; CHECK-NEXT: [[T27:%.*]] = ashr exact i32 [[T0]], 16
252253
; CHECK-NEXT: ret i32 [[T27]]
253254
;
254255
%sh1 = ashr exact i32 %x, 16
255-
%t0 = xor i32 %sh1, shl (i32 ptrtoint (ptr @g to i32), i32 16)
256+
%sh2 = shl i32 ptrtoint (ptr @g to i32), 16
257+
%t0 = xor i32 %sh1, %sh2
256258
%t27 = ashr exact i32 %t0, 16
257259
ret i32 %t27
258260
}

llvm/test/Transforms/InstCombine/udiv-simplify.ll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ define i64 @test2_PR2274(i32 %x, i32 %v) nounwind {
5555
define i32 @PR30366(i1 %a) {
5656
; CHECK-LABEL: @PR30366(
5757
; CHECK-NEXT: [[Z:%.*]] = zext i1 [[A:%.*]] to i32
58-
; CHECK-NEXT: [[Z2:%.*]] = zext nneg i16 shl (i16 1, i16 ptrtoint (ptr @b to i16)) to i32
59-
; CHECK-NEXT: [[D:%.*]] = udiv i32 [[Z]], [[Z2]]
60-
; CHECK-NEXT: ret i32 [[D]]
58+
; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i16 ptrtoint (ptr @b to i16) to i32
59+
; CHECK-NEXT: [[D1:%.*]] = lshr i32 [[Z]], [[TMP1]]
60+
; CHECK-NEXT: ret i32 [[D1]]
6161
;
6262
%z = zext i1 %a to i32
63-
%z2 = zext i16 shl (i16 1, i16 ptrtoint (ptr @b to i16)) to i32
63+
%shl = shl i16 1, ptrtoint (ptr @b to i16)
64+
%z2 = zext i16 %shl to i32
6465
%d = udiv i32 %z, %z2
6566
ret i32 %d
6667
}

llvm/test/Transforms/LoopStrengthReduce/pr2537.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ entry:
88
dobody: ; preds = %dobody, %entry
99
%y.0 = phi i128 [ 0, %entry ], [ %add, %dobody ]
1010
%x.0 = phi i128 [ 0, %entry ], [ %add2, %dobody ]
11-
%add = add i128 %y.0, shl (i128 1, i128 64)
12-
%add2 = add i128 %x.0, shl (i128 1, i128 48)
11+
%add = add i128 %y.0, u0x10000000000000000
12+
%add2 = add i128 %x.0, u0x1000000000000
1313
call void @b( i128 %add )
14-
%cmp = icmp ult i128 %add2, shl (i128 1, i128 64)
14+
%cmp = icmp ult i128 %add2, u0x10000000000000000
1515
br i1 %cmp, label %dobody, label %afterdo
1616

1717
afterdo: ; preds = %dobody

llvm/test/Verifier/ifunc-opaque.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ define ptr @resolver() {
1414

1515
; CHECK: IFunc must have a Function resolver
1616
; CHECK-NEXT: ptr @ifunc_shl
17-
@ifunc_shl = ifunc void (), ptr inttoptr (i64 shl (i64 ptrtoint (ptr @resolver to i64), i64 4) to ptr)
17+
@ifunc_shl = ifunc void (), ptr inttoptr (i64 add (i64 ptrtoint (ptr @resolver to i64), i64 4) to ptr)

llvm/unittests/IR/ConstantsTest.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ TEST(ConstantsTest, Integer_i1) {
5656

5757
// @h = constant i1 shl(i1 1 , i1 1) ; poison
5858
// @h = constant i1 poison
59-
EXPECT_EQ(Poison, ConstantExpr::getShl(One, One));
59+
EXPECT_EQ(Poison, ConstantFoldBinaryInstruction(Instruction::Shl, One, One));
6060

6161
// @i = constant i1 shl(i1 1 , i1 0)
6262
// @i = constant i1 true
63-
EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
63+
EXPECT_EQ(One, ConstantFoldBinaryInstruction(Instruction::Shl, One, Zero));
6464

6565
// @n = constant i1 mul(i1 -1, i1 1)
6666
// @n = constant i1 true
@@ -216,10 +216,6 @@ TEST(ConstantsTest, AsInstructionsTest) {
216216
CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
217217
CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
218218
CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
219-
CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
220-
CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
221-
CHECK(ConstantExpr::getShl(P0, P0, false, true),
222-
"shl nsw i32 " P0STR ", " P0STR);
223219

224220
std::vector<Constant *> V;
225221
V.push_back(One);

0 commit comments

Comments
 (0)