2008-04-13 06:22:09 +00:00
|
|
|
//===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
|
2007-05-27 15:09:34 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-27 15:09:34 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-04-13 06:22:09 +00:00
|
|
|
// This file defines the IRBuilder class, which is used as a convenient way
|
2007-05-27 15:09:34 +00:00
|
|
|
// to create LLVM instructions with a consistent and simplified interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-13 06:22:09 +00:00
|
|
|
#ifndef LLVM_SUPPORT_IRBUILDER_H
|
|
|
|
#define LLVM_SUPPORT_IRBUILDER_H
|
2007-05-27 15:09:34 +00:00
|
|
|
|
2007-10-09 19:49:19 +00:00
|
|
|
#include "llvm/Constants.h"
|
2008-08-11 15:29:30 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2009-06-17 22:20:46 +00:00
|
|
|
#include "llvm/GlobalAlias.h"
|
2008-07-02 22:57:59 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/Function.h"
|
2009-07-08 20:50:47 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2008-08-11 15:29:30 +00:00
|
|
|
#include "llvm/Support/ConstantFolder.h"
|
2007-05-27 15:09:34 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2008-04-13 06:22:09 +00:00
|
|
|
/// IRBuilder - This provides a uniform API for creating instructions and
|
2008-08-11 15:29:30 +00:00
|
|
|
/// inserting them into a basic block: either at the end of a BasicBlock, or
|
2007-05-27 15:09:34 +00:00
|
|
|
/// at a specific iterator location in a block.
|
|
|
|
///
|
|
|
|
/// Note that the builder does not expose the full generality of LLVM
|
|
|
|
/// instructions. For example, it cannot be used to create instructions with
|
|
|
|
/// arbitrary names (specifically, names with nul characters in them) - It only
|
|
|
|
/// supports nul-terminated C strings. For fully generic names, use
|
|
|
|
/// I->setName(). For access to extra instruction properties, use the mutators
|
|
|
|
/// (e.g. setVolatile) on the instructions after they have been created.
|
2008-08-11 15:29:30 +00:00
|
|
|
/// 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.
|
|
|
|
template <bool preserveNames=true, typename T = ConstantFolder> class IRBuilder{
|
2007-05-27 15:09:34 +00:00
|
|
|
BasicBlock *BB;
|
|
|
|
BasicBlock::iterator InsertPt;
|
2009-07-08 20:50:47 +00:00
|
|
|
LLVMContext &Context;
|
2009-07-10 00:49:53 +00:00
|
|
|
T Folder;
|
2007-05-27 15:09:34 +00:00
|
|
|
public:
|
2009-07-10 00:49:53 +00:00
|
|
|
IRBuilder(LLVMContext &C, const T& F) :
|
|
|
|
Context(C), Folder(F) { ClearInsertionPoint(); }
|
|
|
|
|
2009-08-11 15:57:42 +00:00
|
|
|
explicit IRBuilder(LLVMContext &C) : Context(C), Folder(C) {
|
|
|
|
ClearInsertionPoint();
|
|
|
|
}
|
2009-07-08 20:50:47 +00:00
|
|
|
|
2009-07-10 00:49:53 +00:00
|
|
|
explicit IRBuilder(BasicBlock *TheBB, const T& F)
|
2009-07-22 00:24:57 +00:00
|
|
|
: Context(TheBB->getContext()), Folder(F) {
|
2009-07-08 20:50:47 +00:00
|
|
|
SetInsertPoint(TheBB);
|
|
|
|
}
|
|
|
|
|
2009-07-10 00:49:53 +00:00
|
|
|
explicit IRBuilder(BasicBlock *TheBB)
|
2009-07-22 00:24:57 +00:00
|
|
|
: Context(TheBB->getContext()), Folder(Context) {
|
2009-07-10 00:49:53 +00:00
|
|
|
SetInsertPoint(TheBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
|
2009-07-22 00:24:57 +00:00
|
|
|
: Context(TheBB->getContext()), Folder(F) {
|
2009-07-10 00:49:53 +00:00
|
|
|
SetInsertPoint(TheBB, IP);
|
|
|
|
}
|
|
|
|
|
|
|
|
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
|
2009-07-22 00:24:57 +00:00
|
|
|
: Context(TheBB->getContext()), Folder(Context) {
|
2009-07-08 20:50:47 +00:00
|
|
|
SetInsertPoint(TheBB, IP);
|
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
|
|
|
/// getFolder - Get the constant folder being used.
|
|
|
|
const T& getFolder() { return Folder; }
|
2007-05-27 15:09:34 +00:00
|
|
|
|
2009-03-22 00:18:18 +00:00
|
|
|
/// isNamePreserving - Return true if this builder is configured to actually
|
|
|
|
/// add the requested names to IR created through it.
|
|
|
|
bool isNamePreserving() const { return preserveNames; }
|
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Builder configuration methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ClearInsertionPoint - Clear the insertion point: created instructions will
|
|
|
|
/// not be inserted into a block.
|
|
|
|
void ClearInsertionPoint() {
|
|
|
|
BB = 0;
|
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
BasicBlock *GetInsertBlock() const { return BB; }
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2009-05-20 21:45:41 +00:00
|
|
|
BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
|
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
/// SetInsertPoint - This specifies that created instructions should be
|
|
|
|
/// appended to the end of the specified block.
|
|
|
|
void SetInsertPoint(BasicBlock *TheBB) {
|
|
|
|
BB = TheBB;
|
|
|
|
InsertPt = BB->end();
|
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
/// SetInsertPoint - This specifies that created instructions should be
|
|
|
|
/// inserted at the specified point.
|
|
|
|
void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
|
|
|
|
BB = TheBB;
|
|
|
|
InsertPt = IP;
|
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
/// Insert - Insert and return the specified instruction.
|
|
|
|
template<typename InstTy>
|
2008-06-27 18:49:21 +00:00
|
|
|
InstTy *Insert(InstTy *I, const char *Name = "") const {
|
|
|
|
InsertHelper(I, Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
return I;
|
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
/// InsertHelper - Insert the specified instruction at the specified insertion
|
|
|
|
/// point. This is split out of Insert so that it isn't duplicated for every
|
|
|
|
/// template instantiation.
|
2008-06-27 18:49:21 +00:00
|
|
|
void InsertHelper(Instruction *I, const char *Name) const {
|
2007-05-27 15:09:34 +00:00
|
|
|
if (BB) BB->getInstList().insert(InsertPt, I);
|
2008-08-08 19:39:37 +00:00
|
|
|
if (preserveNames && Name[0])
|
2008-06-27 18:49:21 +00:00
|
|
|
I->setName(Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Instruction creation methods: Terminators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// CreateRetVoid - Create a 'ret void' instruction.
|
|
|
|
ReturnInst *CreateRetVoid() {
|
2009-08-13 21:58:54 +00:00
|
|
|
return Insert(ReturnInst::Create(getGlobalContext()));
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
2008-08-11 15:29:30 +00:00
|
|
|
/// @verbatim
|
|
|
|
/// CreateRet - Create a 'ret <val>' instruction.
|
2007-08-06 17:10:29 +00:00
|
|
|
/// @endverbatim
|
2007-05-27 15:09:34 +00:00
|
|
|
ReturnInst *CreateRet(Value *V) {
|
2009-08-13 21:58:54 +00:00
|
|
|
return Insert(ReturnInst::Create(getGlobalContext(), V));
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-08 07:30:13 +00:00
|
|
|
|
2008-07-25 20:36:15 +00:00
|
|
|
/// CreateAggregateRet - Create a sequence of N insertvalue instructions,
|
|
|
|
/// with one Value from the retVals array each, that build a aggregate
|
|
|
|
/// return value one value at a time, and a ret instruction to return
|
|
|
|
/// the resulting aggregate value. This is a convenience function for
|
|
|
|
/// code that uses aggregate return values as a vehicle for having
|
|
|
|
/// multiple return values.
|
|
|
|
///
|
|
|
|
ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
|
2008-07-23 00:34:11 +00:00
|
|
|
const Type *RetType = BB->getParent()->getReturnType();
|
2009-07-30 23:03:37 +00:00
|
|
|
Value *V = UndefValue::get(RetType);
|
2008-07-23 00:34:11 +00:00
|
|
|
for (unsigned i = 0; i != N; ++i)
|
|
|
|
V = CreateInsertValue(V, retVals[i], i, "mrv");
|
2009-08-13 21:58:54 +00:00
|
|
|
return Insert(ReturnInst::Create(getGlobalContext(), V));
|
2008-04-08 07:30:13 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
/// CreateBr - Create an unconditional 'br label X' instruction.
|
|
|
|
BranchInst *CreateBr(BasicBlock *Dest) {
|
2008-04-06 20:25:17 +00:00
|
|
|
return Insert(BranchInst::Create(Dest));
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
|
|
|
|
/// instruction.
|
|
|
|
BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
|
2008-04-06 20:25:17 +00:00
|
|
|
return Insert(BranchInst::Create(True, False, Cond));
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
/// CreateSwitch - Create a switch instruction with the specified value,
|
|
|
|
/// default dest, and with a hint for the number of cases that will be added
|
|
|
|
/// (for efficient allocation).
|
|
|
|
SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
|
2008-04-06 20:25:17 +00:00
|
|
|
return Insert(SwitchInst::Create(V, Dest, NumCases));
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
/// CreateInvoke - Create an invoke instruction.
|
2007-08-27 19:04:21 +00:00
|
|
|
template<typename InputIterator>
|
2008-08-11 15:29:30 +00:00
|
|
|
InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
|
|
|
|
BasicBlock *UnwindDest, InputIterator ArgBegin,
|
2007-08-27 19:04:21 +00:00
|
|
|
InputIterator ArgEnd, const char *Name = "") {
|
2009-06-18 20:05:31 +00:00
|
|
|
return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
|
|
|
|
ArgBegin, ArgEnd), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
UnwindInst *CreateUnwind() {
|
2009-08-13 21:58:54 +00:00
|
|
|
return Insert(new UnwindInst(Context));
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnreachableInst *CreateUnreachable() {
|
2009-08-13 21:58:54 +00:00
|
|
|
return Insert(new UnreachableInst(Context));
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Instruction creation methods: Binary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateAdd(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2009-08-11 20:20:39 +00:00
|
|
|
Value *CreateNSWAdd(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
|
|
|
return Folder.CreateNSWAdd(LC, RC);
|
|
|
|
return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
|
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
|
|
|
return Folder.CreateFAdd(LC, RC);
|
|
|
|
return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
|
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateSub(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Value *CreateFSub(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
|
|
|
return Folder.CreateFSub(LC, RC);
|
|
|
|
return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
|
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateMul(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Value *CreateFMul(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2009-06-17 06:31:02 +00:00
|
|
|
return Folder.CreateFMul(LC, RC);
|
2009-06-04 22:49:04 +00:00
|
|
|
return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
|
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateUDiv(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateSDiv(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2009-08-11 17:05:24 +00:00
|
|
|
Value *CreateExactSDiv(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
|
|
|
return Folder.CreateExactSDiv(LC, RC);
|
|
|
|
return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
|
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateFDiv(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateURem(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateSRem(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateFRem(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateShl(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateLShr(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateAShr(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateAnd(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateOr(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateXor(LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
2008-07-23 06:58:10 +00:00
|
|
|
Value *CreateBinOp(Instruction::BinaryOps Opc,
|
|
|
|
Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateBinOp(Opc, LC, RC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2008-07-23 06:58:10 +00:00
|
|
|
Value *CreateNeg(Value *V, const char *Name = "") {
|
|
|
|
if (Constant *VC = dyn_cast<Constant>(V))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateNeg(VC);
|
2009-08-12 16:23:25 +00:00
|
|
|
return Insert(BinaryOperator::CreateNeg(V), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2009-06-15 22:50:40 +00:00
|
|
|
Value *CreateFNeg(Value *V, const char *Name = "") {
|
|
|
|
if (Constant *VC = dyn_cast<Constant>(V))
|
|
|
|
return Folder.CreateFNeg(VC);
|
2009-08-12 16:23:25 +00:00
|
|
|
return Insert(BinaryOperator::CreateFNeg(V), Name);
|
2009-06-15 22:50:40 +00:00
|
|
|
}
|
2008-07-23 06:58:10 +00:00
|
|
|
Value *CreateNot(Value *V, const char *Name = "") {
|
|
|
|
if (Constant *VC = dyn_cast<Constant>(V))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateNot(VC);
|
2009-08-12 16:23:25 +00:00
|
|
|
return Insert(BinaryOperator::CreateNot(V), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Instruction creation methods: Memory Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
|
|
|
|
const char *Name = "") {
|
2009-07-15 23:53:25 +00:00
|
|
|
return Insert(new MallocInst(Ty, ArraySize), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
|
|
|
|
const char *Name = "") {
|
2009-07-15 23:53:25 +00:00
|
|
|
return Insert(new AllocaInst(Ty, ArraySize), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
FreeInst *CreateFree(Value *Ptr) {
|
|
|
|
return Insert(new FreeInst(Ptr));
|
|
|
|
}
|
2008-08-09 15:14:59 +00:00
|
|
|
LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
|
2008-08-09 06:26:23 +00:00
|
|
|
return Insert(new LoadInst(Ptr), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
2008-08-09 15:14:59 +00:00
|
|
|
LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
|
2008-08-09 06:26:23 +00:00
|
|
|
return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
|
|
|
|
return Insert(new StoreInst(Val, Ptr, isVolatile));
|
|
|
|
}
|
2007-12-17 19:06:26 +00:00
|
|
|
template<typename InputIterator>
|
2008-11-15 20:32:33 +00:00
|
|
|
Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
|
|
|
|
const char *Name = "") {
|
2007-12-17 19:06:26 +00:00
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr)) {
|
|
|
|
// Every index must be constant.
|
|
|
|
InputIterator i;
|
2009-08-11 17:57:01 +00:00
|
|
|
for (i = IdxBegin; i < IdxEnd; ++i)
|
2009-08-11 17:20:16 +00:00
|
|
|
if (!isa<Constant>(*i))
|
2007-12-17 19:06:26 +00:00
|
|
|
break;
|
|
|
|
if (i == IdxEnd)
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
|
|
|
|
}
|
2008-08-09 06:26:23 +00:00
|
|
|
return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
|
2007-12-17 19:06:26 +00:00
|
|
|
}
|
2009-08-11 17:57:01 +00:00
|
|
|
template<typename InputIterator>
|
|
|
|
Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
|
|
|
|
const char *Name = "") {
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr)) {
|
|
|
|
// Every index must be constant.
|
|
|
|
InputIterator i;
|
|
|
|
for (i = IdxBegin; i < IdxEnd; ++i)
|
|
|
|
if (!isa<Constant>(*i))
|
|
|
|
break;
|
|
|
|
if (i == IdxEnd)
|
|
|
|
return Folder.CreateInBoundsGetElementPtr(PC,
|
|
|
|
&IdxBegin[0],
|
|
|
|
IdxEnd - IdxBegin);
|
|
|
|
}
|
|
|
|
return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
|
|
|
|
Name);
|
|
|
|
}
|
2007-12-17 19:06:26 +00:00
|
|
|
Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
if (Constant *IC = dyn_cast<Constant>(Idx))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateGetElementPtr(PC, &IC, 1);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
|
2008-04-13 06:22:09 +00:00
|
|
|
}
|
2009-08-11 17:57:01 +00:00
|
|
|
Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const char *Name = "") {
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
if (Constant *IC = dyn_cast<Constant>(Idx))
|
|
|
|
return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
|
|
|
|
return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
|
|
|
|
}
|
2009-03-06 22:26:07 +00:00
|
|
|
Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
|
2009-03-06 22:26:07 +00:00
|
|
|
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
return Folder.CreateGetElementPtr(PC, &Idx, 1);
|
|
|
|
|
|
|
|
return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
|
|
|
|
}
|
2009-08-11 17:57:01 +00:00
|
|
|
Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
|
|
|
|
const char *Name = "") {
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
|
2009-08-11 17:57:01 +00:00
|
|
|
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
|
|
|
|
|
|
|
|
return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
|
|
|
|
}
|
2009-03-06 22:26:07 +00:00
|
|
|
Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
|
|
|
|
const char *Name = "") {
|
2009-04-10 06:54:06 +00:00
|
|
|
Value *Idxs[] = {
|
2009-08-13 21:58:54 +00:00
|
|
|
ConstantInt::get(Type::getInt32Ty(Context), Idx0),
|
|
|
|
ConstantInt::get(Type::getInt32Ty(Context), Idx1)
|
2009-03-06 22:26:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
return Folder.CreateGetElementPtr(PC, Idxs, 2);
|
|
|
|
|
|
|
|
return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
|
|
|
|
}
|
2009-08-11 17:57:01 +00:00
|
|
|
Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
|
|
|
|
const char *Name = "") {
|
|
|
|
Value *Idxs[] = {
|
2009-08-13 21:58:54 +00:00
|
|
|
ConstantInt::get(Type::getInt32Ty(Context), Idx0),
|
|
|
|
ConstantInt::get(Type::getInt32Ty(Context), Idx1)
|
2009-08-11 17:57:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
|
|
|
|
|
|
|
|
return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
|
|
|
|
}
|
2009-03-06 22:26:07 +00:00
|
|
|
Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
|
2009-03-06 22:26:07 +00:00
|
|
|
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
return Folder.CreateGetElementPtr(PC, &Idx, 1);
|
|
|
|
|
|
|
|
return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
|
|
|
|
}
|
2009-08-11 17:57:01 +00:00
|
|
|
Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
|
|
|
|
const char *Name = "") {
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
|
2009-08-11 17:57:01 +00:00
|
|
|
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
|
|
|
|
|
|
|
|
return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
|
|
|
|
}
|
|
|
|
Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
|
2009-03-06 22:26:07 +00:00
|
|
|
const char *Name = "") {
|
2009-04-10 06:54:06 +00:00
|
|
|
Value *Idxs[] = {
|
2009-08-13 21:58:54 +00:00
|
|
|
ConstantInt::get(Type::getInt64Ty(Context), Idx0),
|
|
|
|
ConstantInt::get(Type::getInt64Ty(Context), Idx1)
|
2008-04-13 06:22:09 +00:00
|
|
|
};
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2008-04-13 06:22:09 +00:00
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateGetElementPtr(PC, Idxs, 2);
|
|
|
|
|
2009-03-06 22:26:07 +00:00
|
|
|
return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
|
|
|
|
}
|
2009-08-11 17:57:01 +00:00
|
|
|
Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
|
|
|
|
const char *Name = "") {
|
|
|
|
Value *Idxs[] = {
|
2009-08-13 21:58:54 +00:00
|
|
|
ConstantInt::get(Type::getInt64Ty(Context), Idx0),
|
|
|
|
ConstantInt::get(Type::getInt64Ty(Context), Idx1)
|
2009-08-11 17:57:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Constant *PC = dyn_cast<Constant>(Ptr))
|
|
|
|
return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
|
|
|
|
|
|
|
|
return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
|
|
|
|
}
|
2009-03-06 22:26:07 +00:00
|
|
|
Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
|
2009-08-11 17:57:01 +00:00
|
|
|
return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
|
2007-12-17 19:06:26 +00:00
|
|
|
}
|
2008-07-02 22:57:59 +00:00
|
|
|
Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
|
2009-08-13 21:58:54 +00:00
|
|
|
Constant *StrConstant = ConstantArray::get(Context, Str, true);
|
2009-07-09 00:52:44 +00:00
|
|
|
Module &M = *BB->getParent()->getParent();
|
|
|
|
GlobalVariable *gv = new GlobalVariable(M,
|
2009-07-08 01:26:06 +00:00
|
|
|
StrConstant->getType(),
|
2009-04-10 06:54:06 +00:00
|
|
|
true,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
StrConstant,
|
|
|
|
"",
|
2009-07-08 19:03:57 +00:00
|
|
|
0,
|
2009-04-10 06:54:06 +00:00
|
|
|
false);
|
2008-07-02 22:57:59 +00:00
|
|
|
gv->setName(Name);
|
|
|
|
return gv;
|
|
|
|
}
|
|
|
|
Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
|
|
|
|
Value *gv = CreateGlobalString(Str, Name);
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
|
2008-07-02 22:57:59 +00:00
|
|
|
Value *Args[] = { zero, zero };
|
2009-08-11 17:57:01 +00:00
|
|
|
return CreateInBoundsGEP(gv, Args, Args+2, Name);
|
2008-07-02 22:57:59 +00:00
|
|
|
}
|
2007-12-17 19:06:26 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Instruction creation methods: Cast/Conversion Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-12-17 19:06:26 +00:00
|
|
|
Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
|
|
|
|
return CreateCast(Instruction::Trunc, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
|
|
|
|
return CreateCast(Instruction::ZExt, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
|
|
|
|
return CreateCast(Instruction::SExt, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
|
|
|
|
return CreateCast(Instruction::FPToUI, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
|
|
|
|
return CreateCast(Instruction::FPToSI, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
|
|
|
|
return CreateCast(Instruction::UIToFP, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
|
|
|
|
return CreateCast(Instruction::SIToFP, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFPTrunc(Value *V, const Type *DestTy,
|
|
|
|
const char *Name = "") {
|
|
|
|
return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
|
|
|
|
return CreateCast(Instruction::FPExt, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreatePtrToInt(Value *V, const Type *DestTy,
|
|
|
|
const char *Name = "") {
|
|
|
|
return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateIntToPtr(Value *V, const Type *DestTy,
|
|
|
|
const char *Name = "") {
|
|
|
|
return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
|
|
|
|
}
|
|
|
|
Value *CreateBitCast(Value *V, const Type *DestTy,
|
|
|
|
const char *Name = "") {
|
|
|
|
return CreateCast(Instruction::BitCast, V, DestTy, Name);
|
|
|
|
}
|
2008-04-04 08:28:13 +00:00
|
|
|
|
2007-12-17 19:06:26 +00:00
|
|
|
Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
|
2008-11-15 20:32:33 +00:00
|
|
|
const char *Name = "") {
|
2008-04-04 08:28:13 +00:00
|
|
|
if (V->getType() == DestTy)
|
|
|
|
return V;
|
2007-12-17 19:06:26 +00:00
|
|
|
if (Constant *VC = dyn_cast<Constant>(V))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateCast(Op, VC, DestTy);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(CastInst::Create(Op, V, DestTy), Name);
|
2007-12-17 19:06:26 +00:00
|
|
|
}
|
|
|
|
Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
|
2008-11-15 20:32:33 +00:00
|
|
|
const char *Name = "") {
|
2008-04-04 08:28:13 +00:00
|
|
|
if (V->getType() == DestTy)
|
|
|
|
return V;
|
2007-12-17 19:06:26 +00:00
|
|
|
if (Constant *VC = dyn_cast<Constant>(V))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateIntCast(VC, DestTy, isSigned);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
|
2007-12-17 19:06:26 +00:00
|
|
|
}
|
2008-04-04 08:28:13 +00:00
|
|
|
|
2007-10-09 19:49:19 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Instruction creation methods: Compare Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-10-09 19:49:19 +00:00
|
|
|
Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
|
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-10-09 19:49:19 +00:00
|
|
|
Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
|
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
|
2008-08-11 15:29:30 +00:00
|
|
|
Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
|
2008-05-14 20:29:46 +00:00
|
|
|
const char *Name = "") {
|
2007-10-09 21:41:00 +00:00
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
2007-10-09 19:49:19 +00:00
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-12 20:39:27 +00:00
|
|
|
return Folder.CreateICmp(P, LC, RC);
|
2009-07-09 23:48:35 +00:00
|
|
|
return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
|
2007-10-09 19:49:19 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
|
2008-05-14 20:29:46 +00:00
|
|
|
const char *Name = "") {
|
2007-10-09 21:41:00 +00:00
|
|
|
if (Constant *LC = dyn_cast<Constant>(LHS))
|
2007-10-09 19:49:19 +00:00
|
|
|
if (Constant *RC = dyn_cast<Constant>(RHS))
|
2008-08-12 20:39:27 +00:00
|
|
|
return Folder.CreateFCmp(P, LC, RC);
|
2009-07-09 23:48:35 +00:00
|
|
|
return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
|
2007-10-09 19:49:19 +00:00
|
|
|
}
|
2007-12-17 19:06:26 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Instruction creation methods: Other Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
2008-04-13 06:22:09 +00:00
|
|
|
|
|
|
|
PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(PHINode::Create(Ty), Name);
|
2008-04-13 06:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CallInst *CreateCall(Value *Callee, const char *Name = "") {
|
2009-06-18 20:05:31 +00:00
|
|
|
return Insert(CallInst::Create(Callee), Name);
|
2008-04-13 06:22:09 +00:00
|
|
|
}
|
|
|
|
CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
|
2009-06-18 20:05:31 +00:00
|
|
|
return Insert(CallInst::Create(Callee, Arg), Name);
|
2008-04-13 06:22:09 +00:00
|
|
|
}
|
2008-05-01 05:11:00 +00:00
|
|
|
CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
|
|
|
|
const char *Name = "") {
|
|
|
|
Value *Args[] = { Arg1, Arg2 };
|
2009-06-18 20:05:31 +00:00
|
|
|
return Insert(CallInst::Create(Callee, Args, Args+2), Name);
|
2008-05-01 05:11:00 +00:00
|
|
|
}
|
2008-05-01 05:23:45 +00:00
|
|
|
CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
|
|
|
|
const char *Name = "") {
|
|
|
|
Value *Args[] = { Arg1, Arg2, Arg3 };
|
2009-06-18 20:05:31 +00:00
|
|
|
return Insert(CallInst::Create(Callee, Args, Args+3), Name);
|
2008-05-01 05:23:45 +00:00
|
|
|
}
|
|
|
|
CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
|
|
|
|
Value *Arg4, const char *Name = "") {
|
|
|
|
Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
|
2009-06-18 20:05:31 +00:00
|
|
|
return Insert(CallInst::Create(Callee, Args, Args+4), Name);
|
2008-05-01 05:23:45 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2008-04-13 06:22:09 +00:00
|
|
|
template<typename InputIterator>
|
2008-08-11 15:29:30 +00:00
|
|
|
CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
|
2008-05-01 05:11:00 +00:00
|
|
|
InputIterator ArgEnd, const char *Name = "") {
|
2009-06-18 20:05:31 +00:00
|
|
|
return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
|
2008-04-13 06:22:09 +00:00
|
|
|
}
|
|
|
|
|
2007-12-17 19:06:26 +00:00
|
|
|
Value *CreateSelect(Value *C, Value *True, Value *False,
|
2008-05-01 05:11:00 +00:00
|
|
|
const char *Name = "") {
|
2007-12-17 19:06:26 +00:00
|
|
|
if (Constant *CC = dyn_cast<Constant>(C))
|
|
|
|
if (Constant *TC = dyn_cast<Constant>(True))
|
|
|
|
if (Constant *FC = dyn_cast<Constant>(False))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateSelect(CC, TC, FC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(SelectInst::Create(C, True, False), Name);
|
2007-12-17 19:06:26 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
|
|
|
|
VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(new VAArgInst(List, Ty), Name);
|
2008-04-13 06:22:09 +00:00
|
|
|
}
|
|
|
|
|
2007-12-17 19:06:26 +00:00
|
|
|
Value *CreateExtractElement(Value *Vec, Value *Idx,
|
2008-11-15 20:32:33 +00:00
|
|
|
const char *Name = "") {
|
2007-12-17 19:06:26 +00:00
|
|
|
if (Constant *VC = dyn_cast<Constant>(Vec))
|
|
|
|
if (Constant *IC = dyn_cast<Constant>(Idx))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateExtractElement(VC, IC);
|
2009-07-25 02:28:41 +00:00
|
|
|
return Insert(ExtractElementInst::Create(Vec, Idx), Name);
|
2007-12-17 19:06:26 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
|
2007-12-17 19:06:26 +00:00
|
|
|
Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
|
|
|
|
const char *Name = "") {
|
|
|
|
if (Constant *VC = dyn_cast<Constant>(Vec))
|
|
|
|
if (Constant *NC = dyn_cast<Constant>(NewElt))
|
|
|
|
if (Constant *IC = dyn_cast<Constant>(Idx))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateInsertElement(VC, NC, IC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
|
2007-12-17 19:06:26 +00:00
|
|
|
}
|
2008-04-13 06:22:09 +00:00
|
|
|
|
2007-12-17 19:06:26 +00:00
|
|
|
Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
|
2008-11-15 20:32:33 +00:00
|
|
|
const char *Name = "") {
|
2007-12-17 19:06:26 +00:00
|
|
|
if (Constant *V1C = dyn_cast<Constant>(V1))
|
|
|
|
if (Constant *V2C = dyn_cast<Constant>(V2))
|
|
|
|
if (Constant *MC = dyn_cast<Constant>(Mask))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateShuffleVector(V1C, V2C, MC);
|
2008-06-27 18:49:21 +00:00
|
|
|
return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
|
2007-12-17 19:06:26 +00:00
|
|
|
}
|
2008-07-22 18:25:25 +00:00
|
|
|
|
2008-07-22 20:19:25 +00:00
|
|
|
Value *CreateExtractValue(Value *Agg, unsigned Idx,
|
|
|
|
const char *Name = "") {
|
|
|
|
if (Constant *AggC = dyn_cast<Constant>(Agg))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateExtractValue(AggC, &Idx, 1);
|
2008-07-22 18:25:25 +00:00
|
|
|
return Insert(ExtractValueInst::Create(Agg, Idx), Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator>
|
2008-07-22 20:19:25 +00:00
|
|
|
Value *CreateExtractValue(Value *Agg,
|
|
|
|
InputIterator IdxBegin,
|
|
|
|
InputIterator IdxEnd,
|
|
|
|
const char *Name = "") {
|
|
|
|
if (Constant *AggC = dyn_cast<Constant>(Agg))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
|
2008-07-22 18:25:25 +00:00
|
|
|
return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
|
|
|
|
}
|
|
|
|
|
2008-07-22 20:19:25 +00:00
|
|
|
Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
|
|
|
|
const char *Name = "") {
|
|
|
|
if (Constant *AggC = dyn_cast<Constant>(Agg))
|
|
|
|
if (Constant *ValC = dyn_cast<Constant>(Val))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
|
2008-07-22 18:25:25 +00:00
|
|
|
return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator>
|
2008-07-22 20:19:25 +00:00
|
|
|
Value *CreateInsertValue(Value *Agg, Value *Val,
|
|
|
|
InputIterator IdxBegin,
|
|
|
|
InputIterator IdxEnd,
|
|
|
|
const char *Name = "") {
|
|
|
|
if (Constant *AggC = dyn_cast<Constant>(Agg))
|
|
|
|
if (Constant *ValC = dyn_cast<Constant>(Val))
|
2008-08-11 15:29:30 +00:00
|
|
|
return Folder.CreateInsertValue(AggC, ValC,
|
2008-07-22 20:19:25 +00:00
|
|
|
IdxBegin, IdxEnd - IdxBegin);
|
2008-07-22 18:25:25 +00:00
|
|
|
return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
|
|
|
|
}
|
2008-09-27 23:22:55 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Utility creation methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// CreateIsNull - Return an i1 value testing if \arg Arg is null.
|
|
|
|
Value *CreateIsNull(Value *Arg, const char *Name = "") {
|
2009-07-31 20:28:14 +00:00
|
|
|
return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
|
2008-09-27 23:22:55 +00:00
|
|
|
Name);
|
|
|
|
}
|
|
|
|
|
2008-10-02 17:05:03 +00:00
|
|
|
/// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
|
|
|
|
Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
|
2009-07-31 20:28:14 +00:00
|
|
|
return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
|
2008-09-27 23:22:55 +00:00
|
|
|
Name);
|
|
|
|
}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-04-10 05:30:48 +00:00
|
|
|
/// CreatePtrDiff - Return the i64 difference between two pointer values,
|
|
|
|
/// dividing out the size of the pointed-to objects. This is intended to
|
2009-08-11 17:05:24 +00:00
|
|
|
/// implement C-style pointer subtraction. As such, the pointers must be
|
|
|
|
/// appropriately aligned for their element types and pointing into the
|
|
|
|
/// same object.
|
2009-04-10 05:30:48 +00:00
|
|
|
Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
|
|
|
|
assert(LHS->getType() == RHS->getType() &&
|
|
|
|
"Pointer subtraction operand types must match!");
|
|
|
|
const PointerType *ArgType = cast<PointerType>(LHS->getType());
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
|
|
|
|
Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
|
2009-04-10 05:30:48 +00:00
|
|
|
Value *Difference = CreateSub(LHS_int, RHS_int);
|
2009-08-11 17:05:24 +00:00
|
|
|
return CreateExactSDiv(Difference,
|
|
|
|
ConstantExpr::getSizeOf(ArgType->getElementType()),
|
|
|
|
Name);
|
2009-04-10 05:30:48 +00:00
|
|
|
}
|
2007-10-09 19:49:19 +00:00
|
|
|
};
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2007-05-27 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|