Skip to content

Commit da7b747

Browse files
committed
[LLVM] Remove nuw neg
1 parent d1f182c commit da7b747

File tree

17 files changed

+23
-91
lines changed

17 files changed

+23
-91
lines changed

llvm/bindings/ocaml/llvm/llvm.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,6 @@ external align_of : lltype -> llvalue = "llvm_align_of"
637637
external size_of : lltype -> llvalue = "llvm_size_of"
638638
external const_neg : llvalue -> llvalue = "llvm_const_neg"
639639
external const_nsw_neg : llvalue -> llvalue = "llvm_const_nsw_neg"
640-
external const_nuw_neg : llvalue -> llvalue = "llvm_const_nuw_neg"
641640
external const_not : llvalue -> llvalue = "llvm_const_not"
642641
external const_add : llvalue -> llvalue -> llvalue = "llvm_const_add"
643642
external const_nsw_add : llvalue -> llvalue -> llvalue = "llvm_const_nsw_add"
@@ -1253,8 +1252,6 @@ external build_neg : llvalue -> string -> llbuilder -> llvalue
12531252
= "llvm_build_neg"
12541253
external build_nsw_neg : llvalue -> string -> llbuilder -> llvalue
12551254
= "llvm_build_nsw_neg"
1256-
external build_nuw_neg : llvalue -> string -> llbuilder -> llvalue
1257-
= "llvm_build_nuw_neg"
12581255
external build_fneg : llvalue -> string -> llbuilder -> llvalue
12591256
= "llvm_build_fneg"
12601257
external build_not : llvalue -> string -> llbuilder -> llvalue

llvm/bindings/ocaml/llvm/llvm.mli

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,11 +1074,6 @@ val const_neg : llvalue -> llvalue
10741074
See the method [llvm::ConstantExpr::getNSWNeg]. *)
10751075
val const_nsw_neg : llvalue -> llvalue
10761076

1077-
(** [const_nuw_neg c] returns the arithmetic negation of the constant [c] with
1078-
no unsigned wrapping. The result is undefined if the negation overflows.
1079-
See the method [llvm::ConstantExpr::getNUWNeg]. *)
1080-
val const_nuw_neg : llvalue -> llvalue
1081-
10821077
(** [const_not c] returns the bitwise inverse of the constant [c].
10831078
See the method [llvm::ConstantExpr::getNot]. *)
10841079
val const_not : llvalue -> llvalue
@@ -2201,13 +2196,6 @@ val build_neg : llvalue -> string -> llbuilder -> llvalue
22012196
See the method [llvm::LLVMBuilder::CreateNeg]. *)
22022197
val build_nsw_neg : llvalue -> string -> llbuilder -> llvalue
22032198

2204-
(** [build_nuw_neg x name b] creates a
2205-
[%name = nuw sub 0, %x]
2206-
instruction at the position specified by the instruction builder [b].
2207-
[-0.0] is used for floating point types to compute the correct sign.
2208-
See the method [llvm::LLVMBuilder::CreateNeg]. *)
2209-
val build_nuw_neg : llvalue -> string -> llbuilder -> llvalue
2210-
22112199
(** [build_fneg x name b] creates a
22122200
[%name = fsub 0, %x]
22132201
instruction at the position specified by the instruction builder [b].

llvm/bindings/ocaml/llvm/llvm_ocaml.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,12 +1143,6 @@ value llvm_const_nsw_neg(value Value) {
11431143
return to_val(NegValue);
11441144
}
11451145

1146-
/* llvalue -> llvalue */
1147-
value llvm_const_nuw_neg(value Value) {
1148-
LLVMValueRef NegValue = LLVMConstNUWNeg(Value_val(Value));
1149-
return to_val(NegValue);
1150-
}
1151-
11521146
/* llvalue -> llvalue */
11531147
value llvm_const_not(value Value) {
11541148
LLVMValueRef NotValue = LLVMConstNot(Value_val(Value));
@@ -2355,12 +2349,6 @@ value llvm_build_nsw_neg(value X, value Name, value B) {
23552349
LLVMBuildNSWNeg(Builder_val(B), Value_val(X), String_val(Name)));
23562350
}
23572351

2358-
/* llvalue -> string -> llbuilder -> llvalue */
2359-
value llvm_build_nuw_neg(value X, value Name, value B) {
2360-
return to_val(
2361-
LLVMBuildNUWNeg(Builder_val(B), Value_val(X), String_val(Name)));
2362-
}
2363-
23642352
/* llvalue -> string -> llbuilder -> llvalue */
23652353
value llvm_build_fneg(value X, value Name, value B) {
23662354
return to_val(LLVMBuildFNeg(Builder_val(B), Value_val(X), String_val(Name)));

llvm/include/llvm-c/Core.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,6 @@ LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty);
23162316
LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty);
23172317
LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal);
23182318
LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal);
2319-
LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal);
23202319
LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal);
23212320
LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
23222321
LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
@@ -4152,8 +4151,6 @@ LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
41524151
LLVMValueRef LLVMBuildNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name);
41534152
LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
41544153
const char *Name);
4155-
LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
4156-
const char *Name);
41574154
LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name);
41584155
LLVMValueRef LLVMBuildNot(LLVMBuilderRef, LLVMValueRef V, const char *Name);
41594156

