[llvm-commits] [llvm-gcc-4.2] r73354 - in /llvm-gcc-4.2/trunk/gcc: llvm-convert.cpp llvm-internal.h

Duncan Sands baldrick at free.fr
Sun Jun 14 15:25:11 PDT 2009


Author: baldrick
Date: Sun Jun 14 17:25:11 2009
New Revision: 73354

URL: http://llvm.org/viewvc/llvm-project?rev=73354&view=rev
Log:
Store the log of the alignment rather than the
actual alignment in LValue/MemRef.  Previously
LValue could store at most an alignment of 128,
resulting in crashes and other strangeness for
larger alignments.  This fixes PR4332.

Modified:
    llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp
    llvm-gcc-4.2/trunk/gcc/llvm-internal.h

Modified: llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp?rev=73354&r1=73353&r2=73354&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp Sun Jun 14 17:25:11 2009
@@ -947,7 +947,7 @@
 /// the address of the result.
 LValue TreeToLLVM::EmitLV(tree exp) {
   // Needs to be in sync with EmitVIEW_CONVERT_EXPR.
-  LValue LV(0, 0);
+  LValue LV;
 
   switch (TREE_CODE(exp)) {
   default:
@@ -982,7 +982,7 @@
   // Constants.
   case LABEL_DECL: {
     Value *Ptr = TreeConstantToLLVM::EmitLV_LABEL_DECL(exp);
-    LV = LValue(Ptr, DECL_ALIGN(exp) / 8);
+    LV = LValue(Ptr, 1);
     break;
   }
   case COMPLEX_CST: {
@@ -1180,7 +1180,7 @@
   const Type *ElTy =
     cast<PointerType>(DestLoc.Ptr->getType())->getElementType();
 
-  unsigned Alignment = std::min(DestLoc.Alignment, SrcLoc.Alignment);
+  unsigned Alignment = std::min(DestLoc.getAlignment(), SrcLoc.getAlignment());
 
   if (ElTy->isSingleValueType()) {
     LoadInst *V = Builder.CreateLoad(SrcLoc.Ptr, SrcLoc.Volatile);
@@ -1291,7 +1291,7 @@
 
   Value *TypeSize = Emit(TYPE_SIZE_UNIT(type), 0);
   EmitMemCpy(DestLoc.Ptr, SrcLoc.Ptr, TypeSize,
-             std::min(DestLoc.Alignment, SrcLoc.Alignment));
+             std::min(DestLoc.getAlignment(), SrcLoc.getAlignment()));
 }
 
 /// ZeroAggregate - Recursively traverse the potentially aggregate DestLoc,
@@ -1302,12 +1302,13 @@
   if (ElTy->isSingleValueType()) {
     StoreInst *St = Builder.CreateStore(Constant::getNullValue(ElTy),
                                         DestLoc.Ptr, DestLoc.Volatile);
-    St->setAlignment(DestLoc.Alignment);
+    St->setAlignment(DestLoc.getAlignment());
   } else if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
     const StructLayout *SL = getTargetData().getStructLayout(STy);
     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
       Value *Ptr = Builder.CreateStructGEP(DestLoc.Ptr, i);
-      unsigned Alignment = MinAlign(DestLoc.Alignment, SL->getElementOffset(i));
+      unsigned Alignment = MinAlign(DestLoc.getAlignment(),
+                                    SL->getElementOffset(i));
       ZeroAggregate(MemRef(Ptr, Alignment, DestLoc.Volatile), Builder);
     }
   } else {
@@ -1315,7 +1316,7 @@
     unsigned EltSize = getTargetData().getTypeAllocSize(ATy->getElementType());
     for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
       Value *Ptr = Builder.CreateStructGEP(DestLoc.Ptr, i);
-      unsigned Alignment = MinAlign(DestLoc.Alignment, i * EltSize);
+      unsigned Alignment = MinAlign(DestLoc.getAlignment(), i * EltSize);
       ZeroAggregate(MemRef(Ptr, Alignment, DestLoc.Volatile), Builder);
     }
   }
@@ -1341,7 +1342,7 @@
   }
 
   EmitMemSet(DestLoc.Ptr, ConstantInt::get(Type::Int8Ty, 0),
-             Emit(TYPE_SIZE_UNIT(type), 0), DestLoc.Alignment);
+             Emit(TYPE_SIZE_UNIT(type), 0), DestLoc.getAlignment());
 }
 
 Value *TreeToLLVM::EmitMemCpy(Value *DestPtr, Value *SrcPtr, Value *Size,
@@ -2730,7 +2731,7 @@
   }
   Ptr = BitCastToType(Ptr, PointerType::getUnqual(Call->getType()));
   StoreInst *St = Builder.CreateStore(Call, Ptr, DestLoc->Volatile);
