Skip to content

Commit 4251aa7

Browse files
committed
[IRBuilder] Migrate most casts to folding API
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.
1 parent fb0f557 commit 4251aa7

File tree

8 files changed

+62
-213
lines changed

8 files changed

+62
-213
lines changed

llvm/include/llvm/Analysis/InstSimplifyFolder.h

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -112,56 +112,20 @@ class InstSimplifyFolder final : public IRBuilderFolder {
112112
return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
113113
}
114114

115+
Value *FoldCast(Instruction::CastOps Op, Value *V,
116+
Type *DestTy) const override {
117+
return simplifyCastInst(Op, V, DestTy, SQ);
118+
}
119+
115120
//===--------------------------------------------------------------------===//
116121
// Cast/Conversion Operators
117122
//===--------------------------------------------------------------------===//
118123

119-
Value *CreateCast(Instruction::CastOps Op, Constant *C,
120-
Type *DestTy) const override {
121-
if (C->getType() == DestTy)
122-
return C; // avoid calling Fold
123-
return ConstFolder.CreateCast(Op, C, DestTy);
124-
}
125-
Value *CreateIntCast(Constant *C, Type *DestTy,
126-
bool isSigned) const override {
127-
if (C->getType() == DestTy)
128-
return C; // avoid calling Fold
129-
return ConstFolder.CreateIntCast(C, DestTy, isSigned);
130-
}
131124
Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
132125
if (C->getType() == DestTy)
133126
return C; // avoid calling Fold
134127
return ConstFolder.CreatePointerCast(C, DestTy);
135128
}
136-
Value *CreateFPCast(Constant *C, Type *DestTy) const override {
137-
if (C->getType() == DestTy)
138-
return C; // avoid calling Fold
139-
return ConstFolder.CreateFPCast(C, DestTy);
140-
}
141-
Value *CreateBitCast(Constant *C, Type *DestTy) const override {
142-
return ConstFolder.CreateBitCast(C, DestTy);
143-
}
144-
Value *CreateIntToPtr(Constant *C, Type *DestTy) const override {
145-
return ConstFolder.CreateIntToPtr(C, DestTy);
146-
}
147-
Value *CreatePtrToInt(Constant *C, Type *DestTy) const override {
148-
return ConstFolder.CreatePtrToInt(C, DestTy);
149-
}
150-
Value *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
151-
if (C->getType() == DestTy)
152-
return C; // avoid calling Fold
153-
return ConstFolder.CreateZExtOrBitCast(C, DestTy);
154-
}
155-
Value *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
156-
if (C->getType() == DestTy)
157-
return C; // avoid calling Fold
158-
return ConstFolder.CreateSExtOrBitCast(C, DestTy);
159-
}
160-
Value *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
161-
if (C->getType() == DestTy)
162-
return C; // avoid calling Fold
163-
return ConstFolder.CreateTruncOrBitCast(C, DestTy);
164-
}
165129

166130
Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
167131
Type *DestTy) const override {

llvm/include/llvm/Analysis/TargetFolder.h

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -184,56 +184,22 @@ class TargetFolder final : public IRBuilderFolder {
184184
return nullptr;
185185
}
186186

187+
Value *FoldCast(Instruction::CastOps Op, Value *V,
188+
Type *DestTy) const override {
189+
if (auto *C = dyn_cast<Constant>(V))
190+
return Fold(ConstantExpr::getCast(Op, C, DestTy));
191+
return nullptr;
192+
}
193+
187194
//===--------------------------------------------------------------------===//
188195
// Cast/Conversion Operators
189196
//===--------------------------------------------------------------------===//
190197

191-
Constant *CreateCast(Instruction::CastOps Op, Constant *C,
192-
Type *DestTy) const override {
193-
if (C->getType() == DestTy)
194-
return C; // avoid calling Fold
195-
return Fold(ConstantExpr::getCast(Op, C, DestTy));
196-
}
197-
Constant *CreateIntCast(Constant *C, Type *DestTy,
198-
bool isSigned) const override {
199-
if (C->getType() == DestTy)
200-
return C; // avoid calling Fold
201-
return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
202-
}
203198
Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
204199
if (C->getType() == DestTy)
205200
return C; // avoid calling Fold
206201
return Fold(ConstantExpr::getPointerCast(C, DestTy));
207202
}
208-
Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
209-
if (C->getType() == DestTy)
210-
return C; // avoid calling Fold
211-
return Fold(ConstantExpr::getFPCast(C, DestTy));
212-
}
213-
Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
214-
return CreateCast(Instruction::BitCast, C, DestTy);
215-
}
216-
Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
217-
return CreateCast(Instruction::IntToPtr, C, DestTy);
218-
}
219-
Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
220-
return CreateCast(Instruction::PtrToInt, C, DestTy);
221-
}
222-
Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
223-
if (C->getType() == DestTy)
224-
return C; // avoid calling Fold
225-
return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
226-
}
227-
Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
228-
if (C->getType() == DestTy)
229-
return C; // avoid calling Fold
230-
return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
231-
}
232-
Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
233-
if (C->getType() == DestTy)
234-
return C; // avoid calling Fold
235-
return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
236-
}
237203

