2008-08-13 08:56:48 +00:00
|
|
|
//======-- llvm/Support/NoFolder.h - Constant folding helper -*- C++ -*-======//
|
2008-08-12 20:39:27 +00: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 08:56:48 +00:00
|
|
|
// This file defines the NoFolder class, a helper for IRBuilder. It provides
|
2008-08-12 20:39:27 +00: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
|
|
|
|
// class returns values rather than constants. The values do not have names,
|
|
|
|
// even if names were provided to IRBuilder, which may be confusing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-28 00:52:46 +00:00
|
|
|
#ifndef LLVM_SUPPORT_NOFOLDER_H
|
|
|
|
#define LLVM_SUPPORT_NOFOLDER_H
|
2008-08-12 20:39:27 +00:00
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2009-08-11 17:45:13 +00:00
|
|
|
class LLVMContext;
|
2009-08-11 15:56:12 +00:00
|
|
|
|
2008-08-13 08:56:48 +00:00
|
|
|
/// NoFolder - Create "constants" (actually, values) with no folding.
|
|
|
|
class NoFolder {
|
2008-08-12 20:39:27 +00:00
|
|
|
public:
|
2009-08-11 15:56:12 +00:00
|
|
|
explicit NoFolder(LLVMContext &) {}
|
2008-08-12 20:39:27 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Binary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Value *CreateAdd(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateAdd(LHS, RHS);
|
|
|
|
}
|
2009-08-11 20:20:39 +00:00
|
|
|
Value *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateNSWAdd(LHS, RHS);
|
|
|
|
}
|
2010-02-02 12:53:04 +00:00
|
|
|
Value *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateNUWAdd(LHS, RHS);
|
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Value *CreateFAdd(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateFAdd(LHS, RHS);
|
|
|
|
}
|
2008-08-12 20:39:27 +00:00
|
|
|
Value *CreateSub(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateSub(LHS, RHS);
|
|
|
|
}
|
2009-09-26 15:35:35 +00:00
|
|
|
Value *CreateNSWSub(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateNSWSub(LHS, RHS);
|
|
|
|
}
|
2010-02-02 12:53:04 +00:00
|
|
|
Value *CreateNUWSub(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateNUWSub(LHS, RHS);
|
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Value *CreateFSub(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateFSub(LHS, RHS);
|
|
|
|
}
|
2008-08-12 20:39:27 +00:00
|
|
|
Value *CreateMul(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateMul(LHS, RHS);
|
|
|
|
}
|
2009-12-18 03:10:26 +00:00
|
|
|
Value *CreateNSWMul(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateNSWMul(LHS, RHS);
|
|
|
|
}
|
2010-02-02 12:53:04 +00:00
|
|
|
Value *CreateNUWMul(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateNUWMul(LHS, RHS);
|
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Value *CreateFMul(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateFMul(LHS, RHS);
|
|
|
|
}
|
2008-08-12 20:39:27 +00:00
|
|
|
Value *CreateUDiv(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateUDiv(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateSDiv(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateSDiv(LHS, RHS);
|
|
|
|
}
|
2009-08-11 17:05:24 +00:00
|
|
|
Value *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateExactSDiv(LHS, RHS);
|
|
|
|
}
|
2008-08-12 20:39:27 +00:00
|
|
|
Value *CreateFDiv(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateFDiv(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateURem(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateURem(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateSRem(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateSRem(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateFRem(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateFRem(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateShl(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateShl(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateLShr(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateLShr(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateAShr(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateAShr(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateAnd(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateAnd(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateOr(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateOr(LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateXor(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateXor(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *CreateBinOp(Instruction::BinaryOps Opc,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::Create(Opc, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Unary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Value *CreateNeg(Constant *C) const {
|
|
|
|
return BinaryOperator::CreateNeg(C);
|
|
|
|
}
|
2009-12-18 02:58:50 +00:00
|
|
|
Value *CreateNSWNeg(Constant *C) const {
|
|
|
|
return BinaryOperator::CreateNSWNeg(C);
|
|
|
|
}
|
2010-02-02 12:53:04 +00:00
|
|
|
Value *CreateNUWNeg(Constant *C) const {
|
|
|
|
return BinaryOperator::CreateNUWNeg(C);
|
|
|
|
}
|
2008-08-12 20:39:27 +00:00
|
|
|
Value *CreateNot(Constant *C) const {
|
|
|
|
return BinaryOperator::CreateNot(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Memory Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
|
|
|
Value *CreateGetElementPtr(Constant *C, Value* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
|
|
|
return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
|
|
|
|
}
|
|
|
|
|
2009-08-11 17:57:01 +00:00
|
|
|
Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2009-09-03 23:34:49 +00:00
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
|
2009-08-11 17:57:01 +00:00
|
|
|
}
|
|
|
|
Value *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
|
|
|
return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
|
|
|
|
}
|
|
|
|
|
2008-08-12 20:39:27 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Cast/Conversion Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Value *CreateCast(Instruction::CastOps Op, Constant *C,
|
|
|
|
const Type *DestTy) const {
|
|
|
|
return CastInst::Create(Op, C, DestTy);
|
|
|
|
}
|
|
|
|
Value *CreateIntCast(Constant *C, const Type *DestTy,
|
|
|
|
bool isSigned) const {
|
|
|
|
return CastInst::CreateIntegerCast(C, DestTy, isSigned);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Compare Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Value *CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
|
|
|
|
return new ICmpInst(P, LHS, RHS);
|
|
|
|
}
|
|
|
|
Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
|
|
|
|
return new FCmpInst(P, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Other Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Value *CreateSelect(Constant *C, Constant *True, Constant *False) const {
|
|
|
|
return SelectInst::Create(C, True, False);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *CreateExtractElement(Constant *Vec, Constant *Idx) const {
|
2009-11-22 18:27:43 +00:00
|
|
|
return ExtractElementInst::Create(Vec, Idx);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *CreateInsertElement(Constant *Vec, Constant *NewElt,
|
|
|
|
Constant *Idx) const {
|
|
|
|
return InsertElementInst::Create(Vec, NewElt, Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const {
|
|
|
|
return new ShuffleVectorInst(V1, V2, Mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
|
|
|
return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *CreateInsertValue(Constant *Agg, Constant *Val,
|
|
|
|
const unsigned *IdxList, unsigned NumIdx) const {
|
|
|
|
return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|