-  St->setAlignment(DestLoc->Alignment);
+  St->setAlignment(DestLoc->getAlignment());
   return 0;
 }
 
@@ -2982,7 +2983,7 @@
   Value *Ptr = BitCastToType(DestLoc->Ptr, 
                              PointerType::getUnqual(OpVal->getType()));
   StoreInst *St = Builder.CreateStore(OpVal, Ptr, DestLoc->Volatile);
-  St->setAlignment(DestLoc->Alignment);
+  St->setAlignment(DestLoc->getAlignment());
   return 0;
 }
 
@@ -3059,7 +3060,7 @@
     Value *Ptr = BitCastToType(DestLoc->Ptr,
                                PointerType::getUnqual(OpVal->getType()));
     StoreInst *St = Builder.CreateStore(OpVal, Ptr, DestLoc->Volatile);
-    St->setAlignment(DestLoc->Alignment);
+    St->setAlignment(DestLoc->getAlignment());
     return 0;
   }
 
@@ -5719,12 +5720,12 @@
                                      MemRef SrcComplex) {
   Value *RealPtr = Builder.CreateStructGEP(SrcComplex.Ptr, 0, "real");
   Real = Builder.CreateLoad(RealPtr, SrcComplex.Volatile, "real");
-  cast<LoadInst>(Real)->setAlignment(SrcComplex.Alignment);
+  cast<LoadInst>(Real)->setAlignment(SrcComplex.getAlignment());
 
   Value *ImagPtr = Builder.CreateStructGEP(SrcComplex.Ptr, 1, "imag");
   Imag = Builder.CreateLoad(ImagPtr, SrcComplex.Volatile, "imag");
   cast<LoadInst>(Imag)->setAlignment(
-    MinAlign(SrcComplex.Alignment, TD.getTypeAllocSize(Real->getType()))
+    MinAlign(SrcComplex.getAlignment(), TD.getTypeAllocSize(Real->getType()))
   );
 }
 
@@ -5734,12 +5735,12 @@
 
   Value *RealPtr = Builder.CreateStructGEP(DestComplex.Ptr, 0, "real");
   St = Builder.CreateStore(Real, RealPtr, DestComplex.Volatile);
-  St->setAlignment(DestComplex.Alignment);
+  St->setAlignment(DestComplex.getAlignment());
 
   Value *ImagPtr = Builder.CreateStructGEP(DestComplex.Ptr, 1, "imag");
   St = Builder.CreateStore(Imag, ImagPtr, DestComplex.Volatile);
   St->setAlignment(
-    MinAlign(DestComplex.Alignment, TD.getTypeAllocSize(Real->getType()))
+    MinAlign(DestComplex.getAlignment(), TD.getTypeAllocSize(Real->getType()))
   );
 }
 
@@ -5949,7 +5950,7 @@
     LValue ArrayAddrLV = EmitLV(Array);
     assert(!ArrayAddrLV.isBitfield() && "Arrays cannot be bitfields!");
     ArrayAddr = ArrayAddrLV.Ptr;
