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