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