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"
32 /// NoFolder - Create "constants" (actually, instructions) with no folding.
35 explicit NoFolder(LLVMContext &) {}
37 //===--------------------------------------------------------------------===//
39 //===--------------------------------------------------------------------===//
41 Instruction *CreateAdd(Constant *LHS, Constant *RHS,
42 bool HasNUW = false, bool HasNSW = false) const {
43 BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS);
44 if (HasNUW) BO->setHasNoUnsignedWrap();
45 if (HasNSW) BO->setHasNoSignedWrap();
48 Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
49 return BinaryOperator::CreateNSWAdd(LHS, RHS);
51 Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
52 return BinaryOperator::CreateNUWAdd(LHS, RHS);
54 Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
55 return BinaryOperator::CreateFAdd(LHS, RHS);
57 Instruction *CreateSub(Constant *LHS, Constant *RHS,
58 bool HasNUW = false, bool HasNSW = false) const {
59 BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
60 if (HasNUW) BO->setHasNoUnsignedWrap();
61 if (HasNSW) BO->setHasNoSignedWrap();
64 Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
65 return BinaryOperator::CreateNSWSub(LHS, RHS);
67 Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
68 return BinaryOperator::CreateNUWSub(LHS, RHS);
70 Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
71 return BinaryOperator::CreateFSub(LHS, RHS);
73 Instruction *CreateMul(Constant *LHS, Constant *RHS,
74 bool HasNUW = false, bool HasNSW = false) const {
75 BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS);
76 if (HasNUW) BO->setHasNoUnsignedWrap();
77 if (HasNSW) BO->setHasNoSignedWrap();
80 Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
81 return BinaryOperator::CreateNSWMul(LHS, RHS);
83 Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
84 return BinaryOperator::CreateNUWMul(LHS, RHS);
86 Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
87 return BinaryOperator::CreateFMul(LHS, RHS);
89 Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
90 bool isExact = false) const {
92 return BinaryOperator::CreateUDiv(LHS, RHS);
93 return BinaryOperator::CreateExactUDiv(LHS, RHS);
95 Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
96 return BinaryOperator::CreateExactUDiv(LHS, RHS);
98 Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
99 bool isExact = false) const {
101 return BinaryOperator::CreateSDiv(LHS, RHS);
102 return BinaryOperator::CreateExactSDiv(LHS, RHS);
104 Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
105 return BinaryOperator::CreateExactSDiv(LHS, RHS);
107 Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
108 return BinaryOperator::CreateFDiv(LHS, RHS);
110 Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
111 return BinaryOperator::CreateURem(LHS, RHS);
113 Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
114 return BinaryOperator::CreateSRem(LHS, RHS);
116 Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
117 return BinaryOperator::CreateFRem(LHS, RHS);
119 Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
120 bool HasNSW = false) const {
121 BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
122 if (HasNUW) BO->setHasNoUnsignedWrap();
123 if (HasNSW) BO->setHasNoSignedWrap();
126 Instruction *CreateLShr(Constant *LHS, Constant *RHS,
127 bool isExact = false) const {
129 return BinaryOperator::CreateLShr(LHS, RHS);
130 return BinaryOperator::CreateExactLShr(LHS, RHS);
132 Instruction *CreateAShr(Constant *LHS, Constant *RHS,
133 bool isExact = false) const {
135 return BinaryOperator::CreateAShr(LHS, RHS);
136 return BinaryOperator::CreateExactAShr(LHS, RHS);
138 Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
139 return BinaryOperator::CreateAnd(LHS, RHS);
141 Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
142 return BinaryOperator::CreateOr(LHS, RHS);
144 Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
145 return BinaryOperator::CreateXor(LHS, RHS);
148 Instruction *CreateBinOp(Instruction::BinaryOps Opc,
149 Constant *LHS, Constant *RHS) const {
150 return BinaryOperator::Create(Opc, LHS, RHS);
153 //===--------------------------------------------------------------------===//
155 //===--------------------------------------------------------------------===//
157 Instruction *CreateNeg(Constant *C,
158 bool HasNUW = false, bool HasNSW = false) const {
159 BinaryOperator *BO = BinaryOperator::CreateNeg(C);
160 if (HasNUW) BO->setHasNoUnsignedWrap();
161 if (HasNSW) BO->setHasNoSignedWrap();
164 Instruction *CreateNSWNeg(Constant *C) const {
165 return BinaryOperator::CreateNSWNeg(C);
167 Instruction *CreateNUWNeg(Constant *C) const {
168 return BinaryOperator::CreateNUWNeg(C);
170 Instruction *CreateFNeg(Constant *C) const {
171 return BinaryOperator::CreateFNeg(C);
173 Instruction *CreateNot(Constant *C) const {
174 return BinaryOperator::CreateNot(C);
177 //===--------------------------------------------------------------------===//
178 // Memory Instructions
179 //===--------------------------------------------------------------------===//
181 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
182 unsigned NumIdx) const {
183 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
185 Instruction *CreateGetElementPtr(Constant *C, Value* const *IdxList,
186 unsigned NumIdx) const {
187 return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
190 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
191 unsigned NumIdx) const {
192 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
194 Instruction *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
195 unsigned NumIdx) const {
196 return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
199 //===--------------------------------------------------------------------===//
200 // Cast/Conversion Operators
201 //===--------------------------------------------------------------------===//
203 Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
204 const Type *DestTy) const {
205 return CastInst::Create(Op, C, DestTy);
207 Instruction *CreatePointerCast(Constant *C, const Type *DestTy) const {
208 return CastInst::CreatePointerCast(C, DestTy);
210 Instruction *CreateIntCast(Constant *C, const Type *DestTy,
211 bool isSigned) const {
212 return CastInst::CreateIntegerCast(C, DestTy, isSigned);
214 Instruction *CreateFPCast(Constant *C, const Type *DestTy) const {
215 return CastInst::CreateFPCast(C, DestTy);
218 Instruction *CreateBitCast(Constant *C, const Type *DestTy) const {
219 return CreateCast(Instruction::BitCast, C, DestTy);
221 Instruction *CreateIntToPtr(Constant *C, const Type *DestTy) const {
222 return CreateCast(Instruction::IntToPtr, C, DestTy);
224 Instruction *CreatePtrToInt(Constant *C, const Type *DestTy) const {
225 return CreateCast(Instruction::PtrToInt, C, DestTy);
227 Instruction *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
228 return CastInst::CreateZExtOrBitCast(C, DestTy);
230 Instruction *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
231 return CastInst::CreateSExtOrBitCast(C, DestTy);
234 Instruction *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
235 return CastInst::CreateTruncOrBitCast(C, DestTy);
238 //===--------------------------------------------------------------------===//
239 // Compare Instructions
240 //===--------------------------------------------------------------------===//
242 Instruction *CreateICmp(CmpInst::Predicate P,
243 Constant *LHS, Constant *RHS) const {
244 return new ICmpInst(P, LHS, RHS);
246 Instruction *CreateFCmp(CmpInst::Predicate P,
247 Constant *LHS, Constant *RHS) const {
248 return new FCmpInst(P, LHS, RHS);
251 //===--------------------------------------------------------------------===//
252 // Other Instructions
253 //===--------------------------------------------------------------------===//
255 Instruction *CreateSelect(Constant *C,
256 Constant *True, Constant *False) const {
257 return SelectInst::Create(C, True, False);
260 Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
261 return ExtractElementInst::Create(Vec, Idx);
264 Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
265 Constant *Idx) const {
266 return InsertElementInst::Create(Vec, NewElt, Idx);
269 Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
270 Constant *Mask) const {
271 return new ShuffleVectorInst(V1, V2, Mask);
274 Instruction *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
275 unsigned NumIdx) const {
276 return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
279 Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
280 const unsigned *IdxList,
281 unsigned NumIdx) const {
282 return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);