[cfe-commits] Patch to change how const arrays/structs are handled

Douglas Gregor dgregor at apple.com
Wed Oct 21 16:12:24 PDT 2009

On Oct 21, 2009, at 2:10 PM, Tanya Lattner wrote:

> Hi. I'd like to get a review of my patch that takes const arrays and  
> structs (unions, classes, etc) and creates a global instead of  
> storing it to the stack (treating it like a static const, for  
> performance reasons).
> Please let me know if you have questions.

Index: lib/CodeGen/CGDecl.cpp
--- lib/CodeGen/CGDecl.cpp	(revision 84777)
+++ lib/CodeGen/CGDecl.cpp	(working copy)
@@ -316,6 +316,14 @@
    llvm::Value *DeclPtr;
    if (Ty->isConstantSizeType()) {
      if (!Target.useGlobalsForAutomaticVariables()) {
+      // All constant structs and arrays should be global.
+      if (Ty.isConstant(getContext())
+          && (isa<ArrayType>(Ty) || isa<RecordType>(Ty))) {
+        EmitStaticBlockVarDecl(D);
+        return;
+      }

This looks too aggressive. There are more conditions that need to be  
checked before we can perform this optimization, e.g.,

	(1) Is the initializer constant?
	(2) In C++, is the type/element type of the array a POD?

There are probably other conditions; for example, we might not want to  
perform this optimization if the address of the variable is ever taken.

	- Doug

More information about the cfe-commits mailing list