initial support for uniquing SIL type lists, not used yet.

Swift SVN r3204
This commit is contained in:
Chris Lattner
2012-11-16 19:45:51 +00:00
parent b27c6d94f1
commit 4643edc850
4 changed files with 90 additions and 3 deletions

View File

@@ -17,7 +17,6 @@
#ifndef SWIFT_SIL_INSTRUCTION_H
#define SWIFT_SIL_INSTRUCTION_H
#include "swift/Basic/LLVM.h"
#include "swift/SIL/SILBase.h"
#include "swift/SIL/SILLocation.h"
#include "swift/SIL/SILSuccessor.h"

View File

@@ -18,19 +18,30 @@
#ifndef SWIFT_SIL_SILBase_H
#define SWIFT_SIL_SILBase_H
#include "llvm/ADT/PointerUnion.h"
#include "swift/Basic/LLVM.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/AlignOf.h"
namespace swift {
class SILTypeList;
class Type;
class SILBase {
/// Allocator that manages the memory of all the pieces of the Function.
mutable llvm::BumpPtrAllocator BPA;
void *TypeListUniquing;
SILBase(const SILBase&) = delete;
void operator=(const SILBase&) = delete;
public:
SILBase();
~SILBase();
/// Allocate memory using Function's internal allocator.
void *allocate(unsigned Size, unsigned Align) const {
return BPA.Allocate(Size, Align);
}
/// getSILTypeList - Get a uniqued pointer to a SIL type list. This can only
/// be used by Value.
SILTypeList *getSILTypeList(llvm::ArrayRef<Type> Types);
};
template <typename DERIVED>
@@ -49,7 +60,7 @@ public:
/// Custom version of 'new' that uses the Function's BumpPtrAllocator with
/// precise alignment knowledge.
void *operator new(size_t Bytes, const swift::SILBase &C,
void *operator new(size_t Bytes, const SILBase &C,
size_t Alignment = llvm::AlignOf<DERIVED>::Alignment) {
return C.allocate(Bytes, Alignment);
}

View File

@@ -17,9 +17,12 @@
#ifndef SWIFT_SIL_VALUE_H
#define SWIFT_SIL_VALUE_H
#include "swift/SIL/SILBase.h"
#include "swift/AST/Type.h"
namespace swift {
class SILTypeList;
enum class ValueKind {
#define VALUE(Id, Parent) Id,
#define VALUE_RANGE(Id, FirstId, LastId) \

74
lib/SIL/SILBase.cpp Normal file
View File

@@ -0,0 +1,74 @@
//===--- SILBase.cpp - SIL Memory Allocation utilities --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/Value.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FoldingSet.h"
using namespace swift;
namespace swift {
/// SILTypeList - The uniqued backing store for the Value type list. This
/// is only expose out of Value as an ArrayRef of types, so it should never be
/// used outside of libSIL.
class SILTypeList : public llvm::FoldingSetNode {
public:
unsigned NumTypes;
Type Types[1]; // Actually variable sized.
void Profile(llvm::FoldingSetNodeID &ID) const {
for (unsigned i = 0, e = NumTypes; i != e; ++i)
ID.AddPointer(Types[i].getPointer());
}
};
}
/// SILTypeListUniquingType - This is the type of the folding set maintained by
/// SILBase that these things are uniqued into.
typedef llvm::FoldingSet<SILTypeList> SILTypeListUniquingType;
SILBase::SILBase() {
TypeListUniquing = new SILTypeListUniquingType();
}
SILBase::~SILBase() {
delete (SILTypeListUniquingType*)TypeListUniquing;
}
/// getSILTypeList - Get a uniqued pointer to a SIL type list. This can only
/// be used by Value.
SILTypeList *SILBase::getSILTypeList(ArrayRef<Type> Types) {
auto UniqueMap = (SILTypeListUniquingType*)TypeListUniquing;
llvm::FoldingSetNodeID ID;
for (auto T : Types)
ID.AddPointer(T.getPointer());
// If we already have this type list, just return it.
void *InsertPoint = 0;
if (SILTypeList *TypeList = UniqueMap->FindNodeOrInsertPos(ID, InsertPoint))
return TypeList;
// Otherwise, allocate a new one.
void *NewListP = BPA.Allocate(sizeof(SILTypeList)+
sizeof(Type)*(Types.size()-1),
llvm::AlignOf<SILTypeList>::Alignment);
SILTypeList *NewList = new (NewListP) SILTypeList();
NewList->NumTypes = Types.size();
std::copy(Types.begin(), Types.end(), NewList->Types);
UniqueMap->InsertNode(NewList, InsertPoint);
return NewList;
}