[llvm-commits] [llvm] r49967 - in /llvm/trunk: lib/VMCore/ConstantFold.cpp test/Assembler/ConstantExprFold.llx

Chris Lattner sabre at nondot.org
Sat Apr 19 15:17:26 PDT 2008


Author: lattner
Date: Sat Apr 19 17:17:26 2008
New Revision: 49967

URL: http://llvm.org/viewvc/llvm-project?rev=49967&view=rev
Log:
Implement PR2206.

Modified:
    llvm/trunk/lib/VMCore/ConstantFold.cpp
    llvm/trunk/test/Assembler/ConstantExprFold.llx

Modified: llvm/trunk/lib/VMCore/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantFold.cpp?rev=49967&r1=49966&r2=49967&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/ConstantFold.cpp (original)
+++ llvm/trunk/lib/VMCore/ConstantFold.cpp Sat Apr 19 17:17:26 2008
@@ -548,8 +548,8 @@
       if (CI2->isAllOnesValue())
         return const_cast<Constant*>(C1);                     // X & -1 == X
       
-      // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
       if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
+        // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
         if (CE1->getOpcode() == Instruction::ZExt) {
           unsigned DstWidth = CI2->getType()->getBitWidth();
           unsigned SrcWidth =
@@ -559,16 +559,25 @@
             return const_cast<Constant*>(C1);
         }
         
+        // If and'ing the address of a global with a constant, fold it.
         if (CE1->getOpcode() == Instruction::PtrToInt && 
             isa<GlobalValue>(CE1->getOperand(0))) {
-          GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
+          GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
         
-          // Functions are at least 4-byte aligned.  If and'ing the address of a
-          // function with a constant < 4, fold it to zero.
-          if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
-            if (CI->getValue().ult(APInt(CI->getType()->getBitWidth(),4)) && 
-                isa<Function>(CPR))
-              return Constant::getNullValue(CI->getType());
+          // Functions are at least 4-byte aligned.
+          unsigned GVAlign = GV->getAlignment();
+          if (isa<Function>(GV))
+            GVAlign = std::max(GVAlign, 4U);
+          
+          if (GVAlign > 1) {
+            unsigned DstWidth = CI2->getType()->getBitWidth();
+            unsigned SrcWidth = std::min(SrcWidth, Log2_32(GVAlign));
+            APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
+
+            // If checking bits we know are clear, return zero.
+            if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
+              return Constant::getNullValue(CI2->getType());
+          }
         }
       }
       break;

Modified: llvm/trunk/test/Assembler/ConstantExprFold.llx
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/ConstantExprFold.llx?rev=49967&r1=49966&r2=49967&view=diff

==============================================================================
--- llvm/trunk/test/Assembler/ConstantExprFold.llx (original)
+++ llvm/trunk/test/Assembler/ConstantExprFold.llx Sat Apr 19 17:17:26 2008
@@ -24,3 +24,7 @@
                    i32* getelementptr (%Ty* @B, i64 0, i32 1))            ; true
 ;global i1 icmp ne (i64* @A, i64* bitcast (%Ty* @B to i64*))                 ; true
 
+; PR2206
+ at cons = weak global i32 0, align 8              ; <i32*> [#uses=1]
+global i64 and (i64 ptrtoint (i32* @cons to i64), i64 7)
+





More information about the llvm-commits mailing list