Skip to content

Commit

Permalink
GlobalISel: support irtranslation of icmp instructions.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278969 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
TNorthover committed Aug 17, 2016
1 parent 1d359ed commit 3ed44cd
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 38 deletions.
4 changes: 3 additions & 1 deletion include/llvm/CodeGen/GlobalISel/IRTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class IRTranslator : public MachineFunctionPass {
/// Translate a phi instruction.
bool translatePHI(const User &U);

/// Translate an integer compare instruction (or constant).
bool translateICmp(const User &U);

/// Add remaining operands onto phis we've translated. Executed after all
/// MachineBasicBlocks for the function have been created.
void finishPendingPhis();
Expand Down Expand Up @@ -231,7 +234,6 @@ class IRTranslator : public MachineFunctionPass {
bool translateAddrSpaceCast(const User &U) { return false; }
bool translateCleanupPad(const User &U) { return false; }
bool translateCatchPad(const User &U) { return false; }
bool translateICmp(const User &U) { return false; }
bool translateFCmp(const User &U) { return false; }
bool translateSelect(const User &U) { return false; }
bool translateUserOp1(const User &U) { return false; }
Expand Down
8 changes: 8 additions & 0 deletions include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ class MachineIRBuilder {
///
/// \return The newly created instruction.
MachineInstrBuilder buildTrunc(LLT Ty, unsigned Res, unsigned Op);

/// Build and insert either a G_ICMP
///
/// \pre setBasicBlock or setMI must have been called.
///
/// \return a MachineInstrBuilder for the newly created instruction.
MachineInstrBuilder buildICmp(ArrayRef<LLT> Tys, CmpInst::Predicate Pred,
unsigned Res, unsigned Op0, unsigned Op1);
};

} // End namespace llvm.
Expand Down
5 changes: 5 additions & 0 deletions include/llvm/CodeGen/MachineInstrBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ class MachineInstrBuilder {
return *this;
}

const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
return *this;
}

