1 //===- InstCombineLoadStoreAlloca.cpp -------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the visit functions for load, store and alloca. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "InstCombineInternal.h" 15 #include "llvm/ADT/Statistic.h" 16 #include "llvm/Analysis/Loads.h" 17 #include "llvm/IR/DataLayout.h" 18 #include "llvm/IR/LLVMContext.h" 19 #include "llvm/IR/IntrinsicInst.h" 20 #include "llvm/IR/MDBuilder.h" 21 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 22 #include "llvm/Transforms/Utils/Local.h" 23 using namespace llvm; 24 25 #define DEBUG_TYPE "instcombine" 26 27 STATISTIC(NumDeadStore, "Number of dead stores eliminated"); 28 STATISTIC(NumGlobalCopies, "Number of allocas copied from constant global"); 29 30 /// pointsToConstantGlobal - Return true if V (possibly indirectly) points to 31 /// some part of a constant global variable. This intentionally only accepts 32 /// constant expressions because we can't rewrite arbitrary instructions. 33 static bool pointsToConstantGlobal(Value *V) { 34 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) 35 return GV->isConstant(); 36 37 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 38 if (CE->getOpcode() == Instruction::BitCast || 39 CE->getOpcode() == Instruction::AddrSpaceCast || 40 CE->getOpcode() == Instruction::GetElementPtr) 41 return pointsToConstantGlobal(CE->getOperand(0)); 42 } 43 return false; 44 } 45 46 /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) 47 /// pointer to an alloca. Ignore any reads of the pointer, return false if we 48 /// see any stores or other unknown uses. If we see pointer arithmetic, keep 49 /// track of whether it moves the pointer (with IsOffset) but otherwise traverse 50 /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to 51 /// the alloca, and if the source pointer is a pointer to a constant global, we 52 /// can optimize this. 53 static bool 54 isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, 55 SmallVectorImpl<Instruction *> &ToDelete) { 56 // We track lifetime intrinsics as we encounter them. If we decide to go 57 // ahead and replace the value with the global, this lets the caller quickly 58 // eliminate the markers. 59 60 SmallVector<std::pair<Value *, bool>, 35> ValuesToInspect; 61 ValuesToInspect.push_back(std::make_pair(V, false)); 62 while (!ValuesToInspect.empty()) { 63 auto ValuePair = ValuesToInspect.pop_back_val(); 64 const bool IsOffset = ValuePair.second; 65 for (auto &U : ValuePair.first->uses()) { 66 Instruction *I = cast<Instruction>(U.getUser()); 67 68 if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 69 // Ignore non-volatile loads, they are always ok. 70 if (!LI->isSimple()) return false; 71 continue; 72 } 73 74 if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) { 75 // If uses of the bitcast are ok, we are ok. 76 ValuesToInspect.push_back(std::make_pair(I, IsOffset)); 77 continue; 78 } 79 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { 80 // If the GEP has all zero indices, it doesn't offset the pointer. If it 81 // doesn't, it does. 82 ValuesToInspect.push_back( 83 std::make_pair(I, IsOffset || !GEP->hasAllZeroIndices())); 84 continue; 85 } 86 87 if (auto CS = CallSite(I)) { 88 // If this is the function being called then we treat it like a load and 89 // ignore it. 90 if (CS.isCallee(&U)) 91 continue; 92 93 // Inalloca arguments are clobbered by the call. 94 unsigned ArgNo = CS.getArgumentNo(&U); 95 if (CS.isInAllocaArgument(ArgNo)) 96 return false; 97 98 // If this is a readonly/readnone call site, then we know it is just a 99 // load (but one that potentially returns the value itself), so we can 100 // ignore it if we know that the value isn't captured. 101 if (CS.onlyReadsMemory() && 102 (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo))) 103 continue; 104 105 // If this is being passed as a byval argument, the caller is making a 106 // copy, so it is only a read of the alloca. 107 if (CS.isByValArgument(ArgNo)) 108 continue; 109 } 110 111 // Lifetime intrinsics can be handled by the caller. 112 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 113 if (II->getIntrinsicID() == Intrinsic::lifetime_start || 114 II->getIntrinsicID() == Intrinsic::lifetime_end) { 115 assert(II->use_empty() && "Lifetime markers have no result to use!"); 116 ToDelete.push_back(II); 117 continue; 118 } 119 } 120 121 // If this is isn't our memcpy/memmove, reject it as something we can't 122 // handle. 123 MemTransferInst *MI = dyn_cast<MemTransferInst>(I); 124 if (!MI) 125 return false; 126 127 // If the transfer is using the alloca as a source of the transfer, then 128 // ignore it since it is a load (unless the transfer is volatile). 129 if (U.getOperandNo() == 1) { 130 if (MI->isVolatile()) return false; 131 continue; 132 } 133 134 // If we already have seen a copy, reject the second one. 135 if (TheCopy) return false; 136 137 // If the pointer has been offset from the start of the alloca, we can't 138 // safely handle this. 139 if (IsOffset) return false; 140 141 // If the memintrinsic isn't using the alloca as the dest, reject it. 142 if (U.getOperandNo() != 0) return false; 143 144 // If the source of the memcpy/move is not a constant global, reject it. 145 if (!pointsToConstantGlobal(MI->getSource())) 146 return false; 147 148 // Otherwise, the transform is safe. Remember the copy instruction. 149 TheCopy = MI; 150 } 151 } 152 return true; 153 } 154 155 /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only 156 /// modified by a copy from a constant global. If we can prove this, we can 157 /// replace any uses of the alloca with uses of the global directly. 158 static MemTransferInst * 159 isOnlyCopiedFromConstantGlobal(AllocaInst *AI, 160 SmallVectorImpl<Instruction *> &ToDelete) { 161 MemTransferInst *TheCopy = nullptr; 162 if (isOnlyCopiedFromConstantGlobal(AI, TheCopy, ToDelete)) 163 return TheCopy; 164 return nullptr; 165 } 166 167 static Instruction *simplifyAllocaArraySize(InstCombiner &IC, AllocaInst &AI) { 168 // Check for array size of 1 (scalar allocation). 169 if (!AI.isArrayAllocation()) { 170 // i32 1 is the canonical array size for scalar allocations. 171 if (AI.getArraySize()->getType()->isIntegerTy(32)) 172 return nullptr; 173 174 // Canonicalize it. 175 Value *V = IC.Builder->getInt32(1); 176 AI.setOperand(0, V); 177 return &AI; 178 } 179 180 // Convert: alloca Ty, C - where C is a constant != 1 into: alloca [C x Ty], 1 181 if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { 182 Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getZExtValue()); 183 AllocaInst *New = IC.Builder->CreateAlloca(NewTy, nullptr, AI.getName()); 184 New->setAlignment(AI.getAlignment()); 185 186 // Scan to the end of the allocation instructions, to skip over a block of 187 // allocas if possible...also skip interleaved debug info 188 // 189 BasicBlock::iterator It = New; 190 while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) 191 ++It; 192 193 // Now that I is pointing to the first non-allocation-inst in the block, 194 // insert our getelementptr instruction... 195 // 196 Type *IdxTy = IC.getDataLayout().getIntPtrType(AI.getType()); 197 Value *NullIdx = Constant::getNullValue(IdxTy); 198 Value *Idx[2] = {NullIdx, NullIdx}; 199 Instruction *GEP = 200 GetElementPtrInst::CreateInBounds(New, Idx, New->getName() + ".sub"); 201 IC.InsertNewInstBefore(GEP, *It); 202 203 // Now make everything use the getelementptr instead of the original 204 // allocation. 205 return IC.ReplaceInstUsesWith(AI, GEP); 206 } 207 208 if (isa<UndefValue>(AI.getArraySize())) 209 return IC.ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType())); 210 211 // Ensure that the alloca array size argument has type intptr_t, so that 212 // any casting is exposed early. 213 Type *IntPtrTy = IC.getDataLayout().getIntPtrType(AI.getType()); 214 if (AI.getArraySize()->getType() != IntPtrTy) { 215 Value *V = IC.Builder->CreateIntCast(AI.getArraySize(), IntPtrTy, false); 216 AI.setOperand(0, V); 217 return &AI; 218 } 219 220 return nullptr; 221 } 222 223 Instruction *InstCombiner::visitAllocaInst(AllocaInst &AI) { 224 if (auto *I = simplifyAllocaArraySize(*this, AI)) 225 return I; 226 227 if (AI.getAllocatedType()->isSized()) { 228 // If the alignment is 0 (unspecified), assign it the preferred alignment. 229 if (AI.getAlignment() == 0) 230 AI.setAlignment(DL.getPrefTypeAlignment(AI.getAllocatedType())); 231 232 // Move all alloca's of zero byte objects to the entry block and merge them 233 // together. Note that we only do this for alloca's, because malloc should 234 // allocate and return a unique pointer, even for a zero byte allocation. 235 if (DL.getTypeAllocSize(AI.getAllocatedType()) == 0) { 236 // For a zero sized alloca there is no point in doing an array allocation. 237 // This is helpful if the array size is a complicated expression not used 238 // elsewhere. 239 if (AI.isArrayAllocation()) { 240 AI.setOperand(0, ConstantInt::get(AI.getArraySize()->getType(), 1)); 241 return &AI; 242 } 243 244 // Get the first instruction in the entry block. 245 BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock(); 246 Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg(); 247 if (FirstInst != &AI) { 248 // If the entry block doesn't start with a zero-size alloca then move 249 // this one to the start of the entry block. There is no problem with 250 // dominance as the array size was forced to a constant earlier already. 251 AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst); 252 if (!EntryAI || !EntryAI->getAllocatedType()->isSized() || 253 DL.getTypeAllocSize(EntryAI->getAllocatedType()) != 0) { 254 AI.moveBefore(FirstInst); 255 return &AI; 256 } 257 258 // If the alignment of the entry block alloca is 0 (unspecified), 259 // assign it the preferred alignment. 260 if (EntryAI->getAlignment() == 0) 261 EntryAI->setAlignment( 262 DL.getPrefTypeAlignment(EntryAI->getAllocatedType())); 263 // Replace this zero-sized alloca with the one at the start of the entry 264 // block after ensuring that the address will be aligned enough for both 265 // types. 266 unsigned MaxAlign = std::max(EntryAI->getAlignment(), 267 AI.getAlignment()); 268 EntryAI->setAlignment(MaxAlign); 269 if (AI.getType() != EntryAI->getType()) 270 return new BitCastInst(EntryAI, AI.getType()); 271 return ReplaceInstUsesWith(AI, EntryAI); 272 } 273 } 274 } 275 276 if (AI.getAlignment()) { 277 // Check to see if this allocation is only modified by a memcpy/memmove from 278 // a constant global whose alignment is equal to or exceeds that of the 279 // allocation. If this is the case, we can change all users to use 280 // the constant global instead. This is commonly produced by the CFE by 281 // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' 282 // is only subsequently read. 283 SmallVector<Instruction *, 4> ToDelete; 284 if (MemTransferInst *Copy = isOnlyCopiedFromConstantGlobal(&AI, ToDelete)) { 285 unsigned SourceAlign = getOrEnforceKnownAlignment( 286 Copy->getSource(), AI.getAlignment(), DL, &AI, AC, DT); 287 if (AI.getAlignment() <= SourceAlign) { 288 DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n'); 289 DEBUG(dbgs() << " memcpy = " << *Copy << '\n'); 290 for (unsigned i = 0, e = ToDelete.size(); i != e; ++i) 291 EraseInstFromFunction(*ToDelete[i]); 292 Constant *TheSrc = cast<Constant>(Copy->getSource()); 293 Constant *Cast 294 = ConstantExpr::getPointerBitCastOrAddrSpaceCast(TheSrc, AI.getType()); 295 Instruction *NewI = ReplaceInstUsesWith(AI, Cast); 296 EraseInstFromFunction(*Copy); 297 ++NumGlobalCopies; 298 return NewI; 299 } 300 } 301 } 302 303 // At last, use the generic allocation site handler to aggressively remove 304 // unused allocas. 305 return visitAllocSite(AI); 306 } 307 308 /// \brief Helper to combine a load to a new type. 309 /// 310 /// This just does the work of combining a load to a new type. It handles 311 /// metadata, etc., and returns the new instruction. The \c NewTy should be the 312 /// loaded *value* type. This will convert it to a pointer, cast the operand to 313 /// that pointer type, load it, etc. 314 /// 315 /// Note that this will create all of the instructions with whatever insert 316 /// point the \c InstCombiner currently is using. 317 static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy, 318 const Twine &Suffix = "") { 319 Value *Ptr = LI.getPointerOperand(); 320 unsigned AS = LI.getPointerAddressSpace(); 321 SmallVector<std::pair<unsigned, MDNode *>, 8> MD; 322 LI.getAllMetadata(MD); 323 324 LoadInst *NewLoad = IC.Builder->CreateAlignedLoad( 325 IC.Builder->CreateBitCast(Ptr, NewTy->getPointerTo(AS)), 326 LI.getAlignment(), LI.getName() + Suffix); 327 MDBuilder MDB(NewLoad->getContext()); 328 for (const auto &MDPair : MD) { 329 unsigned ID = MDPair.first; 330 MDNode *N = MDPair.second; 331 // Note, essentially every kind of metadata should be preserved here! This 332 // routine is supposed to clone a load instruction changing *only its type*. 333 // The only metadata it makes sense to drop is metadata which is invalidated 334 // when the pointer type changes. This should essentially never be the case 335 // in LLVM, but we explicitly switch over only known metadata to be 336 // conservatively correct. If you are adding metadata to LLVM which pertains 337 // to loads, you almost certainly want to add it here. 338 switch (ID) { 339 case LLVMContext::MD_dbg: 340 case LLVMContext::MD_tbaa: 341 case LLVMContext::MD_prof: 342 case LLVMContext::MD_fpmath: 343 case LLVMContext::MD_tbaa_struct: 344 case LLVMContext::MD_invariant_load: 345 case LLVMContext::MD_alias_scope: 346 case LLVMContext::MD_noalias: 347 case LLVMContext::MD_nontemporal: 348 case LLVMContext::MD_mem_parallel_loop_access: 349 // All of these directly apply. 350 NewLoad->setMetadata(ID, N); 351 break; 352 353 case LLVMContext::MD_nonnull: 354 // This only directly applies if the new type is also a pointer. 355 if (NewTy->isPointerTy()) { 356 NewLoad->setMetadata(ID, N); 357 break; 358 } 359 // If it's integral now, translate it to !range metadata. 360 if (NewTy->isIntegerTy()) { 361 auto *ITy = cast<IntegerType>(NewTy); 362 auto *NullInt = ConstantExpr::getPtrToInt( 363 ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy); 364 auto *NonNullInt = 365 ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1)); 366 NewLoad->setMetadata(LLVMContext::MD_range, 367 MDB.createRange(NonNullInt, NullInt)); 368 } 369 break; 370 371 case LLVMContext::MD_range: 372 // FIXME: It would be nice to propagate this in some way, but the type 373 // conversions make it hard. If the new type is a pointer, we could 374 // translate it to !nonnull metadata. 375 break; 376 } 377 } 378 return NewLoad; 379 } 380 381 /// \brief Combine a store to a new type. 382 /// 383 /// Returns the newly created store instruction. 384 static StoreInst *combineStoreToNewValue(InstCombiner &IC, StoreInst &SI, Value *V) { 385 Value *Ptr = SI.getPointerOperand(); 386 unsigned AS = SI.getPointerAddressSpace(); 387 SmallVector<std::pair<unsigned, MDNode *>, 8> MD; 388 SI.getAllMetadata(MD); 389 390 StoreInst *NewStore = IC.Builder->CreateAlignedStore( 391 V, IC.Builder->CreateBitCast(Ptr, V->getType()->getPointerTo(AS)), 392 SI.getAlignment()); 393 for (const auto &MDPair : MD) { 394 unsigned ID = MDPair.first; 395 MDNode *N = MDPair.second; 396 // Note, essentially every kind of metadata should be preserved here! This 397 // routine is supposed to clone a store instruction changing *only its 398 // type*. The only metadata it makes sense to drop is metadata which is 399 // invalidated when the pointer type changes. This should essentially 400 // never be the case in LLVM, but we explicitly switch over only known 401 // metadata to be conservatively correct. If you are adding metadata to 402 // LLVM which pertains to stores, you almost certainly want to add it 403 // here. 404 switch (ID) { 405 case LLVMContext::MD_dbg: 406 case LLVMContext::MD_tbaa: 407 case LLVMContext::MD_prof: 408 case LLVMContext::MD_fpmath: 409 case LLVMContext::MD_tbaa_struct: 410 case LLVMContext::MD_alias_scope: 411 case LLVMContext::MD_noalias: 412 case LLVMContext::MD_nontemporal: 413 case LLVMContext::MD_mem_parallel_loop_access: 414 // All of these directly apply. 415 NewStore->setMetadata(ID, N); 416 break; 417 418 case LLVMContext::MD_invariant_load: 419 case LLVMContext::MD_nonnull: 420 case LLVMContext::MD_range: 421 // These don't apply for stores. 422 break; 423 } 424 } 425 426 return NewStore; 427 } 428 429 /// \brief Combine loads to match the type of value their uses after looking 430 /// through intervening bitcasts. 431 /// 432 /// The core idea here is that if the result of a load is used in an operation, 433 /// we should load the type most conducive to that operation. For example, when 434 /// loading an integer and converting that immediately to a pointer, we should 435 /// instead directly load a pointer. 436 /// 437 /// However, this routine must never change the width of a load or the number of 438 /// loads as that would introduce a semantic change. This combine is expected to 439 /// be a semantic no-op which just allows loads to more closely model the types 440 /// of their consuming operations. 441 /// 442 /// Currently, we also refuse to change the precise type used for an atomic load 443 /// or a volatile load. This is debatable, and might be reasonable to change 444 /// later. However, it is risky in case some backend or other part of LLVM is 445 /// relying on the exact type loaded to select appropriate atomic operations. 446 static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) { 447 // FIXME: We could probably with some care handle both volatile and atomic 448 // loads here but it isn't clear that this is important. 449 if (!LI.isSimple()) 450 return nullptr; 451 452 if (LI.use_empty()) 453 return nullptr; 454 455 Type *Ty = LI.getType(); 456 const DataLayout &DL = IC.getDataLayout(); 457 458 // Try to canonicalize loads which are only ever stored to operate over 459 // integers instead of any other type. We only do this when the loaded type 460 // is sized and has a size exactly the same as its store size and the store 461 // size is a legal integer type. 462 if (!Ty->isIntegerTy() && Ty->isSized() && 463 DL.isLegalInteger(DL.getTypeStoreSizeInBits(Ty)) && 464 DL.getTypeStoreSizeInBits(Ty) == DL.getTypeSizeInBits(Ty)) { 465 if (std::all_of(LI.user_begin(), LI.user_end(), [&LI](User *U) { 466 auto *SI = dyn_cast<StoreInst>(U); 467 return SI && SI->getPointerOperand() != &LI; 468 })) { 469 LoadInst *NewLoad = combineLoadToNewType( 470 IC, LI, 471 Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty))); 472 // Replace all the stores with stores of the newly loaded value. 473 for (auto UI = LI.user_begin(), UE = LI.user_end(); UI != UE;) { 474 auto *SI = cast<StoreInst>(*UI++); 475 IC.Builder->SetInsertPoint(SI); 476 combineStoreToNewValue(IC, *SI, NewLoad); 477 IC.EraseInstFromFunction(*SI); 478 } 479 assert(LI.use_empty() && "Failed to remove all users of the load!"); 480 // Return the old load so the combiner can delete it safely. 481 return &LI; 482 } 483 } 484 485 // Fold away bit casts of the loaded value by loading the desired type. 486 if (LI.hasOneUse()) 487 if (auto *BC = dyn_cast<BitCastInst>(LI.user_back())) { 488 LoadInst *NewLoad = combineLoadToNewType(IC, LI, BC->getDestTy()); 489 BC->replaceAllUsesWith(NewLoad); 490 IC.EraseInstFromFunction(*BC); 491 return &LI; 492 } 493 494 // FIXME: We should also canonicalize loads of vectors when their elements are 495 // cast to other types. 496 return nullptr; 497 } 498 499 static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) { 500 // FIXME: We could probably with some care handle both volatile and atomic 501 // stores here but it isn't clear that this is important. 502 if (!LI.isSimple()) 503 return nullptr; 504 505 Type *T = LI.getType(); 506 if (!T->isAggregateType()) 507 return nullptr; 508 509 assert(LI.getAlignment() && "Alignement must be set at this point"); 510 511 if (auto *ST = dyn_cast<StructType>(T)) { 512 // If the struct only have one element, we unpack. 513 if (ST->getNumElements() == 1) { 514 LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U), 515 ".unpack"); 516 return IC.ReplaceInstUsesWith(LI, IC.Builder->CreateInsertValue( 517 UndefValue::get(T), NewLoad, 0, LI.getName())); 518 } 519 } 520 521 if (auto *AT = dyn_cast<ArrayType>(T)) { 522 // If the array only have one element, we unpack. 523 if (AT->getNumElements() == 1) { 524 LoadInst *NewLoad = combineLoadToNewType(IC, LI, AT->getElementType(), 525 ".unpack"); 526 return IC.ReplaceInstUsesWith(LI, IC.Builder->CreateInsertValue( 527 UndefValue::get(T), NewLoad, 0, LI.getName())); 528 } 529 } 530 531 return nullptr; 532 } 533 534 // If we can determine that all possible objects pointed to by the provided 535 // pointer value are, not only dereferenceable, but also definitively less than 536 // or equal to the provided maximum size, then return true. Otherwise, return 537 // false (constant global values and allocas fall into this category). 538 // 539 // FIXME: This should probably live in ValueTracking (or similar). 540 static bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize, 541 const DataLayout &DL) { 542 SmallPtrSet<Value *, 4> Visited; 543 SmallVector<Value *, 4> Worklist(1, V); 544 545 do { 546 Value *P = Worklist.pop_back_val(); 547 P = P->stripPointerCasts(); 548 549 if (!Visited.insert(P).second) 550 continue; 551 552 if (SelectInst *SI = dyn_cast<SelectInst>(P)) { 553 Worklist.push_back(SI->getTrueValue()); 554 Worklist.push_back(SI->getFalseValue()); 555 continue; 556 } 557 558 if (PHINode *PN = dyn_cast<PHINode>(P)) { 559 for (Value *IncValue : PN->incoming_values()) 560 Worklist.push_back(IncValue); 561 continue; 562 } 563 564 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(P)) { 565 if (GA->mayBeOverridden()) 566 return false; 567 Worklist.push_back(GA->getAliasee()); 568 continue; 569 } 570 571 // If we know how big this object is, and it is less than MaxSize, continue 572 // searching. Otherwise, return false. 573 if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) { 574 if (!AI->getAllocatedType()->isSized()) 575 return false; 576 577 ConstantInt *CS = dyn_cast<ConstantInt>(AI->getArraySize()); 578 if (!CS) 579 return false; 580 581 uint64_t TypeSize = DL.getTypeAllocSize(AI->getAllocatedType()); 582 // Make sure that, even if the multiplication below would wrap as an 583 // uint64_t, we still do the right thing. 584 if ((CS->getValue().zextOrSelf(128)*APInt(128, TypeSize)).ugt(MaxSize)) 585 return false; 586 continue; 587 } 588 589 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) { 590 if (!GV->hasDefinitiveInitializer() || !GV->isConstant()) 591 return false; 592 593 uint64_t InitSize = DL.getTypeAllocSize(GV->getType()->getElementType()); 594 if (InitSize > MaxSize) 595 return false; 596 continue; 597 } 598 599 return false; 600 } while (!Worklist.empty()); 601 602 return true; 603 } 604 605 // If we're indexing into an object of a known size, and the outer index is 606 // not a constant, but having any value but zero would lead to undefined 607 // behavior, replace it with zero. 608 // 609 // For example, if we have: 610 // @f.a = private unnamed_addr constant [1 x i32] [i32 12], align 4 611 // ... 612 // %arrayidx = getelementptr inbounds [1 x i32]* @f.a, i64 0, i64 %x 613 // ... = load i32* %arrayidx, align 4 614 // Then we know that we can replace %x in the GEP with i64 0. 615 // 616 // FIXME: We could fold any GEP index to zero that would cause UB if it were 617 // not zero. Currently, we only handle the first such index. Also, we could 618 // also search through non-zero constant indices if we kept track of the 619 // offsets those indices implied. 620 static bool canReplaceGEPIdxWithZero(InstCombiner &IC, GetElementPtrInst *GEPI, 621 Instruction *MemI, unsigned &Idx) { 622 if (GEPI->getNumOperands() < 2) 623 return false; 624 625 // Find the first non-zero index of a GEP. If all indices are zero, return 626 // one past the last index. 627 auto FirstNZIdx = [](const GetElementPtrInst *GEPI) { 628 unsigned I = 1; 629 for (unsigned IE = GEPI->getNumOperands(); I != IE; ++I) { 630 Value *V = GEPI->getOperand(I); 631 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) 632 if (CI->isZero()) 633 continue; 634 635 break; 636 } 637 638 return I; 639 }; 640 641 // Skip through initial 'zero' indices, and find the corresponding pointer 642 // type. See if the next index is not a constant. 643 Idx = FirstNZIdx(GEPI); 644 if (Idx == GEPI->getNumOperands()) 645 return false; 646 if (isa<Constant>(GEPI->getOperand(Idx))) 647 return false; 648 649 SmallVector<Value *, 4> Ops(GEPI->idx_begin(), GEPI->idx_begin() + Idx); 650 Type *AllocTy = GetElementPtrInst::getIndexedType( 651 cast<PointerType>(GEPI->getOperand(0)->getType()->getScalarType()) 652 ->getElementType(), 653 Ops); 654 if (!AllocTy || !AllocTy->isSized()) 655 return false; 656 const DataLayout &DL = IC.getDataLayout(); 657 uint64_t TyAllocSize = DL.getTypeAllocSize(AllocTy); 658 659 // If there are more indices after the one we might replace with a zero, make 660 // sure they're all non-negative. If any of them are negative, the overall 661 // address being computed might be before the base address determined by the 662 // first non-zero index. 663 auto IsAllNonNegative = [&]() { 664 for (unsigned i = Idx+1, e = GEPI->getNumOperands(); i != e; ++i) { 665 bool KnownNonNegative, KnownNegative; 666 IC.ComputeSignBit(GEPI->getOperand(i), KnownNonNegative, 667 KnownNegative, 0, MemI); 668 if (KnownNonNegative) 669 continue; 670 return false; 671 } 672 673 return true; 674 }; 675 676 // FIXME: If the GEP is not inbounds, and there are extra indices after the 677 // one we'll replace, those could cause the address computation to wrap 678 // (rendering the IsAllNonNegative() check below insufficient). We can do 679 // better, ignoring zero indicies (and other indicies we can prove small 680 // enough not to wrap). 681 if (Idx+1 != GEPI->getNumOperands() && !GEPI->isInBounds()) 682 return false; 683 684 // Note that isObjectSizeLessThanOrEq will return true only if the pointer is 685 // also known to be dereferenceable. 686 return isObjectSizeLessThanOrEq(GEPI->getOperand(0), TyAllocSize, DL) && 687 IsAllNonNegative(); 688 } 689 690 // If we're indexing into an object with a variable index for the memory 691 // access, but the object has only one element, we can assume that the index 692 // will always be zero. If we replace the GEP, return it. 693 template <typename T> 694 static Instruction *replaceGEPIdxWithZero(InstCombiner &IC, Value *Ptr, 695 T &MemI) { 696 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr)) { 697 unsigned Idx; 698 if (canReplaceGEPIdxWithZero(IC, GEPI, &MemI, Idx)) { 699 Instruction *NewGEPI = GEPI->clone(); 700 NewGEPI->setOperand(Idx, 701 ConstantInt::get(GEPI->getOperand(Idx)->getType(), 0)); 702 NewGEPI->insertBefore(GEPI); 703 MemI.setOperand(MemI.getPointerOperandIndex(), NewGEPI); 704 return NewGEPI; 705 } 706 } 707 708 return nullptr; 709 } 710 711 Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { 712 Value *Op = LI.getOperand(0); 713 714 // Try to canonicalize the loaded type. 715 if (Instruction *Res = combineLoadToOperationType(*this, LI)) 716 return Res; 717 718 // Attempt to improve the alignment. 719 unsigned KnownAlign = getOrEnforceKnownAlignment( 720 Op, DL.getPrefTypeAlignment(LI.getType()), DL, &LI, AC, DT); 721 unsigned LoadAlign = LI.getAlignment(); 722 unsigned EffectiveLoadAlign = 723 LoadAlign != 0 ? LoadAlign : DL.getABITypeAlignment(LI.getType()); 724 725 if (KnownAlign > EffectiveLoadAlign) 726 LI.setAlignment(KnownAlign); 727 else if (LoadAlign == 0) 728 LI.setAlignment(EffectiveLoadAlign); 729 730 // Replace GEP indices if possible. 731 if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Op, LI)) { 732 Worklist.Add(NewGEPI); 733 return &LI; 734 } 735 736 // None of the following transforms are legal for volatile/atomic loads. 737 // FIXME: Some of it is okay for atomic loads; needs refactoring. 738 if (!LI.isSimple()) return nullptr; 739 740 if (Instruction *Res = unpackLoadToAggregate(*this, LI)) 741 return Res; 742 743 // Do really simple store-to-load forwarding and load CSE, to catch cases 744 // where there are several consecutive memory accesses to the same location, 745 // separated by a few arithmetic operations. 746 BasicBlock::iterator BBI = &LI; 747 if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6)) 748 return ReplaceInstUsesWith( 749 LI, Builder->CreateBitOrPointerCast(AvailableVal, LI.getType(), 750 LI.getName() + ".cast")); 751 752 // load(gep null, ...) -> unreachable 753 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { 754 const Value *GEPI0 = GEPI->getOperand(0); 755 // TODO: Consider a target hook for valid address spaces for this xform. 756 if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){ 757 // Insert a new store to null instruction before the load to indicate 758 // that this code is not reachable. We do this instead of inserting 759 // an unreachable instruction directly because we cannot modify the 760 // CFG. 761 new StoreInst(UndefValue::get(LI.getType()), 762 Constant::getNullValue(Op->getType()), &LI); 763 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); 764 } 765 } 766 767 // load null/undef -> unreachable 768 // TODO: Consider a target hook for valid address spaces for this xform. 769 if (isa<UndefValue>(Op) || 770 (isa<ConstantPointerNull>(Op) && LI.getPointerAddressSpace() == 0)) { 771 // Insert a new store to null instruction before the load to indicate that 772 // this code is not reachable. We do this instead of inserting an 773 // unreachable instruction directly because we cannot modify the CFG. 774 new StoreInst(UndefValue::get(LI.getType()), 775 Constant::getNullValue(Op->getType()), &LI); 776 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType())); 777 } 778 779 if (Op->hasOneUse()) { 780 // Change select and PHI nodes to select values instead of addresses: this 781 // helps alias analysis out a lot, allows many others simplifications, and 782 // exposes redundancy in the code. 783 // 784 // Note that we cannot do the transformation unless we know that the 785 // introduced loads cannot trap! Something like this is valid as long as 786 // the condition is always false: load (select bool %C, int* null, int* %G), 787 // but it would not be valid if we transformed it to load from null 788 // unconditionally. 789 // 790 if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { 791 // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). 792 unsigned Align = LI.getAlignment(); 793 if (isSafeToLoadUnconditionally(SI->getOperand(1), SI, Align) && 794 isSafeToLoadUnconditionally(SI->getOperand(2), SI, Align)) { 795 LoadInst *V1 = Builder->CreateLoad(SI->getOperand(1), 796 SI->getOperand(1)->getName()+".val"); 797 LoadInst *V2 = Builder->CreateLoad(SI->getOperand(2), 798 SI->getOperand(2)->getName()+".val"); 799 V1->setAlignment(Align); 800 V2->setAlignment(Align); 801 return SelectInst::Create(SI->getCondition(), V1, V2); 802 } 803 804 // load (select (cond, null, P)) -> load P 805 if (isa<ConstantPointerNull>(SI->getOperand(1)) && 806 LI.getPointerAddressSpace() == 0) { 807 LI.setOperand(0, SI->getOperand(2)); 808 return &LI; 809 } 810 811 // load (select (cond, P, null)) -> load P 812 if (isa<ConstantPointerNull>(SI->getOperand(2)) && 813 LI.getPointerAddressSpace() == 0) { 814 LI.setOperand(0, SI->getOperand(1)); 815 return &LI; 816 } 817 } 818 } 819 return nullptr; 820 } 821 822 /// \brief Combine stores to match the type of value being stored. 823 /// 824 /// The core idea here is that the memory does not have any intrinsic type and 825 /// where we can we should match the type of a store to the type of value being 826 /// stored. 827 /// 828 /// However, this routine must never change the width of a store or the number of 829 /// stores as that would introduce a semantic change. This combine is expected to 830 /// be a semantic no-op which just allows stores to more closely model the types 831 /// of their incoming values. 832 /// 833 /// Currently, we also refuse to change the precise type used for an atomic or 834 /// volatile store. This is debatable, and might be reasonable to change later. 835 /// However, it is risky in case some backend or other part of LLVM is relying 836 /// on the exact type stored to select appropriate atomic operations. 837 /// 838 /// \returns true if the store was successfully combined away. This indicates 839 /// the caller must erase the store instruction. We have to let the caller erase 840 /// the store instruction sas otherwise there is no way to signal whether it was 841 /// combined or not: IC.EraseInstFromFunction returns a null pointer. 842 static bool combineStoreToValueType(InstCombiner &IC, StoreInst &SI) { 843 // FIXME: We could probably with some care handle both volatile and atomic 844 // stores here but it isn't clear that this is important. 845 if (!SI.isSimple()) 846 return false; 847 848 Value *V = SI.getValueOperand(); 849 850 // Fold away bit casts of the stored value by storing the original type. 851 if (auto *BC = dyn_cast<BitCastInst>(V)) { 852 V = BC->getOperand(0); 853 combineStoreToNewValue(IC, SI, V); 854 return true; 855 } 856 857 // FIXME: We should also canonicalize loads of vectors when their elements are 858 // cast to other types. 859 return false; 860 } 861 862 static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI) { 863 // FIXME: We could probably with some care handle both volatile and atomic 864 // stores here but it isn't clear that this is important. 865 if (!SI.isSimple()) 866 return false; 867 868 Value *V = SI.getValueOperand(); 869 Type *T = V->getType(); 870 871 if (!T->isAggregateType()) 872 return false; 873 874 if (auto *ST = dyn_cast<StructType>(T)) { 875 // If the struct only have one element, we unpack. 876 if (ST->getNumElements() == 1) { 877 V = IC.Builder->CreateExtractValue(V, 0); 878 combineStoreToNewValue(IC, SI, V); 879 return true; 880 } 881 } 882 883 if (auto *AT = dyn_cast<ArrayType>(T)) { 884 // If the array only have one element, we unpack. 885 if (AT->getNumElements() == 1) { 886 V = IC.Builder->CreateExtractValue(V, 0); 887 combineStoreToNewValue(IC, SI, V); 888 return true; 889 } 890 } 891 892 return false; 893 } 894 895 /// equivalentAddressValues - Test if A and B will obviously have the same 896 /// value. This includes recognizing that %t0 and %t1 will have the same 897 /// value in code like this: 898 /// %t0 = getelementptr \@a, 0, 3 899 /// store i32 0, i32* %t0 900 /// %t1 = getelementptr \@a, 0, 3 901 /// %t2 = load i32* %t1 902 /// 903 static bool equivalentAddressValues(Value *A, Value *B) { 904 // Test if the values are trivially equivalent. 905 if (A == B) return true; 906 907 // Test if the values come form identical arithmetic instructions. 908 // This uses isIdenticalToWhenDefined instead of isIdenticalTo because 909 // its only used to compare two uses within the same basic block, which 910 // means that they'll always either have the same value or one of them 911 // will have an undefined value. 912 if (isa<BinaryOperator>(A) || 913 isa<CastInst>(A) || 914 isa<PHINode>(A) || 915 isa<GetElementPtrInst>(A)) 916 if (Instruction *BI = dyn_cast<Instruction>(B)) 917 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) 918 return true; 919 920 // Otherwise they may not be equivalent. 921 return false; 922 } 923 924 Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { 925 Value *Val = SI.getOperand(0); 926 Value *Ptr = SI.getOperand(1); 927 928 // Try to canonicalize the stored type. 929 if (combineStoreToValueType(*this, SI)) 930 return EraseInstFromFunction(SI); 931 932 // Attempt to improve the alignment. 933 unsigned KnownAlign = getOrEnforceKnownAlignment( 934 Ptr, DL.getPrefTypeAlignment(Val->getType()), DL, &SI, AC, DT); 935 unsigned StoreAlign = SI.getAlignment(); 936 unsigned EffectiveStoreAlign = 937 StoreAlign != 0 ? StoreAlign : DL.getABITypeAlignment(Val->getType()); 938 939 if (KnownAlign > EffectiveStoreAlign) 940 SI.setAlignment(KnownAlign); 941 else if (StoreAlign == 0) 942 SI.setAlignment(EffectiveStoreAlign); 943 944 // Try to canonicalize the stored type. 945 if (unpackStoreToAggregate(*this, SI)) 946 return EraseInstFromFunction(SI); 947 948 // Replace GEP indices if possible. 949 if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Ptr, SI)) { 950 Worklist.Add(NewGEPI); 951 return &SI; 952 } 953 954 // Don't hack volatile/atomic stores. 955 // FIXME: Some bits are legal for atomic stores; needs refactoring. 956 if (!SI.isSimple()) return nullptr; 957 958 // If the RHS is an alloca with a single use, zapify the store, making the 959 // alloca dead. 960 if (Ptr->hasOneUse()) { 961 if (isa<AllocaInst>(Ptr)) 962 return EraseInstFromFunction(SI); 963 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { 964 if (isa<AllocaInst>(GEP->getOperand(0))) { 965 if (GEP->getOperand(0)->hasOneUse()) 966 return EraseInstFromFunction(SI); 967 } 968 } 969 } 970 971 // Do really simple DSE, to catch cases where there are several consecutive 972 // stores to the same location, separated by a few arithmetic operations. This 973 // situation often occurs with bitfield accesses. 974 BasicBlock::iterator BBI = &SI; 975 for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; 976 --ScanInsts) { 977 --BBI; 978 // Don't count debug info directives, lest they affect codegen, 979 // and we skip pointer-to-pointer bitcasts, which are NOPs. 980 if (isa<DbgInfoIntrinsic>(BBI) || 981 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { 982 ScanInsts++; 983 continue; 984 } 985 986 if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { 987 // Prev store isn't volatile, and stores to the same location? 988 if (PrevSI->isSimple() && equivalentAddressValues(PrevSI->getOperand(1), 989 SI.getOperand(1))) { 990 ++NumDeadStore; 991 ++BBI; 992 EraseInstFromFunction(*PrevSI); 993 continue; 994 } 995 break; 996 } 997 998 // If this is a load, we have to stop. However, if the loaded value is from 999 // the pointer we're loading and is producing the pointer we're storing, 1000 // then *this* store is dead (X = load P; store X -> P). 1001 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { 1002 if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) && 1003 LI->isSimple()) 1004 return EraseInstFromFunction(SI); 1005 1006 // Otherwise, this is a load from some other location. Stores before it 1007 // may not be dead. 1008 break; 1009 } 1010 1011 // Don't skip over loads or things that can modify memory. 1012 if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) 1013 break; 1014 } 1015 1016 // store X, null -> turns into 'unreachable' in SimplifyCFG 1017 if (isa<ConstantPointerNull>(Ptr) && SI.getPointerAddressSpace() == 0) { 1018 if (!isa<UndefValue>(Val)) { 1019 SI.setOperand(0, UndefValue::get(Val->getType())); 1020 if (Instruction *U = dyn_cast<Instruction>(Val)) 1021 Worklist.Add(U); // Dropped a use. 1022 } 1023 return nullptr; // Do not modify these! 1024 } 1025 1026 // store undef, Ptr -> noop 1027 if (isa<UndefValue>(Val)) 1028 return EraseInstFromFunction(SI); 1029 1030 // If this store is the last instruction in the basic block (possibly 1031 // excepting debug info instructions), and if the block ends with an 1032 // unconditional branch, try to move it to the successor block. 1033 BBI = &SI; 1034 do { 1035 ++BBI; 1036 } while (isa<DbgInfoIntrinsic>(BBI) || 1037 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())); 1038 if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) 1039 if (BI->isUnconditional()) 1040 if (SimplifyStoreAtEndOfBlock(SI)) 1041 return nullptr; // xform done! 1042 1043 return nullptr; 1044 } 1045 1046 /// SimplifyStoreAtEndOfBlock - Turn things like: 1047 /// if () { *P = v1; } else { *P = v2 } 1048 /// into a phi node with a store in the successor. 1049 /// 1050 /// Simplify things like: 1051 /// *P = v1; if () { *P = v2; } 1052 /// into a phi node with a store in the successor. 1053 /// 1054 bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { 1055 BasicBlock *StoreBB = SI.getParent(); 1056 1057 // Check to see if the successor block has exactly two incoming edges. If 1058 // so, see if the other predecessor contains a store to the same location. 1059 // if so, insert a PHI node (if needed) and move the stores down. 1060 BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); 1061 1062 // Determine whether Dest has exactly two predecessors and, if so, compute 1063 // the other predecessor. 1064 pred_iterator PI = pred_begin(DestBB); 1065 BasicBlock *P = *PI; 1066 BasicBlock *OtherBB = nullptr; 1067 1068 if (P != StoreBB) 1069 OtherBB = P; 1070 1071 if (++PI == pred_end(DestBB)) 1072 return false; 1073 1074 P = *PI; 1075 if (P != StoreBB) { 1076 if (OtherBB) 1077 return false; 1078 OtherBB = P; 1079 } 1080 if (++PI != pred_end(DestBB)) 1081 return false; 1082 1083 // Bail out if all the relevant blocks aren't distinct (this can happen, 1084 // for example, if SI is in an infinite loop) 1085 if (StoreBB == DestBB || OtherBB == DestBB) 1086 return false; 1087 1088 // Verify that the other block ends in a branch and is not otherwise empty. 1089 BasicBlock::iterator BBI = OtherBB->getTerminator(); 1090 BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); 1091 if (!OtherBr || BBI == OtherBB->begin()) 1092 return false; 1093 1094 // If the other block ends in an unconditional branch, check for the 'if then 1095 // else' case. there is an instruction before the branch. 1096 StoreInst *OtherStore = nullptr; 1097 if (OtherBr->isUnconditional()) { 1098 --BBI; 1099 // Skip over debugging info. 1100 while (isa<DbgInfoIntrinsic>(BBI) || 1101 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { 1102 if (BBI==OtherBB->begin()) 1103 return false; 1104 --BBI; 1105 } 1106 // If this isn't a store, isn't a store to the same location, or is not the 1107 // right kind of store, bail out. 1108 OtherStore = dyn_cast<StoreInst>(BBI); 1109 if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1) || 1110 !SI.isSameOperationAs(OtherStore)) 1111 return false; 1112 } else { 1113 // Otherwise, the other block ended with a conditional branch. If one of the 1114 // destinations is StoreBB, then we have the if/then case. 1115 if (OtherBr->getSuccessor(0) != StoreBB && 1116 OtherBr->getSuccessor(1) != StoreBB) 1117 return false; 1118 1119 // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an 1120 // if/then triangle. See if there is a store to the same ptr as SI that 1121 // lives in OtherBB. 1122 for (;; --BBI) { 1123 // Check to see if we find the matching store. 1124 if ((OtherStore = dyn_cast<StoreInst>(BBI))) { 1125 if (OtherStore->getOperand(1) != SI.getOperand(1) || 1126 !SI.isSameOperationAs(OtherStore)) 1127 return false; 1128 break; 1129 } 1130 // If we find something that may be using or overwriting the stored 1131 // value, or if we run out of instructions, we can't do the xform. 1132 if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || 1133 BBI == OtherBB->begin()) 1134 return false; 1135 } 1136 1137 // In order to eliminate the store in OtherBr, we have to 1138 // make sure nothing reads or overwrites the stored value in 1139 // StoreBB. 1140 for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { 1141 // FIXME: This should really be AA driven. 1142 if (I->mayReadFromMemory() || I->mayWriteToMemory()) 1143 return false; 1144 } 1145 } 1146 1147 // Insert a PHI node now if we need it. 1148 Value *MergedVal = OtherStore->getOperand(0); 1149 if (MergedVal != SI.getOperand(0)) { 1150 PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge"); 1151 PN->addIncoming(SI.getOperand(0), SI.getParent()); 1152 PN->addIncoming(OtherStore->getOperand(0), OtherBB); 1153 MergedVal = InsertNewInstBefore(PN, DestBB->front()); 1154 } 1155 1156 // Advance to a place where it is safe to insert the new store and 1157 // insert it. 1158 BBI = DestBB->getFirstInsertionPt(); 1159 StoreInst *NewSI = new StoreInst(MergedVal, SI.getOperand(1), 1160 SI.isVolatile(), 1161 SI.getAlignment(), 1162 SI.getOrdering(), 1163 SI.getSynchScope()); 1164 InsertNewInstBefore(NewSI, *BBI); 1165 NewSI->setDebugLoc(OtherStore->getDebugLoc()); 1166 1167 // If the two stores had AA tags, merge them. 1168 AAMDNodes AATags; 1169 SI.getAAMetadata(AATags); 1170 if (AATags) { 1171 OtherStore->getAAMetadata(AATags, /* Merge = */ true); 1172 NewSI->setAAMetadata(AATags); 1173 } 1174 1175 // Nuke the old stores. 1176 EraseInstFromFunction(SI); 1177 EraseInstFromFunction(*OtherStore); 1178 return true; 1179 } 1180