Fix some significant problems with constant pools that resulted in unnecessary paddings between constant pool entries, larger than necessary alignments (e.g. 8 byte alignment for .literal4 sections), and potentially other issues.
1. ConstantPoolSDNode alignment field is log2 value of the alignment requirement. This is not consistent with other SDNode variants.
2. MachineConstantPool alignment field is also a log2 value.
3. However, some places are creating ConstantPoolSDNode with alignment value rather than log2 values. This creates entries with artificially large alignments, e.g. 256 for SSE vector values.
4. Constant pool entry offsets are computed when they are created. However, asm printer group them by sections. That means the offsets are no longer valid. However, asm printer uses them to determine size of padding between entries.
5. Asm printer uses expensive data structure multimap to track constant pool entries by sections.
6. Asm printer iterate over SmallPtrSet when it's emitting constant pool entries. This is non-deterministic.
Solutions:
1. ConstantPoolSDNode alignment field is changed to keep non-log2 value.
2. MachineConstantPool alignment field is also changed to keep non-log2 value.
3. Functions that create ConstantPool nodes are passing in non-log2 alignments.
4. MachineConstantPoolEntry no longer keeps an offset field. It's replaced with an alignment field. Offsets are not computed when constant pool entries are created. They are computed on the fly in asm printer and JIT.
5. Asm printer uses cheaper data structure to group constant pool entries.
6. Asm printer compute entry offsets after grouping is done.
7. Change JIT code to compute entry offsets on the fly.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66875 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 2a3a8bb..b25776a 100644
--- a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -240,6 +240,16 @@
IncrementFunctionNumber();
}
+namespace {
+ // SectionCPs - Keep track the alignment, constpool entries per Section.
+ struct SectionCPs {
+ const Section *S;
+ unsigned Alignment;
+ SmallVector<unsigned, 4> CPEs;
+ SectionCPs(const Section *s, unsigned a) : S(s), Alignment(a) {};
+ };
+}
+
/// EmitConstantPool - Print to the current output stream assembly
/// representations of the constants in the constant pool MCP. This is
/// used to print out constants which have been "spilled to memory" by
@@ -251,48 +261,60 @@
// Calculate sections for constant pool entries. We collect entries to go into
// the same section together to reduce amount of section switch statements.
- typedef
- std::multimap<const Section*,
- std::pair<MachineConstantPoolEntry, unsigned> > CPMap;
- CPMap CPs;
- SmallPtrSet<const Section*, 5> Sections;
-
+ SmallVector<SectionCPs, 4> CPSections;
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
MachineConstantPoolEntry CPE = CP[i];
+ unsigned Align = CPE.getAlignment();
const Section* S = TAI->SelectSectionForMachineConst(CPE.getType());
- CPs.insert(std::make_pair(S, std::make_pair(CPE, i)));
- Sections.insert(S);
+ // The number of sections are small, just do a linear search from the
+ // last section to the first.
+ bool Found = false;
+ unsigned SecIdx = CPSections.size();
+ while (SecIdx != 0) {
+ if (CPSections[--SecIdx].S == S) {
+ Found = true;
+ break;
+ }
+ }
+ if (!Found) {
+ SecIdx = CPSections.size();
+ CPSections.push_back(SectionCPs(S, Align));
+ }
+
+ if (Align > CPSections[SecIdx].Alignment)
+ CPSections[SecIdx].Alignment = Align;
+ CPSections[SecIdx].CPEs.push_back(i);
}
// Now print stuff into the calculated sections.
- for (SmallPtrSet<const Section*, 5>::iterator IS = Sections.begin(),
- ES = Sections.end(); IS != ES; ++IS) {
- SwitchToSection(*IS);
- EmitAlignment(MCP->getConstantPoolAlignment());
+ for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
+ SwitchToSection(CPSections[i].S);
+ EmitAlignment(Log2_32(CPSections[i].Alignment));
- std::pair<CPMap::iterator, CPMap::iterator> II = CPs.equal_range(*IS);
- for (CPMap::iterator I = II.first, E = II.second; I != E; ++I) {
- CPMap::iterator J = next(I);
- MachineConstantPoolEntry Entry = I->second.first;
- unsigned index = I->second.second;
-
- O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
- << index << ":\t\t\t\t\t";
- // O << TAI->getCommentString() << ' ' <<
- // WriteTypeSymbolic(O, CP[i].first.getType(), 0);
- O << '\n';
- if (Entry.isMachineConstantPoolEntry())
- EmitMachineConstantPoolValue(Entry.Val.MachineCPVal);
- else
- EmitGlobalConstant(Entry.Val.ConstVal);
+ unsigned Offset = 0;
+ for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
+ unsigned CPI = CPSections[i].CPEs[j];
+ MachineConstantPoolEntry CPE = CP[CPI];
// Emit inter-object padding for alignment.
- if (J != E) {
- const Type *Ty = Entry.getType();
- unsigned EntSize = TM.getTargetData()->getTypePaddedSize(Ty);
- unsigned ValEnd = Entry.getOffset() + EntSize;
- EmitZeros(J->second.first.getOffset()-ValEnd);
+ unsigned AlignMask = CPE.getAlignment() - 1;
+ unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
+ EmitZeros(NewOffset - Offset);
+
+ const Type *Ty = CPE.getType();
+ Offset = NewOffset + TM.getTargetData()->getTypePaddedSize(Ty);
+
+ O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
+ << CPI << ":\t\t\t\t\t";
+ if (VerboseAsm) {
+ O << TAI->getCommentString() << ' ';
+ WriteTypeSymbolic(O, CPE.getType(), 0);
}
+ O << '\n';
+ if (CPE.isMachineConstantPoolEntry())
+ EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
+ else
+ EmitGlobalConstant(CPE.Val.ConstVal);
}
}
}