1 //===-- llvm/Support/ConstantFolder.h - Constant folding helper -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ConstantFolder class, a helper for IRBuilder.
11 // It provides IRBuilder with a set of methods for creating constants
12 // with minimal folding. For general constant creation and folding,
13 // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_SUPPORT_CONSTANTFOLDER_H
18 #define LLVM_SUPPORT_CONSTANTFOLDER_H
20 #include "llvm/Constants.h"
21 #include "llvm/LLVMContext.h"
25 /// ConstantFolder - Create constants with minimum, target independent, folding.
26 class ConstantFolder {
30 ConstantFolder(LLVMContext &C) : Context(C) { }
32 //===--------------------------------------------------------------------===//
34 //===--------------------------------------------------------------------===//
36 Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
37 return Context.getConstantExprAdd(LHS, RHS);
39 Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
40 return Context.getConstantExprFAdd(LHS, RHS);
42 Constant *CreateSub(Constant *LHS, Constant *RHS) const {
43 return Context.getConstantExprSub(LHS, RHS);
45 Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
46 return Context.getConstantExprFSub(LHS, RHS);
48 Constant *CreateMul(Constant *LHS, Constant *RHS) const {
49 return Context.getConstantExprMul(LHS, RHS);
51 Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
52 return Context.getConstantExprFMul(LHS, RHS);
54 Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
55 return Context.getConstantExprUDiv(LHS, RHS);
57 Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
58 return Context.getConstantExprSDiv(LHS, RHS);
60 Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
61 return Context.getConstantExprFDiv(LHS, RHS);
63 Constant *CreateURem(Constant *LHS, Constant *RHS) const {
64 return Context.getConstantExprURem(LHS, RHS);
66 Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
67 return Context.getConstantExprSRem(LHS, RHS);
69 Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
70 return Context.getConstantExprFRem(LHS, RHS);
72 Constant *CreateShl(Constant *LHS, Constant *RHS) const {
73 return Context.getConstantExprShl(LHS, RHS);
75 Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
76 return Context.getConstantExprLShr(LHS, RHS);
78 Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
79 return Context.getConstantExprAShr(LHS, RHS);
81 Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
82 return Context.getConstantExprAnd(LHS, RHS);
84 Constant *CreateOr(Constant *LHS, Constant *RHS) const {
85 return Context.getConstantExprOr(LHS, RHS);
87 Constant *CreateXor(Constant *LHS, Constant *RHS) const {
88 return Context.getConstantExprXor(LHS, RHS);
91 Constant *CreateBinOp(Instruction::BinaryOps Opc,
92 Constant *LHS, Constant *RHS) const {
93 return Context.getConstantExpr(Opc, LHS, RHS);
96 //===--------------------------------------------------------------------===//
98 //===--------------------------------------------------------------------===//
100 Constant *CreateNeg(Constant *C) const {
101 return Context.getConstantExprNeg(C);
103 Constant *CreateFNeg(Constant *C) const {
104 return Context.getConstantExprFNeg(C);
106 Constant *CreateNot(Constant *C) const {
107 return Context.getConstantExprNot(C);
110 //===--------------------------------------------------------------------===//
111 // Memory Instructions
112 //===--------------------------------------------------------------------===//
114 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
115 unsigned NumIdx) const {
116 return Context.getConstantExprGetElementPtr(C, IdxList, NumIdx);
118 Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
119 unsigned NumIdx) const {
120 return Context.getConstantExprGetElementPtr(C, IdxList, NumIdx);
123 //===--------------------------------------------------------------------===//
124 // Cast/Conversion Operators
125 //===--------------------------------------------------------------------===//
127 Constant *CreateCast(Instruction::CastOps Op, Constant *C,
128 const Type *DestTy) const {
129 return Context.getConstantExprCast(Op, C, DestTy);
131 Constant *CreateIntCast(Constant *C, const Type *DestTy,
132 bool isSigned) const {
133 return Context.getConstantExprIntegerCast(C, DestTy, isSigned);
136 Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
137 return CreateCast(Instruction::BitCast, C, DestTy);
139 Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
140 return CreateCast(Instruction::IntToPtr, C, DestTy);
142 Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
143 return CreateCast(Instruction::PtrToInt, C, DestTy);
145 Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
146 return Context.getConstantExprTruncOrBitCast(C, DestTy);
149 //===--------------------------------------------------------------------===//
150 // Compare Instructions
151 //===--------------------------------------------------------------------===//
153 Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
154 Constant *RHS) const {
155 return Context.getConstantExprCompare(P, LHS, RHS);
157 Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
158 Constant *RHS) const {
159 return Context.getConstantExprCompare(P, LHS, RHS);
162 //===--------------------------------------------------------------------===//
163 // Other Instructions
164 //===--------------------------------------------------------------------===//
166 Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
167 return Context.getConstantExprSelect(C, True, False);
170 Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
171 return Context.getConstantExprExtractElement(Vec, Idx);
174 Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
175 Constant *Idx) const {
176 return Context.getConstantExprInsertElement(Vec, NewElt, Idx);
179 Constant *CreateShuffleVector(Constant *V1, Constant *V2,
180 Constant *Mask) const {
181 return Context.getConstantExprShuffleVector(V1, V2, Mask);
184 Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
185 unsigned NumIdx) const {
186 return Context.getConstantExprExtractValue(Agg, IdxList, NumIdx);
189 Constant *CreateInsertValue(Constant *Agg, Constant *Val,
190 const unsigned *IdxList, unsigned NumIdx) const {
191 return Context.getConstantExprInsertValue(Agg, Val, IdxList, NumIdx);