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