1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2018-2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 #ifndef IGCLLVM_IR_INSTRUCTIONS_H
10 #define IGCLLVM_IR_INSTRUCTIONS_H
11 
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/User.h"
15 #if LLVM_VERSION_MAJOR <= 7
16 #include "llvm/Support/Casting.h"
17 #endif
18 
19 #include "Probe/Assertion.h"
20 
21 namespace IGCLLVM
22 {
23 
getCalledValue(llvm::CallInst & CI)24     inline llvm::Value* getCalledValue(llvm::CallInst& CI)
25     {
26 #if LLVM_VERSION_MAJOR <= 10
27         return CI.getCalledValue();
28 #else
29         return CI.getCalledOperand();
30 #endif
31     }
32 
getCalledValue(llvm::CallInst * CI)33     inline llvm::Value* getCalledValue(llvm::CallInst* CI)
34     {
35 #if LLVM_VERSION_MAJOR <= 10
36         return CI->getCalledValue();
37 #else
38         return CI->getCalledOperand();
39 #endif
40     }
41 
getCalledValue(const llvm::CallInst * CI)42     inline const llvm::Value* getCalledValue(const llvm::CallInst* CI)
43     {
44 #if LLVM_VERSION_MAJOR <= 10
45         return CI->getCalledValue();
46 #else
47         return CI->getCalledOperand();
48 #endif
49     }
50 
isIndirectCall(const llvm::CallInst & CI)51     inline bool isIndirectCall(const llvm::CallInst& CI)
52     {
53 #if LLVM_VERSION_MAJOR == 7
54         const llvm::Value *V = CI.getCalledValue();
55         if (llvm::isa<llvm::Function>(V) || llvm::isa<llvm::Constant>(V))
56             return false;
57         if (CI.isInlineAsm())
58             return false;
59         return true;
60 #else
61         return CI.isIndirectCall();
62 #endif
63     }
64 
arg_size(const llvm::CallInst & CI)65     inline unsigned arg_size(const llvm::CallInst& CI)
66     {
67 #if LLVM_VERSION_MAJOR < 8
68         return (unsigned)(CI.arg_end() - CI.arg_begin());
69 #else
70         return (unsigned)CI.arg_size();
71 #endif
72     }
73 
args(llvm::CallInst & CI)74     inline llvm::iterator_range<llvm::User::op_iterator> args(llvm::CallInst& CI)
75     {
76 #if LLVM_VERSION_MAJOR < 8
77         return CI.arg_operands();
78 #else
79         return CI.args();
80 #endif
81     }
82 
getArgOperandNo(llvm::CallInst & CI,const llvm::Use * U)83     inline unsigned getArgOperandNo(llvm::CallInst &CI, const llvm::Use *U) {
84 #if LLVM_VERSION_MAJOR < 10
85       IGC_ASSERT_MESSAGE(CI.isArgOperand(U), "Arg operand # out of range!");
86       return (unsigned)(U - CI.arg_begin());
87 #else
88       return CI.getArgOperandNo(U);
89 #endif
90     }
91 
getShuffleMaskForBitcode(llvm::ShuffleVectorInst * SVI)92     inline llvm::Constant* getShuffleMaskForBitcode(llvm::ShuffleVectorInst* SVI)
93     {
94 #if LLVM_VERSION_MAJOR < 11
95         return SVI->getMask();
96 #else
97         return SVI->getShuffleMaskForBitcode();
98 #endif
99     }
100 }
101 
102 #endif
103