1 //===- CodeGenPrepare.cpp - Prepare a function for code generation --------===//
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 pass munges the code in the input function to better prepare it for
11 // SelectionDAG-based code generation. This works around limitations in it's
12 // basic-block-at-a-time approach. It should eventually be removed.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "codegenprepare"
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Function.h"
21 #include "llvm/InlineAsm.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/IntrinsicInst.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Analysis/Dominators.h"
26 #include "llvm/Analysis/InstructionSimplify.h"
27 #include "llvm/Analysis/ProfileInfo.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetLibraryInfo.h"
30 #include "llvm/Target/TargetLowering.h"
31 #include "llvm/Transforms/Utils/AddrModeMatcher.h"
32 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
33 #include "llvm/Transforms/Utils/Local.h"
34 #include "llvm/Transforms/Utils/BuildLibCalls.h"
35 #include "llvm/ADT/DenseMap.h"
36 #include "llvm/ADT/SmallSet.h"
37 #include "llvm/ADT/Statistic.h"
38 #include "llvm/Assembly/Writer.h"
39 #include "llvm/Support/CallSite.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/GetElementPtrTypeIterator.h"
43 #include "llvm/Support/PatternMatch.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Support/IRBuilder.h"
46 #include "llvm/Support/ValueHandle.h"
48 using namespace llvm::PatternMatch;
50 STATISTIC(NumBlocksElim, "Number of blocks eliminated");
51 STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated");
52 STATISTIC(NumGEPsElim, "Number of GEPs converted to casts");
53 STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "
55 STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "
57 STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "
58 "computations were sunk");
59 STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads");
60 STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized");
61 STATISTIC(NumRetsDup, "Number of return instructions duplicated");
62 STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved");
64 static cl::opt<bool> DisableBranchOpts(
65 "disable-cgp-branch-opts", cl::Hidden, cl::init(false),
66 cl::desc("Disable branch optimizations in CodeGenPrepare"));
68 // FIXME: Remove this abomination once all of the tests pass without it!
69 static cl::opt<bool> DisableDeleteDeadBlocks(
70 "disable-cgp-delete-dead-blocks", cl::Hidden, cl::init(false),
71 cl::desc("Disable deleting dead blocks in CodeGenPrepare"));
74 class CodeGenPrepare : public FunctionPass {
75 /// TLI - Keep a pointer of a TargetLowering to consult for determining
76 /// transformation profitability.
77 const TargetLowering *TLI;
78 const TargetLibraryInfo *TLInfo;
82 /// CurInstIterator - As we scan instructions optimizing them, this is the
83 /// next instruction to optimize. Xforms that can invalidate this should
85 BasicBlock::iterator CurInstIterator;
87 /// Keeps track of non-local addresses that have been sunk into a block.
88 /// This allows us to avoid inserting duplicate code for blocks with
89 /// multiple load/stores of the same address.
90 DenseMap<Value*, Value*> SunkAddrs;
92 /// ModifiedDT - If CFG is modified in anyway, dominator tree may need to
97 static char ID; // Pass identification, replacement for typeid
98 explicit CodeGenPrepare(const TargetLowering *tli = 0)
99 : FunctionPass(ID), TLI(tli) {
100 initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
102 bool runOnFunction(Function &F);
104 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
105 AU.addPreserved<DominatorTree>();
106 AU.addPreserved<ProfileInfo>();
107 AU.addRequired<TargetLibraryInfo>();
111 bool EliminateMostlyEmptyBlocks(Function &F);
112 bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
113 void EliminateMostlyEmptyBlock(BasicBlock *BB);
114 bool OptimizeBlock(BasicBlock &BB);
115 bool OptimizeInst(Instruction *I);
116 bool OptimizeMemoryInst(Instruction *I, Value *Addr, Type *AccessTy);
117 bool OptimizeInlineAsmInst(CallInst *CS);
118 bool OptimizeCallInst(CallInst *CI);
119 bool MoveExtToFormExtLoad(Instruction *I);
120 bool OptimizeExtUses(Instruction *I);
121 bool DupRetToEnableTailCallOpts(ReturnInst *RI);
122 bool PlaceDbgValues(Function &F);
126 char CodeGenPrepare::ID = 0;
127 INITIALIZE_PASS_BEGIN(CodeGenPrepare, "codegenprepare",
128 "Optimize for code generation", false, false)
129 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
130 INITIALIZE_PASS_END(CodeGenPrepare, "codegenprepare",
131 "Optimize for code generation", false, false)
133 FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) {
134 return new CodeGenPrepare(TLI);
137 bool CodeGenPrepare::runOnFunction(Function &F) {
138 bool EverMadeChange = false;
141 TLInfo = &getAnalysis<TargetLibraryInfo>();
142 DT = getAnalysisIfAvailable<DominatorTree>();
143 PFI = getAnalysisIfAvailable<ProfileInfo>();
145 // First pass, eliminate blocks that contain only PHI nodes and an
146 // unconditional branch.
147 EverMadeChange |= EliminateMostlyEmptyBlocks(F);
149 // llvm.dbg.value is far away from the value then iSel may not be able
150 // handle it properly. iSel will drop llvm.dbg.value if it can not
151 // find a node corresponding to the value.
152 EverMadeChange |= PlaceDbgValues(F);
154 bool MadeChange = true;
157 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
158 BasicBlock *BB = I++;
159 MadeChange |= OptimizeBlock(*BB);
161 EverMadeChange |= MadeChange;
166 if (!DisableBranchOpts) {
168 SmallPtrSet<BasicBlock*, 8> WorkList;
169 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
170 SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB));
171 MadeChange |= ConstantFoldTerminator(BB, true);
172 if (!MadeChange) continue;
174 for (SmallVectorImpl<BasicBlock*>::iterator
175 II = Successors.begin(), IE = Successors.end(); II != IE; ++II)
176 if (pred_begin(*II) == pred_end(*II))
177 WorkList.insert(*II);
180 if (!DisableDeleteDeadBlocks)
181 for (SmallPtrSet<BasicBlock*, 8>::iterator
182 I = WorkList.begin(), E = WorkList.end(); I != E; ++I)
187 EverMadeChange |= MadeChange;
190 if (ModifiedDT && DT)
191 DT->DT->recalculate(F);
193 return EverMadeChange;
196 /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes,
197 /// debug info directives, and an unconditional branch. Passes before isel
198 /// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for
199 /// isel. Start by eliminating these blocks so we can split them the way we
201 bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) {
202 bool MadeChange = false;
203 // Note that this intentionally skips the entry block.
204 for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) {
205 BasicBlock *BB = I++;
207 // If this block doesn't end with an uncond branch, ignore it.
208 BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
209 if (!BI || !BI->isUnconditional())
212 // If the instruction before the branch (skipping debug info) isn't a phi
213 // node, then other stuff is happening here.
214 BasicBlock::iterator BBI = BI;
215 if (BBI != BB->begin()) {
217 while (isa<DbgInfoIntrinsic>(BBI)) {
218 if (BBI == BB->begin())
222 if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
226 // Do not break infinite loops.
227 BasicBlock *DestBB = BI->getSuccessor(0);
231 if (!CanMergeBlocks(BB, DestBB))
234 EliminateMostlyEmptyBlock(BB);
240 /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a
241 /// single uncond branch between them, and BB contains no other non-phi
243 bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB,
244 const BasicBlock *DestBB) const {
245 // We only want to eliminate blocks whose phi nodes are used by phi nodes in
246 // the successor. If there are more complex condition (e.g. preheaders),
247 // don't mess around with them.
248 BasicBlock::const_iterator BBI = BB->begin();
249 while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
250 for (Value::const_use_iterator UI = PN->use_begin(), E = PN->use_end();
252 const Instruction *User = cast<Instruction>(*UI);
253 if (User->getParent() != DestBB || !isa<PHINode>(User))
255 // If User is inside DestBB block and it is a PHINode then check
256 // incoming value. If incoming value is not from BB then this is
257 // a complex condition (e.g. preheaders) we want to avoid here.
258 if (User->getParent() == DestBB) {
259 if (const PHINode *UPN = dyn_cast<PHINode>(User))
260 for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
261 Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
262 if (Insn && Insn->getParent() == BB &&
263 Insn->getParent() != UPN->getIncomingBlock(I))
270 // If BB and DestBB contain any common predecessors, then the phi nodes in BB
271 // and DestBB may have conflicting incoming values for the block. If so, we
272 // can't merge the block.
273 const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
274 if (!DestBBPN) return true; // no conflict.
276 // Collect the preds of BB.
277 SmallPtrSet<const BasicBlock*, 16> BBPreds;
278 if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
279 // It is faster to get preds from a PHI than with pred_iterator.
280 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
281 BBPreds.insert(BBPN->getIncomingBlock(i));
283 BBPreds.insert(pred_begin(BB), pred_end(BB));
286 // Walk the preds of DestBB.
287 for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
288 BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
289 if (BBPreds.count(Pred)) { // Common predecessor?
290 BBI = DestBB->begin();
291 while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
292 const Value *V1 = PN->getIncomingValueForBlock(Pred);
293 const Value *V2 = PN->getIncomingValueForBlock(BB);
295 // If V2 is a phi node in BB, look up what the mapped value will be.
296 if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
297 if (V2PN->getParent() == BB)
298 V2 = V2PN->getIncomingValueForBlock(Pred);
300 // If there is a conflict, bail out.
301 if (V1 != V2) return false;
310 /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and
311 /// an unconditional branch in it.
312 void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
313 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
314 BasicBlock *DestBB = BI->getSuccessor(0);
316 DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB);
318 // If the destination block has a single pred, then this is a trivial edge,
320 if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
321 if (SinglePred != DestBB) {
322 // Remember if SinglePred was the entry block of the function. If so, we
323 // will need to move BB back to the entry position.
324 bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
325 MergeBasicBlockIntoOnlyPred(DestBB, this);
327 if (isEntry && BB != &BB->getParent()->getEntryBlock())
328 BB->moveBefore(&BB->getParent()->getEntryBlock());
330 DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
335 // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB
336 // to handle the new incoming edges it is about to have.
338 for (BasicBlock::iterator BBI = DestBB->begin();
339 (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
340 // Remove the incoming value for BB, and remember it.
341 Value *InVal = PN->removeIncomingValue(BB, false);
343 // Two options: either the InVal is a phi node defined in BB or it is some
344 // value that dominates BB.
345 PHINode *InValPhi = dyn_cast<PHINode>(InVal);
346 if (InValPhi && InValPhi->getParent() == BB) {
347 // Add all of the input values of the input PHI as inputs of this phi.
348 for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
349 PN->addIncoming(InValPhi->getIncomingValue(i),
350 InValPhi->getIncomingBlock(i));
352 // Otherwise, add one instance of the dominating value for each edge that
353 // we will be adding.
354 if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
355 for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
356 PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
358 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
359 PN->addIncoming(InVal, *PI);
364 // The PHIs are now updated, change everything that refers to BB to use
365 // DestBB and remove BB.
366 BB->replaceAllUsesWith(DestBB);
367 if (DT && !ModifiedDT) {
368 BasicBlock *BBIDom = DT->getNode(BB)->getIDom()->getBlock();
369 BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock();
370 BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom);
371 DT->changeImmediateDominator(DestBB, NewIDom);
375 PFI->replaceAllUses(BB, DestBB);
376 PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB));
378 BB->eraseFromParent();
381 DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
384 /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop
385 /// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC),
386 /// sink it into user blocks to reduce the number of virtual
387 /// registers that must be created and coalesced.
389 /// Return true if any changes are made.
391 static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
392 // If this is a noop copy,
393 EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
394 EVT DstVT = TLI.getValueType(CI->getType());
396 // This is an fp<->int conversion?
397 if (SrcVT.isInteger() != DstVT.isInteger())
400 // If this is an extension, it will be a zero or sign extension, which
402 if (SrcVT.bitsLT(DstVT)) return false;
404 // If these values will be promoted, find out what they will be promoted
405 // to. This helps us consider truncates on PPC as noop copies when they
407 if (TLI.getTypeAction(CI->getContext(), SrcVT) ==
408 TargetLowering::TypePromoteInteger)
409 SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
410 if (TLI.getTypeAction(CI->getContext(), DstVT) ==
411 TargetLowering::TypePromoteInteger)
412 DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
414 // If, after promotion, these are the same types, this is a noop copy.
418 BasicBlock *DefBB = CI->getParent();
420 /// InsertedCasts - Only insert a cast in each block once.
421 DenseMap<BasicBlock*, CastInst*> InsertedCasts;
423 bool MadeChange = false;
424 for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
426 Use &TheUse = UI.getUse();
427 Instruction *User = cast<Instruction>(*UI);
429 // Figure out which BB this cast is used in. For PHI's this is the
430 // appropriate predecessor block.
431 BasicBlock *UserBB = User->getParent();
432 if (PHINode *PN = dyn_cast<PHINode>(User)) {
433 UserBB = PN->getIncomingBlock(UI);
436 // Preincrement use iterator so we don't invalidate it.
439 // If this user is in the same block as the cast, don't change the cast.
440 if (UserBB == DefBB) continue;
442 // If we have already inserted a cast into this block, use it.
443 CastInst *&InsertedCast = InsertedCasts[UserBB];
446 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
448 CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
453 // Replace a use of the cast with a use of the new cast.
454 TheUse = InsertedCast;
458 // If we removed all uses, nuke the cast.
459 if (CI->use_empty()) {
460 CI->eraseFromParent();
467 /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce
468 /// the number of virtual registers that must be created and coalesced. This is
469 /// a clear win except on targets with multiple condition code registers
470 /// (PowerPC), where it might lose; some adjustment may be wanted there.
472 /// Return true if any changes are made.
473 static bool OptimizeCmpExpression(CmpInst *CI) {
474 BasicBlock *DefBB = CI->getParent();
476 /// InsertedCmp - Only insert a cmp in each block once.
477 DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
479 bool MadeChange = false;
480 for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
482 Use &TheUse = UI.getUse();
483 Instruction *User = cast<Instruction>(*UI);
485 // Preincrement use iterator so we don't invalidate it.
488 // Don't bother for PHI nodes.
489 if (isa<PHINode>(User))
492 // Figure out which BB this cmp is used in.
493 BasicBlock *UserBB = User->getParent();
495 // If this user is in the same block as the cmp, don't change the cmp.
496 if (UserBB == DefBB) continue;
498 // If we have already inserted a cmp into this block, use it.
499 CmpInst *&InsertedCmp = InsertedCmps[UserBB];
502 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
504 CmpInst::Create(CI->getOpcode(),
505 CI->getPredicate(), CI->getOperand(0),
506 CI->getOperand(1), "", InsertPt);
510 // Replace a use of the cmp with a use of the new cmp.
511 TheUse = InsertedCmp;
515 // If we removed all uses, nuke the cmp.
517 CI->eraseFromParent();
523 class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls {
525 void replaceCall(Value *With) {
526 CI->replaceAllUsesWith(With);
527 CI->eraseFromParent();
529 bool isFoldable(unsigned SizeCIOp, unsigned, bool) const {
530 if (ConstantInt *SizeCI =
531 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp)))
532 return SizeCI->isAllOnesValue();
536 } // end anonymous namespace
538 bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) {
539 BasicBlock *BB = CI->getParent();
541 // Lower inline assembly if we can.
542 // If we found an inline asm expession, and if the target knows how to
543 // lower it to normal LLVM code, do so now.
544 if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
545 if (TLI->ExpandInlineAsm(CI)) {
546 // Avoid invalidating the iterator.
547 CurInstIterator = BB->begin();
548 // Avoid processing instructions out of order, which could cause
549 // reuse before a value is defined.
553 // Sink address computing for memory operands into the block.
554 if (OptimizeInlineAsmInst(CI))
558 // Lower all uses of llvm.objectsize.*
559 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
560 if (II && II->getIntrinsicID() == Intrinsic::objectsize) {
561 bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1);
562 Type *ReturnTy = CI->getType();
563 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
565 // Substituting this can cause recursive simplifications, which can
566 // invalidate our iterator. Use a WeakVH to hold onto it in case this
568 WeakVH IterHandle(CurInstIterator);
570 ReplaceAndSimplifyAllUses(CI, RetVal, TLI ? TLI->getTargetData() : 0,
571 TLInfo, ModifiedDT ? 0 : DT);
573 // If the iterator instruction was recursively deleted, start over at the
574 // start of the block.
575 if (IterHandle != CurInstIterator) {
576 CurInstIterator = BB->begin();
583 SmallVector<Value*, 2> PtrOps;
585 if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy))
586 while (!PtrOps.empty())
587 if (OptimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy))
591 // From here on out we're working with named functions.
592 if (CI->getCalledFunction() == 0) return false;
594 // We'll need TargetData from here on out.
595 const TargetData *TD = TLI ? TLI->getTargetData() : 0;
596 if (!TD) return false;
598 // Lower all default uses of _chk calls. This is very similar
599 // to what InstCombineCalls does, but here we are only lowering calls
600 // that have the default "don't know" as the objectsize. Anything else
601 // should be left alone.
602 CodeGenPrepareFortifiedLibCalls Simplifier;
603 return Simplifier.fold(CI, TD);
606 /// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return
607 /// instructions to the predecessor to enable tail call optimizations. The
608 /// case it is currently looking for is:
610 /// %tmp0 = tail call i32 @f0()
613 /// %tmp1 = tail call i32 @f1()
616 /// %tmp2 = tail call i32 @f2()
619 /// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
625 /// %tmp0 = tail call i32 @f0()
628 /// %tmp1 = tail call i32 @f1()
631 /// %tmp2 = tail call i32 @f2()
634 bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
638 Value *V = RI->getReturnValue();
639 PHINode *PN = V ? dyn_cast<PHINode>(V) : NULL;
643 BasicBlock *BB = RI->getParent();
644 if (PN && PN->getParent() != BB)
647 // It's not safe to eliminate the sign / zero extension of the return value.
648 // See llvm::isInTailCallPosition().
649 const Function *F = BB->getParent();
650 Attributes CallerRetAttr = F->getAttributes().getRetAttributes();
651 if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
654 // Make sure there are no instructions between the PHI and return, or that the
655 // return is the first instruction in the block.
657 BasicBlock::iterator BI = BB->begin();
658 do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
662 BasicBlock::iterator BI = BB->begin();
663 while (isa<DbgInfoIntrinsic>(BI)) ++BI;
668 /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
670 SmallVector<CallInst*, 4> TailCalls;
672 for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
673 CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
674 // Make sure the phi value is indeed produced by the tail call.
675 if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
676 TLI->mayBeEmittedAsTailCall(CI))
677 TailCalls.push_back(CI);
680 SmallPtrSet<BasicBlock*, 4> VisitedBBs;
681 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
682 if (!VisitedBBs.insert(*PI))
685 BasicBlock::InstListType &InstList = (*PI)->getInstList();
686 BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
687 BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
688 do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI));
692 CallInst *CI = dyn_cast<CallInst>(&*RI);
693 if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI))
694 TailCalls.push_back(CI);
698 bool Changed = false;
699 for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) {
700 CallInst *CI = TailCalls[i];
703 // Conservatively require the attributes of the call to match those of the
704 // return. Ignore noalias because it doesn't affect the call sequence.
705 Attributes CalleeRetAttr = CS.getAttributes().getRetAttributes();
706 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
709 // Make sure the call instruction is followed by an unconditional branch to
711 BasicBlock *CallBB = CI->getParent();
712 BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator());
713 if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
716 // Duplicate the return into CallBB.
717 (void)FoldReturnIntoUncondBranch(RI, BB, CallBB);
718 ModifiedDT = Changed = true;
722 // If we eliminated all predecessors of the block, delete the block now.
723 if (Changed && pred_begin(BB) == pred_end(BB))
724 BB->eraseFromParent();
729 //===----------------------------------------------------------------------===//
730 // Memory Optimization
731 //===----------------------------------------------------------------------===//
733 /// IsNonLocalValue - Return true if the specified values are defined in a
734 /// different basic block than BB.
735 static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
736 if (Instruction *I = dyn_cast<Instruction>(V))
737 return I->getParent() != BB;
741 /// OptimizeMemoryInst - Load and Store Instructions often have
742 /// addressing modes that can do significant amounts of computation. As such,
743 /// instruction selection will try to get the load or store to do as much
744 /// computation as possible for the program. The problem is that isel can only
745 /// see within a single block. As such, we sink as much legal addressing mode
746 /// stuff into the block as possible.
748 /// This method is used to optimize both load/store and inline asms with memory
750 bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
754 // Try to collapse single-value PHI nodes. This is necessary to undo
755 // unprofitable PRE transformations.
756 SmallVector<Value*, 8> worklist;
757 SmallPtrSet<Value*, 16> Visited;
758 worklist.push_back(Addr);
760 // Use a worklist to iteratively look through PHI nodes, and ensure that
761 // the addressing mode obtained from the non-PHI roots of the graph
763 Value *Consensus = 0;
764 unsigned NumUsesConsensus = 0;
765 bool IsNumUsesConsensusValid = false;
766 SmallVector<Instruction*, 16> AddrModeInsts;
767 ExtAddrMode AddrMode;
768 while (!worklist.empty()) {
769 Value *V = worklist.back();
772 // Break use-def graph loops.
773 if (!Visited.insert(V)) {
778 // For a PHI node, push all of its incoming values.
779 if (PHINode *P = dyn_cast<PHINode>(V)) {
780 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i)
781 worklist.push_back(P->getIncomingValue(i));
785 // For non-PHIs, determine the addressing mode being computed.
786 SmallVector<Instruction*, 16> NewAddrModeInsts;
787 ExtAddrMode NewAddrMode =
788 AddressingModeMatcher::Match(V, AccessTy, MemoryInst,
789 NewAddrModeInsts, *TLI);
791 // This check is broken into two cases with very similar code to avoid using
792 // getNumUses() as much as possible. Some values have a lot of uses, so
793 // calling getNumUses() unconditionally caused a significant compile-time
797 AddrMode = NewAddrMode;
798 AddrModeInsts = NewAddrModeInsts;
800 } else if (NewAddrMode == AddrMode) {
801 if (!IsNumUsesConsensusValid) {
802 NumUsesConsensus = Consensus->getNumUses();
803 IsNumUsesConsensusValid = true;
806 // Ensure that the obtained addressing mode is equivalent to that obtained
807 // for all other roots of the PHI traversal. Also, when choosing one
808 // such root as representative, select the one with the most uses in order
809 // to keep the cost modeling heuristics in AddressingModeMatcher
811 unsigned NumUses = V->getNumUses();
812 if (NumUses > NumUsesConsensus) {
814 NumUsesConsensus = NumUses;
815 AddrModeInsts = NewAddrModeInsts;
824 // If the addressing mode couldn't be determined, or if multiple different
825 // ones were determined, bail out now.
826 if (!Consensus) return false;
828 // Check to see if any of the instructions supersumed by this addr mode are
829 // non-local to I's BB.
830 bool AnyNonLocal = false;
831 for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) {
832 if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) {
838 // If all the instructions matched are already in this BB, don't do anything.
840 DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n");
844 // Insert this computation right after this user. Since our caller is
845 // scanning from the top of the BB to the bottom, reuse of the expr are
846 // guaranteed to happen later.
847 IRBuilder<> Builder(MemoryInst);
849 // Now that we determined the addressing expression we want to use and know
850 // that we have to sink it into this block. Check to see if we have already
851 // done this for some other load/store instr in this block. If so, reuse the
853 Value *&SunkAddr = SunkAddrs[Addr];
855 DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for "
857 if (SunkAddr->getType() != Addr->getType())
858 SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType());
860 DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
863 TLI->getTargetData()->getIntPtrType(AccessTy->getContext());
867 // Start with the base register. Do this first so that subsequent address
868 // matching finds it last, which will prevent it from trying to match it
869 // as the scaled value in case it happens to be a mul. That would be
870 // problematic if we've sunk a different mul for the scale, because then
871 // we'd end up sinking both muls.
872 if (AddrMode.BaseReg) {
873 Value *V = AddrMode.BaseReg;
874 if (V->getType()->isPointerTy())
875 V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
876 if (V->getType() != IntPtrTy)
877 V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
881 // Add the scale value.
882 if (AddrMode.Scale) {
883 Value *V = AddrMode.ScaledReg;
884 if (V->getType() == IntPtrTy) {
886 } else if (V->getType()->isPointerTy()) {
887 V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
888 } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
889 cast<IntegerType>(V->getType())->getBitWidth()) {
890 V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
892 V = Builder.CreateSExt(V, IntPtrTy, "sunkaddr");
894 if (AddrMode.Scale != 1)
895 V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
898 Result = Builder.CreateAdd(Result, V, "sunkaddr");
903 // Add in the BaseGV if present.
904 if (AddrMode.BaseGV) {
905 Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr");
907 Result = Builder.CreateAdd(Result, V, "sunkaddr");
912 // Add in the Base Offset if present.
913 if (AddrMode.BaseOffs) {
914 Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
916 Result = Builder.CreateAdd(Result, V, "sunkaddr");
922 SunkAddr = Constant::getNullValue(Addr->getType());
924 SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr");
927 MemoryInst->replaceUsesOfWith(Repl, SunkAddr);
929 // If we have no uses, recursively delete the value and all dead instructions
931 if (Repl->use_empty()) {
932 // This can cause recursive deletion, which can invalidate our iterator.
933 // Use a WeakVH to hold onto it in case this happens.
934 WeakVH IterHandle(CurInstIterator);
935 BasicBlock *BB = CurInstIterator->getParent();
937 RecursivelyDeleteTriviallyDeadInstructions(Repl);
939 if (IterHandle != CurInstIterator) {
940 // If the iterator instruction was recursively deleted, start over at the
941 // start of the block.
942 CurInstIterator = BB->begin();
945 // This address is now available for reassignment, so erase the table
946 // entry; we don't want to match some completely different instruction.
954 /// OptimizeInlineAsmInst - If there are any memory operands, use
955 /// OptimizeMemoryInst to sink their address computing into the block when
956 /// possible / profitable.
957 bool CodeGenPrepare::OptimizeInlineAsmInst(CallInst *CS) {
958 bool MadeChange = false;
960 TargetLowering::AsmOperandInfoVector
961 TargetConstraints = TLI->ParseConstraints(CS);
963 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
964 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
966 // Compute the constraint code and ConstraintType to use.
967 TLI->ComputeConstraintToUse(OpInfo, SDValue());
969 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
971 Value *OpVal = CS->getArgOperand(ArgNo++);
972 MadeChange |= OptimizeMemoryInst(CS, OpVal, OpVal->getType());
973 } else if (OpInfo.Type == InlineAsm::isInput)
980 /// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same
981 /// basic block as the load, unless conditions are unfavorable. This allows
982 /// SelectionDAG to fold the extend into the load.
984 bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) {
985 // Look for a load being extended.
986 LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0));
987 if (!LI) return false;
989 // If they're already in the same block, there's nothing to do.
990 if (LI->getParent() == I->getParent())
993 // If the load has other users and the truncate is not free, this probably
995 if (!LI->hasOneUse() &&
996 TLI && (TLI->isTypeLegal(TLI->getValueType(LI->getType())) ||
997 !TLI->isTypeLegal(TLI->getValueType(I->getType()))) &&
998 !TLI->isTruncateFree(I->getType(), LI->getType()))
1001 // Check whether the target supports casts folded into loads.
1003 if (isa<ZExtInst>(I))
1004 LType = ISD::ZEXTLOAD;
1006 assert(isa<SExtInst>(I) && "Unexpected ext type!");
1007 LType = ISD::SEXTLOAD;
1009 if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType())))
1012 // Move the extend into the same block as the load, so that SelectionDAG
1014 I->removeFromParent();
1020 bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
1021 BasicBlock *DefBB = I->getParent();
1023 // If the result of a {s|z}ext and its source are both live out, rewrite all
1024 // other uses of the source with result of extension.
1025 Value *Src = I->getOperand(0);
1026 if (Src->hasOneUse())
1029 // Only do this xform if truncating is free.
1030 if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
1033 // Only safe to perform the optimization if the source is also defined in
1035 if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
1038 bool DefIsLiveOut = false;
1039 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1041 Instruction *User = cast<Instruction>(*UI);
1043 // Figure out which BB this ext is used in.
1044 BasicBlock *UserBB = User->getParent();
1045 if (UserBB == DefBB) continue;
1046 DefIsLiveOut = true;
1052 // Make sure non of the uses are PHI nodes.
1053 for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
1055 Instruction *User = cast<Instruction>(*UI);
1056 BasicBlock *UserBB = User->getParent();
1057 if (UserBB == DefBB) continue;
1058 // Be conservative. We don't want this xform to end up introducing
1059 // reloads just before load / store instructions.
1060 if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User))
1064 // InsertedTruncs - Only insert one trunc in each block once.
1065 DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
1067 bool MadeChange = false;
1068 for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
1070 Use &TheUse = UI.getUse();
1071 Instruction *User = cast<Instruction>(*UI);
1073 // Figure out which BB this ext is used in.
1074 BasicBlock *UserBB = User->getParent();
1075 if (UserBB == DefBB) continue;
1077 // Both src and def are live in this block. Rewrite the use.
1078 Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
1080 if (!InsertedTrunc) {
1081 BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
1082 InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
1085 // Replace a use of the {s|z}ext source with a use of the result.
1086 TheUse = InsertedTrunc;
1094 bool CodeGenPrepare::OptimizeInst(Instruction *I) {
1095 if (PHINode *P = dyn_cast<PHINode>(I)) {
1096 // It is possible for very late stage optimizations (such as SimplifyCFG)
1097 // to introduce PHI nodes too late to be cleaned up. If we detect such a
1098 // trivial PHI, go ahead and zap it here.
1099 if (Value *V = SimplifyInstruction(P)) {
1100 P->replaceAllUsesWith(V);
1101 P->eraseFromParent();
1108 if (CastInst *CI = dyn_cast<CastInst>(I)) {
1109 // If the source of the cast is a constant, then this should have
1110 // already been constant folded. The only reason NOT to constant fold
1111 // it is if something (e.g. LSR) was careful to place the constant
1112 // evaluation in a block other than then one that uses it (e.g. to hoist
1113 // the address of globals out of a loop). If this is the case, we don't
1114 // want to forward-subst the cast.
1115 if (isa<Constant>(CI->getOperand(0)))
1118 if (TLI && OptimizeNoopCopyExpression(CI, *TLI))
1121 if (isa<ZExtInst>(I) || isa<SExtInst>(I)) {
1122 bool MadeChange = MoveExtToFormExtLoad(I);
1123 return MadeChange | OptimizeExtUses(I);
1128 if (CmpInst *CI = dyn_cast<CmpInst>(I))
1129 return OptimizeCmpExpression(CI);
1131 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1133 return OptimizeMemoryInst(I, I->getOperand(0), LI->getType());
1137 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1139 return OptimizeMemoryInst(I, SI->getOperand(1),
1140 SI->getOperand(0)->getType());
1144 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
1145 if (GEPI->hasAllZeroIndices()) {
1146 /// The GEP operand must be a pointer, so must its result -> BitCast
1147 Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
1148 GEPI->getName(), GEPI);
1149 GEPI->replaceAllUsesWith(NC);
1150 GEPI->eraseFromParent();
1158 if (CallInst *CI = dyn_cast<CallInst>(I))
1159 return OptimizeCallInst(CI);
1161 if (ReturnInst *RI = dyn_cast<ReturnInst>(I))
1162 return DupRetToEnableTailCallOpts(RI);
1167 // In this pass we look for GEP and cast instructions that are used
1168 // across basic blocks and rewrite them to improve basic-block-at-a-time
1170 bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) {
1172 bool MadeChange = false;
1174 CurInstIterator = BB.begin();
1175 for (BasicBlock::iterator E = BB.end(); CurInstIterator != E; )
1176 MadeChange |= OptimizeInst(CurInstIterator++);
1181 // llvm.dbg.value is far away from the value then iSel may not be able
1182 // handle it properly. iSel will drop llvm.dbg.value if it can not
1183 // find a node corresponding to the value.
1184 bool CodeGenPrepare::PlaceDbgValues(Function &F) {
1185 bool MadeChange = false;
1186 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
1187 Instruction *PrevNonDbgInst = NULL;
1188 for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {
1189 Instruction *Insn = BI; ++BI;
1190 DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn);
1192 PrevNonDbgInst = Insn;
1196 Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue());
1197 if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) {
1198 DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI);
1199 DVI->removeFromParent();
1200 if (isa<PHINode>(VI))
1201 DVI->insertBefore(VI->getParent()->getFirstInsertionPt());
1203 DVI->insertAfter(VI);