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 values rather than constants. The values do not have names,
19 // even if names were provided to IRBuilder, which may be confusing.
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_SUPPORT_NOFOLDER_H
24 #define LLVM_SUPPORT_NOFOLDER_H
26 #include "llvm/Constants.h"
27 #include "llvm/Instructions.h"
33 /// NoFolder - Create "constants" (actually, values) with no folding.
36 explicit NoFolder(LLVMContext &) {}
38 //===--------------------------------------------------------------------===//
40 //===--------------------------------------------------------------------===//
42 Value *CreateAdd(Constant *LHS, Constant *RHS) const {
43 return BinaryOperator::CreateAdd(LHS, RHS);
45 Value *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
46 return BinaryOperator::CreateNSWAdd(LHS, RHS);
48 Value *CreateFAdd(Constant *LHS, Constant *RHS) const {
49 return BinaryOperator::CreateFAdd(LHS, RHS);
51 Value *CreateSub(Constant *LHS, Constant *RHS) const {
52 return BinaryOperator::CreateSub(LHS, RHS);
54 Value *CreateNSWSub(Constant *LHS, Constant *RHS) const {
55 return BinaryOperator::CreateNSWSub(LHS, RHS);
57 Value *CreateFSub(Constant *LHS, Constant *RHS) const {
58 return BinaryOperator::CreateFSub(LHS, RHS);
60 Value *CreateMul(Constant *LHS, Constant *RHS) const {
61 return BinaryOperator::CreateMul(LHS, RHS);
63 Value *CreateNSWMul(Constant *LHS, Constant *RHS) const {
64 return BinaryOperator::CreateNSWMul(LHS, RHS);
66 Value *CreateFMul(Constant *LHS, Constant *RHS) const {
67 return BinaryOperator::CreateFMul(LHS, RHS);
69 Value *CreateUDiv(Constant *LHS, Constant *RHS) const {
70 return BinaryOperator::CreateUDiv(LHS, RHS);
72 Value *CreateSDiv(Constant *LHS, Constant *RHS) const {
73 return BinaryOperator::CreateSDiv(LHS, RHS);
75 Value *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
76 return BinaryOperator::CreateExactSDiv(LHS, RHS);
78 Value *CreateFDiv(Constant *LHS, Constant *RHS) const {
79 return BinaryOperator::CreateFDiv(LHS, RHS);
81 Value *CreateURem(Constant *LHS, Constant *RHS) const {
82 return BinaryOperator::CreateURem(LHS, RHS);
84 Value *CreateSRem(Constant *LHS, Constant *RHS) const {
85 return BinaryOperator::CreateSRem(LHS, RHS);
87 Value *CreateFRem(Constant *LHS, Constant *RHS) const {
88 return BinaryOperator::CreateFRem(LHS, RHS);
90 Value *CreateShl(Constant *LHS, Constant *RHS) const {
91 return BinaryOperator::CreateShl(LHS, RHS);
93 Value *CreateLShr(Constant *LHS, Constant *RHS) const {
94 return BinaryOperator::CreateLShr(LHS, RHS);
96 Value *CreateAShr(Constant *LHS, Constant *RHS) const {
97 return BinaryOperator::CreateAShr(LHS, RHS);
99 Value *CreateAnd(Constant *LHS, Constant *RHS) const {
100 return BinaryOperator::CreateAnd(LHS, RHS);
102 Value *CreateOr(Constant *LHS, Constant *RHS) const {
103 return BinaryOperator::CreateOr(LHS, RHS);
105 Value *CreateXor(Constant *LHS, Constant *RHS) const {
106 return BinaryOperator::CreateXor(LHS, RHS);
109 Value *CreateBinOp(Instruction::BinaryOps Opc,
110 Constant *LHS, Constant *RHS) const {
111 return BinaryOperator::Create(Opc, LHS, RHS);
114 //===--------------------------------------------------------------------===//
116 //===--------------------------------------------------------------------===//
118 Value *CreateNeg(Constant *C) const {
119 return BinaryOperator::CreateNeg(C);
121 Value *CreateNSWNeg(Constant *C) const {
122 return BinaryOperator::CreateNSWNeg(C);
124 Value *CreateNot(Constant *C) const {
125 return BinaryOperator::CreateNot(C);
128 //===--------------------------------------------------------------------===//
129 // Memory Instructions
130 //===--------------------------------------------------------------------===//
132 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
133 unsigned NumIdx) const {
134 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
136 Value *CreateGetElementPtr(Constant *C, Value* const *IdxList,
137 unsigned NumIdx) const {
138 return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
141 Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
142 unsigned NumIdx) const {
143 return ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx);
145 Value *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
146 unsigned NumIdx) const {
147 return GetElementPtrInst::CreateInBounds(C, IdxList, IdxList+NumIdx);
150 //===--------------------------------------------------------------------===//
151 // Cast/Conversion Operators
152 //===--------------------------------------------------------------------===//
154 Value *CreateCast(Instruction::CastOps Op, Constant *C,
155 const Type *DestTy) const {
156 return CastInst::Create(Op, C, DestTy);
158 Value *CreateIntCast(Constant *C, const Type *DestTy,
159 bool isSigned) const {
160 return CastInst::CreateIntegerCast(C, DestTy, isSigned);
163 //===--------------------------------------------------------------------===//
164 // Compare Instructions
165 //===--------------------------------------------------------------------===//
167 Value *CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
168 return new ICmpInst(P, LHS, RHS);
170 Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
171 return new FCmpInst(P, LHS, RHS);
174 //===--------------------------------------------------------------------===//
175 // Other Instructions
176 //===--------------------------------------------------------------------===//
178 Value *CreateSelect(Constant *C, Constant *True, Constant *False) const {
179 return SelectInst::Create(C, True, False);
182 Value *CreateExtractElement(Constant *Vec, Constant *Idx) const {
183 return ExtractElementInst::Create(Vec, Idx);
186 Value *CreateInsertElement(Constant *Vec, Constant *NewElt,
187 Constant *Idx) const {
188 return InsertElementInst::Create(Vec, NewElt, Idx);
191 Value *CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const {
192 return new ShuffleVectorInst(V1, V2, Mask);
195 Value *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
196 unsigned NumIdx) const {
197 return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
200 Value *CreateInsertValue(Constant *Agg, Constant *Val,
201 const unsigned *IdxList, unsigned NumIdx) const {
202 return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);