ScalarReplAggregates.cpp revision 7b929dad59785f62a66f7c58615082f98441e95e
1//===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===// 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 transformation implements the well known scalar replacement of 11// aggregates transformation. This xform breaks up alloca instructions of 12// aggregate type (structure or array) into individual alloca instructions for 13// each member (if possible). Then, if possible, it transforms the individual 14// alloca instructions into nice clean scalar SSA form. 15// 16// This combines a simple SRoA algorithm with the Mem2Reg algorithm because 17// often interact, especially for C++ programs. As such, iterating between 18// SRoA, then Mem2Reg until we run out of things to promote works well. 19// 20//===----------------------------------------------------------------------===// 21 22#define DEBUG_TYPE "scalarrepl" 23#include "llvm/Transforms/Scalar.h" 24#include "llvm/Constants.h" 25#include "llvm/DerivedTypes.h" 26#include "llvm/Function.h" 27#include "llvm/GlobalVariable.h" 28#include "llvm/Instructions.h" 29#include "llvm/IntrinsicInst.h" 30#include "llvm/LLVMContext.h" 31#include "llvm/Pass.h" 32#include "llvm/Analysis/Dominators.h" 33#include "llvm/Target/TargetData.h" 34#include "llvm/Transforms/Utils/PromoteMemToReg.h" 35#include "llvm/Transforms/Utils/Local.h" 36#include "llvm/Support/Debug.h" 37#include "llvm/Support/ErrorHandling.h" 38#include "llvm/Support/GetElementPtrTypeIterator.h" 39#include "llvm/Support/IRBuilder.h" 40#include "llvm/Support/MathExtras.h" 41#include "llvm/Support/raw_ostream.h" 42#include "llvm/ADT/SmallVector.h" 43#include "llvm/ADT/Statistic.h" 44using namespace llvm; 45 46STATISTIC(NumReplaced, "Number of allocas broken up"); 47STATISTIC(NumPromoted, "Number of allocas promoted"); 48STATISTIC(NumConverted, "Number of aggregates converted to scalar"); 49STATISTIC(NumGlobals, "Number of allocas copied from constant global"); 50 51namespace { 52 struct SROA : public FunctionPass { 53 static char ID; // Pass identification, replacement for typeid 54 explicit SROA(signed T = -1) : FunctionPass(&ID) { 55 if (T == -1) 56 SRThreshold = 128; 57 else 58 SRThreshold = T; 59 } 60 61 bool runOnFunction(Function &F); 62 63 bool performScalarRepl(Function &F); 64 bool performPromotion(Function &F); 65 66 // getAnalysisUsage - This pass does not require any passes, but we know it 67 // will not alter the CFG, so say so. 68 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 69 AU.addRequired<DominatorTree>(); 70 AU.addRequired<DominanceFrontier>(); 71 AU.setPreservesCFG(); 72 } 73 74 private: 75 TargetData *TD; 76 77 /// AllocaInfo - When analyzing uses of an alloca instruction, this captures 78 /// information about the uses. All these fields are initialized to false 79 /// and set to true when something is learned. 80 struct AllocaInfo { 81 /// isUnsafe - This is set to true if the alloca cannot be SROA'd. 82 bool isUnsafe : 1; 83 84 /// needsCleanup - This is set to true if there is some use of the alloca 85 /// that requires cleanup. 86 bool needsCleanup : 1; 87 88 /// isMemCpySrc - This is true if this aggregate is memcpy'd from. 89 bool isMemCpySrc : 1; 90 91 /// isMemCpyDst - This is true if this aggregate is memcpy'd into. 92 bool isMemCpyDst : 1; 93 94 AllocaInfo() 95 : isUnsafe(false), needsCleanup(false), 96 isMemCpySrc(false), isMemCpyDst(false) {} 97 }; 98 99 unsigned SRThreshold; 100 101 void MarkUnsafe(AllocaInfo &I) { I.isUnsafe = true; } 102 103 int isSafeAllocaToScalarRepl(AllocaInst *AI); 104 105 void isSafeUseOfAllocation(Instruction *User, AllocaInst *AI, 106 AllocaInfo &Info); 107 void isSafeElementUse(Value *Ptr, bool isFirstElt, AllocaInst *AI, 108 AllocaInfo &Info); 109 void isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocaInst *AI, 110 unsigned OpNo, AllocaInfo &Info); 111 void isSafeUseOfBitCastedAllocation(BitCastInst *User, AllocaInst *AI, 112 AllocaInfo &Info); 113 114 void DoScalarReplacement(AllocaInst *AI, 115 std::vector<AllocaInst*> &WorkList); 116 void CleanupGEP(GetElementPtrInst *GEP); 117 void CleanupAllocaUsers(AllocaInst *AI); 118 AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocaInst *Base); 119 120 void RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocaInst *AI, 121 SmallVector<AllocaInst*, 32> &NewElts); 122 123 void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst, 124 AllocaInst *AI, 125 SmallVector<AllocaInst*, 32> &NewElts); 126 void RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, 127 SmallVector<AllocaInst*, 32> &NewElts); 128 void RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, 129 SmallVector<AllocaInst*, 32> &NewElts); 130 131 bool CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy, 132 bool &SawVec, uint64_t Offset, unsigned AllocaSize); 133 void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset); 134 Value *ConvertScalar_ExtractValue(Value *NV, const Type *ToType, 135 uint64_t Offset, IRBuilder<> &Builder); 136 Value *ConvertScalar_InsertValue(Value *StoredVal, Value *ExistingVal, 137 uint64_t Offset, IRBuilder<> &Builder); 138 static Instruction *isOnlyCopiedFromConstantGlobal(AllocaInst *AI); 139 }; 140} 141 142char SROA::ID = 0; 143static RegisterPass<SROA> X("scalarrepl", "Scalar Replacement of Aggregates"); 144 145// Public interface to the ScalarReplAggregates pass 146FunctionPass *llvm::createScalarReplAggregatesPass(signed int Threshold) { 147 return new SROA(Threshold); 148} 149 150 151bool SROA::runOnFunction(Function &F) { 152 TD = getAnalysisIfAvailable<TargetData>(); 153 154 bool Changed = performPromotion(F); 155 156 // FIXME: ScalarRepl currently depends on TargetData more than it 157 // theoretically needs to. It should be refactored in order to support 158 // target-independent IR. Until this is done, just skip the actual 159 // scalar-replacement portion of this pass. 160 if (!TD) return Changed; 161 162 while (1) { 163 bool LocalChange = performScalarRepl(F); 164 if (!LocalChange) break; // No need to repromote if no scalarrepl 165 Changed = true; 166 LocalChange = performPromotion(F); 167 if (!LocalChange) break; // No need to re-scalarrepl if no promotion 168 } 169 170 return Changed; 171} 172 173 174bool SROA::performPromotion(Function &F) { 175 std::vector<AllocaInst*> Allocas; 176 DominatorTree &DT = getAnalysis<DominatorTree>(); 177 DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); 178 179 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function 180 181 bool Changed = false; 182 183 while (1) { 184 Allocas.clear(); 185 186 // Find allocas that are safe to promote, by looking at all instructions in 187 // the entry node 188 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) 189 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? 190 if (isAllocaPromotable(AI)) 191 Allocas.push_back(AI); 192 193 if (Allocas.empty()) break; 194 195 PromoteMemToReg(Allocas, DT, DF, F.getContext()); 196 NumPromoted += Allocas.size(); 197 Changed = true; 198 } 199 200 return Changed; 201} 202 203/// getNumSAElements - Return the number of elements in the specific struct or 204/// array. 205static uint64_t getNumSAElements(const Type *T) { 206 if (const StructType *ST = dyn_cast<StructType>(T)) 207 return ST->getNumElements(); 208 return cast<ArrayType>(T)->getNumElements(); 209} 210 211// performScalarRepl - This algorithm is a simple worklist driven algorithm, 212// which runs on all of the malloc/alloca instructions in the function, removing 213// them if they are only used by getelementptr instructions. 214// 215bool SROA::performScalarRepl(Function &F) { 216 std::vector<AllocaInst*> WorkList; 217 218 // Scan the entry basic block, adding any alloca's and mallocs to the worklist 219 BasicBlock &BB = F.getEntryBlock(); 220 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) 221 if (AllocaInst *A = dyn_cast<AllocaInst>(I)) 222 WorkList.push_back(A); 223 224 // Process the worklist 225 bool Changed = false; 226 while (!WorkList.empty()) { 227 AllocaInst *AI = WorkList.back(); 228 WorkList.pop_back(); 229 230 // Handle dead allocas trivially. These can be formed by SROA'ing arrays 231 // with unused elements. 232 if (AI->use_empty()) { 233 AI->eraseFromParent(); 234 continue; 235 } 236 237 // If this alloca is impossible for us to promote, reject it early. 238 if (AI->isArrayAllocation() || !AI->getAllocatedType()->isSized()) 239 continue; 240 241 // Check to see if this allocation is only modified by a memcpy/memmove from 242 // a constant global. If this is the case, we can change all users to use 243 // the constant global instead. This is commonly produced by the CFE by 244 // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' 245 // is only subsequently read. 246 if (Instruction *TheCopy = isOnlyCopiedFromConstantGlobal(AI)) { 247 DEBUG(errs() << "Found alloca equal to global: " << *AI << '\n'); 248 DEBUG(errs() << " memcpy = " << *TheCopy << '\n'); 249 Constant *TheSrc = cast<Constant>(TheCopy->getOperand(2)); 250 AI->replaceAllUsesWith(ConstantExpr::getBitCast(TheSrc, AI->getType())); 251 TheCopy->eraseFromParent(); // Don't mutate the global. 252 AI->eraseFromParent(); 253 ++NumGlobals; 254 Changed = true; 255 continue; 256 } 257 258 // Check to see if we can perform the core SROA transformation. We cannot 259 // transform the allocation instruction if it is an array allocation 260 // (allocations OF arrays are ok though), and an allocation of a scalar 261 // value cannot be decomposed at all. 262 uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType()); 263 264 // Do not promote [0 x %struct]. 265 if (AllocaSize == 0) continue; 266 267 // Do not promote any struct whose size is too big. 268 if (AllocaSize > SRThreshold) continue; 269 270 if ((isa<StructType>(AI->getAllocatedType()) || 271 isa<ArrayType>(AI->getAllocatedType())) && 272 // Do not promote any struct into more than "32" separate vars. 273 getNumSAElements(AI->getAllocatedType()) <= SRThreshold/4) { 274 // Check that all of the users of the allocation are capable of being 275 // transformed. 276 switch (isSafeAllocaToScalarRepl(AI)) { 277 default: llvm_unreachable("Unexpected value!"); 278 case 0: // Not safe to scalar replace. 279 break; 280 case 1: // Safe, but requires cleanup/canonicalizations first 281 CleanupAllocaUsers(AI); 282 // FALL THROUGH. 283 case 3: // Safe to scalar replace. 284 DoScalarReplacement(AI, WorkList); 285 Changed = true; 286 continue; 287 } 288 } 289 290 // If we can turn this aggregate value (potentially with casts) into a 291 // simple scalar value that can be mem2reg'd into a register value. 292 // IsNotTrivial tracks whether this is something that mem2reg could have 293 // promoted itself. If so, we don't want to transform it needlessly. Note 294 // that we can't just check based on the type: the alloca may be of an i32 295 // but that has pointer arithmetic to set byte 3 of it or something. 296 bool IsNotTrivial = false; 297 const Type *VectorTy = 0; 298 bool HadAVector = false; 299 if (CanConvertToScalar(AI, IsNotTrivial, VectorTy, HadAVector, 300 0, unsigned(AllocaSize)) && IsNotTrivial) { 301 AllocaInst *NewAI; 302 // If we were able to find a vector type that can handle this with 303 // insert/extract elements, and if there was at least one use that had 304 // a vector type, promote this to a vector. We don't want to promote 305 // random stuff that doesn't use vectors (e.g. <9 x double>) because then 306 // we just get a lot of insert/extracts. If at least one vector is 307 // involved, then we probably really do have a union of vector/array. 308 if (VectorTy && isa<VectorType>(VectorTy) && HadAVector) { 309 DEBUG(errs() << "CONVERT TO VECTOR: " << *AI << "\n TYPE = " 310 << *VectorTy << '\n'); 311 312 // Create and insert the vector alloca. 313 NewAI = new AllocaInst(VectorTy, 0, "", AI->getParent()->begin()); 314 ConvertUsesToScalar(AI, NewAI, 0); 315 } else { 316 DEBUG(errs() << "CONVERT TO SCALAR INTEGER: " << *AI << "\n"); 317 318 // Create and insert the integer alloca. 319 const Type *NewTy = IntegerType::get(AI->getContext(), AllocaSize*8); 320 NewAI = new AllocaInst(NewTy, 0, "", AI->getParent()->begin()); 321 ConvertUsesToScalar(AI, NewAI, 0); 322 } 323 NewAI->takeName(AI); 324 AI->eraseFromParent(); 325 ++NumConverted; 326 Changed = true; 327 continue; 328 } 329 330 // Otherwise, couldn't process this alloca. 331 } 332 333 return Changed; 334} 335 336/// DoScalarReplacement - This alloca satisfied the isSafeAllocaToScalarRepl 337/// predicate, do SROA now. 338void SROA::DoScalarReplacement(AllocaInst *AI, 339 std::vector<AllocaInst*> &WorkList) { 340 DEBUG(errs() << "Found inst to SROA: " << *AI << '\n'); 341 SmallVector<AllocaInst*, 32> ElementAllocas; 342 if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { 343 ElementAllocas.reserve(ST->getNumContainedTypes()); 344 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { 345 AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0, 346 AI->getAlignment(), 347 AI->getName() + "." + Twine(i), AI); 348 ElementAllocas.push_back(NA); 349 WorkList.push_back(NA); // Add to worklist for recursive processing 350 } 351 } else { 352 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); 353 ElementAllocas.reserve(AT->getNumElements()); 354 const Type *ElTy = AT->getElementType(); 355 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { 356 AllocaInst *NA = new AllocaInst(ElTy, 0, AI->getAlignment(), 357 AI->getName() + "." + Twine(i), AI); 358 ElementAllocas.push_back(NA); 359 WorkList.push_back(NA); // Add to worklist for recursive processing 360 } 361 } 362 363 // Now that we have created the alloca instructions that we want to use, 364 // expand the getelementptr instructions to use them. 365 // 366 while (!AI->use_empty()) { 367 Instruction *User = cast<Instruction>(AI->use_back()); 368 if (BitCastInst *BCInst = dyn_cast<BitCastInst>(User)) { 369 RewriteBitCastUserOfAlloca(BCInst, AI, ElementAllocas); 370 BCInst->eraseFromParent(); 371 continue; 372 } 373 374 // Replace: 375 // %res = load { i32, i32 }* %alloc 376 // with: 377 // %load.0 = load i32* %alloc.0 378 // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0 379 // %load.1 = load i32* %alloc.1 380 // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1 381 // (Also works for arrays instead of structs) 382 if (LoadInst *LI = dyn_cast<LoadInst>(User)) { 383 Value *Insert = UndefValue::get(LI->getType()); 384 for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) { 385 Value *Load = new LoadInst(ElementAllocas[i], "load", LI); 386 Insert = InsertValueInst::Create(Insert, Load, i, "insert", LI); 387 } 388 LI->replaceAllUsesWith(Insert); 389 LI->eraseFromParent(); 390 continue; 391 } 392 393 // Replace: 394 // store { i32, i32 } %val, { i32, i32 }* %alloc 395 // with: 396 // %val.0 = extractvalue { i32, i32 } %val, 0 397 // store i32 %val.0, i32* %alloc.0 398 // %val.1 = extractvalue { i32, i32 } %val, 1 399 // store i32 %val.1, i32* %alloc.1 400 // (Also works for arrays instead of structs) 401 if (StoreInst *SI = dyn_cast<StoreInst>(User)) { 402 Value *Val = SI->getOperand(0); 403 for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) { 404 Value *Extract = ExtractValueInst::Create(Val, i, Val->getName(), SI); 405 new StoreInst(Extract, ElementAllocas[i], SI); 406 } 407 SI->eraseFromParent(); 408 continue; 409 } 410 411 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User); 412 // We now know that the GEP is of the form: GEP <ptr>, 0, <cst> 413 unsigned Idx = 414 (unsigned)cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue(); 415 416 assert(Idx < ElementAllocas.size() && "Index out of range?"); 417 AllocaInst *AllocaToUse = ElementAllocas[Idx]; 418 419 Value *RepValue; 420 if (GEPI->getNumOperands() == 3) { 421 // Do not insert a new getelementptr instruction with zero indices, only 422 // to have it optimized out later. 423 RepValue = AllocaToUse; 424 } else { 425 // We are indexing deeply into the structure, so we still need a 426 // getelement ptr instruction to finish the indexing. This may be 427 // expanded itself once the worklist is rerun. 428 // 429 SmallVector<Value*, 8> NewArgs; 430 NewArgs.push_back(Constant::getNullValue( 431 Type::getInt32Ty(AI->getContext()))); 432 NewArgs.append(GEPI->op_begin()+3, GEPI->op_end()); 433 RepValue = GetElementPtrInst::Create(AllocaToUse, NewArgs.begin(), 434 NewArgs.end(), "", GEPI); 435 RepValue->takeName(GEPI); 436 } 437 438 // If this GEP is to the start of the aggregate, check for memcpys. 439 if (Idx == 0 && GEPI->hasAllZeroIndices()) 440 RewriteBitCastUserOfAlloca(GEPI, AI, ElementAllocas); 441 442 // Move all of the users over to the new GEP. 443 GEPI->replaceAllUsesWith(RepValue); 444 // Delete the old GEP 445 GEPI->eraseFromParent(); 446 } 447 448 // Finally, delete the Alloca instruction 449 AI->eraseFromParent(); 450 NumReplaced++; 451} 452 453 454/// isSafeElementUse - Check to see if this use is an allowed use for a 455/// getelementptr instruction of an array aggregate allocation. isFirstElt 456/// indicates whether Ptr is known to the start of the aggregate. 457/// 458void SROA::isSafeElementUse(Value *Ptr, bool isFirstElt, AllocaInst *AI, 459 AllocaInfo &Info) { 460 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); 461 I != E; ++I) { 462 Instruction *User = cast<Instruction>(*I); 463 switch (User->getOpcode()) { 464 case Instruction::Load: break; 465 case Instruction::Store: 466 // Store is ok if storing INTO the pointer, not storing the pointer 467 if (User->getOperand(0) == Ptr) return MarkUnsafe(Info); 468 break; 469 case Instruction::GetElementPtr: { 470 GetElementPtrInst *GEP = cast<GetElementPtrInst>(User); 471 bool AreAllZeroIndices = isFirstElt; 472 if (GEP->getNumOperands() > 1) { 473 if (!isa<ConstantInt>(GEP->getOperand(1)) || 474 !cast<ConstantInt>(GEP->getOperand(1))->isZero()) 475 // Using pointer arithmetic to navigate the array. 476 return MarkUnsafe(Info); 477 478 if (AreAllZeroIndices) 479 AreAllZeroIndices = GEP->hasAllZeroIndices(); 480 } 481 isSafeElementUse(GEP, AreAllZeroIndices, AI, Info); 482 if (Info.isUnsafe) return; 483 break; 484 } 485 case Instruction::BitCast: 486 if (isFirstElt) { 487 isSafeUseOfBitCastedAllocation(cast<BitCastInst>(User), AI, Info); 488 if (Info.isUnsafe) return; 489 break; 490 } 491 DEBUG(errs() << " Transformation preventing inst: " << *User << '\n'); 492 return MarkUnsafe(Info); 493 case Instruction::Call: 494 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { 495 if (isFirstElt) { 496 isSafeMemIntrinsicOnAllocation(MI, AI, I.getOperandNo(), Info); 497 if (Info.isUnsafe) return; 498 break; 499 } 500 } 501 DEBUG(errs() << " Transformation preventing inst: " << *User << '\n'); 502 return MarkUnsafe(Info); 503 default: 504 DEBUG(errs() << " Transformation preventing inst: " << *User << '\n'); 505 return MarkUnsafe(Info); 506 } 507 } 508 return; // All users look ok :) 509} 510 511/// AllUsersAreLoads - Return true if all users of this value are loads. 512static bool AllUsersAreLoads(Value *Ptr) { 513 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); 514 I != E; ++I) 515 if (cast<Instruction>(*I)->getOpcode() != Instruction::Load) 516 return false; 517 return true; 518} 519 520/// isSafeUseOfAllocation - Check to see if this user is an allowed use for an 521/// aggregate allocation. 522/// 523void SROA::isSafeUseOfAllocation(Instruction *User, AllocaInst *AI, 524 AllocaInfo &Info) { 525 if (BitCastInst *C = dyn_cast<BitCastInst>(User)) 526 return isSafeUseOfBitCastedAllocation(C, AI, Info); 527 528 if (LoadInst *LI = dyn_cast<LoadInst>(User)) 529 if (!LI->isVolatile()) 530 return;// Loads (returning a first class aggregrate) are always rewritable 531 532 if (StoreInst *SI = dyn_cast<StoreInst>(User)) 533 if (!SI->isVolatile() && SI->getOperand(0) != AI) 534 return;// Store is ok if storing INTO the pointer, not storing the pointer 535 536 GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User); 537 if (GEPI == 0) 538 return MarkUnsafe(Info); 539 540 gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI); 541 542 // The GEP is not safe to transform if not of the form "GEP <ptr>, 0, <cst>". 543 if (I == E || 544 I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) { 545 return MarkUnsafe(Info); 546 } 547 548 ++I; 549 if (I == E) return MarkUnsafe(Info); // ran out of GEP indices?? 550 551 bool IsAllZeroIndices = true; 552 553 // If the first index is a non-constant index into an array, see if we can 554 // handle it as a special case. 555 if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { 556 if (!isa<ConstantInt>(I.getOperand())) { 557 IsAllZeroIndices = 0; 558 uint64_t NumElements = AT->getNumElements(); 559 560 // If this is an array index and the index is not constant, we cannot 561 // promote... that is unless the array has exactly one or two elements in 562 // it, in which case we CAN promote it, but we have to canonicalize this 563 // out if this is the only problem. 564 if ((NumElements == 1 || NumElements == 2) && 565 AllUsersAreLoads(GEPI)) { 566 Info.needsCleanup = true; 567 return; // Canonicalization required! 568 } 569 return MarkUnsafe(Info); 570 } 571 } 572 573 // Walk through the GEP type indices, checking the types that this indexes 574 // into. 575 for (; I != E; ++I) { 576 // Ignore struct elements, no extra checking needed for these. 577 if (isa<StructType>(*I)) 578 continue; 579 580 ConstantInt *IdxVal = dyn_cast<ConstantInt>(I.getOperand()); 581 if (!IdxVal) return MarkUnsafe(Info); 582 583 // Are all indices still zero? 584 IsAllZeroIndices &= IdxVal->isZero(); 585 586 if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { 587 // This GEP indexes an array. Verify that this is an in-range constant 588 // integer. Specifically, consider A[0][i]. We cannot know that the user 589 // isn't doing invalid things like allowing i to index an out-of-range 590 // subscript that accesses A[1]. Because of this, we have to reject SROA 591 // of any accesses into structs where any of the components are variables. 592 if (IdxVal->getZExtValue() >= AT->getNumElements()) 593 return MarkUnsafe(Info); 594 } else if (const VectorType *VT = dyn_cast<VectorType>(*I)) { 595 if (IdxVal->getZExtValue() >= VT->getNumElements()) 596 return MarkUnsafe(Info); 597 } 598 } 599 600 // If there are any non-simple uses of this getelementptr, make sure to reject 601 // them. 602 return isSafeElementUse(GEPI, IsAllZeroIndices, AI, Info); 603} 604 605/// isSafeMemIntrinsicOnAllocation - Return true if the specified memory 606/// intrinsic can be promoted by SROA. At this point, we know that the operand 607/// of the memintrinsic is a pointer to the beginning of the allocation. 608void SROA::isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocaInst *AI, 609 unsigned OpNo, AllocaInfo &Info) { 610 // If not constant length, give up. 611 ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); 612 if (!Length) return MarkUnsafe(Info); 613 614 // If not the whole aggregate, give up. 615 if (Length->getZExtValue() != 616 TD->getTypeAllocSize(AI->getType()->getElementType())) 617 return MarkUnsafe(Info); 618 619 // We only know about memcpy/memset/memmove. 620 if (!isa<MemIntrinsic>(MI)) 621 return MarkUnsafe(Info); 622 623 // Otherwise, we can transform it. Determine whether this is a memcpy/set 624 // into or out of the aggregate. 625 if (OpNo == 1) 626 Info.isMemCpyDst = true; 627 else { 628 assert(OpNo == 2); 629 Info.isMemCpySrc = true; 630 } 631} 632 633/// isSafeUseOfBitCastedAllocation - Return true if all users of this bitcast 634/// are 635void SROA::isSafeUseOfBitCastedAllocation(BitCastInst *BC, AllocaInst *AI, 636 AllocaInfo &Info) { 637 for (Value::use_iterator UI = BC->use_begin(), E = BC->use_end(); 638 UI != E; ++UI) { 639 if (BitCastInst *BCU = dyn_cast<BitCastInst>(UI)) { 640 isSafeUseOfBitCastedAllocation(BCU, AI, Info); 641 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(UI)) { 642 isSafeMemIntrinsicOnAllocation(MI, AI, UI.getOperandNo(), Info); 643 } else if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { 644 if (SI->isVolatile()) 645 return MarkUnsafe(Info); 646 647 // If storing the entire alloca in one chunk through a bitcasted pointer 648 // to integer, we can transform it. This happens (for example) when you 649 // cast a {i32,i32}* to i64* and store through it. This is similar to the 650 // memcpy case and occurs in various "byval" cases and emulated memcpys. 651 if (isa<IntegerType>(SI->getOperand(0)->getType()) && 652 TD->getTypeAllocSize(SI->getOperand(0)->getType()) == 653 TD->getTypeAllocSize(AI->getType()->getElementType())) { 654 Info.isMemCpyDst = true; 655 continue; 656 } 657 return MarkUnsafe(Info); 658 } else if (LoadInst *LI = dyn_cast<LoadInst>(UI)) { 659 if (LI->isVolatile()) 660 return MarkUnsafe(Info); 661 662 // If loading the entire alloca in one chunk through a bitcasted pointer 663 // to integer, we can transform it. This happens (for example) when you 664 // cast a {i32,i32}* to i64* and load through it. This is similar to the 665 // memcpy case and occurs in various "byval" cases and emulated memcpys. 666 if (isa<IntegerType>(LI->getType()) && 667 TD->getTypeAllocSize(LI->getType()) == 668 TD->getTypeAllocSize(AI->getType()->getElementType())) { 669 Info.isMemCpySrc = true; 670 continue; 671 } 672 return MarkUnsafe(Info); 673 } else if (isa<DbgInfoIntrinsic>(UI)) { 674 // If one user is DbgInfoIntrinsic then check if all users are 675 // DbgInfoIntrinsics. 676 if (OnlyUsedByDbgInfoIntrinsics(BC)) { 677 Info.needsCleanup = true; 678 return; 679 } 680 else 681 MarkUnsafe(Info); 682 } 683 else { 684 return MarkUnsafe(Info); 685 } 686 if (Info.isUnsafe) return; 687 } 688} 689 690/// RewriteBitCastUserOfAlloca - BCInst (transitively) bitcasts AI, or indexes 691/// to its first element. Transform users of the cast to use the new values 692/// instead. 693void SROA::RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocaInst *AI, 694 SmallVector<AllocaInst*, 32> &NewElts) { 695 Value::use_iterator UI = BCInst->use_begin(), UE = BCInst->use_end(); 696 while (UI != UE) { 697 Instruction *User = cast<Instruction>(*UI++); 698 if (BitCastInst *BCU = dyn_cast<BitCastInst>(User)) { 699 RewriteBitCastUserOfAlloca(BCU, AI, NewElts); 700 if (BCU->use_empty()) BCU->eraseFromParent(); 701 continue; 702 } 703 704 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { 705 // This must be memcpy/memmove/memset of the entire aggregate. 706 // Split into one per element. 707 RewriteMemIntrinUserOfAlloca(MI, BCInst, AI, NewElts); 708 continue; 709 } 710 711 if (StoreInst *SI = dyn_cast<StoreInst>(User)) { 712 // If this is a store of the entire alloca from an integer, rewrite it. 713 RewriteStoreUserOfWholeAlloca(SI, AI, NewElts); 714 continue; 715 } 716 717 if (LoadInst *LI = dyn_cast<LoadInst>(User)) { 718 // If this is a load of the entire alloca to an integer, rewrite it. 719 RewriteLoadUserOfWholeAlloca(LI, AI, NewElts); 720 continue; 721 } 722 723 // Otherwise it must be some other user of a gep of the first pointer. Just 724 // leave these alone. 725 continue; 726 } 727} 728 729/// RewriteMemIntrinUserOfAlloca - MI is a memcpy/memset/memmove from or to AI. 730/// Rewrite it to copy or set the elements of the scalarized memory. 731void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst, 732 AllocaInst *AI, 733 SmallVector<AllocaInst*, 32> &NewElts) { 734 735 // If this is a memcpy/memmove, construct the other pointer as the 736 // appropriate type. The "Other" pointer is the pointer that goes to memory 737 // that doesn't have anything to do with the alloca that we are promoting. For 738 // memset, this Value* stays null. 739 Value *OtherPtr = 0; 740 LLVMContext &Context = MI->getContext(); 741 unsigned MemAlignment = MI->getAlignment(); 742 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { // memmove/memcopy 743 if (BCInst == MTI->getRawDest()) 744 OtherPtr = MTI->getRawSource(); 745 else { 746 assert(BCInst == MTI->getRawSource()); 747 OtherPtr = MTI->getRawDest(); 748 } 749 } 750 751 // If there is an other pointer, we want to convert it to the same pointer 752 // type as AI has, so we can GEP through it safely. 753 if (OtherPtr) { 754 // It is likely that OtherPtr is a bitcast, if so, remove it. 755 if (BitCastInst *BC = dyn_cast<BitCastInst>(OtherPtr)) 756 OtherPtr = BC->getOperand(0); 757 // All zero GEPs are effectively bitcasts. 758 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(OtherPtr)) 759 if (GEP->hasAllZeroIndices()) 760 OtherPtr = GEP->getOperand(0); 761 762 if (ConstantExpr *BCE = dyn_cast<ConstantExpr>(OtherPtr)) 763 if (BCE->getOpcode() == Instruction::BitCast) 764 OtherPtr = BCE->getOperand(0); 765 766 // If the pointer is not the right type, insert a bitcast to the right 767 // type. 768 if (OtherPtr->getType() != AI->getType()) 769 OtherPtr = new BitCastInst(OtherPtr, AI->getType(), OtherPtr->getName(), 770 MI); 771 } 772 773 // Process each element of the aggregate. 774 Value *TheFn = MI->getOperand(0); 775 const Type *BytePtrTy = MI->getRawDest()->getType(); 776 bool SROADest = MI->getRawDest() == BCInst; 777 778 Constant *Zero = Constant::getNullValue(Type::getInt32Ty(MI->getContext())); 779 780 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { 781 // If this is a memcpy/memmove, emit a GEP of the other element address. 782 Value *OtherElt = 0; 783 unsigned OtherEltAlign = MemAlignment; 784 785 if (OtherPtr) { 786 Value *Idx[2] = { Zero, 787 ConstantInt::get(Type::getInt32Ty(MI->getContext()), i) }; 788 OtherElt = GetElementPtrInst::Create(OtherPtr, Idx, Idx + 2, 789 OtherPtr->getNameStr()+"."+Twine(i), 790 MI); 791 uint64_t EltOffset; 792 const PointerType *OtherPtrTy = cast<PointerType>(OtherPtr->getType()); 793 if (const StructType *ST = 794 dyn_cast<StructType>(OtherPtrTy->getElementType())) { 795 EltOffset = TD->getStructLayout(ST)->getElementOffset(i); 796 } else { 797 const Type *EltTy = 798 cast<SequentialType>(OtherPtr->getType())->getElementType(); 799 EltOffset = TD->getTypeAllocSize(EltTy)*i; 800 } 801 802 // The alignment of the other pointer is the guaranteed alignment of the 803 // element, which is affected by both the known alignment of the whole 804 // mem intrinsic and the alignment of the element. If the alignment of 805 // the memcpy (f.e.) is 32 but the element is at a 4-byte offset, then the 806 // known alignment is just 4 bytes. 807 OtherEltAlign = (unsigned)MinAlign(OtherEltAlign, EltOffset); 808 } 809 810 Value *EltPtr = NewElts[i]; 811 const Type *EltTy = cast<PointerType>(EltPtr->getType())->getElementType(); 812 813 // If we got down to a scalar, insert a load or store as appropriate. 814 if (EltTy->isSingleValueType()) { 815 if (isa<MemTransferInst>(MI)) { 816 if (SROADest) { 817 // From Other to Alloca. 818 Value *Elt = new LoadInst(OtherElt, "tmp", false, OtherEltAlign, MI); 819 new StoreInst(Elt, EltPtr, MI); 820 } else { 821 // From Alloca to Other. 822 Value *Elt = new LoadInst(EltPtr, "tmp", MI); 823 new StoreInst(Elt, OtherElt, false, OtherEltAlign, MI); 824 } 825 continue; 826 } 827 assert(isa<MemSetInst>(MI)); 828 829 // If the stored element is zero (common case), just store a null 830 // constant. 831 Constant *StoreVal; 832 if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getOperand(2))) { 833 if (CI->isZero()) { 834 StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0> 835 } else { 836 // If EltTy is a vector type, get the element type. 837 const Type *ValTy = EltTy->getScalarType(); 838 839 // Construct an integer with the right value. 840 unsigned EltSize = TD->getTypeSizeInBits(ValTy); 841 APInt OneVal(EltSize, CI->getZExtValue()); 842 APInt TotalVal(OneVal); 843 // Set each byte. 844 for (unsigned i = 0; 8*i < EltSize; ++i) { 845 TotalVal = TotalVal.shl(8); 846 TotalVal |= OneVal; 847 } 848 849 // Convert the integer value to the appropriate type. 850 StoreVal = ConstantInt::get(Context, TotalVal); 851 if (isa<PointerType>(ValTy)) 852 StoreVal = ConstantExpr::getIntToPtr(StoreVal, ValTy); 853 else if (ValTy->isFloatingPoint()) 854 StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy); 855 assert(StoreVal->getType() == ValTy && "Type mismatch!"); 856 857 // If the requested value was a vector constant, create it. 858 if (EltTy != ValTy) { 859 unsigned NumElts = cast<VectorType>(ValTy)->getNumElements(); 860 SmallVector<Constant*, 16> Elts(NumElts, StoreVal); 861 StoreVal = ConstantVector::get(&Elts[0], NumElts); 862 } 863 } 864 new StoreInst(StoreVal, EltPtr, MI); 865 continue; 866 } 867 // Otherwise, if we're storing a byte variable, use a memset call for 868 // this element. 869 } 870 871 // Cast the element pointer to BytePtrTy. 872 if (EltPtr->getType() != BytePtrTy) 873 EltPtr = new BitCastInst(EltPtr, BytePtrTy, EltPtr->getNameStr(), MI); 874 875 // Cast the other pointer (if we have one) to BytePtrTy. 876 if (OtherElt && OtherElt->getType() != BytePtrTy) 877 OtherElt = new BitCastInst(OtherElt, BytePtrTy,OtherElt->getNameStr(), 878 MI); 879 880 unsigned EltSize = TD->getTypeAllocSize(EltTy); 881 882 // Finally, insert the meminst for this element. 883 if (isa<MemTransferInst>(MI)) { 884 Value *Ops[] = { 885 SROADest ? EltPtr : OtherElt, // Dest ptr 886 SROADest ? OtherElt : EltPtr, // Src ptr 887 ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size 888 // Align 889 ConstantInt::get(Type::getInt32Ty(MI->getContext()), OtherEltAlign) 890 }; 891 CallInst::Create(TheFn, Ops, Ops + 4, "", MI); 892 } else { 893 assert(isa<MemSetInst>(MI)); 894 Value *Ops[] = { 895 EltPtr, MI->getOperand(2), // Dest, Value, 896 ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size 897 Zero // Align 898 }; 899 CallInst::Create(TheFn, Ops, Ops + 4, "", MI); 900 } 901 } 902 MI->eraseFromParent(); 903} 904 905/// RewriteStoreUserOfWholeAlloca - We found an store of an integer that 906/// overwrites the entire allocation. Extract out the pieces of the stored 907/// integer and store them individually. 908void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, 909 SmallVector<AllocaInst*, 32> &NewElts){ 910 // Extract each element out of the integer according to its structure offset 911 // and store the element value to the individual alloca. 912 Value *SrcVal = SI->getOperand(0); 913 const Type *AllocaEltTy = AI->getType()->getElementType(); 914 uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy); 915 916 // If this isn't a store of an integer to the whole alloca, it may be a store 917 // to the first element. Just ignore the store in this case and normal SROA 918 // will handle it. 919 if (!isa<IntegerType>(SrcVal->getType()) || 920 TD->getTypeAllocSizeInBits(SrcVal->getType()) != AllocaSizeBits) 921 return; 922 // Handle tail padding by extending the operand 923 if (TD->getTypeSizeInBits(SrcVal->getType()) != AllocaSizeBits) 924 SrcVal = new ZExtInst(SrcVal, 925 IntegerType::get(SI->getContext(), AllocaSizeBits), 926 "", SI); 927 928 DEBUG(errs() << "PROMOTING STORE TO WHOLE ALLOCA: " << *AI << '\n' << *SI 929 << '\n'); 930 931 // There are two forms here: AI could be an array or struct. Both cases 932 // have different ways to compute the element offset. 933 if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { 934 const StructLayout *Layout = TD->getStructLayout(EltSTy); 935 936 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { 937 // Get the number of bits to shift SrcVal to get the value. 938 const Type *FieldTy = EltSTy->getElementType(i); 939 uint64_t Shift = Layout->getElementOffsetInBits(i); 940 941 if (TD->isBigEndian()) 942 Shift = AllocaSizeBits-Shift-TD->getTypeAllocSizeInBits(FieldTy); 943 944 Value *EltVal = SrcVal; 945 if (Shift) { 946 Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); 947 EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal, 948 "sroa.store.elt", SI); 949 } 950 951 // Truncate down to an integer of the right size. 952 uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy); 953 954 // Ignore zero sized fields like {}, they obviously contain no data. 955 if (FieldSizeBits == 0) continue; 956 957 if (FieldSizeBits != AllocaSizeBits) 958 EltVal = new TruncInst(EltVal, 959 IntegerType::get(SI->getContext(), FieldSizeBits), 960 "", SI); 961 Value *DestField = NewElts[i]; 962 if (EltVal->getType() == FieldTy) { 963 // Storing to an integer field of this size, just do it. 964 } else if (FieldTy->isFloatingPoint() || isa<VectorType>(FieldTy)) { 965 // Bitcast to the right element type (for fp/vector values). 966 EltVal = new BitCastInst(EltVal, FieldTy, "", SI); 967 } else { 968 // Otherwise, bitcast the dest pointer (for aggregates). 969 DestField = new BitCastInst(DestField, 970 PointerType::getUnqual(EltVal->getType()), 971 "", SI); 972 } 973 new StoreInst(EltVal, DestField, SI); 974 } 975 976 } else { 977 const ArrayType *ATy = cast<ArrayType>(AllocaEltTy); 978 const Type *ArrayEltTy = ATy->getElementType(); 979 uint64_t ElementOffset = TD->getTypeAllocSizeInBits(ArrayEltTy); 980 uint64_t ElementSizeBits = TD->getTypeSizeInBits(ArrayEltTy); 981 982 uint64_t Shift; 983 984 if (TD->isBigEndian()) 985 Shift = AllocaSizeBits-ElementOffset; 986 else 987 Shift = 0; 988 989 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { 990 // Ignore zero sized fields like {}, they obviously contain no data. 991 if (ElementSizeBits == 0) continue; 992 993 Value *EltVal = SrcVal; 994 if (Shift) { 995 Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); 996 EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal, 997 "sroa.store.elt", SI); 998 } 999 1000 // Truncate down to an integer of the right size. 1001 if (ElementSizeBits != AllocaSizeBits) 1002 EltVal = new TruncInst(EltVal, 1003 IntegerType::get(SI->getContext(), 1004 ElementSizeBits),"",SI); 1005 Value *DestField = NewElts[i]; 1006 if (EltVal->getType() == ArrayEltTy) { 1007 // Storing to an integer field of this size, just do it. 1008 } else if (ArrayEltTy->isFloatingPoint() || isa<VectorType>(ArrayEltTy)) { 1009 // Bitcast to the right element type (for fp/vector values). 1010 EltVal = new BitCastInst(EltVal, ArrayEltTy, "", SI); 1011 } else { 1012 // Otherwise, bitcast the dest pointer (for aggregates). 1013 DestField = new BitCastInst(DestField, 1014 PointerType::getUnqual(EltVal->getType()), 1015 "", SI); 1016 } 1017 new StoreInst(EltVal, DestField, SI); 1018 1019 if (TD->isBigEndian()) 1020 Shift -= ElementOffset; 1021 else 1022 Shift += ElementOffset; 1023 } 1024 } 1025 1026 SI->eraseFromParent(); 1027} 1028 1029/// RewriteLoadUserOfWholeAlloca - We found an load of the entire allocation to 1030/// an integer. Load the individual pieces to form the aggregate value. 1031void SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, 1032 SmallVector<AllocaInst*, 32> &NewElts) { 1033 // Extract each element out of the NewElts according to its structure offset 1034 // and form the result value. 1035 const Type *AllocaEltTy = AI->getType()->getElementType(); 1036 uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy); 1037 1038 // If this isn't a load of the whole alloca to an integer, it may be a load 1039 // of the first element. Just ignore the load in this case and normal SROA 1040 // will handle it. 1041 if (!isa<IntegerType>(LI->getType()) || 1042 TD->getTypeAllocSizeInBits(LI->getType()) != AllocaSizeBits) 1043 return; 1044 1045 DEBUG(errs() << "PROMOTING LOAD OF WHOLE ALLOCA: " << *AI << '\n' << *LI 1046 << '\n'); 1047 1048 // There are two forms here: AI could be an array or struct. Both cases 1049 // have different ways to compute the element offset. 1050 const StructLayout *Layout = 0; 1051 uint64_t ArrayEltBitOffset = 0; 1052 if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { 1053 Layout = TD->getStructLayout(EltSTy); 1054 } else { 1055 const Type *ArrayEltTy = cast<ArrayType>(AllocaEltTy)->getElementType(); 1056 ArrayEltBitOffset = TD->getTypeAllocSizeInBits(ArrayEltTy); 1057 } 1058 1059 Value *ResultVal = 1060 Constant::getNullValue(IntegerType::get(LI->getContext(), AllocaSizeBits)); 1061 1062 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { 1063 // Load the value from the alloca. If the NewElt is an aggregate, cast 1064 // the pointer to an integer of the same size before doing the load. 1065 Value *SrcField = NewElts[i]; 1066 const Type *FieldTy = 1067 cast<PointerType>(SrcField->getType())->getElementType(); 1068 uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy); 1069 1070 // Ignore zero sized fields like {}, they obviously contain no data. 1071 if (FieldSizeBits == 0) continue; 1072 1073 const IntegerType *FieldIntTy = IntegerType::get(LI->getContext(), 1074 FieldSizeBits); 1075 if (!isa<IntegerType>(FieldTy) && !FieldTy->isFloatingPoint() && 1076 !isa<VectorType>(FieldTy)) 1077 SrcField = new BitCastInst(SrcField, 1078 PointerType::getUnqual(FieldIntTy), 1079 "", LI); 1080 SrcField = new LoadInst(SrcField, "sroa.load.elt", LI); 1081 1082 // If SrcField is a fp or vector of the right size but that isn't an 1083 // integer type, bitcast to an integer so we can shift it. 1084 if (SrcField->getType() != FieldIntTy) 1085 SrcField = new BitCastInst(SrcField, FieldIntTy, "", LI); 1086 1087 // Zero extend the field to be the same size as the final alloca so that 1088 // we can shift and insert it. 1089 if (SrcField->getType() != ResultVal->getType()) 1090 SrcField = new ZExtInst(SrcField, ResultVal->getType(), "", LI); 1091 1092 // Determine the number of bits to shift SrcField. 1093 uint64_t Shift; 1094 if (Layout) // Struct case. 1095 Shift = Layout->getElementOffsetInBits(i); 1096 else // Array case. 1097 Shift = i*ArrayEltBitOffset; 1098 1099 if (TD->isBigEndian()) 1100 Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth(); 1101 1102 if (Shift) { 1103 Value *ShiftVal = ConstantInt::get(SrcField->getType(), Shift); 1104 SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI); 1105 } 1106 1107 ResultVal = BinaryOperator::CreateOr(SrcField, ResultVal, "", LI); 1108 } 1109 1110 // Handle tail padding by truncating the result 1111 if (TD->getTypeSizeInBits(LI->getType()) != AllocaSizeBits) 1112 ResultVal = new TruncInst(ResultVal, LI->getType(), "", LI); 1113 1114 LI->replaceAllUsesWith(ResultVal); 1115 LI->eraseFromParent(); 1116} 1117 1118 1119/// HasPadding - Return true if the specified type has any structure or 1120/// alignment padding, false otherwise. 1121static bool HasPadding(const Type *Ty, const TargetData &TD) { 1122 if (const StructType *STy = dyn_cast<StructType>(Ty)) { 1123 const StructLayout *SL = TD.getStructLayout(STy); 1124 unsigned PrevFieldBitOffset = 0; 1125 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1126 unsigned FieldBitOffset = SL->getElementOffsetInBits(i); 1127 1128 // Padding in sub-elements? 1129 if (HasPadding(STy->getElementType(i), TD)) 1130 return true; 1131 1132 // Check to see if there is any padding between this element and the 1133 // previous one. 1134 if (i) { 1135 unsigned PrevFieldEnd = 1136 PrevFieldBitOffset+TD.getTypeSizeInBits(STy->getElementType(i-1)); 1137 if (PrevFieldEnd < FieldBitOffset) 1138 return true; 1139 } 1140 1141 PrevFieldBitOffset = FieldBitOffset; 1142 } 1143 1144 // Check for tail padding. 1145 if (unsigned EltCount = STy->getNumElements()) { 1146 unsigned PrevFieldEnd = PrevFieldBitOffset + 1147 TD.getTypeSizeInBits(STy->getElementType(EltCount-1)); 1148 if (PrevFieldEnd < SL->getSizeInBits()) 1149 return true; 1150 } 1151 1152 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 1153 return HasPadding(ATy->getElementType(), TD); 1154 } else if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { 1155 return HasPadding(VTy->getElementType(), TD); 1156 } 1157 return TD.getTypeSizeInBits(Ty) != TD.getTypeAllocSizeInBits(Ty); 1158} 1159 1160/// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of 1161/// an aggregate can be broken down into elements. Return 0 if not, 3 if safe, 1162/// or 1 if safe after canonicalization has been performed. 1163/// 1164int SROA::isSafeAllocaToScalarRepl(AllocaInst *AI) { 1165 // Loop over the use list of the alloca. We can only transform it if all of 1166 // the users are safe to transform. 1167 AllocaInfo Info; 1168 1169 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); 1170 I != E; ++I) { 1171 isSafeUseOfAllocation(cast<Instruction>(*I), AI, Info); 1172 if (Info.isUnsafe) { 1173 DEBUG(errs() << "Cannot transform: " << *AI << "\n due to user: " 1174 << **I << '\n'); 1175 return 0; 1176 } 1177 } 1178 1179 // Okay, we know all the users are promotable. If the aggregate is a memcpy 1180 // source and destination, we have to be careful. In particular, the memcpy 1181 // could be moving around elements that live in structure padding of the LLVM 1182 // types, but may actually be used. In these cases, we refuse to promote the 1183 // struct. 1184 if (Info.isMemCpySrc && Info.isMemCpyDst && 1185 HasPadding(AI->getType()->getElementType(), *TD)) 1186 return 0; 1187 1188 // If we require cleanup, return 1, otherwise return 3. 1189 return Info.needsCleanup ? 1 : 3; 1190} 1191 1192/// CleanupGEP - GEP is used by an Alloca, which can be prompted after the GEP 1193/// is canonicalized here. 1194void SROA::CleanupGEP(GetElementPtrInst *GEPI) { 1195 gep_type_iterator I = gep_type_begin(GEPI); 1196 ++I; 1197 1198 const ArrayType *AT = dyn_cast<ArrayType>(*I); 1199 if (!AT) 1200 return; 1201 1202 uint64_t NumElements = AT->getNumElements(); 1203 1204 if (isa<ConstantInt>(I.getOperand())) 1205 return; 1206 1207 if (NumElements == 1) { 1208 GEPI->setOperand(2, 1209 Constant::getNullValue(Type::getInt32Ty(GEPI->getContext()))); 1210 return; 1211 } 1212 1213 assert(NumElements == 2 && "Unhandled case!"); 1214 // All users of the GEP must be loads. At each use of the GEP, insert 1215 // two loads of the appropriate indexed GEP and select between them. 1216 Value *IsOne = new ICmpInst(GEPI, ICmpInst::ICMP_NE, I.getOperand(), 1217 Constant::getNullValue(I.getOperand()->getType()), 1218 "isone"); 1219 // Insert the new GEP instructions, which are properly indexed. 1220 SmallVector<Value*, 8> Indices(GEPI->op_begin()+1, GEPI->op_end()); 1221 Indices[1] = Constant::getNullValue(Type::getInt32Ty(GEPI->getContext())); 1222 Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0), 1223 Indices.begin(), 1224 Indices.end(), 1225 GEPI->getName()+".0", GEPI); 1226 Indices[1] = ConstantInt::get(Type::getInt32Ty(GEPI->getContext()), 1); 1227 Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0), 1228 Indices.begin(), 1229 Indices.end(), 1230 GEPI->getName()+".1", GEPI); 1231 // Replace all loads of the variable index GEP with loads from both 1232 // indexes and a select. 1233 while (!GEPI->use_empty()) { 1234 LoadInst *LI = cast<LoadInst>(GEPI->use_back()); 1235 Value *Zero = new LoadInst(ZeroIdx, LI->getName()+".0", LI); 1236 Value *One = new LoadInst(OneIdx , LI->getName()+".1", LI); 1237 Value *R = SelectInst::Create(IsOne, One, Zero, LI->getName(), LI); 1238 LI->replaceAllUsesWith(R); 1239 LI->eraseFromParent(); 1240 } 1241 GEPI->eraseFromParent(); 1242} 1243 1244 1245/// CleanupAllocaUsers - If SROA reported that it can promote the specified 1246/// allocation, but only if cleaned up, perform the cleanups required. 1247void SROA::CleanupAllocaUsers(AllocaInst *AI) { 1248 // At this point, we know that the end result will be SROA'd and promoted, so 1249 // we can insert ugly code if required so long as sroa+mem2reg will clean it 1250 // up. 1251 for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); 1252 UI != E; ) { 1253 User *U = *UI++; 1254 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) 1255 CleanupGEP(GEPI); 1256 else { 1257 Instruction *I = cast<Instruction>(U); 1258 SmallVector<DbgInfoIntrinsic *, 2> DbgInUses; 1259 if (!isa<StoreInst>(I) && OnlyUsedByDbgInfoIntrinsics(I, &DbgInUses)) { 1260 // Safe to remove debug info uses. 1261 while (!DbgInUses.empty()) { 1262 DbgInfoIntrinsic *DI = DbgInUses.back(); DbgInUses.pop_back(); 1263 DI->eraseFromParent(); 1264 } 1265 I->eraseFromParent(); 1266 } 1267 } 1268 } 1269} 1270 1271/// MergeInType - Add the 'In' type to the accumulated type (Accum) so far at 1272/// the offset specified by Offset (which is specified in bytes). 1273/// 1274/// There are two cases we handle here: 1275/// 1) A union of vector types of the same size and potentially its elements. 1276/// Here we turn element accesses into insert/extract element operations. 1277/// This promotes a <4 x float> with a store of float to the third element 1278/// into a <4 x float> that uses insert element. 1279/// 2) A fully general blob of memory, which we turn into some (potentially 1280/// large) integer type with extract and insert operations where the loads 1281/// and stores would mutate the memory. 1282static void MergeInType(const Type *In, uint64_t Offset, const Type *&VecTy, 1283 unsigned AllocaSize, const TargetData &TD, 1284 LLVMContext &Context) { 1285 // If this could be contributing to a vector, analyze it. 1286 if (VecTy != Type::getVoidTy(Context)) { // either null or a vector type. 1287 1288 // If the In type is a vector that is the same size as the alloca, see if it 1289 // matches the existing VecTy. 1290 if (const VectorType *VInTy = dyn_cast<VectorType>(In)) { 1291 if (VInTy->getBitWidth()/8 == AllocaSize && Offset == 0) { 1292 // If we're storing/loading a vector of the right size, allow it as a 1293 // vector. If this the first vector we see, remember the type so that 1294 // we know the element size. 1295 if (VecTy == 0) 1296 VecTy = VInTy; 1297 return; 1298 } 1299 } else if (In->isFloatTy() || In->isDoubleTy() || 1300 (isa<IntegerType>(In) && In->getPrimitiveSizeInBits() >= 8 && 1301 isPowerOf2_32(In->getPrimitiveSizeInBits()))) { 1302 // If we're accessing something that could be an element of a vector, see 1303 // if the implied vector agrees with what we already have and if Offset is 1304 // compatible with it. 1305 unsigned EltSize = In->getPrimitiveSizeInBits()/8; 1306 if (Offset % EltSize == 0 && 1307 AllocaSize % EltSize == 0 && 1308 (VecTy == 0 || 1309 cast<VectorType>(VecTy)->getElementType() 1310 ->getPrimitiveSizeInBits()/8 == EltSize)) { 1311 if (VecTy == 0) 1312 VecTy = VectorType::get(In, AllocaSize/EltSize); 1313 return; 1314 } 1315 } 1316 } 1317 1318 // Otherwise, we have a case that we can't handle with an optimized vector 1319 // form. We can still turn this into a large integer. 1320 VecTy = Type::getVoidTy(Context); 1321} 1322 1323/// CanConvertToScalar - V is a pointer. If we can convert the pointee and all 1324/// its accesses to use a to single vector type, return true, and set VecTy to 1325/// the new type. If we could convert the alloca into a single promotable 1326/// integer, return true but set VecTy to VoidTy. Further, if the use is not a 1327/// completely trivial use that mem2reg could promote, set IsNotTrivial. Offset 1328/// is the current offset from the base of the alloca being analyzed. 1329/// 1330/// If we see at least one access to the value that is as a vector type, set the 1331/// SawVec flag. 1332/// 1333bool SROA::CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy, 1334 bool &SawVec, uint64_t Offset, 1335 unsigned AllocaSize) { 1336 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { 1337 Instruction *User = cast<Instruction>(*UI); 1338 1339 if (LoadInst *LI = dyn_cast<LoadInst>(User)) { 1340 // Don't break volatile loads. 1341 if (LI->isVolatile()) 1342 return false; 1343 MergeInType(LI->getType(), Offset, VecTy, 1344 AllocaSize, *TD, V->getContext()); 1345 SawVec |= isa<VectorType>(LI->getType()); 1346 continue; 1347 } 1348 1349 if (StoreInst *SI = dyn_cast<StoreInst>(User)) { 1350 // Storing the pointer, not into the value? 1351 if (SI->getOperand(0) == V || SI->isVolatile()) return 0; 1352 MergeInType(SI->getOperand(0)->getType(), Offset, 1353 VecTy, AllocaSize, *TD, V->getContext()); 1354 SawVec |= isa<VectorType>(SI->getOperand(0)->getType()); 1355 continue; 1356 } 1357 1358 if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) { 1359 if (!CanConvertToScalar(BCI, IsNotTrivial, VecTy, SawVec, Offset, 1360 AllocaSize)) 1361 return false; 1362 IsNotTrivial = true; 1363 continue; 1364 } 1365 1366 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { 1367 // If this is a GEP with a variable indices, we can't handle it. 1368 if (!GEP->hasAllConstantIndices()) 1369 return false; 1370 1371 // Compute the offset that this GEP adds to the pointer. 1372 SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); 1373 uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(), 1374 &Indices[0], Indices.size()); 1375 // See if all uses can be converted. 1376 if (!CanConvertToScalar(GEP, IsNotTrivial, VecTy, SawVec,Offset+GEPOffset, 1377 AllocaSize)) 1378 return false; 1379 IsNotTrivial = true; 1380 continue; 1381 } 1382 1383 // If this is a constant sized memset of a constant value (e.g. 0) we can 1384 // handle it. 1385 if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { 1386 // Store of constant value and constant size. 1387 if (isa<ConstantInt>(MSI->getValue()) && 1388 isa<ConstantInt>(MSI->getLength())) { 1389 IsNotTrivial = true; 1390 continue; 1391 } 1392 } 1393 1394 // If this is a memcpy or memmove into or out of the whole allocation, we 1395 // can handle it like a load or store of the scalar type. 1396 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { 1397 if (ConstantInt *Len = dyn_cast<ConstantInt>(MTI->getLength())) 1398 if (Len->getZExtValue() == AllocaSize && Offset == 0) { 1399 IsNotTrivial = true; 1400 continue; 1401 } 1402 } 1403 1404 // Ignore dbg intrinsic. 1405 if (isa<DbgInfoIntrinsic>(User)) 1406 continue; 1407 1408 // Otherwise, we cannot handle this! 1409 return false; 1410 } 1411 1412 return true; 1413} 1414 1415 1416/// ConvertUsesToScalar - Convert all of the users of Ptr to use the new alloca 1417/// directly. This happens when we are converting an "integer union" to a 1418/// single integer scalar, or when we are converting a "vector union" to a 1419/// vector with insert/extractelement instructions. 1420/// 1421/// Offset is an offset from the original alloca, in bits that need to be 1422/// shifted to the right. By the end of this, there should be no uses of Ptr. 1423void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset) { 1424 while (!Ptr->use_empty()) { 1425 Instruction *User = cast<Instruction>(Ptr->use_back()); 1426 1427 if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { 1428 ConvertUsesToScalar(CI, NewAI, Offset); 1429 CI->eraseFromParent(); 1430 continue; 1431 } 1432 1433 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { 1434 // Compute the offset that this GEP adds to the pointer. 1435 SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); 1436 uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(), 1437 &Indices[0], Indices.size()); 1438 ConvertUsesToScalar(GEP, NewAI, Offset+GEPOffset*8); 1439 GEP->eraseFromParent(); 1440 continue; 1441 } 1442 1443 IRBuilder<> Builder(User->getParent(), User); 1444 1445 if (LoadInst *LI = dyn_cast<LoadInst>(User)) { 1446 // The load is a bit extract from NewAI shifted right by Offset bits. 1447 Value *LoadedVal = Builder.CreateLoad(NewAI, "tmp"); 1448 Value *NewLoadVal 1449 = ConvertScalar_ExtractValue(LoadedVal, LI->getType(), Offset, Builder); 1450 LI->replaceAllUsesWith(NewLoadVal); 1451 LI->eraseFromParent(); 1452 continue; 1453 } 1454 1455 if (StoreInst *SI = dyn_cast<StoreInst>(User)) { 1456 assert(SI->getOperand(0) != Ptr && "Consistency error!"); 1457 // FIXME: Remove once builder has Twine API. 1458 Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").str().c_str()); 1459 Value *New = ConvertScalar_InsertValue(SI->getOperand(0), Old, Offset, 1460 Builder); 1461 Builder.CreateStore(New, NewAI); 1462 SI->eraseFromParent(); 1463 continue; 1464 } 1465 1466 // If this is a constant sized memset of a constant value (e.g. 0) we can 1467 // transform it into a store of the expanded constant value. 1468 if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { 1469 assert(MSI->getRawDest() == Ptr && "Consistency error!"); 1470 unsigned NumBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue(); 1471 if (NumBytes != 0) { 1472 unsigned Val = cast<ConstantInt>(MSI->getValue())->getZExtValue(); 1473 1474 // Compute the value replicated the right number of times. 1475 APInt APVal(NumBytes*8, Val); 1476 1477 // Splat the value if non-zero. 1478 if (Val) 1479 for (unsigned i = 1; i != NumBytes; ++i) 1480 APVal |= APVal << 8; 1481 1482 // FIXME: Remove once builder has Twine API. 1483 Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").str().c_str()); 1484 Value *New = ConvertScalar_InsertValue( 1485 ConstantInt::get(User->getContext(), APVal), 1486 Old, Offset, Builder); 1487 Builder.CreateStore(New, NewAI); 1488 } 1489 MSI->eraseFromParent(); 1490 continue; 1491 } 1492 1493 // If this is a memcpy or memmove into or out of the whole allocation, we 1494 // can handle it like a load or store of the scalar type. 1495 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { 1496 assert(Offset == 0 && "must be store to start of alloca"); 1497 1498 // If the source and destination are both to the same alloca, then this is 1499 // a noop copy-to-self, just delete it. Otherwise, emit a load and store 1500 // as appropriate. 1501 AllocaInst *OrigAI = cast<AllocaInst>(Ptr->getUnderlyingObject()); 1502 1503 if (MTI->getSource()->getUnderlyingObject() != OrigAI) { 1504 // Dest must be OrigAI, change this to be a load from the original 1505 // pointer (bitcasted), then a store to our new alloca. 1506 assert(MTI->getRawDest() == Ptr && "Neither use is of pointer?"); 1507 Value *SrcPtr = MTI->getSource(); 1508 SrcPtr = Builder.CreateBitCast(SrcPtr, NewAI->getType()); 1509 1510 LoadInst *SrcVal = Builder.CreateLoad(SrcPtr, "srcval"); 1511 SrcVal->setAlignment(MTI->getAlignment()); 1512 Builder.CreateStore(SrcVal, NewAI); 1513 } else if (MTI->getDest()->getUnderlyingObject() != OrigAI) { 1514 // Src must be OrigAI, change this to be a load from NewAI then a store 1515 // through the original dest pointer (bitcasted). 1516 assert(MTI->getRawSource() == Ptr && "Neither use is of pointer?"); 1517 LoadInst *SrcVal = Builder.CreateLoad(NewAI, "srcval"); 1518 1519 Value *DstPtr = Builder.CreateBitCast(MTI->getDest(), NewAI->getType()); 1520 StoreInst *NewStore = Builder.CreateStore(SrcVal, DstPtr); 1521 NewStore->setAlignment(MTI->getAlignment()); 1522 } else { 1523 // Noop transfer. Src == Dst 1524 } 1525 1526 1527 MTI->eraseFromParent(); 1528 continue; 1529 } 1530 1531 // If user is a dbg info intrinsic then it is safe to remove it. 1532 if (isa<DbgInfoIntrinsic>(User)) { 1533 User->eraseFromParent(); 1534 continue; 1535 } 1536 1537 llvm_unreachable("Unsupported operation!"); 1538 } 1539} 1540 1541/// ConvertScalar_ExtractValue - Extract a value of type ToType from an integer 1542/// or vector value FromVal, extracting the bits from the offset specified by 1543/// Offset. This returns the value, which is of type ToType. 1544/// 1545/// This happens when we are converting an "integer union" to a single 1546/// integer scalar, or when we are converting a "vector union" to a vector with 1547/// insert/extractelement instructions. 1548/// 1549/// Offset is an offset from the original alloca, in bits that need to be 1550/// shifted to the right. 1551Value *SROA::ConvertScalar_ExtractValue(Value *FromVal, const Type *ToType, 1552 uint64_t Offset, IRBuilder<> &Builder) { 1553 // If the load is of the whole new alloca, no conversion is needed. 1554 if (FromVal->getType() == ToType && Offset == 0) 1555 return FromVal; 1556 1557 // If the result alloca is a vector type, this is either an element 1558 // access or a bitcast to another vector type of the same size. 1559 if (const VectorType *VTy = dyn_cast<VectorType>(FromVal->getType())) { 1560 if (isa<VectorType>(ToType)) 1561 return Builder.CreateBitCast(FromVal, ToType, "tmp"); 1562 1563 // Otherwise it must be an element access. 1564 unsigned Elt = 0; 1565 if (Offset) { 1566 unsigned EltSize = TD->getTypeAllocSizeInBits(VTy->getElementType()); 1567 Elt = Offset/EltSize; 1568 assert(EltSize*Elt == Offset && "Invalid modulus in validity checking"); 1569 } 1570 // Return the element extracted out of it. 1571 Value *V = Builder.CreateExtractElement(FromVal, ConstantInt::get( 1572 Type::getInt32Ty(FromVal->getContext()), Elt), "tmp"); 1573 if (V->getType() != ToType) 1574 V = Builder.CreateBitCast(V, ToType, "tmp"); 1575 return V; 1576 } 1577 1578 // If ToType is a first class aggregate, extract out each of the pieces and 1579 // use insertvalue's to form the FCA. 1580 if (const StructType *ST = dyn_cast<StructType>(ToType)) { 1581 const StructLayout &Layout = *TD->getStructLayout(ST); 1582 Value *Res = UndefValue::get(ST); 1583 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { 1584 Value *Elt = ConvertScalar_ExtractValue(FromVal, ST->getElementType(i), 1585 Offset+Layout.getElementOffsetInBits(i), 1586 Builder); 1587 Res = Builder.CreateInsertValue(Res, Elt, i, "tmp"); 1588 } 1589 return Res; 1590 } 1591 1592 if (const ArrayType *AT = dyn_cast<ArrayType>(ToType)) { 1593 uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType()); 1594 Value *Res = UndefValue::get(AT); 1595 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { 1596 Value *Elt = ConvertScalar_ExtractValue(FromVal, AT->getElementType(), 1597 Offset+i*EltSize, Builder); 1598 Res = Builder.CreateInsertValue(Res, Elt, i, "tmp"); 1599 } 1600 return Res; 1601 } 1602 1603 // Otherwise, this must be a union that was converted to an integer value. 1604 const IntegerType *NTy = cast<IntegerType>(FromVal->getType()); 1605 1606 // If this is a big-endian system and the load is narrower than the 1607 // full alloca type, we need to do a shift to get the right bits. 1608 int ShAmt = 0; 1609 if (TD->isBigEndian()) { 1610 // On big-endian machines, the lowest bit is stored at the bit offset 1611 // from the pointer given by getTypeStoreSizeInBits. This matters for 1612 // integers with a bitwidth that is not a multiple of 8. 1613 ShAmt = TD->getTypeStoreSizeInBits(NTy) - 1614 TD->getTypeStoreSizeInBits(ToType) - Offset; 1615 } else { 1616 ShAmt = Offset; 1617 } 1618 1619 // Note: we support negative bitwidths (with shl) which are not defined. 1620 // We do this to support (f.e.) loads off the end of a structure where 1621 // only some bits are used. 1622 if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth()) 1623 FromVal = Builder.CreateLShr(FromVal, 1624 ConstantInt::get(FromVal->getType(), 1625 ShAmt), "tmp"); 1626 else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth()) 1627 FromVal = Builder.CreateShl(FromVal, 1628 ConstantInt::get(FromVal->getType(), 1629 -ShAmt), "tmp"); 1630 1631 // Finally, unconditionally truncate the integer to the right width. 1632 unsigned LIBitWidth = TD->getTypeSizeInBits(ToType); 1633 if (LIBitWidth < NTy->getBitWidth()) 1634 FromVal = 1635 Builder.CreateTrunc(FromVal, IntegerType::get(FromVal->getContext(), 1636 LIBitWidth), "tmp"); 1637 else if (LIBitWidth > NTy->getBitWidth()) 1638 FromVal = 1639 Builder.CreateZExt(FromVal, IntegerType::get(FromVal->getContext(), 1640 LIBitWidth), "tmp"); 1641 1642 // If the result is an integer, this is a trunc or bitcast. 1643 if (isa<IntegerType>(ToType)) { 1644 // Should be done. 1645 } else if (ToType->isFloatingPoint() || isa<VectorType>(ToType)) { 1646 // Just do a bitcast, we know the sizes match up. 1647 FromVal = Builder.CreateBitCast(FromVal, ToType, "tmp"); 1648 } else { 1649 // Otherwise must be a pointer. 1650 FromVal = Builder.CreateIntToPtr(FromVal, ToType, "tmp"); 1651 } 1652 assert(FromVal->getType() == ToType && "Didn't convert right?"); 1653 return FromVal; 1654} 1655 1656 1657/// ConvertScalar_InsertValue - Insert the value "SV" into the existing integer 1658/// or vector value "Old" at the offset specified by Offset. 1659/// 1660/// This happens when we are converting an "integer union" to a 1661/// single integer scalar, or when we are converting a "vector union" to a 1662/// vector with insert/extractelement instructions. 1663/// 1664/// Offset is an offset from the original alloca, in bits that need to be 1665/// shifted to the right. 1666Value *SROA::ConvertScalar_InsertValue(Value *SV, Value *Old, 1667 uint64_t Offset, IRBuilder<> &Builder) { 1668 1669 // Convert the stored type to the actual type, shift it left to insert 1670 // then 'or' into place. 1671 const Type *AllocaType = Old->getType(); 1672 LLVMContext &Context = Old->getContext(); 1673 1674 if (const VectorType *VTy = dyn_cast<VectorType>(AllocaType)) { 1675 uint64_t VecSize = TD->getTypeAllocSizeInBits(VTy); 1676 uint64_t ValSize = TD->getTypeAllocSizeInBits(SV->getType()); 1677 1678 // Changing the whole vector with memset or with an access of a different 1679 // vector type? 1680 if (ValSize == VecSize) 1681 return Builder.CreateBitCast(SV, AllocaType, "tmp"); 1682 1683 uint64_t EltSize = TD->getTypeAllocSizeInBits(VTy->getElementType()); 1684 1685 // Must be an element insertion. 1686 unsigned Elt = Offset/EltSize; 1687 1688 if (SV->getType() != VTy->getElementType()) 1689 SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp"); 1690 1691 SV = Builder.CreateInsertElement(Old, SV, 1692 ConstantInt::get(Type::getInt32Ty(SV->getContext()), Elt), 1693 "tmp"); 1694 return SV; 1695 } 1696 1697 // If SV is a first-class aggregate value, insert each value recursively. 1698 if (const StructType *ST = dyn_cast<StructType>(SV->getType())) { 1699 const StructLayout &Layout = *TD->getStructLayout(ST); 1700 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { 1701 Value *Elt = Builder.CreateExtractValue(SV, i, "tmp"); 1702 Old = ConvertScalar_InsertValue(Elt, Old, 1703 Offset+Layout.getElementOffsetInBits(i), 1704 Builder); 1705 } 1706 return Old; 1707 } 1708 1709 if (const ArrayType *AT = dyn_cast<ArrayType>(SV->getType())) { 1710 uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType()); 1711 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { 1712 Value *Elt = Builder.CreateExtractValue(SV, i, "tmp"); 1713 Old = ConvertScalar_InsertValue(Elt, Old, Offset+i*EltSize, Builder); 1714 } 1715 return Old; 1716 } 1717 1718 // If SV is a float, convert it to the appropriate integer type. 1719 // If it is a pointer, do the same. 1720 unsigned SrcWidth = TD->getTypeSizeInBits(SV->getType()); 1721 unsigned DestWidth = TD->getTypeSizeInBits(AllocaType); 1722 unsigned SrcStoreWidth = TD->getTypeStoreSizeInBits(SV->getType()); 1723 unsigned DestStoreWidth = TD->getTypeStoreSizeInBits(AllocaType); 1724 if (SV->getType()->isFloatingPoint() || isa<VectorType>(SV->getType())) 1725 SV = Builder.CreateBitCast(SV, 1726 IntegerType::get(SV->getContext(),SrcWidth), "tmp"); 1727 else if (isa<PointerType>(SV->getType())) 1728 SV = Builder.CreatePtrToInt(SV, TD->getIntPtrType(SV->getContext()), "tmp"); 1729 1730 // Zero extend or truncate the value if needed. 1731 if (SV->getType() != AllocaType) { 1732 if (SV->getType()->getPrimitiveSizeInBits() < 1733 AllocaType->getPrimitiveSizeInBits()) 1734 SV = Builder.CreateZExt(SV, AllocaType, "tmp"); 1735 else { 1736 // Truncation may be needed if storing more than the alloca can hold 1737 // (undefined behavior). 1738 SV = Builder.CreateTrunc(SV, AllocaType, "tmp"); 1739 SrcWidth = DestWidth; 1740 SrcStoreWidth = DestStoreWidth; 1741 } 1742 } 1743 1744 // If this is a big-endian system and the store is narrower than the 1745 // full alloca type, we need to do a shift to get the right bits. 1746 int ShAmt = 0; 1747 if (TD->isBigEndian()) { 1748 // On big-endian machines, the lowest bit is stored at the bit offset 1749 // from the pointer given by getTypeStoreSizeInBits. This matters for 1750 // integers with a bitwidth that is not a multiple of 8. 1751 ShAmt = DestStoreWidth - SrcStoreWidth - Offset; 1752 } else { 1753 ShAmt = Offset; 1754 } 1755 1756 // Note: we support negative bitwidths (with shr) which are not defined. 1757 // We do this to support (f.e.) stores off the end of a structure where 1758 // only some bits in the structure are set. 1759 APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth)); 1760 if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) { 1761 SV = Builder.CreateShl(SV, ConstantInt::get(SV->getType(), 1762 ShAmt), "tmp"); 1763 Mask <<= ShAmt; 1764 } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) { 1765 SV = Builder.CreateLShr(SV, ConstantInt::get(SV->getType(), 1766 -ShAmt), "tmp"); 1767 Mask = Mask.lshr(-ShAmt); 1768 } 1769 1770 // Mask out the bits we are about to insert from the old value, and or 1771 // in the new bits. 1772 if (SrcWidth != DestWidth) { 1773 assert(DestWidth > SrcWidth); 1774 Old = Builder.CreateAnd(Old, ConstantInt::get(Context, ~Mask), "mask"); 1775 SV = Builder.CreateOr(Old, SV, "ins"); 1776 } 1777 return SV; 1778} 1779 1780 1781 1782/// PointsToConstantGlobal - Return true if V (possibly indirectly) points to 1783/// some part of a constant global variable. This intentionally only accepts 1784/// constant expressions because we don't can't rewrite arbitrary instructions. 1785static bool PointsToConstantGlobal(Value *V) { 1786 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) 1787 return GV->isConstant(); 1788 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 1789 if (CE->getOpcode() == Instruction::BitCast || 1790 CE->getOpcode() == Instruction::GetElementPtr) 1791 return PointsToConstantGlobal(CE->getOperand(0)); 1792 return false; 1793} 1794 1795/// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) 1796/// pointer to an alloca. Ignore any reads of the pointer, return false if we 1797/// see any stores or other unknown uses. If we see pointer arithmetic, keep 1798/// track of whether it moves the pointer (with isOffset) but otherwise traverse 1799/// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to 1800/// the alloca, and if the source pointer is a pointer to a constant global, we 1801/// can optimize this. 1802static bool isOnlyCopiedFromConstantGlobal(Value *V, Instruction *&TheCopy, 1803 bool isOffset) { 1804 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { 1805 if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) 1806 // Ignore non-volatile loads, they are always ok. 1807 if (!LI->isVolatile()) 1808 continue; 1809 1810 if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) { 1811 // If uses of the bitcast are ok, we are ok. 1812 if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, isOffset)) 1813 return false; 1814 continue; 1815 } 1816 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { 1817 // If the GEP has all zero indices, it doesn't offset the pointer. If it 1818 // doesn't, it does. 1819 if (!isOnlyCopiedFromConstantGlobal(GEP, TheCopy, 1820 isOffset || !GEP->hasAllZeroIndices())) 1821 return false; 1822 continue; 1823 } 1824 1825 // If this is isn't our memcpy/memmove, reject it as something we can't 1826 // handle. 1827 if (!isa<MemTransferInst>(*UI)) 1828 return false; 1829 1830 // If we already have seen a copy, reject the second one. 1831 if (TheCopy) return false; 1832 1833 // If the pointer has been offset from the start of the alloca, we can't 1834 // safely handle this. 1835 if (isOffset) return false; 1836 1837 // If the memintrinsic isn't using the alloca as the dest, reject it. 1838 if (UI.getOperandNo() != 1) return false; 1839 1840 MemIntrinsic *MI = cast<MemIntrinsic>(*UI); 1841 1842 // If the source of the memcpy/move is not a constant global, reject it. 1843 if (!PointsToConstantGlobal(MI->getOperand(2))) 1844 return false; 1845 1846 // Otherwise, the transform is safe. Remember the copy instruction. 1847 TheCopy = MI; 1848 } 1849 return true; 1850} 1851 1852/// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only 1853/// modified by a copy from a constant global. If we can prove this, we can 1854/// replace any uses of the alloca with uses of the global directly. 1855Instruction *SROA::isOnlyCopiedFromConstantGlobal(AllocaInst *AI) { 1856 Instruction *TheCopy = 0; 1857 if (::isOnlyCopiedFromConstantGlobal(AI, TheCopy, false)) 1858 return TheCopy; 1859 return 0; 1860} 1861