2008-08-13 10:56:48 +02:00
|
|
|
//======-- llvm/Support/NoFolder.h - Constant folding helper -*- C++ -*-======//
|
2008-08-12 22:39:27 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-08-13 10:56:48 +02:00
|
|
|
// This file defines the NoFolder class, a helper for IRBuilder. It provides
|
2008-08-12 22:39:27 +02:00
|
|
|
// IRBuilder with a set of methods for creating unfolded constants. This is
|
|
|
|
// useful for learners trying to understand how LLVM IR works, and who don't
|
|
|
|
// want details to be hidden by the constant folder. For general constant
|
|
|
|
// creation and folding, use ConstantExpr and the routines in
|
|
|
|
// llvm/Analysis/ConstantFolding.h.
|
|
|
|
//
|
|
|
|
// Note: since it is not actually possible to create unfolded constants, this
|
2010-11-18 03:15:42 +01:00
|
|
|
// class returns instructions rather than constants.
|
2008-08-12 22:39:27 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-28 01:52:46 +01:00
|
|
|
#ifndef LLVM_SUPPORT_NOFOLDER_H
|
|
|
|
#define LLVM_SUPPORT_NOFOLDER_H
|
2008-08-12 22:39:27 +02:00
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2009-08-11 19:45:13 +02:00
|
|
|
class LLVMContext;
|
2009-08-11 17:56:12 +02:00
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
/// NoFolder - Create "constants" (actually, instructions) with no folding.
|
2008-08-13 10:56:48 +02:00
|
|
|
class NoFolder {
|
2008-08-12 22:39:27 +02:00
|
|
|
public:
|
2009-08-11 17:56:12 +02:00
|
|
|
explicit NoFolder(LLVMContext &) {}
|
2008-08-12 22:39:27 +02:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Binary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateAdd(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateAdd(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
|
2009-08-11 22:20:39 +02:00
|
|
|
return BinaryOperator::CreateNSWAdd(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
|
2010-02-02 13:53:04 +01:00
|
|
|
return BinaryOperator::CreateNUWAdd(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
|
2009-06-05 00:49:04 +02:00
|
|
|
return BinaryOperator::CreateFAdd(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateSub(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateSub(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
|
2009-09-26 17:35:35 +02:00
|
|
|
return BinaryOperator::CreateNSWSub(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
|
2010-02-02 13:53:04 +01:00
|
|
|
return BinaryOperator::CreateNUWSub(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
|
2009-06-05 00:49:04 +02:00
|
|
|
return BinaryOperator::CreateFSub(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateMul(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateMul(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
|
2009-12-18 04:10:26 +01:00
|
|
|
return BinaryOperator::CreateNSWMul(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
|
2010-02-02 13:53:04 +01:00
|
|
|
return BinaryOperator::CreateNUWMul(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
|
2009-06-05 00:49:04 +02:00
|
|
|
return BinaryOperator::CreateFMul(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateUDiv(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateUDiv(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateSDiv(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateSDiv(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
|
2009-08-11 19:05:24 +02:00
|
|
|
return BinaryOperator::CreateExactSDiv(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateFDiv(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateURem(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateSRem(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateFRem(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateShl(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateShl(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateLShr(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateLShr(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateAShr(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateAShr(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateAnd(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateOr(LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateXor(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateBinOp(Instruction::BinaryOps Opc,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::Create(Opc, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Unary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNeg(Constant *C) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateNeg(C);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNSWNeg(Constant *C) const {
|
2009-12-18 03:58:50 +01:00
|
|
|
return BinaryOperator::CreateNSWNeg(C);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateNUWNeg(Constant *C) const {
|
2010-02-02 13:53:04 +01:00
|
|
|
return BinaryOperator::CreateNUWNeg(C);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateFNeg(Constant *C) const {
|
|
|
|
return BinaryOperator::CreateFNeg(C);
|
|
|
|
}
|
|
|
|
Instruction *CreateNot(Constant *C) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return BinaryOperator::CreateNot(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Memory Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
|
2008-08-12 22:39:27 +02:00
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateGetElementPtr(Constant *C, Value* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
|
|
|
|
}
|
|
|
|
|
2009-08-11 19:57:01 +02:00
|
|
|
Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2009-09-04 01:34:49 +02:00
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
|
2009-08-11 19:57:01 +02:00
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2009-08-11 19:57:01 +02:00
|
|
|
return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
|
|
|
|
}
|
|
|
|
|
2008-08-12 22:39:27 +02:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Cast/Conversion Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
|
2008-08-12 22:39:27 +02:00
|
|
|
const Type *DestTy) const {
|
|
|
|
return CastInst::Create(Op, C, DestTy);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreatePointerCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return CastInst::CreatePointerCast(C, DestTy);
|
|
|
|
}
|
|
|
|
Instruction *CreateIntCast(Constant *C, const Type *DestTy,
|
2008-08-12 22:39:27 +02:00
|
|
|
bool isSigned) const {
|
|
|
|
return CastInst::CreateIntegerCast(C, DestTy, isSigned);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateFPCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return CastInst::CreateFPCast(C, DestTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *CreateBitCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return CreateCast(Instruction::BitCast, C, DestTy);
|
|
|
|
}
|
|
|
|
Instruction *CreateIntToPtr(Constant *C, const Type *DestTy) const {
|
|
|
|
return CreateCast(Instruction::IntToPtr, C, DestTy);
|
|
|
|
}
|
|
|
|
Instruction *CreatePtrToInt(Constant *C, const Type *DestTy) const {
|
|
|
|
return CreateCast(Instruction::PtrToInt, C, DestTy);
|
|
|
|
}
|
|
|
|
Instruction *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return CastInst::CreateZExtOrBitCast(C, DestTy);
|
|
|
|
}
|
|
|
|
Instruction *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return CastInst::CreateSExtOrBitCast(C, DestTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return CastInst::CreateTruncOrBitCast(C, DestTy);
|
|
|
|
}
|
2008-08-12 22:39:27 +02:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Compare Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateICmp(CmpInst::Predicate P,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return new ICmpInst(P, LHS, RHS);
|
|
|
|
}
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateFCmp(CmpInst::Predicate P,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return new FCmpInst(P, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Other Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateSelect(Constant *C,
|
|
|
|
Constant *True, Constant *False) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return SelectInst::Create(C, True, False);
|
|
|
|
}
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
|
2009-11-22 19:27:43 +01:00
|
|
|
return ExtractElementInst::Create(Vec, Idx);
|
2008-08-12 22:39:27 +02:00
|
|
|
}
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
|
|
|
|
Constant *Idx) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return InsertElementInst::Create(Vec, NewElt, Idx);
|
|
|
|
}
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
|
|
|
|
Constant *Mask) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return new ShuffleVectorInst(V1, V2, Mask);
|
|
|
|
}
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
|
|
|
|
}
|
|
|
|
|
2010-11-18 03:15:42 +01:00
|
|
|
Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
|
|
|
|
const unsigned *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2008-08-12 22:39:27 +02:00
|
|
|
return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|