2014-03-04 12:02:47 +00:00
|
|
|
//===- ConstantFolder.h - Constant folding helper ---------------*- C++ -*-===//
|
2008-08-11 15:29:30 +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-12 09:43:15 +00:00
|
|
|
// This file defines the ConstantFolder class, a helper for IRBuilder.
|
|
|
|
// It provides IRBuilder with a set of methods for creating constants
|
|
|
|
// with minimal folding. For general constant creation and folding,
|
|
|
|
// use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
|
2008-08-11 15:29:30 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 12:02:47 +00:00
|
|
|
#ifndef LLVM_IR_CONSTANTFOLDER_H
|
|
|
|
#define LLVM_IR_CONSTANTFOLDER_H
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
2008-08-11 15:29:30 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-08-11 15:56:12 +00:00
|
|
|
|
2008-08-11 15:29:30 +00:00
|
|
|
/// ConstantFolder - Create constants with minimum, target independent, folding.
|
|
|
|
class ConstantFolder {
|
|
|
|
public:
|
2011-04-03 19:46:28 +00:00
|
|
|
explicit ConstantFolder() {}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Binary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 07:01:55 +00:00
|
|
|
Constant *CreateAdd(Constant *LHS, Constant *RHS,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
|
2010-02-02 12:53:04 +00:00
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getFAdd(LHS, RHS);
|
2009-06-04 22:49:04 +00:00
|
|
|
}
|
2011-02-10 07:01:55 +00:00
|
|
|
Constant *CreateSub(Constant *LHS, Constant *RHS,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
|
2010-02-02 12:53:04 +00:00
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getFSub(LHS, RHS);
|
2009-06-04 22:49:04 +00:00
|
|
|
}
|
2011-02-10 07:01:55 +00:00
|
|
|
Constant *CreateMul(Constant *LHS, Constant *RHS,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
|
2010-02-02 12:53:04 +00:00
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getFMul(LHS, RHS);
|
2009-06-04 22:49:04 +00:00
|
|
|
}
|
2011-02-09 16:44:36 +00:00
|
|
|
Constant *CreateUDiv(Constant *LHS, Constant *RHS,
|
|
|
|
bool isExact = false) const {
|
|
|
|
return ConstantExpr::getUDiv(LHS, RHS, isExact);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
2011-02-09 16:44:36 +00:00
|
|
|
Constant *CreateSDiv(Constant *LHS, Constant *RHS,
|
|
|
|
bool isExact = false) const {
|
|
|
|
return ConstantExpr::getSDiv(LHS, RHS, isExact);
|
2009-08-11 17:05:24 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getFDiv(LHS, RHS);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
Constant *CreateURem(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getURem(LHS, RHS);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getSRem(LHS, RHS);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getFRem(LHS, RHS);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
2011-02-10 07:01:55 +00:00
|
|
|
Constant *CreateShl(Constant *LHS, Constant *RHS,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
2011-02-09 16:44:36 +00:00
|
|
|
Constant *CreateLShr(Constant *LHS, Constant *RHS,
|
|
|
|
bool isExact = false) const {
|
|
|
|
return ConstantExpr::getLShr(LHS, RHS, isExact);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
2011-02-09 16:44:36 +00:00
|
|
|
Constant *CreateAShr(Constant *LHS, Constant *RHS,
|
|
|
|
bool isExact = false) const {
|
|
|
|
return ConstantExpr::getAShr(LHS, RHS, isExact);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getAnd(LHS, RHS);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
Constant *CreateOr(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getOr(LHS, RHS);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
Constant *CreateXor(Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getXor(LHS, RHS);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateBinOp(Instruction::BinaryOps Opc,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::get(Opc, LHS, RHS);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Unary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2011-02-10 07:01:55 +00:00
|
|
|
Constant *CreateNeg(Constant *C,
|
|
|
|
bool HasNUW = false, bool HasNSW = false) const {
|
|
|
|
return ConstantExpr::getNeg(C, HasNUW, HasNSW);
|
2010-02-02 12:53:04 +00:00
|
|
|
}
|
2009-06-04 22:49:04 +00:00
|
|
|
Constant *CreateFNeg(Constant *C) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getFNeg(C);
|
2009-06-04 22:49:04 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
Constant *CreateNot(Constant *C) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getNot(C);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// 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-11 15:29:30 +00:00
|
|
|
}
|
2011-07-21 07:52:17 +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 *>.
|
2011-07-21 14:31:17 +00:00
|
|
|
return ConstantExpr::getGetElementPtr(C, Idx);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
2011-07-21 07:52:17 +00:00
|
|
|
Constant *CreateGetElementPtr(Constant *C,
|
|
|
|
ArrayRef<Value *> IdxList) const {
|
2011-07-21 14:31:17 +00:00
|
|
|
return ConstantExpr::getGetElementPtr(C, IdxList);
|
2009-08-11 17:57:01 +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
|
|
|
}
|
|
|
|
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 *>.
|
2011-07-21 14:31:17 +00:00
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
|
2011-07-21 07:52:17 +00:00
|
|
|
}
|
|
|
|
Constant *CreateInBoundsGetElementPtr(Constant *C,
|
|
|
|
ArrayRef<Value *> IdxList) const {
|
2011-07-21 14:31:17 +00:00
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
|
2009-08-11 17:57:01 +00:00
|
|
|
}
|
|
|
|
|
2008-08-11 15:29:30 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Cast/Conversion Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Constant *CreateCast(Instruction::CastOps Op, Constant *C,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *DestTy) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getCast(Op, C, DestTy);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
|
2009-08-16 02:19:46 +00:00
|
|
|
return ConstantExpr::getPointerCast(C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateIntCast(Constant *C, Type *DestTy,
|
2008-08-11 15:29:30 +00:00
|
|
|
bool isSigned) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateFPCast(Constant *C, Type *DestTy) const {
|
2009-08-16 02:19:46 +00:00
|
|
|
return ConstantExpr::getFPCast(C, DestTy);
|
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateBitCast(Constant *C, Type *DestTy) const {
|
2008-08-11 15:29:30 +00:00
|
|
|
return CreateCast(Instruction::BitCast, C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
|
2008-08-11 15:29:30 +00:00
|
|
|
return CreateCast(Instruction::IntToPtr, C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
|
2008-08-11 15:29:30 +00:00
|
|
|
return CreateCast(Instruction::PtrToInt, C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
|
2009-08-16 02:19:46 +00:00
|
|
|
return ConstantExpr::getZExtOrBitCast(C, DestTy);
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
|
2009-08-16 02:19:46 +00:00
|
|
|
return ConstantExpr::getSExtOrBitCast(C, DestTy);
|
|
|
|
}
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getTruncOrBitCast(C, DestTy);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Compare Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2008-08-12 20:39:27 +00:00
|
|
|
Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
|
|
|
|
Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getCompare(P, LHS, RHS);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
|
|
|
Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
|
|
|
|
Constant *RHS) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getCompare(P, LHS, RHS);
|
2008-08-12 20:39:27 +00:00
|
|
|
}
|
2008-08-11 15:29:30 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Other Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getSelect(C, True, False);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getExtractElement(Vec, Idx);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
|
|
|
|
Constant *Idx) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateShuffleVector(Constant *V1, Constant *V2,
|
|
|
|
Constant *Mask) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getShuffleVector(V1, V2, Mask);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
2011-07-13 10:26:04 +00:00
|
|
|
Constant *CreateExtractValue(Constant *Agg,
|
|
|
|
ArrayRef<unsigned> IdxList) const {
|
|
|
|
return ConstantExpr::getExtractValue(Agg, IdxList);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateInsertValue(Constant *Agg, Constant *Val,
|
2011-07-13 10:26:04 +00:00
|
|
|
ArrayRef<unsigned> IdxList) const {
|
|
|
|
return ConstantExpr::getInsertValue(Agg, Val, IdxList);
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|