238204
Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
239205
Type *DestTy) const override {

llvm/include/llvm/IR/ConstantFolder.h

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,17 @@ class ConstantFolder final : public IRBuilderFolder {
173173
return nullptr;
174174
}
175175

176+
Value *FoldCast(Instruction::CastOps Op, Value *V,
177+
Type *DestTy) const override {
178+
if (auto *C = dyn_cast<Constant>(V))
179+
return ConstantExpr::getCast(Op, C, DestTy);
180+
return nullptr;
181+
}
182+
176183
//===--------------------------------------------------------------------===//
177184
// Cast/Conversion Operators
178185
//===--------------------------------------------------------------------===//
179186

180-
Constant *CreateCast(Instruction::CastOps Op, Constant *C,
181-
Type *DestTy) const override {
182-
return ConstantExpr::getCast(Op, C, DestTy);
183-
}
184-
185187
Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
186188
return ConstantExpr::getPointerCast(C, DestTy);
187189
}
@@ -191,39 +193,6 @@ class ConstantFolder final : public IRBuilderFolder {
191193
return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
192194
}
193195

194-
Constant *CreateIntCast(Constant *C, Type *DestTy,
195-
bool isSigned) const override {
196-
return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
197-
}
198-
199-
Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
200-
return ConstantExpr::getFPCast(C, DestTy);
201-
}
202-
203-
Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
204-
return CreateCast(Instruction::BitCast, C, DestTy);
205-
}
206-
207-
Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
208-
return CreateCast(Instruction::IntToPtr, C, DestTy);
209-
}
210-
211-
Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
212-
return CreateCast(Instruction::PtrToInt, C, DestTy);
213-
}
214-
215-
Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
216-
return ConstantExpr::getZExtOrBitCast(C, DestTy);
217-
}
218-
219-
Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
220-
return ConstantExpr::getSExtOrBitCast(C, DestTy);
221-
}
222-
223-
Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
224-
return ConstantExpr::getTruncOrBitCast(C, DestTy);
225-
}
226-
227196
//===--------------------------------------------------------------------===//
228197
// Compare Instructions
229198
//===--------------------------------------------------------------------===//

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,39 +2096,36 @@ class IRBuilderBase {
20962096
return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
20972097
}
20982098

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

2108-
Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
2109-
const Twine &Name = "") {
2110-
if (V->getType() == DestTy)
2111-
return V;
2112-
if (auto *VC = dyn_cast<Constant>(V))
2113-
return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
2114-
return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
2107+
Value *CreateSExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2108+
Instruction::CastOps CastOp =
2109+
V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2110+
? Instruction::BitCast
2111+
: Instruction::SExt;
2112+
return CreateCast(CastOp, V, DestTy, Name);
21152113
}
21162114

2117-
Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
2118-
const Twine &Name = "") {
2119-
if (V->getType() == DestTy)
2120-
return V;
2121-
if (auto *VC = dyn_cast<Constant>(V))
2122-
return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
2123-
return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
2115+
Value *CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2116+
Instruction::CastOps CastOp =
2117+
V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2118+
? Instruction::BitCast
2119+
: Instruction::Trunc;
2120+
return CreateCast(CastOp, V, DestTy, Name);
21242121
}
21252122

21262123
Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
21272124
const Twine &Name = "") {
21282125
if (V->getType() == DestTy)
21292126
return V;
2130-
if (auto *VC = dyn_cast<Constant>(V))
2131-
return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
2127+
if (Value *Folded = Folder.FoldCast(Op, V, DestTy))
2128+
return Folded;
21322129
return Insert(CastInst::Create(Op, V, DestTy), Name);
21332130
}
21342131

