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"
31 /// NoFolder - Create "constants" (actually, values) with no folding.
35 //===--------------------------------------------------------------------===//
37 //===--------------------------------------------------------------------===//
39 Value *CreateAdd(Constant *LHS, Constant *RHS) const {
40 return BinaryOperator::CreateAdd(LHS, RHS);
42 Value *CreateFAdd(Constant *LHS, Constant *RHS) const {
43 return BinaryOperator::CreateFAdd(LHS, RHS);
45 Value *CreateSub(Constant *LHS, Constant *RHS) const {
46 return BinaryOperator::CreateSub(LHS, RHS);
48 Value *CreateFSub(Constant *LHS, Constant *RHS) const {
49 return BinaryOperator::CreateFSub(LHS, RHS);
51 Value *CreateMul(Constant *LHS, Constant *RHS) const {
52 return BinaryOperator::CreateMul(LHS, RHS);
54 Value *CreateFMul(Constant *LHS, Constant *RHS) const {
55 return BinaryOperator::CreateFMul(LHS, RHS);
57 Value *CreateUDiv(Constant *LHS, Constant *RHS) const {
58 return BinaryOperator::CreateUDiv(LHS, RHS);
60 Value *CreateSDiv(Constant *LHS, Constant *RHS) const {
61 return BinaryOperator::CreateSDiv(LHS, RHS);
63 Value *CreateFDiv(Constant *LHS, Constant *RHS) const {
64 return BinaryOperator::CreateFDiv(LHS, RHS);
66 Value *CreateURem(Constant *LHS, Constant *RHS) const {
67 return BinaryOperator::CreateURem(LHS, RHS);
69 Value *CreateSRem(Constant *LHS, Constant *RHS) const {
70 return BinaryOperator::CreateSRem(LHS, RHS);
72 Value *CreateFRem(Constant *LHS, Constant *RHS) const {
73 return BinaryOperator::CreateFRem(LHS, RHS);
75 Value *CreateShl(Constant *LHS, Constant *RHS) const {
76 return BinaryOperator::CreateShl(LHS, RHS);
78 Value *CreateLShr(Constant *LHS, Constant *RHS) const {
79 return BinaryOperator::CreateLShr(LHS, RHS);
81 Value *CreateAShr(Constant *LHS, Constant *RHS) const {
82 return BinaryOperator::CreateAShr(LHS, RHS);
84 Value *CreateAnd(Constant *LHS, Constant *RHS) const {
85 return BinaryOperator::CreateAnd(LHS, RHS);
87 Value *CreateOr(Constant *LHS, Constant *RHS) const {
88 return BinaryOperator::CreateOr(LHS, RHS);
90 Value *CreateXor(Constant *LHS, Constant *RHS) const {
91 return BinaryOperator::CreateXor(LHS, RHS);
94 Value *CreateBinOp(Instruction::BinaryOps Opc,
95 Constant *LHS, Constant *RHS) const {
96 return BinaryOperator::Create(Opc, LHS, RHS);
99 //===--------------------------------------------------------------------===//
101 //===--------------------------------------------------------------------===//
103 Value *CreateNeg(Constant *C) const {
104 return BinaryOperator::CreateNeg(C);
106 Value *CreateNot(Constant *C) const {
107 return BinaryOperator::CreateNot(C);
110 //===--------------------------------------------------------------------===//
111 // Memory Instructions
112 //===--------------------------------------------------------------------===//
114 Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
115 unsigned NumIdx) const {
116 return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx);
118 Value *CreateGetElementPtr(Constant *C, Value* const *IdxList,
119 unsigned NumIdx) const {
120 return GetElementPtrInst::Create(C, IdxList, IdxList+NumIdx);
123 //===--------------------------------------------------------------------===//
124 // Cast/Conversion Operators
125 //===--------------------------------------------------------------------===//
127 Value *CreateCast(Instruction::CastOps Op, Constant *C,
128 const Type *DestTy) const {
129 return CastInst::Create(Op, C, DestTy);
131 Value *CreateIntCast(Constant *C, const Type *DestTy,
132 bool isSigned) const {
133 return CastInst::CreateIntegerCast(C, DestTy, isSigned);
136 //===--------------------------------------------------------------------===//
137 // Compare Instructions
138 //===--------------------------------------------------------------------===//
140 Value *CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
141 return new ICmpInst(P, LHS, RHS);
143 Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
144 return new FCmpInst(P, LHS, RHS);
146 Value *CreateVICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
147 return new VICmpInst(P, LHS, RHS);
149 Value *CreateVFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const {
150 return new VFCmpInst(P, LHS, RHS);
153 //===--------------------------------------------------------------------===//
154 // Other Instructions
155 //===--------------------------------------------------------------------===//
157 Value *CreateSelect(Constant *C, Constant *True, Constant *False) const {
158 return SelectInst::Create(C, True, False);
161 Value *CreateExtractElement(Constant *Vec, Constant *Idx) const {
162 return new ExtractElementInst(Vec, Idx);
165 Value *CreateInsertElement(Constant *Vec, Constant *NewElt,
166 Constant *Idx) const {
167 return InsertElementInst::Create(Vec, NewElt, Idx);
170 Value *CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const {
171 return new ShuffleVectorInst(V1, V2, Mask);
174 Value *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
175 unsigned NumIdx) const {
176 return ExtractValueInst::Create(Agg, IdxList, IdxList+NumIdx);
179 Value *CreateInsertValue(Constant *Agg, Constant *Val,
180 const unsigned *IdxList, unsigned NumIdx) const {
181 return InsertValueInst::Create(Agg, Val, IdxList, IdxList+NumIdx);