Remove Value::getNameLen

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77148 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2009-07-26 08:34:35 +00:00
parent 92fbbc7588
commit f0443c1eb4
6 changed files with 62 additions and 120 deletions

View File

@ -124,10 +124,6 @@ public:
return getName().end(); return getName().end();
} }
/// getNameLen - Return the length of the string, correctly handling nul
/// characters embedded into them.
unsigned getNameLen() const { return getName().size(); }
/// getName() - Return a constant reference to the value's name. This is cheap /// getName() - Return a constant reference to the value's name. This is cheap
/// and guaranteed to return the same reference as long as the value is not /// and guaranteed to return the same reference as long as the value is not
/// modified. /// modified.

View File

@ -617,64 +617,31 @@ llvm::canConstantFoldCallTo(const Function *F) {
} }
if (!F->hasName()) return false; if (!F->hasName()) return false;
const char *Str = F->getNameStart(); StringRef Name = F->getName();
unsigned Len = F->getNameLen();
// In these cases, the check of the length is required. We don't want to // In these cases, the check of the length is required. We don't want to
// return true for a name like "cos\0blah" which strcmp would return equal to // return true for a name like "cos\0blah" which strcmp would return equal to
// "cos", but has length 8. // "cos", but has length 8.
switch (Str[0]) { switch (Name[0]) {
default: return false; default: return false;
case 'a': case 'a':
if (Len == 4) return Name == "acos" || Name == "asin" ||
return !strcmp(Str, "acos") || !strcmp(Str, "asin") || Name == "atan" || Name == "atan2";
!strcmp(Str, "atan");
else if (Len == 5)
return !strcmp(Str, "atan2");
return false;
case 'c': case 'c':
if (Len == 3) return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
return !strcmp(Str, "cos");
else if (Len == 4)
return !strcmp(Str, "ceil") || !strcmp(Str, "cosf") ||
!strcmp(Str, "cosh");
return false;
case 'e': case 'e':
if (Len == 3) return Name == "exp";
return !strcmp(Str, "exp");
return false;
case 'f': case 'f':
if (Len == 4) return Name == "fabs" || Name == "fmod" || Name == "floor";
return !strcmp(Str, "fabs") || !strcmp(Str, "fmod");
else if (Len == 5)
return !strcmp(Str, "floor");
return false;
break;
case 'l': case 'l':
if (Len == 3 && !strcmp(Str, "log")) return Name == "log" || Name == "log10";
return true;
if (Len == 5 && !strcmp(Str, "log10"))
return true;
return false;
case 'p': case 'p':
if (Len == 3 && !strcmp(Str, "pow")) return Name == "pow";
return true;
return false;
case 's': case 's':
if (Len == 3) return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
return !strcmp(Str, "sin"); Name == "sinf" || Name == "sqrtf";
if (Len == 4)
return !strcmp(Str, "sinh") || !strcmp(Str, "sqrt") ||
!strcmp(Str, "sinf");
if (Len == 5)
return !strcmp(Str, "sqrtf");
return false;
case 't': case 't':
if (Len == 3 && !strcmp(Str, "tan")) return Name == "tan" || Name == "tanh";
return true;
else if (Len == 4 && !strcmp(Str, "tanh"))
return true;
return false;
} }
} }
@ -722,8 +689,7 @@ llvm::ConstantFoldCall(Function *F,
Constant* const* Operands, unsigned NumOperands) { Constant* const* Operands, unsigned NumOperands) {
if (!F->hasName()) return 0; if (!F->hasName()) return 0;
LLVMContext &Context = F->getContext(); LLVMContext &Context = F->getContext();
const char *Str = F->getNameStart(); StringRef Name = F->getName();
unsigned Len = F->getNameLen();
const Type *Ty = F->getReturnType(); const Type *Ty = F->getReturnType();
if (NumOperands == 1) { if (NumOperands == 1) {
@ -736,42 +702,42 @@ llvm::ConstantFoldCall(Function *F,
/// f(arg). Long double not supported yet. /// f(arg). Long double not supported yet.
double V = Ty==Type::FloatTy ? (double)Op->getValueAPF().convertToFloat(): double V = Ty==Type::FloatTy ? (double)Op->getValueAPF().convertToFloat():
Op->getValueAPF().convertToDouble(); Op->getValueAPF().convertToDouble();
switch (Str[0]) { switch (Name[0]) {
case 'a': case 'a':
if (Len == 4 && !strcmp(Str, "acos")) if (Name == "acos")
return ConstantFoldFP(acos, V, Ty, Context); return ConstantFoldFP(acos, V, Ty, Context);
else if (Len == 4 && !strcmp(Str, "asin")) else if (Name == "asin")
return ConstantFoldFP(asin, V, Ty, Context); return ConstantFoldFP(asin, V, Ty, Context);
else if (Len == 4 && !strcmp(Str, "atan")) else if (Name == "atan")
return ConstantFoldFP(atan, V, Ty, Context); return ConstantFoldFP(atan, V, Ty, Context);
break; break;
case 'c': case 'c':
if (Len == 4 && !strcmp(Str, "ceil")) if (Name == "ceil")
return ConstantFoldFP(ceil, V, Ty, Context); return ConstantFoldFP(ceil, V, Ty, Context);
else if (Len == 3 && !strcmp(Str, "cos")) else if (Name == "cos")
return ConstantFoldFP(cos, V, Ty, Context); return ConstantFoldFP(cos, V, Ty, Context);
else if (Len == 4 && !strcmp(Str, "cosh")) else if (Name == "cosh")
return ConstantFoldFP(cosh, V, Ty, Context); return ConstantFoldFP(cosh, V, Ty, Context);
else if (Len == 4 && !strcmp(Str, "cosf")) else if (Name == "cosf")
return ConstantFoldFP(cos, V, Ty, Context); return ConstantFoldFP(cos, V, Ty, Context);
break; break;
case 'e': case 'e':
if (Len == 3 && !strcmp(Str, "exp")) if (Name == "exp")
return ConstantFoldFP(exp, V, Ty, Context); return ConstantFoldFP(exp, V, Ty, Context);
break; break;
case 'f': case 'f':
if (Len == 4 && !strcmp(Str, "fabs")) if (Name == "fabs")
return ConstantFoldFP(fabs, V, Ty, Context); return ConstantFoldFP(fabs, V, Ty, Context);
else if (Len == 5 && !strcmp(Str, "floor")) else if (Name == "floor")
return ConstantFoldFP(floor, V, Ty, Context); return ConstantFoldFP(floor, V, Ty, Context);
break; break;
case 'l': case 'l':
if (Len == 3 && !strcmp(Str, "log") && V > 0) if (Name == "log" && V > 0)
return ConstantFoldFP(log, V, Ty, Context); return ConstantFoldFP(log, V, Ty, Context);
else if (Len == 5 && !strcmp(Str, "log10") && V > 0) else if (Name == "log10" && V > 0)
return ConstantFoldFP(log10, V, Ty, Context); return ConstantFoldFP(log10, V, Ty, Context);
else if (!strcmp(Str, "llvm.sqrt.f32") || else if (Name == "llvm.sqrt.f32" ||
!strcmp(Str, "llvm.sqrt.f64")) { Name == "llvm.sqrt.f64") {
if (V >= -0.0) if (V >= -0.0)
return ConstantFoldFP(sqrt, V, Ty, Context); return ConstantFoldFP(sqrt, V, Ty, Context);
else // Undefined else // Undefined
@ -779,34 +745,34 @@ llvm::ConstantFoldCall(Function *F,
} }
break; break;
case 's': case 's':
if (Len == 3 && !strcmp(Str, "sin")) if (Name == "sin")
return ConstantFoldFP(sin, V, Ty, Context); return ConstantFoldFP(sin, V, Ty, Context);
else if (Len == 4 && !strcmp(Str, "sinh")) else if (Name == "sinh")
return ConstantFoldFP(sinh, V, Ty, Context); return ConstantFoldFP(sinh, V, Ty, Context);
else if (Len == 4 && !strcmp(Str, "sqrt") && V >= 0) else if (Name == "sqrt" && V >= 0)
return ConstantFoldFP(sqrt, V, Ty, Context); return ConstantFoldFP(sqrt, V, Ty, Context);
else if (Len == 5 && !strcmp(Str, "sqrtf") && V >= 0) else if (Name == "sqrtf" && V >= 0)
return ConstantFoldFP(sqrt, V, Ty, Context); return ConstantFoldFP(sqrt, V, Ty, Context);
else if (Len == 4 && !strcmp(Str, "sinf")) else if (Name == "sinf")
return ConstantFoldFP(sin, V, Ty, Context); return ConstantFoldFP(sin, V, Ty, Context);
break; break;
case 't': case 't':
if (Len == 3 && !strcmp(Str, "tan")) if (Name == "tan")
return ConstantFoldFP(tan, V, Ty, Context); return ConstantFoldFP(tan, V, Ty, Context);
else if (Len == 4 && !strcmp(Str, "tanh")) else if (Name == "tanh")
return ConstantFoldFP(tanh, V, Ty, Context); return ConstantFoldFP(tanh, V, Ty, Context);
break; break;
default: default:
break; break;
} }
} else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) { } else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
if (Len > 11 && !memcmp(Str, "llvm.bswap", 10)) if (Name.startswith("llvm.bswap"))
return ConstantInt::get(Context, Op->getValue().byteSwap()); return ConstantInt::get(Context, Op->getValue().byteSwap());
else if (Len > 11 && !memcmp(Str, "llvm.ctpop", 10)) else if (Name.startswith("llvm.ctpop"))
return ConstantInt::get(Ty, Op->getValue().countPopulation()); return ConstantInt::get(Ty, Op->getValue().countPopulation());
else if (Len > 10 && !memcmp(Str, "llvm.cttz", 9)) else if (Name.startswith("llvm.cttz"))
return ConstantInt::get(Ty, Op->getValue().countTrailingZeros()); return ConstantInt::get(Ty, Op->getValue().countTrailingZeros());
else if (Len > 10 && !memcmp(Str, "llvm.ctlz", 9)) else if (Name.startswith("llvm.ctlz"))
return ConstantInt::get(Ty, Op->getValue().countLeadingZeros()); return ConstantInt::get(Ty, Op->getValue().countLeadingZeros());
} }
} else if (NumOperands == 2) { } else if (NumOperands == 2) {
@ -821,18 +787,18 @@ llvm::ConstantFoldCall(Function *F,
(double)Op2->getValueAPF().convertToFloat(): (double)Op2->getValueAPF().convertToFloat():
Op2->getValueAPF().convertToDouble(); Op2->getValueAPF().convertToDouble();
if (Len == 3 && !strcmp(Str, "pow")) { if (Name == "pow") {
return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty, Context); return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty, Context);
} else if (Len == 4 && !strcmp(Str, "fmod")) { } else if (Name == "fmod") {
return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty, Context); return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty, Context);
} else if (Len == 5 && !strcmp(Str, "atan2")) { } else if (Name == "atan2") {
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty, Context); return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty, Context);
} }
} else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) { } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
if (!strcmp(Str, "llvm.powi.f32")) { if (Name == "llvm.powi.f32") {
return Context.getConstantFP(APFloat((float)std::pow((float)Op1V, return Context.getConstantFP(APFloat((float)std::pow((float)Op1V,
(int)Op2C->getZExtValue()))); (int)Op2C->getZExtValue())));
} else if (!strcmp(Str, "llvm.powi.f64")) { } else if (Name == "llvm.powi.f64") {
return Context.getConstantFP(APFloat((double)std::pow((double)Op1V, return Context.getConstantFP(APFloat((double)std::pow((double)Op1V,
(int)Op2C->getZExtValue()))); (int)Op2C->getZExtValue())));
} }