llvm/include/llvm/IR/Constants.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,7 @@ class ConstantExpr : public Constant {
10461046
///
10471047
static Constant *getSizeOf(Type *Ty);
10481048

1049-
static Constant *getNeg(Constant *C, bool HasNUW = false,
1050-
bool HasNSW = false);
1049+
static Constant *getNeg(Constant *C, bool HasNSW = false);
10511050
static Constant *getNot(Constant *C);
10521051
static Constant *getAdd(Constant *C1, Constant *C2, bool HasNUW = false,
10531052
bool HasNSW = false);
@@ -1068,8 +1067,7 @@ class ConstantExpr : public Constant {
10681067
static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
10691068
bool OnlyIfReduced = false);
10701069

1071-
static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
1072-
static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
1070+
static Constant *getNSWNeg(Constant *C) { return getNeg(C, /*HasNSW=*/true); }
10731071

10741072
static Constant *getNSWAdd(Constant *C1, Constant *C2) {
10751073
return getAdd(C1, C2, false, true);

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,18 +1712,13 @@ class IRBuilderBase {
17121712
const Twine &Name = "", MDNode *FPMathTag = nullptr,
17131713
std::optional<fp::ExceptionBehavior> Except = std::nullopt);
17141714

1715-
Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNUW = false,
1716-
bool HasNSW = false) {
1717-
return CreateSub(Constant::getNullValue(V->getType()), V, Name, HasNUW,
1718-
HasNSW);
1715+
Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNSW = false) {
1716+
return CreateSub(Constant::getNullValue(V->getType()), V, Name,
1717+
/*HasNUW=*/0, HasNSW);
17191718
}
17201719

17211720
Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1722-
return CreateNeg(V, Name, false, true);
1723-
}
1724-
1725-
Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1726-
return CreateNeg(V, Name, true, false);
1721+
return CreateNeg(V, Name, /*HasNSW=*/true);
17271722
}
17281723

17291724
Value *CreateFNeg(Value *V, const Twine &Name = "",

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,6 @@ class BinaryOperator : public Instruction {
476476
Instruction *InsertBefore = nullptr);
477477
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
478478
BasicBlock *InsertAtEnd);
479-
static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
480-
BasicBlock::iterator InsertBefore);
481-
static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
482-
Instruction *InsertBefore = nullptr);
483-
static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
484-
BasicBlock *InsertAtEnd);
485479
static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
486480
BasicBlock::iterator InsertBefore);
487481
static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",

llvm/lib/IR/Constants.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,10 +2520,10 @@ Constant *ConstantExpr::getShuffleVector(Constant *V1, Constant *V2,
25202520
return pImpl->ExprConstants.getOrCreate(ShufTy, Key);
25212521
}
25222522

2523-
Constant *ConstantExpr::getNeg(Constant *C, bool HasNUW, bool HasNSW) {
2523+
Constant *ConstantExpr::getNeg(Constant *C, bool HasNSW) {
25242524
assert(C->getType()->isIntOrIntVectorTy() &&
25252525
"Cannot NEG a nonintegral value!");
2526-
return getSub(ConstantInt::get(C->getType(), 0), C, HasNUW, HasNSW);
2526+
return getSub(ConstantInt::get(C->getType(), 0), C, /*HasNUW=*/false, HasNSW);
25272527
}
25282528

25292529
Constant *ConstantExpr::getNot(Constant *C) {

llvm/lib/IR/Core.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,11 +1647,6 @@ LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
16471647
return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
16481648
}
16491649

1650-
LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1651-
return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1652-
}
1653-
1654-
16551650
LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
16561651
return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
16571652
}
@@ -3555,11 +3550,6 @@ LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
35553550
return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
35563551
}
35573552

