comparison lib/Transforms/IPO/GlobalOpt.cpp @ 80:67baa08a3894

update to LLVM 3.6
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Thu, 25 Sep 2014 16:56:18 +0900
parents 54457678186b
children 60c9769439b8
comparison
equal deleted inserted replaced
76:9e74acfe8c42 80:67baa08a3894
11 // taken. If obviously true, it marks read/write globals as constant, deletes 11 // taken. If obviously true, it marks read/write globals as constant, deletes
12 // variables only stored to, etc. 12 // variables only stored to, etc.
13 // 13 //
14 //===----------------------------------------------------------------------===// 14 //===----------------------------------------------------------------------===//
15 15
16 #define DEBUG_TYPE "globalopt"
17 #include "llvm/Transforms/IPO.h" 16 #include "llvm/Transforms/IPO.h"
18 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h" 22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/ConstantFolding.h" 23 #include "llvm/Analysis/ConstantFolding.h"
24 #include "llvm/Analysis/MemoryBuiltins.h" 24 #include "llvm/Analysis/MemoryBuiltins.h"
25 #include "llvm/IR/CallSite.h"
25 #include "llvm/IR/CallingConv.h" 26 #include "llvm/IR/CallingConv.h"
26 #include "llvm/IR/Constants.h" 27 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DataLayout.h" 28 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/GetElementPtrTypeIterator.h"
29 #include "llvm/IR/Instructions.h" 31 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/IntrinsicInst.h" 32 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/IR/Module.h" 33 #include "llvm/IR/Module.h"
32 #include "llvm/IR/Operator.h" 34 #include "llvm/IR/Operator.h"
35 #include "llvm/IR/ValueHandle.h"
33 #include "llvm/Pass.h" 36 #include "llvm/Pass.h"
34 #include "llvm/Support/CallSite.h"
35 #include "llvm/Support/Debug.h" 37 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h" 38 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/GetElementPtrTypeIterator.h"
38 #include "llvm/Support/MathExtras.h" 39 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/raw_ostream.h" 40 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Target/TargetLibraryInfo.h" 41 #include "llvm/Target/TargetLibraryInfo.h"
42 #include "llvm/Transforms/Utils/CtorUtils.h"
41 #include "llvm/Transforms/Utils/GlobalStatus.h" 43 #include "llvm/Transforms/Utils/GlobalStatus.h"
42 #include "llvm/Transforms/Utils/ModuleUtils.h" 44 #include "llvm/Transforms/Utils/ModuleUtils.h"
43 #include <algorithm> 45 #include <algorithm>
46 #include <deque>
44 using namespace llvm; 47 using namespace llvm;
48
49 #define DEBUG_TYPE "globalopt"
45 50
46 STATISTIC(NumMarked , "Number of globals marked constant"); 51 STATISTIC(NumMarked , "Number of globals marked constant");
47 STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr"); 52 STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr");
48 STATISTIC(NumSRA , "Number of aggregate globals broken into scalars"); 53 STATISTIC(NumSRA , "Number of aggregate globals broken into scalars");
49 STATISTIC(NumHeapSRA , "Number of heap objects SRA'd"); 54 STATISTIC(NumHeapSRA , "Number of heap objects SRA'd");
60 STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated"); 65 STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
61 STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed"); 66 STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
62 67
63 namespace { 68 namespace {
64 struct GlobalOpt : public ModulePass { 69 struct GlobalOpt : public ModulePass {
65 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 70 void getAnalysisUsage(AnalysisUsage &AU) const override {
66 AU.addRequired<TargetLibraryInfo>(); 71 AU.addRequired<TargetLibraryInfo>();
67 } 72 }
68 static char ID; // Pass identification, replacement for typeid 73 static char ID; // Pass identification, replacement for typeid
69 GlobalOpt() : ModulePass(ID) { 74 GlobalOpt() : ModulePass(ID) {
70 initializeGlobalOptPass(*PassRegistry::getPassRegistry()); 75 initializeGlobalOptPass(*PassRegistry::getPassRegistry());
71 } 76 }
72 77
73 bool runOnModule(Module &M); 78 bool runOnModule(Module &M) override;
74 79
75 private: 80 private:
76 GlobalVariable *FindGlobalCtors(Module &M);
77 bool OptimizeFunctions(Module &M); 81 bool OptimizeFunctions(Module &M);
78 bool OptimizeGlobalVars(Module &M); 82 bool OptimizeGlobalVars(Module &M);
79 bool OptimizeGlobalAliases(Module &M); 83 bool OptimizeGlobalAliases(Module &M);
80 bool OptimizeGlobalCtorsList(GlobalVariable *&GCL);
81 bool ProcessGlobal(GlobalVariable *GV,Module::global_iterator &GVI); 84 bool ProcessGlobal(GlobalVariable *GV,Module::global_iterator &GVI);
82 bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI, 85 bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI,
83 const GlobalStatus &GS); 86 const GlobalStatus &GS);
84 bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn); 87 bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);
85 88
86 DataLayout *TD; 89 const DataLayout *DL;
87 TargetLibraryInfo *TLI; 90 TargetLibraryInfo *TLI;
88 }; 91 };
89 } 92 }
90 93
91 char GlobalOpt::ID = 0; 94 char GlobalOpt::ID = 0;
193 // If Dead[n].first is the only use of a malloc result, we can delete its 196 // If Dead[n].first is the only use of a malloc result, we can delete its
194 // chain of computation and the store to the global in Dead[n].second. 197 // chain of computation and the store to the global in Dead[n].second.
195 SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead; 198 SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead;
196 199
197 // Constants can't be pointers to dynamically allocated memory. 200 // Constants can't be pointers to dynamically allocated memory.
198 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); 201 for (Value::user_iterator UI = GV->user_begin(), E = GV->user_end();
199 UI != E;) { 202 UI != E;) {
200 User *U = *UI++; 203 User *U = *UI++;
201 if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 204 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
202 Value *V = SI->getValueOperand(); 205 Value *V = SI->getValueOperand();
203 if (isa<Constant>(V)) { 206 if (isa<Constant>(V)) {
263 /// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all 266 /// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all
264 /// users of the global, cleaning up the obvious ones. This is largely just a 267 /// users of the global, cleaning up the obvious ones. This is largely just a
265 /// quick scan over the use list to clean up the easy and obvious cruft. This 268 /// quick scan over the use list to clean up the easy and obvious cruft. This
266 /// returns true if it made a change. 269 /// returns true if it made a change.
267 static bool CleanupConstantGlobalUsers(Value *V, Constant *Init, 270 static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
268 DataLayout *TD, TargetLibraryInfo *TLI) { 271 const DataLayout *DL,
272 TargetLibraryInfo *TLI) {
269 bool Changed = false; 273 bool Changed = false;
270 SmallVector<User*, 8> WorkList(V->use_begin(), V->use_end()); 274 // Note that we need to use a weak value handle for the worklist items. When
275 // we delete a constant array, we may also be holding pointer to one of its
276 // elements (or an element of one of its elements if we're dealing with an
277 // array of arrays) in the worklist.
278 SmallVector<WeakVH, 8> WorkList(V->user_begin(), V->user_end());
271 while (!WorkList.empty()) { 279 while (!WorkList.empty()) {
272 User *U = WorkList.pop_back_val(); 280 Value *UV = WorkList.pop_back_val();
281 if (!UV)
282 continue;
283
284 User *U = cast<User>(UV);
273 285
274 if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 286 if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
275 if (Init) { 287 if (Init) {
276 // Replace the load with the initializer. 288 // Replace the load with the initializer.
277 LI->replaceAllUsesWith(Init); 289 LI->replaceAllUsesWith(Init);
282 // Store must be unreachable or storing Init into the global. 294 // Store must be unreachable or storing Init into the global.
283 SI->eraseFromParent(); 295 SI->eraseFromParent();
284 Changed = true; 296 Changed = true;
285 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { 297 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
286 if (CE->getOpcode() == Instruction::GetElementPtr) { 298 if (CE->getOpcode() == Instruction::GetElementPtr) {
287 Constant *SubInit = 0; 299 Constant *SubInit = nullptr;
288 if (Init) 300 if (Init)
289 SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); 301 SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
290 Changed |= CleanupConstantGlobalUsers(CE, SubInit, TD, TLI); 302 Changed |= CleanupConstantGlobalUsers(CE, SubInit, DL, TLI);
291 } else if (CE->getOpcode() == Instruction::BitCast && 303 } else if ((CE->getOpcode() == Instruction::BitCast &&
292 CE->getType()->isPointerTy()) { 304 CE->getType()->isPointerTy()) ||
305 CE->getOpcode() == Instruction::AddrSpaceCast) {
293 // Pointer cast, delete any stores and memsets to the global. 306 // Pointer cast, delete any stores and memsets to the global.
294 Changed |= CleanupConstantGlobalUsers(CE, 0, TD, TLI); 307 Changed |= CleanupConstantGlobalUsers(CE, nullptr, DL, TLI);
295 } 308 }
296 309
297 if (CE->use_empty()) { 310 if (CE->use_empty()) {
298 CE->destroyConstant(); 311 CE->destroyConstant();
299 Changed = true; 312 Changed = true;
300 } 313 }
301 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { 314 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
302 // Do not transform "gepinst (gep constexpr (GV))" here, because forming 315 // Do not transform "gepinst (gep constexpr (GV))" here, because forming
303 // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold 316 // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold
304 // and will invalidate our notion of what Init is. 317 // and will invalidate our notion of what Init is.
305 Constant *SubInit = 0; 318 Constant *SubInit = nullptr;
306 if (!isa<ConstantExpr>(GEP->getOperand(0))) { 319 if (!isa<ConstantExpr>(GEP->getOperand(0))) {
307 ConstantExpr *CE = 320 ConstantExpr *CE =
308 dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP, TD, TLI)); 321 dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP, DL, TLI));
309 if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr) 322 if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
310 SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); 323 SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
311 324
312 // If the initializer is an all-null value and we have an inbounds GEP, 325 // If the initializer is an all-null value and we have an inbounds GEP,
313 // we already know what the result of any load from that GEP is. 326 // we already know what the result of any load from that GEP is.
314 // TODO: Handle splats. 327 // TODO: Handle splats.
315 if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds()) 328 if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds())
316 SubInit = Constant::getNullValue(GEP->getType()->getElementType()); 329 SubInit = Constant::getNullValue(GEP->getType()->getElementType());
317 } 330 }
318 Changed |= CleanupConstantGlobalUsers(GEP, SubInit, TD, TLI); 331 Changed |= CleanupConstantGlobalUsers(GEP, SubInit, DL, TLI);
319 332
320 if (GEP->use_empty()) { 333 if (GEP->use_empty()) {
321 GEP->eraseFromParent(); 334 GEP->eraseFromParent();
322 Changed = true; 335 Changed = true;
323 } 336 }
330 } else if (Constant *C = dyn_cast<Constant>(U)) { 343 } else if (Constant *C = dyn_cast<Constant>(U)) {
331 // If we have a chain of dead constantexprs or other things dangling from 344 // If we have a chain of dead constantexprs or other things dangling from
332 // us, and if they are all dead, nuke them without remorse. 345 // us, and if they are all dead, nuke them without remorse.
333 if (isSafeToDestroyConstant(C)) { 346 if (isSafeToDestroyConstant(C)) {
334 C->destroyConstant(); 347 C->destroyConstant();
335 CleanupConstantGlobalUsers(V, Init, TD, TLI); 348 CleanupConstantGlobalUsers(V, Init, DL, TLI);
336 return true; 349 return true;
337 } 350 }
338 } 351 }
339 } 352 }
340 return Changed; 353 return Changed;
357 if (StoreInst *SI = dyn_cast<StoreInst>(I)) 370 if (StoreInst *SI = dyn_cast<StoreInst>(I))
358 return SI->getOperand(0) != V; 371 return SI->getOperand(0) != V;
359 372
360 // Otherwise, it must be a GEP. 373 // Otherwise, it must be a GEP.
361 GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I); 374 GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I);
362 if (GEPI == 0) return false; 375 if (!GEPI) return false;
363 376
364 if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) || 377 if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) ||
365 !cast<Constant>(GEPI->getOperand(1))->isNullValue()) 378 !cast<Constant>(GEPI->getOperand(1))->isNullValue())
366 return false; 379 return false;
367 380
368 for (Value::use_iterator I = GEPI->use_begin(), E = GEPI->use_end(); 381 for (User *U : GEPI->users())
369 I != E; ++I) 382 if (!isSafeSROAElementUse(U))
370 if (!isSafeSROAElementUse(*I))
371 return false; 383 return false;
372 return true; 384 return true;
373 } 385 }
374 386
375 387
431 if (!IdxVal || IdxVal->getZExtValue() >= NumElements) 443 if (!IdxVal || IdxVal->getZExtValue() >= NumElements)
432 return false; 444 return false;
433 } 445 }
434 } 446 }
435 447
436 for (Value::use_iterator I = U->use_begin(), E = U->use_end(); I != E; ++I) 448 for (User *UU : U->users())
437 if (!isSafeSROAElementUse(*I)) 449 if (!isSafeSROAElementUse(UU))
438 return false; 450 return false;
451
439 return true; 452 return true;
440 } 453 }
441 454
442 /// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it 455 /// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it
443 /// is safe for us to perform this transformation. 456 /// is safe for us to perform this transformation.
444 /// 457 ///
445 static bool GlobalUsersSafeToSRA(GlobalValue *GV) { 458 static bool GlobalUsersSafeToSRA(GlobalValue *GV) {
446 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); 459 for (User *U : GV->users())
447 UI != E; ++UI) { 460 if (!IsUserOfGlobalSafeForSRA(U, GV))
448 if (!IsUserOfGlobalSafeForSRA(*UI, GV))
449 return false; 461 return false;
450 } 462
451 return true; 463 return true;
452 } 464 }
453 465
454 466
455 /// SRAGlobal - Perform scalar replacement of aggregates on the specified global 467 /// SRAGlobal - Perform scalar replacement of aggregates on the specified global
456 /// variable. This opens the door for other optimizations by exposing the 468 /// variable. This opens the door for other optimizations by exposing the
457 /// behavior of the program in a more fine-grained way. We have determined that 469 /// behavior of the program in a more fine-grained way. We have determined that
458 /// this transformation is safe already. We return the first global variable we 470 /// this transformation is safe already. We return the first global variable we
459 /// insert so that the caller can reprocess it. 471 /// insert so that the caller can reprocess it.
460 static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &TD) { 472 static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
461 // Make sure this global only has simple uses that we can SRA. 473 // Make sure this global only has simple uses that we can SRA.
462 if (!GlobalUsersSafeToSRA(GV)) 474 if (!GlobalUsersSafeToSRA(GV))
463 return 0; 475 return nullptr;
464 476
465 assert(GV->hasLocalLinkage() && !GV->isConstant()); 477 assert(GV->hasLocalLinkage() && !GV->isConstant());
466 Constant *Init = GV->getInitializer(); 478 Constant *Init = GV->getInitializer();
467 Type *Ty = Init->getType(); 479 Type *Ty = Init->getType();
468 480
470 Module::GlobalListType &Globals = GV->getParent()->getGlobalList(); 482 Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
471 483
472 // Get the alignment of the global, either explicit or target-specific. 484 // Get the alignment of the global, either explicit or target-specific.
473 unsigned StartAlignment = GV->getAlignment(); 485 unsigned StartAlignment = GV->getAlignment();
474 if (StartAlignment == 0) 486 if (StartAlignment == 0)
475 StartAlignment = TD.getABITypeAlignment(GV->getType()); 487 StartAlignment = DL.getABITypeAlignment(GV->getType());
476 488
477 if (StructType *STy = dyn_cast<StructType>(Ty)) { 489 if (StructType *STy = dyn_cast<StructType>(Ty)) {
478 NewGlobals.reserve(STy->getNumElements()); 490 NewGlobals.reserve(STy->getNumElements());
479 const StructLayout &Layout = *TD.getStructLayout(STy); 491 const StructLayout &Layout = *DL.getStructLayout(STy);
480 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 492 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
481 Constant *In = Init->getAggregateElement(i); 493 Constant *In = Init->getAggregateElement(i);
482 assert(In && "Couldn't get element of initializer?"); 494 assert(In && "Couldn't get element of initializer?");
483 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false, 495 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
484 GlobalVariable::InternalLinkage, 496 GlobalVariable::InternalLinkage,
491 // Calculate the known alignment of the field. If the original aggregate 503 // Calculate the known alignment of the field. If the original aggregate
492 // had 256 byte alignment for example, something might depend on that: 504 // had 256 byte alignment for example, something might depend on that:
493 // propagate info to each field. 505 // propagate info to each field.
494 uint64_t FieldOffset = Layout.getElementOffset(i); 506 uint64_t FieldOffset = Layout.getElementOffset(i);
495 unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset); 507 unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset);
496 if (NewAlign > TD.getABITypeAlignment(STy->getElementType(i))) 508 if (NewAlign > DL.getABITypeAlignment(STy->getElementType(i)))
497 NGV->setAlignment(NewAlign); 509 NGV->setAlignment(NewAlign);
498 } 510 }
499 } else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) { 511 } else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
500 unsigned NumElements = 0; 512 unsigned NumElements = 0;
501 if (ArrayType *ATy = dyn_cast<ArrayType>(STy)) 513 if (ArrayType *ATy = dyn_cast<ArrayType>(STy))
502 NumElements = ATy->getNumElements(); 514 NumElements = ATy->getNumElements();
503 else 515 else
504 NumElements = cast<VectorType>(STy)->getNumElements(); 516 NumElements = cast<VectorType>(STy)->getNumElements();
505 517
506 if (NumElements > 16 && GV->hasNUsesOrMore(16)) 518 if (NumElements > 16 && GV->hasNUsesOrMore(16))
507 return 0; // It's not worth it. 519 return nullptr; // It's not worth it.
508 NewGlobals.reserve(NumElements); 520 NewGlobals.reserve(NumElements);
509 521
510 uint64_t EltSize = TD.getTypeAllocSize(STy->getElementType()); 522 uint64_t EltSize = DL.getTypeAllocSize(STy->getElementType());
511 unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType()); 523 unsigned EltAlign = DL.getABITypeAlignment(STy->getElementType());
512 for (unsigned i = 0, e = NumElements; i != e; ++i) { 524 for (unsigned i = 0, e = NumElements; i != e; ++i) {
513 Constant *In = Init->getAggregateElement(i); 525 Constant *In = Init->getAggregateElement(i);
514 assert(In && "Couldn't get element of initializer?"); 526 assert(In && "Couldn't get element of initializer?");
515 527
516 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false, 528 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
529 NGV->setAlignment(NewAlign); 541 NGV->setAlignment(NewAlign);
530 } 542 }
531 } 543 }
532 544
533 if (NewGlobals.empty()) 545 if (NewGlobals.empty())
534 return 0; 546 return nullptr;
535 547
536 DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV); 548 DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV);
537 549
538 Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext())); 550 Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext()));
539 551
540 // Loop over all of the uses of the global, replacing the constantexpr geps, 552 // Loop over all of the uses of the global, replacing the constantexpr geps,
541 // with smaller constantexpr geps or direct references. 553 // with smaller constantexpr geps or direct references.
542 while (!GV->use_empty()) { 554 while (!GV->use_empty()) {
543 User *GEP = GV->use_back(); 555 User *GEP = GV->user_back();
544 assert(((isa<ConstantExpr>(GEP) && 556 assert(((isa<ConstantExpr>(GEP) &&
545 cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)|| 557 cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
546 isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!"); 558 isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
547 559
548 // Ignore the 1th operand, which has to be zero or else the program is quite 560 // Ignore the 1th operand, which has to be zero or else the program is quite
591 if (NewGlobals[i]->use_empty()) { 603 if (NewGlobals[i]->use_empty()) {
592 Globals.erase(NewGlobals[i]); 604 Globals.erase(NewGlobals[i]);
593 if (FirstGlobal == i) ++FirstGlobal; 605 if (FirstGlobal == i) ++FirstGlobal;
594 } 606 }
595 607
596 return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0; 608 return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : nullptr;
597 } 609 }
598 610
599 /// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified 611 /// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified
600 /// value will trap if the value is dynamically null. PHIs keeps track of any 612 /// value will trap if the value is dynamically null. PHIs keeps track of any
601 /// phi nodes we've seen to avoid reprocessing them. 613 /// phi nodes we've seen to avoid reprocessing them.
602 static bool AllUsesOfValueWillTrapIfNull(const Value *V, 614 static bool AllUsesOfValueWillTrapIfNull(const Value *V,
603 SmallPtrSet<const PHINode*, 8> &PHIs) { 615 SmallPtrSetImpl<const PHINode*> &PHIs) {
604 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; 616 for (const User *U : V->users())
605 ++UI) {
606 const User *U = *UI;
607
608 if (isa<LoadInst>(U)) { 617 if (isa<LoadInst>(U)) {
609 // Will trap. 618 // Will trap.
610 } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) { 619 } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
611 if (SI->getOperand(0) == V) { 620 if (SI->getOperand(0) == V) {
612 //cerr << "NONTRAPPING USE: " << *U; 621 //cerr << "NONTRAPPING USE: " << *U;
630 // If we've already seen this phi node, ignore it, it has already been 639 // If we've already seen this phi node, ignore it, it has already been
631 // checked. 640 // checked.
632 if (PHIs.insert(PN) && !AllUsesOfValueWillTrapIfNull(PN, PHIs)) 641 if (PHIs.insert(PN) && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
633 return false; 642 return false;
634 } else if (isa<ICmpInst>(U) && 643 } else if (isa<ICmpInst>(U) &&
635 isa<ConstantPointerNull>(UI->getOperand(1))) { 644 isa<ConstantPointerNull>(U->getOperand(1))) {
636 // Ignore icmp X, null 645 // Ignore icmp X, null
637 } else { 646 } else {
638 //cerr << "NONTRAPPING USE: " << *U; 647 //cerr << "NONTRAPPING USE: " << *U;
639 return false; 648 return false;
640 } 649 }
641 } 650
642 return true; 651 return true;
643 } 652 }
644 653
645 /// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads 654 /// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads
646 /// from GV will trap if the loaded value is null. Note that this also permits 655 /// from GV will trap if the loaded value is null. Note that this also permits
647 /// comparisons of the loaded value against null, as a special case. 656 /// comparisons of the loaded value against null, as a special case.
648 static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) { 657 static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
649 for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end(); 658 for (const User *U : GV->users())
650 UI != E; ++UI) {
651 const User *U = *UI;
652
653 if (const LoadInst *LI = dyn_cast<LoadInst>(U)) { 659 if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
654 SmallPtrSet<const PHINode*, 8> PHIs; 660 SmallPtrSet<const PHINode*, 8> PHIs;
655 if (!AllUsesOfValueWillTrapIfNull(LI, PHIs)) 661 if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
656 return false; 662 return false;
657 } else if (isa<StoreInst>(U)) { 663 } else if (isa<StoreInst>(U)) {
659 } else { 665 } else {
660 // We don't know or understand this user, bail out. 666 // We don't know or understand this user, bail out.
661 //cerr << "UNKNOWN USER OF GLOBAL!: " << *U; 667 //cerr << "UNKNOWN USER OF GLOBAL!: " << *U;
662 return false; 668 return false;
663 } 669 }
664 }
665 return true; 670 return true;
666 } 671 }
667 672
668 static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) { 673 static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
669 bool Changed = false; 674 bool Changed = false;
670 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) { 675 for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {
671 Instruction *I = cast<Instruction>(*UI++); 676 Instruction *I = cast<Instruction>(*UI++);
672 if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 677 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
673 LI->setOperand(0, NewV); 678 LI->setOperand(0, NewV);
674 Changed = true; 679 Changed = true;
675 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 680 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
691 CS.setArgument(i, NewV); 696 CS.setArgument(i, NewV);
692 } 697 }
693 698
694 if (PassedAsArg) { 699 if (PassedAsArg) {
695 // Being passed as an argument also. Be careful to not invalidate UI! 700 // Being passed as an argument also. Be careful to not invalidate UI!
696 UI = V->use_begin(); 701 UI = V->user_begin();
697 } 702 }
698 } 703 }
699 } else if (CastInst *CI = dyn_cast<CastInst>(I)) { 704 } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
700 Changed |= OptimizeAwayTrappingUsesOfValue(CI, 705 Changed |= OptimizeAwayTrappingUsesOfValue(CI,
701 ConstantExpr::getCast(CI->getOpcode(), 706 ConstantExpr::getCast(CI->getOpcode(),
731 /// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null 736 /// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null
732 /// value stored into it. If there are uses of the loaded value that would trap 737 /// value stored into it. If there are uses of the loaded value that would trap
733 /// if the loaded value is dynamically null, then we know that they cannot be 738 /// if the loaded value is dynamically null, then we know that they cannot be
734 /// reachable with a null optimize away the load. 739 /// reachable with a null optimize away the load.
735 static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV, 740 static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
736 DataLayout *TD, 741 const DataLayout *DL,
737 TargetLibraryInfo *TLI) { 742 TargetLibraryInfo *TLI) {
738 bool Changed = false; 743 bool Changed = false;
739 744
740 // Keep track of whether we are able to remove all the uses of the global 745 // Keep track of whether we are able to remove all the uses of the global
741 // other than the store that defines it. 746 // other than the store that defines it.
742 bool AllNonStoreUsesGone = true; 747 bool AllNonStoreUsesGone = true;
743 748
744 // Replace all uses of loads with uses of uses of the stored value. 749 // Replace all uses of loads with uses of uses of the stored value.
745 for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); GUI != E;){ 750 for (Value::user_iterator GUI = GV->user_begin(), E = GV->user_end(); GUI != E;){
746 User *GlobalUser = *GUI++; 751 User *GlobalUser = *GUI++;
747 if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) { 752 if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
748 Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV); 753 Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
749 // If we were able to delete all uses of the loads 754 // If we were able to delete all uses of the loads
750 if (LI->use_empty()) { 755 if (LI->use_empty()) {
780 if (AllNonStoreUsesGone) { 785 if (AllNonStoreUsesGone) {
781 if (isLeakCheckerRoot(GV)) { 786 if (isLeakCheckerRoot(GV)) {
782 Changed |= CleanupPointerRootUsers(GV, TLI); 787 Changed |= CleanupPointerRootUsers(GV, TLI);
783 } else { 788 } else {
784 Changed = true; 789 Changed = true;
785 CleanupConstantGlobalUsers(GV, 0, TD, TLI); 790 CleanupConstantGlobalUsers(GV, nullptr, DL, TLI);
786 } 791 }
787 if (GV->use_empty()) { 792 if (GV->use_empty()) {
788 DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n"); 793 DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n");
789 Changed = true; 794 Changed = true;
790 GV->eraseFromParent(); 795 GV->eraseFromParent();
794 return Changed; 799 return Changed;
795 } 800 }
796 801
797 /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the 802 /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
798 /// instructions that are foldable. 803 /// instructions that are foldable.
799 static void ConstantPropUsersOf(Value *V, 804 static void ConstantPropUsersOf(Value *V, const DataLayout *DL,
800 DataLayout *TD, TargetLibraryInfo *TLI) { 805 TargetLibraryInfo *TLI) {
801 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) 806 for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; )
802 if (Instruction *I = dyn_cast<Instruction>(*UI++)) 807 if (Instruction *I = dyn_cast<Instruction>(*UI++))
803 if (Constant *NewC = ConstantFoldInstruction(I, TD, TLI)) { 808 if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) {
804 I->replaceAllUsesWith(NewC); 809 I->replaceAllUsesWith(NewC);
805 810
806 // Advance UI to the next non-I use to avoid invalidating it! 811 // Advance UI to the next non-I use to avoid invalidating it!
807 // Instructions could multiply use V. 812 // Instructions could multiply use V.
808 while (UI != E && *UI == I) 813 while (UI != E && *UI == I)
818 /// malloc into a global, and any loads of GV as uses of the new global. 823 /// malloc into a global, and any loads of GV as uses of the new global.
819 static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, 824 static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
820 CallInst *CI, 825 CallInst *CI,
821 Type *AllocTy, 826 Type *AllocTy,
822 ConstantInt *NElements, 827 ConstantInt *NElements,
823 DataLayout *TD, 828 const DataLayout *DL,
824 TargetLibraryInfo *TLI) { 829 TargetLibraryInfo *TLI) {
825 DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n'); 830 DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n');
826 831
827 Type *GlobalType; 832 Type *GlobalType;
828 if (NElements->getZExtValue() == 1) 833 if (NElements->getZExtValue() == 1)
842 GV->getThreadLocalMode()); 847 GV->getThreadLocalMode());
843 848
844 // If there are bitcast users of the malloc (which is typical, usually we have 849 // If there are bitcast users of the malloc (which is typical, usually we have
845 // a malloc + bitcast) then replace them with uses of the new global. Update 850 // a malloc + bitcast) then replace them with uses of the new global. Update
846 // other users to use the global as well. 851 // other users to use the global as well.
847 BitCastInst *TheBC = 0; 852 BitCastInst *TheBC = nullptr;
848 while (!CI->use_empty()) { 853 while (!CI->use_empty()) {
849 Instruction *User = cast<Instruction>(CI->use_back()); 854 Instruction *User = cast<Instruction>(CI->user_back());
850 if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) { 855 if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
851 if (BCI->getType() == NewGV->getType()) { 856 if (BCI->getType() == NewGV->getType()) {
852 BCI->replaceAllUsesWith(NewGV); 857 BCI->replaceAllUsesWith(NewGV);
853 BCI->eraseFromParent(); 858 BCI->eraseFromParent();
854 } else { 859 } else {
855 BCI->setOperand(0, NewGV); 860 BCI->setOperand(0, NewGV);
856 } 861 }
857 } else { 862 } else {
858 if (TheBC == 0) 863 if (!TheBC)
859 TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI); 864 TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI);
860 User->replaceUsesOfWith(CI, TheBC); 865 User->replaceUsesOfWith(CI, TheBC);
861 } 866 }
862 } 867 }
863 868
875 GV->getName()+".init", GV->getThreadLocalMode()); 880 GV->getName()+".init", GV->getThreadLocalMode());
876 bool InitBoolUsed = false; 881 bool InitBoolUsed = false;
877 882
878 // Loop over all uses of GV, processing them in turn. 883 // Loop over all uses of GV, processing them in turn.
879 while (!GV->use_empty()) { 884 while (!GV->use_empty()) {
880 if (StoreInst *SI = dyn_cast<StoreInst>(GV->use_back())) { 885 if (StoreInst *SI = dyn_cast<StoreInst>(GV->user_back())) {
881 // The global is initialized when the store to it occurs. 886 // The global is initialized when the store to it occurs.
882 new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0, 887 new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0,
883 SI->getOrdering(), SI->getSynchScope(), SI); 888 SI->getOrdering(), SI->getSynchScope(), SI);
884 SI->eraseFromParent(); 889 SI->eraseFromParent();
885 continue; 890 continue;
886 } 891 }
887 892
888 LoadInst *LI = cast<LoadInst>(GV->use_back()); 893 LoadInst *LI = cast<LoadInst>(GV->user_back());
889 while (!LI->use_empty()) { 894 while (!LI->use_empty()) {
890 Use &LoadUse = LI->use_begin().getUse(); 895 Use &LoadUse = *LI->use_begin();
891 if (!isa<ICmpInst>(LoadUse.getUser())) { 896 ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());
897 if (!ICI) {
892 LoadUse = RepValue; 898 LoadUse = RepValue;
893 continue; 899 continue;
894 } 900 }
895 901
896 ICmpInst *ICI = cast<ICmpInst>(LoadUse.getUser());
897 // Replace the cmp X, 0 with a use of the bool value. 902 // Replace the cmp X, 0 with a use of the bool value.
898 // Sink the load to where the compare was, if atomic rules allow us to. 903 // Sink the load to where the compare was, if atomic rules allow us to.
899 Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0, 904 Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0,
900 LI->getOrdering(), LI->getSynchScope(), 905 LI->getOrdering(), LI->getSynchScope(),
901 LI->isUnordered() ? (Instruction*)ICI : LI); 906 LI->isUnordered() ? (Instruction*)ICI : LI);
925 } 930 }
926 931
927 // If the initialization boolean was used, insert it, otherwise delete it. 932 // If the initialization boolean was used, insert it, otherwise delete it.
928 if (!InitBoolUsed) { 933 if (!InitBoolUsed) {
929 while (!InitBool->use_empty()) // Delete initializations 934 while (!InitBool->use_empty()) // Delete initializations
930 cast<StoreInst>(InitBool->use_back())->eraseFromParent(); 935 cast<StoreInst>(InitBool->user_back())->eraseFromParent();
931 delete InitBool; 936 delete InitBool;
932 } else 937 } else
933 GV->getParent()->getGlobalList().insert(GV, InitBool); 938 GV->getParent()->getGlobalList().insert(GV, InitBool);
934 939
935 // Now the GV is dead, nuke it and the malloc.. 940 // Now the GV is dead, nuke it and the malloc..
937 CI->eraseFromParent(); 942 CI->eraseFromParent();
938 943
939 // To further other optimizations, loop over all users of NewGV and try to 944 // To further other optimizations, loop over all users of NewGV and try to
940 // constant prop them. This will promote GEP instructions with constant 945 // constant prop them. This will promote GEP instructions with constant
941 // indices into GEP constant-exprs, which will allow global-opt to hack on it. 946 // indices into GEP constant-exprs, which will allow global-opt to hack on it.
942 ConstantPropUsersOf(NewGV, TD, TLI); 947 ConstantPropUsersOf(NewGV, DL, TLI);
943 if (RepValue != NewGV) 948 if (RepValue != NewGV)
944 ConstantPropUsersOf(RepValue, TD, TLI); 949 ConstantPropUsersOf(RepValue, DL, TLI);
945 950
946 return NewGV; 951 return NewGV;
947 } 952 }
948 953
949 /// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking 954 /// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
950 /// to make sure that there are no complex uses of V. We permit simple things 955 /// to make sure that there are no complex uses of V. We permit simple things
951 /// like dereferencing the pointer, but not storing through the address, unless 956 /// like dereferencing the pointer, but not storing through the address, unless
952 /// it is to the specified global. 957 /// it is to the specified global.
953 static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V, 958 static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V,
954 const GlobalVariable *GV, 959 const GlobalVariable *GV,
955 SmallPtrSet<const PHINode*, 8> &PHIs) { 960 SmallPtrSetImpl<const PHINode*> &PHIs) {
956 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); 961 for (const User *U : V->users()) {
957 UI != E; ++UI) { 962 const Instruction *Inst = cast<Instruction>(U);
958 const Instruction *Inst = cast<Instruction>(*UI);
959 963
960 if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) { 964 if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) {
961 continue; // Fine, ignore. 965 continue; // Fine, ignore.
962 } 966 }
963 967
1000 /// GV. This assumes that these value pass the 1004 /// GV. This assumes that these value pass the
1001 /// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate. 1005 /// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate.
1002 static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc, 1006 static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
1003 GlobalVariable *GV) { 1007 GlobalVariable *GV) {
1004 while (!Alloc->use_empty()) { 1008 while (!Alloc->use_empty()) {
1005 Instruction *U = cast<Instruction>(*Alloc->use_begin()); 1009 Instruction *U = cast<Instruction>(*Alloc->user_begin());
1006 Instruction *InsertPt = U; 1010 Instruction *InsertPt = U;
1007 if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 1011 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
1008 // If this is the store of the allocation into the global, remove it. 1012 // If this is the store of the allocation into the global, remove it.
1009 if (SI->getOperand(1) == GV) { 1013 if (SI->getOperand(1) == GV) {
1010 SI->eraseFromParent(); 1014 SI->eraseFromParent();
1011 continue; 1015 continue;
1012 } 1016 }
1013 } else if (PHINode *PN = dyn_cast<PHINode>(U)) { 1017 } else if (PHINode *PN = dyn_cast<PHINode>(U)) {
1014 // Insert the load in the corresponding predecessor, not right before the 1018 // Insert the load in the corresponding predecessor, not right before the
1015 // PHI. 1019 // PHI.
1016 InsertPt = PN->getIncomingBlock(Alloc->use_begin())->getTerminator(); 1020 InsertPt = PN->getIncomingBlock(*Alloc->use_begin())->getTerminator();
1017 } else if (isa<BitCastInst>(U)) { 1021 } else if (isa<BitCastInst>(U)) {
1018 // Must be bitcast between the malloc and store to initialize the global. 1022 // Must be bitcast between the malloc and store to initialize the global.
1019 ReplaceUsesOfMallocWithGlobal(U, GV); 1023 ReplaceUsesOfMallocWithGlobal(U, GV);
1020 U->eraseFromParent(); 1024 U->eraseFromParent();
1021 continue; 1025 continue;
1022 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) { 1026 } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
1023 // If this is a "GEP bitcast" and the user is a store to the global, then 1027 // If this is a "GEP bitcast" and the user is a store to the global, then
1024 // just process it as a bitcast. 1028 // just process it as a bitcast.
1025 if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse()) 1029 if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse())
1026 if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->use_back())) 1030 if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->user_back()))
1027 if (SI->getOperand(1) == GV) { 1031 if (SI->getOperand(1) == GV) {
1028 // Must be bitcast GEP between the malloc and store to initialize 1032 // Must be bitcast GEP between the malloc and store to initialize
1029 // the global. 1033 // the global.
1030 ReplaceUsesOfMallocWithGlobal(GEPI, GV); 1034 ReplaceUsesOfMallocWithGlobal(GEPI, GV);
1031 GEPI->eraseFromParent(); 1035 GEPI->eraseFromParent();
1041 1045
1042 /// LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi 1046 /// LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi
1043 /// of a load) are simple enough to perform heap SRA on. This permits GEP's 1047 /// of a load) are simple enough to perform heap SRA on. This permits GEP's
1044 /// that index through the array and struct field, icmps of null, and PHIs. 1048 /// that index through the array and struct field, icmps of null, and PHIs.
1045 static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V, 1049 static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V,
1046 SmallPtrSet<const PHINode*, 32> &LoadUsingPHIs, 1050 SmallPtrSetImpl<const PHINode*> &LoadUsingPHIs,
1047 SmallPtrSet<const PHINode*, 32> &LoadUsingPHIsPerLoad) { 1051 SmallPtrSetImpl<const PHINode*> &LoadUsingPHIsPerLoad) {
1048 // We permit two users of the load: setcc comparing against the null 1052 // We permit two users of the load: setcc comparing against the null
1049 // pointer, and a getelementptr of a specific form. 1053 // pointer, and a getelementptr of a specific form.
1050 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; 1054 for (const User *U : V->users()) {
1051 ++UI) { 1055 const Instruction *UI = cast<Instruction>(U);
1052 const Instruction *User = cast<Instruction>(*UI);
1053 1056
1054 // Comparison against null is ok. 1057 // Comparison against null is ok.
1055 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(User)) { 1058 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UI)) {
1056 if (!isa<ConstantPointerNull>(ICI->getOperand(1))) 1059 if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
1057 return false; 1060 return false;
1058 continue; 1061 continue;
1059 } 1062 }
1060 1063
1061 // getelementptr is also ok, but only a simple form. 1064 // getelementptr is also ok, but only a simple form.
1062 if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { 1065 if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(UI)) {
1063 // Must index into the array and into the struct. 1066 // Must index into the array and into the struct.
1064 if (GEPI->getNumOperands() < 3) 1067 if (GEPI->getNumOperands() < 3)
1065 return false; 1068 return false;
1066 1069
1067 // Otherwise the GEP is ok. 1070 // Otherwise the GEP is ok.
1068 continue; 1071 continue;
1069 } 1072 }
1070 1073
1071 if (const PHINode *PN = dyn_cast<PHINode>(User)) { 1074 if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
1072 if (!LoadUsingPHIsPerLoad.insert(PN)) 1075 if (!LoadUsingPHIsPerLoad.insert(PN))
1073 // This means some phi nodes are dependent on each other. 1076 // This means some phi nodes are dependent on each other.
1074 // Avoid infinite looping! 1077 // Avoid infinite looping!
1075 return false; 1078 return false;
1076 if (!LoadUsingPHIs.insert(PN)) 1079 if (!LoadUsingPHIs.insert(PN))
1097 /// GV are simple enough to perform HeapSRA, return true. 1100 /// GV are simple enough to perform HeapSRA, return true.
1098 static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV, 1101 static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV,
1099 Instruction *StoredVal) { 1102 Instruction *StoredVal) {
1100 SmallPtrSet<const PHINode*, 32> LoadUsingPHIs; 1103 SmallPtrSet<const PHINode*, 32> LoadUsingPHIs;
1101 SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad; 1104 SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad;
1102 for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end(); 1105 for (const User *U : GV->users())
1103 UI != E; ++UI) 1106 if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
1104 if (const LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
1105 if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs, 1107 if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs,
1106 LoadUsingPHIsPerLoad)) 1108 LoadUsingPHIsPerLoad))
1107 return false; 1109 return false;
1108 LoadUsingPHIsPerLoad.clear(); 1110 LoadUsingPHIsPerLoad.clear();
1109 } 1111 }
1111 // If we reach here, we know that all uses of the loads and transitive uses 1113 // If we reach here, we know that all uses of the loads and transitive uses
1112 // (through PHI nodes) are simple enough to transform. However, we don't know 1114 // (through PHI nodes) are simple enough to transform. However, we don't know
1113 // that all inputs the to the PHI nodes are in the same equivalence sets. 1115 // that all inputs the to the PHI nodes are in the same equivalence sets.
1114 // Check to verify that all operands of the PHIs are either PHIS that can be 1116 // Check to verify that all operands of the PHIs are either PHIS that can be
1115 // transformed, loads from GV, or MI itself. 1117 // transformed, loads from GV, or MI itself.
1116 for (SmallPtrSet<const PHINode*, 32>::const_iterator I = LoadUsingPHIs.begin() 1118 for (const PHINode *PN : LoadUsingPHIs) {
1117 , E = LoadUsingPHIs.end(); I != E; ++I) {
1118 const PHINode *PN = *I;
1119 for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) { 1119 for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) {
1120 Value *InVal = PN->getIncomingValue(op); 1120 Value *InVal = PN->getIncomingValue(op);
1121 1121
1122 // PHI of the stored value itself is ok. 1122 // PHI of the stored value itself is ok.
1123 if (InVal == StoredVal) continue; 1123 if (InVal == StoredVal) continue;
1167 PHIsToRewrite), 1167 PHIsToRewrite),
1168 LI->getName()+".f"+Twine(FieldNo), LI); 1168 LI->getName()+".f"+Twine(FieldNo), LI);
1169 } else if (PHINode *PN = dyn_cast<PHINode>(V)) { 1169 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
1170 // PN's type is pointer to struct. Make a new PHI of pointer to struct 1170 // PN's type is pointer to struct. Make a new PHI of pointer to struct
1171 // field. 1171 // field.
1172 StructType *ST = cast<StructType>(PN->getType()->getPointerElementType()); 1172
1173 1173 PointerType *PTy = cast<PointerType>(PN->getType());
1174 StructType *ST = cast<StructType>(PTy->getElementType());
1175
1176 unsigned AS = PTy->getAddressSpace();
1174 PHINode *NewPN = 1177 PHINode *NewPN =
1175 PHINode::Create(PointerType::getUnqual(ST->getElementType(FieldNo)), 1178 PHINode::Create(PointerType::get(ST->getElementType(FieldNo), AS),
1176 PN->getNumIncomingValues(), 1179 PN->getNumIncomingValues(),
1177 PN->getName()+".f"+Twine(FieldNo), PN); 1180 PN->getName()+".f"+Twine(FieldNo), PN);
1178 Result = NewPN; 1181 Result = NewPN;
1179 PHIsToRewrite.push_back(std::make_pair(PN, FieldNo)); 1182 PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
1180 } else { 1183 } else {
1238 std::vector<Value*>())).second) 1241 std::vector<Value*>())).second)
1239 return; 1242 return;
1240 1243
1241 // If this is the first time we've seen this PHI, recursively process all 1244 // If this is the first time we've seen this PHI, recursively process all
1242 // users. 1245 // users.
1243 for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; ) { 1246 for (auto UI = PN->user_begin(), E = PN->user_end(); UI != E;) {
1244 Instruction *User = cast<Instruction>(*UI++); 1247 Instruction *User = cast<Instruction>(*UI++);
1245 RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite); 1248 RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
1246 } 1249 }
1247 } 1250 }
1248 1251
1251 /// use FieldGlobals instead. All uses of loaded values satisfy 1254 /// use FieldGlobals instead. All uses of loaded values satisfy
1252 /// AllGlobalLoadUsesSimpleEnoughForHeapSRA. 1255 /// AllGlobalLoadUsesSimpleEnoughForHeapSRA.
1253 static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load, 1256 static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
1254 DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, 1257 DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1255 std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) { 1258 std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1256 for (Value::use_iterator UI = Load->use_begin(), E = Load->use_end(); 1259 for (auto UI = Load->user_begin(), E = Load->user_end(); UI != E;) {
1257 UI != E; ) {
1258 Instruction *User = cast<Instruction>(*UI++); 1260 Instruction *User = cast<Instruction>(*UI++);
1259 RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite); 1261 RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
1260 } 1262 }
1261 1263
1262 if (Load->use_empty()) { 1264 if (Load->use_empty()) {
1266 } 1268 }
1267 1269
1268 /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break 1270 /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break
1269 /// it up into multiple allocations of arrays of the fields. 1271 /// it up into multiple allocations of arrays of the fields.
1270 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI, 1272 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI,
1271 Value *NElems, DataLayout *TD, 1273 Value *NElems, const DataLayout *DL,
1272 const TargetLibraryInfo *TLI) { 1274 const TargetLibraryInfo *TLI) {
1273 DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n'); 1275 DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n');
1274 Type *MAT = getMallocAllocatedType(CI, TLI); 1276 Type *MAT = getMallocAllocatedType(CI, TLI);
1275 StructType *STy = cast<StructType>(MAT); 1277 StructType *STy = cast<StructType>(MAT);
1276 1278
1283 // Okay, at this point, there are no users of the malloc. Insert N 1285 // Okay, at this point, there are no users of the malloc. Insert N
1284 // new mallocs at the same place as CI, and N globals. 1286 // new mallocs at the same place as CI, and N globals.
1285 std::vector<Value*> FieldGlobals; 1287 std::vector<Value*> FieldGlobals;
1286 std::vector<Value*> FieldMallocs; 1288 std::vector<Value*> FieldMallocs;
1287 1289
1290 unsigned AS = GV->getType()->getPointerAddressSpace();
1288 for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){ 1291 for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
1289 Type *FieldTy = STy->getElementType(FieldNo); 1292 Type *FieldTy = STy->getElementType(FieldNo);
1290 PointerType *PFieldTy = PointerType::getUnqual(FieldTy); 1293 PointerType *PFieldTy = PointerType::get(FieldTy, AS);
1291 1294
1292 GlobalVariable *NGV = 1295 GlobalVariable *NGV =
1293 new GlobalVariable(*GV->getParent(), 1296 new GlobalVariable(*GV->getParent(),
1294 PFieldTy, false, GlobalValue::InternalLinkage, 1297 PFieldTy, false, GlobalValue::InternalLinkage,
1295 Constant::getNullValue(PFieldTy), 1298 Constant::getNullValue(PFieldTy),
1296 GV->getName() + ".f" + Twine(FieldNo), GV, 1299 GV->getName() + ".f" + Twine(FieldNo), GV,
1297 GV->getThreadLocalMode()); 1300 GV->getThreadLocalMode());
1298 FieldGlobals.push_back(NGV); 1301 FieldGlobals.push_back(NGV);
1299 1302
1300 unsigned TypeSize = TD->getTypeAllocSize(FieldTy); 1303 unsigned TypeSize = DL->getTypeAllocSize(FieldTy);
1301 if (StructType *ST = dyn_cast<StructType>(FieldTy)) 1304 if (StructType *ST = dyn_cast<StructType>(FieldTy))
1302 TypeSize = TD->getStructLayout(ST)->getSizeInBytes(); 1305 TypeSize = DL->getStructLayout(ST)->getSizeInBytes();
1303 Type *IntPtrTy = TD->getIntPtrType(CI->getType()); 1306 Type *IntPtrTy = DL->getIntPtrType(CI->getType());
1304 Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy, 1307 Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
1305 ConstantInt::get(IntPtrTy, TypeSize), 1308 ConstantInt::get(IntPtrTy, TypeSize),
1306 NElems, 0, 1309 NElems, nullptr,
1307 CI->getName() + ".f" + Twine(FieldNo)); 1310 CI->getName() + ".f" + Twine(FieldNo));
1308 FieldMallocs.push_back(NMI); 1311 FieldMallocs.push_back(NMI);
1309 new StoreInst(NMI, NGV, CI); 1312 new StoreInst(NMI, NGV, CI);
1310 } 1313 }
1311 1314
1383 std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite; 1386 std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite;
1384 1387
1385 // Okay, the malloc site is completely handled. All of the uses of GV are now 1388 // Okay, the malloc site is completely handled. All of the uses of GV are now
1386 // loads, and all uses of those loads are simple. Rewrite them to use loads 1389 // loads, and all uses of those loads are simple. Rewrite them to use loads
1387 // of the per-field globals instead. 1390 // of the per-field globals instead.
1388 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;) { 1391 for (auto UI = GV->user_begin(), E = GV->user_end(); UI != E;) {
1389 Instruction *User = cast<Instruction>(*UI++); 1392 Instruction *User = cast<Instruction>(*UI++);
1390 1393
1391 if (LoadInst *LI = dyn_cast<LoadInst>(User)) { 1394 if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
1392 RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite); 1395 RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite);
1393 continue; 1396 continue;
1458 static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV, 1461 static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
1459 CallInst *CI, 1462 CallInst *CI,
1460 Type *AllocTy, 1463 Type *AllocTy,
1461 AtomicOrdering Ordering, 1464 AtomicOrdering Ordering,
1462 Module::global_iterator &GVI, 1465 Module::global_iterator &GVI,
1463 DataLayout *TD, 1466 const DataLayout *DL,
1464 TargetLibraryInfo *TLI) { 1467 TargetLibraryInfo *TLI) {
1465 if (!TD) 1468 if (!DL)
1466 return false; 1469 return false;
1467 1470
1468 // If this is a malloc of an abstract type, don't touch it. 1471 // If this is a malloc of an abstract type, don't touch it.
1469 if (!AllocTy->isSized()) 1472 if (!AllocTy->isSized())
1470 return false; 1473 return false;
1490 // If we have a global that is only initialized with a fixed size malloc, 1493 // If we have a global that is only initialized with a fixed size malloc,
1491 // transform the program to use global memory instead of malloc'd memory. 1494 // transform the program to use global memory instead of malloc'd memory.
1492 // This eliminates dynamic allocation, avoids an indirection accessing the 1495 // This eliminates dynamic allocation, avoids an indirection accessing the
1493 // data, and exposes the resultant global to further GlobalOpt. 1496 // data, and exposes the resultant global to further GlobalOpt.
1494 // We cannot optimize the malloc if we cannot determine malloc array size. 1497 // We cannot optimize the malloc if we cannot determine malloc array size.
1495 Value *NElems = getMallocArraySize(CI, TD, TLI, true); 1498 Value *NElems = getMallocArraySize(CI, DL, TLI, true);
1496 if (!NElems) 1499 if (!NElems)
1497 return false; 1500 return false;
1498 1501
1499 if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems)) 1502 if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
1500 // Restrict this transformation to only working on small allocations 1503 // Restrict this transformation to only working on small allocations
1501 // (2048 bytes currently), as we don't want to introduce a 16M global or 1504 // (2048 bytes currently), as we don't want to introduce a 16M global or
1502 // something. 1505 // something.
1503 if (NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) { 1506 if (NElements->getZExtValue() * DL->getTypeAllocSize(AllocTy) < 2048) {
1504 GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, TD, TLI); 1507 GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, DL, TLI);
1505 return true; 1508 return true;
1506 } 1509 }
1507 1510
1508 // If the allocation is an array of structures, consider transforming this 1511 // If the allocation is an array of structures, consider transforming this
1509 // into multiple malloc'd arrays, one for each field. This is basically 1512 // into multiple malloc'd arrays, one for each field. This is basically
1528 AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) { 1531 AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) {
1529 1532
1530 // If this is a fixed size array, transform the Malloc to be an alloc of 1533 // If this is a fixed size array, transform the Malloc to be an alloc of
1531 // structs. malloc [100 x struct],1 -> malloc struct, 100 1534 // structs. malloc [100 x struct],1 -> malloc struct, 100
1532 if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) { 1535 if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) {
1533 Type *IntPtrTy = TD->getIntPtrType(CI->getType()); 1536 Type *IntPtrTy = DL->getIntPtrType(CI->getType());
1534 unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes(); 1537 unsigned TypeSize = DL->getStructLayout(AllocSTy)->getSizeInBytes();
1535 Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize); 1538 Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
1536 Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements()); 1539 Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
1537 Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy, 1540 Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy,
1538 AllocSize, NumElements, 1541 AllocSize, NumElements,
1539 0, CI->getName()); 1542 nullptr, CI->getName());
1540 Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI); 1543 Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
1541 CI->replaceAllUsesWith(Cast); 1544 CI->replaceAllUsesWith(Cast);
1542 CI->eraseFromParent(); 1545 CI->eraseFromParent();
1543 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc)) 1546 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc))
1544 CI = cast<CallInst>(BCI->getOperand(0)); 1547 CI = cast<CallInst>(BCI->getOperand(0));
1545 else 1548 else
1546 CI = cast<CallInst>(Malloc); 1549 CI = cast<CallInst>(Malloc);
1547 } 1550 }
1548 1551
1549 GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, TD, TLI, true), 1552 GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, DL, TLI, true),
1550 TD, TLI); 1553 DL, TLI);
1551 return true; 1554 return true;
1552 } 1555 }
1553 1556
1554 return false; 1557 return false;
1555 } 1558 }
1557 // OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge 1560 // OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
1558 // that only one value (besides its initializer) is ever stored to the global. 1561 // that only one value (besides its initializer) is ever stored to the global.
1559 static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, 1562 static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
1560 AtomicOrdering Ordering, 1563 AtomicOrdering Ordering,
1561 Module::global_iterator &GVI, 1564 Module::global_iterator &GVI,
1562 DataLayout *TD, TargetLibraryInfo *TLI) { 1565 const DataLayout *DL,
1566 TargetLibraryInfo *TLI) {
1563 // Ignore no-op GEPs and bitcasts. 1567 // Ignore no-op GEPs and bitcasts.
1564 StoredOnceVal = StoredOnceVal->stripPointerCasts(); 1568 StoredOnceVal = StoredOnceVal->stripPointerCasts();
1565 1569
1566 // If we are dealing with a pointer global that is initialized to null and 1570 // If we are dealing with a pointer global that is initialized to null and
1567 // only has one (non-null) value stored into it, then we can optimize any 1571 // only has one (non-null) value stored into it, then we can optimize any
1572 if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) { 1576 if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
1573 if (GV->getInitializer()->getType() != SOVC->getType()) 1577 if (GV->getInitializer()->getType() != SOVC->getType())
1574 SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType()); 1578 SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
1575 1579
1576 // Optimize away any trapping uses of the loaded value. 1580 // Optimize away any trapping uses of the loaded value.
1577 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, TD, TLI)) 1581 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, TLI))
1578 return true; 1582 return true;
1579 } else if (CallInst *CI = extractMallocCall(StoredOnceVal, TLI)) { 1583 } else if (CallInst *CI = extractMallocCall(StoredOnceVal, TLI)) {
1580 Type *MallocType = getMallocAllocatedType(CI, TLI); 1584 Type *MallocType = getMallocAllocatedType(CI, TLI);
1581 if (MallocType && 1585 if (MallocType &&
1582 TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, Ordering, GVI, 1586 TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, Ordering, GVI,
1583 TD, TLI)) 1587 DL, TLI))
1584 return true; 1588 return true;
1585 } 1589 }
1586 } 1590 }
1587 1591
1588 return false; 1592 return false;
1605 GVElType->isPointerTy() || GVElType->isVectorTy()) 1609 GVElType->isPointerTy() || GVElType->isVectorTy())
1606 return false; 1610 return false;
1607 1611
1608 // Walk the use list of the global seeing if all the uses are load or store. 1612 // Walk the use list of the global seeing if all the uses are load or store.
1609 // If there is anything else, bail out. 1613 // If there is anything else, bail out.
1610 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){ 1614 for (User *U : GV->users())
1611 User *U = *I;
1612 if (!isa<LoadInst>(U) && !isa<StoreInst>(U)) 1615 if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
1613 return false; 1616 return false;
1614 }
1615 1617
1616 DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV); 1618 DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV);
1617 1619
1618 // Create the new global, initializing it to false. 1620 // Create the new global, initializing it to false.
1619 GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()), 1621 GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),
1634 bool IsOneZero = false; 1636 bool IsOneZero = false;
1635 if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) 1637 if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
1636 IsOneZero = InitVal->isNullValue() && CI->isOne(); 1638 IsOneZero = InitVal->isNullValue() && CI->isOne();
1637 1639
1638 while (!GV->use_empty()) { 1640 while (!GV->use_empty()) {
1639 Instruction *UI = cast<Instruction>(GV->use_back()); 1641 Instruction *UI = cast<Instruction>(GV->user_back());
1640 if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { 1642 if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
1641 // Change the store into a boolean store. 1643 // Change the store into a boolean store.
1642 bool StoringOther = SI->getOperand(0) == OtherVal; 1644 bool StoringOther = SI->getOperand(0) == OtherVal;
1643 // Only do this if we weren't storing a loaded value. 1645 // Only do this if we weren't storing a loaded value.
1644 Value *StoreVal; 1646 Value *StoreVal;
1694 1696
1695 /// ProcessGlobal - Analyze the specified global variable and optimize it if 1697 /// ProcessGlobal - Analyze the specified global variable and optimize it if
1696 /// possible. If we make a change, return true. 1698 /// possible. If we make a change, return true.
1697 bool GlobalOpt::ProcessGlobal(GlobalVariable *GV, 1699 bool GlobalOpt::ProcessGlobal(GlobalVariable *GV,
1698 Module::global_iterator &GVI) { 1700 Module::global_iterator &GVI) {
1699 if (!GV->isDiscardableIfUnused())
1700 return false;
1701
1702 // Do more involved optimizations if the global is internal. 1701 // Do more involved optimizations if the global is internal.
1703 GV->removeDeadConstantUsers(); 1702 GV->removeDeadConstantUsers();
1704 1703
1705 if (GV->use_empty()) { 1704 if (GV->use_empty()) {
1706 DEBUG(dbgs() << "GLOBAL DEAD: " << *GV); 1705 DEBUG(dbgs() << "GLOBAL DEAD: " << *GV);
1750 DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV); 1749 DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV);
1751 Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction 1750 Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
1752 ->getEntryBlock().begin()); 1751 ->getEntryBlock().begin());
1753 Type *ElemTy = GV->getType()->getElementType(); 1752 Type *ElemTy = GV->getType()->getElementType();
1754 // FIXME: Pass Global's alignment when globals have alignment 1753 // FIXME: Pass Global's alignment when globals have alignment
1755 AllocaInst *Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), &FirstI); 1754 AllocaInst *Alloca = new AllocaInst(ElemTy, nullptr,
1755 GV->getName(), &FirstI);
1756 if (!isa<UndefValue>(GV->getInitializer())) 1756 if (!isa<UndefValue>(GV->getInitializer()))
1757 new StoreInst(GV->getInitializer(), Alloca, &FirstI); 1757 new StoreInst(GV->getInitializer(), Alloca, &FirstI);
1758 1758
1759 GV->replaceAllUsesWith(Alloca); 1759 GV->replaceAllUsesWith(Alloca);
1760 GV->eraseFromParent(); 1760 GV->eraseFromParent();
1772 // Delete any constant stores to the global. 1772 // Delete any constant stores to the global.
1773 Changed = CleanupPointerRootUsers(GV, TLI); 1773 Changed = CleanupPointerRootUsers(GV, TLI);
1774 } else { 1774 } else {
1775 // Delete any stores we can find to the global. We may not be able to 1775 // Delete any stores we can find to the global. We may not be able to
1776 // make it completely dead though. 1776 // make it completely dead though.
1777 Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI); 1777 Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1778 } 1778 }
1779 1779
1780 // If the global is dead now, delete it. 1780 // If the global is dead now, delete it.
1781 if (GV->use_empty()) { 1781 if (GV->use_empty()) {
1782 GV->eraseFromParent(); 1782 GV->eraseFromParent();
1788 } else if (GS.StoredType <= GlobalStatus::InitializerStored) { 1788 } else if (GS.StoredType <= GlobalStatus::InitializerStored) {
1789 DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n"); 1789 DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
1790 GV->setConstant(true); 1790 GV->setConstant(true);
1791 1791
1792 // Clean up any obviously simplifiable users now. 1792 // Clean up any obviously simplifiable users now.
1793 CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI); 1793 CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1794 1794
1795 // If the global is dead now, just nuke it. 1795 // If the global is dead now, just nuke it.
1796 if (GV->use_empty()) { 1796 if (GV->use_empty()) {
1797 DEBUG(dbgs() << " *** Marking constant allowed us to simplify " 1797 DEBUG(dbgs() << " *** Marking constant allowed us to simplify "
1798 << "all users and delete global!\n"); 1798 << "all users and delete global!\n");
1801 } 1801 }
1802 1802
1803 ++NumMarked; 1803 ++NumMarked;
1804 return true; 1804 return true;
1805 } else if (!GV->getInitializer()->getType()->isSingleValueType()) { 1805 } else if (!GV->getInitializer()->getType()->isSingleValueType()) {
1806 if (DataLayout *TD = getAnalysisIfAvailable<DataLayout>()) 1806 if (DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>()) {
1807 if (GlobalVariable *FirstNewGV = SRAGlobal(GV, *TD)) { 1807 const DataLayout &DL = DLP->getDataLayout();
1808 if (GlobalVariable *FirstNewGV = SRAGlobal(GV, DL)) {
1808 GVI = FirstNewGV; // Don't skip the newly produced globals! 1809 GVI = FirstNewGV; // Don't skip the newly produced globals!
1809 return true; 1810 return true;
1810 } 1811 }
1812 }
1811 } else if (GS.StoredType == GlobalStatus::StoredOnce) { 1813 } else if (GS.StoredType == GlobalStatus::StoredOnce) {
1812 // If the initial value for the global was an undef value, and if only 1814 // If the initial value for the global was an undef value, and if only
1813 // one other value was stored into it, we can just change the 1815 // one other value was stored into it, we can just change the
1814 // initializer to be the stored value, then delete all stores to the 1816 // initializer to be the stored value, then delete all stores to the
1815 // global. This allows us to mark it constant. 1817 // global. This allows us to mark it constant.
1817 if (isa<UndefValue>(GV->getInitializer())) { 1819 if (isa<UndefValue>(GV->getInitializer())) {
1818 // Change the initial value here. 1820 // Change the initial value here.
1819 GV->setInitializer(SOVConstant); 1821 GV->setInitializer(SOVConstant);
1820 1822
1821 // Clean up any obviously simplifiable users now. 1823 // Clean up any obviously simplifiable users now.
1822 CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI); 1824 CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1823 1825
1824 if (GV->use_empty()) { 1826 if (GV->use_empty()) {
1825 DEBUG(dbgs() << " *** Substituting initializer allowed us to " 1827 DEBUG(dbgs() << " *** Substituting initializer allowed us to "
1826 << "simplify all users and delete global!\n"); 1828 << "simplify all users and delete global!\n");
1827 GV->eraseFromParent(); 1829 GV->eraseFromParent();
1834 } 1836 }
1835 1837
1836 // Try to optimize globals based on the knowledge that only one value 1838 // Try to optimize globals based on the knowledge that only one value
1837 // (besides its initializer) is ever stored to the global. 1839 // (besides its initializer) is ever stored to the global.
1838 if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, GVI, 1840 if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, GVI,
1839 TD, TLI)) 1841 DL, TLI))
1840 return true; 1842 return true;
1841 1843
1842 // Otherwise, if the global was not a boolean, we can shrink it to be a 1844 // Otherwise, if the global was not a boolean, we can shrink it to be a
1843 // boolean. 1845 // boolean.
1844 if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) { 1846 if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) {
1855 } 1857 }
1856 1858
1857 /// ChangeCalleesToFastCall - Walk all of the direct calls of the specified 1859 /// ChangeCalleesToFastCall - Walk all of the direct calls of the specified
1858 /// function, changing them to FastCC. 1860 /// function, changing them to FastCC.
1859 static void ChangeCalleesToFastCall(Function *F) { 1861 static void ChangeCalleesToFastCall(Function *F) {
1860 for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ 1862 for (User *U : F->users()) {
1861 if (isa<BlockAddress>(*UI)) 1863 if (isa<BlockAddress>(U))
1862 continue; 1864 continue;
1863 CallSite User(cast<Instruction>(*UI)); 1865 CallSite CS(cast<Instruction>(U));
1864 User.setCallingConv(CallingConv::Fast); 1866 CS.setCallingConv(CallingConv::Fast);
1865 } 1867 }
1866 } 1868 }
1867 1869
1868 static AttributeSet StripNest(LLVMContext &C, const AttributeSet &Attrs) { 1870 static AttributeSet StripNest(LLVMContext &C, const AttributeSet &Attrs) {
1869 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) { 1871 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
1878 return Attrs; 1880 return Attrs;
1879 } 1881 }
1880 1882
1881 static void RemoveNestAttribute(Function *F) { 1883 static void RemoveNestAttribute(Function *F) {
1882 F->setAttributes(StripNest(F->getContext(), F->getAttributes())); 1884 F->setAttributes(StripNest(F->getContext(), F->getAttributes()));
1883 for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ 1885 for (User *U : F->users()) {
1884 if (isa<BlockAddress>(*UI)) 1886 if (isa<BlockAddress>(U))
1885 continue; 1887 continue;
1886 CallSite User(cast<Instruction>(*UI)); 1888 CallSite CS(cast<Instruction>(U));
1887 User.setAttributes(StripNest(F->getContext(), User.getAttributes())); 1889 CS.setAttributes(StripNest(F->getContext(), CS.getAttributes()));
1888 } 1890 }
1891 }
1892
1893 /// Return true if this is a calling convention that we'd like to change. The
1894 /// idea here is that we don't want to mess with the convention if the user
1895 /// explicitly requested something with performance implications like coldcc,
1896 /// GHC, or anyregcc.
1897 static bool isProfitableToMakeFastCC(Function *F) {
1898 CallingConv::ID CC = F->getCallingConv();
1899 // FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc?
1900 return CC == CallingConv::C || CC == CallingConv::X86_ThisCall;
1889 } 1901 }
1890 1902
1891 bool GlobalOpt::OptimizeFunctions(Module &M) { 1903 bool GlobalOpt::OptimizeFunctions(Module &M) {
1892 bool Changed = false; 1904 bool Changed = false;
1893 // Optimize functions. 1905 // Optimize functions.
1894 for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) { 1906 for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
1895 Function *F = FI++; 1907 Function *F = FI++;
1896 // Functions without names cannot be referenced outside this module. 1908 // Functions without names cannot be referenced outside this module.
1897 if (!F->hasName() && !F->isDeclaration()) 1909 if (!F->hasName() && !F->isDeclaration() && !F->hasLocalLinkage())
1898 F->setLinkage(GlobalValue::InternalLinkage); 1910 F->setLinkage(GlobalValue::InternalLinkage);
1899 F->removeDeadConstantUsers(); 1911 F->removeDeadConstantUsers();
1900 if (F->isDefTriviallyDead()) { 1912 if (F->isDefTriviallyDead()) {
1901 F->eraseFromParent(); 1913 F->eraseFromParent();
1902 Changed = true; 1914 Changed = true;
1903 ++NumFnDeleted; 1915 ++NumFnDeleted;
1904 } else if (F->hasLocalLinkage()) { 1916 } else if (F->hasLocalLinkage()) {
1905 if (F->getCallingConv() == CallingConv::C && !F->isVarArg() && 1917 if (isProfitableToMakeFastCC(F) && !F->isVarArg() &&
1906 !F->hasAddressTaken()) { 1918 !F->hasAddressTaken()) {
1907 // If this function has C calling conventions, is not a varargs 1919 // If this function has a calling convention worth changing, is not a
1908 // function, and is only called directly, promote it to use the Fast 1920 // varargs function, and is only called directly, promote it to use the
1909 // calling convention. 1921 // Fast calling convention.
1910 F->setCallingConv(CallingConv::Fast); 1922 F->setCallingConv(CallingConv::Fast);
1911 ChangeCalleesToFastCall(F); 1923 ChangeCalleesToFastCall(F);
1912 ++NumFastCallFns; 1924 ++NumFastCallFns;
1913 Changed = true; 1925 Changed = true;
1914 } 1926 }
1926 return Changed; 1938 return Changed;
1927 } 1939 }
1928 1940
1929 bool GlobalOpt::OptimizeGlobalVars(Module &M) { 1941 bool GlobalOpt::OptimizeGlobalVars(Module &M) {
1930 bool Changed = false; 1942 bool Changed = false;
1943
1944 SmallSet<const Comdat *, 8> NotDiscardableComdats;
1945 for (const GlobalVariable &GV : M.globals())
1946 if (const Comdat *C = GV.getComdat())
1947 if (!GV.isDiscardableIfUnused())
1948 NotDiscardableComdats.insert(C);
1949
1931 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); 1950 for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1932 GVI != E; ) { 1951 GVI != E; ) {
1933 GlobalVariable *GV = GVI++; 1952 GlobalVariable *GV = GVI++;
1934 // Global variables without names cannot be referenced outside this module. 1953 // Global variables without names cannot be referenced outside this module.
1935 if (!GV->hasName() && !GV->isDeclaration()) 1954 if (!GV->hasName() && !GV->isDeclaration() && !GV->hasLocalLinkage())
1936 GV->setLinkage(GlobalValue::InternalLinkage); 1955 GV->setLinkage(GlobalValue::InternalLinkage);
1937 // Simplify the initializer. 1956 // Simplify the initializer.
1938 if (GV->hasInitializer()) 1957 if (GV->hasInitializer())
1939 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) { 1958 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) {
1940 Constant *New = ConstantFoldConstantExpression(CE, TD, TLI); 1959 Constant *New = ConstantFoldConstantExpression(CE, DL, TLI);
1941 if (New && New != CE) 1960 if (New && New != CE)
1942 GV->setInitializer(New); 1961 GV->setInitializer(New);
1943 } 1962 }
1944 1963
1945 Changed |= ProcessGlobal(GV, GVI); 1964 if (GV->isDiscardableIfUnused()) {
1965 if (const Comdat *C = GV->getComdat())
1966 if (NotDiscardableComdats.count(C))
1967 continue;
1968 Changed |= ProcessGlobal(GV, GVI);
1969 }
1946 } 1970 }
1947 return Changed; 1971 return Changed;
1948 } 1972 }
1949
1950 /// FindGlobalCtors - Find the llvm.global_ctors list, verifying that all
1951 /// initializers have an init priority of 65535.
1952 GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) {
1953 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
1954 if (GV == 0) return 0;
1955
1956 // Verify that the initializer is simple enough for us to handle. We are
1957 // only allowed to optimize the initializer if it is unique.
1958 if (!GV->hasUniqueInitializer()) return 0;
1959
1960 if (isa<ConstantAggregateZero>(GV->getInitializer()))
1961 return GV;
1962 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
1963
1964 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
1965 if (isa<ConstantAggregateZero>(*i))
1966 continue;
1967 ConstantStruct *CS = cast<ConstantStruct>(*i);
1968 if (isa<ConstantPointerNull>(CS->getOperand(1)))
1969 continue;
1970
1971 // Must have a function or null ptr.
1972 if (!isa<Function>(CS->getOperand(1)))
1973 return 0;
1974
1975 // Init priority must be standard.
1976 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
1977 if (CI->getZExtValue() != 65535)
1978 return 0;
1979 }
1980
1981 return GV;
1982 }
1983
1984 /// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand,
1985 /// return a list of the functions and null terminator as a vector.
1986 static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) {
1987 if (GV->getInitializer()->isNullValue())
1988 return std::vector<Function*>();
1989 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
1990 std::vector<Function*> Result;
1991 Result.reserve(CA->getNumOperands());
1992 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
1993 ConstantStruct *CS = cast<ConstantStruct>(*i);
1994 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
1995 }
1996 return Result;
1997 }
1998
1999 /// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the
2000 /// specified array, returning the new global to use.
2001 static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
2002 const std::vector<Function*> &Ctors) {
2003 // If we made a change, reassemble the initializer list.
2004 Constant *CSVals[2];
2005 CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()), 65535);
2006 CSVals[1] = 0;
2007
2008 StructType *StructTy =
2009 cast<StructType>(GCL->getType()->getElementType()->getArrayElementType());
2010
2011 // Create the new init list.
2012 std::vector<Constant*> CAList;
2013 for (unsigned i = 0, e = Ctors.size(); i != e; ++i) {
2014 if (Ctors[i]) {
2015 CSVals[1] = Ctors[i];
2016 } else {
2017 Type *FTy = FunctionType::get(Type::getVoidTy(GCL->getContext()),
2018 false);
2019 PointerType *PFTy = PointerType::getUnqual(FTy);
2020 CSVals[1] = Constant::getNullValue(PFTy);
2021 CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()),
2022 0x7fffffff);
2023 }
2024 CAList.push_back(ConstantStruct::get(StructTy, CSVals));
2025 }
2026
2027 // Create the array initializer.
2028 Constant *CA = ConstantArray::get(ArrayType::get(StructTy,
2029 CAList.size()), CAList);
2030
2031 // If we didn't change the number of elements, don't create a new GV.
2032 if (CA->getType() == GCL->getInitializer()->getType()) {
2033 GCL->setInitializer(CA);
2034 return GCL;
2035 }
2036
2037 // Create the new global and insert it next to the existing list.
2038 GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(),
2039 GCL->getLinkage(), CA, "",
2040 GCL->getThreadLocalMode());
2041 GCL->getParent()->getGlobalList().insert(GCL, NGV);
2042 NGV->takeName(GCL);
2043
2044 // Nuke the old list, replacing any uses with the new one.
2045 if (!GCL->use_empty()) {
2046 Constant *V = NGV;
2047 if (V->getType() != GCL->getType())
2048 V = ConstantExpr::getBitCast(V, GCL->getType());
2049 GCL->replaceAllUsesWith(V);
2050 }
2051 GCL->eraseFromParent();
2052
2053 if (Ctors.size())
2054 return NGV;
2055 else
2056 return 0;
2057 }
2058
2059 1973
2060 static inline bool 1974 static inline bool
2061 isSimpleEnoughValueToCommit(Constant *C, 1975 isSimpleEnoughValueToCommit(Constant *C,
2062 SmallPtrSet<Constant*, 8> &SimpleConstants, 1976 SmallPtrSetImpl<Constant*> &SimpleConstants,
2063 const DataLayout *TD); 1977 const DataLayout *DL);
2064 1978
2065 1979
2066 /// isSimpleEnoughValueToCommit - Return true if the specified constant can be 1980 /// isSimpleEnoughValueToCommit - Return true if the specified constant can be
2067 /// handled by the code generator. We don't want to generate something like: 1981 /// handled by the code generator. We don't want to generate something like:
2068 /// void *X = &X/42; 1982 /// void *X = &X/42;
2070 /// 1984 ///
2071 /// This function should be called if C was not found (but just got inserted) 1985 /// This function should be called if C was not found (but just got inserted)
2072 /// in SimpleConstants to avoid having to rescan the same constants all the 1986 /// in SimpleConstants to avoid having to rescan the same constants all the
2073 /// time. 1987 /// time.
2074 static bool isSimpleEnoughValueToCommitHelper(Constant *C, 1988 static bool isSimpleEnoughValueToCommitHelper(Constant *C,
2075 SmallPtrSet<Constant*, 8> &SimpleConstants, 1989 SmallPtrSetImpl<Constant*> &SimpleConstants,
2076 const DataLayout *TD) { 1990 const DataLayout *DL) {
2077 // Simple integer, undef, constant aggregate zero, global addresses, etc are 1991 // Simple global addresses are supported, do not allow dllimport or
2078 // all supported. 1992 // thread-local globals.
2079 if (C->getNumOperands() == 0 || isa<BlockAddress>(C) || 1993 if (auto *GV = dyn_cast<GlobalValue>(C))
2080 isa<GlobalValue>(C)) 1994 return !GV->hasDLLImportStorageClass() && !GV->isThreadLocal();
1995
1996 // Simple integer, undef, constant aggregate zero, etc are all supported.
1997 if (C->getNumOperands() == 0 || isa<BlockAddress>(C))
2081 return true; 1998 return true;
2082 1999
2083 // Aggregate values are safe if all their elements are. 2000 // Aggregate values are safe if all their elements are.
2084 if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) || 2001 if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
2085 isa<ConstantVector>(C)) { 2002 isa<ConstantVector>(C)) {
2086 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { 2003 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
2087 Constant *Op = cast<Constant>(C->getOperand(i)); 2004 Constant *Op = cast<Constant>(C->getOperand(i));
2088 if (!isSimpleEnoughValueToCommit(Op, SimpleConstants, TD)) 2005 if (!isSimpleEnoughValueToCommit(Op, SimpleConstants, DL))
2089 return false; 2006 return false;
2090 } 2007 }
2091 return true; 2008 return true;
2092 } 2009 }
2093 2010
2096 // across targets. 2013 // across targets.
2097 ConstantExpr *CE = cast<ConstantExpr>(C); 2014 ConstantExpr *CE = cast<ConstantExpr>(C);
2098 switch (CE->getOpcode()) { 2015 switch (CE->getOpcode()) {
2099 case Instruction::BitCast: 2016 case Instruction::BitCast:
2100 // Bitcast is fine if the casted value is fine. 2017 // Bitcast is fine if the casted value is fine.
2101 return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD); 2018 return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
2102 2019
2103 case Instruction::IntToPtr: 2020 case Instruction::IntToPtr:
2104 case Instruction::PtrToInt: 2021 case Instruction::PtrToInt:
2105 // int <=> ptr is fine if the int type is the same size as the 2022 // int <=> ptr is fine if the int type is the same size as the
2106 // pointer type. 2023 // pointer type.
2107 if (!TD || TD->getTypeSizeInBits(CE->getType()) != 2024 if (!DL || DL->getTypeSizeInBits(CE->getType()) !=
2108 TD->getTypeSizeInBits(CE->getOperand(0)->getType())) 2025 DL->getTypeSizeInBits(CE->getOperand(0)->getType()))
2109 return false; 2026 return false;
2110 return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD); 2027 return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
2111 2028
2112 // GEP is fine if it is simple + constant offset. 2029 // GEP is fine if it is simple + constant offset.
2113 case Instruction::GetElementPtr: 2030 case Instruction::GetElementPtr:
2114 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) 2031 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
2115 if (!isa<ConstantInt>(CE->getOperand(i))) 2032 if (!isa<ConstantInt>(CE->getOperand(i)))
2116 return false; 2033 return false;
2117 return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD); 2034 return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
2118 2035
2119 case Instruction::Add: 2036 case Instruction::Add:
2120 // We allow simple+cst. 2037 // We allow simple+cst.
2121 if (!isa<ConstantInt>(CE->getOperand(1))) 2038 if (!isa<ConstantInt>(CE->getOperand(1)))
2122 return false; 2039 return false;
2123 return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD); 2040 return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
2124 } 2041 }
2125 return false; 2042 return false;
2126 } 2043 }
2127 2044
2128 static inline bool 2045 static inline bool
2129 isSimpleEnoughValueToCommit(Constant *C, 2046 isSimpleEnoughValueToCommit(Constant *C,
2130 SmallPtrSet<Constant*, 8> &SimpleConstants, 2047 SmallPtrSetImpl<Constant*> &SimpleConstants,
2131 const DataLayout *TD) { 2048 const DataLayout *DL) {
2132 // If we already checked this constant, we win. 2049 // If we already checked this constant, we win.
2133 if (!SimpleConstants.insert(C)) return true; 2050 if (!SimpleConstants.insert(C)) return true;
2134 // Check the constant. 2051 // Check the constant.
2135 return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, TD); 2052 return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, DL);
2136 } 2053 }
2137 2054
2138 2055
2139 /// isSimpleEnoughPointerToCommit - Return true if this constant is simple 2056 /// isSimpleEnoughPointerToCommit - Return true if this constant is simple
2140 /// enough for us to understand. In particular, if it is a cast to anything 2057 /// enough for us to understand. In particular, if it is a cast to anything
2146 // want to worry about them partially overlapping other stores. 2063 // want to worry about them partially overlapping other stores.
2147 if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType()) 2064 if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType())
2148 return false; 2065 return false;
2149 2066
2150 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) 2067 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
2151 // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or 2068 // Do not allow weak/*_odr/linkonce linkage or external globals.
2152 // external globals.
2153 return GV->hasUniqueInitializer(); 2069 return GV->hasUniqueInitializer();
2154 2070
2155 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 2071 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2156 // Handle a constantexpr gep. 2072 // Handle a constantexpr gep.
2157 if (CE->getOpcode() == Instruction::GetElementPtr && 2073 if (CE->getOpcode() == Instruction::GetElementPtr &&
2162 // external globals. 2078 // external globals.
2163 if (!GV->hasUniqueInitializer()) 2079 if (!GV->hasUniqueInitializer())
2164 return false; 2080 return false;
2165 2081
2166 // The first index must be zero. 2082 // The first index must be zero.
2167 ConstantInt *CI = dyn_cast<ConstantInt>(*llvm::next(CE->op_begin())); 2083 ConstantInt *CI = dyn_cast<ConstantInt>(*std::next(CE->op_begin()));
2168 if (!CI || !CI->isZero()) return false; 2084 if (!CI || !CI->isZero()) return false;
2169 2085
2170 // The remaining indices must be compile-time known integers within the 2086 // The remaining indices must be compile-time known integers within the
2171 // notional bounds of the corresponding static array types. 2087 // notional bounds of the corresponding static array types.
2172 if (!CE->isGEPWithNoNotionalOverIndexing()) 2088 if (!CE->isGEPWithNoNotionalOverIndexing())
2257 /// representing each SSA instruction. Changes to global variables are stored 2173 /// representing each SSA instruction. Changes to global variables are stored
2258 /// in a mapping that can be iterated over after the evaluation is complete. 2174 /// in a mapping that can be iterated over after the evaluation is complete.
2259 /// Once an evaluation call fails, the evaluation object should not be reused. 2175 /// Once an evaluation call fails, the evaluation object should not be reused.
2260 class Evaluator { 2176 class Evaluator {
2261 public: 2177 public:
2262 Evaluator(const DataLayout *TD, const TargetLibraryInfo *TLI) 2178 Evaluator(const DataLayout *DL, const TargetLibraryInfo *TLI)
2263 : TD(TD), TLI(TLI) { 2179 : DL(DL), TLI(TLI) {
2264 ValueStack.push_back(new DenseMap<Value*, Constant*>); 2180 ValueStack.emplace_back();
2265 } 2181 }
2266 2182
2267 ~Evaluator() { 2183 ~Evaluator() {
2268 DeleteContainerPointers(ValueStack); 2184 for (auto &Tmp : AllocaTmps)
2269 while (!AllocaTmps.empty()) {
2270 GlobalVariable *Tmp = AllocaTmps.back();
2271 AllocaTmps.pop_back();
2272
2273 // If there are still users of the alloca, the program is doing something 2185 // If there are still users of the alloca, the program is doing something
2274 // silly, e.g. storing the address of the alloca somewhere and using it 2186 // silly, e.g. storing the address of the alloca somewhere and using it
2275 // later. Since this is undefined, we'll just make it be null. 2187 // later. Since this is undefined, we'll just make it be null.
2276 if (!Tmp->use_empty()) 2188 if (!Tmp->use_empty())
2277 Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType())); 2189 Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
2278 delete Tmp;
2279 }
2280 } 2190 }
2281 2191
2282 /// EvaluateFunction - Evaluate a call to function F, returning true if 2192 /// EvaluateFunction - Evaluate a call to function F, returning true if
2283 /// successful, false if we can't evaluate it. ActualArgs contains the formal 2193 /// successful, false if we can't evaluate it. ActualArgs contains the formal
2284 /// arguments for the function. 2194 /// arguments for the function.
2290 /// control flows into, or null upon return. 2200 /// control flows into, or null upon return.
2291 bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB); 2201 bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB);
2292 2202
2293 Constant *getVal(Value *V) { 2203 Constant *getVal(Value *V) {
2294 if (Constant *CV = dyn_cast<Constant>(V)) return CV; 2204 if (Constant *CV = dyn_cast<Constant>(V)) return CV;
2295 Constant *R = ValueStack.back()->lookup(V); 2205 Constant *R = ValueStack.back().lookup(V);
2296 assert(R && "Reference to an uncomputed value!"); 2206 assert(R && "Reference to an uncomputed value!");
2297 return R; 2207 return R;
2298 } 2208 }
2299 2209
2300 void setVal(Value *V, Constant *C) { 2210 void setVal(Value *V, Constant *C) {
2301 ValueStack.back()->operator[](V) = C; 2211 ValueStack.back()[V] = C;
2302 } 2212 }
2303 2213
2304 const DenseMap<Constant*, Constant*> &getMutatedMemory() const { 2214 const DenseMap<Constant*, Constant*> &getMutatedMemory() const {
2305 return MutatedMemory; 2215 return MutatedMemory;
2306 } 2216 }
2307 2217
2308 const SmallPtrSet<GlobalVariable*, 8> &getInvariants() const { 2218 const SmallPtrSetImpl<GlobalVariable*> &getInvariants() const {
2309 return Invariants; 2219 return Invariants;
2310 } 2220 }
2311 2221
2312 private: 2222 private:
2313 Constant *ComputeLoadResult(Constant *P); 2223 Constant *ComputeLoadResult(Constant *P);
2314 2224
2315 /// ValueStack - As we compute SSA register values, we store their contents 2225 /// ValueStack - As we compute SSA register values, we store their contents
2316 /// here. The back of the vector contains the current function and the stack 2226 /// here. The back of the deque contains the current function and the stack
2317 /// contains the values in the calling frames. 2227 /// contains the values in the calling frames.
2318 SmallVector<DenseMap<Value*, Constant*>*, 4> ValueStack; 2228 std::deque<DenseMap<Value*, Constant*>> ValueStack;
2319 2229
2320 /// CallStack - This is used to detect recursion. In pathological situations 2230 /// CallStack - This is used to detect recursion. In pathological situations
2321 /// we could hit exponential behavior, but at least there is nothing 2231 /// we could hit exponential behavior, but at least there is nothing
2322 /// unbounded. 2232 /// unbounded.
2323 SmallVector<Function*, 4> CallStack; 2233 SmallVector<Function*, 4> CallStack;
2328 DenseMap<Constant*, Constant*> MutatedMemory; 2238 DenseMap<Constant*, Constant*> MutatedMemory;
2329 2239
2330 /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable 2240 /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable
2331 /// to represent its body. This vector is needed so we can delete the 2241 /// to represent its body. This vector is needed so we can delete the
2332 /// temporary globals when we are done. 2242 /// temporary globals when we are done.
2333 SmallVector<GlobalVariable*, 32> AllocaTmps; 2243 SmallVector<std::unique_ptr<GlobalVariable>, 32> AllocaTmps;
2334 2244
2335 /// Invariants - These global variables have been marked invariant by the 2245 /// Invariants - These global variables have been marked invariant by the
2336 /// static constructor. 2246 /// static constructor.
2337 SmallPtrSet<GlobalVariable*, 8> Invariants; 2247 SmallPtrSet<GlobalVariable*, 8> Invariants;
2338 2248
2339 /// SimpleConstants - These are constants we have checked and know to be 2249 /// SimpleConstants - These are constants we have checked and know to be
2340 /// simple enough to live in a static initializer of a global. 2250 /// simple enough to live in a static initializer of a global.
2341 SmallPtrSet<Constant*, 8> SimpleConstants; 2251 SmallPtrSet<Constant*, 8> SimpleConstants;
2342 2252
2343 const DataLayout *TD; 2253 const DataLayout *DL;
2344 const TargetLibraryInfo *TLI; 2254 const TargetLibraryInfo *TLI;
2345 }; 2255 };
2346 2256
2347 } // anonymous namespace 2257 } // anonymous namespace
2348 2258
2357 2267
2358 // Access it. 2268 // Access it.
2359 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) { 2269 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
2360 if (GV->hasDefinitiveInitializer()) 2270 if (GV->hasDefinitiveInitializer())
2361 return GV->getInitializer(); 2271 return GV->getInitializer();
2362 return 0; 2272 return nullptr;
2363 } 2273 }
2364 2274
2365 // Handle a constantexpr getelementptr. 2275 // Handle a constantexpr getelementptr.
2366 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P)) 2276 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P))
2367 if (CE->getOpcode() == Instruction::GetElementPtr && 2277 if (CE->getOpcode() == Instruction::GetElementPtr &&
2369 GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); 2279 GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
2370 if (GV->hasDefinitiveInitializer()) 2280 if (GV->hasDefinitiveInitializer())
2371 return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE); 2281 return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
2372 } 2282 }
2373 2283
2374 return 0; // don't know how to evaluate. 2284 return nullptr; // don't know how to evaluate.
2375 } 2285 }
2376 2286
2377 /// EvaluateBlock - Evaluate all instructions in block BB, returning true if 2287 /// EvaluateBlock - Evaluate all instructions in block BB, returning true if
2378 /// successful, false if we can't evaluate it. NewBB returns the next BB that 2288 /// successful, false if we can't evaluate it. NewBB returns the next BB that
2379 /// control flows into, or null upon return. 2289 /// control flows into, or null upon return.
2380 bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, 2290 bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
2381 BasicBlock *&NextBB) { 2291 BasicBlock *&NextBB) {
2382 // This is the main evaluation loop. 2292 // This is the main evaluation loop.
2383 while (1) { 2293 while (1) {
2384 Constant *InstResult = 0; 2294 Constant *InstResult = nullptr;
2385 2295
2386 DEBUG(dbgs() << "Evaluating Instruction: " << *CurInst << "\n"); 2296 DEBUG(dbgs() << "Evaluating Instruction: " << *CurInst << "\n");
2387 2297
2388 if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) { 2298 if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
2389 if (!SI->isSimple()) { 2299 if (!SI->isSimple()) {
2391 return false; // no volatile/atomic accesses. 2301 return false; // no volatile/atomic accesses.
2392 } 2302 }
2393 Constant *Ptr = getVal(SI->getOperand(1)); 2303 Constant *Ptr = getVal(SI->getOperand(1));
2394 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { 2304 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
2395 DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr); 2305 DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr);
2396 Ptr = ConstantFoldConstantExpression(CE, TD, TLI); 2306 Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
2397 DEBUG(dbgs() << "; To: " << *Ptr << "\n"); 2307 DEBUG(dbgs() << "; To: " << *Ptr << "\n");
2398 } 2308 }
2399 if (!isSimpleEnoughPointerToCommit(Ptr)) { 2309 if (!isSimpleEnoughPointerToCommit(Ptr)) {
2400 // If this is too complex for us to commit, reject it. 2310 // If this is too complex for us to commit, reject it.
2401 DEBUG(dbgs() << "Pointer is too complex for us to evaluate store."); 2311 DEBUG(dbgs() << "Pointer is too complex for us to evaluate store.");
2404 2314
2405 Constant *Val = getVal(SI->getOperand(0)); 2315 Constant *Val = getVal(SI->getOperand(0));
2406 2316
2407 // If this might be too difficult for the backend to handle (e.g. the addr 2317 // If this might be too difficult for the backend to handle (e.g. the addr
2408 // of one global variable divided by another) then we can't commit it. 2318 // of one global variable divided by another) then we can't commit it.
2409 if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, TD)) { 2319 if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, DL)) {
2410 DEBUG(dbgs() << "Store value is too complex to evaluate store. " << *Val 2320 DEBUG(dbgs() << "Store value is too complex to evaluate store. " << *Val
2411 << "\n"); 2321 << "\n");
2412 return false; 2322 return false;
2413 } 2323 }
2414 2324
2436 Constant *IdxZero = ConstantInt::get(IdxTy, 0, false); 2346 Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
2437 Constant * const IdxList[] = {IdxZero, IdxZero}; 2347 Constant * const IdxList[] = {IdxZero, IdxZero};
2438 2348
2439 Ptr = ConstantExpr::getGetElementPtr(Ptr, IdxList); 2349 Ptr = ConstantExpr::getGetElementPtr(Ptr, IdxList);
2440 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) 2350 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
2441 Ptr = ConstantFoldConstantExpression(CE, TD, TLI); 2351 Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
2442 2352
2443 // If we can't improve the situation by introspecting NewTy, 2353 // If we can't improve the situation by introspecting NewTy,
2444 // we have to give up. 2354 // we have to give up.
2445 } else { 2355 } else {
2446 DEBUG(dbgs() << "Failed to bitcast constant ptr, can not " 2356 DEBUG(dbgs() << "Failed to bitcast constant ptr, can not "
2480 InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)), 2390 InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)),
2481 getVal(SI->getOperand(1)), 2391 getVal(SI->getOperand(1)),
2482 getVal(SI->getOperand(2))); 2392 getVal(SI->getOperand(2)));
2483 DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult 2393 DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult
2484 << "\n"); 2394 << "\n");
2395 } else if (auto *EVI = dyn_cast<ExtractValueInst>(CurInst)) {
2396 InstResult = ConstantExpr::getExtractValue(
2397 getVal(EVI->getAggregateOperand()), EVI->getIndices());
2398 DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: " << *InstResult
2399 << "\n");
2400 } else if (auto *IVI = dyn_cast<InsertValueInst>(CurInst)) {
2401 InstResult = ConstantExpr::getInsertValue(
2402 getVal(IVI->getAggregateOperand()),
2403 getVal(IVI->getInsertedValueOperand()), IVI->getIndices());
2404 DEBUG(dbgs() << "Found an InsertValueInst! Simplifying: " << *InstResult
2405 << "\n");
2485 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) { 2406 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
2486 Constant *P = getVal(GEP->getOperand(0)); 2407 Constant *P = getVal(GEP->getOperand(0));
2487 SmallVector<Constant*, 8> GEPOps; 2408 SmallVector<Constant*, 8> GEPOps;
2488 for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); 2409 for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
2489 i != e; ++i) 2410 i != e; ++i)
2500 return false; // no volatile/atomic accesses. 2421 return false; // no volatile/atomic accesses.
2501 } 2422 }
2502 2423
2503 Constant *Ptr = getVal(LI->getOperand(0)); 2424 Constant *Ptr = getVal(LI->getOperand(0));
2504 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) { 2425 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
2505 Ptr = ConstantFoldConstantExpression(CE, TD, TLI); 2426 Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
2506 DEBUG(dbgs() << "Found a constant pointer expression, constant " 2427 DEBUG(dbgs() << "Found a constant pointer expression, constant "
2507 "folding: " << *Ptr << "\n"); 2428 "folding: " << *Ptr << "\n");
2508 } 2429 }
2509 InstResult = ComputeLoadResult(Ptr); 2430 InstResult = ComputeLoadResult(Ptr);
2510 if (InstResult == 0) { 2431 if (!InstResult) {
2511 DEBUG(dbgs() << "Failed to compute load result. Can not evaluate load." 2432 DEBUG(dbgs() << "Failed to compute load result. Can not evaluate load."
2512 "\n"); 2433 "\n");
2513 return false; // Could not evaluate load. 2434 return false; // Could not evaluate load.
2514 } 2435 }
2515 2436
2518 if (AI->isArrayAllocation()) { 2439 if (AI->isArrayAllocation()) {
2519 DEBUG(dbgs() << "Found an array alloca. Can not evaluate.\n"); 2440 DEBUG(dbgs() << "Found an array alloca. Can not evaluate.\n");
2520 return false; // Cannot handle array allocs. 2441 return false; // Cannot handle array allocs.
2521 } 2442 }
2522 Type *Ty = AI->getType()->getElementType(); 2443 Type *Ty = AI->getType()->getElementType();
2523 AllocaTmps.push_back(new GlobalVariable(Ty, false, 2444 AllocaTmps.push_back(
2524 GlobalValue::InternalLinkage, 2445 make_unique<GlobalVariable>(Ty, false, GlobalValue::InternalLinkage,
2525 UndefValue::get(Ty), 2446 UndefValue::get(Ty), AI->getName()));
2526 AI->getName())); 2447 InstResult = AllocaTmps.back().get();
2527 InstResult = AllocaTmps.back();
2528 DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n"); 2448 DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n");
2529 } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) { 2449 } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
2530 CallSite CS(CurInst); 2450 CallSite CS(CurInst);
2531 2451
2532 // Debug info can safely be ignored here. 2452 // Debug info can safely be ignored here.
2577 ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0)); 2497 ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0));
2578 Value *PtrArg = getVal(II->getArgOperand(1)); 2498 Value *PtrArg = getVal(II->getArgOperand(1));
2579 Value *Ptr = PtrArg->stripPointerCasts(); 2499 Value *Ptr = PtrArg->stripPointerCasts();
2580 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) { 2500 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
2581 Type *ElemTy = cast<PointerType>(GV->getType())->getElementType(); 2501 Type *ElemTy = cast<PointerType>(GV->getType())->getElementType();
2582 if (TD && !Size->isAllOnesValue() && 2502 if (DL && !Size->isAllOnesValue() &&
2583 Size->getValue().getLimitedValue() >= 2503 Size->getValue().getLimitedValue() >=
2584 TD->getTypeStoreSize(ElemTy)) { 2504 DL->getTypeStoreSize(ElemTy)) {
2585 Invariants.insert(GV); 2505 Invariants.insert(GV);
2586 DEBUG(dbgs() << "Found a global var that is an invariant: " << *GV 2506 DEBUG(dbgs() << "Found a global var that is an invariant: " << *GV
2587 << "\n"); 2507 << "\n");
2588 } else { 2508 } else {
2589 DEBUG(dbgs() << "Found a global var, but can not treat it as an " 2509 DEBUG(dbgs() << "Found a global var, but can not treat it as an "
2624 if (Callee->getFunctionType()->isVarArg()) { 2544 if (Callee->getFunctionType()->isVarArg()) {
2625 DEBUG(dbgs() << "Can not constant fold vararg function call.\n"); 2545 DEBUG(dbgs() << "Can not constant fold vararg function call.\n");
2626 return false; 2546 return false;
2627 } 2547 }
2628 2548
2629 Constant *RetVal = 0; 2549 Constant *RetVal = nullptr;
2630 // Execute the call, if successful, use the return value. 2550 // Execute the call, if successful, use the return value.
2631 ValueStack.push_back(new DenseMap<Value*, Constant*>); 2551 ValueStack.emplace_back();
2632 if (!EvaluateFunction(Callee, RetVal, Formals)) { 2552 if (!EvaluateFunction(Callee, RetVal, Formals)) {
2633 DEBUG(dbgs() << "Failed to evaluate function.\n"); 2553 DEBUG(dbgs() << "Failed to evaluate function.\n");
2634 return false; 2554 return false;
2635 } 2555 }
2636 delete ValueStack.pop_back_val(); 2556 ValueStack.pop_back();
2637 InstResult = RetVal; 2557 InstResult = RetVal;
2638 2558
2639 if (InstResult != NULL) { 2559 if (InstResult) {
2640 DEBUG(dbgs() << "Successfully evaluated function. Result: " << 2560 DEBUG(dbgs() << "Successfully evaluated function. Result: " <<
2641 InstResult << "\n\n"); 2561 InstResult << "\n\n");
2642 } else { 2562 } else {
2643 DEBUG(dbgs() << "Successfully evaluated function. Result: 0\n\n"); 2563 DEBUG(dbgs() << "Successfully evaluated function. Result: 0\n\n");
2644 } 2564 }
2666 if (BlockAddress *BA = dyn_cast<BlockAddress>(Val)) 2586 if (BlockAddress *BA = dyn_cast<BlockAddress>(Val))
2667 NextBB = BA->getBasicBlock(); 2587 NextBB = BA->getBasicBlock();
2668 else 2588 else
2669 return false; // Cannot determine. 2589 return false; // Cannot determine.
2670 } else if (isa<ReturnInst>(CurInst)) { 2590 } else if (isa<ReturnInst>(CurInst)) {
2671 NextBB = 0; 2591 NextBB = nullptr;
2672 } else { 2592 } else {
2673 // invoke, unwind, resume, unreachable. 2593 // invoke, unwind, resume, unreachable.
2674 DEBUG(dbgs() << "Can not handle terminator."); 2594 DEBUG(dbgs() << "Can not handle terminator.");
2675 return false; // Cannot handle this terminator. 2595 return false; // Cannot handle this terminator.
2676 } 2596 }
2685 return false; 2605 return false;
2686 } 2606 }
2687 2607
2688 if (!CurInst->use_empty()) { 2608 if (!CurInst->use_empty()) {
2689 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InstResult)) 2609 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InstResult))
2690 InstResult = ConstantFoldConstantExpression(CE, TD, TLI); 2610 InstResult = ConstantFoldConstantExpression(CE, DL, TLI);
2691 2611
2692 setVal(CurInst, InstResult); 2612 setVal(CurInst, InstResult);
2693 } 2613 }
2694 2614
2695 // If we just processed an invoke, we finished evaluating the block. 2615 // If we just processed an invoke, we finished evaluating the block.
2731 BasicBlock *CurBB = F->begin(); 2651 BasicBlock *CurBB = F->begin();
2732 2652
2733 BasicBlock::iterator CurInst = CurBB->begin(); 2653 BasicBlock::iterator CurInst = CurBB->begin();
2734 2654
2735 while (1) { 2655 while (1) {
2736 BasicBlock *NextBB = 0; // Initialized to avoid compiler warnings. 2656 BasicBlock *NextBB = nullptr; // Initialized to avoid compiler warnings.
2737 DEBUG(dbgs() << "Trying to evaluate BB: " << *CurBB << "\n"); 2657 DEBUG(dbgs() << "Trying to evaluate BB: " << *CurBB << "\n");
2738 2658
2739 if (!EvaluateBlock(CurInst, NextBB)) 2659 if (!EvaluateBlock(CurInst, NextBB))
2740 return false; 2660 return false;
2741 2661
2742 if (NextBB == 0) { 2662 if (!NextBB) {
2743 // Successfully running until there's no next block means that we found 2663 // Successfully running until there's no next block means that we found
2744 // the return. Fill it the return value and pop the call stack. 2664 // the return. Fill it the return value and pop the call stack.
2745 ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator()); 2665 ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator());
2746 if (RI->getNumOperands()) 2666 if (RI->getNumOperands())
2747 RetVal = getVal(RI->getOperand(0)); 2667 RetVal = getVal(RI->getOperand(0));
2756 return false; // looped! 2676 return false; // looped!
2757 2677
2758 // Okay, we have never been in this block before. Check to see if there 2678 // Okay, we have never been in this block before. Check to see if there
2759 // are any PHI nodes. If so, evaluate them with information about where 2679 // are any PHI nodes. If so, evaluate them with information about where
2760 // we came from. 2680 // we came from.
2761 PHINode *PN = 0; 2681 PHINode *PN = nullptr;
2762 for (CurInst = NextBB->begin(); 2682 for (CurInst = NextBB->begin();
2763 (PN = dyn_cast<PHINode>(CurInst)); ++CurInst) 2683 (PN = dyn_cast<PHINode>(CurInst)); ++CurInst)
2764 setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB))); 2684 setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB)));
2765 2685
2766 // Advance to the next block. 2686 // Advance to the next block.
2768 } 2688 }
2769 } 2689 }
2770 2690
2771 /// EvaluateStaticConstructor - Evaluate static constructors in the function, if 2691 /// EvaluateStaticConstructor - Evaluate static constructors in the function, if
2772 /// we can. Return true if we can, false otherwise. 2692 /// we can. Return true if we can, false otherwise.
2773 static bool EvaluateStaticConstructor(Function *F, const DataLayout *TD, 2693 static bool EvaluateStaticConstructor(Function *F, const DataLayout *DL,
2774 const TargetLibraryInfo *TLI) { 2694 const TargetLibraryInfo *TLI) {
2775 // Call the function. 2695 // Call the function.
2776 Evaluator Eval(TD, TLI); 2696 Evaluator Eval(DL, TLI);
2777 Constant *RetValDummy; 2697 Constant *RetValDummy;
2778 bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy, 2698 bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
2779 SmallVector<Constant*, 0>()); 2699 SmallVector<Constant*, 0>());
2780 2700
2781 if (EvalSuccess) { 2701 if (EvalSuccess) {
2702 ++NumCtorsEvaluated;
2703
2782 // We succeeded at evaluation: commit the result. 2704 // We succeeded at evaluation: commit the result.
2783 DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '" 2705 DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
2784 << F->getName() << "' to " << Eval.getMutatedMemory().size() 2706 << F->getName() << "' to " << Eval.getMutatedMemory().size()
2785 << " stores.\n"); 2707 << " stores.\n");
2786 for (DenseMap<Constant*, Constant*>::const_iterator I = 2708 for (DenseMap<Constant*, Constant*>::const_iterator I =
2787 Eval.getMutatedMemory().begin(), E = Eval.getMutatedMemory().end(); 2709 Eval.getMutatedMemory().begin(), E = Eval.getMutatedMemory().end();
2788 I != E; ++I) 2710 I != E; ++I)
2789 CommitValueTo(I->second, I->first); 2711 CommitValueTo(I->second, I->first);
2790 for (SmallPtrSet<GlobalVariable*, 8>::const_iterator I = 2712 for (GlobalVariable *GV : Eval.getInvariants())
2791 Eval.getInvariants().begin(), E = Eval.getInvariants().end(); 2713 GV->setConstant(true);
2792 I != E; ++I)
2793 (*I)->setConstant(true);
2794 } 2714 }
2795 2715
2796 return EvalSuccess; 2716 return EvalSuccess;
2797 }
2798
2799 /// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible.
2800 /// Return true if anything changed.
2801 bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
2802 std::vector<Function*> Ctors = ParseGlobalCtors(GCL);
2803 bool MadeChange = false;
2804 if (Ctors.empty()) return false;
2805
2806 // Loop over global ctors, optimizing them when we can.
2807 for (unsigned i = 0; i != Ctors.size(); ++i) {
2808 Function *F = Ctors[i];
2809 // Found a null terminator in the middle of the list, prune off the rest of
2810 // the list.
2811 if (F == 0) {
2812 if (i != Ctors.size()-1) {
2813 Ctors.resize(i+1);
2814 MadeChange = true;
2815 }
2816 break;
2817 }
2818 DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
2819
2820 // We cannot simplify external ctor functions.
2821 if (F->empty()) continue;
2822
2823 // If we can evaluate the ctor at compile time, do.
2824 if (EvaluateStaticConstructor(F, TD, TLI)) {
2825 Ctors.erase(Ctors.begin()+i);
2826 MadeChange = true;
2827 --i;
2828 ++NumCtorsEvaluated;
2829 continue;
2830 }
2831 }
2832
2833 if (!MadeChange) return false;
2834
2835 GCL = InstallGlobalCtors(GCL, Ctors);
2836 return true;
2837 } 2717 }
2838 2718
2839 static int compareNames(Constant *const *A, Constant *const *B) { 2719 static int compareNames(Constant *const *A, Constant *const *B) {
2840 return (*A)->getName().compare((*B)->getName()); 2720 return (*A)->getName().compare((*B)->getName());
2841 } 2721 }
2842 2722
2843 static void setUsedInitializer(GlobalVariable &V, 2723 static void setUsedInitializer(GlobalVariable &V,
2844 SmallPtrSet<GlobalValue *, 8> Init) { 2724 const SmallPtrSet<GlobalValue *, 8> &Init) {
2845 if (Init.empty()) { 2725 if (Init.empty()) {
2846 V.eraseFromParent(); 2726 V.eraseFromParent();
2847 return; 2727 return;
2848 } 2728 }
2849 2729
2730 // Type of pointer to the array of pointers.
2731 PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0);
2732
2850 SmallVector<llvm::Constant *, 8> UsedArray; 2733 SmallVector<llvm::Constant *, 8> UsedArray;
2851 PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext()); 2734 for (GlobalValue *GV : Init) {
2852 2735 Constant *Cast
2853 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Init.begin(), E = Init.end(); 2736 = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy);
2854 I != E; ++I) {
2855 Constant *Cast = llvm::ConstantExpr::getBitCast(*I, Int8PtrTy);
2856 UsedArray.push_back(Cast); 2737 UsedArray.push_back(Cast);
2857 } 2738 }
2858 // Sort to get deterministic order. 2739 // Sort to get deterministic order.
2859 array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames); 2740 array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);
2860 ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size()); 2741 ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size());
2881 LLVMUsed(Module &M) { 2762 LLVMUsed(Module &M) {
2882 UsedV = collectUsedGlobalVariables(M, Used, false); 2763 UsedV = collectUsedGlobalVariables(M, Used, false);
2883 CompilerUsedV = collectUsedGlobalVariables(M, CompilerUsed, true); 2764 CompilerUsedV = collectUsedGlobalVariables(M, CompilerUsed, true);
2884 } 2765 }
2885 typedef SmallPtrSet<GlobalValue *, 8>::iterator iterator; 2766 typedef SmallPtrSet<GlobalValue *, 8>::iterator iterator;
2767 typedef iterator_range<iterator> used_iterator_range;
2886 iterator usedBegin() { return Used.begin(); } 2768 iterator usedBegin() { return Used.begin(); }
2887 iterator usedEnd() { return Used.end(); } 2769 iterator usedEnd() { return Used.end(); }
2770 used_iterator_range used() {
2771 return used_iterator_range(usedBegin(), usedEnd());
2772 }
2888 iterator compilerUsedBegin() { return CompilerUsed.begin(); } 2773 iterator compilerUsedBegin() { return CompilerUsed.begin(); }
2889 iterator compilerUsedEnd() { return CompilerUsed.end(); } 2774 iterator compilerUsedEnd() { return CompilerUsed.end(); }
2775 used_iterator_range compilerUsed() {
2776 return used_iterator_range(compilerUsedBegin(), compilerUsedEnd());
2777 }
2890 bool usedCount(GlobalValue *GV) const { return Used.count(GV); } 2778 bool usedCount(GlobalValue *GV) const { return Used.count(GV); }
2891 bool compilerUsedCount(GlobalValue *GV) const { 2779 bool compilerUsedCount(GlobalValue *GV) const {
2892 return CompilerUsed.count(GV); 2780 return CompilerUsed.count(GV);
2893 } 2781 }
2894 bool usedErase(GlobalValue *GV) { return Used.erase(GV); } 2782 bool usedErase(GlobalValue *GV) { return Used.erase(GV); }
2937 return true; 2825 return true;
2938 2826
2939 return U.usedCount(&GA) || U.compilerUsedCount(&GA); 2827 return U.usedCount(&GA) || U.compilerUsedCount(&GA);
2940 } 2828 }
2941 2829
2942 static bool hasUsesToReplace(GlobalAlias &GA, LLVMUsed &U, bool &RenameTarget) { 2830 static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
2831 bool &RenameTarget) {
2943 RenameTarget = false; 2832 RenameTarget = false;
2944 bool Ret = false; 2833 bool Ret = false;
2945 if (hasUseOtherThanLLVMUsed(GA, U)) 2834 if (hasUseOtherThanLLVMUsed(GA, U))
2946 Ret = true; 2835 Ret = true;
2947 2836
2972 2861
2973 bool GlobalOpt::OptimizeGlobalAliases(Module &M) { 2862 bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
2974 bool Changed = false; 2863 bool Changed = false;
2975 LLVMUsed Used(M); 2864 LLVMUsed Used(M);
2976 2865
2977 for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.usedBegin(), 2866 for (GlobalValue *GV : Used.used())
2978 E = Used.usedEnd(); 2867 Used.compilerUsedErase(GV);
2979 I != E; ++I)
2980 Used.compilerUsedErase(*I);
2981 2868
2982 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); 2869 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
2983 I != E;) { 2870 I != E;) {
2984 Module::alias_iterator J = I++; 2871 Module::alias_iterator J = I++;
2985 // Aliases without names cannot be referenced outside this module. 2872 // Aliases without names cannot be referenced outside this module.
2986 if (!J->hasName() && !J->isDeclaration()) 2873 if (!J->hasName() && !J->isDeclaration() && !J->hasLocalLinkage())
2987 J->setLinkage(GlobalValue::InternalLinkage); 2874 J->setLinkage(GlobalValue::InternalLinkage);
2988 // If the aliasee may change at link time, nothing can be done - bail out. 2875 // If the aliasee may change at link time, nothing can be done - bail out.
2989 if (J->mayBeOverridden()) 2876 if (J->mayBeOverridden())
2990 continue; 2877 continue;
2991 2878
2992 Constant *Aliasee = J->getAliasee(); 2879 Constant *Aliasee = J->getAliasee();
2993 GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts()); 2880 GlobalValue *Target = dyn_cast<GlobalValue>(Aliasee->stripPointerCasts());
2881 // We can't trivially replace the alias with the aliasee if the aliasee is
2882 // non-trivial in some way.
2883 // TODO: Try to handle non-zero GEPs of local aliasees.
2884 if (!Target)
2885 continue;
2994 Target->removeDeadConstantUsers(); 2886 Target->removeDeadConstantUsers();
2995 2887
2996 // Make all users of the alias use the aliasee instead. 2888 // Make all users of the alias use the aliasee instead.
2997 bool RenameTarget; 2889 bool RenameTarget;
2998 if (!hasUsesToReplace(*J, Used, RenameTarget)) 2890 if (!hasUsesToReplace(*J, Used, RenameTarget))
2999 continue; 2891 continue;
3000 2892
3001 J->replaceAllUsesWith(Aliasee); 2893 J->replaceAllUsesWith(ConstantExpr::getBitCast(Aliasee, J->getType()));
3002 ++NumAliasesResolved; 2894 ++NumAliasesResolved;
3003 Changed = true; 2895 Changed = true;
3004 2896
3005 if (RenameTarget) { 2897 if (RenameTarget) {
3006 // Give the aliasee the name, linkage and other attributes of the alias. 2898 // Give the aliasee the name, linkage and other attributes of the alias.
3007 Target->takeName(J); 2899 Target->takeName(J);
3008 Target->setLinkage(J->getLinkage()); 2900 Target->setLinkage(J->getLinkage());
3009 Target->GlobalValue::copyAttributesFrom(J); 2901 Target->setVisibility(J->getVisibility());
2902 Target->setDLLStorageClass(J->getDLLStorageClass());
3010 2903
3011 if (Used.usedErase(J)) 2904 if (Used.usedErase(J))
3012 Used.usedInsert(Target); 2905 Used.usedInsert(Target);
3013 2906
3014 if (Used.compilerUsedErase(J)) 2907 if (Used.compilerUsedErase(J))
3027 return Changed; 2920 return Changed;
3028 } 2921 }
3029 2922
3030 static Function *FindCXAAtExit(Module &M, TargetLibraryInfo *TLI) { 2923 static Function *FindCXAAtExit(Module &M, TargetLibraryInfo *TLI) {
3031 if (!TLI->has(LibFunc::cxa_atexit)) 2924 if (!TLI->has(LibFunc::cxa_atexit))
3032 return 0; 2925 return nullptr;
3033 2926
3034 Function *Fn = M.getFunction(TLI->getName(LibFunc::cxa_atexit)); 2927 Function *Fn = M.getFunction(TLI->getName(LibFunc::cxa_atexit));
3035 2928
3036 if (!Fn) 2929 if (!Fn)
3037 return 0; 2930 return nullptr;
3038 2931
3039 FunctionType *FTy = Fn->getFunctionType(); 2932 FunctionType *FTy = Fn->getFunctionType();
3040 2933
3041 // Checking that the function has the right return type, the right number of 2934 // Checking that the function has the right return type, the right number of
3042 // parameters and that they all have pointer types should be enough. 2935 // parameters and that they all have pointer types should be enough.
3043 if (!FTy->getReturnType()->isIntegerTy() || 2936 if (!FTy->getReturnType()->isIntegerTy() ||
3044 FTy->getNumParams() != 3 || 2937 FTy->getNumParams() != 3 ||
3045 !FTy->getParamType(0)->isPointerTy() || 2938 !FTy->getParamType(0)->isPointerTy() ||
3046 !FTy->getParamType(1)->isPointerTy() || 2939 !FTy->getParamType(1)->isPointerTy() ||
3047 !FTy->getParamType(2)->isPointerTy()) 2940 !FTy->getParamType(2)->isPointerTy())
3048 return 0; 2941 return nullptr;
3049 2942
3050 return Fn; 2943 return Fn;
3051 } 2944 }
3052 2945
3053 /// cxxDtorIsEmpty - Returns whether the given function is an empty C++ 2946 /// cxxDtorIsEmpty - Returns whether the given function is an empty C++
3111 3004
3112 // This pass will look for calls to __cxa_atexit where the function is trivial 3005 // This pass will look for calls to __cxa_atexit where the function is trivial
3113 // and remove them. 3006 // and remove them.
3114 bool Changed = false; 3007 bool Changed = false;
3115 3008
3116 for (Function::use_iterator I = CXAAtExitFn->use_begin(), 3009 for (auto I = CXAAtExitFn->user_begin(), E = CXAAtExitFn->user_end();
3117 E = CXAAtExitFn->use_end(); I != E;) { 3010 I != E;) {
3118 // We're only interested in calls. Theoretically, we could handle invoke 3011 // We're only interested in calls. Theoretically, we could handle invoke
3119 // instructions as well, but neither llvm-gcc nor clang generate invokes 3012 // instructions as well, but neither llvm-gcc nor clang generate invokes
3120 // to __cxa_atexit. 3013 // to __cxa_atexit.
3121 CallInst *CI = dyn_cast<CallInst>(*I++); 3014 CallInst *CI = dyn_cast<CallInst>(*I++);
3122 if (!CI) 3015 if (!CI)
3144 } 3037 }
3145 3038
3146 bool GlobalOpt::runOnModule(Module &M) { 3039 bool GlobalOpt::runOnModule(Module &M) {
3147 bool Changed = false; 3040 bool Changed = false;
3148 3041
3149 TD = getAnalysisIfAvailable<DataLayout>(); 3042 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
3043 DL = DLP ? &DLP->getDataLayout() : nullptr;
3150 TLI = &getAnalysis<TargetLibraryInfo>(); 3044 TLI = &getAnalysis<TargetLibraryInfo>();
3151
3152 // Try to find the llvm.globalctors list.
3153 GlobalVariable *GlobalCtors = FindGlobalCtors(M);
3154 3045
3155 bool LocalChange = true; 3046 bool LocalChange = true;
3156 while (LocalChange) { 3047 while (LocalChange) {
3157 LocalChange = false; 3048 LocalChange = false;
3158 3049
3159 // Delete functions that are trivially dead, ccc -> fastcc 3050 // Delete functions that are trivially dead, ccc -> fastcc
3160 LocalChange |= OptimizeFunctions(M); 3051 LocalChange |= OptimizeFunctions(M);
3161 3052
3162 // Optimize global_ctors list. 3053 // Optimize global_ctors list.
3163 if (GlobalCtors) 3054 LocalChange |= optimizeGlobalCtorsList(M, [&](Function *F) {
3164 LocalChange |= OptimizeGlobalCtorsList(GlobalCtors); 3055 return EvaluateStaticConstructor(F, DL, TLI);
3056 });
3165 3057
3166 // Optimize non-address-taken globals. 3058 // Optimize non-address-taken globals.
3167 LocalChange |= OptimizeGlobalVars(M); 3059 LocalChange |= OptimizeGlobalVars(M);
3168 3060
3169 // Resolve aliases, when possible. 3061 // Resolve aliases, when possible.