| 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/ADT/ScopeExit.h" |
| 11 |
#include "llvm/Analysis/DomTreeUpdater.h" |
| 12 |
#include "llvm/Analysis/InstructionSimplify.h" |
| 13 |
#include "llvm/Analysis/PostDominators.h" |
| 14 |
#include "llvm/Analysis/TargetTransformInfo.h" |
| 15 |
#include "llvm/AsmParser/Parser.h" |
| 16 |
#include "llvm/IR/BasicBlock.h" |
| 17 |
#include "llvm/IR/DIBuilder.h" |
| 18 |
#include "llvm/IR/DebugInfo.h" |
| 19 |
#include "llvm/IR/DebugProgramInstruction.h" |
| 20 |
#include "llvm/IR/IRBuilder.h" |
| 21 |
#include "llvm/IR/Instructions.h" |
| 22 |
#include "llvm/IR/IntrinsicInst.h" |
| 23 |
#include "llvm/IR/LLVMContext.h" |
| 24 |
#include "llvm/IR/Module.h" |
| 25 |
#include "llvm/IR/Verifier.h" |
| 26 |
#include "llvm/Support/SourceMgr.h" |
| 27 |
#include "gtest/gtest.h" |
| 28 |
|
| 29 |
using namespace llvm; |
| 30 |
|
| 31 |
TEST(Local, RecursivelyDeleteDeadPHINodes) { |
| 32 |
LLVMContext C; |
| 33 |
|
| 34 |
IRBuilder<> builder(C); |
| 35 |
|
| 36 |
// Make blocks |
| 37 |
BasicBlock *bb0 = BasicBlock::Create(C); |
| 38 |
BasicBlock *bb1 = BasicBlock::Create(C); |
| 39 |
|
| 40 |
builder.SetInsertPoint(bb0); |
| 41 |
PHINode *phi = builder.CreatePHI(Type::getInt32Ty(C), 2); |
| 42 |
BranchInst *br0 = builder.CreateCondBr(builder.getTrue(), bb0, bb1); |
| 43 |
|
| 44 |
builder.SetInsertPoint(bb1); |
| 45 |
BranchInst *br1 = builder.CreateBr(bb0); |
| 46 |
|
| 47 |
phi->addIncoming(phi, bb0); |
| 48 |
phi->addIncoming(phi, bb1); |
| 49 |
|
| 50 |
// The PHI will be removed |
| 51 |
EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi)); |
| 52 |
|
| 53 |
// Make sure the blocks only contain the branches |
| 54 |
EXPECT_EQ(&bb0->front(), br0); |
| 55 |
EXPECT_EQ(&bb1->front(), br1); |
| 56 |
|
| 57 |
builder.SetInsertPoint(bb0); |
| 58 |
phi = builder.CreatePHI(Type::getInt32Ty(C), 0); |
| 59 |
|
| 60 |
EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi)); |
| 61 |
|
| 62 |
builder.SetInsertPoint(bb0); |
| 63 |
phi = builder.CreatePHI(Type::getInt32Ty(C), 0); |
| 64 |
builder.CreateAdd(phi, phi); |
| 65 |
|
| 66 |
EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi)); |
| 67 |
|
| 68 |
bb0->dropAllReferences(); |
| 69 |
bb1->dropAllReferences(); |
| 70 |
delete bb0; |
| 71 |
delete bb1; |
| 72 |
} |
| 73 |
|
| 74 |
TEST(Local, RemoveDuplicatePHINodes) { |
| 75 |
LLVMContext C; |
| 76 |
IRBuilder<> B(C); |
| 77 |
|
| 78 |
std::unique_ptr<Function> F( |
| 79 |
Function::Create(FunctionType::get(B.getVoidTy(), false), |
| 80 |
GlobalValue::ExternalLinkage, "F")); |
| 81 |
BasicBlock *Entry(BasicBlock::Create(C, "", F.get())); |
| 82 |
BasicBlock *BB(BasicBlock::Create(C, "", F.get())); |
| 83 |
BranchInst::Create(BB, Entry); |
| 84 |
|
| 85 |
B.SetInsertPoint(BB); |
| 86 |
|
| 87 |
AssertingVH<PHINode> P1 = B.CreatePHI(Type::getInt32Ty(C), 2); |
| 88 |
P1->addIncoming(B.getInt32(42), Entry); |
| 89 |
|
| 90 |
PHINode *P2 = B.CreatePHI(Type::getInt32Ty(C), 2); |
| 91 |
P2->addIncoming(B.getInt32(42), Entry); |
| 92 |
|
| 93 |
AssertingVH<PHINode> P3 = B.CreatePHI(Type::getInt32Ty(C), 2); |
| 94 |
P3->addIncoming(B.getInt32(42), Entry); |
| 95 |
P3->addIncoming(B.getInt32(23), BB); |
| 96 |
|
| 97 |
PHINode *P4 = B.CreatePHI(Type::getInt32Ty(C), 2); |
| 98 |
P4->addIncoming(B.getInt32(42), Entry); |
| 99 |
P4->addIncoming(B.getInt32(23), BB); |
| 100 |
|
| 101 |
P1->addIncoming(P3, BB); |
| 102 |
P2->addIncoming(P4, BB); |
| 103 |
BranchInst::Create(BB, BB); |
| 104 |
|
| 105 |
// Verify that we can eliminate PHIs that become duplicates after chaning PHIs |
| 106 |
// downstream. |
| 107 |
EXPECT_TRUE(EliminateDuplicatePHINodes(BB)); |
| 108 |
EXPECT_EQ(3U, BB->size()); |
| 109 |
} |
| 110 |
|
| 111 |
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { |
| 112 |
SMDiagnostic Err; |
| 113 |
std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C); |
| 114 |
if (!Mod) |
| 115 |
Err.print("UtilsTests", errs()); |
| 116 |
return Mod; |
| 117 |
} |
| 118 |
|
| 119 |
TEST(Local, ReplaceDbgDeclare) { |
| 120 |
LLVMContext C; |
| 121 |
// Original C source to get debug info for a local variable: |
| 122 |
// void f() { int x; } |
| 123 |
std::unique_ptr<Module> M = parseIR(C, |
| 124 |
R"( |
| 125 |
define void @f() !dbg !8 { |
| 126 |
entry: |
| 127 |
%x = alloca i32, align 4 |
| 128 |
#dbg_declare(ptr %x, !11, !DIExpression(), !13) |
| 129 |
#dbg_declare(ptr %x, !11, !DIExpression(), !13) |
| 130 |
ret void, !dbg !14 |
| 131 |
} |
| 132 |
|
| 133 |
!llvm.dbg.cu = !{!0} |
| 134 |
!llvm.module.flags = !{!3, !4} |
| 135 |
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) |
| 136 |
!1 = !DIFile(filename: "t2.c", directory: "foo") |
| 137 |
!2 = !{} |
| 138 |
!3 = !{i32 2, !"Dwarf Version", i32 4} |
| 139 |
!4 = !{i32 2, !"Debug Info Version", i32 3} |
| 140 |
!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) |
| 141 |
!9 = !DISubroutineType(types: !10) |
| 142 |
!10 = !{null} |
| 143 |
!11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12) |
| 144 |
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) |
| 145 |
!13 = !DILocation(line: 2, column: 7, scope: !8) |
| 146 |
!14 = !DILocation(line: 3, column: 1, scope: !8) |
| 147 |
)"); |
| 148 |
auto *GV = M->getNamedValue("f"); |
| 149 |
ASSERT_TRUE(GV); |
| 150 |
auto *F = dyn_cast<Function>(GV); |
| 151 |
ASSERT_TRUE(F); |
| 152 |
Instruction *Inst = &F->front().front(); |
| 153 |
auto *AI = dyn_cast<AllocaInst>(Inst); |
| 154 |
ASSERT_TRUE(AI); |
| 155 |
|
| 156 |
Value *NewBase = Constant::getNullValue(PointerType::getUnqual(C)); |
| 157 |
DIBuilder DIB(*M); |
| 158 |
replaceDbgDeclare(AI, NewBase, DIB, DIExpression::ApplyOffset, 0); |
| 159 |
|
| 160 |
// There should be exactly two dbg.declares, attached to the terminator. |
| 161 |
Inst = F->front().getTerminator(); |
| 162 |
ASSERT_TRUE(Inst); |
| 163 |
EXPECT_TRUE(Inst->hasDbgRecords()); |
| 164 |
EXPECT_EQ(range_size(Inst->getDbgRecordRange()), 2u); |
| 165 |
for (DbgVariableRecord &DVR : filterDbgVars(Inst->getDbgRecordRange())) |
| 166 |
EXPECT_EQ(DVR.getAddress(), NewBase); |
| 167 |
} |
| 168 |
|
| 169 |
/// Build the dominator tree for the function and run the Test. |
| 170 |
static void runWithDomTree( |
| 171 |
Module &M, StringRef FuncName, |
| 172 |
function_ref<void(Function &F, DominatorTree *DT)> Test) { |
| 173 |
auto *F = M.getFunction(FuncName); |
| 174 |
ASSERT_NE(F, nullptr) << "Could not find " << FuncName; |
| 175 |
// Compute the dominator tree for the function. |
| 176 |
DominatorTree DT(*F); |
| 177 |
Test(*F, &DT); |
| 178 |
} |
| 179 |
|
| 180 |
TEST(Local, MergeBasicBlockIntoOnlyPred) { |
| 181 |
LLVMContext C; |
| 182 |
std::unique_ptr<Module> M; |
| 183 |
auto resetIR = [&]() { |
| 184 |
M = parseIR(C, |
| 185 |
R"( |
| 186 |
define i32 @f(i8* %str) { |
| 187 |
entry: |
| 188 |
br label %bb2.i |
| 189 |
bb2.i: ; preds = %bb4.i, %entry |
| 190 |
br i1 false, label %bb4.i, label %base2flt.exit204 |
| 191 |
bb4.i: ; preds = %bb2.i |
| 192 |
br i1 false, label %base2flt.exit204, label %bb2.i |
| 193 |
bb10.i196.bb7.i197_crit_edge: ; No predecessors! |
| 194 |
br label %bb7.i197 |
| 195 |
bb7.i197: ; preds = %bb10.i196.bb7.i197_crit_edge |
| 196 |
%.reg2mem.0 = phi i32 [ %.reg2mem.0, %bb10.i196.bb7.i197_crit_edge ] |
| 197 |
br i1 undef, label %base2flt.exit204, label %base2flt.exit204 |
| 198 |
base2flt.exit204: ; preds = %bb7.i197, %bb7.i197, %bb2.i, %bb4.i |
| 199 |
ret i32 0 |
| 200 |
} |
| 201 |
)"); |
| 202 |
}; |
| 203 |
|
| 204 |
auto resetIRReplaceEntry = [&]() { |
| 205 |
M = parseIR(C, |
| 206 |
R"( |
| 207 |
define i32 @f() { |
| 208 |
entry: |
| 209 |
br label %bb2.i |
| 210 |
bb2.i: ; preds = %entry |
| 211 |
ret i32 0 |
| 212 |
} |
| 213 |
)"); |
| 214 |
}; |
| 215 |
|
| 216 |
auto Test = [&](Function &F, DomTreeUpdater &DTU) { |
| 217 |
for (Function::iterator I = F.begin(), E = F.end(); I != E;) { |
| 218 |
BasicBlock *BB = &*I++; |
| 219 |
BasicBlock *SinglePred = BB->getSinglePredecessor(); |
| 220 |
if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) |
| 221 |
continue; |
| 222 |
BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); |
| 223 |
if (Term && !Term->isConditional()) |
| 224 |
MergeBasicBlockIntoOnlyPred(BB, &DTU); |
| 225 |
} |
| 226 |
if (DTU.hasDomTree()) { |
| 227 |
EXPECT_TRUE(DTU.getDomTree().verify()); |
| 228 |
} |
| 229 |
if (DTU.hasPostDomTree()) { |
| 230 |
EXPECT_TRUE(DTU.getPostDomTree().verify()); |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
// Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with |
| 235 |
// both DT and PDT. |
| 236 |
resetIR(); |
| 237 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 238 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 239 |
DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager); |
| 240 |
Test(F, DTU); |
| 241 |
}); |
| 242 |
|
| 243 |
// Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with |
| 244 |
// DT. |
| 245 |
resetIR(); |
| 246 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 247 |
DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Eager); |
| 248 |
Test(F, DTU); |
| 249 |
}); |
| 250 |
|
| 251 |
// Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with |
| 252 |
// PDT. |
| 253 |
resetIR(); |
| 254 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 255 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 256 |
DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Eager); |
| 257 |
Test(F, DTU); |
| 258 |
}); |
| 259 |
|
| 260 |
// Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with |
| 261 |
// both DT and PDT. |
| 262 |
resetIR(); |
| 263 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 264 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 265 |
DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 266 |
Test(F, DTU); |
| 267 |
}); |
| 268 |
|
| 269 |
// Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with |
| 270 |
// PDT. |
| 271 |
resetIR(); |
| 272 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 273 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 274 |
DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 275 |
Test(F, DTU); |
| 276 |
}); |
| 277 |
|
| 278 |
// Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with DT. |
| 279 |
resetIR(); |
| 280 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 281 |
DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 282 |
Test(F, DTU); |
| 283 |
}); |
| 284 |
|
| 285 |
// Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with |
| 286 |
// both DT and PDT. |
| 287 |
resetIRReplaceEntry(); |
| 288 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 289 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 290 |
DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager); |
| 291 |
Test(F, DTU); |
| 292 |
}); |
| 293 |
|
| 294 |
// Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with |
| 295 |
// DT. |
| 296 |
resetIRReplaceEntry(); |
| 297 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 298 |
DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Eager); |
| 299 |
Test(F, DTU); |
| 300 |
}); |
| 301 |
|
| 302 |
// Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with |
| 303 |
// PDT. |
| 304 |
resetIRReplaceEntry(); |
| 305 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 306 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 307 |
DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Eager); |
| 308 |
Test(F, DTU); |
| 309 |
}); |
| 310 |
|
| 311 |
// Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with |
| 312 |
// both DT and PDT. |
| 313 |
resetIRReplaceEntry(); |
| 314 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 315 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 316 |
DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 317 |
Test(F, DTU); |
| 318 |
}); |
| 319 |
|
| 320 |
// Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with |
| 321 |
// PDT. |
| 322 |
resetIRReplaceEntry(); |
| 323 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 324 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 325 |
DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 326 |
Test(F, DTU); |
| 327 |
}); |
| 328 |
|
| 329 |
// Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with DT. |
| 330 |
resetIRReplaceEntry(); |
| 331 |
runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { |
| 332 |
DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 333 |
Test(F, DTU); |
| 334 |
}); |
| 335 |
} |
| 336 |
|
| 337 |
TEST(Local, ConstantFoldTerminator) { |
| 338 |
LLVMContext C; |
| 339 |
|
| 340 |
std::unique_ptr<Module> M = parseIR(C, |
| 341 |
R"( |
| 342 |
define void @br_same_dest() { |
| 343 |
entry: |
| 344 |
br i1 false, label %bb0, label %bb0 |
| 345 |
bb0: |
| 346 |
ret void |
| 347 |
} |
| 348 |
|
| 349 |
define void @br_different_dest() { |
| 350 |
entry: |
| 351 |
br i1 true, label %bb0, label %bb1 |
| 352 |
bb0: |
| 353 |
br label %exit |
| 354 |
bb1: |
| 355 |
br label %exit |
| 356 |
exit: |
| 357 |
ret void |
| 358 |
} |
| 359 |
|
| 360 |
define void @switch_2_different_dest() { |
| 361 |
entry: |
| 362 |
switch i32 0, label %default [ i32 0, label %bb0 ] |
| 363 |
default: |
| 364 |
ret void |
| 365 |
bb0: |
| 366 |
ret void |
| 367 |
} |
| 368 |
define void @switch_2_different_dest_default() { |
| 369 |
entry: |
| 370 |
switch i32 1, label %default [ i32 0, label %bb0 ] |
| 371 |
default: |
| 372 |
ret void |
| 373 |
bb0: |
| 374 |
ret void |
| 375 |
} |
| 376 |
define void @switch_3_different_dest() { |
| 377 |
entry: |
| 378 |
switch i32 0, label %default [ i32 0, label %bb0 |
| 379 |
i32 1, label %bb1 ] |
| 380 |
default: |
| 381 |
ret void |
| 382 |
bb0: |
| 383 |
ret void |
| 384 |
bb1: |
| 385 |
ret void |
| 386 |
} |
| 387 |
|
| 388 |
define void @switch_variable_2_default_dest(i32 %arg) { |
| 389 |
entry: |
| 390 |
switch i32 %arg, label %default [ i32 0, label %default ] |
| 391 |
default: |
| 392 |
ret void |
| 393 |
} |
| 394 |
|
| 395 |
define void @switch_constant_2_default_dest() { |
| 396 |
entry: |
| 397 |
switch i32 1, label %default [ i32 0, label %default ] |
| 398 |
default: |
| 399 |
ret void |
| 400 |
} |
| 401 |
|
| 402 |
define void @switch_constant_3_repeated_dest() { |
| 403 |
entry: |
| 404 |
switch i32 0, label %default [ i32 0, label %bb0 |
| 405 |
i32 1, label %bb0 ] |
| 406 |
bb0: |
| 407 |
ret void |
| 408 |
default: |
| 409 |
ret void |
| 410 |
} |
| 411 |
|
| 412 |
define void @indirectbr() { |
| 413 |
entry: |
| 414 |
indirectbr i8* blockaddress(@indirectbr, %bb0), [label %bb0, label %bb1] |
| 415 |
bb0: |
| 416 |
ret void |
| 417 |
bb1: |
| 418 |
ret void |
| 419 |
} |
| 420 |
|
| 421 |
define void @indirectbr_repeated() { |
| 422 |
entry: |
| 423 |
indirectbr i8* blockaddress(@indirectbr_repeated, %bb0), [label %bb0, label %bb0] |
| 424 |
bb0: |
| 425 |
ret void |
| 426 |
} |
| 427 |
|
| 428 |
define void @indirectbr_unreachable() { |
| 429 |
entry: |
| 430 |
indirectbr i8* blockaddress(@indirectbr_unreachable, %bb0), [label %bb1] |
| 431 |
bb0: |
| 432 |
ret void |
| 433 |
bb1: |
| 434 |
ret void |
| 435 |
} |
| 436 |
)"); |
| 437 |
|
| 438 |
auto CFAllTerminatorsEager = [&](Function &F, DominatorTree *DT) { |
| 439 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 440 |
DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager); |
| 441 |
for (Function::iterator I = F.begin(), E = F.end(); I != E;) { |
| 442 |
BasicBlock *BB = &*I++; |
| 443 |
ConstantFoldTerminator(BB, true, nullptr, &DTU); |
| 444 |
} |
| 445 |
|
| 446 |
EXPECT_TRUE(DTU.getDomTree().verify()); |
| 447 |
EXPECT_TRUE(DTU.getPostDomTree().verify()); |
| 448 |
}; |
| 449 |
|
| 450 |
auto CFAllTerminatorsLazy = [&](Function &F, DominatorTree *DT) { |
| 451 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 452 |
DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 453 |
for (Function::iterator I = F.begin(), E = F.end(); I != E;) { |
| 454 |
BasicBlock *BB = &*I++; |
| 455 |
ConstantFoldTerminator(BB, true, nullptr, &DTU); |
| 456 |
} |
| 457 |
|
| 458 |
EXPECT_TRUE(DTU.getDomTree().verify()); |
| 459 |
EXPECT_TRUE(DTU.getPostDomTree().verify()); |
| 460 |
}; |
| 461 |
|
| 462 |
// Test ConstantFoldTerminator under Eager UpdateStrategy. |
| 463 |
runWithDomTree(*M, "br_same_dest", CFAllTerminatorsEager); |
| 464 |
runWithDomTree(*M, "br_different_dest", CFAllTerminatorsEager); |
| 465 |
runWithDomTree(*M, "switch_2_different_dest", CFAllTerminatorsEager); |
| 466 |
runWithDomTree(*M, "switch_2_different_dest_default", CFAllTerminatorsEager); |
| 467 |
runWithDomTree(*M, "switch_3_different_dest", CFAllTerminatorsEager); |
| 468 |
runWithDomTree(*M, "switch_variable_2_default_dest", CFAllTerminatorsEager); |
| 469 |
runWithDomTree(*M, "switch_constant_2_default_dest", CFAllTerminatorsEager); |
| 470 |
runWithDomTree(*M, "switch_constant_3_repeated_dest", CFAllTerminatorsEager); |
| 471 |
runWithDomTree(*M, "indirectbr", CFAllTerminatorsEager); |
| 472 |
runWithDomTree(*M, "indirectbr_repeated", CFAllTerminatorsEager); |
| 473 |
runWithDomTree(*M, "indirectbr_unreachable", CFAllTerminatorsEager); |
| 474 |
|
| 475 |
// Test ConstantFoldTerminator under Lazy UpdateStrategy. |
| 476 |
runWithDomTree(*M, "br_same_dest", CFAllTerminatorsLazy); |
| 477 |
runWithDomTree(*M, "br_different_dest", CFAllTerminatorsLazy); |
| 478 |
runWithDomTree(*M, "switch_2_different_dest", CFAllTerminatorsLazy); |
| 479 |
runWithDomTree(*M, "switch_2_different_dest_default", CFAllTerminatorsLazy); |
| 480 |
runWithDomTree(*M, "switch_3_different_dest", CFAllTerminatorsLazy); |
| 481 |
runWithDomTree(*M, "switch_variable_2_default_dest", CFAllTerminatorsLazy); |
| 482 |
runWithDomTree(*M, "switch_constant_2_default_dest", CFAllTerminatorsLazy); |
| 483 |
runWithDomTree(*M, "switch_constant_3_repeated_dest", CFAllTerminatorsLazy); |
| 484 |
runWithDomTree(*M, "indirectbr", CFAllTerminatorsLazy); |
| 485 |
runWithDomTree(*M, "indirectbr_repeated", CFAllTerminatorsLazy); |
| 486 |
runWithDomTree(*M, "indirectbr_unreachable", CFAllTerminatorsLazy); |
| 487 |
} |
| 488 |
|
| 489 |
struct SalvageDebugInfoTest : ::testing::Test { |
| 490 |
LLVMContext C; |
| 491 |
std::unique_ptr<Module> M; |
| 492 |
Function *F = nullptr; |
| 493 |
|
| 494 |
void SetUp() override { |
| 495 |
M = parseIR(C, |
| 496 |
R"( |
| 497 |
define void @f() !dbg !8 { |
| 498 |
entry: |
| 499 |
%x = add i32 0, 1 |
| 500 |
%y = add i32 %x, 2 |
| 501 |
#dbg_value(i32 %x, !11, !DIExpression(), !13) |
| 502 |
#dbg_value(i32 %y, !11, !DIExpression(), !13) |
| 503 |
ret void, !dbg !14 |
| 504 |
} |
| 505 |
!llvm.dbg.cu = !{!0} |
| 506 |
!llvm.module.flags = !{!3, !4} |
| 507 |
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) |
| 508 |
!1 = !DIFile(filename: "t2.c", directory: "foo") |
| 509 |
!2 = !{} |
| 510 |
!3 = !{i32 2, !"Dwarf Version", i32 4} |
| 511 |
!4 = !{i32 2, !"Debug Info Version", i32 3} |
| 512 |
!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) |
| 513 |
!9 = !DISubroutineType(types: !10) |
| 514 |
!10 = !{null} |
| 515 |
!11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12) |
| 516 |
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) |
| 517 |
!13 = !DILocation(line: 2, column: 7, scope: !8) |
| 518 |
!14 = !DILocation(line: 3, column: 1, scope: !8) |
| 519 |
)"); |
| 520 |
|
| 521 |
auto *GV = M->getNamedValue("f"); |
| 522 |
ASSERT_TRUE(GV); |
| 523 |
F = dyn_cast<Function>(GV); |
| 524 |
ASSERT_TRUE(F); |
| 525 |
} |
| 526 |
|
| 527 |
bool doesDebugValueDescribeX(const DbgVariableRecord &DVR) { |
| 528 |
if (DVR.getNumVariableLocationOps() != 1u) |
| 529 |
return false; |
| 530 |
const auto &CI = *cast<ConstantInt>(DVR.getValue(0)); |
| 531 |
if (CI.isZero()) |
| 532 |
return DVR.getExpression()->getElements().equals( |
| 533 |
{dwarf::DW_OP_plus_uconst, 1, dwarf::DW_OP_stack_value}); |
| 534 |
else if (CI.isOneValue()) |
| 535 |
return DVR.getExpression()->getElements().empty(); |
| 536 |
return false; |
| 537 |
} |
| 538 |
|
| 539 |
bool doesDebugValueDescribeY(const DbgVariableRecord &DVR) { |
| 540 |
if (DVR.getNumVariableLocationOps() != 1u) |
| 541 |
return false; |
| 542 |
const auto &CI = *cast<ConstantInt>(DVR.getVariableLocationOp(0)); |
| 543 |
if (CI.isZero()) |
| 544 |
return DVR.getExpression()->getElements().equals( |
| 545 |
{dwarf::DW_OP_plus_uconst, 3, dwarf::DW_OP_stack_value}); |
| 546 |
else if (CI.isOneValue()) |
| 547 |
return DVR.getExpression()->getElements().equals( |
| 548 |
{dwarf::DW_OP_plus_uconst, 2, dwarf::DW_OP_stack_value}); |
| 549 |
return false; |
| 550 |
} |
| 551 |
|
| 552 |
void verifyDebugValuesAreSalvaged() { |
| 553 |
// The function should only contain debug values and a terminator. |
| 554 |
EXPECT_EQ(F->size(), 1u); |
| 555 |
EXPECT_TRUE(F->begin()->begin()->isTerminator()); |
| 556 |
|
| 557 |
// Check that the debug values for %x and %y are preserved. |
| 558 |
bool FoundX = false; |
| 559 |
bool FoundY = false; |
| 560 |
for (DbgVariableRecord &DVR : |
| 561 |
filterDbgVars(F->begin()->begin()->getDbgRecordRange())) { |
| 562 |
EXPECT_EQ(DVR.getVariable()->getName(), "x"); |
| 563 |
FoundX |= doesDebugValueDescribeX(DVR); |
| 564 |
FoundY |= doesDebugValueDescribeY(DVR); |
| 565 |
} |
| 566 |
EXPECT_TRUE(FoundX); |
| 567 |
EXPECT_TRUE(FoundY); |
| 568 |
} |
| 569 |
}; |
| 570 |
|
| 571 |
TEST_F(SalvageDebugInfoTest, RecursiveInstDeletion) { |
| 572 |
Instruction *Inst = &F->front().front(); |
| 573 |
Inst = Inst->getNextNode(); // Get %y = add ... |
| 574 |
ASSERT_TRUE(Inst); |
| 575 |
bool Deleted = RecursivelyDeleteTriviallyDeadInstructions(Inst); |
| 576 |
ASSERT_TRUE(Deleted); |
| 577 |
verifyDebugValuesAreSalvaged(); |
| 578 |
} |
| 579 |
|
| 580 |
TEST_F(SalvageDebugInfoTest, RecursiveBlockSimplification) { |
| 581 |
BasicBlock *BB = &F->front(); |
| 582 |
ASSERT_TRUE(BB); |
| 583 |
bool Deleted = SimplifyInstructionsInBlock(BB); |
| 584 |
ASSERT_TRUE(Deleted); |
| 585 |
verifyDebugValuesAreSalvaged(); |
| 586 |
} |
| 587 |
|
| 588 |
TEST(Local, ChangeToUnreachable) { |
| 589 |
LLVMContext Ctx; |
| 590 |
|
| 591 |
std::unique_ptr<Module> M = parseIR(Ctx, |
| 592 |
R"( |
| 593 |
define internal void @foo() !dbg !6 { |
| 594 |
entry: |
| 595 |
ret void, !dbg !8 |
| 596 |
} |
| 597 |
|
| 598 |
!llvm.dbg.cu = !{!0} |
| 599 |
!llvm.debugify = !{!3, !4} |
| 600 |
!llvm.module.flags = !{!5} |
| 601 |
|
| 602 |
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) |
| 603 |
!1 = !DIFile(filename: "test.ll", directory: "/") |
| 604 |
!2 = !{} |
| 605 |
!3 = !{i32 1} |
| 606 |
!4 = !{i32 0} |
| 607 |
!5 = !{i32 2, !"Debug Info Version", i32 3} |
| 608 |
!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) |
| 609 |
!7 = !DISubroutineType(types: !2) |
| 610 |
!8 = !DILocation(line: 1, column: 1, scope: !6) |
| 611 |
)"); |
| 612 |
|
| 613 |
bool BrokenDebugInfo = true; |
| 614 |
verifyModule(*M, &errs(), &BrokenDebugInfo); |
| 615 |
ASSERT_FALSE(BrokenDebugInfo); |
| 616 |
|
| 617 |
Function &F = *cast<Function>(M->getNamedValue("foo")); |
| 618 |
|
| 619 |
BasicBlock &BB = F.front(); |
| 620 |
Instruction &A = BB.front(); |
| 621 |
DebugLoc DLA = A.getDebugLoc(); |
| 622 |
|
| 623 |
ASSERT_TRUE(isa<ReturnInst>(&A)); |
| 624 |
// One instruction should be affected. |
| 625 |
EXPECT_EQ(changeToUnreachable(&A), 1U); |
| 626 |
|
| 627 |
Instruction &B = BB.front(); |
| 628 |
|
| 629 |
// There should be an uncreachable instruction. |
| 630 |
ASSERT_TRUE(isa<UnreachableInst>(&B)); |
| 631 |
|
| 632 |
DebugLoc DLB = B.getDebugLoc(); |
| 633 |
EXPECT_EQ(DLA, DLB); |
| 634 |
} |
| 635 |
|
| 636 |
TEST(Local, FindDbgUsers) { |
| 637 |
LLVMContext Ctx; |
| 638 |
std::unique_ptr<Module> M = parseIR(Ctx, |
| 639 |
R"( |
| 640 |
define dso_local void @fun(ptr %a) #0 !dbg !11 { |
| 641 |
entry: |
| 642 |
#dbg_assign(ptr %a, !16, !DIExpression(), !15, ptr %a, !DIExpression(), !19) |
| 643 |
ret void |
| 644 |
} |
| 645 |
|
| 646 |
!llvm.dbg.cu = !{!0} |
| 647 |
!llvm.module.flags = !{!2, !3, !9} |
| 648 |
!llvm.ident = !{!10} |
| 649 |
|
| 650 |
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 17.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) |
| 651 |
!1 = !DIFile(filename: "test.cpp", directory: "/") |
| 652 |
!2 = !{i32 7, !"Dwarf Version", i32 5} |
| 653 |
!3 = !{i32 2, !"Debug Info Version", i32 3} |
| 654 |
!4 = !{i32 1, !"wchar_size", i32 4} |
| 655 |
!9 = !{i32 7, !"debug-info-assignment-tracking", i1 true} |
| 656 |
!10 = !{!"clang version 17.0.0"} |
| 657 |
!11 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 1, type: !12, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14) |
| 658 |
!12 = !DISubroutineType(types: !13) |
| 659 |
!13 = !{null} |
| 660 |
!14 = !{} |
| 661 |
!15 = distinct !DIAssignID() |
| 662 |
!16 = !DILocalVariable(name: "x", scope: !11, file: !1, line: 2, type: !17) |
| 663 |
!17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !18, size: 64) |
| 664 |
!18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) |
| 665 |
!19 = !DILocation(line: 0, scope: !11) |
| 666 |
)"); |
| 667 |
|
| 668 |
bool BrokenDebugInfo = true; |
| 669 |
verifyModule(*M, &errs(), &BrokenDebugInfo); |
| 670 |
ASSERT_FALSE(BrokenDebugInfo); |
| 671 |
|
| 672 |
// Convert to debug intrinsics as we want to test findDbgUsers and |
| 673 |
// findDbgValue's debug-intrinsic-finding code here. |
| 674 |
// TODO: Remove this test when debug intrinsics are removed. |
| 675 |
M->convertFromNewDbgValues(); |
| 676 |
|
| 677 |
Function &Fun = *cast<Function>(M->getNamedValue("fun")); |
| 678 |
Value *Arg = Fun.getArg(0); |
| 679 |
SmallVector<DbgVariableIntrinsic *> Users; |
| 680 |
// Arg (%a) is used twice by a single dbg.assign. Check findDbgUsers returns |
| 681 |
// only 1 pointer to it rather than 2. |
| 682 |
findDbgUsers(Users, Arg); |
| 683 |
EXPECT_EQ(Users.size(), 1u); |
| 684 |
|
| 685 |
SmallVector<DbgValueInst *> Vals; |
| 686 |
// Arg (%a) is used twice by a single dbg.assign. Check findDbgValues returns |
| 687 |
// only 1 pointer to it rather than 2. |
| 688 |
findDbgValues(Vals, Arg); |
| 689 |
EXPECT_EQ(Vals.size(), 1u); |
| 690 |
} |
| 691 |
|
| 692 |
TEST(Local, FindDbgRecords) { |
| 693 |
// DbgRecord copy of the FindDbgUsers test above. |
| 694 |
LLVMContext Ctx; |
| 695 |
std::unique_ptr<Module> M = parseIR(Ctx, |
| 696 |
R"( |
| 697 |
define dso_local void @fun(ptr %a) #0 !dbg !11 { |
| 698 |
entry: |
| 699 |
#dbg_assign(ptr %a, !16, !DIExpression(), !15, ptr %a, !DIExpression(), !19) |
| 700 |
ret void |
| 701 |
} |
| 702 |
|
| 703 |
!llvm.dbg.cu = !{!0} |
| 704 |
!llvm.module.flags = !{!2, !3, !9} |
| 705 |
!llvm.ident = !{!10} |
| 706 |
|
| 707 |
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 17.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) |
| 708 |
!1 = !DIFile(filename: "test.cpp", directory: "/") |
| 709 |
!2 = !{i32 7, !"Dwarf Version", i32 5} |
| 710 |
!3 = !{i32 2, !"Debug Info Version", i32 3} |
| 711 |
!4 = !{i32 1, !"wchar_size", i32 4} |
| 712 |
!9 = !{i32 7, !"debug-info-assignment-tracking", i1 true} |
| 713 |
!10 = !{!"clang version 17.0.0"} |
| 714 |
!11 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 1, type: !12, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14) |
| 715 |
!12 = !DISubroutineType(types: !13) |
| 716 |
!13 = !{null} |
| 717 |
!14 = !{} |
| 718 |
!15 = distinct !DIAssignID() |
| 719 |
!16 = !DILocalVariable(name: "x", scope: !11, file: !1, line: 2, type: !17) |
| 720 |
!17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !18, size: 64) |
| 721 |
!18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) |
| 722 |
!19 = !DILocation(line: 0, scope: !11) |
| 723 |
)"); |
| 724 |
|
| 725 |
bool BrokenDebugInfo = true; |
| 726 |
verifyModule(*M, &errs(), &BrokenDebugInfo); |
| 727 |
ASSERT_FALSE(BrokenDebugInfo); |
| 728 |
|
| 729 |
Function &Fun = *cast<Function>(M->getNamedValue("fun")); |
| 730 |
Value *Arg = Fun.getArg(0); |
| 731 |
|
| 732 |
SmallVector<DbgVariableIntrinsic *> Users; |
| 733 |
SmallVector<DbgVariableRecord *> Records; |
| 734 |
// Arg (%a) is used twice by a single dbg_assign. Check findDbgUsers returns |
| 735 |
// only 1 pointer to it rather than 2. |
| 736 |
findDbgUsers(Users, Arg, &Records); |
| 737 |
EXPECT_EQ(Users.size(), 0u); |
| 738 |
EXPECT_EQ(Records.size(), 1u); |
| 739 |
|
| 740 |
SmallVector<DbgValueInst *> Vals; |
| 741 |
Records.clear(); |
| 742 |
// Arg (%a) is used twice by a single dbg_assign. Check findDbgValues returns |
| 743 |
// only 1 pointer to it rather than 2. |
| 744 |
findDbgValues(Vals, Arg, &Records); |
| 745 |
EXPECT_EQ(Vals.size(), 0u); |
| 746 |
EXPECT_EQ(Records.size(), 1u); |
| 747 |
} |
| 748 |
|
| 749 |
TEST(Local, ReplaceAllDbgUsesWith) { |
| 750 |
using namespace llvm::dwarf; |
| 751 |
LLVMContext Ctx; |
| 752 |
|
| 753 |
// Note: The datalayout simulates Darwin/x86_64. |
| 754 |
std::unique_ptr<Module> M = parseIR(Ctx, |
| 755 |
R"( |
| 756 |
target datalayout = "e-m:o-i63:64-f80:128-n8:16:32:64-S128" |
| 757 |
|
| 758 |
declare i32 @escape(i32) |
| 759 |
|
| 760 |
define void @f() !dbg !6 { |
| 761 |
entry: |
| 762 |
%a = add i32 0, 1, !dbg !15 |
| 763 |
|
| 764 |
#dbg_value(i32 %a, !9, !DIExpression(), !15) |
| 765 |
%b = add i64 0, 1, !dbg !16 |
| 766 |
|
| 767 |
#dbg_value(i64 %b, !11, !DIExpression(), !16) |
| 768 |
#dbg_value(i64 %b, !11, !DIExpression(DW_OP_lit0, DW_OP_mul), !16) |
| 769 |
#dbg_value(i64 %b, !11, !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value), !16) |
| 770 |
#dbg_value(i64 %b, !11, !DIExpression(DW_OP_LLVM_fragment, 0, 8), !16) |
| 771 |
#dbg_value(i64 %b, !11, !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_fragment, 0, 8), !16) |
| 772 |
#dbg_value(i64 %b, !11, !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8), !16) |
| 773 |
%c = inttoptr i64 0 to ptr, !dbg !17 |
| 774 |
|
| 775 |
#dbg_declare(ptr %c, !13, !DIExpression(), !17) |
| 776 |
%d = inttoptr i64 0 to ptr, !dbg !18 |
| 777 |
|
| 778 |
#dbg_declare(ptr %d, !20, !DIExpression(), !18) |
| 779 |
%e = add <2 x i16> zeroinitializer, zeroinitializer |
| 780 |
|
| 781 |
#dbg_value(<2 x i16> %e, !14, !DIExpression(), !18) |
| 782 |
%f = call i32 @escape(i32 0) |
| 783 |
|
| 784 |
#dbg_value(i32 %f, !9, !DIExpression(), !15) |
| 785 |
%barrier = call i32 @escape(i32 0) |
| 786 |
|
| 787 |
%g = call i32 @escape(i32 %f) |
| 788 |
|
| 789 |
#dbg_value(i32 %g, !9, !DIExpression(), !15) |
| 790 |
ret void, !dbg !19 |
| 791 |
} |
| 792 |
|
| 793 |
!llvm.dbg.cu = !{!0} |
| 794 |
!llvm.module.flags = !{!5} |
| 795 |
|
| 796 |
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) |
| 797 |
!1 = !DIFile(filename: "/Users/vsk/Desktop/foo.ll", directory: "/") |
| 798 |
!2 = !{} |
| 799 |
!5 = !{i32 2, !"Debug Info Version", i32 3} |
| 800 |
!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) |
| 801 |
!7 = !DISubroutineType(types: !2) |
| 802 |
!8 = !{!9, !11, !13, !14} |
| 803 |
!9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10) |
| 804 |
!10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_signed) |
| 805 |
!11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12) |
| 806 |
!12 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_signed) |
| 807 |
!13 = !DILocalVariable(name: "3", scope: !6, file: !1, line: 3, type: !12) |
| 808 |
!14 = !DILocalVariable(name: "4", scope: !6, file: !1, line: 4, type: !10) |
| 809 |
!15 = !DILocation(line: 1, column: 1, scope: !6) |
| 810 |
!16 = !DILocation(line: 2, column: 1, scope: !6) |
| 811 |
!17 = !DILocation(line: 3, column: 1, scope: !6) |
| 812 |
!18 = !DILocation(line: 4, column: 1, scope: !6) |
| 813 |
!19 = !DILocation(line: 5, column: 1, scope: !6) |
| 814 |
!20 = !DILocalVariable(name: "5", scope: !6, file: !1, line: 5, type: !10) |
| 815 |
)"); |
| 816 |
|
| 817 |
bool BrokenDebugInfo = true; |
| 818 |
verifyModule(*M, &errs(), &BrokenDebugInfo); |
| 819 |
ASSERT_FALSE(BrokenDebugInfo); |
| 820 |
|
| 821 |
Function &F = *cast<Function>(M->getNamedValue("f")); |
| 822 |
DominatorTree DT{F}; |
| 823 |
|
| 824 |
BasicBlock &BB = F.front(); |
| 825 |
Instruction &A = BB.front(); |
| 826 |
Instruction &B = *A.getNextNonDebugInstruction(); |
| 827 |
Instruction &C = *B.getNextNonDebugInstruction(); |
| 828 |
Instruction &D = *C.getNextNonDebugInstruction(); |
| 829 |
Instruction &E = *D.getNextNonDebugInstruction(); |
| 830 |
Instruction &F_ = *E.getNextNonDebugInstruction(); |
| 831 |
Instruction &Barrier = *F_.getNextNonDebugInstruction(); |
| 832 |
Instruction &G = *Barrier.getNextNonDebugInstruction(); |
| 833 |
|
| 834 |
// Simulate i32 <-> i64* conversion. Expect no updates: the datalayout says |
| 835 |
// pointers are 64 bits, so the conversion would be lossy. |
| 836 |
EXPECT_FALSE(replaceAllDbgUsesWith(A, C, C, DT)); |
| 837 |
EXPECT_FALSE(replaceAllDbgUsesWith(C, A, A, DT)); |
| 838 |
|
| 839 |
// Simulate i32 <-> <2 x i16> conversion. This is unsupported. |
| 840 |
EXPECT_FALSE(replaceAllDbgUsesWith(E, A, A, DT)); |
| 841 |
EXPECT_FALSE(replaceAllDbgUsesWith(A, E, E, DT)); |
| 842 |
|
| 843 |
// Simulate i32* <-> i64* conversion. |
| 844 |
EXPECT_TRUE(replaceAllDbgUsesWith(D, C, C, DT)); |
| 845 |
|
| 846 |
SmallVector<DbgVariableIntrinsic *, 2> CDbgVals; |
| 847 |
SmallVector<DbgVariableRecord *, 2> CDbgRecords; |
| 848 |
findDbgUsers(CDbgVals, &C, &CDbgRecords); |
| 849 |
EXPECT_EQ(0U, CDbgVals.size()); |
| 850 |
EXPECT_EQ(2U, CDbgRecords.size()); |
| 851 |
EXPECT_TRUE(all_of( |
| 852 |
CDbgRecords, [](DbgVariableRecord *DVR) { return DVR->isDbgDeclare(); })); |
| 853 |
|
| 854 |
EXPECT_TRUE(replaceAllDbgUsesWith(C, D, D, DT)); |
| 855 |
|
| 856 |
SmallVector<DbgVariableIntrinsic *, 2> DDbgVals; |
| 857 |
SmallVector<DbgVariableRecord *, 2> DDbgRecords; |
| 858 |
findDbgUsers(DDbgVals, &D, &DDbgRecords); |
| 859 |
EXPECT_EQ(0U, DDbgVals.size()); |
| 860 |
EXPECT_EQ(2U, DDbgRecords.size()); |
| 861 |
EXPECT_TRUE(all_of( |
| 862 |
DDbgRecords, [](DbgVariableRecord *DVR) { return DVR->isDbgDeclare(); })); |
| 863 |
|
| 864 |
// Introduce a use-before-def. Check that the dbg.value for %a is salvaged. |
| 865 |
EXPECT_TRUE(replaceAllDbgUsesWith(A, F_, F_, DT)); |
| 866 |
|
| 867 |
EXPECT_FALSE(A.hasDbgRecords()); |
| 868 |
EXPECT_TRUE(B.hasDbgRecords()); |
| 869 |
DbgVariableRecord *BDbgVal = |
| 870 |
cast<DbgVariableRecord>(&*B.getDbgRecordRange().begin()); |
| 871 |
EXPECT_EQ(BDbgVal->getNumVariableLocationOps(), 1u); |
| 872 |
EXPECT_EQ(ConstantInt::get(A.getType(), 0), |
| 873 |
BDbgVal->getVariableLocationOp(0)); |
| 874 |
|
| 875 |
// Introduce a use-before-def. Check that the dbg.values for %f become undef. |
| 876 |
EXPECT_TRUE(replaceAllDbgUsesWith(F_, G, G, DT)); |
| 877 |
|
| 878 |
DbgVariableRecord *BarrierDbgVal = |
| 879 |
cast<DbgVariableRecord>(&*Barrier.getDbgRecordRange().begin()); |
| 880 |
EXPECT_EQ(BarrierDbgVal->getNumVariableLocationOps(), 1u); |
| 881 |
EXPECT_TRUE(BarrierDbgVal->isKillLocation()); |
| 882 |
|
| 883 |
SmallVector<DbgValueInst *, 1> BarrierDbgVals; |
| 884 |
SmallVector<DbgVariableRecord *, 8> BarrierDbgRecs; |
| 885 |
findDbgValues(BarrierDbgVals, &F_, &BarrierDbgRecs); |
| 886 |
EXPECT_EQ(0U, BarrierDbgVals.size()); |
| 887 |
EXPECT_EQ(0U, BarrierDbgRecs.size()); |
| 888 |
|
| 889 |
// Simulate i32 -> i64 conversion to test sign-extension. Here are some |
| 890 |
// interesting cases to handle: |
| 891 |
// 1) debug user has empty DIExpression |
| 892 |
// 2) debug user has non-empty, non-stack-value'd DIExpression |
| 893 |
// 3) debug user has non-empty, stack-value'd DIExpression |
| 894 |
// 4-6) like (1-3), but with a fragment |
| 895 |
EXPECT_TRUE(replaceAllDbgUsesWith(B, A, A, DT)); |
| 896 |
|
| 897 |
SmallVector<DbgValueInst *, 8> BDbgVals; |
| 898 |
SmallVector<DbgVariableRecord *, 8> BDbgRecs; |
| 899 |
findDbgValues(BDbgVals, &A, &BDbgRecs); |
| 900 |
EXPECT_EQ(0U, BDbgVals.size()); |
| 901 |
EXPECT_EQ(6U, BDbgRecs.size()); |
| 902 |
|
| 903 |
// Check that %a has a dbg.value with a DIExpression matching \p Ops. |
| 904 |
auto hasADbgVal = [&](ArrayRef<uint64_t> Ops) { |
| 905 |
return any_of(BDbgRecs, [&](DbgVariableRecord *DVI) { |
| 906 |
assert(DVI->getVariable()->getName() == "2"); |
| 907 |
return DVI->getExpression()->getElements() == Ops; |
| 908 |
}); |
| 909 |
}; |
| 910 |
|
| 911 |
// Case 1: The original expr is empty, so no deref is needed. |
| 912 |
EXPECT_TRUE(hasADbgVal({DW_OP_LLVM_convert, 32, DW_ATE_signed, |
| 913 |
DW_OP_LLVM_convert, 64, DW_ATE_signed, |
| 914 |
DW_OP_stack_value})); |
| 915 |
|
| 916 |
// Case 2: Perform an address calculation with the original expr, deref it, |
| 917 |
// then sign-extend the result. |
| 918 |
EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref, |
| 919 |
DW_OP_LLVM_convert, 32, DW_ATE_signed, |
| 920 |
DW_OP_LLVM_convert, 64, DW_ATE_signed, |
| 921 |
DW_OP_stack_value})); |
| 922 |
|
| 923 |
// Case 3: Insert the sign-extension logic before the DW_OP_stack_value. |
| 924 |
EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_convert, 32, |
| 925 |
DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, |
| 926 |
DW_OP_stack_value})); |
| 927 |
|
| 928 |
// Cases 4-6: Just like cases 1-3, but preserve the fragment at the end. |
| 929 |
EXPECT_TRUE(hasADbgVal({DW_OP_LLVM_convert, 32, DW_ATE_signed, |
| 930 |
DW_OP_LLVM_convert, 64, DW_ATE_signed, |
| 931 |
DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); |
| 932 |
|
| 933 |
EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref, |
| 934 |
DW_OP_LLVM_convert, 32, DW_ATE_signed, |
| 935 |
DW_OP_LLVM_convert, 64, DW_ATE_signed, |
| 936 |
DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); |
| 937 |
|
| 938 |
EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_convert, 32, |
| 939 |
DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, |
| 940 |
DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); |
| 941 |
|
| 942 |
verifyModule(*M, &errs(), &BrokenDebugInfo); |
| 943 |
ASSERT_FALSE(BrokenDebugInfo); |
| 944 |
} |
| 945 |
|
| 946 |
TEST(Local, RemoveUnreachableBlocks) { |
| 947 |
LLVMContext C; |
| 948 |
|
| 949 |
std::unique_ptr<Module> M = parseIR(C, |
| 950 |
R"( |
| 951 |
define void @br_simple() { |
| 952 |
entry: |
| 953 |
br label %bb0 |
| 954 |
bb0: |
| 955 |
ret void |
| 956 |
bb1: |
| 957 |
ret void |
| 958 |
} |
| 959 |
|
| 960 |
define void @br_self_loop() { |
| 961 |
entry: |
| 962 |
br label %bb0 |
| 963 |
bb0: |
| 964 |
br i1 true, label %bb1, label %bb0 |
| 965 |
bb1: |
| 966 |
br i1 true, label %bb0, label %bb2 |
| 967 |
bb2: |
| 968 |
br label %bb2 |
| 969 |
} |
| 970 |
|
| 971 |
define void @br_constant() { |
| 972 |
entry: |
| 973 |
br label %bb0 |
| 974 |
bb0: |
| 975 |
br i1 true, label %bb1, label %bb2 |
| 976 |
bb1: |
| 977 |
br i1 true, label %bb0, label %bb2 |
| 978 |
bb2: |
| 979 |
br label %bb2 |
| 980 |
} |
| 981 |
|
| 982 |
define void @br_loop() { |
| 983 |
entry: |
| 984 |
br label %bb0 |
| 985 |
bb0: |
| 986 |
br label %bb0 |
| 987 |
bb1: |
| 988 |
br label %bb2 |
| 989 |
bb2: |
| 990 |
br label %bb1 |
| 991 |
} |
| 992 |
|
| 993 |
declare i32 @__gxx_personality_v0(...) |
| 994 |
|
| 995 |
define void @invoke_terminator() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { |
| 996 |
entry: |
| 997 |
br i1 undef, label %invoke.block, label %exit |
| 998 |
|
| 999 |
invoke.block: |
| 1000 |
%cond = invoke zeroext i1 @invokable() |
| 1001 |
to label %continue.block unwind label %lpad.block |
| 1002 |
|
| 1003 |
continue.block: |
| 1004 |
br i1 %cond, label %if.then, label %if.end |
| 1005 |
|
| 1006 |
if.then: |
| 1007 |
unreachable |
| 1008 |
|
| 1009 |
if.end: |
| 1010 |
unreachable |
| 1011 |
|
| 1012 |
lpad.block: |
| 1013 |
%lp = landingpad { i8*, i32 } |
| 1014 |
catch i8* null |
| 1015 |
br label %exit |
| 1016 |
|
| 1017 |
exit: |
| 1018 |
ret void |
| 1019 |
} |
| 1020 |
|
| 1021 |
declare i1 @invokable() |
| 1022 |
)"); |
| 1023 |
|
| 1024 |
auto runEager = [&](Function &F, DominatorTree *DT) { |
| 1025 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 1026 |
DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager); |
| 1027 |
removeUnreachableBlocks(F, &DTU); |
| 1028 |
EXPECT_TRUE(DTU.getDomTree().verify()); |
| 1029 |
EXPECT_TRUE(DTU.getPostDomTree().verify()); |
| 1030 |
}; |
| 1031 |
|
| 1032 |
auto runLazy = [&](Function &F, DominatorTree *DT) { |
| 1033 |
PostDominatorTree PDT = PostDominatorTree(F); |
| 1034 |
DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 1035 |
removeUnreachableBlocks(F, &DTU); |
| 1036 |
EXPECT_TRUE(DTU.getDomTree().verify()); |
| 1037 |
EXPECT_TRUE(DTU.getPostDomTree().verify()); |
| 1038 |
}; |
| 1039 |
|
| 1040 |
// Test removeUnreachableBlocks under Eager UpdateStrategy. |
| 1041 |
runWithDomTree(*M, "br_simple", runEager); |
| 1042 |
runWithDomTree(*M, "br_self_loop", runEager); |
| 1043 |
runWithDomTree(*M, "br_constant", runEager); |
| 1044 |
runWithDomTree(*M, "br_loop", runEager); |
| 1045 |
runWithDomTree(*M, "invoke_terminator", runEager); |
| 1046 |
|
| 1047 |
// Test removeUnreachableBlocks under Lazy UpdateStrategy. |
| 1048 |
runWithDomTree(*M, "br_simple", runLazy); |
| 1049 |
runWithDomTree(*M, "br_self_loop", runLazy); |
| 1050 |
runWithDomTree(*M, "br_constant", runLazy); |
| 1051 |
runWithDomTree(*M, "br_loop", runLazy); |
| 1052 |
runWithDomTree(*M, "invoke_terminator", runLazy); |
| 1053 |
|
| 1054 |
M = parseIR(C, |
| 1055 |
R"( |
| 1056 |
define void @f() { |
| 1057 |
entry: |
| 1058 |
ret void |
| 1059 |
bb0: |
| 1060 |
ret void |
| 1061 |
} |
| 1062 |
)"); |
| 1063 |
|
| 1064 |
auto checkRUBlocksRetVal = [&](Function &F, DominatorTree *DT) { |
| 1065 |
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); |
| 1066 |
EXPECT_TRUE(removeUnreachableBlocks(F, &DTU)); |
| 1067 |
EXPECT_FALSE(removeUnreachableBlocks(F, &DTU)); |
| 1068 |
EXPECT_TRUE(DTU.getDomTree().verify()); |
| 1069 |
}; |
| 1070 |
|
| 1071 |
runWithDomTree(*M, "f", checkRUBlocksRetVal); |
| 1072 |
} |
| 1073 |
|
| 1074 |
TEST(Local, SimplifyCFGWithNullAC) { |
| 1075 |
LLVMContext Ctx; |
| 1076 |
|
| 1077 |
std::unique_ptr<Module> M = parseIR(Ctx, R"( |
| 1078 |
declare void @true_path() |
| 1079 |
declare void @false_path() |
| 1080 |
declare void @llvm.assume(i1 %cond); |
| 1081 |
|
| 1082 |
define i32 @foo(i1, i32) { |
| 1083 |
entry: |
| 1084 |
%cmp = icmp sgt i32 %1, 0 |
| 1085 |
br i1 %cmp, label %if.bb1, label %then.bb1 |
| 1086 |
if.bb1: |
| 1087 |
call void @true_path() |
| 1088 |
br label %test.bb |
| 1089 |
then.bb1: |
| 1090 |
call void @false_path() |
| 1091 |
br label %test.bb |
| 1092 |
test.bb: |
| 1093 |
%phi = phi i1 [1, %if.bb1], [%0, %then.bb1] |
| 1094 |
call void @llvm.assume(i1 %0) |
| 1095 |
br i1 %phi, label %if.bb2, label %then.bb2 |
| 1096 |
if.bb2: |
| 1097 |
ret i32 %1 |
| 1098 |
then.bb2: |
| 1099 |
ret i32 0 |
| 1100 |
} |
| 1101 |
)"); |
| 1102 |
|
| 1103 |
Function &F = *cast<Function>(M->getNamedValue("foo")); |
| 1104 |
TargetTransformInfo TTI(M->getDataLayout()); |
| 1105 |
|
| 1106 |
SimplifyCFGOptions Options{}; |
| 1107 |
Options.setAssumptionCache(nullptr); |
| 1108 |
|
| 1109 |
// Obtain BasicBlock of interest to this test, %test.bb. |
| 1110 |
BasicBlock *TestBB = nullptr; |
| 1111 |
for (BasicBlock &BB : F) { |
| 1112 |
if (BB.getName() == "test.bb") { |
| 1113 |
TestBB = &BB; |
| 1114 |
break; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
ASSERT_TRUE(TestBB); |
| 1118 |
|
| 1119 |
DominatorTree DT(F); |
| 1120 |
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); |
| 1121 |
|
| 1122 |
// %test.bb is expected to be simplified by FoldCondBranchOnPHI. |
| 1123 |
EXPECT_TRUE(simplifyCFG(TestBB, TTI, |
| 1124 |
RequireAndPreserveDomTree ? &DTU : nullptr, Options)); |
| 1125 |
} |
| 1126 |
|
| 1127 |
TEST(Local, CanReplaceOperandWithVariable) { |
| 1128 |
LLVMContext Ctx; |
| 1129 |
Module M("test_module", Ctx); |
| 1130 |
IRBuilder<> B(Ctx); |
| 1131 |
|
| 1132 |
FunctionType *FnType = |
| 1133 |
FunctionType::get(Type::getVoidTy(Ctx), {}, false); |
| 1134 |
|
| 1135 |
FunctionType *VarArgFnType = |
| 1136 |
FunctionType::get(Type::getVoidTy(Ctx), {B.getInt32Ty()}, true); |
| 1137 |
|
| 1138 |
Function *TestBody = Function::Create(FnType, GlobalValue::ExternalLinkage, |
| 1139 |
0, "", &M); |
| 1140 |
|
| 1141 |
BasicBlock *BB0 = BasicBlock::Create(Ctx, "", TestBody); |
| 1142 |
B.SetInsertPoint(BB0); |
| 1143 |
|
| 1144 |
FunctionCallee Intrin = M.getOrInsertFunction("llvm.foo", FnType); |
| 1145 |
FunctionCallee Func = M.getOrInsertFunction("foo", FnType); |
| 1146 |
FunctionCallee VarArgFunc |
| 1147 |
= M.getOrInsertFunction("foo.vararg", VarArgFnType); |
| 1148 |
FunctionCallee VarArgIntrin |
| 1149 |
= M.getOrInsertFunction("llvm.foo.vararg", VarArgFnType); |
| 1150 |
|
| 1151 |
auto *CallToIntrin = B.CreateCall(Intrin); |
| 1152 |
auto *CallToFunc = B.CreateCall(Func); |
| 1153 |
|
| 1154 |
// Test if it's valid to replace the callee operand. |
| 1155 |
EXPECT_FALSE(canReplaceOperandWithVariable(CallToIntrin, 0)); |
| 1156 |
EXPECT_TRUE(canReplaceOperandWithVariable(CallToFunc, 0)); |
| 1157 |
|
| 1158 |
// That it's invalid to replace an argument in the variadic argument list for |
| 1159 |
// an intrinsic, but OK for a normal function. |
| 1160 |
auto *CallToVarArgFunc = B.CreateCall( |
| 1161 |
VarArgFunc, {B.getInt32(0), B.getInt32(1), B.getInt32(2)}); |
| 1162 |
EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 0)); |
| 1163 |
EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 1)); |
| 1164 |
EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 2)); |
| 1165 |
EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 3)); |
| 1166 |
|
| 1167 |
auto *CallToVarArgIntrin = B.CreateCall( |
| 1168 |
VarArgIntrin, {B.getInt32(0), B.getInt32(1), B.getInt32(2)}); |
| 1169 |
EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgIntrin, 0)); |
| 1170 |
EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 1)); |
| 1171 |
EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 2)); |
| 1172 |
EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 3)); |
| 1173 |
|
| 1174 |
// Test that it's invalid to replace gcroot operands, even though it can't use |
| 1175 |
// immarg. |
| 1176 |
Type *PtrPtr = B.getPtrTy(0); |
| 1177 |
Value *Alloca = B.CreateAlloca(PtrPtr, (unsigned)0); |
| 1178 |
CallInst *GCRoot = B.CreateIntrinsic( |
| 1179 |
Intrinsic::gcroot, {Alloca, Constant::getNullValue(PtrPtr)}); |
| 1180 |
EXPECT_TRUE(canReplaceOperandWithVariable(GCRoot, 0)); // Alloca |
| 1181 |
EXPECT_FALSE(canReplaceOperandWithVariable(GCRoot, 1)); |
| 1182 |
EXPECT_FALSE(canReplaceOperandWithVariable(GCRoot, 2)); |
| 1183 |
|
| 1184 |
BB0->dropAllReferences(); |
| 1185 |
} |
| 1186 |
|
| 1187 |
TEST(Local, ExpressionForConstant) { |
| 1188 |
LLVMContext Context; |
| 1189 |
Module M("test_module", Context); |
| 1190 |
DIBuilder DIB(M); |
| 1191 |
DIExpression *Expr = nullptr; |
| 1192 |
|
| 1193 |
auto createExpression = [&](Constant *C, Type *Ty) -> DIExpression * { |
| 1194 |
EXPECT_NE(C, nullptr); |
| 1195 |
EXPECT_NE(Ty, nullptr); |
| 1196 |
EXPECT_EQ(C->getType(), Ty); |
| 1197 |
std::unique_ptr<GlobalVariable> GV = std::make_unique<GlobalVariable>( |
| 1198 |
Ty, false, GlobalValue::ExternalLinkage, C, "GV"); |
| 1199 |
EXPECT_NE(GV, nullptr); |
| 1200 |
|
| 1201 |
DIExpression *Expr = getExpressionForConstant(DIB, *GV->getInitializer(), |
| 1202 |
*GV->getValueType()); |
| 1203 |
if (Expr) { |
| 1204 |
EXPECT_EQ(Expr->getNumElements(), 3u); |
| 1205 |
EXPECT_EQ(Expr->getElement(0), dwarf::DW_OP_constu); |
| 1206 |
EXPECT_EQ(Expr->getElement(2), dwarf::DW_OP_stack_value); |
| 1207 |
} |
| 1208 |
return Expr; |
| 1209 |
}; |
| 1210 |
|
| 1211 |
// Integer. |
| 1212 |
IntegerType *Int1Ty = Type::getInt1Ty(Context); |
| 1213 |
Expr = createExpression(ConstantInt::getTrue(Context), Int1Ty); |
| 1214 |
EXPECT_NE(Expr, nullptr); |
| 1215 |
EXPECT_EQ(Expr->getElement(1), 18446744073709551615U); |
| 1216 |
|
| 1217 |
Expr = createExpression(ConstantInt::getFalse(Context), Int1Ty); |
| 1218 |
EXPECT_NE(Expr, nullptr); |
| 1219 |
EXPECT_EQ(Expr->getElement(1), 0U); |
| 1220 |
|
| 1221 |
IntegerType *Int8Ty = Type::getInt8Ty(Context); |
| 1222 |
Expr = createExpression(ConstantInt::get(Int8Ty, 100), Int8Ty); |
| 1223 |
EXPECT_NE(Expr, nullptr); |
| 1224 |
EXPECT_EQ(Expr->getElement(1), 100U); |
| 1225 |
|
| 1226 |
IntegerType *Int16Ty = Type::getInt16Ty(Context); |
| 1227 |
Expr = createExpression(ConstantInt::getSigned(Int16Ty, -50), Int16Ty); |
| 1228 |
EXPECT_NE(Expr, nullptr); |
| 1229 |
EXPECT_EQ(Expr->getElement(1), -50ULL); |
| 1230 |
|
| 1231 |
IntegerType *Int32Ty = Type::getInt32Ty(Context); |
| 1232 |
Expr = createExpression(ConstantInt::get(Int32Ty, 0x7FFFFFFF), Int32Ty); |
| 1233 |
EXPECT_NE(Expr, nullptr); |
| 1234 |
EXPECT_EQ(Expr->getElement(1), 0x7FFFFFFFU); |
| 1235 |
|
| 1236 |
IntegerType *Int64Ty = Type::getInt64Ty(Context); |
| 1237 |
Expr = |
| 1238 |
createExpression(ConstantInt::get(Int64Ty, 0x7FFFFFFFFFFFFFFF), Int64Ty); |
| 1239 |
EXPECT_NE(Expr, nullptr); |
| 1240 |
EXPECT_EQ(Expr->getElement(1), 0x7FFFFFFFFFFFFFFFU); |
| 1241 |
|
| 1242 |
IntegerType *Int128Ty = Type::getInt128Ty(Context); |
| 1243 |
Expr = createExpression(ConstantInt::get(Int128Ty, 0x7FFFFFFFFFFFFFFF), |
| 1244 |
Int128Ty); |
| 1245 |
EXPECT_NE(Expr, nullptr); |
| 1246 |
EXPECT_EQ(Expr->getElement(1), 0x7FFFFFFFFFFFFFFFU); |
| 1247 |
|
| 1248 |
GlobalVariable *String = |
| 1249 |
IRBuilder<>(Context).CreateGlobalString("hello", "hello", 0, &M); |
| 1250 |
Expr = createExpression(ConstantExpr::getPtrToInt(String, Int32Ty), Int32Ty); |
| 1251 |
EXPECT_EQ(Expr, nullptr); |
| 1252 |
|
| 1253 |
// Float. |
| 1254 |
Type *FloatTy = Type::getFloatTy(Context); |
| 1255 |
Expr = createExpression(ConstantFP::get(FloatTy, 5.55), FloatTy); |
| 1256 |
EXPECT_NE(Expr, nullptr); |
| 1257 |
EXPECT_EQ(Expr->getElement(1), 1085381018U); |
| 1258 |
|
| 1259 |
// Double. |
| 1260 |
Type *DoubleTy = Type::getDoubleTy(Context); |
| 1261 |
Expr = createExpression(ConstantFP::get(DoubleTy, -5.55), DoubleTy); |
| 1262 |
EXPECT_NE(Expr, nullptr); |
| 1263 |
EXPECT_EQ(Expr->getElement(1), 13841306799765140275U); |
| 1264 |
|
| 1265 |
// Half. |
| 1266 |
Type *HalfTy = Type::getHalfTy(Context); |
| 1267 |
Expr = createExpression(ConstantFP::get(HalfTy, 5.55), HalfTy); |
| 1268 |
EXPECT_NE(Expr, nullptr); |
| 1269 |
EXPECT_EQ(Expr->getElement(1), 17805U); |
| 1270 |
|
| 1271 |
// BFloat. |
| 1272 |
Type *BFloatTy = Type::getBFloatTy(Context); |
| 1273 |
Expr = createExpression(ConstantFP::get(BFloatTy, -5.55), BFloatTy); |
| 1274 |
EXPECT_NE(Expr, nullptr); |
| 1275 |
EXPECT_EQ(Expr->getElement(1), 49330U); |
| 1276 |
|
| 1277 |
// Pointer. |
| 1278 |
PointerType *PtrTy = PointerType::get(Context, 0); |
| 1279 |
Expr = createExpression(ConstantPointerNull::get(PtrTy), PtrTy); |
| 1280 |
EXPECT_NE(Expr, nullptr); |
| 1281 |
EXPECT_EQ(Expr->getElement(1), 0U); |
| 1282 |
|
| 1283 |
ConstantInt *K1 = ConstantInt::get(Type::getInt32Ty(Context), 1234); |
| 1284 |
Expr = createExpression(ConstantExpr::getIntToPtr(K1, PtrTy), PtrTy); |
| 1285 |
EXPECT_NE(Expr, nullptr); |
| 1286 |
EXPECT_EQ(Expr->getElement(1), 1234U); |
| 1287 |
|
| 1288 |
ConstantInt *K2 = ConstantInt::get(Type::getInt64Ty(Context), 5678); |
| 1289 |
Expr = createExpression(ConstantExpr::getIntToPtr(K2, PtrTy), PtrTy); |
| 1290 |
EXPECT_NE(Expr, nullptr); |
| 1291 |
EXPECT_EQ(Expr->getElement(1), 5678U); |
| 1292 |
|
| 1293 |
Type *FP128Ty = Type::getFP128Ty(Context); |
| 1294 |
Expr = createExpression(ConstantFP::get(FP128Ty, 32), FP128Ty); |
| 1295 |
EXPECT_EQ(Expr, nullptr); |
| 1296 |
|
| 1297 |
Type *X86_FP80Ty = Type::getX86_FP80Ty(Context); |
| 1298 |
Expr = createExpression(ConstantFP::get(X86_FP80Ty, 32), X86_FP80Ty); |
| 1299 |
EXPECT_EQ(Expr, nullptr); |
| 1300 |
|
| 1301 |
Type *PPC_FP128Ty = Type::getPPC_FP128Ty(Context); |
| 1302 |
Expr = createExpression(ConstantFP::get(PPC_FP128Ty, 32), PPC_FP128Ty); |
| 1303 |
EXPECT_EQ(Expr, nullptr); |
| 1304 |
} |
| 1305 |
|
| 1306 |
TEST(Local, ReplaceDbgVariableRecord) { |
| 1307 |
LLVMContext C; |
| 1308 |
|
| 1309 |
// Test that RAUW also replaces the operands of DbgVariableRecord objects, |
| 1310 |
// i.e. non-instruction stored debugging information. |
| 1311 |
std::unique_ptr<Module> M = parseIR(C, |
| 1312 |
R"( |
| 1313 |
declare void @llvm.dbg.value(metadata, metadata, metadata) |
| 1314 |
define void @f(i32 %a) !dbg !8 { |
| 1315 |
entry: |
| 1316 |
%foo = add i32 %a, 1, !dbg !13 |
| 1317 |
%bar = add i32 %foo, 0, !dbg !13 |
| 1318 |
#dbg_value(i32 %bar, !11, !DIExpression(), !13) |
| 1319 |
ret void, !dbg !14 |
| 1320 |
} |
| 1321 |
!llvm.dbg.cu = !{!0} |
| 1322 |
!llvm.module.flags = !{!3, !4} |
| 1323 |
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) |
| 1324 |
!1 = !DIFile(filename: "t2.c", directory: "foo") |
| 1325 |
!2 = !{} |
| 1326 |
!3 = !{i32 2, !"Dwarf Version", i32 4} |
| 1327 |
!4 = !{i32 2, !"Debug Info Version", i32 3} |
| 1328 |
!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) |
| 1329 |
!9 = !DISubroutineType(types: !10) |
| 1330 |
!10 = !{null} |
| 1331 |
!11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12) |
| 1332 |
!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) |
| 1333 |
!13 = !DILocation(line: 2, column: 7, scope: !8) |
| 1334 |
!14 = !DILocation(line: 3, column: 1, scope: !8) |
| 1335 |
)"); |
| 1336 |
auto *GV = M->getNamedValue("f"); |
| 1337 |
ASSERT_TRUE(GV); |
| 1338 |
auto *F = dyn_cast<Function>(GV); |
| 1339 |
ASSERT_TRUE(F); |
| 1340 |
BasicBlock::iterator It = F->front().begin(); |
| 1341 |
Instruction *FooInst = &*It; |
| 1342 |
It = std::next(It); |
| 1343 |
Instruction *BarInst = &*It; |
| 1344 |
It = std::next(It); |
| 1345 |
Instruction *RetInst = &*It; |
| 1346 |
|
| 1347 |
// There should be a DbgVariableRecord on the return. |
| 1348 |
auto Range = RetInst->getDbgRecordRange(); |
| 1349 |
ASSERT_FALSE(Range.empty()); |
| 1350 |
DbgVariableRecord *DVR = dyn_cast<DbgVariableRecord>(&*Range.begin()); |
| 1351 |
ASSERT_NE(DVR, nullptr); |
| 1352 |
|
| 1353 |
// DVR should originally refer to %bar, |
| 1354 |
EXPECT_EQ(DVR->getVariableLocationOp(0), BarInst); |
| 1355 |
|
| 1356 |
// Now try to replace the computation of %bar with %foo -- this should cause |
| 1357 |
// the DbgVariableRecord's to have it's operand updated beneath it. |
| 1358 |
BarInst->replaceAllUsesWith(FooInst); |
| 1359 |
// Check DVR now points at %foo. |
| 1360 |
EXPECT_EQ(DVR->getVariableLocationOp(0), FooInst); |
| 1361 |
|
| 1362 |
// Teardown. |
| 1363 |
RetInst->DebugMarker->eraseFromParent(); |
| 1364 |
} |