LLVM 18.0.0git
NoFolder.h
Go to the documentation of this file.
1//===- NoFolder.h - Constant folding helper ---------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the NoFolder class, a helper for IRBuilder. It provides
10// IRBuilder with a set of methods for creating unfolded constants. This is
11// useful for learners trying to understand how LLVM IR works, and who don't
12// want details to be hidden by the constant folder. For general constant
13// creation and folding, use ConstantExpr and the routines in
14// llvm/Analysis/ConstantFolding.h.
15//
16// Note: since it is not actually possible to create unfolded constants, this
17// class returns instructions rather than constants.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_IR_NOFOLDER_H
22#define LLVM_IR_NOFOLDER_H
23
24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/FMF.h"
28#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
31
32namespace llvm {
33
34/// NoFolder - Create "constants" (actually, instructions) with no folding.
35class NoFolder final : public IRBuilderFolder {
36 virtual void anchor();
37
38public:
39 explicit NoFolder() = default;
40
41 //===--------------------------------------------------------------------===//
42 // Value-based folders.
43 //
44 // Return an existing value or a constant if the operation can be simplified.
45 // Otherwise return nullptr.
46 //===--------------------------------------------------------------------===//
47
49 Value *RHS) const override {
50 return nullptr;
51 }
52
54 bool IsExact) const override {
55 return nullptr;
56 }
57
59 bool HasNUW, bool HasNSW) const override {
60 return nullptr;
61 }
62
64 FastMathFlags FMF) const override {
65 return nullptr;
66 }
67
69 FastMathFlags FMF) const override {
70 return nullptr;
71 }
72
74 return nullptr;
75 }
76
78 bool IsInBounds = false) const override {
79 return nullptr;
80 }
81
82 Value *FoldSelect(Value *C, Value *True, Value *False) const override {
83 return nullptr;
84 }
85
87 ArrayRef<unsigned> IdxList) const override {
88 return nullptr;
89 }
90
92 ArrayRef<unsigned> IdxList) const override {
93 return nullptr;
94 }
95
96 Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
97 return nullptr;
98 }
99
101 Value *Idx) const override {
102 return nullptr;
103 }
104
106 ArrayRef<int> Mask) const override {
107 return nullptr;
108 }
109
110 //===--------------------------------------------------------------------===//
111 // Cast/Conversion Operators
112 //===--------------------------------------------------------------------===//
113
115 Type *DestTy) const override {
116 return CastInst::Create(Op, C, DestTy);
117 }
118
119 Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
120 return CastInst::CreatePointerCast(C, DestTy);
121 }
122
124 Constant *C, Type *DestTy) const override {
126 }
127
129 bool isSigned) const override {
130 return CastInst::CreateIntegerCast(C, DestTy, isSigned);
131 }
132
133 Instruction *CreateFPCast(Constant *C, Type *DestTy) const override {
134 return CastInst::CreateFPCast(C, DestTy);
135 }
136
137 Instruction *CreateBitCast(Constant *C, Type *DestTy) const override {
138 return CreateCast(Instruction::BitCast, C, DestTy);
139 }
140
141 Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const override {
142 return CreateCast(Instruction::IntToPtr, C, DestTy);
143 }
144
145 Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const override {
146 return CreateCast(Instruction::PtrToInt, C, DestTy);
147 }
148
149 Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
150 return CastInst::CreateZExtOrBitCast(C, DestTy);
151 }
152
153 Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
154 return CastInst::CreateSExtOrBitCast(C, DestTy);
155 }
156
157 Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
158 return CastInst::CreateTruncOrBitCast(C, DestTy);
159 }
160
161 //===--------------------------------------------------------------------===//
162 // Compare Instructions
163 //===--------------------------------------------------------------------===//
164
166 Constant *LHS, Constant *RHS) const override {
167 return new FCmpInst(P, LHS, RHS);
168 }
169};
170
171} // end namespace llvm
172
173#endif // LLVM_IR_NOFOLDER_H
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static bool isSigned(unsigned int Opcode)
#define P(N)
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
static CastInst * CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a SExt or BitCast cast instruction.
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
static CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a Trunc or BitCast cast instruction.
static CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:711
This is an important base class in LLVM.
Definition: Constant.h:41
This class represents an Operation in the Expression.
This instruction compares its operands according to the predicate given to the constructor.
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
IRBuilderFolder - Interface for constant folding in IRBuilder.
NoFolder - Create "constants" (actually, instructions) with no folding.
Definition: NoFolder.h:35
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
Definition: NoFolder.h:105
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Definition: NoFolder.h:53
Instruction * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:141
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Definition: NoFolder.h:82
Value * FoldBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS) const override
Definition: NoFolder.h:48
Instruction * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:137
Instruction * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:153
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Definition: NoFolder.h:73
Instruction * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: NoFolder.h:128
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) const override
Definition: NoFolder.h:77
Instruction * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:157
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Definition: NoFolder.h:91
Instruction * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:145
Instruction * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:165
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Definition: NoFolder.h:63
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Definition: NoFolder.h:58
Instruction * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:133
Instruction * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:123
Value * FoldExtractElement(Value *Vec, Value *Idx) const override
Definition: NoFolder.h:96
Instruction * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:119
Value * FoldInsertElement(Value *Vec, Value *NewElt, Value *Idx) const override
Definition: NoFolder.h:100
Instruction * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: NoFolder.h:114
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Definition: NoFolder.h:86
NoFolder()=default
Instruction * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:149
Value * FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override
Definition: NoFolder.h:68
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18