@@ -2157,11 +2154,11 @@ class IRBuilderBase {
21572154

21582155
Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
21592156
const Twine &Name = "") {
2160-
if (V->getType() == DestTy)
2161-
return V;
2162-
if (auto *VC = dyn_cast<Constant>(V))
2163-
return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
2164-
return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
2157+
Instruction::CastOps CastOp =
2158+
V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2159+
? Instruction::Trunc
2160+
: (isSigned ? Instruction::SExt : Instruction::ZExt);
2161+
return CreateCast(CastOp, V, DestTy, Name);
21652162
}
21662163

21672164
Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
@@ -2177,11 +2174,11 @@ class IRBuilderBase {
21772174
}
21782175

21792176
Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
2180-
if (V->getType() == DestTy)
2181-
return V;
2182-
if (auto *VC = dyn_cast<Constant>(V))
2183-
return Insert(Folder.CreateFPCast(VC, DestTy), Name);
2184-
return Insert(CastInst::CreateFPCast(V, DestTy), Name);
2177+
Instruction::CastOps CastOp =
2178+
V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2179+
? Instruction::FPTrunc
2180+
: Instruction::FPExt;
2181+
return CreateCast(CastOp, V, DestTy, Name);
21852182
}
21862183

21872184
CallInst *CreateConstrainedFPCast(

llvm/include/llvm/IR/IRBuilderFolder.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,16 @@ class IRBuilderFolder {
7070
virtual Value *FoldShuffleVector(Value *V1, Value *V2,
7171
ArrayRef<int> Mask) const = 0;
7272

73+
virtual Value *FoldCast(Instruction::CastOps Op, Value *V,
74+
Type *DestTy) const = 0;
75+
7376
//===--------------------------------------------------------------------===//
7477
// Cast/Conversion Operators
7578
//===--------------------------------------------------------------------===//
7679

77-
virtual Value *CreateCast(Instruction::CastOps Op, Constant *C,
78-
Type *DestTy) const = 0;
7980
virtual Value *CreatePointerCast(Constant *C, Type *DestTy) const = 0;
8081
virtual Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
8182
Type *DestTy) const = 0;
82-
virtual Value *CreateIntCast(Constant *C, Type *DestTy,
83-
bool isSigned) const = 0;
84-
virtual Value *CreateFPCast(Constant *C, Type *DestTy) const = 0;
85-
virtual Value *CreateBitCast(Constant *C, Type *DestTy) const = 0;
86-
virtual Value *CreateIntToPtr(Constant *C, Type *DestTy) const = 0;
87-
virtual Value *CreatePtrToInt(Constant *C, Type *DestTy) const = 0;
88-
virtual Value *CreateZExtOrBitCast(Constant *C, Type *DestTy) const = 0;
89-
virtual Value *CreateSExtOrBitCast(Constant *C, Type *DestTy) const = 0;
90-
virtual Value *CreateTruncOrBitCast(Constant *C, Type *DestTy) const = 0;
9183

9284
//===--------------------------------------------------------------------===//
9385
// Compare Instructions

llvm/include/llvm/IR/NoFolder.h

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ class NoFolder final : public IRBuilderFolder {
107107
return nullptr;
108108
}
109109

110+
Value *FoldCast(Instruction::CastOps Op, Value *V,
111+
Type *DestTy) const override {
112+
return nullptr;
113+
}
114+
110115
//===--------------------------------------------------------------------===//
111116
// Cast/Conversion Operators
112117
//===--------------------------------------------------------------------===//
113118

114-
Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
115-
Type *DestTy) const override {
116-
return CastInst::Create(Op, C, DestTy);
117-
}
118-
119119
Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
120120
return CastInst::CreatePointerCast(C, DestTy);
121121
}
@@ -125,39 +125,6 @@ class NoFolder final : public IRBuilderFolder {
125125
return CastInst::CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
126126
}
127127

128-
Instruction *CreateIntCast(Constant *C, Type *DestTy,
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-
161128
//===--------------------------------------------------------------------===//
162129
// Compare Instructions
163130
//===--------------------------------------------------------------------===//

0 commit comments

Comments
 (0)