1 //======-- llvm/Support/NoFolder.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 NoFolder class, a helper for IRBuilder. It provides
11 // IRBuilder with a set of methods for creating unfolded constants. This is
12 // useful for learners trying to understand how LLVM IR works, and who don't
13 // want details to be hidden by the constant folder. For general constant
14 // creation and folding, use ConstantExpr and the routines in
15 // llvm/Analysis/ConstantFolding.h.
17 // Note: since it is not actually possible to create unfolded constants, this
18 // class returns instructions rather than constants.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_SUPPORT_NOFOLDER_H
23 #define LLVM_SUPPORT_NOFOLDER_H
25 #include "llvm/Constants.h"
26 #include "llvm/Instructions.h"
30 /// NoFolder - Create "constants" (actually, instructions) with no folding.
33 explicit NoFolder() {}
35 //===--------------------------------------------------------------------===//
37 //===--------------------------------------------------------------------===//
39 Instruction *CreateAdd(Constant *LHS, Constant *RHS,
40 bool HasNUW = false, bool HasNSW = false) const {
41 BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS);
42 if (HasNUW) BO->setHasNoUnsignedWrap();
43 if (HasNSW) BO->setHasNoSignedWrap();
46 Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
47 return BinaryOperator::CreateNSWAdd(LHS, RHS);
49 Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
50 return BinaryOperator::CreateNUWAdd(LHS, RHS);
52 Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
53 return BinaryOperator::CreateFAdd(LHS, RHS);
55 Instruction *CreateSub(Constant *LHS, Constant *RHS,
56 bool HasNUW = false, bool HasNSW = false) const {
57 BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
58 if (HasNUW) BO->setHasNoUnsignedWrap();
59 if (HasNSW) BO->setHasNoSignedWrap();
62 Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
63 return BinaryOperator::CreateNSWSub(LHS, RHS);
65 Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
66 return BinaryOperator::CreateNUWSub(LHS, RHS);
68 Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
69 return BinaryOperator::CreateFSub(LHS, RHS);
71 Instruction *CreateMul(Constant *LHS, Constant *RHS,
72 bool HasNUW = false, bool HasNSW = false) const {
73 BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS);
74 if (HasNUW) BO->setHasNoUnsignedWrap();
75 if (HasNSW) BO->setHasNoSignedWrap();
78 Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
79 return BinaryOperator::CreateNSWMul(LHS, RHS);
81 Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
82 return BinaryOperator::CreateNUWMul(LHS, RHS);
84 Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
85 return BinaryOperator::CreateFMul(LHS, RHS);
87 Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
88 bool isExact = false) const {
90 return BinaryOperator::CreateUDiv(LHS, RHS);
91 return BinaryOperator::CreateExactUDiv(LHS, RHS);
93 Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
94 return BinaryOperator::CreateExactUDiv(LHS, RHS);
96 Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
97 bool isExact = false) const {
99 return BinaryOperator::CreateSDiv(LHS, RHS);
100 return BinaryOperator::CreateExactSDiv(LHS, RHS);
102 Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
103 return BinaryOperator::CreateExactSDiv(LHS, RHS);
105 Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
106 return BinaryOperator::CreateFDiv(LHS, RHS);
108 Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
109 return BinaryOperator::CreateURem(LHS, RHS);
111 Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
112 return BinaryOperator::CreateSRem(LHS, RHS);
114 Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
115 return BinaryOperator::CreateFRem(LHS, RHS);
117 Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
118 bool HasNSW = false) const {
119 BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
120 if (HasNUW) BO->setHasNoUnsignedWrap();
121 if (HasNSW) BO->setHasNoSignedWrap();
124 Instruction *CreateLShr(Constant *LHS, Constant *RHS,
125 bool isExact = false) const {
127 return BinaryOperator::CreateLShr(LHS, RHS);
128 return BinaryOperator::CreateExactLShr(LHS, RHS);
130 Instruction *CreateAShr(Constant *LHS, Constant *RHS,
131 bool isExact = false) const {
133 return BinaryOperator::CreateAShr(LHS, RHS);
134 return BinaryOperator::CreateExactAShr(LHS, RHS);
136 Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
137 return BinaryOperator::CreateAnd(LHS, RHS);
139 Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
140 return BinaryOperator::CreateOr(LHS, RHS);
142 Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
143 return BinaryOperator::CreateXor(LHS, RHS);
146 Instruction *CreateBinOp(Instruction::BinaryOps Opc,
147 Constant *LHS, Constant *RHS) const {
148 return BinaryOperator::Create(Opc, LHS, RHS);
151 //===--------------------------------------------------------------------===//
153 //===--------------------------------------------------------------------===//
155 Instruction *CreateNeg(Constant *C,
156 bool HasNUW = false, bool HasNSW = false) const {
157 BinaryOperator *BO = BinaryOperator::CreateNeg(C);
158 if (HasNUW) BO->setHasNoUnsignedWrap();
159 if (HasNSW) BO->setHasNoSignedWrap();
162 Instruction *CreateNSWNeg(Constant *C) const {
163 return BinaryOperator::CreateNSWNeg(C);
165 Instruction *CreateNUWNeg(Constant *C) const {
166 return BinaryOperator::CreateNUWNeg(C);
168 Instruction *CreateFNeg(Constant *C) const {
169 return BinaryOperator::CreateFNeg(C);
171 Instruction *CreateNot(Constant *C) const {
172 return BinaryOperator::CreateNot(C);
175 //===--------------------------------------------------------------------===//
176 // Memory Instructions
177 //===--------------------------------------------------------------------===//
179 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
180 unsigned NumIdx) const {
181 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
183 Instruction *CreateGetElementPtr(Constant *C, Value* const *IdxList,
184 unsigned NumIdx) const {
185 return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
188 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
189 unsigned NumIdx) const {
190 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
192 Instruction *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
193 unsigned NumIdx) const {
194 return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
197 //===--------------------------------------------------------------------===//
198 // Cast/Conversion Operators
199 //===--------------------------------------------------------------------===//
201 Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
202 const Type *DestTy) const {
203 return CastInst::Create(Op, C, DestTy);
205 Instruction *CreatePointerCast(Constant *C, const Type *DestTy) const {
206 return CastInst::CreatePointerCast(C, DestTy);
208 Instruction *CreateIntCast(Constant *C, const Type *DestTy,
209 bool isSigned) const {
210 return CastInst::CreateIntegerCast(C, DestTy, isSigned);
212 Instruction *CreateFPCast(Constant *C, const Type *DestTy) const {
213 return CastInst::CreateFPCast(C, DestTy);
216 Instruction *CreateBitCast(Constant *C, const Type *DestTy) const {
217 return CreateCast(Instruction::BitCast, C, DestTy);
219 Instruction *CreateIntToPtr(Constant *C, const Type *DestTy) const {
220 return CreateCast(Instruction::IntToPtr, C, DestTy);
222 Instruction *CreatePtrToInt(Constant *C, const Type *DestTy) const {
223 return CreateCast(Instruction::PtrToInt, C, DestTy);
225 Instruction *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
226 return CastInst::CreateZExtOrBitCast(C, DestTy);
228 Instruction *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
229 return CastInst::CreateSExtOrBitCast(C, DestTy);
232 Instruction *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
233 return CastInst::CreateTruncOrBitCast(C, DestTy);
236 //===--------------------------------------------------------------------===//
237 // Compare Instructions
238 //===--------------------------------------------------------------------===//
240 Instruction *CreateICmp(CmpInst::Predicate P,
241 Constant *LHS, Constant *RHS) const {
242 return new ICmpInst(P, LHS, RHS);
244 Instruction *CreateFCmp(CmpInst::Predicate P,
245 Constant *LHS, Constant *RHS) const {
246 return new FCmpInst(P, LHS, RHS);
249 //===--------------------------------------------------------------------===//
250 // Other Instructions
251 //===--------------------------------------------------------------------===//
253 Instruction *CreateSelect(Constant *C,
254 Constant *True, Constant *False) const {
255 return SelectInst::Create(C, True, False);
258 Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
259 return ExtractElementInst::Create(Vec, Idx);
262 Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
263 Constant *Idx) const {
264 return InsertElementInst::Create(Vec, NewElt, Idx);
267 Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
268 Constant *Mask) const {
269 return new ShuffleVectorInst(V1, V2, Mask);
272 Instruction *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
273 unsigned NumIdx) const {
274 return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
277 Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
278 const unsigned *IdxList,
279 unsigned NumIdx) const {
280 return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);