mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-14 14:24:05 +00:00
split code that doesn't need to be templated out of IRBuilder into a new
non-templated IRBuilderBase class. Move that large CreateGlobalString out of line, eliminating the need to #include GlobalVariable.h in IRBuilder.h git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92227 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
#include "llvm/Constants.h"
|
#include "llvm/Constants.h"
|
||||||
#include "llvm/Instructions.h"
|
#include "llvm/Instructions.h"
|
||||||
#include "llvm/GlobalAlias.h"
|
|
||||||
#include "llvm/GlobalVariable.h"
|
|
||||||
#include "llvm/Function.h"
|
#include "llvm/Function.h"
|
||||||
#include "llvm/Metadata.h"
|
#include "llvm/Metadata.h"
|
||||||
#include "llvm/LLVMContext.h"
|
#include "llvm/LLVMContext.h"
|
||||||
@ -41,98 +39,49 @@ protected:
|
|||||||
I->setName(Name);
|
I->setName(Name);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// IRBuilderBase - Common base class shared among various IRBuilders.
|
||||||
/// IRBuilder - This provides a uniform API for creating instructions and
|
class IRBuilderBase {
|
||||||
/// inserting them into a basic block: either at the end of a BasicBlock, or
|
protected:
|
||||||
/// at a specific iterator location in a block.
|
|
||||||
///
|
|
||||||
/// Note that the builder does not expose the full generality of LLVM
|
|
||||||
/// instructions. For access to extra instruction properties, use the mutators
|
|
||||||
/// (e.g. setVolatile) on the instructions after they have been created.
|
|
||||||
/// The first template argument handles whether or not to preserve names in the
|
|
||||||
/// final instruction output. This defaults to on. The second template argument
|
|
||||||
/// specifies a class to use for creating constants. This defaults to creating
|
|
||||||
/// minimally folded constants. The fourth template argument allows clients to
|
|
||||||
/// specify custom insertion hooks that are called on every newly created
|
|
||||||
/// insertion.
|
|
||||||
template<bool preserveNames = true, typename T = ConstantFolder,
|
|
||||||
typename Inserter = IRBuilderDefaultInserter<preserveNames> >
|
|
||||||
class IRBuilder : public Inserter {
|
|
||||||
BasicBlock *BB;
|
BasicBlock *BB;
|
||||||
BasicBlock::iterator InsertPt;
|
BasicBlock::iterator InsertPt;
|
||||||
unsigned DbgMDKind;
|
unsigned DbgMDKind;
|
||||||
MDNode *CurDbgLocation;
|
MDNode *CurDbgLocation;
|
||||||
LLVMContext &Context;
|
LLVMContext &Context;
|
||||||
T Folder;
|
|
||||||
public:
|
public:
|
||||||
IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
|
|
||||||
: Inserter(I), DbgMDKind(0), CurDbgLocation(0), Context(C), Folder(F) {
|
|
||||||
ClearInsertionPoint();
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit IRBuilder(LLVMContext &C)
|
IRBuilderBase(LLVMContext &context)
|
||||||
: DbgMDKind(0), CurDbgLocation(0), Context(C), Folder(C) {
|
: DbgMDKind(0), CurDbgLocation(0), Context(context) {
|
||||||
ClearInsertionPoint();
|
ClearInsertionPoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit IRBuilder(BasicBlock *TheBB, const T &F)
|
|
||||||
: DbgMDKind(0), CurDbgLocation(0), Context(TheBB->getContext()), Folder(F) {
|
|
||||||
SetInsertPoint(TheBB);
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit IRBuilder(BasicBlock *TheBB)
|
|
||||||
: DbgMDKind(0), CurDbgLocation(0), Context(TheBB->getContext()),
|
|
||||||
Folder(Context) {
|
|
||||||
SetInsertPoint(TheBB);
|
|
||||||
}
|
|
||||||
|
|
||||||
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
|
|
||||||
: DbgMDKind(0), CurDbgLocation(0), Context(TheBB->getContext()), Folder(F) {
|
|
||||||
SetInsertPoint(TheBB, IP);
|
|
||||||
}
|
|
||||||
|
|
||||||
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
|
|
||||||
: DbgMDKind(0), CurDbgLocation(0), Context(TheBB->getContext()),
|
|
||||||
Folder(Context) {
|
|
||||||
SetInsertPoint(TheBB, IP);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// getFolder - Get the constant folder being used.
|
|
||||||
const T &getFolder() { return Folder; }
|
|
||||||
|
|
||||||
/// isNamePreserving - Return true if this builder is configured to actually
|
|
||||||
/// add the requested names to IR created through it.
|
|
||||||
bool isNamePreserving() const { return preserveNames; }
|
|
||||||
|
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
// Builder configuration methods
|
// Builder configuration methods
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
|
|
||||||
/// ClearInsertionPoint - Clear the insertion point: created instructions will
|
/// ClearInsertionPoint - Clear the insertion point: created instructions will
|
||||||
/// not be inserted into a block.
|
/// not be inserted into a block.
|
||||||
void ClearInsertionPoint() {
|
void ClearInsertionPoint() {
|
||||||
BB = 0;
|
BB = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicBlock *GetInsertBlock() const { return BB; }
|
BasicBlock *GetInsertBlock() const { return BB; }
|
||||||
|
|
||||||
BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
|
BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
|
||||||
|
|
||||||
/// SetInsertPoint - This specifies that created instructions should be
|
/// SetInsertPoint - This specifies that created instructions should be
|
||||||
/// appended to the end of the specified block.
|
/// appended to the end of the specified block.
|
||||||
void SetInsertPoint(BasicBlock *TheBB) {
|
void SetInsertPoint(BasicBlock *TheBB) {
|
||||||
BB = TheBB;
|
BB = TheBB;
|
||||||
InsertPt = BB->end();
|
InsertPt = BB->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SetInsertPoint - This specifies that created instructions should be
|
/// SetInsertPoint - This specifies that created instructions should be
|
||||||
/// inserted at the specified point.
|
/// inserted at the specified point.
|
||||||
void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
|
void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
|
||||||
BB = TheBB;
|
BB = TheBB;
|
||||||
InsertPt = IP;
|
InsertPt = IP;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SetCurrentDebugLocation - Set location information used by debugging
|
/// SetCurrentDebugLocation - Set location information used by debugging
|
||||||
/// information.
|
/// information.
|
||||||
void SetCurrentDebugLocation(MDNode *L) {
|
void SetCurrentDebugLocation(MDNode *L) {
|
||||||
@ -140,35 +89,36 @@ public:
|
|||||||
DbgMDKind = Context.getMetadata().getMDKindID("dbg");
|
DbgMDKind = Context.getMetadata().getMDKindID("dbg");
|
||||||
CurDbgLocation = L;
|
CurDbgLocation = L;
|
||||||
}
|
}
|
||||||
|
|
||||||
MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
|
MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
|
||||||
|
|
||||||
/// SetDebugLocation - Set location information for the given instruction.
|
/// SetDebugLocation - Set location information for the given instruction.
|
||||||
void SetDebugLocation(Instruction *I) {
|
void SetDebugLocation(Instruction *I) {
|
||||||
if (CurDbgLocation)
|
if (CurDbgLocation)
|
||||||
Context.getMetadata().addMD(DbgMDKind, CurDbgLocation, I);
|
Context.getMetadata().addMD(DbgMDKind, CurDbgLocation, I);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SetDebugLocation - Set location information for the given instruction.
|
/// SetDebugLocation - Set location information for the given instruction.
|
||||||
void SetDebugLocation(Instruction *I, MDNode *Loc) {
|
void SetDebugLocation(Instruction *I, MDNode *Loc) {
|
||||||
if (DbgMDKind == 0)
|
if (DbgMDKind == 0)
|
||||||
DbgMDKind = Context.getMetadata().getMDKindID("dbg");
|
DbgMDKind = Context.getMetadata().getMDKindID("dbg");
|
||||||
Context.getMetadata().addMD(DbgMDKind, Loc, I);
|
Context.getMetadata().addMD(DbgMDKind, Loc, I);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert - Insert and return the specified instruction.
|
//===--------------------------------------------------------------------===//
|
||||||
template<typename InstTy>
|
// Miscellaneous creation methods.
|
||||||
InstTy *Insert(InstTy *I, const Twine &Name = "") const {
|
//===--------------------------------------------------------------------===//
|
||||||
this->InsertHelper(I, Name, BB, InsertPt);
|
|
||||||
if (CurDbgLocation)
|
/// CreateGlobalString - Make a new global variable with an initializer that
|
||||||
Context.getMetadata().addMD(DbgMDKind, CurDbgLocation, I);
|
/// has array of i8 type filled in the the nul terminated string value
|
||||||
return I;
|
/// specified. If Name is specified, it is the name of the global variable
|
||||||
}
|
/// created.
|
||||||
|
Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
|
||||||
|
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
// Type creation methods
|
// Type creation methods
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
|
|
||||||
/// getInt1Ty - Fetch the type representing a single bit
|
/// getInt1Ty - Fetch the type representing a single bit
|
||||||
const Type *getInt1Ty() {
|
const Type *getInt1Ty() {
|
||||||
return Type::getInt1Ty(Context);
|
return Type::getInt1Ty(Context);
|
||||||
@ -193,7 +143,7 @@ public:
|
|||||||
const Type *getInt64Ty() {
|
const Type *getInt64Ty() {
|
||||||
return Type::getInt64Ty(Context);
|
return Type::getInt64Ty(Context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// getFloatTy - Fetch the type representing a 32-bit floating point value.
|
/// getFloatTy - Fetch the type representing a 32-bit floating point value.
|
||||||
const Type *getFloatTy() {
|
const Type *getFloatTy() {
|
||||||
return Type::getFloatTy(Context);
|
return Type::getFloatTy(Context);
|
||||||
@ -208,6 +158,68 @@ public:
|
|||||||
const Type *getVoidTy() {
|
const Type *getVoidTy() {
|
||||||
return Type::getVoidTy(Context);
|
return Type::getVoidTy(Context);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// IRBuilder - This provides a uniform API for creating instructions and
|
||||||
|
/// inserting them into a basic block: either at the end of a BasicBlock, or
|
||||||
|
/// at a specific iterator location in a block.
|
||||||
|
///
|
||||||
|
/// Note that the builder does not expose the full generality of LLVM
|
||||||
|
/// instructions. For access to extra instruction properties, use the mutators
|
||||||
|
/// (e.g. setVolatile) on the instructions after they have been created.
|
||||||
|
/// The first template argument handles whether or not to preserve names in the
|
||||||
|
/// final instruction output. This defaults to on. The second template argument
|
||||||
|
/// specifies a class to use for creating constants. This defaults to creating
|
||||||
|
/// minimally folded constants. The fourth template argument allows clients to
|
||||||
|
/// specify custom insertion hooks that are called on every newly created
|
||||||
|
/// insertion.
|
||||||
|
template<bool preserveNames = true, typename T = ConstantFolder,
|
||||||
|
typename Inserter = IRBuilderDefaultInserter<preserveNames> >
|
||||||
|
class IRBuilder : public IRBuilderBase, public Inserter {
|
||||||
|
T Folder;
|
||||||
|
public:
|
||||||
|
IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
|
||||||
|
: IRBuilderBase(C), Inserter(I), Folder(F) {
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit IRBuilder(BasicBlock *TheBB, const T &F)
|
||||||
|
: IRBuilderBase(TheBB->getContext()), Folder(F) {
|
||||||
|
SetInsertPoint(TheBB);
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit IRBuilder(BasicBlock *TheBB)
|
||||||
|
: IRBuilderBase(TheBB->getContext()), Folder(Context) {
|
||||||
|
SetInsertPoint(TheBB);
|
||||||
|
}
|
||||||
|
|
||||||
|
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
|
||||||
|
: IRBuilderBase(TheBB->getContext()), Folder(F) {
|
||||||
|
SetInsertPoint(TheBB, IP);
|
||||||
|
}
|
||||||
|
|
||||||
|
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
|
||||||
|
: IRBuilderBase(TheBB->getContext()), Folder(Context) {
|
||||||
|
SetInsertPoint(TheBB, IP);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// getFolder - Get the constant folder being used.
|
||||||
|
const T &getFolder() { return Folder; }
|
||||||
|
|
||||||
|
/// isNamePreserving - Return true if this builder is configured to actually
|
||||||
|
/// add the requested names to IR created through it.
|
||||||
|
bool isNamePreserving() const { return preserveNames; }
|
||||||
|
|
||||||
|
/// Insert - Insert and return the specified instruction.
|
||||||
|
template<typename InstTy>
|
||||||
|
InstTy *Insert(InstTy *I, const Twine &Name = "") const {
|
||||||
|
this->InsertHelper(I, Name, BB, InsertPt);
|
||||||
|
if (CurDbgLocation)
|
||||||
|
Context.getMetadata().addMD(DbgMDKind, CurDbgLocation, I);
|
||||||
|
return I;
|
||||||
|
}
|
||||||
|
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
// Instruction creation methods: Terminators
|
// Instruction creation methods: Terminators
|
||||||
@ -646,26 +658,16 @@ public:
|
|||||||
Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
|
Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
|
||||||
return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
|
return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
|
||||||
}
|
}
|
||||||
Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
|
|
||||||
Constant *StrConstant = ConstantArray::get(Context, Str, true);
|
/// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
|
||||||
Module &M = *BB->getParent()->getParent();
|
/// with "i8*" type instead of a pointer to array of i8.
|
||||||
GlobalVariable *gv = new GlobalVariable(M,
|
|
||||||
StrConstant->getType(),
|
|
||||||
true,
|
|
||||||
GlobalValue::InternalLinkage,
|
|
||||||
StrConstant,
|
|
||||||
"",
|
|
||||||
0,
|
|
||||||
false);
|
|
||||||
gv->setName(Name);
|
|
||||||
return gv;
|
|
||||||
}
|
|
||||||
Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
|
Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
|
||||||
Value *gv = CreateGlobalString(Str, Name);
|
Value *gv = CreateGlobalString(Str, Name);
|
||||||
Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
|
Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
|
||||||
Value *Args[] = { zero, zero };
|
Value *Args[] = { zero, zero };
|
||||||
return CreateInBoundsGEP(gv, Args, Args+2, Name);
|
return CreateInBoundsGEP(gv, Args, Args+2, Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
// Instruction creation methods: Cast/Conversion Operators
|
// Instruction creation methods: Cast/Conversion Operators
|
||||||
//===--------------------------------------------------------------------===//
|
//===--------------------------------------------------------------------===//
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include "llvm/BasicBlock.h"
|
#include "llvm/BasicBlock.h"
|
||||||
#include "llvm/Constants.h"
|
#include "llvm/Constants.h"
|
||||||
#include "llvm/DerivedTypes.h"
|
#include "llvm/DerivedTypes.h"
|
||||||
|
#include "llvm/GlobalVariable.h"
|
||||||
#include "llvm/Function.h"
|
#include "llvm/Function.h"
|
||||||
#include "llvm/IntrinsicInst.h"
|
#include "llvm/IntrinsicInst.h"
|
||||||
#include "llvm/LLVMContext.h"
|
#include "llvm/LLVMContext.h"
|
||||||
|
31
lib/VMCore/IRBuilder.cpp
Normal file
31
lib/VMCore/IRBuilder.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
//===---- IRBuilder.cpp - Builder for LLVM Instrs -------------------------===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This file implements the IRBuilder class, which is used as a convenient way
|
||||||
|
// to create LLVM instructions with a consistent and simplified interface.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/Support/IRBuilder.h"
|
||||||
|
#include "llvm/GlobalVariable.h"
|
||||||
|
using namespace llvm;
|
||||||
|
|
||||||
|
/// CreateGlobalString - Make a new global variable with an initializer that
|
||||||
|
/// has array of i8 type filled in the the nul terminated string value
|
||||||
|
/// specified. If Name is specified, it is the name of the global variable
|
||||||
|
/// created.
|
||||||
|
Value *IRBuilderBase::CreateGlobalString(const char *Str, const Twine &Name) {
|
||||||
|
Constant *StrConstant = ConstantArray::get(Context, Str, true);
|
||||||
|
Module &M = *BB->getParent()->getParent();
|
||||||
|
GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
|
||||||
|
true, GlobalValue::InternalLinkage,
|
||||||
|
StrConstant, "", 0, false);
|
||||||
|
GV->setName(Name);
|
||||||
|
return GV;
|
||||||
|
}
|
Reference in New Issue
Block a user