3558-
LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
3559-
const char *Name) {
3560-
return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3561-
}
3562-
35633553
LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
35643554
return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
35653555
}

llvm/lib/IR/Instructions.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,18 +3372,6 @@ BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
33723372
return BinaryOperator::CreateNSWSub(Zero, Op, Name, InsertAtEnd);
33733373
}
33743374

3375-
BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
3376-
Instruction *InsertBefore) {
3377-
Value *Zero = ConstantInt::get(Op->getType(), 0);
3378-
return BinaryOperator::CreateNUWSub(Zero, Op, Name, InsertBefore);
3379-
}
3380-
3381-
BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
3382-
BasicBlock *InsertAtEnd) {
3383-
Value *Zero = ConstantInt::get(Op->getType(), 0);
3384-
return BinaryOperator::CreateNUWSub(Zero, Op, Name, InsertAtEnd);
3385-
}
3386-
33873375
BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
33883376
BasicBlock::iterator InsertBefore) {
33893377
Constant *C = Constant::getAllOnesValue(Op->getType());

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,9 +2522,10 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
25222522
// sub (xor A, B), B ; flip bits if negative and subtract -1 (add 1)
25232523
// --> (A < 0) ? -A : A
25242524
Value *IsNeg = Builder.CreateIsNeg(A);
2525-
// Copy the nuw/nsw flags from the sub to the negate.
2526-
Value *NegA = Builder.CreateNeg(A, "", I.hasNoUnsignedWrap(),
2527-
I.hasNoSignedWrap());
2525+
// Copy the nsw flags from the sub to the negate.
2526+
Value *NegA = I.hasNoUnsignedWrap()
2527+
? Constant::getNullValue(A->getType())
2528+
: Builder.CreateNeg(A, "", I.hasNoSignedWrap());
25282529
return SelectInst::Create(IsNeg, NegA, A);
25292530
}
25302531

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,10 +4250,11 @@ static Instruction *canonicalizeAbs(BinaryOperator &Xor,
42504250
// xor (add A, Op1), Op1 ; add -1 and flip bits if negative
42514251
// --> (A < 0) ? -A : A
42524252
Value *IsNeg = Builder.CreateIsNeg(A);
4253-
// Copy the nuw/nsw flags from the add to the negate.
4253+
// Copy the nsw flags from the add to the negate.
42544254
auto *Add = cast<BinaryOperator>(Op0);
4255-
Value *NegA = Builder.CreateNeg(A, "", Add->hasNoUnsignedWrap(),
4256-
Add->hasNoSignedWrap());
4255+
Value *NegA = Add->hasNoUnsignedWrap()
4256+
? Constant::getNullValue(A->getType())
4257+
: Builder.CreateNeg(A, "", Add->hasNoSignedWrap());
42574258
return SelectInst::Create(IsNeg, NegA, A);
42584259
}
42594260
return nullptr;

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
17951795
// We don't have a "nabs" intrinsic, so negate if needed based on the
17961796
// max/min operation.
17971797
if (IID == Intrinsic::smin || IID == Intrinsic::umax)
1798-
Abs = Builder.CreateNeg(Abs, "nabs", /* NUW */ false, IntMinIsPoison);
1798+
Abs = Builder.CreateNeg(Abs, "nabs", IntMinIsPoison);
17991799
return replaceInstUsesWith(CI, Abs);
18001800
}
18011801

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ static Value *foldMulSelectToNegate(BinaryOperator &I,
105105
if (match(&I, m_c_Mul(m_OneUse(m_Select(m_Value(Cond), m_One(), m_AllOnes())),
106106
m_Value(OtherOp)))) {
107107
bool HasAnyNoWrap = I.hasNoSignedWrap() || I.hasNoUnsignedWrap();
108-
Value *Neg = Builder.CreateNeg(OtherOp, "", false, HasAnyNoWrap);
108+
Value *Neg = Builder.CreateNeg(OtherOp, "", HasAnyNoWrap);
109109
return Builder.CreateSelect(Cond, OtherOp, Neg);
110110
}
111111
// mul (select Cond, -1, 1), OtherOp --> select Cond, -OtherOp, OtherOp
112112
// mul OtherOp, (select Cond, -1, 1) --> select Cond, -OtherOp, OtherOp
113113
if (match(&I, m_c_Mul(m_OneUse(m_Select(m_Value(Cond), m_AllOnes(), m_One())),
114114
m_Value(OtherOp)))) {
115115
bool HasAnyNoWrap = I.hasNoSignedWrap() || I.hasNoUnsignedWrap();
116-
Value *Neg = Builder.CreateNeg(OtherOp, "", false, HasAnyNoWrap);
116+
Value *Neg = Builder.CreateNeg(OtherOp, "", HasAnyNoWrap);
117117
return Builder.CreateSelect(Cond, Neg, OtherOp);
118118
}
119119

@@ -452,9 +452,8 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
452452
// mul Y, (sext X) -> select X, -Y, 0
453453
if (match(&I, m_c_Mul(m_OneUse(m_SExt(m_Value(X))), m_Value(Y))) &&
454454
X->getType()->isIntOrIntVectorTy(1))
455-
return SelectInst::Create(
456-
X, Builder.CreateNeg(Y, "", /*HasNUW=*/false, I.hasNoSignedWrap()),
457-
ConstantInt::getNullValue(Op0->getType()));
455+
return SelectInst::Create(X, Builder.CreateNeg(Y, "", I.hasNoSignedWrap()),
456+
ConstantInt::getNullValue(Op0->getType()));
458457

459458
Constant *ImmC;
460459
if (match(Op1, m_ImmConstant(ImmC))) {

llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
140140

141141
// Integral constants can be freely negated.
142142
if (match(V, m_AnyIntegralConstant()))
143-
return ConstantExpr::getNeg(cast<Constant>(V), /*HasNUW=*/false,
143+
return ConstantExpr::getNeg(cast<Constant>(V),
144144
/*HasNSW=*/false);
145145

146146
// If we have a non-instruction, then give up.

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static bool processAbsIntrinsic(IntrinsicInst *II, LazyValueInfo *LVI) {
497497
// Is X in [IntMin, 0]? NOTE: INT_MIN is fine!
498498
if (Range.getSignedMax().isNonPositive()) {
499499
IRBuilder<> B(II);
500-
Value *NegX = B.CreateNeg(X, II->getName(), /*HasNUW=*/false,
500+
Value *NegX = B.CreateNeg(X, II->getName(),
501501
/*HasNSW=*/IsIntMinPoison);
502502
++NumAbs;
503503
II->replaceAllUsesWith(NegX);

llvm/test/Bindings/OCaml/core.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ let test_constants () =
252252
group "constant arithmetic";
253253
(* CHECK: @const_neg = global i64 sub
254254
* CHECK: @const_nsw_neg = global i64 sub nsw
255-
* CHECK: @const_nuw_neg = global i64 sub nuw
256255
* CHECK: @const_not = global i64 xor
257256
* CHECK: @const_add = global i64 add
258257
* CHECK: @const_nsw_add = global i64 add nsw
@@ -272,7 +271,6 @@ let test_constants () =
272271
let foldbomb = const_ptrtoint foldbomb_gv i64_type in
273272
ignore (define_global "const_neg" (const_neg foldbomb) m);
274273
ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m);
275-
ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m);
276274
ignore (define_global "const_not" (const_not foldbomb) m);
277275
ignore (define_global "const_add" (const_add foldbomb five) m);
278276
ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m);
@@ -1304,7 +1302,6 @@ let test_builder () =
13041302
* CHECK: %build_xor = xor i32 %P1, %P2
13051303
* CHECK: %build_neg = sub i32 0, %P1
13061304
* CHECK: %build_nsw_neg = sub nsw i32 0, %P1
1307-
* CHECK: %build_nuw_neg = sub nuw i32 0, %P1
13081305
* CHECK: %build_fneg = fneg float %F1
13091306
* CHECK: %build_not = xor i32 %P1, -1
13101307
* CHECK: %build_freeze = freeze i32 %P1
@@ -1336,7 +1333,6 @@ let test_builder () =
13361333
ignore (build_xor p1 p2 "build_xor" b);
13371334
ignore (build_neg p1 "build_neg" b);
13381335
ignore (build_nsw_neg p1 "build_nsw_neg" b);
1339-
ignore (build_nuw_neg p1 "build_nuw_neg" b);
13401336
ignore (build_fneg f1 "build_fneg" b);
13411337
ignore (build_not p1 "build_not" b);
13421338
ignore (build_freeze p1 "build_freeze" b);

0 commit comments

Comments
 (0)