llvm.org GIT mirror llvm / 72bb402
Treat [1 x i8] zeroinitializer as a C string, placing such stuff into mergeable string section. I don't see any bad impact of such decision (rather then placing it into mergeable const section, as it was before), but at least Darwin linker won't complain anymore. The problem in LLVM is that we don't have special type for string constants (like gcc does). Even more, we have two separate types: ConstatArray for non-null strings and ConstantAggregateZero for null stuff.... It's a bit weird :) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@63142 91177308-0d34-0410-b5e6-96231b3b80d8 Anton Korobeynikov 11 years ago
3 changed file(s) with 24 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
114114 DarwinTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
115115 const TargetData *TD = TM.getTargetData();
116116 Constant *C = cast(GV)->getInitializer();
117 const Type *Type = cast(C)->getType()->getElementType();
117 const Type *Ty = cast(C->getType())->getElementType();
118118
119 unsigned Size = TD->getTypePaddedSize(Type);
119 unsigned Size = TD->getTypePaddedSize(Ty);
120120 if (Size) {
121121 unsigned Align = TD->getPreferredAlignment(GV);
122122 if (Align <= 32)
125125 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
126126 const TargetData *TD = TM.getTargetData();
127127 Constant *C = cast(GV)->getInitializer();
128 const ConstantArray *CVA = cast(C);
129 const Type *Ty = CVA->getType()->getElementType();
128 const Type *Ty = cast(C->getType())->getElementType();
130129
131130 unsigned Size = TD->getTypePaddedSize(Ty);
132131 if (Size <= 16) {
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "llvm/Constants.h"
15 #include "llvm/DerivedTypes.h"
1516 #include "llvm/GlobalVariable.h"
1617 #include "llvm/Function.h"
1718 #include "llvm/Module.h"
169170 return (C->isNullValue() && !GV->isConstant() && !NoZerosInBSS);
170171 }
171172
173 static bool isConstantString(const Constant *C) {
174 // First check: is we have constant array of i8 terminated with zero
175 const ConstantArray *CVA = dyn_cast(C);
176 // Check, if initializer is a null-terminated string
177 if (CVA && CVA->isCString())
178 return true;
179
180 // Another possibility: [1 x i8] zeroinitializer
181 if (isa(C)) {
182 if (const ArrayType *Ty = dyn_cast(C->getType())) {
183 return (Ty->getElementType() == Type::Int8Ty &&
184 Ty->getNumElements() == 1);
185 }
186 }
187
188 return false;
189 }
190
191
172192 SectionKind::Kind
173193 TargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
174194 // Early exit - functions should be always in text sections.
190210 if (C->ContainsRelocations())
191211 return SectionKind::ROData;
192212 else {
193 const ConstantArray *CVA = dyn_cast(C);
194213 // Check, if initializer is a null-terminated string
195 if (CVA && CVA->isCString())
214 if (isConstantString(C))
196215 return SectionKind::RODataMergeStr;
197216 else
198217 return SectionKind::RODataMergeConst;