1 //===-- AMDGPUFixFunctionBitcasts.cpp - Fix function bitcasts -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Promote indirect (bitcast) calls to direct calls when they are statically
11 /// known to be direct. Required when InstCombine is not run (e.g. at OptNone)
12 /// because AMDGPU does not support indirect calls.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "AMDGPU.h"
17 #include "llvm/IR/InstVisitor.h"
18 #include "llvm/Transforms/Utils/CallPromotionUtils.h"
19 
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "amdgpu-fix-function-bitcasts"
23 
24 namespace {
25 class AMDGPUFixFunctionBitcasts final
26     : public ModulePass,
27       public InstVisitor<AMDGPUFixFunctionBitcasts> {
28 
29   bool runOnModule(Module &M) override;
30 
31   bool Modified;
32 
33 public:
visitCallSite(CallSite CS)34   void visitCallSite(CallSite CS) {
35     if (CS.getCalledFunction())
36       return;
37     auto Callee = dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts());
38     if (Callee && isLegalToPromote(CS, Callee)) {
39       promoteCall(CS, Callee);
40       Modified = true;
41     }
42   }
43 
44   static char ID;
AMDGPUFixFunctionBitcasts()45   AMDGPUFixFunctionBitcasts() : ModulePass(ID) {}
46 };
47 } // End anonymous namespace
48 
49 char AMDGPUFixFunctionBitcasts::ID = 0;
50 char &llvm::AMDGPUFixFunctionBitcastsID = AMDGPUFixFunctionBitcasts::ID;
51 INITIALIZE_PASS(AMDGPUFixFunctionBitcasts, DEBUG_TYPE,
52                 "Fix function bitcasts for AMDGPU", false, false)
53 
createAMDGPUFixFunctionBitcastsPass()54 ModulePass *llvm::createAMDGPUFixFunctionBitcastsPass() {
55   return new AMDGPUFixFunctionBitcasts();
56 }
57 
runOnModule(Module & M)58 bool AMDGPUFixFunctionBitcasts::runOnModule(Module &M) {
59   Modified = false;
60   visit(M);
61   return Modified;
62 }
63