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