1 //===- Local.cpp - Unit tests for Local -----------------------------------===//
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 #include "llvm/Transforms/Utils/Local.h"
10 #include "llvm/Analysis/DomTreeUpdater.h"
11 #include "llvm/Analysis/PostDominators.h"
12 #include "llvm/Analysis/TargetTransformInfo.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/IR/BasicBlock.h"
15 #include "llvm/IR/DIBuilder.h"
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "gtest/gtest.h"
23
24 using namespace llvm;
25
TEST(Local,RecursivelyDeleteDeadPHINodes)26 TEST(Local, RecursivelyDeleteDeadPHINodes) {
27 LLVMContext C;
28
29 IRBuilder<> builder(C);
30
31 // Make blocks
32 BasicBlock *bb0 = BasicBlock::Create(C);
33 BasicBlock *bb1 = BasicBlock::Create(C);
34
35 builder.SetInsertPoint(bb0);
36 PHINode *phi = builder.CreatePHI(Type::getInt32Ty(C), 2);
37 BranchInst *br0 = builder.CreateCondBr(builder.getTrue(), bb0, bb1);
38
39 builder.SetInsertPoint(bb1);
40 BranchInst *br1 = builder.CreateBr(bb0);
41
42 phi->addIncoming(phi, bb0);
43 phi->addIncoming(phi, bb1);
44
45 // The PHI will be removed
46 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
47
48 // Make sure the blocks only contain the branches
49 EXPECT_EQ(&bb0->front(), br0);
50 EXPECT_EQ(&bb1->front(), br1);
51
52 builder.SetInsertPoint(bb0);
53 phi = builder.CreatePHI(Type::getInt32Ty(C), 0);
54
55 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
56
57 builder.SetInsertPoint(bb0);
58 phi = builder.CreatePHI(Type::getInt32Ty(C), 0);
59 builder.CreateAdd(phi, phi);
60
61 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi));
62
63 bb0->dropAllReferences();
64 bb1->dropAllReferences();
65 delete bb0;
66 delete bb1;
67 }
68
TEST(Local,RemoveDuplicatePHINodes)69 TEST(Local, RemoveDuplicatePHINodes) {
70 LLVMContext C;
71 IRBuilder<> B(C);
72
73 std::unique_ptr<Function> F(
74 Function::Create(FunctionType::get(B.getVoidTy(), false),
75 GlobalValue::ExternalLinkage, "F"));
76 BasicBlock *Entry(BasicBlock::Create(C, "", F.get()));
77 BasicBlock *BB(BasicBlock::Create(C, "", F.get()));
78 BranchInst::Create(BB, Entry);
79
80 B.SetInsertPoint(BB);
81
82 AssertingVH<PHINode> P1 = B.CreatePHI(Type::getInt32Ty(C), 2);
83 P1->addIncoming(B.getInt32(42), Entry);
84
85 PHINode *P2 = B.CreatePHI(Type::getInt32Ty(C), 2);
86 P2->addIncoming(B.getInt32(42), Entry);
87
88 AssertingVH<PHINode> P3 = B.CreatePHI(Type::getInt32Ty(C), 2);
89 P3->addIncoming(B.getInt32(42), Entry);
90 P3->addIncoming(B.getInt32(23), BB);
91
92 PHINode *P4 = B.CreatePHI(Type::getInt32Ty(C), 2);
93 P4->addIncoming(B.getInt32(42), Entry);
94 P4->addIncoming(B.getInt32(23), BB);
95
96 P1->addIncoming(P3, BB);
97 P2->addIncoming(P4, BB);
98 BranchInst::Create(BB, BB);
99
100 // Verify that we can eliminate PHIs that become duplicates after chaning PHIs
101 // downstream.
102 EXPECT_TRUE(EliminateDuplicatePHINodes(BB));
103 EXPECT_EQ(3U, BB->size());
104 }
105
parseIR(LLVMContext & C,const char * IR)106 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
107 SMDiagnostic Err;
108 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
109 if (!Mod)
110 Err.print("UtilsTests", errs());
111 return Mod;
112 }
113
TEST(Local,ReplaceDbgDeclare)114 TEST(Local, ReplaceDbgDeclare) {
115 LLVMContext C;
116
117 // Original C source to get debug info for a local variable:
118 // void f() { int x; }
119 std::unique_ptr<Module> M = parseIR(C,
120 R"(
121 define void @f() !dbg !8 {
122 entry:
123 %x = alloca i32, align 4
124 call void @llvm.dbg.declare(metadata i32* %x, metadata !11, metadata !DIExpression()), !dbg !13
125 call void @llvm.dbg.declare(metadata i32* %x, metadata !11, metadata !DIExpression()), !dbg !13
126 ret void, !dbg !14
127 }
128 declare void @llvm.dbg.declare(metadata, metadata, metadata)
129 !llvm.dbg.cu = !{!0}
130 !llvm.module.flags = !{!3, !4}
131 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
132 !1 = !DIFile(filename: "t2.c", directory: "foo")
133 !2 = !{}
134 !3 = !{i32 2, !"Dwarf Version", i32 4}
135 !4 = !{i32 2, !"Debug Info Version", i32 3}
136 !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
137 !9 = !DISubroutineType(types: !10)
138 !10 = !{null}
139 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
140 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
141 !13 = !DILocation(line: 2, column: 7, scope: !8)
142 !14 = !DILocation(line: 3, column: 1, scope: !8)
143 )");
144 auto *GV = M->getNamedValue("f");
145 ASSERT_TRUE(GV);
146 auto *F = dyn_cast<Function>(GV);
147 ASSERT_TRUE(F);
148 Instruction *Inst = &F->front().front();
149 auto *AI = dyn_cast<AllocaInst>(Inst);
150 ASSERT_TRUE(AI);
151 Inst = Inst->getNextNode()->getNextNode();
152 ASSERT_TRUE(Inst);
153 auto *DII = dyn_cast<DbgDeclareInst>(Inst);
154 ASSERT_TRUE(DII);
155 Value *NewBase = Constant::getNullValue(Type::getInt32PtrTy(C));
156 DIBuilder DIB(*M);
157 replaceDbgDeclare(AI, NewBase, DIB, DIExpression::ApplyOffset, 0);
158
159 // There should be exactly two dbg.declares.
160 int Declares = 0;
161 for (const Instruction &I : F->front())
162 if (isa<DbgDeclareInst>(I))
163 Declares++;
164 EXPECT_EQ(2, Declares);
165 }
166
167 /// Build the dominator tree for the function and run the Test.
runWithDomTree(Module & M,StringRef FuncName,function_ref<void (Function & F,DominatorTree * DT)> Test)168 static void runWithDomTree(
169 Module &M, StringRef FuncName,
170 function_ref<void(Function &F, DominatorTree *DT)> Test) {
171 auto *F = M.getFunction(FuncName);
172 ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
173 // Compute the dominator tree for the function.
174 DominatorTree DT(*F);
175 Test(*F, &DT);
176 }
177
TEST(Local,MergeBasicBlockIntoOnlyPred)178 TEST(Local, MergeBasicBlockIntoOnlyPred) {
179 LLVMContext C;
180 std::unique_ptr<Module> M;
181 auto resetIR = [&]() {
182 M = parseIR(C,
183 R"(
184 define i32 @f(i8* %str) {
185 entry:
186 br label %bb2.i
187 bb2.i: ; preds = %bb4.i, %entry
188 br i1 false, label %bb4.i, label %base2flt.exit204
189 bb4.i: ; preds = %bb2.i
190 br i1 false, label %base2flt.exit204, label %bb2.i
191 bb10.i196.bb7.i197_crit_edge: ; No predecessors!
192 br label %bb7.i197
193 bb7.i197: ; preds = %bb10.i196.bb7.i197_crit_edge
194 %.reg2mem.0 = phi i32 [ %.reg2mem.0, %bb10.i196.bb7.i197_crit_edge ]
195 br i1 undef, label %base2flt.exit204, label %base2flt.exit204
196 base2flt.exit204: ; preds = %bb7.i197, %bb7.i197, %bb2.i, %bb4.i
197 ret i32 0
198 }
199 )");
200 };
201
202 auto resetIRReplaceEntry = [&]() {
203 M = parseIR(C,
204 R"(
205 define i32 @f() {
206 entry:
207 br label %bb2.i
208 bb2.i: ; preds = %entry
209 ret i32 0
210 }
211 )");
212 };
213
214 auto Test = [&](Function &F, DomTreeUpdater &DTU) {
215 for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
216 BasicBlock *BB = &*I++;
217 BasicBlock *SinglePred = BB->getSinglePredecessor();
218 if (!SinglePred || SinglePred == BB || BB->hasAddressTaken())
219 continue;
220 BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator());
221 if (Term && !Term->isConditional())
222 MergeBasicBlockIntoOnlyPred(BB, &DTU);
223 }
224 if (DTU.hasDomTree()) {
225 EXPECT_TRUE(DTU.getDomTree().verify());
226 }
227 if (DTU.hasPostDomTree()) {
228 EXPECT_TRUE(DTU.getPostDomTree().verify());
229 }
230 };
231
232 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
233 // both DT and PDT.
234 resetIR();
235 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
236 PostDominatorTree PDT = PostDominatorTree(F);
237 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager);
238 Test(F, DTU);
239 });
240
241 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
242 // DT.
243 resetIR();
244 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
245 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Eager);
246 Test(F, DTU);
247 });
248
249 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
250 // PDT.
251 resetIR();
252 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
253 PostDominatorTree PDT = PostDominatorTree(F);
254 DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Eager);
255 Test(F, DTU);
256 });
257
258 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with
259 // both DT and PDT.
260 resetIR();
261 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
262 PostDominatorTree PDT = PostDominatorTree(F);
263 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
264 Test(F, DTU);
265 });
266
267 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with
268 // PDT.
269 resetIR();
270 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
271 PostDominatorTree PDT = PostDominatorTree(F);
272 DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Lazy);
273 Test(F, DTU);
274 });
275
276 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with DT.
277 resetIR();
278 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
279 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
280 Test(F, DTU);
281 });
282
283 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
284 // both DT and PDT.
285 resetIRReplaceEntry();
286 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
287 PostDominatorTree PDT = PostDominatorTree(F);
288 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager);
289 Test(F, DTU);
290 });
291
292 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
293 // DT.
294 resetIRReplaceEntry();
295 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
296 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Eager);
297 Test(F, DTU);
298 });
299
300 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with
301 // PDT.
302 resetIRReplaceEntry();
303 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
304 PostDominatorTree PDT = PostDominatorTree(F);
305 DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Eager);
306 Test(F, DTU);
307 });
308
309 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with
310 // both DT and PDT.
311 resetIRReplaceEntry();
312 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
313 PostDominatorTree PDT = PostDominatorTree(F);
314 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
315 Test(F, DTU);
316 });
317
318 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with
319 // PDT.
320 resetIRReplaceEntry();
321 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
322 PostDominatorTree PDT = PostDominatorTree(F);
323 DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Lazy);
324 Test(F, DTU);
325 });
326
327 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with DT.
328 resetIRReplaceEntry();
329 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) {
330 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
331 Test(F, DTU);
332 });
333 }
334
TEST(Local,ConstantFoldTerminator)335 TEST(Local, ConstantFoldTerminator) {
336 LLVMContext C;
337
338 std::unique_ptr<Module> M = parseIR(C,
339 R"(
340 define void @br_same_dest() {
341 entry:
342 br i1 false, label %bb0, label %bb0
343 bb0:
344 ret void
345 }
346
347 define void @br_different_dest() {
348 entry:
349 br i1 true, label %bb0, label %bb1
350 bb0:
351 br label %exit
352 bb1:
353 br label %exit
354 exit:
355 ret void
356 }
357
358 define void @switch_2_different_dest() {
359 entry:
360 switch i32 0, label %default [ i32 0, label %bb0 ]
361 default:
362 ret void
363 bb0:
364 ret void
365 }
366 define void @switch_2_different_dest_default() {
367 entry:
368 switch i32 1, label %default [ i32 0, label %bb0 ]
369 default:
370 ret void
371 bb0:
372 ret void
373 }
374 define void @switch_3_different_dest() {
375 entry:
376 switch i32 0, label %default [ i32 0, label %bb0
377 i32 1, label %bb1 ]
378 default:
379 ret void
380 bb0:
381 ret void
382 bb1:
383 ret void
384 }
385
386 define void @switch_variable_2_default_dest(i32 %arg) {
387 entry:
388 switch i32 %arg, label %default [ i32 0, label %default ]
389 default:
390 ret void
391 }
392
393 define void @switch_constant_2_default_dest() {
394 entry:
395 switch i32 1, label %default [ i32 0, label %default ]
396 default:
397 ret void
398 }
399
400 define void @switch_constant_3_repeated_dest() {
401 entry:
402 switch i32 0, label %default [ i32 0, label %bb0
403 i32 1, label %bb0 ]
404 bb0:
405 ret void
406 default:
407 ret void
408 }
409
410 define void @indirectbr() {
411 entry:
412 indirectbr i8* blockaddress(@indirectbr, %bb0), [label %bb0, label %bb1]
413 bb0:
414 ret void
415 bb1:
416 ret void
417 }
418
419 define void @indirectbr_repeated() {
420 entry:
421 indirectbr i8* blockaddress(@indirectbr_repeated, %bb0), [label %bb0, label %bb0]
422 bb0:
423 ret void
424 }
425
426 define void @indirectbr_unreachable() {
427 entry:
428 indirectbr i8* blockaddress(@indirectbr_unreachable, %bb0), [label %bb1]
429 bb0:
430 ret void
431 bb1:
432 ret void
433 }
434 )");
435
436 auto CFAllTerminatorsEager = [&](Function &F, DominatorTree *DT) {
437 PostDominatorTree PDT = PostDominatorTree(F);
438 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager);
439 for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
440 BasicBlock *BB = &*I++;
441 ConstantFoldTerminator(BB, true, nullptr, &DTU);
442 }
443
444 EXPECT_TRUE(DTU.getDomTree().verify());
445 EXPECT_TRUE(DTU.getPostDomTree().verify());
446 };
447
448 auto CFAllTerminatorsLazy = [&](Function &F, DominatorTree *DT) {
449 PostDominatorTree PDT = PostDominatorTree(F);
450 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
451 for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
452 BasicBlock *BB = &*I++;
453 ConstantFoldTerminator(BB, true, nullptr, &DTU);
454 }
455
456 EXPECT_TRUE(DTU.getDomTree().verify());
457 EXPECT_TRUE(DTU.getPostDomTree().verify());
458 };
459
460 // Test ConstantFoldTerminator under Eager UpdateStrategy.
461 runWithDomTree(*M, "br_same_dest", CFAllTerminatorsEager);
462 runWithDomTree(*M, "br_different_dest", CFAllTerminatorsEager);
463 runWithDomTree(*M, "switch_2_different_dest", CFAllTerminatorsEager);
464 runWithDomTree(*M, "switch_2_different_dest_default", CFAllTerminatorsEager);
465 runWithDomTree(*M, "switch_3_different_dest", CFAllTerminatorsEager);
466 runWithDomTree(*M, "switch_variable_2_default_dest", CFAllTerminatorsEager);
467 runWithDomTree(*M, "switch_constant_2_default_dest", CFAllTerminatorsEager);
468 runWithDomTree(*M, "switch_constant_3_repeated_dest", CFAllTerminatorsEager);
469 runWithDomTree(*M, "indirectbr", CFAllTerminatorsEager);
470 runWithDomTree(*M, "indirectbr_repeated", CFAllTerminatorsEager);
471 runWithDomTree(*M, "indirectbr_unreachable", CFAllTerminatorsEager);
472
473 // Test ConstantFoldTerminator under Lazy UpdateStrategy.
474 runWithDomTree(*M, "br_same_dest", CFAllTerminatorsLazy);
475 runWithDomTree(*M, "br_different_dest", CFAllTerminatorsLazy);
476 runWithDomTree(*M, "switch_2_different_dest", CFAllTerminatorsLazy);
477 runWithDomTree(*M, "switch_2_different_dest_default", CFAllTerminatorsLazy);
478 runWithDomTree(*M, "switch_3_different_dest", CFAllTerminatorsLazy);
479 runWithDomTree(*M, "switch_variable_2_default_dest", CFAllTerminatorsLazy);
480 runWithDomTree(*M, "switch_constant_2_default_dest", CFAllTerminatorsLazy);
481 runWithDomTree(*M, "switch_constant_3_repeated_dest", CFAllTerminatorsLazy);
482 runWithDomTree(*M, "indirectbr", CFAllTerminatorsLazy);
483 runWithDomTree(*M, "indirectbr_repeated", CFAllTerminatorsLazy);
484 runWithDomTree(*M, "indirectbr_unreachable", CFAllTerminatorsLazy);
485 }
486
487 struct SalvageDebugInfoTest : ::testing::Test {
488 LLVMContext C;
489 std::unique_ptr<Module> M;
490 Function *F = nullptr;
491
SetUpSalvageDebugInfoTest492 void SetUp() override {
493 M = parseIR(C,
494 R"(
495 define void @f() !dbg !8 {
496 entry:
497 %x = add i32 0, 1
498 %y = add i32 %x, 2
499 call void @llvm.dbg.value(metadata i32 %x, metadata !11, metadata !DIExpression()), !dbg !13
500 call void @llvm.dbg.value(metadata i32 %y, metadata !11, metadata !DIExpression()), !dbg !13
501 ret void, !dbg !14
502 }
503 declare void @llvm.dbg.value(metadata, metadata, metadata)
504 !llvm.dbg.cu = !{!0}
505 !llvm.module.flags = !{!3, !4}
506 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
507 !1 = !DIFile(filename: "t2.c", directory: "foo")
508 !2 = !{}
509 !3 = !{i32 2, !"Dwarf Version", i32 4}
510 !4 = !{i32 2, !"Debug Info Version", i32 3}
511 !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2)
512 !9 = !DISubroutineType(types: !10)
513 !10 = !{null}
514 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12)
515 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
516 !13 = !DILocation(line: 2, column: 7, scope: !8)
517 !14 = !DILocation(line: 3, column: 1, scope: !8)
518 )");
519
520 auto *GV = M->getNamedValue("f");
521 ASSERT_TRUE(GV);
522 F = dyn_cast<Function>(GV);
523 ASSERT_TRUE(F);
524 }
525
doesDebugValueDescribeXSalvageDebugInfoTest526 bool doesDebugValueDescribeX(const DbgValueInst &DI) {
527 if (DI.getNumVariableLocationOps() != 1u)
528 return false;
529 const auto &CI = *cast<ConstantInt>(DI.getValue(0));
530 if (CI.isZero())
531 return DI.getExpression()->getElements().equals(
532 {dwarf::DW_OP_plus_uconst, 1, dwarf::DW_OP_stack_value});
533 else if (CI.isOneValue())
534 return DI.getExpression()->getElements().empty();
535 return false;
536 }
537
doesDebugValueDescribeYSalvageDebugInfoTest538 bool doesDebugValueDescribeY(const DbgValueInst &DI) {
539 if (DI.getNumVariableLocationOps() != 1u)
540 return false;
541 const auto &CI = *cast<ConstantInt>(DI.getVariableLocationOp(0));
542 if (CI.isZero())
543 return DI.getExpression()->getElements().equals(
544 {dwarf::DW_OP_plus_uconst, 1, dwarf::DW_OP_plus_uconst, 2,
545 dwarf::DW_OP_stack_value});
546 else if (CI.isOneValue())
547 return DI.getExpression()->getElements().equals(
548 {dwarf::DW_OP_plus_uconst, 2, dwarf::DW_OP_stack_value});
549 return false;
550 }
551
verifyDebugValuesAreSalvagedSalvageDebugInfoTest552 void verifyDebugValuesAreSalvaged() {
553 // Check that the debug values for %x and %y are preserved.
554 bool FoundX = false;
555 bool FoundY = false;
556 for (const Instruction &I : F->front()) {
557 auto DI = dyn_cast<DbgValueInst>(&I);
558 if (!DI) {
559 // The function should only contain debug values and a terminator.
560 ASSERT_TRUE(I.isTerminator());
561 continue;
562 }
563 EXPECT_EQ(DI->getVariable()->getName(), "x");
564 FoundX |= doesDebugValueDescribeX(*DI);
565 FoundY |= doesDebugValueDescribeY(*DI);
566 }
567 ASSERT_TRUE(FoundX);
568 ASSERT_TRUE(FoundY);
569 }
570 };
571
TEST_F(SalvageDebugInfoTest,RecursiveInstDeletion)572 TEST_F(SalvageDebugInfoTest, RecursiveInstDeletion) {
573 Instruction *Inst = &F->front().front();
574 Inst = Inst->getNextNode(); // Get %y = add ...
575 ASSERT_TRUE(Inst);
576 bool Deleted = RecursivelyDeleteTriviallyDeadInstructions(Inst);
577 ASSERT_TRUE(Deleted);
578 verifyDebugValuesAreSalvaged();
579 }
580
TEST_F(SalvageDebugInfoTest,RecursiveBlockSimplification)581 TEST_F(SalvageDebugInfoTest, RecursiveBlockSimplification) {
582 BasicBlock *BB = &F->front();
583 ASSERT_TRUE(BB);
584 bool Deleted = SimplifyInstructionsInBlock(BB);
585 ASSERT_TRUE(Deleted);
586 verifyDebugValuesAreSalvaged();
587 }
588
TEST(Local,ChangeToUnreachable)589 TEST(Local, ChangeToUnreachable) {
590 LLVMContext Ctx;
591
592 std::unique_ptr<Module> M = parseIR(Ctx,
593 R"(
594 define internal void @foo() !dbg !6 {
595 entry:
596 ret void, !dbg !8
597 }
598
599 !llvm.dbg.cu = !{!0}
600 !llvm.debugify = !{!3, !4}
601 !llvm.module.flags = !{!5}
602
603 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
604 !1 = !DIFile(filename: "test.ll", directory: "/")
605 !2 = !{}
606 !3 = !{i32 1}
607 !4 = !{i32 0}
608 !5 = !{i32 2, !"Debug Info Version", i32 3}
609 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, isLocal: true, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !2)
610 !7 = !DISubroutineType(types: !2)
611 !8 = !DILocation(line: 1, column: 1, scope: !6)
612 )");
613
614 bool BrokenDebugInfo = true;
615 verifyModule(*M, &errs(), &BrokenDebugInfo);
616 ASSERT_FALSE(BrokenDebugInfo);
617
618 Function &F = *cast<Function>(M->getNamedValue("foo"));
619
620 BasicBlock &BB = F.front();
621 Instruction &A = BB.front();
622 DebugLoc DLA = A.getDebugLoc();
623
624 ASSERT_TRUE(isa<ReturnInst>(&A));
625 // One instruction should be affected.
626 EXPECT_EQ(changeToUnreachable(&A), 1U);
627
628 Instruction &B = BB.front();
629
630 // There should be an uncreachable instruction.
631 ASSERT_TRUE(isa<UnreachableInst>(&B));
632
633 DebugLoc DLB = B.getDebugLoc();
634 EXPECT_EQ(DLA, DLB);
635 }
636
TEST(Local,ReplaceAllDbgUsesWith)637 TEST(Local, ReplaceAllDbgUsesWith) {
638 using namespace llvm::dwarf;
639
640 LLVMContext Ctx;
641
642 // Note: The datalayout simulates Darwin/x86_64.
643 std::unique_ptr<Module> M = parseIR(Ctx,
644 R"(
645 target datalayout = "e-m:o-i63:64-f80:128-n8:16:32:64-S128"
646
647 declare i32 @escape(i32)
648
649 define void @f() !dbg !6 {
650 entry:
651 %a = add i32 0, 1, !dbg !15
652 call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15
653
654 %b = add i64 0, 1, !dbg !16
655 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression()), !dbg !16
656 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul)), !dbg !16
657 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value)), !dbg !16
658 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 8)), !dbg !16
659 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_fragment, 0, 8)), !dbg !16
660 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8)), !dbg !16
661
662 %c = inttoptr i64 0 to i64*, !dbg !17
663 call void @llvm.dbg.declare(metadata i64* %c, metadata !13, metadata !DIExpression()), !dbg !17
664
665 %d = inttoptr i64 0 to i32*, !dbg !18
666 call void @llvm.dbg.addr(metadata i32* %d, metadata !20, metadata !DIExpression()), !dbg !18
667
668 %e = add <2 x i16> zeroinitializer, zeroinitializer
669 call void @llvm.dbg.value(metadata <2 x i16> %e, metadata !14, metadata !DIExpression()), !dbg !18
670
671 %f = call i32 @escape(i32 0)
672 call void @llvm.dbg.value(metadata i32 %f, metadata !9, metadata !DIExpression()), !dbg !15
673
674 %barrier = call i32 @escape(i32 0)
675
676 %g = call i32 @escape(i32 %f)
677 call void @llvm.dbg.value(metadata i32 %g, metadata !9, metadata !DIExpression()), !dbg !15
678
679 ret void, !dbg !19
680 }
681
682 declare void @llvm.dbg.addr(metadata, metadata, metadata)
683 declare void @llvm.dbg.declare(metadata, metadata, metadata)
684 declare void @llvm.dbg.value(metadata, metadata, metadata)
685
686 !llvm.dbg.cu = !{!0}
687 !llvm.module.flags = !{!5}
688
689 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
690 !1 = !DIFile(filename: "/Users/vsk/Desktop/foo.ll", directory: "/")
691 !2 = !{}
692 !5 = !{i32 2, !"Debug Info Version", i32 3}
693 !6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !8)
694 !7 = !DISubroutineType(types: !2)
695 !8 = !{!9, !11, !13, !14}
696 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
697 !10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_signed)
698 !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12)
699 !12 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_signed)
700 !13 = !DILocalVariable(name: "3", scope: !6, file: !1, line: 3, type: !12)
701 !14 = !DILocalVariable(name: "4", scope: !6, file: !1, line: 4, type: !10)
702 !15 = !DILocation(line: 1, column: 1, scope: !6)
703 !16 = !DILocation(line: 2, column: 1, scope: !6)
704 !17 = !DILocation(line: 3, column: 1, scope: !6)
705 !18 = !DILocation(line: 4, column: 1, scope: !6)
706 !19 = !DILocation(line: 5, column: 1, scope: !6)
707 !20 = !DILocalVariable(name: "5", scope: !6, file: !1, line: 5, type: !10)
708 )");
709
710 bool BrokenDebugInfo = true;
711 verifyModule(*M, &errs(), &BrokenDebugInfo);
712 ASSERT_FALSE(BrokenDebugInfo);
713
714 Function &F = *cast<Function>(M->getNamedValue("f"));
715 DominatorTree DT{F};
716
717 BasicBlock &BB = F.front();
718 Instruction &A = BB.front();
719 Instruction &B = *A.getNextNonDebugInstruction();
720 Instruction &C = *B.getNextNonDebugInstruction();
721 Instruction &D = *C.getNextNonDebugInstruction();
722 Instruction &E = *D.getNextNonDebugInstruction();
723 Instruction &F_ = *E.getNextNonDebugInstruction();
724 Instruction &Barrier = *F_.getNextNonDebugInstruction();
725 Instruction &G = *Barrier.getNextNonDebugInstruction();
726
727 // Simulate i32 <-> i64* conversion. Expect no updates: the datalayout says
728 // pointers are 64 bits, so the conversion would be lossy.
729 EXPECT_FALSE(replaceAllDbgUsesWith(A, C, C, DT));
730 EXPECT_FALSE(replaceAllDbgUsesWith(C, A, A, DT));
731
732 // Simulate i32 <-> <2 x i16> conversion. This is unsupported.
733 EXPECT_FALSE(replaceAllDbgUsesWith(E, A, A, DT));
734 EXPECT_FALSE(replaceAllDbgUsesWith(A, E, E, DT));
735
736 // Simulate i32* <-> i64* conversion.
737 EXPECT_TRUE(replaceAllDbgUsesWith(D, C, C, DT));
738
739 SmallVector<DbgVariableIntrinsic *, 2> CDbgVals;
740 findDbgUsers(CDbgVals, &C);
741 EXPECT_EQ(2U, CDbgVals.size());
742 EXPECT_TRUE(any_of(CDbgVals, [](DbgVariableIntrinsic *DII) {
743 return isa<DbgAddrIntrinsic>(DII);
744 }));
745 EXPECT_TRUE(any_of(CDbgVals, [](DbgVariableIntrinsic *DII) {
746 return isa<DbgDeclareInst>(DII);
747 }));
748
749 EXPECT_TRUE(replaceAllDbgUsesWith(C, D, D, DT));
750
751 SmallVector<DbgVariableIntrinsic *, 2> DDbgVals;
752 findDbgUsers(DDbgVals, &D);
753 EXPECT_EQ(2U, DDbgVals.size());
754 EXPECT_TRUE(any_of(DDbgVals, [](DbgVariableIntrinsic *DII) {
755 return isa<DbgAddrIntrinsic>(DII);
756 }));
757 EXPECT_TRUE(any_of(DDbgVals, [](DbgVariableIntrinsic *DII) {
758 return isa<DbgDeclareInst>(DII);
759 }));
760
761 // Introduce a use-before-def. Check that the dbg.value for %a is salvaged.
762 EXPECT_TRUE(replaceAllDbgUsesWith(A, F_, F_, DT));
763
764 auto *ADbgVal = cast<DbgValueInst>(A.getNextNode());
765 EXPECT_EQ(ADbgVal->getNumVariableLocationOps(), 1u);
766 EXPECT_EQ(ConstantInt::get(A.getType(), 0), ADbgVal->getVariableLocationOp(0));
767
768 // Introduce a use-before-def. Check that the dbg.values for %f become undef.
769 EXPECT_TRUE(replaceAllDbgUsesWith(F_, G, G, DT));
770
771 auto *FDbgVal = cast<DbgValueInst>(F_.getNextNode());
772 EXPECT_EQ(FDbgVal->getNumVariableLocationOps(), 1u);
773 EXPECT_TRUE(FDbgVal->isUndef());
774
775 SmallVector<DbgValueInst *, 1> FDbgVals;
776 findDbgValues(FDbgVals, &F_);
777 EXPECT_EQ(0U, FDbgVals.size());
778
779 // Simulate i32 -> i64 conversion to test sign-extension. Here are some
780 // interesting cases to handle:
781 // 1) debug user has empty DIExpression
782 // 2) debug user has non-empty, non-stack-value'd DIExpression
783 // 3) debug user has non-empty, stack-value'd DIExpression
784 // 4-6) like (1-3), but with a fragment
785 EXPECT_TRUE(replaceAllDbgUsesWith(B, A, A, DT));
786
787 SmallVector<DbgValueInst *, 8> ADbgVals;
788 findDbgValues(ADbgVals, &A);
789 EXPECT_EQ(6U, ADbgVals.size());
790
791 // Check that %a has a dbg.value with a DIExpression matching \p Ops.
792 auto hasADbgVal = [&](ArrayRef<uint64_t> Ops) {
793 return any_of(ADbgVals, [&](DbgValueInst *DVI) {
794 assert(DVI->getVariable()->getName() == "2");
795 return DVI->getExpression()->getElements() == Ops;
796 });
797 };
798
799 // Case 1: The original expr is empty, so no deref is needed.
800 EXPECT_TRUE(hasADbgVal({DW_OP_LLVM_convert, 32, DW_ATE_signed,
801 DW_OP_LLVM_convert, 64, DW_ATE_signed,
802 DW_OP_stack_value}));
803
804 // Case 2: Perform an address calculation with the original expr, deref it,
805 // then sign-extend the result.
806 EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref,
807 DW_OP_LLVM_convert, 32, DW_ATE_signed,
808 DW_OP_LLVM_convert, 64, DW_ATE_signed,
809 DW_OP_stack_value}));
810
811 // Case 3: Insert the sign-extension logic before the DW_OP_stack_value.
812 EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_convert, 32,
813 DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed,
814 DW_OP_stack_value}));
815
816 // Cases 4-6: Just like cases 1-3, but preserve the fragment at the end.
817 EXPECT_TRUE(hasADbgVal({DW_OP_LLVM_convert, 32, DW_ATE_signed,
818 DW_OP_LLVM_convert, 64, DW_ATE_signed,
819 DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8}));
820
821 EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref,
822 DW_OP_LLVM_convert, 32, DW_ATE_signed,
823 DW_OP_LLVM_convert, 64, DW_ATE_signed,
824 DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8}));
825
826 EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_convert, 32,
827 DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed,
828 DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8}));
829
830 verifyModule(*M, &errs(), &BrokenDebugInfo);
831 ASSERT_FALSE(BrokenDebugInfo);
832 }
833
TEST(Local,RemoveUnreachableBlocks)834 TEST(Local, RemoveUnreachableBlocks) {
835 LLVMContext C;
836
837 std::unique_ptr<Module> M = parseIR(C,
838 R"(
839 define void @br_simple() {
840 entry:
841 br label %bb0
842 bb0:
843 ret void
844 bb1:
845 ret void
846 }
847
848 define void @br_self_loop() {
849 entry:
850 br label %bb0
851 bb0:
852 br i1 true, label %bb1, label %bb0
853 bb1:
854 br i1 true, label %bb0, label %bb2
855 bb2:
856 br label %bb2
857 }
858
859 define void @br_constant() {
860 entry:
861 br label %bb0
862 bb0:
863 br i1 true, label %bb1, label %bb2
864 bb1:
865 br i1 true, label %bb0, label %bb2
866 bb2:
867 br label %bb2
868 }
869
870 define void @br_loop() {
871 entry:
872 br label %bb0
873 bb0:
874 br label %bb0
875 bb1:
876 br label %bb2
877 bb2:
878 br label %bb1
879 }
880
881 declare i32 @__gxx_personality_v0(...)
882
883 define void @invoke_terminator() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
884 entry:
885 br i1 undef, label %invoke.block, label %exit
886
887 invoke.block:
888 %cond = invoke zeroext i1 @invokable()
889 to label %continue.block unwind label %lpad.block
890
891 continue.block:
892 br i1 %cond, label %if.then, label %if.end
893
894 if.then:
895 unreachable
896
897 if.end:
898 unreachable
899
900 lpad.block:
901 %lp = landingpad { i8*, i32 }
902 catch i8* null
903 br label %exit
904
905 exit:
906 ret void
907 }
908
909 declare i1 @invokable()
910 )");
911
912 auto runEager = [&](Function &F, DominatorTree *DT) {
913 PostDominatorTree PDT = PostDominatorTree(F);
914 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager);
915 removeUnreachableBlocks(F, &DTU);
916 EXPECT_TRUE(DTU.getDomTree().verify());
917 EXPECT_TRUE(DTU.getPostDomTree().verify());
918 };
919
920 auto runLazy = [&](Function &F, DominatorTree *DT) {
921 PostDominatorTree PDT = PostDominatorTree(F);
922 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy);
923 removeUnreachableBlocks(F, &DTU);
924 EXPECT_TRUE(DTU.getDomTree().verify());
925 EXPECT_TRUE(DTU.getPostDomTree().verify());
926 };
927
928 // Test removeUnreachableBlocks under Eager UpdateStrategy.
929 runWithDomTree(*M, "br_simple", runEager);
930 runWithDomTree(*M, "br_self_loop", runEager);
931 runWithDomTree(*M, "br_constant", runEager);
932 runWithDomTree(*M, "br_loop", runEager);
933 runWithDomTree(*M, "invoke_terminator", runEager);
934
935 // Test removeUnreachableBlocks under Lazy UpdateStrategy.
936 runWithDomTree(*M, "br_simple", runLazy);
937 runWithDomTree(*M, "br_self_loop", runLazy);
938 runWithDomTree(*M, "br_constant", runLazy);
939 runWithDomTree(*M, "br_loop", runLazy);
940 runWithDomTree(*M, "invoke_terminator", runLazy);
941
942 M = parseIR(C,
943 R"(
944 define void @f() {
945 entry:
946 ret void
947 bb0:
948 ret void
949 }
950 )");
951
952 auto checkRUBlocksRetVal = [&](Function &F, DominatorTree *DT) {
953 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
954 EXPECT_TRUE(removeUnreachableBlocks(F, &DTU));
955 EXPECT_FALSE(removeUnreachableBlocks(F, &DTU));
956 EXPECT_TRUE(DTU.getDomTree().verify());
957 };
958
959 runWithDomTree(*M, "f", checkRUBlocksRetVal);
960 }
961
TEST(Local,SimplifyCFGWithNullAC)962 TEST(Local, SimplifyCFGWithNullAC) {
963 LLVMContext Ctx;
964
965 std::unique_ptr<Module> M = parseIR(Ctx, R"(
966 declare void @true_path()
967 declare void @false_path()
968 declare void @llvm.assume(i1 %cond);
969
970 define i32 @foo(i1, i32) {
971 entry:
972 %cmp = icmp sgt i32 %1, 0
973 br i1 %cmp, label %if.bb1, label %then.bb1
974 if.bb1:
975 call void @true_path()
976 br label %test.bb
977 then.bb1:
978 call void @false_path()
979 br label %test.bb
980 test.bb:
981 %phi = phi i1 [1, %if.bb1], [%0, %then.bb1]
982 call void @llvm.assume(i1 %0)
983 br i1 %phi, label %if.bb2, label %then.bb2
984 if.bb2:
985 ret i32 %1
986 then.bb2:
987 ret i32 0
988 }
989 )");
990
991 Function &F = *cast<Function>(M->getNamedValue("foo"));
992 TargetTransformInfo TTI(M->getDataLayout());
993
994 SimplifyCFGOptions Options{};
995 Options.setAssumptionCache(nullptr);
996
997 // Obtain BasicBlock of interest to this test, %test.bb.
998 BasicBlock *TestBB = nullptr;
999 for (BasicBlock &BB : F) {
1000 if (BB.getName().equals("test.bb")) {
1001 TestBB = &BB;
1002 break;
1003 }
1004 }
1005 ASSERT_TRUE(TestBB);
1006
1007 DominatorTree DT(F);
1008 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
1009
1010 // %test.bb is expected to be simplified by FoldCondBranchOnPHI.
1011 EXPECT_TRUE(simplifyCFG(TestBB, TTI,
1012 RequireAndPreserveDomTree ? &DTU : nullptr, Options));
1013 }
1014
TEST(Local,CanReplaceOperandWithVariable)1015 TEST(Local, CanReplaceOperandWithVariable) {
1016 LLVMContext Ctx;
1017 Module M("test_module", Ctx);
1018 IRBuilder<> B(Ctx);
1019
1020 FunctionType *FnType =
1021 FunctionType::get(Type::getVoidTy(Ctx), {}, false);
1022
1023 FunctionType *VarArgFnType =
1024 FunctionType::get(Type::getVoidTy(Ctx), {B.getInt32Ty()}, true);
1025
1026 Function *TestBody = Function::Create(FnType, GlobalValue::ExternalLinkage,
1027 0, "", &M);
1028
1029 BasicBlock *BB0 = BasicBlock::Create(Ctx, "", TestBody);
1030 B.SetInsertPoint(BB0);
1031
1032 FunctionCallee Intrin = M.getOrInsertFunction("llvm.foo", FnType);
1033 FunctionCallee Func = M.getOrInsertFunction("foo", FnType);
1034 FunctionCallee VarArgFunc
1035 = M.getOrInsertFunction("foo.vararg", VarArgFnType);
1036 FunctionCallee VarArgIntrin
1037 = M.getOrInsertFunction("llvm.foo.vararg", VarArgFnType);
1038
1039 auto *CallToIntrin = B.CreateCall(Intrin);
1040 auto *CallToFunc = B.CreateCall(Func);
1041
1042 // Test if it's valid to replace the callee operand.
1043 EXPECT_FALSE(canReplaceOperandWithVariable(CallToIntrin, 0));
1044 EXPECT_TRUE(canReplaceOperandWithVariable(CallToFunc, 0));
1045
1046 // That it's invalid to replace an argument in the variadic argument list for
1047 // an intrinsic, but OK for a normal function.
1048 auto *CallToVarArgFunc = B.CreateCall(
1049 VarArgFunc, {B.getInt32(0), B.getInt32(1), B.getInt32(2)});
1050 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 0));
1051 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 1));
1052 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 2));
1053 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 3));
1054
1055 auto *CallToVarArgIntrin = B.CreateCall(
1056 VarArgIntrin, {B.getInt32(0), B.getInt32(1), B.getInt32(2)});
1057 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgIntrin, 0));
1058 EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 1));
1059 EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 2));
1060 EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 3));
1061
1062 // Test that it's invalid to replace gcroot operands, even though it can't use
1063 // immarg.
1064 Type *PtrPtr = B.getInt8Ty()->getPointerTo(0);
1065 Value *Alloca = B.CreateAlloca(PtrPtr, (unsigned)0);
1066 CallInst *GCRoot = B.CreateIntrinsic(Intrinsic::gcroot, {},
1067 {Alloca, Constant::getNullValue(PtrPtr)});
1068 EXPECT_TRUE(canReplaceOperandWithVariable(GCRoot, 0)); // Alloca
1069 EXPECT_FALSE(canReplaceOperandWithVariable(GCRoot, 1));
1070 EXPECT_FALSE(canReplaceOperandWithVariable(GCRoot, 2));
1071
1072 BB0->dropAllReferences();
1073 }
1074