@@ -335,112 +335,106 @@ static void removeFunctionBitcasts(Module &M) {
335335 CallInst *pInstCall = dyn_cast<CallInst>(I);
336336 if (!pInstCall || pInstCall->getCalledFunction ())
337337 continue ;
338- if (auto constExpr = dyn_cast<llvm::ConstantExpr>(
339- IGCLLVM::getCalledValue (pInstCall))) {
340- if (auto funcTobeChanged =
341- dyn_cast<llvm::Function>(constExpr->stripPointerCasts ())) {
342- if (funcTobeChanged->isDeclaration ())
343- continue ;
344- // Map between values (functions) in source of bitcast
345- // to their counterpart values in destination
346- llvm::ValueToValueMapTy operandMap;
347- Function *pDstFunc = nullptr ;
348- auto BCFMI = bitcastFunctionMap.find (funcTobeChanged);
349- bool notExists = BCFMI == bitcastFunctionMap.end ();
350- if (!notExists) {
351- auto funcVec = bitcastFunctionMap[funcTobeChanged];
352- notExists = true ;
353- for (Function *F : funcVec) {
354- if (pInstCall->getFunctionType () == F->getFunctionType ()) {
355- notExists = false ;
356- pDstFunc = F;
357- break ;
358- }
359- }
338+ auto *funcToBeChanged = dyn_cast<Function>(
339+ IGCLLVM::getCalledValue (pInstCall)->stripPointerCasts ());
340+ if (!funcToBeChanged || funcToBeChanged->isDeclaration ())
341+ continue ;
342+ // Map between values (functions) in source of bitcast
343+ // to their counterpart values in destination
344+ llvm::ValueToValueMapTy operandMap;
345+ Function *pDstFunc = nullptr ;
346+ auto BCFMI = bitcastFunctionMap.find (funcToBeChanged);
347+ bool notExists = BCFMI == bitcastFunctionMap.end ();
348+ if (!notExists) {
349+ auto funcVec = bitcastFunctionMap[funcToBeChanged];
350+ notExists = true ;
351+ for (Function *F : funcVec) {
352+ if (pInstCall->getFunctionType () == F->getFunctionType ()) {
353+ notExists = false ;
354+ pDstFunc = F;
355+ break ;
360356 }
357+ }
358+ }
361359
362- if (notExists) {
363- pDstFunc = Function::Create (pInstCall->getFunctionType (),
364- funcTobeChanged->getLinkage (),
365- funcTobeChanged->getName (), &M);
366- if (pDstFunc->arg_size () != funcTobeChanged->arg_size ())
367- continue ;
368- // Need to copy the attributes over too.
369- auto FuncAttrs = funcTobeChanged->getAttributes ();
370- pDstFunc->setAttributes (FuncAttrs);
371-
372- // Go through and convert function arguments over, remembering the
373- // mapping.
374- Function::arg_iterator itSrcFunc = funcTobeChanged->arg_begin ();
375- Function::arg_iterator eSrcFunc = funcTobeChanged->arg_end ();
376- llvm::Function::arg_iterator itDest = pDstFunc->arg_begin ();
377-
378- for (; itSrcFunc != eSrcFunc; ++itSrcFunc, ++itDest) {
379- itDest->setName (itSrcFunc->getName ());
380- operandMap[&(*itSrcFunc)] = &(*itDest);
381- }
382-
383- // Clone the body of the function into the dest function.
384- SmallVector<ReturnInst *, 8 > Returns;
385- IGCLLVM::CloneFunctionInto (pDstFunc, funcTobeChanged, operandMap,
386- IGCLLVM::CloneFunctionChangeType::LocalChangesOnly, Returns, " " );
387-
388- // Update returns to match a new function ret type.
389- Instruction::CastOps castOp = Instruction::BitCast;
390- Type *oldRetTy =
391- funcTobeChanged->getFunctionType ()->getReturnType ();
392- Type *newRetTy = pInstCall->getFunctionType ()->getReturnType ();
393- if (oldRetTy == newRetTy) {
394- // Do nothing.
395- } else if (oldRetTy->isIntOrIntVectorTy () &&
396- newRetTy->isIntOrIntVectorTy ()) {
397- unsigned oldRetTypeSize = oldRetTy->getScalarSizeInBits ();
398- unsigned newRetTypeSize = newRetTy->getScalarSizeInBits ();
399- if (oldRetTypeSize > newRetTypeSize)
400- castOp = Instruction::Trunc;
401- else {
402- auto attrSet =
403- IGCLLVM::getRetAttrs (funcTobeChanged->getAttributes ());
404- if (attrSet.hasAttribute (Attribute::ZExt))
405- castOp = Instruction::ZExt;
406- else if (attrSet.hasAttribute (Attribute::SExt))
407- castOp = Instruction::SExt;
408- else
409- llvm_unreachable (" Expected ext attribute on return type." );
410- }
411- } else
412- vc::diagnose (pInstCall->getContext (), " GenXImportOCLBiF" ,
413- " Unhandled function pointer cast." , pInstCall);
414-
415- if (oldRetTy != newRetTy)
416- llvm::for_each (Returns, [castOp, newRetTy](ReturnInst *RI) {
417- auto *CI = CastInst::Create (castOp, RI->getReturnValue (),
418- newRetTy, " .rvc" , RI);
419- RI->setOperand (0 , CI);
420- });
421-
422- pDstFunc->setCallingConv (funcTobeChanged->getCallingConv ());
423- bitcastFunctionMap[funcTobeChanged].push_back (pDstFunc);
424- }
360+ if (notExists) {
361+ pDstFunc = Function::Create (pInstCall->getFunctionType (),
362+ funcToBeChanged->getLinkage (),
363+ funcToBeChanged->getName (), &M);
364+ if (pDstFunc->arg_size () != funcToBeChanged->arg_size ())
365+ continue ;
366+ // Need to copy the attributes over too.
367+ auto FuncAttrs = funcToBeChanged->getAttributes ();
368+ pDstFunc->setAttributes (FuncAttrs);
369+
370+ // Go through and convert function arguments over, remembering the
371+ // mapping.
372+ Function::arg_iterator itSrcFunc = funcToBeChanged->arg_begin ();
373+ Function::arg_iterator eSrcFunc = funcToBeChanged->arg_end ();
374+ llvm::Function::arg_iterator itDest = pDstFunc->arg_begin ();
375+
376+ for (; itSrcFunc != eSrcFunc; ++itSrcFunc, ++itDest) {
377+ itDest->setName (itSrcFunc->getName ());
378+ operandMap[&(*itSrcFunc)] = &(*itDest);
379+ }
425380
426- std::vector<Value *> Args;
427- for (unsigned I = 0 , E = IGCLLVM::getNumArgOperands (pInstCall); I != E;
428- ++I) {
429- Args.push_back (pInstCall->getArgOperand (I));
381+ // Clone the body of the function into the dest function.
382+ SmallVector<ReturnInst *, 8 > Returns;
383+ IGCLLVM::CloneFunctionInto (
384+ pDstFunc, funcToBeChanged, operandMap,
385+ IGCLLVM::CloneFunctionChangeType::LocalChangesOnly, Returns, " " );
386+
387+ // Update returns to match a new function ret type.
388+ Instruction::CastOps castOp = Instruction::BitCast;
389+ Type *oldRetTy = funcToBeChanged->getFunctionType ()->getReturnType ();
390+ Type *newRetTy = pInstCall->getFunctionType ()->getReturnType ();
391+ if (oldRetTy == newRetTy) {
392+ // Do nothing.
393+ } else if (oldRetTy->isIntOrIntVectorTy () &&
394+ newRetTy->isIntOrIntVectorTy ()) {
395+ unsigned oldRetTypeSize = oldRetTy->getScalarSizeInBits ();
396+ unsigned newRetTypeSize = newRetTy->getScalarSizeInBits ();
397+ if (oldRetTypeSize > newRetTypeSize)
398+ castOp = Instruction::Trunc;
399+ else {
400+ auto attrSet =
401+ IGCLLVM::getRetAttrs (funcToBeChanged->getAttributes ());
402+ if (attrSet.hasAttribute (Attribute::ZExt))
403+ castOp = Instruction::ZExt;
404+ else if (attrSet.hasAttribute (Attribute::SExt))
405+ castOp = Instruction::SExt;
406+ else
407+ llvm_unreachable (" Expected ext attribute on return type." );
430408 }
431- auto newCI = CallInst::Create (pDstFunc, Args, " " , pInstCall);
432- newCI->takeName (pInstCall);
433- newCI->setCallingConv (pInstCall->getCallingConv ());
434- pInstCall->replaceAllUsesWith (newCI);
435- pInstCall->dropAllReferences ();
436- if (constExpr->use_empty ())
437- constExpr->dropAllReferences ();
438- if (funcTobeChanged->use_empty ())
439- funcTobeChanged->eraseFromParent ();
440-
441- list_delete.push_back (pInstCall);
442- }
409+ } else
410+ vc::diagnose (pInstCall->getContext (), " GenXImportOCLBiF" ,
411+ " Unhandled function pointer cast." , pInstCall);
412+
413+ if (oldRetTy != newRetTy)
414+ llvm::for_each (Returns, [castOp, newRetTy](ReturnInst *RI) {
415+ auto *CI = CastInst::Create (castOp, RI->getReturnValue (),
416+ newRetTy, " .rvc" , RI);
417+ RI->setOperand (0 , CI);
418+ });
419+
420+ pDstFunc->setCallingConv (funcToBeChanged->getCallingConv ());
421+ bitcastFunctionMap[funcToBeChanged].push_back (pDstFunc);
422+ }
423+
424+ std::vector<Value *> Args;
425+ for (unsigned I = 0 , E = IGCLLVM::getNumArgOperands (pInstCall); I != E;
426+ ++I) {
427+ Args.push_back (pInstCall->getArgOperand (I));
443428 }
429+ auto newCI = CallInst::Create (pDstFunc, Args, " " , pInstCall);
430+ newCI->takeName (pInstCall);
431+ newCI->setCallingConv (pInstCall->getCallingConv ());
432+ pInstCall->replaceAllUsesWith (newCI);
433+ pInstCall->dropAllReferences ();
434+ if (funcToBeChanged->use_empty ())
435+ funcToBeChanged->eraseFromParent ();
436+
437+ list_delete.push_back (pInstCall);
444438 }
445439 }
446440 }
0 commit comments