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) const {
42 return BinaryOperator::CreateAdd(LHS, RHS);
44 Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
45 return BinaryOperator::CreateNSWAdd(LHS, RHS);
47 Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
48 return BinaryOperator::CreateNUWAdd(LHS, RHS);
50 Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
51 return BinaryOperator::CreateFAdd(LHS, RHS);
53 Instruction *CreateSub(Constant *LHS, Constant *RHS) const {
54 return BinaryOperator::CreateSub(LHS, RHS);
56 Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
57 return BinaryOperator::CreateNSWSub(LHS, RHS);
59 Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
60 return BinaryOperator::CreateNUWSub(LHS, RHS);
62 Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
63 return BinaryOperator::CreateFSub(LHS, RHS);
65 Instruction *CreateMul(Constant *LHS, Constant *RHS) const {
66 return BinaryOperator::CreateMul(LHS, RHS);
68 Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
69 return BinaryOperator::CreateNSWMul(LHS, RHS);
71 Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
72 return BinaryOperator::CreateNUWMul(LHS, RHS);
74 Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
75 return BinaryOperator::CreateFMul(LHS, RHS);
77 Instruction *CreateUDiv(Constant *LHS, Constant *RHS) const {
78 return BinaryOperator::CreateUDiv(LHS, RHS);
80 Instruction *CreateSDiv(Constant *LHS, Constant *RHS) const {
81 return BinaryOperator::CreateSDiv(LHS, RHS);
83 Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
84 return BinaryOperator::CreateExactSDiv(LHS, RHS);
86 Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
87 return BinaryOperator::CreateFDiv(LHS, RHS);
89 Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
90 return BinaryOperator::CreateURem(LHS, RHS);
92 Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
93 return BinaryOperator::CreateSRem(LHS, RHS);
95 Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
96 return BinaryOperator::CreateFRem(LHS, RHS);
98 Instruction *CreateShl(Constant *LHS, Constant *RHS) const {
99 return BinaryOperator::CreateShl(LHS, RHS);
101 Instruction *CreateLShr(Constant *LHS, Constant *RHS) const {
102 return BinaryOperator::CreateLShr(LHS, RHS);
104 Instruction *CreateAShr(Constant *LHS, Constant *RHS) const {
105 return BinaryOperator::CreateAShr(LHS, RHS);
107 Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
108 return BinaryOperator::CreateAnd(LHS, RHS);
110 Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
111 return BinaryOperator::CreateOr(LHS, RHS);
113 Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
114 return BinaryOperator::CreateXor(LHS, RHS);
117 Instruction *CreateBinOp(Instruction::BinaryOps Opc,
118 Constant *LHS, Constant *RHS) const {
119 return BinaryOperator::Create(Opc, LHS, RHS);
122 //===--------------------------------------------------------------------===//
124 //===--------------------------------------------------------------------===//
126 Instruction *CreateNeg(Constant *C) const {
127 return BinaryOperator::CreateNeg(C);
129 Instruction *CreateNSWNeg(Constant *C) const {
130 return BinaryOperator::CreateNSWNeg(C);
132 Instruction *CreateNUWNeg(Constant *C) const {
133 return BinaryOperator::CreateNUWNeg(C);
135 Instruction *CreateFNeg(Constant *C) const {
136 return BinaryOperator::CreateFNeg(C);
138 Instruction *CreateNot(Constant *C) const {
139 return BinaryOperator::CreateNot(C);
142 //===--------------------------------------------------------------------===//
143 // Memory Instructions
144 //===--------------------------------------------------------------------===//
146 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
147 unsigned NumIdx) const {
148 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
150 Instruction *CreateGetElementPtr(Constant *C, Value* const *IdxList,
151 unsigned NumIdx) const {
152 return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
155 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
156 unsigned NumIdx) const {
157 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
159 Instruction *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
160 unsigned NumIdx) const {
161 return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
164 //===--------------------------------------------------------------------===//
165 // Cast/Conversion Operators
166 //===--------------------------------------------------------------------===//
168 Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
169 const Type *DestTy) const {
170 return CastInst::Create(Op, C, DestTy);
172 Instruction *CreatePointerCast(Constant *C, const Type *DestTy) const {
173 return CastInst::CreatePointerCast(C, DestTy);
175 Instruction *CreateIntCast(Constant *C, const Type *DestTy,
176 bool isSigned) const {
177 return CastInst::CreateIntegerCast(C, DestTy, isSigned);
179 Instruction *CreateFPCast(Constant *C, const Type *DestTy) const {
180 return CastInst::CreateFPCast(C, DestTy);
183 Instruction *CreateBitCast(Constant *C, const Type *DestTy) const {
184 return CreateCast(Instruction::BitCast, C, DestTy);
186 Instruction *CreateIntToPtr(Constant *C, const Type *DestTy) const {
187 return CreateCast(Instruction::IntToPtr, C, DestTy);
189 Instruction *CreatePtrToInt(Constant *C, const Type *DestTy) const {
190 return CreateCast(Instruction::PtrToInt, C, DestTy);
192 Instruction *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
193 return CastInst::CreateZExtOrBitCast(C, DestTy);
195 Instruction *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
196 return CastInst::CreateSExtOrBitCast(C, DestTy);
199 Instruction *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
200 return CastInst::CreateTruncOrBitCast(C, DestTy);
203 //===--------------------------------------------------------------------===//
204 // Compare Instructions
205 //===--------------------------------------------------------------------===//
207 Instruction *CreateICmp(CmpInst::Predicate P,
208 Constant *LHS, Constant *RHS) const {
209 return new ICmpInst(P, LHS, RHS);
211 Instruction *CreateFCmp(CmpInst::Predicate P,
212 Constant *LHS, Constant *RHS) const {
213 return new FCmpInst(P, LHS, RHS);
216 //===--------------------------------------------------------------------===//
217 // Other Instructions
218 //===--------------------------------------------------------------------===//
220 Instruction *CreateSelect(Constant *C,
221 Constant *True, Constant *False) const {
222 return SelectInst::Create(C, True, False);
225 Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
226 return ExtractElementInst::Create(Vec, Idx);
229 Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
230 Constant *Idx) const {
231 return InsertElementInst::Create(Vec, NewElt, Idx);
234 Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
235 Constant *Mask) const {
236 return new ShuffleVectorInst(V1, V2, Mask);
239 Instruction *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
240 unsigned NumIdx) const {
241 return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
244 Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
245 const unsigned *IdxList,
246 unsigned NumIdx) const {
247 return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);