@@ -1519,6 +1519,26 @@ ModuleImport::convertCallOperands(llvm::CallBase *callInst,
15191519 return operands;
15201520}
15211521
1522+ LLVMFunctionType ModuleImport::convertFunctionType (llvm::CallBase *callInst) {
1523+ llvm::Value *calledOperand = callInst->getCalledOperand ();
1524+ Type converted = [&] {
1525+ if (auto callee = dyn_cast<llvm::Function>(calledOperand))
1526+ return convertType (callee->getFunctionType ());
1527+ return convertType (callInst->getFunctionType ());
1528+ }();
1529+
1530+ if (auto funcTy = dyn_cast_or_null<LLVMFunctionType>(converted))
1531+ return funcTy;
1532+ return {};
1533+ }
1534+
1535+ FlatSymbolRefAttr ModuleImport::convertCalleeName (llvm::CallBase *callInst) {
1536+ llvm::Value *calledOperand = callInst->getCalledOperand ();
1537+ if (auto callee = dyn_cast<llvm::Function>(calledOperand))
1538+ return SymbolRefAttr::get (context, callee->getName ());
1539+ return {};
1540+ }
1541+
15221542LogicalResult ModuleImport::convertIntrinsic (llvm::CallInst *inst) {
15231543 if (succeeded (iface.convertIntrinsic (builder, inst, *this )))
15241544 return success ();
@@ -1623,25 +1643,12 @@ LogicalResult ModuleImport::convertInstruction(llvm::Instruction *inst) {
16231643 else
16241644 mapNoResultOp (inst, callOp);
16251645 } else {
1626- auto funcTy = dyn_cast<LLVMFunctionType>([&]() -> Type {
1627- // Retrieve the real function type. For direct calls, use the callee's
1628- // function type, as it may differ from the operand type in the case of
1629- // variadic functions. For indirect calls, use the call function type.
1630- if (auto callee = dyn_cast<llvm::Function>(calledOperand))
1631- return convertType (callee->getFunctionType ());
1632- return convertType (callInst->getFunctionType ());
1633- }());
1634-
1646+ auto funcTy = convertFunctionType (callInst);
16351647 if (!funcTy)
16361648 return failure ();
16371649
1638- auto callOp = [&]() -> CallOp {
1639- if (auto callee = dyn_cast<llvm::Function>(calledOperand)) {
1640- auto name = SymbolRefAttr::get (context, callee->getName ());
1641- return builder.create <CallOp>(loc, funcTy, name, *operands);
1642- }
1643- return builder.create <CallOp>(loc, funcTy, *operands);
1644- }();
1650+ FlatSymbolRefAttr calleeName = convertCalleeName (callInst);
1651+ auto callOp = builder.create <CallOp>(loc, funcTy, calleeName, *operands);
16451652
16461653 // Handle function attributes.
16471654 callOp.setCConv (convertCConvFromLLVM (callInst->getCallingConv ()));
@@ -1726,26 +1733,19 @@ LogicalResult ModuleImport::convertInstruction(llvm::Instruction *inst) {
17261733 unwindArgs)))
17271734 return failure ();
17281735
1729- auto funcTy =
1730- dyn_cast<LLVMFunctionType>(convertType (invokeInst->getFunctionType ()));
1736+ auto funcTy = convertFunctionType (invokeInst);
17311737 if (!funcTy)
17321738 return failure ();
17331739
1740+ FlatSymbolRefAttr calleeName = convertCalleeName (invokeInst);
1741+
17341742 // Create the invoke operation. Normal destination block arguments will be
17351743 // added later on to handle the case in which the operation result is
17361744 // included in this list.
1737- InvokeOp invokeOp;
1738- if (llvm::Function *callee = invokeInst->getCalledFunction ()) {
1739- invokeOp = builder.create <InvokeOp>(
1740- loc, funcTy,
1741- SymbolRefAttr::get (builder.getContext (), callee->getName ()),
1742- *operands, directNormalDest, ValueRange (),
1743- lookupBlock (invokeInst->getUnwindDest ()), unwindArgs);
1744- } else {
1745- invokeOp = builder.create <InvokeOp>(
1746- loc, funcTy, /* callee=*/ nullptr , *operands, directNormalDest,
1747- ValueRange (), lookupBlock (invokeInst->getUnwindDest ()), unwindArgs);
1748- }
1745+ auto invokeOp = builder.create <InvokeOp>(
1746+ loc, funcTy, calleeName, *operands, directNormalDest, ValueRange (),
1747+ lookupBlock (invokeInst->getUnwindDest ()), unwindArgs);
1748+
17491749 invokeOp.setCConv (convertCConvFromLLVM (invokeInst->getCallingConv ()));
17501750 if (!invokeInst->getType ()->isVoidTy ())
17511751 mapValue (inst, invokeOp.getResults ().front ());
0 commit comments