Skip to content

Commit caa2258

Browse files
authored
[LLVM] Remove nuw neg (#86295)
This patch removes APIs that creating NUW neg. It is a trivial case because `sub nuw 0, X` always gets simplified into zero. I believe there is no optimization opportunities in the real-world applications that we can take advantage of the nuw flag. Motivated by #84792 (comment). Compile-time improvement: https://llvm-compile-time-tracker.com/compare.php?from=d1f182c895728d89c5c3d198b133e212a5d9d4a3&to=da7b7478b7cbb32c09d760f6b8d0e67901e0d533&stat=instructions:u
1 parent feebcd6 commit caa2258

14 files changed

+37
-52
lines changed

llvm/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ Changes to the C API
146146
* ``LLVMGetPrologueData``
147147
* ``LLVMSetPrologueData``
148148

149+
* Deprecated ``LLVMConstNUWNeg`` and ``LLVMBuildNUWNeg``.
150+
149151
Changes to the CodeGen infrastructure
150152
-------------------------------------
151153

llvm/include/llvm-c/Core.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,9 @@ LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty);
23162316
LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty);
23172317
LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal);
23182318
LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal);
2319-
LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal);
2319+
LLVM_ATTRIBUTE_C_DEPRECATED(
2320+
LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal),
2321+
"Use LLVMConstNull instead.");
23202322
LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal);
23212323
LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
23222324
LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
@@ -4152,8 +4154,10 @@ LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
41524154
LLVMValueRef LLVMBuildNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name);
41534155
LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
41544156
const char *Name);
4155-
LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
4156-
const char *Name);
4157+
LLVM_ATTRIBUTE_C_DEPRECATED(LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B,
4158+
LLVMValueRef V,
4159+
const char *Name),
4160+
"Use LLVMBuildNeg + LLVMSetNUW instead.");
41574161
LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name);
41584162
LLVMValueRef LLVMBuildNot(LLVMBuilderRef, LLVMValueRef V, const char *Name);
41594163

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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
16481648
}
16491649

16501650
LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
1651-
return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1651+
return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
16521652
}
16531653

16541654

@@ -3557,7 +3557,10 @@ LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
35573557

35583558
LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
35593559
const char *Name) {
3560-
return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3560+
Value *Neg = unwrap(B)->CreateNeg(unwrap(V), Name);
3561+
if (auto *I = dyn_cast<BinaryOperator>(Neg))
3562+
I->setHasNoUnsignedWrap();
3563+
return wrap(Neg);
35613564
}
35623565

35633566
LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {

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
@@ -2524,9 +2524,10 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
25242524
// sub (xor A, B), B ; flip bits if negative and subtract -1 (add 1)
25252525
// --> (A < 0) ? -A : A
25262526
Value *IsNeg = Builder.CreateIsNeg(A);
2527-
// Copy the nuw/nsw flags from the sub to the negate.
2528-
Value *NegA = Builder.CreateNeg(A, "", I.hasNoUnsignedWrap(),
2529-
I.hasNoSignedWrap());
2527+
// Copy the nsw flags from the sub to the negate.
2528+
Value *NegA = I.hasNoUnsignedWrap()
2529+
? Constant::getNullValue(A->getType())
2530+
: Builder.CreateNeg(A, "", I.hasNoSignedWrap());
25302531
return SelectInst::Create(IsNeg, NegA, A);
25312532
}
25322533

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;

0 commit comments

Comments
 (0)