[llvm-commits] [llvm] r43148 - /llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Devang Patel dpatel at apple.com
Thu Oct 18 12:52:33 PDT 2007


Author: dpatel
Date: Thu Oct 18 14:52:32 2007
New Revision: 43148

URL: http://llvm.org/viewvc/llvm-project?rev=43148&view=rev
Log:
Try again.
Instead of loading small global string from memory, use
integer constant.

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=43148&r1=43147&r2=43148&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Thu Oct 18 14:52:32 2007
@@ -8941,10 +8941,43 @@
 
 
 /// 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<User>(LI.getOperand(0));
   Value *CastOp = CI->getOperand(0);
 
+  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(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<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];
+	    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<PointerType>(CI->getType())->getElementType();
   if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) {
     const Type *SrcPTy = SrcTy->getElementType();
@@ -9050,7 +9083,7 @@
 
   // load (cast X) --> cast (load X) iff safe
   if (isa<CastInst>(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 @@
         }
 
       } else if (CE->isCast()) {
-        if (Instruction *Res = InstCombineLoadCast(*this, LI))
+        if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
           return Res;
       }
   }





More information about the llvm-commits mailing list