clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CallPromotionUtils.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "llvm/Transforms/Utils/CallPromotionUtils.h" |
15 | #include "llvm/Analysis/Loads.h" |
16 | #include "llvm/Analysis/TypeMetadataUtils.h" |
17 | #include "llvm/IR/IRBuilder.h" |
18 | #include "llvm/IR/Instructions.h" |
19 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
20 | |
21 | using namespace llvm; |
22 | |
23 | #define DEBUG_TYPE "call-promotion-utils" |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | static void fixupPHINodeForNormalDest(InvokeInst *Invoke, BasicBlock *OrigBlock, |
51 | BasicBlock *MergeBlock) { |
52 | for (PHINode &Phi : Invoke->getNormalDest()->phis()) { |
53 | int Idx = Phi.getBasicBlockIndex(OrigBlock); |
54 | if (Idx == -1) |
55 | continue; |
56 | Phi.setIncomingBlock(Idx, MergeBlock); |
57 | } |
58 | } |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | static void fixupPHINodeForUnwindDest(InvokeInst *Invoke, BasicBlock *OrigBlock, |
82 | BasicBlock *ThenBlock, |
83 | BasicBlock *ElseBlock) { |
84 | for (PHINode &Phi : Invoke->getUnwindDest()->phis()) { |
85 | int Idx = Phi.getBasicBlockIndex(OrigBlock); |
86 | if (Idx == -1) |
87 | continue; |
88 | auto *V = Phi.getIncomingValue(Idx); |
89 | Phi.setIncomingBlock(Idx, ThenBlock); |
90 | Phi.addIncoming(V, ElseBlock); |
91 | } |
92 | } |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | static void createRetPHINode(Instruction *OrigInst, Instruction *NewInst, |
108 | BasicBlock *MergeBlock, IRBuilder<> &Builder) { |
109 | |
110 | if (OrigInst->getType()->isVoidTy() || OrigInst->use_empty()) |
111 | return; |
112 | |
113 | Builder.SetInsertPoint(&MergeBlock->front()); |
114 | PHINode *Phi = Builder.CreatePHI(OrigInst->getType(), 0); |
115 | SmallVector<User *, 16> UsersToUpdate(OrigInst->users()); |
116 | for (User *U : UsersToUpdate) |
117 | U->replaceUsesOfWith(OrigInst, Phi); |
118 | Phi->addIncoming(OrigInst, OrigInst->getParent()); |
119 | Phi->addIncoming(NewInst, NewInst->getParent()); |
120 | } |
121 | |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) { |
163 | |
164 | |
165 | |
166 | SmallVector<User *, 16> UsersToUpdate(CB.users()); |
167 | |
168 | |
169 | |
170 | Instruction *InsertBefore = nullptr; |
171 | if (auto *Invoke = dyn_cast<InvokeInst>(&CB)) |
172 | InsertBefore = |
173 | &SplitEdge(Invoke->getParent(), Invoke->getNormalDest())->front(); |
174 | else |
175 | InsertBefore = &*std::next(CB.getIterator()); |
176 | |
177 | |
178 | auto *Cast = CastInst::CreateBitOrPointerCast(&CB, RetTy, "", InsertBefore); |
179 | if (RetBitCast) |
180 | *RetBitCast = Cast; |
181 | |
182 | |
183 | for (User *U : UsersToUpdate) |
184 | U->replaceUsesOfWith(&CB, Cast); |
185 | } |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | |
228 | |
229 | |
230 | |
231 | |
232 | |
233 | |
234 | |
235 | |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | |
242 | |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | |
250 | |
251 | |
252 | |
253 | |
254 | |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | |
269 | |
270 | |
271 | |
272 | |
273 | |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | static CallBase &versionCallSite(CallBase &CB, Value *Callee, |
283 | MDNode *BranchWeights) { |
284 | |
285 | IRBuilder<> Builder(&CB); |
286 | CallBase *OrigInst = &CB; |
287 | BasicBlock *OrigBlock = OrigInst->getParent(); |
288 | |
289 | |
290 | |
291 | if (CB.getCalledOperand()->getType() != Callee->getType()) |
| 2 | | Assuming the condition is false | |
|
| |
292 | Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType()); |
293 | auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee); |
294 | |
295 | if (OrigInst->isMustTailCall()) { |
| 4 | | Assuming the condition is true | |
|
| |
296 | |
297 | |
298 | Instruction *ThenTerm = |
299 | SplitBlockAndInsertIfThen(Cond, &CB, false, BranchWeights); |
300 | BasicBlock *ThenBlock = ThenTerm->getParent(); |
301 | ThenBlock->setName("if.true.direct_targ"); |
302 | CallBase *NewInst = cast<CallBase>(OrigInst->clone()); |
| 6 | | The object is a 'CallBase' | |
|
303 | NewInst->insertBefore(ThenTerm); |
304 | |
305 | |
306 | Value *NewRetVal = NewInst; |
307 | auto Next = OrigInst->getNextNode(); |
308 | if (auto *BitCast = dyn_cast_or_null<BitCastInst>(Next)) { |
| 7 | | Assuming null pointer is passed into cast | |
|
| |
309 | assert(BitCast->getOperand(0) == OrigInst && |
310 | "bitcast following musttail call must use the call"); |
311 | auto NewBitCast = BitCast->clone(); |
312 | NewBitCast->replaceUsesOfWith(OrigInst, NewInst); |
313 | NewBitCast->insertBefore(ThenTerm); |
314 | NewRetVal = NewBitCast; |
315 | Next = BitCast->getNextNode(); |
316 | } |
317 | |
318 | |
319 | ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next); |
| 9 | | Assuming null pointer is passed into cast | |
|
| 10 | | 'Ret' initialized to a null pointer value | |
|
320 | assert(Ret && "musttail call must precede a ret with an optional bitcast"); |
321 | auto NewRet = Ret->clone(); |
| 11 | | Called C++ object pointer is null |
|
322 | if (Ret->getReturnValue()) |
323 | NewRet->replaceUsesOfWith(Ret->getReturnValue(), NewRetVal); |
324 | NewRet->insertBefore(ThenTerm); |
325 | |
326 | |
327 | |
328 | ThenTerm->eraseFromParent(); |
329 | |
330 | return *NewInst; |
331 | } |
332 | |
333 | |
334 | |
335 | |
336 | Instruction *ThenTerm = nullptr; |
337 | Instruction *ElseTerm = nullptr; |
338 | SplitBlockAndInsertIfThenElse(Cond, &CB, &ThenTerm, &ElseTerm, BranchWeights); |
339 | BasicBlock *ThenBlock = ThenTerm->getParent(); |
340 | BasicBlock *ElseBlock = ElseTerm->getParent(); |
341 | BasicBlock *MergeBlock = OrigInst->getParent(); |
342 | |
343 | ThenBlock->setName("if.true.direct_targ"); |
344 | ElseBlock->setName("if.false.orig_indirect"); |
345 | MergeBlock->setName("if.end.icp"); |
346 | |
347 | CallBase *NewInst = cast<CallBase>(OrigInst->clone()); |
348 | OrigInst->moveBefore(ElseTerm); |
349 | NewInst->insertBefore(ThenTerm); |
350 | |
351 | |
352 | |
353 | |
354 | if (auto *OrigInvoke = dyn_cast<InvokeInst>(OrigInst)) { |
355 | auto *NewInvoke = cast<InvokeInst>(NewInst); |
356 | |
357 | |
358 | |
359 | ThenTerm->eraseFromParent(); |
360 | ElseTerm->eraseFromParent(); |
361 | |
362 | |
363 | Builder.SetInsertPoint(MergeBlock); |
364 | Builder.CreateBr(OrigInvoke->getNormalDest()); |
365 | |
366 | |
367 | fixupPHINodeForNormalDest(OrigInvoke, OrigBlock, MergeBlock); |
368 | fixupPHINodeForUnwindDest(OrigInvoke, MergeBlock, ThenBlock, ElseBlock); |
369 | |
370 | |
371 | |
372 | OrigInvoke->setNormalDest(MergeBlock); |
373 | NewInvoke->setNormalDest(MergeBlock); |
374 | } |
375 | |
376 | |
377 | createRetPHINode(OrigInst, NewInst, MergeBlock, Builder); |
378 | |
379 | return *NewInst; |
380 | } |
381 | |
382 | bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee, |
383 | const char **FailureReason) { |
384 | assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted"); |
385 | |
386 | auto &DL = Callee->getParent()->getDataLayout(); |
387 | |
388 | |
389 | |
390 | Type *CallRetTy = CB.getType(); |
391 | Type *FuncRetTy = Callee->getReturnType(); |
392 | if (CallRetTy != FuncRetTy) |
393 | if (!CastInst::isBitOrNoopPointerCastable(FuncRetTy, CallRetTy, DL)) { |
394 | if (FailureReason) |
395 | *FailureReason = "Return type mismatch"; |
396 | return false; |
397 | } |
398 | |
399 | |
400 | unsigned NumParams = Callee->getFunctionType()->getNumParams(); |
401 | |
402 | |
403 | unsigned NumArgs = CB.arg_size(); |
404 | |
405 | |
406 | |
407 | if (NumArgs != NumParams && !Callee->isVarArg()) { |
408 | if (FailureReason) |
409 | *FailureReason = "The number of arguments mismatch"; |
410 | return false; |
411 | } |
412 | |
413 | |
414 | |
415 | |
416 | unsigned I = 0; |
417 | for (; I < NumParams; ++I) { |
418 | Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(I); |
419 | Type *ActualTy = CB.getArgOperand(I)->getType(); |
420 | if (FormalTy == ActualTy) |
421 | continue; |
422 | if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) { |
423 | if (FailureReason) |
424 | *FailureReason = "Argument type mismatch"; |
425 | return false; |
426 | } |
427 | } |
428 | for (; I < NumArgs; I++) { |
429 | |
430 | assert(Callee->isVarArg()); |
431 | if (CB.paramHasAttr(I, Attribute::StructRet)) { |
432 | if (FailureReason) |
433 | *FailureReason = "SRet arg to vararg function"; |
434 | return false; |
435 | } |
436 | } |
437 | |
438 | return true; |
439 | } |
440 | |
441 | CallBase &llvm::promoteCall(CallBase &CB, Function *Callee, |
442 | CastInst **RetBitCast) { |
443 | assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted"); |
444 | |
445 | |
446 | |
447 | CB.setCalledOperand(Callee); |
448 | |
449 | |
450 | |
451 | |
452 | CB.setMetadata(LLVMContext::MD_prof, nullptr); |
453 | CB.setMetadata(LLVMContext::MD_callees, nullptr); |
454 | |
455 | |
456 | |
457 | if (CB.getFunctionType() == Callee->getFunctionType()) |
458 | return CB; |
459 | |
460 | |
461 | Type *CallSiteRetTy = CB.getType(); |
462 | Type *CalleeRetTy = Callee->getReturnType(); |
463 | |
464 | |
465 | CB.mutateFunctionType(Callee->getFunctionType()); |
466 | |
467 | |
468 | |
469 | |
470 | auto CalleeType = Callee->getFunctionType(); |
471 | auto CalleeParamNum = CalleeType->getNumParams(); |
472 | |
473 | LLVMContext &Ctx = Callee->getContext(); |
474 | const AttributeList &CallerPAL = CB.getAttributes(); |
475 | |
476 | SmallVector<AttributeSet, 4> NewArgAttrs; |
477 | bool AttributeChanged = false; |
478 | |
479 | for (unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) { |
480 | auto *Arg = CB.getArgOperand(ArgNo); |
481 | Type *FormalTy = CalleeType->getParamType(ArgNo); |
482 | Type *ActualTy = Arg->getType(); |
483 | if (FormalTy != ActualTy) { |
484 | auto *Cast = CastInst::CreateBitOrPointerCast(Arg, FormalTy, "", &CB); |
485 | CB.setArgOperand(ArgNo, Cast); |
486 | |
487 | |
488 | AttrBuilder ArgAttrs(CallerPAL.getParamAttributes(ArgNo)); |
489 | ArgAttrs.remove(AttributeFuncs::typeIncompatible(FormalTy)); |
490 | |
491 | |
492 | |
493 | if (ArgAttrs.getByValType()) |
494 | ArgAttrs.addByValAttr(Callee->getParamByValType(ArgNo)); |
495 | |
496 | NewArgAttrs.push_back(AttributeSet::get(Ctx, ArgAttrs)); |
497 | AttributeChanged = true; |
498 | } else |
499 | NewArgAttrs.push_back(CallerPAL.getParamAttributes(ArgNo)); |
500 | } |
501 | |
502 | |
503 | |
504 | |
505 | AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex); |
506 | if (!CallSiteRetTy->isVoidTy() && CallSiteRetTy != CalleeRetTy) { |
507 | createRetBitCast(CB, CallSiteRetTy, RetBitCast); |
508 | RAttrs.remove(AttributeFuncs::typeIncompatible(CalleeRetTy)); |
509 | AttributeChanged = true; |
510 | } |
511 | |
512 | |
513 | if (AttributeChanged) |
514 | CB.setAttributes(AttributeList::get(Ctx, CallerPAL.getFnAttributes(), |
515 | AttributeSet::get(Ctx, RAttrs), |
516 | NewArgAttrs)); |
517 | |
518 | return CB; |
519 | } |
520 | |
521 | CallBase &llvm::promoteCallWithIfThenElse(CallBase &CB, Function *Callee, |
522 | MDNode *BranchWeights) { |
523 | |
524 | |
525 | |
526 | |
527 | CallBase &NewInst = versionCallSite(CB, Callee, BranchWeights); |
| 1 | Calling 'versionCallSite' | |
|
528 | |
529 | |
530 | return promoteCall(NewInst, Callee); |
531 | } |
532 | |
533 | bool llvm::tryPromoteCall(CallBase &CB) { |
534 | assert(!CB.getCalledFunction()); |
535 | Module *M = CB.getCaller()->getParent(); |
536 | const DataLayout &DL = M->getDataLayout(); |
537 | Value *Callee = CB.getCalledOperand(); |
538 | |
539 | LoadInst *VTableEntryLoad = dyn_cast<LoadInst>(Callee); |
540 | if (!VTableEntryLoad) |
541 | return false; |
542 | Value *VTableEntryPtr = VTableEntryLoad->getPointerOperand(); |
543 | APInt VTableOffset(DL.getTypeSizeInBits(VTableEntryPtr->getType()), 0); |
544 | Value *VTableBasePtr = VTableEntryPtr->stripAndAccumulateConstantOffsets( |
545 | DL, VTableOffset, true); |
546 | LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr); |
547 | if (!VTablePtrLoad) |
548 | return false; |
549 | Value *Object = VTablePtrLoad->getPointerOperand(); |
550 | APInt ObjectOffset(DL.getTypeSizeInBits(Object->getType()), 0); |
551 | Value *ObjectBase = Object->stripAndAccumulateConstantOffsets( |
552 | DL, ObjectOffset, true); |
553 | if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0)) |
554 | |
555 | return false; |
556 | |
557 | |
558 | BasicBlock::iterator BBI(VTablePtrLoad); |
559 | Value *VTablePtr = FindAvailableLoadedValue( |
560 | VTablePtrLoad, VTablePtrLoad->getParent(), BBI, 0, nullptr, nullptr); |
561 | if (!VTablePtr) |
562 | return false; |
563 | APInt VTableOffsetGVBase(DL.getTypeSizeInBits(VTablePtr->getType()), 0); |
564 | Value *VTableGVBase = VTablePtr->stripAndAccumulateConstantOffsets( |
565 | DL, VTableOffsetGVBase, true); |
566 | GlobalVariable *GV = dyn_cast<GlobalVariable>(VTableGVBase); |
567 | if (!(GV && GV->isConstant() && GV->hasDefinitiveInitializer())) |
568 | |
569 | return false; |
570 | |
571 | Constant *VTableGVInitializer = GV->getInitializer(); |
572 | APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset; |
573 | if (!(VTableGVOffset.getActiveBits() <= 64)) |
574 | return false; |
575 | Constant *Ptr = getPointerAtOffset(VTableGVInitializer, |
576 | VTableGVOffset.getZExtValue(), |
577 | *M); |
578 | if (!Ptr) |
579 | return false; |
580 | Function *DirectCallee = dyn_cast<Function>(Ptr->stripPointerCasts()); |
581 | if (!DirectCallee) |
582 | return false; |
583 | |
584 | if (!isLegalToPromote(CB, DirectCallee)) |
585 | return false; |
586 | |
587 | |
588 | promoteCall(CB, DirectCallee); |
589 | return true; |
590 | } |
591 | |
592 | #undef DEBUG_TYPE |