2008-08-11 17:29:30 +02:00
|
|
|
//===-- llvm/Support/ConstantFolder.h - Constant folding helper -*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-08-12 11:43:15 +02: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 17:29:30 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_CONSTANTFOLDER_H
|
|
|
|
#define LLVM_SUPPORT_CONSTANTFOLDER_H
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
2009-11-05 14:39:23 +01:00
|
|
|
#include "llvm/InstrTypes.h"
|
2008-08-11 17:29:30 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-08-11 17:56:12 +02:00
|
|
|
|
2009-08-11 19:45:13 +02:00
|
|
|
class LLVMContext;
|
2009-08-11 17:56:12 +02:00
|
|
|
|
2008-08-11 17:29:30 +02:00
|
|
|
/// ConstantFolder - Create constants with minimum, target independent, folding.
|
|
|
|
class ConstantFolder {
|
|
|
|
public:
|
2009-08-11 17:56:12 +02:00
|
|
|
explicit ConstantFolder(LLVMContext &) {}
|
2008-08-11 17:29:30 +02:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Binary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getAdd(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
2009-08-11 22:20:39 +02:00
|
|
|
Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
|
2009-09-04 01:34:49 +02:00
|
|
|
return ConstantExpr::getNSWAdd(LHS, RHS);
|
2009-08-11 22:20:39 +02:00
|
|
|
}
|
2010-02-02 13:53:04 +01:00
|
|
|
Constant *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
|
|
|
|
return ConstantExpr::getNUWAdd(LHS, RHS);
|
|
|
|
}
|
2009-06-05 00:49:04 +02:00
|
|
|
Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getFAdd(LHS, RHS);
|
2009-06-05 00:49:04 +02:00
|
|
|
}
|
2008-08-11 17:29:30 +02:00
|
|
|
Constant *CreateSub(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getSub(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
2009-09-26 17:35:35 +02:00
|
|
|
Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const {
|
|
|
|
return ConstantExpr::getNSWSub(LHS, RHS);
|
|
|
|
}
|
2010-02-02 13:53:04 +01:00
|
|
|
Constant *CreateNUWSub(Constant *LHS, Constant *RHS) const {
|
|
|
|
return ConstantExpr::getNUWSub(LHS, RHS);
|
|
|
|
}
|
2009-06-05 00:49:04 +02:00
|
|
|
Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getFSub(LHS, RHS);
|
2009-06-05 00:49:04 +02:00
|
|
|
}
|
2008-08-11 17:29:30 +02:00
|
|
|
Constant *CreateMul(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getMul(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
2009-12-18 04:10:26 +01:00
|
|
|
Constant *CreateNSWMul(Constant *LHS, Constant *RHS) const {
|
|
|
|
return ConstantExpr::getNSWMul(LHS, RHS);
|
|
|
|
}
|
2010-02-02 13:53:04 +01:00
|
|
|
Constant *CreateNUWMul(Constant *LHS, Constant *RHS) const {
|
|
|
|
return ConstantExpr::getNUWMul(LHS, RHS);
|
|
|
|
}
|
2009-06-05 00:49:04 +02:00
|
|
|
Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getFMul(LHS, RHS);
|
2009-06-05 00:49:04 +02:00
|
|
|
}
|
2008-08-11 17:29:30 +02:00
|
|
|
Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getUDiv(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getSDiv(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
2009-08-11 19:05:24 +02:00
|
|
|
Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
|
2009-09-04 01:34:49 +02:00
|
|
|
return ConstantExpr::getExactSDiv(LHS, RHS);
|
2009-08-11 19:05:24 +02:00
|
|
|
}
|
2008-08-11 17:29:30 +02:00
|
|
|
Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getFDiv(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateURem(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getURem(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getSRem(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getFRem(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateShl(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getShl(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getLShr(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getAShr(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getAnd(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateOr(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getOr(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateXor(Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getXor(LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateBinOp(Instruction::BinaryOps Opc,
|
|
|
|
Constant *LHS, Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::get(Opc, LHS, RHS);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Unary Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Constant *CreateNeg(Constant *C) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getNeg(C);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
2009-12-18 03:58:50 +01:00
|
|
|
Constant *CreateNSWNeg(Constant *C) const {
|
|
|
|
return ConstantExpr::getNSWNeg(C);
|
|
|
|
}
|
2010-02-02 13:53:04 +01:00
|
|
|
Constant *CreateNUWNeg(Constant *C) const {
|
|
|
|
return ConstantExpr::getNUWNeg(C);
|
|
|
|
}
|
2009-06-05 00:49:04 +02:00
|
|
|
Constant *CreateFNeg(Constant *C) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getFNeg(C);
|
2009-06-05 00:49:04 +02:00
|
|
|
}
|
2008-08-11 17:29:30 +02:00
|
|
|
Constant *CreateNot(Constant *C) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getNot(C);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// 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-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
Constant *CreateInBoundsGetElementPtr(Constant *C, Value* 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
|
|
|
}
|
|
|
|
|
2008-08-11 17:29:30 +02:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Cast/Conversion Operators
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Constant *CreateCast(Instruction::CastOps Op, Constant *C,
|
|
|
|
const Type *DestTy) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getCast(Op, C, DestTy);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
2009-08-16 04:19:46 +02:00
|
|
|
Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return ConstantExpr::getPointerCast(C, DestTy);
|
|
|
|
}
|
2008-08-11 17:29:30 +02:00
|
|
|
Constant *CreateIntCast(Constant *C, const Type *DestTy,
|
|
|
|
bool isSigned) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
2009-08-16 04:19:46 +02:00
|
|
|
Constant *CreateFPCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return ConstantExpr::getFPCast(C, DestTy);
|
|
|
|
}
|
2008-08-11 17:29:30 +02:00
|
|
|
|
|
|
|
Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return CreateCast(Instruction::BitCast, C, DestTy);
|
|
|
|
}
|
|
|
|
Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
|
|
|
|
return CreateCast(Instruction::IntToPtr, C, DestTy);
|
|
|
|
}
|
|
|
|
Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
|
|
|
|
return CreateCast(Instruction::PtrToInt, C, DestTy);
|
|
|
|
}
|
2009-08-16 04:19:46 +02:00
|
|
|
Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return ConstantExpr::getZExtOrBitCast(C, DestTy);
|
|
|
|
}
|
|
|
|
Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
|
|
|
|
return ConstantExpr::getSExtOrBitCast(C, DestTy);
|
|
|
|
}
|
|
|
|
|
2008-08-11 17:29:30 +02:00
|
|
|
Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getTruncOrBitCast(C, DestTy);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Compare Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2008-08-12 22:39:27 +02:00
|
|
|
Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
|
|
|
|
Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getCompare(P, LHS, RHS);
|
2008-08-12 22:39:27 +02:00
|
|
|
}
|
|
|
|
Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
|
|
|
|
Constant *RHS) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getCompare(P, LHS, RHS);
|
2008-08-12 22:39:27 +02:00
|
|
|
}
|
2008-08-11 17:29:30 +02:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Other Instructions
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getSelect(C, True, False);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getExtractElement(Vec, Idx);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
|
|
|
|
Constant *Idx) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateShuffleVector(Constant *V1, Constant *V2,
|
|
|
|
Constant *Mask) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getShuffleVector(V1, V2, Mask);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
|
|
|
|
unsigned NumIdx) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getExtractValue(Agg, IdxList, NumIdx);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *CreateInsertValue(Constant *Agg, Constant *Val,
|
|
|
|
const unsigned *IdxList, unsigned NumIdx) const {
|
2009-07-29 20:55:55 +02:00
|
|
|
return ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx);
|
2008-08-11 17:29:30 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|