SCCP.cpp revision b576c94c15af9a440f69d9d03c2afead7971118c
1//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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// Notice that:
19//   * This pass has a habit of making definitions be dead.  It is a good idea
20//     to to run a DCE pass sometime after running this pass.
21//
22//===----------------------------------------------------------------------===//
23
24#include "llvm/Transforms/Scalar.h"
25#include "llvm/ConstantHandling.h"
26#include "llvm/Function.h"
27#include "llvm/Instructions.h"
28#include "llvm/Pass.h"
29#include "llvm/Support/InstVisitor.h"
30#include "Support/Debug.h"
31#include "Support/Statistic.h"
32#include "Support/STLExtras.h"
33#include <algorithm>
34#include <set>
35
36// InstVal class - This class represents the different lattice values that an
37// instruction may occupy.  It is a simple class with value semantics.
38//
39namespace {
40  Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
41
42class InstVal {
43  enum {
44    undefined,           // This instruction has no known value
45    constant,            // This instruction has a constant value
46    overdefined          // This instruction has an unknown value
47  } LatticeValue;        // The current lattice position
48  Constant *ConstantVal; // If Constant value, the current value
49public:
50  inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
51
52  // markOverdefined - Return true if this is a new status to be in...
53  inline bool markOverdefined() {
54    if (LatticeValue != overdefined) {
55      LatticeValue = overdefined;
56      return true;
57    }
58    return false;
59  }
60
61  // markConstant - Return true if this is a new status for us...
62  inline bool markConstant(Constant *V) {
63    if (LatticeValue != constant) {
64      LatticeValue = constant;
65      ConstantVal = V;
66      return true;
67    } else {
68      assert(ConstantVal == V && "Marking constant with different value");
69    }
70    return false;
71  }
72
73  inline bool isUndefined()   const { return LatticeValue == undefined; }
74  inline bool isConstant()    const { return LatticeValue == constant; }
75  inline bool isOverdefined() const { return LatticeValue == overdefined; }
76
77  inline Constant *getConstant() const { return ConstantVal; }
78};
79
80} // end anonymous namespace
81
82
83//===----------------------------------------------------------------------===//
84// SCCP Class
85//
86// This class does all of the work of Sparse Conditional Constant Propagation.
87//
88namespace {
89class SCCP : public FunctionPass, public InstVisitor<SCCP> {
90  std::set<BasicBlock*>     BBExecutable;// The basic blocks that are executable
91  std::map<Value*, InstVal> ValueState;  // The state each value is in...
92
93  std::vector<Instruction*> InstWorkList;// The instruction work list
94  std::vector<BasicBlock*>  BBWorkList;  // The BasicBlock work list
95
96  /// KnownFeasibleEdges - Entries in this set are edges which have already had
97  /// PHI nodes retriggered.
98  typedef std::pair<BasicBlock*,BasicBlock*> Edge;
99  std::set<Edge> KnownFeasibleEdges;
100public:
101
102  // runOnFunction - Run the Sparse Conditional Constant Propagation algorithm,
103  // and return true if the function was modified.
104  //
105  bool runOnFunction(Function &F);
106
107  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108    AU.setPreservesCFG();
109  }
110
111
112  //===--------------------------------------------------------------------===//
113  // The implementation of this class
114  //
115private:
116  friend class InstVisitor<SCCP>;        // Allow callbacks from visitor
117
118  // markValueOverdefined - Make a value be marked as "constant".  If the value
119  // is not already a constant, add it to the instruction work list so that
120  // the users of the instruction are updated later.
121  //
122  inline void markConstant(InstVal &IV, Instruction *I, Constant *C) {
123    if (IV.markConstant(C)) {
124      DEBUG(std::cerr << "markConstant: " << *C << ": " << *I);
125      InstWorkList.push_back(I);
126    }
127  }
128  inline void markConstant(Instruction *I, Constant *C) {
129    markConstant(ValueState[I], I, C);
130  }
131
132  // markValueOverdefined - Make a value be marked as "overdefined". If the
133  // value is not already overdefined, add it to the instruction work list so
134  // that the users of the instruction are updated later.
135  //
136  inline void markOverdefined(InstVal &IV, Instruction *I) {
137    if (IV.markOverdefined()) {
138      DEBUG(std::cerr << "markOverdefined: " << *I);
139      InstWorkList.push_back(I);  // Only instructions go on the work list
140    }
141  }
142  inline void markOverdefined(Instruction *I) {
143    markOverdefined(ValueState[I], I);
144  }
145
146  // getValueState - Return the InstVal object that corresponds to the value.
147  // This function is necessary because not all values should start out in the
148  // underdefined state... Argument's should be overdefined, and
149  // constants should be marked as constants.  If a value is not known to be an
150  // Instruction object, then use this accessor to get its value from the map.
151  //
152  inline InstVal &getValueState(Value *V) {
153    std::map<Value*, InstVal>::iterator I = ValueState.find(V);
154    if (I != ValueState.end()) return I->second;  // Common case, in the map
155
156    if (Constant *CPV = dyn_cast<Constant>(V)) {  // Constants are constant
157      ValueState[CPV].markConstant(CPV);
158    } else if (isa<Argument>(V)) {                // Arguments are overdefined
159      ValueState[V].markOverdefined();
160    } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
161      // The address of a global is a constant...
162      ValueState[V].markConstant(ConstantPointerRef::get(GV));
163    }
164    // All others are underdefined by default...
165    return ValueState[V];
166  }
167
168  // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
169  // work list if it is not already executable...
170  //
171  void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
172    if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
173      return;  // This edge is already known to be executable!
174
175    if (BBExecutable.count(Dest)) {
176      DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName()
177                      << " -> " << Dest->getName() << "\n");
178
179      // The destination is already executable, but we just made an edge
180      // feasible that wasn't before.  Revisit the PHI nodes in the block
181      // because they have potentially new operands.
182      for (BasicBlock::iterator I = Dest->begin();
183           PHINode *PN = dyn_cast<PHINode>(I); ++I)
184        visitPHINode(*PN);
185
186    } else {
187      DEBUG(std::cerr << "Marking Block Executable: " << Dest->getName()<<"\n");
188      BBExecutable.insert(Dest);   // Basic block is executable!
189      BBWorkList.push_back(Dest);  // Add the block to the work list!
190    }
191  }
192
193
194  // visit implementations - Something changed in this instruction... Either an
195  // operand made a transition, or the instruction is newly executable.  Change
196  // the value type of I to reflect these changes if appropriate.
197  //
198  void visitPHINode(PHINode &I);
199
200  // Terminators
201  void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
202  void visitTerminatorInst(TerminatorInst &TI);
203
204  void visitCastInst(CastInst &I);
205  void visitBinaryOperator(Instruction &I);
206  void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
207
208  // Instructions that cannot be folded away...
209  void visitStoreInst     (Instruction &I) { /*returns void*/ }
210  void visitLoadInst      (Instruction &I) { markOverdefined(&I); }
211  void visitGetElementPtrInst(GetElementPtrInst &I);
212  void visitCallInst      (Instruction &I) { markOverdefined(&I); }
213  void visitInvokeInst    (TerminatorInst &I) {
214    if (I.getType() != Type::VoidTy) markOverdefined(&I);
215    visitTerminatorInst(I);
216  }
217  void visitUnwindInst    (TerminatorInst &I) { /*returns void*/ }
218  void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
219  void visitVANextInst    (Instruction &I) { markOverdefined(&I); }
220  void visitVAArgInst     (Instruction &I) { markOverdefined(&I); }
221  void visitFreeInst      (Instruction &I) { /*returns void*/ }
222
223  void visitInstruction(Instruction &I) {
224    // If a new instruction is added to LLVM that we don't handle...
225    std::cerr << "SCCP: Don't know how to handle: " << I;
226    markOverdefined(&I);   // Just in case
227  }
228
229  // getFeasibleSuccessors - Return a vector of booleans to indicate which
230  // successors are reachable from a given terminator instruction.
231  //
232  void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
233
234  // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
235  // block to the 'To' basic block is currently feasible...
236  //
237  bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
238
239  // OperandChangedState - This method is invoked on all of the users of an
240  // instruction that was just changed state somehow....  Based on this
241  // information, we need to update the specified user of this instruction.
242  //
243  void OperandChangedState(User *U) {
244    // Only instructions use other variable values!
245    Instruction &I = cast<Instruction>(*U);
246    if (BBExecutable.count(I.getParent()))   // Inst is executable?
247      visit(I);
248  }
249};
250
251  RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
252} // end anonymous namespace
253
254
255// createSCCPPass - This is the public interface to this file...
256//
257Pass *createSCCPPass() {
258  return new SCCP();
259}
260
261
262//===----------------------------------------------------------------------===//
263// SCCP Class Implementation
264
265
266// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
267// and return true if the function was modified.
268//
269bool SCCP::runOnFunction(Function &F) {
270  // Mark the first block of the function as being executable...
271  BBExecutable.insert(F.begin());   // Basic block is executable!
272  BBWorkList.push_back(F.begin());  // Add the block to the work list!
273
274  // Process the work lists until their are empty!
275  while (!BBWorkList.empty() || !InstWorkList.empty()) {
276    // Process the instruction work list...
277    while (!InstWorkList.empty()) {
278      Instruction *I = InstWorkList.back();
279      InstWorkList.pop_back();
280
281      DEBUG(std::cerr << "\nPopped off I-WL: " << I);
282
283      // "I" got into the work list because it either made the transition from
284      // bottom to constant, or to Overdefined.
285      //
286      // Update all of the users of this instruction's value...
287      //
288      for_each(I->use_begin(), I->use_end(),
289	       bind_obj(this, &SCCP::OperandChangedState));
290    }
291
292    // Process the basic block work list...
293    while (!BBWorkList.empty()) {
294      BasicBlock *BB = BBWorkList.back();
295      BBWorkList.pop_back();
296
297      DEBUG(std::cerr << "\nPopped off BBWL: " << BB);
298
299      // Notify all instructions in this basic block that they are newly
300      // executable.
301      visit(BB);
302    }
303  }
304
305  if (DebugFlag) {
306    for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
307      if (!BBExecutable.count(I))
308        std::cerr << "BasicBlock Dead:" << *I;
309  }
310
311  // Iterate over all of the instructions in a function, replacing them with
312  // constants if we have found them to be of constant values.
313  //
314  bool MadeChanges = false;
315  for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
316    for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
317      Instruction &Inst = *BI;
318      InstVal &IV = ValueState[&Inst];
319      if (IV.isConstant()) {
320        Constant *Const = IV.getConstant();
321        DEBUG(std::cerr << "Constant: " << Const << " = " << Inst);
322
323        // Replaces all of the uses of a variable with uses of the constant.
324        Inst.replaceAllUsesWith(Const);
325
326        // Remove the operator from the list of definitions... and delete it.
327        BI = BB->getInstList().erase(BI);
328
329        // Hey, we just changed something!
330        MadeChanges = true;
331        ++NumInstRemoved;
332      } else {
333        ++BI;
334      }
335    }
336
337  // Reset state so that the next invocation will have empty data structures
338  BBExecutable.clear();
339  ValueState.clear();
340  std::vector<Instruction*>().swap(InstWorkList);
341  std::vector<BasicBlock*>().swap(BBWorkList);
342
343  return MadeChanges;
344}
345
346
347// getFeasibleSuccessors - Return a vector of booleans to indicate which
348// successors are reachable from a given terminator instruction.
349//
350void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
351  Succs.resize(TI.getNumSuccessors());
352  if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
353    if (BI->isUnconditional()) {
354      Succs[0] = true;
355    } else {
356      InstVal &BCValue = getValueState(BI->getCondition());
357      if (BCValue.isOverdefined()) {
358        // Overdefined condition variables mean the branch could go either way.
359        Succs[0] = Succs[1] = true;
360      } else if (BCValue.isConstant()) {
361        // Constant condition variables mean the branch can only go a single way
362        Succs[BCValue.getConstant() == ConstantBool::False] = true;
363      }
364    }
365  } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
366    // Invoke instructions successors are always executable.
367    Succs[0] = Succs[1] = true;
368  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
369    InstVal &SCValue = getValueState(SI->getCondition());
370    if (SCValue.isOverdefined()) {  // Overdefined condition?
371      // All destinations are executable!
372      Succs.assign(TI.getNumSuccessors(), true);
373    } else if (SCValue.isConstant()) {
374      Constant *CPV = SCValue.getConstant();
375      // Make sure to skip the "default value" which isn't a value
376      for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
377        if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
378          Succs[i] = true;
379          return;
380        }
381      }
382
383      // Constant value not equal to any of the branches... must execute
384      // default branch then...
385      Succs[0] = true;
386    }
387  } else {
388    std::cerr << "SCCP: Don't know how to handle: " << TI;
389    Succs.assign(TI.getNumSuccessors(), true);
390  }
391}
392
393
394// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
395// block to the 'To' basic block is currently feasible...
396//
397bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
398  assert(BBExecutable.count(To) && "Dest should always be alive!");
399
400  // Make sure the source basic block is executable!!
401  if (!BBExecutable.count(From)) return false;
402
403  // Check to make sure this edge itself is actually feasible now...
404  TerminatorInst *TI = From->getTerminator();
405  if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
406    if (BI->isUnconditional())
407      return true;
408    else {
409      InstVal &BCValue = getValueState(BI->getCondition());
410      if (BCValue.isOverdefined()) {
411        // Overdefined condition variables mean the branch could go either way.
412        return true;
413      } else if (BCValue.isConstant()) {
414        // Constant condition variables mean the branch can only go a single way
415        return BI->getSuccessor(BCValue.getConstant() ==
416                                       ConstantBool::False) == To;
417      }
418      return false;
419    }
420  } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
421    // Invoke instructions successors are always executable.
422    return true;
423  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
424    InstVal &SCValue = getValueState(SI->getCondition());
425    if (SCValue.isOverdefined()) {  // Overdefined condition?
426      // All destinations are executable!
427      return true;
428    } else if (SCValue.isConstant()) {
429      Constant *CPV = SCValue.getConstant();
430      // Make sure to skip the "default value" which isn't a value
431      for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
432        if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
433          return SI->getSuccessor(i) == To;
434
435      // Constant value not equal to any of the branches... must execute
436      // default branch then...
437      return SI->getDefaultDest() == To;
438    }
439    return false;
440  } else {
441    std::cerr << "Unknown terminator instruction: " << *TI;
442    abort();
443  }
444}
445
446// visit Implementations - Something changed in this instruction... Either an
447// operand made a transition, or the instruction is newly executable.  Change
448// the value type of I to reflect these changes if appropriate.  This method
449// makes sure to do the following actions:
450//
451// 1. If a phi node merges two constants in, and has conflicting value coming
452//    from different branches, or if the PHI node merges in an overdefined
453//    value, then the PHI node becomes overdefined.
454// 2. If a phi node merges only constants in, and they all agree on value, the
455//    PHI node becomes a constant value equal to that.
456// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
457// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
458// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
459// 6. If a conditional branch has a value that is constant, make the selected
460//    destination executable
461// 7. If a conditional branch has a value that is overdefined, make all
462//    successors executable.
463//
464void SCCP::visitPHINode(PHINode &PN) {
465  InstVal &PNIV = getValueState(&PN);
466  if (PNIV.isOverdefined()) return;  // Quick exit
467
468  // Look at all of the executable operands of the PHI node.  If any of them
469  // are overdefined, the PHI becomes overdefined as well.  If they are all
470  // constant, and they agree with each other, the PHI becomes the identical
471  // constant.  If they are constant and don't agree, the PHI is overdefined.
472  // If there are no executable operands, the PHI remains undefined.
473  //
474  Constant *OperandVal = 0;
475  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
476    InstVal &IV = getValueState(PN.getIncomingValue(i));
477    if (IV.isUndefined()) continue;  // Doesn't influence PHI node.
478
479    if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
480      if (IV.isOverdefined()) {   // PHI node becomes overdefined!
481        markOverdefined(PNIV, &PN);
482        return;
483      }
484
485      if (OperandVal == 0) {   // Grab the first value...
486        OperandVal = IV.getConstant();
487      } else {                // Another value is being merged in!
488        // There is already a reachable operand.  If we conflict with it,
489        // then the PHI node becomes overdefined.  If we agree with it, we
490        // can continue on.
491
492        // Check to see if there are two different constants merging...
493        if (IV.getConstant() != OperandVal) {
494          // Yes there is.  This means the PHI node is not constant.
495          // You must be overdefined poor PHI.
496          //
497          markOverdefined(PNIV, &PN);    // The PHI node now becomes overdefined
498          return;    // I'm done analyzing you
499        }
500      }
501    }
502  }
503
504  // If we exited the loop, this means that the PHI node only has constant
505  // arguments that agree with each other(and OperandVal is the constant) or
506  // OperandVal is null because there are no defined incoming arguments.  If
507  // this is the case, the PHI remains undefined.
508  //
509  if (OperandVal)
510    markConstant(PNIV, &PN, OperandVal);      // Acquire operand value
511}
512
513void SCCP::visitTerminatorInst(TerminatorInst &TI) {
514  std::vector<bool> SuccFeasible;
515  getFeasibleSuccessors(TI, SuccFeasible);
516
517  BasicBlock *BB = TI.getParent();
518
519  // Mark all feasible successors executable...
520  for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
521    if (SuccFeasible[i])
522      markEdgeExecutable(BB, TI.getSuccessor(i));
523}
524
525void SCCP::visitCastInst(CastInst &I) {
526  Value *V = I.getOperand(0);
527  InstVal &VState = getValueState(V);
528  if (VState.isOverdefined()) {        // Inherit overdefinedness of operand
529    markOverdefined(&I);
530  } else if (VState.isConstant()) {    // Propagate constant value
531    Constant *Result =
532      ConstantFoldCastInstruction(VState.getConstant(), I.getType());
533
534    if (Result)   // If this instruction constant folds!
535      markConstant(&I, Result);
536    else
537      markOverdefined(&I);   // Don't know how to fold this instruction.  :(
538  }
539}
540
541// Handle BinaryOperators and Shift Instructions...
542void SCCP::visitBinaryOperator(Instruction &I) {
543  InstVal &V1State = getValueState(I.getOperand(0));
544  InstVal &V2State = getValueState(I.getOperand(1));
545  if (V1State.isOverdefined() || V2State.isOverdefined()) {
546    markOverdefined(&I);
547  } else if (V1State.isConstant() && V2State.isConstant()) {
548    Constant *Result = 0;
549    if (isa<BinaryOperator>(I))
550      Result = ConstantFoldBinaryInstruction(I.getOpcode(),
551                                             V1State.getConstant(),
552                                             V2State.getConstant());
553    else if (isa<ShiftInst>(I))
554      Result = ConstantFoldShiftInstruction(I.getOpcode(),
555                                            V1State.getConstant(),
556                                            V2State.getConstant());
557    if (Result)
558      markConstant(&I, Result);      // This instruction constant folds!
559    else
560      markOverdefined(&I);   // Don't know how to fold this instruction.  :(
561  }
562}
563
564// Handle getelementptr instructions... if all operands are constants then we
565// can turn this into a getelementptr ConstantExpr.
566//
567void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) {
568  std::vector<Constant*> Operands;
569  Operands.reserve(I.getNumOperands());
570
571  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
572    InstVal &State = getValueState(I.getOperand(i));
573    if (State.isUndefined())
574      return;  // Operands are not resolved yet...
575    else if (State.isOverdefined()) {
576      markOverdefined(&I);
577      return;
578    }
579    assert(State.isConstant() && "Unknown state!");
580    Operands.push_back(State.getConstant());
581  }
582
583  Constant *Ptr = Operands[0];
584  Operands.erase(Operands.begin());  // Erase the pointer from idx list...
585
586  markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Operands));
587}
588