Skip to content

Commit 4b367e0

Browse files
authored
[WebAssembly] Use IRBuilder in FixFunctionBitcasts (NFC) (#164268)
Simplifies the code a bit.
1 parent 1458d31 commit 4b367e0

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyFixFunctionBitcasts.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "WebAssembly.h"
2626
#include "llvm/IR/Constants.h"
27+
#include "llvm/IR/IRBuilder.h"
2728
#include "llvm/IR/Instructions.h"
2829
#include "llvm/IR/Module.h"
2930
#include "llvm/IR/Operator.h"
@@ -114,6 +115,7 @@ static Function *createWrapper(Function *F, FunctionType *Ty) {
114115
Wrapper->setAttributes(F->getAttributes());
115116
BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper);
116117
const DataLayout &DL = BB->getDataLayout();
118+
IRBuilder<> Builder(BB);
117119

118120
// Determine what arguments to pass.
119121
SmallVector<Value *, 4> Args;
@@ -140,10 +142,7 @@ static Function *createWrapper(Function *F, FunctionType *Ty) {
140142
Args.push_back(&*AI);
141143
} else {
142144
if (CastInst::isBitOrNoopPointerCastable(ArgType, ParamType, DL)) {
143-
Instruction *PtrCast =
144-
CastInst::CreateBitOrPointerCast(AI, ParamType, "cast");
145-
PtrCast->insertInto(BB, BB->end());
146-
Args.push_back(PtrCast);
145+
Args.push_back(Builder.CreateBitOrPointerCast(AI, ParamType, "cast"));
147146
} else if (ArgType->isStructTy() || ParamType->isStructTy()) {
148147
LLVM_DEBUG(dbgs() << "createWrapper: struct param type in bitcast: "
149148
<< F->getName() << "\n");
@@ -166,24 +165,19 @@ static Function *createWrapper(Function *F, FunctionType *Ty) {
166165
for (; AI != AE; ++AI)
167166
Args.push_back(&*AI);
168167

169-
CallInst *Call = CallInst::Create(F, Args, "", BB);
168+
CallInst *Call = Builder.CreateCall(F, Args);
170169

171-
Type *ExpectedRtnType = F->getFunctionType()->getReturnType();
172-
Type *RtnType = Ty->getReturnType();
173170
// Determine what value to return.
174171
if (RtnType->isVoidTy()) {
175-
ReturnInst::Create(M->getContext(), BB);
172+
Builder.CreateRetVoid();
176173
} else if (ExpectedRtnType->isVoidTy()) {
177174
LLVM_DEBUG(dbgs() << "Creating dummy return: " << *RtnType << "\n");
178-
ReturnInst::Create(M->getContext(), PoisonValue::get(RtnType), BB);
175+
Builder.CreateRet(PoisonValue::get(RtnType));
179176
} else if (RtnType == ExpectedRtnType) {
180-
ReturnInst::Create(M->getContext(), Call, BB);
177+
Builder.CreateRet(Call);
181178
} else if (CastInst::isBitOrNoopPointerCastable(ExpectedRtnType, RtnType,
182179
DL)) {
183-
Instruction *Cast =
184-
CastInst::CreateBitOrPointerCast(Call, RtnType, "cast");
185-
Cast->insertInto(BB, BB->end());
186-
ReturnInst::Create(M->getContext(), Cast, BB);
180+
Builder.CreateRet(Builder.CreateBitOrPointerCast(Call, RtnType, "cast"));
187181
} else if (RtnType->isStructTy() || ExpectedRtnType->isStructTy()) {
188182
LLVM_DEBUG(dbgs() << "createWrapper: struct return type in bitcast: "
189183
<< F->getName() << "\n");
@@ -203,9 +197,8 @@ static Function *createWrapper(Function *F, FunctionType *Ty) {
203197
Wrapper = Function::Create(Ty, Function::PrivateLinkage,
204198
F->getName() + "_bitcast_invalid", M);
205199
Wrapper->setAttributes(F->getAttributes());
206-
BasicBlock *BB = BasicBlock::Create(M->getContext(), "body", Wrapper);
207-
new UnreachableInst(M->getContext(), BB);
208-
Wrapper->setName(F->getName() + "_bitcast_invalid");
200+
IRBuilder<> Builder(BasicBlock::Create(M->getContext(), "body", Wrapper));
201+
Builder.CreateUnreachable();
209202
} else if (!WrapperNeeded) {
210203
LLVM_DEBUG(dbgs() << "createWrapper: no wrapper needed: " << F->getName()
211204
<< "\n");

llvm/test/CodeGen/WebAssembly/unsupported-function-bitcasts.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare i32 @has_ptr_arg(ptr)
1010

1111
; CHECK-LABEL: test_invalid_rtn:
1212
; CHECK: i32.const $push[[L0:[0-9]+]]=, 0{{$}}
13-
; CHECK-NEXT: call $push[[L1:[0-9]+]]=, .Lhas_i64_arg_bitcast_invalid.2, $pop[[L0]]{{$}}
13+
; CHECK-NEXT: call $push[[L1:[0-9]+]]=, .Lhas_i64_arg_bitcast_invalid.1, $pop[[L0]]{{$}}
1414
; CHECK-NEXT: drop $pop[[L1]]{{$}}
1515
; CHECK-NEXT: i64.const $push[[L0:[0-9]+]]=, 0{{$}}
1616
; CHECK-NEXT: call $push[[L1:[0-9]+]]=, .Lhas_i64_arg_bitcast_invalid, $pop[[L0]]{{$}}
@@ -32,7 +32,7 @@ define void @test_struct_rtn() {
3232

3333
; CHECK-LABEL: test_invalid_arg:
3434
; CHECK: i32.const $push[[L0:[0-9]+]]=, 2{{$}}
35-
; CHECK-NEXT: call $push[[L1:[0-9]+]]=, .Lhas_ptr_arg_bitcast_invalid.4, $pop[[L0]]{{$}}
35+
; CHECK-NEXT: call $push[[L1:[0-9]+]]=, .Lhas_ptr_arg_bitcast_invalid.2, $pop[[L0]]{{$}}
3636
; CHECK-NEXT: drop $pop[[L1]]{{$}}
3737
; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 2{{$}}
3838
; CHECK-NEXT: call $push[[L1:[0-9]+]]=, has_ptr_arg, $pop[[L0]]{{$}}
@@ -54,8 +54,8 @@ entry:
5454
; CHECK-NEXT: unreachable
5555
; CHECK-NEXT: end_function
5656

57-
; CHECK-LABEL: .Lhas_i64_arg_bitcast_invalid.2:
58-
; CHECK-NEXT: .functype .Lhas_i64_arg_bitcast_invalid.2 (i32) -> (i32)
57+
; CHECK-LABEL: .Lhas_i64_arg_bitcast_invalid.1:
58+
; CHECK-NEXT: .functype .Lhas_i64_arg_bitcast_invalid.1 (i32) -> (i32)
5959
; CHECK-NEXT: unreachable
6060
; CHECK-NEXT: end_function
6161

@@ -64,7 +64,7 @@ entry:
6464
; CHECK-NEXT: unreachable
6565
; CHECK-NEXT: end_function
6666

67-
; CHECK-LABEL: .Lhas_ptr_arg_bitcast_invalid.4:
68-
; CHECK-NEXT: .functype .Lhas_ptr_arg_bitcast_invalid.4 (i32) -> (i32)
67+
; CHECK-LABEL: .Lhas_ptr_arg_bitcast_invalid.2:
68+
; CHECK-NEXT: .functype .Lhas_ptr_arg_bitcast_invalid.2 (i32) -> (i32)
6969
; CHECK-NEXT: unreachable
7070
; CHECK-NEXT: end_function

0 commit comments

Comments
 (0)