const MachineInstrBuilder &addSym(MCSymbol *Sym,
unsigned char TargetFlags = 0) const {
MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
Expand Down
16 changes: 15 additions & 1 deletion include/llvm/CodeGen/MachineOperand.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class MachineOperand {
MO_Metadata, ///< Metadata reference (for debug info)
MO_MCSymbol, ///< MCSymbol reference (for debug/eh info)
MO_CFIIndex, ///< MCCFIInstruction index.
MO_IntrinsicID, ///< Intrinsic ID
MO_IntrinsicID, ///< Intrinsic ID for ISel
MO_Predicate, ///< Generic predicate for ISel
};

private:
Expand Down Expand Up @@ -164,6 +165,7 @@ class MachineOperand {
MCSymbol *Sym; // For MO_MCSymbol.
unsigned CFIIndex; // For MO_CFI.
Intrinsic::ID IntrinsicID; // For MO_IntrinsicID.
unsigned Pred; // For MO_Predicate

struct { // For MO_Register.
// Register number is in SmallContents.RegNo.
Expand Down Expand Up @@ -265,6 +267,7 @@ class MachineOperand {
bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
bool isCFIIndex() const { return OpKind == MO_CFIIndex; }
bool isIntrinsicID() const { return OpKind == MO_IntrinsicID; }
bool isPredicate() const { return OpKind == MO_Predicate; }
//===--------------------------------------------------------------------===//
// Accessors for Register Operands
//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -464,6 +467,11 @@ class MachineOperand {
return Contents.IntrinsicID;
}

unsigned getPredicate() const {
assert(isPredicate() && "Wrong MachineOperand accessor");
return Contents.Pred;
}

/// Return the offset from the symbol in this operand. This always returns 0
/// for ExternalSymbol operands.
int64_t getOffset() const {
Expand Down Expand Up @@ -749,6 +757,12 @@ class MachineOperand {
return Op;
}

static MachineOperand CreatePredicate(unsigned Pred) {
MachineOperand Op(MachineOperand::MO_Predicate);
Op.Contents.Pred = Pred;
return Op;
}

friend class MachineInstr;
friend class MachineRegisterInfo;
private:
Expand Down
2 changes: 2 additions & 0 deletions include/llvm/IR/InstrTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,8 @@ class CmpInst : public Instruction {
return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
}

static StringRef getPredicateName(Predicate P);

bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
bool isIntPredicate() const { return isIntPredicate(getPredicate()); }

Expand Down
7 changes: 7 additions & 0 deletions include/llvm/Target/GenericOpcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ def G_ASHR : Instruction {
let hasSideEffects = 0;
}

// Generic bitwise or.
def G_ICMP : Instruction {
let OutOperandList = (outs unknown:$dst);
let InOperandList = (ins unknown:$tst, unknown:$src1, unknown:$src2);
let hasSideEffects = 0;
}

//------------------------------------------------------------------------------
// Memory ops
//------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions include/llvm/Target/TargetOpcodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ HANDLE_TARGET_OPCODE(G_LSHR)
// Generic arithmetic right-shift
HANDLE_TARGET_OPCODE(G_ASHR)

/// Generic integer-base comparison, also applicable to vectors of integers.
HANDLE_TARGET_OPCODE(G_ICMP)

/// Generic BRANCH instruction. This is an unconditional branch.
HANDLE_TARGET_OPCODE(G_BR)

Expand Down
14 changes: 14 additions & 0 deletions lib/CodeGen/GlobalISel/IRTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U) {
return true;
}

bool IRTranslator::translateICmp(const User &U) {
const CmpInst &CI = cast<CmpInst>(U);
unsigned Op0 = getOrCreateVReg(*CI.getOperand(0));
unsigned Op1 = getOrCreateVReg(*CI.getOperand(1));
unsigned Res = getOrCreateVReg(CI);
CmpInst::Predicate Pred = CI.getPredicate();

assert(isa<ICmpInst>(CI) && "only integer comparisons supported now");
assert(CmpInst::isIntPredicate(Pred) && "only int comparisons supported now");
MIRBuilder.buildICmp({LLT{*CI.getType()}, LLT{*CI.getOperand(0)->getType()}},
Pred, Res, Op0, Op1);
return true;
}

bool IRTranslator::translateRet(const User &U) {
const ReturnInst &RI = cast<ReturnInst>(U);
const Value *Ret = RI.getReturnValue();
Expand Down
11 changes: 11 additions & 0 deletions lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,14 @@ MachineInstrBuilder MachineIRBuilder::buildTrunc(LLT Ty, unsigned Res,
unsigned Op) {
return buildInstr(TargetOpcode::G_TRUNC, Ty).addDef(Res).addUse(Op);
}

MachineInstrBuilder MachineIRBuilder::buildICmp(ArrayRef<LLT> Tys,
CmpInst::Predicate Pred,
unsigned Res, unsigned Op0,
unsigned Op1) {
return buildInstr(TargetOpcode::G_ICMP, Tys)
.addDef(Res)
.addPredicate(Pred)
.addUse(Op0)
.addUse(Op1);
}
2 changes: 2 additions & 0 deletions lib/CodeGen/MIRParser/MILexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
.Case("landing-pad", MIToken::kw_landing_pad)
.Case("liveins", MIToken::kw_liveins)
.Case("successors", MIToken::kw_successors)
.Case("floatpred", MIToken::kw_floatpred)
.Case("intpred", MIToken::kw_intpred)
.Default(MIToken::Identifier);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/CodeGen/MIRParser/MILexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ struct MIToken {
kw_landing_pad,
kw_liveins,
kw_successors,
kw_floatpred,
kw_intpred,

// Named metadata keywords
md_tbaa,
Expand Down
62 changes: 62 additions & 0 deletions lib/CodeGen/MIRParser/MIParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class MIParser {
bool parseIRBlock(BasicBlock *&BB, const Function &F);
bool parseBlockAddressOperand(MachineOperand &Dest);
bool parseIntrinsicOperand(MachineOperand &Dest);
bool parsePredicateOperand(MachineOperand &Dest);
bool parseTargetIndexOperand(MachineOperand &Dest);
bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
bool parseMachineOperand(MachineOperand &Dest,
Expand Down Expand Up @@ -1469,6 +1470,64 @@ bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
return false;
}

bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
bool IsFloat = Token.is(MIToken::kw_floatpred);
lex();

if (expectAndConsume(MIToken::lparen))
return error("expected syntax intpred(whatever) or floatpred(whatever");

if (Token.isNot(MIToken::Identifier))
return error("whatever");

CmpInst::Predicate Pred;
if (IsFloat) {
Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
.Case("false", CmpInst::FCMP_FALSE)
.Case("oeq", CmpInst::FCMP_OEQ)
.Case("ogt", CmpInst::FCMP_OGT)
.Case("oge", CmpInst::FCMP_OGE)
.Case("olt", CmpInst::FCMP_OLT)
.Case("ole", CmpInst::FCMP_OLE)
.Case("one", CmpInst::FCMP_ONE)
.Case("ord", CmpInst::FCMP_ORD)
.Case("uno", CmpInst::FCMP_UNO)
.Case("ueq", CmpInst::FCMP_UEQ)
.Case("ugt", CmpInst::FCMP_UGT)
.Case("uge", CmpInst::FCMP_UGE)
.Case("ult", CmpInst::FCMP_ULT)
.Case("ule", CmpInst::FCMP_ULE)
.Case("une", CmpInst::FCMP_UNE)
.Case("true", CmpInst::FCMP_TRUE)
.Default(CmpInst::BAD_FCMP_PREDICATE);
if (!CmpInst::isFPPredicate(Pred))
return error("invalid floating-point predicate");
} else {
Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
.Case("eq", CmpInst::ICMP_EQ)
.Case("ne", CmpInst::ICMP_NE)
.Case("sgt", CmpInst::ICMP_SGT)
.Case("sge", CmpInst::ICMP_SGE)
.Case("slt", CmpInst::ICMP_SLT)
.Case("sle", CmpInst::ICMP_SLE)
.Case("ugt", CmpInst::ICMP_UGT)
.Case("uge", CmpInst::ICMP_UGE)
.Case("ult", CmpInst::ICMP_ULT)
.Case("ule", CmpInst::ICMP_ULE)
.Default(CmpInst::BAD_ICMP_PREDICATE);
if (!CmpInst::isIntPredicate(Pred))
return error("invalid integer predicate");
}

lex();
Dest = MachineOperand::CreatePredicate(Pred);
if (!expectAndConsume(MIToken::rparen))
return error("predicate should be terminated by ')'.");

return false;
}

bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
assert(Token.is(MIToken::kw_target_index));
lex();
Expand Down Expand Up @@ -1575,6 +1634,9 @@ bool MIParser::parseMachineOperand(MachineOperand &Dest,
return parseTargetIndexOperand(Dest);
case MIToken::kw_liveout:
return parseLiveoutRegisterMaskOperand(Dest);
case MIToken::kw_floatpred:
case MIToken::kw_intpred:
return parsePredicateOperand(Dest);
case MIToken::Error:
return true;
case MIToken::Identifier:
Expand Down
6 changes: 6 additions & 0 deletions lib/CodeGen/MIRPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,12 @@ void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI,
}
break;
}
case MachineOperand::MO_Predicate: {
auto Pred = static_cast<CmpInst::Predicate>(Op.getPredicate());
OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
<< CmpInst::getPredicateName(Pred) << ')';
break;
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion lib/CodeGen/MachineInstr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
return getMetadata() == Other.getMetadata();
case MachineOperand::MO_IntrinsicID:
return getIntrinsicID() == Other.getIntrinsicID();
case MachineOperand::MO_Predicate:
return getPredicate() == Other.getPredicate();
}
llvm_unreachable("Invalid machine operand type");
}
Expand Down Expand Up @@ -306,6 +308,8 @@ hash_code llvm::hash_value(const MachineOperand &MO) {
return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
case MachineOperand::MO_IntrinsicID:
return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
case MachineOperand::MO_Predicate:
return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
}
llvm_unreachable("Invalid machine operand type");
}
Expand Down Expand Up @@ -471,8 +475,12 @@ void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
OS << "<intrinsic:" << ID << '>';
break;
}
case MachineOperand::MO_Predicate: {
auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
<< CmpInst::getPredicateName(Pred) << '>';
}
}

