Add utility routines for creating integer negation operators with NSW set.

Integer negation only overflows with INT_MIN, but that's an important case.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91662 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2009-12-18 02:58:50 +00:00
parent 2df72c1441
commit bdc46c6af5
8 changed files with 37 additions and 0 deletions

View File

@ -692,6 +692,7 @@ public:
static Constant *getIntToPtr(Constant *C, const Type *Ty); static Constant *getIntToPtr(Constant *C, const Type *Ty);
static Constant *getBitCast (Constant *C, const Type *Ty); static Constant *getBitCast (Constant *C, const Type *Ty);
static Constant *getNSWNeg(Constant *C);
static Constant *getNSWAdd(Constant *C1, Constant *C2); static Constant *getNSWAdd(Constant *C1, Constant *C2);
static Constant *getNSWSub(Constant *C1, Constant *C2); static Constant *getNSWSub(Constant *C1, Constant *C2);
static Constant *getExactSDiv(Constant *C1, Constant *C2); static Constant *getExactSDiv(Constant *C1, Constant *C2);

View File

@ -308,6 +308,10 @@ public:
Instruction *InsertBefore = 0); Instruction *InsertBefore = 0);
static BinaryOperator *CreateNeg(Value *Op, const Twine &Name, static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
BasicBlock *InsertAtEnd); BasicBlock *InsertAtEnd);
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
Instruction *InsertBefore = 0);
static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
BasicBlock *InsertAtEnd);
static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "", static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
Instruction *InsertBefore = 0); Instruction *InsertBefore = 0);
static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name, static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,

View File

@ -109,6 +109,9 @@ public:
Constant *CreateNeg(Constant *C) const { Constant *CreateNeg(Constant *C) const {
return ConstantExpr::getNeg(C); return ConstantExpr::getNeg(C);
} }
Constant *CreateNSWNeg(Constant *C) const {
return ConstantExpr::getNSWNeg(C);
}
Constant *CreateFNeg(Constant *C) const { Constant *CreateFNeg(Constant *C) const {
return ConstantExpr::getFNeg(C); return ConstantExpr::getFNeg(C);
} }

View File

@ -478,6 +478,11 @@ public:
return Folder.CreateNeg(VC); return Folder.CreateNeg(VC);
return Insert(BinaryOperator::CreateNeg(V), Name); return Insert(BinaryOperator::CreateNeg(V), Name);
} }
Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
if (Constant *VC = dyn_cast<Constant>(V))
return Folder.CreateNSWNeg(VC);
return Insert(BinaryOperator::CreateNSWNeg(V), Name);
}
Value *CreateFNeg(Value *V, const Twine &Name = "") { Value *CreateFNeg(Value *V, const Twine &Name = "") {
if (Constant *VC = dyn_cast<Constant>(V)) if (Constant *VC = dyn_cast<Constant>(V))
return Folder.CreateFNeg(VC); return Folder.CreateFNeg(VC);

View File

@ -115,6 +115,9 @@ public:
Value *CreateNeg(Constant *C) const { Value *CreateNeg(Constant *C) const {
return BinaryOperator::CreateNeg(C); return BinaryOperator::CreateNeg(C);
} }
Value *CreateNSWNeg(Constant *C) const {
return BinaryOperator::CreateNSWNeg(C);
}
Value *CreateNot(Constant *C) const { Value *CreateNot(Constant *C) const {
return BinaryOperator::CreateNot(C); return BinaryOperator::CreateNot(C);
} }

View File

@ -122,6 +122,9 @@ public:
Constant *CreateNeg(Constant *C) const { Constant *CreateNeg(Constant *C) const {
return Fold(ConstantExpr::getNeg(C)); return Fold(ConstantExpr::getNeg(C));
} }
Constant *CreateNSWNeg(Constant *C) const {
return Fold(ConstantExpr::getNSWNeg(C));
}
Constant *CreateFNeg(Constant *C) const { Constant *CreateFNeg(Constant *C) const {
return Fold(ConstantExpr::getFNeg(C)); return Fold(ConstantExpr::getFNeg(C));
} }

View File

@ -627,6 +627,12 @@ Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
return get(std::vector<Constant*>(Vals, Vals+NumVals)); return get(std::vector<Constant*>(Vals, Vals+NumVals));
} }
Constant* ConstantExpr::getNSWNeg(Constant* C) {
assert(C->getType()->isIntOrIntVector() &&
"Cannot NEG a nonintegral value!");
return getNSWSub(ConstantFP::getZeroValueForNegation(C->getType()), C);
}
Constant* ConstantExpr::getNSWAdd(Constant* C1, Constant* C2) { Constant* ConstantExpr::getNSWAdd(Constant* C1, Constant* C2) {
return getTy(C1->getType(), Instruction::Add, C1, C2, return getTy(C1->getType(), Instruction::Add, C1, C2,
OverflowingBinaryOperator::NoSignedWrap); OverflowingBinaryOperator::NoSignedWrap);

View File

@ -1772,6 +1772,18 @@ BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Op->getType(), Name, InsertAtEnd); Op->getType(), Name, InsertAtEnd);
} }
BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
Instruction *InsertBefore) {
Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
}
BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
BasicBlock *InsertAtEnd) {
Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
}
BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Instruction *InsertBefore) { Instruction *InsertBefore) {
Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());