From 693173feefaa326fad0e386470846fb3199ba381 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 30 Oct 2010 19:23:13 +0000 Subject: [PATCH] Implement (and document!) support for MnemonicAlias's to have Requires directives, allowing things like this: def : MnemonicAlias<"pop", "popl">, Requires<[In32BitMode]>; def : MnemonicAlias<"pop", "popq">, Requires<[In64BitMode]>; Move the rest of the X86 MnemonicAliases over to the .td file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@117830 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/CodeGenerator.html | 18 +++++- include/llvm/Target/Target.td | 9 ++- lib/Target/X86/AsmParser/X86AsmParser.cpp | 20 ++---- lib/Target/X86/X86InstrInfo.td | 14 +++- utils/TableGen/AsmMatcherEmitter.cpp | 78 +++++++++++++++++++---- 5 files changed, 108 insertions(+), 31 deletions(-) diff --git a/docs/CodeGenerator.html b/docs/CodeGenerator.html index de6a5c1ff4d..60d54e507bc 100644 --- a/docs/CodeGenerator.html +++ b/docs/CodeGenerator.html @@ -1896,7 +1896,7 @@ compiler.

The MatchInstructionImpl function performs alias processing and then does actual matching.

-

Alias processing if the phase that canonicalizes different lexical forms of +

