blob: 000e19c0c0a4eb64d7caa4a6fcc605c9b8f3d7c5 [file] [log] [blame]
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001//===- AggressiveInstCombine.cpp ------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the aggressive expression pattern combiner classes.
11// Currently, it handles expression patterns for:
12// * Truncate instruction
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
17#include "AggressiveInstCombineInternal.h"
George Burgess IV34828712018-07-10 22:48:13 +000018#include "llvm-c/Initialization.h"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000019#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Analysis/BasicAliasAnalysis.h"
21#include "llvm/Analysis/GlobalsModRef.h"
22#include "llvm/Analysis/TargetLibraryInfo.h"
23#include "llvm/IR/DataLayout.h"
Amjad Aboudd895bff2018-01-31 10:41:31 +000024#include "llvm/IR/Dominators.h"
Sanjay Pateld2025a22018-05-01 21:02:09 +000025#include "llvm/IR/IRBuilder.h"
David Blaikieba47dd12018-04-24 15:40:07 +000026#include "llvm/IR/LegacyPassManager.h"
Sanjay Pateld2025a22018-05-01 21:02:09 +000027#include "llvm/IR/PatternMatch.h"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000028#include "llvm/Pass.h"
George Burgess IV34828712018-07-10 22:48:13 +000029#include "llvm/Transforms/Utils/Local.h"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000030using namespace llvm;
Sanjay Pateld2025a22018-05-01 21:02:09 +000031using namespace PatternMatch;
Amjad Aboudf1f57a32018-01-25 12:06:32 +000032
33#define DEBUG_TYPE "aggressive-instcombine"
34
35namespace {
36/// Contains expression pattern combiner logic.
37/// This class provides both the logic to combine expression patterns and
38/// combine them. It differs from InstCombiner class in that each pattern
39/// combiner runs only once as opposed to InstCombine's multi-iteration,
40/// which allows pattern combiner to have higher complexity than the O(1)
41/// required by the instruction combiner.
42class AggressiveInstCombinerLegacyPass : public FunctionPass {
43public:
44 static char ID; // Pass identification, replacement for typeid
45
46 AggressiveInstCombinerLegacyPass() : FunctionPass(ID) {
47 initializeAggressiveInstCombinerLegacyPassPass(
48 *PassRegistry::getPassRegistry());
49 }
50
51 void getAnalysisUsage(AnalysisUsage &AU) const override;
52
53 /// Run all expression pattern optimizations on the given /p F function.
54 ///
55 /// \param F function to optimize.
56 /// \returns true if the IR is changed.
57 bool runOnFunction(Function &F) override;
58};
59} // namespace
60
Sanjay Patelac3951a2018-05-09 23:08:15 +000061/// This is used by foldAnyOrAllBitsSet() to capture a source value (Root) and
62/// the bit indexes (Mask) needed by a masked compare. If we're matching a chain
63/// of 'and' ops, then we also need to capture the fact that we saw an
64/// "and X, 1", so that's an extra return value for that case.
65struct MaskOps {
66 Value *Root;
67 APInt Mask;
68 bool MatchAndChain;
69 bool FoundAnd1;
70
71 MaskOps(unsigned BitWidth, bool MatchAnds) :
72 Root(nullptr), Mask(APInt::getNullValue(BitWidth)),
73 MatchAndChain(MatchAnds), FoundAnd1(false) {}
74};
75
76/// This is a recursive helper for foldAnyOrAllBitsSet() that walks through a
77/// chain of 'and' or 'or' instructions looking for shift ops of a common source
78/// value. Examples:
79/// or (or (or X, (X >> 3)), (X >> 5)), (X >> 8)
80/// returns { X, 0x129 }
81/// and (and (X >> 1), 1), (X >> 4)
82/// returns { X, 0x12 }
83static bool matchAndOrChain(Value *V, MaskOps &MOps) {
Sanjay Pateld2025a22018-05-01 21:02:09 +000084 Value *Op0, *Op1;
Sanjay Patelac3951a2018-05-09 23:08:15 +000085 if (MOps.MatchAndChain) {
86 // Recurse through a chain of 'and' operands. This requires an extra check
87 // vs. the 'or' matcher: we must find an "and X, 1" instruction somewhere
88 // in the chain to know that all of the high bits are cleared.
89 if (match(V, m_And(m_Value(Op0), m_One()))) {
90 MOps.FoundAnd1 = true;
91 return matchAndOrChain(Op0, MOps);
92 }
93 if (match(V, m_And(m_Value(Op0), m_Value(Op1))))
94 return matchAndOrChain(Op0, MOps) && matchAndOrChain(Op1, MOps);
95 } else {
96 // Recurse through a chain of 'or' operands.
97 if (match(V, m_Or(m_Value(Op0), m_Value(Op1))))
98 return matchAndOrChain(Op0, MOps) && matchAndOrChain(Op1, MOps);
99 }
Sanjay Pateld2025a22018-05-01 21:02:09 +0000100
101 // We need a shift-right or a bare value representing a compare of bit 0 of
102 // the original source operand.
103 Value *Candidate;
104 uint64_t BitIndex = 0;
105 if (!match(V, m_LShr(m_Value(Candidate), m_ConstantInt(BitIndex))))
106 Candidate = V;
107
108 // Initialize result source operand.
Sanjay Patelac3951a2018-05-09 23:08:15 +0000109 if (!MOps.Root)
110 MOps.Root = Candidate;
Sanjay Pateld2025a22018-05-01 21:02:09 +0000111
Sanjay Patelbf55e6d2018-05-14 13:43:32 +0000112 // The shift constant is out-of-range? This code hasn't been simplified.
113 if (BitIndex >= MOps.Mask.getBitWidth())
114 return false;
115
Sanjay Pateld2025a22018-05-01 21:02:09 +0000116 // Fill in the mask bit derived from the shift constant.
Sanjay Patelac3951a2018-05-09 23:08:15 +0000117 MOps.Mask.setBit(BitIndex);
118 return MOps.Root == Candidate;
Sanjay Pateld2025a22018-05-01 21:02:09 +0000119}
120
Sanjay Patelac3951a2018-05-09 23:08:15 +0000121/// Match patterns that correspond to "any-bits-set" and "all-bits-set".
122/// These will include a chain of 'or' or 'and'-shifted bits from a
123/// common source value:
124/// and (or (lshr X, C), ...), 1 --> (X & CMask) != 0
125/// and (and (lshr X, C), ...), 1 --> (X & CMask) == CMask
126/// Note: "any-bits-clear" and "all-bits-clear" are variations of these patterns
127/// that differ only with a final 'not' of the result. We expect that final
128/// 'not' to be folded with the compare that we create here (invert predicate).
129static bool foldAnyOrAllBitsSet(Instruction &I) {
130 // The 'any-bits-set' ('or' chain) pattern is simpler to match because the
131 // final "and X, 1" instruction must be the final op in the sequence.
132 bool MatchAllBitsSet;
133 if (match(&I, m_c_And(m_OneUse(m_And(m_Value(), m_Value())), m_Value())))
134 MatchAllBitsSet = true;
135 else if (match(&I, m_And(m_OneUse(m_Or(m_Value(), m_Value())), m_One())))
136 MatchAllBitsSet = false;
137 else
Sanjay Pateld2025a22018-05-01 21:02:09 +0000138 return false;
139
Sanjay Patelac3951a2018-05-09 23:08:15 +0000140 MaskOps MOps(I.getType()->getScalarSizeInBits(), MatchAllBitsSet);
141 if (MatchAllBitsSet) {
142 if (!matchAndOrChain(cast<BinaryOperator>(&I), MOps) || !MOps.FoundAnd1)
143 return false;
144 } else {
145 if (!matchAndOrChain(cast<BinaryOperator>(&I)->getOperand(0), MOps))
146 return false;
147 }
Sanjay Pateld2025a22018-05-01 21:02:09 +0000148
Sanjay Patelac3951a2018-05-09 23:08:15 +0000149 // The pattern was found. Create a masked compare that replaces all of the
150 // shift and logic ops.
Sanjay Pateld2025a22018-05-01 21:02:09 +0000151 IRBuilder<> Builder(&I);
Sanjay Patelac3951a2018-05-09 23:08:15 +0000152 Constant *Mask = ConstantInt::get(I.getType(), MOps.Mask);
153 Value *And = Builder.CreateAnd(MOps.Root, Mask);
154 Value *Cmp = MatchAllBitsSet ? Builder.CreateICmpEQ(And, Mask) :
155 Builder.CreateIsNotNull(And);
156 Value *Zext = Builder.CreateZExt(Cmp, I.getType());
Sanjay Pateld2025a22018-05-01 21:02:09 +0000157 I.replaceAllUsesWith(Zext);
158 return true;
159}
160
161/// This is the entry point for folds that could be implemented in regular
162/// InstCombine, but they are separated because they are not expected to
163/// occur frequently and/or have more than a constant-length pattern match.
164static bool foldUnusualPatterns(Function &F, DominatorTree &DT) {
165 bool MadeChange = false;
166 for (BasicBlock &BB : F) {
167 // Ignore unreachable basic blocks.
168 if (!DT.isReachableFromEntry(&BB))
169 continue;
170 // Do not delete instructions under here and invalidate the iterator.
Sanjay Patelac3951a2018-05-09 23:08:15 +0000171 // Walk the block backwards for efficiency. We're matching a chain of
172 // use->defs, so we're more likely to succeed by starting from the bottom.
173 // Also, we want to avoid matching partial patterns.
174 // TODO: It would be more efficient if we removed dead instructions
175 // iteratively in this loop rather than waiting until the end.
176 for (Instruction &I : make_range(BB.rbegin(), BB.rend()))
177 MadeChange |= foldAnyOrAllBitsSet(I);
Sanjay Pateld2025a22018-05-01 21:02:09 +0000178 }
179
180 // We're done with transforms, so remove dead instructions.
181 if (MadeChange)
182 for (BasicBlock &BB : F)
183 SimplifyInstructionsInBlock(&BB);
184
185 return MadeChange;
186}
187
188/// This is the entry point for all transforms. Pass manager differences are
189/// handled in the callers of this function.
190static bool runImpl(Function &F, TargetLibraryInfo &TLI, DominatorTree &DT) {
191 bool MadeChange = false;
192 const DataLayout &DL = F.getParent()->getDataLayout();
193 TruncInstCombine TIC(TLI, DL, DT);
194 MadeChange |= TIC.run(F);
195 MadeChange |= foldUnusualPatterns(F, DT);
196 return MadeChange;
197}
198
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000199void AggressiveInstCombinerLegacyPass::getAnalysisUsage(
200 AnalysisUsage &AU) const {
201 AU.setPreservesCFG();
Amjad Aboudd895bff2018-01-31 10:41:31 +0000202 AU.addRequired<DominatorTreeWrapperPass>();
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000203 AU.addRequired<TargetLibraryInfoWrapperPass>();
204 AU.addPreserved<AAResultsWrapperPass>();
205 AU.addPreserved<BasicAAWrapperPass>();
Amjad Aboudd895bff2018-01-31 10:41:31 +0000206 AU.addPreserved<DominatorTreeWrapperPass>();
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000207 AU.addPreserved<GlobalsAAWrapperPass>();
208}
209
210bool AggressiveInstCombinerLegacyPass::runOnFunction(Function &F) {
211 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Sanjay Pateld2025a22018-05-01 21:02:09 +0000212 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
213 return runImpl(F, TLI, DT);
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000214}
215
216PreservedAnalyses AggressiveInstCombinePass::run(Function &F,
217 FunctionAnalysisManager &AM) {
218 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
Sanjay Pateld2025a22018-05-01 21:02:09 +0000219 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
220 if (!runImpl(F, TLI, DT)) {
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000221 // No changes, all analyses are preserved.
222 return PreservedAnalyses::all();
Sanjay Pateld2025a22018-05-01 21:02:09 +0000223 }
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000224 // Mark all the analyses that instcombine updates as preserved.
225 PreservedAnalyses PA;
226 PA.preserveSet<CFGAnalyses>();
227 PA.preserve<AAManager>();
228 PA.preserve<GlobalsAA>();
229 return PA;
230}
231
232char AggressiveInstCombinerLegacyPass::ID = 0;
233INITIALIZE_PASS_BEGIN(AggressiveInstCombinerLegacyPass,
234 "aggressive-instcombine",
235 "Combine pattern based expressions", false, false)
Amjad Aboudd895bff2018-01-31 10:41:31 +0000236INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000237INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
238INITIALIZE_PASS_END(AggressiveInstCombinerLegacyPass, "aggressive-instcombine",
239 "Combine pattern based expressions", false, false)
240
Craig Topperd4eb2072018-04-24 00:05:21 +0000241// Initialization Routines
242void llvm::initializeAggressiveInstCombine(PassRegistry &Registry) {
243 initializeAggressiveInstCombinerLegacyPassPass(Registry);
244}
245
Craig Topper1bcb2582018-04-24 00:39:29 +0000246void LLVMInitializeAggressiveInstCombiner(LLVMPassRegistryRef R) {
247 initializeAggressiveInstCombinerLegacyPassPass(*unwrap(R));
248}
249
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000250FunctionPass *llvm::createAggressiveInstCombinerPass() {
251 return new AggressiveInstCombinerLegacyPass();
252}
David Blaikieba47dd12018-04-24 15:40:07 +0000253
254void LLVMAddAggressiveInstCombinerPass(LLVMPassManagerRef PM) {
255 unwrap(PM)->add(createAggressiveInstCombinerPass());
256}