1 //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements sparse conditional constant propagation and merging:
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
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.
22 //===----------------------------------------------------------------------===//
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"
37 // InstVal class - This class represents the different lattice values that an
38 // instruction may occupy. It is a simple class with value semantics.
41 Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
45 undefined, // This instruction has no known value
46 constant, // This instruction has a constant value
47 overdefined // This instruction has an unknown value
48 } LatticeValue; // The current lattice position
49 Constant *ConstantVal; // If Constant value, the current value
51 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
53 // markOverdefined - Return true if this is a new status to be in...
54 inline bool markOverdefined() {
55 if (LatticeValue != overdefined) {
56 LatticeValue = overdefined;
62 // markConstant - Return true if this is a new status for us...
63 inline bool markConstant(Constant *V) {
64 if (LatticeValue != constant) {
65 LatticeValue = constant;
69 assert(ConstantVal == V && "Marking constant with different value");
74 inline bool isUndefined() const { return LatticeValue == undefined; }
75 inline bool isConstant() const { return LatticeValue == constant; }
76 inline bool isOverdefined() const { return LatticeValue == overdefined; }
78 inline Constant *getConstant() const {
79 assert(isConstant() && "Cannot get the constant of a non-constant!");
84 } // end anonymous namespace
87 //===----------------------------------------------------------------------===//
90 // This class does all of the work of Sparse Conditional Constant Propagation.
93 class SCCP : public FunctionPass, public InstVisitor<SCCP> {
94 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
95 std::map<Value*, InstVal> ValueState; // The state each value is in...
97 std::vector<Instruction*> InstWorkList;// The instruction work list
98 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
100 /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not
101 /// overdefined, despite the fact that the PHI node is overdefined.
102 std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs;
104 /// KnownFeasibleEdges - Entries in this set are edges which have already had
105 /// PHI nodes retriggered.
106 typedef std::pair<BasicBlock*,BasicBlock*> Edge;
107 std::set<Edge> KnownFeasibleEdges;
110 // runOnFunction - Run the Sparse Conditional Constant Propagation algorithm,
111 // and return true if the function was modified.
113 bool runOnFunction(Function &F);
115 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
116 AU.setPreservesCFG();
120 //===--------------------------------------------------------------------===//
121 // The implementation of this class
124 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
126 // markValueOverdefined - Make a value be marked as "constant". If the value
127 // is not already a constant, add it to the instruction work list so that
128 // the users of the instruction are updated later.
130 inline void markConstant(InstVal &IV, Instruction *I, Constant *C) {
131 if (IV.markConstant(C)) {
132 DEBUG(std::cerr << "markConstant: " << *C << ": " << *I);
133 InstWorkList.push_back(I);
136 inline void markConstant(Instruction *I, Constant *C) {
137 markConstant(ValueState[I], I, C);
140 // markValueOverdefined - Make a value be marked as "overdefined". If the
141 // value is not already overdefined, add it to the instruction work list so
142 // that the users of the instruction are updated later.
144 inline void markOverdefined(InstVal &IV, Instruction *I) {
145 if (IV.markOverdefined()) {
146 DEBUG(std::cerr << "markOverdefined: " << *I);
147 InstWorkList.push_back(I); // Only instructions go on the work list
150 inline void markOverdefined(Instruction *I) {
151 markOverdefined(ValueState[I], I);
154 // getValueState - Return the InstVal object that corresponds to the value.
155 // This function is necessary because not all values should start out in the
156 // underdefined state... Argument's should be overdefined, and
157 // constants should be marked as constants. If a value is not known to be an
158 // Instruction object, then use this accessor to get its value from the map.
160 inline InstVal &getValueState(Value *V) {
161 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
162 if (I != ValueState.end()) return I->second; // Common case, in the map
164 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
165 ValueState[CPV].markConstant(CPV);
166 } else if (isa<Argument>(V)) { // Arguments are overdefined
167 ValueState[V].markOverdefined();
168 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
169 // The address of a global is a constant...
170 ValueState[V].markConstant(ConstantPointerRef::get(GV));
172 // All others are underdefined by default...
173 return ValueState[V];
176 // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
177 // work list if it is not already executable...
179 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
180 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
181 return; // This edge is already known to be executable!
183 if (BBExecutable.count(Dest)) {
184 DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName()
185 << " -> " << Dest->getName() << "\n");
187 // The destination is already executable, but we just made an edge
188 // feasible that wasn't before. Revisit the PHI nodes in the block
189 // because they have potentially new operands.
190 for (BasicBlock::iterator I = Dest->begin();
191 PHINode *PN = dyn_cast<PHINode>(I); ++I)
195 DEBUG(std::cerr << "Marking Block Executable: " << Dest->getName()<<"\n");
196 BBExecutable.insert(Dest); // Basic block is executable!
197 BBWorkList.push_back(Dest); // Add the block to the work list!
202 // visit implementations - Something changed in this instruction... Either an
203 // operand made a transition, or the instruction is newly executable. Change
204 // the value type of I to reflect these changes if appropriate.
206 void visitPHINode(PHINode &I);
209 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
210 void visitTerminatorInst(TerminatorInst &TI);
212 void visitCastInst(CastInst &I);
213 void visitBinaryOperator(Instruction &I);
214 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
216 // Instructions that cannot be folded away...
217 void visitStoreInst (Instruction &I) { /*returns void*/ }
218 void visitLoadInst (Instruction &I) { markOverdefined(&I); }
219 void visitGetElementPtrInst(GetElementPtrInst &I);
220 void visitCallInst (Instruction &I) { markOverdefined(&I); }
221 void visitInvokeInst (TerminatorInst &I) {
222 if (I.getType() != Type::VoidTy) markOverdefined(&I);
223 visitTerminatorInst(I);
225 void visitUnwindInst (TerminatorInst &I) { /*returns void*/ }
226 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
227 void visitVANextInst (Instruction &I) { markOverdefined(&I); }
228 void visitVAArgInst (Instruction &I) { markOverdefined(&I); }
229 void visitFreeInst (Instruction &I) { /*returns void*/ }
231 void visitInstruction(Instruction &I) {
232 // If a new instruction is added to LLVM that we don't handle...
233 std::cerr << "SCCP: Don't know how to handle: " << I;
234 markOverdefined(&I); // Just in case
237 // getFeasibleSuccessors - Return a vector of booleans to indicate which
238 // successors are reachable from a given terminator instruction.
240 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
242 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
243 // block to the 'To' basic block is currently feasible...
245 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
247 // OperandChangedState - This method is invoked on all of the users of an
248 // instruction that was just changed state somehow.... Based on this
249 // information, we need to update the specified user of this instruction.
251 void OperandChangedState(User *U) {
252 // Only instructions use other variable values!
253 Instruction &I = cast<Instruction>(*U);
254 if (BBExecutable.count(I.getParent())) // Inst is executable?
259 RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
260 } // end anonymous namespace
263 // createSCCPPass - This is the public interface to this file...
264 Pass *llvm::createSCCPPass() {
269 //===----------------------------------------------------------------------===//
270 // SCCP Class Implementation
273 // runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
274 // and return true if the function was modified.
276 bool SCCP::runOnFunction(Function &F) {
277 // Mark the first block of the function as being executable...
278 BBExecutable.insert(F.begin()); // Basic block is executable!
279 BBWorkList.push_back(F.begin()); // Add the block to the work list!
281 // Process the work lists until their are empty!
282 while (!BBWorkList.empty() || !InstWorkList.empty()) {
283 // Process the instruction work list...
284 while (!InstWorkList.empty()) {
285 Instruction *I = InstWorkList.back();
286 InstWorkList.pop_back();
288 DEBUG(std::cerr << "\nPopped off I-WL: " << I);
290 // "I" got into the work list because it either made the transition from
291 // bottom to constant, or to Overdefined.
293 // Update all of the users of this instruction's value...
295 for_each(I->use_begin(), I->use_end(),
296 bind_obj(this, &SCCP::OperandChangedState));
299 // Process the basic block work list...
300 while (!BBWorkList.empty()) {
301 BasicBlock *BB = BBWorkList.back();
302 BBWorkList.pop_back();
304 DEBUG(std::cerr << "\nPopped off BBWL: " << BB);
306 // Notify all instructions in this basic block that they are newly
313 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
314 if (!BBExecutable.count(I))
315 std::cerr << "BasicBlock Dead:" << *I;
318 // Iterate over all of the instructions in a function, replacing them with
319 // constants if we have found them to be of constant values.
321 bool MadeChanges = false;
322 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
323 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
324 Instruction &Inst = *BI;
325 InstVal &IV = ValueState[&Inst];
326 if (IV.isConstant()) {
327 Constant *Const = IV.getConstant();
328 DEBUG(std::cerr << "Constant: " << Const << " = " << Inst);
330 // Replaces all of the uses of a variable with uses of the constant.
331 Inst.replaceAllUsesWith(Const);
333 // Remove the operator from the list of definitions... and delete it.
334 BI = BB->getInstList().erase(BI);
336 // Hey, we just changed something!
344 // Reset state so that the next invocation will have empty data structures
345 BBExecutable.clear();
347 std::vector<Instruction*>().swap(InstWorkList);
348 std::vector<BasicBlock*>().swap(BBWorkList);
354 // getFeasibleSuccessors - Return a vector of booleans to indicate which
355 // successors are reachable from a given terminator instruction.
357 void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
358 Succs.resize(TI.getNumSuccessors());
359 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
360 if (BI->isUnconditional()) {
363 InstVal &BCValue = getValueState(BI->getCondition());
364 if (BCValue.isOverdefined()) {
365 // Overdefined condition variables mean the branch could go either way.
366 Succs[0] = Succs[1] = true;
367 } else if (BCValue.isConstant()) {
368 // Constant condition variables mean the branch can only go a single way
369 Succs[BCValue.getConstant() == ConstantBool::False] = true;
372 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
373 // Invoke instructions successors are always executable.
374 Succs[0] = Succs[1] = true;
375 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
376 InstVal &SCValue = getValueState(SI->getCondition());
377 if (SCValue.isOverdefined()) { // Overdefined condition?
378 // All destinations are executable!
379 Succs.assign(TI.getNumSuccessors(), true);
380 } else if (SCValue.isConstant()) {
381 Constant *CPV = SCValue.getConstant();
382 // Make sure to skip the "default value" which isn't a value
383 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
384 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
390 // Constant value not equal to any of the branches... must execute
391 // default branch then...
395 std::cerr << "SCCP: Don't know how to handle: " << TI;
396 Succs.assign(TI.getNumSuccessors(), true);
401 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
402 // block to the 'To' basic block is currently feasible...
404 bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
405 assert(BBExecutable.count(To) && "Dest should always be alive!");
407 // Make sure the source basic block is executable!!
408 if (!BBExecutable.count(From)) return false;
410 // Check to make sure this edge itself is actually feasible now...
411 TerminatorInst *TI = From->getTerminator();
412 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
413 if (BI->isUnconditional())
416 InstVal &BCValue = getValueState(BI->getCondition());
417 if (BCValue.isOverdefined()) {
418 // Overdefined condition variables mean the branch could go either way.
420 } else if (BCValue.isConstant()) {
421 // Constant condition variables mean the branch can only go a single way
422 return BI->getSuccessor(BCValue.getConstant() ==
423 ConstantBool::False) == To;
427 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
428 // Invoke instructions successors are always executable.
430 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
431 InstVal &SCValue = getValueState(SI->getCondition());
432 if (SCValue.isOverdefined()) { // Overdefined condition?
433 // All destinations are executable!
435 } else if (SCValue.isConstant()) {
436 Constant *CPV = SCValue.getConstant();
437 // Make sure to skip the "default value" which isn't a value
438 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
439 if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
440 return SI->getSuccessor(i) == To;
442 // Constant value not equal to any of the branches... must execute
443 // default branch then...
444 return SI->getDefaultDest() == To;
448 std::cerr << "Unknown terminator instruction: " << *TI;
453 // visit Implementations - Something changed in this instruction... Either an
454 // operand made a transition, or the instruction is newly executable. Change
455 // the value type of I to reflect these changes if appropriate. This method
456 // makes sure to do the following actions:
458 // 1. If a phi node merges two constants in, and has conflicting value coming
459 // from different branches, or if the PHI node merges in an overdefined
460 // value, then the PHI node becomes overdefined.
461 // 2. If a phi node merges only constants in, and they all agree on value, the
462 // PHI node becomes a constant value equal to that.
463 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
464 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
465 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
466 // 6. If a conditional branch has a value that is constant, make the selected
467 // destination executable
468 // 7. If a conditional branch has a value that is overdefined, make all
469 // successors executable.
471 void SCCP::visitPHINode(PHINode &PN) {
472 InstVal &PNIV = getValueState(&PN);
473 if (PNIV.isOverdefined()) {
474 // There may be instructions using this PHI node that are not overdefined
475 // themselves. If so, make sure that they know that the PHI node operand
477 std::multimap<PHINode*, Instruction*>::iterator I, E;
478 tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN);
480 std::vector<Instruction*> Users;
481 Users.reserve(std::distance(I, E));
482 for (; I != E; ++I) Users.push_back(I->second);
483 while (!Users.empty()) {
488 return; // Quick exit
491 // Look at all of the executable operands of the PHI node. If any of them
492 // are overdefined, the PHI becomes overdefined as well. If they are all
493 // constant, and they agree with each other, the PHI becomes the identical
494 // constant. If they are constant and don't agree, the PHI is overdefined.
495 // If there are no executable operands, the PHI remains undefined.
497 Constant *OperandVal = 0;
498 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
499 InstVal &IV = getValueState(PN.getIncomingValue(i));
500 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
502 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
503 if (IV.isOverdefined()) { // PHI node becomes overdefined!
504 markOverdefined(PNIV, &PN);
508 if (OperandVal == 0) { // Grab the first value...
509 OperandVal = IV.getConstant();
510 } else { // Another value is being merged in!
511 // There is already a reachable operand. If we conflict with it,
512 // then the PHI node becomes overdefined. If we agree with it, we
515 // Check to see if there are two different constants merging...
516 if (IV.getConstant() != OperandVal) {
517 // Yes there is. This means the PHI node is not constant.
518 // You must be overdefined poor PHI.
520 markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined
521 return; // I'm done analyzing you
527 // If we exited the loop, this means that the PHI node only has constant
528 // arguments that agree with each other(and OperandVal is the constant) or
529 // OperandVal is null because there are no defined incoming arguments. If
530 // this is the case, the PHI remains undefined.
533 markConstant(PNIV, &PN, OperandVal); // Acquire operand value
536 void SCCP::visitTerminatorInst(TerminatorInst &TI) {
537 std::vector<bool> SuccFeasible;
538 getFeasibleSuccessors(TI, SuccFeasible);
540 BasicBlock *BB = TI.getParent();
542 // Mark all feasible successors executable...
543 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
545 markEdgeExecutable(BB, TI.getSuccessor(i));
548 void SCCP::visitCastInst(CastInst &I) {
549 Value *V = I.getOperand(0);
550 InstVal &VState = getValueState(V);
551 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
553 } else if (VState.isConstant()) { // Propagate constant value
555 ConstantFoldCastInstruction(VState.getConstant(), I.getType());
557 if (Result) // If this instruction constant folds!
558 markConstant(&I, Result);
560 markOverdefined(&I); // Don't know how to fold this instruction. :(
564 // Handle BinaryOperators and Shift Instructions...
565 void SCCP::visitBinaryOperator(Instruction &I) {
566 InstVal &IV = ValueState[&I];
567 if (IV.isOverdefined()) return;
569 InstVal &V1State = getValueState(I.getOperand(0));
570 InstVal &V2State = getValueState(I.getOperand(1));
572 if (V1State.isOverdefined() || V2State.isOverdefined()) {
573 // If both operands are PHI nodes, it is possible that this instruction has
574 // a constant value, despite the fact that the PHI node doesn't. Check for
575 // this condition now.
576 if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
577 if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
578 if (PN1->getParent() == PN2->getParent()) {
579 // Since the two PHI nodes are in the same basic block, they must have
580 // entries for the same predecessors. Walk the predecessor list, and
581 // if all of the incoming values are constants, and the result of
582 // evaluating this expression with all incoming value pairs is the
583 // same, then this expression is a constant even though the PHI node
584 // is not a constant!
586 for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
587 InstVal &In1 = getValueState(PN1->getIncomingValue(i));
588 BasicBlock *InBlock = PN1->getIncomingBlock(i);
589 InstVal &In2 =getValueState(PN2->getIncomingValueForBlock(InBlock));
591 if (In1.isOverdefined() || In2.isOverdefined()) {
592 Result.markOverdefined();
593 break; // Cannot fold this operation over the PHI nodes!
594 } else if (In1.isConstant() && In2.isConstant()) {
596 if (isa<BinaryOperator>(I))
597 Val = ConstantExpr::get(I.getOpcode(), In1.getConstant(),
600 assert(isa<ShiftInst>(I) &&
601 "Can only handle binops and shifts here!");
602 Val = ConstantExpr::getShift(I.getOpcode(), In1.getConstant(),
605 if (Result.isUndefined())
606 Result.markConstant(Val);
607 else if (Result.isConstant() && Result.getConstant() != Val) {
608 Result.markOverdefined();
614 // If we found a constant value here, then we know the instruction is
615 // constant despite the fact that the PHI nodes are overdefined.
616 if (Result.isConstant()) {
617 markConstant(IV, &I, Result.getConstant());
618 // Remember that this instruction is virtually using the PHI node
620 UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
621 UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
623 } else if (Result.isUndefined()) {
627 // Okay, this really is overdefined now. Since we might have
628 // speculatively thought that this was not overdefined before, and
629 // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
630 // make sure to clean out any entries that we put there, for
632 std::multimap<PHINode*, Instruction*>::iterator It, E;
633 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
635 if (It->second == &I) {
636 UsersOfOverdefinedPHIs.erase(It++);
640 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
642 if (It->second == &I) {
643 UsersOfOverdefinedPHIs.erase(It++);
649 markOverdefined(IV, &I);
650 } else if (V1State.isConstant() && V2State.isConstant()) {
651 Constant *Result = 0;
652 if (isa<BinaryOperator>(I))
653 Result = ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
654 V2State.getConstant());
656 assert (isa<ShiftInst>(I) && "Can only handle binops and shifts here!");
657 Result = ConstantExpr::getShift(I.getOpcode(), V1State.getConstant(),
658 V2State.getConstant());
661 markConstant(IV, &I, Result); // This instruction constant folds!
665 // Handle getelementptr instructions... if all operands are constants then we
666 // can turn this into a getelementptr ConstantExpr.
668 void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) {
669 std::vector<Constant*> Operands;
670 Operands.reserve(I.getNumOperands());
672 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
673 InstVal &State = getValueState(I.getOperand(i));
674 if (State.isUndefined())
675 return; // Operands are not resolved yet...
676 else if (State.isOverdefined()) {
680 assert(State.isConstant() && "Unknown state!");
681 Operands.push_back(State.getConstant());
684 Constant *Ptr = Operands[0];
685 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
687 markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Operands));