2014-03-04 11:59:06 +00:00
|
|
|
//====- TargetFolder.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 TargetFolder class, a helper for IRBuilder.
|
|
|
|
// It provides IRBuilder with a set of methods for creating constants with
|
2009-06-03 00:13:48 +00:00
|
|
|
// target dependent folding, in addition to the same target-independent
|
|
|
|
// folding that the ConstantFolder class provides. 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 11:59:06 +00:00
|
|
|
#ifndef LLVM_ANALYSIS_TARGETFOLDER_H
|
|
|
|
#define LLVM_ANALYSIS_TARGETFOLDER_H
|
2008-08-11 15:29:30 +00:00
|
|
|
|
2011-07-13 10:26:04 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2008-08-11 15:29:30 +00:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
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 {
|
|
|
|
|
2012-10-08 16:38:25 +00:00
|
|
|
class DataLayout;
|
2008-08-11 15:29:30 +00:00
|
|
|
|
|
|
|
/// TargetFolder - Create constants with target dependent folding.
|
|
|
|
class TargetFolder {
|
2014-02-18 15:33:12 +00:00
|
|
|
const DataLayout *DL;
|
2008-08-11 15:29:30 +00:00
|
|
|
|
|
|
|
/// Fold - Fold the constant using target specific information.
|
|
|
|
Constant *Fold(Constant *C) const {
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
|
2014-02-18 15:33:12 +00:00
|
|
|
if (Constant *CF = ConstantFoldConstantExpression(CE, DL))
|
2008-08-11 15:29:30 +00:00
|
|
|
return CF;
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2014-02-18 15:33:12 +00:00
|
|
|
explicit TargetFolder(const DataLayout *DL) : DL(DL) {}
|
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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(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 Fold(ConstantExpr::getGetElementPtr(C, IdxList));
|
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 Fold(ConstantExpr::getGetElementPtr(C, Idx));
|
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 Fold(ConstantExpr::getGetElementPtr(C, IdxList));
|
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 Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList));
|
2011-07-21 07:52:17 +00:00
|
|
|
}
|
2011-07-21 09:19:11 +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 Fold(ConstantExpr::getInBoundsGetElementPtr(C, Idx));
|
2011-07-21 09:19:11 +00:00
|
|
|
}
|
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 Fold(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 {
|
2008-08-11 15:29:30 +00:00
|
|
|
if (C->getType() == DestTy)
|
|
|
|
return C; // avoid calling Fold
|
2009-07-29 18:55:55 +00:00
|
|
|
return Fold(ConstantExpr::getCast(Op, C, DestTy));
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
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 {
|
|
|
|
if (C->getType() == DestTy)
|
|
|
|
return C; // avoid calling Fold
|
2009-07-29 18:55:55 +00:00
|
|
|
return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
|
2012-09-27 06:33:40 +00:00
|
|
|
if (C->getType() == DestTy)
|
|
|
|
return C; // avoid calling Fold
|
|
|
|
return Fold(ConstantExpr::getPointerCast(C, DestTy));
|
|
|
|
}
|
|
|
|
Constant *CreateFPCast(Constant *C, Type *DestTy) const {
|
|
|
|
if (C->getType() == DestTy)
|
|
|
|
return C; // avoid calling Fold
|
|
|
|
return Fold(ConstantExpr::getFPCast(C, DestTy));
|
2010-02-17 18:39:56 +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-10-26 18:36:40 +00:00
|
|
|
if (C->getType() == DestTy)
|
|
|
|
return C; // avoid calling Fold
|
|
|
|
return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
|
2009-10-26 18:36:40 +00:00
|
|
|
if (C->getType() == DestTy)
|
|
|
|
return C; // avoid calling Fold
|
|
|
|
return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
|
2008-08-11 15:29:30 +00:00
|
|
|
if (C->getType() == DestTy)
|
|
|
|
return C; // avoid calling Fold
|
2009-07-29 18:55:55 +00:00
|
|
|
return Fold(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 Fold(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 Fold(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 Fold(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 Fold(ConstantExpr::getExtractElement(Vec, Idx));
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
2008-08-12 09:43:15 +00:00
|
|
|
Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
|
|
|
|
Constant *Idx) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
|
2008-08-12 09:43:15 +00:00
|
|
|
Constant *CreateShuffleVector(Constant *V1, Constant *V2,
|
|
|
|
Constant *Mask) const {
|
2009-07-29 18:55:55 +00:00
|
|
|
return Fold(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 Fold(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 Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList));
|
2008-08-11 15:29:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|