Skip to content

Commit

Permalink
[IRBuilder] Migrate most casts to folding API
Browse files Browse the repository at this point in the history
Migrate creation of most casts to use the FoldXYZ rather than
CreateXYZ style APIs. This means that InstSimplifyFolder now
works for these, which is what accounts for the AMDGPU test changes.
  • Loading branch information
nikic committed Sep 29, 2023
1 parent fb0f557 commit 4251aa7
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 213 deletions.
46 changes: 5 additions & 41 deletions llvm/include/llvm/Analysis/InstSimplifyFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,56 +112,20 @@ class InstSimplifyFolder final : public IRBuilderFolder {
return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
}

Value *FoldCast(Instruction::CastOps Op, Value *V,
Type *DestTy) const override {
return simplifyCastInst(Op, V, DestTy, SQ);
}

//===--------------------------------------------------------------------===//
// Cast/Conversion Operators
//===--------------------------------------------------------------------===//

Value *CreateCast(Instruction::CastOps Op, Constant *C,
Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return ConstFolder.CreateCast(Op, C, DestTy);
}
Value *CreateIntCast(Constant *C, Type *DestTy,
bool isSigned) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return ConstFolder.CreateIntCast(C, DestTy, isSigned);
}
Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return ConstFolder.CreatePointerCast(C, DestTy);
}
Value *CreateFPCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return ConstFolder.CreateFPCast(C, DestTy);
}
Value *CreateBitCast(Constant *C, Type *DestTy) const override {
return ConstFolder.CreateBitCast(C, DestTy);
}
Value *CreateIntToPtr(Constant *C, Type *DestTy) const override {
return ConstFolder.CreateIntToPtr(C, DestTy);
}
Value *CreatePtrToInt(Constant *C, Type *DestTy) const override {
return ConstFolder.CreatePtrToInt(C, DestTy);
}
Value *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return ConstFolder.CreateZExtOrBitCast(C, DestTy);
}
Value *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return ConstFolder.CreateSExtOrBitCast(C, DestTy);
}
Value *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return ConstFolder.CreateTruncOrBitCast(C, DestTy);
}

Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
Type *DestTy) const override {
Expand Down
48 changes: 7 additions & 41 deletions llvm/include/llvm/Analysis/TargetFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,56 +184,22 @@ class TargetFolder final : public IRBuilderFolder {
return nullptr;
}

Value *FoldCast(Instruction::CastOps Op, Value *V,
Type *DestTy) const override {
if (auto *C = dyn_cast<Constant>(V))
return Fold(ConstantExpr::getCast(Op, C, DestTy));
return nullptr;
}

//===--------------------------------------------------------------------===//
// Cast/Conversion Operators
//===--------------------------------------------------------------------===//

Constant *CreateCast(Instruction::CastOps Op, Constant *C,
Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return Fold(ConstantExpr::getCast(Op, C, DestTy));
}
Constant *CreateIntCast(Constant *C, Type *DestTy,
bool isSigned) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
}
Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return Fold(ConstantExpr::getPointerCast(C, DestTy));
}
Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return Fold(ConstantExpr::getFPCast(C, DestTy));
}
Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::BitCast, C, DestTy);
}
Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::IntToPtr, C, DestTy);
}
Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::PtrToInt, C, DestTy);
}
Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
}
Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
}
Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
if (C->getType() == DestTy)
return C; // avoid calling Fold
return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
}

Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
Type *DestTy) const override {
Expand Down
45 changes: 7 additions & 38 deletions llvm/include/llvm/IR/ConstantFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,17 @@ class ConstantFolder final : public IRBuilderFolder {
return nullptr;
}

Value *FoldCast(Instruction::CastOps Op, Value *V,
Type *DestTy) const override {
if (auto *C = dyn_cast<Constant>(V))
return ConstantExpr::getCast(Op, C, DestTy);
return nullptr;
}

//===--------------------------------------------------------------------===//
// Cast/Conversion Operators
//===--------------------------------------------------------------------===//

Constant *CreateCast(Instruction::CastOps Op, Constant *C,
Type *DestTy) const override {
return ConstantExpr::getCast(Op, C, DestTy);
}

Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
return ConstantExpr::getPointerCast(C, DestTy);
}
Expand All @@ -191,39 +193,6 @@ class ConstantFolder final : public IRBuilderFolder {
return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
}

Constant *CreateIntCast(Constant *C, Type *DestTy,
bool isSigned) const override {
return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
}

Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
return ConstantExpr::getFPCast(C, DestTy);
}

Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::BitCast, C, DestTy);
}

Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::IntToPtr, C, DestTy);
}

Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::PtrToInt, C, DestTy);
}

Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
return ConstantExpr::getZExtOrBitCast(C, DestTy);
}

Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
return ConstantExpr::getSExtOrBitCast(C, DestTy);
}

Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
return ConstantExpr::getTruncOrBitCast(C, DestTy);
}

