1 //===-- Local.cpp - Functions to perform local transformations ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions perform various local transformations to the
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Utils/Local.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/Analysis/ConstantFolding.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Support/GetElementPtrTypeIterator.h"
24 #include "llvm/Support/MathExtras.h"
28 //===----------------------------------------------------------------------===//
29 // Local constant propagation...
32 /// doConstantPropagation - If an instruction references constants, try to fold
35 bool llvm::doConstantPropagation(BasicBlock::iterator &II,
36 const TargetData *TD) {
37 if (Constant *C = ConstantFoldInstruction(II, TD)) {
38 // Replaces all of the uses of a variable with uses of the constant.
39 II->replaceAllUsesWith(C);
41 // Remove the instruction from the basic block...
42 II = II->getParent()->getInstList().erase(II);
49 // ConstantFoldTerminator - If a terminator instruction is predicated on a
50 // constant value, convert it into an unconditional branch to the constant
53 bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
54 TerminatorInst *T = BB->getTerminator();
56 // Branch - See if we are conditional jumping on constant
57 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
58 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
59 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
60 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
62 if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
63 // Are we branching on constant?
64 // YES. Change to unconditional branch...
65 BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
66 BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
68 //cerr << "Function: " << T->getParent()->getParent()
69 // << "\nRemoving branch from " << T->getParent()
70 // << "\n\nTo: " << OldDest << endl;
72 // Let the basic block know that we are letting go of it. Based on this,
73 // it will adjust it's PHI nodes.
74 assert(BI->getParent() && "Terminator not inserted in block!");
75 OldDest->removePredecessor(BI->getParent());
77 // Set the unconditional destination, and change the insn to be an
78 // unconditional branch.
79 BI->setUnconditionalDest(Destination);
81 } else if (Dest2 == Dest1) { // Conditional branch to same location?
82 // This branch matches something like this:
83 // br bool %cond, label %Dest, label %Dest
84 // and changes it into: br label %Dest
86 // Let the basic block know that we are letting go of one copy of it.
87 assert(BI->getParent() && "Terminator not inserted in block!");
88 Dest1->removePredecessor(BI->getParent());
90 // Change a conditional branch to unconditional.
91 BI->setUnconditionalDest(Dest1);
94 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
95 // If we are switching on a constant, we can convert the switch into a
96 // single branch instruction!
97 ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
98 BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest
99 BasicBlock *DefaultDest = TheOnlyDest;
100 assert(TheOnlyDest == SI->getDefaultDest() &&
101 "Default destination is not successor #0?");
103 // Figure out which case it goes to...
104 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
105 // Found case matching a constant operand?
106 if (SI->getSuccessorValue(i) == CI) {
107 TheOnlyDest = SI->getSuccessor(i);
111 // Check to see if this branch is going to the same place as the default
112 // dest. If so, eliminate it as an explicit compare.
113 if (SI->getSuccessor(i) == DefaultDest) {
114 // Remove this entry...
115 DefaultDest->removePredecessor(SI->getParent());
117 --i; --e; // Don't skip an entry...
121 // Otherwise, check to see if the switch only branches to one destination.
122 // We do this by reseting "TheOnlyDest" to null when we find two non-equal
124 if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
127 if (CI && !TheOnlyDest) {
128 // Branching on a constant, but not any of the cases, go to the default
130 TheOnlyDest = SI->getDefaultDest();
133 // If we found a single destination that we can fold the switch into, do so
136 // Insert the new branch..
137 new BranchInst(TheOnlyDest, SI);
138 BasicBlock *BB = SI->getParent();
140 // Remove entries from PHI nodes which we no longer branch to...
141 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
142 // Found case matching a constant operand?
143 BasicBlock *Succ = SI->getSuccessor(i);
144 if (Succ == TheOnlyDest)
145 TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest
147 Succ->removePredecessor(BB);
150 // Delete the old switch...
151 BB->getInstList().erase(SI);
153 } else if (SI->getNumSuccessors() == 2) {
154 // Otherwise, we can fold this switch into a conditional branch
155 // instruction if it has only one non-default destination.
156 Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, SI->getCondition(),
157 SI->getSuccessorValue(1), "cond", SI);
158 // Insert the new branch...
159 new BranchInst(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
161 // Delete the old switch...
162 SI->getParent()->getInstList().erase(SI);
170 //===----------------------------------------------------------------------===//
171 // Local dead code elimination...
174 bool llvm::isInstructionTriviallyDead(Instruction *I) {
175 if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
177 if (!I->mayWriteToMemory())
180 // Special case intrinsics that "may write to memory" but can be deleted when
182 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
183 // Safe to delete llvm.stacksave if dead.
184 if (II->getIntrinsicID() == Intrinsic::stacksave)
190 // dceInstruction - Inspect the instruction at *BBI and figure out if it's
191 // [trivially] dead. If so, remove the instruction and update the iterator
192 // to point to the instruction that immediately succeeded the original
195 bool llvm::dceInstruction(BasicBlock::iterator &BBI) {
196 // Look for un"used" definitions...
197 if (isInstructionTriviallyDead(BBI)) {
198 BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye