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