Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit df8d787

Browse files
committed
align the current stack frame to something larger than the default, when necessary
1 parent 7b1254a commit df8d787

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lib/Target/JSBackend/AllocaManager.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ uint64_t AllocaManager::getSize(const AllocaInst *AI) {
4040
// Return the alignment of the given alloca.
4141
unsigned AllocaManager::getAlignment(const AllocaInst *AI) {
4242
assert(AI->isStaticAlloca());
43-
return std::max(AI->getAlignment(),
44-
DL->getABITypeAlignment(AI->getAllocatedType()));
43+
unsigned Alignment = std::max(AI->getAlignment(),
44+
DL->getABITypeAlignment(AI->getAllocatedType()));
45+
MaxAlignment = std::max(Alignment, MaxAlignment);
46+
return Alignment;
4547
}
4648

4749
AllocaManager::AllocaInfo AllocaManager::getInfo(const AllocaInst *AI) {
@@ -452,7 +454,7 @@ void AllocaManager::computeFrameOffsets() {
452454
"Statically allocated frame size is " << FrameSize << "\n");
453455
}
454456

455-
AllocaManager::AllocaManager() {
457+
AllocaManager::AllocaManager() : MaxAlignment(0) {
456458
}
457459

458460
void AllocaManager::analyze(const Function &Func, const DataLayout &Layout,

lib/Target/JSBackend/AllocaManager.h

+5
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ class AllocaManager {
142142
void computeRepresentatives();
143143
void computeFrameOffsets();
144144

145+
unsigned MaxAlignment;
146+
145147
public:
146148
AllocaManager();
147149

@@ -166,6 +168,9 @@ class AllocaManager {
166168

167169
/// Return the total frame size for all static allocas and associated padding.
168170
uint64_t getFrameSize() const { return FrameSize; }
171+
172+
/// Return the largest alignment seen.
173+
unsigned getMaxAlignment() const { return MaxAlignment; }
169174
};
170175

171176
} // namespace llvm

lib/Target/JSBackend/JSBackend.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -1456,10 +1456,14 @@ void JSWriter::generateExpression(const User *I, raw_string_ostream& Code) {
14561456
if (AI->isStaticAlloca()) {
14571457
uint64_t Offset;
14581458
if (Allocas.getFrameOffset(AI, &Offset)) {
1459-
if (Offset != 0) {
1460-
Code << getAssign(AI) << "sp + " << Offset << "|0";
1459+
Code << getAssign(AI);
1460+
if (Allocas.getMaxAlignment() <= STACK_ALIGN) {
1461+
Code << "sp";
14611462
} else {
1462-
Code << getAssign(AI) << "sp";
1463+
Code << "sp_a"; // aligned base of stack is different, use that
1464+
}
1465+
if (Offset != 0) {
1466+
Code << " + " << Offset << "|0";
14631467
}
14641468
break;
14651469
}
@@ -1468,6 +1472,8 @@ void JSWriter::generateExpression(const User *I, raw_string_ostream& Code) {
14681472
return;
14691473
}
14701474

1475+
assert(AI->getAlignment() <= STACK_ALIGN); // TODO
1476+
14711477
Type *T = AI->getAllocatedType();
14721478
std::string Size;
14731479
uint64_t BaseSize = DL->getTypeAllocSize(T);
@@ -1822,6 +1828,10 @@ void JSWriter::printFunctionBody(const Function *F) {
18221828

18231829
// Emit local variables
18241830
UsedVars["sp"] = Type::IntegerTyID;
1831+
unsigned MaxAlignment = Allocas.getMaxAlignment();
1832+
if (MaxAlignment > STACK_ALIGN) {
1833+
UsedVars["sp_a"] = Type::IntegerTyID;
1834+
}
18251835
UsedVars["label"] = Type::IntegerTyID;
18261836
if (!UsedVars.empty()) {
18271837
unsigned Count = 0;
@@ -1864,6 +1874,11 @@ void JSWriter::printFunctionBody(const Function *F) {
18641874
// Emit stack entry
18651875
Out << " " << getAdHocAssign("sp", Type::getInt32Ty(F->getContext())) << "STACKTOP;";
18661876
if (uint64_t FrameSize = Allocas.getFrameSize()) {
1877+
if (MaxAlignment > STACK_ALIGN) {
1878+
// We must align this entire stack frame to something higher than the default
1879+
Out << "\n ";
1880+
Out << "sp_a = STACKTOP = (STACKTOP + " << utostr(MaxAlignment-1) << ")&-" << utostr(MaxAlignment) << ";";
1881+
}
18671882
Out << "\n ";
18681883
Out << getStackBump(FrameSize);
18691884
}

0 commit comments

Comments
 (0)