Chris Lattner | f2836d1 | 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 | f3ebc3f | 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 | f2836d1 | 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 | 829046b | 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 | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Quentin Colombet | a349084 | 2014-02-22 00:07:45 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Function.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 27 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/IRBuilder.h" |
| 29 | #include "llvm/IR/InlineAsm.h" |
| 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 32 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 33 | #include "llvm/IR/ValueHandle.h" |
Chandler Carruth | a4ea269 | 2014-03-04 11:26:31 +0000 | [diff] [blame] | 34 | #include "llvm/IR/ValueMap.h" |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 35 | #include "llvm/Pass.h" |
Evan Cheng | 8b637b1 | 2010-08-17 01:34:49 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetLibraryInfo.h" |
| 40 | #include "llvm/Target/TargetLowering.h" |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 43 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Preston Gurd | cdf540d | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/BypassSlowDivision.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 45 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 46 | using namespace llvm; |
Chris Lattner | d616ef5 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 47 | using namespace llvm::PatternMatch; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 48 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame^] | 49 | #define DEBUG_TYPE "codegenprepare" |
| 50 | |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 51 | STATISTIC(NumBlocksElim, "Number of blocks eliminated"); |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 52 | STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated"); |
| 53 | STATISTIC(NumGEPsElim, "Number of GEPs converted to casts"); |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 54 | STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of " |
| 55 | "sunken Cmps"); |
| 56 | STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses " |
| 57 | "of sunken Casts"); |
| 58 | STATISTIC(NumMemoryInsts, "Number of memory instructions whose address " |
| 59 | "computations were sunk"); |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 60 | STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads"); |
| 61 | STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized"); |
| 62 | STATISTIC(NumRetsDup, "Number of return instructions duplicated"); |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 63 | STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved"); |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 64 | STATISTIC(NumSelectsExpanded, "Number of selects turned into branches"); |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 65 | STATISTIC(NumAndCmpsMoved, "Number of and/cmp's pushed into branches"); |
Jakob Stoklund Olesen | eb12f49 | 2010-09-30 20:51:52 +0000 | [diff] [blame] | 66 | |
Cameron Zwarich | 338d362 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 67 | static cl::opt<bool> DisableBranchOpts( |
| 68 | "disable-cgp-branch-opts", cl::Hidden, cl::init(false), |
| 69 | cl::desc("Disable branch optimizations in CodeGenPrepare")); |
| 70 | |
Benjamin Kramer | 3d38c17 | 2012-05-06 14:25:16 +0000 | [diff] [blame] | 71 | static cl::opt<bool> DisableSelectToBranch( |
| 72 | "disable-cgp-select2branch", cl::Hidden, cl::init(false), |
| 73 | cl::desc("Disable select to branch conversion.")); |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 74 | |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 75 | static cl::opt<bool> AddrSinkUsingGEPs( |
| 76 | "addr-sink-using-gep", cl::Hidden, cl::init(false), |
| 77 | cl::desc("Address sinking in CGP using GEPs.")); |
| 78 | |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 79 | static cl::opt<bool> EnableAndCmpSinking( |
| 80 | "enable-andcmp-sinking", cl::Hidden, cl::init(true), |
| 81 | cl::desc("Enable sinkinig and/cmp into branches.")); |
| 82 | |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 83 | namespace { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 84 | typedef SmallPtrSet<Instruction *, 16> SetOfInstrs; |
| 85 | typedef DenseMap<Instruction *, Type *> InstrToOrigTy; |
| 86 | |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 87 | class CodeGenPrepare : public FunctionPass { |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 88 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 89 | /// transformation profitability. |
Bill Wendling | 7a639ea | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 90 | const TargetMachine *TM; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 91 | const TargetLowering *TLI; |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 92 | const TargetLibraryInfo *TLInfo; |
Cameron Zwarich | 84986b2 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 93 | DominatorTree *DT; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 95 | /// CurInstIterator - As we scan instructions optimizing them, this is the |
| 96 | /// next instruction to optimize. Xforms that can invalidate this should |
| 97 | /// update it. |
| 98 | BasicBlock::iterator CurInstIterator; |
Evan Cheng | 3b3de7c | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 99 | |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 100 | /// Keeps track of non-local addresses that have been sunk into a block. |
| 101 | /// This allows us to avoid inserting duplicate code for blocks with |
| 102 | /// multiple load/stores of the same address. |
Nick Lewycky | 5fb1963 | 2013-05-08 09:00:10 +0000 | [diff] [blame] | 103 | ValueMap<Value*, Value*> SunkAddrs; |
Cameron Zwarich | ce3b930 | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 104 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 105 | /// Keeps track of all truncates inserted for the current function. |
| 106 | SetOfInstrs InsertedTruncsSet; |
| 107 | /// Keeps track of the type of the related instruction before their |
| 108 | /// promotion for the current function. |
| 109 | InstrToOrigTy PromotedInsts; |
| 110 | |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 111 | /// ModifiedDT - If CFG is modified in anyway, dominator tree may need to |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 112 | /// be updated. |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 113 | bool ModifiedDT; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 114 | |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 115 | /// OptSize - True if optimizing for size. |
| 116 | bool OptSize; |
| 117 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 118 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 119 | static char ID; // Pass identification, replacement for typeid |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 120 | explicit CodeGenPrepare(const TargetMachine *TM = nullptr) |
| 121 | : FunctionPass(ID), TM(TM), TLI(nullptr) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 122 | initializeCodeGenPreparePass(*PassRegistry::getPassRegistry()); |
| 123 | } |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 124 | bool runOnFunction(Function &F) override; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 125 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 126 | const char *getPassName() const override { return "CodeGen Prepare"; } |
Evan Cheng | 99cafb1 | 2012-12-21 01:48:14 +0000 | [diff] [blame] | 127 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 128 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 129 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 130 | AU.addRequired<TargetLibraryInfo>(); |
Andreas Neustifter | f8cb758 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 133 | private: |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 134 | bool EliminateFallThrough(Function &F); |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 135 | bool EliminateMostlyEmptyBlocks(Function &F); |
| 136 | bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 137 | void EliminateMostlyEmptyBlock(BasicBlock *BB); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 138 | bool OptimizeBlock(BasicBlock &BB); |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 139 | bool OptimizeInst(Instruction *I); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 140 | bool OptimizeMemoryInst(Instruction *I, Value *Addr, Type *AccessTy); |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 141 | bool OptimizeInlineAsmInst(CallInst *CS); |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 142 | bool OptimizeCallInst(CallInst *CI); |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 143 | bool MoveExtToFormExtLoad(Instruction *I); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 144 | bool OptimizeExtUses(Instruction *I); |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 145 | bool OptimizeSelectInst(SelectInst *SI); |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 146 | bool OptimizeShuffleVectorInst(ShuffleVectorInst *SI); |
Benjamin Kramer | 455fa35 | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 147 | bool DupRetToEnableTailCallOpts(BasicBlock *BB); |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 148 | bool PlaceDbgValues(Function &F); |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 149 | bool sinkAndCmp(Function &F); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 150 | }; |
| 151 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 152 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 153 | char CodeGenPrepare::ID = 0; |
Quentin Colombet | dc0b2ea | 2014-01-16 21:44:34 +0000 | [diff] [blame] | 154 | static void *initializeCodeGenPreparePassOnce(PassRegistry &Registry) { |
| 155 | initializeTargetLibraryInfoPass(Registry); |
| 156 | PassInfo *PI = new PassInfo( |
| 157 | "Optimize for code generation", "codegenprepare", &CodeGenPrepare::ID, |
| 158 | PassInfo::NormalCtor_t(callDefaultCtor<CodeGenPrepare>), false, false, |
| 159 | PassInfo::TargetMachineCtor_t(callTargetMachineCtor<CodeGenPrepare>)); |
| 160 | Registry.registerPass(*PI, true); |
| 161 | return PI; |
| 162 | } |
| 163 | |
| 164 | void llvm::initializeCodeGenPreparePass(PassRegistry &Registry) { |
| 165 | CALL_ONCE_INITIALIZATION(initializeCodeGenPreparePassOnce) |
| 166 | } |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 167 | |
Bill Wendling | 7a639ea | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 168 | FunctionPass *llvm::createCodeGenPreparePass(const TargetMachine *TM) { |
| 169 | return new CodeGenPrepare(TM); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 172 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 173 | if (skipOptnoneFunction(F)) |
| 174 | return false; |
| 175 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 176 | bool EverMadeChange = false; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 177 | // Clear per function information. |
| 178 | InsertedTruncsSet.clear(); |
| 179 | PromotedInsts.clear(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 180 | |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 181 | ModifiedDT = false; |
Bill Wendling | 7a639ea | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 182 | if (TM) TLI = TM->getTargetLowering(); |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 183 | TLInfo = &getAnalysis<TargetLibraryInfo>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 184 | DominatorTreeWrapperPass *DTWP = |
| 185 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 186 | DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Bill Wendling | 698e84f | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 187 | OptSize = F.getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 188 | Attribute::OptimizeForSize); |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 189 | |
Preston Gurd | cdf540d | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 190 | /// This optimization identifies DIV instructions that can be |
| 191 | /// profitably bypassed and carried out with a shorter, faster divide. |
Preston Gurd | 485296d | 2013-03-04 18:13:57 +0000 | [diff] [blame] | 192 | if (!OptSize && TLI && TLI->isSlowDivBypassed()) { |
Preston Gurd | 0d67f51 | 2012-10-04 21:33:40 +0000 | [diff] [blame] | 193 | const DenseMap<unsigned int, unsigned int> &BypassWidths = |
| 194 | TLI->getBypassSlowDivWidths(); |
Evan Cheng | 71be12b | 2012-09-14 21:25:34 +0000 | [diff] [blame] | 195 | for (Function::iterator I = F.begin(); I != F.end(); I++) |
Preston Gurd | 0d67f51 | 2012-10-04 21:33:40 +0000 | [diff] [blame] | 196 | EverMadeChange |= bypassSlowDivision(F, I, BypassWidths); |
Preston Gurd | cdf540d | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Eliminate blocks that contain only PHI nodes and an |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 200 | // unconditional branch. |
| 201 | EverMadeChange |= EliminateMostlyEmptyBlocks(F); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 202 | |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 203 | // llvm.dbg.value is far away from the value then iSel may not be able |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 204 | // handle it properly. iSel will drop llvm.dbg.value if it can not |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 205 | // find a node corresponding to the value. |
| 206 | EverMadeChange |= PlaceDbgValues(F); |
| 207 | |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 208 | // If there is a mask, compare against zero, and branch that can be combined |
| 209 | // into a single target instruction, push the mask and compare into branch |
| 210 | // users. Do this before OptimizeBlock -> OptimizeInst -> |
| 211 | // OptimizeCmpExpression, which perturbs the pattern being searched for. |
| 212 | if (!DisableBranchOpts) |
| 213 | EverMadeChange |= sinkAndCmp(F); |
| 214 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 215 | bool MadeChange = true; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 216 | while (MadeChange) { |
| 217 | MadeChange = false; |
Hans Wennborg | 02fbc71 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 218 | for (Function::iterator I = F.begin(); I != F.end(); ) { |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 219 | BasicBlock *BB = I++; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 220 | MadeChange |= OptimizeBlock(*BB); |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 222 | EverMadeChange |= MadeChange; |
| 223 | } |
Cameron Zwarich | ce3b930 | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 224 | |
| 225 | SunkAddrs.clear(); |
| 226 | |
Cameron Zwarich | 338d362 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 227 | if (!DisableBranchOpts) { |
| 228 | MadeChange = false; |
Bill Wendling | 97b9359 | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 229 | SmallPtrSet<BasicBlock*, 8> WorkList; |
| 230 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 231 | SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB)); |
Frits van Bommel | ad96455 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 232 | MadeChange |= ConstantFoldTerminator(BB, true); |
Bill Wendling | 97b9359 | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 233 | if (!MadeChange) continue; |
| 234 | |
| 235 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 236 | II = Successors.begin(), IE = Successors.end(); II != IE; ++II) |
| 237 | if (pred_begin(*II) == pred_end(*II)) |
| 238 | WorkList.insert(*II); |
| 239 | } |
| 240 | |
Bill Wendling | f3614fd | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 241 | // Delete the dead blocks and any of their dead successors. |
Bill Wendling | ab417b6 | 2012-12-06 00:30:20 +0000 | [diff] [blame] | 242 | MadeChange |= !WorkList.empty(); |
Bill Wendling | f3614fd | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 243 | while (!WorkList.empty()) { |
| 244 | BasicBlock *BB = *WorkList.begin(); |
| 245 | WorkList.erase(BB); |
| 246 | SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB)); |
| 247 | |
| 248 | DeleteDeadBlock(BB); |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 249 | |
Bill Wendling | f3614fd | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 250 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 251 | II = Successors.begin(), IE = Successors.end(); II != IE; ++II) |
| 252 | if (pred_begin(*II) == pred_end(*II)) |
| 253 | WorkList.insert(*II); |
| 254 | } |
Cameron Zwarich | 338d362 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 255 | |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 256 | // Merge pairs of basic blocks with unconditional branches, connected by |
| 257 | // a single edge. |
| 258 | if (EverMadeChange || MadeChange) |
| 259 | MadeChange |= EliminateFallThrough(F); |
| 260 | |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 261 | if (MadeChange) |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 262 | ModifiedDT = true; |
Cameron Zwarich | 338d362 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 263 | EverMadeChange |= MadeChange; |
| 264 | } |
| 265 | |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 266 | if (ModifiedDT && DT) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 267 | DT->recalculate(F); |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 268 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 269 | return EverMadeChange; |
| 270 | } |
| 271 | |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 272 | /// EliminateFallThrough - Merge basic blocks which are connected |
| 273 | /// by a single edge, where one of the basic blocks has a single successor |
| 274 | /// pointing to the other basic block, which has a single predecessor. |
| 275 | bool CodeGenPrepare::EliminateFallThrough(Function &F) { |
| 276 | bool Changed = false; |
| 277 | // Scan all of the blocks in the function, except for the entry block. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 278 | for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) { |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 279 | BasicBlock *BB = I++; |
| 280 | // If the destination block has a single pred, then this is a trivial |
| 281 | // edge, just collapse it. |
| 282 | BasicBlock *SinglePred = BB->getSinglePredecessor(); |
| 283 | |
Evan Cheng | 64a223a | 2012-09-28 23:58:57 +0000 | [diff] [blame] | 284 | // Don't merge if BB's address is taken. |
| 285 | if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue; |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 286 | |
| 287 | BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); |
| 288 | if (Term && !Term->isConditional()) { |
| 289 | Changed = true; |
Michael Liao | 6e12d12 | 2012-08-21 05:55:22 +0000 | [diff] [blame] | 290 | DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n"); |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 291 | // Remember if SinglePred was the entry block of the function. |
| 292 | // If so, we will need to move BB back to the entry position. |
| 293 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
| 294 | MergeBasicBlockIntoOnlyPred(BB, this); |
| 295 | |
| 296 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 297 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
| 298 | |
| 299 | // We have erased a block. Update the iterator. |
| 300 | I = BB; |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | return Changed; |
| 304 | } |
| 305 | |
Dale Johannesen | 4026b04 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 306 | /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes, |
| 307 | /// debug info directives, and an unconditional branch. Passes before isel |
| 308 | /// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for |
| 309 | /// isel. Start by eliminating these blocks so we can split them the way we |
| 310 | /// want them. |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 311 | bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) { |
| 312 | bool MadeChange = false; |
| 313 | // Note that this intentionally skips the entry block. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 314 | for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) { |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 315 | BasicBlock *BB = I++; |
| 316 | |
| 317 | // If this block doesn't end with an uncond branch, ignore it. |
| 318 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 319 | if (!BI || !BI->isUnconditional()) |
| 320 | continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 321 | |
Dale Johannesen | 4026b04 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 322 | // If the instruction before the branch (skipping debug info) isn't a phi |
| 323 | // node, then other stuff is happening here. |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 324 | BasicBlock::iterator BBI = BI; |
| 325 | if (BBI != BB->begin()) { |
| 326 | --BBI; |
Dale Johannesen | 4026b04 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 327 | while (isa<DbgInfoIntrinsic>(BBI)) { |
| 328 | if (BBI == BB->begin()) |
| 329 | break; |
| 330 | --BBI; |
| 331 | } |
| 332 | if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI)) |
| 333 | continue; |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 334 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 335 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 336 | // Do not break infinite loops. |
| 337 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 338 | if (DestBB == BB) |
| 339 | continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 340 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 341 | if (!CanMergeBlocks(BB, DestBB)) |
| 342 | continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 343 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 344 | EliminateMostlyEmptyBlock(BB); |
| 345 | MadeChange = true; |
| 346 | } |
| 347 | return MadeChange; |
| 348 | } |
| 349 | |
| 350 | /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a |
| 351 | /// single uncond branch between them, and BB contains no other non-phi |
| 352 | /// instructions. |
| 353 | bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB, |
| 354 | const BasicBlock *DestBB) const { |
| 355 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 356 | // the successor. If there are more complex condition (e.g. preheaders), |
| 357 | // don't mess around with them. |
| 358 | BasicBlock::const_iterator BBI = BB->begin(); |
| 359 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 360 | for (const User *U : PN->users()) { |
| 361 | const Instruction *UI = cast<Instruction>(U); |
| 362 | if (UI->getParent() != DestBB || !isa<PHINode>(UI)) |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 363 | return false; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 364 | // If User is inside DestBB block and it is a PHINode then check |
| 365 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | d320852 | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 366 | // a complex condition (e.g. preheaders) we want to avoid here. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 367 | if (UI->getParent() == DestBB) { |
| 368 | if (const PHINode *UPN = dyn_cast<PHINode>(UI)) |
Devang Patel | d320852 | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 369 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 370 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 371 | if (Insn && Insn->getParent() == BB && |
| 372 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 373 | return false; |
| 374 | } |
| 375 | } |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 378 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 379 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 380 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 381 | // can't merge the block. |
| 382 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 383 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 384 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 385 | // Collect the preds of BB. |
Chris Lattner | 8201a9b | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 386 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 387 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 388 | // It is faster to get preds from a PHI than with pred_iterator. |
| 389 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 390 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 391 | } else { |
| 392 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 393 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 394 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 395 | // Walk the preds of DestBB. |
| 396 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 397 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 398 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 399 | BBI = DestBB->begin(); |
| 400 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 401 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 402 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 403 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 404 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 405 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 406 | if (V2PN->getParent() == BB) |
| 407 | V2 = V2PN->getIncomingValueForBlock(Pred); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 408 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 409 | // If there is a conflict, bail out. |
| 410 | if (V1 != V2) return false; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and |
| 420 | /// an unconditional branch in it. |
| 421 | void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) { |
| 422 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 423 | BasicBlock *DestBB = BI->getSuccessor(0); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 424 | |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 425 | DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 426 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 427 | // If the destination block has a single pred, then this is a trivial edge, |
| 428 | // just collapse it. |
Chris Lattner | 4059f43 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 429 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { |
Chris Lattner | 8a172da | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 430 | if (SinglePred != DestBB) { |
| 431 | // Remember if SinglePred was the entry block of the function. If so, we |
| 432 | // will need to move BB back to the entry position. |
| 433 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Andreas Neustifter | f8cb758 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 434 | MergeBasicBlockIntoOnlyPred(DestBB, this); |
Chris Lattner | 4059f43 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 435 | |
Chris Lattner | 8a172da | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 436 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 437 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 438 | |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 439 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | 8a172da | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 440 | return; |
| 441 | } |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 442 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 443 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 444 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 445 | // to handle the new incoming edges it is about to have. |
| 446 | PHINode *PN; |
| 447 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 448 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 449 | // Remove the incoming value for BB, and remember it. |
| 450 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 451 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 452 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 453 | // value that dominates BB. |
| 454 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 455 | if (InValPhi && InValPhi->getParent() == BB) { |
| 456 | // Add all of the input values of the input PHI as inputs of this phi. |
| 457 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 458 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 459 | InValPhi->getIncomingBlock(i)); |
| 460 | } else { |
| 461 | // Otherwise, add one instance of the dominating value for each edge that |
| 462 | // we will be adding. |
| 463 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 464 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 465 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 466 | } else { |
| 467 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 468 | PN->addIncoming(InVal, *PI); |
| 469 | } |
| 470 | } |
| 471 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 472 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 473 | // The PHIs are now updated, change everything that refers to BB to use |
| 474 | // DestBB and remove BB. |
| 475 | BB->replaceAllUsesWith(DestBB); |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 476 | if (DT && !ModifiedDT) { |
Cameron Zwarich | 84986b2 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 477 | BasicBlock *BBIDom = DT->getNode(BB)->getIDom()->getBlock(); |
| 478 | BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock(); |
| 479 | BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom); |
| 480 | DT->changeImmediateDominator(DestBB, NewIDom); |
| 481 | DT->eraseNode(BB); |
| 482 | } |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 483 | BB->eraseFromParent(); |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 484 | ++NumBlocksElim; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 485 | |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 486 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 489 | /// SinkCast - Sink the specified cast instruction into its user blocks |
| 490 | static bool SinkCast(CastInst *CI) { |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 491 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 492 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 493 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 494 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 495 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 496 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 497 | for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end(); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 498 | UI != E; ) { |
| 499 | Use &TheUse = UI.getUse(); |
| 500 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 501 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 502 | // Figure out which BB this cast is used in. For PHI's this is the |
| 503 | // appropriate predecessor block. |
| 504 | BasicBlock *UserBB = User->getParent(); |
| 505 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 506 | UserBB = PN->getIncomingBlock(TheUse); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 507 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 508 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 509 | // Preincrement use iterator so we don't invalidate it. |
| 510 | ++UI; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 511 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 512 | // If this user is in the same block as the cast, don't change the cast. |
| 513 | if (UserBB == DefBB) continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 514 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 515 | // If we have already inserted a cast into this block, use it. |
| 516 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 517 | |
| 518 | if (!InsertedCast) { |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 519 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 520 | InsertedCast = |
| 521 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 522 | InsertPt); |
| 523 | MadeChange = true; |
| 524 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 525 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 526 | // Replace a use of the cast with a use of the new cast. |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 527 | TheUse = InsertedCast; |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 528 | ++NumCastUses; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 529 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 530 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 531 | // If we removed all uses, nuke the cast. |
Duncan Sands | afa84da4 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 532 | if (CI->use_empty()) { |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 533 | CI->eraseFromParent(); |
Duncan Sands | afa84da4 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 534 | MadeChange = true; |
| 535 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 536 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 537 | return MadeChange; |
| 538 | } |
| 539 | |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 540 | /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop |
| 541 | /// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC), |
| 542 | /// sink it into user blocks to reduce the number of virtual |
| 543 | /// registers that must be created and coalesced. |
| 544 | /// |
| 545 | /// Return true if any changes are made. |
| 546 | /// |
| 547 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){ |
| 548 | // If this is a noop copy, |
| 549 | EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 550 | EVT DstVT = TLI.getValueType(CI->getType()); |
| 551 | |
| 552 | // This is an fp<->int conversion? |
| 553 | if (SrcVT.isInteger() != DstVT.isInteger()) |
| 554 | return false; |
| 555 | |
| 556 | // If this is an extension, it will be a zero or sign extension, which |
| 557 | // isn't a noop. |
| 558 | if (SrcVT.bitsLT(DstVT)) return false; |
| 559 | |
| 560 | // If these values will be promoted, find out what they will be promoted |
| 561 | // to. This helps us consider truncates on PPC as noop copies when they |
| 562 | // are. |
| 563 | if (TLI.getTypeAction(CI->getContext(), SrcVT) == |
| 564 | TargetLowering::TypePromoteInteger) |
| 565 | SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT); |
| 566 | if (TLI.getTypeAction(CI->getContext(), DstVT) == |
| 567 | TargetLowering::TypePromoteInteger) |
| 568 | DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT); |
| 569 | |
| 570 | // If, after promotion, these are the same types, this is a noop copy. |
| 571 | if (SrcVT != DstVT) |
| 572 | return false; |
| 573 | |
| 574 | return SinkCast(CI); |
| 575 | } |
| 576 | |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 577 | /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 578 | /// the number of virtual registers that must be created and coalesced. This is |
Chris Lattner | 2740694 | 2007-08-02 16:53:43 +0000 | [diff] [blame] | 579 | /// a clear win except on targets with multiple condition code registers |
| 580 | /// (PowerPC), where it might lose; some adjustment may be wanted there. |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 581 | /// |
| 582 | /// Return true if any changes are made. |
Chris Lattner | 6416a6b | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 583 | static bool OptimizeCmpExpression(CmpInst *CI) { |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 584 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 585 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 586 | /// InsertedCmp - Only insert a cmp in each block once. |
| 587 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 588 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 589 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 590 | for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end(); |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 591 | UI != E; ) { |
| 592 | Use &TheUse = UI.getUse(); |
| 593 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 594 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 595 | // Preincrement use iterator so we don't invalidate it. |
| 596 | ++UI; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 597 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 598 | // Don't bother for PHI nodes. |
| 599 | if (isa<PHINode>(User)) |
| 600 | continue; |
| 601 | |
| 602 | // Figure out which BB this cmp is used in. |
| 603 | BasicBlock *UserBB = User->getParent(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 604 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 605 | // If this user is in the same block as the cmp, don't change the cmp. |
| 606 | if (UserBB == DefBB) continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 607 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 608 | // If we have already inserted a cmp into this block, use it. |
| 609 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 610 | |
| 611 | if (!InsertedCmp) { |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 612 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 613 | InsertedCmp = |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 614 | CmpInst::Create(CI->getOpcode(), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 615 | CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 616 | CI->getOperand(1), "", InsertPt); |
| 617 | MadeChange = true; |
| 618 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 619 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 620 | // Replace a use of the cmp with a use of the new cmp. |
| 621 | TheUse = InsertedCmp; |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 622 | ++NumCmpUses; |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 623 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 624 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 625 | // If we removed all uses, nuke the cmp. |
| 626 | if (CI->use_empty()) |
| 627 | CI->eraseFromParent(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 628 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 629 | return MadeChange; |
| 630 | } |
| 631 | |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 632 | /// isExtractBitsCandidateUse - Check if the candidates could |
| 633 | /// be combined with shift instruction, which includes: |
| 634 | /// 1. Truncate instruction |
| 635 | /// 2. And instruction and the imm is a mask of the low bits: |
| 636 | /// imm & (imm+1) == 0 |
| 637 | bool isExtractBitsCandidateUse(Instruction *User) { |
| 638 | if (!isa<TruncInst>(User)) { |
| 639 | if (User->getOpcode() != Instruction::And || |
| 640 | !isa<ConstantInt>(User->getOperand(1))) |
| 641 | return false; |
| 642 | |
Quentin Colombet | d4f4469 | 2014-04-22 01:20:34 +0000 | [diff] [blame] | 643 | const APInt &Cimm = cast<ConstantInt>(User->getOperand(1))->getValue(); |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 644 | |
Quentin Colombet | d4f4469 | 2014-04-22 01:20:34 +0000 | [diff] [blame] | 645 | if ((Cimm & (Cimm + 1)).getBoolValue()) |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 646 | return false; |
| 647 | } |
| 648 | return true; |
| 649 | } |
| 650 | |
| 651 | /// SinkShiftAndTruncate - sink both shift and truncate instruction |
| 652 | /// to the use of truncate's BB. |
| 653 | bool |
| 654 | SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI, |
| 655 | DenseMap<BasicBlock *, BinaryOperator *> &InsertedShifts, |
| 656 | const TargetLowering &TLI) { |
| 657 | BasicBlock *UserBB = User->getParent(); |
| 658 | DenseMap<BasicBlock *, CastInst *> InsertedTruncs; |
| 659 | TruncInst *TruncI = dyn_cast<TruncInst>(User); |
| 660 | bool MadeChange = false; |
| 661 | |
| 662 | for (Value::user_iterator TruncUI = TruncI->user_begin(), |
| 663 | TruncE = TruncI->user_end(); |
| 664 | TruncUI != TruncE;) { |
| 665 | |
| 666 | Use &TruncTheUse = TruncUI.getUse(); |
| 667 | Instruction *TruncUser = cast<Instruction>(*TruncUI); |
| 668 | // Preincrement use iterator so we don't invalidate it. |
| 669 | |
| 670 | ++TruncUI; |
| 671 | |
| 672 | int ISDOpcode = TLI.InstructionOpcodeToISD(TruncUser->getOpcode()); |
| 673 | if (!ISDOpcode) |
| 674 | continue; |
| 675 | |
| 676 | // If the use is actually a legal node, there will not be an implicit |
| 677 | // truncate. |
| 678 | if (TLI.isOperationLegalOrCustom(ISDOpcode, |
| 679 | EVT::getEVT(TruncUser->getType()))) |
| 680 | continue; |
| 681 | |
| 682 | // Don't bother for PHI nodes. |
| 683 | if (isa<PHINode>(TruncUser)) |
| 684 | continue; |
| 685 | |
| 686 | BasicBlock *TruncUserBB = TruncUser->getParent(); |
| 687 | |
| 688 | if (UserBB == TruncUserBB) |
| 689 | continue; |
| 690 | |
| 691 | BinaryOperator *&InsertedShift = InsertedShifts[TruncUserBB]; |
| 692 | CastInst *&InsertedTrunc = InsertedTruncs[TruncUserBB]; |
| 693 | |
| 694 | if (!InsertedShift && !InsertedTrunc) { |
| 695 | BasicBlock::iterator InsertPt = TruncUserBB->getFirstInsertionPt(); |
| 696 | // Sink the shift |
| 697 | if (ShiftI->getOpcode() == Instruction::AShr) |
| 698 | InsertedShift = |
| 699 | BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, "", InsertPt); |
| 700 | else |
| 701 | InsertedShift = |
| 702 | BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, "", InsertPt); |
| 703 | |
| 704 | // Sink the trunc |
| 705 | BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt(); |
| 706 | TruncInsertPt++; |
| 707 | |
| 708 | InsertedTrunc = CastInst::Create(TruncI->getOpcode(), InsertedShift, |
| 709 | TruncI->getType(), "", TruncInsertPt); |
| 710 | |
| 711 | MadeChange = true; |
| 712 | |
| 713 | TruncTheUse = InsertedTrunc; |
| 714 | } |
| 715 | } |
| 716 | return MadeChange; |
| 717 | } |
| 718 | |
| 719 | /// OptimizeExtractBits - sink the shift *right* instruction into user blocks if |
| 720 | /// the uses could potentially be combined with this shift instruction and |
| 721 | /// generate BitExtract instruction. It will only be applied if the architecture |
| 722 | /// supports BitExtract instruction. Here is an example: |
| 723 | /// BB1: |
| 724 | /// %x.extract.shift = lshr i64 %arg1, 32 |
| 725 | /// BB2: |
| 726 | /// %x.extract.trunc = trunc i64 %x.extract.shift to i16 |
| 727 | /// ==> |
| 728 | /// |
| 729 | /// BB2: |
| 730 | /// %x.extract.shift.1 = lshr i64 %arg1, 32 |
| 731 | /// %x.extract.trunc = trunc i64 %x.extract.shift.1 to i16 |
| 732 | /// |
| 733 | /// CodeGen will recoginze the pattern in BB2 and generate BitExtract |
| 734 | /// instruction. |
| 735 | /// Return true if any changes are made. |
| 736 | static bool OptimizeExtractBits(BinaryOperator *ShiftI, ConstantInt *CI, |
| 737 | const TargetLowering &TLI) { |
| 738 | BasicBlock *DefBB = ShiftI->getParent(); |
| 739 | |
| 740 | /// Only insert instructions in each block once. |
| 741 | DenseMap<BasicBlock *, BinaryOperator *> InsertedShifts; |
| 742 | |
| 743 | bool shiftIsLegal = TLI.isTypeLegal(TLI.getValueType(ShiftI->getType())); |
| 744 | |
| 745 | bool MadeChange = false; |
| 746 | for (Value::user_iterator UI = ShiftI->user_begin(), E = ShiftI->user_end(); |
| 747 | UI != E;) { |
| 748 | Use &TheUse = UI.getUse(); |
| 749 | Instruction *User = cast<Instruction>(*UI); |
| 750 | // Preincrement use iterator so we don't invalidate it. |
| 751 | ++UI; |
| 752 | |
| 753 | // Don't bother for PHI nodes. |
| 754 | if (isa<PHINode>(User)) |
| 755 | continue; |
| 756 | |
| 757 | if (!isExtractBitsCandidateUse(User)) |
| 758 | continue; |
| 759 | |
| 760 | BasicBlock *UserBB = User->getParent(); |
| 761 | |
| 762 | if (UserBB == DefBB) { |
| 763 | // If the shift and truncate instruction are in the same BB. The use of |
| 764 | // the truncate(TruncUse) may still introduce another truncate if not |
| 765 | // legal. In this case, we would like to sink both shift and truncate |
| 766 | // instruction to the BB of TruncUse. |
| 767 | // for example: |
| 768 | // BB1: |
| 769 | // i64 shift.result = lshr i64 opnd, imm |
| 770 | // trunc.result = trunc shift.result to i16 |
| 771 | // |
| 772 | // BB2: |
| 773 | // ----> We will have an implicit truncate here if the architecture does |
| 774 | // not have i16 compare. |
| 775 | // cmp i16 trunc.result, opnd2 |
| 776 | // |
| 777 | if (isa<TruncInst>(User) && shiftIsLegal |
| 778 | // If the type of the truncate is legal, no trucate will be |
| 779 | // introduced in other basic blocks. |
| 780 | && (!TLI.isTypeLegal(TLI.getValueType(User->getType())))) |
| 781 | MadeChange = |
| 782 | SinkShiftAndTruncate(ShiftI, User, CI, InsertedShifts, TLI); |
| 783 | |
| 784 | continue; |
| 785 | } |
| 786 | // If we have already inserted a shift into this block, use it. |
| 787 | BinaryOperator *&InsertedShift = InsertedShifts[UserBB]; |
| 788 | |
| 789 | if (!InsertedShift) { |
| 790 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
| 791 | |
| 792 | if (ShiftI->getOpcode() == Instruction::AShr) |
| 793 | InsertedShift = |
| 794 | BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, "", InsertPt); |
| 795 | else |
| 796 | InsertedShift = |
| 797 | BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, "", InsertPt); |
| 798 | |
| 799 | MadeChange = true; |
| 800 | } |
| 801 | |
| 802 | // Replace a use of the shift with a use of the new shift. |
| 803 | TheUse = InsertedShift; |
| 804 | } |
| 805 | |
| 806 | // If we removed all uses, nuke the shift. |
| 807 | if (ShiftI->use_empty()) |
| 808 | ShiftI->eraseFromParent(); |
| 809 | |
| 810 | return MadeChange; |
| 811 | } |
| 812 | |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 813 | namespace { |
| 814 | class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls { |
| 815 | protected: |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 816 | void replaceCall(Value *With) override { |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 817 | CI->replaceAllUsesWith(With); |
| 818 | CI->eraseFromParent(); |
| 819 | } |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 820 | bool isFoldable(unsigned SizeCIOp, unsigned, bool) const override { |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 821 | if (ConstantInt *SizeCI = |
| 822 | dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) |
| 823 | return SizeCI->isAllOnesValue(); |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 824 | return false; |
| 825 | } |
| 826 | }; |
| 827 | } // end anonymous namespace |
| 828 | |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 829 | bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) { |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 830 | BasicBlock *BB = CI->getParent(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 831 | |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 832 | // Lower inline assembly if we can. |
| 833 | // If we found an inline asm expession, and if the target knows how to |
| 834 | // lower it to normal LLVM code, do so now. |
| 835 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) { |
| 836 | if (TLI->ExpandInlineAsm(CI)) { |
| 837 | // Avoid invalidating the iterator. |
| 838 | CurInstIterator = BB->begin(); |
| 839 | // Avoid processing instructions out of order, which could cause |
| 840 | // reuse before a value is defined. |
| 841 | SunkAddrs.clear(); |
| 842 | return true; |
| 843 | } |
| 844 | // Sink address computing for memory operands into the block. |
| 845 | if (OptimizeInlineAsmInst(CI)) |
| 846 | return true; |
| 847 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 848 | |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 849 | // Lower all uses of llvm.objectsize.* |
| 850 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); |
| 851 | if (II && II->getIntrinsicID() == Intrinsic::objectsize) { |
Gabor Greif | 4a39b84 | 2010-06-24 00:44:01 +0000 | [diff] [blame] | 852 | bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 853 | Type *ReturnTy = CI->getType(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 854 | Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); |
| 855 | |
Chris Lattner | 1b93be5 | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 856 | // Substituting this can cause recursive simplifications, which can |
| 857 | // invalidate our iterator. Use a WeakVH to hold onto it in case this |
| 858 | // happens. |
| 859 | WeakVH IterHandle(CurInstIterator); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 860 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 861 | replaceAndRecursivelySimplify(CI, RetVal, |
| 862 | TLI ? TLI->getDataLayout() : nullptr, |
| 863 | TLInfo, ModifiedDT ? nullptr : DT); |
Chris Lattner | 1b93be5 | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 864 | |
| 865 | // If the iterator instruction was recursively deleted, start over at the |
| 866 | // start of the block. |
Chris Lattner | 86d56c6 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 867 | if (IterHandle != CurInstIterator) { |
Chris Lattner | 1b93be5 | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 868 | CurInstIterator = BB->begin(); |
Chris Lattner | 86d56c6 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 869 | SunkAddrs.clear(); |
| 870 | } |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 871 | return true; |
| 872 | } |
Michael Zolotukhin | f2ba994 | 2014-04-21 12:01:33 +0000 | [diff] [blame] | 873 | // Lower all uses of llvm.safe.[us]{div|rem}... |
| 874 | if (II && |
| 875 | (II->getIntrinsicID() == Intrinsic::safe_sdiv || |
| 876 | II->getIntrinsicID() == Intrinsic::safe_udiv || |
| 877 | II->getIntrinsicID() == Intrinsic::safe_srem || |
| 878 | II->getIntrinsicID() == Intrinsic::safe_urem)) { |
| 879 | // Given |
| 880 | // result_struct = type {iN, i1} |
| 881 | // %R = call result_struct llvm.safe.sdiv.iN(iN %x, iN %y) |
| 882 | // Expand it to actual IR, which produces result to the same variable %R. |
| 883 | // First element of the result %R.1 is the result of division, second |
| 884 | // element shows whether the division was correct or not. |
| 885 | // If %y is 0, %R.1 is 0, %R.2 is 1. (1) |
| 886 | // If %x is minSignedValue and %y is -1, %R.1 is %x, %R.2 is 1. (2) |
| 887 | // In other cases %R.1 is (sdiv %x, %y), %R.2 is 0. (3) |
| 888 | // |
| 889 | // Similar applies to srem, udiv, and urem builtins, except that in unsigned |
| 890 | // variants we don't check condition (2). |
| 891 | |
| 892 | bool IsSigned; |
| 893 | BinaryOperator::BinaryOps Op; |
| 894 | switch (II->getIntrinsicID()) { |
| 895 | case Intrinsic::safe_sdiv: |
| 896 | IsSigned = true; |
| 897 | Op = Instruction::SDiv; |
| 898 | break; |
| 899 | case Intrinsic::safe_udiv: |
| 900 | IsSigned = false; |
| 901 | Op = Instruction::UDiv; |
| 902 | break; |
| 903 | case Intrinsic::safe_srem: |
| 904 | IsSigned = true; |
| 905 | Op = Instruction::SRem; |
| 906 | break; |
| 907 | case Intrinsic::safe_urem: |
| 908 | IsSigned = false; |
| 909 | Op = Instruction::URem; |
| 910 | break; |
| 911 | default: |
| 912 | llvm_unreachable("Only Div/Rem intrinsics are handled here."); |
| 913 | } |
| 914 | |
| 915 | Value *LHS = II->getOperand(0), *RHS = II->getOperand(1); |
| 916 | bool DivWellDefined = TLI && TLI->isDivWellDefined(); |
| 917 | |
| 918 | bool ResultNeeded[2] = {false, false}; |
| 919 | SmallVector<User*, 1> ResultsUsers[2]; |
| 920 | bool BadCase = false; |
| 921 | for (User *U: II->users()) { |
| 922 | ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U); |
| 923 | if (!EVI || EVI->getNumIndices() > 1 || EVI->getIndices()[0] > 1) { |
| 924 | BadCase = true; |
| 925 | break; |
| 926 | } |
| 927 | ResultNeeded[EVI->getIndices()[0]] = true; |
| 928 | ResultsUsers[EVI->getIndices()[0]].push_back(U); |
| 929 | } |
| 930 | // Behave conservatively, if there is an unusual user of the results. |
| 931 | if (BadCase) |
| 932 | ResultNeeded[0] = ResultNeeded[1] = true; |
| 933 | |
| 934 | // Early exit if non of the results is ever used. |
| 935 | if (!ResultNeeded[0] && !ResultNeeded[1]) { |
| 936 | II->eraseFromParent(); |
| 937 | return true; |
| 938 | } |
| 939 | |
| 940 | // Early exit if the second result (flag) isn't used and target |
| 941 | // div-instruction computes exactly what we want to get as the first result |
| 942 | // and never traps. |
| 943 | if (ResultNeeded[0] && !ResultNeeded[1] && DivWellDefined) { |
| 944 | BinaryOperator *Div = BinaryOperator::Create(Op, LHS, RHS); |
| 945 | Div->insertAfter(II); |
| 946 | for (User *U: ResultsUsers[0]) { |
| 947 | Instruction *UserInst = dyn_cast<Instruction>(U); |
| 948 | assert(UserInst && "Unexpected null-instruction"); |
| 949 | UserInst->replaceAllUsesWith(Div); |
| 950 | UserInst->eraseFromParent(); |
| 951 | } |
| 952 | II->eraseFromParent(); |
| 953 | CurInstIterator = Div; |
| 954 | ModifiedDT = true; |
| 955 | return true; |
| 956 | } |
| 957 | |
| 958 | Value *MinusOne = Constant::getAllOnesValue(LHS->getType()); |
| 959 | Value *Zero = Constant::getNullValue(LHS->getType()); |
| 960 | |
| 961 | // Split the original BB and create other basic blocks that will be used |
| 962 | // for checks. |
| 963 | BasicBlock *StartBB = II->getParent(); |
| 964 | BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(II)); |
| 965 | BasicBlock *NextBB = StartBB->splitBasicBlock(SplitPt, "div.end"); |
| 966 | |
| 967 | BasicBlock *DivByZeroBB; |
| 968 | DivByZeroBB = BasicBlock::Create(II->getContext(), "div.divz", |
| 969 | NextBB->getParent(), NextBB); |
| 970 | BranchInst::Create(NextBB, DivByZeroBB); |
| 971 | BasicBlock *DivBB = BasicBlock::Create(II->getContext(), "div.div", |
| 972 | NextBB->getParent(), NextBB); |
| 973 | BranchInst::Create(NextBB, DivBB); |
| 974 | |
| 975 | // For signed variants, check the condition (2): |
| 976 | // LHS == SignedMinValue, RHS == -1. |
| 977 | Value *CmpMinusOne; |
| 978 | Value *CmpMinValue; |
| 979 | BasicBlock *ChkDivMinBB; |
| 980 | BasicBlock *DivMinBB; |
| 981 | Value *MinValue; |
| 982 | if (IsSigned) { |
| 983 | APInt SignedMinValue = |
| 984 | APInt::getSignedMinValue(LHS->getType()->getPrimitiveSizeInBits()); |
| 985 | MinValue = Constant::getIntegerValue(LHS->getType(), SignedMinValue); |
| 986 | ChkDivMinBB = BasicBlock::Create(II->getContext(), "div.chkdivmin", |
| 987 | NextBB->getParent(), NextBB); |
| 988 | BranchInst::Create(NextBB, ChkDivMinBB); |
| 989 | DivMinBB = BasicBlock::Create(II->getContext(), "div.divmin", |
| 990 | NextBB->getParent(), NextBB); |
| 991 | BranchInst::Create(NextBB, DivMinBB); |
| 992 | CmpMinusOne = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, |
| 993 | RHS, MinusOne, "cmp.rhs.minus.one", |
| 994 | ChkDivMinBB->getTerminator()); |
| 995 | CmpMinValue = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, |
| 996 | LHS, MinValue, "cmp.lhs.signed.min", |
| 997 | ChkDivMinBB->getTerminator()); |
| 998 | BinaryOperator *CmpSignedOvf = BinaryOperator::Create(Instruction::And, |
| 999 | CmpMinusOne, |
| 1000 | CmpMinValue); |
| 1001 | // Here we're interested in the case when both %x is TMin and %y is -1. |
| 1002 | // In this case the result will overflow. |
| 1003 | // If that's not the case, we can perform usual division. These blocks |
| 1004 | // will be inserted after DivByZero, so the division will be safe. |
| 1005 | CmpSignedOvf->insertBefore(ChkDivMinBB->getTerminator()); |
| 1006 | BranchInst::Create(DivMinBB, DivBB, CmpSignedOvf, |
| 1007 | ChkDivMinBB->getTerminator()); |
| 1008 | ChkDivMinBB->getTerminator()->eraseFromParent(); |
| 1009 | } |
| 1010 | |
| 1011 | // Check the condition (1): |
| 1012 | // RHS == 0. |
| 1013 | Value *CmpDivZero = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, |
| 1014 | RHS, Zero, "cmp.rhs.zero", |
| 1015 | StartBB->getTerminator()); |
| 1016 | |
| 1017 | // If RHS != 0, we want to check condition (2) in signed case, or proceed |
| 1018 | // to usual division in unsigned case. |
| 1019 | BranchInst::Create(DivByZeroBB, IsSigned ? ChkDivMinBB : DivBB, CmpDivZero, |
| 1020 | StartBB->getTerminator()); |
| 1021 | StartBB->getTerminator()->eraseFromParent(); |
| 1022 | |
| 1023 | // At the moment we have all the control flow created. We just need to |
| 1024 | // insert DIV and PHI (if needed) to get the result value. |
| 1025 | Instruction *DivRes, *FlagRes; |
| 1026 | Instruction *InsPoint = nullptr; |
| 1027 | if (ResultNeeded[0]) { |
| 1028 | BinaryOperator *Div = BinaryOperator::Create(Op, LHS, RHS); |
| 1029 | if (DivWellDefined) { |
| 1030 | // The result value is the result of DIV operation placed right at the |
| 1031 | // original place of the intrinsic. |
| 1032 | Div->insertAfter(II); |
| 1033 | DivRes = Div; |
| 1034 | } else { |
| 1035 | // The result is a PHI-node. |
| 1036 | Div->insertBefore(DivBB->getTerminator()); |
| 1037 | PHINode *DivResPN = |
| 1038 | PHINode::Create(LHS->getType(), IsSigned ? 3 : 2, "div.res.phi", |
| 1039 | NextBB->begin()); |
| 1040 | DivResPN->addIncoming(Div, DivBB); |
| 1041 | DivResPN->addIncoming(Zero, DivByZeroBB); |
| 1042 | if (IsSigned) |
| 1043 | DivResPN->addIncoming(MinValue, DivMinBB); |
| 1044 | DivRes = DivResPN; |
| 1045 | InsPoint = DivResPN; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | // Prepare a value for the second result (flag) if it is needed. |
| 1050 | if (ResultNeeded[1]) { |
| 1051 | Type *FlagTy = II->getType()->getStructElementType(1); |
| 1052 | PHINode *FlagResPN = |
| 1053 | PHINode::Create(FlagTy, IsSigned ? 3 : 2, "div.flag.phi", |
| 1054 | NextBB->begin()); |
| 1055 | FlagResPN->addIncoming(Constant::getNullValue(FlagTy), DivBB); |
| 1056 | FlagResPN->addIncoming(Constant::getAllOnesValue(FlagTy), DivByZeroBB); |
| 1057 | if (IsSigned) |
| 1058 | FlagResPN->addIncoming(Constant::getAllOnesValue(FlagTy), DivMinBB); |
| 1059 | FlagRes = FlagResPN; |
| 1060 | if (!InsPoint) |
| 1061 | InsPoint = FlagRes; |
| 1062 | } |
| 1063 | |
| 1064 | // If possible, propagate the results to the user. Otherwise, create alloca, |
| 1065 | // and create a struct with the results on stack. |
| 1066 | if (!BadCase) { |
| 1067 | if (ResultNeeded[0]) { |
| 1068 | for (User *U: ResultsUsers[0]) { |
| 1069 | Instruction *UserInst = dyn_cast<Instruction>(U); |
| 1070 | assert(UserInst && "Unexpected null-instruction"); |
| 1071 | UserInst->replaceAllUsesWith(DivRes); |
| 1072 | UserInst->eraseFromParent(); |
| 1073 | } |
| 1074 | } |
| 1075 | if (ResultNeeded[1]) { |
| 1076 | for (User *FlagU: ResultsUsers[1]) { |
| 1077 | Instruction *FlagUInst = dyn_cast<Instruction>(FlagU); |
| 1078 | FlagUInst->replaceAllUsesWith(FlagRes); |
| 1079 | FlagUInst->eraseFromParent(); |
| 1080 | } |
| 1081 | } |
| 1082 | } else { |
| 1083 | // Create alloca, store our new values to it, and then load the final |
| 1084 | // result from it. |
| 1085 | Constant *Idx0 = ConstantInt::get(Type::getInt32Ty(II->getContext()), 0); |
| 1086 | Constant *Idx1 = ConstantInt::get(Type::getInt32Ty(II->getContext()), 1); |
| 1087 | Value *Idxs_DivRes[2] = {Idx0, Idx0}; |
| 1088 | Value *Idxs_FlagRes[2] = {Idx0, Idx1}; |
| 1089 | Value *NewRes = new llvm::AllocaInst(II->getType(), 0, "div.res.ptr", II); |
| 1090 | Instruction *ResDivAddr = GetElementPtrInst::Create(NewRes, Idxs_DivRes); |
| 1091 | Instruction *ResFlagAddr = |
| 1092 | GetElementPtrInst::Create(NewRes, Idxs_FlagRes); |
| 1093 | ResDivAddr->insertAfter(InsPoint); |
| 1094 | ResFlagAddr->insertAfter(ResDivAddr); |
| 1095 | StoreInst *StoreResDiv = new StoreInst(DivRes, ResDivAddr); |
| 1096 | StoreInst *StoreResFlag = new StoreInst(FlagRes, ResFlagAddr); |
| 1097 | StoreResDiv->insertAfter(ResFlagAddr); |
| 1098 | StoreResFlag->insertAfter(StoreResDiv); |
| 1099 | LoadInst *LoadRes = new LoadInst(NewRes, "div.res"); |
| 1100 | LoadRes->insertAfter(StoreResFlag); |
| 1101 | II->replaceAllUsesWith(LoadRes); |
| 1102 | } |
| 1103 | |
| 1104 | II->eraseFromParent(); |
| 1105 | CurInstIterator = StartBB->end(); |
| 1106 | ModifiedDT = true; |
| 1107 | return true; |
| 1108 | } |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1109 | |
Pete Cooper | 615fd89 | 2012-03-13 20:59:56 +0000 | [diff] [blame] | 1110 | if (II && TLI) { |
| 1111 | SmallVector<Value*, 2> PtrOps; |
| 1112 | Type *AccessTy; |
| 1113 | if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy)) |
| 1114 | while (!PtrOps.empty()) |
| 1115 | if (OptimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy)) |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1119 | // From here on out we're working with named functions. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1120 | if (!CI->getCalledFunction()) return false; |
Devang Patel | 0da5250 | 2011-05-26 21:51:06 +0000 | [diff] [blame] | 1121 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1122 | // We'll need DataLayout from here on out. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1123 | const DataLayout *TD = TLI ? TLI->getDataLayout() : nullptr; |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1124 | if (!TD) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1125 | |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 1126 | // Lower all default uses of _chk calls. This is very similar |
| 1127 | // to what InstCombineCalls does, but here we are only lowering calls |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1128 | // that have the default "don't know" as the objectsize. Anything else |
| 1129 | // should be left alone. |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 1130 | CodeGenPrepareFortifiedLibCalls Simplifier; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 1131 | return Simplifier.fold(CI, TD, TLInfo); |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1132 | } |
Chris Lattner | 1b93be5 | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 1133 | |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1134 | /// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return |
| 1135 | /// instructions to the predecessor to enable tail call optimizations. The |
| 1136 | /// case it is currently looking for is: |
Dmitri Gribenko | 2bc1d48 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 1137 | /// @code |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1138 | /// bb0: |
| 1139 | /// %tmp0 = tail call i32 @f0() |
| 1140 | /// br label %return |
| 1141 | /// bb1: |
| 1142 | /// %tmp1 = tail call i32 @f1() |
| 1143 | /// br label %return |
| 1144 | /// bb2: |
| 1145 | /// %tmp2 = tail call i32 @f2() |
| 1146 | /// br label %return |
| 1147 | /// return: |
| 1148 | /// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ] |
| 1149 | /// ret i32 %retval |
Dmitri Gribenko | 2bc1d48 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 1150 | /// @endcode |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1151 | /// |
| 1152 | /// => |
| 1153 | /// |
Dmitri Gribenko | 2bc1d48 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 1154 | /// @code |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1155 | /// bb0: |
| 1156 | /// %tmp0 = tail call i32 @f0() |
| 1157 | /// ret i32 %tmp0 |
| 1158 | /// bb1: |
| 1159 | /// %tmp1 = tail call i32 @f1() |
| 1160 | /// ret i32 %tmp1 |
| 1161 | /// bb2: |
| 1162 | /// %tmp2 = tail call i32 @f2() |
| 1163 | /// ret i32 %tmp2 |
Dmitri Gribenko | 2bc1d48 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 1164 | /// @endcode |
Benjamin Kramer | 455fa35 | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 1165 | bool CodeGenPrepare::DupRetToEnableTailCallOpts(BasicBlock *BB) { |
Cameron Zwarich | 47e7175 | 2011-03-24 04:51:51 +0000 | [diff] [blame] | 1166 | if (!TLI) |
| 1167 | return false; |
| 1168 | |
Benjamin Kramer | 455fa35 | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 1169 | ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); |
| 1170 | if (!RI) |
| 1171 | return false; |
| 1172 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1173 | PHINode *PN = nullptr; |
| 1174 | BitCastInst *BCI = nullptr; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1175 | Value *V = RI->getReturnValue(); |
Evan Cheng | 249716e | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 1176 | if (V) { |
| 1177 | BCI = dyn_cast<BitCastInst>(V); |
| 1178 | if (BCI) |
| 1179 | V = BCI->getOperand(0); |
| 1180 | |
| 1181 | PN = dyn_cast<PHINode>(V); |
| 1182 | if (!PN) |
| 1183 | return false; |
| 1184 | } |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1185 | |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1186 | if (PN && PN->getParent() != BB) |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1187 | return false; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1188 | |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1189 | // It's not safe to eliminate the sign / zero extension of the return value. |
| 1190 | // See llvm::isInTailCallPosition(). |
| 1191 | const Function *F = BB->getParent(); |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1192 | AttributeSet CallerAttrs = F->getAttributes(); |
| 1193 | if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) || |
| 1194 | CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt)) |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1195 | return false; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1196 | |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1197 | // Make sure there are no instructions between the PHI and return, or that the |
| 1198 | // return is the first instruction in the block. |
| 1199 | if (PN) { |
| 1200 | BasicBlock::iterator BI = BB->begin(); |
| 1201 | do { ++BI; } while (isa<DbgInfoIntrinsic>(BI)); |
Evan Cheng | 249716e | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 1202 | if (&*BI == BCI) |
| 1203 | // Also skip over the bitcast. |
| 1204 | ++BI; |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1205 | if (&*BI != RI) |
| 1206 | return false; |
| 1207 | } else { |
Cameron Zwarich | 74157ab | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 1208 | BasicBlock::iterator BI = BB->begin(); |
| 1209 | while (isa<DbgInfoIntrinsic>(BI)) ++BI; |
| 1210 | if (&*BI != RI) |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1211 | return false; |
| 1212 | } |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1213 | |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1214 | /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail |
| 1215 | /// call. |
| 1216 | SmallVector<CallInst*, 4> TailCalls; |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1217 | if (PN) { |
| 1218 | for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) { |
| 1219 | CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I)); |
| 1220 | // Make sure the phi value is indeed produced by the tail call. |
| 1221 | if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) && |
| 1222 | TLI->mayBeEmittedAsTailCall(CI)) |
| 1223 | TailCalls.push_back(CI); |
| 1224 | } |
| 1225 | } else { |
| 1226 | SmallPtrSet<BasicBlock*, 4> VisitedBBs; |
| 1227 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { |
| 1228 | if (!VisitedBBs.insert(*PI)) |
| 1229 | continue; |
| 1230 | |
| 1231 | BasicBlock::InstListType &InstList = (*PI)->getInstList(); |
| 1232 | BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin(); |
| 1233 | BasicBlock::InstListType::reverse_iterator RE = InstList.rend(); |
Cameron Zwarich | 74157ab | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 1234 | do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI)); |
| 1235 | if (RI == RE) |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1236 | continue; |
Cameron Zwarich | 74157ab | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 1237 | |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1238 | CallInst *CI = dyn_cast<CallInst>(&*RI); |
Cameron Zwarich | 2edfe77 | 2011-03-24 15:54:11 +0000 | [diff] [blame] | 1239 | if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI)) |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1240 | TailCalls.push_back(CI); |
| 1241 | } |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1244 | bool Changed = false; |
| 1245 | for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) { |
| 1246 | CallInst *CI = TailCalls[i]; |
| 1247 | CallSite CS(CI); |
| 1248 | |
| 1249 | // Conservatively require the attributes of the call to match those of the |
| 1250 | // return. Ignore noalias because it doesn't affect the call sequence. |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1251 | AttributeSet CalleeAttrs = CS.getAttributes(); |
| 1252 | if (AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex). |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1253 | removeAttribute(Attribute::NoAlias) != |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1254 | AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex). |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1255 | removeAttribute(Attribute::NoAlias)) |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1256 | continue; |
| 1257 | |
| 1258 | // Make sure the call instruction is followed by an unconditional branch to |
| 1259 | // the return block. |
| 1260 | BasicBlock *CallBB = CI->getParent(); |
| 1261 | BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator()); |
| 1262 | if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB) |
| 1263 | continue; |
| 1264 | |
| 1265 | // Duplicate the return into CallBB. |
| 1266 | (void)FoldReturnIntoUncondBranch(RI, BB, CallBB); |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 1267 | ModifiedDT = Changed = true; |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1268 | ++NumRetsDup; |
| 1269 | } |
| 1270 | |
| 1271 | // If we eliminated all predecessors of the block, delete the block now. |
Evan Cheng | 64a223a | 2012-09-28 23:58:57 +0000 | [diff] [blame] | 1272 | if (Changed && !BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB)) |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1273 | BB->eraseFromParent(); |
| 1274 | |
| 1275 | return Changed; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Chris Lattner | 728f902 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1278 | //===----------------------------------------------------------------------===// |
Chris Lattner | 728f902 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1279 | // Memory Optimization |
| 1280 | //===----------------------------------------------------------------------===// |
| 1281 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1282 | namespace { |
| 1283 | |
| 1284 | /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode |
| 1285 | /// which holds actual Value*'s for register values. |
Chandler Carruth | 95f83e0 | 2013-01-07 15:14:13 +0000 | [diff] [blame] | 1286 | struct ExtAddrMode : public TargetLowering::AddrMode { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1287 | Value *BaseReg; |
| 1288 | Value *ScaledReg; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1289 | ExtAddrMode() : BaseReg(nullptr), ScaledReg(nullptr) {} |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1290 | void print(raw_ostream &OS) const; |
| 1291 | void dump() const; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1292 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1293 | bool operator==(const ExtAddrMode& O) const { |
| 1294 | return (BaseReg == O.BaseReg) && (ScaledReg == O.ScaledReg) && |
| 1295 | (BaseGV == O.BaseGV) && (BaseOffs == O.BaseOffs) && |
| 1296 | (HasBaseReg == O.HasBaseReg) && (Scale == O.Scale); |
| 1297 | } |
| 1298 | }; |
| 1299 | |
Eli Friedman | c1f1f85 | 2013-09-10 23:09:24 +0000 | [diff] [blame] | 1300 | #ifndef NDEBUG |
| 1301 | static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) { |
| 1302 | AM.print(OS); |
| 1303 | return OS; |
| 1304 | } |
| 1305 | #endif |
| 1306 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1307 | void ExtAddrMode::print(raw_ostream &OS) const { |
| 1308 | bool NeedPlus = false; |
| 1309 | OS << "["; |
| 1310 | if (BaseGV) { |
| 1311 | OS << (NeedPlus ? " + " : "") |
| 1312 | << "GV:"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1313 | BaseGV->printAsOperand(OS, /*PrintType=*/false); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1314 | NeedPlus = true; |
| 1315 | } |
| 1316 | |
| 1317 | if (BaseOffs) |
| 1318 | OS << (NeedPlus ? " + " : "") << BaseOffs, NeedPlus = true; |
| 1319 | |
| 1320 | if (BaseReg) { |
| 1321 | OS << (NeedPlus ? " + " : "") |
| 1322 | << "Base:"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1323 | BaseReg->printAsOperand(OS, /*PrintType=*/false); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1324 | NeedPlus = true; |
| 1325 | } |
| 1326 | if (Scale) { |
| 1327 | OS << (NeedPlus ? " + " : "") |
| 1328 | << Scale << "*"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1329 | ScaledReg->printAsOperand(OS, /*PrintType=*/false); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | OS << ']'; |
| 1333 | } |
| 1334 | |
| 1335 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 1336 | void ExtAddrMode::dump() const { |
| 1337 | print(dbgs()); |
| 1338 | dbgs() << '\n'; |
| 1339 | } |
| 1340 | #endif |
| 1341 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1342 | /// \brief This class provides transaction based operation on the IR. |
| 1343 | /// Every change made through this class is recorded in the internal state and |
| 1344 | /// can be undone (rollback) until commit is called. |
| 1345 | class TypePromotionTransaction { |
| 1346 | |
| 1347 | /// \brief This represents the common interface of the individual transaction. |
| 1348 | /// Each class implements the logic for doing one specific modification on |
| 1349 | /// the IR via the TypePromotionTransaction. |
| 1350 | class TypePromotionAction { |
| 1351 | protected: |
| 1352 | /// The Instruction modified. |
| 1353 | Instruction *Inst; |
| 1354 | |
| 1355 | public: |
| 1356 | /// \brief Constructor of the action. |
| 1357 | /// The constructor performs the related action on the IR. |
| 1358 | TypePromotionAction(Instruction *Inst) : Inst(Inst) {} |
| 1359 | |
| 1360 | virtual ~TypePromotionAction() {} |
| 1361 | |
| 1362 | /// \brief Undo the modification done by this action. |
| 1363 | /// When this method is called, the IR must be in the same state as it was |
| 1364 | /// before this action was applied. |
| 1365 | /// \pre Undoing the action works if and only if the IR is in the exact same |
| 1366 | /// state as it was directly after this action was applied. |
| 1367 | virtual void undo() = 0; |
| 1368 | |
| 1369 | /// \brief Advocate every change made by this action. |
| 1370 | /// When the results on the IR of the action are to be kept, it is important |
| 1371 | /// to call this function, otherwise hidden information may be kept forever. |
| 1372 | virtual void commit() { |
| 1373 | // Nothing to be done, this action is not doing anything. |
| 1374 | } |
| 1375 | }; |
| 1376 | |
| 1377 | /// \brief Utility to remember the position of an instruction. |
| 1378 | class InsertionHandler { |
| 1379 | /// Position of an instruction. |
| 1380 | /// Either an instruction: |
| 1381 | /// - Is the first in a basic block: BB is used. |
| 1382 | /// - Has a previous instructon: PrevInst is used. |
| 1383 | union { |
| 1384 | Instruction *PrevInst; |
| 1385 | BasicBlock *BB; |
| 1386 | } Point; |
| 1387 | /// Remember whether or not the instruction had a previous instruction. |
| 1388 | bool HasPrevInstruction; |
| 1389 | |
| 1390 | public: |
| 1391 | /// \brief Record the position of \p Inst. |
| 1392 | InsertionHandler(Instruction *Inst) { |
| 1393 | BasicBlock::iterator It = Inst; |
| 1394 | HasPrevInstruction = (It != (Inst->getParent()->begin())); |
| 1395 | if (HasPrevInstruction) |
| 1396 | Point.PrevInst = --It; |
| 1397 | else |
| 1398 | Point.BB = Inst->getParent(); |
| 1399 | } |
| 1400 | |
| 1401 | /// \brief Insert \p Inst at the recorded position. |
| 1402 | void insert(Instruction *Inst) { |
| 1403 | if (HasPrevInstruction) { |
| 1404 | if (Inst->getParent()) |
| 1405 | Inst->removeFromParent(); |
| 1406 | Inst->insertAfter(Point.PrevInst); |
| 1407 | } else { |
| 1408 | Instruction *Position = Point.BB->getFirstInsertionPt(); |
| 1409 | if (Inst->getParent()) |
| 1410 | Inst->moveBefore(Position); |
| 1411 | else |
| 1412 | Inst->insertBefore(Position); |
| 1413 | } |
| 1414 | } |
| 1415 | }; |
| 1416 | |
| 1417 | /// \brief Move an instruction before another. |
| 1418 | class InstructionMoveBefore : public TypePromotionAction { |
| 1419 | /// Original position of the instruction. |
| 1420 | InsertionHandler Position; |
| 1421 | |
| 1422 | public: |
| 1423 | /// \brief Move \p Inst before \p Before. |
| 1424 | InstructionMoveBefore(Instruction *Inst, Instruction *Before) |
| 1425 | : TypePromotionAction(Inst), Position(Inst) { |
| 1426 | DEBUG(dbgs() << "Do: move: " << *Inst << "\nbefore: " << *Before << "\n"); |
| 1427 | Inst->moveBefore(Before); |
| 1428 | } |
| 1429 | |
| 1430 | /// \brief Move the instruction back to its original position. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1431 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1432 | DEBUG(dbgs() << "Undo: moveBefore: " << *Inst << "\n"); |
| 1433 | Position.insert(Inst); |
| 1434 | } |
| 1435 | }; |
| 1436 | |
| 1437 | /// \brief Set the operand of an instruction with a new value. |
| 1438 | class OperandSetter : public TypePromotionAction { |
| 1439 | /// Original operand of the instruction. |
| 1440 | Value *Origin; |
| 1441 | /// Index of the modified instruction. |
| 1442 | unsigned Idx; |
| 1443 | |
| 1444 | public: |
| 1445 | /// \brief Set \p Idx operand of \p Inst with \p NewVal. |
| 1446 | OperandSetter(Instruction *Inst, unsigned Idx, Value *NewVal) |
| 1447 | : TypePromotionAction(Inst), Idx(Idx) { |
| 1448 | DEBUG(dbgs() << "Do: setOperand: " << Idx << "\n" |
| 1449 | << "for:" << *Inst << "\n" |
| 1450 | << "with:" << *NewVal << "\n"); |
| 1451 | Origin = Inst->getOperand(Idx); |
| 1452 | Inst->setOperand(Idx, NewVal); |
| 1453 | } |
| 1454 | |
| 1455 | /// \brief Restore the original value of the instruction. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1456 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1457 | DEBUG(dbgs() << "Undo: setOperand:" << Idx << "\n" |
| 1458 | << "for: " << *Inst << "\n" |
| 1459 | << "with: " << *Origin << "\n"); |
| 1460 | Inst->setOperand(Idx, Origin); |
| 1461 | } |
| 1462 | }; |
| 1463 | |
| 1464 | /// \brief Hide the operands of an instruction. |
| 1465 | /// Do as if this instruction was not using any of its operands. |
| 1466 | class OperandsHider : public TypePromotionAction { |
| 1467 | /// The list of original operands. |
| 1468 | SmallVector<Value *, 4> OriginalValues; |
| 1469 | |
| 1470 | public: |
| 1471 | /// \brief Remove \p Inst from the uses of the operands of \p Inst. |
| 1472 | OperandsHider(Instruction *Inst) : TypePromotionAction(Inst) { |
| 1473 | DEBUG(dbgs() << "Do: OperandsHider: " << *Inst << "\n"); |
| 1474 | unsigned NumOpnds = Inst->getNumOperands(); |
| 1475 | OriginalValues.reserve(NumOpnds); |
| 1476 | for (unsigned It = 0; It < NumOpnds; ++It) { |
| 1477 | // Save the current operand. |
| 1478 | Value *Val = Inst->getOperand(It); |
| 1479 | OriginalValues.push_back(Val); |
| 1480 | // Set a dummy one. |
| 1481 | // We could use OperandSetter here, but that would implied an overhead |
| 1482 | // that we are not willing to pay. |
| 1483 | Inst->setOperand(It, UndefValue::get(Val->getType())); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /// \brief Restore the original list of uses. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1488 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1489 | DEBUG(dbgs() << "Undo: OperandsHider: " << *Inst << "\n"); |
| 1490 | for (unsigned It = 0, EndIt = OriginalValues.size(); It != EndIt; ++It) |
| 1491 | Inst->setOperand(It, OriginalValues[It]); |
| 1492 | } |
| 1493 | }; |
| 1494 | |
| 1495 | /// \brief Build a truncate instruction. |
| 1496 | class TruncBuilder : public TypePromotionAction { |
| 1497 | public: |
| 1498 | /// \brief Build a truncate instruction of \p Opnd producing a \p Ty |
| 1499 | /// result. |
| 1500 | /// trunc Opnd to Ty. |
| 1501 | TruncBuilder(Instruction *Opnd, Type *Ty) : TypePromotionAction(Opnd) { |
| 1502 | IRBuilder<> Builder(Opnd); |
| 1503 | Inst = cast<Instruction>(Builder.CreateTrunc(Opnd, Ty, "promoted")); |
| 1504 | DEBUG(dbgs() << "Do: TruncBuilder: " << *Inst << "\n"); |
| 1505 | } |
| 1506 | |
| 1507 | /// \brief Get the built instruction. |
| 1508 | Instruction *getBuiltInstruction() { return Inst; } |
| 1509 | |
| 1510 | /// \brief Remove the built instruction. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1511 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1512 | DEBUG(dbgs() << "Undo: TruncBuilder: " << *Inst << "\n"); |
| 1513 | Inst->eraseFromParent(); |
| 1514 | } |
| 1515 | }; |
| 1516 | |
| 1517 | /// \brief Build a sign extension instruction. |
| 1518 | class SExtBuilder : public TypePromotionAction { |
| 1519 | public: |
| 1520 | /// \brief Build a sign extension instruction of \p Opnd producing a \p Ty |
| 1521 | /// result. |
| 1522 | /// sext Opnd to Ty. |
| 1523 | SExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty) |
| 1524 | : TypePromotionAction(Inst) { |
| 1525 | IRBuilder<> Builder(InsertPt); |
| 1526 | Inst = cast<Instruction>(Builder.CreateSExt(Opnd, Ty, "promoted")); |
| 1527 | DEBUG(dbgs() << "Do: SExtBuilder: " << *Inst << "\n"); |
| 1528 | } |
| 1529 | |
| 1530 | /// \brief Get the built instruction. |
| 1531 | Instruction *getBuiltInstruction() { return Inst; } |
| 1532 | |
| 1533 | /// \brief Remove the built instruction. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1534 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1535 | DEBUG(dbgs() << "Undo: SExtBuilder: " << *Inst << "\n"); |
| 1536 | Inst->eraseFromParent(); |
| 1537 | } |
| 1538 | }; |
| 1539 | |
| 1540 | /// \brief Mutate an instruction to another type. |
| 1541 | class TypeMutator : public TypePromotionAction { |
| 1542 | /// Record the original type. |
| 1543 | Type *OrigTy; |
| 1544 | |
| 1545 | public: |
| 1546 | /// \brief Mutate the type of \p Inst into \p NewTy. |
| 1547 | TypeMutator(Instruction *Inst, Type *NewTy) |
| 1548 | : TypePromotionAction(Inst), OrigTy(Inst->getType()) { |
| 1549 | DEBUG(dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTy |
| 1550 | << "\n"); |
| 1551 | Inst->mutateType(NewTy); |
| 1552 | } |
| 1553 | |
| 1554 | /// \brief Mutate the instruction back to its original type. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1555 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1556 | DEBUG(dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTy |
| 1557 | << "\n"); |
| 1558 | Inst->mutateType(OrigTy); |
| 1559 | } |
| 1560 | }; |
| 1561 | |
| 1562 | /// \brief Replace the uses of an instruction by another instruction. |
| 1563 | class UsesReplacer : public TypePromotionAction { |
| 1564 | /// Helper structure to keep track of the replaced uses. |
| 1565 | struct InstructionAndIdx { |
| 1566 | /// The instruction using the instruction. |
| 1567 | Instruction *Inst; |
| 1568 | /// The index where this instruction is used for Inst. |
| 1569 | unsigned Idx; |
| 1570 | InstructionAndIdx(Instruction *Inst, unsigned Idx) |
| 1571 | : Inst(Inst), Idx(Idx) {} |
| 1572 | }; |
| 1573 | |
| 1574 | /// Keep track of the original uses (pair Instruction, Index). |
| 1575 | SmallVector<InstructionAndIdx, 4> OriginalUses; |
| 1576 | typedef SmallVectorImpl<InstructionAndIdx>::iterator use_iterator; |
| 1577 | |
| 1578 | public: |
| 1579 | /// \brief Replace all the use of \p Inst by \p New. |
| 1580 | UsesReplacer(Instruction *Inst, Value *New) : TypePromotionAction(Inst) { |
| 1581 | DEBUG(dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *New |
| 1582 | << "\n"); |
| 1583 | // Record the original uses. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1584 | for (Use &U : Inst->uses()) { |
| 1585 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| 1586 | OriginalUses.push_back(InstructionAndIdx(UserI, U.getOperandNo())); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1587 | } |
| 1588 | // Now, we can replace the uses. |
| 1589 | Inst->replaceAllUsesWith(New); |
| 1590 | } |
| 1591 | |
| 1592 | /// \brief Reassign the original uses of Inst to Inst. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1593 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1594 | DEBUG(dbgs() << "Undo: UsersReplacer: " << *Inst << "\n"); |
| 1595 | for (use_iterator UseIt = OriginalUses.begin(), |
| 1596 | EndIt = OriginalUses.end(); |
| 1597 | UseIt != EndIt; ++UseIt) { |
| 1598 | UseIt->Inst->setOperand(UseIt->Idx, Inst); |
| 1599 | } |
| 1600 | } |
| 1601 | }; |
| 1602 | |
| 1603 | /// \brief Remove an instruction from the IR. |
| 1604 | class InstructionRemover : public TypePromotionAction { |
| 1605 | /// Original position of the instruction. |
| 1606 | InsertionHandler Inserter; |
| 1607 | /// Helper structure to hide all the link to the instruction. In other |
| 1608 | /// words, this helps to do as if the instruction was removed. |
| 1609 | OperandsHider Hider; |
| 1610 | /// Keep track of the uses replaced, if any. |
| 1611 | UsesReplacer *Replacer; |
| 1612 | |
| 1613 | public: |
| 1614 | /// \brief Remove all reference of \p Inst and optinally replace all its |
| 1615 | /// uses with New. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1616 | /// \pre If !Inst->use_empty(), then New != nullptr |
| 1617 | InstructionRemover(Instruction *Inst, Value *New = nullptr) |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1618 | : TypePromotionAction(Inst), Inserter(Inst), Hider(Inst), |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1619 | Replacer(nullptr) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1620 | if (New) |
| 1621 | Replacer = new UsesReplacer(Inst, New); |
| 1622 | DEBUG(dbgs() << "Do: InstructionRemover: " << *Inst << "\n"); |
| 1623 | Inst->removeFromParent(); |
| 1624 | } |
| 1625 | |
| 1626 | ~InstructionRemover() { delete Replacer; } |
| 1627 | |
| 1628 | /// \brief Really remove the instruction. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1629 | void commit() override { delete Inst; } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1630 | |
| 1631 | /// \brief Resurrect the instruction and reassign it to the proper uses if |
| 1632 | /// new value was provided when build this action. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1633 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1634 | DEBUG(dbgs() << "Undo: InstructionRemover: " << *Inst << "\n"); |
| 1635 | Inserter.insert(Inst); |
| 1636 | if (Replacer) |
| 1637 | Replacer->undo(); |
| 1638 | Hider.undo(); |
| 1639 | } |
| 1640 | }; |
| 1641 | |
| 1642 | public: |
| 1643 | /// Restoration point. |
| 1644 | /// The restoration point is a pointer to an action instead of an iterator |
| 1645 | /// because the iterator may be invalidated but not the pointer. |
| 1646 | typedef const TypePromotionAction *ConstRestorationPt; |
| 1647 | /// Advocate every changes made in that transaction. |
| 1648 | void commit(); |
| 1649 | /// Undo all the changes made after the given point. |
| 1650 | void rollback(ConstRestorationPt Point); |
| 1651 | /// Get the current restoration point. |
| 1652 | ConstRestorationPt getRestorationPoint() const; |
| 1653 | |
| 1654 | /// \name API for IR modification with state keeping to support rollback. |
| 1655 | /// @{ |
| 1656 | /// Same as Instruction::setOperand. |
| 1657 | void setOperand(Instruction *Inst, unsigned Idx, Value *NewVal); |
| 1658 | /// Same as Instruction::eraseFromParent. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1659 | void eraseInstruction(Instruction *Inst, Value *NewVal = nullptr); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1660 | /// Same as Value::replaceAllUsesWith. |
| 1661 | void replaceAllUsesWith(Instruction *Inst, Value *New); |
| 1662 | /// Same as Value::mutateType. |
| 1663 | void mutateType(Instruction *Inst, Type *NewTy); |
| 1664 | /// Same as IRBuilder::createTrunc. |
| 1665 | Instruction *createTrunc(Instruction *Opnd, Type *Ty); |
| 1666 | /// Same as IRBuilder::createSExt. |
| 1667 | Instruction *createSExt(Instruction *Inst, Value *Opnd, Type *Ty); |
| 1668 | /// Same as Instruction::moveBefore. |
| 1669 | void moveBefore(Instruction *Inst, Instruction *Before); |
| 1670 | /// @} |
| 1671 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1672 | private: |
| 1673 | /// The ordered list of actions made so far. |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1674 | SmallVector<std::unique_ptr<TypePromotionAction>, 16> Actions; |
| 1675 | typedef SmallVectorImpl<std::unique_ptr<TypePromotionAction>>::iterator CommitPt; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1676 | }; |
| 1677 | |
| 1678 | void TypePromotionTransaction::setOperand(Instruction *Inst, unsigned Idx, |
| 1679 | Value *NewVal) { |
| 1680 | Actions.push_back( |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1681 | make_unique<TypePromotionTransaction::OperandSetter>(Inst, Idx, NewVal)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | void TypePromotionTransaction::eraseInstruction(Instruction *Inst, |
| 1685 | Value *NewVal) { |
| 1686 | Actions.push_back( |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1687 | make_unique<TypePromotionTransaction::InstructionRemover>(Inst, NewVal)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | void TypePromotionTransaction::replaceAllUsesWith(Instruction *Inst, |
| 1691 | Value *New) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1692 | Actions.push_back(make_unique<TypePromotionTransaction::UsesReplacer>(Inst, New)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | void TypePromotionTransaction::mutateType(Instruction *Inst, Type *NewTy) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1696 | Actions.push_back(make_unique<TypePromotionTransaction::TypeMutator>(Inst, NewTy)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | Instruction *TypePromotionTransaction::createTrunc(Instruction *Opnd, |
| 1700 | Type *Ty) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1701 | std::unique_ptr<TruncBuilder> Ptr(new TruncBuilder(Opnd, Ty)); |
| 1702 | Instruction *I = Ptr->getBuiltInstruction(); |
| 1703 | Actions.push_back(std::move(Ptr)); |
| 1704 | return I; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | Instruction *TypePromotionTransaction::createSExt(Instruction *Inst, |
| 1708 | Value *Opnd, Type *Ty) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1709 | std::unique_ptr<SExtBuilder> Ptr(new SExtBuilder(Inst, Opnd, Ty)); |
| 1710 | Instruction *I = Ptr->getBuiltInstruction(); |
| 1711 | Actions.push_back(std::move(Ptr)); |
| 1712 | return I; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | void TypePromotionTransaction::moveBefore(Instruction *Inst, |
| 1716 | Instruction *Before) { |
| 1717 | Actions.push_back( |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1718 | make_unique<TypePromotionTransaction::InstructionMoveBefore>(Inst, Before)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | TypePromotionTransaction::ConstRestorationPt |
| 1722 | TypePromotionTransaction::getRestorationPoint() const { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1723 | return !Actions.empty() ? Actions.back().get() : nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | void TypePromotionTransaction::commit() { |
| 1727 | for (CommitPt It = Actions.begin(), EndIt = Actions.end(); It != EndIt; |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1728 | ++It) |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1729 | (*It)->commit(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1730 | Actions.clear(); |
| 1731 | } |
| 1732 | |
| 1733 | void TypePromotionTransaction::rollback( |
| 1734 | TypePromotionTransaction::ConstRestorationPt Point) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 1735 | while (!Actions.empty() && Point != Actions.back().get()) { |
| 1736 | std::unique_ptr<TypePromotionAction> Curr = Actions.pop_back_val(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1737 | Curr->undo(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1741 | /// \brief A helper class for matching addressing modes. |
| 1742 | /// |
| 1743 | /// This encapsulates the logic for matching the target-legal addressing modes. |
| 1744 | class AddressingModeMatcher { |
| 1745 | SmallVectorImpl<Instruction*> &AddrModeInsts; |
| 1746 | const TargetLowering &TLI; |
| 1747 | |
| 1748 | /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and |
| 1749 | /// the memory instruction that we're computing this address for. |
| 1750 | Type *AccessTy; |
| 1751 | Instruction *MemoryInst; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1752 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1753 | /// AddrMode - This is the addressing mode that we're building up. This is |
| 1754 | /// part of the return value of this addressing mode matching stuff. |
| 1755 | ExtAddrMode &AddrMode; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1756 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1757 | /// The truncate instruction inserted by other CodeGenPrepare optimizations. |
| 1758 | const SetOfInstrs &InsertedTruncs; |
| 1759 | /// A map from the instructions to their type before promotion. |
| 1760 | InstrToOrigTy &PromotedInsts; |
| 1761 | /// The ongoing transaction where every action should be registered. |
| 1762 | TypePromotionTransaction &TPT; |
| 1763 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1764 | /// IgnoreProfitability - This is set to true when we should not do |
| 1765 | /// profitability checks. When true, IsProfitableToFoldIntoAddressingMode |
| 1766 | /// always returns true. |
| 1767 | bool IgnoreProfitability; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1768 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1769 | AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI, |
| 1770 | const TargetLowering &T, Type *AT, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1771 | Instruction *MI, ExtAddrMode &AM, |
| 1772 | const SetOfInstrs &InsertedTruncs, |
| 1773 | InstrToOrigTy &PromotedInsts, |
| 1774 | TypePromotionTransaction &TPT) |
| 1775 | : AddrModeInsts(AMI), TLI(T), AccessTy(AT), MemoryInst(MI), AddrMode(AM), |
| 1776 | InsertedTruncs(InsertedTruncs), PromotedInsts(PromotedInsts), TPT(TPT) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1777 | IgnoreProfitability = false; |
| 1778 | } |
| 1779 | public: |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1780 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1781 | /// Match - Find the maximal addressing mode that a load/store of V can fold, |
| 1782 | /// give an access type of AccessTy. This returns a list of involved |
| 1783 | /// instructions in AddrModeInsts. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1784 | /// \p InsertedTruncs The truncate instruction inserted by other |
| 1785 | /// CodeGenPrepare |
| 1786 | /// optimizations. |
| 1787 | /// \p PromotedInsts maps the instructions to their type before promotion. |
| 1788 | /// \p The ongoing transaction where every action should be registered. |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1789 | static ExtAddrMode Match(Value *V, Type *AccessTy, |
| 1790 | Instruction *MemoryInst, |
| 1791 | SmallVectorImpl<Instruction*> &AddrModeInsts, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1792 | const TargetLowering &TLI, |
| 1793 | const SetOfInstrs &InsertedTruncs, |
| 1794 | InstrToOrigTy &PromotedInsts, |
| 1795 | TypePromotionTransaction &TPT) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1796 | ExtAddrMode Result; |
| 1797 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1798 | bool Success = AddressingModeMatcher(AddrModeInsts, TLI, AccessTy, |
| 1799 | MemoryInst, Result, InsertedTruncs, |
| 1800 | PromotedInsts, TPT).MatchAddr(V, 0); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1801 | (void)Success; assert(Success && "Couldn't select *anything*?"); |
| 1802 | return Result; |
| 1803 | } |
| 1804 | private: |
| 1805 | bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth); |
| 1806 | bool MatchAddr(Value *V, unsigned Depth); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1807 | bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1808 | bool *MovedAway = nullptr); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1809 | bool IsProfitableToFoldIntoAddressingMode(Instruction *I, |
| 1810 | ExtAddrMode &AMBefore, |
| 1811 | ExtAddrMode &AMAfter); |
| 1812 | bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2); |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 1813 | bool IsPromotionProfitable(unsigned MatchedSize, unsigned SizeWithPromotion, |
| 1814 | Value *PromotedOperand) const; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1815 | }; |
| 1816 | |
| 1817 | /// MatchScaledValue - Try adding ScaleReg*Scale to the current addressing mode. |
| 1818 | /// Return true and update AddrMode if this addr mode is legal for the target, |
| 1819 | /// false if not. |
| 1820 | bool AddressingModeMatcher::MatchScaledValue(Value *ScaleReg, int64_t Scale, |
| 1821 | unsigned Depth) { |
| 1822 | // If Scale is 1, then this is the same as adding ScaleReg to the addressing |
| 1823 | // mode. Just process that directly. |
| 1824 | if (Scale == 1) |
| 1825 | return MatchAddr(ScaleReg, Depth); |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1826 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1827 | // If the scale is 0, it takes nothing to add this. |
| 1828 | if (Scale == 0) |
| 1829 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1830 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1831 | // If we already have a scale of this value, we can add to it, otherwise, we |
| 1832 | // need an available scale field. |
| 1833 | if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg) |
| 1834 | return false; |
| 1835 | |
| 1836 | ExtAddrMode TestAddrMode = AddrMode; |
| 1837 | |
| 1838 | // Add scale to turn X*4+X*3 -> X*7. This could also do things like |
| 1839 | // [A+B + A*7] -> [B+A*8]. |
| 1840 | TestAddrMode.Scale += Scale; |
| 1841 | TestAddrMode.ScaledReg = ScaleReg; |
| 1842 | |
| 1843 | // If the new address isn't legal, bail out. |
| 1844 | if (!TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) |
| 1845 | return false; |
| 1846 | |
| 1847 | // It was legal, so commit it. |
| 1848 | AddrMode = TestAddrMode; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1849 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1850 | // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now |
| 1851 | // to see if ScaleReg is actually X+C. If so, we can turn this into adding |
| 1852 | // X*Scale + C*Scale to addr mode. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1853 | ConstantInt *CI = nullptr; Value *AddLHS = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1854 | if (isa<Instruction>(ScaleReg) && // not a constant expr. |
| 1855 | match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) { |
| 1856 | TestAddrMode.ScaledReg = AddLHS; |
| 1857 | TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1858 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1859 | // If this addressing mode is legal, commit it and remember that we folded |
| 1860 | // this instruction. |
| 1861 | if (TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) { |
| 1862 | AddrModeInsts.push_back(cast<Instruction>(ScaleReg)); |
| 1863 | AddrMode = TestAddrMode; |
| 1864 | return true; |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | // Otherwise, not (x+c)*scale, just return what we have. |
| 1869 | return true; |
| 1870 | } |
| 1871 | |
| 1872 | /// MightBeFoldableInst - This is a little filter, which returns true if an |
| 1873 | /// addressing computation involving I might be folded into a load/store |
| 1874 | /// accessing it. This doesn't need to be perfect, but needs to accept at least |
| 1875 | /// the set of instructions that MatchOperationAddr can. |
| 1876 | static bool MightBeFoldableInst(Instruction *I) { |
| 1877 | switch (I->getOpcode()) { |
| 1878 | case Instruction::BitCast: |
| 1879 | // Don't touch identity bitcasts. |
| 1880 | if (I->getType() == I->getOperand(0)->getType()) |
| 1881 | return false; |
| 1882 | return I->getType()->isPointerTy() || I->getType()->isIntegerTy(); |
| 1883 | case Instruction::PtrToInt: |
| 1884 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
| 1885 | return true; |
| 1886 | case Instruction::IntToPtr: |
| 1887 | // We know the input is intptr_t, so this is foldable. |
| 1888 | return true; |
| 1889 | case Instruction::Add: |
| 1890 | return true; |
| 1891 | case Instruction::Mul: |
| 1892 | case Instruction::Shl: |
| 1893 | // Can only handle X*C and X << C. |
| 1894 | return isa<ConstantInt>(I->getOperand(1)); |
| 1895 | case Instruction::GetElementPtr: |
| 1896 | return true; |
| 1897 | default: |
| 1898 | return false; |
| 1899 | } |
| 1900 | } |
| 1901 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1902 | /// \brief Hepler class to perform type promotion. |
| 1903 | class TypePromotionHelper { |
| 1904 | /// \brief Utility function to check whether or not a sign extension of |
| 1905 | /// \p Inst with \p ConsideredSExtType can be moved through \p Inst by either |
| 1906 | /// using the operands of \p Inst or promoting \p Inst. |
| 1907 | /// In other words, check if: |
| 1908 | /// sext (Ty Inst opnd1 opnd2 ... opndN) to ConsideredSExtType. |
| 1909 | /// #1 Promotion applies: |
| 1910 | /// ConsideredSExtType Inst (sext opnd1 to ConsideredSExtType, ...). |
| 1911 | /// #2 Operand reuses: |
| 1912 | /// sext opnd1 to ConsideredSExtType. |
| 1913 | /// \p PromotedInsts maps the instructions to their type before promotion. |
| 1914 | static bool canGetThrough(const Instruction *Inst, Type *ConsideredSExtType, |
| 1915 | const InstrToOrigTy &PromotedInsts); |
| 1916 | |
| 1917 | /// \brief Utility function to determine if \p OpIdx should be promoted when |
| 1918 | /// promoting \p Inst. |
| 1919 | static bool shouldSExtOperand(const Instruction *Inst, int OpIdx) { |
| 1920 | if (isa<SelectInst>(Inst) && OpIdx == 0) |
| 1921 | return false; |
| 1922 | return true; |
| 1923 | } |
| 1924 | |
| 1925 | /// \brief Utility function to promote the operand of \p SExt when this |
| 1926 | /// operand is a promotable trunc or sext. |
| 1927 | /// \p PromotedInsts maps the instructions to their type before promotion. |
| 1928 | /// \p CreatedInsts[out] contains how many non-free instructions have been |
| 1929 | /// created to promote the operand of SExt. |
| 1930 | /// Should never be called directly. |
| 1931 | /// \return The promoted value which is used instead of SExt. |
| 1932 | static Value *promoteOperandForTruncAndSExt(Instruction *SExt, |
| 1933 | TypePromotionTransaction &TPT, |
| 1934 | InstrToOrigTy &PromotedInsts, |
| 1935 | unsigned &CreatedInsts); |
| 1936 | |
| 1937 | /// \brief Utility function to promote the operand of \p SExt when this |
| 1938 | /// operand is promotable and is not a supported trunc or sext. |
| 1939 | /// \p PromotedInsts maps the instructions to their type before promotion. |
| 1940 | /// \p CreatedInsts[out] contains how many non-free instructions have been |
| 1941 | /// created to promote the operand of SExt. |
| 1942 | /// Should never be called directly. |
| 1943 | /// \return The promoted value which is used instead of SExt. |
| 1944 | static Value *promoteOperandForOther(Instruction *SExt, |
| 1945 | TypePromotionTransaction &TPT, |
| 1946 | InstrToOrigTy &PromotedInsts, |
| 1947 | unsigned &CreatedInsts); |
| 1948 | |
| 1949 | public: |
| 1950 | /// Type for the utility function that promotes the operand of SExt. |
| 1951 | typedef Value *(*Action)(Instruction *SExt, TypePromotionTransaction &TPT, |
| 1952 | InstrToOrigTy &PromotedInsts, |
| 1953 | unsigned &CreatedInsts); |
| 1954 | /// \brief Given a sign extend instruction \p SExt, return the approriate |
| 1955 | /// action to promote the operand of \p SExt instead of using SExt. |
| 1956 | /// \return NULL if no promotable action is possible with the current |
| 1957 | /// sign extension. |
| 1958 | /// \p InsertedTruncs keeps track of all the truncate instructions inserted by |
| 1959 | /// the others CodeGenPrepare optimizations. This information is important |
| 1960 | /// because we do not want to promote these instructions as CodeGenPrepare |
| 1961 | /// will reinsert them later. Thus creating an infinite loop: create/remove. |
| 1962 | /// \p PromotedInsts maps the instructions to their type before promotion. |
| 1963 | static Action getAction(Instruction *SExt, const SetOfInstrs &InsertedTruncs, |
| 1964 | const TargetLowering &TLI, |
| 1965 | const InstrToOrigTy &PromotedInsts); |
| 1966 | }; |
| 1967 | |
| 1968 | bool TypePromotionHelper::canGetThrough(const Instruction *Inst, |
| 1969 | Type *ConsideredSExtType, |
| 1970 | const InstrToOrigTy &PromotedInsts) { |
| 1971 | // We can always get through sext. |
| 1972 | if (isa<SExtInst>(Inst)) |
| 1973 | return true; |
| 1974 | |
| 1975 | // We can get through binary operator, if it is legal. In other words, the |
| 1976 | // binary operator must have a nuw or nsw flag. |
| 1977 | const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst); |
| 1978 | if (BinOp && isa<OverflowingBinaryOperator>(BinOp) && |
| 1979 | (BinOp->hasNoUnsignedWrap() || BinOp->hasNoSignedWrap())) |
| 1980 | return true; |
| 1981 | |
| 1982 | // Check if we can do the following simplification. |
| 1983 | // sext(trunc(sext)) --> sext |
| 1984 | if (!isa<TruncInst>(Inst)) |
| 1985 | return false; |
| 1986 | |
| 1987 | Value *OpndVal = Inst->getOperand(0); |
| 1988 | // Check if we can use this operand in the sext. |
| 1989 | // If the type is larger than the result type of the sign extension, |
| 1990 | // we cannot. |
| 1991 | if (OpndVal->getType()->getIntegerBitWidth() > |
| 1992 | ConsideredSExtType->getIntegerBitWidth()) |
| 1993 | return false; |
| 1994 | |
| 1995 | // If the operand of the truncate is not an instruction, we will not have |
| 1996 | // any information on the dropped bits. |
| 1997 | // (Actually we could for constant but it is not worth the extra logic). |
| 1998 | Instruction *Opnd = dyn_cast<Instruction>(OpndVal); |
| 1999 | if (!Opnd) |
| 2000 | return false; |
| 2001 | |
| 2002 | // Check if the source of the type is narrow enough. |
| 2003 | // I.e., check that trunc just drops sign extended bits. |
| 2004 | // #1 get the type of the operand. |
| 2005 | const Type *OpndType; |
| 2006 | InstrToOrigTy::const_iterator It = PromotedInsts.find(Opnd); |
| 2007 | if (It != PromotedInsts.end()) |
| 2008 | OpndType = It->second; |
| 2009 | else if (isa<SExtInst>(Opnd)) |
| 2010 | OpndType = cast<Instruction>(Opnd)->getOperand(0)->getType(); |
| 2011 | else |
| 2012 | return false; |
| 2013 | |
| 2014 | // #2 check that the truncate just drop sign extended bits. |
| 2015 | if (Inst->getType()->getIntegerBitWidth() >= OpndType->getIntegerBitWidth()) |
| 2016 | return true; |
| 2017 | |
| 2018 | return false; |
| 2019 | } |
| 2020 | |
| 2021 | TypePromotionHelper::Action TypePromotionHelper::getAction( |
| 2022 | Instruction *SExt, const SetOfInstrs &InsertedTruncs, |
| 2023 | const TargetLowering &TLI, const InstrToOrigTy &PromotedInsts) { |
| 2024 | Instruction *SExtOpnd = dyn_cast<Instruction>(SExt->getOperand(0)); |
| 2025 | Type *SExtTy = SExt->getType(); |
| 2026 | // If the operand of the sign extension is not an instruction, we cannot |
| 2027 | // get through. |
| 2028 | // If it, check we can get through. |
| 2029 | if (!SExtOpnd || !canGetThrough(SExtOpnd, SExtTy, PromotedInsts)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2030 | return nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Do not promote if the operand has been added by codegenprepare. |
| 2033 | // Otherwise, it means we are undoing an optimization that is likely to be |
| 2034 | // redone, thus causing potential infinite loop. |
| 2035 | if (isa<TruncInst>(SExtOpnd) && InsertedTruncs.count(SExtOpnd)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2036 | return nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2037 | |
| 2038 | // SExt or Trunc instructions. |
| 2039 | // Return the related handler. |
| 2040 | if (isa<SExtInst>(SExtOpnd) || isa<TruncInst>(SExtOpnd)) |
| 2041 | return promoteOperandForTruncAndSExt; |
| 2042 | |
| 2043 | // Regular instruction. |
| 2044 | // Abort early if we will have to insert non-free instructions. |
| 2045 | if (!SExtOpnd->hasOneUse() && |
| 2046 | !TLI.isTruncateFree(SExtTy, SExtOpnd->getType())) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2047 | return nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2048 | return promoteOperandForOther; |
| 2049 | } |
| 2050 | |
| 2051 | Value *TypePromotionHelper::promoteOperandForTruncAndSExt( |
| 2052 | llvm::Instruction *SExt, TypePromotionTransaction &TPT, |
| 2053 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInsts) { |
| 2054 | // By construction, the operand of SExt is an instruction. Otherwise we cannot |
| 2055 | // get through it and this method should not be called. |
| 2056 | Instruction *SExtOpnd = cast<Instruction>(SExt->getOperand(0)); |
| 2057 | // Replace sext(trunc(opnd)) or sext(sext(opnd)) |
| 2058 | // => sext(opnd). |
| 2059 | TPT.setOperand(SExt, 0, SExtOpnd->getOperand(0)); |
| 2060 | CreatedInsts = 0; |
| 2061 | |
| 2062 | // Remove dead code. |
| 2063 | if (SExtOpnd->use_empty()) |
| 2064 | TPT.eraseInstruction(SExtOpnd); |
| 2065 | |
| 2066 | // Check if the sext is still needed. |
| 2067 | if (SExt->getType() != SExt->getOperand(0)->getType()) |
| 2068 | return SExt; |
| 2069 | |
| 2070 | // At this point we have: sext ty opnd to ty. |
| 2071 | // Reassign the uses of SExt to the opnd and remove SExt. |
| 2072 | Value *NextVal = SExt->getOperand(0); |
| 2073 | TPT.eraseInstruction(SExt, NextVal); |
| 2074 | return NextVal; |
| 2075 | } |
| 2076 | |
| 2077 | Value * |
| 2078 | TypePromotionHelper::promoteOperandForOther(Instruction *SExt, |
| 2079 | TypePromotionTransaction &TPT, |
| 2080 | InstrToOrigTy &PromotedInsts, |
| 2081 | unsigned &CreatedInsts) { |
| 2082 | // By construction, the operand of SExt is an instruction. Otherwise we cannot |
| 2083 | // get through it and this method should not be called. |
| 2084 | Instruction *SExtOpnd = cast<Instruction>(SExt->getOperand(0)); |
| 2085 | CreatedInsts = 0; |
| 2086 | if (!SExtOpnd->hasOneUse()) { |
| 2087 | // SExtOpnd will be promoted. |
| 2088 | // All its uses, but SExt, will need to use a truncated value of the |
| 2089 | // promoted version. |
| 2090 | // Create the truncate now. |
| 2091 | Instruction *Trunc = TPT.createTrunc(SExt, SExtOpnd->getType()); |
| 2092 | Trunc->removeFromParent(); |
| 2093 | // Insert it just after the definition. |
| 2094 | Trunc->insertAfter(SExtOpnd); |
| 2095 | |
| 2096 | TPT.replaceAllUsesWith(SExtOpnd, Trunc); |
| 2097 | // Restore the operand of SExt (which has been replace by the previous call |
| 2098 | // to replaceAllUsesWith) to avoid creating a cycle trunc <-> sext. |
| 2099 | TPT.setOperand(SExt, 0, SExtOpnd); |
| 2100 | } |
| 2101 | |
| 2102 | // Get through the Instruction: |
| 2103 | // 1. Update its type. |
| 2104 | // 2. Replace the uses of SExt by Inst. |
| 2105 | // 3. Sign extend each operand that needs to be sign extended. |
| 2106 | |
| 2107 | // Remember the original type of the instruction before promotion. |
| 2108 | // This is useful to know that the high bits are sign extended bits. |
| 2109 | PromotedInsts.insert( |
| 2110 | std::pair<Instruction *, Type *>(SExtOpnd, SExtOpnd->getType())); |
| 2111 | // Step #1. |
| 2112 | TPT.mutateType(SExtOpnd, SExt->getType()); |
| 2113 | // Step #2. |
| 2114 | TPT.replaceAllUsesWith(SExt, SExtOpnd); |
| 2115 | // Step #3. |
| 2116 | Instruction *SExtForOpnd = SExt; |
| 2117 | |
| 2118 | DEBUG(dbgs() << "Propagate SExt to operands\n"); |
| 2119 | for (int OpIdx = 0, EndOpIdx = SExtOpnd->getNumOperands(); OpIdx != EndOpIdx; |
| 2120 | ++OpIdx) { |
| 2121 | DEBUG(dbgs() << "Operand:\n" << *(SExtOpnd->getOperand(OpIdx)) << '\n'); |
| 2122 | if (SExtOpnd->getOperand(OpIdx)->getType() == SExt->getType() || |
| 2123 | !shouldSExtOperand(SExtOpnd, OpIdx)) { |
| 2124 | DEBUG(dbgs() << "No need to propagate\n"); |
| 2125 | continue; |
| 2126 | } |
| 2127 | // Check if we can statically sign extend the operand. |
| 2128 | Value *Opnd = SExtOpnd->getOperand(OpIdx); |
| 2129 | if (const ConstantInt *Cst = dyn_cast<ConstantInt>(Opnd)) { |
| 2130 | DEBUG(dbgs() << "Statically sign extend\n"); |
| 2131 | TPT.setOperand( |
| 2132 | SExtOpnd, OpIdx, |
| 2133 | ConstantInt::getSigned(SExt->getType(), Cst->getSExtValue())); |
| 2134 | continue; |
| 2135 | } |
| 2136 | // UndefValue are typed, so we have to statically sign extend them. |
| 2137 | if (isa<UndefValue>(Opnd)) { |
| 2138 | DEBUG(dbgs() << "Statically sign extend\n"); |
| 2139 | TPT.setOperand(SExtOpnd, OpIdx, UndefValue::get(SExt->getType())); |
| 2140 | continue; |
| 2141 | } |
| 2142 | |
| 2143 | // Otherwise we have to explicity sign extend the operand. |
| 2144 | // Check if SExt was reused to sign extend an operand. |
| 2145 | if (!SExtForOpnd) { |
| 2146 | // If yes, create a new one. |
| 2147 | DEBUG(dbgs() << "More operands to sext\n"); |
| 2148 | SExtForOpnd = TPT.createSExt(SExt, Opnd, SExt->getType()); |
| 2149 | ++CreatedInsts; |
| 2150 | } |
| 2151 | |
| 2152 | TPT.setOperand(SExtForOpnd, 0, Opnd); |
| 2153 | |
| 2154 | // Move the sign extension before the insertion point. |
| 2155 | TPT.moveBefore(SExtForOpnd, SExtOpnd); |
| 2156 | TPT.setOperand(SExtOpnd, OpIdx, SExtForOpnd); |
| 2157 | // If more sext are required, new instructions will have to be created. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2158 | SExtForOpnd = nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2159 | } |
| 2160 | if (SExtForOpnd == SExt) { |
| 2161 | DEBUG(dbgs() << "Sign extension is useless now\n"); |
| 2162 | TPT.eraseInstruction(SExt); |
| 2163 | } |
| 2164 | return SExtOpnd; |
| 2165 | } |
| 2166 | |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2167 | /// IsPromotionProfitable - Check whether or not promoting an instruction |
| 2168 | /// to a wider type was profitable. |
| 2169 | /// \p MatchedSize gives the number of instructions that have been matched |
| 2170 | /// in the addressing mode after the promotion was applied. |
| 2171 | /// \p SizeWithPromotion gives the number of created instructions for |
| 2172 | /// the promotion plus the number of instructions that have been |
| 2173 | /// matched in the addressing mode before the promotion. |
| 2174 | /// \p PromotedOperand is the value that has been promoted. |
| 2175 | /// \return True if the promotion is profitable, false otherwise. |
| 2176 | bool |
| 2177 | AddressingModeMatcher::IsPromotionProfitable(unsigned MatchedSize, |
| 2178 | unsigned SizeWithPromotion, |
| 2179 | Value *PromotedOperand) const { |
| 2180 | // We folded less instructions than what we created to promote the operand. |
| 2181 | // This is not profitable. |
| 2182 | if (MatchedSize < SizeWithPromotion) |
| 2183 | return false; |
| 2184 | if (MatchedSize > SizeWithPromotion) |
| 2185 | return true; |
| 2186 | // The promotion is neutral but it may help folding the sign extension in |
| 2187 | // loads for instance. |
| 2188 | // Check that we did not create an illegal instruction. |
| 2189 | Instruction *PromotedInst = dyn_cast<Instruction>(PromotedOperand); |
| 2190 | if (!PromotedInst) |
| 2191 | return false; |
Quentin Colombet | 1627a41 | 2014-02-22 01:06:41 +0000 | [diff] [blame] | 2192 | int ISDOpcode = TLI.InstructionOpcodeToISD(PromotedInst->getOpcode()); |
| 2193 | // If the ISDOpcode is undefined, it was undefined before the promotion. |
| 2194 | if (!ISDOpcode) |
| 2195 | return true; |
| 2196 | // Otherwise, check if the promoted instruction is legal or not. |
| 2197 | return TLI.isOperationLegalOrCustom(ISDOpcode, |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2198 | EVT::getEVT(PromotedInst->getType())); |
| 2199 | } |
| 2200 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2201 | /// MatchOperationAddr - Given an instruction or constant expr, see if we can |
| 2202 | /// fold the operation into the addressing mode. If so, update the addressing |
| 2203 | /// mode and return true, otherwise return false without modifying AddrMode. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2204 | /// If \p MovedAway is not NULL, it contains the information of whether or |
| 2205 | /// not AddrInst has to be folded into the addressing mode on success. |
| 2206 | /// If \p MovedAway == true, \p AddrInst will not be part of the addressing |
| 2207 | /// because it has been moved away. |
| 2208 | /// Thus AddrInst must not be added in the matched instructions. |
| 2209 | /// This state can happen when AddrInst is a sext, since it may be moved away. |
| 2210 | /// Therefore, AddrInst may not be valid when MovedAway is true and it must |
| 2211 | /// not be referenced anymore. |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2212 | bool AddressingModeMatcher::MatchOperationAddr(User *AddrInst, unsigned Opcode, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2213 | unsigned Depth, |
| 2214 | bool *MovedAway) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2215 | // Avoid exponential behavior on extremely deep expression trees. |
| 2216 | if (Depth >= 5) return false; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2217 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2218 | // By default, all matched instructions stay in place. |
| 2219 | if (MovedAway) |
| 2220 | *MovedAway = false; |
| 2221 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2222 | switch (Opcode) { |
| 2223 | case Instruction::PtrToInt: |
| 2224 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
| 2225 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 2226 | case Instruction::IntToPtr: |
| 2227 | // This inttoptr is a no-op if the integer type is pointer sized. |
| 2228 | if (TLI.getValueType(AddrInst->getOperand(0)->getType()) == |
Matt Arsenault | 37d42ec | 2013-09-06 00:18:43 +0000 | [diff] [blame] | 2229 | TLI.getPointerTy(AddrInst->getType()->getPointerAddressSpace())) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2230 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 2231 | return false; |
| 2232 | case Instruction::BitCast: |
| 2233 | // BitCast is always a noop, and we can handle it as long as it is |
| 2234 | // int->int or pointer->pointer (we don't want int<->fp or something). |
| 2235 | if ((AddrInst->getOperand(0)->getType()->isPointerTy() || |
| 2236 | AddrInst->getOperand(0)->getType()->isIntegerTy()) && |
| 2237 | // Don't touch identity bitcasts. These were probably put here by LSR, |
| 2238 | // and we don't want to mess around with them. Assume it knows what it |
| 2239 | // is doing. |
| 2240 | AddrInst->getOperand(0)->getType() != AddrInst->getType()) |
| 2241 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 2242 | return false; |
| 2243 | case Instruction::Add: { |
| 2244 | // Check to see if we can merge in the RHS then the LHS. If so, we win. |
| 2245 | ExtAddrMode BackupAddrMode = AddrMode; |
| 2246 | unsigned OldSize = AddrModeInsts.size(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2247 | // Start a transaction at this point. |
| 2248 | // The LHS may match but not the RHS. |
| 2249 | // Therefore, we need a higher level restoration point to undo partially |
| 2250 | // matched operation. |
| 2251 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 2252 | TPT.getRestorationPoint(); |
| 2253 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2254 | if (MatchAddr(AddrInst->getOperand(1), Depth+1) && |
| 2255 | MatchAddr(AddrInst->getOperand(0), Depth+1)) |
| 2256 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2257 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2258 | // Restore the old addr mode info. |
| 2259 | AddrMode = BackupAddrMode; |
| 2260 | AddrModeInsts.resize(OldSize); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2261 | TPT.rollback(LastKnownGood); |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2262 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2263 | // Otherwise this was over-aggressive. Try merging in the LHS then the RHS. |
| 2264 | if (MatchAddr(AddrInst->getOperand(0), Depth+1) && |
| 2265 | MatchAddr(AddrInst->getOperand(1), Depth+1)) |
| 2266 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2267 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2268 | // Otherwise we definitely can't merge the ADD in. |
| 2269 | AddrMode = BackupAddrMode; |
| 2270 | AddrModeInsts.resize(OldSize); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2271 | TPT.rollback(LastKnownGood); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2272 | break; |
| 2273 | } |
| 2274 | //case Instruction::Or: |
| 2275 | // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD. |
| 2276 | //break; |
| 2277 | case Instruction::Mul: |
| 2278 | case Instruction::Shl: { |
| 2279 | // Can only handle X*C and X << C. |
| 2280 | ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1)); |
| 2281 | if (!RHS) return false; |
| 2282 | int64_t Scale = RHS->getSExtValue(); |
| 2283 | if (Opcode == Instruction::Shl) |
| 2284 | Scale = 1LL << Scale; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2285 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2286 | return MatchScaledValue(AddrInst->getOperand(0), Scale, Depth); |
| 2287 | } |
| 2288 | case Instruction::GetElementPtr: { |
| 2289 | // Scan the GEP. We check it if it contains constant offsets and at most |
| 2290 | // one variable offset. |
| 2291 | int VariableOperand = -1; |
| 2292 | unsigned VariableScale = 0; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2293 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2294 | int64_t ConstantOffset = 0; |
| 2295 | const DataLayout *TD = TLI.getDataLayout(); |
| 2296 | gep_type_iterator GTI = gep_type_begin(AddrInst); |
| 2297 | for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) { |
| 2298 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 2299 | const StructLayout *SL = TD->getStructLayout(STy); |
| 2300 | unsigned Idx = |
| 2301 | cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue(); |
| 2302 | ConstantOffset += SL->getElementOffset(Idx); |
| 2303 | } else { |
| 2304 | uint64_t TypeSize = TD->getTypeAllocSize(GTI.getIndexedType()); |
| 2305 | if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) { |
| 2306 | ConstantOffset += CI->getSExtValue()*TypeSize; |
| 2307 | } else if (TypeSize) { // Scales of zero don't do anything. |
| 2308 | // We only allow one variable index at the moment. |
| 2309 | if (VariableOperand != -1) |
| 2310 | return false; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2311 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2312 | // Remember the variable index. |
| 2313 | VariableOperand = i; |
| 2314 | VariableScale = TypeSize; |
| 2315 | } |
| 2316 | } |
| 2317 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2318 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2319 | // A common case is for the GEP to only do a constant offset. In this case, |
| 2320 | // just add it to the disp field and check validity. |
| 2321 | if (VariableOperand == -1) { |
| 2322 | AddrMode.BaseOffs += ConstantOffset; |
| 2323 | if (ConstantOffset == 0 || TLI.isLegalAddressingMode(AddrMode, AccessTy)){ |
| 2324 | // Check to see if we can fold the base pointer in too. |
| 2325 | if (MatchAddr(AddrInst->getOperand(0), Depth+1)) |
| 2326 | return true; |
| 2327 | } |
| 2328 | AddrMode.BaseOffs -= ConstantOffset; |
| 2329 | return false; |
| 2330 | } |
| 2331 | |
| 2332 | // Save the valid addressing mode in case we can't match. |
| 2333 | ExtAddrMode BackupAddrMode = AddrMode; |
| 2334 | unsigned OldSize = AddrModeInsts.size(); |
| 2335 | |
| 2336 | // See if the scale and offset amount is valid for this target. |
| 2337 | AddrMode.BaseOffs += ConstantOffset; |
| 2338 | |
| 2339 | // Match the base operand of the GEP. |
| 2340 | if (!MatchAddr(AddrInst->getOperand(0), Depth+1)) { |
| 2341 | // If it couldn't be matched, just stuff the value in a register. |
| 2342 | if (AddrMode.HasBaseReg) { |
| 2343 | AddrMode = BackupAddrMode; |
| 2344 | AddrModeInsts.resize(OldSize); |
| 2345 | return false; |
| 2346 | } |
| 2347 | AddrMode.HasBaseReg = true; |
| 2348 | AddrMode.BaseReg = AddrInst->getOperand(0); |
| 2349 | } |
| 2350 | |
| 2351 | // Match the remaining variable portion of the GEP. |
| 2352 | if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale, |
| 2353 | Depth)) { |
| 2354 | // If it couldn't be matched, try stuffing the base into a register |
| 2355 | // instead of matching it, and retrying the match of the scale. |
| 2356 | AddrMode = BackupAddrMode; |
| 2357 | AddrModeInsts.resize(OldSize); |
| 2358 | if (AddrMode.HasBaseReg) |
| 2359 | return false; |
| 2360 | AddrMode.HasBaseReg = true; |
| 2361 | AddrMode.BaseReg = AddrInst->getOperand(0); |
| 2362 | AddrMode.BaseOffs += ConstantOffset; |
| 2363 | if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), |
| 2364 | VariableScale, Depth)) { |
| 2365 | // If even that didn't work, bail. |
| 2366 | AddrMode = BackupAddrMode; |
| 2367 | AddrModeInsts.resize(OldSize); |
| 2368 | return false; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | return true; |
| 2373 | } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2374 | case Instruction::SExt: { |
| 2375 | // Try to move this sext out of the way of the addressing mode. |
| 2376 | Instruction *SExt = cast<Instruction>(AddrInst); |
| 2377 | // Ask for a method for doing so. |
| 2378 | TypePromotionHelper::Action TPH = TypePromotionHelper::getAction( |
| 2379 | SExt, InsertedTruncs, TLI, PromotedInsts); |
| 2380 | if (!TPH) |
| 2381 | return false; |
| 2382 | |
| 2383 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 2384 | TPT.getRestorationPoint(); |
| 2385 | unsigned CreatedInsts = 0; |
| 2386 | Value *PromotedOperand = TPH(SExt, TPT, PromotedInsts, CreatedInsts); |
| 2387 | // SExt has been moved away. |
| 2388 | // Thus either it will be rematched later in the recursive calls or it is |
| 2389 | // gone. Anyway, we must not fold it into the addressing mode at this point. |
| 2390 | // E.g., |
| 2391 | // op = add opnd, 1 |
| 2392 | // idx = sext op |
| 2393 | // addr = gep base, idx |
| 2394 | // is now: |
| 2395 | // promotedOpnd = sext opnd <- no match here |
| 2396 | // op = promoted_add promotedOpnd, 1 <- match (later in recursive calls) |
| 2397 | // addr = gep base, op <- match |
| 2398 | if (MovedAway) |
| 2399 | *MovedAway = true; |
| 2400 | |
| 2401 | assert(PromotedOperand && |
| 2402 | "TypePromotionHelper should have filtered out those cases"); |
| 2403 | |
| 2404 | ExtAddrMode BackupAddrMode = AddrMode; |
| 2405 | unsigned OldSize = AddrModeInsts.size(); |
| 2406 | |
| 2407 | if (!MatchAddr(PromotedOperand, Depth) || |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2408 | !IsPromotionProfitable(AddrModeInsts.size(), OldSize + CreatedInsts, |
| 2409 | PromotedOperand)) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2410 | AddrMode = BackupAddrMode; |
| 2411 | AddrModeInsts.resize(OldSize); |
| 2412 | DEBUG(dbgs() << "Sign extension does not pay off: rollback\n"); |
| 2413 | TPT.rollback(LastKnownGood); |
| 2414 | return false; |
| 2415 | } |
| 2416 | return true; |
| 2417 | } |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2418 | } |
| 2419 | return false; |
| 2420 | } |
| 2421 | |
| 2422 | /// MatchAddr - If we can, try to add the value of 'Addr' into the current |
| 2423 | /// addressing mode. If Addr can't be added to AddrMode this returns false and |
| 2424 | /// leaves AddrMode unmodified. This assumes that Addr is either a pointer type |
| 2425 | /// or intptr_t for the target. |
| 2426 | /// |
| 2427 | bool AddressingModeMatcher::MatchAddr(Value *Addr, unsigned Depth) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2428 | // Start a transaction at this point that we will rollback if the matching |
| 2429 | // fails. |
| 2430 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 2431 | TPT.getRestorationPoint(); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2432 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) { |
| 2433 | // Fold in immediates if legal for the target. |
| 2434 | AddrMode.BaseOffs += CI->getSExtValue(); |
| 2435 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 2436 | return true; |
| 2437 | AddrMode.BaseOffs -= CI->getSExtValue(); |
| 2438 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) { |
| 2439 | // If this is a global variable, try to fold it into the addressing mode. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2440 | if (!AddrMode.BaseGV) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2441 | AddrMode.BaseGV = GV; |
| 2442 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 2443 | return true; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2444 | AddrMode.BaseGV = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2445 | } |
| 2446 | } else if (Instruction *I = dyn_cast<Instruction>(Addr)) { |
| 2447 | ExtAddrMode BackupAddrMode = AddrMode; |
| 2448 | unsigned OldSize = AddrModeInsts.size(); |
| 2449 | |
| 2450 | // Check to see if it is possible to fold this operation. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2451 | bool MovedAway = false; |
| 2452 | if (MatchOperationAddr(I, I->getOpcode(), Depth, &MovedAway)) { |
| 2453 | // This instruction may have been move away. If so, there is nothing |
| 2454 | // to check here. |
| 2455 | if (MovedAway) |
| 2456 | return true; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2457 | // Okay, it's possible to fold this. Check to see if it is actually |
| 2458 | // *profitable* to do so. We use a simple cost model to avoid increasing |
| 2459 | // register pressure too much. |
| 2460 | if (I->hasOneUse() || |
| 2461 | IsProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) { |
| 2462 | AddrModeInsts.push_back(I); |
| 2463 | return true; |
| 2464 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2465 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2466 | // It isn't profitable to do this, roll back. |
| 2467 | //cerr << "NOT FOLDING: " << *I; |
| 2468 | AddrMode = BackupAddrMode; |
| 2469 | AddrModeInsts.resize(OldSize); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2470 | TPT.rollback(LastKnownGood); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2471 | } |
| 2472 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) { |
| 2473 | if (MatchOperationAddr(CE, CE->getOpcode(), Depth)) |
| 2474 | return true; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2475 | TPT.rollback(LastKnownGood); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2476 | } else if (isa<ConstantPointerNull>(Addr)) { |
| 2477 | // Null pointer gets folded without affecting the addressing mode. |
| 2478 | return true; |
| 2479 | } |
| 2480 | |
| 2481 | // Worse case, the target should support [reg] addressing modes. :) |
| 2482 | if (!AddrMode.HasBaseReg) { |
| 2483 | AddrMode.HasBaseReg = true; |
| 2484 | AddrMode.BaseReg = Addr; |
| 2485 | // Still check for legality in case the target supports [imm] but not [i+r]. |
| 2486 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 2487 | return true; |
| 2488 | AddrMode.HasBaseReg = false; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2489 | AddrMode.BaseReg = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | // If the base register is already taken, see if we can do [r+r]. |
| 2493 | if (AddrMode.Scale == 0) { |
| 2494 | AddrMode.Scale = 1; |
| 2495 | AddrMode.ScaledReg = Addr; |
| 2496 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 2497 | return true; |
| 2498 | AddrMode.Scale = 0; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2499 | AddrMode.ScaledReg = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2500 | } |
| 2501 | // Couldn't match. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2502 | TPT.rollback(LastKnownGood); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2503 | return false; |
| 2504 | } |
| 2505 | |
| 2506 | /// IsOperandAMemoryOperand - Check to see if all uses of OpVal by the specified |
| 2507 | /// inline asm call are due to memory operands. If so, return true, otherwise |
| 2508 | /// return false. |
| 2509 | static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal, |
| 2510 | const TargetLowering &TLI) { |
| 2511 | TargetLowering::AsmOperandInfoVector TargetConstraints = TLI.ParseConstraints(ImmutableCallSite(CI)); |
| 2512 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 2513 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2514 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2515 | // Compute the constraint code and ConstraintType to use. |
| 2516 | TLI.ComputeConstraintToUse(OpInfo, SDValue()); |
| 2517 | |
| 2518 | // If this asm operand is our Value*, and if it isn't an indirect memory |
| 2519 | // operand, we can't fold it! |
| 2520 | if (OpInfo.CallOperandVal == OpVal && |
| 2521 | (OpInfo.ConstraintType != TargetLowering::C_Memory || |
| 2522 | !OpInfo.isIndirect)) |
| 2523 | return false; |
| 2524 | } |
| 2525 | |
| 2526 | return true; |
| 2527 | } |
| 2528 | |
| 2529 | /// FindAllMemoryUses - Recursively walk all the uses of I until we find a |
| 2530 | /// memory use. If we find an obviously non-foldable instruction, return true. |
| 2531 | /// Add the ultimately found memory instructions to MemoryUses. |
| 2532 | static bool FindAllMemoryUses(Instruction *I, |
| 2533 | SmallVectorImpl<std::pair<Instruction*,unsigned> > &MemoryUses, |
| 2534 | SmallPtrSet<Instruction*, 16> &ConsideredInsts, |
| 2535 | const TargetLowering &TLI) { |
| 2536 | // If we already considered this instruction, we're done. |
| 2537 | if (!ConsideredInsts.insert(I)) |
| 2538 | return false; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2539 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2540 | // If this is an obviously unfoldable instruction, bail out. |
| 2541 | if (!MightBeFoldableInst(I)) |
| 2542 | return true; |
| 2543 | |
| 2544 | // Loop over all the uses, recursively processing them. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2545 | for (Use &U : I->uses()) { |
| 2546 | Instruction *UserI = cast<Instruction>(U.getUser()); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2547 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2548 | if (LoadInst *LI = dyn_cast<LoadInst>(UserI)) { |
| 2549 | MemoryUses.push_back(std::make_pair(LI, U.getOperandNo())); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2550 | continue; |
| 2551 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2552 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2553 | if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) { |
| 2554 | unsigned opNo = U.getOperandNo(); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2555 | if (opNo == 0) return true; // Storing addr, not into addr. |
| 2556 | MemoryUses.push_back(std::make_pair(SI, opNo)); |
| 2557 | continue; |
| 2558 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2559 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2560 | if (CallInst *CI = dyn_cast<CallInst>(UserI)) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2561 | InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue()); |
| 2562 | if (!IA) return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2563 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2564 | // If this is a memory operand, we're cool, otherwise bail out. |
| 2565 | if (!IsOperandAMemoryOperand(CI, IA, I, TLI)) |
| 2566 | return true; |
| 2567 | continue; |
| 2568 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2569 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2570 | if (FindAllMemoryUses(UserI, MemoryUses, ConsideredInsts, TLI)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2571 | return true; |
| 2572 | } |
| 2573 | |
| 2574 | return false; |
| 2575 | } |
| 2576 | |
| 2577 | /// ValueAlreadyLiveAtInst - Retrn true if Val is already known to be live at |
| 2578 | /// the use site that we're folding it into. If so, there is no cost to |
| 2579 | /// include it in the addressing mode. KnownLive1 and KnownLive2 are two values |
| 2580 | /// that we know are live at the instruction already. |
| 2581 | bool AddressingModeMatcher::ValueAlreadyLiveAtInst(Value *Val,Value *KnownLive1, |
| 2582 | Value *KnownLive2) { |
| 2583 | // If Val is either of the known-live values, we know it is live! |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2584 | if (Val == nullptr || Val == KnownLive1 || Val == KnownLive2) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2585 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2586 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2587 | // All values other than instructions and arguments (e.g. constants) are live. |
| 2588 | if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2589 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2590 | // If Val is a constant sized alloca in the entry block, it is live, this is |
| 2591 | // true because it is just a reference to the stack/frame pointer, which is |
| 2592 | // live for the whole function. |
| 2593 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Val)) |
| 2594 | if (AI->isStaticAlloca()) |
| 2595 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2596 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2597 | // Check to see if this value is already used in the memory instruction's |
| 2598 | // block. If so, it's already live into the block at the very least, so we |
| 2599 | // can reasonably fold it. |
| 2600 | return Val->isUsedInBasicBlock(MemoryInst->getParent()); |
| 2601 | } |
| 2602 | |
| 2603 | /// IsProfitableToFoldIntoAddressingMode - It is possible for the addressing |
| 2604 | /// mode of the machine to fold the specified instruction into a load or store |
| 2605 | /// that ultimately uses it. However, the specified instruction has multiple |
| 2606 | /// uses. Given this, it may actually increase register pressure to fold it |
| 2607 | /// into the load. For example, consider this code: |
| 2608 | /// |
| 2609 | /// X = ... |
| 2610 | /// Y = X+1 |
| 2611 | /// use(Y) -> nonload/store |
| 2612 | /// Z = Y+1 |
| 2613 | /// load Z |
| 2614 | /// |
| 2615 | /// In this case, Y has multiple uses, and can be folded into the load of Z |
| 2616 | /// (yielding load [X+2]). However, doing this will cause both "X" and "X+1" to |
| 2617 | /// be live at the use(Y) line. If we don't fold Y into load Z, we use one |
| 2618 | /// fewer register. Since Y can't be folded into "use(Y)" we don't increase the |
| 2619 | /// number of computations either. |
| 2620 | /// |
| 2621 | /// Note that this (like most of CodeGenPrepare) is just a rough heuristic. If |
| 2622 | /// X was live across 'load Z' for other reasons, we actually *would* want to |
| 2623 | /// fold the addressing mode in the Z case. This would make Y die earlier. |
| 2624 | bool AddressingModeMatcher:: |
| 2625 | IsProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore, |
| 2626 | ExtAddrMode &AMAfter) { |
| 2627 | if (IgnoreProfitability) return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2628 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2629 | // AMBefore is the addressing mode before this instruction was folded into it, |
| 2630 | // and AMAfter is the addressing mode after the instruction was folded. Get |
| 2631 | // the set of registers referenced by AMAfter and subtract out those |
| 2632 | // referenced by AMBefore: this is the set of values which folding in this |
| 2633 | // address extends the lifetime of. |
| 2634 | // |
| 2635 | // Note that there are only two potential values being referenced here, |
| 2636 | // BaseReg and ScaleReg (global addresses are always available, as are any |
| 2637 | // folded immediates). |
| 2638 | Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2639 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2640 | // If the BaseReg or ScaledReg was referenced by the previous addrmode, their |
| 2641 | // lifetime wasn't extended by adding this instruction. |
| 2642 | if (ValueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2643 | BaseReg = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2644 | if (ValueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2645 | ScaledReg = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2646 | |
| 2647 | // If folding this instruction (and it's subexprs) didn't extend any live |
| 2648 | // ranges, we're ok with it. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2649 | if (!BaseReg && !ScaledReg) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2650 | return true; |
| 2651 | |
| 2652 | // If all uses of this instruction are ultimately load/store/inlineasm's, |
| 2653 | // check to see if their addressing modes will include this instruction. If |
| 2654 | // so, we can fold it into all uses, so it doesn't matter if it has multiple |
| 2655 | // uses. |
| 2656 | SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses; |
| 2657 | SmallPtrSet<Instruction*, 16> ConsideredInsts; |
| 2658 | if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TLI)) |
| 2659 | return false; // Has a non-memory, non-foldable use! |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2660 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2661 | // Now that we know that all uses of this instruction are part of a chain of |
| 2662 | // computation involving only operations that could theoretically be folded |
| 2663 | // into a memory use, loop over each of these uses and see if they could |
| 2664 | // *actually* fold the instruction. |
| 2665 | SmallVector<Instruction*, 32> MatchedAddrModeInsts; |
| 2666 | for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) { |
| 2667 | Instruction *User = MemoryUses[i].first; |
| 2668 | unsigned OpNo = MemoryUses[i].second; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2669 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2670 | // Get the access type of this use. If the use isn't a pointer, we don't |
| 2671 | // know what it accesses. |
| 2672 | Value *Address = User->getOperand(OpNo); |
| 2673 | if (!Address->getType()->isPointerTy()) |
| 2674 | return false; |
Matt Arsenault | 8227b9f | 2013-09-06 00:37:24 +0000 | [diff] [blame] | 2675 | Type *AddressAccessTy = Address->getType()->getPointerElementType(); |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2676 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2677 | // Do a match against the root of this address, ignoring profitability. This |
| 2678 | // will tell us if the addressing mode for the memory operation will |
| 2679 | // *actually* cover the shared instruction. |
| 2680 | ExtAddrMode Result; |
Quentin Colombet | 5a69dda | 2014-02-11 01:59:02 +0000 | [diff] [blame] | 2681 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 2682 | TPT.getRestorationPoint(); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2683 | AddressingModeMatcher Matcher(MatchedAddrModeInsts, TLI, AddressAccessTy, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2684 | MemoryInst, Result, InsertedTruncs, |
| 2685 | PromotedInsts, TPT); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2686 | Matcher.IgnoreProfitability = true; |
| 2687 | bool Success = Matcher.MatchAddr(Address, 0); |
| 2688 | (void)Success; assert(Success && "Couldn't select *anything*?"); |
| 2689 | |
Quentin Colombet | 5a69dda | 2014-02-11 01:59:02 +0000 | [diff] [blame] | 2690 | // The match was to check the profitability, the changes made are not |
| 2691 | // part of the original matcher. Therefore, they should be dropped |
| 2692 | // otherwise the original matcher will not present the right state. |
| 2693 | TPT.rollback(LastKnownGood); |
| 2694 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2695 | // If the match didn't cover I, then it won't be shared by it. |
| 2696 | if (std::find(MatchedAddrModeInsts.begin(), MatchedAddrModeInsts.end(), |
| 2697 | I) == MatchedAddrModeInsts.end()) |
| 2698 | return false; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2699 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2700 | MatchedAddrModeInsts.clear(); |
| 2701 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2702 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2703 | return true; |
| 2704 | } |
| 2705 | |
| 2706 | } // end anonymous namespace |
| 2707 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2708 | /// IsNonLocalValue - Return true if the specified values are defined in a |
| 2709 | /// different basic block than BB. |
| 2710 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 2711 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 2712 | return I->getParent() != BB; |
| 2713 | return false; |
| 2714 | } |
| 2715 | |
Bob Wilson | 53bdae3 | 2009-12-03 21:47:07 +0000 | [diff] [blame] | 2716 | /// OptimizeMemoryInst - Load and Store Instructions often have |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2717 | /// addressing modes that can do significant amounts of computation. As such, |
| 2718 | /// instruction selection will try to get the load or store to do as much |
| 2719 | /// computation as possible for the program. The problem is that isel can only |
| 2720 | /// see within a single block. As such, we sink as much legal addressing mode |
| 2721 | /// stuff into the block as possible. |
Chris Lattner | 728f902 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 2722 | /// |
| 2723 | /// This method is used to optimize both load/store and inline asms with memory |
| 2724 | /// operands. |
Chris Lattner | 6d71b7f | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 2725 | bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2726 | Type *AccessTy) { |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2727 | Value *Repl = Addr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2728 | |
| 2729 | // Try to collapse single-value PHI nodes. This is necessary to undo |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 2730 | // unprofitable PRE transformations. |
Cameron Zwarich | 43cecb1 | 2011-01-03 06:33:01 +0000 | [diff] [blame] | 2731 | SmallVector<Value*, 8> worklist; |
| 2732 | SmallPtrSet<Value*, 16> Visited; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2733 | worklist.push_back(Addr); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2734 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2735 | // Use a worklist to iteratively look through PHI nodes, and ensure that |
| 2736 | // the addressing mode obtained from the non-PHI roots of the graph |
| 2737 | // are equivalent. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2738 | Value *Consensus = nullptr; |
Cameron Zwarich | b7f8eaa | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 2739 | unsigned NumUsesConsensus = 0; |
Cameron Zwarich | 13c885d | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 2740 | bool IsNumUsesConsensusValid = false; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2741 | SmallVector<Instruction*, 16> AddrModeInsts; |
| 2742 | ExtAddrMode AddrMode; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2743 | TypePromotionTransaction TPT; |
| 2744 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 2745 | TPT.getRestorationPoint(); |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2746 | while (!worklist.empty()) { |
| 2747 | Value *V = worklist.back(); |
| 2748 | worklist.pop_back(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2749 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2750 | // Break use-def graph loops. |
Nick Lewycky | a3e7ffd | 2011-09-29 23:40:12 +0000 | [diff] [blame] | 2751 | if (!Visited.insert(V)) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2752 | Consensus = nullptr; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2753 | break; |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 2754 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2755 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2756 | // For a PHI node, push all of its incoming values. |
| 2757 | if (PHINode *P = dyn_cast<PHINode>(V)) { |
| 2758 | for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) |
| 2759 | worklist.push_back(P->getIncomingValue(i)); |
| 2760 | continue; |
| 2761 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2762 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2763 | // For non-PHIs, determine the addressing mode being computed. |
| 2764 | SmallVector<Instruction*, 16> NewAddrModeInsts; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2765 | ExtAddrMode NewAddrMode = AddressingModeMatcher::Match( |
| 2766 | V, AccessTy, MemoryInst, NewAddrModeInsts, *TLI, InsertedTruncsSet, |
| 2767 | PromotedInsts, TPT); |
Cameron Zwarich | 13c885d | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 2768 | |
| 2769 | // This check is broken into two cases with very similar code to avoid using |
| 2770 | // getNumUses() as much as possible. Some values have a lot of uses, so |
| 2771 | // calling getNumUses() unconditionally caused a significant compile-time |
| 2772 | // regression. |
| 2773 | if (!Consensus) { |
| 2774 | Consensus = V; |
| 2775 | AddrMode = NewAddrMode; |
| 2776 | AddrModeInsts = NewAddrModeInsts; |
| 2777 | continue; |
| 2778 | } else if (NewAddrMode == AddrMode) { |
| 2779 | if (!IsNumUsesConsensusValid) { |
| 2780 | NumUsesConsensus = Consensus->getNumUses(); |
| 2781 | IsNumUsesConsensusValid = true; |
| 2782 | } |
| 2783 | |
| 2784 | // Ensure that the obtained addressing mode is equivalent to that obtained |
| 2785 | // for all other roots of the PHI traversal. Also, when choosing one |
| 2786 | // such root as representative, select the one with the most uses in order |
| 2787 | // to keep the cost modeling heuristics in AddressingModeMatcher |
| 2788 | // applicable. |
Cameron Zwarich | b7f8eaa | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 2789 | unsigned NumUses = V->getNumUses(); |
| 2790 | if (NumUses > NumUsesConsensus) { |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2791 | Consensus = V; |
Cameron Zwarich | b7f8eaa | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 2792 | NumUsesConsensus = NumUses; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2793 | AddrModeInsts = NewAddrModeInsts; |
| 2794 | } |
| 2795 | continue; |
| 2796 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2797 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2798 | Consensus = nullptr; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2799 | break; |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 2800 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2801 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 2802 | // If the addressing mode couldn't be determined, or if multiple different |
| 2803 | // ones were determined, bail out now. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2804 | if (!Consensus) { |
| 2805 | TPT.rollback(LastKnownGood); |
| 2806 | return false; |
| 2807 | } |
| 2808 | TPT.commit(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2809 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2810 | // Check to see if any of the instructions supersumed by this addr mode are |
| 2811 | // non-local to I's BB. |
| 2812 | bool AnyNonLocal = false; |
| 2813 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
Chris Lattner | 6d71b7f | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 2814 | if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) { |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2815 | AnyNonLocal = true; |
| 2816 | break; |
| 2817 | } |
| 2818 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 2819 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2820 | // If all the instructions matched are already in this BB, don't do anything. |
| 2821 | if (!AnyNonLocal) { |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 2822 | DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2823 | return false; |
| 2824 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 2825 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2826 | // Insert this computation right after this user. Since our caller is |
| 2827 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 2828 | // guaranteed to happen later. |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 2829 | IRBuilder<> Builder(MemoryInst); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 2830 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2831 | // Now that we determined the addressing expression we want to use and know |
| 2832 | // that we have to sink it into this block. Check to see if we have already |
| 2833 | // done this for some other load/store instr in this block. If so, reuse the |
| 2834 | // computation. |
| 2835 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 2836 | if (SunkAddr) { |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 2837 | DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " |
Dan Gohman | 29f2baf | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 2838 | << *MemoryInst); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2839 | if (SunkAddr->getType() != Addr->getType()) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2840 | SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType()); |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 2841 | } else if (AddrSinkUsingGEPs || (!AddrSinkUsingGEPs.getNumOccurrences() && |
| 2842 | TM && TM->getSubtarget<TargetSubtargetInfo>().useAA())) { |
| 2843 | // By default, we use the GEP-based method when AA is used later. This |
| 2844 | // prevents new inttoptr/ptrtoint pairs from degrading AA capabilities. |
| 2845 | DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
| 2846 | << *MemoryInst); |
| 2847 | Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(Addr->getType()); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2848 | Value *ResultPtr = nullptr, *ResultIndex = nullptr; |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 2849 | |
| 2850 | // First, find the pointer. |
| 2851 | if (AddrMode.BaseReg && AddrMode.BaseReg->getType()->isPointerTy()) { |
| 2852 | ResultPtr = AddrMode.BaseReg; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2853 | AddrMode.BaseReg = nullptr; |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | if (AddrMode.Scale && AddrMode.ScaledReg->getType()->isPointerTy()) { |
| 2857 | // We can't add more than one pointer together, nor can we scale a |
| 2858 | // pointer (both of which seem meaningless). |
| 2859 | if (ResultPtr || AddrMode.Scale != 1) |
| 2860 | return false; |
| 2861 | |
| 2862 | ResultPtr = AddrMode.ScaledReg; |
| 2863 | AddrMode.Scale = 0; |
| 2864 | } |
| 2865 | |
| 2866 | if (AddrMode.BaseGV) { |
| 2867 | if (ResultPtr) |
| 2868 | return false; |
| 2869 | |
| 2870 | ResultPtr = AddrMode.BaseGV; |
| 2871 | } |
| 2872 | |
| 2873 | // If the real base value actually came from an inttoptr, then the matcher |
| 2874 | // will look through it and provide only the integer value. In that case, |
| 2875 | // use it here. |
| 2876 | if (!ResultPtr && AddrMode.BaseReg) { |
| 2877 | ResultPtr = |
| 2878 | Builder.CreateIntToPtr(AddrMode.BaseReg, Addr->getType(), "sunkaddr"); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2879 | AddrMode.BaseReg = nullptr; |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 2880 | } else if (!ResultPtr && AddrMode.Scale == 1) { |
| 2881 | ResultPtr = |
| 2882 | Builder.CreateIntToPtr(AddrMode.ScaledReg, Addr->getType(), "sunkaddr"); |
| 2883 | AddrMode.Scale = 0; |
| 2884 | } |
| 2885 | |
| 2886 | if (!ResultPtr && |
| 2887 | !AddrMode.BaseReg && !AddrMode.Scale && !AddrMode.BaseOffs) { |
| 2888 | SunkAddr = Constant::getNullValue(Addr->getType()); |
| 2889 | } else if (!ResultPtr) { |
| 2890 | return false; |
| 2891 | } else { |
| 2892 | Type *I8PtrTy = |
| 2893 | Builder.getInt8PtrTy(Addr->getType()->getPointerAddressSpace()); |
| 2894 | |
| 2895 | // Start with the base register. Do this first so that subsequent address |
| 2896 | // matching finds it last, which will prevent it from trying to match it |
| 2897 | // as the scaled value in case it happens to be a mul. That would be |
| 2898 | // problematic if we've sunk a different mul for the scale, because then |
| 2899 | // we'd end up sinking both muls. |
| 2900 | if (AddrMode.BaseReg) { |
| 2901 | Value *V = AddrMode.BaseReg; |
| 2902 | if (V->getType() != IntPtrTy) |
| 2903 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); |
| 2904 | |
| 2905 | ResultIndex = V; |
| 2906 | } |
| 2907 | |
| 2908 | // Add the scale value. |
| 2909 | if (AddrMode.Scale) { |
| 2910 | Value *V = AddrMode.ScaledReg; |
| 2911 | if (V->getType() == IntPtrTy) { |
| 2912 | // done. |
| 2913 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 2914 | cast<IntegerType>(V->getType())->getBitWidth()) { |
| 2915 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); |
| 2916 | } else { |
| 2917 | // It is only safe to sign extend the BaseReg if we know that the math |
| 2918 | // required to create it did not overflow before we extend it. Since |
| 2919 | // the original IR value was tossed in favor of a constant back when |
| 2920 | // the AddrMode was created we need to bail out gracefully if widths |
| 2921 | // do not match instead of extending it. |
| 2922 | Instruction *I = dyn_cast_or_null<Instruction>(ResultIndex); |
| 2923 | if (I && (ResultIndex != AddrMode.BaseReg)) |
| 2924 | I->eraseFromParent(); |
| 2925 | return false; |
| 2926 | } |
| 2927 | |
| 2928 | if (AddrMode.Scale != 1) |
| 2929 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), |
| 2930 | "sunkaddr"); |
| 2931 | if (ResultIndex) |
| 2932 | ResultIndex = Builder.CreateAdd(ResultIndex, V, "sunkaddr"); |
| 2933 | else |
| 2934 | ResultIndex = V; |
| 2935 | } |
| 2936 | |
| 2937 | // Add in the Base Offset if present. |
| 2938 | if (AddrMode.BaseOffs) { |
| 2939 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
| 2940 | if (ResultIndex) { |
| 2941 | // We need to add this separately from the scale above to help with |
| 2942 | // SDAG consecutive load/store merging. |
| 2943 | if (ResultPtr->getType() != I8PtrTy) |
| 2944 | ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy); |
| 2945 | ResultPtr = Builder.CreateGEP(ResultPtr, ResultIndex, "sunkaddr"); |
| 2946 | } |
| 2947 | |
| 2948 | ResultIndex = V; |
| 2949 | } |
| 2950 | |
| 2951 | if (!ResultIndex) { |
| 2952 | SunkAddr = ResultPtr; |
| 2953 | } else { |
| 2954 | if (ResultPtr->getType() != I8PtrTy) |
| 2955 | ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy); |
| 2956 | SunkAddr = Builder.CreateGEP(ResultPtr, ResultIndex, "sunkaddr"); |
| 2957 | } |
| 2958 | |
| 2959 | if (SunkAddr->getType() != Addr->getType()) |
| 2960 | SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType()); |
| 2961 | } |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2962 | } else { |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 2963 | DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
Dan Gohman | 29f2baf | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 2964 | << *MemoryInst); |
Matt Arsenault | 37d42ec | 2013-09-06 00:18:43 +0000 | [diff] [blame] | 2965 | Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(Addr->getType()); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2966 | Value *Result = nullptr; |
Dan Gohman | ca19445 | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 2967 | |
| 2968 | // Start with the base register. Do this first so that subsequent address |
| 2969 | // matching finds it last, which will prevent it from trying to match it |
| 2970 | // as the scaled value in case it happens to be a mul. That would be |
| 2971 | // problematic if we've sunk a different mul for the scale, because then |
| 2972 | // we'd end up sinking both muls. |
| 2973 | if (AddrMode.BaseReg) { |
| 2974 | Value *V = AddrMode.BaseReg; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2975 | if (V->getType()->isPointerTy()) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 2976 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Dan Gohman | ca19445 | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 2977 | if (V->getType() != IntPtrTy) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 2978 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); |
Dan Gohman | ca19445 | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 2979 | Result = V; |
| 2980 | } |
| 2981 | |
| 2982 | // Add the scale value. |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2983 | if (AddrMode.Scale) { |
| 2984 | Value *V = AddrMode.ScaledReg; |
| 2985 | if (V->getType() == IntPtrTy) { |
| 2986 | // done. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2987 | } else if (V->getType()->isPointerTy()) { |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 2988 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2989 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 2990 | cast<IntegerType>(V->getType())->getBitWidth()) { |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 2991 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 2992 | } else { |
Jim Grosbach | ed2cd39 | 2014-03-26 17:27:01 +0000 | [diff] [blame] | 2993 | // It is only safe to sign extend the BaseReg if we know that the math |
| 2994 | // required to create it did not overflow before we extend it. Since |
| 2995 | // the original IR value was tossed in favor of a constant back when |
| 2996 | // the AddrMode was created we need to bail out gracefully if widths |
| 2997 | // do not match instead of extending it. |
Jim Grosbach | 83b44e1 | 2014-04-10 00:27:45 +0000 | [diff] [blame] | 2998 | Instruction *I = dyn_cast<Instruction>(Result); |
| 2999 | if (I && (Result != AddrMode.BaseReg)) |
| 3000 | I->eraseFromParent(); |
Jim Grosbach | ed2cd39 | 2014-03-26 17:27:01 +0000 | [diff] [blame] | 3001 | return false; |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3002 | } |
| 3003 | if (AddrMode.Scale != 1) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3004 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), |
| 3005 | "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3006 | if (Result) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3007 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3008 | else |
| 3009 | Result = V; |
| 3010 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3011 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3012 | // Add in the BaseGV if present. |
| 3013 | if (AddrMode.BaseGV) { |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3014 | Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3015 | if (Result) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3016 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3017 | else |
| 3018 | Result = V; |
| 3019 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3020 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3021 | // Add in the Base Offset if present. |
| 3022 | if (AddrMode.BaseOffs) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3023 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3024 | if (Result) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3025 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3026 | else |
| 3027 | Result = V; |
| 3028 | } |
| 3029 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3030 | if (!Result) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3031 | SunkAddr = Constant::getNullValue(Addr->getType()); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3032 | else |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3033 | SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3034 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3035 | |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 3036 | MemoryInst->replaceUsesOfWith(Repl, SunkAddr); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3037 | |
Chris Lattner | af1bcce | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 3038 | // If we have no uses, recursively delete the value and all dead instructions |
| 3039 | // using it. |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 3040 | if (Repl->use_empty()) { |
Chris Lattner | af1bcce | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 3041 | // This can cause recursive deletion, which can invalidate our iterator. |
| 3042 | // Use a WeakVH to hold onto it in case this happens. |
| 3043 | WeakVH IterHandle(CurInstIterator); |
| 3044 | BasicBlock *BB = CurInstIterator->getParent(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3045 | |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 3046 | RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo); |
Chris Lattner | af1bcce | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 3047 | |
| 3048 | if (IterHandle != CurInstIterator) { |
| 3049 | // If the iterator instruction was recursively deleted, start over at the |
| 3050 | // start of the block. |
| 3051 | CurInstIterator = BB->begin(); |
| 3052 | SunkAddrs.clear(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3053 | } |
Dale Johannesen | b67a6e66 | 2010-03-31 20:37:15 +0000 | [diff] [blame] | 3054 | } |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 3055 | ++NumMemoryInsts; |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3056 | return true; |
| 3057 | } |
| 3058 | |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3059 | /// OptimizeInlineAsmInst - If there are any memory operands, use |
Chris Lattner | 728f902 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 3060 | /// OptimizeMemoryInst to sink their address computing into the block when |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3061 | /// possible / profitable. |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 3062 | bool CodeGenPrepare::OptimizeInlineAsmInst(CallInst *CS) { |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3063 | bool MadeChange = false; |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3064 | |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3065 | TargetLowering::AsmOperandInfoVector |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 3066 | TargetConstraints = TLI->ParseConstraints(CS); |
Dale Johannesen | f95f59a | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 3067 | unsigned ArgNo = 0; |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 3068 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 3069 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3070 | |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3071 | // Compute the constraint code and ConstraintType to use. |
Dale Johannesen | ce97d55 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 3072 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3073 | |
Eli Friedman | 666bbe3 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 3074 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 3075 | OpInfo.isIndirect) { |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 3076 | Value *OpVal = CS->getArgOperand(ArgNo++); |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3077 | MadeChange |= OptimizeMemoryInst(CS, OpVal, OpVal->getType()); |
Dale Johannesen | f95f59a | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 3078 | } else if (OpInfo.Type == InlineAsm::isInput) |
| 3079 | ArgNo++; |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | return MadeChange; |
| 3083 | } |
| 3084 | |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3085 | /// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same |
| 3086 | /// basic block as the load, unless conditions are unfavorable. This allows |
| 3087 | /// SelectionDAG to fold the extend into the load. |
| 3088 | /// |
| 3089 | bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) { |
| 3090 | // Look for a load being extended. |
| 3091 | LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0)); |
| 3092 | if (!LI) return false; |
| 3093 | |
| 3094 | // If they're already in the same block, there's nothing to do. |
| 3095 | if (LI->getParent() == I->getParent()) |
| 3096 | return false; |
| 3097 | |
| 3098 | // If the load has other users and the truncate is not free, this probably |
| 3099 | // isn't worthwhile. |
| 3100 | if (!LI->hasOneUse() && |
Bob Wilson | b6832a4 | 2010-09-22 18:44:56 +0000 | [diff] [blame] | 3101 | TLI && (TLI->isTypeLegal(TLI->getValueType(LI->getType())) || |
| 3102 | !TLI->isTypeLegal(TLI->getValueType(I->getType()))) && |
Bob Wilson | 4ddcb6a | 2010-09-21 21:54:27 +0000 | [diff] [blame] | 3103 | !TLI->isTruncateFree(I->getType(), LI->getType())) |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3104 | return false; |
| 3105 | |
| 3106 | // Check whether the target supports casts folded into loads. |
| 3107 | unsigned LType; |
| 3108 | if (isa<ZExtInst>(I)) |
| 3109 | LType = ISD::ZEXTLOAD; |
| 3110 | else { |
| 3111 | assert(isa<SExtInst>(I) && "Unexpected ext type!"); |
| 3112 | LType = ISD::SEXTLOAD; |
| 3113 | } |
Patrik Hagglund | e98b7a0 | 2012-12-11 11:14:33 +0000 | [diff] [blame] | 3114 | if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType()))) |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3115 | return false; |
| 3116 | |
| 3117 | // Move the extend into the same block as the load, so that SelectionDAG |
| 3118 | // can fold it. |
| 3119 | I->removeFromParent(); |
| 3120 | I->insertAfter(LI); |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 3121 | ++NumExtsMoved; |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3122 | return true; |
| 3123 | } |
| 3124 | |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3125 | bool CodeGenPrepare::OptimizeExtUses(Instruction *I) { |
| 3126 | BasicBlock *DefBB = I->getParent(); |
| 3127 | |
Bob Wilson | ff714f9 | 2010-09-21 21:44:14 +0000 | [diff] [blame] | 3128 | // If the result of a {s|z}ext and its source are both live out, rewrite all |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3129 | // other uses of the source with result of extension. |
| 3130 | Value *Src = I->getOperand(0); |
| 3131 | if (Src->hasOneUse()) |
| 3132 | return false; |
| 3133 | |
Evan Cheng | 2011df4 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 3134 | // Only do this xform if truncating is free. |
Gabor Greif | aa26172 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 3135 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | 37c36ed | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 3136 | return false; |
| 3137 | |
Evan Cheng | 7bc8942 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 3138 | // Only safe to perform the optimization if the source is also defined in |
Evan Cheng | 63d33cf | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 3139 | // this block. |
| 3140 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 7bc8942 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 3141 | return false; |
| 3142 | |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3143 | bool DefIsLiveOut = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3144 | for (User *U : I->users()) { |
| 3145 | Instruction *UI = cast<Instruction>(U); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3146 | |
| 3147 | // Figure out which BB this ext is used in. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3148 | BasicBlock *UserBB = UI->getParent(); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3149 | if (UserBB == DefBB) continue; |
| 3150 | DefIsLiveOut = true; |
| 3151 | break; |
| 3152 | } |
| 3153 | if (!DefIsLiveOut) |
| 3154 | return false; |
| 3155 | |
Jim Grosbach | 0f38c1e | 2013-04-15 17:40:48 +0000 | [diff] [blame] | 3156 | // Make sure none of the uses are PHI nodes. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3157 | for (User *U : Src->users()) { |
| 3158 | Instruction *UI = cast<Instruction>(U); |
| 3159 | BasicBlock *UserBB = UI->getParent(); |
Evan Cheng | 37c36ed | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 3160 | if (UserBB == DefBB) continue; |
| 3161 | // Be conservative. We don't want this xform to end up introducing |
| 3162 | // reloads just before load / store instructions. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3163 | if (isa<PHINode>(UI) || isa<LoadInst>(UI) || isa<StoreInst>(UI)) |
Evan Cheng | 63d33cf | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 3164 | return false; |
| 3165 | } |
| 3166 | |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3167 | // InsertedTruncs - Only insert one trunc in each block once. |
| 3168 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 3169 | |
| 3170 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3171 | for (Use &U : Src->uses()) { |
| 3172 | Instruction *User = cast<Instruction>(U.getUser()); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3173 | |
| 3174 | // Figure out which BB this ext is used in. |
| 3175 | BasicBlock *UserBB = User->getParent(); |
| 3176 | if (UserBB == DefBB) continue; |
| 3177 | |
| 3178 | // Both src and def are live in this block. Rewrite the use. |
| 3179 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 3180 | |
| 3181 | if (!InsertedTrunc) { |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 3182 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3183 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 3184 | InsertedTruncsSet.insert(InsertedTrunc); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | // Replace a use of the {s|z}ext source with a use of the result. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3188 | U = InsertedTrunc; |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 3189 | ++NumExtUses; |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3190 | MadeChange = true; |
| 3191 | } |
| 3192 | |
| 3193 | return MadeChange; |
| 3194 | } |
| 3195 | |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3196 | /// isFormingBranchFromSelectProfitable - Returns true if a SelectInst should be |
| 3197 | /// turned into an explicit branch. |
| 3198 | static bool isFormingBranchFromSelectProfitable(SelectInst *SI) { |
| 3199 | // FIXME: This should use the same heuristics as IfConversion to determine |
| 3200 | // whether a select is better represented as a branch. This requires that |
| 3201 | // branch probability metadata is preserved for the select, which is not the |
| 3202 | // case currently. |
| 3203 | |
| 3204 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 3205 | |
| 3206 | // If the branch is predicted right, an out of order CPU can avoid blocking on |
| 3207 | // the compare. Emit cmovs on compares with a memory operand as branches to |
| 3208 | // avoid stalls on the load from memory. If the compare has more than one use |
| 3209 | // there's probably another cmov or setcc around so it's not worth emitting a |
| 3210 | // branch. |
| 3211 | if (!Cmp) |
| 3212 | return false; |
| 3213 | |
| 3214 | Value *CmpOp0 = Cmp->getOperand(0); |
| 3215 | Value *CmpOp1 = Cmp->getOperand(1); |
| 3216 | |
| 3217 | // We check that the memory operand has one use to avoid uses of the loaded |
| 3218 | // value directly after the compare, making branches unprofitable. |
| 3219 | return Cmp->hasOneUse() && |
| 3220 | ((isa<LoadInst>(CmpOp0) && CmpOp0->hasOneUse()) || |
| 3221 | (isa<LoadInst>(CmpOp1) && CmpOp1->hasOneUse())); |
| 3222 | } |
| 3223 | |
| 3224 | |
Nadav Rotem | 9d83202 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 3225 | /// If we have a SelectInst that will likely profit from branch prediction, |
| 3226 | /// turn it into a branch. |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3227 | bool CodeGenPrepare::OptimizeSelectInst(SelectInst *SI) { |
Nadav Rotem | 9d83202 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 3228 | bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1); |
| 3229 | |
| 3230 | // Can we convert the 'select' to CF ? |
| 3231 | if (DisableSelectToBranch || OptSize || !TLI || VectorCond) |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3232 | return false; |
| 3233 | |
Nadav Rotem | 9d83202 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 3234 | TargetLowering::SelectSupportKind SelectKind; |
| 3235 | if (VectorCond) |
| 3236 | SelectKind = TargetLowering::VectorMaskSelect; |
| 3237 | else if (SI->getType()->isVectorTy()) |
| 3238 | SelectKind = TargetLowering::ScalarCondVectorVal; |
| 3239 | else |
| 3240 | SelectKind = TargetLowering::ScalarValSelect; |
| 3241 | |
| 3242 | // Do we have efficient codegen support for this kind of 'selects' ? |
| 3243 | if (TLI->isSelectSupported(SelectKind)) { |
| 3244 | // We have efficient codegen support for the select instruction. |
| 3245 | // Check if it is profitable to keep this 'select'. |
| 3246 | if (!TLI->isPredictableSelectExpensive() || |
| 3247 | !isFormingBranchFromSelectProfitable(SI)) |
| 3248 | return false; |
| 3249 | } |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3250 | |
| 3251 | ModifiedDT = true; |
| 3252 | |
| 3253 | // First, we split the block containing the select into 2 blocks. |
| 3254 | BasicBlock *StartBlock = SI->getParent(); |
| 3255 | BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(SI)); |
| 3256 | BasicBlock *NextBlock = StartBlock->splitBasicBlock(SplitPt, "select.end"); |
| 3257 | |
| 3258 | // Create a new block serving as the landing pad for the branch. |
| 3259 | BasicBlock *SmallBlock = BasicBlock::Create(SI->getContext(), "select.mid", |
| 3260 | NextBlock->getParent(), NextBlock); |
| 3261 | |
| 3262 | // Move the unconditional branch from the block with the select in it into our |
| 3263 | // landing pad block. |
| 3264 | StartBlock->getTerminator()->eraseFromParent(); |
| 3265 | BranchInst::Create(NextBlock, SmallBlock); |
| 3266 | |
| 3267 | // Insert the real conditional branch based on the original condition. |
| 3268 | BranchInst::Create(NextBlock, SmallBlock, SI->getCondition(), SI); |
| 3269 | |
| 3270 | // The select itself is replaced with a PHI Node. |
| 3271 | PHINode *PN = PHINode::Create(SI->getType(), 2, "", NextBlock->begin()); |
| 3272 | PN->takeName(SI); |
| 3273 | PN->addIncoming(SI->getTrueValue(), StartBlock); |
| 3274 | PN->addIncoming(SI->getFalseValue(), SmallBlock); |
| 3275 | SI->replaceAllUsesWith(PN); |
| 3276 | SI->eraseFromParent(); |
| 3277 | |
| 3278 | // Instruct OptimizeBlock to skip to the next block. |
| 3279 | CurInstIterator = StartBlock->end(); |
| 3280 | ++NumSelectsExpanded; |
| 3281 | return true; |
| 3282 | } |
| 3283 | |
Benjamin Kramer | 573ff36 | 2014-03-01 17:24:40 +0000 | [diff] [blame] | 3284 | static bool isBroadcastShuffle(ShuffleVectorInst *SVI) { |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3285 | SmallVector<int, 16> Mask(SVI->getShuffleMask()); |
| 3286 | int SplatElem = -1; |
| 3287 | for (unsigned i = 0; i < Mask.size(); ++i) { |
| 3288 | if (SplatElem != -1 && Mask[i] != -1 && Mask[i] != SplatElem) |
| 3289 | return false; |
| 3290 | SplatElem = Mask[i]; |
| 3291 | } |
| 3292 | |
| 3293 | return true; |
| 3294 | } |
| 3295 | |
| 3296 | /// Some targets have expensive vector shifts if the lanes aren't all the same |
| 3297 | /// (e.g. x86 only introduced "vpsllvd" and friends with AVX2). In these cases |
| 3298 | /// it's often worth sinking a shufflevector splat down to its use so that |
| 3299 | /// codegen can spot all lanes are identical. |
| 3300 | bool CodeGenPrepare::OptimizeShuffleVectorInst(ShuffleVectorInst *SVI) { |
| 3301 | BasicBlock *DefBB = SVI->getParent(); |
| 3302 | |
| 3303 | // Only do this xform if variable vector shifts are particularly expensive. |
| 3304 | if (!TLI || !TLI->isVectorShiftByScalarCheap(SVI->getType())) |
| 3305 | return false; |
| 3306 | |
| 3307 | // We only expect better codegen by sinking a shuffle if we can recognise a |
| 3308 | // constant splat. |
| 3309 | if (!isBroadcastShuffle(SVI)) |
| 3310 | return false; |
| 3311 | |
| 3312 | // InsertedShuffles - Only insert a shuffle in each block once. |
| 3313 | DenseMap<BasicBlock*, Instruction*> InsertedShuffles; |
| 3314 | |
| 3315 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3316 | for (User *U : SVI->users()) { |
| 3317 | Instruction *UI = cast<Instruction>(U); |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3318 | |
| 3319 | // Figure out which BB this ext is used in. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3320 | BasicBlock *UserBB = UI->getParent(); |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3321 | if (UserBB == DefBB) continue; |
| 3322 | |
| 3323 | // For now only apply this when the splat is used by a shift instruction. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3324 | if (!UI->isShift()) continue; |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3325 | |
| 3326 | // Everything checks out, sink the shuffle if the user's block doesn't |
| 3327 | // already have a copy. |
| 3328 | Instruction *&InsertedShuffle = InsertedShuffles[UserBB]; |
| 3329 | |
| 3330 | if (!InsertedShuffle) { |
| 3331 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
| 3332 | InsertedShuffle = new ShuffleVectorInst(SVI->getOperand(0), |
| 3333 | SVI->getOperand(1), |
| 3334 | SVI->getOperand(2), "", InsertPt); |
| 3335 | } |
| 3336 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3337 | UI->replaceUsesOfWith(SVI, InsertedShuffle); |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3338 | MadeChange = true; |
| 3339 | } |
| 3340 | |
| 3341 | // If we removed all uses, nuke the shuffle. |
| 3342 | if (SVI->use_empty()) { |
| 3343 | SVI->eraseFromParent(); |
| 3344 | MadeChange = true; |
| 3345 | } |
| 3346 | |
| 3347 | return MadeChange; |
| 3348 | } |
| 3349 | |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3350 | bool CodeGenPrepare::OptimizeInst(Instruction *I) { |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3351 | if (PHINode *P = dyn_cast<PHINode>(I)) { |
| 3352 | // It is possible for very late stage optimizations (such as SimplifyCFG) |
| 3353 | // to introduce PHI nodes too late to be cleaned up. If we detect such a |
| 3354 | // trivial PHI, go ahead and zap it here. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3355 | if (Value *V = SimplifyInstruction(P, TLI ? TLI->getDataLayout() : nullptr, |
Benjamin Kramer | 30d249a | 2013-09-24 16:37:40 +0000 | [diff] [blame] | 3356 | TLInfo, DT)) { |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3357 | P->replaceAllUsesWith(V); |
| 3358 | P->eraseFromParent(); |
| 3359 | ++NumPHIsElim; |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3360 | return true; |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3361 | } |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3362 | return false; |
| 3363 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3364 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3365 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3366 | // If the source of the cast is a constant, then this should have |
| 3367 | // already been constant folded. The only reason NOT to constant fold |
| 3368 | // it is if something (e.g. LSR) was careful to place the constant |
| 3369 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 3370 | // the address of globals out of a loop). If this is the case, we don't |
| 3371 | // want to forward-subst the cast. |
| 3372 | if (isa<Constant>(CI->getOperand(0))) |
| 3373 | return false; |
| 3374 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3375 | if (TLI && OptimizeNoopCopyExpression(CI, *TLI)) |
| 3376 | return true; |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3377 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3378 | if (isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 3379 | /// Sink a zext or sext into its user blocks if the target type doesn't |
| 3380 | /// fit in one register |
| 3381 | if (TLI && TLI->getTypeAction(CI->getContext(), |
| 3382 | TLI->getValueType(CI->getType())) == |
| 3383 | TargetLowering::TypeExpandInteger) { |
| 3384 | return SinkCast(CI); |
| 3385 | } else { |
| 3386 | bool MadeChange = MoveExtToFormExtLoad(I); |
| 3387 | return MadeChange | OptimizeExtUses(I); |
| 3388 | } |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3389 | } |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3390 | return false; |
| 3391 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3392 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3393 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
Hal Finkel | decb024 | 2014-01-02 21:13:43 +0000 | [diff] [blame] | 3394 | if (!TLI || !TLI->hasMultipleConditionRegisters()) |
| 3395 | return OptimizeCmpExpression(CI); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3396 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3397 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3398 | if (TLI) |
Hans Wennborg | f325483 | 2012-10-30 11:23:25 +0000 | [diff] [blame] | 3399 | return OptimizeMemoryInst(I, I->getOperand(0), LI->getType()); |
| 3400 | return false; |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3401 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3402 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3403 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3404 | if (TLI) |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3405 | return OptimizeMemoryInst(I, SI->getOperand(1), |
| 3406 | SI->getOperand(0)->getType()); |
| 3407 | return false; |
| 3408 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3409 | |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 3410 | BinaryOperator *BinOp = dyn_cast<BinaryOperator>(I); |
| 3411 | |
| 3412 | if (BinOp && (BinOp->getOpcode() == Instruction::AShr || |
| 3413 | BinOp->getOpcode() == Instruction::LShr)) { |
| 3414 | ConstantInt *CI = dyn_cast<ConstantInt>(BinOp->getOperand(1)); |
| 3415 | if (TLI && CI && TLI->hasExtractBitsInsn()) |
| 3416 | return OptimizeExtractBits(BinOp, CI, *TLI); |
| 3417 | |
| 3418 | return false; |
| 3419 | } |
| 3420 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3421 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Cameron Zwarich | d28c78e | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 3422 | if (GEPI->hasAllZeroIndices()) { |
| 3423 | /// The GEP operand must be a pointer, so must its result -> BitCast |
| 3424 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
| 3425 | GEPI->getName(), GEPI); |
| 3426 | GEPI->replaceAllUsesWith(NC); |
| 3427 | GEPI->eraseFromParent(); |
| 3428 | ++NumGEPsElim; |
Cameron Zwarich | d28c78e | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 3429 | OptimizeInst(NC); |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3430 | return true; |
Cameron Zwarich | d28c78e | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 3431 | } |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3432 | return false; |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3433 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3434 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3435 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 3436 | return OptimizeCallInst(CI); |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3437 | |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3438 | if (SelectInst *SI = dyn_cast<SelectInst>(I)) |
| 3439 | return OptimizeSelectInst(SI); |
| 3440 | |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3441 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) |
| 3442 | return OptimizeShuffleVectorInst(SVI); |
| 3443 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 3444 | return false; |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 3447 | // In this pass we look for GEP and cast instructions that are used |
| 3448 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 3449 | // selection. |
| 3450 | bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) { |
Cameron Zwarich | ce3b930 | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 3451 | SunkAddrs.clear(); |
Cameron Zwarich | 5dd2aa2 | 2011-03-02 03:31:46 +0000 | [diff] [blame] | 3452 | bool MadeChange = false; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3453 | |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 3454 | CurInstIterator = BB.begin(); |
Hans Wennborg | 02fbc71 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 3455 | while (CurInstIterator != BB.end()) |
Chris Lattner | 1b93be5 | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 3456 | MadeChange |= OptimizeInst(CurInstIterator++); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3457 | |
Benjamin Kramer | 455fa35 | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 3458 | MadeChange |= DupRetToEnableTailCallOpts(&BB); |
| 3459 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 3460 | return MadeChange; |
| 3461 | } |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 3462 | |
| 3463 | // llvm.dbg.value is far away from the value then iSel may not be able |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3464 | // handle it properly. iSel will drop llvm.dbg.value if it can not |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 3465 | // find a node corresponding to the value. |
| 3466 | bool CodeGenPrepare::PlaceDbgValues(Function &F) { |
| 3467 | bool MadeChange = false; |
| 3468 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3469 | Instruction *PrevNonDbgInst = nullptr; |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 3470 | for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) { |
| 3471 | Instruction *Insn = BI; ++BI; |
| 3472 | DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn); |
| 3473 | if (!DVI) { |
| 3474 | PrevNonDbgInst = Insn; |
| 3475 | continue; |
| 3476 | } |
| 3477 | |
| 3478 | Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue()); |
| 3479 | if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) { |
| 3480 | DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI); |
| 3481 | DVI->removeFromParent(); |
| 3482 | if (isa<PHINode>(VI)) |
| 3483 | DVI->insertBefore(VI->getParent()->getFirstInsertionPt()); |
| 3484 | else |
| 3485 | DVI->insertAfter(VI); |
| 3486 | MadeChange = true; |
| 3487 | ++NumDbgValueMoved; |
| 3488 | } |
| 3489 | } |
| 3490 | } |
| 3491 | return MadeChange; |
| 3492 | } |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 3493 | |
| 3494 | // If there is a sequence that branches based on comparing a single bit |
| 3495 | // against zero that can be combined into a single instruction, and the |
| 3496 | // target supports folding these into a single instruction, sink the |
| 3497 | // mask and compare into the branch uses. Do this before OptimizeBlock -> |
| 3498 | // OptimizeInst -> OptimizeCmpExpression, which perturbs the pattern being |
| 3499 | // searched for. |
| 3500 | bool CodeGenPrepare::sinkAndCmp(Function &F) { |
| 3501 | if (!EnableAndCmpSinking) |
| 3502 | return false; |
| 3503 | if (!TLI || !TLI->isMaskAndBranchFoldingLegal()) |
| 3504 | return false; |
| 3505 | bool MadeChange = false; |
| 3506 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) { |
| 3507 | BasicBlock *BB = I++; |
| 3508 | |
| 3509 | // Does this BB end with the following? |
| 3510 | // %andVal = and %val, #single-bit-set |
| 3511 | // %icmpVal = icmp %andResult, 0 |
| 3512 | // br i1 %cmpVal label %dest1, label %dest2" |
| 3513 | BranchInst *Brcc = dyn_cast<BranchInst>(BB->getTerminator()); |
| 3514 | if (!Brcc || !Brcc->isConditional()) |
| 3515 | continue; |
| 3516 | ICmpInst *Cmp = dyn_cast<ICmpInst>(Brcc->getOperand(0)); |
| 3517 | if (!Cmp || Cmp->getParent() != BB) |
| 3518 | continue; |
| 3519 | ConstantInt *Zero = dyn_cast<ConstantInt>(Cmp->getOperand(1)); |
| 3520 | if (!Zero || !Zero->isZero()) |
| 3521 | continue; |
| 3522 | Instruction *And = dyn_cast<Instruction>(Cmp->getOperand(0)); |
| 3523 | if (!And || And->getOpcode() != Instruction::And || And->getParent() != BB) |
| 3524 | continue; |
| 3525 | ConstantInt* Mask = dyn_cast<ConstantInt>(And->getOperand(1)); |
| 3526 | if (!Mask || !Mask->getUniqueInteger().isPowerOf2()) |
| 3527 | continue; |
| 3528 | DEBUG(dbgs() << "found and; icmp ?,0; brcc\n"); DEBUG(BB->dump()); |
| 3529 | |
| 3530 | // Push the "and; icmp" for any users that are conditional branches. |
| 3531 | // Since there can only be one branch use per BB, we don't need to keep |
| 3532 | // track of which BBs we insert into. |
| 3533 | for (Value::use_iterator UI = Cmp->use_begin(), E = Cmp->use_end(); |
| 3534 | UI != E; ) { |
| 3535 | Use &TheUse = *UI; |
| 3536 | // Find brcc use. |
| 3537 | BranchInst *BrccUser = dyn_cast<BranchInst>(*UI); |
| 3538 | ++UI; |
| 3539 | if (!BrccUser || !BrccUser->isConditional()) |
| 3540 | continue; |
| 3541 | BasicBlock *UserBB = BrccUser->getParent(); |
| 3542 | if (UserBB == BB) continue; |
| 3543 | DEBUG(dbgs() << "found Brcc use\n"); |
| 3544 | |
| 3545 | // Sink the "and; icmp" to use. |
| 3546 | MadeChange = true; |
| 3547 | BinaryOperator *NewAnd = |
| 3548 | BinaryOperator::CreateAnd(And->getOperand(0), And->getOperand(1), "", |
| 3549 | BrccUser); |
| 3550 | CmpInst *NewCmp = |
| 3551 | CmpInst::Create(Cmp->getOpcode(), Cmp->getPredicate(), NewAnd, Zero, |
| 3552 | "", BrccUser); |
| 3553 | TheUse = NewCmp; |
| 3554 | ++NumAndCmpsMoved; |
| 3555 | DEBUG(BrccUser->getParent()->dump()); |
| 3556 | } |
| 3557 | } |
| 3558 | return MadeChange; |
| 3559 | } |