From 5d52135a14da7ed417122b65e5493d9f15f2423c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 29 Nov 2006 01:48:01 +0000 Subject: [PATCH] Upgrade the ugly darwin 64-bit bswap idiom (bswap %eax / bswap %edx / xchgl %eax, %edx) to llvm.bswap.i64. This compiles: long long test2(long long A) { return _OSSwapInt64(A); } to: _test2: movl 8(%esp), %eax movl 4(%esp), %edx bswapl %eax bswapl %edx ret instead of: _test2: movl 8(%esp), %edx movl 4(%esp), %eax bswap %eax bswap %edx xchgl %eax, %edx ret GCC manages (with -fomit-frame-pointer) the uglier: _test2: subl $4, %esp movl 8(%esp), %eax movl 12(%esp), %edx bswap %eax bswap %edx xchgl %eax, %edx addl $4, %esp ret git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32001 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/X86/X86TargetAsmInfo.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/Target/X86/X86TargetAsmInfo.cpp b/lib/Target/X86/X86TargetAsmInfo.cpp index 32d918efec1..4d44b7d591e 100644 --- a/lib/Target/X86/X86TargetAsmInfo.cpp +++ b/lib/Target/X86/X86TargetAsmInfo.cpp @@ -199,7 +199,7 @@ bool X86TargetAsmInfo::LowerToBSwap(CallInst *CI) const { bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const { InlineAsm *IA = cast(CI->getCalledValue()); - //std::vector Constraints = IA->ParseConstraints(); + std::vector Constraints = IA->ParseConstraints(); std::string AsmStr = IA->getAsmString(); @@ -214,6 +214,7 @@ bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const { AsmPieces.clear(); SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace. + // bswap $0 if (AsmPieces.size() == 2 && AsmPieces[0] == "bswap" && AsmPieces[1] == "$0") { // No need to check constraints, nothing other than the equivalent of @@ -221,6 +222,27 @@ bool X86TargetAsmInfo::ExpandInlineAsm(CallInst *CI) const { return LowerToBSwap(CI); } break; + case 3: + if (CI->getType() == Type::ULongTy && Constraints.size() >= 2 && + Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" && + Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") { + // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64 + std::vector Words; + SplitString(AsmPieces[0], Words, " \t"); + if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") { + Words.clear(); + 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") { + return LowerToBSwap(CI); + } + } + } + } + break; } return false; }