From f492cb1b62b9113d80d85d32515566d6c2a8cdb3 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 31 Jan 2012 04:36:19 +0000 Subject: [PATCH] enhance some optimization logic to handle ConstantDataSequential as well as ConstantArray. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149347 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGDecl.cpp | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 13b9e27d5d..82f7fed7ba 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -644,6 +644,16 @@ static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, } return true; } + + if (llvm::ConstantDataSequential *CDS = + dyn_cast(Init)) { + for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { + llvm::Constant *Elt = CDS->getElementAsConstant(i); + if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores)) + return false; + } + return true; + } // Anything else is hard and scary. return false; @@ -654,17 +664,26 @@ static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, /// stores that would be required. static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, bool isVolatile, CGBuilderTy &Builder) { - // Zero doesn't require any stores. - if (isa(Init) || - isa(Init) || - isa(Init)) + // Zero doesn't require a store. + if (Init->isNullValue() || isa(Init)) return; if (isa(Init) || isa(Init) || isa(Init) || isa(Init) || isa(Init)) { - if (!Init->isNullValue()) - Builder.CreateStore(Init, Loc, isVolatile); + Builder.CreateStore(Init, Loc, isVolatile); + return; + } + + if (llvm::ConstantDataSequential *CDS = + dyn_cast(Init)) { + for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { + llvm::Constant *Elt = CDS->getElementAsConstant(i); + + // Get a pointer to the element and emit it. + emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i), + isVolatile, Builder); + } return; } @@ -673,9 +692,7 @@ static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) { llvm::Constant *Elt = cast(Init->getOperand(i)); - if (Elt->isNullValue()) continue; - - // Otherwise, get a pointer to the element and emit it. + // Get a pointer to the element and emit it. emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i), isVolatile, Builder); } -- 2.40.0