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
|
2010-11-18 02:15:42 +00:00
|
|
|
// class returns instructions rather than constants.
|
2008-08-12 20:39:27 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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
|
|
|
|
2011-07-13 10:26:04 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2008-08-12 20:39:27 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
/// NoFolder - Create "constants" (actually, instructions) with no folding.
|
2008-08-13 08:56:48 +00:00
|
|
|
class NoFolder {
|
2008-08-12 20:39:27 +00:00
|
|
|
public:
|
2011-04-03 19:46:28 +00:00
|
|
|
explicit NoFolder() {}
|
2008-08-12 20:39:27 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Binary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2011-02-25 07:13:35 +00:00
|
|
|
Instruction *CreateAdd(Constant *LHS, Constant *RHS,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS);
|
|
|
|
if (HasNUW) BO->setHasNoUnsignedWrap();
|
|
|
|
if (HasNSW) BO->setHasNoSignedWrap();
|
|
|
|
return BO;
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
|
2009-08-11 20:20:39 +00:00
|
|
|
return BinaryOperator::CreateNSWAdd(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
|
2010-02-02 12:53:04 +00:00
|
|
|
return BinaryOperator::CreateNUWAdd(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
|
2009-06-04 22:49:04 +00:00
|
|
|
return BinaryOperator::CreateFAdd(LHS, RHS);
|
|
|
|
}
|
2011-02-25 07:13:35 +00:00
|
|
|
Instruction *CreateSub(Constant *LHS, Constant *RHS,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
|
|
|
|
if (HasNUW) BO->setHasNoUnsignedWrap();
|
|
|
|
if (HasNSW) BO->setHasNoSignedWrap();
|
|
|
|
return BO;
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
|
2009-09-26 15:35:35 +00:00
|
|
|
return BinaryOperator::CreateNSWSub(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
|
2010-02-02 12:53:04 +00:00
|
|
|
return BinaryOperator::CreateNUWSub(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
|
2009-06-04 22:49:04 +00:00
|
|
|
return BinaryOperator::CreateFSub(LHS, RHS);
|
|
|
|
}
|
2011-02-25 07:13:35 +00:00
|
|
|
Instruction *CreateMul(Constant *LHS, Constant *RHS,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS);
|
|
|
|
if (HasNUW) BO->setHasNoUnsignedWrap();
|
|
|
|
if (HasNSW) BO->setHasNoSignedWrap();
|
|
|
|
return BO;
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
|
2009-12-18 03:10:26 +00:00
|
|
|
return BinaryOperator::CreateNSWMul(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
|
2010-02-02 12:53:04 +00:00
|
|
|
return BinaryOperator::CreateNUWMul(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
|
2009-06-04 22:49:04 +00:00
|
|
|
return BinaryOperator::CreateFMul(LHS, RHS);
|
|
|
|
}
|
2011-02-25 07:13:35 +00:00
|
|
|
Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
|
|
|
|
bool isExact = false) const {
|
|
|
|
if (!isExact)
|
|
|
|
return BinaryOperator::CreateUDiv(LHS, RHS);
|
|
|
|
return BinaryOperator::CreateExactUDiv(LHS, RHS);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2011-02-07 09:21:52 +00:00
|
|
|
Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
|
|
|
|
return BinaryOperator::CreateExactUDiv(LHS, RHS);
|
|
|
|
}
|
2011-02-25 07:13:35 +00:00
|
|
|
Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
|
|
|
|
bool isExact = false) const {
|
|
|
|
if (!isExact)
|
|
|
|
return BinaryOperator::CreateSDiv(LHS, RHS);
|
|
|
|
return BinaryOperator::CreateExactSDiv(LHS, RHS);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
|
2009-08-11 17:05:24 +00:00
|
|
|
return BinaryOperator::CreateExactSDiv(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::CreateFDiv(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::CreateURem(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::CreateSRem(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::CreateFRem(LHS, RHS);
|
|
|
|
}
|
2011-02-25 07:13:35 +00:00
|
|
|
Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
|
|
|
|
bool HasNSW = false) const {
|
|
|
|
BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
|
|
|
|
if (HasNUW) BO->setHasNoUnsignedWrap();
|
|
|
|
if (HasNSW) BO->setHasNoSignedWrap();
|
|
|
|
return BO;
|
|
|
|
}
|
|
|
|
Instruction *CreateLShr(Constant *LHS, Constant *RHS,
|
|
|
|
bool isExact = false) const {
|
|
|
|
if (!isExact)
|
|
|
|
return BinaryOperator::CreateLShr(LHS, RHS);
|
|
|
|
return BinaryOperator::CreateExactLShr(LHS, RHS);
|
|
|
|
}
|
|
|
|
Instruction *CreateAShr(Constant *LHS, Constant *RHS,
|
|
|
|
bool isExact = false) const {
|
|
|
|
if (!isExact)
|
|
|
|
return BinaryOperator::CreateAShr(LHS, RHS);
|
|
|
|
return BinaryOperator::CreateExactAShr(LHS, RHS);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::CreateAnd(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::CreateOr(LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::CreateXor(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateBinOp(Instruction::BinaryOps Opc,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::Create(Opc, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Unary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2011-02-25 07:13:35 +00:00
|
|
|
Instruction *CreateNeg(Constant *C,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
BinaryOperator *BO = BinaryOperator::CreateNeg(C);
|
|
|
|
if (HasNUW) BO->setHasNoUnsignedWrap();
|
|
|
|
if (HasNSW) BO->setHasNoSignedWrap();
|
|
|
|
return BO;
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateNSWNeg(Constant *C) const {
|
2009-12-18 02:58:50 +00:00
|
|
|
return BinaryOperator::CreateNSWNeg(C);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateNUWNeg(Constant *C) const {
|
2010-02-02 12:53:04 +00:00
|
|
|
return BinaryOperator::CreateNUWNeg(C);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateFNeg(Constant *C) const {
|
|
|
|
return BinaryOperator::CreateFNeg(C);
|
|
|
|
}
|
|
|
|
Instruction *CreateNot(Constant *C) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return BinaryOperator::CreateNot(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Memory Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2011-07-21 07:52:17 +00:00
|
|
|
Constant *CreateGetElementPtr(Constant *C,
|
|
|
|
ArrayRef<Constant *> IdxList) const {
|
2011-07-21 14:31:17 +00:00
|
|
|
return ConstantExpr::getGetElementPtr(C, IdxList);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2012-08-17 08:54:57 +00:00
|
|
|
Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const {
|
|
|
|
// This form of the function only exists to avoid ambiguous overload
|
|
|
|
// warnings about whether to convert Idx to ArrayRef<Constant *> or
|
|
|
|
// ArrayRef<Value *>.
|
|
|
|
return ConstantExpr::getGetElementPtr(C, Idx);
|
|
|
|
}
|
2011-07-21 07:52:17 +00:00
|
|
|
Instruction *CreateGetElementPtr(Constant *C,
|
|
|
|
ArrayRef<Value *> IdxList) const {
|
2011-07-25 09:48:08 +00:00
|
|
|
return GetElementPtrInst::Create(C, IdxList);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 07:52:17 +00:00
|
|
|
Constant *CreateInBoundsGetElementPtr(Constant *C,
|
|
|
|
ArrayRef<Constant *> IdxList) const {
|
2011-07-21 14:31:17 +00:00
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
|
2011-07-21 07:52:17 +00:00
|
|
|
}
|
2012-08-17 08:54:57 +00:00
|
|
|
Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const {
|
|
|
|
// This form of the function only exists to avoid ambiguous overload
|
|
|
|
// warnings about whether to convert Idx to ArrayRef<Constant *> or
|
|
|
|
// ArrayRef<Value *>.
|
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
|
|
|
|
}
|
2011-07-21 07:52:17 +00:00
|
|
|
Instruction *CreateInBoundsGetElementPtr(Constant *C,
|
|
|
|
ArrayRef<Value *> IdxList) const {
|
2011-07-25 09:48:08 +00:00
|
|
|
return GetElementPtrInst::CreateInBounds(C, IdxList);
|
2009-08-11 17:57:01 +00:00
|
|
|
}
|
|
|
|
|
2008-08-12 20:39:27 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Cast/Conversion Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *DestTy) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return CastInst::Create(Op, C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreatePointerCast(Constant *C, Type *DestTy) const {
|
2010-11-18 02:15:42 +00:00
|
|
|
return CastInst::CreatePointerCast(C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreateIntCast(Constant *C, Type *DestTy,
|
2008-08-12 20:39:27 +00:00
|
|
|
bool isSigned) const {
|
|
|
|
return CastInst::CreateIntegerCast(C, DestTy, isSigned);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreateFPCast(Constant *C, Type *DestTy) const {
|
2010-11-18 02:15:42 +00:00
|
|
|
return CastInst::CreateFPCast(C, DestTy);
|
|
|
|
}
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreateBitCast(Constant *C, Type *DestTy) const {
|
2010-11-18 02:15:42 +00:00
|
|
|
return CreateCast(Instruction::BitCast, C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const {
|
2010-11-18 02:15:42 +00:00
|
|
|
return CreateCast(Instruction::IntToPtr, C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const {
|
2010-11-18 02:15:42 +00:00
|
|
|
return CreateCast(Instruction::PtrToInt, C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
|
2010-11-18 02:15:42 +00:00
|
|
|
return CastInst::CreateZExtOrBitCast(C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
|
2010-11-18 02:15:42 +00:00
|
|
|
return CastInst::CreateSExtOrBitCast(C, DestTy);
|
|
|
|
}
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
|
2010-11-18 02:15:42 +00:00
|
|
|
return CastInst::CreateTruncOrBitCast(C, DestTy);
|
|
|
|
}
|
2008-08-12 20:39:27 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Compare Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateICmp(CmpInst::Predicate P,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return new ICmpInst(P, LHS, RHS);
|
|
|
|
}
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateFCmp(CmpInst::Predicate P,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return new FCmpInst(P, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Other Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateSelect(Constant *C,
|
|
|
|
Constant *True, Constant *False) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return SelectInst::Create(C, True, False);
|
|
|
|
}
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *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
|
|
|
}
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
|
|
|
|
Constant *Idx) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return InsertElementInst::Create(Vec, NewElt, Idx);
|
|
|
|
}
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
|
|
|
|
Constant *Mask) const {
|
2008-08-12 20:39:27 +00:00
|
|
|
return new ShuffleVectorInst(V1, V2, Mask);
|
|
|
|
}
|
|
|
|
|
2011-07-13 10:26:04 +00:00
|
|
|
Instruction *CreateExtractValue(Constant *Agg,
|
|
|
|
ArrayRef<unsigned> IdxList) const {
|
|
|
|
return ExtractValueInst::Create(Agg, IdxList);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
|
|
|
|
2010-11-18 02:15:42 +00:00
|
|
|
Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
|
2011-07-13 10:26:04 +00:00
|
|
|
ArrayRef<unsigned> IdxList) const {
|
|
|
|
return InsertValueInst::Create(Agg, Val, IdxList);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|