1 //===- Local.cpp - Functions to perform local transformations -------------===// 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 // This family of functions perform various local transformations to the 10 // program. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/Utils/Local.h" 15 #include "llvm/ADT/APInt.h" 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/ADT/DenseMapInfo.h" 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/ADT/Hashing.h" 20 #include "llvm/ADT/None.h" 21 #include "llvm/ADT/Optional.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/SetVector.h" 24 #include "llvm/ADT/SmallPtrSet.h" 25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/ADT/Statistic.h" 27 #include "llvm/Analysis/AssumeBundleQueries.h" 28 #include "llvm/Analysis/ConstantFolding.h" 29 #include "llvm/Analysis/DomTreeUpdater.h" 30 #include "llvm/Analysis/EHPersonalities.h" 31 #include "llvm/Analysis/InstructionSimplify.h" 32 #include "llvm/Analysis/LazyValueInfo.h" 33 #include "llvm/Analysis/MemoryBuiltins.h" 34 #include "llvm/Analysis/MemorySSAUpdater.h" 35 #include "llvm/Analysis/TargetLibraryInfo.h" 36 #include "llvm/Analysis/ValueTracking.h" 37 #include "llvm/Analysis/VectorUtils.h" 38 #include "llvm/BinaryFormat/Dwarf.h" 39 #include "llvm/IR/Argument.h" 40 #include "llvm/IR/Attributes.h" 41 #include "llvm/IR/BasicBlock.h" 42 #include "llvm/IR/CFG.h" 43 #include "llvm/IR/Constant.h" 44 #include "llvm/IR/ConstantRange.h" 45 #include "llvm/IR/Constants.h" 46 #include "llvm/IR/DIBuilder.h" 47 #include "llvm/IR/DataLayout.h" 48 #include "llvm/IR/DebugInfoMetadata.h" 49 #include "llvm/IR/DebugLoc.h" 50 #include "llvm/IR/DerivedTypes.h" 51 #include "llvm/IR/Dominators.h" 52 #include "llvm/IR/Function.h" 53 #include "llvm/IR/GetElementPtrTypeIterator.h" 54 #include "llvm/IR/GlobalObject.h" 55 #include "llvm/IR/IRBuilder.h" 56 #include "llvm/IR/InstrTypes.h" 57 #include "llvm/IR/Instruction.h" 58 #include "llvm/IR/Instructions.h" 59 #include "llvm/IR/IntrinsicInst.h" 60 #include "llvm/IR/Intrinsics.h" 61 #include "llvm/IR/LLVMContext.h" 62 #include "llvm/IR/MDBuilder.h" 63 #include "llvm/IR/Metadata.h" 64 #include "llvm/IR/Module.h" 65 #include "llvm/IR/Operator.h" 66 #include "llvm/IR/PatternMatch.h" 67 #include "llvm/IR/PseudoProbe.h" 68 #include "llvm/IR/Type.h" 69 #include "llvm/IR/Use.h" 70 #include "llvm/IR/User.h" 71 #include "llvm/IR/Value.h" 72 #include "llvm/IR/ValueHandle.h" 73 #include "llvm/Support/Casting.h" 74 #include "llvm/Support/Debug.h" 75 #include "llvm/Support/ErrorHandling.h" 76 #include "llvm/Support/KnownBits.h" 77 #include "llvm/Support/raw_ostream.h" 78 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 79 #include "llvm/Transforms/Utils/ValueMapper.h" 80 #include <algorithm> 81 #include <cassert> 82 #include <climits> 83 #include <cstdint> 84 #include <iterator> 85 #include <map> 86 #include <utility> 87 88 using namespace llvm; 89 using namespace llvm::PatternMatch; 90 91 #define DEBUG_TYPE "local" 92 93 STATISTIC(NumRemoved, "Number of unreachable basic blocks removed"); 94 STATISTIC(NumPHICSEs, "Number of PHI's that got CSE'd"); 95 96 static cl::opt<bool> PHICSEDebugHash( 97 "phicse-debug-hash", 98 #ifdef EXPENSIVE_CHECKS 99 cl::init(true), 100 #else 101 cl::init(false), 102 #endif 103 cl::Hidden, 104 cl::desc("Perform extra assertion checking to verify that PHINodes's hash " 105 "function is well-behaved w.r.t. its isEqual predicate")); 106 107 static cl::opt<unsigned> PHICSENumPHISmallSize( 108 "phicse-num-phi-smallsize", cl::init(32), cl::Hidden, 109 cl::desc( 110 "When the basic block contains not more than this number of PHI nodes, " 111 "perform a (faster!) exhaustive search instead of set-driven one.")); 112 113 // Max recursion depth for collectBitParts used when detecting bswap and 114 // bitreverse idioms. 115 static const unsigned BitPartRecursionMaxDepth = 48; 116 117 //===----------------------------------------------------------------------===// 118 // Local constant propagation. 119 // 120 121 /// ConstantFoldTerminator - If a terminator instruction is predicated on a 122 /// constant value, convert it into an unconditional branch to the constant 123 /// destination. This is a nontrivial operation because the successors of this 124 /// basic block must have their PHI nodes updated. 125 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch 126 /// conditions and indirectbr addresses this might make dead if 127 /// DeleteDeadConditions is true. 128 bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, 129 const TargetLibraryInfo *TLI, 130 DomTreeUpdater *DTU) { 131 Instruction *T = BB->getTerminator(); 132 IRBuilder<> Builder(T); 133 134 // Branch - See if we are conditional jumping on constant 135 if (auto *BI = dyn_cast<BranchInst>(T)) { 136 if (BI->isUnconditional()) return false; // Can't optimize uncond branch 137 138 BasicBlock *Dest1 = BI->getSuccessor(0); 139 BasicBlock *Dest2 = BI->getSuccessor(1); 140 141 if (Dest2 == Dest1) { // Conditional branch to same location? 142 // This branch matches something like this: 143 // br bool %cond, label %Dest, label %Dest 144 // and changes it into: br label %Dest 145 146 // Let the basic block know that we are letting go of one copy of it. 147 assert(BI->getParent() && "Terminator not inserted in block!"); 148 Dest1->removePredecessor(BI->getParent()); 149 150 // Replace the conditional branch with an unconditional one. 151 BranchInst *NewBI = Builder.CreateBr(Dest1); 152 153 // Transfer the metadata to the new branch instruction. 154 NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg, 155 LLVMContext::MD_annotation}); 156 157 Value *Cond = BI->getCondition(); 158 BI->eraseFromParent(); 159 if (DeleteDeadConditions) 160 RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); 161 return true; 162 } 163 164 if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) { 165 // Are we branching on constant? 166 // YES. Change to unconditional branch... 167 BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2; 168 BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1; 169 170 // Let the basic block know that we are letting go of it. Based on this, 171 // it will adjust it's PHI nodes. 172 OldDest->removePredecessor(BB); 173 174 // Replace the conditional branch with an unconditional one. 175 BranchInst *NewBI = Builder.CreateBr(Destination); 176 177 // Transfer the metadata to the new branch instruction. 178 NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg, 179 LLVMContext::MD_annotation}); 180 181 BI->eraseFromParent(); 182 if (DTU) 183 DTU->applyUpdates({{DominatorTree::Delete, BB, OldDest}}); 184 return true; 185 } 186 187 return false; 188 } 189 190 if (auto *SI = dyn_cast<SwitchInst>(T)) { 191 // If we are switching on a constant, we can convert the switch to an 192 // unconditional branch. 193 auto *CI = dyn_cast<ConstantInt>(SI->getCondition()); 194 BasicBlock *DefaultDest = SI->getDefaultDest(); 195 BasicBlock *TheOnlyDest = DefaultDest; 196 197 // If the default is unreachable, ignore it when searching for TheOnlyDest. 198 if (isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()) && 199 SI->getNumCases() > 0) { 200 TheOnlyDest = SI->case_begin()->getCaseSuccessor(); 201 } 202 203 bool Changed = false; 204 205 // Figure out which case it goes to. 206 for (auto i = SI->case_begin(), e = SI->case_end(); i != e;) { 207 // Found case matching a constant operand? 208 if (i->getCaseValue() == CI) { 209 TheOnlyDest = i->getCaseSuccessor(); 210 break; 211 } 212 213 // Check to see if this branch is going to the same place as the default 214 // dest. If so, eliminate it as an explicit compare. 215 if (i->getCaseSuccessor() == DefaultDest) { 216 MDNode *MD = SI->getMetadata(LLVMContext::MD_prof); 217 unsigned NCases = SI->getNumCases(); 218 // Fold the case metadata into the default if there will be any branches 219 // left, unless the metadata doesn't match the switch. 220 if (NCases > 1 && MD && MD->getNumOperands() == 2 + NCases) { 221 // Collect branch weights into a vector. 222 SmallVector<uint32_t, 8> Weights; 223 for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e; 224 ++MD_i) { 225 auto *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i)); 226 Weights.push_back(CI->getValue().getZExtValue()); 227 } 228 // Merge weight of this case to the default weight. 229 unsigned idx = i->getCaseIndex(); 230 Weights[0] += Weights[idx+1]; 231 // Remove weight for this case. 232 std::swap(Weights[idx+1], Weights.back()); 233 Weights.pop_back(); 234 SI->setMetadata(LLVMContext::MD_prof, 235 MDBuilder(BB->getContext()). 236 createBranchWeights(Weights)); 237 } 238 // Remove this entry. 239 BasicBlock *ParentBB = SI->getParent(); 240 DefaultDest->removePredecessor(ParentBB); 241 i = SI->removeCase(i); 242 e = SI->case_end(); 243 Changed = true; 244 continue; 245 } 246 247 // Otherwise, check to see if the switch only branches to one destination. 248 // We do this by reseting "TheOnlyDest" to null when we find two non-equal 249 // destinations. 250 if (i->getCaseSuccessor() != TheOnlyDest) 251 TheOnlyDest = nullptr; 252 253 // Increment this iterator as we haven't removed the case. 254 ++i; 255 } 256 257 if (CI && !TheOnlyDest) { 258 // Branching on a constant, but not any of the cases, go to the default 259 // successor. 260 TheOnlyDest = SI->getDefaultDest(); 261 } 262 263 // If we found a single destination that we can fold the switch into, do so 264 // now. 265 if (TheOnlyDest) { 266 // Insert the new branch. 267 Builder.CreateBr(TheOnlyDest); 268 BasicBlock *BB = SI->getParent(); 269 270 SmallSet<BasicBlock *, 8> RemovedSuccessors; 271 272 // Remove entries from PHI nodes which we no longer branch to... 273 BasicBlock *SuccToKeep = TheOnlyDest; 274 for (BasicBlock *Succ : successors(SI)) { 275 if (DTU && Succ != TheOnlyDest) 276 RemovedSuccessors.insert(Succ); 277 // Found case matching a constant operand? 278 if (Succ == SuccToKeep) { 279 SuccToKeep = nullptr; // Don't modify the first branch to TheOnlyDest 280 } else { 281 Succ->removePredecessor(BB); 282 } 283 } 284 285 // Delete the old switch. 286 Value *Cond = SI->getCondition(); 287 SI->eraseFromParent(); 288 if (DeleteDeadConditions) 289 RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); 290 if (DTU) { 291 std::vector<DominatorTree::UpdateType> Updates; 292 Updates.reserve(RemovedSuccessors.size()); 293 for (auto *RemovedSuccessor : RemovedSuccessors) 294 Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor}); 295 DTU->applyUpdates(Updates); 296 } 297 return true; 298 } 299 300 if (SI->getNumCases() == 1) { 301 // Otherwise, we can fold this switch into a conditional branch 302 // instruction if it has only one non-default destination. 303 auto FirstCase = *SI->case_begin(); 304 Value *Cond = Builder.CreateICmpEQ(SI->getCondition(), 305 FirstCase.getCaseValue(), "cond"); 306 307 // Insert the new branch. 308 BranchInst *NewBr = Builder.CreateCondBr(Cond, 309 FirstCase.getCaseSuccessor(), 310 SI->getDefaultDest()); 311 MDNode *MD = SI->getMetadata(LLVMContext::MD_prof); 312 if (MD && MD->getNumOperands() == 3) { 313 ConstantInt *SICase = 314 mdconst::dyn_extract<ConstantInt>(MD->getOperand(2)); 315 ConstantInt *SIDef = 316 mdconst::dyn_extract<ConstantInt>(MD->getOperand(1)); 317 assert(SICase && SIDef); 318 // The TrueWeight should be the weight for the single case of SI. 319 NewBr->setMetadata(LLVMContext::MD_prof, 320 MDBuilder(BB->getContext()). 321 createBranchWeights(SICase->getValue().getZExtValue(), 322 SIDef->getValue().getZExtValue())); 323 } 324 325 // Update make.implicit metadata to the newly-created conditional branch. 326 MDNode *MakeImplicitMD = SI->getMetadata(LLVMContext::MD_make_implicit); 327 if (MakeImplicitMD) 328 NewBr->setMetadata(LLVMContext::MD_make_implicit, MakeImplicitMD); 329 330 // Delete the old switch. 331 SI->eraseFromParent(); 332 return true; 333 } 334 return Changed; 335 } 336 337 if (auto *IBI = dyn_cast<IndirectBrInst>(T)) { 338 // indirectbr blockaddress(@F, @BB) -> br label @BB 339 if (auto *BA = 340 dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) { 341 BasicBlock *TheOnlyDest = BA->getBasicBlock(); 342 SmallSet<BasicBlock *, 8> RemovedSuccessors; 343 344 // Insert the new branch. 345 Builder.CreateBr(TheOnlyDest); 346 347 BasicBlock *SuccToKeep = TheOnlyDest; 348 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { 349 BasicBlock *DestBB = IBI->getDestination(i); 350 if (DTU && DestBB != TheOnlyDest) 351 RemovedSuccessors.insert(DestBB); 352 if (IBI->getDestination(i) == SuccToKeep) { 353 SuccToKeep = nullptr; 354 } else { 355 DestBB->removePredecessor(BB); 356 } 357 } 358 Value *Address = IBI->getAddress(); 359 IBI->eraseFromParent(); 360 if (DeleteDeadConditions) 361 // Delete pointer cast instructions. 362 RecursivelyDeleteTriviallyDeadInstructions(Address, TLI); 363 364 // Also zap the blockaddress constant if there are no users remaining, 365 // otherwise the destination is still marked as having its address taken. 366 if (BA->use_empty()) 367 BA->destroyConstant(); 368 369 // If we didn't find our destination in the IBI successor list, then we 370 // have undefined behavior. Replace the unconditional branch with an 371 // 'unreachable' instruction. 372 if (SuccToKeep) { 373 BB->getTerminator()->eraseFromParent(); 374 new UnreachableInst(BB->getContext(), BB); 375 } 376 377 if (DTU) { 378 std::vector<DominatorTree::UpdateType> Updates; 379 Updates.reserve(RemovedSuccessors.size()); 380 for (auto *RemovedSuccessor : RemovedSuccessors) 381 Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor}); 382 DTU->applyUpdates(Updates); 383 } 384 return true; 385 } 386 } 387 388 return false; 389 } 390 391 //===----------------------------------------------------------------------===// 392 // Local dead code elimination. 393 // 394 395 /// isInstructionTriviallyDead - Return true if the result produced by the 396 /// instruction is not used, and the instruction has no side effects. 397 /// 398 bool llvm::isInstructionTriviallyDead(Instruction *I, 399 const TargetLibraryInfo *TLI) { 400 if (!I->use_empty()) 401 return false; 402 return wouldInstructionBeTriviallyDead(I, TLI); 403 } 404 405 bool llvm::wouldInstructionBeTriviallyDead(Instruction *I, 406 const TargetLibraryInfo *TLI) { 407 if (I->isTerminator()) 408 return false; 409 410 // We don't want the landingpad-like instructions removed by anything this 411 // general. 412 if (I->isEHPad()) 413 return false; 414 415 // We don't want debug info removed by anything this general, unless 416 // debug info is empty. 417 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) { 418 if (DDI->getAddress()) 419 return false; 420 return true; 421 } 422 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) { 423 if (DVI->hasArgList() || DVI->getValue(0)) 424 return false; 425 return true; 426 } 427 if (DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(I)) { 428 if (DLI->getLabel()) 429 return false; 430 return true; 431 } 432 433 if (!I->willReturn()) 434 return false; 435 436 if (!I->mayHaveSideEffects()) 437 return true; 438 439 // Special case intrinsics that "may have side effects" but can be deleted 440 // when dead. 441 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 442 // Safe to delete llvm.stacksave and launder.invariant.group if dead. 443 if (II->getIntrinsicID() == Intrinsic::stacksave || 444 II->getIntrinsicID() == Intrinsic::launder_invariant_group) 445 return true; 446 447 if (II->isLifetimeStartOrEnd()) { 448 auto *Arg = II->getArgOperand(1); 449 // Lifetime intrinsics are dead when their right-hand is undef. 450 if (isa<UndefValue>(Arg)) 451 return true; 452 // If the right-hand is an alloc, global, or argument and the only uses 453 // are lifetime intrinsics then the intrinsics are dead. 454 if (isa<AllocaInst>(Arg) || isa<GlobalValue>(Arg) || isa<Argument>(Arg)) 455 return llvm::all_of(Arg->uses(), [](Use &Use) { 456 if (IntrinsicInst *IntrinsicUse = 457 dyn_cast<IntrinsicInst>(Use.getUser())) 458 return IntrinsicUse->isLifetimeStartOrEnd(); 459 return false; 460 }); 461 return false; 462 } 463 464 // Assumptions are dead if their condition is trivially true. Guards on 465 // true are operationally no-ops. In the future we can consider more 466 // sophisticated tradeoffs for guards considering potential for check 467 // widening, but for now we keep things simple. 468 if ((II->getIntrinsicID() == Intrinsic::assume && 469 isAssumeWithEmptyBundle(cast<AssumeInst>(*II))) || 470 II->getIntrinsicID() == Intrinsic::experimental_guard) { 471 if (ConstantInt *Cond = dyn_cast<ConstantInt>(II->getArgOperand(0))) 472 return !Cond->isZero(); 473 474 return false; 475 } 476 477 if (auto *FPI = dyn_cast<ConstrainedFPIntrinsic>(I)) { 478 Optional<fp::ExceptionBehavior> ExBehavior = FPI->getExceptionBehavior(); 479 return ExBehavior.getValue() != fp::ebStrict; 480 } 481 } 482 483 if (isAllocLikeFn(I, TLI)) 484 return true; 485 486 if (CallInst *CI = isFreeCall(I, TLI)) 487 if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0))) 488 return C->isNullValue() || isa<UndefValue>(C); 489 490 if (auto *Call = dyn_cast<CallBase>(I)) 491 if (isMathLibCallNoop(Call, TLI)) 492 return true; 493 494 // To express possible interaction with floating point environment constrained 495 // intrinsics are described as if they access memory. So they look like having 496 // side effect but actually do not have it unless they raise floating point 497 // exception. If FP exceptions are ignored, the intrinsic may be deleted. 498 if (auto *CI = dyn_cast<ConstrainedFPIntrinsic>(I)) { 499 Optional<fp::ExceptionBehavior> EB = CI->getExceptionBehavior(); 500 if (!EB || *EB == fp::ExceptionBehavior::ebIgnore) 501 return true; 502 } 503 504 return false; 505 } 506 507 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a 508 /// trivially dead instruction, delete it. If that makes any of its operands 509 /// trivially dead, delete them too, recursively. Return true if any 510 /// instructions were deleted. 511 bool llvm::RecursivelyDeleteTriviallyDeadInstructions( 512 Value *V, const TargetLibraryInfo *TLI, MemorySSAUpdater *MSSAU, 513 std::function<void(Value *)> AboutToDeleteCallback) { 514 Instruction *I = dyn_cast<Instruction>(V); 515 if (!I || !isInstructionTriviallyDead(I, TLI)) 516 return false; 517 518 SmallVector<WeakTrackingVH, 16> DeadInsts; 519 DeadInsts.push_back(I); 520 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU, 521 AboutToDeleteCallback); 522 523 return true; 524 } 525 526 bool llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive( 527 SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI, 528 MemorySSAUpdater *MSSAU, 529 std::function<void(Value *)> AboutToDeleteCallback) { 530 unsigned S = 0, E = DeadInsts.size(), Alive = 0; 531 for (; S != E; ++S) { 532 auto *I = cast<Instruction>(DeadInsts[S]); 533 if (!isInstructionTriviallyDead(I)) { 534 DeadInsts[S] = nullptr; 535 ++Alive; 536 } 537 } 538 if (Alive == E) 539 return false; 540 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU, 541 AboutToDeleteCallback); 542 return true; 543 } 544 545 void llvm::RecursivelyDeleteTriviallyDeadInstructions( 546 SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI, 547 MemorySSAUpdater *MSSAU, 548 std::function<void(Value *)> AboutToDeleteCallback) { 549 // Process the dead instruction list until empty. 550 while (!DeadInsts.empty()) { 551 Value *V = DeadInsts.pop_back_val(); 552 Instruction *I = cast_or_null<Instruction>(V); 553 if (!I) 554 continue; 555 assert(isInstructionTriviallyDead(I, TLI) && 556 "Live instruction found in dead worklist!"); 557 assert(I->use_empty() && "Instructions with uses are not dead."); 558 559 // Don't lose the debug info while deleting the instructions. 560 salvageDebugInfo(*I); 561 562 if (AboutToDeleteCallback) 563 AboutToDeleteCallback(I); 564 565 // Null out all of the instruction's operands to see if any operand becomes 566 // dead as we go. 567 for (Use &OpU : I->operands()) { 568 Value *OpV = OpU.get(); 569 OpU.set(nullptr); 570 571 if (!OpV->use_empty()) 572 continue; 573 574 // If the operand is an instruction that became dead as we nulled out the 575 // operand, and if it is 'trivially' dead, delete it in a future loop 576 // iteration. 577 if (Instruction *OpI = dyn_cast<Instruction>(OpV)) 578 if (isInstructionTriviallyDead(OpI, TLI)) 579 DeadInsts.push_back(OpI); 580 } 581 if (MSSAU) 582 MSSAU->removeMemoryAccess(I); 583 584 I->eraseFromParent(); 585 } 586 } 587 588 bool llvm::replaceDbgUsesWithUndef(Instruction *I) { 589 SmallVector<DbgVariableIntrinsic *, 1> DbgUsers; 590 findDbgUsers(DbgUsers, I); 591 for (auto *DII : DbgUsers) { 592 Value *Undef = UndefValue::get(I->getType()); 593 DII->replaceVariableLocationOp(I, Undef); 594 } 595 return !DbgUsers.empty(); 596 } 597 598 /// areAllUsesEqual - Check whether the uses of a value are all the same. 599 /// This is similar to Instruction::hasOneUse() except this will also return 600 /// true when there are no uses or multiple uses that all refer to the same 601 /// value. 602 static bool areAllUsesEqual(Instruction *I) { 603 Value::user_iterator UI = I->user_begin(); 604 Value::user_iterator UE = I->user_end(); 605 if (UI == UE) 606 return true; 607 608 User *TheUse = *UI; 609 for (++UI; UI != UE; ++UI) { 610 if (*UI != TheUse) 611 return false; 612 } 613 return true; 614 } 615 616 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively 617 /// dead PHI node, due to being a def-use chain of single-use nodes that 618 /// either forms a cycle or is terminated by a trivially dead instruction, 619 /// delete it. If that makes any of its operands trivially dead, delete them 620 /// too, recursively. Return true if a change was made. 621 bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN, 622 const TargetLibraryInfo *TLI, 623 llvm::MemorySSAUpdater *MSSAU) { 624 SmallPtrSet<Instruction*, 4> Visited; 625 for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects(); 626 I = cast<Instruction>(*I->user_begin())) { 627 if (I->use_empty()) 628 return RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU); 629 630 // If we find an instruction more than once, we're on a cycle that 631 // won't prove fruitful. 632 if (!Visited.insert(I).second) { 633 // Break the cycle and delete the instruction and its operands. 634 I->replaceAllUsesWith(UndefValue::get(I->getType())); 635 (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU); 636 return true; 637 } 638 } 639 return false; 640 } 641 642 static bool 643 simplifyAndDCEInstruction(Instruction *I, 644 SmallSetVector<Instruction *, 16> &WorkList, 645 const DataLayout &DL, 646 const TargetLibraryInfo *TLI) { 647 if (isInstructionTriviallyDead(I, TLI)) { 648 salvageDebugInfo(*I); 649 650 // Null out all of the instruction's operands to see if any operand becomes 651 // dead as we go. 652 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 653 Value *OpV = I->getOperand(i); 654 I->setOperand(i, nullptr); 655 656 if (!OpV->use_empty() || I == OpV) 657 continue; 658 659 // If the operand is an instruction that became dead as we nulled out the 660 // operand, and if it is 'trivially' dead, delete it in a future loop 661 // iteration. 662 if (Instruction *OpI = dyn_cast<Instruction>(OpV)) 663 if (isInstructionTriviallyDead(OpI, TLI)) 664 WorkList.insert(OpI); 665 } 666 667 I->eraseFromParent(); 668 669 return true; 670 } 671 672 if (Value *SimpleV = SimplifyInstruction(I, DL)) { 673 // Add the users to the worklist. CAREFUL: an instruction can use itself, 674 // in the case of a phi node. 675 for (User *U : I->users()) { 676 if (U != I) { 677 WorkList.insert(cast<Instruction>(U)); 678 } 679 } 680 681 // Replace the instruction with its simplified value. 682 bool Changed = false; 683 if (!I->use_empty()) { 684 I->replaceAllUsesWith(SimpleV); 685 Changed = true; 686 } 687 if (isInstructionTriviallyDead(I, TLI)) { 688 I->eraseFromParent(); 689 Changed = true; 690 } 691 return Changed; 692 } 693 return false; 694 } 695 696 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to 697 /// simplify any instructions in it and recursively delete dead instructions. 698 /// 699 /// This returns true if it changed the code, note that it can delete 700 /// instructions in other blocks as well in this block. 701 bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, 702 const TargetLibraryInfo *TLI) { 703 bool MadeChange = false; 704 const DataLayout &DL = BB->getModule()->getDataLayout(); 705 706 #ifndef NDEBUG 707 // In debug builds, ensure that the terminator of the block is never replaced 708 // or deleted by these simplifications. The idea of simplification is that it 709 // cannot introduce new instructions, and there is no way to replace the 710 // terminator of a block without introducing a new instruction. 711 AssertingVH<Instruction> TerminatorVH(&BB->back()); 712 #endif 713 714 SmallSetVector<Instruction *, 16> WorkList; 715 // Iterate over the original function, only adding insts to the worklist 716 // if they actually need to be revisited. This avoids having to pre-init 717 // the worklist with the entire function's worth of instructions. 718 for (BasicBlock::iterator BI = BB->begin(), E = std::prev(BB->end()); 719 BI != E;) { 720 assert(!BI->isTerminator()); 721 Instruction *I = &*BI; 722 ++BI; 723 724 // We're visiting this instruction now, so make sure it's not in the 725 // worklist from an earlier visit. 726 if (!WorkList.count(I)) 727 MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI); 728 } 729 730 while (!WorkList.empty()) { 731 Instruction *I = WorkList.pop_back_val(); 732 MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI); 733 } 734 return MadeChange; 735 } 736 737 //===----------------------------------------------------------------------===// 738 // Control Flow Graph Restructuring. 739 // 740 741 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, 742 DomTreeUpdater *DTU) { 743 744 // If BB has single-entry PHI nodes, fold them. 745 while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { 746 Value *NewVal = PN->getIncomingValue(0); 747 // Replace self referencing PHI with undef, it must be dead. 748 if (NewVal == PN) NewVal = UndefValue::get(PN->getType()); 749 PN->replaceAllUsesWith(NewVal); 750 PN->eraseFromParent(); 751 } 752 753 BasicBlock *PredBB = DestBB->getSinglePredecessor(); 754 assert(PredBB && "Block doesn't have a single predecessor!"); 755 756 bool ReplaceEntryBB = PredBB->isEntryBlock(); 757 758 // DTU updates: Collect all the edges that enter 759 // PredBB. These dominator edges will be redirected to DestBB. 760 SmallVector<DominatorTree::UpdateType, 32> Updates; 761 762 if (DTU) { 763 SmallPtrSet<BasicBlock *, 2> PredsOfPredBB(pred_begin(PredBB), 764 pred_end(PredBB)); 765 Updates.reserve(Updates.size() + 2 * PredsOfPredBB.size() + 1); 766 for (BasicBlock *PredOfPredBB : PredsOfPredBB) 767 // This predecessor of PredBB may already have DestBB as a successor. 768 if (PredOfPredBB != PredBB) 769 Updates.push_back({DominatorTree::Insert, PredOfPredBB, DestBB}); 770 for (BasicBlock *PredOfPredBB : PredsOfPredBB) 771 Updates.push_back({DominatorTree::Delete, PredOfPredBB, PredBB}); 772 Updates.push_back({DominatorTree::Delete, PredBB, DestBB}); 773 } 774 775 // Zap anything that took the address of DestBB. Not doing this will give the 776 // address an invalid value. 777 if (DestBB->hasAddressTaken()) { 778 BlockAddress *BA = BlockAddress::get(DestBB); 779 Constant *Replacement = 780 ConstantInt::get(Type::getInt32Ty(BA->getContext()), 1); 781 BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement, 782 BA->getType())); 783 BA->destroyConstant(); 784 } 785 786 // Anything that branched to PredBB now branches to DestBB. 787 PredBB->replaceAllUsesWith(DestBB); 788 789 // Splice all the instructions from PredBB to DestBB. 790 PredBB->getTerminator()->eraseFromParent(); 791 DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList()); 792 new UnreachableInst(PredBB->getContext(), PredBB); 793 794 // If the PredBB is the entry block of the function, move DestBB up to 795 // become the entry block after we erase PredBB. 796 if (ReplaceEntryBB) 797 DestBB->moveAfter(PredBB); 798 799 if (DTU) { 800 assert(PredBB->getInstList().size() == 1 && 801 isa<UnreachableInst>(PredBB->getTerminator()) && 802 "The successor list of PredBB isn't empty before " 803 "applying corresponding DTU updates."); 804 DTU->applyUpdatesPermissive(Updates); 805 DTU->deleteBB(PredBB); 806 // Recalculation of DomTree is needed when updating a forward DomTree and 807 // the Entry BB is replaced. 808 if (ReplaceEntryBB && DTU->hasDomTree()) { 809 // The entry block was removed and there is no external interface for 810 // the dominator tree to be notified of this change. In this corner-case 811 // we recalculate the entire tree. 812 DTU->recalculate(*(DestBB->getParent())); 813 } 814 } 815 816 else { 817 PredBB->eraseFromParent(); // Nuke BB if DTU is nullptr. 818 } 819 } 820 821 /// Return true if we can choose one of these values to use in place of the 822 /// other. Note that we will always choose the non-undef value to keep. 823 static bool CanMergeValues(Value *First, Value *Second) { 824 return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second); 825 } 826 827 /// Return true if we can fold BB, an almost-empty BB ending in an unconditional 828 /// branch to Succ, into Succ. 829 /// 830 /// Assumption: Succ is the single successor for BB. 831 static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { 832 assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); 833 834 LLVM_DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into " 835 << Succ->getName() << "\n"); 836 // Shortcut, if there is only a single predecessor it must be BB and merging 837 // is always safe 838 if (Succ->getSinglePredecessor()) return true; 839 840 // Make a list of the predecessors of BB 841 SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); 842 843 // Look at all the phi nodes in Succ, to see if they present a conflict when 844 // merging these blocks 845 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { 846 PHINode *PN = cast<PHINode>(I); 847 848 // If the incoming value from BB is again a PHINode in 849 // BB which has the same incoming value for *PI as PN does, we can 850 // merge the phi nodes and then the blocks can still be merged 851 PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB)); 852 if (BBPN && BBPN->getParent() == BB) { 853 for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { 854 BasicBlock *IBB = PN->getIncomingBlock(PI); 855 if (BBPreds.count(IBB) && 856 !CanMergeValues(BBPN->getIncomingValueForBlock(IBB), 857 PN->getIncomingValue(PI))) { 858 LLVM_DEBUG(dbgs() 859 << "Can't fold, phi node " << PN->getName() << " in " 860 << Succ->getName() << " is conflicting with " 861 << BBPN->getName() << " with regard to common predecessor " 862 << IBB->getName() << "\n"); 863 return false; 864 } 865 } 866 } else { 867 Value* Val = PN->getIncomingValueForBlock(BB); 868 for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) { 869 // See if the incoming value for the common predecessor is equal to the 870 // one for BB, in which case this phi node will not prevent the merging 871 // of the block. 872 BasicBlock *IBB = PN->getIncomingBlock(PI); 873 if (BBPreds.count(IBB) && 874 !CanMergeValues(Val, PN->getIncomingValue(PI))) { 875 LLVM_DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() 876 << " in " << Succ->getName() 877 << " is conflicting with regard to common " 878 << "predecessor " << IBB->getName() << "\n"); 879 return false; 880 } 881 } 882 } 883 } 884 885 return true; 886 } 887 888 using PredBlockVector = SmallVector<BasicBlock *, 16>; 889 using IncomingValueMap = DenseMap<BasicBlock *, Value *>; 890 891 /// Determines the value to use as the phi node input for a block. 892 /// 893 /// Select between \p OldVal any value that we know flows from \p BB 894 /// to a particular phi on the basis of which one (if either) is not 895 /// undef. Update IncomingValues based on the selected value. 896 /// 897 /// \param OldVal The value we are considering selecting. 898 /// \param BB The block that the value flows in from. 899 /// \param IncomingValues A map from block-to-value for other phi inputs 900 /// that we have examined. 901 /// 902 /// \returns the selected value. 903 static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB, 904 IncomingValueMap &IncomingValues) { 905 if (!isa<UndefValue>(OldVal)) { 906 assert((!IncomingValues.count(BB) || 907 IncomingValues.find(BB)->second == OldVal) && 908 "Expected OldVal to match incoming value from BB!"); 909 910 IncomingValues.insert(std::make_pair(BB, OldVal)); 911 return OldVal; 912 } 913 914 IncomingValueMap::const_iterator It = IncomingValues.find(BB); 915 if (It != IncomingValues.end()) return It->second; 916 917 return OldVal; 918 } 919 920 /// Create a map from block to value for the operands of a 921 /// given phi. 922 /// 923 /// Create a map from block to value for each non-undef value flowing 924 /// into \p PN. 925 /// 926 /// \param PN The phi we are collecting the map for. 927 /// \param IncomingValues [out] The map from block to value for this phi. 928 static void gatherIncomingValuesToPhi(PHINode *PN, 929 IncomingValueMap &IncomingValues) { 930 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 931 BasicBlock *BB = PN->getIncomingBlock(i); 932 Value *V = PN->getIncomingValue(i); 933 934 if (!isa<UndefValue>(V)) 935 IncomingValues.insert(std::make_pair(BB, V)); 936 } 937 } 938 939 /// Replace the incoming undef values to a phi with the values 940 /// from a block-to-value map. 941 /// 942 /// \param PN The phi we are replacing the undefs in. 943 /// \param IncomingValues A map from block to value. 944 static void replaceUndefValuesInPhi(PHINode *PN, 945 const IncomingValueMap &IncomingValues) { 946 SmallVector<unsigned> TrueUndefOps; 947 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 948 Value *V = PN->getIncomingValue(i); 949 950 if (!isa<UndefValue>(V)) continue; 951 952 BasicBlock *BB = PN->getIncomingBlock(i); 953 IncomingValueMap::const_iterator It = IncomingValues.find(BB); 954 955 // Keep track of undef/poison incoming values. Those must match, so we fix 956 // them up below if needed. 957 // Note: this is conservatively correct, but we could try harder and group 958 // the undef values per incoming basic block. 959 if (It == IncomingValues.end()) { 960 TrueUndefOps.push_back(i); 961 continue; 962 } 963 964 // There is a defined value for this incoming block, so map this undef 965 // incoming value to the defined value. 966 PN->setIncomingValue(i, It->second); 967 } 968 969 // If there are both undef and poison values incoming, then convert those 970 // values to undef. It is invalid to have different values for the same 971 // incoming block. 972 unsigned PoisonCount = count_if(TrueUndefOps, [&](unsigned i) { 973 return isa<PoisonValue>(PN->getIncomingValue(i)); 974 }); 975 if (PoisonCount != 0 && PoisonCount != TrueUndefOps.size()) { 976 for (unsigned i : TrueUndefOps) 977 PN->setIncomingValue(i, UndefValue::get(PN->getType())); 978 } 979 } 980 981 /// Replace a value flowing from a block to a phi with 982 /// potentially multiple instances of that value flowing from the 983 /// block's predecessors to the phi. 984 /// 985 /// \param BB The block with the value flowing into the phi. 986 /// \param BBPreds The predecessors of BB. 987 /// \param PN The phi that we are updating. 988 static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB, 989 const PredBlockVector &BBPreds, 990 PHINode *PN) { 991 Value *OldVal = PN->removeIncomingValue(BB, false); 992 assert(OldVal && "No entry in PHI for Pred BB!"); 993 994 IncomingValueMap IncomingValues; 995 996 // We are merging two blocks - BB, and the block containing PN - and 997 // as a result we need to redirect edges from the predecessors of BB 998 // to go to the block containing PN, and update PN 999 // accordingly. Since we allow merging blocks in the case where the 1000 // predecessor and successor blocks both share some predecessors, 1001 // and where some of those common predecessors might have undef 1002 // values flowing into PN, we want to rewrite those values to be 1003 // consistent with the non-undef values. 1004 1005 gatherIncomingValuesToPhi(PN, IncomingValues); 1006 1007 // If this incoming value is one of the PHI nodes in BB, the new entries 1008 // in the PHI node are the entries from the old PHI. 1009 if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) { 1010 PHINode *OldValPN = cast<PHINode>(OldVal); 1011 for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) { 1012 // Note that, since we are merging phi nodes and BB and Succ might 1013 // have common predecessors, we could end up with a phi node with 1014 // identical incoming branches. This will be cleaned up later (and 1015 // will trigger asserts if we try to clean it up now, without also 1016 // simplifying the corresponding conditional branch). 1017 BasicBlock *PredBB = OldValPN->getIncomingBlock(i); 1018 Value *PredVal = OldValPN->getIncomingValue(i); 1019 Value *Selected = selectIncomingValueForBlock(PredVal, PredBB, 1020 IncomingValues); 1021 1022 // And add a new incoming value for this predecessor for the 1023 // newly retargeted branch. 1024 PN->addIncoming(Selected, PredBB); 1025 } 1026 } else { 1027 for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) { 1028 // Update existing incoming values in PN for this 1029 // predecessor of BB. 1030 BasicBlock *PredBB = BBPreds[i]; 1031 Value *Selected = selectIncomingValueForBlock(OldVal, PredBB, 1032 IncomingValues); 1033 1034 // And add a new incoming value for this predecessor for the 1035 // newly retargeted branch. 1036 PN->addIncoming(Selected, PredBB); 1037 } 1038 } 1039 1040 replaceUndefValuesInPhi(PN, IncomingValues); 1041 } 1042 1043 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, 1044 DomTreeUpdater *DTU) { 1045 assert(BB != &BB->getParent()->getEntryBlock() && 1046 "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!"); 1047 1048 // We can't eliminate infinite loops. 1049 BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0); 1050 if (BB == Succ) return false; 1051 1052 // Check to see if merging these blocks would cause conflicts for any of the 1053 // phi nodes in BB or Succ. If not, we can safely merge. 1054 if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false; 1055 1056 // Check for cases where Succ has multiple predecessors and a PHI node in BB 1057 // has uses which will not disappear when the PHI nodes are merged. It is 1058 // possible to handle such cases, but difficult: it requires checking whether 1059 // BB dominates Succ, which is non-trivial to calculate in the case where 1060 // Succ has multiple predecessors. Also, it requires checking whether 1061 // constructing the necessary self-referential PHI node doesn't introduce any 1062 // conflicts; this isn't too difficult, but the previous code for doing this 1063 // was incorrect. 1064 // 1065 // Note that if this check finds a live use, BB dominates Succ, so BB is 1066 // something like a loop pre-header (or rarely, a part of an irreducible CFG); 1067 // folding the branch isn't profitable in that case anyway. 1068 if (!Succ->getSinglePredecessor()) { 1069 BasicBlock::iterator BBI = BB->begin(); 1070 while (isa<PHINode>(*BBI)) { 1071 for (Use &U : BBI->uses()) { 1072 if (PHINode* PN = dyn_cast<PHINode>(U.getUser())) { 1073 if (PN->getIncomingBlock(U) != BB) 1074 return false; 1075 } else { 1076 return false; 1077 } 1078 } 1079 ++BBI; 1080 } 1081 } 1082 1083 // We cannot fold the block if it's a branch to an already present callbr 1084 // successor because that creates duplicate successors. 1085 for (BasicBlock *PredBB : predecessors(BB)) { 1086 if (auto *CBI = dyn_cast<CallBrInst>(PredBB->getTerminator())) { 1087 if (Succ == CBI->getDefaultDest()) 1088 return false; 1089 for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) 1090 if (Succ == CBI->getIndirectDest(i)) 1091 return false; 1092 } 1093 } 1094 1095 LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB); 1096 1097 SmallVector<DominatorTree::UpdateType, 32> Updates; 1098 if (DTU) { 1099 // All predecessors of BB will be moved to Succ. 1100 SmallPtrSet<BasicBlock *, 8> PredsOfBB(pred_begin(BB), pred_end(BB)); 1101 SmallPtrSet<BasicBlock *, 8> PredsOfSucc(pred_begin(Succ), pred_end(Succ)); 1102 Updates.reserve(Updates.size() + 2 * PredsOfBB.size() + 1); 1103 for (auto *PredOfBB : PredsOfBB) 1104 // This predecessor of BB may already have Succ as a successor. 1105 if (!PredsOfSucc.contains(PredOfBB)) 1106 Updates.push_back({DominatorTree::Insert, PredOfBB, Succ}); 1107 for (auto *PredOfBB : PredsOfBB) 1108 Updates.push_back({DominatorTree::Delete, PredOfBB, BB}); 1109 Updates.push_back({DominatorTree::Delete, BB, Succ}); 1110 } 1111 1112 if (isa<PHINode>(Succ->begin())) { 1113 // If there is more than one pred of succ, and there are PHI nodes in 1114 // the successor, then we need to add incoming edges for the PHI nodes 1115 // 1116 const PredBlockVector BBPreds(pred_begin(BB), pred_end(BB)); 1117 1118 // Loop over all of the PHI nodes in the successor of BB. 1119 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { 1120 PHINode *PN = cast<PHINode>(I); 1121 1122 redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN); 1123 } 1124 } 1125 1126 if (Succ->getSinglePredecessor()) { 1127 // BB is the only predecessor of Succ, so Succ will end up with exactly 1128 // the same predecessors BB had. 1129 1130 // Copy over any phi, debug or lifetime instruction. 1131 BB->getTerminator()->eraseFromParent(); 1132 Succ->getInstList().splice(Succ->getFirstNonPHI()->getIterator(), 1133 BB->getInstList()); 1134 } else { 1135 while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { 1136 // We explicitly check for such uses in CanPropagatePredecessorsForPHIs. 1137 assert(PN->use_empty() && "There shouldn't be any uses here!"); 1138 PN->eraseFromParent(); 1139 } 1140 } 1141 1142 // If the unconditional branch we replaced contains llvm.loop metadata, we 1143 // add the metadata to the branch instructions in the predecessors. 1144 unsigned LoopMDKind = BB->getContext().getMDKindID("llvm.loop"); 1145 Instruction *TI = BB->getTerminator(); 1146 if (TI) 1147 if (MDNode *LoopMD = TI->getMetadata(LoopMDKind)) 1148 for (BasicBlock *Pred : predecessors(BB)) 1149 Pred->getTerminator()->setMetadata(LoopMDKind, LoopMD); 1150 1151 // Everything that jumped to BB now goes to Succ. 1152 BB->replaceAllUsesWith(Succ); 1153 if (!Succ->hasName()) Succ->takeName(BB); 1154 1155 // Clear the successor list of BB to match updates applying to DTU later. 1156 if (BB->getTerminator()) 1157 BB->getInstList().pop_back(); 1158 new UnreachableInst(BB->getContext(), BB); 1159 assert(succ_empty(BB) && "The successor list of BB isn't empty before " 1160 "applying corresponding DTU updates."); 1161 1162 if (DTU) 1163 DTU->applyUpdates(Updates); 1164 1165 DeleteDeadBlock(BB, DTU); 1166 1167 return true; 1168 } 1169 1170 static bool EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB) { 1171 // This implementation doesn't currently consider undef operands 1172 // specially. Theoretically, two phis which are identical except for 1173 // one having an undef where the other doesn't could be collapsed. 1174 1175 bool Changed = false; 1176 1177 // Examine each PHI. 1178 // Note that increment of I must *NOT* be in the iteration_expression, since 1179 // we don't want to immediately advance when we restart from the beginning. 1180 for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I);) { 1181 ++I; 1182 // Is there an identical PHI node in this basic block? 1183 // Note that we only look in the upper square's triangle, 1184 // we already checked that the lower triangle PHI's aren't identical. 1185 for (auto J = I; PHINode *DuplicatePN = dyn_cast<PHINode>(J); ++J) { 1186 if (!DuplicatePN->isIdenticalToWhenDefined(PN)) 1187 continue; 1188 // A duplicate. Replace this PHI with the base PHI. 1189 ++NumPHICSEs; 1190 DuplicatePN->replaceAllUsesWith(PN); 1191 DuplicatePN->eraseFromParent(); 1192 Changed = true; 1193 1194 // The RAUW can change PHIs that we already visited. 1195 I = BB->begin(); 1196 break; // Start over from the beginning. 1197 } 1198 } 1199 return Changed; 1200 } 1201 1202 static bool EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB) { 1203 // This implementation doesn't currently consider undef operands 1204 // specially. Theoretically, two phis which are identical except for 1205 // one having an undef where the other doesn't could be collapsed. 1206 1207 struct PHIDenseMapInfo { 1208 static PHINode *getEmptyKey() { 1209 return DenseMapInfo<PHINode *>::getEmptyKey(); 1210 } 1211 1212 static PHINode *getTombstoneKey() { 1213 return DenseMapInfo<PHINode *>::getTombstoneKey(); 1214 } 1215 1216 static bool isSentinel(PHINode *PN) { 1217 return PN == getEmptyKey() || PN == getTombstoneKey(); 1218 } 1219 1220 // WARNING: this logic must be kept in sync with 1221 // Instruction::isIdenticalToWhenDefined()! 1222 static unsigned getHashValueImpl(PHINode *PN) { 1223 // Compute a hash value on the operands. Instcombine will likely have 1224 // sorted them, which helps expose duplicates, but we have to check all 1225 // the operands to be safe in case instcombine hasn't run. 1226 return static_cast<unsigned>(hash_combine( 1227 hash_combine_range(PN->value_op_begin(), PN->value_op_end()), 1228 hash_combine_range(PN->block_begin(), PN->block_end()))); 1229 } 1230 1231 static unsigned getHashValue(PHINode *PN) { 1232 #ifndef NDEBUG 1233 // If -phicse-debug-hash was specified, return a constant -- this 1234 // will force all hashing to collide, so we'll exhaustively search 1235 // the table for a match, and the assertion in isEqual will fire if 1236 // there's a bug causing equal keys to hash differently. 1237 if (PHICSEDebugHash) 1238 return 0; 1239 #endif 1240 return getHashValueImpl(PN); 1241 } 1242 1243 static bool isEqualImpl(PHINode *LHS, PHINode *RHS) { 1244 if (isSentinel(LHS) || isSentinel(RHS)) 1245 return LHS == RHS; 1246 return LHS->isIdenticalTo(RHS); 1247 } 1248 1249 static bool isEqual(PHINode *LHS, PHINode *RHS) { 1250 // These comparisons are nontrivial, so assert that equality implies 1251 // hash equality (DenseMap demands this as an invariant). 1252 bool Result = isEqualImpl(LHS, RHS); 1253 assert(!Result || (isSentinel(LHS) && LHS == RHS) || 1254 getHashValueImpl(LHS) == getHashValueImpl(RHS)); 1255 return Result; 1256 } 1257 }; 1258 1259 // Set of unique PHINodes. 1260 DenseSet<PHINode *, PHIDenseMapInfo> PHISet; 1261 PHISet.reserve(4 * PHICSENumPHISmallSize); 1262 1263 // Examine each PHI. 1264 bool Changed = false; 1265 for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I++);) { 1266 auto Inserted = PHISet.insert(PN); 1267 if (!Inserted.second) { 1268 // A duplicate. Replace this PHI with its duplicate. 1269 ++NumPHICSEs; 1270 PN->replaceAllUsesWith(*Inserted.first); 1271 PN->eraseFromParent(); 1272 Changed = true; 1273 1274 // The RAUW can change PHIs that we already visited. Start over from the 1275 // beginning. 1276 PHISet.clear(); 1277 I = BB->begin(); 1278 } 1279 } 1280 1281 return Changed; 1282 } 1283 1284 bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) { 1285 if ( 1286 #ifndef NDEBUG 1287 !PHICSEDebugHash && 1288 #endif 1289 hasNItemsOrLess(BB->phis(), PHICSENumPHISmallSize)) 1290 return EliminateDuplicatePHINodesNaiveImpl(BB); 1291 return EliminateDuplicatePHINodesSetBasedImpl(BB); 1292 } 1293 1294 /// If the specified pointer points to an object that we control, try to modify 1295 /// the object's alignment to PrefAlign. Returns a minimum known alignment of 1296 /// the value after the operation, which may be lower than PrefAlign. 1297 /// 1298 /// Increating value alignment isn't often possible though. If alignment is 1299 /// important, a more reliable approach is to simply align all global variables 1300 /// and allocation instructions to their preferred alignment from the beginning. 1301 static Align tryEnforceAlignment(Value *V, Align PrefAlign, 1302 const DataLayout &DL) { 1303 V = V->stripPointerCasts(); 1304 1305 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { 1306 // TODO: Ideally, this function would not be called if PrefAlign is smaller 1307 // than the current alignment, as the known bits calculation should have 1308 // already taken it into account. However, this is not always the case, 1309 // as computeKnownBits() has a depth limit, while stripPointerCasts() 1310 // doesn't. 1311 Align CurrentAlign = AI->getAlign(); 1312 if (PrefAlign <= CurrentAlign) 1313 return CurrentAlign; 1314 1315 // If the preferred alignment is greater than the natural stack alignment 1316 // then don't round up. This avoids dynamic stack realignment. 1317 if (DL.exceedsNaturalStackAlignment(PrefAlign)) 1318 return CurrentAlign; 1319 AI->setAlignment(PrefAlign); 1320 return PrefAlign; 1321 } 1322 1323 if (auto *GO = dyn_cast<GlobalObject>(V)) { 1324 // TODO: as above, this shouldn't be necessary. 1325 Align CurrentAlign = GO->getPointerAlignment(DL); 1326 if (PrefAlign <= CurrentAlign) 1327 return CurrentAlign; 1328 1329 // If there is a large requested alignment and we can, bump up the alignment 1330 // of the global. If the memory we set aside for the global may not be the 1331 // memory used by the final program then it is impossible for us to reliably 1332 // enforce the preferred alignment. 1333 if (!GO->canIncreaseAlignment()) 1334 return CurrentAlign; 1335 1336 GO->setAlignment(PrefAlign); 1337 return PrefAlign; 1338 } 1339 1340 return Align(1); 1341 } 1342 1343 Align llvm::getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, 1344 const DataLayout &DL, 1345 const Instruction *CxtI, 1346 AssumptionCache *AC, 1347 const DominatorTree *DT) { 1348 assert(V->getType()->isPointerTy() && 1349 "getOrEnforceKnownAlignment expects a pointer!"); 1350 1351 KnownBits Known = computeKnownBits(V, DL, 0, AC, CxtI, DT); 1352 unsigned TrailZ = Known.countMinTrailingZeros(); 1353 1354 // Avoid trouble with ridiculously large TrailZ values, such as 1355 // those computed from a null pointer. 1356 // LLVM doesn't support alignments larger than (1 << MaxAlignmentExponent). 1357 TrailZ = std::min(TrailZ, +Value::MaxAlignmentExponent); 1358 1359 Align Alignment = Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ)); 1360 1361 if (PrefAlign && *PrefAlign > Alignment) 1362 Alignment = std::max(Alignment, tryEnforceAlignment(V, *PrefAlign, DL)); 1363 1364 // We don't need to make any adjustment. 1365 return Alignment; 1366 } 1367 1368 ///===---------------------------------------------------------------------===// 1369 /// Dbg Intrinsic utilities 1370 /// 1371 1372 /// See if there is a dbg.value intrinsic for DIVar for the PHI node. 1373 static bool PhiHasDebugValue(DILocalVariable *DIVar, 1374 DIExpression *DIExpr, 1375 PHINode *APN) { 1376 // Since we can't guarantee that the original dbg.declare instrinsic 1377 // is removed by LowerDbgDeclare(), we need to make sure that we are 1378 // not inserting the same dbg.value intrinsic over and over. 1379 SmallVector<DbgValueInst *, 1> DbgValues; 1380 findDbgValues(DbgValues, APN); 1381 for (auto *DVI : DbgValues) { 1382 assert(is_contained(DVI->getValues(), APN)); 1383 if ((DVI->getVariable() == DIVar) && (DVI->getExpression() == DIExpr)) 1384 return true; 1385 } 1386 return false; 1387 } 1388 1389 /// Check if the alloc size of \p ValTy is large enough to cover the variable 1390 /// (or fragment of the variable) described by \p DII. 1391 /// 1392 /// This is primarily intended as a helper for the different 1393 /// ConvertDebugDeclareToDebugValue functions. The dbg.declare/dbg.addr that is 1394 /// converted describes an alloca'd variable, so we need to use the 1395 /// alloc size of the value when doing the comparison. E.g. an i1 value will be 1396 /// identified as covering an n-bit fragment, if the store size of i1 is at 1397 /// least n bits. 1398 static bool valueCoversEntireFragment(Type *ValTy, DbgVariableIntrinsic *DII) { 1399 const DataLayout &DL = DII->getModule()->getDataLayout(); 1400 TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy); 1401 if (Optional<uint64_t> FragmentSize = DII->getFragmentSizeInBits()) { 1402 assert(!ValueSize.isScalable() && 1403 "Fragments don't work on scalable types."); 1404 return ValueSize.getFixedSize() >= *FragmentSize; 1405 } 1406 // We can't always calculate the size of the DI variable (e.g. if it is a 1407 // VLA). Try to use the size of the alloca that the dbg intrinsic describes 1408 // intead. 1409 if (DII->isAddressOfVariable()) { 1410 // DII should have exactly 1 location when it is an address. 1411 assert(DII->getNumVariableLocationOps() == 1 && 1412 "address of variable must have exactly 1 location operand."); 1413 if (auto *AI = 1414 dyn_cast_or_null<AllocaInst>(DII->getVariableLocationOp(0))) { 1415 if (Optional<TypeSize> FragmentSize = AI->getAllocationSizeInBits(DL)) { 1416 assert(ValueSize.isScalable() == FragmentSize->isScalable() && 1417 "Both sizes should agree on the scalable flag."); 1418 return TypeSize::isKnownGE(ValueSize, *FragmentSize); 1419 } 1420 } 1421 } 1422 // Could not determine size of variable. Conservatively return false. 1423 return false; 1424 } 1425 1426 /// Produce a DebugLoc to use for each dbg.declare/inst pair that are promoted 1427 /// to a dbg.value. Because no machine insts can come from debug intrinsics, 1428 /// only the scope and inlinedAt is significant. Zero line numbers are used in 1429 /// case this DebugLoc leaks into any adjacent instructions. 1430 static DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII, Instruction *Src) { 1431 // Original dbg.declare must have a location. 1432 const DebugLoc &DeclareLoc = DII->getDebugLoc(); 1433 MDNode *Scope = DeclareLoc.getScope(); 1434 DILocation *InlinedAt = DeclareLoc.getInlinedAt(); 1435 // Produce an unknown location with the correct scope / inlinedAt fields. 1436 return DILocation::get(DII->getContext(), 0, 0, Scope, InlinedAt); 1437 } 1438 1439 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value 1440 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic. 1441 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, 1442 StoreInst *SI, DIBuilder &Builder) { 1443 assert(DII->isAddressOfVariable()); 1444 auto *DIVar = DII->getVariable(); 1445 assert(DIVar && "Missing variable"); 1446 auto *DIExpr = DII->getExpression(); 1447 Value *DV = SI->getValueOperand(); 1448 1449 DebugLoc NewLoc = getDebugValueLoc(DII, SI); 1450 1451 if (!valueCoversEntireFragment(DV->getType(), DII)) { 1452 // FIXME: If storing to a part of the variable described by the dbg.declare, 1453 // then we want to insert a dbg.value for the corresponding fragment. 1454 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " 1455 << *DII << '\n'); 1456 // For now, when there is a store to parts of the variable (but we do not 1457 // know which part) we insert an dbg.value instrinsic to indicate that we 1458 // know nothing about the variable's content. 1459 DV = UndefValue::get(DV->getType()); 1460 Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI); 1461 return; 1462 } 1463 1464 Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI); 1465 } 1466 1467 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value 1468 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic. 1469 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, 1470 LoadInst *LI, DIBuilder &Builder) { 1471 auto *DIVar = DII->getVariable(); 1472 auto *DIExpr = DII->getExpression(); 1473 assert(DIVar && "Missing variable"); 1474 1475 if (!valueCoversEntireFragment(LI->getType(), DII)) { 1476 // FIXME: If only referring to a part of the variable described by the 1477 // dbg.declare, then we want to insert a dbg.value for the corresponding 1478 // fragment. 1479 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " 1480 << *DII << '\n'); 1481 return; 1482 } 1483 1484 DebugLoc NewLoc = getDebugValueLoc(DII, nullptr); 1485 1486 // We are now tracking the loaded value instead of the address. In the 1487 // future if multi-location support is added to the IR, it might be 1488 // preferable to keep tracking both the loaded value and the original 1489 // address in case the alloca can not be elided. 1490 Instruction *DbgValue = Builder.insertDbgValueIntrinsic( 1491 LI, DIVar, DIExpr, NewLoc, (Instruction *)nullptr); 1492 DbgValue->insertAfter(LI); 1493 } 1494 1495 /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated 1496 /// llvm.dbg.declare or llvm.dbg.addr intrinsic. 1497 void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, 1498 PHINode *APN, DIBuilder &Builder) { 1499 auto *DIVar = DII->getVariable(); 1500 auto *DIExpr = DII->getExpression(); 1501 assert(DIVar && "Missing variable"); 1502 1503 if (PhiHasDebugValue(DIVar, DIExpr, APN)) 1504 return; 1505 1506 if (!valueCoversEntireFragment(APN->getType(), DII)) { 1507 // FIXME: If only referring to a part of the variable described by the 1508 // dbg.declare, then we want to insert a dbg.value for the corresponding 1509 // fragment. 1510 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " 1511 << *DII << '\n'); 1512 return; 1513 } 1514 1515 BasicBlock *BB = APN->getParent(); 1516 auto InsertionPt = BB->getFirstInsertionPt(); 1517 1518 DebugLoc NewLoc = getDebugValueLoc(DII, nullptr); 1519 1520 // The block may be a catchswitch block, which does not have a valid 1521 // insertion point. 1522 // FIXME: Insert dbg.value markers in the successors when appropriate. 1523 if (InsertionPt != BB->end()) 1524 Builder.insertDbgValueIntrinsic(APN, DIVar, DIExpr, NewLoc, &*InsertionPt); 1525 } 1526 1527 /// Determine whether this alloca is either a VLA or an array. 1528 static bool isArray(AllocaInst *AI) { 1529 return AI->isArrayAllocation() || 1530 (AI->getAllocatedType() && AI->getAllocatedType()->isArrayTy()); 1531 } 1532 1533 /// Determine whether this alloca is a structure. 1534 static bool isStructure(AllocaInst *AI) { 1535 return AI->getAllocatedType() && AI->getAllocatedType()->isStructTy(); 1536 } 1537 1538 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set 1539 /// of llvm.dbg.value intrinsics. 1540 bool llvm::LowerDbgDeclare(Function &F) { 1541 bool Changed = false; 1542 DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false); 1543 SmallVector<DbgDeclareInst *, 4> Dbgs; 1544 for (auto &FI : F) 1545 for (Instruction &BI : FI) 1546 if (auto DDI = dyn_cast<DbgDeclareInst>(&BI)) 1547 Dbgs.push_back(DDI); 1548 1549 if (Dbgs.empty()) 1550 return Changed; 1551 1552 for (auto &I : Dbgs) { 1553 DbgDeclareInst *DDI = I; 1554 AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress()); 1555 // If this is an alloca for a scalar variable, insert a dbg.value 1556 // at each load and store to the alloca and erase the dbg.declare. 1557 // The dbg.values allow tracking a variable even if it is not 1558 // stored on the stack, while the dbg.declare can only describe 1559 // the stack slot (and at a lexical-scope granularity). Later 1560 // passes will attempt to elide the stack slot. 1561 if (!AI || isArray(AI) || isStructure(AI)) 1562 continue; 1563 1564 // A volatile load/store means that the alloca can't be elided anyway. 1565 if (llvm::any_of(AI->users(), [](User *U) -> bool { 1566 if (LoadInst *LI = dyn_cast<LoadInst>(U)) 1567 return LI->isVolatile(); 1568 if (StoreInst *SI = dyn_cast<StoreInst>(U)) 1569 return SI->isVolatile(); 1570 return false; 1571 })) 1572 continue; 1573 1574 SmallVector<const Value *, 8> WorkList; 1575 WorkList.push_back(AI); 1576 while (!WorkList.empty()) { 1577 const Value *V = WorkList.pop_back_val(); 1578 for (auto &AIUse : V->uses()) { 1579 User *U = AIUse.getUser(); 1580 if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 1581 if (AIUse.getOperandNo() == 1) 1582 ConvertDebugDeclareToDebugValue(DDI, SI, DIB); 1583 } else if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 1584 ConvertDebugDeclareToDebugValue(DDI, LI, DIB); 1585 } else if (CallInst *CI = dyn_cast<CallInst>(U)) { 1586 // This is a call by-value or some other instruction that takes a 1587 // pointer to the variable. Insert a *value* intrinsic that describes 1588 // the variable by dereferencing the alloca. 1589 if (!CI->isLifetimeStartOrEnd()) { 1590 DebugLoc NewLoc = getDebugValueLoc(DDI, nullptr); 1591 auto *DerefExpr = 1592 DIExpression::append(DDI->getExpression(), dwarf::DW_OP_deref); 1593 DIB.insertDbgValueIntrinsic(AI, DDI->getVariable(), DerefExpr, 1594 NewLoc, CI); 1595 } 1596 } else if (BitCastInst *BI = dyn_cast<BitCastInst>(U)) { 1597 if (BI->getType()->isPointerTy()) 1598 WorkList.push_back(BI); 1599 } 1600 } 1601 } 1602 DDI->eraseFromParent(); 1603 Changed = true; 1604 } 1605 1606 if (Changed) 1607 for (BasicBlock &BB : F) 1608 RemoveRedundantDbgInstrs(&BB); 1609 1610 return Changed; 1611 } 1612 1613 /// Propagate dbg.value intrinsics through the newly inserted PHIs. 1614 void llvm::insertDebugValuesForPHIs(BasicBlock *BB, 1615 SmallVectorImpl<PHINode *> &InsertedPHIs) { 1616 assert(BB && "No BasicBlock to clone dbg.value(s) from."); 1617 if (InsertedPHIs.size() == 0) 1618 return; 1619 1620 // Map existing PHI nodes to their dbg.values. 1621 ValueToValueMapTy DbgValueMap; 1622 for (auto &I : *BB) { 1623 if (auto DbgII = dyn_cast<DbgVariableIntrinsic>(&I)) { 1624 for (Value *V : DbgII->location_ops()) 1625 if (auto *Loc = dyn_cast_or_null<PHINode>(V)) 1626 DbgValueMap.insert({Loc, DbgII}); 1627 } 1628 } 1629 if (DbgValueMap.size() == 0) 1630 return; 1631 1632 // Map a pair of the destination BB and old dbg.value to the new dbg.value, 1633 // so that if a dbg.value is being rewritten to use more than one of the 1634 // inserted PHIs in the same destination BB, we can update the same dbg.value 1635 // with all the new PHIs instead of creating one copy for each. 1636 MapVector<std::pair<BasicBlock *, DbgVariableIntrinsic *>, 1637 DbgVariableIntrinsic *> 1638 NewDbgValueMap; 1639 // Then iterate through the new PHIs and look to see if they use one of the 1640 // previously mapped PHIs. If so, create a new dbg.value intrinsic that will 1641 // propagate the info through the new PHI. If we use more than one new PHI in 1642 // a single destination BB with the same old dbg.value, merge the updates so 1643 // that we get a single new dbg.value with all the new PHIs. 1644 for (auto PHI : InsertedPHIs) { 1645 BasicBlock *Parent = PHI->getParent(); 1646 // Avoid inserting an intrinsic into an EH block. 1647 if (Parent->getFirstNonPHI()->isEHPad()) 1648 continue; 1649 for (auto VI : PHI->operand_values()) { 1650 auto V = DbgValueMap.find(VI); 1651 if (V != DbgValueMap.end()) { 1652 auto *DbgII = cast<DbgVariableIntrinsic>(V->second); 1653 auto NewDI = NewDbgValueMap.find({Parent, DbgII}); 1654 if (NewDI == NewDbgValueMap.end()) { 1655 auto *NewDbgII = cast<DbgVariableIntrinsic>(DbgII->clone()); 1656 NewDI = NewDbgValueMap.insert({{Parent, DbgII}, NewDbgII}).first; 1657 } 1658 DbgVariableIntrinsic *NewDbgII = NewDI->second; 1659 // If PHI contains VI as an operand more than once, we may 1660 // replaced it in NewDbgII; confirm that it is present. 1661 if (is_contained(NewDbgII->location_ops(), VI)) 1662 NewDbgII->replaceVariableLocationOp(VI, PHI); 1663 } 1664 } 1665 } 1666 // Insert thew new dbg.values into their destination blocks. 1667 for (auto DI : NewDbgValueMap) { 1668 BasicBlock *Parent = DI.first.first; 1669 auto *NewDbgII = DI.second; 1670 auto InsertionPt = Parent->getFirstInsertionPt(); 1671 assert(InsertionPt != Parent->end() && "Ill-formed basic block"); 1672 NewDbgII->insertBefore(&*InsertionPt); 1673 } 1674 } 1675 1676 bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress, 1677 DIBuilder &Builder, uint8_t DIExprFlags, 1678 int Offset) { 1679 auto DbgAddrs = FindDbgAddrUses(Address); 1680 for (DbgVariableIntrinsic *DII : DbgAddrs) { 1681 const DebugLoc &Loc = DII->getDebugLoc(); 1682 auto *DIVar = DII->getVariable(); 1683 auto *DIExpr = DII->getExpression(); 1684 assert(DIVar && "Missing variable"); 1685 DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset); 1686 // Insert llvm.dbg.declare immediately before DII, and remove old 1687 // llvm.dbg.declare. 1688 Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, DII); 1689 DII->eraseFromParent(); 1690 } 1691 return !DbgAddrs.empty(); 1692 } 1693 1694 static void replaceOneDbgValueForAlloca(DbgValueInst *DVI, Value *NewAddress, 1695 DIBuilder &Builder, int Offset) { 1696 const DebugLoc &Loc = DVI->getDebugLoc(); 1697 auto *DIVar = DVI->getVariable(); 1698 auto *DIExpr = DVI->getExpression(); 1699 assert(DIVar && "Missing variable"); 1700 1701 // This is an alloca-based llvm.dbg.value. The first thing it should do with 1702 // the alloca pointer is dereference it. Otherwise we don't know how to handle 1703 // it and give up. 1704 if (!DIExpr || DIExpr->getNumElements() < 1 || 1705 DIExpr->getElement(0) != dwarf::DW_OP_deref) 1706 return; 1707 1708 // Insert the offset before the first deref. 1709 // We could just change the offset argument of dbg.value, but it's unsigned... 1710 if (Offset) 1711 DIExpr = DIExpression::prepend(DIExpr, 0, Offset); 1712 1713 Builder.insertDbgValueIntrinsic(NewAddress, DIVar, DIExpr, Loc, DVI); 1714 DVI->eraseFromParent(); 1715 } 1716 1717 void llvm::replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress, 1718 DIBuilder &Builder, int Offset) { 1719 if (auto *L = LocalAsMetadata::getIfExists(AI)) 1720 if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L)) 1721 for (Use &U : llvm::make_early_inc_range(MDV->uses())) 1722 if (auto *DVI = dyn_cast<DbgValueInst>(U.getUser())) 1723 replaceOneDbgValueForAlloca(DVI, NewAllocaAddress, Builder, Offset); 1724 } 1725 1726 /// Where possible to salvage debug information for \p I do so 1727 /// and return True. If not possible mark undef and return False. 1728 void llvm::salvageDebugInfo(Instruction &I) { 1729 SmallVector<DbgVariableIntrinsic *, 1> DbgUsers; 1730 findDbgUsers(DbgUsers, &I); 1731 salvageDebugInfoForDbgValues(I, DbgUsers); 1732 } 1733 1734 void llvm::salvageDebugInfoForDbgValues( 1735 Instruction &I, ArrayRef<DbgVariableIntrinsic *> DbgUsers) { 1736 // These are arbitrary chosen limits on the maximum number of values and the 1737 // maximum size of a debug expression we can salvage up to, used for 1738 // performance reasons. 1739 const unsigned MaxDebugArgs = 16; 1740 const unsigned MaxExpressionSize = 128; 1741 bool Salvaged = false; 1742 1743 for (auto *DII : DbgUsers) { 1744 // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they 1745 // are implicitly pointing out the value as a DWARF memory location 1746 // description. 1747 bool StackValue = isa<DbgValueInst>(DII); 1748 auto DIILocation = DII->location_ops(); 1749 assert( 1750 is_contained(DIILocation, &I) && 1751 "DbgVariableIntrinsic must use salvaged instruction as its location"); 1752 SmallVector<Value *, 4> AdditionalValues; 1753 // `I` may appear more than once in DII's location ops, and each use of `I` 1754 // must be updated in the DIExpression and potentially have additional 1755 // values added; thus we call salvageDebugInfoImpl for each `I` instance in 1756 // DIILocation. 1757 Value *Op0 = nullptr; 1758 DIExpression *SalvagedExpr = DII->getExpression(); 1759 auto LocItr = find(DIILocation, &I); 1760 while (SalvagedExpr && LocItr != DIILocation.end()) { 1761 SmallVector<uint64_t, 16> Ops; 1762 unsigned LocNo = std::distance(DIILocation.begin(), LocItr); 1763 uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands(); 1764 Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues); 1765 if (!Op0) 1766 break; 1767 SalvagedExpr = 1768 DIExpression::appendOpsToArg(SalvagedExpr, Ops, LocNo, StackValue); 1769 LocItr = std::find(++LocItr, DIILocation.end(), &I); 1770 } 1771 // salvageDebugInfoImpl should fail on examining the first element of 1772 // DbgUsers, or none of them. 1773 if (!Op0) 1774 break; 1775 1776 DII->replaceVariableLocationOp(&I, Op0); 1777 bool IsValidSalvageExpr = SalvagedExpr->getNumElements() <= MaxExpressionSize; 1778 if (AdditionalValues.empty() && IsValidSalvageExpr) { 1779 DII->setExpression(SalvagedExpr); 1780 } else if (isa<DbgValueInst>(DII) && IsValidSalvageExpr && 1781 DII->getNumVariableLocationOps() + AdditionalValues.size() <= 1782 MaxDebugArgs) { 1783 DII->addVariableLocationOps(AdditionalValues, SalvagedExpr); 1784 } else { 1785 // Do not salvage using DIArgList for dbg.addr/dbg.declare, as it is 1786 // currently only valid for stack value expressions. 1787 // Also do not salvage if the resulting DIArgList would contain an 1788 // unreasonably large number of values. 1789 Value *Undef = UndefValue::get(I.getOperand(0)->getType()); 1790 DII->replaceVariableLocationOp(I.getOperand(0), Undef); 1791 } 1792 LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n'); 1793 Salvaged = true; 1794 } 1795 1796 if (Salvaged) 1797 return; 1798 1799 for (auto *DII : DbgUsers) { 1800 Value *Undef = UndefValue::get(I.getType()); 1801 DII->replaceVariableLocationOp(&I, Undef); 1802 } 1803 } 1804 1805 Value *getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL, 1806 uint64_t CurrentLocOps, 1807 SmallVectorImpl<uint64_t> &Opcodes, 1808 SmallVectorImpl<Value *> &AdditionalValues) { 1809 unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace()); 1810 // Rewrite a GEP into a DIExpression. 1811 MapVector<Value *, APInt> VariableOffsets; 1812 APInt ConstantOffset(BitWidth, 0); 1813 if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) 1814 return nullptr; 1815 if (!VariableOffsets.empty() && !CurrentLocOps) { 1816 Opcodes.insert(Opcodes.begin(), {dwarf::DW_OP_LLVM_arg, 0}); 1817 CurrentLocOps = 1; 1818 } 1819 for (auto Offset : VariableOffsets) { 1820 AdditionalValues.push_back(Offset.first); 1821 assert(Offset.second.isStrictlyPositive() && 1822 "Expected strictly positive multiplier for offset."); 1823 Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps++, dwarf::DW_OP_constu, 1824 Offset.second.getZExtValue(), dwarf::DW_OP_mul, 1825 dwarf::DW_OP_plus}); 1826 } 1827 DIExpression::appendOffset(Opcodes, ConstantOffset.getSExtValue()); 1828 return GEP->getOperand(0); 1829 } 1830 1831 uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode) { 1832 switch (Opcode) { 1833 case Instruction::Add: 1834 return dwarf::DW_OP_plus; 1835 case Instruction::Sub: 1836 return dwarf::DW_OP_minus; 1837 case Instruction::Mul: 1838 return dwarf::DW_OP_mul; 1839 case Instruction::SDiv: 1840 return dwarf::DW_OP_div; 1841 case Instruction::SRem: 1842 return dwarf::DW_OP_mod; 1843 case Instruction::Or: 1844 return dwarf::DW_OP_or; 1845 case Instruction::And: 1846 return dwarf::DW_OP_and; 1847 case Instruction::Xor: 1848 return dwarf::DW_OP_xor; 1849 case Instruction::Shl: 1850 return dwarf::DW_OP_shl; 1851 case Instruction::LShr: 1852 return dwarf::DW_OP_shr; 1853 case Instruction::AShr: 1854 return dwarf::DW_OP_shra; 1855 default: 1856 // TODO: Salvage from each kind of binop we know about. 1857 return 0; 1858 } 1859 } 1860 1861 Value *getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps, 1862 SmallVectorImpl<uint64_t> &Opcodes, 1863 SmallVectorImpl<Value *> &AdditionalValues) { 1864 // Handle binary operations with constant integer operands as a special case. 1865 auto *ConstInt = dyn_cast<ConstantInt>(BI->getOperand(1)); 1866 // Values wider than 64 bits cannot be represented within a DIExpression. 1867 if (ConstInt && ConstInt->getBitWidth() > 64) 1868 return nullptr; 1869 1870 Instruction::BinaryOps BinOpcode = BI->getOpcode(); 1871 // Push any Constant Int operand onto the expression stack. 1872 if (ConstInt) { 1873 uint64_t Val = ConstInt->getSExtValue(); 1874 // Add or Sub Instructions with a constant operand can potentially be 1875 // simplified. 1876 if (BinOpcode == Instruction::Add || BinOpcode == Instruction::Sub) { 1877 uint64_t Offset = BinOpcode == Instruction::Add ? Val : -int64_t(Val); 1878 DIExpression::appendOffset(Opcodes, Offset); 1879 return BI->getOperand(0); 1880 } 1881 Opcodes.append({dwarf::DW_OP_constu, Val}); 1882 } else { 1883 if (!CurrentLocOps) { 1884 Opcodes.append({dwarf::DW_OP_LLVM_arg, 0}); 1885 CurrentLocOps = 1; 1886 } 1887 Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps}); 1888 AdditionalValues.push_back(BI->getOperand(1)); 1889 } 1890 1891 // Add salvaged binary operator to expression stack, if it has a valid 1892 // representation in a DIExpression. 1893 uint64_t DwarfBinOp = getDwarfOpForBinOp(BinOpcode); 1894 if (!DwarfBinOp) 1895 return nullptr; 1896 Opcodes.push_back(DwarfBinOp); 1897 return BI->getOperand(0); 1898 } 1899 1900 Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, 1901 SmallVectorImpl<uint64_t> &Ops, 1902 SmallVectorImpl<Value *> &AdditionalValues) { 1903 auto &M = *I.getModule(); 1904 auto &DL = M.getDataLayout(); 1905 1906 if (auto *CI = dyn_cast<CastInst>(&I)) { 1907 Value *FromValue = CI->getOperand(0); 1908 // No-op casts are irrelevant for debug info. 1909 if (CI->isNoopCast(DL)) { 1910 return FromValue; 1911 } 1912 1913 Type *Type = CI->getType(); 1914 if (Type->isPointerTy()) 1915 Type = DL.getIntPtrType(Type); 1916 // Casts other than Trunc, SExt, or ZExt to scalar types cannot be salvaged. 1917 if (Type->isVectorTy() || 1918 !(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I) || 1919 isa<IntToPtrInst>(&I) || isa<PtrToIntInst>(&I))) 1920 return nullptr; 1921 1922 llvm::Type *FromType = FromValue->getType(); 1923 if (FromType->isPointerTy()) 1924 FromType = DL.getIntPtrType(FromType); 1925 1926 unsigned FromTypeBitSize = FromType->getScalarSizeInBits(); 1927 unsigned ToTypeBitSize = Type->getScalarSizeInBits(); 1928 1929 auto ExtOps = DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize, 1930 isa<SExtInst>(&I)); 1931 Ops.append(ExtOps.begin(), ExtOps.end()); 1932 return FromValue; 1933 } 1934 1935 if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) 1936 return getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues); 1937 if (auto *BI = dyn_cast<BinaryOperator>(&I)) 1938 return getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues); 1939 1940 // *Not* to do: we should not attempt to salvage load instructions, 1941 // because the validity and lifetime of a dbg.value containing 1942 // DW_OP_deref becomes difficult to analyze. See PR40628 for examples. 1943 return nullptr; 1944 } 1945 1946 /// A replacement for a dbg.value expression. 1947 using DbgValReplacement = Optional<DIExpression *>; 1948 1949 /// Point debug users of \p From to \p To using exprs given by \p RewriteExpr, 1950 /// possibly moving/undefing users to prevent use-before-def. Returns true if 1951 /// changes are made. 1952 static bool rewriteDebugUsers( 1953 Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT, 1954 function_ref<DbgValReplacement(DbgVariableIntrinsic &DII)> RewriteExpr) { 1955 // Find debug users of From. 1956 SmallVector<DbgVariableIntrinsic *, 1> Users; 1957 findDbgUsers(Users, &From); 1958 if (Users.empty()) 1959 return false; 1960 1961 // Prevent use-before-def of To. 1962 bool Changed = false; 1963 SmallPtrSet<DbgVariableIntrinsic *, 1> UndefOrSalvage; 1964 if (isa<Instruction>(&To)) { 1965 bool DomPointAfterFrom = From.getNextNonDebugInstruction() == &DomPoint; 1966 1967 for (auto *DII : Users) { 1968 // It's common to see a debug user between From and DomPoint. Move it 1969 // after DomPoint to preserve the variable update without any reordering. 1970 if (DomPointAfterFrom && DII->getNextNonDebugInstruction() == &DomPoint) { 1971 LLVM_DEBUG(dbgs() << "MOVE: " << *DII << '\n'); 1972 DII->moveAfter(&DomPoint); 1973 Changed = true; 1974 1975 // Users which otherwise aren't dominated by the replacement value must 1976 // be salvaged or deleted. 1977 } else if (!DT.dominates(&DomPoint, DII)) { 1978 UndefOrSalvage.insert(DII); 1979 } 1980 } 1981 } 1982 1983 // Update debug users without use-before-def risk. 1984 for (auto *DII : Users) { 1985 if (UndefOrSalvage.count(DII)) 1986 continue; 1987 1988 DbgValReplacement DVR = RewriteExpr(*DII); 1989 if (!DVR) 1990 continue; 1991 1992 DII->replaceVariableLocationOp(&From, &To); 1993 DII->setExpression(*DVR); 1994 LLVM_DEBUG(dbgs() << "REWRITE: " << *DII << '\n'); 1995 Changed = true; 1996 } 1997 1998 if (!UndefOrSalvage.empty()) { 1999 // Try to salvage the remaining debug users. 2000 salvageDebugInfo(From); 2001 Changed = true; 2002 } 2003 2004 return Changed; 2005 } 2006 2007 /// Check if a bitcast between a value of type \p FromTy to type \p ToTy would 2008 /// losslessly preserve the bits and semantics of the value. This predicate is 2009 /// symmetric, i.e swapping \p FromTy and \p ToTy should give the same result. 2010 /// 2011 /// Note that Type::canLosslesslyBitCastTo is not suitable here because it 2012 /// allows semantically unequivalent bitcasts, such as <2 x i64> -> <4 x i32>, 2013 /// and also does not allow lossless pointer <-> integer conversions. 2014 static bool isBitCastSemanticsPreserving(const DataLayout &DL, Type *FromTy, 2015 Type *ToTy) { 2016 // Trivially compatible types. 2017 if (FromTy == ToTy) 2018 return true; 2019 2020 // Handle compatible pointer <-> integer conversions. 2021 if (FromTy->isIntOrPtrTy() && ToTy->isIntOrPtrTy()) { 2022 bool SameSize = DL.getTypeSizeInBits(FromTy) == DL.getTypeSizeInBits(ToTy); 2023 bool LosslessConversion = !DL.isNonIntegralPointerType(FromTy) && 2024 !DL.isNonIntegralPointerType(ToTy); 2025 return SameSize && LosslessConversion; 2026 } 2027 2028 // TODO: This is not exhaustive. 2029 return false; 2030 } 2031 2032 bool llvm::replaceAllDbgUsesWith(Instruction &From, Value &To, 2033 Instruction &DomPoint, DominatorTree &DT) { 2034 // Exit early if From has no debug users. 2035 if (!From.isUsedByMetadata()) 2036 return false; 2037 2038 assert(&From != &To && "Can't replace something with itself"); 2039 2040 Type *FromTy = From.getType(); 2041 Type *ToTy = To.getType(); 2042 2043 auto Identity = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement { 2044 return DII.getExpression(); 2045 }; 2046 2047 // Handle no-op conversions. 2048 Module &M = *From.getModule(); 2049 const DataLayout &DL = M.getDataLayout(); 2050 if (isBitCastSemanticsPreserving(DL, FromTy, ToTy)) 2051 return rewriteDebugUsers(From, To, DomPoint, DT, Identity); 2052 2053 // Handle integer-to-integer widening and narrowing. 2054 // FIXME: Use DW_OP_convert when it's available everywhere. 2055 if (FromTy->isIntegerTy() && ToTy->isIntegerTy()) { 2056 uint64_t FromBits = FromTy->getPrimitiveSizeInBits(); 2057 uint64_t ToBits = ToTy->getPrimitiveSizeInBits(); 2058 assert(FromBits != ToBits && "Unexpected no-op conversion"); 2059 2060 // When the width of the result grows, assume that a debugger will only 2061 // access the low `FromBits` bits when inspecting the source variable. 2062 if (FromBits < ToBits) 2063 return rewriteDebugUsers(From, To, DomPoint, DT, Identity); 2064 2065 // The width of the result has shrunk. Use sign/zero extension to describe 2066 // the source variable's high bits. 2067 auto SignOrZeroExt = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement { 2068 DILocalVariable *Var = DII.getVariable(); 2069 2070 // Without knowing signedness, sign/zero extension isn't possible. 2071 auto Signedness = Var->getSignedness(); 2072 if (!Signedness) 2073 return None; 2074 2075 bool Signed = *Signedness == DIBasicType::Signedness::Signed; 2076 return DIExpression::appendExt(DII.getExpression(), ToBits, FromBits, 2077 Signed); 2078 }; 2079 return rewriteDebugUsers(From, To, DomPoint, DT, SignOrZeroExt); 2080 } 2081 2082 // TODO: Floating-point conversions, vectors. 2083 return false; 2084 } 2085 2086 std::pair<unsigned, unsigned> 2087 llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) { 2088 unsigned NumDeadInst = 0; 2089 unsigned NumDeadDbgInst = 0; 2090 // Delete the instructions backwards, as it has a reduced likelihood of 2091 // having to update as many def-use and use-def chains. 2092 Instruction *EndInst = BB->getTerminator(); // Last not to be deleted. 2093 while (EndInst != &BB->front()) { 2094 // Delete the next to last instruction. 2095 Instruction *Inst = &*--EndInst->getIterator(); 2096 if (!Inst->use_empty() && !Inst->getType()->isTokenTy()) 2097 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); 2098 if (Inst->isEHPad() || Inst->getType()->isTokenTy()) { 2099 EndInst = Inst; 2100 continue; 2101 } 2102 if (isa<DbgInfoIntrinsic>(Inst)) 2103 ++NumDeadDbgInst; 2104 else 2105 ++NumDeadInst; 2106 Inst->eraseFromParent(); 2107 } 2108 return {NumDeadInst, NumDeadDbgInst}; 2109 } 2110 2111 unsigned llvm::changeToUnreachable(Instruction *I, bool PreserveLCSSA, 2112 DomTreeUpdater *DTU, 2113 MemorySSAUpdater *MSSAU) { 2114 BasicBlock *BB = I->getParent(); 2115 2116 if (MSSAU) 2117 MSSAU->changeToUnreachable(I); 2118 2119 SmallSet<BasicBlock *, 8> UniqueSuccessors; 2120 2121 // Loop over all of the successors, removing BB's entry from any PHI 2122 // nodes. 2123 for (BasicBlock *Successor : successors(BB)) { 2124 Successor->removePredecessor(BB, PreserveLCSSA); 2125 if (DTU) 2126 UniqueSuccessors.insert(Successor); 2127 } 2128 auto *UI = new UnreachableInst(I->getContext(), I); 2129 UI->setDebugLoc(I->getDebugLoc()); 2130 2131 // All instructions after this are dead. 2132 unsigned NumInstrsRemoved = 0; 2133 BasicBlock::iterator BBI = I->getIterator(), BBE = BB->end(); 2134 while (BBI != BBE) { 2135 if (!BBI->use_empty()) 2136 BBI->replaceAllUsesWith(UndefValue::get(BBI->getType())); 2137 BB->getInstList().erase(BBI++); 2138 ++NumInstrsRemoved; 2139 } 2140 if (DTU) { 2141 SmallVector<DominatorTree::UpdateType, 8> Updates; 2142 Updates.reserve(UniqueSuccessors.size()); 2143 for (BasicBlock *UniqueSuccessor : UniqueSuccessors) 2144 Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor}); 2145 DTU->applyUpdates(Updates); 2146 } 2147 return NumInstrsRemoved; 2148 } 2149 2150 CallInst *llvm::createCallMatchingInvoke(InvokeInst *II) { 2151 SmallVector<Value *, 8> Args(II->args()); 2152 SmallVector<OperandBundleDef, 1> OpBundles; 2153 II->getOperandBundlesAsDefs(OpBundles); 2154 CallInst *NewCall = CallInst::Create(II->getFunctionType(), 2155 II->getCalledOperand(), Args, OpBundles); 2156 NewCall->setCallingConv(II->getCallingConv()); 2157 NewCall->setAttributes(II->getAttributes()); 2158 NewCall->setDebugLoc(II->getDebugLoc()); 2159 NewCall->copyMetadata(*II); 2160 2161 // If the invoke had profile metadata, try converting them for CallInst. 2162 uint64_t TotalWeight; 2163 if (NewCall->extractProfTotalWeight(TotalWeight)) { 2164 // Set the total weight if it fits into i32, otherwise reset. 2165 MDBuilder MDB(NewCall->getContext()); 2166 auto NewWeights = uint32_t(TotalWeight) != TotalWeight 2167 ? nullptr 2168 : MDB.createBranchWeights({uint32_t(TotalWeight)}); 2169 NewCall->setMetadata(LLVMContext::MD_prof, NewWeights); 2170 } 2171 2172 return NewCall; 2173 } 2174 2175 /// changeToCall - Convert the specified invoke into a normal call. 2176 void llvm::changeToCall(InvokeInst *II, DomTreeUpdater *DTU) { 2177 CallInst *NewCall = createCallMatchingInvoke(II); 2178 NewCall->takeName(II); 2179 NewCall->insertBefore(II); 2180 II->replaceAllUsesWith(NewCall); 2181 2182 // Follow the call by a branch to the normal destination. 2183 BasicBlock *NormalDestBB = II->getNormalDest(); 2184 BranchInst::Create(NormalDestBB, II); 2185 2186 // Update PHI nodes in the unwind destination 2187 BasicBlock *BB = II->getParent(); 2188 BasicBlock *UnwindDestBB = II->getUnwindDest(); 2189 UnwindDestBB->removePredecessor(BB); 2190 II->eraseFromParent(); 2191 if (DTU) 2192 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}}); 2193 } 2194 2195 BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI, 2196 BasicBlock *UnwindEdge, 2197 DomTreeUpdater *DTU) { 2198 BasicBlock *BB = CI->getParent(); 2199 2200 // Convert this function call into an invoke instruction. First, split the 2201 // basic block. 2202 BasicBlock *Split = SplitBlock(BB, CI, DTU, /*LI=*/nullptr, /*MSSAU*/ nullptr, 2203 CI->getName() + ".noexc"); 2204 2205 // Delete the unconditional branch inserted by SplitBlock 2206 BB->getInstList().pop_back(); 2207 2208 // Create the new invoke instruction. 2209 SmallVector<Value *, 8> InvokeArgs(CI->args()); 2210 SmallVector<OperandBundleDef, 1> OpBundles; 2211 2212 CI->getOperandBundlesAsDefs(OpBundles); 2213 2214 // Note: we're round tripping operand bundles through memory here, and that 2215 // can potentially be avoided with a cleverer API design that we do not have 2216 // as of this time. 2217 2218 InvokeInst *II = 2219 InvokeInst::Create(CI->getFunctionType(), CI->getCalledOperand(), Split, 2220 UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB); 2221 II->setDebugLoc(CI->getDebugLoc()); 2222 II->setCallingConv(CI->getCallingConv()); 2223 II->setAttributes(CI->getAttributes()); 2224 2225 if (DTU) 2226 DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}}); 2227 2228 // Make sure that anything using the call now uses the invoke! This also 2229 // updates the CallGraph if present, because it uses a WeakTrackingVH. 2230 CI->replaceAllUsesWith(II); 2231 2232 // Delete the original call 2233 Split->getInstList().pop_front(); 2234 return Split; 2235 } 2236 2237 static bool markAliveBlocks(Function &F, 2238 SmallPtrSetImpl<BasicBlock *> &Reachable, 2239 DomTreeUpdater *DTU = nullptr) { 2240 SmallVector<BasicBlock*, 128> Worklist; 2241 BasicBlock *BB = &F.front(); 2242 Worklist.push_back(BB); 2243 Reachable.insert(BB); 2244 bool Changed = false; 2245 do { 2246 BB = Worklist.pop_back_val(); 2247 2248 // Do a quick scan of the basic block, turning any obviously unreachable 2249 // instructions into LLVM unreachable insts. The instruction combining pass 2250 // canonicalizes unreachable insts into stores to null or undef. 2251 for (Instruction &I : *BB) { 2252 if (auto *CI = dyn_cast<CallInst>(&I)) { 2253 Value *Callee = CI->getCalledOperand(); 2254 // Handle intrinsic calls. 2255 if (Function *F = dyn_cast<Function>(Callee)) { 2256 auto IntrinsicID = F->getIntrinsicID(); 2257 // Assumptions that are known to be false are equivalent to 2258 // unreachable. Also, if the condition is undefined, then we make the 2259 // choice most beneficial to the optimizer, and choose that to also be 2260 // unreachable. 2261 if (IntrinsicID == Intrinsic::assume) { 2262 if (match(CI->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) { 2263 // Don't insert a call to llvm.trap right before the unreachable. 2264 changeToUnreachable(CI, false, DTU); 2265 Changed = true; 2266 break; 2267 } 2268 } else if (IntrinsicID == Intrinsic::experimental_guard) { 2269 // A call to the guard intrinsic bails out of the current 2270 // compilation unit if the predicate passed to it is false. If the 2271 // predicate is a constant false, then we know the guard will bail 2272 // out of the current compile unconditionally, so all code following 2273 // it is dead. 2274 // 2275 // Note: unlike in llvm.assume, it is not "obviously profitable" for 2276 // guards to treat `undef` as `false` since a guard on `undef` can 2277 // still be useful for widening. 2278 if (match(CI->getArgOperand(0), m_Zero())) 2279 if (!isa<UnreachableInst>(CI->getNextNode())) { 2280 changeToUnreachable(CI->getNextNode(), false, DTU); 2281 Changed = true; 2282 break; 2283 } 2284 } 2285 } else if ((isa<ConstantPointerNull>(Callee) && 2286 !NullPointerIsDefined(CI->getFunction())) || 2287 isa<UndefValue>(Callee)) { 2288 changeToUnreachable(CI, false, DTU); 2289 Changed = true; 2290 break; 2291 } 2292 if (CI->doesNotReturn() && !CI->isMustTailCall()) { 2293 // If we found a call to a no-return function, insert an unreachable 2294 // instruction after it. Make sure there isn't *already* one there 2295 // though. 2296 if (!isa<UnreachableInst>(CI->getNextNode())) { 2297 // Don't insert a call to llvm.trap right before the unreachable. 2298 changeToUnreachable(CI->getNextNode(), false, DTU); 2299 Changed = true; 2300 } 2301 break; 2302 } 2303 } else if (auto *SI = dyn_cast<StoreInst>(&I)) { 2304 // Store to undef and store to null are undefined and used to signal 2305 // that they should be changed to unreachable by passes that can't 2306 // modify the CFG. 2307 2308 // Don't touch volatile stores. 2309 if (SI->isVolatile()) continue; 2310 2311 Value *Ptr = SI->getOperand(1); 2312 2313 if (isa<UndefValue>(Ptr) || 2314 (isa<ConstantPointerNull>(Ptr) && 2315 !NullPointerIsDefined(SI->getFunction(), 2316 SI->getPointerAddressSpace()))) { 2317 changeToUnreachable(SI, false, DTU); 2318 Changed = true; 2319 break; 2320 } 2321 } 2322 } 2323 2324 Instruction *Terminator = BB->getTerminator(); 2325 if (auto *II = dyn_cast<InvokeInst>(Terminator)) { 2326 // Turn invokes that call 'nounwind' functions into ordinary calls. 2327 Value *Callee = II->getCalledOperand(); 2328 if ((isa<ConstantPointerNull>(Callee) && 2329 !NullPointerIsDefined(BB->getParent())) || 2330 isa<UndefValue>(Callee)) { 2331 changeToUnreachable(II, false, DTU); 2332 Changed = true; 2333 } else if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) { 2334 if (II->use_empty() && II->onlyReadsMemory()) { 2335 // jump to the normal destination branch. 2336 BasicBlock *NormalDestBB = II->getNormalDest(); 2337 BasicBlock *UnwindDestBB = II->getUnwindDest(); 2338 BranchInst::Create(NormalDestBB, II); 2339 UnwindDestBB->removePredecessor(II->getParent()); 2340 II->eraseFromParent(); 2341 if (DTU) 2342 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}}); 2343 } else 2344 changeToCall(II, DTU); 2345 Changed = true; 2346 } 2347 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Terminator)) { 2348 // Remove catchpads which cannot be reached. 2349 struct CatchPadDenseMapInfo { 2350 static CatchPadInst *getEmptyKey() { 2351 return DenseMapInfo<CatchPadInst *>::getEmptyKey(); 2352 } 2353 2354 static CatchPadInst *getTombstoneKey() { 2355 return DenseMapInfo<CatchPadInst *>::getTombstoneKey(); 2356 } 2357 2358 static unsigned getHashValue(CatchPadInst *CatchPad) { 2359 return static_cast<unsigned>(hash_combine_range( 2360 CatchPad->value_op_begin(), CatchPad->value_op_end())); 2361 } 2362 2363 static bool isEqual(CatchPadInst *LHS, CatchPadInst *RHS) { 2364 if (LHS == getEmptyKey() || LHS == getTombstoneKey() || 2365 RHS == getEmptyKey() || RHS == getTombstoneKey()) 2366 return LHS == RHS; 2367 return LHS->isIdenticalTo(RHS); 2368 } 2369 }; 2370 2371 SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases; 2372 // Set of unique CatchPads. 2373 SmallDenseMap<CatchPadInst *, detail::DenseSetEmpty, 4, 2374 CatchPadDenseMapInfo, detail::DenseSetPair<CatchPadInst *>> 2375 HandlerSet; 2376 detail::DenseSetEmpty Empty; 2377 for (CatchSwitchInst::handler_iterator I = CatchSwitch->handler_begin(), 2378 E = CatchSwitch->handler_end(); 2379 I != E; ++I) { 2380 BasicBlock *HandlerBB = *I; 2381 if (DTU) 2382 ++NumPerSuccessorCases[HandlerBB]; 2383 auto *CatchPad = cast<CatchPadInst>(HandlerBB->getFirstNonPHI()); 2384 if (!HandlerSet.insert({CatchPad, Empty}).second) { 2385 if (DTU) 2386 --NumPerSuccessorCases[HandlerBB]; 2387 CatchSwitch->removeHandler(I); 2388 --I; 2389 --E; 2390 Changed = true; 2391 } 2392 } 2393 if (DTU) { 2394 std::vector<DominatorTree::UpdateType> Updates; 2395 for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases) 2396 if (I.second == 0) 2397 Updates.push_back({DominatorTree::Delete, BB, I.first}); 2398 DTU->applyUpdates(Updates); 2399 } 2400 } 2401 2402 Changed |= ConstantFoldTerminator(BB, true, nullptr, DTU); 2403 for (BasicBlock *Successor : successors(BB)) 2404 if (Reachable.insert(Successor).second) 2405 Worklist.push_back(Successor); 2406 } while (!Worklist.empty()); 2407 return Changed; 2408 } 2409 2410 void llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) { 2411 Instruction *TI = BB->getTerminator(); 2412 2413 if (auto *II = dyn_cast<InvokeInst>(TI)) { 2414 changeToCall(II, DTU); 2415 return; 2416 } 2417 2418 Instruction *NewTI; 2419 BasicBlock *UnwindDest; 2420 2421 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) { 2422 NewTI = CleanupReturnInst::Create(CRI->getCleanupPad(), nullptr, CRI); 2423 UnwindDest = CRI->getUnwindDest(); 2424 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) { 2425 auto *NewCatchSwitch = CatchSwitchInst::Create( 2426 CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(), 2427 CatchSwitch->getName(), CatchSwitch); 2428 for (BasicBlock *PadBB : CatchSwitch->handlers()) 2429 NewCatchSwitch->addHandler(PadBB); 2430 2431 NewTI = NewCatchSwitch; 2432 UnwindDest = CatchSwitch->getUnwindDest(); 2433 } else { 2434 llvm_unreachable("Could not find unwind successor"); 2435 } 2436 2437 NewTI->takeName(TI); 2438 NewTI->setDebugLoc(TI->getDebugLoc()); 2439 UnwindDest->removePredecessor(BB); 2440 TI->replaceAllUsesWith(NewTI); 2441 TI->eraseFromParent(); 2442 if (DTU) 2443 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDest}}); 2444 } 2445 2446 /// removeUnreachableBlocks - Remove blocks that are not reachable, even 2447 /// if they are in a dead cycle. Return true if a change was made, false 2448 /// otherwise. 2449 bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU, 2450 MemorySSAUpdater *MSSAU) { 2451 SmallPtrSet<BasicBlock *, 16> Reachable; 2452 bool Changed = markAliveBlocks(F, Reachable, DTU); 2453 2454 // If there are unreachable blocks in the CFG... 2455 if (Reachable.size() == F.size()) 2456 return Changed; 2457 2458 assert(Reachable.size() < F.size()); 2459 2460 // Are there any blocks left to actually delete? 2461 SmallSetVector<BasicBlock *, 8> BlocksToRemove; 2462 for (BasicBlock &BB : F) { 2463 // Skip reachable basic blocks 2464 if (Reachable.count(&BB)) 2465 continue; 2466 // Skip already-deleted blocks 2467 if (DTU && DTU->isBBPendingDeletion(&BB)) 2468 continue; 2469 BlocksToRemove.insert(&BB); 2470 } 2471 2472 if (BlocksToRemove.empty()) 2473 return Changed; 2474 2475 Changed = true; 2476 NumRemoved += BlocksToRemove.size(); 2477 2478 if (MSSAU) 2479 MSSAU->removeBlocks(BlocksToRemove); 2480 2481 DeleteDeadBlocks(BlocksToRemove.takeVector(), DTU); 2482 2483 return Changed; 2484 } 2485 2486 void llvm::combineMetadata(Instruction *K, const Instruction *J, 2487 ArrayRef<unsigned> KnownIDs, bool DoesKMove) { 2488 SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata; 2489 K->dropUnknownNonDebugMetadata(KnownIDs); 2490 K->getAllMetadataOtherThanDebugLoc(Metadata); 2491 for (const auto &MD : Metadata) { 2492 unsigned Kind = MD.first; 2493 MDNode *JMD = J->getMetadata(Kind); 2494 MDNode *KMD = MD.second; 2495 2496 switch (Kind) { 2497 default: 2498 K->setMetadata(Kind, nullptr); // Remove unknown metadata 2499 break; 2500 case LLVMContext::MD_dbg: 2501 llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg"); 2502 case LLVMContext::MD_tbaa: 2503 K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD)); 2504 break; 2505 case LLVMContext::MD_alias_scope: 2506 K->setMetadata(Kind, MDNode::getMostGenericAliasScope(JMD, KMD)); 2507 break; 2508 case LLVMContext::MD_noalias: 2509 case LLVMContext::MD_mem_parallel_loop_access: 2510 K->setMetadata(Kind, MDNode::intersect(JMD, KMD)); 2511 break; 2512 case LLVMContext::MD_access_group: 2513 K->setMetadata(LLVMContext::MD_access_group, 2514 intersectAccessGroups(K, J)); 2515 break; 2516 case LLVMContext::MD_range: 2517 2518 // If K does move, use most generic range. Otherwise keep the range of 2519 // K. 2520 if (DoesKMove) 2521 // FIXME: If K does move, we should drop the range info and nonnull. 2522 // Currently this function is used with DoesKMove in passes 2523 // doing hoisting/sinking and the current behavior of using the 2524 // most generic range is correct in those cases. 2525 K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD)); 2526 break; 2527 case LLVMContext::MD_fpmath: 2528 K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD)); 2529 break; 2530 case LLVMContext::MD_invariant_load: 2531 // Only set the !invariant.load if it is present in both instructions. 2532 K->setMetadata(Kind, JMD); 2533 break; 2534 case LLVMContext::MD_nonnull: 2535 // If K does move, keep nonull if it is present in both instructions. 2536 if (DoesKMove) 2537 K->setMetadata(Kind, JMD); 2538 break; 2539 case LLVMContext::MD_invariant_group: 2540 // Preserve !invariant.group in K. 2541 break; 2542 case LLVMContext::MD_align: 2543 K->setMetadata(Kind, 2544 MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD)); 2545 break; 2546 case LLVMContext::MD_dereferenceable: 2547 case LLVMContext::MD_dereferenceable_or_null: 2548 K->setMetadata(Kind, 2549 MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD)); 2550 break; 2551 case LLVMContext::MD_preserve_access_index: 2552 // Preserve !preserve.access.index in K. 2553 break; 2554 } 2555 } 2556 // Set !invariant.group from J if J has it. If both instructions have it 2557 // then we will just pick it from J - even when they are different. 2558 // Also make sure that K is load or store - f.e. combining bitcast with load 2559 // could produce bitcast with invariant.group metadata, which is invalid. 2560 // FIXME: we should try to preserve both invariant.group md if they are 2561 // different, but right now instruction can only have one invariant.group. 2562 if (auto *JMD = J->getMetadata(LLVMContext::MD_invariant_group)) 2563 if (isa<LoadInst>(K) || isa<StoreInst>(K)) 2564 K->setMetadata(LLVMContext::MD_invariant_group, JMD); 2565 } 2566 2567 void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J, 2568 bool KDominatesJ) { 2569 unsigned KnownIDs[] = { 2570 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, 2571 LLVMContext::MD_noalias, LLVMContext::MD_range, 2572 LLVMContext::MD_invariant_load, LLVMContext::MD_nonnull, 2573 LLVMContext::MD_invariant_group, LLVMContext::MD_align, 2574 LLVMContext::MD_dereferenceable, 2575 LLVMContext::MD_dereferenceable_or_null, 2576 LLVMContext::MD_access_group, LLVMContext::MD_preserve_access_index}; 2577 combineMetadata(K, J, KnownIDs, KDominatesJ); 2578 } 2579 2580 void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) { 2581 SmallVector<std::pair<unsigned, MDNode *>, 8> MD; 2582 Source.getAllMetadata(MD); 2583 MDBuilder MDB(Dest.getContext()); 2584 Type *NewType = Dest.getType(); 2585 const DataLayout &DL = Source.getModule()->getDataLayout(); 2586 for (const auto &MDPair : MD) { 2587 unsigned ID = MDPair.first; 2588 MDNode *N = MDPair.second; 2589 // Note, essentially every kind of metadata should be preserved here! This 2590 // routine is supposed to clone a load instruction changing *only its type*. 2591 // The only metadata it makes sense to drop is metadata which is invalidated 2592 // when the pointer type changes. This should essentially never be the case 2593 // in LLVM, but we explicitly switch over only known metadata to be 2594 // conservatively correct. If you are adding metadata to LLVM which pertains 2595 // to loads, you almost certainly want to add it here. 2596 switch (ID) { 2597 case LLVMContext::MD_dbg: 2598 case LLVMContext::MD_tbaa: 2599 case LLVMContext::MD_prof: 2600 case LLVMContext::MD_fpmath: 2601 case LLVMContext::MD_tbaa_struct: 2602 case LLVMContext::MD_invariant_load: 2603 case LLVMContext::MD_alias_scope: 2604 case LLVMContext::MD_noalias: 2605 case LLVMContext::MD_nontemporal: 2606 case LLVMContext::MD_mem_parallel_loop_access: 2607 case LLVMContext::MD_access_group: 2608 // All of these directly apply. 2609 Dest.setMetadata(ID, N); 2610 break; 2611 2612 case LLVMContext::MD_nonnull: 2613 copyNonnullMetadata(Source, N, Dest); 2614 break; 2615 2616 case LLVMContext::MD_align: 2617 case LLVMContext::MD_dereferenceable: 2618 case LLVMContext::MD_dereferenceable_or_null: 2619 // These only directly apply if the new type is also a pointer. 2620 if (NewType->isPointerTy()) 2621 Dest.setMetadata(ID, N); 2622 break; 2623 2624 case LLVMContext::MD_range: 2625 copyRangeMetadata(DL, Source, N, Dest); 2626 break; 2627 } 2628 } 2629 } 2630 2631 void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) { 2632 auto *ReplInst = dyn_cast<Instruction>(Repl); 2633 if (!ReplInst) 2634 return; 2635 2636 // Patch the replacement so that it is not more restrictive than the value 2637 // being replaced. 2638 // Note that if 'I' is a load being replaced by some operation, 2639 // for example, by an arithmetic operation, then andIRFlags() 2640 // would just erase all math flags from the original arithmetic 2641 // operation, which is clearly not wanted and not needed. 2642 if (!isa<LoadInst>(I)) 2643 ReplInst->andIRFlags(I); 2644 2645 // FIXME: If both the original and replacement value are part of the 2646 // same control-flow region (meaning that the execution of one 2647 // guarantees the execution of the other), then we can combine the 2648 // noalias scopes here and do better than the general conservative 2649 // answer used in combineMetadata(). 2650 2651 // In general, GVN unifies expressions over different control-flow 2652 // regions, and so we need a conservative combination of the noalias 2653 // scopes. 2654 static const unsigned KnownIDs[] = { 2655 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, 2656 LLVMContext::MD_noalias, LLVMContext::MD_range, 2657 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load, 2658 LLVMContext::MD_invariant_group, LLVMContext::MD_nonnull, 2659 LLVMContext::MD_access_group, LLVMContext::MD_preserve_access_index}; 2660 combineMetadata(ReplInst, I, KnownIDs, false); 2661 } 2662 2663 template <typename RootType, typename DominatesFn> 2664 static unsigned replaceDominatedUsesWith(Value *From, Value *To, 2665 const RootType &Root, 2666 const DominatesFn &Dominates) { 2667 assert(From->getType() == To->getType()); 2668 2669 unsigned Count = 0; 2670 for (Use &U : llvm::make_early_inc_range(From->uses())) { 2671 if (!Dominates(Root, U)) 2672 continue; 2673 U.set(To); 2674 LLVM_DEBUG(dbgs() << "Replace dominated use of '" << From->getName() 2675 << "' as " << *To << " in " << *U << "\n"); 2676 ++Count; 2677 } 2678 return Count; 2679 } 2680 2681 unsigned llvm::replaceNonLocalUsesWith(Instruction *From, Value *To) { 2682 assert(From->getType() == To->getType()); 2683 auto *BB = From->getParent(); 2684 unsigned Count = 0; 2685 2686 for (Use &U : llvm::make_early_inc_range(From->uses())) { 2687 auto *I = cast<Instruction>(U.getUser()); 2688 if (I->getParent() == BB) 2689 continue; 2690 U.set(To); 2691 ++Count; 2692 } 2693 return Count; 2694 } 2695 2696 unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, 2697 DominatorTree &DT, 2698 const BasicBlockEdge &Root) { 2699 auto Dominates = [&DT](const BasicBlockEdge &Root, const Use &U) { 2700 return DT.dominates(Root, U); 2701 }; 2702 return ::replaceDominatedUsesWith(From, To, Root, Dominates); 2703 } 2704 2705 unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, 2706 DominatorTree &DT, 2707 const BasicBlock *BB) { 2708 auto Dominates = [&DT](const BasicBlock *BB, const Use &U) { 2709 return DT.dominates(BB, U); 2710 }; 2711 return ::replaceDominatedUsesWith(From, To, BB, Dominates); 2712 } 2713 2714 bool llvm::callsGCLeafFunction(const CallBase *Call, 2715 const TargetLibraryInfo &TLI) { 2716 // Check if the function is specifically marked as a gc leaf function. 2717 if (Call->hasFnAttr("gc-leaf-function")) 2718 return true; 2719 if (const Function *F = Call->getCalledFunction()) { 2720 if (F->hasFnAttribute("gc-leaf-function")) 2721 return true; 2722 2723 if (auto IID = F->getIntrinsicID()) { 2724 // Most LLVM intrinsics do not take safepoints. 2725 return IID != Intrinsic::experimental_gc_statepoint && 2726 IID != Intrinsic::experimental_deoptimize && 2727 IID != Intrinsic::memcpy_element_unordered_atomic && 2728 IID != Intrinsic::memmove_element_unordered_atomic; 2729 } 2730 } 2731 2732 // Lib calls can be materialized by some passes, and won't be 2733 // marked as 'gc-leaf-function.' All available Libcalls are 2734 // GC-leaf. 2735 LibFunc LF; 2736 if (TLI.getLibFunc(*Call, LF)) { 2737 return TLI.has(LF); 2738 } 2739 2740 return false; 2741 } 2742 2743 void llvm::copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, 2744 LoadInst &NewLI) { 2745 auto *NewTy = NewLI.getType(); 2746 2747 // This only directly applies if the new type is also a pointer. 2748 if (NewTy->isPointerTy()) { 2749 NewLI.setMetadata(LLVMContext::MD_nonnull, N); 2750 return; 2751 } 2752 2753 // The only other translation we can do is to integral loads with !range 2754 // metadata. 2755 if (!NewTy->isIntegerTy()) 2756 return; 2757 2758 MDBuilder MDB(NewLI.getContext()); 2759 const Value *Ptr = OldLI.getPointerOperand(); 2760 auto *ITy = cast<IntegerType>(NewTy); 2761 auto *NullInt = ConstantExpr::getPtrToInt( 2762 ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy); 2763 auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1)); 2764 NewLI.setMetadata(LLVMContext::MD_range, 2765 MDB.createRange(NonNullInt, NullInt)); 2766 } 2767 2768 void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, 2769 MDNode *N, LoadInst &NewLI) { 2770 auto *NewTy = NewLI.getType(); 2771 2772 // Give up unless it is converted to a pointer where there is a single very 2773 // valuable mapping we can do reliably. 2774 // FIXME: It would be nice to propagate this in more ways, but the type 2775 // conversions make it hard. 2776 if (!NewTy->isPointerTy()) 2777 return; 2778 2779 unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy); 2780 if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) { 2781 MDNode *NN = MDNode::get(OldLI.getContext(), None); 2782 NewLI.setMetadata(LLVMContext::MD_nonnull, NN); 2783 } 2784 } 2785 2786 void llvm::dropDebugUsers(Instruction &I) { 2787 SmallVector<DbgVariableIntrinsic *, 1> DbgUsers; 2788 findDbgUsers(DbgUsers, &I); 2789 for (auto *DII : DbgUsers) 2790 DII->eraseFromParent(); 2791 } 2792 2793 void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt, 2794 BasicBlock *BB) { 2795 // Since we are moving the instructions out of its basic block, we do not 2796 // retain their original debug locations (DILocations) and debug intrinsic 2797 // instructions. 2798 // 2799 // Doing so would degrade the debugging experience and adversely affect the 2800 // accuracy of profiling information. 2801 // 2802 // Currently, when hoisting the instructions, we take the following actions: 2803 // - Remove their debug intrinsic instructions. 2804 // - Set their debug locations to the values from the insertion point. 2805 // 2806 // As per PR39141 (comment #8), the more fundamental reason why the dbg.values 2807 // need to be deleted, is because there will not be any instructions with a 2808 // DILocation in either branch left after performing the transformation. We 2809 // can only insert a dbg.value after the two branches are joined again. 2810 // 2811 // See PR38762, PR39243 for more details. 2812 // 2813 // TODO: Extend llvm.dbg.value to take more than one SSA Value (PR39141) to 2814 // encode predicated DIExpressions that yield different results on different 2815 // code paths. 2816 2817 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) { 2818 Instruction *I = &*II; 2819 I->dropUndefImplyingAttrsAndUnknownMetadata(); 2820 if (I->isUsedByMetadata()) 2821 dropDebugUsers(*I); 2822 if (I->isDebugOrPseudoInst()) { 2823 // Remove DbgInfo and pseudo probe Intrinsics. 2824 II = I->eraseFromParent(); 2825 continue; 2826 } 2827 I->setDebugLoc(InsertPt->getDebugLoc()); 2828 ++II; 2829 } 2830 DomBlock->getInstList().splice(InsertPt->getIterator(), BB->getInstList(), 2831 BB->begin(), 2832 BB->getTerminator()->getIterator()); 2833 } 2834 2835 namespace { 2836 2837 /// A potential constituent of a bitreverse or bswap expression. See 2838 /// collectBitParts for a fuller explanation. 2839 struct BitPart { 2840 BitPart(Value *P, unsigned BW) : Provider(P) { 2841 Provenance.resize(BW); 2842 } 2843 2844 /// The Value that this is a bitreverse/bswap of. 2845 Value *Provider; 2846 2847 /// The "provenance" of each bit. Provenance[A] = B means that bit A 2848 /// in Provider becomes bit B in the result of this expression. 2849 SmallVector<int8_t, 32> Provenance; // int8_t means max size is i128. 2850 2851 enum { Unset = -1 }; 2852 }; 2853 2854 } // end anonymous namespace 2855 2856 /// Analyze the specified subexpression and see if it is capable of providing 2857 /// pieces of a bswap or bitreverse. The subexpression provides a potential 2858 /// piece of a bswap or bitreverse if it can be proved that each non-zero bit in 2859 /// the output of the expression came from a corresponding bit in some other 2860 /// value. This function is recursive, and the end result is a mapping of 2861 /// bitnumber to bitnumber. It is the caller's responsibility to validate that 2862 /// the bitnumber to bitnumber mapping is correct for a bswap or bitreverse. 2863 /// 2864 /// For example, if the current subexpression if "(shl i32 %X, 24)" then we know 2865 /// that the expression deposits the low byte of %X into the high byte of the 2866 /// result and that all other bits are zero. This expression is accepted and a 2867 /// BitPart is returned with Provider set to %X and Provenance[24-31] set to 2868 /// [0-7]. 2869 /// 2870 /// For vector types, all analysis is performed at the per-element level. No 2871 /// cross-element analysis is supported (shuffle/insertion/reduction), and all 2872 /// constant masks must be splatted across all elements. 2873 /// 2874 /// To avoid revisiting values, the BitPart results are memoized into the 2875 /// provided map. To avoid unnecessary copying of BitParts, BitParts are 2876 /// constructed in-place in the \c BPS map. Because of this \c BPS needs to 2877 /// store BitParts objects, not pointers. As we need the concept of a nullptr 2878 /// BitParts (Value has been analyzed and the analysis failed), we an Optional 2879 /// type instead to provide the same functionality. 2880 /// 2881 /// Because we pass around references into \c BPS, we must use a container that 2882 /// does not invalidate internal references (std::map instead of DenseMap). 2883 static const Optional<BitPart> & 2884 collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, 2885 std::map<Value *, Optional<BitPart>> &BPS, int Depth, 2886 bool &FoundRoot) { 2887 auto I = BPS.find(V); 2888 if (I != BPS.end()) 2889 return I->second; 2890 2891 auto &Result = BPS[V] = None; 2892 auto BitWidth = V->getType()->getScalarSizeInBits(); 2893 2894 // Can't do integer/elements > 128 bits. 2895 if (BitWidth > 128) 2896 return Result; 2897 2898 // Prevent stack overflow by limiting the recursion depth 2899 if (Depth == BitPartRecursionMaxDepth) { 2900 LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n"); 2901 return Result; 2902 } 2903 2904 if (auto *I = dyn_cast<Instruction>(V)) { 2905 Value *X, *Y; 2906 const APInt *C; 2907 2908 // If this is an or instruction, it may be an inner node of the bswap. 2909 if (match(V, m_Or(m_Value(X), m_Value(Y)))) { 2910 // Check we have both sources and they are from the same provider. 2911 const auto &A = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, 2912 Depth + 1, FoundRoot); 2913 if (!A || !A->Provider) 2914 return Result; 2915 2916 const auto &B = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS, 2917 Depth + 1, FoundRoot); 2918 if (!B || A->Provider != B->Provider) 2919 return Result; 2920 2921 // Try and merge the two together. 2922 Result = BitPart(A->Provider, BitWidth); 2923 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) { 2924 if (A->Provenance[BitIdx] != BitPart::Unset && 2925 B->Provenance[BitIdx] != BitPart::Unset && 2926 A->Provenance[BitIdx] != B->Provenance[BitIdx]) 2927 return Result = None; 2928 2929 if (A->Provenance[BitIdx] == BitPart::Unset) 2930 Result->Provenance[BitIdx] = B->Provenance[BitIdx]; 2931 else 2932 Result->Provenance[BitIdx] = A->Provenance[BitIdx]; 2933 } 2934 2935 return Result; 2936 } 2937 2938 // If this is a logical shift by a constant, recurse then shift the result. 2939 if (match(V, m_LogicalShift(m_Value(X), m_APInt(C)))) { 2940 const APInt &BitShift = *C; 2941 2942 // Ensure the shift amount is defined. 2943 if (BitShift.uge(BitWidth)) 2944 return Result; 2945 2946 // For bswap-only, limit shift amounts to whole bytes, for an early exit. 2947 if (!MatchBitReversals && (BitShift.getZExtValue() % 8) != 0) 2948 return Result; 2949 2950 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, 2951 Depth + 1, FoundRoot); 2952 if (!Res) 2953 return Result; 2954 Result = Res; 2955 2956 // Perform the "shift" on BitProvenance. 2957 auto &P = Result->Provenance; 2958 if (I->getOpcode() == Instruction::Shl) { 2959 P.erase(std::prev(P.end(), BitShift.getZExtValue()), P.end()); 2960 P.insert(P.begin(), BitShift.getZExtValue(), BitPart::Unset); 2961 } else { 2962 P.erase(P.begin(), std::next(P.begin(), BitShift.getZExtValue())); 2963 P.insert(P.end(), BitShift.getZExtValue(), BitPart::Unset); 2964 } 2965 2966 return Result; 2967 } 2968 2969 // If this is a logical 'and' with a mask that clears bits, recurse then 2970 // unset the appropriate bits. 2971 if (match(V, m_And(m_Value(X), m_APInt(C)))) { 2972 const APInt &AndMask = *C; 2973 2974 // Check that the mask allows a multiple of 8 bits for a bswap, for an 2975 // early exit. 2976 unsigned NumMaskedBits = AndMask.countPopulation(); 2977 if (!MatchBitReversals && (NumMaskedBits % 8) != 0) 2978 return Result; 2979 2980 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, 2981 Depth + 1, FoundRoot); 2982 if (!Res) 2983 return Result; 2984 Result = Res; 2985 2986 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) 2987 // If the AndMask is zero for this bit, clear the bit. 2988 if (AndMask[BitIdx] == 0) 2989 Result->Provenance[BitIdx] = BitPart::Unset; 2990 return Result; 2991 } 2992 2993 // If this is a zext instruction zero extend the result. 2994 if (match(V, m_ZExt(m_Value(X)))) { 2995 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, 2996 Depth + 1, FoundRoot); 2997 if (!Res) 2998 return Result; 2999 3000 Result = BitPart(Res->Provider, BitWidth); 3001 auto NarrowBitWidth = X->getType()->getScalarSizeInBits(); 3002 for (unsigned BitIdx = 0; BitIdx < NarrowBitWidth; ++BitIdx) 3003 Result->Provenance[BitIdx] = Res->Provenance[BitIdx]; 3004 for (unsigned BitIdx = NarrowBitWidth; BitIdx < BitWidth; ++BitIdx) 3005 Result->Provenance[BitIdx] = BitPart::Unset; 3006 return Result; 3007 } 3008 3009 // If this is a truncate instruction, extract the lower bits. 3010 if (match(V, m_Trunc(m_Value(X)))) { 3011 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, 3012 Depth + 1, FoundRoot); 3013 if (!Res) 3014 return Result; 3015 3016 Result = BitPart(Res->Provider, BitWidth); 3017 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) 3018 Result->Provenance[BitIdx] = Res->Provenance[BitIdx]; 3019 return Result; 3020 } 3021 3022 // BITREVERSE - most likely due to us previous matching a partial 3023 // bitreverse. 3024 if (match(V, m_BitReverse(m_Value(X)))) { 3025 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, 3026 Depth + 1, FoundRoot); 3027 if (!Res) 3028 return Result; 3029 3030 Result = BitPart(Res->Provider, BitWidth); 3031 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) 3032 Result->Provenance[(BitWidth - 1) - BitIdx] = Res->Provenance[BitIdx]; 3033 return Result; 3034 } 3035 3036 // BSWAP - most likely due to us previous matching a partial bswap. 3037 if (match(V, m_BSwap(m_Value(X)))) { 3038 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, 3039 Depth + 1, FoundRoot); 3040 if (!Res) 3041 return Result; 3042 3043 unsigned ByteWidth = BitWidth / 8; 3044 Result = BitPart(Res->Provider, BitWidth); 3045 for (unsigned ByteIdx = 0; ByteIdx < ByteWidth; ++ByteIdx) { 3046 unsigned ByteBitOfs = ByteIdx * 8; 3047 for (unsigned BitIdx = 0; BitIdx < 8; ++BitIdx) 3048 Result->Provenance[(BitWidth - 8 - ByteBitOfs) + BitIdx] = 3049 Res->Provenance[ByteBitOfs + BitIdx]; 3050 } 3051 return Result; 3052 } 3053 3054 // Funnel 'double' shifts take 3 operands, 2 inputs and the shift 3055 // amount (modulo). 3056 // fshl(X,Y,Z): (X << (Z % BW)) | (Y >> (BW - (Z % BW))) 3057 // fshr(X,Y,Z): (X << (BW - (Z % BW))) | (Y >> (Z % BW)) 3058 if (match(V, m_FShl(m_Value(X), m_Value(Y), m_APInt(C))) || 3059 match(V, m_FShr(m_Value(X), m_Value(Y), m_APInt(C)))) { 3060 // We can treat fshr as a fshl by flipping the modulo amount. 3061 unsigned ModAmt = C->urem(BitWidth); 3062 if (cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fshr) 3063 ModAmt = BitWidth - ModAmt; 3064 3065 // For bswap-only, limit shift amounts to whole bytes, for an early exit. 3066 if (!MatchBitReversals && (ModAmt % 8) != 0) 3067 return Result; 3068 3069 // Check we have both sources and they are from the same provider. 3070 const auto &LHS = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS, 3071 Depth + 1, FoundRoot); 3072 if (!LHS || !LHS->Provider) 3073 return Result; 3074 3075 const auto &RHS = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS, 3076 Depth + 1, FoundRoot); 3077 if (!RHS || LHS->Provider != RHS->Provider) 3078 return Result; 3079 3080 unsigned StartBitRHS = BitWidth - ModAmt; 3081 Result = BitPart(LHS->Provider, BitWidth); 3082 for (unsigned BitIdx = 0; BitIdx < StartBitRHS; ++BitIdx) 3083 Result->Provenance[BitIdx + ModAmt] = LHS->Provenance[BitIdx]; 3084 for (unsigned BitIdx = 0; BitIdx < ModAmt; ++BitIdx) 3085 Result->Provenance[BitIdx] = RHS->Provenance[BitIdx + StartBitRHS]; 3086 return Result; 3087 } 3088 } 3089 3090 // If we've already found a root input value then we're never going to merge 3091 // these back together. 3092 if (FoundRoot) 3093 return Result; 3094 3095 // Okay, we got to something that isn't a shift, 'or', 'and', etc. This must 3096 // be the root input value to the bswap/bitreverse. 3097 FoundRoot = true; 3098 Result = BitPart(V, BitWidth); 3099 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) 3100 Result->Provenance[BitIdx] = BitIdx; 3101 return Result; 3102 } 3103 3104 static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To, 3105 unsigned BitWidth) { 3106 if (From % 8 != To % 8) 3107 return false; 3108 // Convert from bit indices to byte indices and check for a byte reversal. 3109 From >>= 3; 3110 To >>= 3; 3111 BitWidth >>= 3; 3112 return From == BitWidth - To - 1; 3113 } 3114 3115 static bool bitTransformIsCorrectForBitReverse(unsigned From, unsigned To, 3116 unsigned BitWidth) { 3117 return From == BitWidth - To - 1; 3118 } 3119 3120 bool llvm::recognizeBSwapOrBitReverseIdiom( 3121 Instruction *I, bool MatchBSwaps, bool MatchBitReversals, 3122 SmallVectorImpl<Instruction *> &InsertedInsts) { 3123 if (!match(I, m_Or(m_Value(), m_Value())) && 3124 !match(I, m_FShl(m_Value(), m_Value(), m_Value())) && 3125 !match(I, m_FShr(m_Value(), m_Value(), m_Value()))) 3126 return false; 3127 if (!MatchBSwaps && !MatchBitReversals) 3128 return false; 3129 Type *ITy = I->getType(); 3130 if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() > 128) 3131 return false; // Can't do integer/elements > 128 bits. 3132 3133 Type *DemandedTy = ITy; 3134 if (I->hasOneUse()) 3135 if (auto *Trunc = dyn_cast<TruncInst>(I->user_back())) 3136 DemandedTy = Trunc->getType(); 3137 3138 // Try to find all the pieces corresponding to the bswap. 3139 bool FoundRoot = false; 3140 std::map<Value *, Optional<BitPart>> BPS; 3141 const auto &Res = 3142 collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0, FoundRoot); 3143 if (!Res) 3144 return false; 3145 ArrayRef<int8_t> BitProvenance = Res->Provenance; 3146 assert(all_of(BitProvenance, 3147 [](int8_t I) { return I == BitPart::Unset || 0 <= I; }) && 3148 "Illegal bit provenance index"); 3149 3150 // If the upper bits are zero, then attempt to perform as a truncated op. 3151 if (BitProvenance.back() == BitPart::Unset) { 3152 while (!BitProvenance.empty() && BitProvenance.back() == BitPart::Unset) 3153 BitProvenance = BitProvenance.drop_back(); 3154 if (BitProvenance.empty()) 3155 return false; // TODO - handle null value? 3156 DemandedTy = Type::getIntNTy(I->getContext(), BitProvenance.size()); 3157 if (auto *IVecTy = dyn_cast<VectorType>(ITy)) 3158 DemandedTy = VectorType::get(DemandedTy, IVecTy); 3159 } 3160 3161 // Check BitProvenance hasn't found a source larger than the result type. 3162 unsigned DemandedBW = DemandedTy->getScalarSizeInBits(); 3163 if (DemandedBW > ITy->getScalarSizeInBits()) 3164 return false; 3165 3166 // Now, is the bit permutation correct for a bswap or a bitreverse? We can 3167 // only byteswap values with an even number of bytes. 3168 APInt DemandedMask = APInt::getAllOnes(DemandedBW); 3169 bool OKForBSwap = MatchBSwaps && (DemandedBW % 16) == 0; 3170 bool OKForBitReverse = MatchBitReversals; 3171 for (unsigned BitIdx = 0; 3172 (BitIdx < DemandedBW) && (OKForBSwap || OKForBitReverse); ++BitIdx) { 3173 if (BitProvenance[BitIdx] == BitPart::Unset) { 3174 DemandedMask.clearBit(BitIdx); 3175 continue; 3176 } 3177 OKForBSwap &= bitTransformIsCorrectForBSwap(BitProvenance[BitIdx], BitIdx, 3178 DemandedBW); 3179 OKForBitReverse &= bitTransformIsCorrectForBitReverse(BitProvenance[BitIdx], 3180 BitIdx, DemandedBW); 3181 } 3182 3183 Intrinsic::ID Intrin; 3184 if (OKForBSwap) 3185 Intrin = Intrinsic::bswap; 3186 else if (OKForBitReverse) 3187 Intrin = Intrinsic::bitreverse; 3188 else 3189 return false; 3190 3191 Function *F = Intrinsic::getDeclaration(I->getModule(), Intrin, DemandedTy); 3192 Value *Provider = Res->Provider; 3193 3194 // We may need to truncate the provider. 3195 if (DemandedTy != Provider->getType()) { 3196 auto *Trunc = 3197 CastInst::CreateIntegerCast(Provider, DemandedTy, false, "trunc", I); 3198 InsertedInsts.push_back(Trunc); 3199 Provider = Trunc; 3200 } 3201 3202 Instruction *Result = CallInst::Create(F, Provider, "rev", I); 3203 InsertedInsts.push_back(Result); 3204 3205 if (!DemandedMask.isAllOnes()) { 3206 auto *Mask = ConstantInt::get(DemandedTy, DemandedMask); 3207 Result = BinaryOperator::Create(Instruction::And, Result, Mask, "mask", I); 3208 InsertedInsts.push_back(Result); 3209 } 3210 3211 // We may need to zeroextend back to the result type. 3212 if (ITy != Result->getType()) { 3213 auto *ExtInst = CastInst::CreateIntegerCast(Result, ITy, false, "zext", I); 3214 InsertedInsts.push_back(ExtInst); 3215 } 3216 3217 return true; 3218 } 3219 3220 // CodeGen has special handling for some string functions that may replace 3221 // them with target-specific intrinsics. Since that'd skip our interceptors 3222 // in ASan/MSan/TSan/DFSan, and thus make us miss some memory accesses, 3223 // we mark affected calls as NoBuiltin, which will disable optimization 3224 // in CodeGen. 3225 void llvm::maybeMarkSanitizerLibraryCallNoBuiltin( 3226 CallInst *CI, const TargetLibraryInfo *TLI) { 3227 Function *F = CI->getCalledFunction(); 3228 LibFunc Func; 3229 if (F && !F->hasLocalLinkage() && F->hasName() && 3230 TLI->getLibFunc(F->getName(), Func) && TLI->hasOptimizedCodeGen(Func) && 3231 !F->doesNotAccessMemory()) 3232 CI->addFnAttr(Attribute::NoBuiltin); 3233 } 3234 3235 bool llvm::canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx) { 3236 // We can't have a PHI with a metadata type. 3237 if (I->getOperand(OpIdx)->getType()->isMetadataTy()) 3238 return false; 3239 3240 // Early exit. 3241 if (!isa<Constant>(I->getOperand(OpIdx))) 3242 return true; 3243 3244 switch (I->getOpcode()) { 3245 default: 3246 return true; 3247 case Instruction::Call: 3248 case Instruction::Invoke: { 3249 const auto &CB = cast<CallBase>(*I); 3250 3251 // Can't handle inline asm. Skip it. 3252 if (CB.isInlineAsm()) 3253 return false; 3254 3255 // Constant bundle operands may need to retain their constant-ness for 3256 // correctness. 3257 if (CB.isBundleOperand(OpIdx)) 3258 return false; 3259 3260 if (OpIdx < CB.arg_size()) { 3261 // Some variadic intrinsics require constants in the variadic arguments, 3262 // which currently aren't markable as immarg. 3263 if (isa<IntrinsicInst>(CB) && 3264 OpIdx >= CB.getFunctionType()->getNumParams()) { 3265 // This is known to be OK for stackmap. 3266 return CB.getIntrinsicID() == Intrinsic::experimental_stackmap; 3267 } 3268 3269 // gcroot is a special case, since it requires a constant argument which 3270 // isn't also required to be a simple ConstantInt. 3271 if (CB.getIntrinsicID() == Intrinsic::gcroot) 3272 return false; 3273 3274 // Some intrinsic operands are required to be immediates. 3275 return !CB.paramHasAttr(OpIdx, Attribute::ImmArg); 3276 } 3277 3278 // It is never allowed to replace the call argument to an intrinsic, but it 3279 // may be possible for a call. 3280 return !isa<IntrinsicInst>(CB); 3281 } 3282 case Instruction::ShuffleVector: 3283 // Shufflevector masks are constant. 3284 return OpIdx != 2; 3285 case Instruction::Switch: 3286 case Instruction::ExtractValue: 3287 // All operands apart from the first are constant. 3288 return OpIdx == 0; 3289 case Instruction::InsertValue: 3290 // All operands apart from the first and the second are constant. 3291 return OpIdx < 2; 3292 case Instruction::Alloca: 3293 // Static allocas (constant size in the entry block) are handled by 3294 // prologue/epilogue insertion so they're free anyway. We definitely don't 3295 // want to make them non-constant. 3296 return !cast<AllocaInst>(I)->isStaticAlloca(); 3297 case Instruction::GetElementPtr: 3298 if (OpIdx == 0) 3299 return true; 3300 gep_type_iterator It = gep_type_begin(I); 3301 for (auto E = std::next(It, OpIdx); It != E; ++It) 3302 if (It.isStruct()) 3303 return false; 3304 return true; 3305 } 3306 } 3307 3308 Value *llvm::invertCondition(Value *Condition) { 3309 // First: Check if it's a constant 3310 if (Constant *C = dyn_cast<Constant>(Condition)) 3311 return ConstantExpr::getNot(C); 3312 3313 // Second: If the condition is already inverted, return the original value 3314 Value *NotCondition; 3315 if (match(Condition, m_Not(m_Value(NotCondition)))) 3316 return NotCondition; 3317 3318 BasicBlock *Parent = nullptr; 3319 Instruction *Inst = dyn_cast<Instruction>(Condition); 3320 if (Inst) 3321 Parent = Inst->getParent(); 3322 else if (Argument *Arg = dyn_cast<Argument>(Condition)) 3323 Parent = &Arg->getParent()->getEntryBlock(); 3324 assert(Parent && "Unsupported condition to invert"); 3325 3326 // Third: Check all the users for an invert 3327 for (User *U : Condition->users()) 3328 if (Instruction *I = dyn_cast<Instruction>(U)) 3329 if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition)))) 3330 return I; 3331 3332 // Last option: Create a new instruction 3333 auto *Inverted = 3334 BinaryOperator::CreateNot(Condition, Condition->getName() + ".inv"); 3335 if (Inst && !isa<PHINode>(Inst)) 3336 Inverted->insertAfter(Inst); 3337 else 3338 Inverted->insertBefore(&*Parent->getFirstInsertionPt()); 3339 return Inverted; 3340 } 3341 3342 bool llvm::inferAttributesFromOthers(Function &F) { 3343 // Note: We explicitly check for attributes rather than using cover functions 3344 // because some of the cover functions include the logic being implemented. 3345 3346 bool Changed = false; 3347 // readnone + not convergent implies nosync 3348 if (!F.hasFnAttribute(Attribute::NoSync) && 3349 F.doesNotAccessMemory() && !F.isConvergent()) { 3350 F.setNoSync(); 3351 Changed = true; 3352 } 3353 3354 // readonly implies nofree 3355 if (!F.hasFnAttribute(Attribute::NoFree) && F.onlyReadsMemory()) { 3356 F.setDoesNotFreeMemory(); 3357 Changed = true; 3358 } 3359 3360 // willreturn implies mustprogress 3361 if (!F.hasFnAttribute(Attribute::MustProgress) && F.willReturn()) { 3362 F.setMustProgress(); 3363 Changed = true; 3364 } 3365 3366 // TODO: There are a bunch of cases of restrictive memory effects we 3367 // can infer by inspecting arguments of argmemonly-ish functions. 3368 3369 return Changed; 3370 } 3371