llvm-6502/lib/Support/Twine.cpp
Chris Lattner ea03e10fac Implement rdar://7415680 - Twine integer support lacks greatness
Microoptimize Twine's with unsigned and int to not pin their value to
the stack.  This saves stack space in common cases and allows mem2reg
in the caller.  A simple example is:

void foo(const Twine &);
void bar(int x) {
  foo("xyz: " + Twine(x));
}

Before:

__Z3bari:
	subq	$40, %rsp
	movl	%edi, 36(%rsp)
	leaq	L_.str3(%rip), %rax
	leaq	36(%rsp), %rcx
	leaq	8(%rsp), %rdi
	movq	%rax, 8(%rsp)
	movq	%rcx, 16(%rsp)
	movb	$3, 24(%rsp)
	movb	$7, 25(%rsp)
	callq	__Z3fooRKN4llvm5TwineE
	addq	$40, %rsp
	ret

After:

__Z3bari:
	subq	$24, %rsp
	leaq	L_.str3(%rip), %rax
	movq	%rax, (%rsp)
	movslq	%edi, %rax
	movq	%rax, 8(%rsp)
	movb	$3, 16(%rsp)
	movb	$7, 17(%rsp)
	leaq	(%rsp), %rdi
	callq	__Z3fooRKN4llvm5TwineE
	addq	$24, %rsp
	ret

It saves 16 bytes of stack and one instruction in this case.




git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@103107 91177308-0d34-0410-b5e6-96231b3b80d8
2010-05-05 18:40:33 +00:00

141 lines
3.7 KiB
C++

//===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
std::string Twine::str() const {
SmallString<256> Vec;
return toStringRef(Vec).str();
}
void Twine::toVector(SmallVectorImpl<char> &Out) const {
raw_svector_ostream OS(Out);
print(OS);
}
StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
if (isSingleStringRef())
return getSingleStringRef();
toVector(Out);
return StringRef(Out.data(), Out.size());
}
void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
NodeKind Kind) const {
switch (Kind) {
case Twine::NullKind: break;
case Twine::EmptyKind: break;
case Twine::TwineKind:
static_cast<const Twine*>(Ptr)->print(OS);
break;
case Twine::CStringKind:
OS << static_cast<const char*>(Ptr);
break;
case Twine::StdStringKind:
OS << *static_cast<const std::string*>(Ptr);
break;
case Twine::StringRefKind:
OS << *static_cast<const StringRef*>(Ptr);
break;
case Twine::DecUIKind:
OS << (unsigned)(uintptr_t)Ptr;
break;
case Twine::DecIKind:
OS << (int)(intptr_t)Ptr;
break;
case Twine::DecULKind:
OS << *static_cast<const unsigned long*>(Ptr);
break;
case Twine::DecLKind:
OS << *static_cast<const long*>(Ptr);
break;
case Twine::DecULLKind:
OS << *static_cast<const unsigned long long*>(Ptr);
break;
case Twine::DecLLKind:
OS << *static_cast<const long long*>(Ptr);
break;
case Twine::UHexKind:
OS.write_hex(*static_cast<const uint64_t*>(Ptr));
break;
}
}
void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
NodeKind Kind) const {
switch (Kind) {
case Twine::NullKind:
OS << "null"; break;
case Twine::EmptyKind:
OS << "empty"; break;
case Twine::TwineKind:
OS << "rope:";
static_cast<const Twine*>(Ptr)->printRepr(OS);
break;
case Twine::CStringKind:
OS << "cstring:\""
<< static_cast<const char*>(Ptr) << "\"";
break;
case Twine::StdStringKind:
OS << "std::string:\""
<< static_cast<const std::string*>(Ptr) << "\"";
break;
case Twine::StringRefKind:
OS << "stringref:\""
<< static_cast<const StringRef*>(Ptr) << "\"";
break;
case Twine::DecUIKind:
OS << "decUI:\"" << (unsigned)(uintptr_t)Ptr << "\"";
break;
case Twine::DecIKind:
OS << "decI:\"" << (int)(intptr_t)Ptr << "\"";
break;
case Twine::DecULKind:
OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
break;
case Twine::DecLKind:
OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
break;
case Twine::DecULLKind:
OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
break;
case Twine::DecLLKind:
OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
break;
case Twine::UHexKind:
OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
break;
}
}
void Twine::print(raw_ostream &OS) const {
printOneChild(OS, LHS, getLHSKind());
printOneChild(OS, RHS, getRHSKind());
}
void Twine::printRepr(raw_ostream &OS) const {
OS << "(Twine ";
printOneChildRepr(OS, LHS, getLHSKind());
OS << " ";
printOneChildRepr(OS, RHS, getRHSKind());
OS << ")";
}
void Twine::dump() const {
print(llvm::dbgs());
}
void Twine::dumpRepr() const {
printRepr(llvm::dbgs());
}