blob: 7e7973ae0b737b3cc76533cdc2103e039c00186e [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 Lattner6cc8a932009-06-16 17:23:12 +000017#include "llvm/GlobalAlias.h"
Devang Patelc79e1182009-03-06 00:19:37 +000018#include "llvm/GlobalVariable.h"
Chris Lattnerc5f52e62005-09-26 05:27:10 +000019#include "llvm/DerivedTypes.h"
Chris Lattner7822c2a2004-01-12 19:56:36 +000020#include "llvm/Instructions.h"
Chris Lattnercf110352004-06-11 06:16:23 +000021#include "llvm/Intrinsics.h"
Chris Lattner741c0ae2007-12-29 00:59:12 +000022#include "llvm/IntrinsicInst.h"
Chris Lattner19f2dc42009-12-29 09:12:29 +000023#include "llvm/ADT/DenseMap.h"
Dan Gohmanafc36a92009-05-02 18:29:22 +000024#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnercbbc6b72005-10-27 16:34:00 +000025#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner40d8c282009-11-10 22:26:15 +000026#include "llvm/Analysis/InstructionSimplify.h"
Andreas Neustifterad809812009-09-16 09:26:52 +000027#include "llvm/Analysis/ProfileInfo.h"
Chris Lattner9fa038d2007-01-30 23:13:49 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerdce94d92009-11-10 05:59:26 +000029#include "llvm/Support/CFG.h"
30#include "llvm/Support/Debug.h"
Chris Lattnerc5f52e62005-09-26 05:27:10 +000031#include "llvm/Support/GetElementPtrTypeIterator.h"
32#include "llvm/Support/MathExtras.h"
Chris Lattner19f2dc42009-12-29 09:12:29 +000033#include "llvm/Support/ValueHandle.h"
Chris Lattnerdce94d92009-11-10 05:59:26 +000034#include "llvm/Support/raw_ostream.h"
Chris Lattnerabbc2dd2003-12-19 05:56:28 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner4d1e46e2002-05-07 18:07:59 +000037//===----------------------------------------------------------------------===//
Chris Lattner6cc8a932009-06-16 17:23:12 +000038// Local analysis.
39//
40
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000041/// getUnderlyingObjectWithOffset - Strip off up to MaxLookup GEPs and
42/// bitcasts to get back to the underlying object being addressed, keeping
43/// track of the offset in bytes from the GEPs relative to the result.
44/// This is closely related to Value::getUnderlyingObject but is located
45/// here to avoid making VMCore depend on TargetData.
46static Value *getUnderlyingObjectWithOffset(Value *V, const TargetData *TD,
Bob Wilson963e84f2010-01-29 20:34:28 +000047 uint64_t &ByteOffset,
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000048 unsigned MaxLookup = 6) {
49 if (!isa<PointerType>(V->getType()))
50 return V;
51 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
52 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
53 if (!GEP->hasAllConstantIndices())
54 return V;
55 SmallVector<Value*, 8> Indices(GEP->op_begin() + 1, GEP->op_end());
56 ByteOffset += TD->getIndexedOffset(GEP->getPointerOperandType(),
57 &Indices[0], Indices.size());
58 V = GEP->getPointerOperand();
59 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
60 V = cast<Operator>(V)->getOperand(0);
61 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
62 if (GA->mayBeOverridden())
63 return V;
64 V = GA->getAliasee();
65 } else {
66 return V;
67 }
68 assert(isa<PointerType>(V->getType()) && "Unexpected operand type!");
69 }
70 return V;
71}
72
Chris Lattner6cc8a932009-06-16 17:23:12 +000073/// isSafeToLoadUnconditionally - Return true if we know that executing a load
74/// from this value cannot trap. If it is not obviously safe to load from the
75/// specified pointer, we do a quick local scan of the basic block containing
76/// ScanFrom, to determine if the address is already accessed.
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000077bool llvm::isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom,
Bob Wilson49db68f2010-01-30 04:42:39 +000078 unsigned Align, const TargetData *TD) {
Bob Wilson963e84f2010-01-29 20:34:28 +000079 uint64_t ByteOffset = 0;
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000080 Value *Base = V;
81 if (TD)
82 Base = getUnderlyingObjectWithOffset(V, TD, ByteOffset);
Chris Lattner6cc8a932009-06-16 17:23:12 +000083
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000084 const Type *BaseType = 0;
Bob Wilson49db68f2010-01-30 04:42:39 +000085 unsigned BaseAlign = 0;
86 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
87 // An alloca is safe to load from as load as it is suitably aligned.
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000088 BaseType = AI->getAllocatedType();
Bob Wilson49db68f2010-01-30 04:42:39 +000089 BaseAlign = AI->getAlignment();
90 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(Base)) {
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000091 // Global variables are safe to load from but their size cannot be
92 // guaranteed if they are overridden.
Bob Wilson49db68f2010-01-30 04:42:39 +000093 if (!isa<GlobalAlias>(GV) && !GV->mayBeOverridden()) {
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000094 BaseType = GV->getType()->getElementType();
Bob Wilson49db68f2010-01-30 04:42:39 +000095 BaseAlign = GV->getAlignment();
96 }
Bob Wilson3eb4f7e2010-01-29 19:19:08 +000097 }
Chris Lattner6cc8a932009-06-16 17:23:12 +000098
Bob Wilson3cb85092010-02-01 17:41:44 +000099 if (BaseType && BaseType->isSized()) {
100 if (TD && BaseAlign == 0)
101 BaseAlign = TD->getPrefTypeAlignment(BaseType);
102
103 if (Align <= BaseAlign) {
104 if (!TD)
105 return true; // Loading directly from an alloca or global is OK.
106
Bob Wilson3eb4f7e2010-01-29 19:19:08 +0000107 // Check if the load is within the bounds of the underlying object.
108 const PointerType *AddrTy = cast<PointerType>(V->getType());
Bob Wilson963e84f2010-01-29 20:34:28 +0000109 uint64_t LoadSize = TD->getTypeStoreSize(AddrTy->getElementType());
Bob Wilson49db68f2010-01-30 04:42:39 +0000110 if (ByteOffset + LoadSize <= TD->getTypeAllocSize(BaseType) &&
111 (Align == 0 || (ByteOffset % Align) == 0))
Bob Wilson3eb4f7e2010-01-29 19:19:08 +0000112 return true;
113 }
114 }
115
116 // Otherwise, be a little bit aggressive by scanning the local block where we
Chris Lattner6cc8a932009-06-16 17:23:12 +0000117 // want to check to see if the pointer is already being loaded or stored
118 // from/to. If so, the previous load or store would have already trapped,
119 // so there is no harm doing an extra load (also, CSE will later eliminate
120 // the load entirely).
121 BasicBlock::iterator BBI = ScanFrom, E = ScanFrom->getParent()->begin();
122
123 while (BBI != E) {
124 --BBI;
125
126 // If we see a free or a call which may write to memory (i.e. which might do
127 // a free) the pointer could be marked invalid.
Chris Lattner938e1762009-11-03 05:33:46 +0000128 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
129 !isa<DbgInfoIntrinsic>(BBI))
Chris Lattner6cc8a932009-06-16 17:23:12 +0000130 return false;
131
132 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
133 if (LI->getOperand(0) == V) return true;
134 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
135 if (SI->getOperand(1) == V) return true;
136 }
137 }
138 return false;
139}
140
141
142//===----------------------------------------------------------------------===//
Chris Lattner3481f242008-11-27 22:57:53 +0000143// Local constant propagation.
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000144//
145
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000146// ConstantFoldTerminator - If a terminator instruction is predicated on a
147// constant value, convert it into an unconditional branch to the constant
148// destination.
149//
Chris Lattnerabbc2dd2003-12-19 05:56:28 +0000150bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
Chris Lattner76ae3442002-05-21 20:04:50 +0000151 TerminatorInst *T = BB->getTerminator();
Misha Brukmanfd939082005-04-21 23:48:37 +0000152
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000153 // Branch - See if we are conditional jumping on constant
154 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
155 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
Gabor Greifc1bb13f2009-01-30 18:21:13 +0000156 BasicBlock *Dest1 = BI->getSuccessor(0);
157 BasicBlock *Dest2 = BI->getSuccessor(1);
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000158
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000159 if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000160 // Are we branching on constant?
161 // YES. Change to unconditional branch...
Reid Spencer579dca12007-01-12 04:24:46 +0000162 BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
163 BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000164
Misha Brukmanfd939082005-04-21 23:48:37 +0000165 //cerr << "Function: " << T->getParent()->getParent()
166 // << "\nRemoving branch from " << T->getParent()
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000167 // << "\n\nTo: " << OldDest << endl;
168
169 // Let the basic block know that we are letting go of it. Based on this,
170 // it will adjust it's PHI nodes.
171 assert(BI->getParent() && "Terminator not inserted in block!");
172 OldDest->removePredecessor(BI->getParent());
173
174 // Set the unconditional destination, and change the insn to be an
175 // unconditional branch.
176 BI->setUnconditionalDest(Destination);
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000177 return true;
Chris Lattner0a4c6782009-11-01 03:40:38 +0000178 }
179
180 if (Dest2 == Dest1) { // Conditional branch to same location?
Misha Brukmanfd939082005-04-21 23:48:37 +0000181 // This branch matches something like this:
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000182 // br bool %cond, label %Dest, label %Dest
183 // and changes it into: br label %Dest
184
185 // Let the basic block know that we are letting go of one copy of it.
186 assert(BI->getParent() && "Terminator not inserted in block!");
187 Dest1->removePredecessor(BI->getParent());
188
189 // Change a conditional branch to unconditional.
190 BI->setUnconditionalDest(Dest1);
191 return true;
192 }
Chris Lattner0a4c6782009-11-01 03:40:38 +0000193 return false;
194 }
195
196 if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000197 // If we are switching on a constant, we can convert the switch into a
198 // single branch instruction!
199 ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
200 BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest
Chris Lattner7d6c24c2003-08-23 23:18:19 +0000201 BasicBlock *DefaultDest = TheOnlyDest;
202 assert(TheOnlyDest == SI->getDefaultDest() &&
203 "Default destination is not successor #0?");
Chris Lattner694e37f2003-08-17 19:41:53 +0000204
Chris Lattner0a4c6782009-11-01 03:40:38 +0000205 // Figure out which case it goes to.
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000206 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
207 // Found case matching a constant operand?
208 if (SI->getSuccessorValue(i) == CI) {
209 TheOnlyDest = SI->getSuccessor(i);
210 break;
211 }
Chris Lattner694e37f2003-08-17 19:41:53 +0000212
Chris Lattner7d6c24c2003-08-23 23:18:19 +0000213 // Check to see if this branch is going to the same place as the default
214 // dest. If so, eliminate it as an explicit compare.
215 if (SI->getSuccessor(i) == DefaultDest) {
Chris Lattner0a4c6782009-11-01 03:40:38 +0000216 // Remove this entry.
Chris Lattner7d6c24c2003-08-23 23:18:19 +0000217 DefaultDest->removePredecessor(SI->getParent());
218 SI->removeCase(i);
219 --i; --e; // Don't skip an entry...
220 continue;
221 }
222
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000223 // Otherwise, check to see if the switch only branches to one destination.
224 // We do this by reseting "TheOnlyDest" to null when we find two non-equal
225 // destinations.
226 if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
Chris Lattner694e37f2003-08-17 19:41:53 +0000227 }
228
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000229 if (CI && !TheOnlyDest) {
230 // Branching on a constant, but not any of the cases, go to the default
231 // successor.
232 TheOnlyDest = SI->getDefaultDest();
233 }
234
235 // If we found a single destination that we can fold the switch into, do so
236 // now.
237 if (TheOnlyDest) {
Chris Lattner0a4c6782009-11-01 03:40:38 +0000238 // Insert the new branch.
Gabor Greif051a9502008-04-06 20:25:17 +0000239 BranchInst::Create(TheOnlyDest, SI);
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000240 BasicBlock *BB = SI->getParent();
241
242 // Remove entries from PHI nodes which we no longer branch to...
243 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
244 // Found case matching a constant operand?
245 BasicBlock *Succ = SI->getSuccessor(i);
246 if (Succ == TheOnlyDest)
247 TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest
248 else
249 Succ->removePredecessor(BB);
250 }
251
Chris Lattner0a4c6782009-11-01 03:40:38 +0000252 // Delete the old switch.
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000253 BB->getInstList().erase(SI);
254 return true;
Chris Lattner0a4c6782009-11-01 03:40:38 +0000255 }
256
257 if (SI->getNumSuccessors() == 2) {
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000258 // Otherwise, we can fold this switch into a conditional branch
259 // instruction if it has only one non-default destination.
Owen Anderson333c4002009-07-09 23:48:35 +0000260 Value *Cond = new ICmpInst(SI, ICmpInst::ICMP_EQ, SI->getCondition(),
261 SI->getSuccessorValue(1), "cond");
Chris Lattner0a4c6782009-11-01 03:40:38 +0000262 // Insert the new branch.
Gabor Greif051a9502008-04-06 20:25:17 +0000263 BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000264
Chris Lattner0a4c6782009-11-01 03:40:38 +0000265 // Delete the old switch.
Dan Gohman1adec832008-06-21 22:08:46 +0000266 SI->eraseFromParent();
Chris Lattner10b1f5a2003-08-17 20:21:14 +0000267 return true;
268 }
Chris Lattner0a4c6782009-11-01 03:40:38 +0000269 return false;
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000270 }
Chris Lattner0a4c6782009-11-01 03:40:38 +0000271
272 if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(T)) {
273 // indirectbr blockaddress(@F, @BB) -> br label @BB
274 if (BlockAddress *BA =
275 dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
276 BasicBlock *TheOnlyDest = BA->getBasicBlock();
277 // Insert the new branch.
278 BranchInst::Create(TheOnlyDest, IBI);
279
280 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
281 if (IBI->getDestination(i) == TheOnlyDest)
282 TheOnlyDest = 0;
283 else
284 IBI->getDestination(i)->removePredecessor(IBI->getParent());
285 }
286 IBI->eraseFromParent();
287
288 // If we didn't find our destination in the IBI successor list, then we
289 // have undefined behavior. Replace the unconditional branch with an
290 // 'unreachable' instruction.
291 if (TheOnlyDest) {
292 BB->getTerminator()->eraseFromParent();
293 new UnreachableInst(BB->getContext(), BB);
294 }
295
296 return true;
297 }
298 }
299
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000300 return false;
301}
302
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000303
304//===----------------------------------------------------------------------===//
Chris Lattner40d8c282009-11-10 22:26:15 +0000305// Local dead code elimination.
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000306//
307
Chris Lattner3481f242008-11-27 22:57:53 +0000308/// isInstructionTriviallyDead - Return true if the result produced by the
309/// instruction is not used, and the instruction has no side effects.
310///
Chris Lattnerabbc2dd2003-12-19 05:56:28 +0000311bool llvm::isInstructionTriviallyDead(Instruction *I) {
Chris Lattnerec710c52005-05-06 05:27:34 +0000312 if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000313
Dale Johannesen127a7932009-03-03 23:30:00 +0000314 // We don't want debug info removed by anything this general.
315 if (isa<DbgInfoIntrinsic>(I)) return false;
Chris Lattnerec710c52005-05-06 05:27:34 +0000316
Duncan Sandsa3da9222009-11-11 15:34:13 +0000317 // Likewise for memory use markers.
318 if (isa<MemoryUseIntrinsic>(I)) return false;
319
Duncan Sands7af1c782009-05-06 06:49:50 +0000320 if (!I->mayHaveSideEffects()) return true;
321
322 // Special case intrinsics that "may have side effects" but can be deleted
323 // when dead.
Chris Lattner741c0ae2007-12-29 00:59:12 +0000324 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
325 // Safe to delete llvm.stacksave if dead.
326 if (II->getIntrinsicID() == Intrinsic::stacksave)
327 return true;
Chris Lattnerec710c52005-05-06 05:27:34 +0000328 return false;
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000329}
330
Chris Lattner3481f242008-11-27 22:57:53 +0000331/// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
332/// trivially dead instruction, delete it. If that makes any of its operands
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000333/// trivially dead, delete them too, recursively. Return true if any
334/// instructions were deleted.
335bool llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V) {
Chris Lattner3481f242008-11-27 22:57:53 +0000336 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattner76057302008-11-28 01:20:46 +0000337 if (!I || !I->use_empty() || !isInstructionTriviallyDead(I))
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000338 return false;
Chris Lattner3481f242008-11-27 22:57:53 +0000339
Chris Lattner76057302008-11-28 01:20:46 +0000340 SmallVector<Instruction*, 16> DeadInsts;
341 DeadInsts.push_back(I);
Chris Lattner3481f242008-11-27 22:57:53 +0000342
Dan Gohman321a8132010-01-05 16:27:25 +0000343 do {
Dan Gohmane9d87f42009-05-06 17:22:41 +0000344 I = DeadInsts.pop_back_val();
Chris Lattner28721772008-11-28 00:58:15 +0000345
Chris Lattner76057302008-11-28 01:20:46 +0000346 // Null out all of the instruction's operands to see if any operand becomes
347 // dead as we go.
348 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
349 Value *OpV = I->getOperand(i);
350 I->setOperand(i, 0);
351
352 if (!OpV->use_empty()) continue;
353
354 // If the operand is an instruction that became dead as we nulled out the
355 // operand, and if it is 'trivially' dead, delete it in a future loop
356 // iteration.
357 if (Instruction *OpI = dyn_cast<Instruction>(OpV))
358 if (isInstructionTriviallyDead(OpI))
359 DeadInsts.push_back(OpI);
360 }
361
362 I->eraseFromParent();
Dan Gohman321a8132010-01-05 16:27:25 +0000363 } while (!DeadInsts.empty());
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000364
365 return true;
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000366}
Chris Lattnerb29714a2008-11-27 07:43:12 +0000367
Dan Gohmanafc36a92009-05-02 18:29:22 +0000368/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
369/// dead PHI node, due to being a def-use chain of single-use nodes that
370/// either forms a cycle or is terminated by a trivially dead instruction,
371/// delete it. If that makes any of its operands trivially dead, delete them
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000372/// too, recursively. Return true if the PHI node is actually deleted.
373bool
Dan Gohman35738ac2009-05-04 22:30:44 +0000374llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) {
Dan Gohmanafc36a92009-05-02 18:29:22 +0000375 // We can remove a PHI if it is on a cycle in the def-use graph
376 // where each node in the cycle has degree one, i.e. only one use,
377 // and is an instruction with no side effects.
378 if (!PN->hasOneUse())
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000379 return false;
Dan Gohmanafc36a92009-05-02 18:29:22 +0000380
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000381 bool Changed = false;
Dan Gohmanafc36a92009-05-02 18:29:22 +0000382 SmallPtrSet<PHINode *, 4> PHIs;
383 PHIs.insert(PN);
384 for (Instruction *J = cast<Instruction>(*PN->use_begin());
Duncan Sands7af1c782009-05-06 06:49:50 +0000385 J->hasOneUse() && !J->mayHaveSideEffects();
Dan Gohmanafc36a92009-05-02 18:29:22 +0000386 J = cast<Instruction>(*J->use_begin()))
387 // If we find a PHI more than once, we're on a cycle that
388 // won't prove fruitful.
389 if (PHINode *JP = dyn_cast<PHINode>(J))
390 if (!PHIs.insert(cast<PHINode>(JP))) {
391 // Break the cycle and delete the PHI and its operands.
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000392 JP->replaceAllUsesWith(UndefValue::get(JP->getType()));
Dan Gohmanba25f092010-01-05 17:50:58 +0000393 (void)RecursivelyDeleteTriviallyDeadInstructions(JP);
394 Changed = true;
Dan Gohmanafc36a92009-05-02 18:29:22 +0000395 break;
396 }
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000397 return Changed;
Dan Gohmanafc36a92009-05-02 18:29:22 +0000398}
Chris Lattner3481f242008-11-27 22:57:53 +0000399
Chris Lattnere234a302010-01-12 19:40:54 +0000400/// SimplifyInstructionsInBlock - Scan the specified basic block and try to
401/// simplify any instructions in it and recursively delete dead instructions.
402///
403/// This returns true if it changed the code, note that it can delete
404/// instructions in other blocks as well in this block.
405bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD) {
406 bool MadeChange = false;
407 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
408 Instruction *Inst = BI++;
409
410 if (Value *V = SimplifyInstruction(Inst, TD)) {
411 WeakVH BIHandle(BI);
412 ReplaceAndSimplifyAllUses(Inst, V, TD);
413 MadeChange = true;
414 if (BIHandle == 0)
415 BI = BB->begin();
416 continue;
417 }
418
419 MadeChange |= RecursivelyDeleteTriviallyDeadInstructions(Inst);
420 }
421 return MadeChange;
422}
423
Chris Lattnerb29714a2008-11-27 07:43:12 +0000424//===----------------------------------------------------------------------===//
Chris Lattner40d8c282009-11-10 22:26:15 +0000425// Control Flow Graph Restructuring.
Chris Lattnerb29714a2008-11-27 07:43:12 +0000426//
427
Chris Lattner40d8c282009-11-10 22:26:15 +0000428
429/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
430/// method is called when we're about to delete Pred as a predecessor of BB. If
431/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
432///
433/// Unlike the removePredecessor method, this attempts to simplify uses of PHI
434/// nodes that collapse into identity values. For example, if we have:
435/// x = phi(1, 0, 0, 0)
436/// y = and x, z
437///
438/// .. and delete the predecessor corresponding to the '1', this will attempt to
439/// recursively fold the and to 0.
440void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
441 TargetData *TD) {
442 // This only adjusts blocks with PHI nodes.
443 if (!isa<PHINode>(BB->begin()))
444 return;
445
446 // Remove the entries for Pred from the PHI nodes in BB, but do not simplify
447 // them down. This will leave us with single entry phi nodes and other phis
448 // that can be removed.
449 BB->removePredecessor(Pred, true);
450
451 WeakVH PhiIt = &BB->front();
452 while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) {
453 PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt));
454
455 Value *PNV = PN->hasConstantValue();
456 if (PNV == 0) continue;
457
458 // If we're able to simplify the phi to a single value, substitute the new
459 // value into all of its uses.
460 assert(PNV != PN && "hasConstantValue broken");
461
462 ReplaceAndSimplifyAllUses(PN, PNV, TD);
463
464 // If recursive simplification ended up deleting the next PHI node we would
465 // iterate to, then our iterator is invalid, restart scanning from the top
466 // of the block.
467 if (PhiIt == 0) PhiIt = &BB->front();
468 }
469}
470
471
Chris Lattnerb29714a2008-11-27 07:43:12 +0000472/// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
473/// predecessor is known to have one successor (DestBB!). Eliminate the edge
474/// between them, moving the instructions in the predecessor into DestBB and
475/// deleting the predecessor block.
476///
Andreas Neustifterad809812009-09-16 09:26:52 +0000477void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, Pass *P) {
Chris Lattnerb29714a2008-11-27 07:43:12 +0000478 // If BB has single-entry PHI nodes, fold them.
479 while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
480 Value *NewVal = PN->getIncomingValue(0);
481 // Replace self referencing PHI with undef, it must be dead.
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000482 if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
Chris Lattnerb29714a2008-11-27 07:43:12 +0000483 PN->replaceAllUsesWith(NewVal);
484 PN->eraseFromParent();
485 }
486
487 BasicBlock *PredBB = DestBB->getSinglePredecessor();
488 assert(PredBB && "Block doesn't have a single predecessor!");
489
490 // Splice all the instructions from PredBB to DestBB.
491 PredBB->getTerminator()->eraseFromParent();
492 DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
493
494 // Anything that branched to PredBB now branches to DestBB.
495 PredBB->replaceAllUsesWith(DestBB);
496
Andreas Neustifterad809812009-09-16 09:26:52 +0000497 if (P) {
498 ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>();
499 if (PI) {
500 PI->replaceAllUses(PredBB, DestBB);
501 PI->removeEdge(ProfileInfo::getEdge(PredBB, DestBB));
502 }
503 }
Chris Lattnerb29714a2008-11-27 07:43:12 +0000504 // Nuke BB.
505 PredBB->eraseFromParent();
506}
Devang Patel4afc90d2009-02-10 07:00:59 +0000507
Chris Lattnerdce94d92009-11-10 05:59:26 +0000508/// CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an
509/// almost-empty BB ending in an unconditional branch to Succ, into succ.
510///
511/// Assumption: Succ is the single successor for BB.
512///
513static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
514 assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
515
David Greenefae77062010-01-05 01:26:57 +0000516 DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
Chris Lattnerdce94d92009-11-10 05:59:26 +0000517 << Succ->getName() << "\n");
518 // Shortcut, if there is only a single predecessor it must be BB and merging
519 // is always safe
520 if (Succ->getSinglePredecessor()) return true;
521
522 // Make a list of the predecessors of BB
523 typedef SmallPtrSet<BasicBlock*, 16> BlockSet;
524 BlockSet BBPreds(pred_begin(BB), pred_end(BB));
525
526 // Use that list to make another list of common predecessors of BB and Succ
527 BlockSet CommonPreds;
528 for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ);
529 PI != PE; ++PI)
530 if (BBPreds.count(*PI))
531 CommonPreds.insert(*PI);
532
533 // Shortcut, if there are no common predecessors, merging is always safe
534 if (CommonPreds.empty())
535 return true;
536
537 // Look at all the phi nodes in Succ, to see if they present a conflict when
538 // merging these blocks
539 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
540 PHINode *PN = cast<PHINode>(I);
541
542 // If the incoming value from BB is again a PHINode in
543 // BB which has the same incoming value for *PI as PN does, we can
544 // merge the phi nodes and then the blocks can still be merged
545 PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
546 if (BBPN && BBPN->getParent() == BB) {
547 for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
548 PI != PE; PI++) {
549 if (BBPN->getIncomingValueForBlock(*PI)
550 != PN->getIncomingValueForBlock(*PI)) {
David Greenefae77062010-01-05 01:26:57 +0000551 DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
Chris Lattnerdce94d92009-11-10 05:59:26 +0000552 << Succ->getName() << " is conflicting with "
553 << BBPN->getName() << " with regard to common predecessor "
554 << (*PI)->getName() << "\n");
555 return false;
556 }
557 }
558 } else {
559 Value* Val = PN->getIncomingValueForBlock(BB);
560 for (BlockSet::iterator PI = CommonPreds.begin(), PE = CommonPreds.end();
561 PI != PE; PI++) {
562 // See if the incoming value for the common predecessor is equal to the
563 // one for BB, in which case this phi node will not prevent the merging
564 // of the block.
565 if (Val != PN->getIncomingValueForBlock(*PI)) {
David Greenefae77062010-01-05 01:26:57 +0000566 DEBUG(dbgs() << "Can't fold, phi node " << PN->getName() << " in "
Chris Lattnerdce94d92009-11-10 05:59:26 +0000567 << Succ->getName() << " is conflicting with regard to common "
568 << "predecessor " << (*PI)->getName() << "\n");
569 return false;
570 }
571 }
572 }
573 }
574
575 return true;
576}
577
578/// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
579/// unconditional branch, and contains no instructions other than PHI nodes,
580/// potential debug intrinsics and the branch. If possible, eliminate BB by
581/// rewriting all the predecessors to branch to the successor block and return
582/// true. If we can't transform, return false.
583bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
584 // We can't eliminate infinite loops.
585 BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
586 if (BB == Succ) return false;
587
588 // Check to see if merging these blocks would cause conflicts for any of the
589 // phi nodes in BB or Succ. If not, we can safely merge.
590 if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
591
592 // Check for cases where Succ has multiple predecessors and a PHI node in BB
593 // has uses which will not disappear when the PHI nodes are merged. It is
594 // possible to handle such cases, but difficult: it requires checking whether
595 // BB dominates Succ, which is non-trivial to calculate in the case where
596 // Succ has multiple predecessors. Also, it requires checking whether
597 // constructing the necessary self-referential PHI node doesn't intoduce any
598 // conflicts; this isn't too difficult, but the previous code for doing this
599 // was incorrect.
600 //
601 // Note that if this check finds a live use, BB dominates Succ, so BB is
602 // something like a loop pre-header (or rarely, a part of an irreducible CFG);
603 // folding the branch isn't profitable in that case anyway.
604 if (!Succ->getSinglePredecessor()) {
605 BasicBlock::iterator BBI = BB->begin();
606 while (isa<PHINode>(*BBI)) {
607 for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
608 UI != E; ++UI) {
609 if (PHINode* PN = dyn_cast<PHINode>(*UI)) {
610 if (PN->getIncomingBlock(UI) != BB)
611 return false;
612 } else {
613 return false;
614 }
615 }
616 ++BBI;
617 }
618 }
619
David Greenefae77062010-01-05 01:26:57 +0000620 DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
Chris Lattnerdce94d92009-11-10 05:59:26 +0000621
622 if (isa<PHINode>(Succ->begin())) {
623 // If there is more than one pred of succ, and there are PHI nodes in
624 // the successor, then we need to add incoming edges for the PHI nodes
625 //
626 const SmallVector<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
627
628 // Loop over all of the PHI nodes in the successor of BB.
629 for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
630 PHINode *PN = cast<PHINode>(I);
631 Value *OldVal = PN->removeIncomingValue(BB, false);
632 assert(OldVal && "No entry in PHI for Pred BB!");
633
634 // If this incoming value is one of the PHI nodes in BB, the new entries
635 // in the PHI node are the entries from the old PHI.
636 if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
637 PHINode *OldValPN = cast<PHINode>(OldVal);
638 for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i)
639 // Note that, since we are merging phi nodes and BB and Succ might
640 // have common predecessors, we could end up with a phi node with
641 // identical incoming branches. This will be cleaned up later (and
642 // will trigger asserts if we try to clean it up now, without also
643 // simplifying the corresponding conditional branch).
644 PN->addIncoming(OldValPN->getIncomingValue(i),
645 OldValPN->getIncomingBlock(i));
646 } else {
647 // Add an incoming value for each of the new incoming values.
648 for (unsigned i = 0, e = BBPreds.size(); i != e; ++i)
649 PN->addIncoming(OldVal, BBPreds[i]);
650 }
651 }
652 }
653
654 while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
655 if (Succ->getSinglePredecessor()) {
656 // BB is the only predecessor of Succ, so Succ will end up with exactly
657 // the same predecessors BB had.
658 Succ->getInstList().splice(Succ->begin(),
659 BB->getInstList(), BB->begin());
660 } else {
661 // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
662 assert(PN->use_empty() && "There shouldn't be any uses here!");
663 PN->eraseFromParent();
664 }
665 }
666
667 // Everything that jumped to BB now goes to Succ.
668 BB->replaceAllUsesWith(Succ);
669 if (!Succ->hasName()) Succ->takeName(BB);
670 BB->eraseFromParent(); // Delete the old basic block.
671 return true;
672}
673
Jim Grosbach43a82412009-12-02 17:06:45 +0000674/// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
675/// nodes in this block. This doesn't try to be clever about PHI nodes
676/// which differ only in the order of the incoming values, but instcombine
677/// orders them so it usually won't matter.
678///
679bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
680 bool Changed = false;
681
682 // This implementation doesn't currently consider undef operands
683 // specially. Theroetically, two phis which are identical except for
684 // one having an undef where the other doesn't could be collapsed.
685
686 // Map from PHI hash values to PHI nodes. If multiple PHIs have
687 // the same hash value, the element is the first PHI in the
688 // linked list in CollisionMap.
689 DenseMap<uintptr_t, PHINode *> HashMap;
690
691 // Maintain linked lists of PHI nodes with common hash values.
692 DenseMap<PHINode *, PHINode *> CollisionMap;
693
694 // Examine each PHI.
695 for (BasicBlock::iterator I = BB->begin();
696 PHINode *PN = dyn_cast<PHINode>(I++); ) {
697 // Compute a hash value on the operands. Instcombine will likely have sorted
698 // them, which helps expose duplicates, but we have to check all the
699 // operands to be safe in case instcombine hasn't run.
700 uintptr_t Hash = 0;
701 for (User::op_iterator I = PN->op_begin(), E = PN->op_end(); I != E; ++I) {
702 // This hash algorithm is quite weak as hash functions go, but it seems
703 // to do a good enough job for this particular purpose, and is very quick.
704 Hash ^= reinterpret_cast<uintptr_t>(static_cast<Value *>(*I));
705 Hash = (Hash << 7) | (Hash >> (sizeof(uintptr_t) * CHAR_BIT - 7));
706 }
707 // If we've never seen this hash value before, it's a unique PHI.
708 std::pair<DenseMap<uintptr_t, PHINode *>::iterator, bool> Pair =
709 HashMap.insert(std::make_pair(Hash, PN));
710 if (Pair.second) continue;
711 // Otherwise it's either a duplicate or a hash collision.
712 for (PHINode *OtherPN = Pair.first->second; ; ) {
713 if (OtherPN->isIdenticalTo(PN)) {
714 // A duplicate. Replace this PHI with its duplicate.
715 PN->replaceAllUsesWith(OtherPN);
716 PN->eraseFromParent();
717 Changed = true;
718 break;
719 }
720 // A non-duplicate hash collision.
721 DenseMap<PHINode *, PHINode *>::iterator I = CollisionMap.find(OtherPN);
722 if (I == CollisionMap.end()) {
723 // Set this PHI to be the head of the linked list of colliding PHIs.
724 PHINode *Old = Pair.first->second;
725 Pair.first->second = PN;
726 CollisionMap[PN] = Old;
727 break;
728 }
729 // Procede to the next PHI in the list.
730 OtherPN = I->second;
731 }
732 }
733
734 return Changed;
735}