blob: 0f7d02c8886ef4a72eccd36eac1acb040475e970 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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 Lattner741c0ae2007-12-29 00:59:12 +000020#include "llvm/IntrinsicInst.h"
Chris Lattnercbbc6b72005-10-27 16:34:00 +000021#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner9fa038d2007-01-30 23:13:49 +000022#include "llvm/Target/TargetData.h"
Chris Lattnerc5f52e62005-09-26 05:27:10 +000023#include "llvm/Support/GetElementPtrTypeIterator.h"
24#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos09233fb2004-04-21 16:11:40 +000025#include <cerrno>
Chris Lattnerabbc2dd2003-12-19 05:56:28 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner4d1e46e2002-05-07 18:07:59 +000028//===----------------------------------------------------------------------===//
Misha Brukman82c89b92003-05-20 21:01:22 +000029// Local constant propagation...
Chris Lattner4d1e46e2002-05-07 18:07:59 +000030//
31
Chris Lattnerabbc2dd2003-12-19 05:56:28 +000032/// doConstantPropagation - If an instruction references constants, try to fold
33/// them together...
34///
Chris Lattner9fa038d2007-01-30 23:13:49 +000035bool llvm::doConstantPropagation(BasicBlock::iterator &II,
36 const TargetData *TD) {
37 if (Constant *C = ConstantFoldInstruction(II, TD)) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +000038 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner18961502002-06-25 16:12:52 +000039 II->replaceAllUsesWith(C);
Misha Brukmanfd939082005-04-21 23:48:37 +000040
Chris Lattner4d1e46e2002-05-07 18:07:59 +000041 // Remove the instruction from the basic block...
Chris Lattner18961502002-06-25 16:12:52 +000042 II = II->getParent()->getInstList().erase(II);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000043 return true;
44 }
45
46 return false;
47}
48
49// ConstantFoldTerminator - If a terminator instruction is predicated on a
50// constant value, convert it into an unconditional branch to the constant
51// destination.
52//
Chris Lattnerabbc2dd2003-12-19 05:56:28 +000053bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
Chris Lattner76ae3442002-05-21 20:04:50 +000054 TerminatorInst *T = BB->getTerminator();
Misha Brukmanfd939082005-04-21 23:48:37 +000055
Chris Lattner4d1e46e2002-05-07 18:07:59 +000056 // 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));
61
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +000062 if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +000063 // Are we branching on constant?
64 // YES. Change to unconditional branch...
Reid Spencer579dca12007-01-12 04:24:46 +000065 BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
66 BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
Chris Lattner4d1e46e2002-05-07 18:07:59 +000067
Misha Brukmanfd939082005-04-21 23:48:37 +000068 //cerr << "Function: " << T->getParent()->getParent()
69 // << "\nRemoving branch from " << T->getParent()
Chris Lattner4d1e46e2002-05-07 18:07:59 +000070 // << "\n\nTo: " << OldDest << endl;
71
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());
76
77 // Set the unconditional destination, and change the insn to be an
78 // unconditional branch.
79 BI->setUnconditionalDest(Destination);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000080 return true;
Chris Lattner342a9d12003-08-17 19:34:55 +000081 } else if (Dest2 == Dest1) { // Conditional branch to same location?
Misha Brukmanfd939082005-04-21 23:48:37 +000082 // This branch matches something like this:
Chris Lattner4d1e46e2002-05-07 18:07:59 +000083 // br bool %cond, label %Dest, label %Dest
84 // and changes it into: br label %Dest
85
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());
89
90 // Change a conditional branch to unconditional.
91 BI->setUnconditionalDest(Dest1);
92 return true;
93 }
Chris Lattner10b1f5a2003-08-17 20:21:14 +000094 } 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
Chris Lattner7d6c24c2003-08-23 23:18:19 +000099 BasicBlock *DefaultDest = TheOnlyDest;
100 assert(TheOnlyDest == SI->getDefaultDest() &&
101 "Default destination is not successor #0?");
Chris Lattner694e37f2003-08-17 19:41:53 +0000102
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000103 // 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);
108 break;
109 }
Chris Lattner694e37f2003-08-17 19:41:53 +0000110
Chris Lattner7d6c24c2003-08-23 23:18:19 +0000111 // 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());
116 SI->removeCase(i);
117 --i; --e; // Don't skip an entry...
118 continue;
119 }
120
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000121 // 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
123 // destinations.
124 if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
Chris Lattner694e37f2003-08-17 19:41:53 +0000125 }
126
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000127 if (CI && !TheOnlyDest) {
128 // Branching on a constant, but not any of the cases, go to the default
129 // successor.
130 TheOnlyDest = SI->getDefaultDest();
131 }
132
133 // If we found a single destination that we can fold the switch into, do so
134 // now.
135 if (TheOnlyDest) {
136 // Insert the new branch..
Gabor Greif051a9502008-04-06 20:25:17 +0000137 BranchInst::Create(TheOnlyDest, SI);
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000138 BasicBlock *BB = SI->getParent();
139
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
146 else
147 Succ->removePredecessor(BB);
148 }
149
150 // Delete the old switch...
151 BB->getInstList().erase(SI);
152 return true;
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.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000156 Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, SI->getCondition(),
157 SI->getSuccessorValue(1), "cond", SI);
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000158 // Insert the new branch...
Gabor Greif051a9502008-04-06 20:25:17 +0000159 BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000160
161 // Delete the old switch...
162 SI->getParent()->getInstList().erase(SI);
163 return true;
164 }
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000165 }
166 return false;
167}
168
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000169
170//===----------------------------------------------------------------------===//
171// Local dead code elimination...
172//
173
Chris Lattnerabbc2dd2003-12-19 05:56:28 +0000174bool llvm::isInstructionTriviallyDead(Instruction *I) {
Chris Lattnerec710c52005-05-06 05:27:34 +0000175 if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000176
Chris Lattner741c0ae2007-12-29 00:59:12 +0000177 if (!I->mayWriteToMemory())
178 return true;
Chris Lattnerec710c52005-05-06 05:27:34 +0000179
Chris Lattner741c0ae2007-12-29 00:59:12 +0000180 // Special case intrinsics that "may write to memory" but can be deleted when
181 // dead.
182 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
183 // Safe to delete llvm.stacksave if dead.
184 if (II->getIntrinsicID() == Intrinsic::stacksave)
185 return true;
186
Chris Lattnerec710c52005-05-06 05:27:34 +0000187 return false;
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000188}
189
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
193// instruction.
194//
Chris Lattnerabbc2dd2003-12-19 05:56:28 +0000195bool llvm::dceInstruction(BasicBlock::iterator &BBI) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000196 // Look for un"used" definitions...
Chris Lattner18961502002-06-25 16:12:52 +0000197 if (isInstructionTriviallyDead(BBI)) {
198 BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000199 return true;
200 }
201 return false;
202}