aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-03-03 15:40:01 -0800
committerAlon Zakai <alonzakai@gmail.com>2014-03-03 15:40:01 -0800
commit1d33f493d6a107012e667ee9046a15510088f6f3 (patch)
treeec41b7e595bfacbc3a9a02a97d60612a71b88b2f
parent18c94bde936188c07f506fef76da48aa1be0550f (diff)
parent61ea49d98cb3d11cf9a8b7d672c72d41b11771d0 (diff)
Merge pull request #26 from sunfishcode/incoming
Patches that arrose during work on the alloca colorer
-rw-r--r--lib/Target/JSBackend/JSBackend.cpp41
-rw-r--r--lib/Transforms/NaCl/ExpandVarArgs.cpp3
2 files changed, 33 insertions, 11 deletions
diff --git a/lib/Target/JSBackend/JSBackend.cpp b/lib/Target/JSBackend/JSBackend.cpp
index 8a811b6958..b0a4b496ac 100644
--- a/lib/Target/JSBackend/JSBackend.cpp
+++ b/lib/Target/JSBackend/JSBackend.cpp
@@ -40,6 +40,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
+#include "llvm/Support/MathExtras.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/DebugInfo.h"
#include <algorithm>
@@ -167,12 +168,14 @@ namespace {
#define MEM_ALIGN 8
#define MEM_ALIGN_BITS 64
+ #define STACK_ALIGN 16
+ #define STACK_ALIGN_BITS 128
- unsigned memAlign(unsigned x) {
- return x + (x%MEM_ALIGN != 0 ? MEM_ALIGN - x%MEM_ALIGN : 0);
+ unsigned stackAlign(unsigned x) {
+ return RoundUpToAlignment(x, STACK_ALIGN);
}
- std::string memAlignStr(std::string x) {
- return "((" + x + "+" + utostr(MEM_ALIGN-1) + ")&-" + utostr(MEM_ALIGN) + ")";
+ std::string stackAlignStr(std::string x) {
+ return "((" + x + "+" + utostr(STACK_ALIGN-1) + ")&-" + utostr(STACK_ALIGN) + ")";
}
HeapData *allocateAddress(const std::string& Name, unsigned Bits = MEM_ALIGN_BITS) {
@@ -998,6 +1001,9 @@ std::string JSWriter::getConstant(const Constant* CV, AsmCast sign) {
}
std::string JSWriter::getValueAsStr(const Value* V, AsmCast sign) {
+ // Skip past no-op bitcasts and zero-index geps.
+ V = V->stripPointerCasts();
+
if (const Constant *CV = dyn_cast<Constant>(V)) {
return getConstant(CV, sign);
} else {
@@ -1006,6 +1012,9 @@ std::string JSWriter::getValueAsStr(const Value* V, AsmCast sign) {
}
std::string JSWriter::getValueAsCastStr(const Value* V, AsmCast sign) {
+ // Skip past no-op bitcasts and zero-index geps.
+ V = V->stripPointerCasts();
+
if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
return getConstant(cast<Constant>(V), sign);
} else {
@@ -1014,6 +1023,9 @@ std::string JSWriter::getValueAsCastStr(const Value* V, AsmCast sign) {
}
std::string JSWriter::getValueAsParenStr(const Value* V) {
+ // Skip past no-op bitcasts and zero-index geps.
+ V = V->stripPointerCasts();
+
if (const Constant *CV = dyn_cast<Constant>(V)) {
return getConstant(CV);
} else {
@@ -1022,6 +1034,9 @@ std::string JSWriter::getValueAsParenStr(const Value* V) {
}
std::string JSWriter::getValueAsCastParenStr(const Value* V, AsmCast sign) {
+ // Skip past no-op bitcasts and zero-index geps.
+ V = V->stripPointerCasts();
+
if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
return getConstant(cast<Constant>(V), sign);
} else {
@@ -1154,6 +1169,12 @@ static uint64_t LSBMask(unsigned numBits) {
// Generate code for and operator, either an Instruction or a ConstantExpr.
void JSWriter::generateExpression(const User *I, raw_string_ostream& Code) {
+ // To avoid emiting code and variables for the no-op pointer bitcasts
+ // and all-zero-index geps that LLVM needs to satisfy its type system, we
+ // call stripPointerCasts() on all values before translating them. This
+ // includes bitcasts whose only use is lifetime marker intrinsics.
+ assert(I == I->stripPointerCasts());
+
Type *T = I->getType();
if (T->isIntegerTy() && T->getIntegerBitWidth() > 32) {
errs() << *I << "\n";
@@ -1362,9 +1383,9 @@ void JSWriter::generateExpression(const User *I, raw_string_ostream& Code) {
uint64_t BaseSize = DL->getTypeAllocSize(T);
const Value *AS = AI->getArraySize();
if (const ConstantInt *CI = dyn_cast<ConstantInt>(AS)) {
- Size = Twine(memAlign(BaseSize * CI->getZExtValue())).str();
+ Size = Twine(stackAlign(BaseSize * CI->getZExtValue())).str();
} else {
- Size = memAlignStr("((" + utostr(BaseSize) + '*' + getValueAsStr(AS) + ")|0)");
+ Size = stackAlignStr("((" + utostr(BaseSize) + '*' + getValueAsStr(AS) + ")|0)");
}
Code << getAssign(AI) << "STACKTOP; STACKTOP = STACKTOP + " << Size << "|0";
break;
@@ -1586,7 +1607,9 @@ void JSWriter::addBlock(const BasicBlock *BB, Relooper& R, LLVMToRelooperMap& LL
raw_string_ostream CodeStream(Code);
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
I != E; ++I) {
- generateExpression(I, CodeStream);
+ if (I->stripPointerCasts() == I) {
+ generateExpression(I, CodeStream);
+ }
}
CodeStream.flush();
const Value* Condition = considerConditionVar(BB->getTerminator());
@@ -1821,7 +1844,7 @@ void JSWriter::printFunction(const Function *F) {
unsigned BaseSize = DL->getTypeAllocSize(T);
if (const ConstantInt *CI = dyn_cast<ConstantInt>(AS)) {
// TODO: group by alignment to avoid unnecessary padding
- unsigned Size = memAlign(BaseSize * CI->getZExtValue());
+ unsigned Size = stackAlign(BaseSize * CI->getZExtValue());
StackAllocs[AI] = TotalStackAllocs;
TotalStackAllocs += Size;
}
@@ -2089,8 +2112,6 @@ void JSWriter::parseConstant(const std::string& name, const Constant* CV, bool c
} else if (isa<ConstantAggregateZero>(CV)) {
if (calculate) {
unsigned Bytes = DL->getTypeStoreSize(CV->getType());
- // FIXME: assume full 64-bit alignment for now
- Bytes = memAlign(Bytes);
HeapData *GlobalData = allocateAddress(name);
for (unsigned i = 0; i < Bytes; ++i) {
GlobalData->push_back(0);
diff --git a/lib/Transforms/NaCl/ExpandVarArgs.cpp b/lib/Transforms/NaCl/ExpandVarArgs.cpp
index 1b605b79ee..6f41046850 100644
--- a/lib/Transforms/NaCl/ExpandVarArgs.cpp
+++ b/lib/Transforms/NaCl/ExpandVarArgs.cpp
@@ -240,7 +240,8 @@ static bool ExpandVarArgCall(InstType *Call, DataLayout *DL) {
// start of the function so that we don't leak space if the function
// is called in a loop.
Function *Func = Call->getParent()->getParent();
- Instruction *Buf = new AllocaInst(VarArgsTy, "vararg_buffer");
+ AllocaInst *Buf = new AllocaInst(VarArgsTy, "vararg_buffer");
+ Buf->setAlignment(8); // XXX EMSCRIPTEN: Align for 8-byte aligned doubles.
Func->getEntryBlock().getInstList().push_front(Buf);
// Call llvm.lifetime.start/end intrinsics to indicate that Buf is