2008-01-06 01:35:39 +00:00
|
|
|
//===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the CodeGenInstruction class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenInstruction.h"
|
2010-03-27 20:09:24 +00:00
|
|
|
#include "CodeGenTarget.h"
|
2008-01-06 01:35:39 +00:00
|
|
|
#include "Record.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2010-01-11 18:03:24 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-01-06 01:35:39 +00:00
|
|
|
#include <set>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
|
2009-12-16 19:43:02 +00:00
|
|
|
// EARLY_CLOBBER: @early $reg
|
|
|
|
std::string::size_type wpos = CStr.find_first_of(" \t");
|
|
|
|
std::string::size_type start = CStr.find_first_not_of(" \t");
|
|
|
|
std::string Tok = CStr.substr(start, wpos - start);
|
|
|
|
if (Tok == "@earlyclobber") {
|
|
|
|
std::string Name = CStr.substr(wpos+1);
|
|
|
|
wpos = Name.find_first_not_of(" \t");
|
|
|
|
if (wpos == std::string::npos)
|
|
|
|
throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
|
|
|
|
Name = Name.substr(wpos);
|
|
|
|
std::pair<unsigned,unsigned> Op =
|
|
|
|
I->ParseOperandName(Name, false);
|
|
|
|
|
|
|
|
// Build the string for the operand
|
2010-02-10 01:45:28 +00:00
|
|
|
if (!I->OperandList[Op.first].Constraints[Op.second].isNone())
|
2009-12-16 19:43:02 +00:00
|
|
|
throw "Operand '" + Name + "' cannot have multiple constraints!";
|
2010-02-10 01:45:28 +00:00
|
|
|
I->OperandList[Op.first].Constraints[Op.second] =
|
|
|
|
CodeGenInstruction::ConstraintInfo::getEarlyClobber();
|
2009-12-16 19:43:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only other constraint is "TIED_TO" for now.
|
2008-01-06 01:35:39 +00:00
|
|
|
std::string::size_type pos = CStr.find_first_of('=');
|
|
|
|
assert(pos != std::string::npos && "Unrecognized constraint");
|
2009-12-16 19:43:02 +00:00
|
|
|
start = CStr.find_first_not_of(" \t");
|
2009-08-26 22:50:39 +00:00
|
|
|
std::string Name = CStr.substr(start, pos - start);
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
// TIED_TO: $src1 = $dst
|
2009-12-16 19:43:02 +00:00
|
|
|
wpos = Name.find_first_of(" \t");
|
2008-01-06 01:35:39 +00:00
|
|
|
if (wpos == std::string::npos)
|
|
|
|
throw "Illegal format for tied-to constraint: '" + CStr + "'";
|
|
|
|
std::string DestOpName = Name.substr(0, wpos);
|
|
|
|
std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
Name = CStr.substr(pos+1);
|
|
|
|
wpos = Name.find_first_not_of(" \t");
|
|
|
|
if (wpos == std::string::npos)
|
|
|
|
throw "Illegal format for tied-to constraint: '" + CStr + "'";
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
std::pair<unsigned,unsigned> SrcOp =
|
|
|
|
I->ParseOperandName(Name.substr(wpos), false);
|
|
|
|
if (SrcOp > DestOp)
|
|
|
|
throw "Illegal tied-to operand constraint '" + CStr + "'";
|
2009-12-15 19:28:13 +00:00
|
|
|
|
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2010-02-10 01:45:28 +00:00
|
|
|
if (!I->OperandList[DestOp.first].Constraints[DestOp.second].isNone())
|
2008-01-06 01:35:39 +00:00
|
|
|
throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
|
2010-02-10 01:45:28 +00:00
|
|
|
I->OperandList[DestOp.first].Constraints[DestOp.second] =
|
|
|
|
CodeGenInstruction::ConstraintInfo::getTied(FlatOpNo);
|
2008-01-06 01:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
|
|
|
|
// Make sure the constraints list for each operand is large enough to hold
|
|
|
|
// constraint info, even if none is present.
|
2009-12-15 19:28:13 +00:00
|
|
|
for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
|
2008-01-06 01:35:39 +00:00
|
|
|
I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
if (CStr.empty()) return;
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
const std::string delims(",");
|
|
|
|
std::string::size_type bidx, eidx;
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
bidx = CStr.find_first_not_of(delims);
|
|
|
|
while (bidx != std::string::npos) {
|
|
|
|
eidx = CStr.find_first_of(delims, bidx);
|
|
|
|
if (eidx == std::string::npos)
|
|
|
|
eidx = CStr.length();
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2009-08-26 22:50:39 +00:00
|
|
|
ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
|
2008-01-06 01:35:39 +00:00
|
|
|
bidx = CStr.find_first_not_of(delims, eidx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
|
|
|
|
: TheDef(R), AsmString(AsmStr) {
|
|
|
|
Namespace = R->getValueAsString("Namespace");
|
|
|
|
|
|
|
|
isReturn = R->getValueAsBit("isReturn");
|
|
|
|
isBranch = R->getValueAsBit("isBranch");
|
|
|
|
isIndirectBranch = R->getValueAsBit("isIndirectBranch");
|
2010-08-08 01:49:35 +00:00
|
|
|
isCompare = R->getValueAsBit("isCompare");
|
2008-01-06 01:35:39 +00:00
|
|
|
isBarrier = R->getValueAsBit("isBarrier");
|
|
|
|
isCall = R->getValueAsBit("isCall");
|
2008-12-03 18:15:48 +00:00
|
|
|
canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
|
2008-01-08 18:05:21 +00:00
|
|
|
mayLoad = R->getValueAsBit("mayLoad");
|
2008-01-06 08:36:04 +00:00
|
|
|
mayStore = R->getValueAsBit("mayStore");
|
2008-01-06 01:35:39 +00:00
|
|
|
isPredicable = R->getValueAsBit("isPredicable");
|
|
|
|
isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
|
|
|
|
isCommutable = R->getValueAsBit("isCommutable");
|
|
|
|
isTerminator = R->getValueAsBit("isTerminator");
|
|
|
|
isReMaterializable = R->getValueAsBit("isReMaterializable");
|
|
|
|
hasDelaySlot = R->getValueAsBit("hasDelaySlot");
|
2009-10-29 18:10:34 +00:00
|
|
|
usesCustomInserter = R->getValueAsBit("usesCustomInserter");
|
2008-01-06 01:35:39 +00:00
|
|
|
hasCtrlDep = R->getValueAsBit("hasCtrlDep");
|
|
|
|
isNotDuplicable = R->getValueAsBit("isNotDuplicable");
|
2008-01-10 07:59:24 +00:00
|
|
|
hasSideEffects = R->getValueAsBit("hasSideEffects");
|
2008-01-06 01:35:39 +00:00
|
|
|
neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
|
2008-05-28 22:54:52 +00:00
|
|
|
isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
|
2009-10-01 08:21:18 +00:00
|
|
|
hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
|
|
|
|
hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
|
2008-01-06 01:35:39 +00:00
|
|
|
hasOptionalDef = false;
|
2008-01-07 05:19:29 +00:00
|
|
|
isVariadic = false;
|
2010-03-18 21:42:03 +00:00
|
|
|
ImplicitDefs = R->getValueAsListOfDefs("Defs");
|
|
|
|
ImplicitUses = R->getValueAsListOfDefs("Uses");
|
2008-01-06 01:35:39 +00:00
|
|
|
|
2010-02-27 23:47:46 +00:00
|
|
|
if (neverHasSideEffects + hasSideEffects > 1)
|
2008-01-10 07:59:24 +00:00
|
|
|
throw R->getName() + ": multiple conflicting side-effect flags set!";
|
2008-01-06 01:35:39 +00:00
|
|
|
|
2010-03-18 21:07:39 +00:00
|
|
|
DagInit *OutDI = R->getValueAsDag("OutOperandList");
|
2010-03-18 20:50:52 +00:00
|
|
|
|
2010-03-18 21:07:39 +00:00
|
|
|
if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
|
2010-03-18 20:56:35 +00:00
|
|
|
if (Init->getDef()->getName() != "outs")
|
2010-03-18 20:50:52 +00:00
|
|
|
throw R->getName() + ": invalid def name for output list: use 'outs'";
|
|
|
|
} else
|
|
|
|
throw R->getName() + ": invalid output list: use 'outs'";
|
|
|
|
|
2010-03-18 21:07:39 +00:00
|
|
|
NumDefs = OutDI->getNumArgs();
|
2010-03-18 20:50:52 +00:00
|
|
|
|
2010-03-18 21:07:39 +00:00
|
|
|
DagInit *InDI = R->getValueAsDag("InOperandList");
|
|
|
|
if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
|
2010-03-18 20:56:35 +00:00
|
|
|
if (Init->getDef()->getName() != "ins")
|
2010-03-18 20:50:52 +00:00
|
|
|
throw R->getName() + ": invalid def name for input list: use 'ins'";
|
|
|
|
} else
|
|
|
|
throw R->getName() + ": invalid input list: use 'ins'";
|
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
unsigned MIOperandNo = 0;
|
|
|
|
std::set<std::string> OperandNames;
|
2010-03-18 21:07:39 +00:00
|
|
|
for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
|
|
|
|
Init *ArgInit;
|
|
|
|
std::string ArgName;
|
|
|
|
if (i < NumDefs) {
|
|
|
|
ArgInit = OutDI->getArg(i);
|
|
|
|
ArgName = OutDI->getArgName(i);
|
|
|
|
} else {
|
|
|
|
ArgInit = InDI->getArg(i-NumDefs);
|
|
|
|
ArgName = InDI->getArgName(i-NumDefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
|
2008-01-06 01:35:39 +00:00
|
|
|
if (!Arg)
|
|
|
|
throw "Illegal operand for the '" + R->getName() + "' instruction!";
|
|
|
|
|
|
|
|
Record *Rec = Arg->getDef();
|
|
|
|
std::string PrintMethod = "printOperand";
|
2010-10-12 22:21:57 +00:00
|
|
|
std::string EncoderMethod;
|
2008-01-06 01:35:39 +00:00
|
|
|
unsigned NumOps = 1;
|
|
|
|
DagInit *MIOpInfo = 0;
|
|
|
|
if (Rec->isSubClassOf("Operand")) {
|
|
|
|
PrintMethod = Rec->getValueAsString("PrintMethod");
|
2010-10-12 22:21:57 +00:00
|
|
|
// If there is an explicit encoder method, use it.
|
|
|
|
if (Rec->getValue("EncoderMethod"))
|
|
|
|
EncoderMethod = Rec->getValueAsString("EncoderMethod");
|
2008-01-06 01:35:39 +00:00
|
|
|
MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
// Verify that MIOpInfo has an 'ops' root value.
|
|
|
|
if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
|
|
|
|
dynamic_cast<DefInit*>(MIOpInfo->getOperator())
|
|
|
|
->getDef()->getName() != "ops")
|
|
|
|
throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
|
|
|
|
"'\n";
|
|
|
|
|
|
|
|
// If we have MIOpInfo, then we have #operands equal to number of entries
|
|
|
|
// in MIOperandInfo.
|
|
|
|
if (unsigned NumArgs = MIOpInfo->getNumArgs())
|
|
|
|
NumOps = NumArgs;
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("PredicateOperand"))
|
|
|
|
isPredicable = true;
|
|
|
|
else if (Rec->isSubClassOf("OptionalDefOperand"))
|
|
|
|
hasOptionalDef = true;
|
|
|
|
} else if (Rec->getName() == "variable_ops") {
|
2008-01-07 05:19:29 +00:00
|
|
|
isVariadic = true;
|
2008-01-06 01:35:39 +00:00
|
|
|
continue;
|
2009-12-15 19:28:13 +00:00
|
|
|
} else if (!Rec->isSubClassOf("RegisterClass") &&
|
2008-05-31 02:11:25 +00:00
|
|
|
Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
|
2008-01-06 01:35:39 +00:00
|
|
|
throw "Unknown operand class '" + Rec->getName() +
|
2008-10-27 15:59:43 +00:00
|
|
|
"' in '" + R->getName() + "' instruction!";
|
2008-01-06 01:35:39 +00:00
|
|
|
|
|
|
|
// Check that the operand has a name and that it's unique.
|
2010-03-18 21:07:39 +00:00
|
|
|
if (ArgName.empty())
|
2008-01-06 01:35:39 +00:00
|
|
|
throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
|
|
|
|
" has no name!";
|
2010-03-18 21:07:39 +00:00
|
|
|
if (!OperandNames.insert(ArgName).second)
|
2008-01-06 01:35:39 +00:00
|
|
|
throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
|
|
|
|
" has the same name as a previous operand!";
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2010-10-12 22:21:57 +00:00
|
|
|
OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
|
2008-01-06 01:35:39 +00:00
|
|
|
MIOperandNo, NumOps, MIOpInfo));
|
|
|
|
MIOperandNo += NumOps;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse Constraints.
|
|
|
|
ParseConstraints(R->getValueAsString("Constraints"), this);
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
// Parse the DisableEncoding field.
|
|
|
|
std::string DisableEncoding = R->getValueAsString("DisableEncoding");
|
|
|
|
while (1) {
|
2010-01-11 18:03:24 +00:00
|
|
|
std::string OpName;
|
|
|
|
tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
|
2008-01-06 01:35:39 +00:00
|
|
|
if (OpName.empty()) break;
|
|
|
|
|
|
|
|
// Figure out which operand this is.
|
|
|
|
std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
|
|
|
|
|
|
|
|
// Mark the operand as not-to-be encoded.
|
|
|
|
if (Op.second >= OperandList[Op.first].DoNotEncode.size())
|
|
|
|
OperandList[Op.first].DoNotEncode.resize(Op.second+1);
|
|
|
|
OperandList[Op.first].DoNotEncode[Op.second] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getOperandNamed - Return the index of the operand with the specified
|
|
|
|
/// non-empty name. If the instruction does not have an operand with the
|
|
|
|
/// specified name, throw an exception.
|
|
|
|
///
|
|
|
|
unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
|
2010-10-11 18:25:51 +00:00
|
|
|
unsigned OpIdx;
|
|
|
|
if (hasOperandNamed(Name, OpIdx)) return OpIdx;
|
2008-01-06 01:35:39 +00:00
|
|
|
throw "Instruction '" + TheDef->getName() +
|
|
|
|
"' does not have an operand named '$" + Name + "'!";
|
|
|
|
}
|
|
|
|
|
2010-10-11 18:25:51 +00:00
|
|
|
/// hasOperandNamed - Query whether the instruction has an operand of the
|
|
|
|
/// given name. If so, return true and set OpIdx to the index of the
|
|
|
|
/// operand. Otherwise, return false.
|
|
|
|
bool CodeGenInstruction::hasOperandNamed(const std::string &Name,
|
|
|
|
unsigned &OpIdx) const {
|
|
|
|
assert(!Name.empty() && "Cannot search for operand with no name!");
|
|
|
|
for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
|
|
|
|
if (OperandList[i].Name == Name) {
|
|
|
|
OpIdx = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-15 19:28:13 +00:00
|
|
|
std::pair<unsigned,unsigned>
|
2008-01-06 01:35:39 +00:00
|
|
|
CodeGenInstruction::ParseOperandName(const std::string &Op,
|
|
|
|
bool AllowWholeOp) {
|
|
|
|
if (Op.empty() || Op[0] != '$')
|
|
|
|
throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
std::string OpName = Op.substr(1);
|
|
|
|
std::string SubOpName;
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
// Check to see if this is $foo.bar.
|
|
|
|
std::string::size_type DotIdx = OpName.find_first_of(".");
|
|
|
|
if (DotIdx != std::string::npos) {
|
|
|
|
SubOpName = OpName.substr(DotIdx+1);
|
|
|
|
if (SubOpName.empty())
|
|
|
|
throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
|
|
|
|
OpName = OpName.substr(0, DotIdx);
|
|
|
|
}
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
unsigned OpIdx = getOperandNamed(OpName);
|
|
|
|
|
|
|
|
if (SubOpName.empty()) { // If no suboperand name was specified:
|
|
|
|
// If one was needed, throw.
|
|
|
|
if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
|
|
|
|
SubOpName.empty())
|
|
|
|
throw TheDef->getName() + ": Illegal to refer to"
|
|
|
|
" whole operand part of complex operand '" + Op + "'";
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
// Otherwise, return the operand.
|
|
|
|
return std::make_pair(OpIdx, 0U);
|
|
|
|
}
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
// Find the suboperand number involved.
|
|
|
|
DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
|
|
|
|
if (MIOpInfo == 0)
|
|
|
|
throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
|
2009-12-15 19:28:13 +00:00
|
|
|
|
2008-01-06 01:35:39 +00:00
|
|
|
// Find the operand with the right name.
|
|
|
|
for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
|
|
|
|
if (MIOpInfo->getArgName(i) == SubOpName)
|
|
|
|
return std::make_pair(OpIdx, i);
|
|
|
|
|
|
|
|
// Otherwise, didn't find it!
|
|
|
|
throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
|
|
|
|
}
|
2010-03-27 20:09:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
|
|
|
|
/// implicit def and it has a known VT, return the VT, otherwise return
|
|
|
|
/// MVT::Other.
|
|
|
|
MVT::SimpleValueType CodeGenInstruction::
|
|
|
|
HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
|
|
|
|
if (ImplicitDefs.empty()) return MVT::Other;
|
|
|
|
|
|
|
|
// Check to see if the first implicit def has a resolvable type.
|
|
|
|
Record *FirstImplicitDef = ImplicitDefs[0];
|
|
|
|
assert(FirstImplicitDef->isSubClassOf("Register"));
|
|
|
|
const std::vector<MVT::SimpleValueType> &RegVTs =
|
|
|
|
TargetInfo.getRegisterVTs(FirstImplicitDef);
|
|
|
|
if (RegVTs.size() == 1)
|
|
|
|
return RegVTs[0];
|
|
|
|
return MVT::Other;
|
|
|
|
}
|
|
|
|
|