-    ArrayAlign = ArrayAddrLV.Alignment;
+    ArrayAlign = ArrayAddrLV.getAlignment();
   } else {
     ArrayAddr = Emit(Array, 0);
     if (TREE_CODE (ArrayTreeType) == POINTER_TYPE)
@@ -6095,7 +6096,7 @@
 LValue TreeToLLVM::EmitLV_COMPONENT_REF(tree exp) {
   LValue StructAddrLV = EmitLV(TREE_OPERAND(exp, 0));
   tree FieldDecl = TREE_OPERAND(exp, 1); 
-  unsigned LVAlign = StructAddrLV.Alignment;
+  unsigned LVAlign = StructAddrLV.getAlignment();
  
   assert((TREE_CODE(DECL_CONTEXT(FieldDecl)) == RECORD_TYPE ||
           TREE_CODE(DECL_CONTEXT(FieldDecl)) == UNION_TYPE  ||
@@ -6320,7 +6321,7 @@
   // If this is referring to the whole field, return the whole thing.
   if (BitStart == 0 && BitSize == ValueSizeInBits) {
     return LValue(BitCastToType(Ptr.Ptr, PointerType::getUnqual(ValTy)),
-                  Ptr.Alignment);
+                  Ptr.getAlignment());
   }
   
   return LValue(BitCastToType(Ptr.Ptr, PointerType::getUnqual(ValTy)), 1,
@@ -6334,10 +6335,10 @@
   unsigned Alignment;
   if (Idx == 0)
     // REALPART alignment is same as the complex operand.
-    Alignment = Ptr.Alignment;
+    Alignment = Ptr.getAlignment();
   else
     // IMAGPART alignment = MinAlign(Ptr.Alignment, sizeof field);
-    Alignment = MinAlign(Ptr.Alignment,
+    Alignment = MinAlign(Ptr.getAlignment(),
                          TD.getTypeAllocSize(Ptr.Ptr->getType()));
   return LValue(Builder.CreateStructGEP(Ptr.Ptr, Idx), Alignment);
 }
@@ -6452,7 +6453,7 @@
       Value *Ptr = BitCastToType(DestLoc->Ptr, 
                                  PointerType::getUnqual(V->getType()));
       StoreInst *St = Builder.CreateStore(V, Ptr, DestLoc->Volatile);
-      St->setAlignment(DestLoc->Alignment);
+      St->setAlignment(DestLoc->getAlignment());
     }
     break;
   }

Modified: llvm-gcc-4.2/trunk/gcc/llvm-internal.h
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/llvm-internal.h?rev=73354&r1=73353&r2=73354&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-internal.h (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-internal.h Sun Jun 14 17:25:11 2009
@@ -38,6 +38,7 @@
 #include "llvm/ADT/SetVector.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/IRBuilder.h"
+#include "llvm/Support/MathExtras.h"
 #include "llvm/Support/Streams.h"
 #include "llvm/Support/TargetFolder.h"
 
@@ -233,14 +234,20 @@
 /// a pointer to the memory, its alignment and whether the access is volatile.
 struct MemRef {
   Value *Ptr;
-  unsigned Alignment;
   bool Volatile;
+private:
+  unsigned char LogAlign;
+
+public:
+  MemRef() : Ptr(0), Volatile(false), LogAlign(0) {}
+  MemRef(Value *P, uint32_t A, bool V) : Ptr(P), Volatile(V) {
+    // Forbid alignment 0 along with non-power-of-2 alignment values.
+    assert(isPowerOf2_32(A) && "Alignment not a power of 2!");
+    LogAlign = Log2_32(A);
+  }
 
-  MemRef() : Ptr(0), Alignment(0), Volatile(false) {}
-  MemRef(Value *P, unsigned A, bool V)
-    : Ptr(P), Alignment(A), Volatile(V) {
-      // Allowing alignment 0 would complicate calculations, so forbid it.
-      assert(A && !(A & (A - 1)) && "Alignment not a power of 2!");
+  uint32_t getAlignment() const {
+    return 1U << LogAlign;
   }
 };
 
@@ -253,21 +260,29 @@
 /// "LValue" is intended to be a light-weight object passed around by-value.
 struct LValue {
   Value *Ptr;
-  unsigned char Alignment;
   unsigned char BitStart;
   unsigned char BitSize;
-  
-  LValue(Value *P, unsigned Align)
-    : Ptr(P), Alignment(Align), BitStart(255), BitSize(255) {}
-  LValue(Value *P, unsigned Align, unsigned BSt, unsigned BSi) 
-  : Ptr(P), Alignment(Align), BitStart(BSt), BitSize(BSi) {
-      assert(BitStart == BSt && BitSize == BSi &&
-             "Bit values larger than 256?");
-    }
-
-  unsigned getAlignment() const {
-    assert(Alignment && "LValue alignment cannot be zero!");
-    return Alignment;
+private:
+  unsigned char LogAlign;
+
+public:
+  LValue() : Ptr(0), BitStart(255), BitSize(255), LogAlign(0) {}
+  LValue(Value *P, uint32_t A) : Ptr(P), BitStart(255), BitSize(255) {
+    // Forbid alignment 0 along with non-power-of-2 alignment values.
+    assert(isPowerOf2_32(A) && "Alignment not a power of 2!");
+    LogAlign = Log2_32(A);
+  }
+  LValue(Value *P, uint32_t A, unsigned BSt, unsigned BSi)
+  : Ptr(P), BitStart(BSt), BitSize(BSi) {
+    assert(BitStart == BSt && BitSize == BSi &&
+           "Bit values larger than 256?");
+    // Forbid alignment 0 along with non-power-of-2 alignment values.
+    assert(isPowerOf2_32(A) && "Alignment not a power of 2!");
+    LogAlign = Log2_32(A);
+  }
+
+  uint32_t getAlignment() const {
+    return 1U << LogAlign;
   }
   bool isBitfield() const { return BitStart != 255; }
 };





More information about the llvm-commits mailing list