Alias processing is the phase that canonicalizes different lexical forms of the same instructions down to one representation. There are several different kinds of alias that are possible to implement and they are listed below in the order that they are processed (which is in order from simplest/weakest to most @@ -1911,7 +1911,7 @@ description.

The first phase of alias processing is simple instruction mneomonic remapping for classes of instructions which are allowed with two different -mneomonics. This phase is a simple and unconditionally remapping from one input +mnemonics. This phase is a simple and unconditionally remapping from one input mnemonic to one output mnemonic. It isn't possible for this form of alias to look at the operands at all, so the remapping must apply for all forms of a given mnemonic. Mnemonic aliases are defined simply, for example X86 has: @@ -1928,7 +1928,19 @@ def : MnemonicAlias<"ud2a", "ud2">;

... and many others. With a MnemonicAlias definition, the mnemonic is -remapped simply and directly.

+remapped simply and directly. Though MnemonicAlias's can't look at any aspect +of the instruction (such as the operands) they can depend on global modes (the +same ones supported by the matcher), through a Requires clause:

+ +
+
+def : MnemonicAlias<"pushf", "pushfq">, Requires<[In64BitMode]>;
+def : MnemonicAlias<"pushf", "pushfl">, Requires<[In32BitMode]>;
+
+
+ +

In this example, the mnemonic gets mapped into different a new one depending +on the current instruction set.

diff --git a/include/llvm/Target/Target.td b/include/llvm/Target/Target.td index 39f4ba722ce..2af75561ffe 100644 --- a/include/llvm/Target/Target.td +++ b/include/llvm/Target/Target.td @@ -541,11 +541,18 @@ def DefaultAsmParser : AsmParser; /// actually has one for each suffix as well): /// def : MnemonicAlias<"sal", "shl">; /// -/// Mnemonic aliases are mapped before any other translation in the match phase. +/// Mnemonic aliases are mapped before any other translation in the match phase, +/// and do allow Requires predicates, e.g.: +/// +/// def : MnemonicAlias<"pushf", "pushfq">, Requires<[In64BitMode]>; +/// def : MnemonicAlias<"pushf", "pushfl">, Requires<[In32BitMode]>; /// class MnemonicAlias { string FromMnemonic = From; string ToMnemonic = To; + + // Predicates - Predicates that must be true for this remapping to happen. + list Predicates = []; } diff --git a/lib/Target/X86/AsmParser/X86AsmParser.cpp b/lib/Target/X86/AsmParser/X86AsmParser.cpp index 8b410015136..28e30769abc 100644 --- a/lib/Target/X86/AsmParser/X86AsmParser.cpp +++ b/lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -620,16 +620,10 @@ X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) { bool X86ATTAsmParser:: ParseInstruction(StringRef Name, SMLoc NameLoc, SmallVectorImpl &Operands) { - // FIXME: Hack to recognize some aliases. - StringRef PatchedName = StringSwitch(Name) - .Case("push", Is64Bit ? "pushq" : "pushl") - .Case("pop", Is64Bit ? "popq" : "popl") - .Case("pushf", Is64Bit ? "pushfq" : "pushfl") - .Case("popf", Is64Bit ? "popfq" : "popfl") - .Case("retl", Is64Bit ? "retl" : "ret") - .Case("retq", Is64Bit ? "ret" : "retq") - .Case("movzx", "movzb") // FIXME: Not correct. - .Default(Name); + // FIXME: This is not correct at all. + if (Name == "movzx") Name = "movzb"; + + StringRef PatchedName = Name; // FIXME: Hack to recognize cmp{ss,sd,ps,pd}. const MCExpr *ExtraImmOp = 0; @@ -714,9 +708,9 @@ ParseInstruction(StringRef Name, SMLoc NameLoc, // Determine whether this is an instruction prefix. bool isPrefix = - PatchedName == "lock" || PatchedName == "rep" || - PatchedName == "repe" || PatchedName == "repz" || - PatchedName == "repne" || PatchedName == "repnz"; + Name == "lock" || Name == "rep" || + Name == "repe" || Name == "repz" || + Name == "repne" || Name == "repnz"; // This does the actual operand parsing. Don't parse any more if we have a diff --git a/lib/Target/X86/X86InstrInfo.td b/lib/Target/X86/X86InstrInfo.td index 8dc360a5010..c0f350adacf 100644 --- a/lib/Target/X86/X86InstrInfo.td +++ b/lib/Target/X86/X86InstrInfo.td @@ -1266,13 +1266,25 @@ def : MnemonicAlias<"cdq", "cltd">; def : MnemonicAlias<"cwde", "cwtl">; def : MnemonicAlias<"cdqe", "cltq">; -def : MnemonicAlias<"pushfd", "pushfl">; +def : MnemonicAlias<"pop", "popl">, Requires<[In32BitMode]>; +def : MnemonicAlias<"pop", "popq">, Requires<[In64BitMode]>; +def : MnemonicAlias<"popf", "popfl">, Requires<[In32BitMode]>; +def : MnemonicAlias<"popf", "popfq">, Requires<[In64BitMode]>; def : MnemonicAlias<"popfd", "popfl">; +def : MnemonicAlias<"push", "pushl">, Requires<[In32BitMode]>; +def : MnemonicAlias<"push", "pushq">, Requires<[In64BitMode]>; +def : MnemonicAlias<"pushf", "pushfl">, Requires<[In32BitMode]>; +def : MnemonicAlias<"pushf", "pushfq">, Requires<[In64BitMode]>; +def : MnemonicAlias<"pushfd", "pushfl">; + def : MnemonicAlias<"repe", "rep">; def : MnemonicAlias<"repz", "rep">; def : MnemonicAlias<"repnz", "repne">; +def : MnemonicAlias<"retl", "ret">, Requires<[In32BitMode]>; +def : MnemonicAlias<"retq", "ret">, Requires<[In64BitMode]>; + def : MnemonicAlias<"salb", "shlb">; def : MnemonicAlias<"salw", "shlw">; def : MnemonicAlias<"sall", "shll">; diff --git a/utils/TableGen/AsmMatcherEmitter.cpp b/utils/TableGen/AsmMatcherEmitter.cpp index 2fa44b1b136..474319d0116 100644 --- a/utils/TableGen/AsmMatcherEmitter.cpp +++ b/utils/TableGen/AsmMatcherEmitter.cpp @@ -587,7 +587,7 @@ private: SubtargetFeatureInfo *&Entry = SubtargetFeatures[Def]; if (!Entry) { - Entry = new SubtargetFeatureInfo; + Entry = new SubtargetFeatureInfo(); Entry->TheDef = Def; Entry->Index = SubtargetFeatures.size() - 1; Entry->EnumName = "Feature_" + Def->getName(); @@ -1514,6 +1514,35 @@ static void EmitComputeAvailableFeatures(CodeGenTarget &Target, OS << "}\n\n"; } +static std::string GetAliasRequiredFeatures(Record *R) { + // FIXME: This is a total hack. + std::vector ReqFeatures = R->getValueAsListOfDefs("Predicates"); + + std::string Result; + unsigned NumFeatures = 0; + for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) { + Record *Pred = ReqFeatures[i]; + + // FIXME: Total hack; for now, we just limit ourselves to In32BitMode + // and In64BitMode, because we aren't going to have the right feature + // masks for SSE and friends. We need to decide what we are going to do + // about CPU subtypes to implement this the right way. + if (Pred->getName() != "In32BitMode" && + Pred->getName() != "In64BitMode") + continue; + + if (NumFeatures) + Result += '|'; + + Result += "Feature_" + Pred->getName(); + ++NumFeatures; + } + + if (NumFeatures > 1) + Result = '(' + Result + ')'; + return Result; +} + /// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions, /// emit a function for them and return true, otherwise return false. static bool EmitMnemonicAliases(raw_ostream &OS) { @@ -1539,22 +1568,45 @@ static bool EmitMnemonicAliases(raw_ostream &OS) { for (std::map >::iterator I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end(); I != E; ++I) { - const std::string &From = I->first; const std::vector &ToVec = I->second; + + // Loop through each alias and emit code that handles each case. If there + // are two instructions without predicates, emit an error. If there is one, + // emit it last. + std::string MatchCode; + int AliasWithNoPredicate = -1; - // If there is only one destination mnemonic, generate simple code. - if (ToVec.size() == 1) { - Cases.push_back(std::make_pair(From, "Mnemonic = \"" + - ToVec[0]->getValueAsString("ToMnemonic") + - "\"; return;")); - continue; + for (unsigned i = 0, e = ToVec.size(); i != e; ++i) { + Record *R = ToVec[i]; + std::string FeatureMask = GetAliasRequiredFeatures(R); + + // If this unconditionally matches, remember it for later and diagnose + // duplicates. + if (FeatureMask.empty()) { + if (AliasWithNoPredicate != -1) { + // We can't have two aliases from the same mnemonic with no predicate. + PrintError(ToVec[AliasWithNoPredicate]->getLoc(), + "two MnemonicAliases with the same 'from' mnemonic!"); + PrintError(R->getLoc(), "this is the other MnemonicAliases."); + throw std::string("ERROR: Invalid MnemonicAliases definitions!"); + } + + AliasWithNoPredicate = i; + continue; + } + + MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n"; + MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n"; } - // Otherwise, diagnose an error, can't have two aliases from the same - // mnemonic. - PrintError(ToVec[0]->getLoc(), "two MnemonicAliases with the same 'from' mnemonic!"); - PrintError(ToVec[1]->getLoc(), "this is the other MnemonicAliases."); - throw std::string("ERROR: Invalid MnemonicAliases definitions!"); + if (AliasWithNoPredicate != -1) { + Record *R = ToVec[AliasWithNoPredicate]; + MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic") + "\";"; + } + + MatchCode += "return;"; + + Cases.push_back(std::make_pair(I->first, MatchCode)); }