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/Pass.h"
19#include "llvm/Transforms/Utils/CallPromotionUtils.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "amdgpu-fix-function-bitcasts"
24
25namespace {
26class AMDGPUFixFunctionBitcasts final
27    : public ModulePass,
28      public InstVisitor<AMDGPUFixFunctionBitcasts> {
29
30  bool runOnModule(Module &M) override;
31
32  bool Modified;
33
34public:
35  void visitCallBase(CallBase &CB) {
36    if (CB.getCalledFunction())
37      return;
38    auto *Callee =
39        dyn_cast<Function>(CB.getCalledOperand()->stripPointerCasts());
40    if (Callee && isLegalToPromote(CB, Callee)) {
41      promoteCall(CB, Callee);
42      Modified = true;
43    }
44  }
45
46  static char ID;
47  AMDGPUFixFunctionBitcasts() : ModulePass(ID) {}
48};
49} // End anonymous namespace
50
51char AMDGPUFixFunctionBitcasts::ID = 0;
52char &llvm::AMDGPUFixFunctionBitcastsID = AMDGPUFixFunctionBitcasts::ID;
53INITIALIZE_PASS(AMDGPUFixFunctionBitcasts, DEBUG_TYPE,
54                "Fix function bitcasts for AMDGPU", false, false)
55
56ModulePass *llvm::createAMDGPUFixFunctionBitcastsPass() {
57  return new AMDGPUFixFunctionBitcasts();
58}
59
60bool AMDGPUFixFunctionBitcasts::runOnModule(Module &M) {
61  Modified = false;
62  visit(M);
63  return Modified;
64}
65