From: Devang Patel Date: Thu, 18 Oct 2007 19:52:32 +0000 (+0000) Subject: Try again. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=99db6add3d3185c06acb1785d6d685f3e680aa0d;p=oota-llvm.git Try again. Instead of loading small global string from memory, use integer constant. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43148 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 29223daab7a..fb7b8d881d7 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -8941,10 +8941,43 @@ Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. -static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI) { +static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, + const TargetData *TD) { User *CI = cast(LI.getOperand(0)); Value *CastOp = CI->getOperand(0); + if (ConstantExpr *CE = dyn_cast(CI)) { + // Instead of loading constant c string, use corresponding integer value + // directly if string length is small enough. + const std::string &Str = CE->getOperand(0)->getStringValue(); + if (!Str.empty()) { + unsigned len = Str.length(); + const Type *Ty = cast(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]; + StrVal = (StrVal << 8) | SingleChar; + } + } else { + for (unsigned i = 0; i < len; i++) { + SingleChar = (uint64_t) Str[i]; + 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); + } + } + } + const Type *DestPTy = cast(CI->getType())->getElementType(); if (const PointerType *SrcTy = dyn_cast(CastOp->getType())) { const Type *SrcPTy = SrcTy->getElementType(); @@ -9050,7 +9083,7 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { // load (cast X) --> cast (load X) iff safe if (isa(Op)) - if (Instruction *Res = InstCombineLoadCast(*this, LI)) + if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) return Res; // None of the following transforms are legal for volatile loads. @@ -9114,7 +9147,7 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { } } else if (CE->isCast()) { - if (Instruction *Res = InstCombineLoadCast(*this, LI)) + if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) return Res; } }