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" |
| 28 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Local.h" |
| 30 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CallSite.h" |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 35 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 36 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 38 | using namespace llvm::PatternMatch; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 39 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 40 | namespace { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 41 | class VISIBILITY_HIDDEN CodeGenPrepare : public FunctionPass { |
| 42 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 43 | /// transformation profitability. |
| 44 | const TargetLowering *TLI; |
| 45 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 46 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 47 | explicit CodeGenPrepare(const TargetLowering *tli = 0) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 48 | : FunctionPass(&ID), TLI(tli) {} |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 49 | bool runOnFunction(Function &F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 50 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 51 | private: |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 52 | bool EliminateMostlyEmptyBlocks(Function &F); |
| 53 | bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 54 | void EliminateMostlyEmptyBlock(BasicBlock *BB); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 55 | bool OptimizeBlock(BasicBlock &BB); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 56 | bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy, |
| 57 | DenseMap<Value*,Value*> &SunkAddrs); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 58 | bool OptimizeInlineAsmInst(Instruction *I, CallSite CS, |
| 59 | DenseMap<Value*,Value*> &SunkAddrs); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 60 | bool OptimizeExtUses(Instruction *I); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 61 | }; |
| 62 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 63 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 64 | char CodeGenPrepare::ID = 0; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 65 | static RegisterPass<CodeGenPrepare> X("codegenprepare", |
| 66 | "Optimize for code generation"); |
| 67 | |
| 68 | FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) { |
| 69 | return new CodeGenPrepare(TLI); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 74 | bool EverMadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 75 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 76 | // First pass, eliminate blocks that contain only PHI nodes and an |
| 77 | // unconditional branch. |
| 78 | EverMadeChange |= EliminateMostlyEmptyBlocks(F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 79 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 80 | bool MadeChange = true; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 81 | while (MadeChange) { |
| 82 | MadeChange = false; |
| 83 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 84 | MadeChange |= OptimizeBlock(*BB); |
| 85 | EverMadeChange |= MadeChange; |
| 86 | } |
| 87 | return EverMadeChange; |
| 88 | } |
| 89 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 90 | /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 91 | /// and an unconditional branch. Passes before isel (e.g. LSR/loopsimplify) |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 92 | /// often split edges in ways that are non-optimal for isel. Start by |
| 93 | /// eliminating these blocks so we can split them the way we want them. |
| 94 | bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) { |
| 95 | bool MadeChange = false; |
| 96 | // Note that this intentionally skips the entry block. |
| 97 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 98 | BasicBlock *BB = I++; |
| 99 | |
| 100 | // If this block doesn't end with an uncond branch, ignore it. |
| 101 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 102 | if (!BI || !BI->isUnconditional()) |
| 103 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 104 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 105 | // If the instruction before the branch isn't a phi node, then other stuff |
| 106 | // is happening here. |
| 107 | BasicBlock::iterator BBI = BI; |
| 108 | if (BBI != BB->begin()) { |
| 109 | --BBI; |
| 110 | if (!isa<PHINode>(BBI)) continue; |
| 111 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 112 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 113 | // Do not break infinite loops. |
| 114 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 115 | if (DestBB == BB) |
| 116 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 117 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 118 | if (!CanMergeBlocks(BB, DestBB)) |
| 119 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 120 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 121 | EliminateMostlyEmptyBlock(BB); |
| 122 | MadeChange = true; |
| 123 | } |
| 124 | return MadeChange; |
| 125 | } |
| 126 | |
| 127 | /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a |
| 128 | /// single uncond branch between them, and BB contains no other non-phi |
| 129 | /// instructions. |
| 130 | bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB, |
| 131 | const BasicBlock *DestBB) const { |
| 132 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 133 | // the successor. If there are more complex condition (e.g. preheaders), |
| 134 | // don't mess around with them. |
| 135 | BasicBlock::const_iterator BBI = BB->begin(); |
| 136 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 137 | for (Value::use_const_iterator UI = PN->use_begin(), E = PN->use_end(); |
| 138 | UI != E; ++UI) { |
| 139 | const Instruction *User = cast<Instruction>(*UI); |
| 140 | if (User->getParent() != DestBB || !isa<PHINode>(User)) |
| 141 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 142 | // If User is inside DestBB block and it is a PHINode then check |
| 143 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | 75abc1e | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 144 | // a complex condition (e.g. preheaders) we want to avoid here. |
| 145 | if (User->getParent() == DestBB) { |
| 146 | if (const PHINode *UPN = dyn_cast<PHINode>(User)) |
| 147 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 148 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 149 | if (Insn && Insn->getParent() == BB && |
| 150 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 151 | return false; |
| 152 | } |
| 153 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 156 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 157 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 158 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 159 | // can't merge the block. |
| 160 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 161 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 162 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 163 | // Collect the preds of BB. |
Chris Lattner | f67f73a | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 164 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 165 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 166 | // It is faster to get preds from a PHI than with pred_iterator. |
| 167 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 168 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 169 | } else { |
| 170 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 171 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 172 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 173 | // Walk the preds of DestBB. |
| 174 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 175 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 176 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 177 | BBI = DestBB->begin(); |
| 178 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 179 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 180 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 181 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 182 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 183 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 184 | if (V2PN->getParent() == BB) |
| 185 | V2 = V2PN->getIncomingValueForBlock(Pred); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 186 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 187 | // If there is a conflict, bail out. |
| 188 | if (V1 != V2) return false; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and |
| 198 | /// an unconditional branch in it. |
| 199 | void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) { |
| 200 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 201 | BasicBlock *DestBB = BI->getSuccessor(0); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 202 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 203 | DOUT << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 204 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 205 | // If the destination block has a single pred, then this is a trivial edge, |
| 206 | // just collapse it. |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 207 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { |
| 208 | // Remember if SinglePred was the entry block of the function. If so, we |
| 209 | // will need to move BB back to the entry position. |
| 210 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Chris Lattner | 3c4f8b9 | 2008-11-27 07:54:12 +0000 | [diff] [blame] | 211 | MergeBasicBlockIntoOnlyPred(DestBB); |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 212 | |
| 213 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 214 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
| 215 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 216 | DOUT << "AFTER:\n" << *DestBB << "\n\n\n"; |
| 217 | return; |
| 218 | } |
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 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 221 | // to handle the new incoming edges it is about to have. |
| 222 | PHINode *PN; |
| 223 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 224 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 225 | // Remove the incoming value for BB, and remember it. |
| 226 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 227 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 228 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 229 | // value that dominates BB. |
| 230 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 231 | if (InValPhi && InValPhi->getParent() == BB) { |
| 232 | // Add all of the input values of the input PHI as inputs of this phi. |
| 233 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 234 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 235 | InValPhi->getIncomingBlock(i)); |
| 236 | } else { |
| 237 | // Otherwise, add one instance of the dominating value for each edge that |
| 238 | // we will be adding. |
| 239 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 240 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 241 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 242 | } else { |
| 243 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 244 | PN->addIncoming(InVal, *PI); |
| 245 | } |
| 246 | } |
| 247 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 248 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 249 | // The PHIs are now updated, change everything that refers to BB to use |
| 250 | // DestBB and remove BB. |
| 251 | BB->replaceAllUsesWith(DestBB); |
| 252 | BB->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 253 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 254 | DOUT << "AFTER:\n" << *DestBB << "\n\n\n"; |
| 255 | } |
| 256 | |
| 257 | |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 258 | /// SplitEdgeNicely - Split the critical edge from TI to its specified |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 259 | /// successor if it will improve codegen. We only do this if the successor has |
| 260 | /// phi nodes (otherwise critical edges are ok). If there is already another |
| 261 | /// predecessor of the succ that is empty (and thus has no phi nodes), use it |
| 262 | /// instead of introducing a new block. |
| 263 | static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum, Pass *P) { |
| 264 | BasicBlock *TIBB = TI->getParent(); |
| 265 | BasicBlock *Dest = TI->getSuccessor(SuccNum); |
| 266 | assert(isa<PHINode>(Dest->begin()) && |
| 267 | "This should only be called if Dest has a PHI!"); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 268 | |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 269 | // As a hack, never split backedges of loops. Even though the copy for any |
| 270 | // PHIs inserted on the backedge would be dead for exits from the loop, we |
| 271 | // assume that the cost of *splitting* the backedge would be too high. |
Chris Lattner | ff26ab2 | 2007-12-25 19:06:45 +0000 | [diff] [blame] | 272 | if (Dest == TIBB) |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 273 | return; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 274 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 275 | /// TIPHIValues - This array is lazily computed to determine the values of |
| 276 | /// PHIs in Dest that TI would provide. |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 277 | SmallVector<Value*, 32> TIPHIValues; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 278 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 279 | // Check to see if Dest has any blocks that can be used as a split edge for |
| 280 | // this terminator. |
| 281 | for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) { |
| 282 | BasicBlock *Pred = *PI; |
| 283 | // To be usable, the pred has to end with an uncond branch to the dest. |
| 284 | BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator()); |
| 285 | if (!PredBr || !PredBr->isUnconditional() || |
| 286 | // Must be empty other than the branch. |
Dale Johannesen | 6603a1b | 2007-05-08 01:01:04 +0000 | [diff] [blame] | 287 | &Pred->front() != PredBr || |
| 288 | // Cannot be the entry block; its label does not get emitted. |
| 289 | Pred == &(Dest->getParent()->getEntryBlock())) |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 290 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 291 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 292 | // Finally, since we know that Dest has phi nodes in it, we have to make |
| 293 | // sure that jumping to Pred will have the same affect as going to Dest in |
| 294 | // terms of PHI values. |
| 295 | PHINode *PN; |
| 296 | unsigned PHINo = 0; |
| 297 | bool FoundMatch = true; |
| 298 | for (BasicBlock::iterator I = Dest->begin(); |
| 299 | (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) { |
| 300 | if (PHINo == TIPHIValues.size()) |
| 301 | TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB)); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 302 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 303 | // If the PHI entry doesn't work, we can't use this pred. |
| 304 | if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) { |
| 305 | FoundMatch = false; |
| 306 | break; |
| 307 | } |
| 308 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 309 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 310 | // If we found a workable predecessor, change TI to branch to Succ. |
| 311 | if (FoundMatch) { |
| 312 | Dest->removePredecessor(TIBB); |
| 313 | TI->setSuccessor(SuccNum, Pred); |
| 314 | return; |
| 315 | } |
| 316 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 317 | |
| 318 | SplitCriticalEdge(TI, SuccNum, P, true); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 321 | /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop |
| 322 | /// copy (e.g. it's casting from one pointer type to another, int->uint, or |
| 323 | /// 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] | 324 | /// registers that must be created and coalesced. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 325 | /// |
| 326 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 327 | /// |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 328 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){ |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 329 | // If this is a noop copy, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 330 | MVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 331 | MVT DstVT = TLI.getValueType(CI->getType()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 332 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 333 | // This is an fp<->int conversion? |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 334 | if (SrcVT.isInteger() != DstVT.isInteger()) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 335 | return false; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 336 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 337 | // If this is an extension, it will be a zero or sign extension, which |
| 338 | // isn't a noop. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 339 | if (SrcVT.bitsLT(DstVT)) return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 340 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 341 | // If these values will be promoted, find out what they will be promoted |
| 342 | // to. This helps us consider truncates on PPC as noop copies when they |
| 343 | // are. |
| 344 | if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote) |
| 345 | SrcVT = TLI.getTypeToTransformTo(SrcVT); |
| 346 | if (TLI.getTypeAction(DstVT) == TargetLowering::Promote) |
| 347 | DstVT = TLI.getTypeToTransformTo(DstVT); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 348 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 349 | // If, after promotion, these are the same types, this is a noop copy. |
| 350 | if (SrcVT != DstVT) |
| 351 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 352 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 353 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 354 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 355 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 356 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 357 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 358 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 359 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 360 | UI != E; ) { |
| 361 | Use &TheUse = UI.getUse(); |
| 362 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 363 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 364 | // Figure out which BB this cast is used in. For PHI's this is the |
| 365 | // appropriate predecessor block. |
| 366 | BasicBlock *UserBB = User->getParent(); |
| 367 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 368 | unsigned OpVal = UI.getOperandNo()/2; |
| 369 | UserBB = PN->getIncomingBlock(OpVal); |
| 370 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 371 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 372 | // Preincrement use iterator so we don't invalidate it. |
| 373 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 374 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 375 | // If this user is in the same block as the cast, don't change the cast. |
| 376 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 377 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 378 | // If we have already inserted a cast into this block, use it. |
| 379 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 380 | |
| 381 | if (!InsertedCast) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 382 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 383 | |
| 384 | InsertedCast = |
| 385 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 386 | InsertPt); |
| 387 | MadeChange = true; |
| 388 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 389 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 390 | // 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] | 391 | TheUse = InsertedCast; |
| 392 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 393 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 394 | // If we removed all uses, nuke the cast. |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 395 | if (CI->use_empty()) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 396 | CI->eraseFromParent(); |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 397 | MadeChange = true; |
| 398 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 399 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 400 | return MadeChange; |
| 401 | } |
| 402 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 403 | /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 404 | /// 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] | 405 | /// a clear win except on targets with multiple condition code registers |
| 406 | /// (PowerPC), where it might lose; some adjustment may be wanted there. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 407 | /// |
| 408 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 409 | static bool OptimizeCmpExpression(CmpInst *CI) { |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 410 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 411 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 412 | /// InsertedCmp - Only insert a cmp in each block once. |
| 413 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 414 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 415 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 416 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 417 | UI != E; ) { |
| 418 | Use &TheUse = UI.getUse(); |
| 419 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 420 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 421 | // Preincrement use iterator so we don't invalidate it. |
| 422 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 423 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 424 | // Don't bother for PHI nodes. |
| 425 | if (isa<PHINode>(User)) |
| 426 | continue; |
| 427 | |
| 428 | // Figure out which BB this cmp is used in. |
| 429 | BasicBlock *UserBB = User->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 430 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 431 | // If this user is in the same block as the cmp, don't change the cmp. |
| 432 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 433 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 434 | // If we have already inserted a cmp into this block, use it. |
| 435 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 436 | |
| 437 | if (!InsertedCmp) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 438 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 439 | |
| 440 | InsertedCmp = |
| 441 | CmpInst::Create(CI->getOpcode(), CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 442 | CI->getOperand(1), "", InsertPt); |
| 443 | MadeChange = true; |
| 444 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 445 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 446 | // Replace a use of the cmp with a use of the new cmp. |
| 447 | TheUse = InsertedCmp; |
| 448 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 449 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 450 | // If we removed all uses, nuke the cmp. |
| 451 | if (CI->use_empty()) |
| 452 | CI->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 453 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 454 | return MadeChange; |
| 455 | } |
| 456 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 457 | //===----------------------------------------------------------------------===// |
| 458 | // Addressing Mode Analysis and Optimization |
| 459 | //===----------------------------------------------------------------------===// |
| 460 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 461 | namespace { |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 462 | /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode |
| 463 | /// which holds actual Value*'s for register values. |
| 464 | struct ExtAddrMode : public TargetLowering::AddrMode { |
| 465 | Value *BaseReg; |
| 466 | Value *ScaledReg; |
| 467 | ExtAddrMode() : BaseReg(0), ScaledReg(0) {} |
| 468 | void print(OStream &OS) const; |
| 469 | void dump() const { |
| 470 | print(cerr); |
| 471 | cerr << '\n'; |
| 472 | } |
| 473 | }; |
| 474 | } // end anonymous namespace |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 476 | static inline OStream &operator<<(OStream &OS, const ExtAddrMode &AM) { |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 477 | AM.print(OS); |
| 478 | return OS; |
| 479 | } |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 481 | void ExtAddrMode::print(OStream &OS) const { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 482 | bool NeedPlus = false; |
| 483 | OS << "["; |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 484 | if (BaseGV) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 485 | OS << (NeedPlus ? " + " : "") |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 486 | << "GV:%" << BaseGV->getName(), NeedPlus = true; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 488 | if (BaseOffs) |
| 489 | OS << (NeedPlus ? " + " : "") << BaseOffs, NeedPlus = true; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 491 | if (BaseReg) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 492 | OS << (NeedPlus ? " + " : "") |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 493 | << "Base:%" << BaseReg->getName(), NeedPlus = true; |
| 494 | if (Scale) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 495 | OS << (NeedPlus ? " + " : "") |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 496 | << Scale << "*%" << ScaledReg->getName(), NeedPlus = true; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 498 | OS << ']'; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 501 | namespace { |
| 502 | /// AddressingModeMatcher - This class exposes a single public method, which is |
| 503 | /// used to construct a "maximal munch" of the addressing mode for the target |
| 504 | /// specified by TLI for an access to "V" with an access type of AccessTy. This |
| 505 | /// returns the addressing mode that is actually matched by value, but also |
| 506 | /// returns the list of instructions involved in that addressing computation in |
| 507 | /// AddrModeInsts. |
| 508 | class AddressingModeMatcher { |
| 509 | SmallVectorImpl<Instruction*> &AddrModeInsts; |
| 510 | const TargetLowering &TLI; |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 511 | |
| 512 | /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and |
| 513 | /// the memory instruction that we're computing this address for. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 514 | const Type *AccessTy; |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 515 | Instruction *MemoryInst; |
| 516 | |
| 517 | /// AddrMode - This is the addressing mode that we're building up. This is |
| 518 | /// part of the return value of this addressing mode matching stuff. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 519 | ExtAddrMode &AddrMode; |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 520 | |
| 521 | /// IgnoreProfitability - This is set to true when we should not do |
| 522 | /// profitability checks. When true, IsProfitableToFoldIntoAddressingMode |
| 523 | /// always returns true. |
| 524 | bool IgnoreProfitability; |
| 525 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 526 | AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI, |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 527 | const TargetLowering &T, const Type *AT, |
| 528 | Instruction *MI, ExtAddrMode &AM) |
| 529 | : AddrModeInsts(AMI), TLI(T), AccessTy(AT), MemoryInst(MI), AddrMode(AM) { |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 530 | IgnoreProfitability = false; |
| 531 | } |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 532 | public: |
| 533 | |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 534 | /// Match - Find the maximal addressing mode that a load/store of V can fold, |
| 535 | /// give an access type of AccessTy. This returns a list of involved |
| 536 | /// instructions in AddrModeInsts. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 537 | static ExtAddrMode Match(Value *V, const Type *AccessTy, |
| 538 | Instruction *MemoryInst, |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 539 | SmallVectorImpl<Instruction*> &AddrModeInsts, |
| 540 | const TargetLowering &TLI) { |
| 541 | ExtAddrMode Result; |
| 542 | |
| 543 | bool Success = |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 544 | AddressingModeMatcher(AddrModeInsts, TLI, AccessTy, |
| 545 | MemoryInst, Result).MatchAddr(V, 0); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 546 | Success = Success; assert(Success && "Couldn't select *anything*?"); |
| 547 | return Result; |
| 548 | } |
| 549 | private: |
Chris Lattner | 3b48501 | 2008-11-25 07:25:26 +0000 | [diff] [blame] | 550 | bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 551 | bool MatchAddr(Value *V, unsigned Depth); |
| 552 | bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth); |
Chris Lattner | 84d1b40 | 2008-11-26 03:02:41 +0000 | [diff] [blame] | 553 | bool IsProfitableToFoldIntoAddressingMode(Instruction *I, |
| 554 | ExtAddrMode &AMBefore, |
| 555 | ExtAddrMode &AMAfter); |
| 556 | bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 557 | }; |
| 558 | } // end anonymous namespace |
| 559 | |
| 560 | /// MatchScaledValue - Try adding ScaleReg*Scale to the current addressing mode. |
| 561 | /// Return true and update AddrMode if this addr mode is legal for the target, |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 562 | /// false if not. |
Chris Lattner | 3b48501 | 2008-11-25 07:25:26 +0000 | [diff] [blame] | 563 | bool AddressingModeMatcher::MatchScaledValue(Value *ScaleReg, int64_t Scale, |
| 564 | unsigned Depth) { |
| 565 | // If Scale is 1, then this is the same as adding ScaleReg to the addressing |
| 566 | // mode. Just process that directly. |
| 567 | if (Scale == 1) |
| 568 | return MatchAddr(ScaleReg, Depth); |
| 569 | |
| 570 | // If the scale is 0, it takes nothing to add this. |
| 571 | if (Scale == 0) |
| 572 | return true; |
| 573 | |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 574 | // If we already have a scale of this value, we can add to it, otherwise, we |
| 575 | // need an available scale field. |
| 576 | if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg) |
| 577 | return false; |
| 578 | |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 579 | ExtAddrMode TestAddrMode = AddrMode; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 580 | |
| 581 | // Add scale to turn X*4+X*3 -> X*7. This could also do things like |
| 582 | // [A+B + A*7] -> [B+A*8]. |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 583 | TestAddrMode.Scale += Scale; |
| 584 | TestAddrMode.ScaledReg = ScaleReg; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 585 | |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 586 | // If the new address isn't legal, bail out. |
| 587 | if (!TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) |
| 588 | return false; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 590 | // It was legal, so commit it. |
| 591 | AddrMode = TestAddrMode; |
| 592 | |
| 593 | // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now |
| 594 | // to see if ScaleReg is actually X+C. If so, we can turn this into adding |
| 595 | // X*Scale + C*Scale to addr mode. |
| 596 | ConstantInt *CI; Value *AddLHS; |
| 597 | if (match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) { |
| 598 | TestAddrMode.ScaledReg = AddLHS; |
| 599 | TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale; |
| 600 | |
| 601 | // If this addressing mode is legal, commit it and remember that we folded |
| 602 | // this instruction. |
| 603 | if (TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) { |
| 604 | AddrModeInsts.push_back(cast<Instruction>(ScaleReg)); |
| 605 | AddrMode = TestAddrMode; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 606 | return true; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 607 | } |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 610 | // Otherwise, not (x+c)*scale, just return what we have. |
| 611 | return true; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 614 | /// MightBeFoldableInst - This is a little filter, which returns true if an |
| 615 | /// addressing computation involving I might be folded into a load/store |
| 616 | /// accessing it. This doesn't need to be perfect, but needs to accept at least |
| 617 | /// the set of instructions that MatchOperationAddr can. |
| 618 | static bool MightBeFoldableInst(Instruction *I) { |
| 619 | switch (I->getOpcode()) { |
| 620 | case Instruction::BitCast: |
| 621 | // Don't touch identity bitcasts. |
| 622 | if (I->getType() == I->getOperand(0)->getType()) |
| 623 | return false; |
| 624 | return isa<PointerType>(I->getType()) || isa<IntegerType>(I->getType()); |
| 625 | case Instruction::PtrToInt: |
| 626 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
| 627 | return true; |
| 628 | case Instruction::IntToPtr: |
| 629 | // We know the input is intptr_t, so this is foldable. |
| 630 | return true; |
| 631 | case Instruction::Add: |
| 632 | return true; |
| 633 | case Instruction::Mul: |
| 634 | case Instruction::Shl: |
| 635 | // Can only handle X*C and X << C. |
| 636 | return isa<ConstantInt>(I->getOperand(1)); |
| 637 | case Instruction::GetElementPtr: |
| 638 | return true; |
| 639 | default: |
| 640 | return false; |
| 641 | } |
| 642 | } |
| 643 | |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 644 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 645 | /// MatchOperationAddr - Given an instruction or constant expr, see if we can |
| 646 | /// fold the operation into the addressing mode. If so, update the addressing |
| 647 | /// mode and return true, otherwise return false without modifying AddrMode. |
| 648 | bool AddressingModeMatcher::MatchOperationAddr(User *AddrInst, unsigned Opcode, |
| 649 | unsigned Depth) { |
| 650 | // Avoid exponential behavior on extremely deep expression trees. |
| 651 | if (Depth >= 5) return false; |
| 652 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 653 | switch (Opcode) { |
| 654 | case Instruction::PtrToInt: |
| 655 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 656 | return MatchAddr(AddrInst->getOperand(0), Depth); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 657 | case Instruction::IntToPtr: |
| 658 | // This inttoptr is a no-op if the integer type is pointer sized. |
| 659 | if (TLI.getValueType(AddrInst->getOperand(0)->getType()) == |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 660 | TLI.getPointerTy()) |
| 661 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 662 | return false; |
Chris Lattner | 2efbbb3 | 2008-11-26 00:26:16 +0000 | [diff] [blame] | 663 | case Instruction::BitCast: |
| 664 | // BitCast is always a noop, and we can handle it as long as it is |
| 665 | // int->int or pointer->pointer (we don't want int<->fp or something). |
| 666 | if ((isa<PointerType>(AddrInst->getOperand(0)->getType()) || |
| 667 | isa<IntegerType>(AddrInst->getOperand(0)->getType())) && |
| 668 | // Don't touch identity bitcasts. These were probably put here by LSR, |
| 669 | // and we don't want to mess around with them. Assume it knows what it |
| 670 | // is doing. |
| 671 | AddrInst->getOperand(0)->getType() != AddrInst->getType()) |
| 672 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 673 | return false; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 674 | case Instruction::Add: { |
| 675 | // Check to see if we can merge in the RHS then the LHS. If so, we win. |
| 676 | ExtAddrMode BackupAddrMode = AddrMode; |
| 677 | unsigned OldSize = AddrModeInsts.size(); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 678 | if (MatchAddr(AddrInst->getOperand(1), Depth+1) && |
| 679 | MatchAddr(AddrInst->getOperand(0), Depth+1)) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 680 | return true; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 681 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 682 | // Restore the old addr mode info. |
| 683 | AddrMode = BackupAddrMode; |
| 684 | AddrModeInsts.resize(OldSize); |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 685 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 686 | // Otherwise this was over-aggressive. Try merging in the LHS then the RHS. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 687 | if (MatchAddr(AddrInst->getOperand(0), Depth+1) && |
| 688 | MatchAddr(AddrInst->getOperand(1), Depth+1)) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 689 | return true; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 690 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 691 | // Otherwise we definitely can't merge the ADD in. |
| 692 | AddrMode = BackupAddrMode; |
| 693 | AddrModeInsts.resize(OldSize); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 694 | break; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 695 | } |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 696 | //case Instruction::Or: |
| 697 | // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD. |
| 698 | //break; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 699 | case Instruction::Mul: |
| 700 | case Instruction::Shl: { |
Chris Lattner | 7ad1c73 | 2008-11-25 04:47:41 +0000 | [diff] [blame] | 701 | // Can only handle X*C and X << C. |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 702 | ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1)); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 703 | if (!RHS) return false; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 704 | int64_t Scale = RHS->getSExtValue(); |
| 705 | if (Opcode == Instruction::Shl) |
| 706 | Scale = 1 << Scale; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 707 | |
Chris Lattner | 3b48501 | 2008-11-25 07:25:26 +0000 | [diff] [blame] | 708 | return MatchScaledValue(AddrInst->getOperand(0), Scale, Depth); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 709 | } |
| 710 | case Instruction::GetElementPtr: { |
| 711 | // Scan the GEP. We check it if it contains constant offsets and at most |
| 712 | // one variable offset. |
| 713 | int VariableOperand = -1; |
| 714 | unsigned VariableScale = 0; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 715 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 716 | int64_t ConstantOffset = 0; |
| 717 | const TargetData *TD = TLI.getTargetData(); |
| 718 | gep_type_iterator GTI = gep_type_begin(AddrInst); |
| 719 | for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) { |
| 720 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 721 | const StructLayout *SL = TD->getStructLayout(STy); |
| 722 | unsigned Idx = |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 723 | cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue(); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 724 | ConstantOffset += SL->getElementOffset(Idx); |
| 725 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 726 | uint64_t TypeSize = TD->getABITypeSize(GTI.getIndexedType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 727 | if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) { |
| 728 | ConstantOffset += CI->getSExtValue()*TypeSize; |
| 729 | } else if (TypeSize) { // Scales of zero don't do anything. |
| 730 | // We only allow one variable index at the moment. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 731 | if (VariableOperand != -1) |
| 732 | return false; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 733 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 734 | // Remember the variable index. |
| 735 | VariableOperand = i; |
| 736 | VariableScale = TypeSize; |
| 737 | } |
| 738 | } |
| 739 | } |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 740 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 741 | // A common case is for the GEP to only do a constant offset. In this case, |
| 742 | // just add it to the disp field and check validity. |
| 743 | if (VariableOperand == -1) { |
| 744 | AddrMode.BaseOffs += ConstantOffset; |
| 745 | if (ConstantOffset == 0 || TLI.isLegalAddressingMode(AddrMode, AccessTy)){ |
| 746 | // Check to see if we can fold the base pointer in too. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 747 | if (MatchAddr(AddrInst->getOperand(0), Depth+1)) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 748 | return true; |
| 749 | } |
| 750 | AddrMode.BaseOffs -= ConstantOffset; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 751 | return false; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 752 | } |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 753 | |
| 754 | // Save the valid addressing mode in case we can't match. |
| 755 | ExtAddrMode BackupAddrMode = AddrMode; |
| 756 | |
| 757 | // Check that this has no base reg yet. If so, we won't have a place to |
| 758 | // put the base of the GEP (assuming it is not a null ptr). |
| 759 | bool SetBaseReg = true; |
| 760 | if (isa<ConstantPointerNull>(AddrInst->getOperand(0))) |
| 761 | SetBaseReg = false; // null pointer base doesn't need representation. |
| 762 | else if (AddrMode.HasBaseReg) |
| 763 | return false; // Base register already specified, can't match GEP. |
| 764 | else { |
| 765 | // Otherwise, we'll use the GEP base as the BaseReg. |
| 766 | AddrMode.HasBaseReg = true; |
| 767 | AddrMode.BaseReg = AddrInst->getOperand(0); |
| 768 | } |
| 769 | |
| 770 | // See if the scale and offset amount is valid for this target. |
| 771 | AddrMode.BaseOffs += ConstantOffset; |
| 772 | |
Chris Lattner | 3b48501 | 2008-11-25 07:25:26 +0000 | [diff] [blame] | 773 | if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale, |
| 774 | Depth)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 775 | AddrMode = BackupAddrMode; |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | // If we have a null as the base of the GEP, folding in the constant offset |
| 780 | // plus variable scale is all we can do. |
| 781 | if (!SetBaseReg) return true; |
| 782 | |
| 783 | // If this match succeeded, we know that we can form an address with the |
| 784 | // GepBase as the basereg. Match the base pointer of the GEP more |
| 785 | // aggressively by zeroing out BaseReg and rematching. If the base is |
| 786 | // (for example) another GEP, this allows merging in that other GEP into |
| 787 | // the addressing mode we're forming. |
| 788 | AddrMode.HasBaseReg = false; |
| 789 | AddrMode.BaseReg = 0; |
| 790 | bool Success = MatchAddr(AddrInst->getOperand(0), Depth+1); |
| 791 | assert(Success && "MatchAddr should be able to fill in BaseReg!"); |
| 792 | Success=Success; |
| 793 | return true; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 794 | } |
| 795 | } |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 796 | return false; |
| 797 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 798 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 799 | /// MatchAddr - If we can, try to add the value of 'Addr' into the current |
| 800 | /// addressing mode. If Addr can't be added to AddrMode this returns false and |
| 801 | /// leaves AddrMode unmodified. This assumes that Addr is either a pointer type |
| 802 | /// or intptr_t for the target. |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 803 | /// |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 804 | bool AddressingModeMatcher::MatchAddr(Value *Addr, unsigned Depth) { |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 805 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) { |
| 806 | // Fold in immediates if legal for the target. |
| 807 | AddrMode.BaseOffs += CI->getSExtValue(); |
| 808 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 809 | return true; |
| 810 | AddrMode.BaseOffs -= CI->getSExtValue(); |
| 811 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 812 | // If this is a global variable, try to fold it into the addressing mode. |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 813 | if (AddrMode.BaseGV == 0) { |
| 814 | AddrMode.BaseGV = GV; |
| 815 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 816 | return true; |
| 817 | AddrMode.BaseGV = 0; |
| 818 | } |
| 819 | } else if (Instruction *I = dyn_cast<Instruction>(Addr)) { |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 820 | ExtAddrMode BackupAddrMode = AddrMode; |
| 821 | unsigned OldSize = AddrModeInsts.size(); |
| 822 | |
| 823 | // Check to see if it is possible to fold this operation. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 824 | if (MatchOperationAddr(I, I->getOpcode(), Depth)) { |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 825 | // Okay, it's possible to fold this. Check to see if it is actually |
| 826 | // *profitable* to do so. We use a simple cost model to avoid increasing |
| 827 | // register pressure too much. |
Chris Lattner | 84d1b40 | 2008-11-26 03:02:41 +0000 | [diff] [blame] | 828 | if (I->hasOneUse() || |
| 829 | IsProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) { |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 830 | AddrModeInsts.push_back(I); |
| 831 | return true; |
| 832 | } |
| 833 | |
| 834 | // It isn't profitable to do this, roll back. |
| 835 | //cerr << "NOT FOLDING: " << *I; |
| 836 | AddrMode = BackupAddrMode; |
| 837 | AddrModeInsts.resize(OldSize); |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 838 | } |
| 839 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 840 | if (MatchOperationAddr(CE, CE->getOpcode(), Depth)) |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 841 | return true; |
| 842 | } else if (isa<ConstantPointerNull>(Addr)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 843 | // Null pointer gets folded without affecting the addressing mode. |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 844 | return true; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 845 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 846 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 847 | // Worse case, the target should support [reg] addressing modes. :) |
| 848 | if (!AddrMode.HasBaseReg) { |
| 849 | AddrMode.HasBaseReg = true; |
Chris Lattner | 653b258 | 2008-11-26 02:11:11 +0000 | [diff] [blame] | 850 | AddrMode.BaseReg = Addr; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 851 | // Still check for legality in case the target supports [imm] but not [i+r]. |
Chris Lattner | 653b258 | 2008-11-26 02:11:11 +0000 | [diff] [blame] | 852 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 853 | return true; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 854 | AddrMode.HasBaseReg = false; |
Chris Lattner | 653b258 | 2008-11-26 02:11:11 +0000 | [diff] [blame] | 855 | AddrMode.BaseReg = 0; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 856 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 857 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 858 | // If the base register is already taken, see if we can do [r+r]. |
| 859 | if (AddrMode.Scale == 0) { |
| 860 | AddrMode.Scale = 1; |
Chris Lattner | 653b258 | 2008-11-26 02:11:11 +0000 | [diff] [blame] | 861 | AddrMode.ScaledReg = Addr; |
| 862 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 863 | return true; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 864 | AddrMode.Scale = 0; |
Chris Lattner | 653b258 | 2008-11-26 02:11:11 +0000 | [diff] [blame] | 865 | AddrMode.ScaledReg = 0; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 866 | } |
| 867 | // Couldn't match. |
| 868 | return false; |
| 869 | } |
| 870 | |
Chris Lattner | 695d8ec | 2008-11-26 04:59:11 +0000 | [diff] [blame] | 871 | |
| 872 | /// IsOperandAMemoryOperand - Check to see if all uses of OpVal by the specified |
| 873 | /// inline asm call are due to memory operands. If so, return true, otherwise |
| 874 | /// return false. |
| 875 | static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal, |
| 876 | const TargetLowering &TLI) { |
| 877 | std::vector<InlineAsm::ConstraintInfo> |
| 878 | Constraints = IA->ParseConstraints(); |
| 879 | |
| 880 | unsigned ArgNo = 1; // ArgNo - The operand of the CallInst. |
| 881 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
| 882 | TargetLowering::AsmOperandInfo OpInfo(Constraints[i]); |
| 883 | |
| 884 | // Compute the value type for each operand. |
| 885 | switch (OpInfo.Type) { |
| 886 | case InlineAsm::isOutput: |
| 887 | if (OpInfo.isIndirect) |
| 888 | OpInfo.CallOperandVal = CI->getOperand(ArgNo++); |
| 889 | break; |
| 890 | case InlineAsm::isInput: |
| 891 | OpInfo.CallOperandVal = CI->getOperand(ArgNo++); |
| 892 | break; |
| 893 | case InlineAsm::isClobber: |
| 894 | // Nothing to do. |
| 895 | break; |
| 896 | } |
| 897 | |
| 898 | // Compute the constraint code and ConstraintType to use. |
| 899 | TLI.ComputeConstraintToUse(OpInfo, SDValue(), |
| 900 | OpInfo.ConstraintType == TargetLowering::C_Memory); |
| 901 | |
| 902 | // If this asm operand is our Value*, and if it isn't an indirect memory |
| 903 | // operand, we can't fold it! |
| 904 | if (OpInfo.CallOperandVal == OpVal && |
| 905 | (OpInfo.ConstraintType != TargetLowering::C_Memory || |
| 906 | !OpInfo.isIndirect)) |
| 907 | return false; |
| 908 | } |
| 909 | |
| 910 | return true; |
| 911 | } |
| 912 | |
| 913 | |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 914 | /// FindAllMemoryUses - Recursively walk all the uses of I until we find a |
| 915 | /// memory use. If we find an obviously non-foldable instruction, return true. |
| 916 | /// Add the ultimately found memory instructions to MemoryUses. |
| 917 | static bool FindAllMemoryUses(Instruction *I, |
| 918 | SmallVectorImpl<std::pair<Instruction*,unsigned> > &MemoryUses, |
Chris Lattner | 695d8ec | 2008-11-26 04:59:11 +0000 | [diff] [blame] | 919 | SmallPtrSet<Instruction*, 16> &ConsideredInsts, |
| 920 | const TargetLowering &TLI) { |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 921 | // If we already considered this instruction, we're done. |
| 922 | if (!ConsideredInsts.insert(I)) |
| 923 | return false; |
| 924 | |
| 925 | // If this is an obviously unfoldable instruction, bail out. |
| 926 | if (!MightBeFoldableInst(I)) |
| 927 | return true; |
| 928 | |
| 929 | // Loop over all the uses, recursively processing them. |
| 930 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 931 | UI != E; ++UI) { |
| 932 | if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
| 933 | MemoryUses.push_back(std::make_pair(LI, UI.getOperandNo())); |
| 934 | continue; |
| 935 | } |
| 936 | |
| 937 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 938 | if (UI.getOperandNo() == 0) return true; // Storing addr, not into addr. |
| 939 | MemoryUses.push_back(std::make_pair(SI, UI.getOperandNo())); |
| 940 | continue; |
| 941 | } |
| 942 | |
| 943 | if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 944 | InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue()); |
| 945 | if (IA == 0) return true; |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 695d8ec | 2008-11-26 04:59:11 +0000 | [diff] [blame] | 947 | // If this is a memory operand, we're cool, otherwise bail out. |
| 948 | if (!IsOperandAMemoryOperand(CI, IA, I, TLI)) |
| 949 | return true; |
| 950 | continue; |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Chris Lattner | 695d8ec | 2008-11-26 04:59:11 +0000 | [diff] [blame] | 953 | if (FindAllMemoryUses(cast<Instruction>(*UI), MemoryUses, ConsideredInsts, |
| 954 | TLI)) |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 955 | return true; |
| 956 | } |
| 957 | |
| 958 | return false; |
| 959 | } |
Chris Lattner | 84d1b40 | 2008-11-26 03:02:41 +0000 | [diff] [blame] | 960 | |
| 961 | |
| 962 | /// ValueAlreadyLiveAtInst - Retrn true if Val is already known to be live at |
| 963 | /// the use site that we're folding it into. If so, there is no cost to |
| 964 | /// include it in the addressing mode. KnownLive1 and KnownLive2 are two values |
| 965 | /// that we know are live at the instruction already. |
| 966 | bool AddressingModeMatcher::ValueAlreadyLiveAtInst(Value *Val,Value *KnownLive1, |
| 967 | Value *KnownLive2) { |
| 968 | // If Val is either of the known-live values, we know it is live! |
| 969 | if (Val == 0 || Val == KnownLive1 || Val == KnownLive2) |
| 970 | return true; |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 971 | |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 972 | // All values other than instructions and arguments (e.g. constants) are live. |
Chris Lattner | 84d1b40 | 2008-11-26 03:02:41 +0000 | [diff] [blame] | 973 | if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true; |
| 974 | |
| 975 | // If Val is a constant sized alloca in the entry block, it is live, this is |
| 976 | // true because it is just a reference to the stack/frame pointer, which is |
| 977 | // live for the whole function. |
| 978 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Val)) |
| 979 | if (AI->isStaticAlloca()) |
| 980 | return true; |
| 981 | |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 982 | // Check to see if this value is already used in the memory instruction's |
| 983 | // block. If so, it's already live into the block at the very least, so we |
| 984 | // can reasonably fold it. |
| 985 | BasicBlock *MemBB = MemoryInst->getParent(); |
| 986 | for (Value::use_iterator UI = Val->use_begin(), E = Val->use_end(); |
| 987 | UI != E; ++UI) |
| 988 | // We know that uses of arguments and instructions have to be instructions. |
| 989 | if (cast<Instruction>(*UI)->getParent() == MemBB) |
| 990 | return true; |
| 991 | |
Chris Lattner | 84d1b40 | 2008-11-26 03:02:41 +0000 | [diff] [blame] | 992 | return false; |
| 993 | } |
| 994 | |
| 995 | |
| 996 | |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 997 | /// IsProfitableToFoldIntoAddressingMode - It is possible for the addressing |
| 998 | /// mode of the machine to fold the specified instruction into a load or store |
| 999 | /// that ultimately uses it. However, the specified instruction has multiple |
| 1000 | /// uses. Given this, it may actually increase register pressure to fold it |
| 1001 | /// into the load. For example, consider this code: |
| 1002 | /// |
| 1003 | /// X = ... |
| 1004 | /// Y = X+1 |
| 1005 | /// use(Y) -> nonload/store |
| 1006 | /// Z = Y+1 |
| 1007 | /// load Z |
| 1008 | /// |
| 1009 | /// In this case, Y has multiple uses, and can be folded into the load of Z |
| 1010 | /// (yielding load [X+2]). However, doing this will cause both "X" and "X+1" to |
| 1011 | /// be live at the use(Y) line. If we don't fold Y into load Z, we use one |
| 1012 | /// fewer register. Since Y can't be folded into "use(Y)" we don't increase the |
| 1013 | /// number of computations either. |
| 1014 | /// |
| 1015 | /// Note that this (like most of CodeGenPrepare) is just a rough heuristic. If |
| 1016 | /// X was live across 'load Z' for other reasons, we actually *would* want to |
Chris Lattner | 653b258 | 2008-11-26 02:11:11 +0000 | [diff] [blame] | 1017 | /// fold the addressing mode in the Z case. This would make Y die earlier. |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 1018 | bool AddressingModeMatcher:: |
Chris Lattner | 84d1b40 | 2008-11-26 03:02:41 +0000 | [diff] [blame] | 1019 | IsProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore, |
| 1020 | ExtAddrMode &AMAfter) { |
Chris Lattner | ab8b794 | 2008-11-26 22:16:44 +0000 | [diff] [blame] | 1021 | if (IgnoreProfitability) return true; |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 84d1b40 | 2008-11-26 03:02:41 +0000 | [diff] [blame] | 1023 | // AMBefore is the addressing mode before this instruction was folded into it, |
| 1024 | // and AMAfter is the addressing mode after the instruction was folded. Get |
| 1025 | // the set of registers referenced by AMAfter and subtract out those |
| 1026 | // referenced by AMBefore: this is the set of values which folding in this |
| 1027 | // address extends the lifetime of. |
| 1028 | // |
| 1029 | // Note that there are only two potential values being referenced here, |
| 1030 | // BaseReg and ScaleReg (global addresses are always available, as are any |
| 1031 | // folded immediates). |
| 1032 | Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg; |
| 1033 | |
| 1034 | // If the BaseReg or ScaledReg was referenced by the previous addrmode, their |
| 1035 | // lifetime wasn't extended by adding this instruction. |
| 1036 | if (ValueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg)) |
| 1037 | BaseReg = 0; |
| 1038 | if (ValueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg)) |
| 1039 | ScaledReg = 0; |
| 1040 | |
| 1041 | // If folding this instruction (and it's subexprs) didn't extend any live |
| 1042 | // ranges, we're ok with it. |
| 1043 | if (BaseReg == 0 && ScaledReg == 0) |
| 1044 | return true; |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 1045 | |
| 1046 | // If all uses of this instruction are ultimately load/store/inlineasm's, |
| 1047 | // check to see if their addressing modes will include this instruction. If |
| 1048 | // so, we can fold it into all uses, so it doesn't matter if it has multiple |
| 1049 | // uses. |
| 1050 | SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses; |
| 1051 | SmallPtrSet<Instruction*, 16> ConsideredInsts; |
Chris Lattner | 695d8ec | 2008-11-26 04:59:11 +0000 | [diff] [blame] | 1052 | if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TLI)) |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 1053 | return false; // Has a non-memory, non-foldable use! |
| 1054 | |
| 1055 | // Now that we know that all uses of this instruction are part of a chain of |
| 1056 | // computation involving only operations that could theoretically be folded |
| 1057 | // into a memory use, loop over each of these uses and see if they could |
| 1058 | // *actually* fold the instruction. |
| 1059 | SmallVector<Instruction*, 32> MatchedAddrModeInsts; |
| 1060 | for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) { |
| 1061 | Instruction *User = MemoryUses[i].first; |
| 1062 | unsigned OpNo = MemoryUses[i].second; |
| 1063 | |
| 1064 | // Get the access type of this use. If the use isn't a pointer, we don't |
| 1065 | // know what it accesses. |
| 1066 | Value *Address = User->getOperand(OpNo); |
| 1067 | if (!isa<PointerType>(Address->getType())) |
| 1068 | return false; |
| 1069 | const Type *AddressAccessTy = |
| 1070 | cast<PointerType>(Address->getType())->getElementType(); |
| 1071 | |
| 1072 | // Do a match against the root of this address, ignoring profitability. This |
| 1073 | // will tell us if the addressing mode for the memory operation will |
| 1074 | // *actually* cover the shared instruction. |
| 1075 | ExtAddrMode Result; |
| 1076 | AddressingModeMatcher Matcher(MatchedAddrModeInsts, TLI, AddressAccessTy, |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 1077 | MemoryInst, Result); |
Chris Lattner | 5eecb7f | 2008-11-26 02:00:14 +0000 | [diff] [blame] | 1078 | Matcher.IgnoreProfitability = true; |
| 1079 | bool Success = Matcher.MatchAddr(Address, 0); |
| 1080 | Success = Success; assert(Success && "Couldn't select *anything*?"); |
| 1081 | |
| 1082 | // If the match didn't cover I, then it won't be shared by it. |
| 1083 | if (std::find(MatchedAddrModeInsts.begin(), MatchedAddrModeInsts.end(), |
| 1084 | I) == MatchedAddrModeInsts.end()) |
| 1085 | return false; |
| 1086 | |
| 1087 | MatchedAddrModeInsts.clear(); |
| 1088 | } |
| 1089 | |
| 1090 | return true; |
| 1091 | } |
| 1092 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1093 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1094 | //===----------------------------------------------------------------------===// |
| 1095 | // Memory Optimization |
| 1096 | //===----------------------------------------------------------------------===// |
| 1097 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1098 | /// IsNonLocalValue - Return true if the specified values are defined in a |
| 1099 | /// different basic block than BB. |
| 1100 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 1101 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 1102 | return I->getParent() != BB; |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1106 | /// OptimizeMemoryInst - Load and Store Instructions have often have |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1107 | /// addressing modes that can do significant amounts of computation. As such, |
| 1108 | /// instruction selection will try to get the load or store to do as much |
| 1109 | /// computation as possible for the program. The problem is that isel can only |
| 1110 | /// see within a single block. As such, we sink as much legal addressing mode |
| 1111 | /// stuff into the block as possible. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1112 | /// |
| 1113 | /// This method is used to optimize both load/store and inline asms with memory |
| 1114 | /// operands. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 1115 | bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr, |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1116 | const Type *AccessTy, |
| 1117 | DenseMap<Value*,Value*> &SunkAddrs) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1118 | // Figure out what addressing mode will be built up for this operation. |
| 1119 | SmallVector<Instruction*, 16> AddrModeInsts; |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 1120 | ExtAddrMode AddrMode = AddressingModeMatcher::Match(Addr, AccessTy,MemoryInst, |
| 1121 | AddrModeInsts, *TLI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1123 | // Check to see if any of the instructions supersumed by this addr mode are |
| 1124 | // non-local to I's BB. |
| 1125 | bool AnyNonLocal = false; |
| 1126 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 1127 | if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1128 | AnyNonLocal = true; |
| 1129 | break; |
| 1130 | } |
| 1131 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1133 | // If all the instructions matched are already in this BB, don't do anything. |
| 1134 | if (!AnyNonLocal) { |
| 1135 | DEBUG(cerr << "CGP: Found local addrmode: " << AddrMode << "\n"); |
| 1136 | return false; |
| 1137 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1139 | // Insert this computation right after this user. Since our caller is |
| 1140 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 1141 | // guaranteed to happen later. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 1142 | BasicBlock::iterator InsertPt = MemoryInst; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1144 | // Now that we determined the addressing expression we want to use and know |
| 1145 | // that we have to sink it into this block. Check to see if we have already |
| 1146 | // done this for some other load/store instr in this block. If so, reuse the |
| 1147 | // computation. |
| 1148 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 1149 | if (SunkAddr) { |
| 1150 | DEBUG(cerr << "CGP: Reusing nonlocal addrmode: " << AddrMode << "\n"); |
| 1151 | if (SunkAddr->getType() != Addr->getType()) |
| 1152 | SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt); |
| 1153 | } else { |
| 1154 | DEBUG(cerr << "CGP: SINKING nonlocal addrmode: " << AddrMode << "\n"); |
| 1155 | const Type *IntPtrTy = TLI->getTargetData()->getIntPtrType(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1156 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1157 | Value *Result = 0; |
| 1158 | // Start with the scale value. |
| 1159 | if (AddrMode.Scale) { |
| 1160 | Value *V = AddrMode.ScaledReg; |
| 1161 | if (V->getType() == IntPtrTy) { |
| 1162 | // done. |
| 1163 | } else if (isa<PointerType>(V->getType())) { |
| 1164 | V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 1165 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 1166 | cast<IntegerType>(V->getType())->getBitWidth()) { |
| 1167 | V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 1168 | } else { |
| 1169 | V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 1170 | } |
| 1171 | if (AddrMode.Scale != 1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1172 | V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy, |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1173 | AddrMode.Scale), |
| 1174 | "sunkaddr", InsertPt); |
| 1175 | Result = V; |
| 1176 | } |
| 1177 | |
| 1178 | // Add in the base register. |
| 1179 | if (AddrMode.BaseReg) { |
| 1180 | Value *V = AddrMode.BaseReg; |
| 1181 | if (V->getType() != IntPtrTy) |
| 1182 | V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 1183 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1184 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1185 | else |
| 1186 | Result = V; |
| 1187 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1188 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1189 | // Add in the BaseGV if present. |
| 1190 | if (AddrMode.BaseGV) { |
| 1191 | Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr", |
| 1192 | InsertPt); |
| 1193 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1194 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1195 | else |
| 1196 | Result = V; |
| 1197 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1198 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1199 | // Add in the Base Offset if present. |
| 1200 | if (AddrMode.BaseOffs) { |
| 1201 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
| 1202 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1203 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1204 | else |
| 1205 | Result = V; |
| 1206 | } |
| 1207 | |
| 1208 | if (Result == 0) |
| 1209 | SunkAddr = Constant::getNullValue(Addr->getType()); |
| 1210 | else |
| 1211 | SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt); |
| 1212 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 1214 | MemoryInst->replaceUsesOfWith(Addr, SunkAddr); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1215 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1216 | if (Addr->use_empty()) |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame^] | 1217 | RecursivelyDeleteTriviallyDeadInstructions(Addr); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1218 | return true; |
| 1219 | } |
| 1220 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1221 | /// OptimizeInlineAsmInst - If there are any memory operands, use |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1222 | /// OptimizeMemoryInst to sink their address computing into the block when |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1223 | /// possible / profitable. |
| 1224 | bool CodeGenPrepare::OptimizeInlineAsmInst(Instruction *I, CallSite CS, |
| 1225 | DenseMap<Value*,Value*> &SunkAddrs) { |
| 1226 | bool MadeChange = false; |
| 1227 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 1228 | |
| 1229 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 1230 | // ConstraintOperands list. |
| 1231 | std::vector<InlineAsm::ConstraintInfo> |
| 1232 | ConstraintInfos = IA->ParseConstraints(); |
| 1233 | |
| 1234 | /// ConstraintOperands - Information about all of the constraints. |
| 1235 | std::vector<TargetLowering::AsmOperandInfo> ConstraintOperands; |
| 1236 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 1237 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 1238 | ConstraintOperands. |
| 1239 | push_back(TargetLowering::AsmOperandInfo(ConstraintInfos[i])); |
| 1240 | TargetLowering::AsmOperandInfo &OpInfo = ConstraintOperands.back(); |
| 1241 | |
| 1242 | // Compute the value type for each operand. |
| 1243 | switch (OpInfo.Type) { |
| 1244 | case InlineAsm::isOutput: |
| 1245 | if (OpInfo.isIndirect) |
| 1246 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 1247 | break; |
| 1248 | case InlineAsm::isInput: |
| 1249 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 1250 | break; |
| 1251 | case InlineAsm::isClobber: |
| 1252 | // Nothing to do. |
| 1253 | break; |
| 1254 | } |
| 1255 | |
| 1256 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | a7e6146 | 2008-09-24 06:48:55 +0000 | [diff] [blame] | 1257 | TLI->ComputeConstraintToUse(OpInfo, SDValue(), |
| 1258 | OpInfo.ConstraintType == TargetLowering::C_Memory); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1259 | |
Eli Friedman | 9ec8095 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 1260 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 1261 | OpInfo.isIndirect) { |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1262 | Value *OpVal = OpInfo.CallOperandVal; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1263 | MadeChange |= OptimizeMemoryInst(I, OpVal, OpVal->getType(), SunkAddrs); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | return MadeChange; |
| 1268 | } |
| 1269 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1270 | bool CodeGenPrepare::OptimizeExtUses(Instruction *I) { |
| 1271 | BasicBlock *DefBB = I->getParent(); |
| 1272 | |
| 1273 | // If both result of the {s|z}xt and its source are live out, rewrite all |
| 1274 | // other uses of the source with result of extension. |
| 1275 | Value *Src = I->getOperand(0); |
| 1276 | if (Src->hasOneUse()) |
| 1277 | return false; |
| 1278 | |
Evan Cheng | 696e5c0 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 1279 | // Only do this xform if truncating is free. |
Gabor Greif | 53bdbd7 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 1280 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1281 | return false; |
| 1282 | |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1283 | // 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] | 1284 | // this block. |
| 1285 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1286 | return false; |
| 1287 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1288 | bool DefIsLiveOut = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1289 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1290 | UI != E; ++UI) { |
| 1291 | Instruction *User = cast<Instruction>(*UI); |
| 1292 | |
| 1293 | // Figure out which BB this ext is used in. |
| 1294 | BasicBlock *UserBB = User->getParent(); |
| 1295 | if (UserBB == DefBB) continue; |
| 1296 | DefIsLiveOut = true; |
| 1297 | break; |
| 1298 | } |
| 1299 | if (!DefIsLiveOut) |
| 1300 | return false; |
| 1301 | |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1302 | // Make sure non of the uses are PHI nodes. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1303 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1304 | UI != E; ++UI) { |
| 1305 | Instruction *User = cast<Instruction>(*UI); |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1306 | BasicBlock *UserBB = User->getParent(); |
| 1307 | if (UserBB == DefBB) continue; |
| 1308 | // Be conservative. We don't want this xform to end up introducing |
| 1309 | // reloads just before load / store instructions. |
| 1310 | if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User)) |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1311 | return false; |
| 1312 | } |
| 1313 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1314 | // InsertedTruncs - Only insert one trunc in each block once. |
| 1315 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 1316 | |
| 1317 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1318 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1319 | UI != E; ++UI) { |
| 1320 | Use &TheUse = UI.getUse(); |
| 1321 | Instruction *User = cast<Instruction>(*UI); |
| 1322 | |
| 1323 | // Figure out which BB this ext is used in. |
| 1324 | BasicBlock *UserBB = User->getParent(); |
| 1325 | if (UserBB == DefBB) continue; |
| 1326 | |
| 1327 | // Both src and def are live in this block. Rewrite the use. |
| 1328 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 1329 | |
| 1330 | if (!InsertedTrunc) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 1331 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1332 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1333 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
| 1334 | } |
| 1335 | |
| 1336 | // Replace a use of the {s|z}ext source with a use of the result. |
| 1337 | TheUse = InsertedTrunc; |
| 1338 | |
| 1339 | MadeChange = true; |
| 1340 | } |
| 1341 | |
| 1342 | return MadeChange; |
| 1343 | } |
| 1344 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1345 | // In this pass we look for GEP and cast instructions that are used |
| 1346 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 1347 | // selection. |
| 1348 | bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) { |
| 1349 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1350 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1351 | // Split all critical edges where the dest block has a PHI and where the phi |
| 1352 | // has shared immediate operands. |
| 1353 | TerminatorInst *BBTI = BB.getTerminator(); |
| 1354 | if (BBTI->getNumSuccessors() > 1) { |
| 1355 | for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) |
| 1356 | if (isa<PHINode>(BBTI->getSuccessor(i)->begin()) && |
| 1357 | isCriticalEdge(BBTI, i, true)) |
| 1358 | SplitEdgeNicely(BBTI, i, this); |
| 1359 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1360 | |
| 1361 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1362 | // Keep track of non-local addresses that have been sunk into this block. |
| 1363 | // This allows us to avoid inserting duplicate code for blocks with multiple |
| 1364 | // load/stores of the same address. |
| 1365 | DenseMap<Value*, Value*> SunkAddrs; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1366 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1367 | for (BasicBlock::iterator BBI = BB.begin(), E = BB.end(); BBI != E; ) { |
| 1368 | Instruction *I = BBI++; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1369 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1370 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1371 | // If the source of the cast is a constant, then this should have |
| 1372 | // already been constant folded. The only reason NOT to constant fold |
| 1373 | // it is if something (e.g. LSR) was careful to place the constant |
| 1374 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 1375 | // the address of globals out of a loop). If this is the case, we don't |
| 1376 | // want to forward-subst the cast. |
| 1377 | if (isa<Constant>(CI->getOperand(0))) |
| 1378 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1379 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1380 | bool Change = false; |
| 1381 | if (TLI) { |
| 1382 | Change = OptimizeNoopCopyExpression(CI, *TLI); |
| 1383 | MadeChange |= Change; |
| 1384 | } |
| 1385 | |
Evan Cheng | 55e641b | 2008-03-19 22:02:26 +0000 | [diff] [blame] | 1386 | if (!Change && (isa<ZExtInst>(I) || isa<SExtInst>(I))) |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1387 | MadeChange |= OptimizeExtUses(I); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 1388 | } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) { |
| 1389 | MadeChange |= OptimizeCmpExpression(CI); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1390 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 1391 | if (TLI) |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1392 | MadeChange |= OptimizeMemoryInst(I, I->getOperand(0), LI->getType(), |
| 1393 | SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1394 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 1395 | if (TLI) |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1396 | MadeChange |= OptimizeMemoryInst(I, SI->getOperand(1), |
| 1397 | SI->getOperand(0)->getType(), |
| 1398 | SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1399 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Chris Lattner | f25646b | 2007-04-14 00:17:39 +0000 | [diff] [blame] | 1400 | if (GEPI->hasAllZeroIndices()) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1401 | /// The GEP operand must be a pointer, so must its result -> BitCast |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1402 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1403 | GEPI->getName(), GEPI); |
| 1404 | GEPI->replaceAllUsesWith(NC); |
| 1405 | GEPI->eraseFromParent(); |
| 1406 | MadeChange = true; |
| 1407 | BBI = NC; |
| 1408 | } |
| 1409 | } else if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 1410 | // If we found an inline asm expession, and if the target knows how to |
| 1411 | // lower it to normal LLVM code, do so now. |
| 1412 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1413 | if (const TargetAsmInfo *TAI = |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1414 | TLI->getTargetMachine().getTargetAsmInfo()) { |
| 1415 | if (TAI->ExpandInlineAsm(CI)) |
| 1416 | BBI = BB.begin(); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1417 | else |
| 1418 | // Sink address computing for memory operands into the block. |
| 1419 | MadeChange |= OptimizeInlineAsmInst(I, &(*CI), SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1420 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1424 | return MadeChange; |
| 1425 | } |