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 | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 23 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Function.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 29 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/IRBuilder.h" |
| 31 | #include "llvm/IR/InlineAsm.h" |
| 32 | #include "llvm/IR/Instructions.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 34 | #include "llvm/IR/MDBuilder.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 35 | #include "llvm/IR/PatternMatch.h" |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Statepoint.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 37 | #include "llvm/IR/ValueHandle.h" |
Chandler Carruth | a4ea269 | 2014-03-04 11:26:31 +0000 | [diff] [blame] | 38 | #include "llvm/IR/ValueMap.h" |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Evan Cheng | 8b637b1 | 2010-08-17 01:34:49 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetLowering.h" |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 46 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Preston Gurd | cdf540d | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/BypassSlowDivision.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Utils/Local.h" |
Ahmed Bougacha | e03bef7 | 2015-01-12 17:22:43 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/Utils/SimplifyLibCalls.h" |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 50 | using namespace llvm; |
Chris Lattner | d616ef5 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 51 | using namespace llvm::PatternMatch; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 52 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 53 | #define DEBUG_TYPE "codegenprepare" |
| 54 | |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 55 | STATISTIC(NumBlocksElim, "Number of blocks eliminated"); |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 56 | STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated"); |
| 57 | STATISTIC(NumGEPsElim, "Number of GEPs converted to casts"); |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 58 | STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of " |
| 59 | "sunken Cmps"); |
| 60 | STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses " |
| 61 | "of sunken Casts"); |
| 62 | STATISTIC(NumMemoryInsts, "Number of memory instructions whose address " |
| 63 | "computations were sunk"); |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 64 | STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads"); |
| 65 | STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized"); |
| 66 | STATISTIC(NumRetsDup, "Number of return instructions duplicated"); |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 67 | STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved"); |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 68 | STATISTIC(NumSelectsExpanded, "Number of selects turned into branches"); |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 69 | STATISTIC(NumAndCmpsMoved, "Number of and/cmp's pushed into branches"); |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 70 | STATISTIC(NumStoreExtractExposed, "Number of store(extractelement) exposed"); |
Jakob Stoklund Olesen | eb12f49 | 2010-09-30 20:51:52 +0000 | [diff] [blame] | 71 | |
Cameron Zwarich | 338d362 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 72 | static cl::opt<bool> DisableBranchOpts( |
| 73 | "disable-cgp-branch-opts", cl::Hidden, cl::init(false), |
| 74 | cl::desc("Disable branch optimizations in CodeGenPrepare")); |
| 75 | |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 76 | static cl::opt<bool> |
| 77 | DisableGCOpts("disable-cgp-gc-opts", cl::Hidden, cl::init(false), |
| 78 | cl::desc("Disable GC optimizations in CodeGenPrepare")); |
| 79 | |
Benjamin Kramer | 3d38c17 | 2012-05-06 14:25:16 +0000 | [diff] [blame] | 80 | static cl::opt<bool> DisableSelectToBranch( |
| 81 | "disable-cgp-select2branch", cl::Hidden, cl::init(false), |
| 82 | cl::desc("Disable select to branch conversion.")); |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 83 | |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 84 | static cl::opt<bool> AddrSinkUsingGEPs( |
| 85 | "addr-sink-using-gep", cl::Hidden, cl::init(false), |
| 86 | cl::desc("Address sinking in CGP using GEPs.")); |
| 87 | |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 88 | static cl::opt<bool> EnableAndCmpSinking( |
| 89 | "enable-andcmp-sinking", cl::Hidden, cl::init(true), |
| 90 | cl::desc("Enable sinkinig and/cmp into branches.")); |
| 91 | |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 92 | static cl::opt<bool> DisableStoreExtract( |
| 93 | "disable-cgp-store-extract", cl::Hidden, cl::init(false), |
| 94 | cl::desc("Disable store(extract) optimizations in CodeGenPrepare")); |
| 95 | |
| 96 | static cl::opt<bool> StressStoreExtract( |
| 97 | "stress-cgp-store-extract", cl::Hidden, cl::init(false), |
| 98 | cl::desc("Stress test store(extract) optimizations in CodeGenPrepare")); |
| 99 | |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 100 | static cl::opt<bool> DisableExtLdPromotion( |
| 101 | "disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), |
| 102 | cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in " |
| 103 | "CodeGenPrepare")); |
| 104 | |
| 105 | static cl::opt<bool> StressExtLdPromotion( |
| 106 | "stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), |
| 107 | cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) " |
| 108 | "optimization in CodeGenPrepare")); |
| 109 | |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 110 | namespace { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 111 | typedef SmallPtrSet<Instruction *, 16> SetOfInstrs; |
Benjamin Kramer | 4cd5faa | 2015-07-31 17:00:39 +0000 | [diff] [blame] | 112 | typedef PointerIntPair<Type *, 1, bool> TypeIsSExt; |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 113 | typedef DenseMap<Instruction *, TypeIsSExt> InstrToOrigTy; |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 114 | class TypePromotionTransaction; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 116 | class CodeGenPrepare : public FunctionPass { |
Bill Wendling | 7a639ea | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 117 | const TargetMachine *TM; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 118 | const TargetLowering *TLI; |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 119 | const TargetTransformInfo *TTI; |
Chad Rosier | c24b86f | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 120 | const TargetLibraryInfo *TLInfo; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 121 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 122 | /// As we scan instructions optimizing them, this is the next instruction |
| 123 | /// to optimize. Transforms that can invalidate this should update it. |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 124 | BasicBlock::iterator CurInstIterator; |
Evan Cheng | 3b3de7c | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 125 | |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 126 | /// Keeps track of non-local addresses that have been sunk into a block. |
| 127 | /// This allows us to avoid inserting duplicate code for blocks with |
| 128 | /// multiple load/stores of the same address. |
Nick Lewycky | 5fb1963 | 2013-05-08 09:00:10 +0000 | [diff] [blame] | 129 | ValueMap<Value*, Value*> SunkAddrs; |
Cameron Zwarich | ce3b930 | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 130 | |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 131 | /// Keeps track of all instructions inserted for the current function. |
| 132 | SetOfInstrs InsertedInsts; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 133 | /// Keeps track of the type of the related instruction before their |
| 134 | /// promotion for the current function. |
| 135 | InstrToOrigTy PromotedInsts; |
| 136 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 137 | /// True if CFG is modified in any way. |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 138 | bool ModifiedDT; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 139 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 140 | /// True if optimizing for size. |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 141 | bool OptSize; |
| 142 | |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 143 | /// DataLayout for the Function being processed. |
| 144 | const DataLayout *DL; |
| 145 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 146 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 147 | static char ID; // Pass identification, replacement for typeid |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 148 | explicit CodeGenPrepare(const TargetMachine *TM = nullptr) |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 149 | : FunctionPass(ID), TM(TM), TLI(nullptr), TTI(nullptr), DL(nullptr) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 150 | initializeCodeGenPreparePass(*PassRegistry::getPassRegistry()); |
| 151 | } |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 152 | bool runOnFunction(Function &F) override; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 153 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 154 | const char *getPassName() const override { return "CodeGen Prepare"; } |
Evan Cheng | 99cafb1 | 2012-12-21 01:48:14 +0000 | [diff] [blame] | 155 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 156 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 157 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 158 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 159 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Andreas Neustifter | f8cb758 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 162 | private: |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 163 | bool eliminateFallThrough(Function &F); |
| 164 | bool eliminateMostlyEmptyBlocks(Function &F); |
| 165 | bool canMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 166 | void eliminateMostlyEmptyBlock(BasicBlock *BB); |
| 167 | bool optimizeBlock(BasicBlock &BB, bool& ModifiedDT); |
| 168 | bool optimizeInst(Instruction *I, bool& ModifiedDT); |
| 169 | bool optimizeMemoryInst(Instruction *I, Value *Addr, |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 170 | Type *AccessTy, unsigned AS); |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 171 | bool optimizeInlineAsmInst(CallInst *CS); |
| 172 | bool optimizeCallInst(CallInst *CI, bool& ModifiedDT); |
| 173 | bool moveExtToFormExtLoad(Instruction *&I); |
| 174 | bool optimizeExtUses(Instruction *I); |
| 175 | bool optimizeSelectInst(SelectInst *SI); |
| 176 | bool optimizeShuffleVectorInst(ShuffleVectorInst *SI); |
| 177 | bool optimizeExtractElementInst(Instruction *Inst); |
| 178 | bool dupRetToEnableTailCallOpts(BasicBlock *BB); |
| 179 | bool placeDbgValues(Function &F); |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 180 | bool sinkAndCmp(Function &F); |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 181 | bool extLdPromotion(TypePromotionTransaction &TPT, LoadInst *&LI, |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 182 | Instruction *&Inst, |
| 183 | const SmallVectorImpl<Instruction *> &Exts, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 184 | unsigned CreatedInstCost); |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 185 | bool splitBranchCondition(Function &F); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 186 | bool simplifyOffsetableRelocate(Instruction &I); |
Piotr Padlewski | 6c15ec4 | 2015-09-15 18:32:14 +0000 | [diff] [blame] | 187 | void stripInvariantGroupMetadata(Instruction &I); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 188 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 189 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 190 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 191 | char CodeGenPrepare::ID = 0; |
Jiangning Liu | d623c52 | 2014-06-11 07:04:37 +0000 | [diff] [blame] | 192 | INITIALIZE_TM_PASS(CodeGenPrepare, "codegenprepare", |
| 193 | "Optimize for code generation", false, false) |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 194 | |
Bill Wendling | 7a639ea | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 195 | FunctionPass *llvm::createCodeGenPreparePass(const TargetMachine *TM) { |
| 196 | return new CodeGenPrepare(TM); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 199 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 200 | if (skipOptnoneFunction(F)) |
| 201 | return false; |
| 202 | |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 203 | DL = &F.getParent()->getDataLayout(); |
| 204 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 205 | bool EverMadeChange = false; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 206 | // Clear per function information. |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 207 | InsertedInsts.clear(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 208 | PromotedInsts.clear(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 209 | |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 210 | ModifiedDT = false; |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 211 | if (TM) |
Eric Christopher | fccff37 | 2015-01-27 01:01:38 +0000 | [diff] [blame] | 212 | TLI = TM->getSubtargetImpl(F)->getTargetLowering(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 213 | TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chandler Carruth | fdb9c57 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 214 | TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Sanjay Patel | 82d91dd | 2015-08-11 19:39:36 +0000 | [diff] [blame] | 215 | OptSize = F.optForSize(); |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 216 | |
Preston Gurd | cdf540d | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 217 | /// This optimization identifies DIV instructions that can be |
| 218 | /// profitably bypassed and carried out with a shorter, faster divide. |
Preston Gurd | 485296d | 2013-03-04 18:13:57 +0000 | [diff] [blame] | 219 | if (!OptSize && TLI && TLI->isSlowDivBypassed()) { |
Preston Gurd | 0d67f51 | 2012-10-04 21:33:40 +0000 | [diff] [blame] | 220 | const DenseMap<unsigned int, unsigned int> &BypassWidths = |
| 221 | TLI->getBypassSlowDivWidths(); |
Evan Cheng | 71be12b | 2012-09-14 21:25:34 +0000 | [diff] [blame] | 222 | for (Function::iterator I = F.begin(); I != F.end(); I++) |
Preston Gurd | 0d67f51 | 2012-10-04 21:33:40 +0000 | [diff] [blame] | 223 | EverMadeChange |= bypassSlowDivision(F, I, BypassWidths); |
Preston Gurd | cdf540d | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Eliminate blocks that contain only PHI nodes and an |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 227 | // unconditional branch. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 228 | EverMadeChange |= eliminateMostlyEmptyBlocks(F); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 229 | |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 230 | // 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] | 231 | // 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] | 232 | // find a node corresponding to the value. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 233 | EverMadeChange |= placeDbgValues(F); |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 234 | |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 235 | // If there is a mask, compare against zero, and branch that can be combined |
| 236 | // into a single target instruction, push the mask and compare into branch |
| 237 | // users. Do this before OptimizeBlock -> OptimizeInst -> |
| 238 | // OptimizeCmpExpression, which perturbs the pattern being searched for. |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 239 | if (!DisableBranchOpts) { |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 240 | EverMadeChange |= sinkAndCmp(F); |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 241 | EverMadeChange |= splitBranchCondition(F); |
| 242 | } |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 243 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 244 | bool MadeChange = true; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 245 | while (MadeChange) { |
| 246 | MadeChange = false; |
Hans Wennborg | 02fbc71 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 247 | for (Function::iterator I = F.begin(); I != F.end(); ) { |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 248 | BasicBlock *BB = I++; |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 249 | bool ModifiedDTOnIteration = false; |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 250 | MadeChange |= optimizeBlock(*BB, ModifiedDTOnIteration); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 251 | |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 252 | // Restart BB iteration if the dominator tree of the Function was changed |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 253 | if (ModifiedDTOnIteration) |
| 254 | break; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 255 | } |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 256 | EverMadeChange |= MadeChange; |
| 257 | } |
Cameron Zwarich | ce3b930 | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 258 | |
| 259 | SunkAddrs.clear(); |
| 260 | |
Cameron Zwarich | 338d362 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 261 | if (!DisableBranchOpts) { |
| 262 | MadeChange = false; |
Bill Wendling | 97b9359 | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 263 | SmallPtrSet<BasicBlock*, 8> WorkList; |
Duncan P. N. Exon Smith | 5914a97 | 2015-01-08 20:44:33 +0000 | [diff] [blame] | 264 | for (BasicBlock &BB : F) { |
| 265 | SmallVector<BasicBlock *, 2> Successors(succ_begin(&BB), succ_end(&BB)); |
| 266 | MadeChange |= ConstantFoldTerminator(&BB, true); |
Bill Wendling | 97b9359 | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 267 | if (!MadeChange) continue; |
| 268 | |
| 269 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 270 | II = Successors.begin(), IE = Successors.end(); II != IE; ++II) |
| 271 | if (pred_begin(*II) == pred_end(*II)) |
| 272 | WorkList.insert(*II); |
| 273 | } |
| 274 | |
Bill Wendling | f3614fd | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 275 | // Delete the dead blocks and any of their dead successors. |
Bill Wendling | ab417b6 | 2012-12-06 00:30:20 +0000 | [diff] [blame] | 276 | MadeChange |= !WorkList.empty(); |
Bill Wendling | f3614fd | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 277 | while (!WorkList.empty()) { |
| 278 | BasicBlock *BB = *WorkList.begin(); |
| 279 | WorkList.erase(BB); |
| 280 | SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB)); |
| 281 | |
| 282 | DeleteDeadBlock(BB); |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 283 | |
Bill Wendling | f3614fd | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 284 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 285 | II = Successors.begin(), IE = Successors.end(); II != IE; ++II) |
| 286 | if (pred_begin(*II) == pred_end(*II)) |
| 287 | WorkList.insert(*II); |
| 288 | } |
Cameron Zwarich | 338d362 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 289 | |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 290 | // Merge pairs of basic blocks with unconditional branches, connected by |
| 291 | // a single edge. |
| 292 | if (EverMadeChange || MadeChange) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 293 | MadeChange |= eliminateFallThrough(F); |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 294 | |
Cameron Zwarich | 338d362 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 295 | EverMadeChange |= MadeChange; |
| 296 | } |
| 297 | |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 298 | if (!DisableGCOpts) { |
| 299 | SmallVector<Instruction *, 2> Statepoints; |
| 300 | for (BasicBlock &BB : F) |
| 301 | for (Instruction &I : BB) |
| 302 | if (isStatepoint(I)) |
| 303 | Statepoints.push_back(&I); |
| 304 | for (auto &I : Statepoints) |
| 305 | EverMadeChange |= simplifyOffsetableRelocate(*I); |
| 306 | } |
| 307 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 308 | return EverMadeChange; |
| 309 | } |
| 310 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 311 | /// Merge basic blocks which are connected by a single edge, where one of the |
| 312 | /// basic blocks has a single successor pointing to the other basic block, |
| 313 | /// which has a single predecessor. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 314 | bool CodeGenPrepare::eliminateFallThrough(Function &F) { |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 315 | bool Changed = false; |
| 316 | // 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] | 317 | 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] | 318 | BasicBlock *BB = I++; |
| 319 | // If the destination block has a single pred, then this is a trivial |
| 320 | // edge, just collapse it. |
| 321 | BasicBlock *SinglePred = BB->getSinglePredecessor(); |
| 322 | |
Evan Cheng | 64a223a | 2012-09-28 23:58:57 +0000 | [diff] [blame] | 323 | // Don't merge if BB's address is taken. |
| 324 | if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue; |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 325 | |
| 326 | BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); |
| 327 | if (Term && !Term->isConditional()) { |
| 328 | Changed = true; |
Michael Liao | 6e12d12 | 2012-08-21 05:55:22 +0000 | [diff] [blame] | 329 | DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n"); |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 330 | // Remember if SinglePred was the entry block of the function. |
| 331 | // If so, we will need to move BB back to the entry position. |
| 332 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Quentin Colombet | 7bdd50d | 2015-03-18 23:17:28 +0000 | [diff] [blame] | 333 | MergeBasicBlockIntoOnlyPred(BB, nullptr); |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 334 | |
| 335 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 336 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
| 337 | |
| 338 | // We have erased a block. Update the iterator. |
| 339 | I = BB; |
Nadav Rotem | 7040999 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | return Changed; |
| 343 | } |
| 344 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 345 | /// Eliminate blocks that contain only PHI nodes, debug info directives, and an |
| 346 | /// unconditional branch. Passes before isel (e.g. LSR/loopsimplify) often split |
| 347 | /// edges in ways that are non-optimal for isel. Start by eliminating these |
| 348 | /// blocks so we can split them the way we want them. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 349 | bool CodeGenPrepare::eliminateMostlyEmptyBlocks(Function &F) { |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 350 | bool MadeChange = false; |
| 351 | // Note that this intentionally skips the entry block. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 352 | 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] | 353 | BasicBlock *BB = I++; |
| 354 | |
| 355 | // If this block doesn't end with an uncond branch, ignore it. |
| 356 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 357 | if (!BI || !BI->isUnconditional()) |
| 358 | continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 359 | |
Dale Johannesen | 4026b04 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 360 | // If the instruction before the branch (skipping debug info) isn't a phi |
| 361 | // node, then other stuff is happening here. |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 362 | BasicBlock::iterator BBI = BI; |
| 363 | if (BBI != BB->begin()) { |
| 364 | --BBI; |
Dale Johannesen | 4026b04 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 365 | while (isa<DbgInfoIntrinsic>(BBI)) { |
| 366 | if (BBI == BB->begin()) |
| 367 | break; |
| 368 | --BBI; |
| 369 | } |
| 370 | if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI)) |
| 371 | continue; |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 372 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 373 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 374 | // Do not break infinite loops. |
| 375 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 376 | if (DestBB == BB) |
| 377 | continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 378 | |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 379 | if (!canMergeBlocks(BB, DestBB)) |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 380 | continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 381 | |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 382 | eliminateMostlyEmptyBlock(BB); |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 383 | MadeChange = true; |
| 384 | } |
| 385 | return MadeChange; |
| 386 | } |
| 387 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 388 | /// Return true if we can merge BB into DestBB if there is a single |
| 389 | /// unconditional branch between them, and BB contains no other non-phi |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 390 | /// instructions. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 391 | bool CodeGenPrepare::canMergeBlocks(const BasicBlock *BB, |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 392 | const BasicBlock *DestBB) const { |
| 393 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 394 | // the successor. If there are more complex condition (e.g. preheaders), |
| 395 | // don't mess around with them. |
| 396 | BasicBlock::const_iterator BBI = BB->begin(); |
| 397 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 398 | for (const User *U : PN->users()) { |
| 399 | const Instruction *UI = cast<Instruction>(U); |
| 400 | if (UI->getParent() != DestBB || !isa<PHINode>(UI)) |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 401 | return false; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 402 | // If User is inside DestBB block and it is a PHINode then check |
| 403 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | d320852 | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 404 | // a complex condition (e.g. preheaders) we want to avoid here. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 405 | if (UI->getParent() == DestBB) { |
| 406 | if (const PHINode *UPN = dyn_cast<PHINode>(UI)) |
Devang Patel | d320852 | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 407 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 408 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 409 | if (Insn && Insn->getParent() == BB && |
| 410 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 411 | return false; |
| 412 | } |
| 413 | } |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 416 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 417 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 418 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 419 | // can't merge the block. |
| 420 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 421 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 422 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 423 | // Collect the preds of BB. |
Chris Lattner | 8201a9b | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 424 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 425 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 426 | // It is faster to get preds from a PHI than with pred_iterator. |
| 427 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 428 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 429 | } else { |
| 430 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 431 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 432 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 433 | // Walk the preds of DestBB. |
| 434 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 435 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 436 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 437 | BBI = DestBB->begin(); |
| 438 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 439 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 440 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 441 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 442 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 443 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 444 | if (V2PN->getParent() == BB) |
| 445 | V2 = V2PN->getIncomingValueForBlock(Pred); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 446 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 447 | // If there is a conflict, bail out. |
| 448 | if (V1 != V2) return false; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 457 | /// Eliminate a basic block that has only phi's and an unconditional branch in |
| 458 | /// it. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 459 | void CodeGenPrepare::eliminateMostlyEmptyBlock(BasicBlock *BB) { |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 460 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 461 | BasicBlock *DestBB = BI->getSuccessor(0); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 462 | |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 463 | DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 464 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 465 | // If the destination block has a single pred, then this is a trivial edge, |
| 466 | // just collapse it. |
Chris Lattner | 4059f43 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 467 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { |
Chris Lattner | 8a172da | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 468 | if (SinglePred != DestBB) { |
| 469 | // Remember if SinglePred was the entry block of the function. If so, we |
| 470 | // will need to move BB back to the entry position. |
| 471 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Quentin Colombet | 7bdd50d | 2015-03-18 23:17:28 +0000 | [diff] [blame] | 472 | MergeBasicBlockIntoOnlyPred(DestBB, nullptr); |
Chris Lattner | 4059f43 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 8a172da | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 474 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 475 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 476 | |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 477 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | 8a172da | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 478 | return; |
| 479 | } |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 480 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 481 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 482 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 483 | // to handle the new incoming edges it is about to have. |
| 484 | PHINode *PN; |
| 485 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 486 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 487 | // Remove the incoming value for BB, and remember it. |
| 488 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 489 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 490 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 491 | // value that dominates BB. |
| 492 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 493 | if (InValPhi && InValPhi->getParent() == BB) { |
| 494 | // Add all of the input values of the input PHI as inputs of this phi. |
| 495 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 496 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 497 | InValPhi->getIncomingBlock(i)); |
| 498 | } else { |
| 499 | // Otherwise, add one instance of the dominating value for each edge that |
| 500 | // we will be adding. |
| 501 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 502 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 503 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 504 | } else { |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 505 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 506 | PN->addIncoming(InVal, *PI); |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 510 | |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 511 | // The PHIs are now updated, change everything that refers to BB to use |
| 512 | // DestBB and remove BB. |
| 513 | BB->replaceAllUsesWith(DestBB); |
| 514 | BB->eraseFromParent(); |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 515 | ++NumBlocksElim; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 516 | |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 517 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | c374856 | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 520 | // Computes a map of base pointer relocation instructions to corresponding |
| 521 | // derived pointer relocation instructions given a vector of all relocate calls |
| 522 | static void computeBaseDerivedRelocateMap( |
| 523 | const SmallVectorImpl<User *> &AllRelocateCalls, |
| 524 | DenseMap<IntrinsicInst *, SmallVector<IntrinsicInst *, 2>> & |
| 525 | RelocateInstMap) { |
| 526 | // Collect information in two maps: one primarily for locating the base object |
| 527 | // while filling the second map; the second map is the final structure holding |
| 528 | // a mapping between Base and corresponding Derived relocate calls |
| 529 | DenseMap<std::pair<unsigned, unsigned>, IntrinsicInst *> RelocateIdxMap; |
| 530 | for (auto &U : AllRelocateCalls) { |
| 531 | GCRelocateOperands ThisRelocate(U); |
| 532 | IntrinsicInst *I = cast<IntrinsicInst>(U); |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 533 | auto K = std::make_pair(ThisRelocate.getBasePtrIndex(), |
| 534 | ThisRelocate.getDerivedPtrIndex()); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 535 | RelocateIdxMap.insert(std::make_pair(K, I)); |
| 536 | } |
| 537 | for (auto &Item : RelocateIdxMap) { |
| 538 | std::pair<unsigned, unsigned> Key = Item.first; |
| 539 | if (Key.first == Key.second) |
| 540 | // Base relocation: nothing to insert |
| 541 | continue; |
| 542 | |
| 543 | IntrinsicInst *I = Item.second; |
| 544 | auto BaseKey = std::make_pair(Key.first, Key.first); |
Sanjoy Das | b818676 | 2015-02-27 02:24:16 +0000 | [diff] [blame] | 545 | |
| 546 | // We're iterating over RelocateIdxMap so we cannot modify it. |
| 547 | auto MaybeBase = RelocateIdxMap.find(BaseKey); |
| 548 | if (MaybeBase == RelocateIdxMap.end()) |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 549 | // TODO: We might want to insert a new base object relocate and gep off |
| 550 | // that, if there are enough derived object relocates. |
| 551 | continue; |
Sanjoy Das | b818676 | 2015-02-27 02:24:16 +0000 | [diff] [blame] | 552 | |
| 553 | RelocateInstMap[MaybeBase->second].push_back(I); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | // Accepts a GEP and extracts the operands into a vector provided they're all |
| 558 | // small integer constants |
| 559 | static bool getGEPSmallConstantIntOffsetV(GetElementPtrInst *GEP, |
| 560 | SmallVectorImpl<Value *> &OffsetV) { |
| 561 | for (unsigned i = 1; i < GEP->getNumOperands(); i++) { |
| 562 | // Only accept small constant integer operands |
| 563 | auto Op = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 564 | if (!Op || Op->getZExtValue() > 20) |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | for (unsigned i = 1; i < GEP->getNumOperands(); i++) |
| 569 | OffsetV.push_back(GEP->getOperand(i)); |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | // Takes a RelocatedBase (base pointer relocation instruction) and Targets to |
| 574 | // replace, computes a replacement, and affects it. |
| 575 | static bool |
| 576 | simplifyRelocatesOffABase(IntrinsicInst *RelocatedBase, |
| 577 | const SmallVectorImpl<IntrinsicInst *> &Targets) { |
| 578 | bool MadeChange = false; |
| 579 | for (auto &ToReplace : Targets) { |
| 580 | GCRelocateOperands MasterRelocate(RelocatedBase); |
| 581 | GCRelocateOperands ThisRelocate(ToReplace); |
| 582 | |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 583 | assert(ThisRelocate.getBasePtrIndex() == MasterRelocate.getBasePtrIndex() && |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 584 | "Not relocating a derived object of the original base object"); |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 585 | if (ThisRelocate.getBasePtrIndex() == ThisRelocate.getDerivedPtrIndex()) { |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 586 | // A duplicate relocate call. TODO: coalesce duplicates. |
| 587 | continue; |
| 588 | } |
| 589 | |
Sanjoy Das | 499d703 | 2015-05-06 02:36:26 +0000 | [diff] [blame] | 590 | Value *Base = ThisRelocate.getBasePtr(); |
| 591 | auto Derived = dyn_cast<GetElementPtrInst>(ThisRelocate.getDerivedPtr()); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 592 | if (!Derived || Derived->getPointerOperand() != Base) |
| 593 | continue; |
| 594 | |
| 595 | SmallVector<Value *, 2> OffsetV; |
| 596 | if (!getGEPSmallConstantIntOffsetV(Derived, OffsetV)) |
| 597 | continue; |
| 598 | |
| 599 | // Create a Builder and replace the target callsite with a gep |
Sanjoy Das | 3d705e3 | 2015-05-11 23:47:30 +0000 | [diff] [blame] | 600 | assert(RelocatedBase->getNextNode() && "Should always have one since it's not a terminator"); |
| 601 | |
| 602 | // Insert after RelocatedBase |
| 603 | IRBuilder<> Builder(RelocatedBase->getNextNode()); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 604 | Builder.SetCurrentDebugLocation(ToReplace->getDebugLoc()); |
Sanjoy Das | 89c5491 | 2015-05-11 18:49:34 +0000 | [diff] [blame] | 605 | |
| 606 | // If gc_relocate does not match the actual type, cast it to the right type. |
| 607 | // In theory, there must be a bitcast after gc_relocate if the type does not |
| 608 | // match, and we should reuse it to get the derived pointer. But it could be |
| 609 | // cases like this: |
| 610 | // bb1: |
| 611 | // ... |
| 612 | // %g1 = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(...) |
| 613 | // br label %merge |
| 614 | // |
| 615 | // bb2: |
| 616 | // ... |
| 617 | // %g2 = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(...) |
| 618 | // br label %merge |
| 619 | // |
| 620 | // merge: |
| 621 | // %p1 = phi i8 addrspace(1)* [ %g1, %bb1 ], [ %g2, %bb2 ] |
| 622 | // %cast = bitcast i8 addrspace(1)* %p1 in to i32 addrspace(1)* |
| 623 | // |
| 624 | // In this case, we can not find the bitcast any more. So we insert a new bitcast |
| 625 | // no matter there is already one or not. In this way, we can handle all cases, and |
| 626 | // the extra bitcast should be optimized away in later passes. |
| 627 | Instruction *ActualRelocatedBase = RelocatedBase; |
| 628 | if (RelocatedBase->getType() != Base->getType()) { |
| 629 | ActualRelocatedBase = |
| 630 | cast<Instruction>(Builder.CreateBitCast(RelocatedBase, Base->getType())); |
Sanjoy Das | 89c5491 | 2015-05-11 18:49:34 +0000 | [diff] [blame] | 631 | } |
David Blaikie | 68d535c | 2015-03-24 22:38:16 +0000 | [diff] [blame] | 632 | Value *Replacement = Builder.CreateGEP( |
Sanjoy Das | 89c5491 | 2015-05-11 18:49:34 +0000 | [diff] [blame] | 633 | Derived->getSourceElementType(), ActualRelocatedBase, makeArrayRef(OffsetV)); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 634 | Instruction *ReplacementInst = cast<Instruction>(Replacement); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 635 | Replacement->takeName(ToReplace); |
Sanjoy Das | 89c5491 | 2015-05-11 18:49:34 +0000 | [diff] [blame] | 636 | // If the newly generated derived pointer's type does not match the original derived |
| 637 | // pointer's type, cast the new derived pointer to match it. Same reasoning as above. |
| 638 | Instruction *ActualReplacement = ReplacementInst; |
| 639 | if (ReplacementInst->getType() != ToReplace->getType()) { |
| 640 | ActualReplacement = |
| 641 | cast<Instruction>(Builder.CreateBitCast(ReplacementInst, ToReplace->getType())); |
Sanjoy Das | 89c5491 | 2015-05-11 18:49:34 +0000 | [diff] [blame] | 642 | } |
| 643 | ToReplace->replaceAllUsesWith(ActualReplacement); |
Ramkumar Ramachandra | dba7329 | 2015-01-14 23:27:07 +0000 | [diff] [blame] | 644 | ToReplace->eraseFromParent(); |
| 645 | |
| 646 | MadeChange = true; |
| 647 | } |
| 648 | return MadeChange; |
| 649 | } |
| 650 | |
| 651 | // Turns this: |
| 652 | // |
| 653 | // %base = ... |
| 654 | // %ptr = gep %base + 15 |
| 655 | // %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr) |
| 656 | // %base' = relocate(%tok, i32 4, i32 4) |
| 657 | // %ptr' = relocate(%tok, i32 4, i32 5) |
| 658 | // %val = load %ptr' |
| 659 | // |
| 660 | // into this: |
| 661 | // |
| 662 | // %base = ... |
| 663 | // %ptr = gep %base + 15 |
| 664 | // %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr) |
| 665 | // %base' = gc.relocate(%tok, i32 4, i32 4) |
| 666 | // %ptr' = gep %base' + 15 |
| 667 | // %val = load %ptr' |
| 668 | bool CodeGenPrepare::simplifyOffsetableRelocate(Instruction &I) { |
| 669 | bool MadeChange = false; |
| 670 | SmallVector<User *, 2> AllRelocateCalls; |
| 671 | |
| 672 | for (auto *U : I.users()) |
| 673 | if (isGCRelocate(dyn_cast<Instruction>(U))) |
| 674 | // Collect all the relocate calls associated with a statepoint |
| 675 | AllRelocateCalls.push_back(U); |
| 676 | |
| 677 | // We need atleast one base pointer relocation + one derived pointer |
| 678 | // relocation to mangle |
| 679 | if (AllRelocateCalls.size() < 2) |
| 680 | return false; |
| 681 | |
| 682 | // RelocateInstMap is a mapping from the base relocate instruction to the |
| 683 | // corresponding derived relocate instructions |
| 684 | DenseMap<IntrinsicInst *, SmallVector<IntrinsicInst *, 2>> RelocateInstMap; |
| 685 | computeBaseDerivedRelocateMap(AllRelocateCalls, RelocateInstMap); |
| 686 | if (RelocateInstMap.empty()) |
| 687 | return false; |
| 688 | |
| 689 | for (auto &Item : RelocateInstMap) |
| 690 | // Item.first is the RelocatedBase to offset against |
| 691 | // Item.second is the vector of Targets to replace |
| 692 | MadeChange = simplifyRelocatesOffABase(Item.first, Item.second); |
| 693 | return MadeChange; |
| 694 | } |
| 695 | |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 696 | /// SinkCast - Sink the specified cast instruction into its user blocks |
| 697 | static bool SinkCast(CastInst *CI) { |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 698 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 699 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 700 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 701 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 702 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 703 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 704 | for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end(); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 705 | UI != E; ) { |
| 706 | Use &TheUse = UI.getUse(); |
| 707 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 708 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 709 | // Figure out which BB this cast is used in. For PHI's this is the |
| 710 | // appropriate predecessor block. |
| 711 | BasicBlock *UserBB = User->getParent(); |
| 712 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 713 | UserBB = PN->getIncomingBlock(TheUse); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 714 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 715 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 716 | // Preincrement use iterator so we don't invalidate it. |
| 717 | ++UI; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 718 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 719 | // If this user is in the same block as the cast, don't change the cast. |
| 720 | if (UserBB == DefBB) continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 721 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 722 | // If we have already inserted a cast into this block, use it. |
| 723 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 724 | |
| 725 | if (!InsertedCast) { |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 726 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 727 | InsertedCast = |
| 728 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 729 | InsertPt); |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 730 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 731 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 732 | // 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] | 733 | TheUse = InsertedCast; |
Benjamin Kramer | b4bf14c | 2015-04-10 22:25:36 +0000 | [diff] [blame] | 734 | MadeChange = true; |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 735 | ++NumCastUses; |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 736 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 737 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 738 | // If we removed all uses, nuke the cast. |
Duncan Sands | afa84da4 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 739 | if (CI->use_empty()) { |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 740 | CI->eraseFromParent(); |
Duncan Sands | afa84da4 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 741 | MadeChange = true; |
| 742 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 743 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 744 | return MadeChange; |
| 745 | } |
| 746 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 747 | /// If the specified cast instruction is a noop copy (e.g. it's casting from |
| 748 | /// one pointer type to another, i32->i8 on PPC), sink it into user blocks to |
| 749 | /// reduce the number of virtual registers that must be created and coalesced. |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 750 | /// |
| 751 | /// Return true if any changes are made. |
| 752 | /// |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 753 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI, |
| 754 | const DataLayout &DL) { |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 755 | // If this is a noop copy, |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 756 | EVT SrcVT = TLI.getValueType(DL, CI->getOperand(0)->getType()); |
| 757 | EVT DstVT = TLI.getValueType(DL, CI->getType()); |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 758 | |
| 759 | // This is an fp<->int conversion? |
| 760 | if (SrcVT.isInteger() != DstVT.isInteger()) |
| 761 | return false; |
| 762 | |
| 763 | // If this is an extension, it will be a zero or sign extension, which |
| 764 | // isn't a noop. |
| 765 | if (SrcVT.bitsLT(DstVT)) return false; |
| 766 | |
| 767 | // If these values will be promoted, find out what they will be promoted |
| 768 | // to. This helps us consider truncates on PPC as noop copies when they |
| 769 | // are. |
| 770 | if (TLI.getTypeAction(CI->getContext(), SrcVT) == |
| 771 | TargetLowering::TypePromoteInteger) |
| 772 | SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT); |
| 773 | if (TLI.getTypeAction(CI->getContext(), DstVT) == |
| 774 | TargetLowering::TypePromoteInteger) |
| 775 | DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT); |
| 776 | |
| 777 | // If, after promotion, these are the same types, this is a noop copy. |
| 778 | if (SrcVT != DstVT) |
| 779 | return false; |
| 780 | |
| 781 | return SinkCast(CI); |
| 782 | } |
| 783 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 784 | /// Try to combine CI into a call to the llvm.uadd.with.overflow intrinsic if |
| 785 | /// possible. |
Sanjoy Das | b6c5914 | 2015-04-10 21:07:09 +0000 | [diff] [blame] | 786 | /// |
| 787 | /// Return true if any changes were made. |
| 788 | static bool CombineUAddWithOverflow(CmpInst *CI) { |
| 789 | Value *A, *B; |
| 790 | Instruction *AddI; |
| 791 | if (!match(CI, |
| 792 | m_UAddWithOverflow(m_Value(A), m_Value(B), m_Instruction(AddI)))) |
| 793 | return false; |
| 794 | |
| 795 | Type *Ty = AddI->getType(); |
| 796 | if (!isa<IntegerType>(Ty)) |
| 797 | return false; |
| 798 | |
| 799 | // We don't want to move around uses of condition values this late, so we we |
| 800 | // check if it is legal to create the call to the intrinsic in the basic |
| 801 | // block containing the icmp: |
| 802 | |
| 803 | if (AddI->getParent() != CI->getParent() && !AddI->hasOneUse()) |
| 804 | return false; |
| 805 | |
| 806 | #ifndef NDEBUG |
| 807 | // Someday m_UAddWithOverflow may get smarter, but this is a safe assumption |
| 808 | // for now: |
| 809 | if (AddI->hasOneUse()) |
| 810 | assert(*AddI->user_begin() == CI && "expected!"); |
| 811 | #endif |
| 812 | |
| 813 | Module *M = CI->getParent()->getParent()->getParent(); |
| 814 | Value *F = Intrinsic::getDeclaration(M, Intrinsic::uadd_with_overflow, Ty); |
| 815 | |
| 816 | auto *InsertPt = AddI->hasOneUse() ? CI : AddI; |
| 817 | |
| 818 | auto *UAddWithOverflow = |
| 819 | CallInst::Create(F, {A, B}, "uadd.overflow", InsertPt); |
| 820 | auto *UAdd = ExtractValueInst::Create(UAddWithOverflow, 0, "uadd", InsertPt); |
| 821 | auto *Overflow = |
| 822 | ExtractValueInst::Create(UAddWithOverflow, 1, "overflow", InsertPt); |
| 823 | |
| 824 | CI->replaceAllUsesWith(Overflow); |
| 825 | AddI->replaceAllUsesWith(UAdd); |
| 826 | CI->eraseFromParent(); |
| 827 | AddI->eraseFromParent(); |
| 828 | return true; |
| 829 | } |
| 830 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 831 | /// Sink the given CmpInst into user blocks to reduce the number of virtual |
| 832 | /// registers that must be created and coalesced. This is a clear win except on |
| 833 | /// targets with multiple condition code registers (PowerPC), where it might |
| 834 | /// lose; some adjustment may be wanted there. |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 835 | /// |
| 836 | /// Return true if any changes are made. |
Sanjoy Das | b6c5914 | 2015-04-10 21:07:09 +0000 | [diff] [blame] | 837 | static bool SinkCmpExpression(CmpInst *CI) { |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 838 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 839 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 840 | /// Only insert a cmp in each block once. |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 841 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 842 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 843 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 844 | for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end(); |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 845 | UI != E; ) { |
| 846 | Use &TheUse = UI.getUse(); |
| 847 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 848 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 849 | // Preincrement use iterator so we don't invalidate it. |
| 850 | ++UI; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 851 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 852 | // Don't bother for PHI nodes. |
| 853 | if (isa<PHINode>(User)) |
| 854 | continue; |
| 855 | |
| 856 | // Figure out which BB this cmp is used in. |
| 857 | BasicBlock *UserBB = User->getParent(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 858 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 859 | // If this user is in the same block as the cmp, don't change the cmp. |
| 860 | if (UserBB == DefBB) continue; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 861 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 862 | // If we have already inserted a cmp into this block, use it. |
| 863 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 864 | |
| 865 | if (!InsertedCmp) { |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 866 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 867 | InsertedCmp = |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 868 | CmpInst::Create(CI->getOpcode(), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 869 | CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 870 | CI->getOperand(1), "", InsertPt); |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 871 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 872 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 873 | // Replace a use of the cmp with a use of the new cmp. |
| 874 | TheUse = InsertedCmp; |
Benjamin Kramer | b4bf14c | 2015-04-10 22:25:36 +0000 | [diff] [blame] | 875 | MadeChange = true; |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 876 | ++NumCmpUses; |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 877 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 878 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 879 | // If we removed all uses, nuke the cmp. |
Benjamin Kramer | b4bf14c | 2015-04-10 22:25:36 +0000 | [diff] [blame] | 880 | if (CI->use_empty()) { |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 881 | CI->eraseFromParent(); |
Benjamin Kramer | b4bf14c | 2015-04-10 22:25:36 +0000 | [diff] [blame] | 882 | MadeChange = true; |
| 883 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 884 | |
Dale Johannesen | edfec0b | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 885 | return MadeChange; |
| 886 | } |
| 887 | |
Sanjoy Das | b6c5914 | 2015-04-10 21:07:09 +0000 | [diff] [blame] | 888 | static bool OptimizeCmpExpression(CmpInst *CI) { |
| 889 | if (SinkCmpExpression(CI)) |
| 890 | return true; |
| 891 | |
| 892 | if (CombineUAddWithOverflow(CI)) |
| 893 | return true; |
| 894 | |
| 895 | return false; |
| 896 | } |
| 897 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 898 | /// Check if the candidates could be combined with a shift instruction, which |
| 899 | /// includes: |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 900 | /// 1. Truncate instruction |
| 901 | /// 2. And instruction and the imm is a mask of the low bits: |
| 902 | /// imm & (imm+1) == 0 |
Benjamin Kramer | 322053c | 2014-04-27 14:54:59 +0000 | [diff] [blame] | 903 | static bool isExtractBitsCandidateUse(Instruction *User) { |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 904 | if (!isa<TruncInst>(User)) { |
| 905 | if (User->getOpcode() != Instruction::And || |
| 906 | !isa<ConstantInt>(User->getOperand(1))) |
| 907 | return false; |
| 908 | |
Quentin Colombet | d4f4469 | 2014-04-22 01:20:34 +0000 | [diff] [blame] | 909 | const APInt &Cimm = cast<ConstantInt>(User->getOperand(1))->getValue(); |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 910 | |
Quentin Colombet | d4f4469 | 2014-04-22 01:20:34 +0000 | [diff] [blame] | 911 | if ((Cimm & (Cimm + 1)).getBoolValue()) |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 912 | return false; |
| 913 | } |
| 914 | return true; |
| 915 | } |
| 916 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 917 | /// Sink both shift and truncate instruction to the use of truncate's BB. |
Benjamin Kramer | 322053c | 2014-04-27 14:54:59 +0000 | [diff] [blame] | 918 | static bool |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 919 | SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI, |
| 920 | DenseMap<BasicBlock *, BinaryOperator *> &InsertedShifts, |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 921 | const TargetLowering &TLI, const DataLayout &DL) { |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 922 | BasicBlock *UserBB = User->getParent(); |
| 923 | DenseMap<BasicBlock *, CastInst *> InsertedTruncs; |
| 924 | TruncInst *TruncI = dyn_cast<TruncInst>(User); |
| 925 | bool MadeChange = false; |
| 926 | |
| 927 | for (Value::user_iterator TruncUI = TruncI->user_begin(), |
| 928 | TruncE = TruncI->user_end(); |
| 929 | TruncUI != TruncE;) { |
| 930 | |
| 931 | Use &TruncTheUse = TruncUI.getUse(); |
| 932 | Instruction *TruncUser = cast<Instruction>(*TruncUI); |
| 933 | // Preincrement use iterator so we don't invalidate it. |
| 934 | |
| 935 | ++TruncUI; |
| 936 | |
| 937 | int ISDOpcode = TLI.InstructionOpcodeToISD(TruncUser->getOpcode()); |
| 938 | if (!ISDOpcode) |
| 939 | continue; |
| 940 | |
Tim Northover | e2239ff | 2014-07-29 10:20:22 +0000 | [diff] [blame] | 941 | // If the use is actually a legal node, there will not be an |
| 942 | // implicit truncate. |
| 943 | // FIXME: always querying the result type is just an |
| 944 | // approximation; some nodes' legality is determined by the |
| 945 | // operand or other means. There's no good way to find out though. |
Ahmed Bougacha | 0788d49 | 2014-11-12 22:16:55 +0000 | [diff] [blame] | 946 | if (TLI.isOperationLegalOrCustom( |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 947 | ISDOpcode, TLI.getValueType(DL, TruncUser->getType(), true))) |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 948 | continue; |
| 949 | |
| 950 | // Don't bother for PHI nodes. |
| 951 | if (isa<PHINode>(TruncUser)) |
| 952 | continue; |
| 953 | |
| 954 | BasicBlock *TruncUserBB = TruncUser->getParent(); |
| 955 | |
| 956 | if (UserBB == TruncUserBB) |
| 957 | continue; |
| 958 | |
| 959 | BinaryOperator *&InsertedShift = InsertedShifts[TruncUserBB]; |
| 960 | CastInst *&InsertedTrunc = InsertedTruncs[TruncUserBB]; |
| 961 | |
| 962 | if (!InsertedShift && !InsertedTrunc) { |
| 963 | BasicBlock::iterator InsertPt = TruncUserBB->getFirstInsertionPt(); |
| 964 | // Sink the shift |
| 965 | if (ShiftI->getOpcode() == Instruction::AShr) |
| 966 | InsertedShift = |
| 967 | BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, "", InsertPt); |
| 968 | else |
| 969 | InsertedShift = |
| 970 | BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, "", InsertPt); |
| 971 | |
| 972 | // Sink the trunc |
| 973 | BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt(); |
| 974 | TruncInsertPt++; |
| 975 | |
| 976 | InsertedTrunc = CastInst::Create(TruncI->getOpcode(), InsertedShift, |
| 977 | TruncI->getType(), "", TruncInsertPt); |
| 978 | |
| 979 | MadeChange = true; |
| 980 | |
| 981 | TruncTheUse = InsertedTrunc; |
| 982 | } |
| 983 | } |
| 984 | return MadeChange; |
| 985 | } |
| 986 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 987 | /// Sink the shift *right* instruction into user blocks if the uses could |
| 988 | /// potentially be combined with this shift instruction and generate BitExtract |
| 989 | /// instruction. It will only be applied if the architecture supports BitExtract |
| 990 | /// instruction. Here is an example: |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 991 | /// BB1: |
| 992 | /// %x.extract.shift = lshr i64 %arg1, 32 |
| 993 | /// BB2: |
| 994 | /// %x.extract.trunc = trunc i64 %x.extract.shift to i16 |
| 995 | /// ==> |
| 996 | /// |
| 997 | /// BB2: |
| 998 | /// %x.extract.shift.1 = lshr i64 %arg1, 32 |
| 999 | /// %x.extract.trunc = trunc i64 %x.extract.shift.1 to i16 |
| 1000 | /// |
| 1001 | /// CodeGen will recoginze the pattern in BB2 and generate BitExtract |
| 1002 | /// instruction. |
| 1003 | /// Return true if any changes are made. |
| 1004 | static bool OptimizeExtractBits(BinaryOperator *ShiftI, ConstantInt *CI, |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1005 | const TargetLowering &TLI, |
| 1006 | const DataLayout &DL) { |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 1007 | BasicBlock *DefBB = ShiftI->getParent(); |
| 1008 | |
| 1009 | /// Only insert instructions in each block once. |
| 1010 | DenseMap<BasicBlock *, BinaryOperator *> InsertedShifts; |
| 1011 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1012 | bool shiftIsLegal = TLI.isTypeLegal(TLI.getValueType(DL, ShiftI->getType())); |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 1013 | |
| 1014 | bool MadeChange = false; |
| 1015 | for (Value::user_iterator UI = ShiftI->user_begin(), E = ShiftI->user_end(); |
| 1016 | UI != E;) { |
| 1017 | Use &TheUse = UI.getUse(); |
| 1018 | Instruction *User = cast<Instruction>(*UI); |
| 1019 | // Preincrement use iterator so we don't invalidate it. |
| 1020 | ++UI; |
| 1021 | |
| 1022 | // Don't bother for PHI nodes. |
| 1023 | if (isa<PHINode>(User)) |
| 1024 | continue; |
| 1025 | |
| 1026 | if (!isExtractBitsCandidateUse(User)) |
| 1027 | continue; |
| 1028 | |
| 1029 | BasicBlock *UserBB = User->getParent(); |
| 1030 | |
| 1031 | if (UserBB == DefBB) { |
| 1032 | // If the shift and truncate instruction are in the same BB. The use of |
| 1033 | // the truncate(TruncUse) may still introduce another truncate if not |
| 1034 | // legal. In this case, we would like to sink both shift and truncate |
| 1035 | // instruction to the BB of TruncUse. |
| 1036 | // for example: |
| 1037 | // BB1: |
| 1038 | // i64 shift.result = lshr i64 opnd, imm |
| 1039 | // trunc.result = trunc shift.result to i16 |
| 1040 | // |
| 1041 | // BB2: |
| 1042 | // ----> We will have an implicit truncate here if the architecture does |
| 1043 | // not have i16 compare. |
| 1044 | // cmp i16 trunc.result, opnd2 |
| 1045 | // |
| 1046 | if (isa<TruncInst>(User) && shiftIsLegal |
| 1047 | // If the type of the truncate is legal, no trucate will be |
| 1048 | // introduced in other basic blocks. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1049 | && |
| 1050 | (!TLI.isTypeLegal(TLI.getValueType(DL, User->getType())))) |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 1051 | MadeChange = |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1052 | SinkShiftAndTruncate(ShiftI, User, CI, InsertedShifts, TLI, DL); |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 1053 | |
| 1054 | continue; |
| 1055 | } |
| 1056 | // If we have already inserted a shift into this block, use it. |
| 1057 | BinaryOperator *&InsertedShift = InsertedShifts[UserBB]; |
| 1058 | |
| 1059 | if (!InsertedShift) { |
| 1060 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
| 1061 | |
| 1062 | if (ShiftI->getOpcode() == Instruction::AShr) |
| 1063 | InsertedShift = |
| 1064 | BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, "", InsertPt); |
| 1065 | else |
| 1066 | InsertedShift = |
| 1067 | BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, "", InsertPt); |
| 1068 | |
| 1069 | MadeChange = true; |
| 1070 | } |
| 1071 | |
| 1072 | // Replace a use of the shift with a use of the new shift. |
| 1073 | TheUse = InsertedShift; |
| 1074 | } |
| 1075 | |
| 1076 | // If we removed all uses, nuke the shift. |
| 1077 | if (ShiftI->use_empty()) |
| 1078 | ShiftI->eraseFromParent(); |
| 1079 | |
| 1080 | return MadeChange; |
| 1081 | } |
| 1082 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 1083 | // Translate a masked load intrinsic like |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1084 | // <16 x i32 > @llvm.masked.load( <16 x i32>* %addr, i32 align, |
| 1085 | // <16 x i1> %mask, <16 x i32> %passthru) |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1086 | // to a chain of basic blocks, with loading element one-by-one if |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1087 | // the appropriate mask bit is set |
| 1088 | // |
| 1089 | // %1 = bitcast i8* %addr to i32* |
| 1090 | // %2 = extractelement <16 x i1> %mask, i32 0 |
| 1091 | // %3 = icmp eq i1 %2, true |
| 1092 | // br i1 %3, label %cond.load, label %else |
| 1093 | // |
| 1094 | //cond.load: ; preds = %0 |
| 1095 | // %4 = getelementptr i32* %1, i32 0 |
| 1096 | // %5 = load i32* %4 |
| 1097 | // %6 = insertelement <16 x i32> undef, i32 %5, i32 0 |
| 1098 | // br label %else |
| 1099 | // |
| 1100 | //else: ; preds = %0, %cond.load |
| 1101 | // %res.phi.else = phi <16 x i32> [ %6, %cond.load ], [ undef, %0 ] |
| 1102 | // %7 = extractelement <16 x i1> %mask, i32 1 |
| 1103 | // %8 = icmp eq i1 %7, true |
| 1104 | // br i1 %8, label %cond.load1, label %else2 |
| 1105 | // |
| 1106 | //cond.load1: ; preds = %else |
| 1107 | // %9 = getelementptr i32* %1, i32 1 |
| 1108 | // %10 = load i32* %9 |
| 1109 | // %11 = insertelement <16 x i32> %res.phi.else, i32 %10, i32 1 |
| 1110 | // br label %else2 |
| 1111 | // |
| 1112 | //else2: ; preds = %else, %cond.load1 |
| 1113 | // %res.phi.else3 = phi <16 x i32> [ %11, %cond.load1 ], [ %res.phi.else, %else ] |
| 1114 | // %12 = extractelement <16 x i1> %mask, i32 2 |
| 1115 | // %13 = icmp eq i1 %12, true |
| 1116 | // br i1 %13, label %cond.load4, label %else5 |
| 1117 | // |
| 1118 | static void ScalarizeMaskedLoad(CallInst *CI) { |
| 1119 | Value *Ptr = CI->getArgOperand(0); |
| 1120 | Value *Src0 = CI->getArgOperand(3); |
| 1121 | Value *Mask = CI->getArgOperand(2); |
| 1122 | VectorType *VecType = dyn_cast<VectorType>(CI->getType()); |
| 1123 | Type *EltTy = VecType->getElementType(); |
| 1124 | |
| 1125 | assert(VecType && "Unexpected return type of masked load intrinsic"); |
| 1126 | |
| 1127 | IRBuilder<> Builder(CI->getContext()); |
| 1128 | Instruction *InsertPt = CI; |
| 1129 | BasicBlock *IfBlock = CI->getParent(); |
| 1130 | BasicBlock *CondBlock = nullptr; |
| 1131 | BasicBlock *PrevIfBlock = CI->getParent(); |
| 1132 | Builder.SetInsertPoint(InsertPt); |
| 1133 | |
| 1134 | Builder.SetCurrentDebugLocation(CI->getDebugLoc()); |
| 1135 | |
| 1136 | // Bitcast %addr fron i8* to EltTy* |
| 1137 | Type *NewPtrType = |
| 1138 | EltTy->getPointerTo(cast<PointerType>(Ptr->getType())->getAddressSpace()); |
| 1139 | Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType); |
| 1140 | Value *UndefVal = UndefValue::get(VecType); |
| 1141 | |
| 1142 | // The result vector |
| 1143 | Value *VResult = UndefVal; |
| 1144 | |
| 1145 | PHINode *Phi = nullptr; |
| 1146 | Value *PrevPhi = UndefVal; |
| 1147 | |
| 1148 | unsigned VectorWidth = VecType->getNumElements(); |
| 1149 | for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) { |
| 1150 | |
| 1151 | // Fill the "else" block, created in the previous iteration |
| 1152 | // |
| 1153 | // %res.phi.else3 = phi <16 x i32> [ %11, %cond.load1 ], [ %res.phi.else, %else ] |
| 1154 | // %mask_1 = extractelement <16 x i1> %mask, i32 Idx |
| 1155 | // %to_load = icmp eq i1 %mask_1, true |
| 1156 | // br i1 %to_load, label %cond.load, label %else |
| 1157 | // |
| 1158 | if (Idx > 0) { |
| 1159 | Phi = Builder.CreatePHI(VecType, 2, "res.phi.else"); |
| 1160 | Phi->addIncoming(VResult, CondBlock); |
| 1161 | Phi->addIncoming(PrevPhi, PrevIfBlock); |
| 1162 | PrevPhi = Phi; |
| 1163 | VResult = Phi; |
| 1164 | } |
| 1165 | |
| 1166 | Value *Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx)); |
| 1167 | Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, Predicate, |
| 1168 | ConstantInt::get(Predicate->getType(), 1)); |
| 1169 | |
| 1170 | // Create "cond" block |
| 1171 | // |
| 1172 | // %EltAddr = getelementptr i32* %1, i32 0 |
| 1173 | // %Elt = load i32* %EltAddr |
| 1174 | // VResult = insertelement <16 x i32> VResult, i32 %Elt, i32 Idx |
| 1175 | // |
| 1176 | CondBlock = IfBlock->splitBasicBlock(InsertPt, "cond.load"); |
| 1177 | Builder.SetInsertPoint(InsertPt); |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 1178 | |
| 1179 | Value *Gep = |
| 1180 | Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx)); |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1181 | LoadInst* Load = Builder.CreateLoad(Gep, false); |
| 1182 | VResult = Builder.CreateInsertElement(VResult, Load, Builder.getInt32(Idx)); |
| 1183 | |
| 1184 | // Create "else" block, fill it in the next iteration |
| 1185 | BasicBlock *NewIfBlock = CondBlock->splitBasicBlock(InsertPt, "else"); |
| 1186 | Builder.SetInsertPoint(InsertPt); |
| 1187 | Instruction *OldBr = IfBlock->getTerminator(); |
| 1188 | BranchInst::Create(CondBlock, NewIfBlock, Cmp, OldBr); |
| 1189 | OldBr->eraseFromParent(); |
| 1190 | PrevIfBlock = IfBlock; |
| 1191 | IfBlock = NewIfBlock; |
| 1192 | } |
| 1193 | |
| 1194 | Phi = Builder.CreatePHI(VecType, 2, "res.phi.select"); |
| 1195 | Phi->addIncoming(VResult, CondBlock); |
| 1196 | Phi->addIncoming(PrevPhi, PrevIfBlock); |
| 1197 | Value *NewI = Builder.CreateSelect(Mask, Phi, Src0); |
| 1198 | CI->replaceAllUsesWith(NewI); |
| 1199 | CI->eraseFromParent(); |
| 1200 | } |
| 1201 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 1202 | // Translate a masked store intrinsic, like |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1203 | // void @llvm.masked.store(<16 x i32> %src, <16 x i32>* %addr, i32 align, |
| 1204 | // <16 x i1> %mask) |
| 1205 | // to a chain of basic blocks, that stores element one-by-one if |
| 1206 | // the appropriate mask bit is set |
| 1207 | // |
| 1208 | // %1 = bitcast i8* %addr to i32* |
| 1209 | // %2 = extractelement <16 x i1> %mask, i32 0 |
| 1210 | // %3 = icmp eq i1 %2, true |
| 1211 | // br i1 %3, label %cond.store, label %else |
| 1212 | // |
| 1213 | // cond.store: ; preds = %0 |
| 1214 | // %4 = extractelement <16 x i32> %val, i32 0 |
| 1215 | // %5 = getelementptr i32* %1, i32 0 |
| 1216 | // store i32 %4, i32* %5 |
| 1217 | // br label %else |
| 1218 | // |
| 1219 | // else: ; preds = %0, %cond.store |
| 1220 | // %6 = extractelement <16 x i1> %mask, i32 1 |
| 1221 | // %7 = icmp eq i1 %6, true |
| 1222 | // br i1 %7, label %cond.store1, label %else2 |
| 1223 | // |
| 1224 | // cond.store1: ; preds = %else |
| 1225 | // %8 = extractelement <16 x i32> %val, i32 1 |
| 1226 | // %9 = getelementptr i32* %1, i32 1 |
| 1227 | // store i32 %8, i32* %9 |
| 1228 | // br label %else2 |
| 1229 | // . . . |
| 1230 | static void ScalarizeMaskedStore(CallInst *CI) { |
| 1231 | Value *Ptr = CI->getArgOperand(1); |
| 1232 | Value *Src = CI->getArgOperand(0); |
| 1233 | Value *Mask = CI->getArgOperand(3); |
| 1234 | |
| 1235 | VectorType *VecType = dyn_cast<VectorType>(Src->getType()); |
| 1236 | Type *EltTy = VecType->getElementType(); |
| 1237 | |
| 1238 | assert(VecType && "Unexpected data type in masked store intrinsic"); |
| 1239 | |
| 1240 | IRBuilder<> Builder(CI->getContext()); |
| 1241 | Instruction *InsertPt = CI; |
| 1242 | BasicBlock *IfBlock = CI->getParent(); |
| 1243 | Builder.SetInsertPoint(InsertPt); |
| 1244 | Builder.SetCurrentDebugLocation(CI->getDebugLoc()); |
| 1245 | |
| 1246 | // Bitcast %addr fron i8* to EltTy* |
| 1247 | Type *NewPtrType = |
| 1248 | EltTy->getPointerTo(cast<PointerType>(Ptr->getType())->getAddressSpace()); |
| 1249 | Value *FirstEltPtr = Builder.CreateBitCast(Ptr, NewPtrType); |
| 1250 | |
| 1251 | unsigned VectorWidth = VecType->getNumElements(); |
| 1252 | for (unsigned Idx = 0; Idx < VectorWidth; ++Idx) { |
| 1253 | |
| 1254 | // Fill the "else" block, created in the previous iteration |
| 1255 | // |
| 1256 | // %mask_1 = extractelement <16 x i1> %mask, i32 Idx |
| 1257 | // %to_store = icmp eq i1 %mask_1, true |
| 1258 | // br i1 %to_load, label %cond.store, label %else |
| 1259 | // |
| 1260 | Value *Predicate = Builder.CreateExtractElement(Mask, Builder.getInt32(Idx)); |
| 1261 | Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, Predicate, |
| 1262 | ConstantInt::get(Predicate->getType(), 1)); |
| 1263 | |
| 1264 | // Create "cond" block |
| 1265 | // |
| 1266 | // %OneElt = extractelement <16 x i32> %Src, i32 Idx |
| 1267 | // %EltAddr = getelementptr i32* %1, i32 0 |
| 1268 | // %store i32 %OneElt, i32* %EltAddr |
| 1269 | // |
| 1270 | BasicBlock *CondBlock = IfBlock->splitBasicBlock(InsertPt, "cond.store"); |
| 1271 | Builder.SetInsertPoint(InsertPt); |
| 1272 | |
| 1273 | Value *OneElt = Builder.CreateExtractElement(Src, Builder.getInt32(Idx)); |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 1274 | Value *Gep = |
| 1275 | Builder.CreateInBoundsGEP(EltTy, FirstEltPtr, Builder.getInt32(Idx)); |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1276 | Builder.CreateStore(OneElt, Gep); |
| 1277 | |
| 1278 | // Create "else" block, fill it in the next iteration |
| 1279 | BasicBlock *NewIfBlock = CondBlock->splitBasicBlock(InsertPt, "else"); |
| 1280 | Builder.SetInsertPoint(InsertPt); |
| 1281 | Instruction *OldBr = IfBlock->getTerminator(); |
| 1282 | BranchInst::Create(CondBlock, NewIfBlock, Cmp, OldBr); |
| 1283 | OldBr->eraseFromParent(); |
| 1284 | IfBlock = NewIfBlock; |
| 1285 | } |
| 1286 | CI->eraseFromParent(); |
| 1287 | } |
| 1288 | |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 1289 | bool CodeGenPrepare::optimizeCallInst(CallInst *CI, bool& ModifiedDT) { |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1290 | BasicBlock *BB = CI->getParent(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1292 | // Lower inline assembly if we can. |
| 1293 | // If we found an inline asm expession, and if the target knows how to |
| 1294 | // lower it to normal LLVM code, do so now. |
| 1295 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) { |
| 1296 | if (TLI->ExpandInlineAsm(CI)) { |
| 1297 | // Avoid invalidating the iterator. |
| 1298 | CurInstIterator = BB->begin(); |
| 1299 | // Avoid processing instructions out of order, which could cause |
| 1300 | // reuse before a value is defined. |
| 1301 | SunkAddrs.clear(); |
| 1302 | return true; |
| 1303 | } |
| 1304 | // Sink address computing for memory operands into the block. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 1305 | if (optimizeInlineAsmInst(CI)) |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1306 | return true; |
| 1307 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1308 | |
John Brawn | 0dbcd65 | 2015-03-18 12:01:59 +0000 | [diff] [blame] | 1309 | // Align the pointer arguments to this call if the target thinks it's a good |
| 1310 | // idea |
| 1311 | unsigned MinSize, PrefAlign; |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 1312 | if (TLI && TLI->shouldAlignPointerArgs(CI, MinSize, PrefAlign)) { |
John Brawn | 0dbcd65 | 2015-03-18 12:01:59 +0000 | [diff] [blame] | 1313 | for (auto &Arg : CI->arg_operands()) { |
| 1314 | // We want to align both objects whose address is used directly and |
| 1315 | // objects whose address is used in casts and GEPs, though it only makes |
| 1316 | // sense for GEPs if the offset is a multiple of the desired alignment and |
| 1317 | // if size - offset meets the size threshold. |
| 1318 | if (!Arg->getType()->isPointerTy()) |
| 1319 | continue; |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 1320 | APInt Offset(DL->getPointerSizeInBits( |
| 1321 | cast<PointerType>(Arg->getType())->getAddressSpace()), |
| 1322 | 0); |
| 1323 | Value *Val = Arg->stripAndAccumulateInBoundsConstantOffsets(*DL, Offset); |
John Brawn | 0dbcd65 | 2015-03-18 12:01:59 +0000 | [diff] [blame] | 1324 | uint64_t Offset2 = Offset.getLimitedValue(); |
John Brawn | e8fd6c8 | 2015-04-13 10:47:39 +0000 | [diff] [blame] | 1325 | if ((Offset2 & (PrefAlign-1)) != 0) |
| 1326 | continue; |
John Brawn | 0dbcd65 | 2015-03-18 12:01:59 +0000 | [diff] [blame] | 1327 | AllocaInst *AI; |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 1328 | if ((AI = dyn_cast<AllocaInst>(Val)) && AI->getAlignment() < PrefAlign && |
| 1329 | DL->getTypeAllocSize(AI->getAllocatedType()) >= MinSize + Offset2) |
John Brawn | 0dbcd65 | 2015-03-18 12:01:59 +0000 | [diff] [blame] | 1330 | AI->setAlignment(PrefAlign); |
John Brawn | e8fd6c8 | 2015-04-13 10:47:39 +0000 | [diff] [blame] | 1331 | // Global variables can only be aligned if they are defined in this |
| 1332 | // object (i.e. they are uniquely initialized in this object), and |
| 1333 | // over-aligning global variables that have an explicit section is |
| 1334 | // forbidden. |
| 1335 | GlobalVariable *GV; |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 1336 | if ((GV = dyn_cast<GlobalVariable>(Val)) && GV->hasUniqueInitializer() && |
| 1337 | !GV->hasSection() && GV->getAlignment() < PrefAlign && |
| 1338 | DL->getTypeAllocSize(GV->getType()->getElementType()) >= |
| 1339 | MinSize + Offset2) |
John Brawn | e8fd6c8 | 2015-04-13 10:47:39 +0000 | [diff] [blame] | 1340 | GV->setAlignment(PrefAlign); |
John Brawn | 0dbcd65 | 2015-03-18 12:01:59 +0000 | [diff] [blame] | 1341 | } |
| 1342 | // If this is a memcpy (or similar) then we may be able to improve the |
| 1343 | // alignment |
| 1344 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(CI)) { |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 1345 | unsigned Align = getKnownAlignment(MI->getDest(), *DL); |
John Brawn | 0dbcd65 | 2015-03-18 12:01:59 +0000 | [diff] [blame] | 1346 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 1347 | Align = std::min(Align, getKnownAlignment(MTI->getSource(), *DL)); |
John Brawn | 0dbcd65 | 2015-03-18 12:01:59 +0000 | [diff] [blame] | 1348 | if (Align > MI->getAlignment()) |
| 1349 | MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), Align)); |
| 1350 | } |
| 1351 | } |
| 1352 | |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1353 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1354 | if (II) { |
| 1355 | switch (II->getIntrinsicID()) { |
| 1356 | default: break; |
| 1357 | case Intrinsic::objectsize: { |
| 1358 | // Lower all uses of llvm.objectsize.* |
| 1359 | bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1); |
| 1360 | Type *ReturnTy = CI->getType(); |
| 1361 | Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1362 | |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1363 | // Substituting this can cause recursive simplifications, which can |
| 1364 | // invalidate our iterator. Use a WeakVH to hold onto it in case this |
| 1365 | // happens. |
| 1366 | WeakVH IterHandle(CurInstIterator); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1367 | |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1368 | replaceAndRecursivelySimplify(CI, RetVal, |
Quentin Colombet | 7bdd50d | 2015-03-18 23:17:28 +0000 | [diff] [blame] | 1369 | TLInfo, nullptr); |
Chris Lattner | 1b93be5 | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 1370 | |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1371 | // If the iterator instruction was recursively deleted, start over at the |
| 1372 | // start of the block. |
| 1373 | if (IterHandle != CurInstIterator) { |
| 1374 | CurInstIterator = BB->begin(); |
| 1375 | SunkAddrs.clear(); |
| 1376 | } |
| 1377 | return true; |
Chris Lattner | 86d56c6 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 1378 | } |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1379 | case Intrinsic::masked_load: { |
| 1380 | // Scalarize unsupported vector masked load |
| 1381 | if (!TTI->isLegalMaskedLoad(CI->getType(), 1)) { |
| 1382 | ScalarizeMaskedLoad(CI); |
| 1383 | ModifiedDT = true; |
| 1384 | return true; |
| 1385 | } |
| 1386 | return false; |
| 1387 | } |
| 1388 | case Intrinsic::masked_store: { |
| 1389 | if (!TTI->isLegalMaskedStore(CI->getArgOperand(0)->getType(), 1)) { |
| 1390 | ScalarizeMaskedStore(CI); |
| 1391 | ModifiedDT = true; |
| 1392 | return true; |
| 1393 | } |
| 1394 | return false; |
| 1395 | } |
Ahmed Bougacha | 236f904 | 2015-05-22 21:37:17 +0000 | [diff] [blame] | 1396 | case Intrinsic::aarch64_stlxr: |
| 1397 | case Intrinsic::aarch64_stxr: { |
| 1398 | ZExtInst *ExtVal = dyn_cast<ZExtInst>(CI->getArgOperand(0)); |
| 1399 | if (!ExtVal || !ExtVal->hasOneUse() || |
| 1400 | ExtVal->getParent() == CI->getParent()) |
| 1401 | return false; |
| 1402 | // Sink a zext feeding stlxr/stxr before it, so it can be folded into it. |
| 1403 | ExtVal->moveBefore(CI); |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 1404 | // Mark this instruction as "inserted by CGP", so that other |
| 1405 | // optimizations don't touch it. |
| 1406 | InsertedInsts.insert(ExtVal); |
Ahmed Bougacha | 236f904 | 2015-05-22 21:37:17 +0000 | [diff] [blame] | 1407 | return true; |
| 1408 | } |
Piotr Padlewski | 6c15ec4 | 2015-09-15 18:32:14 +0000 | [diff] [blame] | 1409 | case Intrinsic::invariant_group_barrier: |
| 1410 | II->replaceAllUsesWith(II->getArgOperand(0)); |
| 1411 | II->eraseFromParent(); |
| 1412 | return true; |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1413 | } |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1414 | |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1415 | if (TLI) { |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 1416 | // Unknown address space. |
| 1417 | // TODO: Target hook to pick which address space the intrinsic cares |
| 1418 | // about? |
| 1419 | unsigned AddrSpace = ~0u; |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1420 | SmallVector<Value*, 2> PtrOps; |
| 1421 | Type *AccessTy; |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 1422 | if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy, AddrSpace)) |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1423 | while (!PtrOps.empty()) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 1424 | if (optimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy, AddrSpace)) |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 1425 | return true; |
| 1426 | } |
Pete Cooper | 615fd89 | 2012-03-13 20:59:56 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1429 | // From here on out we're working with named functions. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1430 | if (!CI->getCalledFunction()) return false; |
Devang Patel | 0da5250 | 2011-05-26 21:51:06 +0000 | [diff] [blame] | 1431 | |
Benjamin Kramer | 7b88a49 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 1432 | // Lower all default uses of _chk calls. This is very similar |
| 1433 | // to what InstCombineCalls does, but here we are only lowering calls |
Ahmed Bougacha | e03bef7 | 2015-01-12 17:22:43 +0000 | [diff] [blame] | 1434 | // to fortified library functions (e.g. __memcpy_chk) that have the default |
| 1435 | // "don't know" as the objectsize. Anything else should be left alone. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1436 | FortifiedLibCallSimplifier Simplifier(TLInfo, true); |
Ahmed Bougacha | e03bef7 | 2015-01-12 17:22:43 +0000 | [diff] [blame] | 1437 | if (Value *V = Simplifier.optimizeCall(CI)) { |
| 1438 | CI->replaceAllUsesWith(V); |
| 1439 | CI->eraseFromParent(); |
| 1440 | return true; |
| 1441 | } |
| 1442 | return false; |
Eric Christopher | 4b7948e | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 1443 | } |
Chris Lattner | 1b93be5 | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 1444 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 1445 | /// Look for opportunities to duplicate return instructions to the predecessor |
| 1446 | /// to enable tail call optimizations. The case it is currently looking for is: |
Dmitri Gribenko | 2bc1d48 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 1447 | /// @code |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1448 | /// bb0: |
| 1449 | /// %tmp0 = tail call i32 @f0() |
| 1450 | /// br label %return |
| 1451 | /// bb1: |
| 1452 | /// %tmp1 = tail call i32 @f1() |
| 1453 | /// br label %return |
| 1454 | /// bb2: |
| 1455 | /// %tmp2 = tail call i32 @f2() |
| 1456 | /// br label %return |
| 1457 | /// return: |
| 1458 | /// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ] |
| 1459 | /// ret i32 %retval |
Dmitri Gribenko | 2bc1d48 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 1460 | /// @endcode |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1461 | /// |
| 1462 | /// => |
| 1463 | /// |
Dmitri Gribenko | 2bc1d48 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 1464 | /// @code |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1465 | /// bb0: |
| 1466 | /// %tmp0 = tail call i32 @f0() |
| 1467 | /// ret i32 %tmp0 |
| 1468 | /// bb1: |
| 1469 | /// %tmp1 = tail call i32 @f1() |
| 1470 | /// ret i32 %tmp1 |
| 1471 | /// bb2: |
| 1472 | /// %tmp2 = tail call i32 @f2() |
| 1473 | /// ret i32 %tmp2 |
Dmitri Gribenko | 2bc1d48 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 1474 | /// @endcode |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 1475 | bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB) { |
Cameron Zwarich | 47e7175 | 2011-03-24 04:51:51 +0000 | [diff] [blame] | 1476 | if (!TLI) |
| 1477 | return false; |
| 1478 | |
Benjamin Kramer | 455fa35 | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 1479 | ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); |
| 1480 | if (!RI) |
| 1481 | return false; |
| 1482 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1483 | PHINode *PN = nullptr; |
| 1484 | BitCastInst *BCI = nullptr; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1485 | Value *V = RI->getReturnValue(); |
Evan Cheng | 249716e | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 1486 | if (V) { |
| 1487 | BCI = dyn_cast<BitCastInst>(V); |
| 1488 | if (BCI) |
| 1489 | V = BCI->getOperand(0); |
| 1490 | |
| 1491 | PN = dyn_cast<PHINode>(V); |
| 1492 | if (!PN) |
| 1493 | return false; |
| 1494 | } |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1495 | |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1496 | if (PN && PN->getParent() != BB) |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1497 | return false; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1498 | |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1499 | // It's not safe to eliminate the sign / zero extension of the return value. |
| 1500 | // See llvm::isInTailCallPosition(). |
| 1501 | const Function *F = BB->getParent(); |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1502 | AttributeSet CallerAttrs = F->getAttributes(); |
| 1503 | if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) || |
| 1504 | CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt)) |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1505 | return false; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1506 | |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1507 | // Make sure there are no instructions between the PHI and return, or that the |
| 1508 | // return is the first instruction in the block. |
| 1509 | if (PN) { |
| 1510 | BasicBlock::iterator BI = BB->begin(); |
| 1511 | do { ++BI; } while (isa<DbgInfoIntrinsic>(BI)); |
Evan Cheng | 249716e | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 1512 | if (&*BI == BCI) |
| 1513 | // Also skip over the bitcast. |
| 1514 | ++BI; |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1515 | if (&*BI != RI) |
| 1516 | return false; |
| 1517 | } else { |
Cameron Zwarich | 74157ab | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 1518 | BasicBlock::iterator BI = BB->begin(); |
| 1519 | while (isa<DbgInfoIntrinsic>(BI)) ++BI; |
| 1520 | if (&*BI != RI) |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1521 | return false; |
| 1522 | } |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1523 | |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1524 | /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail |
| 1525 | /// call. |
| 1526 | SmallVector<CallInst*, 4> TailCalls; |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1527 | if (PN) { |
| 1528 | for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) { |
| 1529 | CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I)); |
| 1530 | // Make sure the phi value is indeed produced by the tail call. |
| 1531 | if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) && |
| 1532 | TLI->mayBeEmittedAsTailCall(CI)) |
| 1533 | TailCalls.push_back(CI); |
| 1534 | } |
| 1535 | } else { |
| 1536 | SmallPtrSet<BasicBlock*, 4> VisitedBBs; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 1537 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1538 | if (!VisitedBBs.insert(*PI).second) |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1539 | continue; |
| 1540 | |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 1541 | BasicBlock::InstListType &InstList = (*PI)->getInstList(); |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1542 | BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin(); |
| 1543 | BasicBlock::InstListType::reverse_iterator RE = InstList.rend(); |
Cameron Zwarich | 74157ab | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 1544 | do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI)); |
| 1545 | if (RI == RE) |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1546 | continue; |
Cameron Zwarich | 74157ab | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 1547 | |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1548 | CallInst *CI = dyn_cast<CallInst>(&*RI); |
Cameron Zwarich | 2edfe77 | 2011-03-24 15:54:11 +0000 | [diff] [blame] | 1549 | if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI)) |
Cameron Zwarich | 4649f17 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 1550 | TailCalls.push_back(CI); |
| 1551 | } |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1554 | bool Changed = false; |
| 1555 | for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) { |
| 1556 | CallInst *CI = TailCalls[i]; |
| 1557 | CallSite CS(CI); |
| 1558 | |
| 1559 | // Conservatively require the attributes of the call to match those of the |
| 1560 | // return. Ignore noalias because it doesn't affect the call sequence. |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1561 | AttributeSet CalleeAttrs = CS.getAttributes(); |
| 1562 | if (AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex). |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1563 | removeAttribute(Attribute::NoAlias) != |
Bill Wendling | 658d24d | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 1564 | AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex). |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1565 | removeAttribute(Attribute::NoAlias)) |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1566 | continue; |
| 1567 | |
| 1568 | // Make sure the call instruction is followed by an unconditional branch to |
| 1569 | // the return block. |
| 1570 | BasicBlock *CallBB = CI->getParent(); |
| 1571 | BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator()); |
| 1572 | if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB) |
| 1573 | continue; |
| 1574 | |
| 1575 | // Duplicate the return into CallBB. |
| 1576 | (void)FoldReturnIntoUncondBranch(RI, BB, CallBB); |
Devang Patel | 8f606d7 | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 1577 | ModifiedDT = Changed = true; |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1578 | ++NumRetsDup; |
| 1579 | } |
| 1580 | |
| 1581 | // If we eliminated all predecessors of the block, delete the block now. |
Evan Cheng | 64a223a | 2012-09-28 23:58:57 +0000 | [diff] [blame] | 1582 | if (Changed && !BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB)) |
Cameron Zwarich | 0e331c0 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 1583 | BB->eraseFromParent(); |
| 1584 | |
| 1585 | return Changed; |
Evan Cheng | 0663f23 | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Chris Lattner | 728f902 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1588 | //===----------------------------------------------------------------------===// |
Chris Lattner | 728f902 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1589 | // Memory Optimization |
| 1590 | //===----------------------------------------------------------------------===// |
| 1591 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1592 | namespace { |
| 1593 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 1594 | /// This is an extended version of TargetLowering::AddrMode |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1595 | /// which holds actual Value*'s for register values. |
Chandler Carruth | 95f83e0 | 2013-01-07 15:14:13 +0000 | [diff] [blame] | 1596 | struct ExtAddrMode : public TargetLowering::AddrMode { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1597 | Value *BaseReg; |
| 1598 | Value *ScaledReg; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1599 | ExtAddrMode() : BaseReg(nullptr), ScaledReg(nullptr) {} |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1600 | void print(raw_ostream &OS) const; |
| 1601 | void dump() const; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1602 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1603 | bool operator==(const ExtAddrMode& O) const { |
| 1604 | return (BaseReg == O.BaseReg) && (ScaledReg == O.ScaledReg) && |
| 1605 | (BaseGV == O.BaseGV) && (BaseOffs == O.BaseOffs) && |
| 1606 | (HasBaseReg == O.HasBaseReg) && (Scale == O.Scale); |
| 1607 | } |
| 1608 | }; |
| 1609 | |
Eli Friedman | c1f1f85 | 2013-09-10 23:09:24 +0000 | [diff] [blame] | 1610 | #ifndef NDEBUG |
| 1611 | static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) { |
| 1612 | AM.print(OS); |
| 1613 | return OS; |
| 1614 | } |
| 1615 | #endif |
| 1616 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1617 | void ExtAddrMode::print(raw_ostream &OS) const { |
| 1618 | bool NeedPlus = false; |
| 1619 | OS << "["; |
| 1620 | if (BaseGV) { |
| 1621 | OS << (NeedPlus ? " + " : "") |
| 1622 | << "GV:"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1623 | BaseGV->printAsOperand(OS, /*PrintType=*/false); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1624 | NeedPlus = true; |
| 1625 | } |
| 1626 | |
Richard Trieu | c0f9121 | 2014-05-30 03:15:17 +0000 | [diff] [blame] | 1627 | if (BaseOffs) { |
| 1628 | OS << (NeedPlus ? " + " : "") |
| 1629 | << BaseOffs; |
| 1630 | NeedPlus = true; |
| 1631 | } |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1632 | |
| 1633 | if (BaseReg) { |
| 1634 | OS << (NeedPlus ? " + " : "") |
| 1635 | << "Base:"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1636 | BaseReg->printAsOperand(OS, /*PrintType=*/false); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1637 | NeedPlus = true; |
| 1638 | } |
| 1639 | if (Scale) { |
| 1640 | OS << (NeedPlus ? " + " : "") |
| 1641 | << Scale << "*"; |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 1642 | ScaledReg->printAsOperand(OS, /*PrintType=*/false); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | OS << ']'; |
| 1646 | } |
| 1647 | |
| 1648 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 1649 | void ExtAddrMode::dump() const { |
| 1650 | print(dbgs()); |
| 1651 | dbgs() << '\n'; |
| 1652 | } |
| 1653 | #endif |
| 1654 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1655 | /// \brief This class provides transaction based operation on the IR. |
| 1656 | /// Every change made through this class is recorded in the internal state and |
| 1657 | /// can be undone (rollback) until commit is called. |
| 1658 | class TypePromotionTransaction { |
| 1659 | |
| 1660 | /// \brief This represents the common interface of the individual transaction. |
| 1661 | /// Each class implements the logic for doing one specific modification on |
| 1662 | /// the IR via the TypePromotionTransaction. |
| 1663 | class TypePromotionAction { |
| 1664 | protected: |
| 1665 | /// The Instruction modified. |
| 1666 | Instruction *Inst; |
| 1667 | |
| 1668 | public: |
| 1669 | /// \brief Constructor of the action. |
| 1670 | /// The constructor performs the related action on the IR. |
| 1671 | TypePromotionAction(Instruction *Inst) : Inst(Inst) {} |
| 1672 | |
| 1673 | virtual ~TypePromotionAction() {} |
| 1674 | |
| 1675 | /// \brief Undo the modification done by this action. |
| 1676 | /// When this method is called, the IR must be in the same state as it was |
| 1677 | /// before this action was applied. |
| 1678 | /// \pre Undoing the action works if and only if the IR is in the exact same |
| 1679 | /// state as it was directly after this action was applied. |
| 1680 | virtual void undo() = 0; |
| 1681 | |
| 1682 | /// \brief Advocate every change made by this action. |
| 1683 | /// When the results on the IR of the action are to be kept, it is important |
| 1684 | /// to call this function, otherwise hidden information may be kept forever. |
| 1685 | virtual void commit() { |
| 1686 | // Nothing to be done, this action is not doing anything. |
| 1687 | } |
| 1688 | }; |
| 1689 | |
| 1690 | /// \brief Utility to remember the position of an instruction. |
| 1691 | class InsertionHandler { |
| 1692 | /// Position of an instruction. |
| 1693 | /// Either an instruction: |
| 1694 | /// - Is the first in a basic block: BB is used. |
| 1695 | /// - Has a previous instructon: PrevInst is used. |
| 1696 | union { |
| 1697 | Instruction *PrevInst; |
| 1698 | BasicBlock *BB; |
| 1699 | } Point; |
| 1700 | /// Remember whether or not the instruction had a previous instruction. |
| 1701 | bool HasPrevInstruction; |
| 1702 | |
| 1703 | public: |
| 1704 | /// \brief Record the position of \p Inst. |
| 1705 | InsertionHandler(Instruction *Inst) { |
| 1706 | BasicBlock::iterator It = Inst; |
| 1707 | HasPrevInstruction = (It != (Inst->getParent()->begin())); |
| 1708 | if (HasPrevInstruction) |
| 1709 | Point.PrevInst = --It; |
| 1710 | else |
| 1711 | Point.BB = Inst->getParent(); |
| 1712 | } |
| 1713 | |
| 1714 | /// \brief Insert \p Inst at the recorded position. |
| 1715 | void insert(Instruction *Inst) { |
| 1716 | if (HasPrevInstruction) { |
| 1717 | if (Inst->getParent()) |
| 1718 | Inst->removeFromParent(); |
| 1719 | Inst->insertAfter(Point.PrevInst); |
| 1720 | } else { |
| 1721 | Instruction *Position = Point.BB->getFirstInsertionPt(); |
| 1722 | if (Inst->getParent()) |
| 1723 | Inst->moveBefore(Position); |
| 1724 | else |
| 1725 | Inst->insertBefore(Position); |
| 1726 | } |
| 1727 | } |
| 1728 | }; |
| 1729 | |
| 1730 | /// \brief Move an instruction before another. |
| 1731 | class InstructionMoveBefore : public TypePromotionAction { |
| 1732 | /// Original position of the instruction. |
| 1733 | InsertionHandler Position; |
| 1734 | |
| 1735 | public: |
| 1736 | /// \brief Move \p Inst before \p Before. |
| 1737 | InstructionMoveBefore(Instruction *Inst, Instruction *Before) |
| 1738 | : TypePromotionAction(Inst), Position(Inst) { |
| 1739 | DEBUG(dbgs() << "Do: move: " << *Inst << "\nbefore: " << *Before << "\n"); |
| 1740 | Inst->moveBefore(Before); |
| 1741 | } |
| 1742 | |
| 1743 | /// \brief Move the instruction back to its original position. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1744 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1745 | DEBUG(dbgs() << "Undo: moveBefore: " << *Inst << "\n"); |
| 1746 | Position.insert(Inst); |
| 1747 | } |
| 1748 | }; |
| 1749 | |
| 1750 | /// \brief Set the operand of an instruction with a new value. |
| 1751 | class OperandSetter : public TypePromotionAction { |
| 1752 | /// Original operand of the instruction. |
| 1753 | Value *Origin; |
| 1754 | /// Index of the modified instruction. |
| 1755 | unsigned Idx; |
| 1756 | |
| 1757 | public: |
| 1758 | /// \brief Set \p Idx operand of \p Inst with \p NewVal. |
| 1759 | OperandSetter(Instruction *Inst, unsigned Idx, Value *NewVal) |
| 1760 | : TypePromotionAction(Inst), Idx(Idx) { |
| 1761 | DEBUG(dbgs() << "Do: setOperand: " << Idx << "\n" |
| 1762 | << "for:" << *Inst << "\n" |
| 1763 | << "with:" << *NewVal << "\n"); |
| 1764 | Origin = Inst->getOperand(Idx); |
| 1765 | Inst->setOperand(Idx, NewVal); |
| 1766 | } |
| 1767 | |
| 1768 | /// \brief Restore the original value of the instruction. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1769 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1770 | DEBUG(dbgs() << "Undo: setOperand:" << Idx << "\n" |
| 1771 | << "for: " << *Inst << "\n" |
| 1772 | << "with: " << *Origin << "\n"); |
| 1773 | Inst->setOperand(Idx, Origin); |
| 1774 | } |
| 1775 | }; |
| 1776 | |
| 1777 | /// \brief Hide the operands of an instruction. |
| 1778 | /// Do as if this instruction was not using any of its operands. |
| 1779 | class OperandsHider : public TypePromotionAction { |
| 1780 | /// The list of original operands. |
| 1781 | SmallVector<Value *, 4> OriginalValues; |
| 1782 | |
| 1783 | public: |
| 1784 | /// \brief Remove \p Inst from the uses of the operands of \p Inst. |
| 1785 | OperandsHider(Instruction *Inst) : TypePromotionAction(Inst) { |
| 1786 | DEBUG(dbgs() << "Do: OperandsHider: " << *Inst << "\n"); |
| 1787 | unsigned NumOpnds = Inst->getNumOperands(); |
| 1788 | OriginalValues.reserve(NumOpnds); |
| 1789 | for (unsigned It = 0; It < NumOpnds; ++It) { |
| 1790 | // Save the current operand. |
| 1791 | Value *Val = Inst->getOperand(It); |
| 1792 | OriginalValues.push_back(Val); |
| 1793 | // Set a dummy one. |
| 1794 | // We could use OperandSetter here, but that would implied an overhead |
| 1795 | // that we are not willing to pay. |
| 1796 | Inst->setOperand(It, UndefValue::get(Val->getType())); |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /// \brief Restore the original list of uses. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1801 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1802 | DEBUG(dbgs() << "Undo: OperandsHider: " << *Inst << "\n"); |
| 1803 | for (unsigned It = 0, EndIt = OriginalValues.size(); It != EndIt; ++It) |
| 1804 | Inst->setOperand(It, OriginalValues[It]); |
| 1805 | } |
| 1806 | }; |
| 1807 | |
| 1808 | /// \brief Build a truncate instruction. |
| 1809 | class TruncBuilder : public TypePromotionAction { |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1810 | Value *Val; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1811 | public: |
| 1812 | /// \brief Build a truncate instruction of \p Opnd producing a \p Ty |
| 1813 | /// result. |
| 1814 | /// trunc Opnd to Ty. |
| 1815 | TruncBuilder(Instruction *Opnd, Type *Ty) : TypePromotionAction(Opnd) { |
| 1816 | IRBuilder<> Builder(Opnd); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1817 | Val = Builder.CreateTrunc(Opnd, Ty, "promoted"); |
| 1818 | DEBUG(dbgs() << "Do: TruncBuilder: " << *Val << "\n"); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1821 | /// \brief Get the built value. |
| 1822 | Value *getBuiltValue() { return Val; } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1823 | |
| 1824 | /// \brief Remove the built instruction. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1825 | void undo() override { |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1826 | DEBUG(dbgs() << "Undo: TruncBuilder: " << *Val << "\n"); |
| 1827 | if (Instruction *IVal = dyn_cast<Instruction>(Val)) |
| 1828 | IVal->eraseFromParent(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1829 | } |
| 1830 | }; |
| 1831 | |
| 1832 | /// \brief Build a sign extension instruction. |
| 1833 | class SExtBuilder : public TypePromotionAction { |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1834 | Value *Val; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1835 | public: |
| 1836 | /// \brief Build a sign extension instruction of \p Opnd producing a \p Ty |
| 1837 | /// result. |
| 1838 | /// sext Opnd to Ty. |
| 1839 | SExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty) |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1840 | : TypePromotionAction(InsertPt) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1841 | IRBuilder<> Builder(InsertPt); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1842 | Val = Builder.CreateSExt(Opnd, Ty, "promoted"); |
| 1843 | DEBUG(dbgs() << "Do: SExtBuilder: " << *Val << "\n"); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1846 | /// \brief Get the built value. |
| 1847 | Value *getBuiltValue() { return Val; } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1848 | |
| 1849 | /// \brief Remove the built instruction. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1850 | void undo() override { |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1851 | DEBUG(dbgs() << "Undo: SExtBuilder: " << *Val << "\n"); |
| 1852 | if (Instruction *IVal = dyn_cast<Instruction>(Val)) |
| 1853 | IVal->eraseFromParent(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1854 | } |
| 1855 | }; |
| 1856 | |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 1857 | /// \brief Build a zero extension instruction. |
| 1858 | class ZExtBuilder : public TypePromotionAction { |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1859 | Value *Val; |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 1860 | public: |
| 1861 | /// \brief Build a zero extension instruction of \p Opnd producing a \p Ty |
| 1862 | /// result. |
| 1863 | /// zext Opnd to Ty. |
| 1864 | ZExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty) |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1865 | : TypePromotionAction(InsertPt) { |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 1866 | IRBuilder<> Builder(InsertPt); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1867 | Val = Builder.CreateZExt(Opnd, Ty, "promoted"); |
| 1868 | DEBUG(dbgs() << "Do: ZExtBuilder: " << *Val << "\n"); |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1871 | /// \brief Get the built value. |
| 1872 | Value *getBuiltValue() { return Val; } |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 1873 | |
| 1874 | /// \brief Remove the built instruction. |
| 1875 | void undo() override { |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 1876 | DEBUG(dbgs() << "Undo: ZExtBuilder: " << *Val << "\n"); |
| 1877 | if (Instruction *IVal = dyn_cast<Instruction>(Val)) |
| 1878 | IVal->eraseFromParent(); |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 1879 | } |
| 1880 | }; |
| 1881 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1882 | /// \brief Mutate an instruction to another type. |
| 1883 | class TypeMutator : public TypePromotionAction { |
| 1884 | /// Record the original type. |
| 1885 | Type *OrigTy; |
| 1886 | |
| 1887 | public: |
| 1888 | /// \brief Mutate the type of \p Inst into \p NewTy. |
| 1889 | TypeMutator(Instruction *Inst, Type *NewTy) |
| 1890 | : TypePromotionAction(Inst), OrigTy(Inst->getType()) { |
| 1891 | DEBUG(dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTy |
| 1892 | << "\n"); |
| 1893 | Inst->mutateType(NewTy); |
| 1894 | } |
| 1895 | |
| 1896 | /// \brief Mutate the instruction back to its original type. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1897 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1898 | DEBUG(dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTy |
| 1899 | << "\n"); |
| 1900 | Inst->mutateType(OrigTy); |
| 1901 | } |
| 1902 | }; |
| 1903 | |
| 1904 | /// \brief Replace the uses of an instruction by another instruction. |
| 1905 | class UsesReplacer : public TypePromotionAction { |
| 1906 | /// Helper structure to keep track of the replaced uses. |
| 1907 | struct InstructionAndIdx { |
| 1908 | /// The instruction using the instruction. |
| 1909 | Instruction *Inst; |
| 1910 | /// The index where this instruction is used for Inst. |
| 1911 | unsigned Idx; |
| 1912 | InstructionAndIdx(Instruction *Inst, unsigned Idx) |
| 1913 | : Inst(Inst), Idx(Idx) {} |
| 1914 | }; |
| 1915 | |
| 1916 | /// Keep track of the original uses (pair Instruction, Index). |
| 1917 | SmallVector<InstructionAndIdx, 4> OriginalUses; |
| 1918 | typedef SmallVectorImpl<InstructionAndIdx>::iterator use_iterator; |
| 1919 | |
| 1920 | public: |
| 1921 | /// \brief Replace all the use of \p Inst by \p New. |
| 1922 | UsesReplacer(Instruction *Inst, Value *New) : TypePromotionAction(Inst) { |
| 1923 | DEBUG(dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *New |
| 1924 | << "\n"); |
| 1925 | // Record the original uses. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1926 | for (Use &U : Inst->uses()) { |
| 1927 | Instruction *UserI = cast<Instruction>(U.getUser()); |
| 1928 | OriginalUses.push_back(InstructionAndIdx(UserI, U.getOperandNo())); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1929 | } |
| 1930 | // Now, we can replace the uses. |
| 1931 | Inst->replaceAllUsesWith(New); |
| 1932 | } |
| 1933 | |
| 1934 | /// \brief Reassign the original uses of Inst to Inst. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1935 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1936 | DEBUG(dbgs() << "Undo: UsersReplacer: " << *Inst << "\n"); |
| 1937 | for (use_iterator UseIt = OriginalUses.begin(), |
| 1938 | EndIt = OriginalUses.end(); |
| 1939 | UseIt != EndIt; ++UseIt) { |
| 1940 | UseIt->Inst->setOperand(UseIt->Idx, Inst); |
| 1941 | } |
| 1942 | } |
| 1943 | }; |
| 1944 | |
| 1945 | /// \brief Remove an instruction from the IR. |
| 1946 | class InstructionRemover : public TypePromotionAction { |
| 1947 | /// Original position of the instruction. |
| 1948 | InsertionHandler Inserter; |
| 1949 | /// Helper structure to hide all the link to the instruction. In other |
| 1950 | /// words, this helps to do as if the instruction was removed. |
| 1951 | OperandsHider Hider; |
| 1952 | /// Keep track of the uses replaced, if any. |
| 1953 | UsesReplacer *Replacer; |
| 1954 | |
| 1955 | public: |
| 1956 | /// \brief Remove all reference of \p Inst and optinally replace all its |
| 1957 | /// uses with New. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1958 | /// \pre If !Inst->use_empty(), then New != nullptr |
| 1959 | InstructionRemover(Instruction *Inst, Value *New = nullptr) |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1960 | : TypePromotionAction(Inst), Inserter(Inst), Hider(Inst), |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1961 | Replacer(nullptr) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1962 | if (New) |
| 1963 | Replacer = new UsesReplacer(Inst, New); |
| 1964 | DEBUG(dbgs() << "Do: InstructionRemover: " << *Inst << "\n"); |
| 1965 | Inst->removeFromParent(); |
| 1966 | } |
| 1967 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 1968 | ~InstructionRemover() override { delete Replacer; } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1969 | |
| 1970 | /// \brief Really remove the instruction. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1971 | void commit() override { delete Inst; } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1972 | |
| 1973 | /// \brief Resurrect the instruction and reassign it to the proper uses if |
| 1974 | /// new value was provided when build this action. |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1975 | void undo() override { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 1976 | DEBUG(dbgs() << "Undo: InstructionRemover: " << *Inst << "\n"); |
| 1977 | Inserter.insert(Inst); |
| 1978 | if (Replacer) |
| 1979 | Replacer->undo(); |
| 1980 | Hider.undo(); |
| 1981 | } |
| 1982 | }; |
| 1983 | |
| 1984 | public: |
| 1985 | /// Restoration point. |
| 1986 | /// The restoration point is a pointer to an action instead of an iterator |
| 1987 | /// because the iterator may be invalidated but not the pointer. |
| 1988 | typedef const TypePromotionAction *ConstRestorationPt; |
| 1989 | /// Advocate every changes made in that transaction. |
| 1990 | void commit(); |
| 1991 | /// Undo all the changes made after the given point. |
| 1992 | void rollback(ConstRestorationPt Point); |
| 1993 | /// Get the current restoration point. |
| 1994 | ConstRestorationPt getRestorationPoint() const; |
| 1995 | |
| 1996 | /// \name API for IR modification with state keeping to support rollback. |
| 1997 | /// @{ |
| 1998 | /// Same as Instruction::setOperand. |
| 1999 | void setOperand(Instruction *Inst, unsigned Idx, Value *NewVal); |
| 2000 | /// Same as Instruction::eraseFromParent. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2001 | void eraseInstruction(Instruction *Inst, Value *NewVal = nullptr); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2002 | /// Same as Value::replaceAllUsesWith. |
| 2003 | void replaceAllUsesWith(Instruction *Inst, Value *New); |
| 2004 | /// Same as Value::mutateType. |
| 2005 | void mutateType(Instruction *Inst, Type *NewTy); |
| 2006 | /// Same as IRBuilder::createTrunc. |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2007 | Value *createTrunc(Instruction *Opnd, Type *Ty); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2008 | /// Same as IRBuilder::createSExt. |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2009 | Value *createSExt(Instruction *Inst, Value *Opnd, Type *Ty); |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2010 | /// Same as IRBuilder::createZExt. |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2011 | Value *createZExt(Instruction *Inst, Value *Opnd, Type *Ty); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2012 | /// Same as Instruction::moveBefore. |
| 2013 | void moveBefore(Instruction *Inst, Instruction *Before); |
| 2014 | /// @} |
| 2015 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2016 | private: |
| 2017 | /// The ordered list of actions made so far. |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2018 | SmallVector<std::unique_ptr<TypePromotionAction>, 16> Actions; |
| 2019 | typedef SmallVectorImpl<std::unique_ptr<TypePromotionAction>>::iterator CommitPt; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2020 | }; |
| 2021 | |
| 2022 | void TypePromotionTransaction::setOperand(Instruction *Inst, unsigned Idx, |
| 2023 | Value *NewVal) { |
| 2024 | Actions.push_back( |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2025 | make_unique<TypePromotionTransaction::OperandSetter>(Inst, Idx, NewVal)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | void TypePromotionTransaction::eraseInstruction(Instruction *Inst, |
| 2029 | Value *NewVal) { |
| 2030 | Actions.push_back( |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2031 | make_unique<TypePromotionTransaction::InstructionRemover>(Inst, NewVal)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | void TypePromotionTransaction::replaceAllUsesWith(Instruction *Inst, |
| 2035 | Value *New) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2036 | Actions.push_back(make_unique<TypePromotionTransaction::UsesReplacer>(Inst, New)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | void TypePromotionTransaction::mutateType(Instruction *Inst, Type *NewTy) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2040 | Actions.push_back(make_unique<TypePromotionTransaction::TypeMutator>(Inst, NewTy)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2043 | Value *TypePromotionTransaction::createTrunc(Instruction *Opnd, |
| 2044 | Type *Ty) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2045 | std::unique_ptr<TruncBuilder> Ptr(new TruncBuilder(Opnd, Ty)); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2046 | Value *Val = Ptr->getBuiltValue(); |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2047 | Actions.push_back(std::move(Ptr)); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2048 | return Val; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2051 | Value *TypePromotionTransaction::createSExt(Instruction *Inst, |
| 2052 | Value *Opnd, Type *Ty) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2053 | std::unique_ptr<SExtBuilder> Ptr(new SExtBuilder(Inst, Opnd, Ty)); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2054 | Value *Val = Ptr->getBuiltValue(); |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2055 | Actions.push_back(std::move(Ptr)); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2056 | return Val; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2059 | Value *TypePromotionTransaction::createZExt(Instruction *Inst, |
| 2060 | Value *Opnd, Type *Ty) { |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2061 | std::unique_ptr<ZExtBuilder> Ptr(new ZExtBuilder(Inst, Opnd, Ty)); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2062 | Value *Val = Ptr->getBuiltValue(); |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2063 | Actions.push_back(std::move(Ptr)); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2064 | return Val; |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2067 | void TypePromotionTransaction::moveBefore(Instruction *Inst, |
| 2068 | Instruction *Before) { |
| 2069 | Actions.push_back( |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2070 | make_unique<TypePromotionTransaction::InstructionMoveBefore>(Inst, Before)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | TypePromotionTransaction::ConstRestorationPt |
| 2074 | TypePromotionTransaction::getRestorationPoint() const { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2075 | return !Actions.empty() ? Actions.back().get() : nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | void TypePromotionTransaction::commit() { |
| 2079 | for (CommitPt It = Actions.begin(), EndIt = Actions.end(); It != EndIt; |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2080 | ++It) |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2081 | (*It)->commit(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2082 | Actions.clear(); |
| 2083 | } |
| 2084 | |
| 2085 | void TypePromotionTransaction::rollback( |
| 2086 | TypePromotionTransaction::ConstRestorationPt Point) { |
David Blaikie | 7620b31 | 2014-04-15 06:17:44 +0000 | [diff] [blame] | 2087 | while (!Actions.empty() && Point != Actions.back().get()) { |
| 2088 | std::unique_ptr<TypePromotionAction> Curr = Actions.pop_back_val(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2089 | Curr->undo(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2093 | /// \brief A helper class for matching addressing modes. |
| 2094 | /// |
| 2095 | /// This encapsulates the logic for matching the target-legal addressing modes. |
| 2096 | class AddressingModeMatcher { |
| 2097 | SmallVectorImpl<Instruction*> &AddrModeInsts; |
Eric Christopher | d75c00c | 2015-02-26 22:38:34 +0000 | [diff] [blame] | 2098 | const TargetMachine &TM; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2099 | const TargetLowering &TLI; |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 2100 | const DataLayout &DL; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2101 | |
| 2102 | /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and |
| 2103 | /// the memory instruction that we're computing this address for. |
| 2104 | Type *AccessTy; |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 2105 | unsigned AddrSpace; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2106 | Instruction *MemoryInst; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2107 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2108 | /// This is the addressing mode that we're building up. This is |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2109 | /// part of the return value of this addressing mode matching stuff. |
| 2110 | ExtAddrMode &AddrMode; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2111 | |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2112 | /// The instructions inserted by other CodeGenPrepare optimizations. |
| 2113 | const SetOfInstrs &InsertedInsts; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2114 | /// A map from the instructions to their type before promotion. |
| 2115 | InstrToOrigTy &PromotedInsts; |
| 2116 | /// The ongoing transaction where every action should be registered. |
| 2117 | TypePromotionTransaction &TPT; |
| 2118 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2119 | /// This is set to true when we should not do profitability checks. |
| 2120 | /// When true, IsProfitableToFoldIntoAddressingMode always returns true. |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2121 | bool IgnoreProfitability; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2122 | |
Eric Christopher | d75c00c | 2015-02-26 22:38:34 +0000 | [diff] [blame] | 2123 | AddressingModeMatcher(SmallVectorImpl<Instruction *> &AMI, |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 2124 | const TargetMachine &TM, Type *AT, unsigned AS, |
| 2125 | Instruction *MI, ExtAddrMode &AM, |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2126 | const SetOfInstrs &InsertedInsts, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2127 | InstrToOrigTy &PromotedInsts, |
| 2128 | TypePromotionTransaction &TPT) |
Eric Christopher | d75c00c | 2015-02-26 22:38:34 +0000 | [diff] [blame] | 2129 | : AddrModeInsts(AMI), TM(TM), |
| 2130 | TLI(*TM.getSubtargetImpl(*MI->getParent()->getParent()) |
| 2131 | ->getTargetLowering()), |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 2132 | DL(MI->getModule()->getDataLayout()), AccessTy(AT), AddrSpace(AS), |
| 2133 | MemoryInst(MI), AddrMode(AM), InsertedInsts(InsertedInsts), |
| 2134 | PromotedInsts(PromotedInsts), TPT(TPT) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2135 | IgnoreProfitability = false; |
| 2136 | } |
| 2137 | public: |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2138 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2139 | /// Find the maximal addressing mode that a load/store of V can fold, |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2140 | /// give an access type of AccessTy. This returns a list of involved |
| 2141 | /// instructions in AddrModeInsts. |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2142 | /// \p InsertedInsts The instructions inserted by other CodeGenPrepare |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2143 | /// optimizations. |
| 2144 | /// \p PromotedInsts maps the instructions to their type before promotion. |
| 2145 | /// \p The ongoing transaction where every action should be registered. |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 2146 | static ExtAddrMode Match(Value *V, Type *AccessTy, unsigned AS, |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2147 | Instruction *MemoryInst, |
| 2148 | SmallVectorImpl<Instruction*> &AddrModeInsts, |
Eric Christopher | d75c00c | 2015-02-26 22:38:34 +0000 | [diff] [blame] | 2149 | const TargetMachine &TM, |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2150 | const SetOfInstrs &InsertedInsts, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2151 | InstrToOrigTy &PromotedInsts, |
| 2152 | TypePromotionTransaction &TPT) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2153 | ExtAddrMode Result; |
| 2154 | |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 2155 | bool Success = AddressingModeMatcher(AddrModeInsts, TM, AccessTy, AS, |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2156 | MemoryInst, Result, InsertedInsts, |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2157 | PromotedInsts, TPT).matchAddr(V, 0); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2158 | (void)Success; assert(Success && "Couldn't select *anything*?"); |
| 2159 | return Result; |
| 2160 | } |
| 2161 | private: |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2162 | bool matchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth); |
| 2163 | bool matchAddr(Value *V, unsigned Depth); |
| 2164 | bool matchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2165 | bool *MovedAway = nullptr); |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2166 | bool isProfitableToFoldIntoAddressingMode(Instruction *I, |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2167 | ExtAddrMode &AMBefore, |
| 2168 | ExtAddrMode &AMAfter); |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2169 | bool valueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2); |
| 2170 | bool isPromotionProfitable(unsigned NewCost, unsigned OldCost, |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2171 | Value *PromotedOperand) const; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2172 | }; |
| 2173 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2174 | /// Try adding ScaleReg*Scale to the current addressing mode. |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2175 | /// Return true and update AddrMode if this addr mode is legal for the target, |
| 2176 | /// false if not. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2177 | bool AddressingModeMatcher::matchScaledValue(Value *ScaleReg, int64_t Scale, |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2178 | unsigned Depth) { |
| 2179 | // If Scale is 1, then this is the same as adding ScaleReg to the addressing |
| 2180 | // mode. Just process that directly. |
| 2181 | if (Scale == 1) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2182 | return matchAddr(ScaleReg, Depth); |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2183 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2184 | // If the scale is 0, it takes nothing to add this. |
| 2185 | if (Scale == 0) |
| 2186 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2187 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2188 | // If we already have a scale of this value, we can add to it, otherwise, we |
| 2189 | // need an available scale field. |
| 2190 | if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg) |
| 2191 | return false; |
| 2192 | |
| 2193 | ExtAddrMode TestAddrMode = AddrMode; |
| 2194 | |
| 2195 | // Add scale to turn X*4+X*3 -> X*7. This could also do things like |
| 2196 | // [A+B + A*7] -> [B+A*8]. |
| 2197 | TestAddrMode.Scale += Scale; |
| 2198 | TestAddrMode.ScaledReg = ScaleReg; |
| 2199 | |
| 2200 | // If the new address isn't legal, bail out. |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 2201 | if (!TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2202 | return false; |
| 2203 | |
| 2204 | // It was legal, so commit it. |
| 2205 | AddrMode = TestAddrMode; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2206 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2207 | // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now |
| 2208 | // to see if ScaleReg is actually X+C. If so, we can turn this into adding |
| 2209 | // X*Scale + C*Scale to addr mode. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2210 | ConstantInt *CI = nullptr; Value *AddLHS = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2211 | if (isa<Instruction>(ScaleReg) && // not a constant expr. |
| 2212 | match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) { |
| 2213 | TestAddrMode.ScaledReg = AddLHS; |
| 2214 | TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2215 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2216 | // If this addressing mode is legal, commit it and remember that we folded |
| 2217 | // this instruction. |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 2218 | if (TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace)) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2219 | AddrModeInsts.push_back(cast<Instruction>(ScaleReg)); |
| 2220 | AddrMode = TestAddrMode; |
| 2221 | return true; |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | // Otherwise, not (x+c)*scale, just return what we have. |
| 2226 | return true; |
| 2227 | } |
| 2228 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2229 | /// This is a little filter, which returns true if an addressing computation |
| 2230 | /// involving I might be folded into a load/store accessing it. |
| 2231 | /// This doesn't need to be perfect, but needs to accept at least |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2232 | /// the set of instructions that MatchOperationAddr can. |
| 2233 | static bool MightBeFoldableInst(Instruction *I) { |
| 2234 | switch (I->getOpcode()) { |
| 2235 | case Instruction::BitCast: |
Eli Bendersky | f13a056 | 2014-05-22 00:02:52 +0000 | [diff] [blame] | 2236 | case Instruction::AddrSpaceCast: |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2237 | // Don't touch identity bitcasts. |
| 2238 | if (I->getType() == I->getOperand(0)->getType()) |
| 2239 | return false; |
| 2240 | return I->getType()->isPointerTy() || I->getType()->isIntegerTy(); |
| 2241 | case Instruction::PtrToInt: |
| 2242 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
| 2243 | return true; |
| 2244 | case Instruction::IntToPtr: |
| 2245 | // We know the input is intptr_t, so this is foldable. |
| 2246 | return true; |
| 2247 | case Instruction::Add: |
| 2248 | return true; |
| 2249 | case Instruction::Mul: |
| 2250 | case Instruction::Shl: |
| 2251 | // Can only handle X*C and X << C. |
| 2252 | return isa<ConstantInt>(I->getOperand(1)); |
| 2253 | case Instruction::GetElementPtr: |
| 2254 | return true; |
| 2255 | default: |
| 2256 | return false; |
| 2257 | } |
| 2258 | } |
| 2259 | |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2260 | /// \brief Check whether or not \p Val is a legal instruction for \p TLI. |
| 2261 | /// \note \p Val is assumed to be the product of some type promotion. |
| 2262 | /// Therefore if \p Val has an undefined state in \p TLI, this is assumed |
| 2263 | /// to be legal, as the non-promoted value would have had the same state. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2264 | static bool isPromotedInstructionLegal(const TargetLowering &TLI, |
| 2265 | const DataLayout &DL, Value *Val) { |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2266 | Instruction *PromotedInst = dyn_cast<Instruction>(Val); |
| 2267 | if (!PromotedInst) |
| 2268 | return false; |
| 2269 | int ISDOpcode = TLI.InstructionOpcodeToISD(PromotedInst->getOpcode()); |
| 2270 | // If the ISDOpcode is undefined, it was undefined before the promotion. |
| 2271 | if (!ISDOpcode) |
| 2272 | return true; |
| 2273 | // Otherwise, check if the promoted instruction is legal or not. |
| 2274 | return TLI.isOperationLegalOrCustom( |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2275 | ISDOpcode, TLI.getValueType(DL, PromotedInst->getType())); |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2278 | /// \brief Hepler class to perform type promotion. |
| 2279 | class TypePromotionHelper { |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2280 | /// \brief Utility function to check whether or not a sign or zero extension |
| 2281 | /// of \p Inst with \p ConsideredExtType can be moved through \p Inst by |
| 2282 | /// either using the operands of \p Inst or promoting \p Inst. |
| 2283 | /// The type of the extension is defined by \p IsSExt. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2284 | /// In other words, check if: |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2285 | /// ext (Ty Inst opnd1 opnd2 ... opndN) to ConsideredExtType. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2286 | /// #1 Promotion applies: |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2287 | /// ConsideredExtType Inst (ext opnd1 to ConsideredExtType, ...). |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2288 | /// #2 Operand reuses: |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2289 | /// ext opnd1 to ConsideredExtType. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2290 | /// \p PromotedInsts maps the instructions to their type before promotion. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2291 | static bool canGetThrough(const Instruction *Inst, Type *ConsideredExtType, |
| 2292 | const InstrToOrigTy &PromotedInsts, bool IsSExt); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2293 | |
| 2294 | /// \brief Utility function to determine if \p OpIdx should be promoted when |
| 2295 | /// promoting \p Inst. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2296 | static bool shouldExtOperand(const Instruction *Inst, int OpIdx) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2297 | if (isa<SelectInst>(Inst) && OpIdx == 0) |
| 2298 | return false; |
| 2299 | return true; |
| 2300 | } |
| 2301 | |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2302 | /// \brief Utility function to promote the operand of \p Ext when this |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2303 | /// operand is a promotable trunc or sext or zext. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2304 | /// \p PromotedInsts maps the instructions to their type before promotion. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2305 | /// \p CreatedInstsCost[out] contains the cost of all instructions |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2306 | /// created to promote the operand of Ext. |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2307 | /// Newly added extensions are inserted in \p Exts. |
| 2308 | /// Newly added truncates are inserted in \p Truncs. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2309 | /// Should never be called directly. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2310 | /// \return The promoted value which is used instead of Ext. |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2311 | static Value *promoteOperandForTruncAndAnyExt( |
| 2312 | Instruction *Ext, TypePromotionTransaction &TPT, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2313 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2314 | SmallVectorImpl<Instruction *> *Exts, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2315 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2316 | |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2317 | /// \brief Utility function to promote the operand of \p Ext when this |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2318 | /// operand is promotable and is not a supported trunc or sext. |
| 2319 | /// \p PromotedInsts maps the instructions to their type before promotion. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2320 | /// \p CreatedInstsCost[out] contains the cost of all the instructions |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2321 | /// created to promote the operand of Ext. |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2322 | /// Newly added extensions are inserted in \p Exts. |
| 2323 | /// Newly added truncates are inserted in \p Truncs. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2324 | /// Should never be called directly. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2325 | /// \return The promoted value which is used instead of Ext. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2326 | static Value *promoteOperandForOther(Instruction *Ext, |
| 2327 | TypePromotionTransaction &TPT, |
| 2328 | InstrToOrigTy &PromotedInsts, |
| 2329 | unsigned &CreatedInstsCost, |
| 2330 | SmallVectorImpl<Instruction *> *Exts, |
| 2331 | SmallVectorImpl<Instruction *> *Truncs, |
| 2332 | const TargetLowering &TLI, bool IsSExt); |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2333 | |
| 2334 | /// \see promoteOperandForOther. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2335 | static Value *signExtendOperandForOther( |
| 2336 | Instruction *Ext, TypePromotionTransaction &TPT, |
| 2337 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, |
| 2338 | SmallVectorImpl<Instruction *> *Exts, |
| 2339 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) { |
| 2340 | return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost, |
| 2341 | Exts, Truncs, TLI, true); |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /// \see promoteOperandForOther. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2345 | static Value *zeroExtendOperandForOther( |
| 2346 | Instruction *Ext, TypePromotionTransaction &TPT, |
| 2347 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, |
| 2348 | SmallVectorImpl<Instruction *> *Exts, |
| 2349 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) { |
| 2350 | return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost, |
| 2351 | Exts, Truncs, TLI, false); |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2352 | } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2353 | |
| 2354 | public: |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2355 | /// Type for the utility function that promotes the operand of Ext. |
| 2356 | typedef Value *(*Action)(Instruction *Ext, TypePromotionTransaction &TPT, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2357 | InstrToOrigTy &PromotedInsts, |
| 2358 | unsigned &CreatedInstsCost, |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2359 | SmallVectorImpl<Instruction *> *Exts, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2360 | SmallVectorImpl<Instruction *> *Truncs, |
| 2361 | const TargetLowering &TLI); |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2362 | /// \brief Given a sign/zero extend instruction \p Ext, return the approriate |
| 2363 | /// action to promote the operand of \p Ext instead of using Ext. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2364 | /// \return NULL if no promotable action is possible with the current |
| 2365 | /// sign extension. |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2366 | /// \p InsertedInsts keeps track of all the instructions inserted by the |
| 2367 | /// other CodeGenPrepare optimizations. This information is important |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2368 | /// because we do not want to promote these instructions as CodeGenPrepare |
| 2369 | /// will reinsert them later. Thus creating an infinite loop: create/remove. |
| 2370 | /// \p PromotedInsts maps the instructions to their type before promotion. |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2371 | static Action getAction(Instruction *Ext, const SetOfInstrs &InsertedInsts, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2372 | const TargetLowering &TLI, |
| 2373 | const InstrToOrigTy &PromotedInsts); |
| 2374 | }; |
| 2375 | |
| 2376 | bool TypePromotionHelper::canGetThrough(const Instruction *Inst, |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2377 | Type *ConsideredExtType, |
| 2378 | const InstrToOrigTy &PromotedInsts, |
| 2379 | bool IsSExt) { |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2380 | // The promotion helper does not know how to deal with vector types yet. |
| 2381 | // To be able to fix that, we would need to fix the places where we |
| 2382 | // statically extend, e.g., constants and such. |
| 2383 | if (Inst->getType()->isVectorTy()) |
| 2384 | return false; |
| 2385 | |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2386 | // We can always get through zext. |
| 2387 | if (isa<ZExtInst>(Inst)) |
| 2388 | return true; |
| 2389 | |
| 2390 | // sext(sext) is ok too. |
| 2391 | if (IsSExt && isa<SExtInst>(Inst)) |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2392 | return true; |
| 2393 | |
| 2394 | // We can get through binary operator, if it is legal. In other words, the |
| 2395 | // binary operator must have a nuw or nsw flag. |
| 2396 | const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst); |
| 2397 | if (BinOp && isa<OverflowingBinaryOperator>(BinOp) && |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2398 | ((!IsSExt && BinOp->hasNoUnsignedWrap()) || |
| 2399 | (IsSExt && BinOp->hasNoSignedWrap()))) |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2400 | return true; |
| 2401 | |
| 2402 | // Check if we can do the following simplification. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2403 | // ext(trunc(opnd)) --> ext(opnd) |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2404 | if (!isa<TruncInst>(Inst)) |
| 2405 | return false; |
| 2406 | |
| 2407 | Value *OpndVal = Inst->getOperand(0); |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2408 | // Check if we can use this operand in the extension. |
| 2409 | // If the type is larger than the result type of the extension, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2410 | // we cannot. |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2411 | if (!OpndVal->getType()->isIntegerTy() || |
| 2412 | OpndVal->getType()->getIntegerBitWidth() > |
| 2413 | ConsideredExtType->getIntegerBitWidth()) |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2414 | return false; |
| 2415 | |
| 2416 | // If the operand of the truncate is not an instruction, we will not have |
| 2417 | // any information on the dropped bits. |
| 2418 | // (Actually we could for constant but it is not worth the extra logic). |
| 2419 | Instruction *Opnd = dyn_cast<Instruction>(OpndVal); |
| 2420 | if (!Opnd) |
| 2421 | return false; |
| 2422 | |
| 2423 | // Check if the source of the type is narrow enough. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2424 | // I.e., check that trunc just drops extended bits of the same kind of |
| 2425 | // the extension. |
| 2426 | // #1 get the type of the operand and check the kind of the extended bits. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2427 | const Type *OpndType; |
| 2428 | InstrToOrigTy::const_iterator It = PromotedInsts.find(Opnd); |
Benjamin Kramer | 4cd5faa | 2015-07-31 17:00:39 +0000 | [diff] [blame] | 2429 | if (It != PromotedInsts.end() && It->second.getInt() == IsSExt) |
| 2430 | OpndType = It->second.getPointer(); |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2431 | else if ((IsSExt && isa<SExtInst>(Opnd)) || (!IsSExt && isa<ZExtInst>(Opnd))) |
| 2432 | OpndType = Opnd->getOperand(0)->getType(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2433 | else |
| 2434 | return false; |
| 2435 | |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2436 | // #2 check that the truncate just drop extended bits. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2437 | if (Inst->getType()->getIntegerBitWidth() >= OpndType->getIntegerBitWidth()) |
| 2438 | return true; |
| 2439 | |
| 2440 | return false; |
| 2441 | } |
| 2442 | |
| 2443 | TypePromotionHelper::Action TypePromotionHelper::getAction( |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2444 | Instruction *Ext, const SetOfInstrs &InsertedInsts, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2445 | const TargetLowering &TLI, const InstrToOrigTy &PromotedInsts) { |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2446 | assert((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) && |
| 2447 | "Unexpected instruction type"); |
| 2448 | Instruction *ExtOpnd = dyn_cast<Instruction>(Ext->getOperand(0)); |
| 2449 | Type *ExtTy = Ext->getType(); |
| 2450 | bool IsSExt = isa<SExtInst>(Ext); |
| 2451 | // If the operand of the extension is not an instruction, we cannot |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2452 | // get through. |
| 2453 | // If it, check we can get through. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2454 | if (!ExtOpnd || !canGetThrough(ExtOpnd, ExtTy, PromotedInsts, IsSExt)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2455 | return nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2456 | |
| 2457 | // Do not promote if the operand has been added by codegenprepare. |
| 2458 | // Otherwise, it means we are undoing an optimization that is likely to be |
| 2459 | // redone, thus causing potential infinite loop. |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2460 | if (isa<TruncInst>(ExtOpnd) && InsertedInsts.count(ExtOpnd)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2461 | return nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2462 | |
| 2463 | // SExt or Trunc instructions. |
| 2464 | // Return the related handler. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2465 | if (isa<SExtInst>(ExtOpnd) || isa<TruncInst>(ExtOpnd) || |
| 2466 | isa<ZExtInst>(ExtOpnd)) |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2467 | return promoteOperandForTruncAndAnyExt; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2468 | |
| 2469 | // Regular instruction. |
| 2470 | // Abort early if we will have to insert non-free instructions. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2471 | if (!ExtOpnd->hasOneUse() && !TLI.isTruncateFree(ExtTy, ExtOpnd->getType())) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2472 | return nullptr; |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2473 | return IsSExt ? signExtendOperandForOther : zeroExtendOperandForOther; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2476 | Value *TypePromotionHelper::promoteOperandForTruncAndAnyExt( |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2477 | llvm::Instruction *SExt, TypePromotionTransaction &TPT, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2478 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2479 | SmallVectorImpl<Instruction *> *Exts, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2480 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2481 | // By construction, the operand of SExt is an instruction. Otherwise we cannot |
| 2482 | // get through it and this method should not be called. |
| 2483 | Instruction *SExtOpnd = cast<Instruction>(SExt->getOperand(0)); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2484 | Value *ExtVal = SExt; |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2485 | bool HasMergedNonFreeExt = false; |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2486 | if (isa<ZExtInst>(SExtOpnd)) { |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2487 | // Replace s|zext(zext(opnd)) |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2488 | // => zext(opnd). |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2489 | HasMergedNonFreeExt = !TLI.isExtFree(SExtOpnd); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2490 | Value *ZExt = |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2491 | TPT.createZExt(SExt, SExtOpnd->getOperand(0), SExt->getType()); |
| 2492 | TPT.replaceAllUsesWith(SExt, ZExt); |
| 2493 | TPT.eraseInstruction(SExt); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2494 | ExtVal = ZExt; |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2495 | } else { |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2496 | // Replace z|sext(trunc(opnd)) or sext(sext(opnd)) |
| 2497 | // => z|sext(opnd). |
Quentin Colombet | b2c5c6d | 2014-09-11 21:22:14 +0000 | [diff] [blame] | 2498 | TPT.setOperand(SExt, 0, SExtOpnd->getOperand(0)); |
| 2499 | } |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2500 | CreatedInstsCost = 0; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2501 | |
| 2502 | // Remove dead code. |
| 2503 | if (SExtOpnd->use_empty()) |
| 2504 | TPT.eraseInstruction(SExtOpnd); |
| 2505 | |
Quentin Colombet | 9dcb724 | 2014-09-15 18:26:58 +0000 | [diff] [blame] | 2506 | // Check if the extension is still needed. |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2507 | Instruction *ExtInst = dyn_cast<Instruction>(ExtVal); |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2508 | if (!ExtInst || ExtInst->getType() != ExtInst->getOperand(0)->getType()) { |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2509 | if (ExtInst) { |
| 2510 | if (Exts) |
| 2511 | Exts->push_back(ExtInst); |
| 2512 | CreatedInstsCost = !TLI.isExtFree(ExtInst) && !HasMergedNonFreeExt; |
| 2513 | } |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2514 | return ExtVal; |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2515 | } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2516 | |
Quentin Colombet | 9dcb724 | 2014-09-15 18:26:58 +0000 | [diff] [blame] | 2517 | // At this point we have: ext ty opnd to ty. |
| 2518 | // Reassign the uses of ExtInst to the opnd and remove ExtInst. |
| 2519 | Value *NextVal = ExtInst->getOperand(0); |
| 2520 | TPT.eraseInstruction(ExtInst, NextVal); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2521 | return NextVal; |
| 2522 | } |
| 2523 | |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2524 | Value *TypePromotionHelper::promoteOperandForOther( |
| 2525 | Instruction *Ext, TypePromotionTransaction &TPT, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2526 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2527 | SmallVectorImpl<Instruction *> *Exts, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2528 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI, |
| 2529 | bool IsSExt) { |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2530 | // By construction, the operand of Ext is an instruction. Otherwise we cannot |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2531 | // get through it and this method should not be called. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2532 | Instruction *ExtOpnd = cast<Instruction>(Ext->getOperand(0)); |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2533 | CreatedInstsCost = 0; |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2534 | if (!ExtOpnd->hasOneUse()) { |
| 2535 | // ExtOpnd will be promoted. |
| 2536 | // All its uses, but Ext, will need to use a truncated value of the |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2537 | // promoted version. |
| 2538 | // Create the truncate now. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2539 | Value *Trunc = TPT.createTrunc(Ext, ExtOpnd->getType()); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2540 | if (Instruction *ITrunc = dyn_cast<Instruction>(Trunc)) { |
| 2541 | ITrunc->removeFromParent(); |
| 2542 | // Insert it just after the definition. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2543 | ITrunc->insertAfter(ExtOpnd); |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2544 | if (Truncs) |
| 2545 | Truncs->push_back(ITrunc); |
Quentin Colombet | ac55b15 | 2014-09-16 22:36:07 +0000 | [diff] [blame] | 2546 | } |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2547 | |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2548 | TPT.replaceAllUsesWith(ExtOpnd, Trunc); |
| 2549 | // Restore the operand of Ext (which has been replace by the previous call |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2550 | // to replaceAllUsesWith) to avoid creating a cycle trunc <-> sext. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2551 | TPT.setOperand(Ext, 0, ExtOpnd); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | // Get through the Instruction: |
| 2555 | // 1. Update its type. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2556 | // 2. Replace the uses of Ext by Inst. |
| 2557 | // 3. Extend each operand that needs to be extended. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2558 | |
| 2559 | // Remember the original type of the instruction before promotion. |
| 2560 | // This is useful to know that the high bits are sign extended bits. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2561 | PromotedInsts.insert(std::pair<Instruction *, TypeIsSExt>( |
| 2562 | ExtOpnd, TypeIsSExt(ExtOpnd->getType(), IsSExt))); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2563 | // Step #1. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2564 | TPT.mutateType(ExtOpnd, Ext->getType()); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2565 | // Step #2. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2566 | TPT.replaceAllUsesWith(Ext, ExtOpnd); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2567 | // Step #3. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2568 | Instruction *ExtForOpnd = Ext; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2569 | |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2570 | DEBUG(dbgs() << "Propagate Ext to operands\n"); |
| 2571 | for (int OpIdx = 0, EndOpIdx = ExtOpnd->getNumOperands(); OpIdx != EndOpIdx; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2572 | ++OpIdx) { |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2573 | DEBUG(dbgs() << "Operand:\n" << *(ExtOpnd->getOperand(OpIdx)) << '\n'); |
| 2574 | if (ExtOpnd->getOperand(OpIdx)->getType() == Ext->getType() || |
| 2575 | !shouldExtOperand(ExtOpnd, OpIdx)) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2576 | DEBUG(dbgs() << "No need to propagate\n"); |
| 2577 | continue; |
| 2578 | } |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2579 | // Check if we can statically extend the operand. |
| 2580 | Value *Opnd = ExtOpnd->getOperand(OpIdx); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2581 | if (const ConstantInt *Cst = dyn_cast<ConstantInt>(Opnd)) { |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2582 | DEBUG(dbgs() << "Statically extend\n"); |
| 2583 | unsigned BitWidth = Ext->getType()->getIntegerBitWidth(); |
| 2584 | APInt CstVal = IsSExt ? Cst->getValue().sext(BitWidth) |
| 2585 | : Cst->getValue().zext(BitWidth); |
| 2586 | TPT.setOperand(ExtOpnd, OpIdx, ConstantInt::get(Ext->getType(), CstVal)); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2587 | continue; |
| 2588 | } |
| 2589 | // UndefValue are typed, so we have to statically sign extend them. |
| 2590 | if (isa<UndefValue>(Opnd)) { |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2591 | DEBUG(dbgs() << "Statically extend\n"); |
| 2592 | TPT.setOperand(ExtOpnd, OpIdx, UndefValue::get(Ext->getType())); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2593 | continue; |
| 2594 | } |
| 2595 | |
| 2596 | // Otherwise we have to explicity sign extend the operand. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2597 | // Check if Ext was reused to extend an operand. |
| 2598 | if (!ExtForOpnd) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2599 | // If yes, create a new one. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2600 | DEBUG(dbgs() << "More operands to ext\n"); |
Quentin Colombet | 84f89cc | 2014-12-22 18:11:52 +0000 | [diff] [blame] | 2601 | Value *ValForExtOpnd = IsSExt ? TPT.createSExt(Ext, Opnd, Ext->getType()) |
| 2602 | : TPT.createZExt(Ext, Opnd, Ext->getType()); |
| 2603 | if (!isa<Instruction>(ValForExtOpnd)) { |
| 2604 | TPT.setOperand(ExtOpnd, OpIdx, ValForExtOpnd); |
| 2605 | continue; |
| 2606 | } |
| 2607 | ExtForOpnd = cast<Instruction>(ValForExtOpnd); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2608 | } |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2609 | if (Exts) |
| 2610 | Exts->push_back(ExtForOpnd); |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2611 | TPT.setOperand(ExtForOpnd, 0, Opnd); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2612 | |
| 2613 | // Move the sign extension before the insertion point. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2614 | TPT.moveBefore(ExtForOpnd, ExtOpnd); |
| 2615 | TPT.setOperand(ExtOpnd, OpIdx, ExtForOpnd); |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2616 | CreatedInstsCost += !TLI.isExtFree(ExtForOpnd); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2617 | // If more sext are required, new instructions will have to be created. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2618 | ExtForOpnd = nullptr; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2619 | } |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2620 | if (ExtForOpnd == Ext) { |
| 2621 | DEBUG(dbgs() << "Extension is useless now\n"); |
| 2622 | TPT.eraseInstruction(Ext); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2623 | } |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2624 | return ExtOpnd; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2627 | /// Check whether or not promoting an instruction to a wider type is profitable. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2628 | /// \p NewCost gives the cost of extension instructions created by the |
| 2629 | /// promotion. |
| 2630 | /// \p OldCost gives the cost of extension instructions before the promotion |
| 2631 | /// plus the number of instructions that have been |
| 2632 | /// matched in the addressing mode the promotion. |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2633 | /// \p PromotedOperand is the value that has been promoted. |
| 2634 | /// \return True if the promotion is profitable, false otherwise. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2635 | bool AddressingModeMatcher::isPromotionProfitable( |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2636 | unsigned NewCost, unsigned OldCost, Value *PromotedOperand) const { |
| 2637 | DEBUG(dbgs() << "OldCost: " << OldCost << "\tNewCost: " << NewCost << '\n'); |
| 2638 | // The cost of the new extensions is greater than the cost of the |
| 2639 | // old extension plus what we folded. |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2640 | // This is not profitable. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2641 | if (NewCost > OldCost) |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2642 | return false; |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2643 | if (NewCost < OldCost) |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2644 | return true; |
| 2645 | // The promotion is neutral but it may help folding the sign extension in |
| 2646 | // loads for instance. |
| 2647 | // Check that we did not create an illegal instruction. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2648 | return isPromotedInstructionLegal(TLI, DL, PromotedOperand); |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2651 | /// Given an instruction or constant expr, see if we can fold the operation |
| 2652 | /// into the addressing mode. If so, update the addressing mode and return |
| 2653 | /// true, otherwise return false without modifying AddrMode. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2654 | /// If \p MovedAway is not NULL, it contains the information of whether or |
| 2655 | /// not AddrInst has to be folded into the addressing mode on success. |
| 2656 | /// If \p MovedAway == true, \p AddrInst will not be part of the addressing |
| 2657 | /// because it has been moved away. |
| 2658 | /// Thus AddrInst must not be added in the matched instructions. |
| 2659 | /// This state can happen when AddrInst is a sext, since it may be moved away. |
| 2660 | /// Therefore, AddrInst may not be valid when MovedAway is true and it must |
| 2661 | /// not be referenced anymore. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2662 | bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2663 | unsigned Depth, |
| 2664 | bool *MovedAway) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2665 | // Avoid exponential behavior on extremely deep expression trees. |
| 2666 | if (Depth >= 5) return false; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2667 | |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2668 | // By default, all matched instructions stay in place. |
| 2669 | if (MovedAway) |
| 2670 | *MovedAway = false; |
| 2671 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2672 | switch (Opcode) { |
| 2673 | case Instruction::PtrToInt: |
| 2674 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2675 | return matchAddr(AddrInst->getOperand(0), Depth); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2676 | case Instruction::IntToPtr: { |
| 2677 | auto AS = AddrInst->getType()->getPointerAddressSpace(); |
| 2678 | auto PtrTy = MVT::getIntegerVT(DL.getPointerSizeInBits(AS)); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2679 | // This inttoptr is a no-op if the integer type is pointer sized. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2680 | if (TLI.getValueType(DL, AddrInst->getOperand(0)->getType()) == PtrTy) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2681 | return matchAddr(AddrInst->getOperand(0), Depth); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2682 | return false; |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2683 | } |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2684 | case Instruction::BitCast: |
| 2685 | // BitCast is always a noop, and we can handle it as long as it is |
| 2686 | // int->int or pointer->pointer (we don't want int<->fp or something). |
| 2687 | if ((AddrInst->getOperand(0)->getType()->isPointerTy() || |
| 2688 | AddrInst->getOperand(0)->getType()->isIntegerTy()) && |
| 2689 | // Don't touch identity bitcasts. These were probably put here by LSR, |
| 2690 | // and we don't want to mess around with them. Assume it knows what it |
| 2691 | // is doing. |
| 2692 | AddrInst->getOperand(0)->getType() != AddrInst->getType()) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2693 | return matchAddr(AddrInst->getOperand(0), Depth); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2694 | return false; |
Matt Arsenault | f05b023 | 2015-05-26 16:59:43 +0000 | [diff] [blame] | 2695 | case Instruction::AddrSpaceCast: { |
| 2696 | unsigned SrcAS |
| 2697 | = AddrInst->getOperand(0)->getType()->getPointerAddressSpace(); |
| 2698 | unsigned DestAS = AddrInst->getType()->getPointerAddressSpace(); |
| 2699 | if (TLI.isNoopAddrSpaceCast(SrcAS, DestAS)) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2700 | return matchAddr(AddrInst->getOperand(0), Depth); |
Matt Arsenault | f05b023 | 2015-05-26 16:59:43 +0000 | [diff] [blame] | 2701 | return false; |
| 2702 | } |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2703 | case Instruction::Add: { |
| 2704 | // Check to see if we can merge in the RHS then the LHS. If so, we win. |
| 2705 | ExtAddrMode BackupAddrMode = AddrMode; |
| 2706 | unsigned OldSize = AddrModeInsts.size(); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2707 | // Start a transaction at this point. |
| 2708 | // The LHS may match but not the RHS. |
| 2709 | // Therefore, we need a higher level restoration point to undo partially |
| 2710 | // matched operation. |
| 2711 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 2712 | TPT.getRestorationPoint(); |
| 2713 | |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2714 | if (matchAddr(AddrInst->getOperand(1), Depth+1) && |
| 2715 | matchAddr(AddrInst->getOperand(0), Depth+1)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2716 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2717 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2718 | // Restore the old addr mode info. |
| 2719 | AddrMode = BackupAddrMode; |
| 2720 | AddrModeInsts.resize(OldSize); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2721 | TPT.rollback(LastKnownGood); |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2722 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2723 | // Otherwise this was over-aggressive. Try merging in the LHS then the RHS. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2724 | if (matchAddr(AddrInst->getOperand(0), Depth+1) && |
| 2725 | matchAddr(AddrInst->getOperand(1), Depth+1)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2726 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2727 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2728 | // Otherwise we definitely can't merge the ADD in. |
| 2729 | AddrMode = BackupAddrMode; |
| 2730 | AddrModeInsts.resize(OldSize); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2731 | TPT.rollback(LastKnownGood); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2732 | break; |
| 2733 | } |
| 2734 | //case Instruction::Or: |
| 2735 | // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD. |
| 2736 | //break; |
| 2737 | case Instruction::Mul: |
| 2738 | case Instruction::Shl: { |
| 2739 | // Can only handle X*C and X << C. |
| 2740 | ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1)); |
Sanjay Patel | d3bbfa1 | 2014-07-16 22:40:28 +0000 | [diff] [blame] | 2741 | if (!RHS) |
| 2742 | return false; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2743 | int64_t Scale = RHS->getSExtValue(); |
| 2744 | if (Opcode == Instruction::Shl) |
| 2745 | Scale = 1LL << Scale; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2746 | |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2747 | return matchScaledValue(AddrInst->getOperand(0), Scale, Depth); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2748 | } |
| 2749 | case Instruction::GetElementPtr: { |
| 2750 | // Scan the GEP. We check it if it contains constant offsets and at most |
| 2751 | // one variable offset. |
| 2752 | int VariableOperand = -1; |
| 2753 | unsigned VariableScale = 0; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2754 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2755 | int64_t ConstantOffset = 0; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2756 | gep_type_iterator GTI = gep_type_begin(AddrInst); |
| 2757 | for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) { |
| 2758 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 2759 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2760 | unsigned Idx = |
| 2761 | cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue(); |
| 2762 | ConstantOffset += SL->getElementOffset(Idx); |
| 2763 | } else { |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 2764 | uint64_t TypeSize = DL.getTypeAllocSize(GTI.getIndexedType()); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2765 | if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) { |
| 2766 | ConstantOffset += CI->getSExtValue()*TypeSize; |
| 2767 | } else if (TypeSize) { // Scales of zero don't do anything. |
| 2768 | // We only allow one variable index at the moment. |
| 2769 | if (VariableOperand != -1) |
| 2770 | return false; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2771 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2772 | // Remember the variable index. |
| 2773 | VariableOperand = i; |
| 2774 | VariableScale = TypeSize; |
| 2775 | } |
| 2776 | } |
| 2777 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2778 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2779 | // A common case is for the GEP to only do a constant offset. In this case, |
| 2780 | // just add it to the disp field and check validity. |
| 2781 | if (VariableOperand == -1) { |
| 2782 | AddrMode.BaseOffs += ConstantOffset; |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 2783 | if (ConstantOffset == 0 || |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 2784 | TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2785 | // Check to see if we can fold the base pointer in too. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2786 | if (matchAddr(AddrInst->getOperand(0), Depth+1)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2787 | return true; |
| 2788 | } |
| 2789 | AddrMode.BaseOffs -= ConstantOffset; |
| 2790 | return false; |
| 2791 | } |
| 2792 | |
| 2793 | // Save the valid addressing mode in case we can't match. |
| 2794 | ExtAddrMode BackupAddrMode = AddrMode; |
| 2795 | unsigned OldSize = AddrModeInsts.size(); |
| 2796 | |
| 2797 | // See if the scale and offset amount is valid for this target. |
| 2798 | AddrMode.BaseOffs += ConstantOffset; |
| 2799 | |
| 2800 | // Match the base operand of the GEP. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2801 | if (!matchAddr(AddrInst->getOperand(0), Depth+1)) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2802 | // If it couldn't be matched, just stuff the value in a register. |
| 2803 | if (AddrMode.HasBaseReg) { |
| 2804 | AddrMode = BackupAddrMode; |
| 2805 | AddrModeInsts.resize(OldSize); |
| 2806 | return false; |
| 2807 | } |
| 2808 | AddrMode.HasBaseReg = true; |
| 2809 | AddrMode.BaseReg = AddrInst->getOperand(0); |
| 2810 | } |
| 2811 | |
| 2812 | // Match the remaining variable portion of the GEP. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2813 | if (!matchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale, |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2814 | Depth)) { |
| 2815 | // If it couldn't be matched, try stuffing the base into a register |
| 2816 | // instead of matching it, and retrying the match of the scale. |
| 2817 | AddrMode = BackupAddrMode; |
| 2818 | AddrModeInsts.resize(OldSize); |
| 2819 | if (AddrMode.HasBaseReg) |
| 2820 | return false; |
| 2821 | AddrMode.HasBaseReg = true; |
| 2822 | AddrMode.BaseReg = AddrInst->getOperand(0); |
| 2823 | AddrMode.BaseOffs += ConstantOffset; |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2824 | if (!matchScaledValue(AddrInst->getOperand(VariableOperand), |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2825 | VariableScale, Depth)) { |
| 2826 | // If even that didn't work, bail. |
| 2827 | AddrMode = BackupAddrMode; |
| 2828 | AddrModeInsts.resize(OldSize); |
| 2829 | return false; |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | return true; |
| 2834 | } |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2835 | case Instruction::SExt: |
| 2836 | case Instruction::ZExt: { |
| 2837 | Instruction *Ext = dyn_cast<Instruction>(AddrInst); |
| 2838 | if (!Ext) |
Sanjay Patel | d3bbfa1 | 2014-07-16 22:40:28 +0000 | [diff] [blame] | 2839 | return false; |
Sanjay Patel | ab60d04 | 2014-07-16 21:08:10 +0000 | [diff] [blame] | 2840 | |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2841 | // Try to move this ext out of the way of the addressing mode. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2842 | // Ask for a method for doing so. |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2843 | TypePromotionHelper::Action TPH = |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 2844 | TypePromotionHelper::getAction(Ext, InsertedInsts, TLI, PromotedInsts); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2845 | if (!TPH) |
| 2846 | return false; |
| 2847 | |
| 2848 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 2849 | TPT.getRestorationPoint(); |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2850 | unsigned CreatedInstsCost = 0; |
| 2851 | unsigned ExtCost = !TLI.isExtFree(Ext); |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 2852 | Value *PromotedOperand = |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2853 | TPH(Ext, TPT, PromotedInsts, CreatedInstsCost, nullptr, nullptr, TLI); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2854 | // SExt has been moved away. |
| 2855 | // Thus either it will be rematched later in the recursive calls or it is |
| 2856 | // gone. Anyway, we must not fold it into the addressing mode at this point. |
| 2857 | // E.g., |
| 2858 | // op = add opnd, 1 |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2859 | // idx = ext op |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2860 | // addr = gep base, idx |
| 2861 | // is now: |
Quentin Colombet | f5485bb | 2014-11-13 01:44:51 +0000 | [diff] [blame] | 2862 | // promotedOpnd = ext opnd <- no match here |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2863 | // op = promoted_add promotedOpnd, 1 <- match (later in recursive calls) |
| 2864 | // addr = gep base, op <- match |
| 2865 | if (MovedAway) |
| 2866 | *MovedAway = true; |
| 2867 | |
| 2868 | assert(PromotedOperand && |
| 2869 | "TypePromotionHelper should have filtered out those cases"); |
| 2870 | |
| 2871 | ExtAddrMode BackupAddrMode = AddrMode; |
| 2872 | unsigned OldSize = AddrModeInsts.size(); |
| 2873 | |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2874 | if (!matchAddr(PromotedOperand, Depth) || |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2875 | // The total of the new cost is equals to the cost of the created |
| 2876 | // instructions. |
| 2877 | // The total of the old cost is equals to the cost of the extension plus |
| 2878 | // what we have saved in the addressing mode. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2879 | !isPromotionProfitable(CreatedInstsCost, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 2880 | ExtCost + (AddrModeInsts.size() - OldSize), |
Quentin Colombet | 867c550 | 2014-02-14 22:23:22 +0000 | [diff] [blame] | 2881 | PromotedOperand)) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2882 | AddrMode = BackupAddrMode; |
| 2883 | AddrModeInsts.resize(OldSize); |
| 2884 | DEBUG(dbgs() << "Sign extension does not pay off: rollback\n"); |
| 2885 | TPT.rollback(LastKnownGood); |
| 2886 | return false; |
| 2887 | } |
| 2888 | return true; |
| 2889 | } |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2890 | } |
| 2891 | return false; |
| 2892 | } |
| 2893 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2894 | /// If we can, try to add the value of 'Addr' into the current addressing mode. |
| 2895 | /// If Addr can't be added to AddrMode this returns false and leaves AddrMode |
| 2896 | /// unmodified. This assumes that Addr is either a pointer type or intptr_t |
| 2897 | /// for the target. |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2898 | /// |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2899 | bool AddressingModeMatcher::matchAddr(Value *Addr, unsigned Depth) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2900 | // Start a transaction at this point that we will rollback if the matching |
| 2901 | // fails. |
| 2902 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 2903 | TPT.getRestorationPoint(); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2904 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) { |
| 2905 | // Fold in immediates if legal for the target. |
| 2906 | AddrMode.BaseOffs += CI->getSExtValue(); |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 2907 | if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2908 | return true; |
| 2909 | AddrMode.BaseOffs -= CI->getSExtValue(); |
| 2910 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) { |
| 2911 | // 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] | 2912 | if (!AddrMode.BaseGV) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2913 | AddrMode.BaseGV = GV; |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 2914 | if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2915 | return true; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2916 | AddrMode.BaseGV = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2917 | } |
| 2918 | } else if (Instruction *I = dyn_cast<Instruction>(Addr)) { |
| 2919 | ExtAddrMode BackupAddrMode = AddrMode; |
| 2920 | unsigned OldSize = AddrModeInsts.size(); |
| 2921 | |
| 2922 | // Check to see if it is possible to fold this operation. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2923 | bool MovedAway = false; |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2924 | if (matchOperationAddr(I, I->getOpcode(), Depth, &MovedAway)) { |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2925 | // This instruction may have been move away. If so, there is nothing |
| 2926 | // to check here. |
| 2927 | if (MovedAway) |
| 2928 | return true; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2929 | // Okay, it's possible to fold this. Check to see if it is actually |
| 2930 | // *profitable* to do so. We use a simple cost model to avoid increasing |
| 2931 | // register pressure too much. |
| 2932 | if (I->hasOneUse() || |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2933 | isProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2934 | AddrModeInsts.push_back(I); |
| 2935 | return true; |
| 2936 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2937 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2938 | // It isn't profitable to do this, roll back. |
| 2939 | //cerr << "NOT FOLDING: " << *I; |
| 2940 | AddrMode = BackupAddrMode; |
| 2941 | AddrModeInsts.resize(OldSize); |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2942 | TPT.rollback(LastKnownGood); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2943 | } |
| 2944 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) { |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 2945 | if (matchOperationAddr(CE, CE->getOpcode(), Depth)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2946 | return true; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2947 | TPT.rollback(LastKnownGood); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2948 | } else if (isa<ConstantPointerNull>(Addr)) { |
| 2949 | // Null pointer gets folded without affecting the addressing mode. |
| 2950 | return true; |
| 2951 | } |
| 2952 | |
| 2953 | // Worse case, the target should support [reg] addressing modes. :) |
| 2954 | if (!AddrMode.HasBaseReg) { |
| 2955 | AddrMode.HasBaseReg = true; |
| 2956 | AddrMode.BaseReg = Addr; |
| 2957 | // Still check for legality in case the target supports [imm] but not [i+r]. |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 2958 | if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2959 | return true; |
| 2960 | AddrMode.HasBaseReg = false; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2961 | AddrMode.BaseReg = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | // If the base register is already taken, see if we can do [r+r]. |
| 2965 | if (AddrMode.Scale == 0) { |
| 2966 | AddrMode.Scale = 1; |
| 2967 | AddrMode.ScaledReg = Addr; |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 2968 | if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2969 | return true; |
| 2970 | AddrMode.Scale = 0; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 2971 | AddrMode.ScaledReg = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2972 | } |
| 2973 | // Couldn't match. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 2974 | TPT.rollback(LastKnownGood); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2975 | return false; |
| 2976 | } |
| 2977 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 2978 | /// Check to see if all uses of OpVal by the specified inline asm call are due |
| 2979 | /// to memory operands. If so, return true, otherwise return false. |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2980 | static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal, |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 2981 | const TargetMachine &TM) { |
| 2982 | const Function *F = CI->getParent()->getParent(); |
| 2983 | const TargetLowering *TLI = TM.getSubtargetImpl(*F)->getTargetLowering(); |
| 2984 | const TargetRegisterInfo *TRI = TM.getSubtargetImpl(*F)->getRegisterInfo(); |
Eric Christopher | d75c00c | 2015-02-26 22:38:34 +0000 | [diff] [blame] | 2985 | TargetLowering::AsmOperandInfoVector TargetConstraints = |
Mehdi Amini | 8ac7a9d | 2015-07-07 19:07:19 +0000 | [diff] [blame] | 2986 | TLI->ParseConstraints(F->getParent()->getDataLayout(), TRI, |
| 2987 | ImmutableCallSite(CI)); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2988 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 2989 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 2990 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2991 | // Compute the constraint code and ConstraintType to use. |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 2992 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 2993 | |
| 2994 | // If this asm operand is our Value*, and if it isn't an indirect memory |
| 2995 | // operand, we can't fold it! |
| 2996 | if (OpInfo.CallOperandVal == OpVal && |
| 2997 | (OpInfo.ConstraintType != TargetLowering::C_Memory || |
| 2998 | !OpInfo.isIndirect)) |
| 2999 | return false; |
| 3000 | } |
| 3001 | |
| 3002 | return true; |
| 3003 | } |
| 3004 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 3005 | /// Recursively walk all the uses of I until we find a memory use. |
| 3006 | /// If we find an obviously non-foldable instruction, return true. |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3007 | /// Add the ultimately found memory instructions to MemoryUses. |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 3008 | static bool FindAllMemoryUses( |
| 3009 | Instruction *I, |
| 3010 | SmallVectorImpl<std::pair<Instruction *, unsigned>> &MemoryUses, |
| 3011 | SmallPtrSetImpl<Instruction *> &ConsideredInsts, const TargetMachine &TM) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3012 | // If we already considered this instruction, we're done. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3013 | if (!ConsideredInsts.insert(I).second) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3014 | return false; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3015 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3016 | // If this is an obviously unfoldable instruction, bail out. |
| 3017 | if (!MightBeFoldableInst(I)) |
| 3018 | return true; |
| 3019 | |
| 3020 | // Loop over all the uses, recursively processing them. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3021 | for (Use &U : I->uses()) { |
| 3022 | Instruction *UserI = cast<Instruction>(U.getUser()); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3023 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3024 | if (LoadInst *LI = dyn_cast<LoadInst>(UserI)) { |
| 3025 | MemoryUses.push_back(std::make_pair(LI, U.getOperandNo())); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3026 | continue; |
| 3027 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3028 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3029 | if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) { |
| 3030 | unsigned opNo = U.getOperandNo(); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3031 | if (opNo == 0) return true; // Storing addr, not into addr. |
| 3032 | MemoryUses.push_back(std::make_pair(SI, opNo)); |
| 3033 | continue; |
| 3034 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3035 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3036 | if (CallInst *CI = dyn_cast<CallInst>(UserI)) { |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3037 | InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue()); |
| 3038 | if (!IA) return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3039 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3040 | // If this is a memory operand, we're cool, otherwise bail out. |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 3041 | if (!IsOperandAMemoryOperand(CI, IA, I, TM)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3042 | return true; |
| 3043 | continue; |
| 3044 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3045 | |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 3046 | if (FindAllMemoryUses(UserI, MemoryUses, ConsideredInsts, TM)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3047 | return true; |
| 3048 | } |
| 3049 | |
| 3050 | return false; |
| 3051 | } |
| 3052 | |
| 3053 | /// ValueAlreadyLiveAtInst - Retrn true if Val is already known to be live at |
| 3054 | /// the use site that we're folding it into. If so, there is no cost to |
| 3055 | /// include it in the addressing mode. KnownLive1 and KnownLive2 are two values |
| 3056 | /// that we know are live at the instruction already. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3057 | bool AddressingModeMatcher::valueAlreadyLiveAtInst(Value *Val,Value *KnownLive1, |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3058 | Value *KnownLive2) { |
| 3059 | // 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] | 3060 | if (Val == nullptr || Val == KnownLive1 || Val == KnownLive2) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3061 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3062 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3063 | // All values other than instructions and arguments (e.g. constants) are live. |
| 3064 | if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3065 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3066 | // If Val is a constant sized alloca in the entry block, it is live, this is |
| 3067 | // true because it is just a reference to the stack/frame pointer, which is |
| 3068 | // live for the whole function. |
| 3069 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Val)) |
| 3070 | if (AI->isStaticAlloca()) |
| 3071 | return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3072 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3073 | // Check to see if this value is already used in the memory instruction's |
| 3074 | // block. If so, it's already live into the block at the very least, so we |
| 3075 | // can reasonably fold it. |
| 3076 | return Val->isUsedInBasicBlock(MemoryInst->getParent()); |
| 3077 | } |
| 3078 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 3079 | /// It is possible for the addressing mode of the machine to fold the specified |
| 3080 | /// instruction into a load or store that ultimately uses it. |
| 3081 | /// However, the specified instruction has multiple uses. |
| 3082 | /// Given this, it may actually increase register pressure to fold it |
| 3083 | /// into the load. For example, consider this code: |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3084 | /// |
| 3085 | /// X = ... |
| 3086 | /// Y = X+1 |
| 3087 | /// use(Y) -> nonload/store |
| 3088 | /// Z = Y+1 |
| 3089 | /// load Z |
| 3090 | /// |
| 3091 | /// In this case, Y has multiple uses, and can be folded into the load of Z |
| 3092 | /// (yielding load [X+2]). However, doing this will cause both "X" and "X+1" to |
| 3093 | /// be live at the use(Y) line. If we don't fold Y into load Z, we use one |
| 3094 | /// fewer register. Since Y can't be folded into "use(Y)" we don't increase the |
| 3095 | /// number of computations either. |
| 3096 | /// |
| 3097 | /// Note that this (like most of CodeGenPrepare) is just a rough heuristic. If |
| 3098 | /// X was live across 'load Z' for other reasons, we actually *would* want to |
| 3099 | /// fold the addressing mode in the Z case. This would make Y die earlier. |
| 3100 | bool AddressingModeMatcher:: |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3101 | isProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore, |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3102 | ExtAddrMode &AMAfter) { |
| 3103 | if (IgnoreProfitability) return true; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3104 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3105 | // AMBefore is the addressing mode before this instruction was folded into it, |
| 3106 | // and AMAfter is the addressing mode after the instruction was folded. Get |
| 3107 | // the set of registers referenced by AMAfter and subtract out those |
| 3108 | // referenced by AMBefore: this is the set of values which folding in this |
| 3109 | // address extends the lifetime of. |
| 3110 | // |
| 3111 | // Note that there are only two potential values being referenced here, |
| 3112 | // BaseReg and ScaleReg (global addresses are always available, as are any |
| 3113 | // folded immediates). |
| 3114 | Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3115 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3116 | // If the BaseReg or ScaledReg was referenced by the previous addrmode, their |
| 3117 | // lifetime wasn't extended by adding this instruction. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3118 | if (valueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3119 | BaseReg = nullptr; |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3120 | if (valueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3121 | ScaledReg = nullptr; |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3122 | |
| 3123 | // If folding this instruction (and it's subexprs) didn't extend any live |
| 3124 | // ranges, we're ok with it. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3125 | if (!BaseReg && !ScaledReg) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3126 | return true; |
| 3127 | |
| 3128 | // If all uses of this instruction are ultimately load/store/inlineasm's, |
| 3129 | // check to see if their addressing modes will include this instruction. If |
| 3130 | // so, we can fold it into all uses, so it doesn't matter if it has multiple |
| 3131 | // uses. |
| 3132 | SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses; |
| 3133 | SmallPtrSet<Instruction*, 16> ConsideredInsts; |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 3134 | if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TM)) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3135 | return false; // Has a non-memory, non-foldable use! |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3136 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3137 | // Now that we know that all uses of this instruction are part of a chain of |
| 3138 | // computation involving only operations that could theoretically be folded |
| 3139 | // into a memory use, loop over each of these uses and see if they could |
| 3140 | // *actually* fold the instruction. |
| 3141 | SmallVector<Instruction*, 32> MatchedAddrModeInsts; |
| 3142 | for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) { |
| 3143 | Instruction *User = MemoryUses[i].first; |
| 3144 | unsigned OpNo = MemoryUses[i].second; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3145 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3146 | // Get the access type of this use. If the use isn't a pointer, we don't |
| 3147 | // know what it accesses. |
| 3148 | Value *Address = User->getOperand(OpNo); |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 3149 | PointerType *AddrTy = dyn_cast<PointerType>(Address->getType()); |
| 3150 | if (!AddrTy) |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3151 | return false; |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 3152 | Type *AddressAccessTy = AddrTy->getElementType(); |
| 3153 | unsigned AS = AddrTy->getAddressSpace(); |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3154 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3155 | // Do a match against the root of this address, ignoring profitability. This |
| 3156 | // will tell us if the addressing mode for the memory operation will |
| 3157 | // *actually* cover the shared instruction. |
| 3158 | ExtAddrMode Result; |
Quentin Colombet | 5a69dda | 2014-02-11 01:59:02 +0000 | [diff] [blame] | 3159 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 3160 | TPT.getRestorationPoint(); |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 3161 | AddressingModeMatcher Matcher(MatchedAddrModeInsts, TM, AddressAccessTy, AS, |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 3162 | MemoryInst, Result, InsertedInsts, |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 3163 | PromotedInsts, TPT); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3164 | Matcher.IgnoreProfitability = true; |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3165 | bool Success = Matcher.matchAddr(Address, 0); |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3166 | (void)Success; assert(Success && "Couldn't select *anything*?"); |
| 3167 | |
Quentin Colombet | 5a69dda | 2014-02-11 01:59:02 +0000 | [diff] [blame] | 3168 | // The match was to check the profitability, the changes made are not |
| 3169 | // part of the original matcher. Therefore, they should be dropped |
| 3170 | // otherwise the original matcher will not present the right state. |
| 3171 | TPT.rollback(LastKnownGood); |
| 3172 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3173 | // If the match didn't cover I, then it won't be shared by it. |
| 3174 | if (std::find(MatchedAddrModeInsts.begin(), MatchedAddrModeInsts.end(), |
| 3175 | I) == MatchedAddrModeInsts.end()) |
| 3176 | return false; |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3177 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3178 | MatchedAddrModeInsts.clear(); |
| 3179 | } |
Stephen Lin | 837bba1 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 3180 | |
Chandler Carruth | c892591 | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 3181 | return true; |
| 3182 | } |
| 3183 | |
| 3184 | } // end anonymous namespace |
| 3185 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 3186 | /// Return true if the specified values are defined in a |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3187 | /// different basic block than BB. |
| 3188 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 3189 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 3190 | return I->getParent() != BB; |
| 3191 | return false; |
| 3192 | } |
| 3193 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 3194 | /// Load and Store Instructions often have addressing modes that can do |
| 3195 | /// significant amounts of computation. As such, instruction selection will try |
| 3196 | /// to get the load or store to do as much computation as possible for the |
| 3197 | /// program. The problem is that isel can only see within a single block. As |
| 3198 | /// such, we sink as much legal addressing mode work into the block as possible. |
Chris Lattner | 728f902 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 3199 | /// |
| 3200 | /// This method is used to optimize both load/store and inline asms with memory |
| 3201 | /// operands. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3202 | bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr, |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 3203 | Type *AccessTy, unsigned AddrSpace) { |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3204 | Value *Repl = Addr; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3205 | |
| 3206 | // Try to collapse single-value PHI nodes. This is necessary to undo |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 3207 | // unprofitable PRE transformations. |
Cameron Zwarich | 43cecb1 | 2011-01-03 06:33:01 +0000 | [diff] [blame] | 3208 | SmallVector<Value*, 8> worklist; |
| 3209 | SmallPtrSet<Value*, 16> Visited; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3210 | worklist.push_back(Addr); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3211 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3212 | // Use a worklist to iteratively look through PHI nodes, and ensure that |
| 3213 | // the addressing mode obtained from the non-PHI roots of the graph |
| 3214 | // are equivalent. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3215 | Value *Consensus = nullptr; |
Cameron Zwarich | b7f8eaa | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 3216 | unsigned NumUsesConsensus = 0; |
Cameron Zwarich | 13c885d | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 3217 | bool IsNumUsesConsensusValid = false; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3218 | SmallVector<Instruction*, 16> AddrModeInsts; |
| 3219 | ExtAddrMode AddrMode; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 3220 | TypePromotionTransaction TPT; |
| 3221 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 3222 | TPT.getRestorationPoint(); |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3223 | while (!worklist.empty()) { |
| 3224 | Value *V = worklist.back(); |
| 3225 | worklist.pop_back(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3226 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3227 | // Break use-def graph loops. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3228 | if (!Visited.insert(V).second) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3229 | Consensus = nullptr; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3230 | break; |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 3231 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3232 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3233 | // For a PHI node, push all of its incoming values. |
| 3234 | if (PHINode *P = dyn_cast<PHINode>(V)) { |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 3235 | for (Value *IncValue : P->incoming_values()) |
| 3236 | worklist.push_back(IncValue); |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3237 | continue; |
| 3238 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3239 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3240 | // For non-PHIs, determine the addressing mode being computed. |
| 3241 | SmallVector<Instruction*, 16> NewAddrModeInsts; |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 3242 | ExtAddrMode NewAddrMode = AddressingModeMatcher::Match( |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 3243 | V, AccessTy, AddrSpace, MemoryInst, NewAddrModeInsts, *TM, |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 3244 | InsertedInsts, PromotedInsts, TPT); |
Cameron Zwarich | 13c885d | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 3245 | |
| 3246 | // This check is broken into two cases with very similar code to avoid using |
| 3247 | // getNumUses() as much as possible. Some values have a lot of uses, so |
| 3248 | // calling getNumUses() unconditionally caused a significant compile-time |
| 3249 | // regression. |
| 3250 | if (!Consensus) { |
| 3251 | Consensus = V; |
| 3252 | AddrMode = NewAddrMode; |
| 3253 | AddrModeInsts = NewAddrModeInsts; |
| 3254 | continue; |
| 3255 | } else if (NewAddrMode == AddrMode) { |
| 3256 | if (!IsNumUsesConsensusValid) { |
| 3257 | NumUsesConsensus = Consensus->getNumUses(); |
| 3258 | IsNumUsesConsensusValid = true; |
| 3259 | } |
| 3260 | |
| 3261 | // Ensure that the obtained addressing mode is equivalent to that obtained |
| 3262 | // for all other roots of the PHI traversal. Also, when choosing one |
| 3263 | // such root as representative, select the one with the most uses in order |
| 3264 | // to keep the cost modeling heuristics in AddressingModeMatcher |
| 3265 | // applicable. |
Cameron Zwarich | b7f8eaa | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 3266 | unsigned NumUses = V->getNumUses(); |
| 3267 | if (NumUses > NumUsesConsensus) { |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3268 | Consensus = V; |
Cameron Zwarich | b7f8eaa | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 3269 | NumUsesConsensus = NumUses; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3270 | AddrModeInsts = NewAddrModeInsts; |
| 3271 | } |
| 3272 | continue; |
| 3273 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3274 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3275 | Consensus = nullptr; |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3276 | break; |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 3277 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3278 | |
Owen Anderson | 8ba5f39 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 3279 | // If the addressing mode couldn't be determined, or if multiple different |
| 3280 | // ones were determined, bail out now. |
Quentin Colombet | 3a4bf04 | 2014-02-06 21:44:56 +0000 | [diff] [blame] | 3281 | if (!Consensus) { |
| 3282 | TPT.rollback(LastKnownGood); |
| 3283 | return false; |
| 3284 | } |
| 3285 | TPT.commit(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3286 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3287 | // Check to see if any of the instructions supersumed by this addr mode are |
| 3288 | // non-local to I's BB. |
| 3289 | bool AnyNonLocal = false; |
| 3290 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
Chris Lattner | 6d71b7f | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 3291 | if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) { |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3292 | AnyNonLocal = true; |
| 3293 | break; |
| 3294 | } |
| 3295 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3296 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3297 | // If all the instructions matched are already in this BB, don't do anything. |
| 3298 | if (!AnyNonLocal) { |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 3299 | DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3300 | return false; |
| 3301 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3302 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3303 | // Insert this computation right after this user. Since our caller is |
| 3304 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 3305 | // guaranteed to happen later. |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3306 | IRBuilder<> Builder(MemoryInst); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3307 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3308 | // Now that we determined the addressing expression we want to use and know |
| 3309 | // that we have to sink it into this block. Check to see if we have already |
| 3310 | // done this for some other load/store instr in this block. If so, reuse the |
| 3311 | // computation. |
| 3312 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 3313 | if (SunkAddr) { |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 3314 | DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " |
Louis Gerbarg | 1b91aa2 | 2014-05-13 21:54:22 +0000 | [diff] [blame] | 3315 | << *MemoryInst << "\n"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3316 | if (SunkAddr->getType() != Addr->getType()) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 3317 | SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType()); |
Eric Christopher | fccff37 | 2015-01-27 01:01:38 +0000 | [diff] [blame] | 3318 | } else if (AddrSinkUsingGEPs || |
| 3319 | (!AddrSinkUsingGEPs.getNumOccurrences() && TM && |
Eric Christopher | 2c63549 | 2015-01-27 07:54:39 +0000 | [diff] [blame] | 3320 | TM->getSubtargetImpl(*MemoryInst->getParent()->getParent()) |
| 3321 | ->useAA())) { |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 3322 | // By default, we use the GEP-based method when AA is used later. This |
| 3323 | // prevents new inttoptr/ptrtoint pairs from degrading AA capabilities. |
| 3324 | DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
Louis Gerbarg | 1b91aa2 | 2014-05-13 21:54:22 +0000 | [diff] [blame] | 3325 | << *MemoryInst << "\n"); |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 3326 | Type *IntPtrTy = DL->getIntPtrType(Addr->getType()); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3327 | Value *ResultPtr = nullptr, *ResultIndex = nullptr; |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 3328 | |
| 3329 | // First, find the pointer. |
| 3330 | if (AddrMode.BaseReg && AddrMode.BaseReg->getType()->isPointerTy()) { |
| 3331 | ResultPtr = AddrMode.BaseReg; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3332 | AddrMode.BaseReg = nullptr; |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | if (AddrMode.Scale && AddrMode.ScaledReg->getType()->isPointerTy()) { |
| 3336 | // We can't add more than one pointer together, nor can we scale a |
| 3337 | // pointer (both of which seem meaningless). |
| 3338 | if (ResultPtr || AddrMode.Scale != 1) |
| 3339 | return false; |
| 3340 | |
| 3341 | ResultPtr = AddrMode.ScaledReg; |
| 3342 | AddrMode.Scale = 0; |
| 3343 | } |
| 3344 | |
| 3345 | if (AddrMode.BaseGV) { |
| 3346 | if (ResultPtr) |
| 3347 | return false; |
| 3348 | |
| 3349 | ResultPtr = AddrMode.BaseGV; |
| 3350 | } |
| 3351 | |
| 3352 | // If the real base value actually came from an inttoptr, then the matcher |
| 3353 | // will look through it and provide only the integer value. In that case, |
| 3354 | // use it here. |
| 3355 | if (!ResultPtr && AddrMode.BaseReg) { |
| 3356 | ResultPtr = |
| 3357 | Builder.CreateIntToPtr(AddrMode.BaseReg, Addr->getType(), "sunkaddr"); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3358 | AddrMode.BaseReg = nullptr; |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 3359 | } else if (!ResultPtr && AddrMode.Scale == 1) { |
| 3360 | ResultPtr = |
| 3361 | Builder.CreateIntToPtr(AddrMode.ScaledReg, Addr->getType(), "sunkaddr"); |
| 3362 | AddrMode.Scale = 0; |
| 3363 | } |
| 3364 | |
| 3365 | if (!ResultPtr && |
| 3366 | !AddrMode.BaseReg && !AddrMode.Scale && !AddrMode.BaseOffs) { |
| 3367 | SunkAddr = Constant::getNullValue(Addr->getType()); |
| 3368 | } else if (!ResultPtr) { |
| 3369 | return false; |
| 3370 | } else { |
| 3371 | Type *I8PtrTy = |
David Blaikie | 3909da7 | 2015-03-30 20:42:56 +0000 | [diff] [blame] | 3372 | Builder.getInt8PtrTy(Addr->getType()->getPointerAddressSpace()); |
| 3373 | Type *I8Ty = Builder.getInt8Ty(); |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 3374 | |
| 3375 | // Start with the base register. Do this first so that subsequent address |
| 3376 | // matching finds it last, which will prevent it from trying to match it |
| 3377 | // as the scaled value in case it happens to be a mul. That would be |
| 3378 | // problematic if we've sunk a different mul for the scale, because then |
| 3379 | // we'd end up sinking both muls. |
| 3380 | if (AddrMode.BaseReg) { |
| 3381 | Value *V = AddrMode.BaseReg; |
| 3382 | if (V->getType() != IntPtrTy) |
| 3383 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); |
| 3384 | |
| 3385 | ResultIndex = V; |
| 3386 | } |
| 3387 | |
| 3388 | // Add the scale value. |
| 3389 | if (AddrMode.Scale) { |
| 3390 | Value *V = AddrMode.ScaledReg; |
| 3391 | if (V->getType() == IntPtrTy) { |
| 3392 | // done. |
| 3393 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 3394 | cast<IntegerType>(V->getType())->getBitWidth()) { |
| 3395 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); |
| 3396 | } else { |
| 3397 | // It is only safe to sign extend the BaseReg if we know that the math |
| 3398 | // required to create it did not overflow before we extend it. Since |
| 3399 | // the original IR value was tossed in favor of a constant back when |
| 3400 | // the AddrMode was created we need to bail out gracefully if widths |
| 3401 | // do not match instead of extending it. |
| 3402 | Instruction *I = dyn_cast_or_null<Instruction>(ResultIndex); |
| 3403 | if (I && (ResultIndex != AddrMode.BaseReg)) |
| 3404 | I->eraseFromParent(); |
| 3405 | return false; |
| 3406 | } |
| 3407 | |
| 3408 | if (AddrMode.Scale != 1) |
| 3409 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), |
| 3410 | "sunkaddr"); |
| 3411 | if (ResultIndex) |
| 3412 | ResultIndex = Builder.CreateAdd(ResultIndex, V, "sunkaddr"); |
| 3413 | else |
| 3414 | ResultIndex = V; |
| 3415 | } |
| 3416 | |
| 3417 | // Add in the Base Offset if present. |
| 3418 | if (AddrMode.BaseOffs) { |
| 3419 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
| 3420 | if (ResultIndex) { |
NAKAMURA Takumi | f51a34e | 2014-10-29 15:23:11 +0000 | [diff] [blame] | 3421 | // We need to add this separately from the scale above to help with |
| 3422 | // SDAG consecutive load/store merging. |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 3423 | if (ResultPtr->getType() != I8PtrTy) |
| 3424 | ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy); |
David Blaikie | 3909da7 | 2015-03-30 20:42:56 +0000 | [diff] [blame] | 3425 | ResultPtr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr"); |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | ResultIndex = V; |
| 3429 | } |
| 3430 | |
| 3431 | if (!ResultIndex) { |
| 3432 | SunkAddr = ResultPtr; |
| 3433 | } else { |
| 3434 | if (ResultPtr->getType() != I8PtrTy) |
| 3435 | ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy); |
David Blaikie | 3909da7 | 2015-03-30 20:42:56 +0000 | [diff] [blame] | 3436 | SunkAddr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr"); |
Hal Finkel | c399830 | 2014-04-12 00:59:48 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | if (SunkAddr->getType() != Addr->getType()) |
| 3440 | SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType()); |
| 3441 | } |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3442 | } else { |
David Greene | 74e2d49 | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 3443 | DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
Louis Gerbarg | 1b91aa2 | 2014-05-13 21:54:22 +0000 | [diff] [blame] | 3444 | << *MemoryInst << "\n"); |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 3445 | Type *IntPtrTy = DL->getIntPtrType(Addr->getType()); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3446 | Value *Result = nullptr; |
Dan Gohman | ca19445 | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 3447 | |
| 3448 | // Start with the base register. Do this first so that subsequent address |
| 3449 | // matching finds it last, which will prevent it from trying to match it |
| 3450 | // as the scaled value in case it happens to be a mul. That would be |
| 3451 | // problematic if we've sunk a different mul for the scale, because then |
| 3452 | // we'd end up sinking both muls. |
| 3453 | if (AddrMode.BaseReg) { |
| 3454 | Value *V = AddrMode.BaseReg; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3455 | if (V->getType()->isPointerTy()) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3456 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Dan Gohman | ca19445 | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 3457 | if (V->getType() != IntPtrTy) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3458 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); |
Dan Gohman | ca19445 | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 3459 | Result = V; |
| 3460 | } |
| 3461 | |
| 3462 | // Add the scale value. |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3463 | if (AddrMode.Scale) { |
| 3464 | Value *V = AddrMode.ScaledReg; |
| 3465 | if (V->getType() == IntPtrTy) { |
| 3466 | // done. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3467 | } else if (V->getType()->isPointerTy()) { |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3468 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3469 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 3470 | cast<IntegerType>(V->getType())->getBitWidth()) { |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3471 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3472 | } else { |
Jim Grosbach | ed2cd39 | 2014-03-26 17:27:01 +0000 | [diff] [blame] | 3473 | // It is only safe to sign extend the BaseReg if we know that the math |
| 3474 | // required to create it did not overflow before we extend it. Since |
| 3475 | // the original IR value was tossed in favor of a constant back when |
| 3476 | // the AddrMode was created we need to bail out gracefully if widths |
| 3477 | // do not match instead of extending it. |
Joey Gouly | 12a8bf0 | 2014-05-13 15:42:45 +0000 | [diff] [blame] | 3478 | Instruction *I = dyn_cast_or_null<Instruction>(Result); |
Jim Grosbach | 83b44e1 | 2014-04-10 00:27:45 +0000 | [diff] [blame] | 3479 | if (I && (Result != AddrMode.BaseReg)) |
| 3480 | I->eraseFromParent(); |
Jim Grosbach | ed2cd39 | 2014-03-26 17:27:01 +0000 | [diff] [blame] | 3481 | return false; |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3482 | } |
| 3483 | if (AddrMode.Scale != 1) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3484 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), |
| 3485 | "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3486 | if (Result) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3487 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3488 | else |
| 3489 | Result = V; |
| 3490 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3491 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3492 | // Add in the BaseGV if present. |
| 3493 | if (AddrMode.BaseGV) { |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3494 | Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3495 | if (Result) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3496 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3497 | else |
| 3498 | Result = V; |
| 3499 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3500 | |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3501 | // Add in the Base Offset if present. |
| 3502 | if (AddrMode.BaseOffs) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3503 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3504 | if (Result) |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3505 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3506 | else |
| 3507 | Result = V; |
| 3508 | } |
| 3509 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 3510 | if (!Result) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3511 | SunkAddr = Constant::getNullValue(Addr->getType()); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3512 | else |
Devang Patel | c10e52a | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 3513 | SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr"); |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3514 | } |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3515 | |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 3516 | MemoryInst->replaceUsesOfWith(Repl, SunkAddr); |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 3517 | |
Chris Lattner | af1bcce | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 3518 | // If we have no uses, recursively delete the value and all dead instructions |
| 3519 | // using it. |
Owen Anderson | dfb8c3b | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 3520 | if (Repl->use_empty()) { |
Chris Lattner | af1bcce | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 3521 | // This can cause recursive deletion, which can invalidate our iterator. |
| 3522 | // Use a WeakVH to hold onto it in case this happens. |
| 3523 | WeakVH IterHandle(CurInstIterator); |
| 3524 | BasicBlock *BB = CurInstIterator->getParent(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3525 | |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 3526 | RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo); |
Chris Lattner | af1bcce | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 3527 | |
| 3528 | if (IterHandle != CurInstIterator) { |
| 3529 | // If the iterator instruction was recursively deleted, start over at the |
| 3530 | // start of the block. |
| 3531 | CurInstIterator = BB->begin(); |
| 3532 | SunkAddrs.clear(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3533 | } |
Dale Johannesen | b67a6e66 | 2010-03-31 20:37:15 +0000 | [diff] [blame] | 3534 | } |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 3535 | ++NumMemoryInsts; |
Chris Lattner | feee64e | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 3536 | return true; |
| 3537 | } |
| 3538 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 3539 | /// If there are any memory operands, use OptimizeMemoryInst to sink their |
| 3540 | /// address computing into the block when possible / profitable. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3541 | bool CodeGenPrepare::optimizeInlineAsmInst(CallInst *CS) { |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3542 | bool MadeChange = false; |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3543 | |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 3544 | const TargetRegisterInfo *TRI = |
| 3545 | TM->getSubtargetImpl(*CS->getParent()->getParent())->getRegisterInfo(); |
Mehdi Amini | 8ac7a9d | 2015-07-07 19:07:19 +0000 | [diff] [blame] | 3546 | TargetLowering::AsmOperandInfoVector TargetConstraints = |
| 3547 | TLI->ParseConstraints(*DL, TRI, CS); |
Dale Johannesen | f95f59a | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 3548 | unsigned ArgNo = 0; |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 3549 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 3550 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 3551 | |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3552 | // Compute the constraint code and ConstraintType to use. |
Dale Johannesen | ce97d55 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 3553 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3554 | |
Eli Friedman | 666bbe3 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 3555 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 3556 | OpInfo.isIndirect) { |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 3557 | Value *OpVal = CS->getArgOperand(ArgNo++); |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3558 | MadeChange |= optimizeMemoryInst(CS, OpVal, OpVal->getType(), ~0u); |
Dale Johannesen | f95f59a | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 3559 | } else if (OpInfo.Type == InlineAsm::isInput) |
| 3560 | ArgNo++; |
Evan Cheng | 1da2500 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | return MadeChange; |
| 3564 | } |
| 3565 | |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3566 | /// \brief Check if all the uses of \p Inst are equivalent (or free) zero or |
| 3567 | /// sign extensions. |
| 3568 | static bool hasSameExtUse(Instruction *Inst, const TargetLowering &TLI) { |
| 3569 | assert(!Inst->use_empty() && "Input must have at least one use"); |
| 3570 | const Instruction *FirstUser = cast<Instruction>(*Inst->user_begin()); |
| 3571 | bool IsSExt = isa<SExtInst>(FirstUser); |
| 3572 | Type *ExtTy = FirstUser->getType(); |
| 3573 | for (const User *U : Inst->users()) { |
| 3574 | const Instruction *UI = cast<Instruction>(U); |
| 3575 | if ((IsSExt && !isa<SExtInst>(UI)) || (!IsSExt && !isa<ZExtInst>(UI))) |
| 3576 | return false; |
| 3577 | Type *CurTy = UI->getType(); |
| 3578 | // Same input and output types: Same instruction after CSE. |
| 3579 | if (CurTy == ExtTy) |
| 3580 | continue; |
| 3581 | |
| 3582 | // If IsSExt is true, we are in this situation: |
| 3583 | // a = Inst |
| 3584 | // b = sext ty1 a to ty2 |
| 3585 | // c = sext ty1 a to ty3 |
| 3586 | // Assuming ty2 is shorter than ty3, this could be turned into: |
| 3587 | // a = Inst |
| 3588 | // b = sext ty1 a to ty2 |
| 3589 | // c = sext ty2 b to ty3 |
| 3590 | // However, the last sext is not free. |
| 3591 | if (IsSExt) |
| 3592 | return false; |
| 3593 | |
| 3594 | // This is a ZExt, maybe this is free to extend from one type to another. |
| 3595 | // In that case, we would not account for a different use. |
| 3596 | Type *NarrowTy; |
| 3597 | Type *LargeTy; |
| 3598 | if (ExtTy->getScalarType()->getIntegerBitWidth() > |
| 3599 | CurTy->getScalarType()->getIntegerBitWidth()) { |
| 3600 | NarrowTy = CurTy; |
| 3601 | LargeTy = ExtTy; |
| 3602 | } else { |
| 3603 | NarrowTy = ExtTy; |
| 3604 | LargeTy = CurTy; |
| 3605 | } |
| 3606 | |
| 3607 | if (!TLI.isZExtFree(NarrowTy, LargeTy)) |
| 3608 | return false; |
| 3609 | } |
| 3610 | // All uses are the same or can be derived from one another for free. |
| 3611 | return true; |
| 3612 | } |
| 3613 | |
| 3614 | /// \brief Try to form ExtLd by promoting \p Exts until they reach a |
| 3615 | /// load instruction. |
| 3616 | /// If an ext(load) can be formed, it is returned via \p LI for the load |
| 3617 | /// and \p Inst for the extension. |
| 3618 | /// Otherwise LI == nullptr and Inst == nullptr. |
| 3619 | /// When some promotion happened, \p TPT contains the proper state to |
| 3620 | /// revert them. |
| 3621 | /// |
| 3622 | /// \return true when promoting was necessary to expose the ext(load) |
| 3623 | /// opportunity, false otherwise. |
| 3624 | /// |
| 3625 | /// Example: |
| 3626 | /// \code |
| 3627 | /// %ld = load i32* %addr |
| 3628 | /// %add = add nuw i32 %ld, 4 |
| 3629 | /// %zext = zext i32 %add to i64 |
| 3630 | /// \endcode |
| 3631 | /// => |
| 3632 | /// \code |
| 3633 | /// %ld = load i32* %addr |
| 3634 | /// %zext = zext i32 %ld to i64 |
| 3635 | /// %add = add nuw i64 %zext, 4 |
| 3636 | /// \encode |
| 3637 | /// Thanks to the promotion, we can match zext(load i32*) to i64. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3638 | bool CodeGenPrepare::extLdPromotion(TypePromotionTransaction &TPT, |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3639 | LoadInst *&LI, Instruction *&Inst, |
| 3640 | const SmallVectorImpl<Instruction *> &Exts, |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 3641 | unsigned CreatedInstsCost = 0) { |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3642 | // Iterate over all the extensions to see if one form an ext(load). |
| 3643 | for (auto I : Exts) { |
| 3644 | // Check if we directly have ext(load). |
| 3645 | if ((LI = dyn_cast<LoadInst>(I->getOperand(0)))) { |
| 3646 | Inst = I; |
| 3647 | // No promotion happened here. |
| 3648 | return false; |
| 3649 | } |
| 3650 | // Check whether or not we want to do any promotion. |
| 3651 | if (!TLI || !TLI->enableExtLdPromotion() || DisableExtLdPromotion) |
| 3652 | continue; |
| 3653 | // Get the action to perform the promotion. |
| 3654 | TypePromotionHelper::Action TPH = TypePromotionHelper::getAction( |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 3655 | I, InsertedInsts, *TLI, PromotedInsts); |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3656 | // Check if we can promote. |
| 3657 | if (!TPH) |
| 3658 | continue; |
| 3659 | // Save the current state. |
| 3660 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 3661 | TPT.getRestorationPoint(); |
| 3662 | SmallVector<Instruction *, 4> NewExts; |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 3663 | unsigned NewCreatedInstsCost = 0; |
| 3664 | unsigned ExtCost = !TLI->isExtFree(I); |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3665 | // Promote. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 3666 | Value *PromotedVal = TPH(I, TPT, PromotedInsts, NewCreatedInstsCost, |
| 3667 | &NewExts, nullptr, *TLI); |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3668 | assert(PromotedVal && |
| 3669 | "TypePromotionHelper should have filtered out those cases"); |
| 3670 | |
| 3671 | // We would be able to merge only one extension in a load. |
| 3672 | // Therefore, if we have more than 1 new extension we heuristically |
| 3673 | // cut this search path, because it means we degrade the code quality. |
| 3674 | // With exactly 2, the transformation is neutral, because we will merge |
| 3675 | // one extension but leave one. However, we optimistically keep going, |
| 3676 | // because the new extension may be removed too. |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 3677 | long long TotalCreatedInstsCost = CreatedInstsCost + NewCreatedInstsCost; |
| 3678 | TotalCreatedInstsCost -= ExtCost; |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3679 | if (!StressExtLdPromotion && |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 3680 | (TotalCreatedInstsCost > 1 || |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3681 | !isPromotedInstructionLegal(*TLI, *DL, PromotedVal))) { |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3682 | // The promotion is not profitable, rollback to the previous state. |
| 3683 | TPT.rollback(LastKnownGood); |
| 3684 | continue; |
| 3685 | } |
| 3686 | // The promotion is profitable. |
| 3687 | // Check if it exposes an ext(load). |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3688 | (void)extLdPromotion(TPT, LI, Inst, NewExts, TotalCreatedInstsCost); |
Quentin Colombet | 1b274f9 | 2015-03-10 21:48:15 +0000 | [diff] [blame] | 3689 | if (LI && (StressExtLdPromotion || NewCreatedInstsCost <= ExtCost || |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3690 | // If we have created a new extension, i.e., now we have two |
| 3691 | // extensions. We must make sure one of them is merged with |
| 3692 | // the load, otherwise we may degrade the code quality. |
| 3693 | (LI->hasOneUse() || hasSameExtUse(LI, *TLI)))) |
| 3694 | // Promotion happened. |
| 3695 | return true; |
| 3696 | // If this does not help to expose an ext(load) then, rollback. |
| 3697 | TPT.rollback(LastKnownGood); |
| 3698 | } |
| 3699 | // None of the extension can form an ext(load). |
| 3700 | LI = nullptr; |
| 3701 | Inst = nullptr; |
| 3702 | return false; |
| 3703 | } |
| 3704 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 3705 | /// Move a zext or sext fed by a load into the same basic block as the load, |
| 3706 | /// unless conditions are unfavorable. This allows SelectionDAG to fold the |
| 3707 | /// extend into the load. |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3708 | /// \p I[in/out] the extension may be modified during the process if some |
| 3709 | /// promotions apply. |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3710 | /// |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3711 | bool CodeGenPrepare::moveExtToFormExtLoad(Instruction *&I) { |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3712 | // Try to promote a chain of computation if it allows to form |
| 3713 | // an extended load. |
| 3714 | TypePromotionTransaction TPT; |
| 3715 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = |
| 3716 | TPT.getRestorationPoint(); |
| 3717 | SmallVector<Instruction *, 1> Exts; |
| 3718 | Exts.push_back(I); |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3719 | // Look for a load being extended. |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3720 | LoadInst *LI = nullptr; |
| 3721 | Instruction *OldExt = I; |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3722 | bool HasPromoted = extLdPromotion(TPT, LI, I, Exts); |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3723 | if (!LI || !I) { |
| 3724 | assert(!HasPromoted && !LI && "If we did not match any load instruction " |
| 3725 | "the code must remain the same"); |
| 3726 | I = OldExt; |
| 3727 | return false; |
| 3728 | } |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3729 | |
| 3730 | // If they're already in the same block, there's nothing to do. |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3731 | // Make the cheap checks first if we did not promote. |
| 3732 | // If we promoted, we need to check if it is indeed profitable. |
| 3733 | if (!HasPromoted && LI->getParent() == I->getParent()) |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3734 | return false; |
| 3735 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 3736 | EVT VT = TLI->getValueType(*DL, I->getType()); |
| 3737 | EVT LoadVT = TLI->getValueType(*DL, LI->getType()); |
Ahmed Bougacha | 55e3c2d | 2014-12-05 18:04:40 +0000 | [diff] [blame] | 3738 | |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3739 | // If the load has other users and the truncate is not free, this probably |
| 3740 | // isn't worthwhile. |
Ahmed Bougacha | 55e3c2d | 2014-12-05 18:04:40 +0000 | [diff] [blame] | 3741 | if (!LI->hasOneUse() && TLI && |
| 3742 | (TLI->isTypeLegal(LoadVT) || !TLI->isTypeLegal(VT)) && |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3743 | !TLI->isTruncateFree(I->getType(), LI->getType())) { |
| 3744 | I = OldExt; |
| 3745 | TPT.rollback(LastKnownGood); |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3746 | return false; |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3747 | } |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3748 | |
| 3749 | // Check whether the target supports casts folded into loads. |
| 3750 | unsigned LType; |
| 3751 | if (isa<ZExtInst>(I)) |
| 3752 | LType = ISD::ZEXTLOAD; |
| 3753 | else { |
| 3754 | assert(isa<SExtInst>(I) && "Unexpected ext type!"); |
| 3755 | LType = ISD::SEXTLOAD; |
| 3756 | } |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 3757 | if (TLI && !TLI->isLoadExtLegal(LType, VT, LoadVT)) { |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3758 | I = OldExt; |
| 3759 | TPT.rollback(LastKnownGood); |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3760 | return false; |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3761 | } |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3762 | |
| 3763 | // Move the extend into the same block as the load, so that SelectionDAG |
| 3764 | // can fold it. |
Quentin Colombet | fc2201e | 2014-12-17 01:36:17 +0000 | [diff] [blame] | 3765 | TPT.commit(); |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3766 | I->removeFromParent(); |
| 3767 | I->insertAfter(LI); |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 3768 | ++NumExtsMoved; |
Dan Gohman | 99429a0 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 3769 | return true; |
| 3770 | } |
| 3771 | |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3772 | bool CodeGenPrepare::optimizeExtUses(Instruction *I) { |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3773 | BasicBlock *DefBB = I->getParent(); |
| 3774 | |
Bob Wilson | ff714f9 | 2010-09-21 21:44:14 +0000 | [diff] [blame] | 3775 | // 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] | 3776 | // other uses of the source with result of extension. |
| 3777 | Value *Src = I->getOperand(0); |
| 3778 | if (Src->hasOneUse()) |
| 3779 | return false; |
| 3780 | |
Evan Cheng | 2011df4 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 3781 | // Only do this xform if truncating is free. |
Gabor Greif | aa26172 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 3782 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | 37c36ed | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 3783 | return false; |
| 3784 | |
Evan Cheng | 7bc8942 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 3785 | // 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] | 3786 | // this block. |
| 3787 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 7bc8942 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 3788 | return false; |
| 3789 | |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3790 | bool DefIsLiveOut = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3791 | for (User *U : I->users()) { |
| 3792 | Instruction *UI = cast<Instruction>(U); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3793 | |
| 3794 | // Figure out which BB this ext is used in. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3795 | BasicBlock *UserBB = UI->getParent(); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3796 | if (UserBB == DefBB) continue; |
| 3797 | DefIsLiveOut = true; |
| 3798 | break; |
| 3799 | } |
| 3800 | if (!DefIsLiveOut) |
| 3801 | return false; |
| 3802 | |
Jim Grosbach | 0f38c1e | 2013-04-15 17:40:48 +0000 | [diff] [blame] | 3803 | // Make sure none of the uses are PHI nodes. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3804 | for (User *U : Src->users()) { |
| 3805 | Instruction *UI = cast<Instruction>(U); |
| 3806 | BasicBlock *UserBB = UI->getParent(); |
Evan Cheng | 37c36ed | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 3807 | if (UserBB == DefBB) continue; |
| 3808 | // Be conservative. We don't want this xform to end up introducing |
| 3809 | // reloads just before load / store instructions. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3810 | if (isa<PHINode>(UI) || isa<LoadInst>(UI) || isa<StoreInst>(UI)) |
Evan Cheng | 63d33cf | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 3811 | return false; |
| 3812 | } |
| 3813 | |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3814 | // InsertedTruncs - Only insert one trunc in each block once. |
| 3815 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 3816 | |
| 3817 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3818 | for (Use &U : Src->uses()) { |
| 3819 | Instruction *User = cast<Instruction>(U.getUser()); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3820 | |
| 3821 | // Figure out which BB this ext is used in. |
| 3822 | BasicBlock *UserBB = User->getParent(); |
| 3823 | if (UserBB == DefBB) continue; |
| 3824 | |
| 3825 | // Both src and def are live in this block. Rewrite the use. |
| 3826 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 3827 | |
| 3828 | if (!InsertedTrunc) { |
Bill Wendling | 8ddfc09 | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 3829 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3830 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 3831 | InsertedInsts.insert(InsertedTrunc); |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | // 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] | 3835 | U = InsertedTrunc; |
Cameron Zwarich | ced753f | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 3836 | ++NumExtUses; |
Evan Cheng | d3d8017 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 3837 | MadeChange = true; |
| 3838 | } |
| 3839 | |
| 3840 | return MadeChange; |
| 3841 | } |
| 3842 | |
Sanjay Patel | 4ac6b11 | 2015-09-21 22:47:23 +0000 | [diff] [blame] | 3843 | /// Returns true if a SelectInst should be turned into an explicit branch. |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3844 | static bool isFormingBranchFromSelectProfitable(SelectInst *SI) { |
| 3845 | // FIXME: This should use the same heuristics as IfConversion to determine |
| 3846 | // whether a select is better represented as a branch. This requires that |
| 3847 | // branch probability metadata is preserved for the select, which is not the |
| 3848 | // case currently. |
| 3849 | |
| 3850 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 3851 | |
| 3852 | // If the branch is predicted right, an out of order CPU can avoid blocking on |
| 3853 | // the compare. Emit cmovs on compares with a memory operand as branches to |
| 3854 | // avoid stalls on the load from memory. If the compare has more than one use |
| 3855 | // there's probably another cmov or setcc around so it's not worth emitting a |
| 3856 | // branch. |
Sanjay Patel | 5e5f0e9 | 2015-09-28 21:44:46 +0000 | [diff] [blame^] | 3857 | if (!Cmp || !Cmp->hasOneUse()) |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3858 | return false; |
| 3859 | |
| 3860 | Value *CmpOp0 = Cmp->getOperand(0); |
| 3861 | Value *CmpOp1 = Cmp->getOperand(1); |
| 3862 | |
| 3863 | // We check that the memory operand has one use to avoid uses of the loaded |
| 3864 | // value directly after the compare, making branches unprofitable. |
Sanjay Patel | 5e5f0e9 | 2015-09-28 21:44:46 +0000 | [diff] [blame^] | 3865 | return ((isa<LoadInst>(CmpOp0) && CmpOp0->hasOneUse()) || |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3866 | (isa<LoadInst>(CmpOp1) && CmpOp1->hasOneUse())); |
| 3867 | } |
| 3868 | |
| 3869 | |
Nadav Rotem | 9d83202 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 3870 | /// If we have a SelectInst that will likely profit from branch prediction, |
| 3871 | /// turn it into a branch. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3872 | bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) { |
Nadav Rotem | 9d83202 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 3873 | bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1); |
| 3874 | |
| 3875 | // Can we convert the 'select' to CF ? |
| 3876 | if (DisableSelectToBranch || OptSize || !TLI || VectorCond) |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3877 | return false; |
| 3878 | |
Nadav Rotem | 9d83202 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 3879 | TargetLowering::SelectSupportKind SelectKind; |
| 3880 | if (VectorCond) |
| 3881 | SelectKind = TargetLowering::VectorMaskSelect; |
| 3882 | else if (SI->getType()->isVectorTy()) |
| 3883 | SelectKind = TargetLowering::ScalarCondVectorVal; |
| 3884 | else |
| 3885 | SelectKind = TargetLowering::ScalarValSelect; |
| 3886 | |
| 3887 | // Do we have efficient codegen support for this kind of 'selects' ? |
| 3888 | if (TLI->isSelectSupported(SelectKind)) { |
| 3889 | // We have efficient codegen support for the select instruction. |
| 3890 | // Check if it is profitable to keep this 'select'. |
| 3891 | if (!TLI->isPredictableSelectExpensive() || |
| 3892 | !isFormingBranchFromSelectProfitable(SI)) |
| 3893 | return false; |
| 3894 | } |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 3895 | |
| 3896 | ModifiedDT = true; |
| 3897 | |
| 3898 | // First, we split the block containing the select into 2 blocks. |
| 3899 | BasicBlock *StartBlock = SI->getParent(); |
| 3900 | BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(SI)); |
| 3901 | BasicBlock *NextBlock = StartBlock->splitBasicBlock(SplitPt, "select.end"); |
| 3902 | |
| 3903 | // Create a new block serving as the landing pad for the branch. |
| 3904 | BasicBlock *SmallBlock = BasicBlock::Create(SI->getContext(), "select.mid", |
| 3905 | NextBlock->getParent(), NextBlock); |
| 3906 | |
| 3907 | // Move the unconditional branch from the block with the select in it into our |
| 3908 | // landing pad block. |
| 3909 | StartBlock->getTerminator()->eraseFromParent(); |
| 3910 | BranchInst::Create(NextBlock, SmallBlock); |
| 3911 | |
| 3912 | // Insert the real conditional branch based on the original condition. |
| 3913 | BranchInst::Create(NextBlock, SmallBlock, SI->getCondition(), SI); |
| 3914 | |
| 3915 | // The select itself is replaced with a PHI Node. |
| 3916 | PHINode *PN = PHINode::Create(SI->getType(), 2, "", NextBlock->begin()); |
| 3917 | PN->takeName(SI); |
| 3918 | PN->addIncoming(SI->getTrueValue(), StartBlock); |
| 3919 | PN->addIncoming(SI->getFalseValue(), SmallBlock); |
| 3920 | SI->replaceAllUsesWith(PN); |
| 3921 | SI->eraseFromParent(); |
| 3922 | |
| 3923 | // Instruct OptimizeBlock to skip to the next block. |
| 3924 | CurInstIterator = StartBlock->end(); |
| 3925 | ++NumSelectsExpanded; |
| 3926 | return true; |
| 3927 | } |
| 3928 | |
Benjamin Kramer | 573ff36 | 2014-03-01 17:24:40 +0000 | [diff] [blame] | 3929 | static bool isBroadcastShuffle(ShuffleVectorInst *SVI) { |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3930 | SmallVector<int, 16> Mask(SVI->getShuffleMask()); |
| 3931 | int SplatElem = -1; |
| 3932 | for (unsigned i = 0; i < Mask.size(); ++i) { |
| 3933 | if (SplatElem != -1 && Mask[i] != -1 && Mask[i] != SplatElem) |
| 3934 | return false; |
| 3935 | SplatElem = Mask[i]; |
| 3936 | } |
| 3937 | |
| 3938 | return true; |
| 3939 | } |
| 3940 | |
| 3941 | /// Some targets have expensive vector shifts if the lanes aren't all the same |
| 3942 | /// (e.g. x86 only introduced "vpsllvd" and friends with AVX2). In these cases |
| 3943 | /// it's often worth sinking a shufflevector splat down to its use so that |
| 3944 | /// codegen can spot all lanes are identical. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 3945 | bool CodeGenPrepare::optimizeShuffleVectorInst(ShuffleVectorInst *SVI) { |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3946 | BasicBlock *DefBB = SVI->getParent(); |
| 3947 | |
| 3948 | // Only do this xform if variable vector shifts are particularly expensive. |
| 3949 | if (!TLI || !TLI->isVectorShiftByScalarCheap(SVI->getType())) |
| 3950 | return false; |
| 3951 | |
| 3952 | // We only expect better codegen by sinking a shuffle if we can recognise a |
| 3953 | // constant splat. |
| 3954 | if (!isBroadcastShuffle(SVI)) |
| 3955 | return false; |
| 3956 | |
| 3957 | // InsertedShuffles - Only insert a shuffle in each block once. |
| 3958 | DenseMap<BasicBlock*, Instruction*> InsertedShuffles; |
| 3959 | |
| 3960 | bool MadeChange = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3961 | for (User *U : SVI->users()) { |
| 3962 | Instruction *UI = cast<Instruction>(U); |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3963 | |
| 3964 | // Figure out which BB this ext is used in. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3965 | BasicBlock *UserBB = UI->getParent(); |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3966 | if (UserBB == DefBB) continue; |
| 3967 | |
| 3968 | // 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] | 3969 | if (!UI->isShift()) continue; |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3970 | |
| 3971 | // Everything checks out, sink the shuffle if the user's block doesn't |
| 3972 | // already have a copy. |
| 3973 | Instruction *&InsertedShuffle = InsertedShuffles[UserBB]; |
| 3974 | |
| 3975 | if (!InsertedShuffle) { |
| 3976 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
| 3977 | InsertedShuffle = new ShuffleVectorInst(SVI->getOperand(0), |
| 3978 | SVI->getOperand(1), |
| 3979 | SVI->getOperand(2), "", InsertPt); |
| 3980 | } |
| 3981 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3982 | UI->replaceUsesOfWith(SVI, InsertedShuffle); |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 3983 | MadeChange = true; |
| 3984 | } |
| 3985 | |
| 3986 | // If we removed all uses, nuke the shuffle. |
| 3987 | if (SVI->use_empty()) { |
| 3988 | SVI->eraseFromParent(); |
| 3989 | MadeChange = true; |
| 3990 | } |
| 3991 | |
| 3992 | return MadeChange; |
| 3993 | } |
| 3994 | |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 3995 | namespace { |
| 3996 | /// \brief Helper class to promote a scalar operation to a vector one. |
| 3997 | /// This class is used to move downward extractelement transition. |
| 3998 | /// E.g., |
| 3999 | /// a = vector_op <2 x i32> |
| 4000 | /// b = extractelement <2 x i32> a, i32 0 |
| 4001 | /// c = scalar_op b |
| 4002 | /// store c |
| 4003 | /// |
| 4004 | /// => |
| 4005 | /// a = vector_op <2 x i32> |
| 4006 | /// c = vector_op a (equivalent to scalar_op on the related lane) |
| 4007 | /// * d = extractelement <2 x i32> c, i32 0 |
| 4008 | /// * store d |
| 4009 | /// Assuming both extractelement and store can be combine, we get rid of the |
| 4010 | /// transition. |
| 4011 | class VectorPromoteHelper { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4012 | /// DataLayout associated with the current module. |
| 4013 | const DataLayout &DL; |
| 4014 | |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4015 | /// Used to perform some checks on the legality of vector operations. |
| 4016 | const TargetLowering &TLI; |
| 4017 | |
| 4018 | /// Used to estimated the cost of the promoted chain. |
| 4019 | const TargetTransformInfo &TTI; |
| 4020 | |
| 4021 | /// The transition being moved downwards. |
| 4022 | Instruction *Transition; |
| 4023 | /// The sequence of instructions to be promoted. |
| 4024 | SmallVector<Instruction *, 4> InstsToBePromoted; |
| 4025 | /// Cost of combining a store and an extract. |
| 4026 | unsigned StoreExtractCombineCost; |
| 4027 | /// Instruction that will be combined with the transition. |
| 4028 | Instruction *CombineInst; |
| 4029 | |
| 4030 | /// \brief The instruction that represents the current end of the transition. |
| 4031 | /// Since we are faking the promotion until we reach the end of the chain |
| 4032 | /// of computation, we need a way to get the current end of the transition. |
| 4033 | Instruction *getEndOfTransition() const { |
| 4034 | if (InstsToBePromoted.empty()) |
| 4035 | return Transition; |
| 4036 | return InstsToBePromoted.back(); |
| 4037 | } |
| 4038 | |
| 4039 | /// \brief Return the index of the original value in the transition. |
| 4040 | /// E.g., for "extractelement <2 x i32> c, i32 1" the original value, |
| 4041 | /// c, is at index 0. |
| 4042 | unsigned getTransitionOriginalValueIdx() const { |
| 4043 | assert(isa<ExtractElementInst>(Transition) && |
| 4044 | "Other kind of transitions are not supported yet"); |
| 4045 | return 0; |
| 4046 | } |
| 4047 | |
| 4048 | /// \brief Return the index of the index in the transition. |
| 4049 | /// E.g., for "extractelement <2 x i32> c, i32 0" the index |
| 4050 | /// is at index 1. |
| 4051 | unsigned getTransitionIdx() const { |
| 4052 | assert(isa<ExtractElementInst>(Transition) && |
| 4053 | "Other kind of transitions are not supported yet"); |
| 4054 | return 1; |
| 4055 | } |
| 4056 | |
| 4057 | /// \brief Get the type of the transition. |
| 4058 | /// This is the type of the original value. |
| 4059 | /// E.g., for "extractelement <2 x i32> c, i32 1" the type of the |
| 4060 | /// transition is <2 x i32>. |
| 4061 | Type *getTransitionType() const { |
| 4062 | return Transition->getOperand(getTransitionOriginalValueIdx())->getType(); |
| 4063 | } |
| 4064 | |
| 4065 | /// \brief Promote \p ToBePromoted by moving \p Def downward through. |
| 4066 | /// I.e., we have the following sequence: |
| 4067 | /// Def = Transition <ty1> a to <ty2> |
| 4068 | /// b = ToBePromoted <ty2> Def, ... |
| 4069 | /// => |
| 4070 | /// b = ToBePromoted <ty1> a, ... |
| 4071 | /// Def = Transition <ty1> ToBePromoted to <ty2> |
| 4072 | void promoteImpl(Instruction *ToBePromoted); |
| 4073 | |
| 4074 | /// \brief Check whether or not it is profitable to promote all the |
| 4075 | /// instructions enqueued to be promoted. |
| 4076 | bool isProfitableToPromote() { |
| 4077 | Value *ValIdx = Transition->getOperand(getTransitionOriginalValueIdx()); |
| 4078 | unsigned Index = isa<ConstantInt>(ValIdx) |
| 4079 | ? cast<ConstantInt>(ValIdx)->getZExtValue() |
| 4080 | : -1; |
| 4081 | Type *PromotedType = getTransitionType(); |
| 4082 | |
| 4083 | StoreInst *ST = cast<StoreInst>(CombineInst); |
| 4084 | unsigned AS = ST->getPointerAddressSpace(); |
| 4085 | unsigned Align = ST->getAlignment(); |
| 4086 | // Check if this store is supported. |
| 4087 | if (!TLI.allowsMisalignedMemoryAccesses( |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4088 | TLI.getValueType(DL, ST->getValueOperand()->getType()), AS, |
| 4089 | Align)) { |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4090 | // If this is not supported, there is no way we can combine |
| 4091 | // the extract with the store. |
| 4092 | return false; |
| 4093 | } |
| 4094 | |
| 4095 | // The scalar chain of computation has to pay for the transition |
| 4096 | // scalar to vector. |
| 4097 | // The vector chain has to account for the combining cost. |
| 4098 | uint64_t ScalarCost = |
| 4099 | TTI.getVectorInstrCost(Transition->getOpcode(), PromotedType, Index); |
| 4100 | uint64_t VectorCost = StoreExtractCombineCost; |
| 4101 | for (const auto &Inst : InstsToBePromoted) { |
| 4102 | // Compute the cost. |
| 4103 | // By construction, all instructions being promoted are arithmetic ones. |
| 4104 | // Moreover, one argument is a constant that can be viewed as a splat |
| 4105 | // constant. |
| 4106 | Value *Arg0 = Inst->getOperand(0); |
| 4107 | bool IsArg0Constant = isa<UndefValue>(Arg0) || isa<ConstantInt>(Arg0) || |
| 4108 | isa<ConstantFP>(Arg0); |
| 4109 | TargetTransformInfo::OperandValueKind Arg0OVK = |
| 4110 | IsArg0Constant ? TargetTransformInfo::OK_UniformConstantValue |
| 4111 | : TargetTransformInfo::OK_AnyValue; |
| 4112 | TargetTransformInfo::OperandValueKind Arg1OVK = |
| 4113 | !IsArg0Constant ? TargetTransformInfo::OK_UniformConstantValue |
| 4114 | : TargetTransformInfo::OK_AnyValue; |
| 4115 | ScalarCost += TTI.getArithmeticInstrCost( |
| 4116 | Inst->getOpcode(), Inst->getType(), Arg0OVK, Arg1OVK); |
| 4117 | VectorCost += TTI.getArithmeticInstrCost(Inst->getOpcode(), PromotedType, |
| 4118 | Arg0OVK, Arg1OVK); |
| 4119 | } |
| 4120 | DEBUG(dbgs() << "Estimated cost of computation to be promoted:\nScalar: " |
| 4121 | << ScalarCost << "\nVector: " << VectorCost << '\n'); |
| 4122 | return ScalarCost > VectorCost; |
| 4123 | } |
| 4124 | |
| 4125 | /// \brief Generate a constant vector with \p Val with the same |
| 4126 | /// number of elements as the transition. |
| 4127 | /// \p UseSplat defines whether or not \p Val should be replicated |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 4128 | /// across the whole vector. |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4129 | /// In other words, if UseSplat == true, we generate <Val, Val, ..., Val>, |
| 4130 | /// otherwise we generate a vector with as many undef as possible: |
| 4131 | /// <undef, ..., undef, Val, undef, ..., undef> where \p Val is only |
| 4132 | /// used at the index of the extract. |
| 4133 | Value *getConstantVector(Constant *Val, bool UseSplat) const { |
| 4134 | unsigned ExtractIdx = UINT_MAX; |
| 4135 | if (!UseSplat) { |
| 4136 | // If we cannot determine where the constant must be, we have to |
| 4137 | // use a splat constant. |
| 4138 | Value *ValExtractIdx = Transition->getOperand(getTransitionIdx()); |
| 4139 | if (ConstantInt *CstVal = dyn_cast<ConstantInt>(ValExtractIdx)) |
| 4140 | ExtractIdx = CstVal->getSExtValue(); |
| 4141 | else |
| 4142 | UseSplat = true; |
| 4143 | } |
| 4144 | |
| 4145 | unsigned End = getTransitionType()->getVectorNumElements(); |
| 4146 | if (UseSplat) |
| 4147 | return ConstantVector::getSplat(End, Val); |
| 4148 | |
| 4149 | SmallVector<Constant *, 4> ConstVec; |
| 4150 | UndefValue *UndefVal = UndefValue::get(Val->getType()); |
| 4151 | for (unsigned Idx = 0; Idx != End; ++Idx) { |
| 4152 | if (Idx == ExtractIdx) |
| 4153 | ConstVec.push_back(Val); |
| 4154 | else |
| 4155 | ConstVec.push_back(UndefVal); |
| 4156 | } |
| 4157 | return ConstantVector::get(ConstVec); |
| 4158 | } |
| 4159 | |
| 4160 | /// \brief Check if promoting to a vector type an operand at \p OperandIdx |
| 4161 | /// in \p Use can trigger undefined behavior. |
| 4162 | static bool canCauseUndefinedBehavior(const Instruction *Use, |
| 4163 | unsigned OperandIdx) { |
| 4164 | // This is not safe to introduce undef when the operand is on |
| 4165 | // the right hand side of a division-like instruction. |
| 4166 | if (OperandIdx != 1) |
| 4167 | return false; |
| 4168 | switch (Use->getOpcode()) { |
| 4169 | default: |
| 4170 | return false; |
| 4171 | case Instruction::SDiv: |
| 4172 | case Instruction::UDiv: |
| 4173 | case Instruction::SRem: |
| 4174 | case Instruction::URem: |
| 4175 | return true; |
| 4176 | case Instruction::FDiv: |
| 4177 | case Instruction::FRem: |
| 4178 | return !Use->hasNoNaNs(); |
| 4179 | } |
| 4180 | llvm_unreachable(nullptr); |
| 4181 | } |
| 4182 | |
| 4183 | public: |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4184 | VectorPromoteHelper(const DataLayout &DL, const TargetLowering &TLI, |
| 4185 | const TargetTransformInfo &TTI, Instruction *Transition, |
| 4186 | unsigned CombineCost) |
| 4187 | : DL(DL), TLI(TLI), TTI(TTI), Transition(Transition), |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4188 | StoreExtractCombineCost(CombineCost), CombineInst(nullptr) { |
| 4189 | assert(Transition && "Do not know how to promote null"); |
| 4190 | } |
| 4191 | |
| 4192 | /// \brief Check if we can promote \p ToBePromoted to \p Type. |
| 4193 | bool canPromote(const Instruction *ToBePromoted) const { |
| 4194 | // We could support CastInst too. |
| 4195 | return isa<BinaryOperator>(ToBePromoted); |
| 4196 | } |
| 4197 | |
| 4198 | /// \brief Check if it is profitable to promote \p ToBePromoted |
| 4199 | /// by moving downward the transition through. |
| 4200 | bool shouldPromote(const Instruction *ToBePromoted) const { |
| 4201 | // Promote only if all the operands can be statically expanded. |
| 4202 | // Indeed, we do not want to introduce any new kind of transitions. |
| 4203 | for (const Use &U : ToBePromoted->operands()) { |
| 4204 | const Value *Val = U.get(); |
| 4205 | if (Val == getEndOfTransition()) { |
| 4206 | // If the use is a division and the transition is on the rhs, |
| 4207 | // we cannot promote the operation, otherwise we may create a |
| 4208 | // division by zero. |
| 4209 | if (canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo())) |
| 4210 | return false; |
| 4211 | continue; |
| 4212 | } |
| 4213 | if (!isa<ConstantInt>(Val) && !isa<UndefValue>(Val) && |
| 4214 | !isa<ConstantFP>(Val)) |
| 4215 | return false; |
| 4216 | } |
| 4217 | // Check that the resulting operation is legal. |
| 4218 | int ISDOpcode = TLI.InstructionOpcodeToISD(ToBePromoted->getOpcode()); |
| 4219 | if (!ISDOpcode) |
| 4220 | return false; |
| 4221 | return StressStoreExtract || |
Ahmed Bougacha | 026600d | 2014-11-12 23:05:03 +0000 | [diff] [blame] | 4222 | TLI.isOperationLegalOrCustom( |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4223 | ISDOpcode, TLI.getValueType(DL, getTransitionType(), true)); |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | /// \brief Check whether or not \p Use can be combined |
| 4227 | /// with the transition. |
| 4228 | /// I.e., is it possible to do Use(Transition) => AnotherUse? |
| 4229 | bool canCombine(const Instruction *Use) { return isa<StoreInst>(Use); } |
| 4230 | |
| 4231 | /// \brief Record \p ToBePromoted as part of the chain to be promoted. |
| 4232 | void enqueueForPromotion(Instruction *ToBePromoted) { |
| 4233 | InstsToBePromoted.push_back(ToBePromoted); |
| 4234 | } |
| 4235 | |
| 4236 | /// \brief Set the instruction that will be combined with the transition. |
| 4237 | void recordCombineInstruction(Instruction *ToBeCombined) { |
| 4238 | assert(canCombine(ToBeCombined) && "Unsupported instruction to combine"); |
| 4239 | CombineInst = ToBeCombined; |
| 4240 | } |
| 4241 | |
| 4242 | /// \brief Promote all the instructions enqueued for promotion if it is |
| 4243 | /// is profitable. |
| 4244 | /// \return True if the promotion happened, false otherwise. |
| 4245 | bool promote() { |
| 4246 | // Check if there is something to promote. |
| 4247 | // Right now, if we do not have anything to combine with, |
| 4248 | // we assume the promotion is not profitable. |
| 4249 | if (InstsToBePromoted.empty() || !CombineInst) |
| 4250 | return false; |
| 4251 | |
| 4252 | // Check cost. |
| 4253 | if (!StressStoreExtract && !isProfitableToPromote()) |
| 4254 | return false; |
| 4255 | |
| 4256 | // Promote. |
| 4257 | for (auto &ToBePromoted : InstsToBePromoted) |
| 4258 | promoteImpl(ToBePromoted); |
| 4259 | InstsToBePromoted.clear(); |
| 4260 | return true; |
| 4261 | } |
| 4262 | }; |
| 4263 | } // End of anonymous namespace. |
| 4264 | |
| 4265 | void VectorPromoteHelper::promoteImpl(Instruction *ToBePromoted) { |
| 4266 | // At this point, we know that all the operands of ToBePromoted but Def |
| 4267 | // can be statically promoted. |
| 4268 | // For Def, we need to use its parameter in ToBePromoted: |
| 4269 | // b = ToBePromoted ty1 a |
| 4270 | // Def = Transition ty1 b to ty2 |
| 4271 | // Move the transition down. |
| 4272 | // 1. Replace all uses of the promoted operation by the transition. |
| 4273 | // = ... b => = ... Def. |
| 4274 | assert(ToBePromoted->getType() == Transition->getType() && |
| 4275 | "The type of the result of the transition does not match " |
| 4276 | "the final type"); |
| 4277 | ToBePromoted->replaceAllUsesWith(Transition); |
| 4278 | // 2. Update the type of the uses. |
| 4279 | // b = ToBePromoted ty2 Def => b = ToBePromoted ty1 Def. |
| 4280 | Type *TransitionTy = getTransitionType(); |
| 4281 | ToBePromoted->mutateType(TransitionTy); |
| 4282 | // 3. Update all the operands of the promoted operation with promoted |
| 4283 | // operands. |
| 4284 | // b = ToBePromoted ty1 Def => b = ToBePromoted ty1 a. |
| 4285 | for (Use &U : ToBePromoted->operands()) { |
| 4286 | Value *Val = U.get(); |
| 4287 | Value *NewVal = nullptr; |
| 4288 | if (Val == Transition) |
| 4289 | NewVal = Transition->getOperand(getTransitionOriginalValueIdx()); |
| 4290 | else if (isa<UndefValue>(Val) || isa<ConstantInt>(Val) || |
| 4291 | isa<ConstantFP>(Val)) { |
| 4292 | // Use a splat constant if it is not safe to use undef. |
| 4293 | NewVal = getConstantVector( |
| 4294 | cast<Constant>(Val), |
| 4295 | isa<UndefValue>(Val) || |
| 4296 | canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo())); |
| 4297 | } else |
Craig Topper | d3c02f1 | 2015-01-05 10:15:49 +0000 | [diff] [blame] | 4298 | llvm_unreachable("Did you modified shouldPromote and forgot to update " |
| 4299 | "this?"); |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4300 | ToBePromoted->setOperand(U.getOperandNo(), NewVal); |
| 4301 | } |
| 4302 | Transition->removeFromParent(); |
| 4303 | Transition->insertAfter(ToBePromoted); |
| 4304 | Transition->setOperand(getTransitionOriginalValueIdx(), ToBePromoted); |
| 4305 | } |
| 4306 | |
| 4307 | /// Some targets can do store(extractelement) with one instruction. |
| 4308 | /// Try to push the extractelement towards the stores when the target |
| 4309 | /// has this feature and this is profitable. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4310 | bool CodeGenPrepare::optimizeExtractElementInst(Instruction *Inst) { |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4311 | unsigned CombineCost = UINT_MAX; |
| 4312 | if (DisableStoreExtract || !TLI || |
| 4313 | (!StressStoreExtract && |
| 4314 | !TLI->canCombineStoreAndExtract(Inst->getOperand(0)->getType(), |
| 4315 | Inst->getOperand(1), CombineCost))) |
| 4316 | return false; |
| 4317 | |
| 4318 | // At this point we know that Inst is a vector to scalar transition. |
| 4319 | // Try to move it down the def-use chain, until: |
| 4320 | // - We can combine the transition with its single use |
| 4321 | // => we got rid of the transition. |
| 4322 | // - We escape the current basic block |
| 4323 | // => we would need to check that we are moving it at a cheaper place and |
| 4324 | // we do not do that for now. |
| 4325 | BasicBlock *Parent = Inst->getParent(); |
| 4326 | DEBUG(dbgs() << "Found an interesting transition: " << *Inst << '\n'); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4327 | VectorPromoteHelper VPH(*DL, *TLI, *TTI, Inst, CombineCost); |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4328 | // If the transition has more than one use, assume this is not going to be |
| 4329 | // beneficial. |
| 4330 | while (Inst->hasOneUse()) { |
| 4331 | Instruction *ToBePromoted = cast<Instruction>(*Inst->user_begin()); |
| 4332 | DEBUG(dbgs() << "Use: " << *ToBePromoted << '\n'); |
| 4333 | |
| 4334 | if (ToBePromoted->getParent() != Parent) { |
| 4335 | DEBUG(dbgs() << "Instruction to promote is in a different block (" |
| 4336 | << ToBePromoted->getParent()->getName() |
| 4337 | << ") than the transition (" << Parent->getName() << ").\n"); |
| 4338 | return false; |
| 4339 | } |
| 4340 | |
| 4341 | if (VPH.canCombine(ToBePromoted)) { |
| 4342 | DEBUG(dbgs() << "Assume " << *Inst << '\n' |
| 4343 | << "will be combined with: " << *ToBePromoted << '\n'); |
| 4344 | VPH.recordCombineInstruction(ToBePromoted); |
| 4345 | bool Changed = VPH.promote(); |
| 4346 | NumStoreExtractExposed += Changed; |
| 4347 | return Changed; |
| 4348 | } |
| 4349 | |
| 4350 | DEBUG(dbgs() << "Try promoting.\n"); |
| 4351 | if (!VPH.canPromote(ToBePromoted) || !VPH.shouldPromote(ToBePromoted)) |
| 4352 | return false; |
| 4353 | |
| 4354 | DEBUG(dbgs() << "Promoting is possible... Enqueue for promotion!\n"); |
| 4355 | |
| 4356 | VPH.enqueueForPromotion(ToBePromoted); |
| 4357 | Inst = ToBePromoted; |
| 4358 | } |
| 4359 | return false; |
| 4360 | } |
| 4361 | |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4362 | bool CodeGenPrepare::optimizeInst(Instruction *I, bool& ModifiedDT) { |
Ahmed Bougacha | f329914 | 2015-06-17 20:44:32 +0000 | [diff] [blame] | 4363 | // Bail out if we inserted the instruction to prevent optimizations from |
| 4364 | // stepping on each other's toes. |
| 4365 | if (InsertedInsts.count(I)) |
| 4366 | return false; |
| 4367 | |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4368 | if (PHINode *P = dyn_cast<PHINode>(I)) { |
| 4369 | // It is possible for very late stage optimizations (such as SimplifyCFG) |
| 4370 | // to introduce PHI nodes too late to be cleaned up. If we detect such a |
| 4371 | // trivial PHI, go ahead and zap it here. |
Mehdi Amini | 4fe3798 | 2015-07-07 18:45:17 +0000 | [diff] [blame] | 4372 | if (Value *V = SimplifyInstruction(P, *DL, TLInfo, nullptr)) { |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4373 | P->replaceAllUsesWith(V); |
| 4374 | P->eraseFromParent(); |
| 4375 | ++NumPHIsElim; |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4376 | return true; |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4377 | } |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4378 | return false; |
| 4379 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 4380 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4381 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4382 | // If the source of the cast is a constant, then this should have |
| 4383 | // already been constant folded. The only reason NOT to constant fold |
| 4384 | // it is if something (e.g. LSR) was careful to place the constant |
| 4385 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 4386 | // the address of globals out of a loop). If this is the case, we don't |
| 4387 | // want to forward-subst the cast. |
| 4388 | if (isa<Constant>(CI->getOperand(0))) |
| 4389 | return false; |
| 4390 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4391 | if (TLI && OptimizeNoopCopyExpression(CI, *TLI, *DL)) |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4392 | return true; |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4393 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4394 | if (isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 4395 | /// Sink a zext or sext into its user blocks if the target type doesn't |
| 4396 | /// fit in one register |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4397 | if (TLI && |
| 4398 | TLI->getTypeAction(CI->getContext(), |
| 4399 | TLI->getValueType(*DL, CI->getType())) == |
| 4400 | TargetLowering::TypeExpandInteger) { |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 4401 | return SinkCast(CI); |
| 4402 | } else { |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4403 | bool MadeChange = moveExtToFormExtLoad(I); |
| 4404 | return MadeChange | optimizeExtUses(I); |
Manuel Jacob | a7c48f9 | 2014-03-13 13:36:25 +0000 | [diff] [blame] | 4405 | } |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4406 | } |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4407 | return false; |
| 4408 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 4409 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4410 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
Hal Finkel | decb024 | 2014-01-02 21:13:43 +0000 | [diff] [blame] | 4411 | if (!TLI || !TLI->hasMultipleConditionRegisters()) |
| 4412 | return OptimizeCmpExpression(CI); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 4413 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4414 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Piotr Padlewski | 6c15ec4 | 2015-09-15 18:32:14 +0000 | [diff] [blame] | 4415 | stripInvariantGroupMetadata(*LI); |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 4416 | if (TLI) { |
| 4417 | unsigned AS = LI->getPointerAddressSpace(); |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4418 | return optimizeMemoryInst(I, I->getOperand(0), LI->getType(), AS); |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 4419 | } |
Hans Wennborg | f325483 | 2012-10-30 11:23:25 +0000 | [diff] [blame] | 4420 | return false; |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4421 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 4422 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4423 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Piotr Padlewski | 6c15ec4 | 2015-09-15 18:32:14 +0000 | [diff] [blame] | 4424 | stripInvariantGroupMetadata(*SI); |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 4425 | if (TLI) { |
| 4426 | unsigned AS = SI->getPointerAddressSpace(); |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4427 | return optimizeMemoryInst(I, SI->getOperand(1), |
Matt Arsenault | f72b49b | 2015-06-04 16:17:38 +0000 | [diff] [blame] | 4428 | SI->getOperand(0)->getType(), AS); |
| 4429 | } |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4430 | return false; |
| 4431 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 4432 | |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 4433 | BinaryOperator *BinOp = dyn_cast<BinaryOperator>(I); |
| 4434 | |
| 4435 | if (BinOp && (BinOp->getOpcode() == Instruction::AShr || |
| 4436 | BinOp->getOpcode() == Instruction::LShr)) { |
| 4437 | ConstantInt *CI = dyn_cast<ConstantInt>(BinOp->getOperand(1)); |
| 4438 | if (TLI && CI && TLI->hasExtractBitsInsn()) |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 4439 | return OptimizeExtractBits(BinOp, CI, *TLI, *DL); |
Yi Jiang | d069f63 | 2014-04-21 19:34:27 +0000 | [diff] [blame] | 4440 | |
| 4441 | return false; |
| 4442 | } |
| 4443 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4444 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Cameron Zwarich | d28c78e | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 4445 | if (GEPI->hasAllZeroIndices()) { |
| 4446 | /// The GEP operand must be a pointer, so must its result -> BitCast |
| 4447 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
| 4448 | GEPI->getName(), GEPI); |
| 4449 | GEPI->replaceAllUsesWith(NC); |
| 4450 | GEPI->eraseFromParent(); |
| 4451 | ++NumGEPsElim; |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4452 | optimizeInst(NC, ModifiedDT); |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4453 | return true; |
Cameron Zwarich | d28c78e | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 4454 | } |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4455 | return false; |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4456 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 4457 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4458 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4459 | return optimizeCallInst(CI, ModifiedDT); |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4460 | |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 4461 | if (SelectInst *SI = dyn_cast<SelectInst>(I)) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4462 | return optimizeSelectInst(SI); |
Benjamin Kramer | 047d7ca | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 4463 | |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 4464 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4465 | return optimizeShuffleVectorInst(SVI); |
Tim Northover | aeb8e06 | 2014-02-19 10:02:43 +0000 | [diff] [blame] | 4466 | |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4467 | if (isa<ExtractElementInst>(I)) |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4468 | return optimizeExtractElementInst(I); |
Quentin Colombet | c32615d | 2014-10-31 17:52:53 +0000 | [diff] [blame] | 4469 | |
Chris Lattner | ee588de | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 4470 | return false; |
Cameron Zwarich | 14ac865 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 4473 | // In this pass we look for GEP and cast instructions that are used |
| 4474 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 4475 | // selection. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4476 | bool CodeGenPrepare::optimizeBlock(BasicBlock &BB, bool& ModifiedDT) { |
Cameron Zwarich | ce3b930 | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 4477 | SunkAddrs.clear(); |
Cameron Zwarich | 5dd2aa2 | 2011-03-02 03:31:46 +0000 | [diff] [blame] | 4478 | bool MadeChange = false; |
Eric Christopher | c1ea149 | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 4479 | |
Chris Lattner | 7a27714 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 4480 | CurInstIterator = BB.begin(); |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 4481 | while (CurInstIterator != BB.end()) { |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4482 | MadeChange |= optimizeInst(CurInstIterator++, ModifiedDT); |
Elena Demikhovsky | 87700a7 | 2014-12-28 08:54:45 +0000 | [diff] [blame] | 4483 | if (ModifiedDT) |
| 4484 | return true; |
| 4485 | } |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4486 | MadeChange |= dupRetToEnableTailCallOpts(&BB); |
Benjamin Kramer | 455fa35 | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 4487 | |
Chris Lattner | f2836d1 | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 4488 | return MadeChange; |
| 4489 | } |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 4490 | |
| 4491 | // 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] | 4492 | // 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] | 4493 | // find a node corresponding to the value. |
Sanjay Patel | fc580a6 | 2015-09-21 23:03:16 +0000 | [diff] [blame] | 4494 | bool CodeGenPrepare::placeDbgValues(Function &F) { |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 4495 | bool MadeChange = false; |
Duncan P. N. Exon Smith | 5914a97 | 2015-01-08 20:44:33 +0000 | [diff] [blame] | 4496 | for (BasicBlock &BB : F) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 4497 | Instruction *PrevNonDbgInst = nullptr; |
Duncan P. N. Exon Smith | 5914a97 | 2015-01-08 20:44:33 +0000 | [diff] [blame] | 4498 | for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) { |
Duncan P. N. Exon Smith | e90f116 | 2015-01-08 21:07:55 +0000 | [diff] [blame] | 4499 | Instruction *Insn = BI++; |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 4500 | DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn); |
Adrian Prantl | 32da889 | 2014-04-25 20:49:25 +0000 | [diff] [blame] | 4501 | // Leave dbg.values that refer to an alloca alone. These |
| 4502 | // instrinsics describe the address of a variable (= the alloca) |
| 4503 | // being taken. They should not be moved next to the alloca |
| 4504 | // (and to the beginning of the scope), but rather stay close to |
| 4505 | // where said address is used. |
| 4506 | if (!DVI || (DVI->getValue() && isa<AllocaInst>(DVI->getValue()))) { |
Devang Patel | 53771ba | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 4507 | PrevNonDbgInst = Insn; |
| 4508 | continue; |
| 4509 | } |
| 4510 | |
| 4511 | Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue()); |
| 4512 | if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) { |
| 4513 | DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI); |
| 4514 | DVI->removeFromParent(); |
| 4515 | if (isa<PHINode>(VI)) |
| 4516 | DVI->insertBefore(VI->getParent()->getFirstInsertionPt()); |
| 4517 | else |
| 4518 | DVI->insertAfter(VI); |
| 4519 | MadeChange = true; |
| 4520 | ++NumDbgValueMoved; |
| 4521 | } |
| 4522 | } |
| 4523 | } |
| 4524 | return MadeChange; |
| 4525 | } |
Tim Northover | cea0abb | 2014-03-29 08:22:29 +0000 | [diff] [blame] | 4526 | |
| 4527 | // If there is a sequence that branches based on comparing a single bit |
| 4528 | // against zero that can be combined into a single instruction, and the |
| 4529 | // target supports folding these into a single instruction, sink the |
| 4530 | // mask and compare into the branch uses. Do this before OptimizeBlock -> |
| 4531 | // OptimizeInst -> OptimizeCmpExpression, which perturbs the pattern being |
| 4532 | // searched for. |
| 4533 | bool CodeGenPrepare::sinkAndCmp(Function &F) { |
| 4534 | if (!EnableAndCmpSinking) |
| 4535 | return false; |
| 4536 | if (!TLI || !TLI->isMaskAndBranchFoldingLegal()) |
| 4537 | return false; |
| 4538 | bool MadeChange = false; |
| 4539 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) { |
| 4540 | BasicBlock *BB = I++; |
| 4541 | |
| 4542 | // Does this BB end with the following? |
| 4543 | // %andVal = and %val, #single-bit-set |
| 4544 | // %icmpVal = icmp %andResult, 0 |
| 4545 | // br i1 %cmpVal label %dest1, label %dest2" |
| 4546 | BranchInst *Brcc = dyn_cast<BranchInst>(BB->getTerminator()); |
| 4547 | if (!Brcc || !Brcc->isConditional()) |
| 4548 | continue; |
| 4549 | ICmpInst *Cmp = dyn_cast<ICmpInst>(Brcc->getOperand(0)); |
| 4550 | if (!Cmp || Cmp->getParent() != BB) |
| 4551 | continue; |
| 4552 | ConstantInt *Zero = dyn_cast<ConstantInt>(Cmp->getOperand(1)); |
| 4553 | if (!Zero || !Zero->isZero()) |
| 4554 | continue; |
| 4555 | Instruction *And = dyn_cast<Instruction>(Cmp->getOperand(0)); |
| 4556 | if (!And || And->getOpcode() != Instruction::And || And->getParent() != BB) |
| 4557 | continue; |
| 4558 | ConstantInt* Mask = dyn_cast<ConstantInt>(And->getOperand(1)); |
| 4559 | if (!Mask || !Mask->getUniqueInteger().isPowerOf2()) |
| 4560 | continue; |
| 4561 | DEBUG(dbgs() << "found and; icmp ?,0; brcc\n"); DEBUG(BB->dump()); |
| 4562 | |
| 4563 | // Push the "and; icmp" for any users that are conditional branches. |
| 4564 | // Since there can only be one branch use per BB, we don't need to keep |
| 4565 | // track of which BBs we insert into. |
| 4566 | for (Value::use_iterator UI = Cmp->use_begin(), E = Cmp->use_end(); |
| 4567 | UI != E; ) { |
| 4568 | Use &TheUse = *UI; |
| 4569 | // Find brcc use. |
| 4570 | BranchInst *BrccUser = dyn_cast<BranchInst>(*UI); |
| 4571 | ++UI; |
| 4572 | if (!BrccUser || !BrccUser->isConditional()) |
| 4573 | continue; |
| 4574 | BasicBlock *UserBB = BrccUser->getParent(); |
| 4575 | if (UserBB == BB) continue; |
| 4576 | DEBUG(dbgs() << "found Brcc use\n"); |
| 4577 | |
| 4578 | // Sink the "and; icmp" to use. |
| 4579 | MadeChange = true; |
| 4580 | BinaryOperator *NewAnd = |
| 4581 | BinaryOperator::CreateAnd(And->getOperand(0), And->getOperand(1), "", |
| 4582 | BrccUser); |
| 4583 | CmpInst *NewCmp = |
| 4584 | CmpInst::Create(Cmp->getOpcode(), Cmp->getPredicate(), NewAnd, Zero, |
| 4585 | "", BrccUser); |
| 4586 | TheUse = NewCmp; |
| 4587 | ++NumAndCmpsMoved; |
| 4588 | DEBUG(BrccUser->getParent()->dump()); |
| 4589 | } |
| 4590 | } |
| 4591 | return MadeChange; |
| 4592 | } |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4593 | |
Juergen Ributzka | 194350a | 2014-12-09 17:32:12 +0000 | [diff] [blame] | 4594 | /// \brief Retrieve the probabilities of a conditional branch. Returns true on |
| 4595 | /// success, or returns false if no or invalid metadata was found. |
| 4596 | static bool extractBranchMetadata(BranchInst *BI, |
| 4597 | uint64_t &ProbTrue, uint64_t &ProbFalse) { |
| 4598 | assert(BI->isConditional() && |
| 4599 | "Looking for probabilities on unconditional branch?"); |
| 4600 | auto *ProfileData = BI->getMetadata(LLVMContext::MD_prof); |
| 4601 | if (!ProfileData || ProfileData->getNumOperands() != 3) |
| 4602 | return false; |
| 4603 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 4604 | const auto *CITrue = |
| 4605 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1)); |
| 4606 | const auto *CIFalse = |
| 4607 | mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2)); |
Juergen Ributzka | 194350a | 2014-12-09 17:32:12 +0000 | [diff] [blame] | 4608 | if (!CITrue || !CIFalse) |
| 4609 | return false; |
| 4610 | |
| 4611 | ProbTrue = CITrue->getValue().getZExtValue(); |
| 4612 | ProbFalse = CIFalse->getValue().getZExtValue(); |
| 4613 | |
| 4614 | return true; |
| 4615 | } |
| 4616 | |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4617 | /// \brief Scale down both weights to fit into uint32_t. |
| 4618 | static void scaleWeights(uint64_t &NewTrue, uint64_t &NewFalse) { |
| 4619 | uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse; |
| 4620 | uint32_t Scale = (NewMax / UINT32_MAX) + 1; |
| 4621 | NewTrue = NewTrue / Scale; |
| 4622 | NewFalse = NewFalse / Scale; |
| 4623 | } |
| 4624 | |
| 4625 | /// \brief Some targets prefer to split a conditional branch like: |
| 4626 | /// \code |
| 4627 | /// %0 = icmp ne i32 %a, 0 |
| 4628 | /// %1 = icmp ne i32 %b, 0 |
| 4629 | /// %or.cond = or i1 %0, %1 |
| 4630 | /// br i1 %or.cond, label %TrueBB, label %FalseBB |
| 4631 | /// \endcode |
| 4632 | /// into multiple branch instructions like: |
| 4633 | /// \code |
| 4634 | /// bb1: |
| 4635 | /// %0 = icmp ne i32 %a, 0 |
| 4636 | /// br i1 %0, label %TrueBB, label %bb2 |
| 4637 | /// bb2: |
| 4638 | /// %1 = icmp ne i32 %b, 0 |
| 4639 | /// br i1 %1, label %TrueBB, label %FalseBB |
| 4640 | /// \endcode |
| 4641 | /// This usually allows instruction selection to do even further optimizations |
| 4642 | /// and combine the compare with the branch instruction. Currently this is |
| 4643 | /// applied for targets which have "cheap" jump instructions. |
| 4644 | /// |
| 4645 | /// FIXME: Remove the (equivalent?) implementation in SelectionDAG. |
| 4646 | /// |
| 4647 | bool CodeGenPrepare::splitBranchCondition(Function &F) { |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 4648 | if (!TM || !TM->Options.EnableFastISel || !TLI || TLI->isJumpExpensive()) |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4649 | return false; |
| 4650 | |
| 4651 | bool MadeChange = false; |
| 4652 | for (auto &BB : F) { |
| 4653 | // Does this BB end with the following? |
| 4654 | // %cond1 = icmp|fcmp|binary instruction ... |
| 4655 | // %cond2 = icmp|fcmp|binary instruction ... |
| 4656 | // %cond.or = or|and i1 %cond1, cond2 |
| 4657 | // br i1 %cond.or label %dest1, label %dest2" |
| 4658 | BinaryOperator *LogicOp; |
| 4659 | BasicBlock *TBB, *FBB; |
| 4660 | if (!match(BB.getTerminator(), m_Br(m_OneUse(m_BinOp(LogicOp)), TBB, FBB))) |
| 4661 | continue; |
| 4662 | |
Sanjay Patel | 4257420 | 2015-09-02 19:23:23 +0000 | [diff] [blame] | 4663 | auto *Br1 = cast<BranchInst>(BB.getTerminator()); |
| 4664 | if (Br1->getMetadata(LLVMContext::MD_unpredictable)) |
| 4665 | continue; |
| 4666 | |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4667 | unsigned Opc; |
Juergen Ributzka | 8bda738 | 2014-12-09 17:50:10 +0000 | [diff] [blame] | 4668 | Value *Cond1, *Cond2; |
| 4669 | if (match(LogicOp, m_And(m_OneUse(m_Value(Cond1)), |
| 4670 | m_OneUse(m_Value(Cond2))))) |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4671 | Opc = Instruction::And; |
Juergen Ributzka | 8bda738 | 2014-12-09 17:50:10 +0000 | [diff] [blame] | 4672 | else if (match(LogicOp, m_Or(m_OneUse(m_Value(Cond1)), |
| 4673 | m_OneUse(m_Value(Cond2))))) |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4674 | Opc = Instruction::Or; |
| 4675 | else |
| 4676 | continue; |
| 4677 | |
| 4678 | if (!match(Cond1, m_CombineOr(m_Cmp(), m_BinOp())) || |
| 4679 | !match(Cond2, m_CombineOr(m_Cmp(), m_BinOp())) ) |
| 4680 | continue; |
| 4681 | |
| 4682 | DEBUG(dbgs() << "Before branch condition splitting\n"; BB.dump()); |
| 4683 | |
| 4684 | // Create a new BB. |
| 4685 | auto *InsertBefore = std::next(Function::iterator(BB)) |
| 4686 | .getNodePtrUnchecked(); |
| 4687 | auto TmpBB = BasicBlock::Create(BB.getContext(), |
| 4688 | BB.getName() + ".cond.split", |
| 4689 | BB.getParent(), InsertBefore); |
| 4690 | |
| 4691 | // Update original basic block by using the first condition directly by the |
| 4692 | // branch instruction and removing the no longer needed and/or instruction. |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4693 | Br1->setCondition(Cond1); |
| 4694 | LogicOp->eraseFromParent(); |
Juergen Ributzka | 8bda738 | 2014-12-09 17:50:10 +0000 | [diff] [blame] | 4695 | |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4696 | // Depending on the conditon we have to either replace the true or the false |
| 4697 | // successor of the original branch instruction. |
| 4698 | if (Opc == Instruction::And) |
| 4699 | Br1->setSuccessor(0, TmpBB); |
| 4700 | else |
| 4701 | Br1->setSuccessor(1, TmpBB); |
| 4702 | |
| 4703 | // Fill in the new basic block. |
| 4704 | auto *Br2 = IRBuilder<>(TmpBB).CreateCondBr(Cond2, TBB, FBB); |
Juergen Ributzka | 8bda738 | 2014-12-09 17:50:10 +0000 | [diff] [blame] | 4705 | if (auto *I = dyn_cast<Instruction>(Cond2)) { |
| 4706 | I->removeFromParent(); |
| 4707 | I->insertBefore(Br2); |
| 4708 | } |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4709 | |
| 4710 | // Update PHI nodes in both successors. The original BB needs to be |
| 4711 | // replaced in one succesor's PHI nodes, because the branch comes now from |
| 4712 | // the newly generated BB (NewBB). In the other successor we need to add one |
| 4713 | // incoming edge to the PHI nodes, because both branch instructions target |
| 4714 | // now the same successor. Depending on the original branch condition |
| 4715 | // (and/or) we have to swap the successors (TrueDest, FalseDest), so that |
| 4716 | // we perfrom the correct update for the PHI nodes. |
| 4717 | // This doesn't change the successor order of the just created branch |
| 4718 | // instruction (or any other instruction). |
| 4719 | if (Opc == Instruction::Or) |
| 4720 | std::swap(TBB, FBB); |
| 4721 | |
| 4722 | // Replace the old BB with the new BB. |
| 4723 | for (auto &I : *TBB) { |
| 4724 | PHINode *PN = dyn_cast<PHINode>(&I); |
| 4725 | if (!PN) |
| 4726 | break; |
| 4727 | int i; |
| 4728 | while ((i = PN->getBasicBlockIndex(&BB)) >= 0) |
| 4729 | PN->setIncomingBlock(i, TmpBB); |
| 4730 | } |
| 4731 | |
| 4732 | // Add another incoming edge form the new BB. |
| 4733 | for (auto &I : *FBB) { |
| 4734 | PHINode *PN = dyn_cast<PHINode>(&I); |
| 4735 | if (!PN) |
| 4736 | break; |
| 4737 | auto *Val = PN->getIncomingValueForBlock(&BB); |
| 4738 | PN->addIncoming(Val, TmpBB); |
| 4739 | } |
| 4740 | |
| 4741 | // Update the branch weights (from SelectionDAGBuilder:: |
| 4742 | // FindMergedConditions). |
| 4743 | if (Opc == Instruction::Or) { |
| 4744 | // Codegen X | Y as: |
| 4745 | // BB1: |
| 4746 | // jmp_if_X TBB |
| 4747 | // jmp TmpBB |
| 4748 | // TmpBB: |
| 4749 | // jmp_if_Y TBB |
| 4750 | // jmp FBB |
| 4751 | // |
| 4752 | |
| 4753 | // We have flexibility in setting Prob for BB1 and Prob for NewBB. |
| 4754 | // The requirement is that |
| 4755 | // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB) |
| 4756 | // = TrueProb for orignal BB. |
| 4757 | // Assuming the orignal weights are A and B, one choice is to set BB1's |
| 4758 | // weights to A and A+2B, and set TmpBB's weights to A and 2B. This choice |
| 4759 | // assumes that |
| 4760 | // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB. |
| 4761 | // Another choice is to assume TrueProb for BB1 equals to TrueProb for |
| 4762 | // TmpBB, but the math is more complicated. |
| 4763 | uint64_t TrueWeight, FalseWeight; |
Juergen Ributzka | 194350a | 2014-12-09 17:32:12 +0000 | [diff] [blame] | 4764 | if (extractBranchMetadata(Br1, TrueWeight, FalseWeight)) { |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4765 | uint64_t NewTrueWeight = TrueWeight; |
| 4766 | uint64_t NewFalseWeight = TrueWeight + 2 * FalseWeight; |
| 4767 | scaleWeights(NewTrueWeight, NewFalseWeight); |
| 4768 | Br1->setMetadata(LLVMContext::MD_prof, MDBuilder(Br1->getContext()) |
| 4769 | .createBranchWeights(TrueWeight, FalseWeight)); |
| 4770 | |
| 4771 | NewTrueWeight = TrueWeight; |
| 4772 | NewFalseWeight = 2 * FalseWeight; |
| 4773 | scaleWeights(NewTrueWeight, NewFalseWeight); |
| 4774 | Br2->setMetadata(LLVMContext::MD_prof, MDBuilder(Br2->getContext()) |
| 4775 | .createBranchWeights(TrueWeight, FalseWeight)); |
| 4776 | } |
| 4777 | } else { |
| 4778 | // Codegen X & Y as: |
| 4779 | // BB1: |
| 4780 | // jmp_if_X TmpBB |
| 4781 | // jmp FBB |
| 4782 | // TmpBB: |
| 4783 | // jmp_if_Y TBB |
| 4784 | // jmp FBB |
| 4785 | // |
| 4786 | // This requires creation of TmpBB after CurBB. |
| 4787 | |
| 4788 | // We have flexibility in setting Prob for BB1 and Prob for TmpBB. |
| 4789 | // The requirement is that |
| 4790 | // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB) |
| 4791 | // = FalseProb for orignal BB. |
| 4792 | // Assuming the orignal weights are A and B, one choice is to set BB1's |
| 4793 | // weights to 2A+B and B, and set TmpBB's weights to 2A and B. This choice |
| 4794 | // assumes that |
| 4795 | // FalseProb for BB1 == TrueProb for BB1 * FalseProb for TmpBB. |
| 4796 | uint64_t TrueWeight, FalseWeight; |
Juergen Ributzka | 194350a | 2014-12-09 17:32:12 +0000 | [diff] [blame] | 4797 | if (extractBranchMetadata(Br1, TrueWeight, FalseWeight)) { |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4798 | uint64_t NewTrueWeight = 2 * TrueWeight + FalseWeight; |
| 4799 | uint64_t NewFalseWeight = FalseWeight; |
| 4800 | scaleWeights(NewTrueWeight, NewFalseWeight); |
| 4801 | Br1->setMetadata(LLVMContext::MD_prof, MDBuilder(Br1->getContext()) |
| 4802 | .createBranchWeights(TrueWeight, FalseWeight)); |
| 4803 | |
| 4804 | NewTrueWeight = 2 * TrueWeight; |
| 4805 | NewFalseWeight = FalseWeight; |
| 4806 | scaleWeights(NewTrueWeight, NewFalseWeight); |
| 4807 | Br2->setMetadata(LLVMContext::MD_prof, MDBuilder(Br2->getContext()) |
| 4808 | .createBranchWeights(TrueWeight, FalseWeight)); |
| 4809 | } |
| 4810 | } |
| 4811 | |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4812 | // Note: No point in getting fancy here, since the DT info is never |
Quentin Colombet | 7bdd50d | 2015-03-18 23:17:28 +0000 | [diff] [blame] | 4813 | // available to CodeGenPrepare. |
Juergen Ributzka | c1bbcbb | 2014-12-09 16:36:13 +0000 | [diff] [blame] | 4814 | ModifiedDT = true; |
| 4815 | |
| 4816 | MadeChange = true; |
| 4817 | |
| 4818 | DEBUG(dbgs() << "After branch condition splitting\n"; BB.dump(); |
| 4819 | TmpBB->dump()); |
| 4820 | } |
| 4821 | return MadeChange; |
| 4822 | } |
Piotr Padlewski | 6c15ec4 | 2015-09-15 18:32:14 +0000 | [diff] [blame] | 4823 | |
| 4824 | void CodeGenPrepare::stripInvariantGroupMetadata(Instruction &I) { |
Piotr Padlewski | ea09288 | 2015-09-17 20:25:07 +0000 | [diff] [blame] | 4825 | if (auto *InvariantMD = I.getMetadata(LLVMContext::MD_invariant_group)) |
Piotr Padlewski | 6c15ec4 | 2015-09-15 18:32:14 +0000 | [diff] [blame] | 4826 | I.dropUnknownNonDebugMetadata(InvariantMD->getMetadataID()); |
| 4827 | } |