Skip to content

Commit 5493ddf

Browse files
committed
Revert "[IR] Remove mul constant expression (llvm#127046)"
This reverts commit d8b2e43.
1 parent 16b9854 commit 5493ddf

31 files changed

+174
-64
lines changed

llvm/bindings/ocaml/llvm/llvm.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,9 @@ external const_nuw_add : llvalue -> llvalue -> llvalue = "llvm_const_nuw_add"
655655
external const_sub : llvalue -> llvalue -> llvalue = "llvm_const_sub"
656656
external const_nsw_sub : llvalue -> llvalue -> llvalue = "llvm_const_nsw_sub"
657657
external const_nuw_sub : llvalue -> llvalue -> llvalue = "llvm_const_nuw_sub"
658+
external const_mul : llvalue -> llvalue -> llvalue = "llvm_const_mul"
659+
external const_nsw_mul : llvalue -> llvalue -> llvalue = "llvm_const_nsw_mul"
660+
external const_nuw_mul : llvalue -> llvalue -> llvalue = "llvm_const_nuw_mul"
658661
external const_xor : llvalue -> llvalue -> llvalue = "llvm_const_xor"
659662
external const_gep : lltype -> llvalue -> llvalue array -> llvalue
660663
= "llvm_const_gep"

llvm/bindings/ocaml/llvm/llvm.mli

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,20 @@ val const_nsw_sub : llvalue -> llvalue -> llvalue
11311131
See the method [llvm::ConstantExpr::getNSWSub]. *)
11321132
val const_nuw_sub : llvalue -> llvalue -> llvalue
11331133

1134+
(** [const_mul c1 c2] returns the constant product of two constants.
1135+
See the method [llvm::ConstantExpr::getMul]. *)
1136+
val const_mul : llvalue -> llvalue -> llvalue
1137+
1138+
(** [const_nsw_mul c1 c2] returns the constant product of two constants with
1139+
no signed wrapping. The result is undefined if the sum overflows.
1140+
See the method [llvm::ConstantExpr::getNSWMul]. *)
1141+
val const_nsw_mul : llvalue -> llvalue -> llvalue
1142+
1143+
(** [const_nuw_mul c1 c2] returns the constant product of two constants with
1144+
no unsigned wrapping. The result is undefined if the sum overflows.
1145+
See the method [llvm::ConstantExpr::getNSWMul]. *)
1146+
val const_nuw_mul : llvalue -> llvalue -> llvalue
1147+
11341148
(** [const_xor c1 c2] returns the constant bitwise [XOR] of two integer
11351149
constants.
11361150
See the method [llvm::ConstantExpr::getXor]. *)

llvm/bindings/ocaml/llvm/llvm_ocaml.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,24 @@ value llvm_const_nuw_sub(value LHS, value RHS) {
12101210
return to_val(Value);
12111211
}
12121212

1213+
/* llvalue -> llvalue -> llvalue */
1214+
value llvm_const_mul(value LHS, value RHS) {
1215+
LLVMValueRef Value = LLVMConstMul(Value_val(LHS), Value_val(RHS));
1216+
return to_val(Value);
1217+
}
1218+
1219+
/* llvalue -> llvalue -> llvalue */
1220+
value llvm_const_nsw_mul(value LHS, value RHS) {
1221+
LLVMValueRef Value = LLVMConstNSWMul(Value_val(LHS), Value_val(RHS));
1222+
return to_val(Value);
1223+
}
1224+
1225+
/* llvalue -> llvalue -> llvalue */
1226+
value llvm_const_nuw_mul(value LHS, value RHS) {
1227+
LLVMValueRef Value = LLVMConstNUWMul(Value_val(LHS), Value_val(RHS));
1228+
return to_val(Value);
1229+
}
1230+
12131231
/* llvalue -> llvalue -> llvalue */
12141232
value llvm_const_xor(value LHS, value RHS) {
12151233
LLVMValueRef Value = LLVMConstXor(Value_val(LHS), Value_val(RHS));

llvm/docs/LangRef.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5123,6 +5123,10 @@ The following is the syntax for constant expressions:
51235123
Perform an addition on constants.
51245124
``sub (LHS, RHS)``
51255125
Perform a subtraction on constants.
5126+
``mul (LHS, RHS)``
5127+
Perform a multiplication on constants.
5128+
``shl (LHS, RHS)``
5129+
Perform a left shift on constants.
51265130
``xor (LHS, RHS)``
51275131
Perform a bitwise xor on constants.
51285132

llvm/docs/ReleaseNotes.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ Changes to the LLVM IR
5757
----------------------
5858

5959
* The `nocapture` attribute has been replaced by `captures(none)`.
60-
* The constant expression variants of the following instructions have been
61-
removed:
62-
63-
* `mul`
6460

6561
* Updated semantics of `llvm.type.checked.load.relative` to match that of
6662
`llvm.load.relative`.
@@ -177,15 +173,6 @@ Changes to the Python bindings
177173
Changes to the C API
178174
--------------------
179175

180-
* The following functions for creating constant expressions have been removed,
181-
because the underlying constant expressions are no longer supported. Instead,
182-
an instruction should be created using the `LLVMBuildXYZ` APIs, which will
183-
constant fold the operands if possible and create an instruction otherwise:
184-
185-
* `LLVMConstMul`
186-
* `LLVMConstNUWMul`
187-
* `LLVMConstNSWMul`
188-
189176
Changes to the CodeGen infrastructure
190177
-------------------------------------
191178

llvm/include/llvm-c/Core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,9 @@ LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant)
24592459
LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
24602460
LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
24612461
LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
2462+
LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
2463+
LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
2464+
LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
24622465
LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
24632466
LLVMValueRef LLVMConstGEP2(LLVMTypeRef Ty, LLVMValueRef ConstantVal,
24642467
LLVMValueRef *ConstantIndices, unsigned NumIndices);

