blob: 187ebdc79793c780e7ec751cb636bb379983a706 [file] [log] [blame]
Chris Lattner4d1e46e2002-05-07 18:07:59 +00001//===-- Local.cpp - Functions to perform local transformations ------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4d1e46e2002-05-07 18:07:59 +00009//
10// This family of functions perform various local transformations to the
11// program.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Utils/Local.h"
Chris Lattner81ebc302004-01-12 18:35:03 +000016#include "llvm/Constants.h"
Chris Lattnerc5f52e62005-09-26 05:27:10 +000017#include "llvm/DerivedTypes.h"
Chris Lattner7822c2a2004-01-12 19:56:36 +000018#include "llvm/Instructions.h"
Chris Lattnercf110352004-06-11 06:16:23 +000019#include "llvm/Intrinsics.h"
Chris Lattnercbbc6b72005-10-27 16:34:00 +000020#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner9fa038d2007-01-30 23:13:49 +000021#include "llvm/Target/TargetData.h"
Chris Lattnerc5f52e62005-09-26 05:27:10 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
23#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000024#include <cerrno>
Chris Lattnerabbc2dd2003-12-19 05:56:28 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner4d1e46e2002-05-07 18:07:59 +000027//===----------------------------------------------------------------------===//
Misha Brukman82c89b92003-05-20 21:01:22 +000028// Local constant propagation...
Chris Lattner4d1e46e2002-05-07 18:07:59 +000029//
30
Chris Lattnerabbc2dd2003-12-19 05:56:28 +000031/// doConstantPropagation - If an instruction references constants, try to fold
32/// them together...
33///
Chris Lattner9fa038d2007-01-30 23:13:49 +000034bool llvm::doConstantPropagation(BasicBlock::iterator &II,
35 const TargetData *TD) {
36 if (Constant *C = ConstantFoldInstruction(II, TD)) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +000037 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner18961502002-06-25 16:12:52 +000038 II->replaceAllUsesWith(C);
Misha Brukmanfd939082005-04-21 23:48:37 +000039
Chris Lattner4d1e46e2002-05-07 18:07:59 +000040 // Remove the instruction from the basic block...
Chris Lattner18961502002-06-25 16:12:52 +000041 II = II->getParent()->getInstList().erase(II);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000042 return true;
43 }
44
45 return false;
46}
47
48// ConstantFoldTerminator - If a terminator instruction is predicated on a
49// constant value, convert it into an unconditional branch to the constant
50// destination.
51//
Chris Lattnerabbc2dd2003-12-19 05:56:28 +000052bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
Chris Lattner76ae3442002-05-21 20:04:50 +000053 TerminatorInst *T = BB->getTerminator();
Misha Brukmanfd939082005-04-21 23:48:37 +000054
Chris Lattner4d1e46e2002-05-07 18:07:59 +000055 // Branch - See if we are conditional jumping on constant
56 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
57 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
58 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
59 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
60
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000061 if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +000062 // Are we branching on constant?
63 // YES. Change to unconditional branch...
Reid Spencer579dca12007-01-12 04:24:46 +000064 BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
65 BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
Chris Lattner4d1e46e2002-05-07 18:07:59 +000066
Misha Brukmanfd939082005-04-21 23:48:37 +000067 //cerr << "Function: " << T->getParent()->getParent()
68 // << "\nRemoving branch from " << T->getParent()
Chris Lattner4d1e46e2002-05-07 18:07:59 +000069 // << "\n\nTo: " << OldDest << endl;
70
71 // Let the basic block know that we are letting go of it. Based on this,
72 // it will adjust it's PHI nodes.
73 assert(BI->getParent() && "Terminator not inserted in block!");
74 OldDest->removePredecessor(BI->getParent());
75
76 // Set the unconditional destination, and change the insn to be an
77 // unconditional branch.
78 BI->setUnconditionalDest(Destination);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000079 return true;
Chris Lattner342a9d12003-08-17 19:34:55 +000080 } else if (Dest2 == Dest1) { // Conditional branch to same location?
Misha Brukmanfd939082005-04-21 23:48:37 +000081 // This branch matches something like this:
Chris Lattner4d1e46e2002-05-07 18:07:59 +000082 // br bool %cond, label %Dest, label %Dest
83 // and changes it into: br label %Dest
84
85 // Let the basic block know that we are letting go of one copy of it.
86 assert(BI->getParent() && "Terminator not inserted in block!");
87 Dest1->removePredecessor(BI->getParent());
88
89 // Change a conditional branch to unconditional.
90 BI->setUnconditionalDest(Dest1);
91 return true;
92 }
Chris Lattner10b1f5a2003-08-17 20:21:14 +000093 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
94 // If we are switching on a constant, we can convert the switch into a
95 // single branch instruction!
96 ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
97 BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest
Chris Lattner7d6c24c2003-08-23 23:18:19 +000098 BasicBlock *DefaultDest = TheOnlyDest;
99 assert(TheOnlyDest == SI->getDefaultDest() &&
100 "Default destination is not successor #0?");
Chris Lattner694e37f2003-08-17 19:41:53 +0000101
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000102 // Figure out which case it goes to...
103 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
104 // Found case matching a constant operand?
105 if (SI->getSuccessorValue(i) == CI) {
106 TheOnlyDest = SI->getSuccessor(i);
107 break;
108 }
Chris Lattner694e37f2003-08-17 19:41:53 +0000109
Chris Lattner7d6c24c2003-08-23 23:18:19 +0000110 // Check to see if this branch is going to the same place as the default
111 // dest. If so, eliminate it as an explicit compare.
112 if (SI->getSuccessor(i) == DefaultDest) {
113 // Remove this entry...
114 DefaultDest->removePredecessor(SI->getParent());
115 SI->removeCase(i);
116 --i; --e; // Don't skip an entry...
117 continue;
118 }
119
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000120 // Otherwise, check to see if the switch only branches to one destination.
121 // We do this by reseting "TheOnlyDest" to null when we find two non-equal
122 // destinations.
123 if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
Chris Lattner694e37f2003-08-17 19:41:53 +0000124 }
125
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000126 if (CI && !TheOnlyDest) {
127 // Branching on a constant, but not any of the cases, go to the default
128 // successor.
129 TheOnlyDest = SI->getDefaultDest();
130 }
131
132 // If we found a single destination that we can fold the switch into, do so
133 // now.
134 if (TheOnlyDest) {
135 // Insert the new branch..
136 new BranchInst(TheOnlyDest, SI);
137 BasicBlock *BB = SI->getParent();
138
139 // Remove entries from PHI nodes which we no longer branch to...
140 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
141 // Found case matching a constant operand?
142 BasicBlock *Succ = SI->getSuccessor(i);
143 if (Succ == TheOnlyDest)
144 TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest
145 else
146 Succ->removePredecessor(BB);
147 }
148
149 // Delete the old switch...
150 BB->getInstList().erase(SI);
151 return true;
152 } else if (SI->getNumSuccessors() == 2) {
153 // Otherwise, we can fold this switch into a conditional branch
154 // instruction if it has only one non-default destination.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000155 Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, SI->getCondition(),
156 SI->getSuccessorValue(1), "cond", SI);
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000157 // Insert the new branch...
158 new BranchInst(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
159
160 // Delete the old switch...
161 SI->getParent()->getInstList().erase(SI);
162 return true;
163 }
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000164 }
165 return false;
166}
167
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000168
169//===----------------------------------------------------------------------===//
170// Local dead code elimination...
171//
172
Chris Lattnerabbc2dd2003-12-19 05:56:28 +0000173bool llvm::isInstructionTriviallyDead(Instruction *I) {
Chris Lattnerec710c52005-05-06 05:27:34 +0000174 if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000175
Chris Lattnerec710c52005-05-06 05:27:34 +0000176 if (!I->mayWriteToMemory()) return true;
177
Chris Lattnerec710c52005-05-06 05:27:34 +0000178 return false;
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000179}
180
181// dceInstruction - Inspect the instruction at *BBI and figure out if it's
182// [trivially] dead. If so, remove the instruction and update the iterator
183// to point to the instruction that immediately succeeded the original
184// instruction.
185//
Chris Lattnerabbc2dd2003-12-19 05:56:28 +0000186bool llvm::dceInstruction(BasicBlock::iterator &BBI) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000187 // Look for un"used" definitions...
Chris Lattner18961502002-06-25 16:12:52 +0000188 if (isInstructionTriviallyDead(BBI)) {
189 BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000190 return true;
191 }
192 return false;
193}