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/ProfileInfo.h"
26
#include "llvm/Target/TargetData.h"
27
#include "llvm/Target/TargetLowering.h"
28
#include "llvm/Transforms/Utils/AddrModeMatcher.h"
29
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
30
#include "llvm/Transforms/Utils/Local.h"
31
#include "llvm/Transforms/Utils/BuildLibCalls.h"
32
#include "llvm/ADT/DenseMap.h"
33
#include "llvm/ADT/SmallSet.h"
34
#include "llvm/Assembly/Writer.h"
35
#include "llvm/Support/CallSite.h"
36
#include "llvm/Support/CommandLine.h"
37
#include "llvm/Support/Debug.h"
38
#include "llvm/Support/GetElementPtrTypeIterator.h"
39
#include "llvm/Support/PatternMatch.h"
40
#include "llvm/Support/raw_ostream.h"
41
#include "llvm/Support/IRBuilder.h"
43
using namespace llvm::PatternMatch;
46
CriticalEdgeSplit("cgp-critical-edge-splitting",
47
cl::desc("Split critical edges during codegen prepare"),
48
cl::init(true), cl::Hidden);
51
class CodeGenPrepare : public FunctionPass {
52
/// TLI - Keep a pointer of a TargetLowering to consult for determining
53
/// transformation profitability.
54
const TargetLowering *TLI;
57
/// BackEdges - Keep a set of all the loop back edges.
59
SmallSet<std::pair<const BasicBlock*, const BasicBlock*>, 8> BackEdges;
61
static char ID; // Pass identification, replacement for typeid
62
explicit CodeGenPrepare(const TargetLowering *tli = 0)
63
: FunctionPass(ID), TLI(tli) {}
64
bool runOnFunction(Function &F);
66
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67
AU.addPreserved<ProfileInfo>();
70
virtual void releaseMemory() {
75
bool EliminateMostlyEmptyBlocks(Function &F);
76
bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
77
void EliminateMostlyEmptyBlock(BasicBlock *BB);
78
bool OptimizeBlock(BasicBlock &BB);
79
bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy,
80
DenseMap<Value*,Value*> &SunkAddrs);
81
bool OptimizeInlineAsmInst(Instruction *I, CallSite CS,
82
DenseMap<Value*,Value*> &SunkAddrs);
83
bool OptimizeCallInst(CallInst *CI);
84
bool MoveExtToFormExtLoad(Instruction *I);
85
bool OptimizeExtUses(Instruction *I);
86
void findLoopBackEdges(const Function &F);
90
char CodeGenPrepare::ID = 0;
91
INITIALIZE_PASS(CodeGenPrepare, "codegenprepare",
92
"Optimize for code generation", false, false);
94
FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) {
95
return new CodeGenPrepare(TLI);
98
/// findLoopBackEdges - Do a DFS walk to find loop back edges.
100
void CodeGenPrepare::findLoopBackEdges(const Function &F) {
101
SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges;
102
FindFunctionBackedges(F, Edges);
104
BackEdges.insert(Edges.begin(), Edges.end());
108
bool CodeGenPrepare::runOnFunction(Function &F) {
109
bool EverMadeChange = false;
111
PFI = getAnalysisIfAvailable<ProfileInfo>();
112
// First pass, eliminate blocks that contain only PHI nodes and an
113
// unconditional branch.
114
EverMadeChange |= EliminateMostlyEmptyBlocks(F);
116
// Now find loop back edges.
117
findLoopBackEdges(F);
119
bool MadeChange = true;
122
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
123
MadeChange |= OptimizeBlock(*BB);
124
EverMadeChange |= MadeChange;
126
return EverMadeChange;
129
/// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes,
130
/// debug info directives, and an unconditional branch. Passes before isel
131
/// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for
132
/// isel. Start by eliminating these blocks so we can split them the way we
134
bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) {
135
bool MadeChange = false;
136
// Note that this intentionally skips the entry block.
137
for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) {
138
BasicBlock *BB = I++;
140
// If this block doesn't end with an uncond branch, ignore it.
141
BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
142
if (!BI || !BI->isUnconditional())
145
// If the instruction before the branch (skipping debug info) isn't a phi
146
// node, then other stuff is happening here.
147
BasicBlock::iterator BBI = BI;
148
if (BBI != BB->begin()) {
150
while (isa<DbgInfoIntrinsic>(BBI)) {
151
if (BBI == BB->begin())
155
if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
159
// Do not break infinite loops.
160
BasicBlock *DestBB = BI->getSuccessor(0);
164
if (!CanMergeBlocks(BB, DestBB))
167
EliminateMostlyEmptyBlock(BB);
173
/// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a
174
/// single uncond branch between them, and BB contains no other non-phi
176
bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB,
177
const BasicBlock *DestBB) const {
178
// We only want to eliminate blocks whose phi nodes are used by phi nodes in
179
// the successor. If there are more complex condition (e.g. preheaders),
180
// don't mess around with them.
181
BasicBlock::const_iterator BBI = BB->begin();
182
while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
183
for (Value::const_use_iterator UI = PN->use_begin(), E = PN->use_end();
185
const Instruction *User = cast<Instruction>(*UI);
186
if (User->getParent() != DestBB || !isa<PHINode>(User))
188
// If User is inside DestBB block and it is a PHINode then check
189
// incoming value. If incoming value is not from BB then this is
190
// a complex condition (e.g. preheaders) we want to avoid here.
191
if (User->getParent() == DestBB) {
192
if (const PHINode *UPN = dyn_cast<PHINode>(User))
193
for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
194
Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
195
if (Insn && Insn->getParent() == BB &&
196
Insn->getParent() != UPN->getIncomingBlock(I))
203
// If BB and DestBB contain any common predecessors, then the phi nodes in BB
204
// and DestBB may have conflicting incoming values for the block. If so, we
205
// can't merge the block.
206
const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
207
if (!DestBBPN) return true; // no conflict.
209
// Collect the preds of BB.
210
SmallPtrSet<const BasicBlock*, 16> BBPreds;
211
if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
212
// It is faster to get preds from a PHI than with pred_iterator.
213
for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
214
BBPreds.insert(BBPN->getIncomingBlock(i));
216
BBPreds.insert(pred_begin(BB), pred_end(BB));
219
// Walk the preds of DestBB.
220
for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
221
BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
222
if (BBPreds.count(Pred)) { // Common predecessor?
223
BBI = DestBB->begin();
224
while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
225
const Value *V1 = PN->getIncomingValueForBlock(Pred);
226
const Value *V2 = PN->getIncomingValueForBlock(BB);
228
// If V2 is a phi node in BB, look up what the mapped value will be.
229
if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
230
if (V2PN->getParent() == BB)
231
V2 = V2PN->getIncomingValueForBlock(Pred);
233
// If there is a conflict, bail out.
234
if (V1 != V2) return false;
243
/// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and
244
/// an unconditional branch in it.
245
void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
246
BranchInst *BI = cast<BranchInst>(BB->getTerminator());
247
BasicBlock *DestBB = BI->getSuccessor(0);
249
DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB);
251
// If the destination block has a single pred, then this is a trivial edge,
253
if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
254
if (SinglePred != DestBB) {
255
// Remember if SinglePred was the entry block of the function. If so, we
256
// will need to move BB back to the entry position.
257
bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
258
MergeBasicBlockIntoOnlyPred(DestBB, this);
260
if (isEntry && BB != &BB->getParent()->getEntryBlock())
261
BB->moveBefore(&BB->getParent()->getEntryBlock());
263
DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
268
// Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB
269
// to handle the new incoming edges it is about to have.
271
for (BasicBlock::iterator BBI = DestBB->begin();
272
(PN = dyn_cast<PHINode>(BBI)); ++BBI) {
273
// Remove the incoming value for BB, and remember it.
274
Value *InVal = PN->removeIncomingValue(BB, false);
276
// Two options: either the InVal is a phi node defined in BB or it is some
277
// value that dominates BB.
278
PHINode *InValPhi = dyn_cast<PHINode>(InVal);
279
if (InValPhi && InValPhi->getParent() == BB) {
280
// Add all of the input values of the input PHI as inputs of this phi.
281
for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
282
PN->addIncoming(InValPhi->getIncomingValue(i),
283
InValPhi->getIncomingBlock(i));
285
// Otherwise, add one instance of the dominating value for each edge that
286
// we will be adding.
287
if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
288
for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
289
PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
291
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
292
PN->addIncoming(InVal, *PI);
297
// The PHIs are now updated, change everything that refers to BB to use
298
// DestBB and remove BB.
299
BB->replaceAllUsesWith(DestBB);
301
PFI->replaceAllUses(BB, DestBB);
302
PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB));
304
BB->eraseFromParent();
306
DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
309
/// FindReusablePredBB - Check all of the predecessors of the block DestPHI
310
/// lives in to see if there is a block that we can reuse as a critical edge
312
static BasicBlock *FindReusablePredBB(PHINode *DestPHI, BasicBlock *TIBB) {
313
BasicBlock *Dest = DestPHI->getParent();
315
/// TIPHIValues - This array is lazily computed to determine the values of
316
/// PHIs in Dest that TI would provide.
317
SmallVector<Value*, 32> TIPHIValues;
319
/// TIBBEntryNo - This is a cache to speed up pred queries for TIBB.
320
unsigned TIBBEntryNo = 0;
322
// Check to see if Dest has any blocks that can be used as a split edge for
324
for (unsigned pi = 0, e = DestPHI->getNumIncomingValues(); pi != e; ++pi) {
325
BasicBlock *Pred = DestPHI->getIncomingBlock(pi);
326
// To be usable, the pred has to end with an uncond branch to the dest.
327
BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator());
328
if (!PredBr || !PredBr->isUnconditional())
330
// Must be empty other than the branch and debug info.
331
BasicBlock::iterator I = Pred->begin();
332
while (isa<DbgInfoIntrinsic>(I))
336
// Cannot be the entry block; its label does not get emitted.
337
if (Pred == &Dest->getParent()->getEntryBlock())
340
// Finally, since we know that Dest has phi nodes in it, we have to make
341
// sure that jumping to Pred will have the same effect as going to Dest in
342
// terms of PHI values.
345
unsigned PredEntryNo = pi;
347
bool FoundMatch = true;
348
for (BasicBlock::iterator I = Dest->begin();
349
(PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) {
350
if (PHINo == TIPHIValues.size()) {
351
if (PN->getIncomingBlock(TIBBEntryNo) != TIBB)
352
TIBBEntryNo = PN->getBasicBlockIndex(TIBB);
353
TIPHIValues.push_back(PN->getIncomingValue(TIBBEntryNo));
356
// If the PHI entry doesn't work, we can't use this pred.
357
if (PN->getIncomingBlock(PredEntryNo) != Pred)
358
PredEntryNo = PN->getBasicBlockIndex(Pred);
360
if (TIPHIValues[PHINo] != PN->getIncomingValue(PredEntryNo)) {
366
// If we found a workable predecessor, change TI to branch to Succ.
374
/// SplitEdgeNicely - Split the critical edge from TI to its specified
375
/// successor if it will improve codegen. We only do this if the successor has
376
/// phi nodes (otherwise critical edges are ok). If there is already another
377
/// predecessor of the succ that is empty (and thus has no phi nodes), use it
378
/// instead of introducing a new block.
379
static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum,
380
SmallSet<std::pair<const BasicBlock*,
381
const BasicBlock*>, 8> &BackEdges,
383
BasicBlock *TIBB = TI->getParent();
384
BasicBlock *Dest = TI->getSuccessor(SuccNum);
385
assert(isa<PHINode>(Dest->begin()) &&
386
"This should only be called if Dest has a PHI!");
387
PHINode *DestPHI = cast<PHINode>(Dest->begin());
389
// Do not split edges to EH landing pads.
390
if (InvokeInst *Invoke = dyn_cast<InvokeInst>(TI))
391
if (Invoke->getSuccessor(1) == Dest)
394
// As a hack, never split backedges of loops. Even though the copy for any
395
// PHIs inserted on the backedge would be dead for exits from the loop, we
396
// assume that the cost of *splitting* the backedge would be too high.
397
if (BackEdges.count(std::make_pair(TIBB, Dest)))
400
if (BasicBlock *ReuseBB = FindReusablePredBB(DestPHI, TIBB)) {
401
ProfileInfo *PFI = P->getAnalysisIfAvailable<ProfileInfo>();
403
PFI->splitEdge(TIBB, Dest, ReuseBB);
404
Dest->removePredecessor(TIBB);
405
TI->setSuccessor(SuccNum, ReuseBB);
409
SplitCriticalEdge(TI, SuccNum, P, true);
413
/// OptimizeNoopCopyExpression - If the specified cast instruction is a noop
414
/// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC),
415
/// sink it into user blocks to reduce the number of virtual
416
/// registers that must be created and coalesced.
418
/// Return true if any changes are made.
420
static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
421
// If this is a noop copy,
422
EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
423
EVT DstVT = TLI.getValueType(CI->getType());
425
// This is an fp<->int conversion?
426
if (SrcVT.isInteger() != DstVT.isInteger())
429
// If this is an extension, it will be a zero or sign extension, which
431
if (SrcVT.bitsLT(DstVT)) return false;
433
// If these values will be promoted, find out what they will be promoted
434
// to. This helps us consider truncates on PPC as noop copies when they
436
if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote)
437
SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
438
if (TLI.getTypeAction(DstVT) == TargetLowering::Promote)
439
DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
441
// If, after promotion, these are the same types, this is a noop copy.
445
BasicBlock *DefBB = CI->getParent();
447
/// InsertedCasts - Only insert a cast in each block once.
448
DenseMap<BasicBlock*, CastInst*> InsertedCasts;
450
bool MadeChange = false;
451
for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
453
Use &TheUse = UI.getUse();
454
Instruction *User = cast<Instruction>(*UI);
456
// Figure out which BB this cast is used in. For PHI's this is the
457
// appropriate predecessor block.
458
BasicBlock *UserBB = User->getParent();
459
if (PHINode *PN = dyn_cast<PHINode>(User)) {
460
UserBB = PN->getIncomingBlock(UI);
463
// Preincrement use iterator so we don't invalidate it.
466
// If this user is in the same block as the cast, don't change the cast.
467
if (UserBB == DefBB) continue;
469
// If we have already inserted a cast into this block, use it.
470
CastInst *&InsertedCast = InsertedCasts[UserBB];
473
BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
476
CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
481
// Replace a use of the cast with a use of the new cast.
482
TheUse = InsertedCast;
485
// If we removed all uses, nuke the cast.
486
if (CI->use_empty()) {
487
CI->eraseFromParent();
494
/// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce
495
/// the number of virtual registers that must be created and coalesced. This is
496
/// a clear win except on targets with multiple condition code registers
497
/// (PowerPC), where it might lose; some adjustment may be wanted there.
499
/// Return true if any changes are made.
500
static bool OptimizeCmpExpression(CmpInst *CI) {
501
BasicBlock *DefBB = CI->getParent();
503
/// InsertedCmp - Only insert a cmp in each block once.
504
DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
506
bool MadeChange = false;
507
for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
509
Use &TheUse = UI.getUse();
510
Instruction *User = cast<Instruction>(*UI);
512
// Preincrement use iterator so we don't invalidate it.
515
// Don't bother for PHI nodes.
516
if (isa<PHINode>(User))
519
// Figure out which BB this cmp is used in.
520
BasicBlock *UserBB = User->getParent();
522
// If this user is in the same block as the cmp, don't change the cmp.
523
if (UserBB == DefBB) continue;
525
// If we have already inserted a cmp into this block, use it.
526
CmpInst *&InsertedCmp = InsertedCmps[UserBB];
529
BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
532
CmpInst::Create(CI->getOpcode(),
533
CI->getPredicate(), CI->getOperand(0),
534
CI->getOperand(1), "", InsertPt);
538
// Replace a use of the cmp with a use of the new cmp.
539
TheUse = InsertedCmp;
542
// If we removed all uses, nuke the cmp.
544
CI->eraseFromParent();
550
class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls {
552
void replaceCall(Value *With) {
553
CI->replaceAllUsesWith(With);
554
CI->eraseFromParent();
556
bool isFoldable(unsigned SizeCIOp, unsigned, bool) const {
557
if (ConstantInt *SizeCI =
558
dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp)))
559
return SizeCI->isAllOnesValue();
563
} // end anonymous namespace
565
bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) {
566
// Lower all uses of llvm.objectsize.*
567
IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
568
if (II && II->getIntrinsicID() == Intrinsic::objectsize) {
569
bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1);
570
const Type *ReturnTy = CI->getType();
571
Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
572
CI->replaceAllUsesWith(RetVal);
573
CI->eraseFromParent();
577
// From here on out we're working with named functions.
578
if (CI->getCalledFunction() == 0) return false;
580
// We'll need TargetData from here on out.
581
const TargetData *TD = TLI ? TLI->getTargetData() : 0;
582
if (!TD) return false;
584
// Lower all default uses of _chk calls. This is very similar
585
// to what InstCombineCalls does, but here we are only lowering calls
586
// that have the default "don't know" as the objectsize. Anything else
587
// should be left alone.
588
CodeGenPrepareFortifiedLibCalls Simplifier;
589
return Simplifier.fold(CI, TD);
591
//===----------------------------------------------------------------------===//
592
// Memory Optimization
593
//===----------------------------------------------------------------------===//
595
/// IsNonLocalValue - Return true if the specified values are defined in a
596
/// different basic block than BB.
597
static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
598
if (Instruction *I = dyn_cast<Instruction>(V))
599
return I->getParent() != BB;
603
/// OptimizeMemoryInst - Load and Store Instructions often have
604
/// addressing modes that can do significant amounts of computation. As such,
605
/// instruction selection will try to get the load or store to do as much
606
/// computation as possible for the program. The problem is that isel can only
607
/// see within a single block. As such, we sink as much legal addressing mode
608
/// stuff into the block as possible.
610
/// This method is used to optimize both load/store and inline asms with memory
612
bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
613
const Type *AccessTy,
614
DenseMap<Value*,Value*> &SunkAddrs) {
615
// Figure out what addressing mode will be built up for this operation.
616
SmallVector<Instruction*, 16> AddrModeInsts;
617
ExtAddrMode AddrMode = AddressingModeMatcher::Match(Addr, AccessTy,MemoryInst,
618
AddrModeInsts, *TLI);
620
// Check to see if any of the instructions supersumed by this addr mode are
621
// non-local to I's BB.
622
bool AnyNonLocal = false;
623
for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) {
624
if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) {
630
// If all the instructions matched are already in this BB, don't do anything.
632
DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n");
636
// Insert this computation right after this user. Since our caller is
637
// scanning from the top of the BB to the bottom, reuse of the expr are
638
// guaranteed to happen later.
639
BasicBlock::iterator InsertPt = MemoryInst;
641
// Now that we determined the addressing expression we want to use and know
642
// that we have to sink it into this block. Check to see if we have already
643
// done this for some other load/store instr in this block. If so, reuse the
645
Value *&SunkAddr = SunkAddrs[Addr];
647
DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for "
649
if (SunkAddr->getType() != Addr->getType())
650
SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt);
652
DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
654
const Type *IntPtrTy =
655
TLI->getTargetData()->getIntPtrType(AccessTy->getContext());
659
// Start with the base register. Do this first so that subsequent address
660
// matching finds it last, which will prevent it from trying to match it
661
// as the scaled value in case it happens to be a mul. That would be
662
// problematic if we've sunk a different mul for the scale, because then
663
// we'd end up sinking both muls.
664
if (AddrMode.BaseReg) {
665
Value *V = AddrMode.BaseReg;
666
if (V->getType()->isPointerTy())
667
V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
668
if (V->getType() != IntPtrTy)
669
V = CastInst::CreateIntegerCast(V, IntPtrTy, /*isSigned=*/true,
670
"sunkaddr", InsertPt);
674
// Add the scale value.
675
if (AddrMode.Scale) {
676
Value *V = AddrMode.ScaledReg;
677
if (V->getType() == IntPtrTy) {
679
} else if (V->getType()->isPointerTy()) {
680
V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
681
} else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
682
cast<IntegerType>(V->getType())->getBitWidth()) {
683
V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt);
685
V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt);
687
if (AddrMode.Scale != 1)
688
V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy,
690
"sunkaddr", InsertPt);
692
Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
697
// Add in the BaseGV if present.
698
if (AddrMode.BaseGV) {
699
Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr",
702
Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
707
// Add in the Base Offset if present.
708
if (AddrMode.BaseOffs) {
709
Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
711
Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
717
SunkAddr = Constant::getNullValue(Addr->getType());
719
SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt);
722
MemoryInst->replaceUsesOfWith(Addr, SunkAddr);
724
if (Addr->use_empty()) {
725
RecursivelyDeleteTriviallyDeadInstructions(Addr);
726
// This address is now available for reassignment, so erase the table entry;
727
// we don't want to match some completely different instruction.
733
/// OptimizeInlineAsmInst - If there are any memory operands, use
734
/// OptimizeMemoryInst to sink their address computing into the block when
735
/// possible / profitable.
736
bool CodeGenPrepare::OptimizeInlineAsmInst(Instruction *I, CallSite CS,
737
DenseMap<Value*,Value*> &SunkAddrs) {
738
bool MadeChange = false;
739
InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
741
// Do a prepass over the constraints, canonicalizing them, and building up the
742
// ConstraintOperands list.
743
std::vector<InlineAsm::ConstraintInfo>
744
ConstraintInfos = IA->ParseConstraints();
746
/// ConstraintOperands - Information about all of the constraints.
747
std::vector<TargetLowering::AsmOperandInfo> ConstraintOperands;
748
unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
749
for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
751
push_back(TargetLowering::AsmOperandInfo(ConstraintInfos[i]));
752
TargetLowering::AsmOperandInfo &OpInfo = ConstraintOperands.back();
754
// Compute the value type for each operand.
755
switch (OpInfo.Type) {
756
case InlineAsm::isOutput:
757
if (OpInfo.isIndirect)
758
OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
760
case InlineAsm::isInput:
761
OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
763
case InlineAsm::isClobber:
768
// Compute the constraint code and ConstraintType to use.
769
TLI->ComputeConstraintToUse(OpInfo, SDValue());
771
if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
773
Value *OpVal = OpInfo.CallOperandVal;
774
MadeChange |= OptimizeMemoryInst(I, OpVal, OpVal->getType(), SunkAddrs);
781
/// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same
782
/// basic block as the load, unless conditions are unfavorable. This allows
783
/// SelectionDAG to fold the extend into the load.
785
bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) {
786
// Look for a load being extended.
787
LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0));
788
if (!LI) return false;
790
// If they're already in the same block, there's nothing to do.
791
if (LI->getParent() == I->getParent())
794
// If the load has other users and the truncate is not free, this probably
796
if (!LI->hasOneUse() &&
797
TLI && !TLI->isTruncateFree(I->getType(), LI->getType()))
800
// Check whether the target supports casts folded into loads.
802
if (isa<ZExtInst>(I))
803
LType = ISD::ZEXTLOAD;
805
assert(isa<SExtInst>(I) && "Unexpected ext type!");
806
LType = ISD::SEXTLOAD;
808
if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType())))
811
// Move the extend into the same block as the load, so that SelectionDAG
813
I->removeFromParent();
818
bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
819
BasicBlock *DefBB = I->getParent();
821
// If both result of the {s|z}xt and its source are live out, rewrite all
822
// other uses of the source with result of extension.
823
Value *Src = I->getOperand(0);
824
if (Src->hasOneUse())
827
// Only do this xform if truncating is free.
828
if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
831
// Only safe to perform the optimization if the source is also defined in
833
if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
836
bool DefIsLiveOut = false;
837
for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
839
Instruction *User = cast<Instruction>(*UI);
841
// Figure out which BB this ext is used in.
842
BasicBlock *UserBB = User->getParent();
843
if (UserBB == DefBB) continue;
850
// Make sure non of the uses are PHI nodes.
851
for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
853
Instruction *User = cast<Instruction>(*UI);
854
BasicBlock *UserBB = User->getParent();
855
if (UserBB == DefBB) continue;
856
// Be conservative. We don't want this xform to end up introducing
857
// reloads just before load / store instructions.
858
if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User))
862
// InsertedTruncs - Only insert one trunc in each block once.
863
DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
865
bool MadeChange = false;
866
for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
868
Use &TheUse = UI.getUse();
869
Instruction *User = cast<Instruction>(*UI);
871
// Figure out which BB this ext is used in.
872
BasicBlock *UserBB = User->getParent();
873
if (UserBB == DefBB) continue;
875
// Both src and def are live in this block. Rewrite the use.
876
Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
878
if (!InsertedTrunc) {
879
BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
881
InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
884
// Replace a use of the {s|z}ext source with a use of the result.
885
TheUse = InsertedTrunc;
893
// In this pass we look for GEP and cast instructions that are used
894
// across basic blocks and rewrite them to improve basic-block-at-a-time
896
bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) {
897
bool MadeChange = false;
899
// Split all critical edges where the dest block has a PHI.
900
if (CriticalEdgeSplit) {
901
TerminatorInst *BBTI = BB.getTerminator();
902
if (BBTI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(BBTI)) {
903
for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) {
904
BasicBlock *SuccBB = BBTI->getSuccessor(i);
905
if (isa<PHINode>(SuccBB->begin()) && isCriticalEdge(BBTI, i, true))
906
SplitEdgeNicely(BBTI, i, BackEdges, this);
911
// Keep track of non-local addresses that have been sunk into this block.
912
// This allows us to avoid inserting duplicate code for blocks with multiple
913
// load/stores of the same address.
914
DenseMap<Value*, Value*> SunkAddrs;
916
for (BasicBlock::iterator BBI = BB.begin(), E = BB.end(); BBI != E; ) {
917
Instruction *I = BBI++;
919
if (CastInst *CI = dyn_cast<CastInst>(I)) {
920
// If the source of the cast is a constant, then this should have
921
// already been constant folded. The only reason NOT to constant fold
922
// it is if something (e.g. LSR) was careful to place the constant
923
// evaluation in a block other than then one that uses it (e.g. to hoist
924
// the address of globals out of a loop). If this is the case, we don't
925
// want to forward-subst the cast.
926
if (isa<Constant>(CI->getOperand(0)))
931
Change = OptimizeNoopCopyExpression(CI, *TLI);
932
MadeChange |= Change;
935
if (!Change && (isa<ZExtInst>(I) || isa<SExtInst>(I))) {
936
MadeChange |= MoveExtToFormExtLoad(I);
937
MadeChange |= OptimizeExtUses(I);
939
} else if (CmpInst *CI = dyn_cast<CmpInst>(I)) {
940
MadeChange |= OptimizeCmpExpression(CI);
941
} else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
943
MadeChange |= OptimizeMemoryInst(I, I->getOperand(0), LI->getType(),
945
} else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
947
MadeChange |= OptimizeMemoryInst(I, SI->getOperand(1),
948
SI->getOperand(0)->getType(),
950
} else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
951
if (GEPI->hasAllZeroIndices()) {
952
/// The GEP operand must be a pointer, so must its result -> BitCast
953
Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
954
GEPI->getName(), GEPI);
955
GEPI->replaceAllUsesWith(NC);
956
GEPI->eraseFromParent();
960
} else if (CallInst *CI = dyn_cast<CallInst>(I)) {
961
// If we found an inline asm expession, and if the target knows how to
962
// lower it to normal LLVM code, do so now.
963
if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
964
if (TLI->ExpandInlineAsm(CI)) {
966
// Avoid processing instructions out of order, which could cause
967
// reuse before a value is defined.
970
// Sink address computing for memory operands into the block.
971
MadeChange |= OptimizeInlineAsmInst(I, &(*CI), SunkAddrs);
973
// Other CallInst optimizations that don't need to muck with the
974
// enclosing iterator here.
975
MadeChange |= OptimizeCallInst(CI);