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
20using namespace llvm;
21
22#define DEBUG_TYPE "amdgpu-fix-function-bitcasts"
23
24namespace {
25class AMDGPUFixFunctionBitcasts final
26    : public ModulePass,
27      public InstVisitor<AMDGPUFixFunctionBitcasts> {
28
29  bool runOnModule(Module &M) override;
30
31  bool Modified;
32
33public:
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;
45  AMDGPUFixFunctionBitcasts() : ModulePass(ID) {}
46};
47} // End anonymous namespace
48
49char AMDGPUFixFunctionBitcasts::ID = 0;
50char &llvm::AMDGPUFixFunctionBitcastsID = AMDGPUFixFunctionBitcasts::ID;
51INITIALIZE_PASS(AMDGPUFixFunctionBitcasts, DEBUG_TYPE,
52                "Fix function bitcasts for AMDGPU", false, false)
53
54ModulePass *llvm::createAMDGPUFixFunctionBitcastsPass() {
55  return new AMDGPUFixFunctionBitcasts();
56}
57
58bool AMDGPUFixFunctionBitcasts::runOnModule(Module &M) {
59  Modified = false;
60  visit(M);
61  return Modified;
62}
63