if (unsigned TF = getTargetFlags())
OS << "[TF=" << TF << ']';
}
Expand Down
39 changes: 4 additions & 35 deletions lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
//
// The LLVM Compiler Infrastructure
Expand Down Expand Up @@ -1041,39 +1042,6 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
SlotTracker *Machine, const Module *Context,
bool FromValue = false);

static const char *getPredicateText(unsigned predicate) {
const char * pred = "unknown";
switch (predicate) {
case FCmpInst::FCMP_FALSE: pred = "false"; break;
case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
case FCmpInst::FCMP_OGT: pred = "ogt"; break;
case FCmpInst::FCMP_OGE: pred = "oge"; break;
case FCmpInst::FCMP_OLT: pred = "olt"; break;
case FCmpInst::FCMP_OLE: pred = "ole"; break;
case FCmpInst::FCMP_ONE: pred = "one"; break;
case FCmpInst::FCMP_ORD: pred = "ord"; break;
case FCmpInst::FCMP_UNO: pred = "uno"; break;
case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
case FCmpInst::FCMP_UGT: pred = "ugt"; break;
case FCmpInst::FCMP_UGE: pred = "uge"; break;
case FCmpInst::FCMP_ULT: pred = "ult"; break;
case FCmpInst::FCMP_ULE: pred = "ule"; break;
case FCmpInst::FCMP_UNE: pred = "une"; break;
case FCmpInst::FCMP_TRUE: pred = "true"; break;
case ICmpInst::ICMP_EQ: pred = "eq"; break;
case ICmpInst::ICMP_NE: pred = "ne"; break;
case ICmpInst::ICMP_SGT: pred = "sgt"; break;
case ICmpInst::ICMP_SGE: pred = "sge"; break;
case ICmpInst::ICMP_SLT: pred = "slt"; break;
case ICmpInst::ICMP_SLE: pred = "sle"; break;
case ICmpInst::ICMP_UGT: pred = "ugt"; break;
case ICmpInst::ICMP_UGE: pred = "uge"; break;
case ICmpInst::ICMP_ULT: pred = "ult"; break;
case ICmpInst::ICMP_ULE: pred = "ule"; break;
}
return pred;
}

static void writeAtomicRMWOperation(raw_ostream &Out,
AtomicRMWInst::BinOp Op) {
switch (Op) {
Expand Down Expand Up @@ -1349,7 +1317,8 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
Out << CE->getOpcodeName();
WriteOptimizationInfo(Out, CE);
if (CE->isCompare())
Out << ' ' << getPredicateText(CE->getPredicate());
Out << ' ' << CmpInst::getPredicateName(
static_cast<CmpInst::Predicate>(CE->getPredicate()));
Out << " (";

if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
Expand Down Expand Up @@ -2869,7 +2838,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {

// Print out the compare instruction predicates
if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Out << ' ' << getPredicateText(CI->getPredicate());
Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());

// Print out the atomicrmw operation
if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
Expand Down
Loading

0 comments on commit 3ed44cd

Please sign in to comment.