move 'loading i32 from string' optimization from instcombine

to libanalysis.  Instcombine shrinking... does this even 
make sense???

llvm-svn: 84840
This commit is contained in:
Chris Lattner 2009-10-22 06:38:35 +00:00
parent 1664a4fd86
commit 51d2f70e32
2 changed files with 35 additions and 40 deletions

View File

@ -24,13 +24,13 @@
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/Intrinsics.h" #include "llvm/Intrinsics.h"
#include "llvm/LLVMContext.h" #include "llvm/LLVMContext.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Target/TargetData.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/GlobalVariable.h"
#include <cerrno> #include <cerrno>
#include <cmath> #include <cmath>
using namespace llvm; using namespace llvm;
@ -111,6 +111,35 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C,
ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
return V; return V;
} }
// Instead of loading constant c string, use corresponding integer value
// directly if string length is small enough.
std::string Str;
if (TD && GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) {
unsigned len = Str.length();
const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
unsigned numBits = Ty->getPrimitiveSizeInBits();
// Replace LI with immediate integer store.
if ((numBits >> 3) == len + 1) {
APInt StrVal(numBits, 0);
APInt SingleChar(numBits, 0);
if (TD->isLittleEndian()) {
for (signed i = len-1; i >= 0; i--) {
SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
StrVal = (StrVal << 8) | SingleChar;
}
} else {
for (unsigned i = 0; i < len; i++) {
SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
StrVal = (StrVal << 8) | SingleChar;
}
// Append NULL at the end.
SingleChar = 0;
StrVal = (StrVal << 8) | SingleChar;
}
return ConstantInt::get(CE->getContext(), StrVal);
}
}
} }
return 0; return 0;
@ -675,15 +704,15 @@ Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
C = UndefValue::get(ATy->getElementType()); C = UndefValue::get(ATy->getElementType());
else else
return 0; return 0;
} else if (const VectorType *PTy = dyn_cast<VectorType>(*I)) { } else if (const VectorType *VTy = dyn_cast<VectorType>(*I)) {
if (CI->getZExtValue() >= PTy->getNumElements()) if (CI->getZExtValue() >= VTy->getNumElements())
return 0; return 0;
if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) if (ConstantVector *CP = dyn_cast<ConstantVector>(C))
C = CP->getOperand(CI->getZExtValue()); C = CP->getOperand(CI->getZExtValue());
else if (isa<ConstantAggregateZero>(C)) else if (isa<ConstantAggregateZero>(C))
C = Constant::getNullValue(PTy->getElementType()); C = Constant::getNullValue(VTy->getElementType());
else if (isa<UndefValue>(C)) else if (isa<UndefValue>(C))
C = UndefValue::get(PTy->getElementType()); C = UndefValue::get(VTy->getElementType());
else else
return 0; return 0;
} else { } else {

View File

@ -11345,40 +11345,6 @@ static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI,
Value *CastOp = CI->getOperand(0); Value *CastOp = CI->getOperand(0);
LLVMContext *Context = IC.getContext(); LLVMContext *Context = IC.getContext();
if (TD) {
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) {
// Instead of loading constant c string, use corresponding integer value
// directly if string length is small enough.
std::string Str;
if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) {
unsigned len = Str.length();
const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
unsigned numBits = Ty->getPrimitiveSizeInBits();
// Replace LI with immediate integer store.
if ((numBits >> 3) == len + 1) {
APInt StrVal(numBits, 0);
APInt SingleChar(numBits, 0);
if (TD->isLittleEndian()) {
for (signed i = len-1; i >= 0; i--) {
SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
StrVal = (StrVal << 8) | SingleChar;
}
} else {
for (unsigned i = 0; i < len; i++) {
SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
StrVal = (StrVal << 8) | SingleChar;
}
// Append NULL at the end.
SingleChar = 0;
StrVal = (StrVal << 8) | SingleChar;
}
Value *NL = ConstantInt::get(*Context, StrVal);
return IC.ReplaceInstUsesWith(LI, NL);
}
}
}
}
const PointerType *DestTy = cast<PointerType>(CI->getType()); const PointerType *DestTy = cast<PointerType>(CI->getType());
const Type *DestPTy = DestTy->getElementType(); const Type *DestPTy = DestTy->getElementType();
if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {