Skip to content

Commit 9671150

Browse files
committed
[IR] Add samesign flag to icmp instruction
1 parent a5dfccc commit 9671150

File tree

16 files changed

+123
-2
lines changed

16 files changed

+123
-2
lines changed

llvm/docs/LangRef.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12239,6 +12239,7 @@ Syntax:
1223912239
::
1224012240

1224112241
<result> = icmp <cond> <ty> <op1>, <op2> ; yields i1 or <N x i1>:result
12242+
<result> = icmp samesign <cond> <ty> <op1>, <op2> ; yields i1 or <N x i1>:result
1224212243

1224312244
Overview:
1224412245
"""""""""
@@ -12308,6 +12309,9 @@ If the operands are integer vectors, then they are compared element by
1230812309
element. The result is an ``i1`` vector with the same number of elements
1230912310
as the values being compared. Otherwise, the result is an ``i1``.
1231012311

12312+
If the ``samesign`` keyword is present and the operands are not of the
12313+
same sign then the result a :ref:`poison value <poisonvalues>`.
12314+
1231112315
Example:
1231212316
""""""""
1231312317

llvm/include/llvm/AsmParser/LLToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ enum Kind {
114114
kw_disjoint,
115115
kw_inbounds,
116116
kw_nneg,
117+
kw_samesign,
117118
kw_inrange,
118119
kw_addrspace,
119120
kw_section,

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ enum GetElementPtrOptionalFlags {
537537
GEP_NUW = 2,
538538
};
539539

540+
/// PossiblySameSignOptionalFlags - Flags for serializing
541+
/// PossiblySameSignInst's SubclassOptionalData contents.
542+
enum PossiblySameSignInstOptionalFlags { PSSI_SAME_SIGN = 0 };
543+
540544
/// Encoded AtomicOrdering values.
541545
enum AtomicOrderingCodes {
542546
ORDERING_NOTATOMIC = 0,

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,28 @@ class CmpInst : public Instruction {
10331033
}
10341034
};
10351035

1036+
/// An icmp instruction, which can be marked as "samesign", indicating that the
1037+
/// two operands have the same sign. This means that we can convert "slt/ult"
1038+
/// to "ult", which enables more optimizations.
1039+
class PossiblySameSignInst : public CmpInst {
1040+
public:
1041+
enum { SameSign = (1 << 0) };
1042+
1043+
void setSameSign(bool B) {
1044+
SubclassOptionalData = (SubclassOptionalData & ~SameSign) | (B * SameSign);
1045+
}
1046+
1047+
bool hasSameSign() const { return SubclassOptionalData & SameSign; }
1048+
1049+
static bool classof(const Instruction *I) {
1050+
return I->getOpcode() == Instruction::ICmp;
1051+
}
1052+
1053+
static bool classof(const Value *V) {
1054+
return isa<Instruction>(V) && classof(cast<Instruction>(V));
1055+
}
1056+
};
1057+
10361058
// FIXME: these are redundant if CmpInst < BinaryOperator
10371059
template <>
10381060
struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {

llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct PoisonFlags {
4747
unsigned Exact : 1;
4848
unsigned Disjoint : 1;
4949
unsigned NNeg : 1;
50+
unsigned SameSign : 1;
5051
GEPNoWrapFlags GEPNW;
5152

5253
PoisonFlags(const Instruction *I);

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ lltok::Kind LLLexer::LexIdentifier() {
571571
KEYWORD(disjoint);
572572
KEYWORD(inbounds);
573573
KEYWORD(nneg);
574+
KEYWORD(samesign);
574575
KEYWORD(inrange);
575576
KEYWORD(addrspace);
576577
KEYWORD(section);

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6950,8 +6950,14 @@ int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB,
69506950
case lltok::kw_and:
69516951
case lltok::kw_xor:
69526952
return parseLogical(Inst, PFS, KeywordVal);
6953-
case lltok::kw_icmp:
6954-
return parseCompare(Inst, PFS, KeywordVal);
6953+
case lltok::kw_icmp: {
6954+
bool SameSign = EatIfPresent(lltok::kw_samesign);
6955+
if (parseCompare(Inst, PFS, KeywordVal))
6956+
return true;
6957+
if (SameSign)
6958+
cast<PossiblySameSignInst>(Inst)->setSameSign(true);
6959+
return false;
6960+
}
69556961
case lltok::kw_fcmp: {
69566962
FastMathFlags FMF = EatFastMathFlagsIfPresent();
69576963
int Res = parseCompare(Inst, PFS, KeywordVal);

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5462,6 +5462,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
54625462
if (!CmpInst::isIntPredicate(PredVal))
54635463
return error("Invalid icmp predicate");
54645464
I = new ICmpInst(PredVal, LHS, RHS);
5465+
if (Record[OpNum] & (1 << bitc::PSSI_SAME_SIGN))
5466+
cast<PossiblySameSignInst>(I)->setSameSign(true);
54655467
}
54665468

54675469
ResTypeID = getVirtualTypeID(I->getType()->getScalarType());

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,9 @@ static uint64_t getOptimizationFlags(const Value *V) {
17141714
Flags |= 1 << bitc::GEP_NUSW;
17151715
if (GEP->hasNoUnsignedWrap())
17161716
Flags |= 1 << bitc::GEP_NUW;
1717+
} else if (const auto *PSSI = dyn_cast<PossiblySameSignInst>(V)) {
1718+
if (PSSI->hasSameSign())
1719+
Flags |= 1 << bitc::PSSI_SAME_SIGN;
17171720
}
17181721

17191722
return Flags;

llvm/lib/IR/AsmWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,9 @@ static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
14331433
Out << " nuw";
14341434
if (TI->hasNoSignedWrap())
14351435
Out << " nsw";
1436+
} else if (const auto *PSSI = dyn_cast<PossiblySameSignInst>(U)) {
1437+
if (PSSI->hasSameSign())
1438+
Out << " samesign";
14361439
}
14371440
}
14381441

0 commit comments

Comments
 (0)