1193323Sed//===-- llvm/Support/ConstantFolder.h - Constant folding helper -*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines the ConstantFolder class, a helper for IRBuilder.
11193323Sed// It provides IRBuilder with a set of methods for creating constants
12193323Sed// with minimal folding.  For general constant creation and folding,
13193323Sed// use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
14193323Sed//
15193323Sed//===----------------------------------------------------------------------===//
16193323Sed
17193323Sed#ifndef LLVM_SUPPORT_CONSTANTFOLDER_H
18193323Sed#define LLVM_SUPPORT_CONSTANTFOLDER_H
19193323Sed
20249423Sdim#include "llvm/IR/Constants.h"
21249423Sdim#include "llvm/IR/InstrTypes.h"
22193323Sed
23193323Sednamespace llvm {
24193323Sed
25193323Sed/// ConstantFolder - Create constants with minimum, target independent, folding.
26193323Sedclass ConstantFolder {
27193323Sedpublic:
28221345Sdim  explicit ConstantFolder() {}
29193323Sed
30193323Sed  //===--------------------------------------------------------------------===//
31193323Sed  // Binary Operators
32193323Sed  //===--------------------------------------------------------------------===//
33193323Sed
34218893Sdim  Constant *CreateAdd(Constant *LHS, Constant *RHS,
35218893Sdim                      bool HasNUW = false, bool HasNSW = false) const {
36218893Sdim    return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
37193323Sed  }
38193574Sed  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
39193574Sed    return ConstantExpr::getFAdd(LHS, RHS);
40193574Sed  }
41218893Sdim  Constant *CreateSub(Constant *LHS, Constant *RHS,
42218893Sdim                      bool HasNUW = false, bool HasNSW = false) const {
43218893Sdim    return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
44193323Sed  }
45193574Sed  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
46193574Sed    return ConstantExpr::getFSub(LHS, RHS);
47193574Sed  }
48218893Sdim  Constant *CreateMul(Constant *LHS, Constant *RHS,
49218893Sdim                      bool HasNUW = false, bool HasNSW = false) const {
50218893Sdim    return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
51193323Sed  }
52193574Sed  Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
53193574Sed    return ConstantExpr::getFMul(LHS, RHS);
54193574Sed  }
55218893Sdim  Constant *CreateUDiv(Constant *LHS, Constant *RHS,
56218893Sdim                       bool isExact = false) const {
57218893Sdim    return ConstantExpr::getUDiv(LHS, RHS, isExact);
58193323Sed  }
59218893Sdim  Constant *CreateSDiv(Constant *LHS, Constant *RHS,
60218893Sdim                       bool isExact = false) const {
61218893Sdim    return ConstantExpr::getSDiv(LHS, RHS, isExact);
62193323Sed  }
63193323Sed  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
64193323Sed    return ConstantExpr::getFDiv(LHS, RHS);
65193323Sed  }
66193323Sed  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
67193323Sed    return ConstantExpr::getURem(LHS, RHS);
68193323Sed  }
69193323Sed  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
70193323Sed    return ConstantExpr::getSRem(LHS, RHS);
71193323Sed  }
72193323Sed  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
73193323Sed    return ConstantExpr::getFRem(LHS, RHS);
74193323Sed  }
75218893Sdim  Constant *CreateShl(Constant *LHS, Constant *RHS,
76218893Sdim                      bool HasNUW = false, bool HasNSW = false) const {
77218893Sdim    return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
78193323Sed  }
79218893Sdim  Constant *CreateLShr(Constant *LHS, Constant *RHS,
80218893Sdim                       bool isExact = false) const {
81218893Sdim    return ConstantExpr::getLShr(LHS, RHS, isExact);
82193323Sed  }
83218893Sdim  Constant *CreateAShr(Constant *LHS, Constant *RHS,
84218893Sdim                       bool isExact = false) const {
85218893Sdim    return ConstantExpr::getAShr(LHS, RHS, isExact);
86193323Sed  }
87193323Sed  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
88193323Sed    return ConstantExpr::getAnd(LHS, RHS);
89193323Sed  }
90193323Sed  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
91193323Sed    return ConstantExpr::getOr(LHS, RHS);
92193323Sed  }
93193323Sed  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
94193323Sed    return ConstantExpr::getXor(LHS, RHS);
95193323Sed  }
96193323Sed
97193323Sed  Constant *CreateBinOp(Instruction::BinaryOps Opc,
98193323Sed                        Constant *LHS, Constant *RHS) const {
99193323Sed    return ConstantExpr::get(Opc, LHS, RHS);
100193323Sed  }
101193323Sed
102193323Sed  //===--------------------------------------------------------------------===//
103193323Sed  // Unary Operators
104193323Sed  //===--------------------------------------------------------------------===//
105193323Sed
106218893Sdim  Constant *CreateNeg(Constant *C,
107218893Sdim                      bool HasNUW = false, bool HasNSW = false) const {
108218893Sdim    return ConstantExpr::getNeg(C, HasNUW, HasNSW);
109193323Sed  }
110193574Sed  Constant *CreateFNeg(Constant *C) const {
111193574Sed    return ConstantExpr::getFNeg(C);
112193574Sed  }
113193323Sed  Constant *CreateNot(Constant *C) const {
114193323Sed    return ConstantExpr::getNot(C);
115193323Sed  }
116193323Sed
117193323Sed  //===--------------------------------------------------------------------===//
118193323Sed  // Memory Instructions
119193323Sed  //===--------------------------------------------------------------------===//
120193323Sed
121226633Sdim  Constant *CreateGetElementPtr(Constant *C,
122226633Sdim                                ArrayRef<Constant *> IdxList) const {
123226633Sdim    return ConstantExpr::getGetElementPtr(C, IdxList);
124193323Sed  }
125226633Sdim  Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const {
126226633Sdim    // This form of the function only exists to avoid ambiguous overload
127226633Sdim    // warnings about whether to convert Idx to ArrayRef<Constant *> or
128226633Sdim    // ArrayRef<Value *>.
129226633Sdim    return ConstantExpr::getGetElementPtr(C, Idx);
130193323Sed  }
131226633Sdim  Constant *CreateGetElementPtr(Constant *C,
132226633Sdim                                ArrayRef<Value *> IdxList) const {
133226633Sdim    return ConstantExpr::getGetElementPtr(C, IdxList);
134226633Sdim  }
135193323Sed
136226633Sdim  Constant *CreateInBoundsGetElementPtr(Constant *C,
137226633Sdim                                        ArrayRef<Constant *> IdxList) const {
138226633Sdim    return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
139198090Srdivacky  }
140226633Sdim  Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const {
141226633Sdim    // This form of the function only exists to avoid ambiguous overload
142226633Sdim    // warnings about whether to convert Idx to ArrayRef<Constant *> or
143226633Sdim    // ArrayRef<Value *>.
144226633Sdim    return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
145198090Srdivacky  }
146226633Sdim  Constant *CreateInBoundsGetElementPtr(Constant *C,
147226633Sdim                                        ArrayRef<Value *> IdxList) const {
148226633Sdim    return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
149226633Sdim  }
150198090Srdivacky
151193323Sed  //===--------------------------------------------------------------------===//
152193323Sed  // Cast/Conversion Operators
153193323Sed  //===--------------------------------------------------------------------===//
154193323Sed
155193323Sed  Constant *CreateCast(Instruction::CastOps Op, Constant *C,
156226633Sdim                       Type *DestTy) const {
157193323Sed    return ConstantExpr::getCast(Op, C, DestTy);
158193323Sed  }
159226633Sdim  Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
160198090Srdivacky    return ConstantExpr::getPointerCast(C, DestTy);
161198090Srdivacky  }
162226633Sdim  Constant *CreateIntCast(Constant *C, Type *DestTy,
163193323Sed                          bool isSigned) const {
164193323Sed    return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
165193323Sed  }
166226633Sdim  Constant *CreateFPCast(Constant *C, Type *DestTy) const {
167198090Srdivacky    return ConstantExpr::getFPCast(C, DestTy);
168198090Srdivacky  }
169193323Sed
170226633Sdim  Constant *CreateBitCast(Constant *C, Type *DestTy) const {
171193323Sed    return CreateCast(Instruction::BitCast, C, DestTy);
172193323Sed  }
173226633Sdim  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
174193323Sed    return CreateCast(Instruction::IntToPtr, C, DestTy);
175193323Sed  }
176226633Sdim  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
177193323Sed    return CreateCast(Instruction::PtrToInt, C, DestTy);
178193323Sed  }
179226633Sdim  Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
180198090Srdivacky    return ConstantExpr::getZExtOrBitCast(C, DestTy);
181198090Srdivacky  }
182226633Sdim  Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
183198090Srdivacky    return ConstantExpr::getSExtOrBitCast(C, DestTy);
184198090Srdivacky  }
185198090Srdivacky
186226633Sdim  Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
187193323Sed    return ConstantExpr::getTruncOrBitCast(C, DestTy);
188193323Sed  }
189193323Sed
190193323Sed  //===--------------------------------------------------------------------===//
191193323Sed  // Compare Instructions
192193323Sed  //===--------------------------------------------------------------------===//
193193323Sed
194193323Sed  Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
195193323Sed                       Constant *RHS) const {
196193323Sed    return ConstantExpr::getCompare(P, LHS, RHS);
197193323Sed  }
198193323Sed  Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
199193323Sed                       Constant *RHS) const {
200193323Sed    return ConstantExpr::getCompare(P, LHS, RHS);
201193323Sed  }
202193323Sed
203193323Sed  //===--------------------------------------------------------------------===//
204193323Sed  // Other Instructions
205193323Sed  //===--------------------------------------------------------------------===//
206193323Sed
207193323Sed  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
208193323Sed    return ConstantExpr::getSelect(C, True, False);
209193323Sed  }
210193323Sed
211193323Sed  Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
212193323Sed    return ConstantExpr::getExtractElement(Vec, Idx);
213193323Sed  }
214193323Sed
215193323Sed  Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
216193323Sed                                Constant *Idx) const {
217193323Sed    return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
218193323Sed  }
219193323Sed
220193323Sed  Constant *CreateShuffleVector(Constant *V1, Constant *V2,
221193323Sed                                Constant *Mask) const {
222193323Sed    return ConstantExpr::getShuffleVector(V1, V2, Mask);
223193323Sed  }
224193323Sed
225224145Sdim  Constant *CreateExtractValue(Constant *Agg,
226224145Sdim                               ArrayRef<unsigned> IdxList) const {
227224145Sdim    return ConstantExpr::getExtractValue(Agg, IdxList);
228193323Sed  }
229193323Sed
230193323Sed  Constant *CreateInsertValue(Constant *Agg, Constant *Val,
231224145Sdim                              ArrayRef<unsigned> IdxList) const {
232224145Sdim    return ConstantExpr::getInsertValue(Agg, Val, IdxList);
233193323Sed  }
234193323Sed};
235193323Sed
236193323Sed}
237193323Sed
238193323Sed#endif
239