[llvm-commits] [dragonegg] r127805 - /dragonegg/trunk/Constants.cpp

Duncan Sands baldrick at free.fr
Thu Mar 17 03:41:39 PDT 2011


Author: baldrick
Date: Thu Mar 17 05:41:39 2011
New Revision: 127805

URL: http://llvm.org/viewvc/llvm-project?rev=127805&view=rev
Log:
Factorize code for getting the address of a simple constant.  While there
add a few missing simple constant types.

Modified:
    dragonegg/trunk/Constants.cpp

Modified: dragonegg/trunk/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/Constants.cpp?rev=127805&r1=127804&r2=127805&view=diff
==============================================================================
--- dragonegg/trunk/Constants.cpp (original)
+++ dragonegg/trunk/Constants.cpp Thu Mar 17 05:41:39 2011
@@ -1297,15 +1297,27 @@
   }
 }
 
-/// get_constant_alignment - Return the alignment of constant EXP in bits.
-static unsigned int
-get_constant_alignment (tree exp)
-{
-    unsigned int align = TYPE_ALIGN (TREE_TYPE (exp));
+static Constant *AddressOfCST(tree exp) {
+  Constant *Init = ConvertInitializer(exp);
+
+  // Cache the constants to avoid making obvious duplicates that have to be
+  // folded by the optimizer.
+  static std::map<Constant*, GlobalVariable*> CSTCache;
+  GlobalVariable *&Slot = CSTCache[Init];
+  if (!Slot) {
+    // Create a new global variable.
+    Slot = new GlobalVariable(*TheModule, Init->getType(), true,
+                              GlobalVariable::PrivateLinkage, Init, ".cst");
+    unsigned align = TYPE_ALIGN (TREE_TYPE (exp));
 #ifdef CONSTANT_ALIGNMENT
-      align = CONSTANT_ALIGNMENT (exp, align);
+    align = CONSTANT_ALIGNMENT (exp, align);
 #endif
-        return align;
+    Slot->setAlignment(align);
+  }
+
+  // The initializer may have any type.  Return a pointer of the expected type.
+  const Type *Ty = ConvertType(TREE_TYPE(exp));
+  return TheFolder->CreateBitCast(Slot, Ty->getPointerTo());
 }
 
 static Constant *AddressOfDecl(tree exp) {
@@ -1339,62 +1351,6 @@
   return TheTreeToLLVM->AddressOfLABEL_DECL(exp);
 }
 
-static Constant *AddressOfCOMPLEX_CST(tree exp) {
-  Constant *Init = ConvertCOMPLEX_CST(exp);
-
-  // Cache the constants to avoid making obvious duplicates that have to be
-  // folded by the optimizer.
-  static std::map<Constant*, GlobalVariable*> ComplexCSTCache;
-  GlobalVariable *&Slot = ComplexCSTCache[Init];
-  if (Slot) return Slot;
-
-  // Create a new complex global.
-  Slot = new GlobalVariable(*TheModule, Init->getType(), true,
-                            GlobalVariable::PrivateLinkage, Init, ".cpx");
-  Slot->setAlignment(get_constant_alignment(exp) / 8);
-
-  return Slot;
-}
-
-static Constant *AddressOfREAL_CST(tree exp) {
-  Constant *Init = ConvertREAL_CST(exp);
-
-  // Cache the constants to avoid making obvious duplicates that have to be
-  // folded by the optimizer.
-  static std::map<Constant*, GlobalVariable*> RealCSTCache;
-  GlobalVariable *&Slot = RealCSTCache[Init];
-  if (Slot) return Slot;
-
-  // Create a new real global.
-  Slot = new GlobalVariable(*TheModule, Init->getType(), true,
-                            GlobalVariable::PrivateLinkage, Init, ".rl");
-  Slot->setAlignment(get_constant_alignment(exp) / 8);
-
-  return Slot;
-}
-
-static Constant *AddressOfSTRING_CST(tree exp) {
-  Constant *Init = ConvertSTRING_CST(exp);
-
-  GlobalVariable **SlotP = 0;
-
-  // Cache the string constants to avoid making obvious duplicate strings that
-  // have to be folded by the optimizer.
-  static std::map<Constant*, GlobalVariable*> StringCSTCache;
-  GlobalVariable *&Slot = StringCSTCache[Init];
-  if (Slot) return Slot;
-  SlotP = &Slot;
-
-  // Create a new string global.
-  GlobalVariable *GV = new GlobalVariable(*TheModule, Init->getType(), true,
-                                          GlobalVariable::PrivateLinkage, Init,
-                                          ".str");
-  GV->setAlignment(get_constant_alignment(exp) / 8);
-
-  if (SlotP) *SlotP = GV;
-  return GV;
-}
-
 static Constant *AddressOfARRAY_REF(tree exp) {
   tree Array = TREE_OPERAND(exp, 0);
   tree Index = TREE_OPERAND(exp, 1);
@@ -1515,6 +1471,14 @@
     debug_tree(exp);
     assert(0 && "Unknown constant lvalue to convert!");
     abort();
+  case COMPLEX_CST:
+  case FIXED_CST:
+  case INTEGER_CST:
+  case REAL_CST:
+  case STRING_CST:
+  case VECTOR_CST:
+    LV = AddressOfCST(exp);
+    break;
   case FUNCTION_DECL:
   case CONST_DECL:
   case VAR_DECL:
@@ -1523,15 +1487,6 @@
   case LABEL_DECL:
     LV = AddressOfLABEL_DECL(exp);
     break;
-  case COMPLEX_CST:
-    LV = AddressOfCOMPLEX_CST(exp);
-    break;
-  case REAL_CST:
-    LV = AddressOfREAL_CST(exp);
-    break;
-  case STRING_CST:
-    LV = AddressOfSTRING_CST(exp);
-    break;
   case COMPONENT_REF:
     LV = AddressOfCOMPONENT_REF(exp);
     break;





More information about the llvm-commits mailing list