[llvm-commits] [llvm] r84841 - in /llvm/trunk/lib: Analysis/ConstantFolding.cpp Transforms/Scalar/InstructionCombining.cpp

Chris Lattner sabre at nondot.org
Wed Oct 21 23:44:08 PDT 2009


Author: lattner
Date: Thu Oct 22 01:44:07 2009
New Revision: 84841

URL: http://llvm.org/viewvc/llvm-project?rev=84841&view=rev
Log:
move another load optimization from instcombine -> libanalysis.
 

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

Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=84841&r1=84840&r2=84841&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Thu Oct 22 01:44:07 2009
@@ -103,45 +103,59 @@
     if (GV->isConstant() && GV->hasDefinitiveInitializer())
       return GV->getInitializer();
 
-  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
-    if (CE->getOpcode() == Instruction::GetElementPtr) {
-      if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
-        if (GV->isConstant() && GV->hasDefinitiveInitializer())
-          if (Constant *V = 
-               ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
-            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;
+  // If the loaded value isn't a constant expr, we can't handle it.
+  ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
+  if (!CE) return 0;
+  
+  if (CE->getOpcode() == Instruction::GetElementPtr) {
+    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
+      if (GV->isConstant() && GV->hasDefinitiveInitializer())
+        if (Constant *V = 
+             ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
+          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;
         }
-        return ConstantInt::get(CE->getContext(), StrVal);
-      }
+      } 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);
     }
   }
-
+  
+  // If this load comes from anywhere in a constant global, and if the global
+  // is all undef or zero, we know what it loads.
+  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getUnderlyingObject())){
+    if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
+      const Type *ResTy = cast<PointerType>(C->getType())->getElementType();
+      if (GV->getInitializer()->isNullValue())
+        return Constant::getNullValue(ResTy);
+      if (isa<UndefValue>(GV->getInitializer()))
+        return UndefValue::get(ResTy);
+    }
+  }
+  
   return 0;
 }
 

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

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Thu Oct 22 01:44:07 2009
@@ -11364,7 +11364,8 @@
         if (Constant *CSrc = dyn_cast<Constant>(CastOp))
           if (ASrcTy->getNumElements() != 0) {
             Value *Idxs[2];
-            Idxs[0] = Idxs[1] = Constant::getNullValue(Type::getInt32Ty(*Context));
+            Idxs[0] = Constant::getNullValue(Type::getInt32Ty(*Context));
+            Idxs[1] = Idxs[0];
             CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
             SrcTy = cast<PointerType>(CastOp->getType());
             SrcPTy = SrcTy->getElementType();
@@ -11453,17 +11454,6 @@
       if (Instruction *Res = InstCombineLoadCast(*this, LI, TD))
         return Res;
   
-  // If this load comes from anywhere in a constant global, and if the global
-  // is all undef or zero, we know what it loads.
-  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op->getUnderlyingObject())){
-    if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
-      if (GV->getInitializer()->isNullValue())
-        return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
-      else if (isa<UndefValue>(GV->getInitializer()))
-        return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
-    }
-  }
-
   if (Op->hasOneUse()) {
     // Change select and PHI nodes to select values instead of addresses: this
     // helps alias analysis out a lot, allows many others simplifications, and





More information about the llvm-commits mailing list