1 //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements sparse conditional constant propagation and merging: 11 // 12 // Specifically, this: 13 // * Assumes values are constant unless proven otherwise 14 // * Assumes BasicBlocks are dead unless proven otherwise 15 // * Proves values to be constant, and replaces them with constants 16 // * Proves conditional branches to be unconditional 17 // 18 //===----------------------------------------------------------------------===// 19 20 #include "llvm/Transforms/IPO/SCCP.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/DenseSet.h" 23 #include "llvm/ADT/PointerIntPair.h" 24 #include "llvm/ADT/SmallPtrSet.h" 25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/ADT/Statistic.h" 27 #include "llvm/Analysis/ConstantFolding.h" 28 #include "llvm/Analysis/GlobalsModRef.h" 29 #include "llvm/Analysis/TargetLibraryInfo.h" 30 #include "llvm/IR/CallSite.h" 31 #include "llvm/IR/Constants.h" 32 #include "llvm/IR/DataLayout.h" 33 #include "llvm/IR/DerivedTypes.h" 34 #include "llvm/IR/InstVisitor.h" 35 #include "llvm/IR/Instructions.h" 36 #include "llvm/Pass.h" 37 #include "llvm/Support/Debug.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include "llvm/Support/raw_ostream.h" 40 #include "llvm/Transforms/IPO.h" 41 #include "llvm/Transforms/Scalar.h" 42 #include "llvm/Transforms/Scalar/SCCP.h" 43 #include "llvm/Transforms/Utils/Local.h" 44 #include <algorithm> 45 using namespace llvm; 46 47 #define DEBUG_TYPE "sccp" 48 49 STATISTIC(NumInstRemoved, "Number of instructions removed"); 50 STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable"); 51 52 STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP"); 53 STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP"); 54 STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP"); 55 56 namespace { 57 /// LatticeVal class - This class represents the different lattice values that 58 /// an LLVM value may occupy. It is a simple class with value semantics. 59 /// 60 class LatticeVal { 61 enum LatticeValueTy { 62 /// unknown - This LLVM Value has no known value yet. 63 unknown, 64 65 /// constant - This LLVM Value has a specific constant value. 66 constant, 67 68 /// forcedconstant - This LLVM Value was thought to be undef until 69 /// ResolvedUndefsIn. This is treated just like 'constant', but if merged 70 /// with another (different) constant, it goes to overdefined, instead of 71 /// asserting. 72 forcedconstant, 73 74 /// overdefined - This instruction is not known to be constant, and we know 75 /// it has a value. 76 overdefined 77 }; 78 79 /// Val: This stores the current lattice value along with the Constant* for 80 /// the constant if this is a 'constant' or 'forcedconstant' value. 81 PointerIntPair<Constant *, 2, LatticeValueTy> Val; 82 83 LatticeValueTy getLatticeValue() const { 84 return Val.getInt(); 85 } 86 87 public: 88 LatticeVal() : Val(nullptr, unknown) {} 89 90 bool isUnknown() const { return getLatticeValue() == unknown; } 91 bool isConstant() const { 92 return getLatticeValue() == constant || getLatticeValue() == forcedconstant; 93 } 94 bool isOverdefined() const { return getLatticeValue() == overdefined; } 95 96 Constant *getConstant() const { 97 assert(isConstant() && "Cannot get the constant of a non-constant!"); 98 return Val.getPointer(); 99 } 100 101 /// markOverdefined - Return true if this is a change in status. 102 bool markOverdefined() { 103 if (isOverdefined()) 104 return false; 105 106 Val.setInt(overdefined); 107 return true; 108 } 109 110 /// markConstant - Return true if this is a change in status. 111 bool markConstant(Constant *V) { 112 if (getLatticeValue() == constant) { // Constant but not forcedconstant. 113 assert(getConstant() == V && "Marking constant with different value"); 114 return false; 115 } 116 117 if (isUnknown()) { 118 Val.setInt(constant); 119 assert(V && "Marking constant with NULL"); 120 Val.setPointer(V); 121 } else { 122 assert(getLatticeValue() == forcedconstant && 123 "Cannot move from overdefined to constant!"); 124 // Stay at forcedconstant if the constant is the same. 125 if (V == getConstant()) return false; 126 127 // Otherwise, we go to overdefined. Assumptions made based on the 128 // forced value are possibly wrong. Assuming this is another constant 129 // could expose a contradiction. 130 Val.setInt(overdefined); 131 } 132 return true; 133 } 134 135 /// getConstantInt - If this is a constant with a ConstantInt value, return it 136 /// otherwise return null. 137 ConstantInt *getConstantInt() const { 138 if (isConstant()) 139 return dyn_cast<ConstantInt>(getConstant()); 140 return nullptr; 141 } 142 143 void markForcedConstant(Constant *V) { 144 assert(isUnknown() && "Can't force a defined value!"); 145 Val.setInt(forcedconstant); 146 Val.setPointer(V); 147 } 148 }; 149 } // end anonymous namespace. 150 151 152 namespace { 153 154 //===----------------------------------------------------------------------===// 155 // 156 /// SCCPSolver - This class is a general purpose solver for Sparse Conditional 157 /// Constant Propagation. 158 /// 159 class SCCPSolver : public InstVisitor<SCCPSolver> { 160 const DataLayout &DL; 161 const TargetLibraryInfo *TLI; 162 SmallPtrSet<BasicBlock*, 8> BBExecutable; // The BBs that are executable. 163 DenseMap<Value*, LatticeVal> ValueState; // The state each value is in. 164 165 /// StructValueState - This maintains ValueState for values that have 166 /// StructType, for example for formal arguments, calls, insertelement, etc. 167 /// 168 DenseMap<std::pair<Value*, unsigned>, LatticeVal> StructValueState; 169 170 /// GlobalValue - If we are tracking any values for the contents of a global 171 /// variable, we keep a mapping from the constant accessor to the element of 172 /// the global, to the currently known value. If the value becomes 173 /// overdefined, it's entry is simply removed from this map. 174 DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals; 175 176 /// TrackedRetVals - If we are tracking arguments into and the return 177 /// value out of a function, it will have an entry in this map, indicating 178 /// what the known return value for the function is. 179 DenseMap<Function*, LatticeVal> TrackedRetVals; 180 181 /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions 182 /// that return multiple values. 183 DenseMap<std::pair<Function*, unsigned>, LatticeVal> TrackedMultipleRetVals; 184 185 /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is 186 /// represented here for efficient lookup. 187 SmallPtrSet<Function*, 16> MRVFunctionsTracked; 188 189 /// TrackingIncomingArguments - This is the set of functions for whose 190 /// arguments we make optimistic assumptions about and try to prove as 191 /// constants. 192 SmallPtrSet<Function*, 16> TrackingIncomingArguments; 193 194 /// The reason for two worklists is that overdefined is the lowest state 195 /// on the lattice, and moving things to overdefined as fast as possible 196 /// makes SCCP converge much faster. 197 /// 198 /// By having a separate worklist, we accomplish this because everything 199 /// possibly overdefined will become overdefined at the soonest possible 200 /// point. 201 SmallVector<Value*, 64> OverdefinedInstWorkList; 202 SmallVector<Value*, 64> InstWorkList; 203 204 205 SmallVector<BasicBlock*, 64> BBWorkList; // The BasicBlock work list 206 207 /// KnownFeasibleEdges - Entries in this set are edges which have already had 208 /// PHI nodes retriggered. 209 typedef std::pair<BasicBlock*, BasicBlock*> Edge; 210 DenseSet<Edge> KnownFeasibleEdges; 211 public: 212 SCCPSolver(const DataLayout &DL, const TargetLibraryInfo *tli) 213 : DL(DL), TLI(tli) {} 214 215 /// MarkBlockExecutable - This method can be used by clients to mark all of 216 /// the blocks that are known to be intrinsically live in the processed unit. 217 /// 218 /// This returns true if the block was not considered live before. 219 bool MarkBlockExecutable(BasicBlock *BB) { 220 if (!BBExecutable.insert(BB).second) 221 return false; 222 DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << '\n'); 223 BBWorkList.push_back(BB); // Add the block to the work list! 224 return true; 225 } 226 227 /// TrackValueOfGlobalVariable - Clients can use this method to 228 /// inform the SCCPSolver that it should track loads and stores to the 229 /// specified global variable if it can. This is only legal to call if 230 /// performing Interprocedural SCCP. 231 void TrackValueOfGlobalVariable(GlobalVariable *GV) { 232 // We only track the contents of scalar globals. 233 if (GV->getValueType()->isSingleValueType()) { 234 LatticeVal &IV = TrackedGlobals[GV]; 235 if (!isa<UndefValue>(GV->getInitializer())) 236 IV.markConstant(GV->getInitializer()); 237 } 238 } 239 240 /// AddTrackedFunction - If the SCCP solver is supposed to track calls into 241 /// and out of the specified function (which cannot have its address taken), 242 /// this method must be called. 243 void AddTrackedFunction(Function *F) { 244 // Add an entry, F -> undef. 245 if (auto *STy = dyn_cast<StructType>(F->getReturnType())) { 246 MRVFunctionsTracked.insert(F); 247 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) 248 TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i), 249 LatticeVal())); 250 } else 251 TrackedRetVals.insert(std::make_pair(F, LatticeVal())); 252 } 253 254 void AddArgumentTrackedFunction(Function *F) { 255 TrackingIncomingArguments.insert(F); 256 } 257 258 /// Solve - Solve for constants and executable blocks. 259 /// 260 void Solve(); 261 262 /// ResolvedUndefsIn - While solving the dataflow for a function, we assume 263 /// that branches on undef values cannot reach any of their successors. 264 /// However, this is not a safe assumption. After we solve dataflow, this 265 /// method should be use to handle this. If this returns true, the solver 266 /// should be rerun. 267 bool ResolvedUndefsIn(Function &F); 268 269 bool isBlockExecutable(BasicBlock *BB) const { 270 return BBExecutable.count(BB); 271 } 272 273 std::vector<LatticeVal> getStructLatticeValueFor(Value *V) const { 274 std::vector<LatticeVal> StructValues; 275 auto *STy = dyn_cast<StructType>(V->getType()); 276 assert(STy && "getStructLatticeValueFor() can be called only on structs"); 277 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 278 auto I = StructValueState.find(std::make_pair(V, i)); 279 assert(I != StructValueState.end() && "Value not in valuemap!"); 280 StructValues.push_back(I->second); 281 } 282 return StructValues; 283 } 284 285 LatticeVal getLatticeValueFor(Value *V) const { 286 DenseMap<Value*, LatticeVal>::const_iterator I = ValueState.find(V); 287 assert(I != ValueState.end() && "V is not in valuemap!"); 288 return I->second; 289 } 290 291 /// getTrackedRetVals - Get the inferred return value map. 292 /// 293 const DenseMap<Function*, LatticeVal> &getTrackedRetVals() { 294 return TrackedRetVals; 295 } 296 297 /// getTrackedGlobals - Get and return the set of inferred initializers for 298 /// global variables. 299 const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() { 300 return TrackedGlobals; 301 } 302 303 /// getMRVFunctionsTracked - Get the set of functions which return multiple 304 /// values tracked by the pass. 305 const SmallPtrSet<Function *, 16> getMRVFunctionsTracked() { 306 return MRVFunctionsTracked; 307 } 308 309 void markOverdefined(Value *V) { 310 assert(!V->getType()->isStructTy() && 311 "structs should use markAnythingOverdefined"); 312 markOverdefined(ValueState[V], V); 313 } 314 315 /// markAnythingOverdefined - Mark the specified value overdefined. This 316 /// works with both scalars and structs. 317 void markAnythingOverdefined(Value *V) { 318 if (auto *STy = dyn_cast<StructType>(V->getType())) 319 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) 320 markOverdefined(getStructValueState(V, i), V); 321 else 322 markOverdefined(V); 323 } 324 325 // isStructLatticeConstant - Return true if all the lattice values 326 // corresponding to elements of the structure are not overdefined, 327 // false otherwise. 328 bool isStructLatticeConstant(Function *F, StructType *STy) { 329 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 330 const auto &It = TrackedMultipleRetVals.find(std::make_pair(F, i)); 331 assert(It != TrackedMultipleRetVals.end()); 332 LatticeVal LV = It->second; 333 if (LV.isOverdefined()) 334 return false; 335 } 336 return true; 337 } 338 339 private: 340 // pushToWorkList - Helper for markConstant/markForcedConstant/markOverdefined 341 void pushToWorkList(LatticeVal &IV, Value *V) { 342 if (IV.isOverdefined()) 343 return OverdefinedInstWorkList.push_back(V); 344 InstWorkList.push_back(V); 345 } 346 347 // markConstant - Make a value be marked as "constant". If the value 348 // is not already a constant, add it to the instruction work list so that 349 // the users of the instruction are updated later. 350 // 351 void markConstant(LatticeVal &IV, Value *V, Constant *C) { 352 if (!IV.markConstant(C)) return; 353 DEBUG(dbgs() << "markConstant: " << *C << ": " << *V << '\n'); 354 pushToWorkList(IV, V); 355 } 356 357 void markConstant(Value *V, Constant *C) { 358 assert(!V->getType()->isStructTy() && "structs should use mergeInValue"); 359 markConstant(ValueState[V], V, C); 360 } 361 362 void markForcedConstant(Value *V, Constant *C) { 363 assert(!V->getType()->isStructTy() && "structs should use mergeInValue"); 364 LatticeVal &IV = ValueState[V]; 365 IV.markForcedConstant(C); 366 DEBUG(dbgs() << "markForcedConstant: " << *C << ": " << *V << '\n'); 367 pushToWorkList(IV, V); 368 } 369 370 371 // markOverdefined - Make a value be marked as "overdefined". If the 372 // value is not already overdefined, add it to the overdefined instruction 373 // work list so that the users of the instruction are updated later. 374 void markOverdefined(LatticeVal &IV, Value *V) { 375 if (!IV.markOverdefined()) return; 376 377 DEBUG(dbgs() << "markOverdefined: "; 378 if (auto *F = dyn_cast<Function>(V)) 379 dbgs() << "Function '" << F->getName() << "'\n"; 380 else 381 dbgs() << *V << '\n'); 382 // Only instructions go on the work list 383 pushToWorkList(IV, V); 384 } 385 386 void mergeInValue(LatticeVal &IV, Value *V, LatticeVal MergeWithV) { 387 if (IV.isOverdefined() || MergeWithV.isUnknown()) 388 return; // Noop. 389 if (MergeWithV.isOverdefined()) 390 return markOverdefined(IV, V); 391 if (IV.isUnknown()) 392 return markConstant(IV, V, MergeWithV.getConstant()); 393 if (IV.getConstant() != MergeWithV.getConstant()) 394 return markOverdefined(IV, V); 395 } 396 397 void mergeInValue(Value *V, LatticeVal MergeWithV) { 398 assert(!V->getType()->isStructTy() && 399 "non-structs should use markConstant"); 400 mergeInValue(ValueState[V], V, MergeWithV); 401 } 402 403 404 /// getValueState - Return the LatticeVal object that corresponds to the 405 /// value. This function handles the case when the value hasn't been seen yet 406 /// by properly seeding constants etc. 407 LatticeVal &getValueState(Value *V) { 408 assert(!V->getType()->isStructTy() && "Should use getStructValueState"); 409 410 std::pair<DenseMap<Value*, LatticeVal>::iterator, bool> I = 411 ValueState.insert(std::make_pair(V, LatticeVal())); 412 LatticeVal &LV = I.first->second; 413 414 if (!I.second) 415 return LV; // Common case, already in the map. 416 417 if (auto *C = dyn_cast<Constant>(V)) { 418 // Undef values remain unknown. 419 if (!isa<UndefValue>(V)) 420 LV.markConstant(C); // Constants are constant 421 } 422 423 // All others are underdefined by default. 424 return LV; 425 } 426 427 /// getStructValueState - Return the LatticeVal object that corresponds to the 428 /// value/field pair. This function handles the case when the value hasn't 429 /// been seen yet by properly seeding constants etc. 430 LatticeVal &getStructValueState(Value *V, unsigned i) { 431 assert(V->getType()->isStructTy() && "Should use getValueState"); 432 assert(i < cast<StructType>(V->getType())->getNumElements() && 433 "Invalid element #"); 434 435 std::pair<DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator, 436 bool> I = StructValueState.insert( 437 std::make_pair(std::make_pair(V, i), LatticeVal())); 438 LatticeVal &LV = I.first->second; 439 440 if (!I.second) 441 return LV; // Common case, already in the map. 442 443 if (auto *C = dyn_cast<Constant>(V)) { 444 Constant *Elt = C->getAggregateElement(i); 445 446 if (!Elt) 447 LV.markOverdefined(); // Unknown sort of constant. 448 else if (isa<UndefValue>(Elt)) 449 ; // Undef values remain unknown. 450 else 451 LV.markConstant(Elt); // Constants are constant. 452 } 453 454 // All others are underdefined by default. 455 return LV; 456 } 457 458 459 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB 460 /// work list if it is not already executable. 461 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) { 462 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second) 463 return; // This edge is already known to be executable! 464 465 if (!MarkBlockExecutable(Dest)) { 466 // If the destination is already executable, we just made an *edge* 467 // feasible that wasn't before. Revisit the PHI nodes in the block 468 // because they have potentially new operands. 469 DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName() 470 << " -> " << Dest->getName() << '\n'); 471 472 PHINode *PN; 473 for (BasicBlock::iterator I = Dest->begin(); 474 (PN = dyn_cast<PHINode>(I)); ++I) 475 visitPHINode(*PN); 476 } 477 } 478 479 // getFeasibleSuccessors - Return a vector of booleans to indicate which 480 // successors are reachable from a given terminator instruction. 481 // 482 void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs); 483 484 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic 485 // block to the 'To' basic block is currently feasible. 486 // 487 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To); 488 489 // OperandChangedState - This method is invoked on all of the users of an 490 // instruction that was just changed state somehow. Based on this 491 // information, we need to update the specified user of this instruction. 492 // 493 void OperandChangedState(Instruction *I) { 494 if (BBExecutable.count(I->getParent())) // Inst is executable? 495 visit(*I); 496 } 497 498 private: 499 friend class InstVisitor<SCCPSolver>; 500 501 // visit implementations - Something changed in this instruction. Either an 502 // operand made a transition, or the instruction is newly executable. Change 503 // the value type of I to reflect these changes if appropriate. 504 void visitPHINode(PHINode &I); 505 506 // Terminators 507 void visitReturnInst(ReturnInst &I); 508 void visitTerminatorInst(TerminatorInst &TI); 509 510 void visitCastInst(CastInst &I); 511 void visitSelectInst(SelectInst &I); 512 void visitBinaryOperator(Instruction &I); 513 void visitCmpInst(CmpInst &I); 514 void visitExtractValueInst(ExtractValueInst &EVI); 515 void visitInsertValueInst(InsertValueInst &IVI); 516 void visitLandingPadInst(LandingPadInst &I) { markAnythingOverdefined(&I); } 517 void visitFuncletPadInst(FuncletPadInst &FPI) { 518 markAnythingOverdefined(&FPI); 519 } 520 void visitCatchSwitchInst(CatchSwitchInst &CPI) { 521 markAnythingOverdefined(&CPI); 522 visitTerminatorInst(CPI); 523 } 524 525 // Instructions that cannot be folded away. 526 void visitStoreInst (StoreInst &I); 527 void visitLoadInst (LoadInst &I); 528 void visitGetElementPtrInst(GetElementPtrInst &I); 529 void visitCallInst (CallInst &I) { 530 visitCallSite(&I); 531 } 532 void visitInvokeInst (InvokeInst &II) { 533 visitCallSite(&II); 534 visitTerminatorInst(II); 535 } 536 void visitCallSite (CallSite CS); 537 void visitResumeInst (TerminatorInst &I) { /*returns void*/ } 538 void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ } 539 void visitFenceInst (FenceInst &I) { /*returns void*/ } 540 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { 541 markAnythingOverdefined(&I); 542 } 543 void visitAtomicRMWInst (AtomicRMWInst &I) { markOverdefined(&I); } 544 void visitAllocaInst (Instruction &I) { markOverdefined(&I); } 545 void visitVAArgInst (Instruction &I) { markAnythingOverdefined(&I); } 546 547 void visitInstruction(Instruction &I) { 548 // If a new instruction is added to LLVM that we don't handle. 549 DEBUG(dbgs() << "SCCP: Don't know how to handle: " << I << '\n'); 550 markAnythingOverdefined(&I); // Just in case 551 } 552 }; 553 554 } // end anonymous namespace 555 556 557 // getFeasibleSuccessors - Return a vector of booleans to indicate which 558 // successors are reachable from a given terminator instruction. 559 // 560 void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI, 561 SmallVectorImpl<bool> &Succs) { 562 Succs.resize(TI.getNumSuccessors()); 563 if (auto *BI = dyn_cast<BranchInst>(&TI)) { 564 if (BI->isUnconditional()) { 565 Succs[0] = true; 566 return; 567 } 568 569 LatticeVal BCValue = getValueState(BI->getCondition()); 570 ConstantInt *CI = BCValue.getConstantInt(); 571 if (!CI) { 572 // Overdefined condition variables, and branches on unfoldable constant 573 // conditions, mean the branch could go either way. 574 if (!BCValue.isUnknown()) 575 Succs[0] = Succs[1] = true; 576 return; 577 } 578 579 // Constant condition variables mean the branch can only go a single way. 580 Succs[CI->isZero()] = true; 581 return; 582 } 583 584 // Unwinding instructions successors are always executable. 585 if (TI.isExceptional()) { 586 Succs.assign(TI.getNumSuccessors(), true); 587 return; 588 } 589 590 if (auto *SI = dyn_cast<SwitchInst>(&TI)) { 591 if (!SI->getNumCases()) { 592 Succs[0] = true; 593 return; 594 } 595 LatticeVal SCValue = getValueState(SI->getCondition()); 596 ConstantInt *CI = SCValue.getConstantInt(); 597 598 if (!CI) { // Overdefined or unknown condition? 599 // All destinations are executable! 600 if (!SCValue.isUnknown()) 601 Succs.assign(TI.getNumSuccessors(), true); 602 return; 603 } 604 605 Succs[SI->findCaseValue(CI).getSuccessorIndex()] = true; 606 return; 607 } 608 609 // TODO: This could be improved if the operand is a [cast of a] BlockAddress. 610 if (isa<IndirectBrInst>(&TI)) { 611 // Just mark all destinations executable! 612 Succs.assign(TI.getNumSuccessors(), true); 613 return; 614 } 615 616 DEBUG(dbgs() << "Unknown terminator instruction: " << TI << '\n'); 617 llvm_unreachable("SCCP: Don't know how to handle this terminator!"); 618 } 619 620 621 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic 622 // block to the 'To' basic block is currently feasible. 623 // 624 bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) { 625 assert(BBExecutable.count(To) && "Dest should always be alive!"); 626 627 // Make sure the source basic block is executable!! 628 if (!BBExecutable.count(From)) return false; 629 630 // Check to make sure this edge itself is actually feasible now. 631 TerminatorInst *TI = From->getTerminator(); 632 if (auto *BI = dyn_cast<BranchInst>(TI)) { 633 if (BI->isUnconditional()) 634 return true; 635 636 LatticeVal BCValue = getValueState(BI->getCondition()); 637 638 // Overdefined condition variables mean the branch could go either way, 639 // undef conditions mean that neither edge is feasible yet. 640 ConstantInt *CI = BCValue.getConstantInt(); 641 if (!CI) 642 return !BCValue.isUnknown(); 643 644 // Constant condition variables mean the branch can only go a single way. 645 return BI->getSuccessor(CI->isZero()) == To; 646 } 647 648 // Unwinding instructions successors are always executable. 649 if (TI->isExceptional()) 650 return true; 651 652 if (auto *SI = dyn_cast<SwitchInst>(TI)) { 653 if (SI->getNumCases() < 1) 654 return true; 655 656 LatticeVal SCValue = getValueState(SI->getCondition()); 657 ConstantInt *CI = SCValue.getConstantInt(); 658 659 if (!CI) 660 return !SCValue.isUnknown(); 661 662 return SI->findCaseValue(CI).getCaseSuccessor() == To; 663 } 664 665 // Just mark all destinations executable! 666 // TODO: This could be improved if the operand is a [cast of a] BlockAddress. 667 if (isa<IndirectBrInst>(TI)) 668 return true; 669 670 DEBUG(dbgs() << "Unknown terminator instruction: " << *TI << '\n'); 671 llvm_unreachable("SCCP: Don't know how to handle this terminator!"); 672 } 673 674 // visit Implementations - Something changed in this instruction, either an 675 // operand made a transition, or the instruction is newly executable. Change 676 // the value type of I to reflect these changes if appropriate. This method 677 // makes sure to do the following actions: 678 // 679 // 1. If a phi node merges two constants in, and has conflicting value coming 680 // from different branches, or if the PHI node merges in an overdefined 681 // value, then the PHI node becomes overdefined. 682 // 2. If a phi node merges only constants in, and they all agree on value, the 683 // PHI node becomes a constant value equal to that. 684 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant 685 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined 686 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined 687 // 6. If a conditional branch has a value that is constant, make the selected 688 // destination executable 689 // 7. If a conditional branch has a value that is overdefined, make all 690 // successors executable. 691 // 692 void SCCPSolver::visitPHINode(PHINode &PN) { 693 // If this PN returns a struct, just mark the result overdefined. 694 // TODO: We could do a lot better than this if code actually uses this. 695 if (PN.getType()->isStructTy()) 696 return markAnythingOverdefined(&PN); 697 698 if (getValueState(&PN).isOverdefined()) 699 return; // Quick exit 700 701 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant, 702 // and slow us down a lot. Just mark them overdefined. 703 if (PN.getNumIncomingValues() > 64) 704 return markOverdefined(&PN); 705 706 // Look at all of the executable operands of the PHI node. If any of them 707 // are overdefined, the PHI becomes overdefined as well. If they are all 708 // constant, and they agree with each other, the PHI becomes the identical 709 // constant. If they are constant and don't agree, the PHI is overdefined. 710 // If there are no executable operands, the PHI remains unknown. 711 // 712 Constant *OperandVal = nullptr; 713 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 714 LatticeVal IV = getValueState(PN.getIncomingValue(i)); 715 if (IV.isUnknown()) continue; // Doesn't influence PHI node. 716 717 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) 718 continue; 719 720 if (IV.isOverdefined()) // PHI node becomes overdefined! 721 return markOverdefined(&PN); 722 723 if (!OperandVal) { // Grab the first value. 724 OperandVal = IV.getConstant(); 725 continue; 726 } 727 728 // There is already a reachable operand. If we conflict with it, 729 // then the PHI node becomes overdefined. If we agree with it, we 730 // can continue on. 731 732 // Check to see if there are two different constants merging, if so, the PHI 733 // node is overdefined. 734 if (IV.getConstant() != OperandVal) 735 return markOverdefined(&PN); 736 } 737 738 // If we exited the loop, this means that the PHI node only has constant 739 // arguments that agree with each other(and OperandVal is the constant) or 740 // OperandVal is null because there are no defined incoming arguments. If 741 // this is the case, the PHI remains unknown. 742 // 743 if (OperandVal) 744 markConstant(&PN, OperandVal); // Acquire operand value 745 } 746 747 void SCCPSolver::visitReturnInst(ReturnInst &I) { 748 if (I.getNumOperands() == 0) return; // ret void 749 750 Function *F = I.getParent()->getParent(); 751 Value *ResultOp = I.getOperand(0); 752 753 // If we are tracking the return value of this function, merge it in. 754 if (!TrackedRetVals.empty() && !ResultOp->getType()->isStructTy()) { 755 DenseMap<Function*, LatticeVal>::iterator TFRVI = 756 TrackedRetVals.find(F); 757 if (TFRVI != TrackedRetVals.end()) { 758 mergeInValue(TFRVI->second, F, getValueState(ResultOp)); 759 return; 760 } 761 } 762 763 // Handle functions that return multiple values. 764 if (!TrackedMultipleRetVals.empty()) { 765 if (auto *STy = dyn_cast<StructType>(ResultOp->getType())) 766 if (MRVFunctionsTracked.count(F)) 767 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) 768 mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F, 769 getStructValueState(ResultOp, i)); 770 771 } 772 } 773 774 void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) { 775 SmallVector<bool, 16> SuccFeasible; 776 getFeasibleSuccessors(TI, SuccFeasible); 777 778 BasicBlock *BB = TI.getParent(); 779 780 // Mark all feasible successors executable. 781 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i) 782 if (SuccFeasible[i]) 783 markEdgeExecutable(BB, TI.getSuccessor(i)); 784 } 785 786 void SCCPSolver::visitCastInst(CastInst &I) { 787 LatticeVal OpSt = getValueState(I.getOperand(0)); 788 if (OpSt.isOverdefined()) // Inherit overdefinedness of operand 789 markOverdefined(&I); 790 else if (OpSt.isConstant()) { 791 // Fold the constant as we build. 792 Constant *C = ConstantFoldCastOperand(I.getOpcode(), OpSt.getConstant(), 793 I.getType(), DL); 794 if (isa<UndefValue>(C)) 795 return; 796 // Propagate constant value 797 markConstant(&I, C); 798 } 799 } 800 801 802 void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) { 803 // If this returns a struct, mark all elements over defined, we don't track 804 // structs in structs. 805 if (EVI.getType()->isStructTy()) 806 return markAnythingOverdefined(&EVI); 807 808 // If this is extracting from more than one level of struct, we don't know. 809 if (EVI.getNumIndices() != 1) 810 return markOverdefined(&EVI); 811 812 Value *AggVal = EVI.getAggregateOperand(); 813 if (AggVal->getType()->isStructTy()) { 814 unsigned i = *EVI.idx_begin(); 815 LatticeVal EltVal = getStructValueState(AggVal, i); 816 mergeInValue(getValueState(&EVI), &EVI, EltVal); 817 } else { 818 // Otherwise, must be extracting from an array. 819 return markOverdefined(&EVI); 820 } 821 } 822 823 void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) { 824 auto *STy = dyn_cast<StructType>(IVI.getType()); 825 if (!STy) 826 return markOverdefined(&IVI); 827 828 // If this has more than one index, we can't handle it, drive all results to 829 // undef. 830 if (IVI.getNumIndices() != 1) 831 return markAnythingOverdefined(&IVI); 832 833 Value *Aggr = IVI.getAggregateOperand(); 834 unsigned Idx = *IVI.idx_begin(); 835 836 // Compute the result based on what we're inserting. 837 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 838 // This passes through all values that aren't the inserted element. 839 if (i != Idx) { 840 LatticeVal EltVal = getStructValueState(Aggr, i); 841 mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal); 842 continue; 843 } 844 845 Value *Val = IVI.getInsertedValueOperand(); 846 if (Val->getType()->isStructTy()) 847 // We don't track structs in structs. 848 markOverdefined(getStructValueState(&IVI, i), &IVI); 849 else { 850 LatticeVal InVal = getValueState(Val); 851 mergeInValue(getStructValueState(&IVI, i), &IVI, InVal); 852 } 853 } 854 } 855 856 void SCCPSolver::visitSelectInst(SelectInst &I) { 857 // If this select returns a struct, just mark the result overdefined. 858 // TODO: We could do a lot better than this if code actually uses this. 859 if (I.getType()->isStructTy()) 860 return markAnythingOverdefined(&I); 861 862 LatticeVal CondValue = getValueState(I.getCondition()); 863 if (CondValue.isUnknown()) 864 return; 865 866 if (ConstantInt *CondCB = CondValue.getConstantInt()) { 867 Value *OpVal = CondCB->isZero() ? I.getFalseValue() : I.getTrueValue(); 868 mergeInValue(&I, getValueState(OpVal)); 869 return; 870 } 871 872 // Otherwise, the condition is overdefined or a constant we can't evaluate. 873 // See if we can produce something better than overdefined based on the T/F 874 // value. 875 LatticeVal TVal = getValueState(I.getTrueValue()); 876 LatticeVal FVal = getValueState(I.getFalseValue()); 877 878 // select ?, C, C -> C. 879 if (TVal.isConstant() && FVal.isConstant() && 880 TVal.getConstant() == FVal.getConstant()) 881 return markConstant(&I, FVal.getConstant()); 882 883 if (TVal.isUnknown()) // select ?, undef, X -> X. 884 return mergeInValue(&I, FVal); 885 if (FVal.isUnknown()) // select ?, X, undef -> X. 886 return mergeInValue(&I, TVal); 887 markOverdefined(&I); 888 } 889 890 // Handle Binary Operators. 891 void SCCPSolver::visitBinaryOperator(Instruction &I) { 892 LatticeVal V1State = getValueState(I.getOperand(0)); 893 LatticeVal V2State = getValueState(I.getOperand(1)); 894 895 LatticeVal &IV = ValueState[&I]; 896 if (IV.isOverdefined()) return; 897 898 if (V1State.isConstant() && V2State.isConstant()) { 899 Constant *C = ConstantExpr::get(I.getOpcode(), V1State.getConstant(), 900 V2State.getConstant()); 901 // X op Y -> undef. 902 if (isa<UndefValue>(C)) 903 return; 904 return markConstant(IV, &I, C); 905 } 906 907 // If something is undef, wait for it to resolve. 908 if (!V1State.isOverdefined() && !V2State.isOverdefined()) 909 return; 910 911 // Otherwise, one of our operands is overdefined. Try to produce something 912 // better than overdefined with some tricks. 913 914 // If this is an AND or OR with 0 or -1, it doesn't matter that the other 915 // operand is overdefined. 916 if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Mul || 917 I.getOpcode() == Instruction::Or) { 918 LatticeVal *NonOverdefVal = nullptr; 919 if (!V1State.isOverdefined()) 920 NonOverdefVal = &V1State; 921 else if (!V2State.isOverdefined()) 922 NonOverdefVal = &V2State; 923 924 if (NonOverdefVal) { 925 if (NonOverdefVal->isUnknown()) 926 return; 927 928 if (I.getOpcode() == Instruction::And || 929 I.getOpcode() == Instruction::Mul) { 930 // X and 0 = 0 931 // X * 0 = 0 932 if (NonOverdefVal->getConstant()->isNullValue()) 933 return markConstant(IV, &I, NonOverdefVal->getConstant()); 934 } else { 935 // X or -1 = -1 936 if (ConstantInt *CI = NonOverdefVal->getConstantInt()) 937 if (CI->isAllOnesValue()) 938 return markConstant(IV, &I, NonOverdefVal->getConstant()); 939 } 940 } 941 } 942 943 944 markOverdefined(&I); 945 } 946 947 // Handle ICmpInst instruction. 948 void SCCPSolver::visitCmpInst(CmpInst &I) { 949 LatticeVal V1State = getValueState(I.getOperand(0)); 950 LatticeVal V2State = getValueState(I.getOperand(1)); 951 952 LatticeVal &IV = ValueState[&I]; 953 if (IV.isOverdefined()) return; 954 955 if (V1State.isConstant() && V2State.isConstant()) { 956 Constant *C = ConstantExpr::getCompare( 957 I.getPredicate(), V1State.getConstant(), V2State.getConstant()); 958 if (isa<UndefValue>(C)) 959 return; 960 return markConstant(IV, &I, C); 961 } 962 963 // If operands are still unknown, wait for it to resolve. 964 if (!V1State.isOverdefined() && !V2State.isOverdefined()) 965 return; 966 967 markOverdefined(&I); 968 } 969 970 // Handle getelementptr instructions. If all operands are constants then we 971 // can turn this into a getelementptr ConstantExpr. 972 // 973 void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) { 974 if (ValueState[&I].isOverdefined()) return; 975 976 SmallVector<Constant*, 8> Operands; 977 Operands.reserve(I.getNumOperands()); 978 979 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { 980 LatticeVal State = getValueState(I.getOperand(i)); 981 if (State.isUnknown()) 982 return; // Operands are not resolved yet. 983 984 if (State.isOverdefined()) 985 return markOverdefined(&I); 986 987 assert(State.isConstant() && "Unknown state!"); 988 Operands.push_back(State.getConstant()); 989 } 990 991 Constant *Ptr = Operands[0]; 992 auto Indices = makeArrayRef(Operands.begin() + 1, Operands.end()); 993 Constant *C = 994 ConstantExpr::getGetElementPtr(I.getSourceElementType(), Ptr, Indices); 995 if (isa<UndefValue>(C)) 996 return; 997 markConstant(&I, C); 998 } 999 1000 void SCCPSolver::visitStoreInst(StoreInst &SI) { 1001 // If this store is of a struct, ignore it. 1002 if (SI.getOperand(0)->getType()->isStructTy()) 1003 return; 1004 1005 if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1))) 1006 return; 1007 1008 GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1)); 1009 DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV); 1010 if (I == TrackedGlobals.end() || I->second.isOverdefined()) return; 1011 1012 // Get the value we are storing into the global, then merge it. 1013 mergeInValue(I->second, GV, getValueState(SI.getOperand(0))); 1014 if (I->second.isOverdefined()) 1015 TrackedGlobals.erase(I); // No need to keep tracking this! 1016 } 1017 1018 1019 // Handle load instructions. If the operand is a constant pointer to a constant 1020 // global, we can replace the load with the loaded constant value! 1021 void SCCPSolver::visitLoadInst(LoadInst &I) { 1022 // If this load is of a struct, just mark the result overdefined. 1023 if (I.getType()->isStructTy()) 1024 return markAnythingOverdefined(&I); 1025 1026 LatticeVal PtrVal = getValueState(I.getOperand(0)); 1027 if (PtrVal.isUnknown()) return; // The pointer is not resolved yet! 1028 1029 LatticeVal &IV = ValueState[&I]; 1030 if (IV.isOverdefined()) return; 1031 1032 if (!PtrVal.isConstant() || I.isVolatile()) 1033 return markOverdefined(IV, &I); 1034 1035 Constant *Ptr = PtrVal.getConstant(); 1036 1037 // load null is undefined. 1038 if (isa<ConstantPointerNull>(Ptr) && I.getPointerAddressSpace() == 0) 1039 return; 1040 1041 // Transform load (constant global) into the value loaded. 1042 if (auto *GV = dyn_cast<GlobalVariable>(Ptr)) { 1043 if (!TrackedGlobals.empty()) { 1044 // If we are tracking this global, merge in the known value for it. 1045 DenseMap<GlobalVariable*, LatticeVal>::iterator It = 1046 TrackedGlobals.find(GV); 1047 if (It != TrackedGlobals.end()) { 1048 mergeInValue(IV, &I, It->second); 1049 return; 1050 } 1051 } 1052 } 1053 1054 // Transform load from a constant into a constant if possible. 1055 if (Constant *C = ConstantFoldLoadFromConstPtr(Ptr, I.getType(), DL)) { 1056 if (isa<UndefValue>(C)) 1057 return; 1058 return markConstant(IV, &I, C); 1059 } 1060 1061 // Otherwise we cannot say for certain what value this load will produce. 1062 // Bail out. 1063 markOverdefined(IV, &I); 1064 } 1065 1066 void SCCPSolver::visitCallSite(CallSite CS) { 1067 Function *F = CS.getCalledFunction(); 1068 Instruction *I = CS.getInstruction(); 1069 1070 // The common case is that we aren't tracking the callee, either because we 1071 // are not doing interprocedural analysis or the callee is indirect, or is 1072 // external. Handle these cases first. 1073 if (!F || F->isDeclaration()) { 1074 CallOverdefined: 1075 // Void return and not tracking callee, just bail. 1076 if (I->getType()->isVoidTy()) return; 1077 1078 // Otherwise, if we have a single return value case, and if the function is 1079 // a declaration, maybe we can constant fold it. 1080 if (F && F->isDeclaration() && !I->getType()->isStructTy() && 1081 canConstantFoldCallTo(F)) { 1082 1083 SmallVector<Constant*, 8> Operands; 1084 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); 1085 AI != E; ++AI) { 1086 LatticeVal State = getValueState(*AI); 1087 1088 if (State.isUnknown()) 1089 return; // Operands are not resolved yet. 1090 if (State.isOverdefined()) 1091 return markOverdefined(I); 1092 assert(State.isConstant() && "Unknown state!"); 1093 Operands.push_back(State.getConstant()); 1094 } 1095 1096 if (getValueState(I).isOverdefined()) 1097 return; 1098 1099 // If we can constant fold this, mark the result of the call as a 1100 // constant. 1101 if (Constant *C = ConstantFoldCall(F, Operands, TLI)) { 1102 // call -> undef. 1103 if (isa<UndefValue>(C)) 1104 return; 1105 return markConstant(I, C); 1106 } 1107 } 1108 1109 // Otherwise, we don't know anything about this call, mark it overdefined. 1110 return markAnythingOverdefined(I); 1111 } 1112 1113 // If this is a local function that doesn't have its address taken, mark its 1114 // entry block executable and merge in the actual arguments to the call into 1115 // the formal arguments of the function. 1116 if (!TrackingIncomingArguments.empty() && TrackingIncomingArguments.count(F)){ 1117 MarkBlockExecutable(&F->front()); 1118 1119 // Propagate information from this call site into the callee. 1120 CallSite::arg_iterator CAI = CS.arg_begin(); 1121 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); 1122 AI != E; ++AI, ++CAI) { 1123 // If this argument is byval, and if the function is not readonly, there 1124 // will be an implicit copy formed of the input aggregate. 1125 if (AI->hasByValAttr() && !F->onlyReadsMemory()) { 1126 markOverdefined(&*AI); 1127 continue; 1128 } 1129 1130 if (auto *STy = dyn_cast<StructType>(AI->getType())) { 1131 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1132 LatticeVal CallArg = getStructValueState(*CAI, i); 1133 mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg); 1134 } 1135 } else { 1136 mergeInValue(&*AI, getValueState(*CAI)); 1137 } 1138 } 1139 } 1140 1141 // If this is a single/zero retval case, see if we're tracking the function. 1142 if (auto *STy = dyn_cast<StructType>(F->getReturnType())) { 1143 if (!MRVFunctionsTracked.count(F)) 1144 goto CallOverdefined; // Not tracking this callee. 1145 1146 // If we are tracking this callee, propagate the result of the function 1147 // into this call site. 1148 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) 1149 mergeInValue(getStructValueState(I, i), I, 1150 TrackedMultipleRetVals[std::make_pair(F, i)]); 1151 } else { 1152 DenseMap<Function*, LatticeVal>::iterator TFRVI = TrackedRetVals.find(F); 1153 if (TFRVI == TrackedRetVals.end()) 1154 goto CallOverdefined; // Not tracking this callee. 1155 1156 // If so, propagate the return value of the callee into this call result. 1157 mergeInValue(I, TFRVI->second); 1158 } 1159 } 1160 1161 void SCCPSolver::Solve() { 1162 // Process the work lists until they are empty! 1163 while (!BBWorkList.empty() || !InstWorkList.empty() || 1164 !OverdefinedInstWorkList.empty()) { 1165 // Process the overdefined instruction's work list first, which drives other 1166 // things to overdefined more quickly. 1167 while (!OverdefinedInstWorkList.empty()) { 1168 Value *I = OverdefinedInstWorkList.pop_back_val(); 1169 1170 DEBUG(dbgs() << "\nPopped off OI-WL: " << *I << '\n'); 1171 1172 // "I" got into the work list because it either made the transition from 1173 // bottom to constant, or to overdefined. 1174 // 1175 // Anything on this worklist that is overdefined need not be visited 1176 // since all of its users will have already been marked as overdefined 1177 // Update all of the users of this instruction's value. 1178 // 1179 for (User *U : I->users()) 1180 if (auto *UI = dyn_cast<Instruction>(U)) 1181 OperandChangedState(UI); 1182 } 1183 1184 // Process the instruction work list. 1185 while (!InstWorkList.empty()) { 1186 Value *I = InstWorkList.pop_back_val(); 1187 1188 DEBUG(dbgs() << "\nPopped off I-WL: " << *I << '\n'); 1189 1190 // "I" got into the work list because it made the transition from undef to 1191 // constant. 1192 // 1193 // Anything on this worklist that is overdefined need not be visited 1194 // since all of its users will have already been marked as overdefined. 1195 // Update all of the users of this instruction's value. 1196 // 1197 if (I->getType()->isStructTy() || !getValueState(I).isOverdefined()) 1198 for (User *U : I->users()) 1199 if (auto *UI = dyn_cast<Instruction>(U)) 1200 OperandChangedState(UI); 1201 } 1202 1203 // Process the basic block work list. 1204 while (!BBWorkList.empty()) { 1205 BasicBlock *BB = BBWorkList.back(); 1206 BBWorkList.pop_back(); 1207 1208 DEBUG(dbgs() << "\nPopped off BBWL: " << *BB << '\n'); 1209 1210 // Notify all instructions in this basic block that they are newly 1211 // executable. 1212 visit(BB); 1213 } 1214 } 1215 } 1216 1217 /// ResolvedUndefsIn - While solving the dataflow for a function, we assume 1218 /// that branches on undef values cannot reach any of their successors. 1219 /// However, this is not a safe assumption. After we solve dataflow, this 1220 /// method should be use to handle this. If this returns true, the solver 1221 /// should be rerun. 1222 /// 1223 /// This method handles this by finding an unresolved branch and marking it one 1224 /// of the edges from the block as being feasible, even though the condition 1225 /// doesn't say it would otherwise be. This allows SCCP to find the rest of the 1226 /// CFG and only slightly pessimizes the analysis results (by marking one, 1227 /// potentially infeasible, edge feasible). This cannot usefully modify the 1228 /// constraints on the condition of the branch, as that would impact other users 1229 /// of the value. 1230 /// 1231 /// This scan also checks for values that use undefs, whose results are actually 1232 /// defined. For example, 'zext i8 undef to i32' should produce all zeros 1233 /// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero, 1234 /// even if X isn't defined. 1235 bool SCCPSolver::ResolvedUndefsIn(Function &F) { 1236 for (BasicBlock &BB : F) { 1237 if (!BBExecutable.count(&BB)) 1238 continue; 1239 1240 for (Instruction &I : BB) { 1241 // Look for instructions which produce undef values. 1242 if (I.getType()->isVoidTy()) continue; 1243 1244 if (auto *STy = dyn_cast<StructType>(I.getType())) { 1245 // Only a few things that can be structs matter for undef. 1246 1247 // Tracked calls must never be marked overdefined in ResolvedUndefsIn. 1248 if (CallSite CS = CallSite(&I)) 1249 if (Function *F = CS.getCalledFunction()) 1250 if (MRVFunctionsTracked.count(F)) 1251 continue; 1252 1253 // extractvalue and insertvalue don't need to be marked; they are 1254 // tracked as precisely as their operands. 1255 if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(I)) 1256 continue; 1257 1258 // Send the results of everything else to overdefined. We could be 1259 // more precise than this but it isn't worth bothering. 1260 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1261 LatticeVal &LV = getStructValueState(&I, i); 1262 if (LV.isUnknown()) 1263 markOverdefined(LV, &I); 1264 } 1265 continue; 1266 } 1267 1268 LatticeVal &LV = getValueState(&I); 1269 if (!LV.isUnknown()) continue; 1270 1271 // extractvalue is safe; check here because the argument is a struct. 1272 if (isa<ExtractValueInst>(I)) 1273 continue; 1274 1275 // Compute the operand LatticeVals, for convenience below. 1276 // Anything taking a struct is conservatively assumed to require 1277 // overdefined markings. 1278 if (I.getOperand(0)->getType()->isStructTy()) { 1279 markOverdefined(&I); 1280 return true; 1281 } 1282 LatticeVal Op0LV = getValueState(I.getOperand(0)); 1283 LatticeVal Op1LV; 1284 if (I.getNumOperands() == 2) { 1285 if (I.getOperand(1)->getType()->isStructTy()) { 1286 markOverdefined(&I); 1287 return true; 1288 } 1289 1290 Op1LV = getValueState(I.getOperand(1)); 1291 } 1292 // If this is an instructions whose result is defined even if the input is 1293 // not fully defined, propagate the information. 1294 Type *ITy = I.getType(); 1295 switch (I.getOpcode()) { 1296 case Instruction::Add: 1297 case Instruction::Sub: 1298 case Instruction::Trunc: 1299 case Instruction::FPTrunc: 1300 case Instruction::BitCast: 1301 break; // Any undef -> undef 1302 case Instruction::FSub: 1303 case Instruction::FAdd: 1304 case Instruction::FMul: 1305 case Instruction::FDiv: 1306 case Instruction::FRem: 1307 // Floating-point binary operation: be conservative. 1308 if (Op0LV.isUnknown() && Op1LV.isUnknown()) 1309 markForcedConstant(&I, Constant::getNullValue(ITy)); 1310 else 1311 markOverdefined(&I); 1312 return true; 1313 case Instruction::ZExt: 1314 case Instruction::SExt: 1315 case Instruction::FPToUI: 1316 case Instruction::FPToSI: 1317 case Instruction::FPExt: 1318 case Instruction::PtrToInt: 1319 case Instruction::IntToPtr: 1320 case Instruction::SIToFP: 1321 case Instruction::UIToFP: 1322 // undef -> 0; some outputs are impossible 1323 markForcedConstant(&I, Constant::getNullValue(ITy)); 1324 return true; 1325 case Instruction::Mul: 1326 case Instruction::And: 1327 // Both operands undef -> undef 1328 if (Op0LV.isUnknown() && Op1LV.isUnknown()) 1329 break; 1330 // undef * X -> 0. X could be zero. 1331 // undef & X -> 0. X could be zero. 1332 markForcedConstant(&I, Constant::getNullValue(ITy)); 1333 return true; 1334 1335 case Instruction::Or: 1336 // Both operands undef -> undef 1337 if (Op0LV.isUnknown() && Op1LV.isUnknown()) 1338 break; 1339 // undef | X -> -1. X could be -1. 1340 markForcedConstant(&I, Constant::getAllOnesValue(ITy)); 1341 return true; 1342 1343 case Instruction::Xor: 1344 // undef ^ undef -> 0; strictly speaking, this is not strictly 1345 // necessary, but we try to be nice to people who expect this 1346 // behavior in simple cases 1347 if (Op0LV.isUnknown() && Op1LV.isUnknown()) { 1348 markForcedConstant(&I, Constant::getNullValue(ITy)); 1349 return true; 1350 } 1351 // undef ^ X -> undef 1352 break; 1353 1354 case Instruction::SDiv: 1355 case Instruction::UDiv: 1356 case Instruction::SRem: 1357 case Instruction::URem: 1358 // X / undef -> undef. No change. 1359 // X % undef -> undef. No change. 1360 if (Op1LV.isUnknown()) break; 1361 1362 // X / 0 -> undef. No change. 1363 // X % 0 -> undef. No change. 1364 if (Op1LV.isConstant() && Op1LV.getConstant()->isZeroValue()) 1365 break; 1366 1367 // undef / X -> 0. X could be maxint. 1368 // undef % X -> 0. X could be 1. 1369 markForcedConstant(&I, Constant::getNullValue(ITy)); 1370 return true; 1371 1372 case Instruction::AShr: 1373 // X >>a undef -> undef. 1374 if (Op1LV.isUnknown()) break; 1375 1376 // Shifting by the bitwidth or more is undefined. 1377 if (Op1LV.isConstant()) { 1378 if (auto *ShiftAmt = Op1LV.getConstantInt()) 1379 if (ShiftAmt->getLimitedValue() >= 1380 ShiftAmt->getType()->getScalarSizeInBits()) 1381 break; 1382 } 1383 1384 // undef >>a X -> 0 1385 markForcedConstant(&I, Constant::getNullValue(ITy)); 1386 return true; 1387 case Instruction::LShr: 1388 case Instruction::Shl: 1389 // X << undef -> undef. 1390 // X >> undef -> undef. 1391 if (Op1LV.isUnknown()) break; 1392 1393 // Shifting by the bitwidth or more is undefined. 1394 if (Op1LV.isConstant()) { 1395 if (auto *ShiftAmt = Op1LV.getConstantInt()) 1396 if (ShiftAmt->getLimitedValue() >= 1397 ShiftAmt->getType()->getScalarSizeInBits()) 1398 break; 1399 } 1400 1401 // undef << X -> 0 1402 // undef >> X -> 0 1403 markForcedConstant(&I, Constant::getNullValue(ITy)); 1404 return true; 1405 case Instruction::Select: 1406 Op1LV = getValueState(I.getOperand(1)); 1407 // undef ? X : Y -> X or Y. There could be commonality between X/Y. 1408 if (Op0LV.isUnknown()) { 1409 if (!Op1LV.isConstant()) // Pick the constant one if there is any. 1410 Op1LV = getValueState(I.getOperand(2)); 1411 } else if (Op1LV.isUnknown()) { 1412 // c ? undef : undef -> undef. No change. 1413 Op1LV = getValueState(I.getOperand(2)); 1414 if (Op1LV.isUnknown()) 1415 break; 1416 // Otherwise, c ? undef : x -> x. 1417 } else { 1418 // Leave Op1LV as Operand(1)'s LatticeValue. 1419 } 1420 1421 if (Op1LV.isConstant()) 1422 markForcedConstant(&I, Op1LV.getConstant()); 1423 else 1424 markOverdefined(&I); 1425 return true; 1426 case Instruction::Load: 1427 // A load here means one of two things: a load of undef from a global, 1428 // a load from an unknown pointer. Either way, having it return undef 1429 // is okay. 1430 break; 1431 case Instruction::ICmp: 1432 // X == undef -> undef. Other comparisons get more complicated. 1433 if (cast<ICmpInst>(&I)->isEquality()) 1434 break; 1435 markOverdefined(&I); 1436 return true; 1437 case Instruction::Call: 1438 case Instruction::Invoke: { 1439 // There are two reasons a call can have an undef result 1440 // 1. It could be tracked. 1441 // 2. It could be constant-foldable. 1442 // Because of the way we solve return values, tracked calls must 1443 // never be marked overdefined in ResolvedUndefsIn. 1444 if (Function *F = CallSite(&I).getCalledFunction()) 1445 if (TrackedRetVals.count(F)) 1446 break; 1447 1448 // If the call is constant-foldable, we mark it overdefined because 1449 // we do not know what return values are valid. 1450 markOverdefined(&I); 1451 return true; 1452 } 1453 default: 1454 // If we don't know what should happen here, conservatively mark it 1455 // overdefined. 1456 markOverdefined(&I); 1457 return true; 1458 } 1459 } 1460 1461 // Check to see if we have a branch or switch on an undefined value. If so 1462 // we force the branch to go one way or the other to make the successor 1463 // values live. It doesn't really matter which way we force it. 1464 TerminatorInst *TI = BB.getTerminator(); 1465 if (auto *BI = dyn_cast<BranchInst>(TI)) { 1466 if (!BI->isConditional()) continue; 1467 if (!getValueState(BI->getCondition()).isUnknown()) 1468 continue; 1469 1470 // If the input to SCCP is actually branch on undef, fix the undef to 1471 // false. 1472 if (isa<UndefValue>(BI->getCondition())) { 1473 BI->setCondition(ConstantInt::getFalse(BI->getContext())); 1474 markEdgeExecutable(&BB, TI->getSuccessor(1)); 1475 return true; 1476 } 1477 1478 // Otherwise, it is a branch on a symbolic value which is currently 1479 // considered to be undef. Handle this by forcing the input value to the 1480 // branch to false. 1481 markForcedConstant(BI->getCondition(), 1482 ConstantInt::getFalse(TI->getContext())); 1483 return true; 1484 } 1485 1486 if (auto *SI = dyn_cast<SwitchInst>(TI)) { 1487 if (!SI->getNumCases() || !getValueState(SI->getCondition()).isUnknown()) 1488 continue; 1489 1490 // If the input to SCCP is actually switch on undef, fix the undef to 1491 // the first constant. 1492 if (isa<UndefValue>(SI->getCondition())) { 1493 SI->setCondition(SI->case_begin().getCaseValue()); 1494 markEdgeExecutable(&BB, SI->case_begin().getCaseSuccessor()); 1495 return true; 1496 } 1497 1498 markForcedConstant(SI->getCondition(), SI->case_begin().getCaseValue()); 1499 return true; 1500 } 1501 } 1502 1503 return false; 1504 } 1505 1506 static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V) { 1507 Constant *Const = nullptr; 1508 if (V->getType()->isStructTy()) { 1509 std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(V); 1510 if (any_of(IVs, [](const LatticeVal &LV) { return LV.isOverdefined(); })) 1511 return false; 1512 std::vector<Constant *> ConstVals; 1513 auto *ST = dyn_cast<StructType>(V->getType()); 1514 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { 1515 LatticeVal V = IVs[i]; 1516 ConstVals.push_back(V.isConstant() 1517 ? V.getConstant() 1518 : UndefValue::get(ST->getElementType(i))); 1519 } 1520 Const = ConstantStruct::get(ST, ConstVals); 1521 } else { 1522 LatticeVal IV = Solver.getLatticeValueFor(V); 1523 if (IV.isOverdefined()) 1524 return false; 1525 Const = IV.isConstant() ? IV.getConstant() : UndefValue::get(V->getType()); 1526 } 1527 assert(Const && "Constant is nullptr here!"); 1528 DEBUG(dbgs() << " Constant: " << *Const << " = " << *V << '\n'); 1529 1530 // Replaces all of the uses of a variable with uses of the constant. 1531 V->replaceAllUsesWith(Const); 1532 return true; 1533 } 1534 1535 // runSCCP() - Run the Sparse Conditional Constant Propagation algorithm, 1536 // and return true if the function was modified. 1537 // 1538 static bool runSCCP(Function &F, const DataLayout &DL, 1539 const TargetLibraryInfo *TLI) { 1540 DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n"); 1541 SCCPSolver Solver(DL, TLI); 1542 1543 // Mark the first block of the function as being executable. 1544 Solver.MarkBlockExecutable(&F.front()); 1545 1546 // Mark all arguments to the function as being overdefined. 1547 for (Argument &AI : F.args()) 1548 Solver.markAnythingOverdefined(&AI); 1549 1550 // Solve for constants. 1551 bool ResolvedUndefs = true; 1552 while (ResolvedUndefs) { 1553 Solver.Solve(); 1554 DEBUG(dbgs() << "RESOLVING UNDEFs\n"); 1555 ResolvedUndefs = Solver.ResolvedUndefsIn(F); 1556 } 1557 1558 bool MadeChanges = false; 1559 1560 // If we decided that there are basic blocks that are dead in this function, 1561 // delete their contents now. Note that we cannot actually delete the blocks, 1562 // as we cannot modify the CFG of the function. 1563 1564 for (BasicBlock &BB : F) { 1565 if (!Solver.isBlockExecutable(&BB)) { 1566 DEBUG(dbgs() << " BasicBlock Dead:" << BB); 1567 1568 ++NumDeadBlocks; 1569 NumInstRemoved += removeAllNonTerminatorAndEHPadInstructions(&BB); 1570 1571 MadeChanges = true; 1572 continue; 1573 } 1574 1575 // Iterate over all of the instructions in a function, replacing them with 1576 // constants if we have found them to be of constant values. 1577 // 1578 for (BasicBlock::iterator BI = BB.begin(), E = BB.end(); BI != E;) { 1579 Instruction *Inst = &*BI++; 1580 if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst)) 1581 continue; 1582 1583 if (tryToReplaceWithConstant(Solver, Inst)) { 1584 if (isInstructionTriviallyDead(Inst)) 1585 Inst->eraseFromParent(); 1586 // Hey, we just changed something! 1587 MadeChanges = true; 1588 ++NumInstRemoved; 1589 } 1590 } 1591 } 1592 1593 return MadeChanges; 1594 } 1595 1596 PreservedAnalyses SCCPPass::run(Function &F, FunctionAnalysisManager &AM) { 1597 const DataLayout &DL = F.getParent()->getDataLayout(); 1598 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); 1599 if (!runSCCP(F, DL, &TLI)) 1600 return PreservedAnalyses::all(); 1601 1602 auto PA = PreservedAnalyses(); 1603 PA.preserve<GlobalsAA>(); 1604 return PA; 1605 } 1606 1607 namespace { 1608 //===--------------------------------------------------------------------===// 1609 // 1610 /// SCCP Class - This class uses the SCCPSolver to implement a per-function 1611 /// Sparse Conditional Constant Propagator. 1612 /// 1613 class SCCPLegacyPass : public FunctionPass { 1614 public: 1615 void getAnalysisUsage(AnalysisUsage &AU) const override { 1616 AU.addRequired<TargetLibraryInfoWrapperPass>(); 1617 AU.addPreserved<GlobalsAAWrapperPass>(); 1618 } 1619 static char ID; // Pass identification, replacement for typeid 1620 SCCPLegacyPass() : FunctionPass(ID) { 1621 initializeSCCPLegacyPassPass(*PassRegistry::getPassRegistry()); 1622 } 1623 1624 // runOnFunction - Run the Sparse Conditional Constant Propagation 1625 // algorithm, and return true if the function was modified. 1626 // 1627 bool runOnFunction(Function &F) override { 1628 if (skipFunction(F)) 1629 return false; 1630 const DataLayout &DL = F.getParent()->getDataLayout(); 1631 const TargetLibraryInfo *TLI = 1632 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 1633 return runSCCP(F, DL, TLI); 1634 } 1635 }; 1636 } // end anonymous namespace 1637 1638 char SCCPLegacyPass::ID = 0; 1639 INITIALIZE_PASS_BEGIN(SCCPLegacyPass, "sccp", 1640 "Sparse Conditional Constant Propagation", false, false) 1641 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 1642 INITIALIZE_PASS_END(SCCPLegacyPass, "sccp", 1643 "Sparse Conditional Constant Propagation", false, false) 1644 1645 // createSCCPPass - This is the public interface to this file. 1646 FunctionPass *llvm::createSCCPPass() { return new SCCPLegacyPass(); } 1647 1648 static bool AddressIsTaken(const GlobalValue *GV) { 1649 // Delete any dead constantexpr klingons. 1650 GV->removeDeadConstantUsers(); 1651 1652 for (const Use &U : GV->uses()) { 1653 const User *UR = U.getUser(); 1654 if (const auto *SI = dyn_cast<StoreInst>(UR)) { 1655 if (SI->getOperand(0) == GV || SI->isVolatile()) 1656 return true; // Storing addr of GV. 1657 } else if (isa<InvokeInst>(UR) || isa<CallInst>(UR)) { 1658 // Make sure we are calling the function, not passing the address. 1659 ImmutableCallSite CS(cast<Instruction>(UR)); 1660 if (!CS.isCallee(&U)) 1661 return true; 1662 } else if (const auto *LI = dyn_cast<LoadInst>(UR)) { 1663 if (LI->isVolatile()) 1664 return true; 1665 } else if (isa<BlockAddress>(UR)) { 1666 // blockaddress doesn't take the address of the function, it takes addr 1667 // of label. 1668 } else { 1669 return true; 1670 } 1671 } 1672 return false; 1673 } 1674 1675 static void findReturnsToZap(Function &F, 1676 SmallPtrSet<Function *, 32> &AddressTakenFunctions, 1677 SmallVector<ReturnInst *, 8> &ReturnsToZap) { 1678 // We can only do this if we know that nothing else can call the function. 1679 if (!F.hasLocalLinkage() || AddressTakenFunctions.count(&F)) 1680 return; 1681 1682 for (BasicBlock &BB : F) 1683 if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator())) 1684 if (!isa<UndefValue>(RI->getOperand(0))) 1685 ReturnsToZap.push_back(RI); 1686 } 1687 1688 static bool runIPSCCP(Module &M, const DataLayout &DL, 1689 const TargetLibraryInfo *TLI) { 1690 SCCPSolver Solver(DL, TLI); 1691 1692 // AddressTakenFunctions - This set keeps track of the address-taken functions 1693 // that are in the input. As IPSCCP runs through and simplifies code, 1694 // functions that were address taken can end up losing their 1695 // address-taken-ness. Because of this, we keep track of their addresses from 1696 // the first pass so we can use them for the later simplification pass. 1697 SmallPtrSet<Function*, 32> AddressTakenFunctions; 1698 1699 // Loop over all functions, marking arguments to those with their addresses 1700 // taken or that are external as overdefined. 1701 // 1702 for (Function &F : M) { 1703 if (F.isDeclaration()) 1704 continue; 1705 1706 // If this is an exact definition of this function, then we can propagate 1707 // information about its result into callsites of it. 1708 if (F.hasExactDefinition()) 1709 Solver.AddTrackedFunction(&F); 1710 1711 // If this function only has direct calls that we can see, we can track its 1712 // arguments and return value aggressively, and can assume it is not called 1713 // unless we see evidence to the contrary. 1714 if (F.hasLocalLinkage()) { 1715 if (AddressIsTaken(&F)) 1716 AddressTakenFunctions.insert(&F); 1717 else { 1718 Solver.AddArgumentTrackedFunction(&F); 1719 continue; 1720 } 1721 } 1722 1723 // Assume the function is called. 1724 Solver.MarkBlockExecutable(&F.front()); 1725 1726 // Assume nothing about the incoming arguments. 1727 for (Argument &AI : F.args()) 1728 Solver.markAnythingOverdefined(&AI); 1729 } 1730 1731 // Loop over global variables. We inform the solver about any internal global 1732 // variables that do not have their 'addresses taken'. If they don't have 1733 // their addresses taken, we can propagate constants through them. 1734 for (GlobalVariable &G : M.globals()) 1735 if (!G.isConstant() && G.hasLocalLinkage() && !AddressIsTaken(&G)) 1736 Solver.TrackValueOfGlobalVariable(&G); 1737 1738 // Solve for constants. 1739 bool ResolvedUndefs = true; 1740 while (ResolvedUndefs) { 1741 Solver.Solve(); 1742 1743 DEBUG(dbgs() << "RESOLVING UNDEFS\n"); 1744 ResolvedUndefs = false; 1745 for (Function &F : M) 1746 ResolvedUndefs |= Solver.ResolvedUndefsIn(F); 1747 } 1748 1749 bool MadeChanges = false; 1750 1751 // Iterate over all of the instructions in the module, replacing them with 1752 // constants if we have found them to be of constant values. 1753 // 1754 SmallVector<BasicBlock*, 512> BlocksToErase; 1755 1756 for (Function &F : M) { 1757 if (F.isDeclaration()) 1758 continue; 1759 1760 if (Solver.isBlockExecutable(&F.front())) { 1761 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; 1762 ++AI) { 1763 if (AI->use_empty()) 1764 continue; 1765 if (tryToReplaceWithConstant(Solver, &*AI)) 1766 ++IPNumArgsElimed; 1767 } 1768 } 1769 1770 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { 1771 if (!Solver.isBlockExecutable(&*BB)) { 1772 DEBUG(dbgs() << " BasicBlock Dead:" << *BB); 1773 1774 ++NumDeadBlocks; 1775 NumInstRemoved += 1776 changeToUnreachable(BB->getFirstNonPHI(), /*UseLLVMTrap=*/false); 1777 1778 MadeChanges = true; 1779 1780 if (&*BB != &F.front()) 1781 BlocksToErase.push_back(&*BB); 1782 continue; 1783 } 1784 1785 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { 1786 Instruction *Inst = &*BI++; 1787 if (Inst->getType()->isVoidTy()) 1788 continue; 1789 if (tryToReplaceWithConstant(Solver, Inst)) { 1790 if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst)) 1791 Inst->eraseFromParent(); 1792 // Hey, we just changed something! 1793 MadeChanges = true; 1794 ++IPNumInstRemoved; 1795 } 1796 } 1797 } 1798 1799 // Now that all instructions in the function are constant folded, erase dead 1800 // blocks, because we can now use ConstantFoldTerminator to get rid of 1801 // in-edges. 1802 for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) { 1803 // If there are any PHI nodes in this successor, drop entries for BB now. 1804 BasicBlock *DeadBB = BlocksToErase[i]; 1805 for (Value::user_iterator UI = DeadBB->user_begin(), 1806 UE = DeadBB->user_end(); 1807 UI != UE;) { 1808 // Grab the user and then increment the iterator early, as the user 1809 // will be deleted. Step past all adjacent uses from the same user. 1810 auto *I = dyn_cast<Instruction>(*UI); 1811 do { ++UI; } while (UI != UE && *UI == I); 1812 1813 // Ignore blockaddress users; BasicBlock's dtor will handle them. 1814 if (!I) continue; 1815 1816 bool Folded = ConstantFoldTerminator(I->getParent()); 1817 if (!Folded) { 1818 // The constant folder may not have been able to fold the terminator 1819 // if this is a branch or switch on undef. Fold it manually as a 1820 // branch to the first successor. 1821 #ifndef NDEBUG 1822 if (auto *BI = dyn_cast<BranchInst>(I)) { 1823 assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) && 1824 "Branch should be foldable!"); 1825 } else if (auto *SI = dyn_cast<SwitchInst>(I)) { 1826 assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold"); 1827 } else { 1828 llvm_unreachable("Didn't fold away reference to block!"); 1829 } 1830 #endif 1831 1832 // Make this an uncond branch to the first successor. 1833 TerminatorInst *TI = I->getParent()->getTerminator(); 1834 BranchInst::Create(TI->getSuccessor(0), TI); 1835 1836 // Remove entries in successor phi nodes to remove edges. 1837 for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i) 1838 TI->getSuccessor(i)->removePredecessor(TI->getParent()); 1839 1840 // Remove the old terminator. 1841 TI->eraseFromParent(); 1842 } 1843 } 1844 1845 // Finally, delete the basic block. 1846 F.getBasicBlockList().erase(DeadBB); 1847 } 1848 BlocksToErase.clear(); 1849 } 1850 1851 // If we inferred constant or undef return values for a function, we replaced 1852 // all call uses with the inferred value. This means we don't need to bother 1853 // actually returning anything from the function. Replace all return 1854 // instructions with return undef. 1855 // 1856 // Do this in two stages: first identify the functions we should process, then 1857 // actually zap their returns. This is important because we can only do this 1858 // if the address of the function isn't taken. In cases where a return is the 1859 // last use of a function, the order of processing functions would affect 1860 // whether other functions are optimizable. 1861 SmallVector<ReturnInst*, 8> ReturnsToZap; 1862 1863 const DenseMap<Function*, LatticeVal> &RV = Solver.getTrackedRetVals(); 1864 for (const auto &I : RV) { 1865 Function *F = I.first; 1866 if (I.second.isOverdefined() || F->getReturnType()->isVoidTy()) 1867 continue; 1868 findReturnsToZap(*F, AddressTakenFunctions, ReturnsToZap); 1869 } 1870 1871 for (const auto &F : Solver.getMRVFunctionsTracked()) { 1872 assert(F->getReturnType()->isStructTy() && 1873 "The return type should be a struct"); 1874 StructType *STy = cast<StructType>(F->getReturnType()); 1875 if (Solver.isStructLatticeConstant(F, STy)) 1876 findReturnsToZap(*F, AddressTakenFunctions, ReturnsToZap); 1877 } 1878 1879 // Zap all returns which we've identified as zap to change. 1880 for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) { 1881 Function *F = ReturnsToZap[i]->getParent()->getParent(); 1882 ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType())); 1883 } 1884 1885 // If we inferred constant or undef values for globals variables, we can 1886 // delete the global and any stores that remain to it. 1887 const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals(); 1888 for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(), 1889 E = TG.end(); I != E; ++I) { 1890 GlobalVariable *GV = I->first; 1891 assert(!I->second.isOverdefined() && 1892 "Overdefined values should have been taken out of the map!"); 1893 DEBUG(dbgs() << "Found that GV '" << GV->getName() << "' is constant!\n"); 1894 while (!GV->use_empty()) { 1895 StoreInst *SI = cast<StoreInst>(GV->user_back()); 1896 SI->eraseFromParent(); 1897 } 1898 M.getGlobalList().erase(GV); 1899 ++IPNumGlobalConst; 1900 } 1901 1902 return MadeChanges; 1903 } 1904 1905 PreservedAnalyses IPSCCPPass::run(Module &M, ModuleAnalysisManager &AM) { 1906 const DataLayout &DL = M.getDataLayout(); 1907 auto &TLI = AM.getResult<TargetLibraryAnalysis>(M); 1908 if (!runIPSCCP(M, DL, &TLI)) 1909 return PreservedAnalyses::all(); 1910 return PreservedAnalyses::none(); 1911 } 1912 1913 namespace { 1914 //===--------------------------------------------------------------------===// 1915 // 1916 /// IPSCCP Class - This class implements interprocedural Sparse Conditional 1917 /// Constant Propagation. 1918 /// 1919 class IPSCCPLegacyPass : public ModulePass { 1920 public: 1921 static char ID; 1922 1923 IPSCCPLegacyPass() : ModulePass(ID) { 1924 initializeIPSCCPLegacyPassPass(*PassRegistry::getPassRegistry()); 1925 } 1926 1927 bool runOnModule(Module &M) override { 1928 if (skipModule(M)) 1929 return false; 1930 const DataLayout &DL = M.getDataLayout(); 1931 const TargetLibraryInfo *TLI = 1932 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 1933 return runIPSCCP(M, DL, TLI); 1934 } 1935 1936 void getAnalysisUsage(AnalysisUsage &AU) const override { 1937 AU.addRequired<TargetLibraryInfoWrapperPass>(); 1938 } 1939 }; 1940 } // end anonymous namespace 1941 1942 char IPSCCPLegacyPass::ID = 0; 1943 INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp", 1944 "Interprocedural Sparse Conditional Constant Propagation", 1945 false, false) 1946 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 1947 INITIALIZE_PASS_END(IPSCCPLegacyPass, "ipsccp", 1948 "Interprocedural Sparse Conditional Constant Propagation", 1949 false, false) 1950 1951 // createIPSCCPPass - This is the public interface to this file. 1952 ModulePass *llvm::createIPSCCPPass() { return new IPSCCPLegacyPass(); } 1953