forked from OSchip/llvm-project
X86: Factor the bswap asm matching to be slightly less horrible to read.
llvm-svn: 146831
This commit is contained in:
parent
7bfba2f0d3
commit
792edd3c75
|
@ -14700,11 +14700,41 @@ bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
|
||||||
// X86 Inline Assembly Support
|
// X86 Inline Assembly Support
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
// Helper to match a string separated by whitespace.
|
||||||
|
static bool END_WITH_NULL matchAsm(StringRef s, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, s);
|
||||||
|
s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace.
|
||||||
|
|
||||||
|
while (const char *p = va_arg(ap, const char *)) {
|
||||||
|
StringRef piece(p);
|
||||||
|
if (!s.startswith(piece)) { // Check if the piece matches.
|
||||||
|
va_end(ap);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
s = s.substr(piece.size());
|
||||||
|
StringRef::size_type i = s.find_first_not_of(" \t");
|
||||||
|
if (i == 0) { // We matched a prefix.
|
||||||
|
va_end(ap);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
s = s.substr(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
va_end(ap);
|
||||||
|
return s.empty();
|
||||||
|
}
|
||||||
|
|
||||||
bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
|
bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
|
||||||
InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
|
InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
|
||||||
|
|
||||||
std::string AsmStr = IA->getAsmString();
|
std::string AsmStr = IA->getAsmString();
|
||||||
|
|
||||||
|
IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
|
||||||
|
if (!Ty || Ty->getBitWidth() % 16 != 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
// TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
|
// TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
|
||||||
SmallVector<StringRef, 4> AsmPieces;
|
SmallVector<StringRef, 4> AsmPieces;
|
||||||
SplitString(AsmStr, AsmPieces, ";\n");
|
SplitString(AsmStr, AsmPieces, ";\n");
|
||||||
|
@ -14712,35 +14742,27 @@ bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
|
||||||
switch (AsmPieces.size()) {
|
switch (AsmPieces.size()) {
|
||||||
default: return false;
|
default: return false;
|
||||||
case 1:
|
case 1:
|
||||||
AsmStr = AsmPieces[0];
|
|
||||||
AsmPieces.clear();
|
|
||||||
SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
|
|
||||||
|
|
||||||
// FIXME: this should verify that we are targeting a 486 or better. If not,
|
// FIXME: this should verify that we are targeting a 486 or better. If not,
|
||||||
// we will turn this bswap into something that will be lowered to logical ops
|
// we will turn this bswap into something that will be lowered to logical
|
||||||
// instead of emitting the bswap asm. For now, we don't support 486 or lower
|
// ops instead of emitting the bswap asm. For now, we don't support 486 or
|
||||||
// so don't worry about this.
|
// lower so don't worry about this.
|
||||||
// bswap $0
|
// bswap $0
|
||||||
if (AsmPieces.size() == 2 &&
|
if (matchAsm(AsmPieces[0], "bswap", "$0", NULL) ||
|
||||||
(AsmPieces[0] == "bswap" ||
|
matchAsm(AsmPieces[0], "bswapl", "$0", NULL) ||
|
||||||
AsmPieces[0] == "bswapq" ||
|
matchAsm(AsmPieces[0], "bswapq", "$0", NULL) ||
|
||||||
AsmPieces[0] == "bswapl") &&
|
matchAsm(AsmPieces[0], "bswap", "${0:q}", NULL) ||
|
||||||
(AsmPieces[1] == "$0" ||
|
matchAsm(AsmPieces[0], "bswapl", "${0:q}", NULL) ||
|
||||||
AsmPieces[1] == "${0:q}")) {
|
matchAsm(AsmPieces[0], "bswapq", "${0:q}", NULL)) {
|
||||||
// No need to check constraints, nothing other than the equivalent of
|
// No need to check constraints, nothing other than the equivalent of
|
||||||
// "=r,0" would be valid here.
|
// "=r,0" would be valid here.
|
||||||
IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
|
|
||||||
if (!Ty || Ty->getBitWidth() % 16 != 0)
|
|
||||||
return false;
|
|
||||||
return IntrinsicLowering::LowerToByteSwap(CI);
|
return IntrinsicLowering::LowerToByteSwap(CI);
|
||||||
}
|
}
|
||||||
|
|
||||||
// rorw $$8, ${0:w} --> llvm.bswap.i16
|
// rorw $$8, ${0:w} --> llvm.bswap.i16
|
||||||
if (CI->getType()->isIntegerTy(16) &&
|
if (CI->getType()->isIntegerTy(16) &&
|
||||||
AsmPieces.size() == 3 &&
|
IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
|
||||||
(AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
|
(matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}", NULL) ||
|
||||||
AsmPieces[1] == "$$8," &&
|
matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}", NULL))) {
|
||||||
AsmPieces[2] == "${0:w}" &&
|
|
||||||
IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
|
|
||||||
AsmPieces.clear();
|
AsmPieces.clear();
|
||||||
const std::string &ConstraintsStr = IA->getConstraintString();
|
const std::string &ConstraintsStr = IA->getConstraintString();
|
||||||
SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
|
SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
|
||||||
|
@ -14749,46 +14771,26 @@ bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
|
||||||
AsmPieces[0] == "~{cc}" &&
|
AsmPieces[0] == "~{cc}" &&
|
||||||
AsmPieces[1] == "~{dirflag}" &&
|
AsmPieces[1] == "~{dirflag}" &&
|
||||||
AsmPieces[2] == "~{flags}" &&
|
AsmPieces[2] == "~{flags}" &&
|
||||||
AsmPieces[3] == "~{fpsr}") {
|
AsmPieces[3] == "~{fpsr}")
|
||||||
IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
|
return IntrinsicLowering::LowerToByteSwap(CI);
|
||||||
if (!Ty || Ty->getBitWidth() % 16 != 0)
|
|
||||||
return false;
|
|
||||||
return IntrinsicLowering::LowerToByteSwap(CI);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
if (CI->getType()->isIntegerTy(32) &&
|
if (CI->getType()->isIntegerTy(32) &&
|
||||||
IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
|
IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
|
||||||
SmallVector<StringRef, 4> Words;
|
matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}", NULL) &&
|
||||||
SplitString(AsmPieces[0], Words, " \t,");
|
matchAsm(AsmPieces[1], "rorl", "$$16,", "$0", NULL) &&
|
||||||
if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
|
matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}", NULL)) {
|
||||||
Words[2] == "${0:w}") {
|
AsmPieces.clear();
|
||||||
Words.clear();
|
const std::string &ConstraintsStr = IA->getConstraintString();
|
||||||
SplitString(AsmPieces[1], Words, " \t,");
|
SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
|
||||||
if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
|
std::sort(AsmPieces.begin(), AsmPieces.end());
|
||||||
Words[2] == "$0") {
|
if (AsmPieces.size() == 4 &&
|
||||||
Words.clear();
|
AsmPieces[0] == "~{cc}" &&
|
||||||
SplitString(AsmPieces[2], Words, " \t,");
|
AsmPieces[1] == "~{dirflag}" &&
|
||||||
if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
|
AsmPieces[2] == "~{flags}" &&
|
||||||
Words[2] == "${0:w}") {
|
AsmPieces[3] == "~{fpsr}")
|
||||||
AsmPieces.clear();
|
return IntrinsicLowering::LowerToByteSwap(CI);
|
||||||
const std::string &ConstraintsStr = IA->getConstraintString();
|
|
||||||
SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
|
|
||||||
std::sort(AsmPieces.begin(), AsmPieces.end());
|
|
||||||
if (AsmPieces.size() == 4 &&
|
|
||||||
AsmPieces[0] == "~{cc}" &&
|
|
||||||
AsmPieces[1] == "~{dirflag}" &&
|
|
||||||
AsmPieces[2] == "~{flags}" &&
|
|
||||||
AsmPieces[3] == "~{fpsr}") {
|
|
||||||
IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
|
|
||||||
if (!Ty || Ty->getBitWidth() % 16 != 0)
|
|
||||||
return false;
|
|
||||||
return IntrinsicLowering::LowerToByteSwap(CI);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CI->getType()->isIntegerTy(64)) {
|
if (CI->getType()->isIntegerTy(64)) {
|
||||||
|
@ -14797,23 +14799,10 @@ bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
|
||||||
Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
|
Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
|
||||||
Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
|
Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
|
||||||
// bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
|
// bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
|
||||||
SmallVector<StringRef, 4> Words;
|
if (matchAsm(AsmPieces[0], "bswap", "%eax", NULL) &&
|
||||||
SplitString(AsmPieces[0], Words, " \t");
|
matchAsm(AsmPieces[1], "bswap", "%edx", NULL) &&
|
||||||
if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
|
matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx", NULL))
|
||||||
Words.clear();
|
return IntrinsicLowering::LowerToByteSwap(CI);
|
||||||
SplitString(AsmPieces[1], Words, " \t");
|
|
||||||
if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
|
|
||||||
Words.clear();
|
|
||||||
SplitString(AsmPieces[2], Words, " \t,");
|
|
||||||
if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
|
|
||||||
Words[2] == "%edx") {
|
|
||||||
IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
|
|
||||||
if (!Ty || Ty->getBitWidth() % 16 != 0)
|
|
||||||
return false;
|
|
||||||
return IntrinsicLowering::LowerToByteSwap(CI);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue