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