1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 11:13:28 +01:00
llvm-mirror/include/llvm/IR/IRBuilderFolder.h

142 lines
7.1 KiB
C
Raw Normal View History

Reapply "[IRBuilder] Virtualize IRBuilder" Relative to the original commit, this fixes some warnings, and is based on the deletion of the IRBuilder copy constructor in D74693. The automatic copy constructor would no longer be safe. ----- Related llvm-dev thread: http://lists.llvm.org/pipermail/llvm-dev/2020-February/138951.html This patch moves the IRBuilder from templating over the constant folder and inserter towards making both of these virtual. There are a couple of motivations for this: 1. It's not possible to share code between use-sites that use different IRBuilder folders/inserters (short of templating the code and moving it into headers). 2. Methods currently defined on IRBuilderBase (which is not templated) do not use the custom inserter, resulting in subtle bugs (e.g. incorrect InstCombine worklist management). It would be possible to move those into the templated IRBuilder, but... 3. The vast majority of the IRBuilder implementation has to live in the header, because it depends on the template arguments. 4. We have many unnecessary dependencies on IRBuilder.h, because it is not easy to forward-declare. (Significant parts of the backend depend on it via TargetLowering.h, for example.) This patch addresses the issue by making the following changes: * IRBuilderDefaultInserter::InsertHelper becomes virtual. IRBuilderBase accepts a reference to it. * IRBuilderFolder is introduced as a virtual base class. It is implemented by ConstantFolder (default), NoFolder and TargetFolder. IRBuilderBase has a reference to this as well. * All the logic is moved from IRBuilder to IRBuilderBase. This means that methods can in the future replace their IRBuilder<> & uses (or other specific IRBuilder types) with IRBuilderBase & and thus be usable with different IRBuilders. * The IRBuilder class is now a thin wrapper around IRBuilderBase. Essentially it only stores the folder and inserter and takes care of constructing the base builder. What this patch doesn't do, but should be simple followups after this change: * Fixing use of the inserter for creation methods originally defined on IRBuilderBase. * Replacing IRBuilder<> uses in arguments with IRBuilderBase, where useful. * Moving code from the IRBuilder header to the source file. From the user perspective, these changes should be mostly transparent: The only thing that consumers using a custom inserted may need to do is inherit from IRBuilderDefaultInserter publicly and mark their InsertHelper as public. Differential Revision: https://reviews.llvm.org/D73835
2020-02-16 17:10:09 +01:00
//===- IRBuilderFolder.h - Const folder interface for IRBuilder -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines for constant folding interface used by IRBuilder.
// It is implemented by ConstantFolder (default), TargetFolder and NoFoler.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_IR_IRBUILDERFOLDER_H
#define LLVM_IR_IRBUILDERFOLDER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
namespace llvm {
/// IRBuilderFolder - Interface for constant folding in IRBuilder.
class IRBuilderFolder {
public:
virtual ~IRBuilderFolder();
//===--------------------------------------------------------------------===//
// Binary Operators
//===--------------------------------------------------------------------===//
virtual Value *CreateAdd(Constant *LHS, Constant *RHS,
bool HasNUW = false, bool HasNSW = false) const = 0;
virtual Value *CreateFAdd(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateSub(Constant *LHS, Constant *RHS,
bool HasNUW = false, bool HasNSW = false) const = 0;
virtual Value *CreateFSub(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateMul(Constant *LHS, Constant *RHS,
bool HasNUW = false, bool HasNSW = false) const = 0;
virtual Value *CreateFMul(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateUDiv(Constant *LHS, Constant *RHS,
bool isExact = false) const = 0;
virtual Value *CreateSDiv(Constant *LHS, Constant *RHS,
bool isExact = false) const = 0;
virtual Value *CreateFDiv(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateURem(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateSRem(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateFRem(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateShl(Constant *LHS, Constant *RHS,
bool HasNUW = false, bool HasNSW = false) const = 0;
virtual Value *CreateLShr(Constant *LHS, Constant *RHS,
bool isExact = false) const = 0;
virtual Value *CreateAShr(Constant *LHS, Constant *RHS,
bool isExact = false) const = 0;
virtual Value *CreateAnd(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateOr(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateXor(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateBinOp(Instruction::BinaryOps Opc,
Constant *LHS, Constant *RHS) const = 0;
//===--------------------------------------------------------------------===//
// Unary Operators
//===--------------------------------------------------------------------===//
virtual Value *CreateNeg(Constant *C,
bool HasNUW = false, bool HasNSW = false) const = 0;
virtual Value *CreateFNeg(Constant *C) const = 0;
virtual Value *CreateNot(Constant *C) const = 0;
virtual Value *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const = 0;
//===--------------------------------------------------------------------===//
// Memory Instructions
//===--------------------------------------------------------------------===//
virtual Value *CreateGetElementPtr(Type *Ty, Constant *C,
ArrayRef<Constant *> IdxList) const = 0;
// This form of the function only exists to avoid ambiguous overload
// warnings about whether to convert Idx to ArrayRef<Constant *> or
// ArrayRef<Value *>.
virtual Value *CreateGetElementPtr(Type *Ty, Constant *C,
Constant *Idx) const = 0;
virtual Value *CreateGetElementPtr(Type *Ty, Constant *C,
ArrayRef<Value *> IdxList) const = 0;
virtual Value *CreateInBoundsGetElementPtr(
Type *Ty, Constant *C, ArrayRef<Constant *> IdxList) const = 0;
// This form of the function only exists to avoid ambiguous overload
// warnings about whether to convert Idx to ArrayRef<Constant *> or
// ArrayRef<Value *>.
virtual Value *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
Constant *Idx) const = 0;
virtual Value *CreateInBoundsGetElementPtr(
Type *Ty, Constant *C, ArrayRef<Value *> IdxList) const = 0;
//===--------------------------------------------------------------------===//
// Cast/Conversion Operators
//===--------------------------------------------------------------------===//
virtual Value *CreateCast(Instruction::CastOps Op, Constant *C,
Type *DestTy) const = 0;
virtual Value *CreatePointerCast(Constant *C, Type *DestTy) const = 0;
virtual Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
Type *DestTy) const = 0;
virtual Value *CreateIntCast(Constant *C, Type *DestTy,
bool isSigned) const = 0;
virtual Value *CreateFPCast(Constant *C, Type *DestTy) const = 0;
virtual Value *CreateBitCast(Constant *C, Type *DestTy) const = 0;
virtual Value *CreateIntToPtr(Constant *C, Type *DestTy) const = 0;
virtual Value *CreatePtrToInt(Constant *C, Type *DestTy) const = 0;
virtual Value *CreateZExtOrBitCast(Constant *C, Type *DestTy) const = 0;
virtual Value *CreateSExtOrBitCast(Constant *C, Type *DestTy) const = 0;
virtual Value *CreateTruncOrBitCast(Constant *C, Type *DestTy) const = 0;
//===--------------------------------------------------------------------===//
// Compare Instructions
//===--------------------------------------------------------------------===//
virtual Value *CreateICmp(CmpInst::Predicate P, Constant *LHS,
Constant *RHS) const = 0;
virtual Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
Constant *RHS) const = 0;
//===--------------------------------------------------------------------===//
// Other Instructions
//===--------------------------------------------------------------------===//
virtual Value *CreateSelect(Constant *C, Constant *True,
Constant *False) const = 0;
virtual Value *CreateExtractElement(Constant *Vec, Constant *Idx) const = 0;
virtual Value *CreateInsertElement(Constant *Vec, Constant *NewElt,
Constant *Idx) const = 0;
virtual Value *CreateShuffleVector(Constant *V1, Constant *V2,
Constant *Mask) const = 0;
virtual Value *CreateExtractValue(Constant *Agg,
ArrayRef<unsigned> IdxList) const = 0;
virtual Value *CreateInsertValue(Constant *Agg, Constant *Val,
ArrayRef<unsigned> IdxList) const = 0;
};
} // end namespace llvm
#endif // LLVM_IR_IRBUILDERFOLDER_H