This transform requires valid TargetData info. Wrap it in 'if (TD)' in

preparation for the day we use null TargetData when no target is specified.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71210 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Nick Lewycky
2009-05-08 06:47:37 +00:00
parent 45e0010e14
commit 48f95ad971

View File

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