llvm/include/llvm/IR/Constants.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,8 @@ class ConstantExpr : public Constant {
11491149
bool HasNSW = false);
11501150
static Constant *getSub(Constant *C1, Constant *C2, bool HasNUW = false,
11511151
bool HasNSW = false);
1152+
static Constant *getMul(Constant *C1, Constant *C2, bool HasNUW = false,
1153+
bool HasNSW = false);
11521154
static Constant *getXor(Constant *C1, Constant *C2);
11531155
static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
11541156
static Constant *getPtrToInt(Constant *C, Type *Ty,
@@ -1178,6 +1180,14 @@ class ConstantExpr : public Constant {
11781180
return getSub(C1, C2, true, false);
11791181
}
11801182

1183+
static Constant *getNSWMul(Constant *C1, Constant *C2) {
1184+
return getMul(C1, C2, false, true);
1185+
}
1186+
1187+
static Constant *getNUWMul(Constant *C1, Constant *C2) {
1188+
return getMul(C1, C2, true, false);
1189+
}
1190+
11811191
/// If C is a scalar/fixed width vector of known powers of 2, then this
11821192
/// function returns a new scalar/fixed width vector obtained from logBase2
11831193
/// of C. Undef vector elements are set to zero.

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4335,8 +4335,6 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
43354335
return error(ID.Loc, "ashr constexprs are no longer supported");
43364336
case lltok::kw_shl:
43374337
return error(ID.Loc, "shl constexprs are no longer supported");
4338-
case lltok::kw_mul:
4339-
return error(ID.Loc, "mul constexprs are no longer supported");
43404338
case lltok::kw_fneg:
43414339
return error(ID.Loc, "fneg constexprs are no longer supported");
43424340
case lltok::kw_select:
@@ -4365,6 +4363,7 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
43654363
// Binary Operators.
43664364
case lltok::kw_add:
43674365
case lltok::kw_sub:
4366+
case lltok::kw_mul:
43684367
case lltok::kw_xor: {
43694368
bool NUW = false;
43704369
bool NSW = false;

llvm/lib/IR/Constants.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2424,10 +2424,10 @@ bool ConstantExpr::isSupportedBinOp(unsigned Opcode) {
24242424
case Instruction::LShr:
24252425
case Instruction::AShr:
24262426
case Instruction::Shl:
2427-
case Instruction::Mul:
24282427
return false;
24292428
case Instruction::Add:
24302429
case Instruction::Sub:
2430+
case Instruction::Mul:
24312431
case Instruction::Xor:
24322432
return true;
24332433
default:
@@ -2651,6 +2651,13 @@ Constant *ConstantExpr::getSub(Constant *C1, Constant *C2,
26512651
return get(Instruction::Sub, C1, C2, Flags);
26522652
}
26532653

2654+
Constant *ConstantExpr::getMul(Constant *C1, Constant *C2,
2655+
bool HasNUW, bool HasNSW) {
2656+
unsigned Flags = (HasNUW ? OverflowingBinaryOperator::NoUnsignedWrap : 0) |
2657+
(HasNSW ? OverflowingBinaryOperator::NoSignedWrap : 0);
2658+
return get(Instruction::Mul, C1, C2, Flags);
2659+
}
2660+
26542661
Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
26552662
return get(Instruction::Xor, C1, C2);
26562663
}

llvm/lib/IR/Core.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,23 @@ LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
18031803
unwrap<Constant>(RHSConstant)));
18041804
}
18051805

1806+
LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1807+
return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1808+
unwrap<Constant>(RHSConstant)));
1809+
}
1810+
1811+
LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
1812+
LLVMValueRef RHSConstant) {
1813+
return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1814+
unwrap<Constant>(RHSConstant)));
1815+
}
1816+
1817+
LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
1818+
LLVMValueRef RHSConstant) {
1819+
return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1820+
unwrap<Constant>(RHSConstant)));
1821+
}
1822+
18061823
LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
18071824
return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
18081825
unwrap<Constant>(RHSConstant)));

0 commit comments

Comments
 (0)