Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1 | //===- CodeGenPrepare.cpp - Prepare a function for code generation --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass munges the code in the input function to better prepare it for |
Gordon Henriksen | a8a118b | 2008-05-08 17:46:35 +0000 | [diff] [blame] | 11 | // SelectionDAG-based code generation. This works around limitations in it's |
| 12 | // basic-block-at-a-time approach. It should eventually be removed. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 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" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 21 | #include "llvm/IRBuilder.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 22 | #include "llvm/InlineAsm.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" |
Dale Johannesen | 6aae1d6 | 2009-03-26 01:15:07 +0000 | [diff] [blame] | 24 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Jakob Stoklund Olesen | 7eb589d | 2010-09-30 20:51:52 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/Dominators.h" |
| 30 | #include "llvm/Analysis/InstructionSimplify.h" |
| 31 | #include "llvm/Analysis/ProfileInfo.h" |
Dan Gohman | 03ce042 | 2009-02-13 17:45:12 +0000 | [diff] [blame] | 32 | #include "llvm/Assembly/Writer.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CallSite.h" |
Evan Cheng | e1bcb44 | 2010-08-17 01:34:49 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 36 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 37 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ValueHandle.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
| 40 | #include "llvm/Target/TargetData.h" |
| 41 | #include "llvm/Target/TargetLibraryInfo.h" |
| 42 | #include "llvm/Target/TargetLowering.h" |
| 43 | #include "llvm/Transforms/Utils/AddrModeMatcher.h" |
| 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 45 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Preston Gurd | 2e2efd9 | 2012-09-04 18:22:17 +0000 | [diff] [blame^] | 46 | #include "llvm/Transforms/Utils/BypassSlowDivision.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 48 | using namespace llvm; |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 49 | using namespace llvm::PatternMatch; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 50 | |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 51 | STATISTIC(NumBlocksElim, "Number of blocks eliminated"); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 52 | STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated"); |
| 53 | STATISTIC(NumGEPsElim, "Number of GEPs converted to casts"); |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 54 | STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of " |
| 55 | "sunken Cmps"); |
| 56 | STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses " |
| 57 | "of sunken Casts"); |
| 58 | STATISTIC(NumMemoryInsts, "Number of memory instructions whose address " |
| 59 | "computations were sunk"); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 60 | STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads"); |
| 61 | STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized"); |
| 62 | STATISTIC(NumRetsDup, "Number of return instructions duplicated"); |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 63 | STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved"); |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 64 | STATISTIC(NumSelectsExpanded, "Number of selects turned into branches"); |
Jakob Stoklund Olesen | 7eb589d | 2010-09-30 20:51:52 +0000 | [diff] [blame] | 65 | |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 66 | static cl::opt<bool> DisableBranchOpts( |
| 67 | "disable-cgp-branch-opts", cl::Hidden, cl::init(false), |
| 68 | cl::desc("Disable branch optimizations in CodeGenPrepare")); |
| 69 | |
Benjamin Kramer | 77c4ef8 | 2012-05-06 14:25:16 +0000 | [diff] [blame] | 70 | static cl::opt<bool> DisableSelectToBranch( |
| 71 | "disable-cgp-select2branch", cl::Hidden, cl::init(false), |
| 72 | cl::desc("Disable select to branch conversion.")); |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 73 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 74 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 75 | class CodeGenPrepare : public FunctionPass { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 76 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 77 | /// transformation profitability. |
| 78 | const TargetLowering *TLI; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 79 | const TargetLibraryInfo *TLInfo; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 80 | DominatorTree *DT; |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 81 | ProfileInfo *PFI; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 83 | /// CurInstIterator - As we scan instructions optimizing them, this is the |
| 84 | /// next instruction to optimize. Xforms that can invalidate this should |
| 85 | /// update it. |
| 86 | BasicBlock::iterator CurInstIterator; |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 87 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 88 | /// Keeps track of non-local addresses that have been sunk into a block. |
| 89 | /// This allows us to avoid inserting duplicate code for blocks with |
| 90 | /// multiple load/stores of the same address. |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 91 | DenseMap<Value*, Value*> SunkAddrs; |
| 92 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 93 | /// ModifiedDT - If CFG is modified in anyway, dominator tree may need to |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 94 | /// be updated. |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 95 | bool ModifiedDT; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 96 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 97 | /// OptSize - True if optimizing for size. |
| 98 | bool OptSize; |
| 99 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 100 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 101 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 102 | explicit CodeGenPrepare(const TargetLowering *tli = 0) |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 103 | : FunctionPass(ID), TLI(tli) { |
| 104 | initializeCodeGenPreparePass(*PassRegistry::getPassRegistry()); |
| 105 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 106 | bool runOnFunction(Function &F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 107 | |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 108 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 109 | AU.addPreserved<DominatorTree>(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 110 | AU.addPreserved<ProfileInfo>(); |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 111 | AU.addRequired<TargetLibraryInfo>(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 114 | private: |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 115 | bool EliminateFallThrough(Function &F); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 116 | bool EliminateMostlyEmptyBlocks(Function &F); |
| 117 | bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 118 | void EliminateMostlyEmptyBlock(BasicBlock *BB); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 119 | bool OptimizeBlock(BasicBlock &BB); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 120 | bool OptimizeInst(Instruction *I); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 121 | bool OptimizeMemoryInst(Instruction *I, Value *Addr, Type *AccessTy); |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 122 | bool OptimizeInlineAsmInst(CallInst *CS); |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 123 | bool OptimizeCallInst(CallInst *CI); |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 124 | bool MoveExtToFormExtLoad(Instruction *I); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 125 | bool OptimizeExtUses(Instruction *I); |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 126 | bool OptimizeSelectInst(SelectInst *SI); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 127 | bool DupRetToEnableTailCallOpts(ReturnInst *RI); |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 128 | bool PlaceDbgValues(Function &F); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 129 | }; |
| 130 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 131 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 132 | char CodeGenPrepare::ID = 0; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 133 | INITIALIZE_PASS_BEGIN(CodeGenPrepare, "codegenprepare", |
| 134 | "Optimize for code generation", false, false) |
| 135 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
| 136 | INITIALIZE_PASS_END(CodeGenPrepare, "codegenprepare", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 137 | "Optimize for code generation", false, false) |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 138 | |
| 139 | FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) { |
| 140 | return new CodeGenPrepare(TLI); |
| 141 | } |
| 142 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 143 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 144 | bool EverMadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 145 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 146 | ModifiedDT = false; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 147 | TLInfo = &getAnalysis<TargetLibraryInfo>(); |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 148 | DT = getAnalysisIfAvailable<DominatorTree>(); |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 149 | PFI = getAnalysisIfAvailable<ProfileInfo>(); |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 150 | OptSize = F.hasFnAttr(Attribute::OptimizeForSize); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 151 | |
Preston Gurd | 2e2efd9 | 2012-09-04 18:22:17 +0000 | [diff] [blame^] | 152 | /// This optimization identifies DIV instructions that can be |
| 153 | /// profitably bypassed and carried out with a shorter, faster divide. |
| 154 | if (TLI && TLI->isSlowDivBypassed()) { |
| 155 | const DenseMap<Type *, Type *> &BypassTypeMap = TLI->getBypassSlowDivTypes(); |
| 156 | |
| 157 | for (Function::iterator I = F.begin(); I != F.end(); I++) { |
| 158 | EverMadeChange |= bypassSlowDivision(F, |
| 159 | I, |
| 160 | BypassTypeMap); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Eliminate blocks that contain only PHI nodes and an |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 165 | // unconditional branch. |
| 166 | EverMadeChange |= EliminateMostlyEmptyBlocks(F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 167 | |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 168 | // llvm.dbg.value is far away from the value then iSel may not be able |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 169 | // handle it properly. iSel will drop llvm.dbg.value if it can not |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 170 | // find a node corresponding to the value. |
| 171 | EverMadeChange |= PlaceDbgValues(F); |
| 172 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 173 | bool MadeChange = true; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 174 | while (MadeChange) { |
| 175 | MadeChange = false; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 176 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) { |
| 177 | BasicBlock *BB = I++; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 178 | MadeChange |= OptimizeBlock(*BB); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 179 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 180 | EverMadeChange |= MadeChange; |
| 181 | } |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 182 | |
| 183 | SunkAddrs.clear(); |
| 184 | |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 185 | if (!DisableBranchOpts) { |
| 186 | MadeChange = false; |
Bill Wendling | e3e394d | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 187 | SmallPtrSet<BasicBlock*, 8> WorkList; |
| 188 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 189 | SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB)); |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 190 | MadeChange |= ConstantFoldTerminator(BB, true); |
Bill Wendling | e3e394d | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 191 | if (!MadeChange) continue; |
| 192 | |
| 193 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 194 | II = Successors.begin(), IE = Successors.end(); II != IE; ++II) |
| 195 | if (pred_begin(*II) == pred_end(*II)) |
| 196 | WorkList.insert(*II); |
| 197 | } |
| 198 | |
Bill Wendling | 8dd2e5b | 2012-08-15 21:18:10 +0000 | [diff] [blame] | 199 | for (SmallPtrSet<BasicBlock*, 8>::iterator |
| 200 | I = WorkList.begin(), E = WorkList.end(); I != E; ++I) |
| 201 | DeleteDeadBlock(*I); |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 202 | |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 203 | // Merge pairs of basic blocks with unconditional branches, connected by |
| 204 | // a single edge. |
| 205 | if (EverMadeChange || MadeChange) |
| 206 | MadeChange |= EliminateFallThrough(F); |
| 207 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 208 | if (MadeChange) |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 209 | ModifiedDT = true; |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 210 | EverMadeChange |= MadeChange; |
| 211 | } |
| 212 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 213 | if (ModifiedDT && DT) |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 214 | DT->DT->recalculate(F); |
| 215 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 216 | return EverMadeChange; |
| 217 | } |
| 218 | |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 219 | /// EliminateFallThrough - Merge basic blocks which are connected |
| 220 | /// by a single edge, where one of the basic blocks has a single successor |
| 221 | /// pointing to the other basic block, which has a single predecessor. |
| 222 | bool CodeGenPrepare::EliminateFallThrough(Function &F) { |
| 223 | bool Changed = false; |
| 224 | // Scan all of the blocks in the function, except for the entry block. |
| 225 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 226 | BasicBlock *BB = I++; |
| 227 | // If the destination block has a single pred, then this is a trivial |
| 228 | // edge, just collapse it. |
| 229 | BasicBlock *SinglePred = BB->getSinglePredecessor(); |
| 230 | |
| 231 | if (!SinglePred || SinglePred == BB) continue; |
| 232 | |
| 233 | BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); |
| 234 | if (Term && !Term->isConditional()) { |
| 235 | Changed = true; |
Michael Liao | 787ed03 | 2012-08-21 05:55:22 +0000 | [diff] [blame] | 236 | DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n"); |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 237 | // Remember if SinglePred was the entry block of the function. |
| 238 | // If so, we will need to move BB back to the entry position. |
| 239 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
| 240 | MergeBasicBlockIntoOnlyPred(BB, this); |
| 241 | |
| 242 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 243 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
| 244 | |
| 245 | // We have erased a block. Update the iterator. |
| 246 | I = BB; |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | return Changed; |
| 250 | } |
| 251 | |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 252 | /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes, |
| 253 | /// debug info directives, and an unconditional branch. Passes before isel |
| 254 | /// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for |
| 255 | /// isel. Start by eliminating these blocks so we can split them the way we |
| 256 | /// want them. |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 257 | bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) { |
| 258 | bool MadeChange = false; |
| 259 | // Note that this intentionally skips the entry block. |
| 260 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 261 | BasicBlock *BB = I++; |
| 262 | |
| 263 | // If this block doesn't end with an uncond branch, ignore it. |
| 264 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 265 | if (!BI || !BI->isUnconditional()) |
| 266 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 267 | |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 268 | // If the instruction before the branch (skipping debug info) isn't a phi |
| 269 | // node, then other stuff is happening here. |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 270 | BasicBlock::iterator BBI = BI; |
| 271 | if (BBI != BB->begin()) { |
| 272 | --BBI; |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 273 | while (isa<DbgInfoIntrinsic>(BBI)) { |
| 274 | if (BBI == BB->begin()) |
| 275 | break; |
| 276 | --BBI; |
| 277 | } |
| 278 | if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI)) |
| 279 | continue; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 280 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 281 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 282 | // Do not break infinite loops. |
| 283 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 284 | if (DestBB == BB) |
| 285 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 286 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 287 | if (!CanMergeBlocks(BB, DestBB)) |
| 288 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 289 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 290 | EliminateMostlyEmptyBlock(BB); |
| 291 | MadeChange = true; |
| 292 | } |
| 293 | return MadeChange; |
| 294 | } |
| 295 | |
| 296 | /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a |
| 297 | /// single uncond branch between them, and BB contains no other non-phi |
| 298 | /// instructions. |
| 299 | bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB, |
| 300 | const BasicBlock *DestBB) const { |
| 301 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 302 | // the successor. If there are more complex condition (e.g. preheaders), |
| 303 | // don't mess around with them. |
| 304 | BasicBlock::const_iterator BBI = BB->begin(); |
| 305 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 306 | for (Value::const_use_iterator UI = PN->use_begin(), E = PN->use_end(); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 307 | UI != E; ++UI) { |
| 308 | const Instruction *User = cast<Instruction>(*UI); |
| 309 | if (User->getParent() != DestBB || !isa<PHINode>(User)) |
| 310 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 311 | // If User is inside DestBB block and it is a PHINode then check |
| 312 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | 75abc1e | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 313 | // a complex condition (e.g. preheaders) we want to avoid here. |
| 314 | if (User->getParent() == DestBB) { |
| 315 | if (const PHINode *UPN = dyn_cast<PHINode>(User)) |
| 316 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 317 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 318 | if (Insn && Insn->getParent() == BB && |
| 319 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 320 | return false; |
| 321 | } |
| 322 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 325 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 326 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 327 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 328 | // can't merge the block. |
| 329 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 330 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 331 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 332 | // Collect the preds of BB. |
Chris Lattner | f67f73a | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 333 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 334 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 335 | // It is faster to get preds from a PHI than with pred_iterator. |
| 336 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 337 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 338 | } else { |
| 339 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 340 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 341 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 342 | // Walk the preds of DestBB. |
| 343 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 344 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 345 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 346 | BBI = DestBB->begin(); |
| 347 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 348 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 349 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 350 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 351 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 352 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 353 | if (V2PN->getParent() == BB) |
| 354 | V2 = V2PN->getIncomingValueForBlock(Pred); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 355 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 356 | // If there is a conflict, bail out. |
| 357 | if (V1 != V2) return false; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and |
| 367 | /// an unconditional branch in it. |
| 368 | void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) { |
| 369 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 370 | BasicBlock *DestBB = BI->getSuccessor(0); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 371 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 372 | DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 373 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 374 | // If the destination block has a single pred, then this is a trivial edge, |
| 375 | // just collapse it. |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 376 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 377 | if (SinglePred != DestBB) { |
| 378 | // Remember if SinglePred was the entry block of the function. If so, we |
| 379 | // will need to move BB back to the entry position. |
| 380 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 381 | MergeBasicBlockIntoOnlyPred(DestBB, this); |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 382 | |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 383 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 384 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 385 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 386 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 387 | return; |
| 388 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 389 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 390 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 391 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 392 | // to handle the new incoming edges it is about to have. |
| 393 | PHINode *PN; |
| 394 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 395 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 396 | // Remove the incoming value for BB, and remember it. |
| 397 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 398 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 399 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 400 | // value that dominates BB. |
| 401 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 402 | if (InValPhi && InValPhi->getParent() == BB) { |
| 403 | // Add all of the input values of the input PHI as inputs of this phi. |
| 404 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 405 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 406 | InValPhi->getIncomingBlock(i)); |
| 407 | } else { |
| 408 | // Otherwise, add one instance of the dominating value for each edge that |
| 409 | // we will be adding. |
| 410 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 411 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 412 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 413 | } else { |
| 414 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 415 | PN->addIncoming(InVal, *PI); |
| 416 | } |
| 417 | } |
| 418 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 419 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 420 | // The PHIs are now updated, change everything that refers to BB to use |
| 421 | // DestBB and remove BB. |
| 422 | BB->replaceAllUsesWith(DestBB); |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 423 | if (DT && !ModifiedDT) { |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 424 | BasicBlock *BBIDom = DT->getNode(BB)->getIDom()->getBlock(); |
| 425 | BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock(); |
| 426 | BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom); |
| 427 | DT->changeImmediateDominator(DestBB, NewIDom); |
| 428 | DT->eraseNode(BB); |
| 429 | } |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 430 | if (PFI) { |
| 431 | PFI->replaceAllUses(BB, DestBB); |
| 432 | PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB)); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 433 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 434 | BB->eraseFromParent(); |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 435 | ++NumBlocksElim; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 436 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 437 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 440 | /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 441 | /// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC), |
| 442 | /// sink it into user blocks to reduce the number of virtual |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 443 | /// registers that must be created and coalesced. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 444 | /// |
| 445 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 446 | /// |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 447 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){ |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 448 | // If this is a noop copy, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 449 | EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 450 | EVT DstVT = TLI.getValueType(CI->getType()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 451 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 452 | // This is an fp<->int conversion? |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 453 | if (SrcVT.isInteger() != DstVT.isInteger()) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 454 | return false; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 455 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 456 | // If this is an extension, it will be a zero or sign extension, which |
| 457 | // isn't a noop. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 458 | if (SrcVT.bitsLT(DstVT)) return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 459 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 460 | // If these values will be promoted, find out what they will be promoted |
| 461 | // to. This helps us consider truncates on PPC as noop copies when they |
| 462 | // are. |
Nadav Rotem | 0ccc12a | 2011-05-29 08:10:47 +0000 | [diff] [blame] | 463 | if (TLI.getTypeAction(CI->getContext(), SrcVT) == |
| 464 | TargetLowering::TypePromoteInteger) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 465 | SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT); |
Nadav Rotem | 0ccc12a | 2011-05-29 08:10:47 +0000 | [diff] [blame] | 466 | if (TLI.getTypeAction(CI->getContext(), DstVT) == |
| 467 | TargetLowering::TypePromoteInteger) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 468 | DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 469 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 470 | // If, after promotion, these are the same types, this is a noop copy. |
| 471 | if (SrcVT != DstVT) |
| 472 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 473 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 474 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 475 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 476 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 477 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 478 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 479 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 480 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 481 | UI != E; ) { |
| 482 | Use &TheUse = UI.getUse(); |
| 483 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 484 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 485 | // Figure out which BB this cast is used in. For PHI's this is the |
| 486 | // appropriate predecessor block. |
| 487 | BasicBlock *UserBB = User->getParent(); |
| 488 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
Gabor Greif | a36791d | 2009-01-23 19:40:15 +0000 | [diff] [blame] | 489 | UserBB = PN->getIncomingBlock(UI); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 490 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 491 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 492 | // Preincrement use iterator so we don't invalidate it. |
| 493 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 494 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 495 | // If this user is in the same block as the cast, don't change the cast. |
| 496 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 497 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 498 | // If we have already inserted a cast into this block, use it. |
| 499 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 500 | |
| 501 | if (!InsertedCast) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 502 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 503 | InsertedCast = |
| 504 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 505 | InsertPt); |
| 506 | MadeChange = true; |
| 507 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 508 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 509 | // Replace a use of the cast with a use of the new cast. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 510 | TheUse = InsertedCast; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 511 | ++NumCastUses; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 512 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 513 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 514 | // If we removed all uses, nuke the cast. |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 515 | if (CI->use_empty()) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 516 | CI->eraseFromParent(); |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 517 | MadeChange = true; |
| 518 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 519 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 520 | return MadeChange; |
| 521 | } |
| 522 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 523 | /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 524 | /// the number of virtual registers that must be created and coalesced. This is |
Chris Lattner | 684b22d | 2007-08-02 16:53:43 +0000 | [diff] [blame] | 525 | /// a clear win except on targets with multiple condition code registers |
| 526 | /// (PowerPC), where it might lose; some adjustment may be wanted there. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 527 | /// |
| 528 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 529 | static bool OptimizeCmpExpression(CmpInst *CI) { |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 530 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 531 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 532 | /// InsertedCmp - Only insert a cmp in each block once. |
| 533 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 534 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 535 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 536 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 537 | UI != E; ) { |
| 538 | Use &TheUse = UI.getUse(); |
| 539 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 540 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 541 | // Preincrement use iterator so we don't invalidate it. |
| 542 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 543 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 544 | // Don't bother for PHI nodes. |
| 545 | if (isa<PHINode>(User)) |
| 546 | continue; |
| 547 | |
| 548 | // Figure out which BB this cmp is used in. |
| 549 | BasicBlock *UserBB = User->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 550 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 551 | // If this user is in the same block as the cmp, don't change the cmp. |
| 552 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 553 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 554 | // If we have already inserted a cmp into this block, use it. |
| 555 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 556 | |
| 557 | if (!InsertedCmp) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 558 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 559 | InsertedCmp = |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 560 | CmpInst::Create(CI->getOpcode(), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 561 | CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 562 | CI->getOperand(1), "", InsertPt); |
| 563 | MadeChange = true; |
| 564 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 565 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 566 | // Replace a use of the cmp with a use of the new cmp. |
| 567 | TheUse = InsertedCmp; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 568 | ++NumCmpUses; |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 569 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 570 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 571 | // If we removed all uses, nuke the cmp. |
| 572 | if (CI->use_empty()) |
| 573 | CI->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 574 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 575 | return MadeChange; |
| 576 | } |
| 577 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 578 | namespace { |
| 579 | class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls { |
| 580 | protected: |
| 581 | void replaceCall(Value *With) { |
| 582 | CI->replaceAllUsesWith(With); |
| 583 | CI->eraseFromParent(); |
| 584 | } |
| 585 | bool isFoldable(unsigned SizeCIOp, unsigned, bool) const { |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 586 | if (ConstantInt *SizeCI = |
| 587 | dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) |
| 588 | return SizeCI->isAllOnesValue(); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 589 | return false; |
| 590 | } |
| 591 | }; |
| 592 | } // end anonymous namespace |
| 593 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 594 | bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) { |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 595 | BasicBlock *BB = CI->getParent(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 597 | // Lower inline assembly if we can. |
| 598 | // If we found an inline asm expession, and if the target knows how to |
| 599 | // lower it to normal LLVM code, do so now. |
| 600 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) { |
| 601 | if (TLI->ExpandInlineAsm(CI)) { |
| 602 | // Avoid invalidating the iterator. |
| 603 | CurInstIterator = BB->begin(); |
| 604 | // Avoid processing instructions out of order, which could cause |
| 605 | // reuse before a value is defined. |
| 606 | SunkAddrs.clear(); |
| 607 | return true; |
| 608 | } |
| 609 | // Sink address computing for memory operands into the block. |
| 610 | if (OptimizeInlineAsmInst(CI)) |
| 611 | return true; |
| 612 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 613 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 614 | // Lower all uses of llvm.objectsize.* |
| 615 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); |
| 616 | if (II && II->getIntrinsicID() == Intrinsic::objectsize) { |
Gabor Greif | de9f545 | 2010-06-24 00:44:01 +0000 | [diff] [blame] | 617 | bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 618 | Type *ReturnTy = CI->getType(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 619 | Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); |
| 620 | |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 621 | // Substituting this can cause recursive simplifications, which can |
| 622 | // invalidate our iterator. Use a WeakVH to hold onto it in case this |
| 623 | // happens. |
| 624 | WeakVH IterHandle(CurInstIterator); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 625 | |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 626 | replaceAndRecursivelySimplify(CI, RetVal, TLI ? TLI->getTargetData() : 0, |
| 627 | TLInfo, ModifiedDT ? 0 : DT); |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 628 | |
| 629 | // If the iterator instruction was recursively deleted, start over at the |
| 630 | // start of the block. |
Chris Lattner | 435b4d2 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 631 | if (IterHandle != CurInstIterator) { |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 632 | CurInstIterator = BB->begin(); |
Chris Lattner | 435b4d2 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 633 | SunkAddrs.clear(); |
| 634 | } |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 635 | return true; |
| 636 | } |
| 637 | |
Pete Cooper | f210b68 | 2012-03-13 20:59:56 +0000 | [diff] [blame] | 638 | if (II && TLI) { |
| 639 | SmallVector<Value*, 2> PtrOps; |
| 640 | Type *AccessTy; |
| 641 | if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy)) |
| 642 | while (!PtrOps.empty()) |
| 643 | if (OptimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy)) |
| 644 | return true; |
| 645 | } |
| 646 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 647 | // From here on out we're working with named functions. |
| 648 | if (CI->getCalledFunction() == 0) return false; |
Devang Patel | 97de92c | 2011-05-26 21:51:06 +0000 | [diff] [blame] | 649 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 650 | // We'll need TargetData from here on out. |
| 651 | const TargetData *TD = TLI ? TLI->getTargetData() : 0; |
| 652 | if (!TD) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 653 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 654 | // Lower all default uses of _chk calls. This is very similar |
| 655 | // to what InstCombineCalls does, but here we are only lowering calls |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 656 | // that have the default "don't know" as the objectsize. Anything else |
| 657 | // should be left alone. |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 658 | CodeGenPrepareFortifiedLibCalls Simplifier; |
Nuno Lopes | 51004df | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 659 | return Simplifier.fold(CI, TD, TLInfo); |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 660 | } |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 661 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 662 | /// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return |
| 663 | /// instructions to the predecessor to enable tail call optimizations. The |
| 664 | /// case it is currently looking for is: |
| 665 | /// bb0: |
| 666 | /// %tmp0 = tail call i32 @f0() |
| 667 | /// br label %return |
| 668 | /// bb1: |
| 669 | /// %tmp1 = tail call i32 @f1() |
| 670 | /// br label %return |
| 671 | /// bb2: |
| 672 | /// %tmp2 = tail call i32 @f2() |
| 673 | /// br label %return |
| 674 | /// return: |
| 675 | /// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ] |
| 676 | /// ret i32 %retval |
| 677 | /// |
| 678 | /// => |
| 679 | /// |
| 680 | /// bb0: |
| 681 | /// %tmp0 = tail call i32 @f0() |
| 682 | /// ret i32 %tmp0 |
| 683 | /// bb1: |
| 684 | /// %tmp1 = tail call i32 @f1() |
| 685 | /// ret i32 %tmp1 |
| 686 | /// bb2: |
| 687 | /// %tmp2 = tail call i32 @f2() |
| 688 | /// ret i32 %tmp2 |
| 689 | /// |
| 690 | bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) { |
Cameron Zwarich | 661a390 | 2011-03-24 04:51:51 +0000 | [diff] [blame] | 691 | if (!TLI) |
| 692 | return false; |
| 693 | |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 694 | PHINode *PN = 0; |
| 695 | BitCastInst *BCI = 0; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 696 | Value *V = RI->getReturnValue(); |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 697 | if (V) { |
| 698 | BCI = dyn_cast<BitCastInst>(V); |
| 699 | if (BCI) |
| 700 | V = BCI->getOperand(0); |
| 701 | |
| 702 | PN = dyn_cast<PHINode>(V); |
| 703 | if (!PN) |
| 704 | return false; |
| 705 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 706 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 707 | BasicBlock *BB = RI->getParent(); |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 708 | if (PN && PN->getParent() != BB) |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 709 | return false; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 710 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 711 | // It's not safe to eliminate the sign / zero extension of the return value. |
| 712 | // See llvm::isInTailCallPosition(). |
| 713 | const Function *F = BB->getParent(); |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 714 | Attributes CallerRetAttr = F->getAttributes().getRetAttributes(); |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 715 | if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt)) |
| 716 | return false; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 717 | |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 718 | // Make sure there are no instructions between the PHI and return, or that the |
| 719 | // return is the first instruction in the block. |
| 720 | if (PN) { |
| 721 | BasicBlock::iterator BI = BB->begin(); |
| 722 | do { ++BI; } while (isa<DbgInfoIntrinsic>(BI)); |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 723 | if (&*BI == BCI) |
| 724 | // Also skip over the bitcast. |
| 725 | ++BI; |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 726 | if (&*BI != RI) |
| 727 | return false; |
| 728 | } else { |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 729 | BasicBlock::iterator BI = BB->begin(); |
| 730 | while (isa<DbgInfoIntrinsic>(BI)) ++BI; |
| 731 | if (&*BI != RI) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 732 | return false; |
| 733 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 734 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 735 | /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail |
| 736 | /// call. |
| 737 | SmallVector<CallInst*, 4> TailCalls; |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 738 | if (PN) { |
| 739 | for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) { |
| 740 | CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I)); |
| 741 | // Make sure the phi value is indeed produced by the tail call. |
| 742 | if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) && |
| 743 | TLI->mayBeEmittedAsTailCall(CI)) |
| 744 | TailCalls.push_back(CI); |
| 745 | } |
| 746 | } else { |
| 747 | SmallPtrSet<BasicBlock*, 4> VisitedBBs; |
| 748 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { |
| 749 | if (!VisitedBBs.insert(*PI)) |
| 750 | continue; |
| 751 | |
| 752 | BasicBlock::InstListType &InstList = (*PI)->getInstList(); |
| 753 | BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin(); |
| 754 | BasicBlock::InstListType::reverse_iterator RE = InstList.rend(); |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 755 | do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI)); |
| 756 | if (RI == RE) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 757 | continue; |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 758 | |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 759 | CallInst *CI = dyn_cast<CallInst>(&*RI); |
Cameron Zwarich | dc31cfe | 2011-03-24 15:54:11 +0000 | [diff] [blame] | 760 | if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI)) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 761 | TailCalls.push_back(CI); |
| 762 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 765 | bool Changed = false; |
| 766 | for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) { |
| 767 | CallInst *CI = TailCalls[i]; |
| 768 | CallSite CS(CI); |
| 769 | |
| 770 | // Conservatively require the attributes of the call to match those of the |
| 771 | // return. Ignore noalias because it doesn't affect the call sequence. |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 772 | Attributes CalleeRetAttr = CS.getAttributes().getRetAttributes(); |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 773 | if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias) |
| 774 | continue; |
| 775 | |
| 776 | // Make sure the call instruction is followed by an unconditional branch to |
| 777 | // the return block. |
| 778 | BasicBlock *CallBB = CI->getParent(); |
| 779 | BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator()); |
| 780 | if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB) |
| 781 | continue; |
| 782 | |
| 783 | // Duplicate the return into CallBB. |
| 784 | (void)FoldReturnIntoUncondBranch(RI, BB, CallBB); |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 785 | ModifiedDT = Changed = true; |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 786 | ++NumRetsDup; |
| 787 | } |
| 788 | |
| 789 | // If we eliminated all predecessors of the block, delete the block now. |
| 790 | if (Changed && pred_begin(BB) == pred_end(BB)) |
| 791 | BB->eraseFromParent(); |
| 792 | |
| 793 | return Changed; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 796 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 797 | // Memory Optimization |
| 798 | //===----------------------------------------------------------------------===// |
| 799 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 800 | /// IsNonLocalValue - Return true if the specified values are defined in a |
| 801 | /// different basic block than BB. |
| 802 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 803 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 804 | return I->getParent() != BB; |
| 805 | return false; |
| 806 | } |
| 807 | |
Bob Wilson | 4a8ee23 | 2009-12-03 21:47:07 +0000 | [diff] [blame] | 808 | /// OptimizeMemoryInst - Load and Store Instructions often have |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 809 | /// addressing modes that can do significant amounts of computation. As such, |
| 810 | /// instruction selection will try to get the load or store to do as much |
| 811 | /// computation as possible for the program. The problem is that isel can only |
| 812 | /// see within a single block. As such, we sink as much legal addressing mode |
| 813 | /// stuff into the block as possible. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 814 | /// |
| 815 | /// This method is used to optimize both load/store and inline asms with memory |
| 816 | /// operands. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 817 | bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 818 | Type *AccessTy) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 819 | Value *Repl = Addr; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 820 | |
| 821 | // Try to collapse single-value PHI nodes. This is necessary to undo |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 822 | // unprofitable PRE transformations. |
Cameron Zwarich | 7cb4fa2 | 2011-01-03 06:33:01 +0000 | [diff] [blame] | 823 | SmallVector<Value*, 8> worklist; |
| 824 | SmallPtrSet<Value*, 16> Visited; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 825 | worklist.push_back(Addr); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 826 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 827 | // Use a worklist to iteratively look through PHI nodes, and ensure that |
| 828 | // the addressing mode obtained from the non-PHI roots of the graph |
| 829 | // are equivalent. |
| 830 | Value *Consensus = 0; |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 831 | unsigned NumUsesConsensus = 0; |
Cameron Zwarich | 7c8d351 | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 832 | bool IsNumUsesConsensusValid = false; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 833 | SmallVector<Instruction*, 16> AddrModeInsts; |
| 834 | ExtAddrMode AddrMode; |
| 835 | while (!worklist.empty()) { |
| 836 | Value *V = worklist.back(); |
| 837 | worklist.pop_back(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 838 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 839 | // Break use-def graph loops. |
Nick Lewycky | 4810528 | 2011-09-29 23:40:12 +0000 | [diff] [blame] | 840 | if (!Visited.insert(V)) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 841 | Consensus = 0; |
| 842 | break; |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 843 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 844 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 845 | // For a PHI node, push all of its incoming values. |
| 846 | if (PHINode *P = dyn_cast<PHINode>(V)) { |
| 847 | for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) |
| 848 | worklist.push_back(P->getIncomingValue(i)); |
| 849 | continue; |
| 850 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 851 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 852 | // For non-PHIs, determine the addressing mode being computed. |
| 853 | SmallVector<Instruction*, 16> NewAddrModeInsts; |
| 854 | ExtAddrMode NewAddrMode = |
Nick Lewycky | 4810528 | 2011-09-29 23:40:12 +0000 | [diff] [blame] | 855 | AddressingModeMatcher::Match(V, AccessTy, MemoryInst, |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 856 | NewAddrModeInsts, *TLI); |
Cameron Zwarich | 7c8d351 | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 857 | |
| 858 | // This check is broken into two cases with very similar code to avoid using |
| 859 | // getNumUses() as much as possible. Some values have a lot of uses, so |
| 860 | // calling getNumUses() unconditionally caused a significant compile-time |
| 861 | // regression. |
| 862 | if (!Consensus) { |
| 863 | Consensus = V; |
| 864 | AddrMode = NewAddrMode; |
| 865 | AddrModeInsts = NewAddrModeInsts; |
| 866 | continue; |
| 867 | } else if (NewAddrMode == AddrMode) { |
| 868 | if (!IsNumUsesConsensusValid) { |
| 869 | NumUsesConsensus = Consensus->getNumUses(); |
| 870 | IsNumUsesConsensusValid = true; |
| 871 | } |
| 872 | |
| 873 | // Ensure that the obtained addressing mode is equivalent to that obtained |
| 874 | // for all other roots of the PHI traversal. Also, when choosing one |
| 875 | // such root as representative, select the one with the most uses in order |
| 876 | // to keep the cost modeling heuristics in AddressingModeMatcher |
| 877 | // applicable. |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 878 | unsigned NumUses = V->getNumUses(); |
| 879 | if (NumUses > NumUsesConsensus) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 880 | Consensus = V; |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 881 | NumUsesConsensus = NumUses; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 882 | AddrModeInsts = NewAddrModeInsts; |
| 883 | } |
| 884 | continue; |
| 885 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 886 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 887 | Consensus = 0; |
| 888 | break; |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 889 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 890 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 891 | // If the addressing mode couldn't be determined, or if multiple different |
| 892 | // ones were determined, bail out now. |
| 893 | if (!Consensus) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 894 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 895 | // Check to see if any of the instructions supersumed by this addr mode are |
| 896 | // non-local to I's BB. |
| 897 | bool AnyNonLocal = false; |
| 898 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 899 | if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 900 | AnyNonLocal = true; |
| 901 | break; |
| 902 | } |
| 903 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 904 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 905 | // If all the instructions matched are already in this BB, don't do anything. |
| 906 | if (!AnyNonLocal) { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 907 | DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 908 | return false; |
| 909 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 910 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 911 | // Insert this computation right after this user. Since our caller is |
| 912 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 913 | // guaranteed to happen later. |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 914 | IRBuilder<> Builder(MemoryInst); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 915 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 916 | // Now that we determined the addressing expression we want to use and know |
| 917 | // that we have to sink it into this block. Check to see if we have already |
| 918 | // done this for some other load/store instr in this block. If so, reuse the |
| 919 | // computation. |
| 920 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 921 | if (SunkAddr) { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 922 | DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 923 | << *MemoryInst); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 924 | if (SunkAddr->getType() != Addr->getType()) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 925 | SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 926 | } else { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 927 | DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 928 | << *MemoryInst); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 929 | Type *IntPtrTy = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 930 | TLI->getTargetData()->getIntPtrType(AccessTy->getContext()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 931 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 932 | Value *Result = 0; |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 933 | |
| 934 | // Start with the base register. Do this first so that subsequent address |
| 935 | // matching finds it last, which will prevent it from trying to match it |
| 936 | // as the scaled value in case it happens to be a mul. That would be |
| 937 | // problematic if we've sunk a different mul for the scale, because then |
| 938 | // we'd end up sinking both muls. |
| 939 | if (AddrMode.BaseReg) { |
| 940 | Value *V = AddrMode.BaseReg; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 941 | if (V->getType()->isPointerTy()) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 942 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 943 | if (V->getType() != IntPtrTy) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 944 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 945 | Result = V; |
| 946 | } |
| 947 | |
| 948 | // Add the scale value. |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 949 | if (AddrMode.Scale) { |
| 950 | Value *V = AddrMode.ScaledReg; |
| 951 | if (V->getType() == IntPtrTy) { |
| 952 | // done. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 953 | } else if (V->getType()->isPointerTy()) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 954 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 955 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 956 | cast<IntegerType>(V->getType())->getBitWidth()) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 957 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 958 | } else { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 959 | V = Builder.CreateSExt(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 960 | } |
| 961 | if (AddrMode.Scale != 1) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 962 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), |
| 963 | "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 964 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 965 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 966 | else |
| 967 | Result = V; |
| 968 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 969 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 970 | // Add in the BaseGV if present. |
| 971 | if (AddrMode.BaseGV) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 972 | Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 973 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 974 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 975 | else |
| 976 | Result = V; |
| 977 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 978 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 979 | // Add in the Base Offset if present. |
| 980 | if (AddrMode.BaseOffs) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 981 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 982 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 983 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 984 | else |
| 985 | Result = V; |
| 986 | } |
| 987 | |
| 988 | if (Result == 0) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 989 | SunkAddr = Constant::getNullValue(Addr->getType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 990 | else |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 991 | SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 992 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 993 | |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 994 | MemoryInst->replaceUsesOfWith(Repl, SunkAddr); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 995 | |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 996 | // If we have no uses, recursively delete the value and all dead instructions |
| 997 | // using it. |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 998 | if (Repl->use_empty()) { |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 999 | // This can cause recursive deletion, which can invalidate our iterator. |
| 1000 | // Use a WeakVH to hold onto it in case this happens. |
| 1001 | WeakVH IterHandle(CurInstIterator); |
| 1002 | BasicBlock *BB = CurInstIterator->getParent(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1003 | |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1004 | RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo); |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (IterHandle != CurInstIterator) { |
| 1007 | // If the iterator instruction was recursively deleted, start over at the |
| 1008 | // start of the block. |
| 1009 | CurInstIterator = BB->begin(); |
| 1010 | SunkAddrs.clear(); |
| 1011 | } else { |
| 1012 | // This address is now available for reassignment, so erase the table |
| 1013 | // entry; we don't want to match some completely different instruction. |
| 1014 | SunkAddrs[Addr] = 0; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1015 | } |
Dale Johannesen | 536d31b | 2010-03-31 20:37:15 +0000 | [diff] [blame] | 1016 | } |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1017 | ++NumMemoryInsts; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1018 | return true; |
| 1019 | } |
| 1020 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1021 | /// OptimizeInlineAsmInst - If there are any memory operands, use |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1022 | /// OptimizeMemoryInst to sink their address computing into the block when |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1023 | /// possible / profitable. |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1024 | bool CodeGenPrepare::OptimizeInlineAsmInst(CallInst *CS) { |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1025 | bool MadeChange = false; |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1026 | |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1027 | TargetLowering::AsmOperandInfoVector |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1028 | TargetConstraints = TLI->ParseConstraints(CS); |
Dale Johannesen | 677c6ec | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 1029 | unsigned ArgNo = 0; |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 1030 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 1031 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1032 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1033 | // Compute the constraint code and ConstraintType to use. |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 1034 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1035 | |
Eli Friedman | 9ec8095 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 1036 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 1037 | OpInfo.isIndirect) { |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1038 | Value *OpVal = CS->getArgOperand(ArgNo++); |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1039 | MadeChange |= OptimizeMemoryInst(CS, OpVal, OpVal->getType()); |
Dale Johannesen | 677c6ec | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 1040 | } else if (OpInfo.Type == InlineAsm::isInput) |
| 1041 | ArgNo++; |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | return MadeChange; |
| 1045 | } |
| 1046 | |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1047 | /// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same |
| 1048 | /// basic block as the load, unless conditions are unfavorable. This allows |
| 1049 | /// SelectionDAG to fold the extend into the load. |
| 1050 | /// |
| 1051 | bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) { |
| 1052 | // Look for a load being extended. |
| 1053 | LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0)); |
| 1054 | if (!LI) return false; |
| 1055 | |
| 1056 | // If they're already in the same block, there's nothing to do. |
| 1057 | if (LI->getParent() == I->getParent()) |
| 1058 | return false; |
| 1059 | |
| 1060 | // If the load has other users and the truncate is not free, this probably |
| 1061 | // isn't worthwhile. |
| 1062 | if (!LI->hasOneUse() && |
Bob Wilson | ec57a1a | 2010-09-22 18:44:56 +0000 | [diff] [blame] | 1063 | TLI && (TLI->isTypeLegal(TLI->getValueType(LI->getType())) || |
| 1064 | !TLI->isTypeLegal(TLI->getValueType(I->getType()))) && |
Bob Wilson | 71dc4d9 | 2010-09-21 21:54:27 +0000 | [diff] [blame] | 1065 | !TLI->isTruncateFree(I->getType(), LI->getType())) |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1066 | return false; |
| 1067 | |
| 1068 | // Check whether the target supports casts folded into loads. |
| 1069 | unsigned LType; |
| 1070 | if (isa<ZExtInst>(I)) |
| 1071 | LType = ISD::ZEXTLOAD; |
| 1072 | else { |
| 1073 | assert(isa<SExtInst>(I) && "Unexpected ext type!"); |
| 1074 | LType = ISD::SEXTLOAD; |
| 1075 | } |
| 1076 | if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType()))) |
| 1077 | return false; |
| 1078 | |
| 1079 | // Move the extend into the same block as the load, so that SelectionDAG |
| 1080 | // can fold it. |
| 1081 | I->removeFromParent(); |
| 1082 | I->insertAfter(LI); |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1083 | ++NumExtsMoved; |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1084 | return true; |
| 1085 | } |
| 1086 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1087 | bool CodeGenPrepare::OptimizeExtUses(Instruction *I) { |
| 1088 | BasicBlock *DefBB = I->getParent(); |
| 1089 | |
Bob Wilson | 9120f5c | 2010-09-21 21:44:14 +0000 | [diff] [blame] | 1090 | // If the result of a {s|z}ext and its source are both live out, rewrite all |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1091 | // other uses of the source with result of extension. |
| 1092 | Value *Src = I->getOperand(0); |
| 1093 | if (Src->hasOneUse()) |
| 1094 | return false; |
| 1095 | |
Evan Cheng | 696e5c0 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 1096 | // Only do this xform if truncating is free. |
Gabor Greif | 53bdbd7 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 1097 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1098 | return false; |
| 1099 | |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1100 | // Only safe to perform the optimization if the source is also defined in |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1101 | // this block. |
| 1102 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1103 | return false; |
| 1104 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1105 | bool DefIsLiveOut = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1106 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1107 | UI != E; ++UI) { |
| 1108 | Instruction *User = cast<Instruction>(*UI); |
| 1109 | |
| 1110 | // Figure out which BB this ext is used in. |
| 1111 | BasicBlock *UserBB = User->getParent(); |
| 1112 | if (UserBB == DefBB) continue; |
| 1113 | DefIsLiveOut = true; |
| 1114 | break; |
| 1115 | } |
| 1116 | if (!DefIsLiveOut) |
| 1117 | return false; |
| 1118 | |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1119 | // Make sure non of the uses are PHI nodes. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1120 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1121 | UI != E; ++UI) { |
| 1122 | Instruction *User = cast<Instruction>(*UI); |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1123 | BasicBlock *UserBB = User->getParent(); |
| 1124 | if (UserBB == DefBB) continue; |
| 1125 | // Be conservative. We don't want this xform to end up introducing |
| 1126 | // reloads just before load / store instructions. |
| 1127 | if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User)) |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1128 | return false; |
| 1129 | } |
| 1130 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1131 | // InsertedTruncs - Only insert one trunc in each block once. |
| 1132 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 1133 | |
| 1134 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1135 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1136 | UI != E; ++UI) { |
| 1137 | Use &TheUse = UI.getUse(); |
| 1138 | Instruction *User = cast<Instruction>(*UI); |
| 1139 | |
| 1140 | // Figure out which BB this ext is used in. |
| 1141 | BasicBlock *UserBB = User->getParent(); |
| 1142 | if (UserBB == DefBB) continue; |
| 1143 | |
| 1144 | // Both src and def are live in this block. Rewrite the use. |
| 1145 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 1146 | |
| 1147 | if (!InsertedTrunc) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 1148 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1149 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
| 1150 | } |
| 1151 | |
| 1152 | // Replace a use of the {s|z}ext source with a use of the result. |
| 1153 | TheUse = InsertedTrunc; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1154 | ++NumExtUses; |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1155 | MadeChange = true; |
| 1156 | } |
| 1157 | |
| 1158 | return MadeChange; |
| 1159 | } |
| 1160 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1161 | /// isFormingBranchFromSelectProfitable - Returns true if a SelectInst should be |
| 1162 | /// turned into an explicit branch. |
| 1163 | static bool isFormingBranchFromSelectProfitable(SelectInst *SI) { |
| 1164 | // FIXME: This should use the same heuristics as IfConversion to determine |
| 1165 | // whether a select is better represented as a branch. This requires that |
| 1166 | // branch probability metadata is preserved for the select, which is not the |
| 1167 | // case currently. |
| 1168 | |
| 1169 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1170 | |
| 1171 | // If the branch is predicted right, an out of order CPU can avoid blocking on |
| 1172 | // the compare. Emit cmovs on compares with a memory operand as branches to |
| 1173 | // avoid stalls on the load from memory. If the compare has more than one use |
| 1174 | // there's probably another cmov or setcc around so it's not worth emitting a |
| 1175 | // branch. |
| 1176 | if (!Cmp) |
| 1177 | return false; |
| 1178 | |
| 1179 | Value *CmpOp0 = Cmp->getOperand(0); |
| 1180 | Value *CmpOp1 = Cmp->getOperand(1); |
| 1181 | |
| 1182 | // We check that the memory operand has one use to avoid uses of the loaded |
| 1183 | // value directly after the compare, making branches unprofitable. |
| 1184 | return Cmp->hasOneUse() && |
| 1185 | ((isa<LoadInst>(CmpOp0) && CmpOp0->hasOneUse()) || |
| 1186 | (isa<LoadInst>(CmpOp1) && CmpOp1->hasOneUse())); |
| 1187 | } |
| 1188 | |
| 1189 | |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1190 | /// If we have a SelectInst that will likely profit from branch prediction, |
| 1191 | /// turn it into a branch. |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1192 | bool CodeGenPrepare::OptimizeSelectInst(SelectInst *SI) { |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1193 | bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1); |
| 1194 | |
| 1195 | // Can we convert the 'select' to CF ? |
| 1196 | if (DisableSelectToBranch || OptSize || !TLI || VectorCond) |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1197 | return false; |
| 1198 | |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1199 | TargetLowering::SelectSupportKind SelectKind; |
| 1200 | if (VectorCond) |
| 1201 | SelectKind = TargetLowering::VectorMaskSelect; |
| 1202 | else if (SI->getType()->isVectorTy()) |
| 1203 | SelectKind = TargetLowering::ScalarCondVectorVal; |
| 1204 | else |
| 1205 | SelectKind = TargetLowering::ScalarValSelect; |
| 1206 | |
| 1207 | // Do we have efficient codegen support for this kind of 'selects' ? |
| 1208 | if (TLI->isSelectSupported(SelectKind)) { |
| 1209 | // We have efficient codegen support for the select instruction. |
| 1210 | // Check if it is profitable to keep this 'select'. |
| 1211 | if (!TLI->isPredictableSelectExpensive() || |
| 1212 | !isFormingBranchFromSelectProfitable(SI)) |
| 1213 | return false; |
| 1214 | } |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1215 | |
| 1216 | ModifiedDT = true; |
| 1217 | |
| 1218 | // First, we split the block containing the select into 2 blocks. |
| 1219 | BasicBlock *StartBlock = SI->getParent(); |
| 1220 | BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(SI)); |
| 1221 | BasicBlock *NextBlock = StartBlock->splitBasicBlock(SplitPt, "select.end"); |
| 1222 | |
| 1223 | // Create a new block serving as the landing pad for the branch. |
| 1224 | BasicBlock *SmallBlock = BasicBlock::Create(SI->getContext(), "select.mid", |
| 1225 | NextBlock->getParent(), NextBlock); |
| 1226 | |
| 1227 | // Move the unconditional branch from the block with the select in it into our |
| 1228 | // landing pad block. |
| 1229 | StartBlock->getTerminator()->eraseFromParent(); |
| 1230 | BranchInst::Create(NextBlock, SmallBlock); |
| 1231 | |
| 1232 | // Insert the real conditional branch based on the original condition. |
| 1233 | BranchInst::Create(NextBlock, SmallBlock, SI->getCondition(), SI); |
| 1234 | |
| 1235 | // The select itself is replaced with a PHI Node. |
| 1236 | PHINode *PN = PHINode::Create(SI->getType(), 2, "", NextBlock->begin()); |
| 1237 | PN->takeName(SI); |
| 1238 | PN->addIncoming(SI->getTrueValue(), StartBlock); |
| 1239 | PN->addIncoming(SI->getFalseValue(), SmallBlock); |
| 1240 | SI->replaceAllUsesWith(PN); |
| 1241 | SI->eraseFromParent(); |
| 1242 | |
| 1243 | // Instruct OptimizeBlock to skip to the next block. |
| 1244 | CurInstIterator = StartBlock->end(); |
| 1245 | ++NumSelectsExpanded; |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1249 | bool CodeGenPrepare::OptimizeInst(Instruction *I) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1250 | if (PHINode *P = dyn_cast<PHINode>(I)) { |
| 1251 | // It is possible for very late stage optimizations (such as SimplifyCFG) |
| 1252 | // to introduce PHI nodes too late to be cleaned up. If we detect such a |
| 1253 | // trivial PHI, go ahead and zap it here. |
| 1254 | if (Value *V = SimplifyInstruction(P)) { |
| 1255 | P->replaceAllUsesWith(V); |
| 1256 | P->eraseFromParent(); |
| 1257 | ++NumPHIsElim; |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1258 | return true; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1259 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1260 | return false; |
| 1261 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1262 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1263 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1264 | // If the source of the cast is a constant, then this should have |
| 1265 | // already been constant folded. The only reason NOT to constant fold |
| 1266 | // it is if something (e.g. LSR) was careful to place the constant |
| 1267 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 1268 | // the address of globals out of a loop). If this is the case, we don't |
| 1269 | // want to forward-subst the cast. |
| 1270 | if (isa<Constant>(CI->getOperand(0))) |
| 1271 | return false; |
| 1272 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1273 | if (TLI && OptimizeNoopCopyExpression(CI, *TLI)) |
| 1274 | return true; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1275 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1276 | if (isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 1277 | bool MadeChange = MoveExtToFormExtLoad(I); |
| 1278 | return MadeChange | OptimizeExtUses(I); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1279 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1280 | return false; |
| 1281 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1283 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 1284 | return OptimizeCmpExpression(CI); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1285 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1286 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1287 | if (TLI) |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1288 | return OptimizeMemoryInst(I, I->getOperand(0), LI->getType()); |
| 1289 | return false; |
| 1290 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1292 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1293 | if (TLI) |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1294 | return OptimizeMemoryInst(I, SI->getOperand(1), |
| 1295 | SI->getOperand(0)->getType()); |
| 1296 | return false; |
| 1297 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1299 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1300 | if (GEPI->hasAllZeroIndices()) { |
| 1301 | /// The GEP operand must be a pointer, so must its result -> BitCast |
| 1302 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
| 1303 | GEPI->getName(), GEPI); |
| 1304 | GEPI->replaceAllUsesWith(NC); |
| 1305 | GEPI->eraseFromParent(); |
| 1306 | ++NumGEPsElim; |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1307 | OptimizeInst(NC); |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1308 | return true; |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1309 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1310 | return false; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1311 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1312 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1313 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 1314 | return OptimizeCallInst(CI); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1315 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1316 | if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) |
| 1317 | return DupRetToEnableTailCallOpts(RI); |
| 1318 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1319 | if (SelectInst *SI = dyn_cast<SelectInst>(I)) |
| 1320 | return OptimizeSelectInst(SI); |
| 1321 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1322 | return false; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1325 | // In this pass we look for GEP and cast instructions that are used |
| 1326 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 1327 | // selection. |
| 1328 | bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) { |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 1329 | SunkAddrs.clear(); |
Cameron Zwarich | 56e3793 | 2011-03-02 03:31:46 +0000 | [diff] [blame] | 1330 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1332 | CurInstIterator = BB.begin(); |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 1333 | for (BasicBlock::iterator E = BB.end(); CurInstIterator != E; ) |
| 1334 | MadeChange |= OptimizeInst(CurInstIterator++); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1336 | return MadeChange; |
| 1337 | } |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 1338 | |
| 1339 | // llvm.dbg.value is far away from the value then iSel may not be able |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1340 | // handle it properly. iSel will drop llvm.dbg.value if it can not |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 1341 | // find a node corresponding to the value. |
| 1342 | bool CodeGenPrepare::PlaceDbgValues(Function &F) { |
| 1343 | bool MadeChange = false; |
| 1344 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 1345 | Instruction *PrevNonDbgInst = NULL; |
| 1346 | for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) { |
| 1347 | Instruction *Insn = BI; ++BI; |
| 1348 | DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn); |
| 1349 | if (!DVI) { |
| 1350 | PrevNonDbgInst = Insn; |
| 1351 | continue; |
| 1352 | } |
| 1353 | |
| 1354 | Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue()); |
| 1355 | if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) { |
| 1356 | DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI); |
| 1357 | DVI->removeFromParent(); |
| 1358 | if (isa<PHINode>(VI)) |
| 1359 | DVI->insertBefore(VI->getParent()->getFirstInsertionPt()); |
| 1360 | else |
| 1361 | DVI->insertAfter(VI); |
| 1362 | MadeChange = true; |
| 1363 | ++NumDbgValueMoved; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | return MadeChange; |
| 1368 | } |