1 //===- 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_IR_NOFOLDER_H
23 #define LLVM_IR_NOFOLDER_H
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/Instructions.h"
31 /// NoFolder - Create "constants" (actually, instructions) with no folding.
34 explicit NoFolder() {}
36 //===--------------------------------------------------------------------===//
38 //===--------------------------------------------------------------------===//
40 Instruction *CreateAdd(Constant *LHS, Constant *RHS,
41 bool HasNUW = false, bool HasNSW = false) const {
42 BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS);
43 if (HasNUW) BO->setHasNoUnsignedWrap();
44 if (HasNSW) BO->setHasNoSignedWrap();
47 Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
48 return BinaryOperator::CreateNSWAdd(LHS, RHS);
50 Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
51 return BinaryOperator::CreateNUWAdd(LHS, RHS);
53 Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
54 return BinaryOperator::CreateFAdd(LHS, RHS);
56 Instruction *CreateSub(Constant *LHS, Constant *RHS,
57 bool HasNUW = false, bool HasNSW = false) const {
58 BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
59 if (HasNUW) BO->setHasNoUnsignedWrap();
60 if (HasNSW) BO->setHasNoSignedWrap();
63 Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
64 return BinaryOperator::CreateNSWSub(LHS, RHS);
66 Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
67 return BinaryOperator::CreateNUWSub(LHS, RHS);
69 Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
70 return BinaryOperator::CreateFSub(LHS, RHS);
72 Instruction *CreateMul(Constant *LHS, Constant *RHS,
73 bool HasNUW = false, bool HasNSW = false) const {
74 BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS);
75 if (HasNUW) BO->setHasNoUnsignedWrap();
76 if (HasNSW) BO->setHasNoSignedWrap();
79 Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
80 return BinaryOperator::CreateNSWMul(LHS, RHS);
82 Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
83 return BinaryOperator::CreateNUWMul(LHS, RHS);
85 Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
86 return BinaryOperator::CreateFMul(LHS, RHS);
88 Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
89 bool isExact = false) const {
91 return BinaryOperator::CreateUDiv(LHS, RHS);
92 return BinaryOperator::CreateExactUDiv(LHS, RHS);
94 Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
95 return BinaryOperator::CreateExactUDiv(LHS, RHS);
97 Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
98 bool isExact = false) const {
100 return BinaryOperator::CreateSDiv(LHS, RHS);
101 return BinaryOperator::CreateExactSDiv(LHS, RHS);
103 Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
104 return BinaryOperator::CreateExactSDiv(LHS, RHS);
106 Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
107 return BinaryOperator::CreateFDiv(LHS, RHS);
109 Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
110 return BinaryOperator::CreateURem(LHS, RHS);
112 Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
113 return BinaryOperator::CreateSRem(LHS, RHS);
115 Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
116 return BinaryOperator::CreateFRem(LHS, RHS);
118 Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
119 bool HasNSW = false) const {
120 BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
121 if (HasNUW) BO->setHasNoUnsignedWrap();
122 if (HasNSW) BO->setHasNoSignedWrap();
125 Instruction *CreateLShr(Constant *LHS, Constant *RHS,
126 bool isExact = false) const {
128 return BinaryOperator::CreateLShr(LHS, RHS);
129 return BinaryOperator::CreateExactLShr(LHS, RHS);
131 Instruction *CreateAShr(Constant *LHS, Constant *RHS,
132 bool isExact = false) const {
134 return BinaryOperator::CreateAShr(LHS, RHS);
135 return BinaryOperator::CreateExactAShr(LHS, RHS);
137 Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
138 return BinaryOperator::CreateAnd(LHS, RHS);
140 Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
141 return BinaryOperator::CreateOr(LHS, RHS);
143 Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
144 return BinaryOperator::CreateXor(LHS, RHS);
147 Instruction *CreateBinOp(Instruction::BinaryOps Opc,
148 Constant *LHS, Constant *RHS) const {
149 return BinaryOperator::Create(Opc, LHS, RHS);
152 //===--------------------------------------------------------------------===//
154 //===--------------------------------------------------------------------===//
156 Instruction *CreateNeg(Constant *C,
157 bool HasNUW = false, bool HasNSW = false) const {
158 BinaryOperator *BO = BinaryOperator::CreateNeg(C);
159 if (HasNUW) BO->setHasNoUnsignedWrap();
160 if (HasNSW) BO->setHasNoSignedWrap();
163 Instruction *CreateNSWNeg(Constant *C) const {
164 return BinaryOperator::CreateNSWNeg(C);
166 Instruction *CreateNUWNeg(Constant *C) const {
167 return BinaryOperator::CreateNUWNeg(C);
169 Instruction *CreateFNeg(Constant *C) const {
170 return BinaryOperator::CreateFNeg(C);
172 Instruction *CreateNot(Constant *C) const {
173 return BinaryOperator::CreateNot(C);
176 //===--------------------------------------------------------------------===//
177 // Memory Instructions
178 //===--------------------------------------------------------------------===//
180 Constant *CreateGetElementPtr(Constant *C,
181 ArrayRef<Constant *> IdxList) const {
182 return ConstantExpr::getGetElementPtr(C, IdxList);
184 Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const {
185 // This form of the function only exists to avoid ambiguous overload
186 // warnings about whether to convert Idx to ArrayRef<Constant *> or
187 // ArrayRef<Value *>.
188 return ConstantExpr::getGetElementPtr(C, Idx);
190 Instruction *CreateGetElementPtr(Constant *C,
191 ArrayRef<Value *> IdxList) const {
192 return GetElementPtrInst::Create(C, IdxList);
195 Constant *CreateInBoundsGetElementPtr(Constant *C,
196 ArrayRef<Constant *> IdxList) const {
197 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
199 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const {
200 // This form of the function only exists to avoid ambiguous overload
201 // warnings about whether to convert Idx to ArrayRef<Constant *> or
202 // ArrayRef<Value *>.
203 return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
205 Instruction *CreateInBoundsGetElementPtr(Constant *C,
206 ArrayRef<Value *> IdxList) const {
207 return GetElementPtrInst::CreateInBounds(C, IdxList);
210 //===--------------------------------------------------------------------===//
211 // Cast/Conversion Operators
212 //===--------------------------------------------------------------------===//
214 Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
215 Type *DestTy) const {
216 return CastInst::Create(Op, C, DestTy);
218 Instruction *CreatePointerCast(Constant *C, Type *DestTy) const {
219 return CastInst::CreatePointerCast(C, DestTy);
221 Instruction *CreateIntCast(Constant *C, Type *DestTy,
222 bool isSigned) const {
223 return CastInst::CreateIntegerCast(C, DestTy, isSigned);
225 Instruction *CreateFPCast(Constant *C, Type *DestTy) const {
226 return CastInst::CreateFPCast(C, DestTy);
229 Instruction *CreateBitCast(Constant *C, Type *DestTy) const {
230 return CreateCast(Instruction::BitCast, C, DestTy);
232 Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const {
233 return CreateCast(Instruction::IntToPtr, C, DestTy);
235 Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const {
236 return CreateCast(Instruction::PtrToInt, C, DestTy);
238 Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
239 return CastInst::CreateZExtOrBitCast(C, DestTy);
241 Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
242 return CastInst::CreateSExtOrBitCast(C, DestTy);
245 Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
246 return CastInst::CreateTruncOrBitCast(C, DestTy);
249 //===--------------------------------------------------------------------===//
250 // Compare Instructions
251 //===--------------------------------------------------------------------===//
253 Instruction *CreateICmp(CmpInst::Predicate P,
254 Constant *LHS, Constant *RHS) const {
255 return new ICmpInst(P, LHS, RHS);
257 Instruction *CreateFCmp(CmpInst::Predicate P,
258 Constant *LHS, Constant *RHS) const {
259 return new FCmpInst(P, LHS, RHS);
262 //===--------------------------------------------------------------------===//
263 // Other Instructions
264 //===--------------------------------------------------------------------===//
266 Instruction *CreateSelect(Constant *C,
267 Constant *True, Constant *False) const {
268 return SelectInst::Create(C, True, False);
271 Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
272 return ExtractElementInst::Create(Vec, Idx);
275 Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
276 Constant *Idx) const {
277 return InsertElementInst::Create(Vec, NewElt, Idx);
280 Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
281 Constant *Mask) const {
282 return new ShuffleVectorInst(V1, V2, Mask);
285 Instruction *CreateExtractValue(Constant *Agg,
286 ArrayRef<unsigned> IdxList) const {
287 return ExtractValueInst::Create(Agg, IdxList);
290 Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
291 ArrayRef<unsigned> IdxList) const {
292 return InsertValueInst::Create(Agg, Val, IdxList);