//===--------------------------------------------------------------------===//
// Compare Instructions
//===--------------------------------------------------------------------===//
Expand Down
63 changes: 30 additions & 33 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2096,39 +2096,36 @@ class IRBuilderBase {
return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
}

Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
const Twine &Name = "") {
if (V->getType() == DestTy)
return V;
if (auto *VC = dyn_cast<Constant>(V))
return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
Value *CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
Instruction::CastOps CastOp =
V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
? Instruction::BitCast
: Instruction::ZExt;
return CreateCast(CastOp, V, DestTy, Name);
}

Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
const Twine &Name = "") {
if (V->getType() == DestTy)
return V;
if (auto *VC = dyn_cast<Constant>(V))
return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
Value *CreateSExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
Instruction::CastOps CastOp =
V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
? Instruction::BitCast
: Instruction::SExt;
return CreateCast(CastOp, V, DestTy, Name);
}

Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
const Twine &Name = "") {
if (V->getType() == DestTy)
return V;
if (auto *VC = dyn_cast<Constant>(V))
return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
Value *CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
Instruction::CastOps CastOp =
V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
? Instruction::BitCast
: Instruction::Trunc;
return CreateCast(CastOp, V, DestTy, Name);
}

Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
const Twine &Name = "") {
if (V->getType() == DestTy)
return V;
if (auto *VC = dyn_cast<Constant>(V))
return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
if (Value *Folded = Folder.FoldCast(Op, V, DestTy))
return Folded;
return Insert(CastInst::Create(Op, V, DestTy), Name);
}

Expand Down Expand Up @@ -2157,11 +2154,11 @@ class IRBuilderBase {

Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
const Twine &Name = "") {
if (V->getType() == DestTy)
return V;
if (auto *VC = dyn_cast<Constant>(V))
return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
Instruction::CastOps CastOp =
V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
? Instruction::Trunc
: (isSigned ? Instruction::SExt : Instruction::ZExt);
return CreateCast(CastOp, V, DestTy, Name);
}

Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
Expand All @@ -2177,11 +2174,11 @@ class IRBuilderBase {
}

Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
if (V->getType() == DestTy)
return V;
if (auto *VC = dyn_cast<Constant>(V))
return Insert(Folder.CreateFPCast(VC, DestTy), Name);
return Insert(CastInst::CreateFPCast(V, DestTy), Name);
Instruction::CastOps CastOp =
V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
? Instruction::FPTrunc
: Instruction::FPExt;
return CreateCast(CastOp, V, DestTy, Name);
}

CallInst *CreateConstrainedFPCast(
Expand Down
14 changes: 3 additions & 11 deletions llvm/include/llvm/IR/IRBuilderFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,16 @@ class IRBuilderFolder {
virtual Value *FoldShuffleVector(Value *V1, Value *V2,
ArrayRef<int> Mask) const = 0;

virtual Value *FoldCast(Instruction::CastOps Op, Value *V,
Type *DestTy) 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
Expand Down
43 changes: 5 additions & 38 deletions llvm/include/llvm/IR/NoFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ class NoFolder final : public IRBuilderFolder {
return nullptr;
}

Value *FoldCast(Instruction::CastOps Op, Value *V,
Type *DestTy) const override {
return nullptr;
}

//===--------------------------------------------------------------------===//
// Cast/Conversion Operators
//===--------------------------------------------------------------------===//

Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
Type *DestTy) const override {
return CastInst::Create(Op, C, DestTy);
}

Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
return CastInst::CreatePointerCast(C, DestTy);
}
Expand All @@ -125,39 +125,6 @@ class NoFolder final : public IRBuilderFolder {
return CastInst::CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
}

Instruction *CreateIntCast(Constant *C, Type *DestTy,
bool isSigned) const override {
return CastInst::CreateIntegerCast(C, DestTy, isSigned);
}

Instruction *CreateFPCast(Constant *C, Type *DestTy) const override {
return CastInst::CreateFPCast(C, DestTy);
}

Instruction *CreateBitCast(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::BitCast, C, DestTy);
}

Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::IntToPtr, C, DestTy);
}

Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const override {
return CreateCast(Instruction::PtrToInt, C, DestTy);
}

Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
return CastInst::CreateZExtOrBitCast(C, DestTy);
}

Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
return CastInst::CreateSExtOrBitCast(C, DestTy);
}

Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
return CastInst::CreateTruncOrBitCast(C, DestTy);
}

//===--------------------------------------------------------------------===//
// Compare Instructions
//===--------------------------------------------------------------------===//
Expand Down
Loading

0 comments on commit 4251aa7

Please sign in to comment.