[cfe-commits] r64191 - in /cfe/trunk/lib/CodeGen: CGExpr.cpp CodeGenFunction.h

Daniel Dunbar daniel at zuster.org
Mon Feb 9 16:57:50 PST 2009


Author: ddunbar
Date: Mon Feb  9 18:57:50 2009
New Revision: 64191

URL: http://llvm.org/viewvc/llvm-project?rev=64191&view=rev
Log:
Add util Emit{LoadOf,StoreTo}Scalar methods to encapsulate conversion
from LLVM memory type to/from LLVM temporary type.
 - No intended functionality change.

Modified:
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=64191&r1=64190&r2=64191&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Mon Feb  9 18:57:50 2009
@@ -178,6 +178,39 @@
   }
 }
 
+llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
+                                               QualType Ty) {
+  llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
+
+  // Bool can have different representation in memory than in
+  // registers.
+  if (Ty->isBooleanType())
+    if (V->getType() != llvm::Type::Int1Ty)
+      V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
+  
+  return V;
+}
+
+void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
+                                        bool Volatile) {
+  // Handle stores of types which have different representations in
+  // memory and as LLVM values.
+
+  // FIXME: We shouldn't be this loose, we should only do this
+  // conversion when we have a type we know has a different memory
+  // representation (e.g., bool).
+
+  const llvm::Type *SrcTy = Value->getType();
+  const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
+  if (DstPtr->getElementType() != SrcTy) {
+    const llvm::Type *MemTy = 
+      llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
+    Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
+  }
+
+  Builder.CreateStore(Value, Addr, Volatile);  
+}
+
 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
 /// this method emits the address of the lvalue, then loads the result as an
 /// rvalue, returning the rvalue.
@@ -196,17 +229,9 @@
       cast<llvm::PointerType>(Ptr->getType())->getElementType();
     
     // Simple scalar l-value.
-    if (EltTy->isSingleValueType()) {
-      llvm::Value *V = Builder.CreateLoad(Ptr, LV.isVolatileQualified(),"tmp");
-      
-      // Bool can have different representation in memory than in registers.
-      if (ExprType->isBooleanType()) {
-        if (V->getType() != llvm::Type::Int1Ty)
-          V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
-      }
-      
-      return RValue::get(V);
-    }
+    if (EltTy->isSingleValueType())
+      return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(), 
+                                          ExprType));
     
     assert(ExprType->isFunctionType() && "Unknown scalar value");
     return RValue::get(Ptr);
@@ -401,19 +426,9 @@
     return;
   }
   
-  llvm::Value *DstAddr = Dst.getAddress();
   assert(Src.isScalar() && "Can't emit an agg store with this method");
-  // FIXME: Handle volatility etc.
-  const llvm::Type *SrcTy = Src.getScalarVal()->getType();
-  const llvm::PointerType *DstPtr = cast<llvm::PointerType>(DstAddr->getType());
-  const llvm::Type *AddrTy = DstPtr->getElementType();
-  unsigned AS = DstPtr->getAddressSpace();
-  
-  if (AddrTy != SrcTy)
-    DstAddr = Builder.CreateBitCast(DstAddr, 
-                                    llvm::PointerType::get(SrcTy, AS),
-                                    "storetmp");
-  Builder.CreateStore(Src.getScalarVal(), DstAddr, Dst.isVolatileQualified());
+  EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(), 
+                    Dst.isVolatileQualified());
 }
 
 void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=64191&r1=64190&r2=64191&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Mon Feb  9 18:57:50 2009
@@ -612,6 +612,18 @@
   ///
   LValue EmitLValue(const Expr *E);
 
+  /// EmitLoadOfScalar - Load a scalar value from an address, taking
+  /// care to appropriately convert from the memory representation to
+  /// the LLVM value representation.
+  llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, 
+                                QualType Ty);
+
+  /// EmitStoreOfScalar - Store a scalar value to an address, taking
+  /// care to appropriately convert from the memory representation to
+  /// the LLVM value representation.
+  void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, 
+                         bool Volatile);
+
   /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
   /// this method emits the address of the lvalue, then loads the result as an
   /// rvalue, returning the rvalue.





More information about the cfe-commits mailing list