View File

@ -57,9 +57,6 @@ const LibCallFunctionInfo *LibCallInfo::getFunctionInfo(Function *F) const {
} }
// Look up this function in the string map. // Look up this function in the string map.
const char *ValueName = F->getNameStart(); return Map->lookup(F->getName());
StringMap<const LibCallFunctionInfo*>::iterator I =
Map->find(StringRef(ValueName, F->getNameLen()));
return I != Map->end() ? I->second : 0;
} }

View File

@ -800,15 +800,11 @@ bool llvm::CannotBeNegativeZero(const Value *V, unsigned Depth) {
if (const CallInst *CI = dyn_cast<CallInst>(I)) if (const CallInst *CI = dyn_cast<CallInst>(I))
if (const Function *F = CI->getCalledFunction()) { if (const Function *F = CI->getCalledFunction()) {
if (F->isDeclaration()) { if (F->isDeclaration()) {
switch (F->getNameLen()) { // abs(x) != -0.0
case 3: // abs(x) != -0.0 if (F->getName() == "abs") return true;
if (!strcmp(F->getNameStart(), "abs")) return true; // abs[lf](x) != -0.0
break; if (F->getName() == "absf") return true;
case 4: // abs[lf](x) != -0.0 if (F->getName() == "absl") return true;
if (!strcmp(F->getNameStart(), "absf")) return true;
if (!strcmp(F->getNameStart(), "absl")) return true;
break;
}
} }
} }

View File

@ -4439,12 +4439,9 @@ void SelectionDAGLowering::visitCall(CallInst &I) {
// Check for well-known libc/libm calls. If the function is internal, it // Check for well-known libc/libm calls. If the function is internal, it
// can't be a library call. // can't be a library call.
unsigned NameLen = F->getNameLen(); if (!F->hasLocalLinkage() && F->hasName()) {
if (!F->hasLocalLinkage() && NameLen) { StringRef Name = F->getName();
const char *NameStr = F->getNameStart(); if (Name == "copysign" || Name == "copysignf") {
if (NameStr[0] == 'c' &&
((NameLen == 8 && !strcmp(NameStr, "copysign")) ||
(NameLen == 9 && !strcmp(NameStr, "copysignf")))) {
if (I.getNumOperands() == 3 && // Basic sanity checks. if (I.getNumOperands() == 3 && // Basic sanity checks.
I.getOperand(1)->getType()->isFloatingPoint() && I.getOperand(1)->getType()->isFloatingPoint() &&
I.getType() == I.getOperand(1)->getType() && I.getType() == I.getOperand(1)->getType() &&
@ -4455,10 +4452,7 @@ void SelectionDAGLowering::visitCall(CallInst &I) {
LHS.getValueType(), LHS, RHS)); LHS.getValueType(), LHS, RHS));
return; return;
} }
} else if (NameStr[0] == 'f' && } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
((NameLen == 4 && !strcmp(NameStr, "fabs")) ||
(NameLen == 5 && !strcmp(NameStr, "fabsf")) ||
(NameLen == 5 && !strcmp(NameStr, "fabsl")))) {
if (I.getNumOperands() == 2 && // Basic sanity checks. if (I.getNumOperands() == 2 && // Basic sanity checks.
I.getOperand(1)->getType()->isFloatingPoint() && I.getOperand(1)->getType()->isFloatingPoint() &&
I.getType() == I.getOperand(1)->getType()) { I.getType() == I.getOperand(1)->getType()) {
@ -4467,10 +4461,7 @@ void SelectionDAGLowering::visitCall(CallInst &I) {
Tmp.getValueType(), Tmp)); Tmp.getValueType(), Tmp));
return; return;
} }
} else if (NameStr[0] == 's' && } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
((NameLen == 3 && !strcmp(NameStr, "sin")) ||
(NameLen == 4 && !strcmp(NameStr, "sinf")) ||
(NameLen == 4 && !strcmp(NameStr, "sinl")))) {
if (I.getNumOperands() == 2 && // Basic sanity checks. if (I.getNumOperands() == 2 && // Basic sanity checks.
I.getOperand(1)->getType()->isFloatingPoint() && I.getOperand(1)->getType()->isFloatingPoint() &&
I.getType() == I.getOperand(1)->getType()) { I.getType() == I.getOperand(1)->getType()) {
@ -4479,10 +4470,7 @@ void SelectionDAGLowering::visitCall(CallInst &I) {
Tmp.getValueType(), Tmp)); Tmp.getValueType(), Tmp));
return; return;
} }
} else if (NameStr[0] == 'c' && } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
((NameLen == 3 && !strcmp(NameStr, "cos")) ||
(NameLen == 4 && !strcmp(NameStr, "cosf")) ||
(NameLen == 4 && !strcmp(NameStr, "cosl")))) {
if (I.getNumOperands() == 2 && // Basic sanity checks. if (I.getNumOperands() == 2 && // Basic sanity checks.
I.getOperand(1)->getType()->isFloatingPoint() && I.getOperand(1)->getType()->isFloatingPoint() &&
I.getType() == I.getOperand(1)->getType()) { I.getType() == I.getOperand(1)->getType()) {

View File

@ -31,6 +31,7 @@
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
#include "llvm/Support/Compiler.h" #include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Config/config.h" #include "llvm/Config/config.h"
using namespace llvm; using namespace llvm;
@ -1655,20 +1656,18 @@ bool SimplifyLibCalls::runOnFunction(Function &F) {
continue; continue;
// Ignore unknown calls. // Ignore unknown calls.
const char *CalleeName = Callee->getNameStart(); LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
StringMap<LibCallOptimization*>::iterator OMI = if (!LCO) continue;
Optimizations.find(StringRef(CalleeName, Callee->getNameLen()));
if (OMI == Optimizations.end()) continue;
// Set the builder to the instruction after the call. // Set the builder to the instruction after the call.
Builder.SetInsertPoint(BB, I); Builder.SetInsertPoint(BB, I);
// Try to optimize this call. // Try to optimize this call.
Value *Result = OMI->second->OptimizeCall(CI, TD, Builder); Value *Result = LCO->OptimizeCall(CI, TD, Builder);
if (Result == 0) continue; if (Result == 0) continue;
DEBUG(DOUT << "SimplifyLibCalls simplified: " << *CI; DEBUG(errs() << "SimplifyLibCalls simplified: " << *CI;
DOUT << " into: " << *Result << "\n"); errs() << " into: " << *Result << "\n");
// Something changed! // Something changed!
Changed = true; Changed = true;