[llvm-commits] CVS: llvm-java/lib/Compiler/OperandStack.cpp Locals.h Locals.cpp Compiler.cpp BasicBlockBuilder.h
Alkis Evlogimenos
alkis at cs.uiuc.edu
Thu Feb 3 13:36:09 PST 2005
Changes in directory llvm-java/lib/Compiler:
OperandStack.cpp updated: 1.10 -> 1.11
Locals.h updated: 1.3 -> 1.4
Locals.cpp updated: 1.8 -> 1.9
Compiler.cpp updated: 1.212 -> 1.213
BasicBlockBuilder.h updated: 1.2 -> 1.3
---
Log message:
Create an entry block to hold all allocas for the operand stack and
locals. This also fixes the problem where the bb0 block is a loop (and
has a predecessor) which results in illegal llvm code.
---
Diffs of the changes: (+63 -32)
BasicBlockBuilder.h | 16 ++++++++++++++--
Compiler.cpp | 24 +++++++++++++-----------
Locals.cpp | 38 ++++++++++++++++++++++++++------------
Locals.h | 6 ++++++
OperandStack.cpp | 11 ++++-------
5 files changed, 63 insertions(+), 32 deletions(-)
Index: llvm-java/lib/Compiler/OperandStack.cpp
diff -u llvm-java/lib/Compiler/OperandStack.cpp:1.10 llvm-java/lib/Compiler/OperandStack.cpp:1.11
--- llvm-java/lib/Compiler/OperandStack.cpp:1.10 Wed Feb 2 10:26:54 2005
+++ llvm-java/lib/Compiler/OperandStack.cpp Thu Feb 3 15:35:58 2005
@@ -20,7 +20,6 @@
#include <llvm/Instructions.h>
#include <llvm/ADT/StringExtras.h>
#include <llvm/Java/Compiler.h>
-#include <iostream>
using namespace llvm;
using namespace llvm::Java;
@@ -48,12 +47,10 @@
if (it == slotMap.end()) {
// Insert the alloca at the beginning of the entry block.
- BasicBlock* entry = &bb->getParent()->getEntryBlock();
- AllocaInst* alloca;
- if (entry->empty())
- alloca = new AllocaInst(type, NULL, "opStack", entry);
- else
- alloca = new AllocaInst(type, NULL, "opStack", &entry->front());
+ BasicBlock& entry = bb->getParent()->getEntryBlock();
+ assert(entry.getTerminator() && "Entry block must have a terminator!");
+ AllocaInst* alloca =
+ new AllocaInst(type, NULL, "opStack", entry.getTerminator());
it = slotMap.insert(it, std::make_pair(type, alloca));
}
Index: llvm-java/lib/Compiler/Locals.h
diff -u llvm-java/lib/Compiler/Locals.h:1.3 llvm-java/lib/Compiler/Locals.h:1.4
--- llvm-java/lib/Compiler/Locals.h:1.3 Fri Jan 21 05:41:38 2005
+++ llvm-java/lib/Compiler/Locals.h Thu Feb 3 15:35:58 2005
@@ -23,6 +23,7 @@
class AllocaInst;
class BasicBlock;
+ class Instruction;
class Type;
class Value;
@@ -42,6 +43,11 @@
/// insertAtEnd BasicBlock
void store(unsigned i, Value* value, BasicBlock* insertAtEnd);
+ /// @brief - Stores the value \c value on the \c i'th local
+ /// variable and prepends any instructions to implement this before \c
+ /// insertBefore Instruction
+ void store(unsigned i, Value* value, Instruction* insertBefore);
+
/// @brief - Loads the value of the \c i'th local variable of type
/// \c type and appends any instructions to implement this to \c
/// insertAtEnd BasicBlock
Index: llvm-java/lib/Compiler/Locals.cpp
diff -u llvm-java/lib/Compiler/Locals.cpp:1.8 llvm-java/lib/Compiler/Locals.cpp:1.9
--- llvm-java/lib/Compiler/Locals.cpp:1.8 Wed Feb 2 10:26:54 2005
+++ llvm-java/lib/Compiler/Locals.cpp Thu Feb 3 15:35:58 2005
@@ -42,24 +42,38 @@
if (it == slotMap.end()) {
// Insert the alloca at the beginning of the entry block.
- BasicBlock* entry = &insertAtEnd->getParent()->getEntryBlock();
- AllocaInst* alloca;
- if (entry->empty())
- alloca = new AllocaInst(storageTy,
- NULL,
- "local" + utostr(i),
- entry);
- else
- alloca = new AllocaInst(storageTy,
- NULL,
- "local" + utostr(i),
- &entry->front());
+ BasicBlock& entry = insertAtEnd->getParent()->getEntryBlock();
+ assert(entry.getTerminator() && "Entry block must have a terminator!");
+ AllocaInst* alloca =
+ new AllocaInst(storageTy, NULL, "local"+utostr(i), entry.getTerminator());
it = slotMap.insert(it, std::make_pair(storageTy, alloca));
}
new StoreInst(value, it->second, insertAtEnd);
}
+void Locals::store(unsigned i, Value* value, Instruction* insertBefore)
+{
+ const Type* valueTy = value->getType();
+ const Type* storageTy = getStorageType(valueTy);
+ if (valueTy != storageTy)
+ value = new CastInst(value, storageTy, "to-storage-type", insertBefore);
+
+ SlotMap& slotMap = TheLocals[i];
+ SlotMap::iterator it = slotMap.find(storageTy);
+
+ if (it == slotMap.end()) {
+ // Insert the alloca at the beginning of the entry block.
+ BasicBlock& entry = insertBefore->getParent()->getParent()->getEntryBlock();
+ assert(entry.getTerminator() && "Entry block must have a terminator!");
+ AllocaInst* alloca =
+ new AllocaInst(storageTy, NULL, "local"+utostr(i), entry.getTerminator());
+ it = slotMap.insert(it, std::make_pair(storageTy, alloca));
+ }
+
+ new StoreInst(value, it->second, insertBefore);
+}
+
llvm::Value* Locals::load(unsigned i, const Type* valueTy,
BasicBlock* insertAtEnd)
{
Index: llvm-java/lib/Compiler/Compiler.cpp
diff -u llvm-java/lib/Compiler/Compiler.cpp:1.212 llvm-java/lib/Compiler/Compiler.cpp:1.213
--- llvm-java/lib/Compiler/Compiler.cpp:1.212 Thu Feb 3 02:01:47 2005
+++ llvm-java/lib/Compiler/Compiler.cpp Thu Feb 3 15:35:58 2005
@@ -23,7 +23,6 @@
#include <llvm/Java/ClassFile.h>
#include <llvm/Constants.h>
#include <llvm/DerivedTypes.h>
-#include <llvm/Instructions.h>
#include <llvm/Value.h>
#include <llvm/Type.h>
#include <llvm/ADT/STLExtras.h>
@@ -1366,20 +1365,23 @@
unsigned index = 0;
for (Function::aiterator
a = function->abegin(), ae = function->aend(); a != ae; ++a) {
- locals_.store(index, a, &function->getEntryBlock());
+ locals_.store(index, a, function->getEntryBlock().getTerminator());
index += isTwoSlotType(a->getType()) ? 2 : 1;
}
- // For the entry block the operand stack is empty and the locals
- // contain the arguments to the function.
+
+ BasicBlock* bb0 = bbBuilder_->getBasicBlock(0);
+
+ // For bb0 the operand stack is empty and the locals contain the
+ // arguments to the function.
//
// NOTE: We create an operand stack one size too big because we
// push extra values on the stack to simplify code generation
// (see implementation of ifne).
opStack_ = OperandStack(codeAttr->getMaxStack()+2);
- opStackDepthMap_.insert(std::make_pair(&function->getEntryBlock(), 0));
+ opStackDepthMap_.insert(std::make_pair(bb0, 0));
- // Insert the entry block to the work list.
- bbWorkList_.push_back(&function->getEntryBlock());
+ // Insert bb0 in the work list.
+ bbWorkList_.push_back(bb0);
// Process the work list until we compile the whole function.
while (!bbWorkList_.empty()) {
@@ -1406,10 +1408,10 @@
new BranchInst(bbBuilder_->getBasicBlock(end), currentBB_);
// For each successor of this basic block we can compute its
- // entry operand stack and locals, do so, and add it to the
- // work list. If a successor already has an entry operand
- // stack and locals we assume the computation was correct and
- // do not add it to the work list.
+ // entry operand stack depth. We do so, and add it to the work
+ // list. If a successor already has an entry operand stack and
+ // locals we assume the computation was correct and do not add
+ // it to the work list.
for (succ_iterator
SI = succ_begin(currentBB_), SE = succ_end(currentBB_);
SI != SE; ++SI) {
Index: llvm-java/lib/Compiler/BasicBlockBuilder.h
diff -u llvm-java/lib/Compiler/BasicBlockBuilder.h:1.2 llvm-java/lib/Compiler/BasicBlockBuilder.h:1.3
--- llvm-java/lib/Compiler/BasicBlockBuilder.h:1.2 Thu Oct 28 03:26:32 2004
+++ llvm-java/lib/Compiler/BasicBlockBuilder.h Thu Feb 3 15:35:58 2005
@@ -17,6 +17,7 @@
#include <llvm/Java/ClassFile.h>
#include <llvm/ADT/STLExtras.h>
#include <llvm/ADT/StringExtras.h>
+#include <llvm/Instructions.h>
#include <map>
namespace llvm { namespace Java {
@@ -44,14 +45,25 @@
BasicBlockBuilder(Function* f, CodeAttribute* c)
: function_(f) {
+ // Create the entry block.
+ BasicBlock* entry = new BasicBlock("entry", function_);
+
+ // Create the block for bytecode 0 (bb0).
BasicBlock* bb = getOrCreateBasicBlockAt(0);
+ // Add an unconditional branch from the entry block to bb0.
+ new BranchInst(bb, entry);
+
+ // Create basic blocks for exception handlers.
const CodeAttribute::Exceptions& exceptions = c->getExceptions();
for (unsigned i = 0, e = exceptions.size(); i != e; ++i)
getOrCreateBasicBlockAt(exceptions[i]->getHandlerPc());
+ // Parse the bytecode and create basic blocks for all targets of
+ // control flow instructions.
parse(c->getCode(), 0, c->getCodeSize());
+ // Build the bytecode->basic block map.
for (BC2BBMap::const_iterator i = bc2bbMap_.begin(), e = bc2bbMap_.end();
i != e; ++i) {
unsigned end = next(i) != e ? next(i)->first : c->getCodeSize();
@@ -59,8 +71,8 @@
std::make_pair(i->second, std::make_pair(i->first, end)));
}
- assert(function_->getEntryBlock().getName() == "bc0");
- assert(bb2bcMap_.find(&function_->getEntryBlock()) != bb2bcMap_.end());
+ assert(function_->getEntryBlock().getName() == "entry");
+ assert(bb2bcMap_.find(&function_->getEntryBlock()) == bb2bcMap_.end());
}
BasicBlock* getBasicBlock(unsigned bcI) const {
More information about the llvm-commits
mailing list