1 //===- 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"
18 #include "llvm-c/Initialization.h"
19 #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"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/LegacyPassManager.h"
27 #include "llvm/IR/PatternMatch.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Transforms/Utils/Local.h"
30 using namespace llvm;
31 using namespace PatternMatch;
32 
33 #define DEBUG_TYPE "aggressive-instcombine"
34 
35 namespace {
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.
42 class AggressiveInstCombinerLegacyPass : public FunctionPass {
43 public:
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 
61 /// 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.
65 struct 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 }
83 static bool matchAndOrChain(Value *V, MaskOps &MOps) {
84   Value *Op0, *Op1;
85   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   }
100 
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.
109   if (!MOps.Root)
110     MOps.Root = Candidate;
111 
112   // The shift constant is out-of-range? This code hasn't been simplified.
113   if (BitIndex >= MOps.Mask.getBitWidth())
114     return false;
115 
116   // Fill in the mask bit derived from the shift constant.
117   MOps.Mask.setBit(BitIndex);
118   return MOps.Root == Candidate;
119 }
120 
121 /// 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).
129 static 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
138     return false;
139 
140   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   }
148 
149   // The pattern was found. Create a masked compare that replaces all of the
150   // shift and logic ops.
151   IRBuilder<> Builder(&I);
152   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());
157   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.
164 static 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.
171     // 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);
178   }
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.
190 static 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 
199 void AggressiveInstCombinerLegacyPass::getAnalysisUsage(
200     AnalysisUsage &AU) const {
201   AU.setPreservesCFG();
202   AU.addRequired<DominatorTreeWrapperPass>();
203   AU.addRequired<TargetLibraryInfoWrapperPass>();
204   AU.addPreserved<AAResultsWrapperPass>();
205   AU.addPreserved<BasicAAWrapperPass>();
206   AU.addPreserved<DominatorTreeWrapperPass>();
207   AU.addPreserved<GlobalsAAWrapperPass>();
208 }
209 
210 bool AggressiveInstCombinerLegacyPass::runOnFunction(Function &F) {
211   auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
212   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
213   return runImpl(F, TLI, DT);
214 }
215 
216 PreservedAnalyses AggressiveInstCombinePass::run(Function &F,
217                                                  FunctionAnalysisManager &AM) {
218   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
219   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
220   if (!runImpl(F, TLI, DT)) {
221     // No changes, all analyses are preserved.
222     return PreservedAnalyses::all();
223   }
224   // 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 
232 char AggressiveInstCombinerLegacyPass::ID = 0;
233 INITIALIZE_PASS_BEGIN(AggressiveInstCombinerLegacyPass,
234                       "aggressive-instcombine",
235                       "Combine pattern based expressions", false, false)
236 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
237 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
238 INITIALIZE_PASS_END(AggressiveInstCombinerLegacyPass, "aggressive-instcombine",
239                     "Combine pattern based expressions", false, false)
240 
241 // Initialization Routines
242 void llvm::initializeAggressiveInstCombine(PassRegistry &Registry) {
243   initializeAggressiveInstCombinerLegacyPassPass(Registry);
244 }
245 
246 void LLVMInitializeAggressiveInstCombiner(LLVMPassRegistryRef R) {
247   initializeAggressiveInstCombinerLegacyPassPass(*unwrap(R));
248 }
249 
250 FunctionPass *llvm::createAggressiveInstCombinerPass() {
251   return new AggressiveInstCombinerLegacyPass();
252 }
253 
254 void LLVMAddAggressiveInstCombinerPass(LLVMPassManagerRef PM) {
255   unwrap(PM)->add(createAggressiveInstCombinerPass());
256 }
257