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" |
| 23 | #include "llvm/Pass.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetAsmInfo.h" |
| 25 | #include "llvm/Target/TargetData.h" |
| 26 | #include "llvm/Target/TargetLowering.h" |
| 27 | #include "llvm/Target/TargetMachine.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" |
| 31 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | 03ce042 | 2009-02-13 17:45:12 +0000 | [diff] [blame] | 33 | #include "llvm/Assembly/Writer.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CallSite.h" |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 38 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 39 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 41 | using namespace llvm::PatternMatch; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 42 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 43 | static cl::opt<bool> FactorCommonPreds("split-critical-paths-tweak", |
| 44 | cl::init(false), cl::Hidden); |
| 45 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 46 | namespace { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 47 | class VISIBILITY_HIDDEN CodeGenPrepare : public FunctionPass { |
| 48 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 49 | /// transformation profitability. |
| 50 | const TargetLowering *TLI; |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 51 | |
| 52 | /// BackEdges - Keep a set of all the loop back edges. |
| 53 | /// |
| 54 | SmallSet<std::pair<BasicBlock*,BasicBlock*>, 8> BackEdges; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 55 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 56 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 57 | explicit CodeGenPrepare(const TargetLowering *tli = 0) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 58 | : FunctionPass(&ID), TLI(tli) {} |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 59 | bool runOnFunction(Function &F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 60 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 61 | private: |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 62 | bool EliminateMostlyEmptyBlocks(Function &F); |
| 63 | bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 64 | void EliminateMostlyEmptyBlock(BasicBlock *BB); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 65 | bool OptimizeBlock(BasicBlock &BB); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 66 | bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy, |
| 67 | DenseMap<Value*,Value*> &SunkAddrs); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 68 | bool OptimizeInlineAsmInst(Instruction *I, CallSite CS, |
| 69 | DenseMap<Value*,Value*> &SunkAddrs); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 70 | bool OptimizeExtUses(Instruction *I); |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 71 | void findLoopBackEdges(Function &F); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 72 | }; |
| 73 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 74 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 75 | char CodeGenPrepare::ID = 0; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 76 | static RegisterPass<CodeGenPrepare> X("codegenprepare", |
| 77 | "Optimize for code generation"); |
| 78 | |
| 79 | FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) { |
| 80 | return new CodeGenPrepare(TLI); |
| 81 | } |
| 82 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 83 | /// findLoopBackEdges - Do a DFS walk to find loop back edges. |
| 84 | /// |
| 85 | void CodeGenPrepare::findLoopBackEdges(Function &F) { |
| 86 | SmallPtrSet<BasicBlock*, 8> Visited; |
| 87 | SmallVector<std::pair<BasicBlock*, succ_iterator>, 8> VisitStack; |
| 88 | SmallPtrSet<BasicBlock*, 8> InStack; |
| 89 | |
| 90 | BasicBlock *BB = &F.getEntryBlock(); |
| 91 | if (succ_begin(BB) == succ_end(BB)) |
| 92 | return; |
| 93 | Visited.insert(BB); |
| 94 | VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); |
| 95 | InStack.insert(BB); |
| 96 | do { |
| 97 | std::pair<BasicBlock*, succ_iterator> &Top = VisitStack.back(); |
| 98 | BasicBlock *ParentBB = Top.first; |
| 99 | succ_iterator &I = Top.second; |
| 100 | |
| 101 | bool FoundNew = false; |
| 102 | while (I != succ_end(ParentBB)) { |
| 103 | BB = *I++; |
| 104 | if (Visited.insert(BB)) { |
| 105 | FoundNew = true; |
| 106 | break; |
| 107 | } |
| 108 | // Successor is in VisitStack, it's a back edge. |
| 109 | if (InStack.count(BB)) |
| 110 | BackEdges.insert(std::make_pair(ParentBB, BB)); |
| 111 | } |
| 112 | |
| 113 | if (FoundNew) { |
| 114 | // Go down one level if there is a unvisited successor. |
| 115 | InStack.insert(BB); |
| 116 | VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); |
| 117 | } else { |
| 118 | // Go up one level. |
| 119 | std::pair<BasicBlock*, succ_iterator> &Pop = VisitStack.back(); |
| 120 | InStack.erase(Pop.first); |
| 121 | VisitStack.pop_back(); |
| 122 | } |
| 123 | } while (!VisitStack.empty()); |
| 124 | } |
| 125 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 126 | |
| 127 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 128 | bool EverMadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 129 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 130 | // First pass, eliminate blocks that contain only PHI nodes and an |
| 131 | // unconditional branch. |
| 132 | EverMadeChange |= EliminateMostlyEmptyBlocks(F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 133 | |
Evan Cheng | 7e66c0d | 2009-01-05 21:17:27 +0000 | [diff] [blame] | 134 | // Now find loop back edges. |
| 135 | findLoopBackEdges(F); |
| 136 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 137 | bool MadeChange = true; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 138 | while (MadeChange) { |
| 139 | MadeChange = false; |
| 140 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 141 | MadeChange |= OptimizeBlock(*BB); |
| 142 | EverMadeChange |= MadeChange; |
| 143 | } |
| 144 | return EverMadeChange; |
| 145 | } |
| 146 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 147 | /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 148 | /// and an unconditional branch. Passes before isel (e.g. LSR/loopsimplify) |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 149 | /// often split edges in ways that are non-optimal for isel. Start by |
| 150 | /// eliminating these blocks so we can split them the way we want them. |
| 151 | bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) { |
| 152 | bool MadeChange = false; |
| 153 | // Note that this intentionally skips the entry block. |
| 154 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 155 | BasicBlock *BB = I++; |
| 156 | |
| 157 | // If this block doesn't end with an uncond branch, ignore it. |
| 158 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 159 | if (!BI || !BI->isUnconditional()) |
| 160 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 161 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 162 | // If the instruction before the branch isn't a phi node, then other stuff |
| 163 | // is happening here. |
| 164 | BasicBlock::iterator BBI = BI; |
| 165 | if (BBI != BB->begin()) { |
| 166 | --BBI; |
| 167 | if (!isa<PHINode>(BBI)) continue; |
| 168 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 169 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 170 | // Do not break infinite loops. |
| 171 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 172 | if (DestBB == BB) |
| 173 | continue; |
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 | if (!CanMergeBlocks(BB, DestBB)) |
| 176 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 177 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 178 | EliminateMostlyEmptyBlock(BB); |
| 179 | MadeChange = true; |
| 180 | } |
| 181 | return MadeChange; |
| 182 | } |
| 183 | |
| 184 | /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a |
| 185 | /// single uncond branch between them, and BB contains no other non-phi |
| 186 | /// instructions. |
| 187 | bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB, |
| 188 | const BasicBlock *DestBB) const { |
| 189 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 190 | // the successor. If there are more complex condition (e.g. preheaders), |
| 191 | // don't mess around with them. |
| 192 | BasicBlock::const_iterator BBI = BB->begin(); |
| 193 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 194 | for (Value::use_const_iterator UI = PN->use_begin(), E = PN->use_end(); |
| 195 | UI != E; ++UI) { |
| 196 | const Instruction *User = cast<Instruction>(*UI); |
| 197 | if (User->getParent() != DestBB || !isa<PHINode>(User)) |
| 198 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 199 | // If User is inside DestBB block and it is a PHINode then check |
| 200 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | 75abc1e | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 201 | // a complex condition (e.g. preheaders) we want to avoid here. |
| 202 | if (User->getParent() == DestBB) { |
| 203 | if (const PHINode *UPN = dyn_cast<PHINode>(User)) |
| 204 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 205 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 206 | if (Insn && Insn->getParent() == BB && |
| 207 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 208 | return false; |
| 209 | } |
| 210 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
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 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 215 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 216 | // can't merge the block. |
| 217 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 218 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 219 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 220 | // Collect the preds of BB. |
Chris Lattner | f67f73a | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 221 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 222 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 223 | // It is faster to get preds from a PHI than with pred_iterator. |
| 224 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 225 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 226 | } else { |
| 227 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 228 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 229 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 230 | // Walk the preds of DestBB. |
| 231 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 232 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 233 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 234 | BBI = DestBB->begin(); |
| 235 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 236 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 237 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 238 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 239 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 240 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 241 | if (V2PN->getParent() == BB) |
| 242 | V2 = V2PN->getIncomingValueForBlock(Pred); |
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 there is a conflict, bail out. |
| 245 | if (V1 != V2) return false; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and |
| 255 | /// an unconditional branch in it. |
| 256 | void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) { |
| 257 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 258 | BasicBlock *DestBB = BI->getSuccessor(0); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 259 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 260 | DOUT << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 261 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 262 | // If the destination block has a single pred, then this is a trivial edge, |
| 263 | // just collapse it. |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 264 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 265 | if (SinglePred != DestBB) { |
| 266 | // Remember if SinglePred was the entry block of the function. If so, we |
| 267 | // will need to move BB back to the entry position. |
| 268 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
| 269 | MergeBasicBlockIntoOnlyPred(DestBB); |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 270 | |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 271 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 272 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
| 273 | |
| 274 | DOUT << "AFTER:\n" << *DestBB << "\n\n\n"; |
| 275 | return; |
| 276 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 277 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 278 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 279 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 280 | // to handle the new incoming edges it is about to have. |
| 281 | PHINode *PN; |
| 282 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 283 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 284 | // Remove the incoming value for BB, and remember it. |
| 285 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 286 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 287 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 288 | // value that dominates BB. |
| 289 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 290 | if (InValPhi && InValPhi->getParent() == BB) { |
| 291 | // Add all of the input values of the input PHI as inputs of this phi. |
| 292 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 293 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 294 | InValPhi->getIncomingBlock(i)); |
| 295 | } else { |
| 296 | // Otherwise, add one instance of the dominating value for each edge that |
| 297 | // we will be adding. |
| 298 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 299 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 300 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 301 | } else { |
| 302 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 303 | PN->addIncoming(InVal, *PI); |
| 304 | } |
| 305 | } |
| 306 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 307 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 308 | // The PHIs are now updated, change everything that refers to BB to use |
| 309 | // DestBB and remove BB. |
| 310 | BB->replaceAllUsesWith(DestBB); |
| 311 | BB->eraseFromParent(); |
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 | DOUT << "AFTER:\n" << *DestBB << "\n\n\n"; |
| 314 | } |
| 315 | |
| 316 | |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 317 | /// SplitEdgeNicely - Split the critical edge from TI to its specified |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 318 | /// successor if it will improve codegen. We only do this if the successor has |
| 319 | /// phi nodes (otherwise critical edges are ok). If there is already another |
| 320 | /// predecessor of the succ that is empty (and thus has no phi nodes), use it |
| 321 | /// instead of introducing a new block. |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 322 | static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum, |
| 323 | SmallSet<std::pair<BasicBlock*,BasicBlock*>, 8> &BackEdges, |
| 324 | Pass *P) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 325 | BasicBlock *TIBB = TI->getParent(); |
| 326 | BasicBlock *Dest = TI->getSuccessor(SuccNum); |
| 327 | assert(isa<PHINode>(Dest->begin()) && |
| 328 | "This should only be called if Dest has a PHI!"); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 329 | |
Evan Cheng | fc0b80d | 2009-03-13 22:59:14 +0000 | [diff] [blame^] | 330 | // Do not split edges to EH landing pads. |
| 331 | if (InvokeInst *Invoke = dyn_cast<InvokeInst>(TI)) { |
| 332 | if (Invoke->getSuccessor(1) == Dest) |
| 333 | return; |
| 334 | } |
| 335 | |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 336 | // As a hack, never split backedges of loops. Even though the copy for any |
| 337 | // PHIs inserted on the backedge would be dead for exits from the loop, we |
| 338 | // assume that the cost of *splitting* the backedge would be too high. |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 339 | if (BackEdges.count(std::make_pair(TIBB, Dest))) |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 340 | return; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 341 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 342 | if (!FactorCommonPreds) { |
| 343 | /// TIPHIValues - This array is lazily computed to determine the values of |
| 344 | /// PHIs in Dest that TI would provide. |
| 345 | SmallVector<Value*, 32> TIPHIValues; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 346 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 347 | // Check to see if Dest has any blocks that can be used as a split edge for |
| 348 | // this terminator. |
| 349 | for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) { |
| 350 | BasicBlock *Pred = *PI; |
| 351 | // To be usable, the pred has to end with an uncond branch to the dest. |
| 352 | BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator()); |
| 353 | if (!PredBr || !PredBr->isUnconditional() || |
| 354 | // Must be empty other than the branch. |
| 355 | &Pred->front() != PredBr || |
| 356 | // Cannot be the entry block; its label does not get emitted. |
| 357 | Pred == &(Dest->getParent()->getEntryBlock())) |
| 358 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 359 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 360 | // Finally, since we know that Dest has phi nodes in it, we have to make |
| 361 | // sure that jumping to Pred will have the same affect as going to Dest in |
| 362 | // terms of PHI values. |
| 363 | PHINode *PN; |
| 364 | unsigned PHINo = 0; |
| 365 | bool FoundMatch = true; |
| 366 | for (BasicBlock::iterator I = Dest->begin(); |
| 367 | (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) { |
| 368 | if (PHINo == TIPHIValues.size()) |
| 369 | TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB)); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 370 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 371 | // If the PHI entry doesn't work, we can't use this pred. |
| 372 | if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) { |
| 373 | FoundMatch = false; |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // If we found a workable predecessor, change TI to branch to Succ. |
| 379 | if (FoundMatch) { |
| 380 | Dest->removePredecessor(TIBB); |
| 381 | TI->setSuccessor(SuccNum, Pred); |
| 382 | return; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 385 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 386 | SplitCriticalEdge(TI, SuccNum, P, true); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | PHINode *PN; |
| 391 | SmallVector<Value*, 8> TIPHIValues; |
| 392 | for (BasicBlock::iterator I = Dest->begin(); |
| 393 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 394 | TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB)); |
| 395 | |
| 396 | SmallVector<BasicBlock*, 8> IdenticalPreds; |
| 397 | for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) { |
| 398 | BasicBlock *Pred = *PI; |
| 399 | if (BackEdges.count(std::make_pair(Pred, Dest))) |
| 400 | continue; |
| 401 | if (PI == TIBB) |
| 402 | IdenticalPreds.push_back(Pred); |
| 403 | else { |
| 404 | bool Identical = true; |
| 405 | unsigned PHINo = 0; |
| 406 | for (BasicBlock::iterator I = Dest->begin(); |
| 407 | (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) |
| 408 | if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) { |
| 409 | Identical = false; |
| 410 | break; |
| 411 | } |
| 412 | if (Identical) |
| 413 | IdenticalPreds.push_back(Pred); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 416 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 417 | assert(!IdenticalPreds.empty()); |
| 418 | SplitBlockPredecessors(Dest, &IdenticalPreds[0], IdenticalPreds.size(), |
| 419 | ".critedge", P); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 422 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 423 | /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop |
| 424 | /// copy (e.g. it's casting from one pointer type to another, int->uint, or |
| 425 | /// int->sbyte on PPC), sink it into user blocks to reduce the number of virtual |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 426 | /// registers that must be created and coalesced. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 427 | /// |
| 428 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 429 | /// |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 430 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){ |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 431 | // If this is a noop copy, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 432 | MVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 433 | MVT DstVT = TLI.getValueType(CI->getType()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 434 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 435 | // This is an fp<->int conversion? |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 436 | if (SrcVT.isInteger() != DstVT.isInteger()) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 437 | return false; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 438 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 439 | // If this is an extension, it will be a zero or sign extension, which |
| 440 | // isn't a noop. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 441 | if (SrcVT.bitsLT(DstVT)) return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 442 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 443 | // If these values will be promoted, find out what they will be promoted |
| 444 | // to. This helps us consider truncates on PPC as noop copies when they |
| 445 | // are. |
| 446 | if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote) |
| 447 | SrcVT = TLI.getTypeToTransformTo(SrcVT); |
| 448 | if (TLI.getTypeAction(DstVT) == TargetLowering::Promote) |
| 449 | DstVT = TLI.getTypeToTransformTo(DstVT); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 450 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 451 | // If, after promotion, these are the same types, this is a noop copy. |
| 452 | if (SrcVT != DstVT) |
| 453 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 454 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 455 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 456 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 457 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 458 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 459 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 460 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 461 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 462 | UI != E; ) { |
| 463 | Use &TheUse = UI.getUse(); |
| 464 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 465 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 466 | // Figure out which BB this cast is used in. For PHI's this is the |
| 467 | // appropriate predecessor block. |
| 468 | BasicBlock *UserBB = User->getParent(); |
| 469 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
Gabor Greif | a36791d | 2009-01-23 19:40:15 +0000 | [diff] [blame] | 470 | UserBB = PN->getIncomingBlock(UI); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 471 | } |
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 | // Preincrement use iterator so we don't invalidate it. |
| 474 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 475 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 476 | // If this user is in the same block as the cast, don't change the cast. |
| 477 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 478 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 479 | // If we have already inserted a cast into this block, use it. |
| 480 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 481 | |
| 482 | if (!InsertedCast) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 483 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 484 | |
| 485 | InsertedCast = |
| 486 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 487 | InsertPt); |
| 488 | MadeChange = true; |
| 489 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 490 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 491 | // 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] | 492 | TheUse = InsertedCast; |
| 493 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 494 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 495 | // If we removed all uses, nuke the cast. |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 496 | if (CI->use_empty()) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 497 | CI->eraseFromParent(); |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 498 | MadeChange = true; |
| 499 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 500 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 501 | return MadeChange; |
| 502 | } |
| 503 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 504 | /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 505 | /// 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] | 506 | /// a clear win except on targets with multiple condition code registers |
| 507 | /// (PowerPC), where it might lose; some adjustment may be wanted there. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 508 | /// |
| 509 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 510 | static bool OptimizeCmpExpression(CmpInst *CI) { |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 511 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 512 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 513 | /// InsertedCmp - Only insert a cmp in each block once. |
| 514 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 515 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 516 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 517 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 518 | UI != E; ) { |
| 519 | Use &TheUse = UI.getUse(); |
| 520 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 521 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 522 | // Preincrement use iterator so we don't invalidate it. |
| 523 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 524 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 525 | // Don't bother for PHI nodes. |
| 526 | if (isa<PHINode>(User)) |
| 527 | continue; |
| 528 | |
| 529 | // Figure out which BB this cmp is used in. |
| 530 | BasicBlock *UserBB = User->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 531 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 532 | // If this user is in the same block as the cmp, don't change the cmp. |
| 533 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 534 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 535 | // If we have already inserted a cmp into this block, use it. |
| 536 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 537 | |
| 538 | if (!InsertedCmp) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 539 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 540 | |
| 541 | InsertedCmp = |
| 542 | CmpInst::Create(CI->getOpcode(), CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 543 | CI->getOperand(1), "", InsertPt); |
| 544 | MadeChange = true; |
| 545 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 546 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 547 | // Replace a use of the cmp with a use of the new cmp. |
| 548 | TheUse = InsertedCmp; |
| 549 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 550 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 551 | // If we removed all uses, nuke the cmp. |
| 552 | if (CI->use_empty()) |
| 553 | CI->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 554 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 555 | return MadeChange; |
| 556 | } |
| 557 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 558 | //===----------------------------------------------------------------------===// |
| 559 | // Addressing Mode Analysis and Optimization |
| 560 | //===----------------------------------------------------------------------===// |
| 561 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 562 | //===----------------------------------------------------------------------===// |
| 563 | // Memory Optimization |
| 564 | //===----------------------------------------------------------------------===// |
| 565 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 566 | /// IsNonLocalValue - Return true if the specified values are defined in a |
| 567 | /// different basic block than BB. |
| 568 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 569 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 570 | return I->getParent() != BB; |
| 571 | return false; |
| 572 | } |
| 573 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 574 | /// OptimizeMemoryInst - Load and Store Instructions have often have |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 575 | /// addressing modes that can do significant amounts of computation. As such, |
| 576 | /// instruction selection will try to get the load or store to do as much |
| 577 | /// computation as possible for the program. The problem is that isel can only |
| 578 | /// see within a single block. As such, we sink as much legal addressing mode |
| 579 | /// stuff into the block as possible. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 580 | /// |
| 581 | /// This method is used to optimize both load/store and inline asms with memory |
| 582 | /// operands. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 583 | bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr, |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 584 | const Type *AccessTy, |
| 585 | DenseMap<Value*,Value*> &SunkAddrs) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 586 | // Figure out what addressing mode will be built up for this operation. |
| 587 | SmallVector<Instruction*, 16> AddrModeInsts; |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 588 | ExtAddrMode AddrMode = AddressingModeMatcher::Match(Addr, AccessTy,MemoryInst, |
| 589 | AddrModeInsts, *TLI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 590 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 591 | // Check to see if any of the instructions supersumed by this addr mode are |
| 592 | // non-local to I's BB. |
| 593 | bool AnyNonLocal = false; |
| 594 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 595 | if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 596 | AnyNonLocal = true; |
| 597 | break; |
| 598 | } |
| 599 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 600 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 601 | // If all the instructions matched are already in this BB, don't do anything. |
| 602 | if (!AnyNonLocal) { |
| 603 | DEBUG(cerr << "CGP: Found local addrmode: " << AddrMode << "\n"); |
| 604 | return false; |
| 605 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 606 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 607 | // Insert this computation right after this user. Since our caller is |
| 608 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 609 | // guaranteed to happen later. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 610 | BasicBlock::iterator InsertPt = MemoryInst; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 611 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 612 | // Now that we determined the addressing expression we want to use and know |
| 613 | // that we have to sink it into this block. Check to see if we have already |
| 614 | // done this for some other load/store instr in this block. If so, reuse the |
| 615 | // computation. |
| 616 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 617 | if (SunkAddr) { |
Chris Lattner | 65c02fb | 2009-02-12 06:56:08 +0000 | [diff] [blame] | 618 | DEBUG(cerr << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " |
| 619 | << *MemoryInst); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 620 | if (SunkAddr->getType() != Addr->getType()) |
| 621 | SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt); |
| 622 | } else { |
Chris Lattner | 65c02fb | 2009-02-12 06:56:08 +0000 | [diff] [blame] | 623 | DEBUG(cerr << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
| 624 | << *MemoryInst); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 625 | const Type *IntPtrTy = TLI->getTargetData()->getIntPtrType(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 626 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 627 | Value *Result = 0; |
| 628 | // Start with the scale value. |
| 629 | if (AddrMode.Scale) { |
| 630 | Value *V = AddrMode.ScaledReg; |
| 631 | if (V->getType() == IntPtrTy) { |
| 632 | // done. |
| 633 | } else if (isa<PointerType>(V->getType())) { |
| 634 | V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 635 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 636 | cast<IntegerType>(V->getType())->getBitWidth()) { |
| 637 | V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 638 | } else { |
| 639 | V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 640 | } |
| 641 | if (AddrMode.Scale != 1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 642 | V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy, |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 643 | AddrMode.Scale), |
| 644 | "sunkaddr", InsertPt); |
| 645 | Result = V; |
| 646 | } |
| 647 | |
| 648 | // Add in the base register. |
| 649 | if (AddrMode.BaseReg) { |
| 650 | Value *V = AddrMode.BaseReg; |
| 651 | if (V->getType() != IntPtrTy) |
| 652 | V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 653 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 654 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 655 | else |
| 656 | Result = V; |
| 657 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 658 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 659 | // Add in the BaseGV if present. |
| 660 | if (AddrMode.BaseGV) { |
| 661 | Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr", |
| 662 | InsertPt); |
| 663 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 664 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 665 | else |
| 666 | Result = V; |
| 667 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 668 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 669 | // Add in the Base Offset if present. |
| 670 | if (AddrMode.BaseOffs) { |
| 671 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
| 672 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 673 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 674 | else |
| 675 | Result = V; |
| 676 | } |
| 677 | |
| 678 | if (Result == 0) |
| 679 | SunkAddr = Constant::getNullValue(Addr->getType()); |
| 680 | else |
| 681 | SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt); |
| 682 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 684 | MemoryInst->replaceUsesOfWith(Addr, SunkAddr); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 685 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 686 | if (Addr->use_empty()) |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 687 | RecursivelyDeleteTriviallyDeadInstructions(Addr); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 688 | return true; |
| 689 | } |
| 690 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 691 | /// OptimizeInlineAsmInst - If there are any memory operands, use |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 692 | /// OptimizeMemoryInst to sink their address computing into the block when |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 693 | /// possible / profitable. |
| 694 | bool CodeGenPrepare::OptimizeInlineAsmInst(Instruction *I, CallSite CS, |
| 695 | DenseMap<Value*,Value*> &SunkAddrs) { |
| 696 | bool MadeChange = false; |
| 697 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 698 | |
| 699 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 700 | // ConstraintOperands list. |
| 701 | std::vector<InlineAsm::ConstraintInfo> |
| 702 | ConstraintInfos = IA->ParseConstraints(); |
| 703 | |
| 704 | /// ConstraintOperands - Information about all of the constraints. |
| 705 | std::vector<TargetLowering::AsmOperandInfo> ConstraintOperands; |
| 706 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 707 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 708 | ConstraintOperands. |
| 709 | push_back(TargetLowering::AsmOperandInfo(ConstraintInfos[i])); |
| 710 | TargetLowering::AsmOperandInfo &OpInfo = ConstraintOperands.back(); |
| 711 | |
| 712 | // Compute the value type for each operand. |
| 713 | switch (OpInfo.Type) { |
| 714 | case InlineAsm::isOutput: |
| 715 | if (OpInfo.isIndirect) |
| 716 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 717 | break; |
| 718 | case InlineAsm::isInput: |
| 719 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 720 | break; |
| 721 | case InlineAsm::isClobber: |
| 722 | // Nothing to do. |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | a7e6146 | 2008-09-24 06:48:55 +0000 | [diff] [blame] | 727 | TLI->ComputeConstraintToUse(OpInfo, SDValue(), |
| 728 | OpInfo.ConstraintType == TargetLowering::C_Memory); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 729 | |
Eli Friedman | 9ec8095 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 730 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 731 | OpInfo.isIndirect) { |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 732 | Value *OpVal = OpInfo.CallOperandVal; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 733 | MadeChange |= OptimizeMemoryInst(I, OpVal, OpVal->getType(), SunkAddrs); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
| 737 | return MadeChange; |
| 738 | } |
| 739 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 740 | bool CodeGenPrepare::OptimizeExtUses(Instruction *I) { |
| 741 | BasicBlock *DefBB = I->getParent(); |
| 742 | |
| 743 | // If both result of the {s|z}xt and its source are live out, rewrite all |
| 744 | // other uses of the source with result of extension. |
| 745 | Value *Src = I->getOperand(0); |
| 746 | if (Src->hasOneUse()) |
| 747 | return false; |
| 748 | |
Evan Cheng | 696e5c0 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 749 | // Only do this xform if truncating is free. |
Gabor Greif | 53bdbd7 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 750 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 751 | return false; |
| 752 | |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 753 | // 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] | 754 | // this block. |
| 755 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 756 | return false; |
| 757 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 758 | bool DefIsLiveOut = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 759 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 760 | UI != E; ++UI) { |
| 761 | Instruction *User = cast<Instruction>(*UI); |
| 762 | |
| 763 | // Figure out which BB this ext is used in. |
| 764 | BasicBlock *UserBB = User->getParent(); |
| 765 | if (UserBB == DefBB) continue; |
| 766 | DefIsLiveOut = true; |
| 767 | break; |
| 768 | } |
| 769 | if (!DefIsLiveOut) |
| 770 | return false; |
| 771 | |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 772 | // Make sure non of the uses are PHI nodes. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 773 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 774 | UI != E; ++UI) { |
| 775 | Instruction *User = cast<Instruction>(*UI); |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 776 | BasicBlock *UserBB = User->getParent(); |
| 777 | if (UserBB == DefBB) continue; |
| 778 | // Be conservative. We don't want this xform to end up introducing |
| 779 | // reloads just before load / store instructions. |
| 780 | if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User)) |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 781 | return false; |
| 782 | } |
| 783 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 784 | // InsertedTruncs - Only insert one trunc in each block once. |
| 785 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 786 | |
| 787 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 788 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 789 | UI != E; ++UI) { |
| 790 | Use &TheUse = UI.getUse(); |
| 791 | Instruction *User = cast<Instruction>(*UI); |
| 792 | |
| 793 | // Figure out which BB this ext is used in. |
| 794 | BasicBlock *UserBB = User->getParent(); |
| 795 | if (UserBB == DefBB) continue; |
| 796 | |
| 797 | // Both src and def are live in this block. Rewrite the use. |
| 798 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 799 | |
| 800 | if (!InsertedTrunc) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 801 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 802 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 803 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
| 804 | } |
| 805 | |
| 806 | // Replace a use of the {s|z}ext source with a use of the result. |
| 807 | TheUse = InsertedTrunc; |
| 808 | |
| 809 | MadeChange = true; |
| 810 | } |
| 811 | |
| 812 | return MadeChange; |
| 813 | } |
| 814 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 815 | // In this pass we look for GEP and cast instructions that are used |
| 816 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 817 | // selection. |
| 818 | bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) { |
| 819 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 820 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 821 | // Split all critical edges where the dest block has a PHI. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 822 | TerminatorInst *BBTI = BB.getTerminator(); |
| 823 | if (BBTI->getNumSuccessors() > 1) { |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 824 | for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) { |
| 825 | BasicBlock *SuccBB = BBTI->getSuccessor(i); |
| 826 | if (isa<PHINode>(SuccBB->begin()) && isCriticalEdge(BBTI, i, true)) |
| 827 | SplitEdgeNicely(BBTI, i, BackEdges, this); |
| 828 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 829 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 830 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 831 | // Keep track of non-local addresses that have been sunk into this block. |
| 832 | // This allows us to avoid inserting duplicate code for blocks with multiple |
| 833 | // load/stores of the same address. |
| 834 | DenseMap<Value*, Value*> SunkAddrs; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 835 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 836 | for (BasicBlock::iterator BBI = BB.begin(), E = BB.end(); BBI != E; ) { |
| 837 | Instruction *I = BBI++; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 838 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 839 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 840 | // If the source of the cast is a constant, then this should have |
| 841 | // already been constant folded. The only reason NOT to constant fold |
| 842 | // it is if something (e.g. LSR) was careful to place the constant |
| 843 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 844 | // the address of globals out of a loop). If this is the case, we don't |
| 845 | // want to forward-subst the cast. |
| 846 | if (isa<Constant>(CI->getOperand(0))) |
| 847 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 848 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 849 | bool Change = false; |
| 850 | if (TLI) { |
| 851 | Change = OptimizeNoopCopyExpression(CI, *TLI); |
| 852 | MadeChange |= Change; |
| 853 | } |
| 854 | |
Evan Cheng | 55e641b | 2008-03-19 22:02:26 +0000 | [diff] [blame] | 855 | if (!Change && (isa<ZExtInst>(I) || isa<SExtInst>(I))) |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 856 | MadeChange |= OptimizeExtUses(I); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 857 | } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) { |
| 858 | MadeChange |= OptimizeCmpExpression(CI); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 859 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 860 | if (TLI) |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 861 | MadeChange |= OptimizeMemoryInst(I, I->getOperand(0), LI->getType(), |
| 862 | SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 863 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 864 | if (TLI) |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 865 | MadeChange |= OptimizeMemoryInst(I, SI->getOperand(1), |
| 866 | SI->getOperand(0)->getType(), |
| 867 | SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 868 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Chris Lattner | f25646b | 2007-04-14 00:17:39 +0000 | [diff] [blame] | 869 | if (GEPI->hasAllZeroIndices()) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 870 | /// The GEP operand must be a pointer, so must its result -> BitCast |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 871 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 872 | GEPI->getName(), GEPI); |
| 873 | GEPI->replaceAllUsesWith(NC); |
| 874 | GEPI->eraseFromParent(); |
| 875 | MadeChange = true; |
| 876 | BBI = NC; |
| 877 | } |
| 878 | } else if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 879 | // If we found an inline asm expession, and if the target knows how to |
| 880 | // lower it to normal LLVM code, do so now. |
| 881 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 882 | if (const TargetAsmInfo *TAI = |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 883 | TLI->getTargetMachine().getTargetAsmInfo()) { |
Chris Lattner | 65c02fb | 2009-02-12 06:56:08 +0000 | [diff] [blame] | 884 | if (TAI->ExpandInlineAsm(CI)) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 885 | BBI = BB.begin(); |
Chris Lattner | 65c02fb | 2009-02-12 06:56:08 +0000 | [diff] [blame] | 886 | // Avoid processing instructions out of order, which could cause |
| 887 | // reuse before a value is defined. |
| 888 | SunkAddrs.clear(); |
| 889 | } else |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 890 | // Sink address computing for memory operands into the block. |
| 891 | MadeChange |= OptimizeInlineAsmInst(I, &(*CI), SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 892 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 893 | } |
| 894 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 895 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 896 | return MadeChange; |
| 897 | } |